]> xenbits.xensource.com Git - libvirt.git/commitdiff
Fix QEMU domain creation by allowing virExec to preserve certain FDs
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 27 Aug 2008 11:42:52 +0000 (11:42 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 27 Aug 2008 11:42:52 +0000 (11:42 +0000)
src/lxc_driver.c
src/qemu_driver.c
src/storage_backend.c
src/util.c
src/util.h

index 57622816913b6eabdbc8c5572df3a48425ed5a74..8d2e617fc435382dcbe9dfa5899bb8b77a63414e 100644 (file)
@@ -621,6 +621,10 @@ static int lxcControllerStart(virConnectPtr conn,
     const char **largv = NULL;
     pid_t child;
     int status;
+    fd_set keepfd;
+    char appPtyStr[30];
+
+    FD_ZERO(&keepfd);
 
 #define ADD_ARG_SPACE                                                   \
     do { \
@@ -644,11 +648,13 @@ static int lxcControllerStart(virConnectPtr conn,
             goto no_memory;                                             \
     } while (0)
 
+    snprintf(appPtyStr, sizeof(appPtyStr), "%d", appPty);
+
     ADD_ARG_LIT(vm->def->emulator);
     ADD_ARG_LIT("--name");
     ADD_ARG_LIT(vm->def->name);
     ADD_ARG_LIT("--console");
-    ADD_ARG_LIT("0");  /* Passing console master PTY as FD 0 */
+    ADD_ARG_LIT(appPtyStr);
     ADD_ARG_LIT("--background");
 
     for (i = 0 ; i < nveths ; i++) {
@@ -658,10 +664,12 @@ static int lxcControllerStart(virConnectPtr conn,
 
     ADD_ARG(NULL);
 
-    vm->stdin_fd = appPty; /* Passing console master PTY as FD 0 */
+    vm->stdin_fd = -1;
     vm->stdout_fd = vm->stderr_fd = logfd;
 
-    if (virExec(conn, largv, NULL, &child,
+    FD_SET(appPty, &keepfd);
+
+    if (virExec(conn, largv, NULL, &keepfd, &child,
                 vm->stdin_fd, &vm->stdout_fd, &vm->stderr_fd,
                 VIR_EXEC_NONE) < 0)
         goto cleanup;
index 105c746ee8c6d6abe822e1512342cca554f375ce..83a2eecf76dd50a6d38b8bf68814f1e761f89fd9 100644 (file)
@@ -847,6 +847,9 @@ static int qemudStartVMDaemon(virConnectPtr conn,
     int *tapfds = NULL;
     int ntapfds = 0;
     int qemuCmdFlags;
+    fd_set keepfd;
+
+    FD_ZERO(&keepfd);
 
     if (virDomainIsActive(vm)) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
@@ -950,7 +953,10 @@ static int qemudStartVMDaemon(virConnectPtr conn,
     vm->stdout_fd = -1;
     vm->stderr_fd = -1;
 
-    ret = virExec(conn, argv, NULL, &vm->pid,
+    for (i = 0 ; i < ntapfds ; i++)
+        FD_SET(tapfds[i], &keepfd);
+
+    ret = virExec(conn, argv, NULL, &keepfd, &vm->pid,
                   vm->stdin_fd, &vm->stdout_fd, &vm->stderr_fd,
                   VIR_EXEC_NONBLOCK);
     if (ret == 0) {
@@ -1219,7 +1225,8 @@ dhcpStartDhcpDaemon(virConnectPtr conn,
     if (qemudBuildDnsmasqArgv(conn, network, &argv) < 0)
         return -1;
 
-    ret = virExec(conn, argv, NULL, &network->dnsmasqPid, -1, NULL, NULL, VIR_EXEC_NONBLOCK);
+    ret = virExec(conn, argv, NULL, NULL,
+                  &network->dnsmasqPid, -1, NULL, NULL, VIR_EXEC_NONBLOCK);
 
     for (i = 0; argv[i]; i++)
         VIR_FREE(argv[i]);
index 72af05cea36b03fe56f085b4d5b389a9b48cbdbe..ff6527e32f1a543c0c5347595ce684a092a69ed4 100644 (file)
@@ -403,7 +403,8 @@ virStorageBackendRunProgRegex(virConnectPtr conn,
 
 
     /* Run the program and capture its output */
-    if (virExec(conn, prog, NULL, &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
+    if (virExec(conn, prog, NULL, NULL,
+                &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
         goto cleanup;
     }
 
@@ -537,7 +538,8 @@ virStorageBackendRunProgNul(virConnectPtr conn,
         v[i] = NULL;
 
     /* Run the program and capture its output */
-    if (virExec(conn, prog, NULL, &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
+    if (virExec(conn, prog, NULL, NULL,
+                &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
         goto cleanup;
     }
 
index 8cf18d3d4cb07de055a3dd939290d6a053007dae..a81af07c858a3f975d4a3ab96911a936a80a0684 100644 (file)
@@ -132,6 +132,7 @@ int
 virExec(virConnectPtr conn,
         const char *const*argv,
         const char *const*envp,
+        const fd_set *keepfd,
         int *retpid,
         int infd, int *outfd, int *errfd,
         int flags) {
@@ -293,7 +294,9 @@ virExec(virConnectPtr conn,
         if (i != infd &&
             i != null &&
             i != childout &&
-            i != childerr)
+            i != childerr &&
+            (!keepfd ||
+             !FD_ISSET(i, keepfd)))
             close(i);
 
     if (flags & VIR_EXEC_DAEMON) {
@@ -403,7 +406,8 @@ virRun(virConnectPtr conn,
        int *status) {
     int childpid, exitstatus, ret;
 
-    if ((ret = virExec(conn, argv, NULL, &childpid, -1, NULL, NULL, VIR_EXEC_NONE)) < 0)
+    if ((ret = virExec(conn, argv, NULL, NULL,
+                       &childpid, -1, NULL, NULL, VIR_EXEC_NONE)) < 0)
         return ret;
 
     while ((ret = waitpid(childpid, &exitstatus, 0) == -1) && errno == EINTR);
index e7dde8ecc13ec49dc24d45fdea936f0a31124336..51515821f94ea89697195c714dd5797bb55e0990 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "util-lib.h"
 #include "verify.h"
+#include <sys/select.h>
 
 enum {
     VIR_EXEC_NONE   = 0,
@@ -36,6 +37,7 @@ enum {
 int virExec(virConnectPtr conn,
             const char *const*argv,
             const char *const*envp,
+            const fd_set *keepfd,
             int *retpid,
             int infd,
             int *outfd,