X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;ds=sidebyside;f=daemon%2Fguestfsd.c;h=eeb84bd1c29f738e4e09e8fddef7fb462b984e39;hb=03ff42cb2e088b00c15023d456058cbcdc5d3c81;hp=d7ba4821263bbbd061f0c49aacdcb5a5483dc826;hpb=11350529fee2dbbcfda333bbe10d72f023dc2109;p=libguestfs.git diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index d7ba482..eeb84bd 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -339,27 +339,85 @@ int command (char **stdoutput, char **stderror, const char *name, ...) { va_list args; - char **argv; + char **argv, **p; char *s; int i, r; /* Collect the command line arguments into an array. */ + i = 2; + argv = malloc (sizeof (char *) * i); + if (argv == NULL) { + perror ("malloc"); + return -1; + } + argv[0] = (char *) name; + argv[1] = NULL; + va_start (args, name); + while ((s = va_arg (args, char *)) != NULL) { + p = realloc (argv, sizeof (char *) * (++i)); + if (p == NULL) { + perror ("realloc"); + free (argv); + va_end (args); + return -1; + } + argv = p; + argv[i-2] = s; + argv[i-1] = NULL; + } + + va_end (args); + + r = commandv (stdoutput, stderror, argv); + + /* NB: Mustn't free the strings which are on the stack. */ + free (argv); + + return r; +} + +/* Same as 'command', but we allow the status code from the + * subcommand to be non-zero, and return that status code. + * We still return -1 if there was some other error. + */ +int +commandr (char **stdoutput, char **stderror, const char *name, ...) +{ + va_list args; + char **argv, **p; + char *s; + int i, r; + + /* Collect the command line arguments into an array. */ i = 2; argv = malloc (sizeof (char *) * i); + if (argv == NULL) { + perror ("malloc"); + return -1; + } argv[0] = (char *) name; argv[1] = NULL; + va_start (args, name); + while ((s = va_arg (args, char *)) != NULL) { - argv = realloc (argv, sizeof (char *) * (++i)); + p = realloc (argv, sizeof (char *) * (++i)); + if (p == NULL) { + perror ("realloc"); + free (argv); + va_end (args); + return -1; + } + argv = p; argv[i-2] = s; argv[i-1] = NULL; } va_end (args); - r = commandv (stdoutput, stderror, argv); + r = commandrv (stdoutput, stderror, argv); /* NB: Mustn't free the strings which are on the stack. */ free (argv); @@ -367,14 +425,28 @@ command (char **stdoutput, char **stderror, const char *name, ...) return r; } +/* Same as 'command', but passing an argv. */ int commandv (char **stdoutput, char **stderror, char * const* const argv) { + int r; + + r = commandrv (stdoutput, stderror, argv); + if (r == 0) + return 0; + else + return -1; +} + +int +commandrv (char **stdoutput, char **stderror, char * const* const argv) +{ int so_size = 0, se_size = 0; int so_fd[2], se_fd[2]; int pid, r, quit, i; fd_set rset, rset2; char buf[256]; + char *p; if (stdoutput) *stdoutput = NULL; if (stderror) *stderror = NULL; @@ -429,6 +501,9 @@ commandv (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); @@ -439,21 +514,18 @@ commandv (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); } } @@ -462,21 +534,18 @@ commandv (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); } } @@ -513,10 +582,7 @@ commandv (char **stdoutput, char **stderror, char * const* const argv) waitpid (pid, &r, 0); if (WIFEXITED (r)) { - if (WEXITSTATUS (r) == 0) - return 0; - else - return -1; + return WEXITSTATUS (r); } else return -1; }