]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: move file descriptor synchronization with child into single function
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 21 May 2021 08:06:27 +0000 (10:06 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 21 May 2021 15:27:58 +0000 (17:27 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c
src/lxc/conf.h
src/lxc/start.c

index 9e3d79a0d91956c391d00b4a44aa2560b9f75fa6..0fc9ddd3cd846837b4f939320944c9c77560ebb7 100644 (file)
@@ -1635,7 +1635,7 @@ static const struct id_map *find_mapped_nsid_entry(const struct lxc_conf *conf,
        return retmap;
 }
 
-int lxc_setup_devpts_parent(struct lxc_handler *handler)
+static int lxc_setup_devpts_parent(struct lxc_handler *handler)
 {
        int ret;
 
@@ -4013,6 +4013,71 @@ int lxc_idmapped_mounts_parent(struct lxc_handler *handler)
        }
 }
 
+static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
+{
+       int i;
+       struct lxc_terminal_info *tty;
+       int ret = -1;
+       int sock = handler->data_sock[1];
+       struct lxc_conf *conf = handler->conf;
+       struct lxc_tty_info *ttys = &conf->ttys;
+
+       if (!conf->ttys.max)
+               return 0;
+
+       ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
+       if (!ttys->tty)
+               return -1;
+
+       for (i = 0; i < conf->ttys.max; i++) {
+               int ttyx = -EBADF, ttyy = -EBADF;
+
+               ret = lxc_abstract_unix_recv_two_fds(sock, &ttyx, &ttyy);
+               if (ret < 0)
+                       break;
+
+               tty = &ttys->tty[i];
+               tty->busy = -1;
+               tty->ptx = ttyx;
+               tty->pty = ttyy;
+               TRACE("Received pty with ptx fd %d and pty fd %d from child", tty->ptx, tty->pty);
+       }
+
+       if (ret < 0)
+               SYSERROR("Failed to receive %zu ttys from child", ttys->max);
+       else
+               TRACE("Received %zu ttys from child", ttys->max);
+
+       return ret;
+}
+
+int lxc_sync_fds_parent(struct lxc_handler *handler)
+{
+       int ret;
+
+       ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, handler->data_sock[1]);
+       if (ret < 0)
+               return syserror_ret(ret, "Failed to receive seccomp notify fd from child");
+
+       ret = lxc_setup_devpts_parent(handler);
+       if (ret < 0)
+               return syserror_ret(ret, "Failed to receive devpts fd from child");
+
+       /* Read tty fds allocated by child. */
+       ret = lxc_recv_ttys_from_child(handler);
+       if (ret < 0)
+               return syserror_ret(ret, "Failed to receive tty info from child process");
+
+       if (handler->ns_clone_flags & CLONE_NEWNET) {
+               ret = lxc_network_recv_name_and_ifindex_from_child(handler);
+               if (ret < 0)
+                       return syserror_ret(ret, "Failed to receive names and ifindices for network devices from child");
+       }
+
+       TRACE("Finished syncing file descriptors with child");
+       return 0;
+}
+
 int lxc_setup(struct lxc_handler *handler)
 {
        int ret;
index a185b2023cd7c668ec53065191abb3d4708fdd32..8702fdcfe41d9203ea818dc6e4d6868e0acca5ba 100644 (file)
@@ -582,7 +582,7 @@ static inline int chown_mapped_root(const char *path, const struct lxc_conf *con
        return userns_exec_mapped_root(path, -EBADF, conf);
 }
 
-__hidden int lxc_setup_devpts_parent(struct lxc_handler *handler);
+__hidden extern int lxc_sync_fds_parent(struct lxc_handler *handler);
 
 static inline const char *get_rootfs_mnt(const struct lxc_rootfs *rootfs)
 {
index 21e70dce85a66fa09c159eddd8bd6ae29be310f8..e9ff4e26673614489ae70ab1d1014f1e28813049 100644 (file)
@@ -1464,44 +1464,6 @@ out_error:
        return -1;
 }
 
-static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
-{
-       int i;
-       struct lxc_terminal_info *tty;
-       int ret = -1;
-       int sock = handler->data_sock[1];
-       struct lxc_conf *conf = handler->conf;
-       struct lxc_tty_info *ttys = &conf->ttys;
-
-       if (!conf->ttys.max)
-               return 0;
-
-       ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
-       if (!ttys->tty)
-               return -1;
-
-       for (i = 0; i < conf->ttys.max; i++) {
-               int ttyx = -EBADF, ttyy = -EBADF;
-
-               ret = lxc_abstract_unix_recv_two_fds(sock, &ttyx, &ttyy);
-               if (ret < 0)
-                       break;
-
-               tty = &ttys->tty[i];
-               tty->busy = -1;
-               tty->ptx = ttyx;
-               tty->pty = ttyy;
-               TRACE("Received pty with ptx fd %d and pty fd %d from child", tty->ptx, tty->pty);
-       }
-
-       if (ret < 0)
-               SYSERROR("Failed to receive %zu ttys from child", ttys->max);
-       else
-               TRACE("Received %zu ttys from child", ttys->max);
-
-       return ret;
-}
-
 int resolve_clone_flags(struct lxc_handler *handler)
 {
        int i;
@@ -1959,33 +1921,12 @@ static int lxc_spawn(struct lxc_handler *handler)
        if (!lxc_sync_wake_child(handler, START_SYNC_FDS))
                goto out_delete_net;
 
-       ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, data_sock1);
+       ret = lxc_sync_fds_parent(handler);
        if (ret < 0) {
-               SYSERROR("Failed to receive seccomp notify fd from child");
+               SYSERROR("Failed to sync file descriptors with child");
                goto out_delete_net;
        }
 
-       ret = lxc_setup_devpts_parent(handler);
-       if (ret < 0) {
-               SYSERROR("Failed to receive devpts fd from child");
-               goto out_delete_net;
-       }
-
-       /* Read tty fds allocated by child. */
-       ret = lxc_recv_ttys_from_child(handler);
-       if (ret < 0) {
-               ERROR("Failed to receive tty info from child process");
-               goto out_delete_net;
-       }
-
-       if (handler->ns_clone_flags & CLONE_NEWNET) {
-               ret = lxc_network_recv_name_and_ifindex_from_child(handler);
-               if (ret < 0) {
-                       ERROR("Failed to receive names and ifindices for network devices from child");
-                       goto out_delete_net;
-               }
-       }
-
        /*
         * Tell the child to complete its initialization and wait for it to
         * exec or return an error. (The child will never return