Use narrowly scoped file descriptor for handling opened TTY in
authorPetter Reinholdtsen <pere@hungry.com>
Mon, 10 Feb 2014 22:44:15 +0000 (22:44 +0000)
committerPetter Reinholdtsen <pere@hungry.com>
Mon, 10 Feb 2014 22:44:15 +0000 (22:44 +0000)
spawn().  Patch from Michał Kulling.

git-svn-id: svn://svn.sv.gnu.org/sysvinit/sysvinit/trunk@158 456724a4-4300-0410-8514-c89748c515a2

doc/Changelog
src/init.c

index f7606a13ab2d3cb1b6f10f2cf83b87a1e3aa4a53..722aef235609761fe5934f966069d3abe3bc65ed 100644 (file)
@@ -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.
     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 <pere@hungry.com>  Sun Apr 11 11:28:55 CEST 2010
 
 
  -- Petter Reinholdtsen <pere@hungry.com>  Sun Apr 11 11:28:55 CEST 2010
 
index fc1a1318cd2f2354424a48ee6699295ee5c23d01..d6234fb6de77791397c9a640e78d92fbb9036ba5 100644 (file)
@@ -1101,17 +1101,18 @@ pid_t spawn(CHILD *ch, int *res)
                 *      to be its controlling tty.
                 */
                if (strchr("*#sS", runlevel) && ch->flags & WAITING) {
                 *      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();
                        /*
                         *      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 */
                                /* 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.
                                 */
                                 *      Small optimization. See if stealing
                                 *      controlling tty back is needed.
                                 */
-                               pgrp = tcgetpgrp(f);
+                               pgrp = tcgetpgrp(ftty);
                                if (pgrp != getpid())
                                        exit(0);
 
                                if (pgrp != getpid())
                                        exit(0);
 
@@ -1160,7 +1161,7 @@ pid_t spawn(CHILD *ch, int *res)
                                }
                                if (pid == 0) {
                                        setsid();
                                }
                                if (pid == 0) {
                                        setsid();
-                                       (void)ioctl(f, TIOCSCTTY, 1);
+                                       (void)ioctl(ftty, TIOCSCTTY, 1);
                                        exit(0);
                                }
                                while((rc = waitpid(pid, &st, 0)) != pid)
                                        exit(0);
                                }
                                while((rc = waitpid(pid, &st, 0)) != pid)