static int lxc_send_ttys_to_parent(struct lxc_handler *handler)
{
int i;
- int *ttyfds;
- struct lxc_pty_info *pty_info;
struct lxc_conf *conf = handler->conf;
- const struct lxc_tty_info *tty_info = &conf->tty_info;
+ struct lxc_tty_info *tty_info = &conf->tty_info;
int sock = handler->data_sock[0];
int ret = -1;
- size_t num_ttyfds = (2 * conf->tty);
- ttyfds = malloc(num_ttyfds * sizeof(int));
- if (!ttyfds)
- return -1;
+ for (i = 0; i < conf->tty; i++) {
+ int ttyfds[2];
+ struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
- for (i = 0; i < num_ttyfds; i++) {
- pty_info = &tty_info->pty_info[i / 2];
- ttyfds[i++] = pty_info->slave;
- ttyfds[i] = pty_info->master;
- TRACE("send pty \"%s\" with master fd %d and slave fd %d to "
- "parent",
- pty_info->name, pty_info->master, pty_info->slave);
+ ttyfds[0] = pty_info->master;
+ ttyfds[1] = pty_info->slave;
+
+ ret = lxc_abstract_unix_send_fds(sock, ttyfds, 2, NULL, 0);
+ if (ret < 0)
+ break;
+
+ TRACE("Send pty \"%s\" with master fd %d and slave fd %d to "
+ "parent", pty_info->name, pty_info->master, pty_info->slave);
}
- ret = lxc_abstract_unix_send_fds(sock, ttyfds, num_ttyfds, NULL, 0);
if (ret < 0)
- ERROR("failed to send %d ttys to parent: %s", conf->tty,
+ ERROR("Failed to send %d ttys to parent: %s", conf->tty,
strerror(errno));
else
- TRACE("sent %d ttys to parent", conf->tty);
+ TRACE("Sent %d ttys to parent", conf->tty);
close(handler->data_sock[0]);
close(handler->data_sock[1]);
-
- for (i = 0; i < num_ttyfds; i++)
- close(ttyfds[i]);
-
- free(ttyfds);
+ lxc_delete_tty(tty_info);
return ret;
}
static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
{
int i;
- int *ttyfds;
struct lxc_pty_info *pty_info;
int ret = -1;
int sock = handler->data_sock[1];
struct lxc_conf *conf = handler->conf;
struct lxc_tty_info *tty_info = &conf->tty_info;
- size_t num_ttyfds = (2 * conf->tty);
if (!conf->tty)
return 0;
if (!tty_info->pty_info)
return -1;
- ttyfds = malloc(num_ttyfds * sizeof(int));
- if (!ttyfds)
- return -1;
+ for (i = 0; i < conf->tty; i++) {
+ int ttyfds[2];
+
+ ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0);
+ if (ret < 0)
+ break;
- ret = lxc_abstract_unix_recv_fds(sock, ttyfds, num_ttyfds, NULL, 0);
- for (i = 0; (ret >= 0 && *ttyfds != -1) && (i < num_ttyfds); i++) {
- pty_info = &tty_info->pty_info[i / 2];
+ pty_info = &tty_info->pty_info[i];
pty_info->busy = 0;
- pty_info->slave = ttyfds[i++];
- pty_info->master = ttyfds[i];
- TRACE("received pty with master fd %d and slave fd %d from "
+ pty_info->master = ttyfds[0];
+ pty_info->slave = ttyfds[1];
+ TRACE("Received pty with master fd %d and slave fd %d from "
"parent", pty_info->master, pty_info->slave);
}
-
- tty_info->nbtty = conf->tty;
-
- free(ttyfds);
-
if (ret < 0)
- ERROR("failed to receive %d ttys from child: %s", conf->tty,
+ ERROR("Failed to receive %d ttys from child: %s", conf->tty,
strerror(errno));
else
- TRACE("received %d ttys from child", conf->tty);
+ TRACE("Received %d ttys from child", conf->tty);
+
+ tty_info->nbtty = conf->tty;
return ret;
}