sync: make all sync helpers return bool

parent 2b695e5f
...@@ -1187,8 +1187,7 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, ...@@ -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. */ /* Wait for the parent to have setup cgroups. */
ret = sync_wait(ipc_sockets[1], ATTACH_SYNC_CGROUP); if (!sync_wait(ipc_sockets[1], ATTACH_SYNC_CGROUP)) {
if (ret) {
shutdown(ipc_sockets[1], SHUT_RDWR); shutdown(ipc_sockets[1], SHUT_RDWR);
put_attach_context(ctx); put_attach_context(ctx);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
...@@ -1333,8 +1332,7 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function, ...@@ -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. */ /* Let the child process know to go ahead. */
ret = sync_wake(ipc_sockets[0], ATTACH_SYNC_CGROUP); if (!sync_wake(ipc_sockets[0], ATTACH_SYNC_CGROUP))
if (ret)
goto close_mainloop; goto close_mainloop;
TRACE("Told intermediate process to start initializing"); TRACE("Told intermediate process to start initializing");
......
...@@ -1069,8 +1069,7 @@ static int do_start(void *data) ...@@ -1069,8 +1069,7 @@ static int do_start(void *data)
/* Don't leak the pinfd to the container. */ /* Don't leak the pinfd to the container. */
close_prot_errno_disarm(handler->pinfd); close_prot_errno_disarm(handler->pinfd);
ret = lxc_sync_wait_parent(handler, START_SYNC_STARTUP); if (!lxc_sync_wait_parent(handler, START_SYNC_STARTUP))
if (ret < 0)
goto out_warn_father; goto out_warn_father;
/* Unshare CLONE_NEWNET after CLONE_NEWUSER. See /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
...@@ -1088,8 +1087,7 @@ static int do_start(void *data) ...@@ -1088,8 +1087,7 @@ static int do_start(void *data)
/* Tell the parent task it can begin to configure the container and wait /* Tell the parent task it can begin to configure the container and wait
* for it to finish. * for it to finish.
*/ */
ret = lxc_sync_barrier_parent(handler, START_SYNC_CONFIGURE); if (!lxc_sync_barrier_parent(handler, START_SYNC_CONFIGURE))
if (ret < 0)
goto out_error; goto out_error;
if (handler->ns_clone_flags & CLONE_NEWNET) { if (handler->ns_clone_flags & CLONE_NEWNET) {
...@@ -1168,8 +1166,7 @@ static int do_start(void *data) ...@@ -1168,8 +1166,7 @@ static int do_start(void *data)
} }
/* Ask father to setup cgroups and wait for him to finish. */ /* Ask father to setup cgroups and wait for him to finish. */
ret = lxc_sync_barrier_parent(handler, START_SYNC_CGROUP); if (!lxc_sync_barrier_parent(handler, START_SYNC_CGROUP))
if (ret < 0)
goto out_error; goto out_error;
/* Unshare cgroup namespace after we have setup our cgroups. If we do it /* Unshare cgroup namespace after we have setup our cgroups. If we do it
...@@ -1353,8 +1350,7 @@ static int do_start(void *data) ...@@ -1353,8 +1350,7 @@ static int do_start(void *data)
} }
} }
ret = lxc_sync_barrier_parent(handler, START_SYNC_CGROUP_LIMITS); if (!lxc_sync_barrier_parent(handler, START_SYNC_CGROUP_LIMITS))
if (ret < 0)
goto out_warn_father; goto out_warn_father;
/* Reset the environment variables the user requested in a clear /* Reset the environment variables the user requested in a clear
...@@ -1630,8 +1626,7 @@ static int lxc_spawn(struct lxc_handler *handler) ...@@ -1630,8 +1626,7 @@ static int lxc_spawn(struct lxc_handler *handler)
share_ns = true; share_ns = true;
} }
ret = lxc_sync_init(handler); if (!lxc_sync_init(handler))
if (ret < 0)
return -1; return -1;
ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
...@@ -1790,12 +1785,10 @@ static int lxc_spawn(struct lxc_handler *handler) ...@@ -1790,12 +1785,10 @@ static int lxc_spawn(struct lxc_handler *handler)
} }
} }
ret = lxc_sync_wake_child(handler, START_SYNC_STARTUP); if (!lxc_sync_wake_child(handler, START_SYNC_STARTUP))
if (ret < 0)
goto out_delete_net; goto out_delete_net;
ret = lxc_sync_wait_child(handler, START_SYNC_CONFIGURE); if (!lxc_sync_wait_child(handler, START_SYNC_CONFIGURE))
if (ret < 0)
goto out_delete_net; goto out_delete_net;
if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) { if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) {
...@@ -1864,8 +1857,7 @@ static int lxc_spawn(struct lxc_handler *handler) ...@@ -1864,8 +1857,7 @@ static int lxc_spawn(struct lxc_handler *handler)
/* Tell the child to continue its initialization. We'll get /* Tell the child to continue its initialization. We'll get
* START_SYNC_CGROUP when it is ready for us to setup cgroups. * START_SYNC_CGROUP when it is ready for us to setup cgroups.
*/ */
ret = lxc_sync_barrier_child(handler, START_SYNC_POST_CONFIGURE); if (!lxc_sync_barrier_child(handler, START_SYNC_POST_CONFIGURE))
if (ret < 0)
goto out_delete_net; goto out_delete_net;
if (!lxc_list_empty(&conf->limits)) { if (!lxc_list_empty(&conf->limits)) {
...@@ -1876,8 +1868,7 @@ static int lxc_spawn(struct lxc_handler *handler) ...@@ -1876,8 +1868,7 @@ static int lxc_spawn(struct lxc_handler *handler)
} }
} }
ret = lxc_sync_barrier_child(handler, START_SYNC_CGROUP_UNSHARE); if (!lxc_sync_barrier_child(handler, START_SYNC_CGROUP_UNSHARE))
if (ret < 0)
goto out_delete_net; goto out_delete_net;
/* /*
...@@ -1941,8 +1932,7 @@ static int lxc_spawn(struct lxc_handler *handler) ...@@ -1941,8 +1932,7 @@ static int lxc_spawn(struct lxc_handler *handler)
* lxc_sync_barrier_child to return success, or return a different * lxc_sync_barrier_child to return success, or return a different
* value, causing us to error out). * value, causing us to error out).
*/ */
ret = lxc_sync_barrier_child(handler, START_SYNC_READY_START); if (!lxc_sync_barrier_child(handler, START_SYNC_READY_START))
if (ret < 0)
goto out_delete_net; goto out_delete_net;
if (handler->ns_clone_flags & CLONE_NEWNET) { if (handler->ns_clone_flags & CLONE_NEWNET) {
......
...@@ -17,44 +17,44 @@ ...@@ -17,44 +17,44 @@
lxc_log_define(sync, lxc); lxc_log_define(sync, lxc);
int sync_wait(int fd, int sequence) bool sync_wait(int fd, int sequence)
{ {
int sync = -1; int sync = -1;
ssize_t ret; ssize_t ret;
ret = lxc_read_nointr(fd, &sync, sizeof(sync)); ret = lxc_read_nointr(fd, &sync, sizeof(sync));
if (ret < 0) if (ret < 0)
return log_error_errno(-1, errno, "Sync wait failure"); return log_error_errno(false, errno, "Sync wait failure");
if (!ret) if (!ret)
return 0; return true;
if ((size_t)ret != sizeof(sync)) 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) 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) 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; int sync = sequence;
if (lxc_write_nointr(fd, &sync, sizeof(sync)) < 0) 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)) if (!sync_wake(fd, sequence))
return -1; return false;
return sync_wait(fd, sequence + 1); return sync_wait(fd, sequence + 1);
} }
...@@ -87,59 +87,59 @@ static inline const char *start_sync_to_string(int state) ...@@ -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", TRACE("Child waking parent with sequence %s and waiting for sequence %s",
start_sync_to_string(sequence), start_sync_to_string(sequence + 1)); start_sync_to_string(sequence), start_sync_to_string(sequence + 1));
return __sync_barrier(handler->sync_sock[0], sequence); 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", TRACE("Parent waking child with sequence %s and waiting with sequence %s",
start_sync_to_string(sequence), start_sync_to_string(sequence + 1)); start_sync_to_string(sequence), start_sync_to_string(sequence + 1));
return __sync_barrier(handler->sync_sock[1], sequence); 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)); TRACE("Child waking parent with sequence %s", start_sync_to_string(sequence));
return sync_wake(handler->sync_sock[0], 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)); TRACE("Parent waiting for child with sequence %s", start_sync_to_string(sequence));
return sync_wait(handler->sync_sock[0], 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)); TRACE("Child waiting for parent with sequence %s", start_sync_to_string(sequence));
return sync_wait(handler->sync_sock[1], 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)); TRACE("Child waking parent with sequence %s", start_sync_to_string(sequence));
return sync_wake(handler->sync_sock[1], 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; int ret;
ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sync_sock); ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sync_sock);
if (ret) 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 */ /* Be sure we don't inherit this after the exec */
ret = fcntl(handler->sync_sock[0], F_SETFD, FD_CLOEXEC); ret = fcntl(handler->sync_sock[0], F_SETFD, FD_CLOEXEC);
if (ret < 0) 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"); TRACE("Initialized synchronization infrastructure");
return 0; return true;
} }
void lxc_sync_fini_child(struct lxc_handler *handler) void lxc_sync_fini_child(struct lxc_handler *handler)
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#ifndef __LXC_SYNC_H #ifndef __LXC_SYNC_H
#define __LXC_SYNC_H #define __LXC_SYNC_H
#include <stdbool.h>
#include "compiler.h" #include "compiler.h"
struct lxc_handler; struct lxc_handler;
...@@ -27,17 +29,17 @@ enum /* attach */ { ...@@ -27,17 +29,17 @@ enum /* attach */ {
ATTACH_SYNC_CGROUP = 0, 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(struct lxc_handler *);
__hidden extern void lxc_sync_fini_parent(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 void lxc_sync_fini_child(struct lxc_handler *);
__hidden extern int lxc_sync_wake_child(struct lxc_handler *, int); __hidden extern bool lxc_sync_wake_child(struct lxc_handler *, int);
__hidden extern int lxc_sync_wait_child(struct lxc_handler *, int); __hidden extern bool lxc_sync_wait_child(struct lxc_handler *, int);
__hidden extern int lxc_sync_wake_parent(struct lxc_handler *, int); __hidden extern bool lxc_sync_wake_parent(struct lxc_handler *, int);
__hidden extern int lxc_sync_wait_parent(struct lxc_handler *, int); __hidden extern bool lxc_sync_wait_parent(struct lxc_handler *, int);
__hidden extern int lxc_sync_barrier_parent(struct lxc_handler *, int); __hidden extern bool lxc_sync_barrier_parent(struct lxc_handler *, int);
__hidden extern int lxc_sync_barrier_child(struct lxc_handler *, int); __hidden extern bool lxc_sync_barrier_child(struct lxc_handler *, int);
__hidden extern int sync_wait(int fd, int sequence); __hidden extern bool sync_wait(int fd, int sequence);
__hidden extern int sync_wake(int fd, int sequence); __hidden extern bool sync_wake(int fd, int sequence);
#endif /* __LXC_SYNC_H */ #endif /* __LXC_SYNC_H */
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