]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
attach: use __do_free cleanup macro for cwd
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 27 Jan 2021 19:28:35 +0000 (20:28 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Wed, 27 Jan 2021 19:52:38 +0000 (20:52 +0100)
but still yield memory immediately once we're done with it to not have it lying
around while the parent process is around.

Cc: stable-4.0
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/attach.c

index f1a59b3306a9444ded4c9aaa82b49ad0e15c7468..b9b8ac9d5438e0fc5e185157b62b525a34d6b807 100644 (file)
@@ -952,9 +952,10 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
               void *exec_payload, lxc_attach_options_t *options,
               pid_t *attached_process)
 {
+       __do_free char *cwd = NULL;
        int i, ret, status;
        int ipc_sockets[2];
-       char *cwd, *new_cwd;
+       char *new_cwd;
        signed long personality;
        pid_t attached_pid, init_pid, pid;
        struct lxc_proc_context_info *init_ctx;
@@ -1033,7 +1034,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
                if (options->namespaces == -1) {
                        ERROR("Failed to automatically determine the "
                              "namespaces which the container uses");
-                       free(cwd);
                        lxc_proc_put_context_info(init_ctx);
                        return -1;
                }
@@ -1083,7 +1083,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
                for (j = 0; j < i; j++)
                        close(init_ctx->ns_fd[j]);
 
-               free(cwd);
                lxc_proc_put_context_info(init_ctx);
                return -1;
        }
@@ -1092,7 +1091,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
                ret = lxc_attach_terminal(name, lxcpath, conf, &terminal);
                if (ret < 0) {
                        ERROR("Failed to setup new terminal");
-                       free(cwd);
                        lxc_proc_put_context_info(init_ctx);
                        return -1;
                }
@@ -1138,7 +1136,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
        ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
        if (ret < 0) {
                SYSERROR("Could not set up required IPC mechanism for attaching");
-               free(cwd);
                lxc_proc_put_context_info(init_ctx);
                return -1;
        }
@@ -1153,7 +1150,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
        pid = fork();
        if (pid < 0) {
                SYSERROR("Failed to create first subprocess");
-               free(cwd);
                lxc_proc_put_context_info(init_ctx);
                return -1;
        }
@@ -1202,7 +1198,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
                        if (ret < 0)
                                WARN("Could not change directory to \"%s\"", new_cwd);
                }
-               free(cwd);
 
                /* Create attached process. */
                payload.ipc_socket = ipc_sockets[1];
@@ -1264,7 +1259,7 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
 
        /* close unneeded file descriptors */
        close(ipc_sockets[1]);
-       free(cwd);
+       free_disarm(cwd);
        lxc_proc_close_ns_fd(init_ctx);
        if (options->attach_flags & LXC_ATTACH_TERMINAL)
                lxc_attach_terminal_close_pts(&terminal);