]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
unshare: Add waitchild helper
authorSean Anderson <seanga2@gmail.com>
Wed, 24 Nov 2021 18:26:14 +0000 (13:26 -0500)
committerKarel Zak <kzak@redhat.com>
Wed, 1 Dec 2021 12:35:59 +0000 (13:35 +0100)
This refactors out the waitpid() logic into a function which will be
reused for the upcoming patches.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
sys-utils/unshare.c

index 3f8287799fcc55d5adea985d24f874bf87907c07..62fa66067e0eecb26bdc64c599faee9f8351e40d 100644 (file)
@@ -226,6 +226,30 @@ static void settime(time_t offset, clockid_t clk_id)
        close(fd);
 }
 
+/**
+ * waitchild() - Wait for a process to exit successfully
+ * @pid: PID of the process to wait for
+ *
+ * Wait for a process to exit successfully. If it exits with a non-zero return
+ * code, then exit() with the same status.
+ */
+static void waitchild(int pid)
+{
+       int rc, status;
+
+       do {
+               rc = waitpid(pid, &status, 0);
+               if (rc < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       err(EXIT_FAILURE, _("waitpid failed"));
+               }
+               if (WIFEXITED(status) &&
+                   WEXITSTATUS(status) != EXIT_SUCCESS)
+                       exit(WEXITSTATUS(status));
+       } while (rc < 0);
+}
+
 static void bind_ns_files_from_child(pid_t *child, int fds[2])
 {
        char ch;
@@ -580,7 +604,6 @@ int main(int argc, char *argv[])
        if (npersists && (pid || !forkit)) {
                /* run in parent */
                if (pid_bind && (unshare_flags & CLONE_NEWNS)) {
-                       int rc;
                        char ch = PIPE_SYNC_BYTE;
 
                        /* signal child we are ready */
@@ -589,17 +612,7 @@ int main(int argc, char *argv[])
                        fds[1] = -1;
 
                        /* wait for bind_ns_files_from_child() */
-                       do {
-                               rc = waitpid(pid_bind, &status, 0);
-                               if (rc < 0) {
-                                       if (errno == EINTR)
-                                               continue;
-                                       err(EXIT_FAILURE, _("waitpid failed"));
-                               }
-                               if (WIFEXITED(status) &&
-                                   WEXITSTATUS(status) != EXIT_SUCCESS)
-                                       return WEXITSTATUS(status);
-                       } while (rc < 0);
+                       waitchild(pid_bind);
                } else
                        /* simple way, just bind */
                        bind_ns_files(getpid());