]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
nsenter: Enhance waiting for a child process
authorEric W. Biederman <ebiederm@xmission.com>
Thu, 17 Jan 2013 00:34:17 +0000 (16:34 -0800)
committerKarel Zak <kzak@redhat.com>
Thu, 17 Jan 2013 12:17:35 +0000 (13:17 +0100)
In the case of a pid namespace we need to fork a child process instead
of calling exec.  Move all of that logic out of line into a function
continue_as_child, making the logic of the primary case easier to
understand.

Update the logic for waiting for a child process to suspend ourselves
when the child processes suspends and to continue the child process
when we are unsuspsended.  This supports the bash suspend command and
various editors that suspend themselves.

If the child process exits with a signal update the logic to run
kill(getpid(), WTERMSIG(status)) so the caller sees the same exit code
that nsenter observed.

There will always be permission to send signals to our children and
the tty is not changing so there is no need for the intermediate
process to forward signals to it's child.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
sys-utils/nsenter.c

index f9c66364851ddbb73b77caf7f7e2d496eecff1fd..a264515ebee0af644478e7af9df0b346505801b1 100644 (file)
@@ -145,6 +145,39 @@ static void open_namespace_fd(int nstype, char *path)
        err(EXIT_FAILURE, "Unrecognized namespace type");
 }
 
+static void continue_as_child(void)
+{
+       pid_t child = fork();
+       int status;
+       pid_t ret;
+
+       if (child < 0)
+               err(EXIT_FAILURE, _("fork failed"));
+
+       /* Only the child returns */
+       if (child == 0)
+               return;
+
+       for (;;) {
+               ret = waitpid(child, &status, WUNTRACED);
+               if ((ret == child) && (WIFSTOPPED(status))) {
+                       /* The child suspended so suspend us as well */
+                       kill(getpid(), SIGSTOP);
+                       kill(child, SIGCONT);
+               } else {
+                       break;
+               }
+       }
+       /* Return the child's exit code if possible */
+       if (WIFEXITED(status)) {
+               exit(WEXITSTATUS(status));
+       }
+       else if (WIFSIGNALED(status)) {
+               kill(getpid(), WTERMSIG(status));
+       }
+       exit(EXIT_FAILURE);
+}
+
 int main(int argc, char *argv[])
 {
        static const struct option longopts[] = {
@@ -266,19 +299,8 @@ int main(int argc, char *argv[])
                wd_fd = -1;
        }
 
-       if (do_fork) {
-               pid_t child = fork();
-               if (child < 0)
-                       err(EXIT_FAILURE, _("fork failed"));
-               if (child != 0) {
-                       int status;
-                       if ((waitpid(child, &status, 0) == child) &&
-                            WIFEXITED(status)) {
-                               exit(WEXITSTATUS(status));
-                       }
-                       exit(EXIT_FAILURE);
-               }
-       }
+       if (do_fork)
+               continue_as_child();
 
        execvp(argv[optind], argv + optind);