From d53cccb54296b94fadbb2753e33484e2e1df9ac5 Mon Sep 17 00:00:00 2001 From: Petter Reinholdtsen Date: Mon, 10 Feb 2014 22:44:15 +0000 Subject: [PATCH] =?UTF-8?q?Use=20narrowly=20scoped=20file=20descriptor=20f?= =?UTF-8?q?or=20handling=20opened=20TTY=20in=20spawn().=20=20Patch=20from?= =?UTF-8?q?=20Micha=C5=82=20Kulling.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/Changelog | 2 ++ src/init.c | 13 +++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index f7606a1..722aef2 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -87,6 +87,8 @@ sysvinit (2.89) UNRELEASED; urgency=low for many years. * Make it possible to specify the initctl path as a compile time define INIT_FIFO. + * Use narrowly scoped file descriptor for handling opened TTY in + spawn(). Patch from MichaƂ Kulling. -- Petter Reinholdtsen Sun Apr 11 11:28:55 CEST 2010 diff --git a/src/init.c b/src/init.c index fc1a131..d6234fb 100644 --- a/src/init.c +++ b/src/init.c @@ -1101,17 +1101,18 @@ pid_t spawn(CHILD *ch, int *res) * to be its controlling tty. */ if (strchr("*#sS", runlevel) && ch->flags & WAITING) { + int ftty; /* Handler for tty controlling */ /* * We fork once extra. This is so that we can * wait and change the process group and session * of the console after exit of the leader. */ 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 */ - (void)ioctl(f, TIOCSCTTY, 1); - dup(f); - dup(f); + (void)ioctl(ftty, TIOCSCTTY, 1); + dup(ftty); + dup(ftty); } /* @@ -1145,7 +1146,7 @@ pid_t spawn(CHILD *ch, int *res) * Small optimization. See if stealing * controlling tty back is needed. */ - pgrp = tcgetpgrp(f); + pgrp = tcgetpgrp(ftty); if (pgrp != getpid()) exit(0); @@ -1160,7 +1161,7 @@ pid_t spawn(CHILD *ch, int *res) } if (pid == 0) { setsid(); - (void)ioctl(f, TIOCSCTTY, 1); + (void)ioctl(ftty, TIOCSCTTY, 1); exit(0); } while((rc = waitpid(pid, &st, 0)) != pid)