Commit ed38feee by Christian Brauner Committed by GitHub

Merge pull request #1690 from brauner/2017-07-10/lifeng68-Modify_fds

start: dup std{in,out,err} to pty slave
parents 1a6cb1ce bbbf65ee
...@@ -1063,8 +1063,13 @@ static int do_start(void *data) ...@@ -1063,8 +1063,13 @@ static int do_start(void *data)
* setup on its console ie. the pty allocated in lxc_console_create() so * setup on its console ie. the pty allocated in lxc_console_create() so
* make sure that that pty is stdin,stdout,stderr. * make sure that that pty is stdin,stdout,stderr.
*/ */
if (lxc_console_set_stdfds(handler->conf->console.slave) < 0) if (handler->conf->console.slave >= 0)
goto out_warn_father; if (set_stdfds(handler->conf->console.slave) < 0) {
ERROR("Failed to redirect std{in,out,err} to pty file "
"descriptor %d",
handler->conf->console.slave);
goto out_warn_father;
}
/* If we mounted a temporary proc, then unmount it now. */ /* If we mounted a temporary proc, then unmount it now. */
tmp_proc_unmount(handler->conf); tmp_proc_unmount(handler->conf);
...@@ -1142,8 +1147,12 @@ static int do_start(void *data) ...@@ -1142,8 +1147,12 @@ static int do_start(void *data)
goto out_warn_father; goto out_warn_father;
} }
if (handler->backgrounded && set_stdfds(devnull_fd)) if (handler->conf->console.slave < 0 && handler->backgrounded)
goto out_warn_father; if (set_stdfds(devnull_fd) < 0) {
ERROR("Failed to redirect std{in,out,err} to "
"\"/dev/null\"");
goto out_warn_father;
}
if (devnull_fd >= 0) { if (devnull_fd >= 0) {
close(devnull_fd); close(devnull_fd);
......
...@@ -1822,14 +1822,21 @@ int open_devnull(void) ...@@ -1822,14 +1822,21 @@ int open_devnull(void)
int set_stdfds(int fd) int set_stdfds(int fd)
{ {
int ret;
if (fd < 0) if (fd < 0)
return -1; return -1;
if (dup2(fd, 0) < 0) ret = dup2(fd, STDIN_FILENO);
if (ret < 0)
return -1; return -1;
if (dup2(fd, 1) < 0)
ret = dup2(fd, STDOUT_FILENO);
if (ret < 0)
return -1; return -1;
if (dup2(fd, 2) < 0)
ret = dup2(fd, STDERR_FILENO);
if (ret < 0)
return -1; return -1;
return 0; return 0;
......
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