]> granicus.if.org Git - sudo/commitdiff
Call tcgetpgrp() in the parent, not the child and have the child spin
authorTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 15 Oct 2009 13:46:11 +0000 (13:46 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 15 Oct 2009 13:46:11 +0000 (13:46 +0000)
until it is granted.  Fixes a race on darwin.

script.c

index 62e1485851a95e9d41a1451f3e2ba6484ed43f47..beef27883011e858fceefb954a49a86ec0962cd1 100644 (file)
--- a/script.c
+++ b/script.c
@@ -523,8 +523,10 @@ script_child(path, argv)
        warning("unable to execute %s", path);
        _exit(127);
     }
-    /* Also set grandchild process group here to avoid a race condition. */
+    /* Set grandchild process group and grant it the controlling tty. */
     setpgid(grandchild, grandchild);
+    if (tcsetpgrp(script_fds[SFD_SLAVE], grandchild) != 0)
+       warning("tcsetpgrp");
 
     gettimeofday(&then, NULL);
 
@@ -724,14 +726,9 @@ script_grandchild(path, argv, rbac_enabled)
 {
     pid_t self = getpid();
 
-    /* Also set our process group here to avoid a race condition. */
-    setpgid(0, self);
-
     dup2(script_fds[SFD_SLAVE], STDIN_FILENO);
     dup2(script_fds[SFD_SLAVE], STDOUT_FILENO);
     dup2(script_fds[SFD_SLAVE], STDERR_FILENO);
-    if (tcsetpgrp(STDIN_FILENO, self) != 0)
-       warning("tcsetpgrp");
 
     /*
      * Close old fds and exec command.
@@ -742,6 +739,10 @@ script_grandchild(path, argv, rbac_enabled)
     close(script_fds[SFD_OUTPUT]);
     close(script_fds[SFD_TIMING]);
 
+    /* Spin until parent grants us the controlling pty */
+    while (tcgetpgrp(STDIN_FILENO) != self)
+       continue;
+
 #ifdef HAVE_SELINUX
     if (rbac_enabled)
       selinux_execv(path, argv);