Unverified Commit c4fb13e9 by Stéphane Graber Committed by GitHub

Merge pull request #2867 from brauner/2019-02-19/cleanup_macros

tree-wide: cleanup macros
parents 8d36ef3b 19a3e906
...@@ -125,14 +125,8 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid) ...@@ -125,14 +125,8 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx) static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx)
{ {
int i; for (int i = 0; i < LXC_NS_MAX; i++) {
__do_close_prot_errno int fd = ctx->ns_fd[i];
for (i = 0; i < LXC_NS_MAX; i++) {
if (ctx->ns_fd[i] < 0)
continue;
close(ctx->ns_fd[i]);
ctx->ns_fd[i] = -EBADF;
} }
} }
...@@ -163,7 +157,8 @@ static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx) ...@@ -163,7 +157,8 @@ static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx)
*/ */
static int in_same_namespace(pid_t pid1, pid_t pid2, const char *ns) static int in_same_namespace(pid_t pid1, pid_t pid2, const char *ns)
{ {
int ns_fd1 = -1, ns_fd2 = -1, ret = -1; __do_close_prot_errno int ns_fd1 = -1, ns_fd2 = -1;
int ret = -1;
int saved_errno; int saved_errno;
struct stat ns_st1, ns_st2; struct stat ns_st1, ns_st2;
...@@ -175,42 +170,27 @@ static int in_same_namespace(pid_t pid1, pid_t pid2, const char *ns) ...@@ -175,42 +170,27 @@ static int in_same_namespace(pid_t pid1, pid_t pid2, const char *ns)
if (errno == ENOENT) if (errno == ENOENT)
return -EINVAL; return -EINVAL;
goto out; return -1;
} }
ns_fd2 = lxc_preserve_ns(pid2, ns); ns_fd2 = lxc_preserve_ns(pid2, ns);
if (ns_fd2 < 0) if (ns_fd2 < 0)
goto out; return -1;
ret = fstat(ns_fd1, &ns_st1); ret = fstat(ns_fd1, &ns_st1);
if (ret < 0) if (ret < 0)
goto out; return -1;
ret = fstat(ns_fd2, &ns_st2); ret = fstat(ns_fd2, &ns_st2);
if (ret < 0) if (ret < 0)
goto out; return -1;
/* processes are in the same namespace */ /* processes are in the same namespace */
if ((ns_st1.st_dev == ns_st2.st_dev ) && (ns_st1.st_ino == ns_st2.st_ino)) { if ((ns_st1.st_dev == ns_st2.st_dev) && (ns_st1.st_ino == ns_st2.st_ino))
ret = -EINVAL; return -EINVAL;
goto out;
}
/* processes are in different namespaces */ /* processes are in different namespaces */
ret = ns_fd2; return move_fd(ns_fd2);
ns_fd2 = -1;
out:
saved_errno = errno;
if (ns_fd1 >= 0)
close(ns_fd1);
if (ns_fd2 >= 0)
close(ns_fd2);
errno = saved_errno;
return ret;
} }
static int lxc_attach_to_ns(pid_t pid, struct lxc_proc_context_info *ctx) static int lxc_attach_to_ns(pid_t pid, struct lxc_proc_context_info *ctx)
...@@ -710,15 +690,8 @@ struct attach_clone_payload { ...@@ -710,15 +690,8 @@ struct attach_clone_payload {
static void lxc_put_attach_clone_payload(struct attach_clone_payload *p) static void lxc_put_attach_clone_payload(struct attach_clone_payload *p)
{ {
if (p->ipc_socket >= 0) { __do_close_prot_errno int ipc_socket = p->ipc_socket;
close(p->ipc_socket); __do_close_prot_errno int terminal_slave_fd = p->terminal_slave_fd;
p->ipc_socket = -EBADF;
}
if (p->terminal_slave_fd >= 0) {
close(p->terminal_slave_fd);
p->terminal_slave_fd = -EBADF;
}
if (p->init_ctx) { if (p->init_ctx) {
lxc_proc_put_context_info(p->init_ctx); lxc_proc_put_context_info(p->init_ctx);
...@@ -1011,38 +984,22 @@ static int lxc_attach_terminal_mainloop_init(struct lxc_terminal *terminal, ...@@ -1011,38 +984,22 @@ static int lxc_attach_terminal_mainloop_init(struct lxc_terminal *terminal,
static inline void lxc_attach_terminal_close_master(struct lxc_terminal *terminal) static inline void lxc_attach_terminal_close_master(struct lxc_terminal *terminal)
{ {
if (terminal->master < 0) close_prot_errno_disarm(terminal->master);
return;
close(terminal->master);
terminal->master = -EBADF;
} }
static inline void lxc_attach_terminal_close_slave(struct lxc_terminal *terminal) static inline void lxc_attach_terminal_close_slave(struct lxc_terminal *terminal)
{ {
if (terminal->slave < 0) close_prot_errno_disarm(terminal->slave);
return;
close(terminal->slave);
terminal->slave = -EBADF;
} }
static inline void lxc_attach_terminal_close_peer(struct lxc_terminal *terminal) static inline void lxc_attach_terminal_close_peer(struct lxc_terminal *terminal)
{ {
if (terminal->peer < 0) close_prot_errno_disarm(terminal->peer);
return;
close(terminal->peer);
terminal->peer = -EBADF;
} }
static inline void lxc_attach_terminal_close_log(struct lxc_terminal *terminal) static inline void lxc_attach_terminal_close_log(struct lxc_terminal *terminal)
{ {
if (terminal->log_fd < 0) close_prot_errno_disarm(terminal->log_fd);
return;
close(terminal->log_fd);
terminal->log_fd = -EBADF;
} }
int lxc_attach(const char *name, const char *lxcpath, int lxc_attach(const char *name, const char *lxcpath,
......
...@@ -82,8 +82,8 @@ int lxc_cmd_sock_rcv_state(int state_client_fd, int timeout) ...@@ -82,8 +82,8 @@ int lxc_cmd_sock_rcv_state(int state_client_fd, int timeout)
int lxc_cmd_sock_get_state(const char *name, const char *lxcpath, int lxc_cmd_sock_get_state(const char *name, const char *lxcpath,
lxc_state_t states[MAX_STATE], int timeout) lxc_state_t states[MAX_STATE], int timeout)
{ {
__do_close_prot_errno int state_client_fd = -EBADF;
int ret; int ret;
int state_client_fd;
ret = lxc_cmd_add_state_client(name, lxcpath, states, &state_client_fd); ret = lxc_cmd_add_state_client(name, lxcpath, states, &state_client_fd);
if (ret < 0) if (ret < 0)
...@@ -92,9 +92,7 @@ int lxc_cmd_sock_get_state(const char *name, const char *lxcpath, ...@@ -92,9 +92,7 @@ int lxc_cmd_sock_get_state(const char *name, const char *lxcpath,
if (ret < MAX_STATE) if (ret < MAX_STATE)
return ret; return ret;
ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout); return lxc_cmd_sock_rcv_state(state_client_fd, timeout);
close(state_client_fd);
return ret;
} }
int lxc_make_abstract_socket_name(char *path, size_t pathlen, int lxc_make_abstract_socket_name(char *path, size_t pathlen,
...@@ -193,9 +191,9 @@ int lxc_cmd_connect(const char *name, const char *lxcpath, ...@@ -193,9 +191,9 @@ int lxc_cmd_connect(const char *name, const char *lxcpath,
int lxc_add_state_client(int state_client_fd, struct lxc_handler *handler, int lxc_add_state_client(int state_client_fd, struct lxc_handler *handler,
lxc_state_t states[MAX_STATE]) lxc_state_t states[MAX_STATE])
{ {
__do_free struct lxc_state_client *newclient = NULL;
__do_free struct lxc_list *tmplist = NULL;
int state; int state;
struct lxc_state_client *newclient;
struct lxc_list *tmplist;
newclient = malloc(sizeof(*newclient)); newclient = malloc(sizeof(*newclient));
if (!newclient) if (!newclient)
...@@ -206,21 +204,19 @@ int lxc_add_state_client(int state_client_fd, struct lxc_handler *handler, ...@@ -206,21 +204,19 @@ int lxc_add_state_client(int state_client_fd, struct lxc_handler *handler,
newclient->clientfd = state_client_fd; newclient->clientfd = state_client_fd;
tmplist = malloc(sizeof(*tmplist)); tmplist = malloc(sizeof(*tmplist));
if (!tmplist) { if (!tmplist)
free(newclient);
return -ENOMEM; return -ENOMEM;
}
state = handler->state; state = handler->state;
if (states[state] != 1) { if (states[state] != 1) {
lxc_list_add_elem(tmplist, newclient); lxc_list_add_elem(tmplist, newclient);
lxc_list_add_tail(&handler->conf->state_clients, tmplist); lxc_list_add_tail(&handler->conf->state_clients, tmplist);
} else { } else {
free(newclient);
free(tmplist);
return state; return state;
} }
TRACE("Added state client %d to state client list", state_client_fd); TRACE("Added state client %d to state client list", state_client_fd);
move_ptr(newclient);
move_ptr(tmplist);
return MAX_STATE; return MAX_STATE;
} }
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