]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/namespaces: Kill grandchild in nsid fixture teardown
authorRicardo B. Marlière <rbm@suse.com>
Tue, 7 Apr 2026 14:35:45 +0000 (11:35 -0300)
committerChristian Brauner <brauner@kernel.org>
Mon, 11 May 2026 10:25:31 +0000 (12:25 +0200)
The timens_separate and pidns_separate test cases fork a grandchild that
calls pause().  FIXTURE_TEARDOWN only kills the direct child, which is the
init process of the grandchild's namespace.  Once the child (init) exits,
the grandchild is reparented to the host init but remains alive and
continues to hold the inherited write end of the test runner's TAP pipe
open.  tap_prefix never receives EOF and blocks indefinitely, hanging the
entire test collection.

Record the grandchild PID in the fixture struct so that teardown can send
SIGKILL and reap it before dealing with the child.  The grandchild must be
reaped first because the child acts as its PID namespace init; killing the
child first would kill the grandchild without giving us a chance to
waitpid() it.

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
Link: https://patch.msgid.link/20260407-selftests-namespaces_fixes-v1-1-59109909d88b@suse.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
tools/testing/selftests/namespaces/nsid_test.c

index b4a14c6693a54e189a0cf74e029cd32214606423..46dc838cba82d2ed72736f03f26e951f6d0dff28 100644 (file)
 /* Fixture for tests that create child processes */
 FIXTURE(nsid) {
        pid_t child_pid;
+       pid_t grandchild_pid;
 };
 
 FIXTURE_SETUP(nsid) {
        self->child_pid = 0;
+       self->grandchild_pid = 0;
 }
 
 FIXTURE_TEARDOWN(nsid) {
-       /* Clean up any child process that may still be running */
+       /*
+        * Kill grandchild first: timens_separate and pidns_separate fork a
+        * grandchild that calls pause().  It is reparented to init on child
+        * exit and keeps the test runner's tap pipe open, hanging the runner.
+        */
+       if (self->grandchild_pid > 0) {
+               kill(self->grandchild_pid, SIGKILL);
+               waitpid(self->grandchild_pid, NULL, 0);
+       }
        if (self->child_pid > 0) {
                kill(self->child_pid, SIGKILL);
                waitpid(self->child_pid, NULL, 0);
@@ -676,6 +686,7 @@ TEST_F(nsid, timens_separate)
 
        pid_t grandchild_pid;
        ASSERT_EQ(read(pipefd[0], &grandchild_pid, sizeof(grandchild_pid)), sizeof(grandchild_pid));
+       self->grandchild_pid = grandchild_pid;
        close(pipefd[0]);
 
        /* Open grandchild's time namespace */
@@ -797,6 +808,7 @@ TEST_F(nsid, pidns_separate)
 
        pid_t grandchild_pid;
        ASSERT_EQ(read(pipefd[0], &grandchild_pid, sizeof(grandchild_pid)), sizeof(grandchild_pid));
+       self->grandchild_pid = grandchild_pid;
        close(pipefd[0]);
 
        /* Open grandchild's PID namespace */