X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fguestfsd.c;h=406c10417493e8d9225ca488ac417a3de029cffd;hp=99055a92e2ad703a78df80fa589b3f0f6d4be4fa;hb=16d6d9851ad33fde675b45c27725ea394b718c7a;hpb=0703248d233744047515418893dac05ce013a642 diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 99055a9..406c104 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -446,6 +446,7 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv) int pid, r, quit, i; fd_set rset, rset2; char buf[256]; + char *p; if (stdoutput) *stdoutput = NULL; if (stderror) *stderror = NULL; @@ -500,6 +501,9 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv) r = select (MAX (so_fd[0], se_fd[0]) + 1, &rset2, NULL, NULL, NULL); if (r == -1) { perror ("select"); + quit: + if (stdoutput) free (*stdoutput); + if (stderror) free (*stderror); close (so_fd[0]); close (se_fd[0]); waitpid (pid, NULL, 0); @@ -510,21 +514,18 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv) r = read (so_fd[0], buf, sizeof buf); if (r == -1) { perror ("read"); - close (so_fd[0]); - close (se_fd[0]); - waitpid (pid, NULL, 0); - return -1; + goto quit; } if (r == 0) { FD_CLR (so_fd[0], &rset); quit++; } if (r > 0 && stdoutput) { so_size += r; - *stdoutput = realloc (*stdoutput, so_size); - if (*stdoutput == NULL) { + p = realloc (*stdoutput, so_size); + if (p == NULL) { perror ("realloc"); - *stdoutput = NULL; - continue; + goto quit; } + *stdoutput = p; memcpy (*stdoutput + so_size - r, buf, r); } } @@ -533,21 +534,18 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv) r = read (se_fd[0], buf, sizeof buf); if (r == -1) { perror ("read"); - close (so_fd[0]); - close (se_fd[0]); - waitpid (pid, NULL, 0); - return -1; + goto quit; } if (r == 0) { FD_CLR (se_fd[0], &rset); quit++; } if (r > 0 && stderror) { se_size += r; - *stderror = realloc (*stderror, se_size); - if (*stderror == NULL) { + p = realloc (*stderror, se_size); + if (p == NULL) { perror ("realloc"); - *stderror = NULL; - continue; + goto quit; } + *stderror = p; memcpy (*stderror + se_size - r, buf, r); } } @@ -589,6 +587,60 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv) return -1; } +/* Split an output string into a NULL-terminated list of lines. + * Typically this is used where we have run an external command + * which has printed out a list of things, and we want to return + * an actual list. + * + * The corner cases here are quite tricky. Note in particular: + * + * "" -> [] + * "\n" -> [""] + * "a\nb" -> ["a"; "b"] + * "a\nb\n" -> ["a"; "b"] + * "a\nb\n\n" -> ["a"; "b"; ""] + * + * The original string is written over and destroyed by this + * function (which is usually OK because it's the 'out' string + * from command()). You can free the original string, because + * add_string() strdups the strings. + */ +char ** +split_lines (char *str) +{ + char **lines = NULL; + int size = 0, alloc = 0; + char *p, *pend; + + if (strcmp (str, "") == 0) + goto empty_list; + + p = str; + while (p) { + /* Empty last line? */ + if (p[0] == '\0') + break; + + pend = strchr (p, '\n'); + if (pend) { + *pend = '\0'; + pend++; + } + + if (add_string (&lines, &size, &alloc, p) == -1) { + return NULL; + } + + p = pend; + } + + empty_list: + if (add_string (&lines, &size, &alloc, NULL) == -1) + return NULL; + + return lines; +} + /* Quote 'in' for the shell, and write max len-1 bytes to out. The * result will be NUL-terminated, even if it is truncated. *