]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
{start,lxccontainer}: add lxc_free_handler()
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 11 Jun 2017 03:44:52 +0000 (05:44 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 13 Jun 2017 04:13:20 +0000 (06:13 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/lxccontainer.c
src/lxc/start.c
src/lxc/start.h

index 1bed3833d6d83eb3448ea2a58e5cbb9620148800..ad35605a46ea735ff1470a099b36b8926f724923 100644 (file)
@@ -792,8 +792,10 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
                char title[2048];
 
                pid_t pid = fork();
-               if (pid < 0)
+               if (pid < 0) {
+                       lxc_free_handler(handler);
                        return false;
+               }
 
                if (pid != 0) {
                        /* Set to NULL because we don't want father unlink
@@ -834,6 +836,7 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
        } else {
                if (!am_single_threaded()) {
                        ERROR("Cannot start non-daemonized container when threaded");
+                       lxc_free_handler(handler);
                        return false;
                }
        }
@@ -846,6 +849,7 @@ 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);
+                       lxc_free_handler(handler);
                        if (daemonize)
                                exit(1);
                        return false;
@@ -855,6 +859,7 @@ 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;
+                       lxc_free_handler(handler);
                        if (daemonize)
                                exit(1);
                        return false;
@@ -870,10 +875,12 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
        if (conf->monitor_unshare) {
                if (unshare(CLONE_NEWNS)) {
                        SYSERROR("failed to unshare mount namespace");
+                       lxc_free_handler(handler);
                        return false;
                }
                if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
                        SYSERROR("Failed to make / rslave at startup");
+                       lxc_free_handler(handler);
                        return false;
                }
        }
@@ -888,6 +895,7 @@ reboot:
 
        if (lxc_check_inherited(conf, daemonize, handler->conf->maincmd_fd)) {
                ERROR("Inherited fds found");
+               lxc_free_handler(handler);
                ret = 1;
                goto out;
        }
index 0b5c85b1af4567825cd03b1c74c8c0a49cff991f..83c8151447b1371b8ab2da89648ada4c0281fedf 100644 (file)
@@ -451,6 +451,18 @@ out_sigfd:
        return -1;
 }
 
+void lxc_free_handler(struct lxc_handler *handler)
+{
+       if (handler->conf && handler->conf->maincmd_fd)
+               close(handler->conf->maincmd_fd);
+
+       if (handler->name)
+               free(handler->name);
+
+       handler->conf = NULL;
+       free(handler);
+}
+
 struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf,
                                     const char *lxcpath)
 {
@@ -477,12 +489,12 @@ struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf,
        handler->name = strdup(name);
        if (!handler->name) {
                ERROR("failed to allocate memory");
-               goto do_partial_cleanup;
+               goto on_error;
        }
 
        if (lxc_cmd_init(name, handler, lxcpath)) {
                ERROR("failed to set up command socket");
-               goto do_full_cleanup;
+               goto on_error;
        }
 
        TRACE("unix domain socket %d for command server is ready",
@@ -490,11 +502,8 @@ struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf,
 
        return handler;
 
-do_full_cleanup:
-       free(handler->name);
-
-do_partial_cleanup:
-       free(handler);
+on_error:
+       lxc_free_handler(handler);
 
        return NULL;
 }
index aa723537dbc4c28432bec0e195b187b6a1ebabfb..103f15b67c74b24e57b61ce614e9d3a0c5482908 100644 (file)
@@ -69,6 +69,7 @@ extern void lxc_abort(const char *name, struct lxc_handler *handler);
 extern struct lxc_handler *lxc_init_handler(const char *name,
                                            struct lxc_conf *conf,
                                            const char *lxcpath);
+extern void lxc_free_handler(struct lxc_handler *handler);
 extern int lxc_init(const char *name, struct lxc_handler *handler);
 extern void lxc_fini(const char *name, struct lxc_handler *handler);