]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
attach: do not reload container 3009/head
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 16 May 2019 13:29:41 +0000 (15:29 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 16 May 2019 14:11:22 +0000 (16:11 +0200)
Let lxc_attach() reuse the already initialized container.

Closes https://github.com/lxc/lxd/issues/5755.

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

index ce51352c67df5f08f5f88ea072d77546f6832356..867aa91c0dea7b8f5ddf94d152c60220f1b48f48 100644 (file)
@@ -1008,9 +1008,9 @@ static inline void lxc_attach_terminal_close_log(struct lxc_terminal *terminal)
        close_prot_errno_disarm(terminal->log_fd);
 }
 
-int lxc_attach(const char *name, const char *lxcpath,
-              lxc_attach_exec_t exec_function, void *exec_payload,
-              lxc_attach_options_t *options, pid_t *attached_process)
+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)
 {
        int i, ret, status;
        int ipc_sockets[2];
@@ -1020,6 +1020,7 @@ int lxc_attach(const char *name, const char *lxcpath,
        struct lxc_proc_context_info *init_ctx;
        struct lxc_terminal terminal;
        struct lxc_conf *conf;
+       char *name, *lxcpath;
        struct attach_clone_payload payload = {0};
 
        ret = access("/proc/self/ns", X_OK);
@@ -1028,21 +1029,34 @@ int lxc_attach(const char *name, const char *lxcpath,
                return -1;
        }
 
+       if (!container)
+               return minus_one_set_errno(EINVAL);
+
+       if (!lxc_container_get(container))
+               return minus_one_set_errno(EINVAL);
+
+       name = container->name;
+       lxcpath = container->config_path;
+
        if (!options)
                options = &attach_static_default_options;
 
        init_pid = lxc_cmd_get_init_pid(name, lxcpath);
        if (init_pid < 0) {
                ERROR("Failed to get init pid");
+               lxc_container_put(container);
                return -1;
        }
 
        init_ctx = lxc_proc_get_context_info(init_pid);
        if (!init_ctx) {
                ERROR("Failed to get context of init process: %ld", (long)init_pid);
+               lxc_container_put(container);
                return -1;
        }
 
+       init_ctx->container = container;
+
        personality = get_personality(name, lxcpath);
        if (init_ctx->personality < 0) {
                ERROR("Failed to get personality of the container");
@@ -1051,12 +1065,6 @@ int lxc_attach(const char *name, const char *lxcpath,
        }
        init_ctx->personality = personality;
 
-       init_ctx->container = lxc_container_new(name, lxcpath);
-       if (!init_ctx->container) {
-               lxc_proc_put_context_info(init_ctx);
-               return -1;
-       }
-
        if (!init_ctx->container->lxc_conf) {
                init_ctx->container->lxc_conf = lxc_conf_init();
                if (!init_ctx->container->lxc_conf) {
index 4bf9578ee95aaf3cf9a47c4c08a4c89bd59c8305..c576aa9fcaac9b201da01d02efca9521a5f488b5 100644 (file)
@@ -41,7 +41,7 @@ struct lxc_proc_context_info {
        int ns_fd[LXC_NS_MAX];
 };
 
-extern int lxc_attach(const char *name, const char *lxcpath,
+extern 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);
 
index cea8aa5d7bbb13a6903e869a7e6e6601192721d7..e3262a4611f34bbfd88fd51a96a9e4b1aa15f785 100644 (file)
@@ -4064,7 +4064,7 @@ static int lxcapi_attach(struct lxc_container *c, lxc_attach_exec_t exec_functio
 
        current_config = c->lxc_conf;
 
-       ret = lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process);
+       ret = lxc_attach(c, exec_function, exec_payload, options, attached_process);
        current_config = NULL;
        return ret;
 }
@@ -4081,7 +4081,7 @@ static int do_lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options
        command.program = (char*)program;
        command.argv = (char**)argv;
 
-       r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid);
+       r = lxc_attach(c, lxc_attach_run_command, &command, options, &pid);
        if (r < 0) {
                ERROR("ups");
                return r;