X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fguestfsd.c;h=7eabbd4710c0f7d5f26879d3475ff93aa440d68a;hp=406c10417493e8d9225ca488ac417a3de029cffd;hb=0884d8bbae6d76a603ec1385ada2938f88981c5c;hpb=b2a5fec5f8b8b6bf1313d8474448cd8b50057d1b diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 406c104..7eabbd4 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include @@ -150,6 +152,17 @@ main (int argc, char *argv[]) if (sigaction (SIGPIPE, &sa, NULL) == -1) perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */ + /* Set up a basic environment. After we are called by /init the + * environment is essentially empty. + * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5 + */ + setenv ("PATH", "/usr/bin:/bin", 1); + 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; @@ -443,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; @@ -579,7 +593,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); @@ -677,3 +694,50 @@ shell_quote (char *out, int len, const char *in) return outlen; } + +/* Perform device name translation. Don't call this directly - + * 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) +{ + int fd; + + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } + + if (errno != ENXIO) { + error: + reply_with_perror ("%s: %s", func, device); + return -1; + } + + /* If the name begins with "/dev/sd" then try the alternatives. */ + if (strncmp (device, "/dev/sd", 7) != 0) + goto error; + + device[5] = 'h'; /* /dev/hd (old IDE driver) */ + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } + + device[5] = 'v'; /* /dev/vd (for virtio devices) */ + fd = open (device, O_RDONLY); + if (fd >= 0) { + close (fd); + return 0; + } + + device[5] = 's'; /* Restore original device name. */ + goto error; +}