X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fguestfsd.c;h=87065b9de30e666555acb72cf06da5bb93e84025;hb=7428b0a70caed014d0cb4126fe8a77432d8957c6;hp=dc839ceac87e7e873669cc73767dfee7a17daf58;hpb=1e97e406c36031617a86a4fa6bb78a112848ee87;p=libguestfs.git diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index dc839ce..87065b9 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -159,6 +160,9 @@ main (int argc, char *argv[]) setenv ("SHELL", "/bin/sh", 1); setenv ("LANG", "C", 1); + /* We document that umask defaults to 022 (it should be this anyway). */ + umask (022); + /* Resolve the hostname. */ memset (&hints, 0, sizeof hints); hints.ai_socktype = SOCK_STREAM; @@ -452,7 +456,8 @@ 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; + pid_t pid; + int r, quit, i; fd_set rset, rset2; char buf[256]; char *p; @@ -567,19 +572,23 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv) * trailing \n characters from the error buffer (not from stdout). */ if (stdoutput) { - *stdoutput = realloc (*stdoutput, so_size+1); - if (*stdoutput == NULL) { + void *q = realloc (*stdoutput, so_size+1); + if (q == NULL) { perror ("realloc"); - *stdoutput = NULL; - } else + free (*stdoutput); + } + *stdoutput = q; + if (*stdoutput) (*stdoutput)[so_size] = '\0'; } if (stderror) { - *stderror = realloc (*stderror, se_size+1); - if (*stderror == NULL) { + void *q = realloc (*stderror, se_size+1); + if (q == NULL) { perror ("realloc"); - *stderror = NULL; - } else { + free (*stderror); + } + *stderror = q; + if (*stderror) { (*stderror)[se_size] = '\0'; se_size--; while (se_size >= 0 && (*stderror)[se_size] == '\n') @@ -588,7 +597,10 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv) } /* Get the exit status of the command. */ - waitpid (pid, &r, 0); + if (waitpid (pid, &r, 0) != pid) { + perror ("waitpid"); + return -1; + } if (WIFEXITED (r)) { return WEXITSTATUS (r); @@ -691,30 +703,58 @@ shell_quote (char *out, int len, const char *in) * use the IS_DEVICE macro. * * See guestfs(3) for the algorithm. + * + * We have to open the device and test for ENXIO, because + * the device nodes themselves will exist in the appliance. */ int device_name_translation (char *device, const char *func) { - struct stat statbuf; + int fd; - if (stat (device, &statbuf) == -1) { - /* If the name begins with "/dev/sd" then try the alternatives. */ - if (strncmp (device, "/dev/sd", 7) != 0) - goto error; + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } - device[5] = 'h'; /* /dev/hd (old IDE driver) */ - if (stat (device, &statbuf) == 0) - return 0; + if (errno != ENXIO && errno != ENOENT) { + error: + reply_with_perror ("%s: %s", func, device); + return -1; + } - device[5] = 'v'; /* /dev/vd (for virtio devices) */ - if (stat (device, &statbuf) == 0) - return 0; + /* If the name begins with "/dev/sd" then try the alternatives. */ + if (strncmp (device, "/dev/sd", 7) != 0) + goto error; - device[5] = 's'; /* Restore original device name. */ + device[5] = 'h'; /* /dev/hd (old IDE driver) */ + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } - error: - reply_with_perror ("%s: %s", func, device); - return -1; + device[5] = 'v'; /* /dev/vd (for virtio devices) */ + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; } - return 0; + + device[5] = 's'; /* Restore original device name. */ + goto error; +} + +/* LVM and other commands aren't synchronous, especially when udev is + * involved. eg. You can create or remove some device, but the /dev + * device node won't appear until some time later. This means that + * you get an error if you run one command followed by another. + * Use 'udevadm settle' after certain commands, but don't be too + * fussed if it fails. + */ +void +udev_settle (void) +{ + command (NULL, NULL, "/sbin/udevadm", "settle", NULL); }