From b68437ab63951e367b6c00f6151fb6f8ca4dc0d4 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sat, 28 Nov 2026 03:28:30 -0500 Subject: [PATCH] fix(handoff): keep __WALL waitid calls on the handoff path --- tools/nativeguest/hook/handoff.c | 15 +++++++++------ tools/tests/test_native_hook_handoff.py | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/tools/nativeguest/hook/handoff.c b/tools/nativeguest/hook/handoff.c index 6505950828..82514ff41a 100644 --- a/tools/nativeguest/hook/handoff.c +++ b/tools/nativeguest/hook/handoff.c @@ -1467,11 +1467,14 @@ int waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) { errno = ENOSYS; return -1; } - // A wait without exits never takes a forked child's exit for the program's. Other option bits - // (invalid ones too, which the kernel refuses) and group 1 (init's, which a waitpid() selector of - // -1 would take for any child): the real call. + // A wait without exits never takes a forked child's exit for the program's. Invalid option bits + // (which the kernel refuses), __WCLONE alone (which selects no forked child) and group 1 (init's, + // which a waitpid() selector of -1 would take for any child): the real call. __WALL and + // __WNOTHREAD go along with each call made here. + int carried = options & (__WALL | __WNOTHREAD); if (!__atomic_load_n(&g_ddh, __ATOMIC_ACQUIRE) || !(options & WEXITED) || - (options & ~(WEXITED | WSTOPPED | WCONTINUED | WNOHANG | WNOWAIT)) || + (options & ~(WEXITED | WSTOPPED | WCONTINUED | WNOHANG | WNOWAIT | __WALL | __WNOTHREAD | __WCLONE)) || + ((options & __WCLONE) && !(options & __WALL)) || (idtype != P_PID && idtype != P_ALL && idtype != P_PGID) || (idtype == P_PGID && id == 1)) return real(idtype, id, info, options); if ((idtype == P_PID && (pid_t)id <= 0) || (idtype == P_PGID && (pid_t)id < 0)) { @@ -1521,7 +1524,7 @@ int waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) { int exited = seen.si_code == CLD_EXITED || seen.si_code == CLD_KILLED || seen.si_code == CLD_DUMPED; struct ddh_entry *h = exited ? ddh_find(child) : NULL; if (h && !h->reaped) { - if (w4 && w4(child, NULL, WNOHANG, NULL) == child) h->reaped = 1; // the forked child that handed its program off + if (w4 && w4(child, NULL, WNOHANG | carried, NULL) == child) h->reaped = 1; // the forked child that handed its program off continue; } if (options & WNOWAIT) { @@ -1533,7 +1536,7 @@ int waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) { int kind = exited ? WEXITED : seen.si_code == CLD_CONTINUED ? WCONTINUED : WSTOPPED; siginfo_t got; memset(&got, 0, sizeof(got)); - if (real(P_PID, (id_t)child, &got, kind | WNOHANG) < 0 || got.si_pid != child) continue; // another waiter took it + if (real(P_PID, (id_t)child, &got, kind | carried | WNOHANG) < 0 || got.si_pid != child) continue; // another waiter took it // A forked child that died while handing its program off (a signal): its entry is done. int died = got.si_code == CLD_EXITED || got.si_code == CLD_KILLED || got.si_code == CLD_DUMPED; if (died && !ddh_find(child) && (h = ddh_claiming(child))) ddh_release(h, NULL); diff --git a/tools/tests/test_native_hook_handoff.py b/tools/tests/test_native_hook_handoff.py index 5de00518a0..21c80f9209 100644 --- a/tools/tests/test_native_hook_handoff.py +++ b/tools/tests/test_native_hook_handoff.py @@ -106,13 +106,14 @@ int main(int argc, char **argv) { printf("again\t%d\n", (int)waitpid(-1, &status, WNOHANG)); return 0; } - if (strcmp(argv[1], "waitid") == 0) { - // at once, before the child has claimed its handoff, and for stops too + int all = strcmp(argv[1], "waitidall") == 0 ? __WALL : 0; + if (strcmp(argv[1], "waitid") == 0 || all) { + // at once, before the child has claimed its handoff, and for stops too (and with __WALL) siginfo_t si; errno = 0; printf("pid0\t%d\n", waitid(P_PID, 0, &si, WEXITED) < 0 && errno == EINVAL); memset(&si, 0, sizeof(si)); - int r = waitid(P_ALL, 0, &si, WEXITED | WSTOPPED); + int r = waitid(P_ALL, 0, &si, WEXITED | WSTOPPED | all); printf("same\t%d\n", r == 0 && si.si_pid == pid); printf("code\t%d\n", si.si_code); printf("exit\t%d\n", si.si_status); @@ -410,6 +411,14 @@ class NativeHookHandoffTest(unittest.TestCase): self.assertEqual(str(os.getuid()), result.get("uid"), out.stdout) self.assertEqual("-1", result.get("again"), out.stdout) + def test_waitid_with_wall_reports_the_program(self): + result, requests, out = self.run_parent("waitidall", "7") + self.assertEqual(1, len(requests), out.stderr) + self.assertEqual("1", result.get("same"), out.stdout) + self.assertEqual(str(CLD_EXITED), result.get("code"), out.stdout) + self.assertEqual("7", result.get("exit"), out.stdout) + self.assertEqual("-1", result.get("again"), out.stdout) + def test_waitid_for_a_child_not_listed_is_the_kernels(self): result, requests, out = self.run_parent("waitid", "3", handoff="other") self.assertEqual([], requests)