attach: move file descriptor closing into attach_context_container()

This reduces the possibility of forgetting to close the namespace file descriptors when we change this codepath. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 72a19d2f
...@@ -572,6 +572,8 @@ static void put_attach_context(struct attach_context *ctx) ...@@ -572,6 +572,8 @@ static void put_attach_context(struct attach_context *ctx)
static int attach_context_container(struct attach_context *ctx) static int attach_context_container(struct attach_context *ctx)
{ {
int fret = 0;
for (int i = 0; i < LXC_NS_MAX; i++) { for (int i = 0; i < LXC_NS_MAX; i++) {
int ret; int ret;
...@@ -579,16 +581,19 @@ static int attach_context_container(struct attach_context *ctx) ...@@ -579,16 +581,19 @@ static int attach_context_container(struct attach_context *ctx)
continue; continue;
ret = setns(ctx->ns_fd[i], ns_info[i].clone_flag); ret = setns(ctx->ns_fd[i], ns_info[i].clone_flag);
if (ret < 0) if (ret)
return log_error_errno(-1, errno, return log_error_errno(-errno, errno, "Failed to attach to %s namespace of %d", ns_info[i].proc_name, ctx->init_pid);
"Failed to attach to %s namespace of %d",
ns_info[i].proc_name, ctx->init_pid);
DEBUG("Attached to %s namespace of %d", DEBUG("Attached to %s namespace of %d", ns_info[i].proc_name, ctx->init_pid);
ns_info[i].proc_name, ctx->init_pid);
if (close(ctx->ns_fd[i])) {
fret = -errno;
SYSERROR("Failed to close file descriptor for %s namespace", ns_info[i].proc_name);
}
ctx->ns_fd[i] = -EBADF;
} }
return 0; return fret;
} }
/* /*
...@@ -1436,9 +1441,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, ...@@ -1436,9 +1441,6 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
/* close namespace file descriptors */
close_nsfds(ctx);
/* Attach succeeded, try to cwd. */ /* Attach succeeded, try to cwd. */
if (options->initial_cwd) if (options->initial_cwd)
new_cwd = options->initial_cwd; new_cwd = options->initial_cwd;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment