]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
su: make wait_for_child() usable in arbitrary situation
authorKarel Zak <kzak@redhat.com>
Wed, 16 Aug 2017 12:03:06 +0000 (14:03 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 18 Sep 2017 09:49:11 +0000 (11:49 +0200)
For example if called more than once; to keep PTY code simple and
robust.

Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/su-common.c

index 48812cbc11643c79f343d0c8fd4692c136a5575d..f4e11dd2d8a293c5618da1a20a0ccfeb473ebe70 100644 (file)
@@ -334,21 +334,23 @@ static void supam_open_session(struct su_context *su)
 
 static int wait_for_child(struct su_context *su)
 {
-       pid_t pid;
+       pid_t pid = (pid_t) -1;;
        int status = 0;
 
-       DBG(SIG, ul_debug("waiting for child [%d]...", su->child));
-       for (;;) {
-               pid = waitpid(su->child, &status, WUNTRACED);
-
-               if (pid != (pid_t) - 1 && WIFSTOPPED(status)) {
-                       kill(getpid(), SIGSTOP);
-                       /* once we get here, we must have resumed */
-                       kill(pid, SIGCONT);
-               } else
-                       break;
+       if (su->child != (pid_t) -1) {
+               DBG(SIG, ul_debug("waiting for child [%d]...", su->child));
+               for (;;) {
+                       pid = waitpid(su->child, &status, WUNTRACED);
+
+                       if (pid != (pid_t) - 1 && WIFSTOPPED(status)) {
+                               kill(getpid(), SIGSTOP);
+                               /* once we get here, we must have resumed */
+                               kill(pid, SIGCONT);
+                       } else
+                               break;
+               }
        }
-       if (pid != (pid_t) - 1) {
+       if (pid != (pid_t) -1) {
                if (WIFSIGNALED(status)) {
                        fprintf(stderr, "%s%s\n",
                                strsignal(WTERMSIG(status)),