Commit 985d15b1 by Michael Tokarev Committed by Daniel Lezcano

fix fdleak and errors in lxc_create_tty()

if, for some reason, openpty() fails, lxc_create_tty() will leak all previous ptys and leave the config structure in a inconsistent state (wrt the number of ptys actually opened) Fix that by explicitly closing all previously opened ptys in case of failure and by setting number of actually opened ttys after actual open Signed-off-by: 's avatarMichael Tokarev <mjt@tls.msk.ru> Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 8f0a524d
...@@ -995,29 +995,29 @@ int lxc_assign_network(struct lxc_list *network, pid_t pid) ...@@ -995,29 +995,29 @@ int lxc_assign_network(struct lxc_list *network, pid_t pid)
int lxc_create_tty(const char *name, struct lxc_conf *conf) int lxc_create_tty(const char *name, struct lxc_conf *conf)
{ {
struct lxc_tty_info *tty_info = &conf->tty_info; struct lxc_tty_info *tty_info = &conf->tty_info;
int i, ret = -1; int i;
/* no tty in the configuration */ /* no tty in the configuration */
if (!conf->tty) if (!conf->tty)
return 0; return 0;
tty_info->nbtty = conf->tty;
tty_info->pty_info = tty_info->pty_info =
malloc(sizeof(*tty_info->pty_info)*tty_info->nbtty); malloc(sizeof(*tty_info->pty_info)*tty_info->nbtty);
if (!tty_info->pty_info) { if (!tty_info->pty_info) {
SYSERROR("failed to allocate pty_info"); SYSERROR("failed to allocate pty_info");
goto out; return -1;
} }
for (i = 0; i < tty_info->nbtty; i++) { for (i = 0; i < conf->tty; i++) {
struct lxc_pty_info *pty_info = &tty_info->pty_info[i]; struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
if (openpty(&pty_info->master, &pty_info->slave, if (openpty(&pty_info->master, &pty_info->slave,
pty_info->name, NULL, NULL)) { pty_info->name, NULL, NULL)) {
SYSERROR("failed to create pty #%d", i); SYSERROR("failed to create pty #%d", i);
goto out_free; tty_info->nbtty = i;
lxc_delete_tty(tty_info);
return -1;
} }
/* Prevent leaking the file descriptors to the container */ /* Prevent leaking the file descriptors to the container */
...@@ -1027,16 +1027,11 @@ int lxc_create_tty(const char *name, struct lxc_conf *conf) ...@@ -1027,16 +1027,11 @@ int lxc_create_tty(const char *name, struct lxc_conf *conf)
pty_info->busy = 0; pty_info->busy = 0;
} }
ret = 0; tty_info->nbtty = conf->tty;
INFO("tty's configured"); INFO("tty's configured");
out: return 0;
return ret;
out_free:
free(tty_info->pty_info);
goto out;
} }
void lxc_delete_tty(struct lxc_tty_info *tty_info) void lxc_delete_tty(struct lxc_tty_info *tty_info)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment