From da1963381b6e630f62b82bc5a3c2c5b7ca4c185a Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 27 Aug 2008 11:42:52 +0000 Subject: [PATCH] Fix QEMU domain creation by allowing virExec to preserve certain FDs --- src/lxc_driver.c | 14 +++++++++++--- src/qemu_driver.c | 11 +++++++++-- src/storage_backend.c | 6 ++++-- src/util.c | 8 ++++++-- src/util.h | 2 ++ 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/lxc_driver.c b/src/lxc_driver.c index 5762281691..8d2e617fc4 100644 --- a/src/lxc_driver.c +++ b/src/lxc_driver.c @@ -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; diff --git a/src/qemu_driver.c b/src/qemu_driver.c index 105c746ee8..83a2eecf76 100644 --- a/src/qemu_driver.c +++ b/src/qemu_driver.c @@ -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]); diff --git a/src/storage_backend.c b/src/storage_backend.c index 72af05cea3..ff6527e32f 100644 --- a/src/storage_backend.c +++ b/src/storage_backend.c @@ -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; } diff --git a/src/util.c b/src/util.c index 8cf18d3d4c..a81af07c85 100644 --- a/src/util.c +++ b/src/util.c @@ -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); diff --git a/src/util.h b/src/util.h index e7dde8ecc1..51515821f9 100644 --- a/src/util.h +++ b/src/util.h @@ -26,6 +26,7 @@ #include "util-lib.h" #include "verify.h" +#include 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, -- 2.39.5