X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fguestfsd.c;h=06ad702e17b1e8007f7cc24e3ec83e562dc9749d;hp=0fc01283901fa84156baa988bb09d67bcaec304e;hb=87e49e7e87e570537f04e34178c9c6d2b876c16c;hpb=5c6147ecc7ee3cf657edb1e19ad5ab4e973424e0 diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 0fc0128..06ad702 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -220,10 +220,14 @@ main (int argc, char *argv[]) /* 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 + * + * NOTE: if you change $PATH, you must also change 'prog_exists' + * function below. */ - setenv ("PATH", "/usr/bin:/bin", 1); + setenv ("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1); setenv ("SHELL", "/bin/sh", 1); setenv ("LC_ALL", "C", 1); + setenv ("TERM", "dumb", 1); #ifndef WIN32 /* We document that umask defaults to 022 (it should be this anyway). */ @@ -928,6 +932,28 @@ split_lines (char *str) return lines; } +/* Skip leading and trailing whitespace, updating the original string + * in-place. + */ +void +trim (char *str) +{ + size_t len = strlen (str); + + while (len > 0 && c_isspace (str[len-1])) { + str[len-1] = '\0'; + len--; + } + + const char *p = str; + while (*p && c_isspace (*p)) { + p++; + len--; + } + + memmove (str, p, len+1); +} + /* printf helper function so we can use %Q ("quoted") and %R to print * shell-quoted strings. See HACKING file for more details. */ @@ -996,42 +1022,55 @@ print_arginfo (const struct printf_info *info, size_t n, int *argtypes) * the device nodes themselves will exist in the appliance. */ int -device_name_translation (char *device, const char *func) +device_name_translation (char *device) { int fd; fd = open (device, O_RDONLY); if (fd >= 0) { + close_ok: close (fd); return 0; } - if (errno != ENXIO && errno != ENOENT) { - error: - reply_with_perror ("%s: %s", func, device); + if (errno != ENXIO && errno != ENOENT) return -1; - } /* If the name begins with "/dev/sd" then try the alternatives. */ if (STRNEQLEN (device, "/dev/sd", 7)) - goto error; + return -1; device[5] = 'h'; /* /dev/hd (old IDE driver) */ fd = open (device, O_RDONLY); - if (fd >= 0) { - close (fd); - return 0; - } + if (fd >= 0) + goto close_ok; device[5] = 'v'; /* /dev/vd (for virtio devices) */ fd = open (device, O_RDONLY); - if (fd >= 0) { - close (fd); - return 0; - } + if (fd >= 0) + goto close_ok; device[5] = 's'; /* Restore original device name. */ - goto error; + return -1; +} + +/* Check program exists and is executable on $PATH. Actually, we + * just assume PATH contains the default entries (see main() above). + */ +int +prog_exists (const char *prog) +{ + static const char * const dirs[] = + { "/sbin", "/usr/sbin", "/bin", "/usr/bin" }; + size_t i; + char buf[1024]; + + for (i = 0; i < sizeof dirs / sizeof dirs[0]; ++i) { + snprintf (buf, sizeof buf, "%s/%s", dirs[i], prog); + if (access (buf, X_OK) == 0) + return 1; + } + return 0; } /* LVM and other commands aren't synchronous, especially when udev is @@ -1051,6 +1090,6 @@ device_name_translation (char *device, const char *func) void udev_settle (void) { - (void) command (NULL, NULL, "/sbin/udevadm", "settle", NULL); - (void) command (NULL, NULL, "/sbin/udevsettle", NULL); + (void) command (NULL, NULL, "udevadm", "settle", NULL); + (void) command (NULL, NULL, "udevsettle", NULL); }