Use narrowly scoped file descriptor for handling opened TTY in

spawn().  Patch from Michał Kulling.
This commit is contained in:
Petter Reinholdtsen 2014-02-10 22:44:15 +00:00
parent da988bb28e
commit d53cccb542
2 changed files with 9 additions and 6 deletions

View File

@ -87,6 +87,8 @@ sysvinit (2.89) UNRELEASED; urgency=low
for many years. for many years.
* Make it possible to specify the initctl path as a compile time * Make it possible to specify the initctl path as a compile time
define INIT_FIFO. define INIT_FIFO.
* Use narrowly scoped file descriptor for handling opened TTY in
spawn(). Patch from Michał Kulling.
-- Petter Reinholdtsen <pere@hungry.com> Sun Apr 11 11:28:55 CEST 2010 -- Petter Reinholdtsen <pere@hungry.com> Sun Apr 11 11:28:55 CEST 2010

View File

@ -1101,17 +1101,18 @@ pid_t spawn(CHILD *ch, int *res)
* to be its controlling tty. * to be its controlling tty.
*/ */
if (strchr("*#sS", runlevel) && ch->flags & WAITING) { if (strchr("*#sS", runlevel) && ch->flags & WAITING) {
int ftty; /* Handler for tty controlling */
/* /*
* We fork once extra. This is so that we can * We fork once extra. This is so that we can
* wait and change the process group and session * wait and change the process group and session
* of the console after exit of the leader. * of the console after exit of the leader.
*/ */
setsid(); setsid();
if ((f = console_open(O_RDWR|O_NOCTTY)) >= 0) { if ((ftty = console_open(O_RDWR|O_NOCTTY)) >= 0) {
/* Take over controlling tty by force */ /* Take over controlling tty by force */
(void)ioctl(f, TIOCSCTTY, 1); (void)ioctl(ftty, TIOCSCTTY, 1);
dup(f); dup(ftty);
dup(f); dup(ftty);
} }
/* /*
@ -1145,7 +1146,7 @@ pid_t spawn(CHILD *ch, int *res)
* Small optimization. See if stealing * Small optimization. See if stealing
* controlling tty back is needed. * controlling tty back is needed.
*/ */
pgrp = tcgetpgrp(f); pgrp = tcgetpgrp(ftty);
if (pgrp != getpid()) if (pgrp != getpid())
exit(0); exit(0);
@ -1160,7 +1161,7 @@ pid_t spawn(CHILD *ch, int *res)
} }
if (pid == 0) { if (pid == 0) {
setsid(); setsid();
(void)ioctl(f, TIOCSCTTY, 1); (void)ioctl(ftty, TIOCSCTTY, 1);
exit(0); exit(0);
} }
while((rc = waitpid(pid, &st, 0)) != pid) while((rc = waitpid(pid, &st, 0)) != pid)