open_init_pty: restore stdin/stdout to blocking upon exit

At exit, restore stdin and stdout to blocking.

Test: run_init id && run_init id
Test: open_init_pty bash -c 'echo hello; exec >&- 2>&- <&-; sleep 1;'

Fixes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=863187
Fixes: https://bugs.gentoo.org/show_bug.cgi?id=621062
Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
This commit is contained in:
Stephen Smalley 2017-07-18 08:59:50 -04:00
parent bea2c19dd2
commit ebd695d5f0
1 changed files with 26 additions and 0 deletions

View File

@ -191,6 +191,28 @@ static void setfd_nonblock(int fd)
} }
} }
static void setfd_block(int fd)
{
int fsflags = fcntl(fd, F_GETFL);
if (fsflags < 0) {
fprintf(stderr, "fcntl(%d, F_GETFL): %s\n", fd, strerror(errno));
exit(EX_IOERR);
}
if (fcntl(fd, F_SETFL, fsflags & ~O_NONBLOCK) < 0) {
fprintf(stderr, "fcntl(%d, F_SETFL, ... & ~O_NONBLOCK): %s\n", fd, strerror(errno));
exit(EX_IOERR);
}
}
static void setfd_atexit(void)
{
setfd_block(STDIN_FILENO);
setfd_block(STDOUT_FILENO);
return;
}
static void sigchld_handler(int asig __attribute__ ((unused))) static void sigchld_handler(int asig __attribute__ ((unused)))
{ {
} }
@ -280,6 +302,10 @@ int main(int argc, char *argv[])
setfd_nonblock(pty_master); setfd_nonblock(pty_master);
setfd_nonblock(STDIN_FILENO); setfd_nonblock(STDIN_FILENO);
setfd_nonblock(STDOUT_FILENO); setfd_nonblock(STDOUT_FILENO);
if (atexit(setfd_atexit) < 0) {
perror("atexit()");
exit(EXIT_FAILURE);
}
if (isatty(STDIN_FILENO)) { if (isatty(STDIN_FILENO)) {
if (tty_semi_raw(STDIN_FILENO) < 0) { if (tty_semi_raw(STDIN_FILENO) < 0) {