2
0
mirror of git://github.com/lxc/lxc synced 2025-08-31 05:09:37 +00:00

conf: refactor lxc_recv_ttys_from_child()

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
Christian Brauner
2021-08-02 19:16:54 +02:00
parent bca3805913
commit 2d7001d9d8

View 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;
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);
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;
}
if (ret < 0)
SYSERROR("Failed to receive %zu ttys from child", ttys->max);
else
TRACE("Received %zu ttys from child", ttys->max);
for (int i = 0; i < ttys_max; i++) {
int ptx = -EBADF, pty = -EBADF;
return ret;
ret = lxc_abstract_unix_recv_two_fds(sock, &ptx, &pty);
if (ret < 0)
return syserror("Failed to receive %zu ttys from child", ttys_max);
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);
}
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)