From 3ece4c837f76dd9b2bc5ccbaa8aee7de7dc3ed7b Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 15 Oct 2009 13:46:11 +0000 Subject: [PATCH] Call tcgetpgrp() in the parent, not the child and have the child spin until it is granted. Fixes a race on darwin. --- script.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/script.c b/script.c index 62e148585..beef27883 100644 --- 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); -- 2.49.0