lib/util: fix a Coverity finding in tfork
authorRalph Boehme <slow@samba.org>
Tue, 25 Apr 2017 15:47:57 +0000 (17:47 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 25 Apr 2017 17:14:11 +0000 (19:14 +0200)
If dup2() fails, fd is -1 and is later used in sys_write().

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/tfork.c

index 27b6cc0..37c00e6 100644 (file)
@@ -131,11 +131,24 @@ static pid_t level2_fork_and_wait(int child_ready_fd)
         * We're going to stay around until child3 exits, so lets close all fds
         * other then the pipe fd we may have inherited from the caller.
         */
-       fd = dup2(tfork_global->status_pipe[1], 0);
-       if (fd == -1) {
-               status = errno;
-               kill(tfork_global->level3_pid, SIGKILL);
-               wait = false;
+       while (true) {
+               fd = dup2(tfork_global->status_pipe[1], 0);
+               if (fd == -1) {
+                       if (errno == EINTR) {
+                               continue;
+                       }
+                       status = errno;
+
+                       kill(tfork_global->level3_pid, SIGKILL);
+
+                       written = sys_write(tfork_global->status_pipe[1],
+                                           &status, sizeof(status));
+                       if (written != sizeof(status)) {
+                               abort();
+                       }
+                       _exit(0);
+               }
+               break;
        }
        closefrom(1);