[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH 4/4] lxc: store tty pid



er, this time with the patch....

Dave Leskovec wrote:
> This patch will use a file in the lxc configuration directory to store the tty
> forwarding process pid.  The pid is stored after the process is fork()'d.  It's
> loaded during startup when the config for a running container is loaded.  The
> file is deleted when the domain is undefined.  This should avoid "losing" the
> tty pid over a libvirtd restart.
> 

-- 
Best Regards,
Dave Leskovec
IBM Linux Technology Center
Open Virtualization
---
 src/lxc_conf.c   |  157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lxc_conf.h   |    5 +
 src/lxc_driver.c |    6 ++
 3 files changed, 168 insertions(+)

Index: b/src/lxc_conf.h
===================================================================
--- a/src/lxc_conf.h	2008-05-29 14:34:48.000000000 -0700
+++ b/src/lxc_conf.h	2008-05-29 14:34:51.000000000 -0700
@@ -71,6 +71,8 @@
     char configFile[PATH_MAX];
     char configFileBase[PATH_MAX];
 
+    char ttyPidFile[PATH_MAX];
+
     int parentTty;
     int containerTtyFd;
     char *containerTty;
@@ -134,6 +136,9 @@
                     lxc_driver_t *driver,
                     const char *configFile,
                     const char *name);
+int lxcStoreTtyPid(lxc_driver_t *driver, lxc_vm_t *vm);
+int lxcLoadTtyPid(lxc_driver_t *driver, lxc_vm_t *vm);
+int lxcDeleteTtyPid(lxc_vm_t *vm);
 
 void lxcError(virConnectPtr conn,
               virDomainPtr dom,
Index: b/src/lxc_conf.c
===================================================================
--- a/src/lxc_conf.c	2008-05-29 14:34:48.000000000 -0700
+++ b/src/lxc_conf.c	2008-05-29 14:34:51.000000000 -0700
@@ -650,6 +650,10 @@
     strncpy(vm->configFileBase, file, PATH_MAX);
     vm->configFile[PATH_MAX-1] = '\0';
 
+    if (lxcLoadTtyPid(driver, vm) < 0) {
+        DEBUG0("failed to load tty pid");
+    }
+
     return vm;
 }
 
@@ -883,4 +887,157 @@
     return 0;
 }
 
+/**
+ * lxcStoreTtyPid:
+ * @driver: pointer to driver
+ * @vm: Ptr to VM
+ *
+ * Stores the pid of the tty forward process contained in vm->pid
+ * SYSCONFIG_DIR/libvirt/lxc/{container_name}.pid
+ *
+ * Returns 0 on success or -1 in case of error
+ */
+int lxcStoreTtyPid(lxc_driver_t *driver, lxc_vm_t *vm)
+{
+    int rc = -1;
+    int fd = -1;
+    FILE *file = NULL;
+
+    if (vm->ttyPidFile[0] == 0x00) {
+        if (virFileBuildPath(driver->configDir, vm->def->name, ".pid",
+                             vm->ttyPidFile, PATH_MAX) < 0) {
+            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                     _("cannot construct tty pid file path"));
+            goto error_out;
+        }
+    }
+
+    if ((fd = open(vm->ttyPidFile,
+                   O_WRONLY | O_CREAT | O_TRUNC,
+                   S_IRUSR | S_IWUSR)) < 0) {
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("cannot create tty pid file %s: %s"),
+                 vm->ttyPidFile, strerror(errno));
+        goto error_out;
+    }
+
+    if (!(file = fdopen(fd, "w"))) {
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("cannot fdopen tty pid file %s: %s"),
+                 vm->ttyPidFile, strerror(errno));
+
+        if (close(fd) < 0) {
+            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                     _("failed to close tty pid file %s: %s"),
+                     vm->ttyPidFile, strerror(errno));
+        }
+
+        goto error_out;
+    }
+
+    if (fprintf(file, "%d", vm->pid) < 0) {
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("cannot write tty pid file %s: %s"),
+                 vm->ttyPidFile, strerror(errno));
+
+        goto fclose_error_out;
+    }
+
+    rc = 0;
+
+fclose_error_out:
+    if (fclose(file) < 0) {
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("failed to close tty pid file %s: %s"),
+                 vm->ttyPidFile, strerror(errno));
+    }
+
+error_out:
+    return rc;
+}
+
+/**
+ * lxcLoadTtyPid:
+ * @driver: pointer to driver
+ * @vm: Ptr to VM
+ *
+ * Loads the pid of the tty forward process from the pid file.
+ * SYSCONFIG_DIR/libvirt/lxc/{container_name}.pid
+ *
+ * Returns
+ * > 0 - pid of tty process
+ *   0 - no tty pid file
+ *  -1 - error
+ */
+int lxcLoadTtyPid(lxc_driver_t *driver, lxc_vm_t *vm)
+{
+    int rc = -1;
+    FILE *file;
+
+    if (vm->ttyPidFile[0] == 0x00) {
+        if (virFileBuildPath(driver->configDir, vm->def->name, ".pid",
+                             vm->ttyPidFile, PATH_MAX) < 0) {
+            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                     _("cannot construct tty pid file path"));
+            goto cleanup;
+        }
+    }
+
+    if (!(file = fopen(vm->ttyPidFile, "r"))) {
+        if (ENOENT == errno) {
+            rc = 0;
+            goto cleanup;
+        }
+
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("cannot open tty pid file %s: %s"),
+                 vm->ttyPidFile, strerror(errno));
+        goto cleanup;
+    }
+
+    if (fscanf(file, "%d", &(vm->pid)) < 0) {
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("cannot read tty pid file %s: %s"),
+                 vm->ttyPidFile, strerror(errno));
+        goto cleanup;
+    }
+
+    if (fclose(file) < 0) {
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("failed to close tty pid file %s: %s"),
+                 vm->ttyPidFile, strerror(errno));
+        goto cleanup;
+    }
+
+    rc = vm->pid;
+
+ cleanup:
+    return rc;
+}
+
+/**
+ * lxcDeleteTtyPid:
+ * @vm: Ptr to VM
+ *
+ * Unlinks the tty pid file for the vm
+ * SYSCONFIG_DIR/libvirt/lxc/{container_name}.pid
+ *
+ * Returns on 0 success or -1 in case of error
+ */
+int lxcDeleteTtyPid(lxc_vm_t *vm)
+{
+    if (vm->ttyPidFile[0] == 0x00) {
+        goto no_file;
+    }
+
+    if (unlink(vm->ttyPidFile) < 0) {
+        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("cannot remove ttyPidFile %s"), vm->ttyPidFile);
+        return -1;
+    }
+
+no_file:
+    return 0;
+}
+
 #endif /* WITH_LXC */
Index: b/src/lxc_driver.c
===================================================================
--- a/src/lxc_driver.c	2008-05-29 14:34:45.000000000 -0700
+++ b/src/lxc_driver.c	2008-05-29 14:34:51.000000000 -0700
@@ -328,6 +328,8 @@
 
     vm->configFile[0] = '\0';
 
+    lxcDeleteTtyPid(vm);
+
     lxcRemoveInactiveVM(driver, vm);
 
     return 0;
@@ -798,6 +800,10 @@
         lxcTtyForward(vm->parentTty, vm->containerTtyFd);
     }
 
+    if (lxcStoreTtyPid(driver, vm)) {
+        DEBUG0("unable to store tty pid");
+    }
+
     close(vm->parentTty);
     close(vm->containerTtyFd);
 

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]