]> xenbits.xensource.com Git - libvirt.git/commitdiff
Ensure binary is resolved wrt $PATH in virExec
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 15 Mar 2011 16:58:28 +0000 (16:58 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Fri, 18 Mar 2011 16:40:01 +0000 (16:40 +0000)
virExec would only resolved the binary to $PATH if no env
variables were being set. Since there is no execvep() API
in POSIX, we use virFindFileInPath to manually resolve
the binary and then use execv() instead of execvp().

src/util/util.c

index e573f4adf9cec1a3f83620157e354cfc7f97051f..5b5dd5e208605b94db4000b98291c0021a4a5d9a 100644 (file)
@@ -475,6 +475,18 @@ __virExec(const char *const*argv,
     int childout = -1;
     int childerr = -1;
     int tmpfd;
+    const char *binary = NULL;
+
+    if (argv[0][0] != '/') {
+        if (!(binary = virFindFileInPath(argv[0]))) {
+            virReportSystemError(ENOENT,
+                                 _("Cannot find '%s' in path"),
+                                 argv[0]);
+            return -1;
+        }
+    } else {
+        binary = argv[0];
+    }
 
     if ((null = open("/dev/null", O_RDWR)) < 0) {
         virReportSystemError(errno,
@@ -694,9 +706,9 @@ __virExec(const char *const*argv,
     virLogReset();
 
     if (envp)
-        execve(argv[0], (char **) argv, (char**)envp);
+        execve(binary, (char **) argv, (char**)envp);
     else
-        execvp(argv[0], (char **) argv);
+        execv(binary, (char **) argv);
 
     virReportSystemError(errno,
                          _("cannot execute binary %s"),
@@ -710,6 +722,9 @@ __virExec(const char *const*argv,
     /* This is cleanup of parent process only - child
        should never jump here on error */
 
+    if (binary != argv[0])
+        VIR_FREE(binary);
+
     /* NB we don't virUtilError() on any failures here
        because the code which jumped hre already raised
        an error condition which we must not overwrite */