]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
sync: make all sync helpers return bool
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 29 Jan 2021 13:01:59 +0000 (14:01 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 29 Jan 2021 13:01:59 +0000 (14:01 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/attach.c
src/lxc/start.c
src/lxc/sync.c
src/lxc/sync.h

index e519c8b38e25b4f7c24318185e79ea6d4f1178b2..e7f7210511ae88e20dbe7b611576edeacec8e6bb 100644 (file)
@@ -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");
index 0397ab66ba8b0c74828f67f5aca25db597ba1e29..abc70d28faeeeaff30a893286c410cf665b23150 100644 (file)
@@ -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) {
index b9221d8d4df8258d7029ceb3a7c29f2033d8831e..1d018387ec0827a3f4e17264899cd46199fc08dd 100644 (file)
 
 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)
index f81cb1e904715d6db94e99cd195641ebfffbccf8..57191c1cbba2dcc26ab9e024f64cbf0a46a8365c 100644 (file)
@@ -3,6 +3,8 @@
 #ifndef __LXC_SYNC_H
 #define __LXC_SYNC_H
 
+#include <stdbool.h>
+
 #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 */