]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/lxccontainer.c
pass on reboot flag and delete old veth on reboot
[mirror_lxc.git] / src / lxc / lxccontainer.c
index 8999f44238619eb3ca863e2e56dda2e17c1a634a..223e78e096bd4813cdd13889e8aae9ea48a1cce7 100644 (file)
@@ -712,16 +712,20 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
                pid = fork();
                if (pid < 0) {
                        SYSERROR("Error doing dual-fork");
-                       return false;
+                       exit(1);
                }
                if (pid != 0)
                        exit(0);
                /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
                if (chdir("/")) {
                        SYSERROR("Error chdir()ing to /.");
-                       return false;
+                       exit(1);
                }
                lxc_check_inherited(conf, true, -1);
+               if (null_stdfds() < 0) {
+                       ERROR("failed to close fds");
+                       exit(1);
+               }
                setsid();
        } else {
                if (!am_single_threaded()) {
@@ -738,6 +742,8 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
                if (pid_fp == NULL) {
                        SYSERROR("Failed to create pidfile '%s' for '%s'",
                                 c->pidfile, c->name);
+                       if (daemonize)
+                               exit(1);
                        return false;
                }
 
@@ -745,6 +751,8 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
                        SYSERROR("Failed to write '%s'", c->pidfile);
                        fclose(pid_fp);
                        pid_fp = NULL;
+                       if (daemonize)
+                               exit(1);
                        return false;
                }
 
@@ -752,9 +760,9 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
                pid_fp = NULL;
        }
 
-reboot:
        conf->reboot = 0;
 
+reboot:
        if (lxc_check_inherited(conf, daemonize, -1)) {
                ERROR("Inherited fds found");
                ret = 1;
@@ -764,9 +772,9 @@ reboot:
        ret = lxc_start(c->name, argv, conf, c->config_path, daemonize);
        c->error_num = ret;
 
-       if (conf->reboot) {
+       if (conf->reboot == 1) {
                INFO("container requested reboot");
-               conf->reboot = 0;
+               conf->reboot = 2;
                goto reboot;
        }
 
@@ -950,7 +958,7 @@ static char *lxcbasename(char *path)
        return p;
 }
 
-static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
+static bool create_run_template(struct lxc_container *c, char *tpath, bool need_null_stdfds,
                                char *const argv[])
 {
        pid_t pid;
@@ -972,13 +980,8 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
                char **newargv;
                struct lxc_conf *conf = c->lxc_conf;
 
-               if (quiet) {
-                       close(0);
-                       close(1);
-                       close(2);
-                       open("/dev/zero", O_RDONLY);
-                       open("/dev/null", O_RDWR);
-                       open("/dev/null", O_RDWR);
+               if (need_null_stdfds && null_stdfds() < 0) {
+                       exit(1);
                }
 
                src = c->lxc_conf->rootfs.path;
@@ -2154,10 +2157,12 @@ static bool container_destroy(struct lxc_container *c)
 {
        bool bret = false;
        int ret;
+       struct lxc_conf *conf;
 
        if (!c || !do_lxcapi_is_defined(c))
                return false;
 
+       conf = c->lxc_conf;
        if (container_disk_lock(c))
                return false;
 
@@ -2167,19 +2172,47 @@ static bool container_destroy(struct lxc_container *c)
                goto out;
        }
 
-       if (current_config && c->lxc_conf == current_config) {
+       if (conf && !lxc_list_empty(&conf->hooks[LXCHOOK_DESTROY])) {
+               /* Start of environment variable setup for hooks */
+               if (setenv("LXC_NAME", c->name, 1)) {
+                       SYSERROR("failed to set environment variable for container name");
+               }
+               if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
+                       SYSERROR("failed to set environment variable for config path");
+               }
+               if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
+                       SYSERROR("failed to set environment variable for rootfs mount");
+               }
+               if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
+                       SYSERROR("failed to set environment variable for rootfs mount");
+               }
+               if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) {
+                       SYSERROR("failed to set environment variable for console path");
+               }
+               if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) {
+                       SYSERROR("failed to set environment variable for console log");
+               }
+               /* End of environment variable setup for hooks */
+
+               if (run_lxc_hooks(c->name, "destroy", conf, c->get_config_path(c), NULL)) {
+                       ERROR("Error executing clone hook for %s", c->name);
+                       goto out;
+               }
+       }
+
+       if (current_config && conf == current_config) {
                current_config = NULL;
-               if (c->lxc_conf->logfd != -1) {
-                       close(c->lxc_conf->logfd);
-                       c->lxc_conf->logfd = -1;
+               if (conf->logfd != -1) {
+                       close(conf->logfd);
+                       conf->logfd = -1;
                }
        }
 
-       if (c->lxc_conf && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount) {
+       if (conf && conf->rootfs.path && conf->rootfs.mount) {
                if (am_unpriv())
-                       ret = userns_exec_1(c->lxc_conf, bdev_destroy_wrapper, c->lxc_conf);
+                       ret = userns_exec_1(conf, bdev_destroy_wrapper, conf);
                else
-                       ret = do_bdev_destroy(c->lxc_conf);
+                       ret = do_bdev_destroy(conf);
                if (ret < 0) {
                        ERROR("Error destroying rootfs for %s", c->name);
                        goto out;
@@ -2192,7 +2225,7 @@ static bool container_destroy(struct lxc_container *c)
        char *path = alloca(strlen(p1) + strlen(c->name) + 2);
        sprintf(path, "%s/%s", p1, c->name);
        if (am_unpriv())
-               ret = userns_exec_1(c->lxc_conf, lxc_rmdir_onedev_wrapper, path);
+               ret = userns_exec_1(conf, lxc_rmdir_onedev_wrapper, path);
        else
                ret = lxc_rmdir_onedev(path, "snaps");
        if (ret < 0) {