From: Christian Brauner Date: Fri, 29 Jan 2021 13:01:59 +0000 (+0100) Subject: sync: make all sync helpers return bool X-Git-Tag: lxc-5.0.0~313^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e48e7c58ec338bd46327d7beb9d45436448ab43;p=thirdparty%2Flxc.git sync: make all sync helpers return bool Signed-off-by: Christian Brauner --- diff --git a/src/lxc/attach.c b/src/lxc/attach.c index e519c8b38..e7f721051 100644 --- a/src/lxc/attach.c +++ b/src/lxc/attach.c @@ -1187,8 +1187,7 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, } /* Wait for the parent to have setup cgroups. */ - ret = sync_wait(ipc_sockets[1], ATTACH_SYNC_CGROUP); - if (ret) { + if (!sync_wait(ipc_sockets[1], ATTACH_SYNC_CGROUP)) { shutdown(ipc_sockets[1], SHUT_RDWR); put_attach_context(ctx); _exit(EXIT_FAILURE); @@ -1333,8 +1332,7 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, } /* Let the child process know to go ahead. */ - ret = sync_wake(ipc_sockets[0], ATTACH_SYNC_CGROUP); - if (ret) + if (!sync_wake(ipc_sockets[0], ATTACH_SYNC_CGROUP)) goto close_mainloop; TRACE("Told intermediate process to start initializing"); diff --git a/src/lxc/start.c b/src/lxc/start.c index 0397ab66b..abc70d28f 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -1069,8 +1069,7 @@ static int do_start(void *data) /* Don't leak the pinfd to the container. */ close_prot_errno_disarm(handler->pinfd); - ret = lxc_sync_wait_parent(handler, START_SYNC_STARTUP); - if (ret < 0) + if (!lxc_sync_wait_parent(handler, START_SYNC_STARTUP)) goto out_warn_father; /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See @@ -1088,8 +1087,7 @@ static int do_start(void *data) /* Tell the parent task it can begin to configure the container and wait * for it to finish. */ - ret = lxc_sync_barrier_parent(handler, START_SYNC_CONFIGURE); - if (ret < 0) + if (!lxc_sync_barrier_parent(handler, START_SYNC_CONFIGURE)) goto out_error; if (handler->ns_clone_flags & CLONE_NEWNET) { @@ -1168,8 +1166,7 @@ static int do_start(void *data) } /* Ask father to setup cgroups and wait for him to finish. */ - ret = lxc_sync_barrier_parent(handler, START_SYNC_CGROUP); - if (ret < 0) + if (!lxc_sync_barrier_parent(handler, START_SYNC_CGROUP)) goto out_error; /* Unshare cgroup namespace after we have setup our cgroups. If we do it @@ -1353,8 +1350,7 @@ static int do_start(void *data) } } - ret = lxc_sync_barrier_parent(handler, START_SYNC_CGROUP_LIMITS); - if (ret < 0) + if (!lxc_sync_barrier_parent(handler, START_SYNC_CGROUP_LIMITS)) goto out_warn_father; /* Reset the environment variables the user requested in a clear @@ -1630,8 +1626,7 @@ static int lxc_spawn(struct lxc_handler *handler) share_ns = true; } - ret = lxc_sync_init(handler); - if (ret < 0) + if (!lxc_sync_init(handler)) return -1; ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, @@ -1790,12 +1785,10 @@ static int lxc_spawn(struct lxc_handler *handler) } } - ret = lxc_sync_wake_child(handler, START_SYNC_STARTUP); - if (ret < 0) + if (!lxc_sync_wake_child(handler, START_SYNC_STARTUP)) goto out_delete_net; - ret = lxc_sync_wait_child(handler, START_SYNC_CONFIGURE); - if (ret < 0) + if (!lxc_sync_wait_child(handler, START_SYNC_CONFIGURE)) goto out_delete_net; if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) { @@ -1864,8 +1857,7 @@ static int lxc_spawn(struct lxc_handler *handler) /* Tell the child to continue its initialization. We'll get * START_SYNC_CGROUP when it is ready for us to setup cgroups. */ - ret = lxc_sync_barrier_child(handler, START_SYNC_POST_CONFIGURE); - if (ret < 0) + if (!lxc_sync_barrier_child(handler, START_SYNC_POST_CONFIGURE)) goto out_delete_net; if (!lxc_list_empty(&conf->limits)) { @@ -1876,8 +1868,7 @@ static int lxc_spawn(struct lxc_handler *handler) } } - ret = lxc_sync_barrier_child(handler, START_SYNC_CGROUP_UNSHARE); - if (ret < 0) + if (!lxc_sync_barrier_child(handler, START_SYNC_CGROUP_UNSHARE)) goto out_delete_net; /* @@ -1941,8 +1932,7 @@ static int lxc_spawn(struct lxc_handler *handler) * lxc_sync_barrier_child to return success, or return a different * value, causing us to error out). */ - ret = lxc_sync_barrier_child(handler, START_SYNC_READY_START); - if (ret < 0) + if (!lxc_sync_barrier_child(handler, START_SYNC_READY_START)) goto out_delete_net; if (handler->ns_clone_flags & CLONE_NEWNET) { diff --git a/src/lxc/sync.c b/src/lxc/sync.c index b9221d8d4..1d018387e 100644 --- a/src/lxc/sync.c +++ b/src/lxc/sync.c @@ -17,44 +17,44 @@ lxc_log_define(sync, lxc); -int sync_wait(int fd, int sequence) +bool sync_wait(int fd, int sequence) { int sync = -1; ssize_t ret; ret = lxc_read_nointr(fd, &sync, sizeof(sync)); if (ret < 0) - return log_error_errno(-1, errno, "Sync wait failure"); + return log_error_errno(false, errno, "Sync wait failure"); if (!ret) - return 0; + return true; if ((size_t)ret != sizeof(sync)) - return log_error(-1, "Unexpected sync size: %zu expected %zu", (size_t)ret, sizeof(sync)); + return log_error(false, "Unexpected sync size: %zu expected %zu", (size_t)ret, sizeof(sync)); if (sync == SYNC_ERROR) - return log_error(-1, "An error occurred in another process (expected sequence number %d)", sequence); + return log_error(false, "An error occurred in another process (expected sequence number %d)", sequence); if (sync != sequence) - return log_error(-1, "Invalid sequence number %d. Expected sequence number %d", sync, sequence); + return log_error(false, "Invalid sequence number %d. Expected sequence number %d", sync, sequence); - return 0; + return true; } -int sync_wake(int fd, int sequence) +bool sync_wake(int fd, int sequence) { int sync = sequence; if (lxc_write_nointr(fd, &sync, sizeof(sync)) < 0) - return log_error_errno(-1, errno, "Sync wake failure"); + return log_error_errno(false, errno, "Sync wake failure"); - return 0; + return true; } -static int __sync_barrier(int fd, int sequence) +static bool __sync_barrier(int fd, int sequence) { - if (sync_wake(fd, sequence)) - return -1; + if (!sync_wake(fd, sequence)) + return false; return sync_wait(fd, sequence + 1); } @@ -87,59 +87,59 @@ static inline const char *start_sync_to_string(int state) } } -int lxc_sync_barrier_parent(struct lxc_handler *handler, int sequence) +bool lxc_sync_barrier_parent(struct lxc_handler *handler, int sequence) { TRACE("Child waking parent with sequence %s and waiting for sequence %s", start_sync_to_string(sequence), start_sync_to_string(sequence + 1)); return __sync_barrier(handler->sync_sock[0], sequence); } -int lxc_sync_barrier_child(struct lxc_handler *handler, int sequence) +bool lxc_sync_barrier_child(struct lxc_handler *handler, int sequence) { TRACE("Parent waking child with sequence %s and waiting with sequence %s", start_sync_to_string(sequence), start_sync_to_string(sequence + 1)); return __sync_barrier(handler->sync_sock[1], sequence); } -int lxc_sync_wake_parent(struct lxc_handler *handler, int sequence) +bool lxc_sync_wake_parent(struct lxc_handler *handler, int sequence) { TRACE("Child waking parent with sequence %s", start_sync_to_string(sequence)); return sync_wake(handler->sync_sock[0], sequence); } -int lxc_sync_wait_parent(struct lxc_handler *handler, int sequence) +bool lxc_sync_wait_parent(struct lxc_handler *handler, int sequence) { TRACE("Parent waiting for child with sequence %s", start_sync_to_string(sequence)); return sync_wait(handler->sync_sock[0], sequence); } -int lxc_sync_wait_child(struct lxc_handler *handler, int sequence) +bool lxc_sync_wait_child(struct lxc_handler *handler, int sequence) { TRACE("Child waiting for parent with sequence %s", start_sync_to_string(sequence)); return sync_wait(handler->sync_sock[1], sequence); } -int lxc_sync_wake_child(struct lxc_handler *handler, int sequence) +bool lxc_sync_wake_child(struct lxc_handler *handler, int sequence) { TRACE("Child waking parent with sequence %s", start_sync_to_string(sequence)); return sync_wake(handler->sync_sock[1], sequence); } -int lxc_sync_init(struct lxc_handler *handler) +bool lxc_sync_init(struct lxc_handler *handler) { int ret; ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sync_sock); if (ret) - return log_error_errno(-1, errno, "failed to create synchronization socketpair"); + return log_error_errno(false, errno, "failed to create synchronization socketpair"); /* Be sure we don't inherit this after the exec */ ret = fcntl(handler->sync_sock[0], F_SETFD, FD_CLOEXEC); if (ret < 0) - return log_error_errno(-1, errno, "Failed to make socket close-on-exec"); + return log_error_errno(false, errno, "Failed to make socket close-on-exec"); TRACE("Initialized synchronization infrastructure"); - return 0; + return true; } void lxc_sync_fini_child(struct lxc_handler *handler) diff --git a/src/lxc/sync.h b/src/lxc/sync.h index f81cb1e90..57191c1cb 100644 --- a/src/lxc/sync.h +++ b/src/lxc/sync.h @@ -3,6 +3,8 @@ #ifndef __LXC_SYNC_H #define __LXC_SYNC_H +#include + #include "compiler.h" struct lxc_handler; @@ -27,17 +29,17 @@ enum /* attach */ { ATTACH_SYNC_CGROUP = 0, }; -__hidden extern int lxc_sync_init(struct lxc_handler *handler); +__hidden extern bool lxc_sync_init(struct lxc_handler *handler); __hidden extern void lxc_sync_fini(struct lxc_handler *); __hidden extern void lxc_sync_fini_parent(struct lxc_handler *); __hidden extern void lxc_sync_fini_child(struct lxc_handler *); -__hidden extern int lxc_sync_wake_child(struct lxc_handler *, int); -__hidden extern int lxc_sync_wait_child(struct lxc_handler *, int); -__hidden extern int lxc_sync_wake_parent(struct lxc_handler *, int); -__hidden extern int lxc_sync_wait_parent(struct lxc_handler *, int); -__hidden extern int lxc_sync_barrier_parent(struct lxc_handler *, int); -__hidden extern int lxc_sync_barrier_child(struct lxc_handler *, int); -__hidden extern int sync_wait(int fd, int sequence); -__hidden extern int sync_wake(int fd, int sequence); +__hidden extern bool lxc_sync_wake_child(struct lxc_handler *, int); +__hidden extern bool lxc_sync_wait_child(struct lxc_handler *, int); +__hidden extern bool lxc_sync_wake_parent(struct lxc_handler *, int); +__hidden extern bool lxc_sync_wait_parent(struct lxc_handler *, int); +__hidden extern bool lxc_sync_barrier_parent(struct lxc_handler *, int); +__hidden extern bool lxc_sync_barrier_child(struct lxc_handler *, int); +__hidden extern bool sync_wait(int fd, int sequence); +__hidden extern bool sync_wake(int fd, int sequence); #endif /* __LXC_SYNC_H */