fix(handoff): __WNOTHREAD on waits for a pid, flags on every shim reap

This commit is contained in:
Kurt Himebauch committed 2026-11-28 03:59:33 -05:00
1 parent b801fb820f
commit 574f1f8700
2 files changed
+22 -5

No files matched your search

+4 -2
View File
@@ -1412,9 +1412,11 @@ static pid_t ddh_wait(pid_t pid, int *status, int options, struct rusage *ru) {
int interrupted = 0;
for (;;) {
ddh_check_gone();
// with __WNOTHREAD, another thread's child is not one this wait may take (ddh_selects())
struct ddh_entry *e = pid > 0 ? ddh_find(pid) : NULL;
if (e && !ddh_selects(e, pid)) e = NULL;
if (pid > 0 && e) {
if (!e->reaped && real(pid, NULL, WNOHANG, NULL) == pid) e->reaped = 1;
if (!e->reaped && real(pid, NULL, WNOHANG | (options & (__WALL | __WNOTHREAD)), NULL) == pid) e->reaped = 1;
if (ddh_take_for(e, pid)) {
// the forked child is between its last write and its exit: reap it first, or a later
// wait() would report its pid a second time (not blocking for WNOHANG: then later)
@@ -1434,7 +1436,7 @@ static pid_t ddh_wait(pid_t pid, int *status, int options, struct rusage *ru) {
interrupted = ddh_nap() != 0;
continue;
}
pid_t done = pid <= 0 ? ddh_reap_exited(real, pid, status, 0, 0) : 0;
pid_t done = pid <= 0 ? ddh_reap_exited(real, pid, status, 0, options & (__WALL | __WNOTHREAD)) : 0;
if (done > 0) {
if (ru) memset(ru, 0, sizeof(*ru));
return done;
+18 -3
View File
@@ -65,6 +65,7 @@ static char *nothread_arg;
static pthread_mutex_t nothread_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t nothread_cond = PTHREAD_COND_INITIALIZER;
static int nothread_step;
static pid_t nothread_child;
static void nothread_await(int step) {
pthread_mutex_lock(&nothread_lock);
@@ -86,6 +87,7 @@ static void *nothread_forker(void *unused) {
execl("/bin/target", "target", nothread_arg, (char *)NULL);
_exit(127);
}
nothread_child = pid;
usleep(1500000); // the program has exited by now
nothread_go(1);
nothread_await(2);
@@ -105,6 +107,7 @@ int main(int argc, char **argv) {
nothread_await(1);
int status = 0;
printf("other\t%d\n", (int)waitpid(-1, &status, WNOHANG | __WNOTHREAD));
printf("otherpid\t%d\n", (int)waitpid(nothread_child, &status, WNOHANG | __WNOTHREAD));
nothread_go(2);
pthread_join(forker, NULL);
return 0;
@@ -134,6 +137,15 @@ int main(int argc, char **argv) {
close(gate[1]);
if (read(gate[0], &c, 1) != 1) _exit(126);
}
if (strcmp(argv[1], "manyargs") == 0) {
// argv[3] after 32 others, as a long env-wrapped launch has it
char *args[37] = {"target", argv[2]};
for (int i = 2; i < 34; i++) args[i] = "x";
args[34] = argv[3];
args[35] = NULL;
execv("/bin/target", args);
_exit(127);
}
execl(argc > 3 ? argv[3] : "/bin/target", "target", argv[2], (char *)NULL);
_exit(127);
}
@@ -512,15 +524,18 @@ class NativeHookHandoffTest(unittest.TestCase):
self.assertEqual(self.child_status, result.get("exit"), out.stdout)
def test_near_the_limit_a_launch_naming_steam_in_any_argument_is_not(self):
# as "env ... /path/Steam/steamrtarm64/steam", with the path late among the arguments
result, requests, out = self.run_parent("wait", "/root/.local/share/Steam/steamrtarm64/steam", handoff="other",
pressure=0, as_shell=True)
# as "env ... /path/Steam/steamrtarm64/steam", with the path after 32 other arguments
result, requests, out = self.run_parent("manyargs", "3", "/root/.local/share/Steam/steamrtarm64/steam",
handoff="other", pressure=0, as_shell=True)
self.assertEqual([], requests)
self.assertEqual("1", result.get("same"), out.stdout)
self.assertEqual(self.child_status, result.get("exit"), out.stdout)
def test_a_wait_of_another_thread_with_wnothread_does_not_take_the_program(self):
result, requests, out = self.run_parent("nothread", "7")
self.assertEqual(1, len(requests), out.stderr)
self.assertEqual("-1", result.get("other"), out.stdout)
self.assertEqual("-1", result.get("otherpid"), out.stdout)
self.assertEqual("1", result.get("own"), out.stdout)
self.assertEqual("7", result.get("exit"), out.stdout)