]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: refactor lxc_recv_ttys_from_child() 3920/head
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 2 Aug 2021 17:16:54 +0000 (19:16 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 2 Aug 2021 17:21:08 +0000 (19:21 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c

index 72f45822554a5d703dbcf38908f867198d8a13bf..8dcfc60f3f84ff0af71509b73f0c7cbaec95f718 100644 (file)
@@ -1089,7 +1089,7 @@ static int lxc_allocate_ttys(struct lxc_conf *conf)
 
 void lxc_delete_tty(struct lxc_tty_info *ttys)
 {
-       if (!ttys->tty)
+       if (!ttys || !ttys->tty)
                return;
 
        for (int i = 0; i < ttys->max; i++) {
@@ -4059,40 +4059,45 @@ 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;
+       call_cleaner(lxc_delete_tty) struct lxc_tty_info *info_new = &(struct lxc_tty_info){};
        int sock = handler->data_sock[1];
        struct lxc_conf *conf = handler->conf;
-       struct lxc_tty_info *ttys = &conf->ttys;
+       struct lxc_tty_info *tty_info = &conf->ttys;
+       size_t ttys_max = tty_info->max;
+       struct lxc_terminal_info *terminal_info;
+       int ret;
 
-       if (!conf->ttys.max)
+       if (!ttys_max)
                return 0;
 
-       ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
-       if (!ttys->tty)
-               return -1;
+       info_new->tty = malloc(sizeof(*(info_new->tty)) * ttys_max);
+       if (!info_new->tty)
+               return ret_errno(ENOMEM);
 
-       for (i = 0; i < conf->ttys.max; i++) {
-               int ttyx = -EBADF, ttyy = -EBADF;
+       for (int i = 0; i < ttys_max; i++) {
+               terminal_info = &info_new->tty[i];
+               terminal_info->busy = -1;
+               terminal_info->ptx = -EBADF;
+               terminal_info->pty = -EBADF;
+       }
 
-               ret = lxc_abstract_unix_recv_two_fds(sock, &ttyx, &ttyy);
+       for (int i = 0; i < ttys_max; i++) {
+               int ptx = -EBADF, pty = -EBADF;
+
+               ret = lxc_abstract_unix_recv_two_fds(sock, &ptx, &pty);
                if (ret < 0)
-                       break;
+                       return syserror("Failed to receive %zu ttys from child", ttys_max);
 
-               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);
+               terminal_info = &info_new->tty[i];
+               terminal_info->ptx = ptx;
+               terminal_info->pty = pty;
+               TRACE("Received pty with ptx fd %d and pty fd %d from child",
+                     terminal_info->ptx, terminal_info->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;
+       tty_info->tty = move_ptr(info_new->tty);
+       TRACE("Received %zu ttys from child", ttys_max);
+       return 0;
 }
 
 static int lxc_send_console_to_parent(struct lxc_handler *handler)