From b35f8f7ef5730045c5561bdb05aca888633e4ea4 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 2 Aug 2021 19:16:54 +0200 Subject: [PATCH] conf: refactor lxc_recv_ttys_from_child() Signed-off-by: Christian Brauner --- src/lxc/conf.c | 53 +++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 72f458225..8dcfc60f3 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -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) -- 2.47.2