diff --git a/docs/phantom-processes.md b/docs/phantom-processes.md index acbc311596..ca03513521 100644 --- a/docs/phantom-processes.md +++ b/docs/phantom-processes.md @@ -478,7 +478,9 @@ the old behavior and the old requirement. - OEM ROMs with their own process managers (MIUI/HyperOS, ColorOS) are outside this. - A listed program's stops and continues (`kill -STOP`, `SIGCONT`) reach its worker, not a wait in its parent: `waitpid(…, WUNTRACED)` and `WCONTINUED` report none for it. Listed programs are the - session's own (Steam, Wine, game helpers), whose parents start them without job control. Reading + session's own (Steam, Wine, game helpers), whose parents start them without job control; one a + shell runs as a job-control job (`wine game.exe &` in an interactive terminal) stays counted, as + any such job does, so its shell gets its stops. Reading the worker's state from `/proc` instead was tried and dropped: a stop and a continue can both fall between two reads. diff --git a/tools/nativeguest/hook/handoff.c b/tools/nativeguest/hook/handoff.c index 084f4f9641..c021430063 100644 --- a/tools/nativeguest/hook/handoff.c +++ b/tools/nativeguest/hook/handoff.c @@ -1239,13 +1239,17 @@ static int ddh_pressure_keeps(const char *filename, char *const argv[]) { return 0; } -static int ddh_pressed(const char *filename, char *const argv[], char *const envp[]) { +// Whether this child is a job a shell with job control runs: a child of a shell, in a process group +// other than the shell's. The shell stops, continues and waits for it with WUNTRACED, and a +// handed-off program's stops and continues never reach a wait (they happen to its worker, which is +// not the shell's child), so such a job is never handed off, listed or not. +static int ddh_job_control_job(void) { pid_t parent = (pid_t)syscall(SYS_getppid); - if (ddh_exec_stub(filename) || ddh_pressure_keeps(filename, argv) || !ddh_waiter(parent)) return 0; - // Not a job a shell with job control runs (in a process group of its own, not the shell's): the - // shell stops, continues and waits for it with WUNTRACED, and a handed-off program's stops and - // continues never reach a wait (they happen to its worker, which is not this process's child). - if ((pid_t)syscall(SYS_getpgid, 0) != (pid_t)syscall(SYS_getpgid, parent)) return 0; + return ddh_waiter(parent) && (pid_t)syscall(SYS_getpgid, 0) != (pid_t)syscall(SYS_getpgid, parent); +} + +static int ddh_pressed(const char *filename, char *const argv[], char *const envp[]) { + if (ddh_exec_stub(filename) || ddh_pressure_keeps(filename, argv) || !ddh_waiter((pid_t)syscall(SYS_getppid))) return 0; // Not a job in the foreground of its controlling terminal: a handed-off program has none (the // worker's setsid()), so /dev/tty, ^C and job control would miss it, whatever its standard fds // are ("cmd | less", "less < file", output redirected). A job started with & from a shell without @@ -1319,6 +1323,10 @@ static void ddh_try(const char *filename, char *const argv[], char *const envp[] } // Whatever this exec becomes can still hand off its own exec (a wrapper, a script). syscall(SYS_fcntl, table_fd, F_SETFD, 0); + if (ddh_job_control_job()) { + ddh_debug(envp, "a job-control job:", filename); + return; + } if (!ddh_program_listed(list, filename, argv) && !ddh_pressed(filename, argv, envp)) { ddh_debug(envp, "not listed:", filename); return; diff --git a/tools/tests/test_native_hook_handoff.py b/tools/tests/test_native_hook_handoff.py index 90b45bb97d..214f4b2b82 100644 --- a/tools/tests/test_native_hook_handoff.py +++ b/tools/tests/test_native_hook_handoff.py @@ -1318,6 +1318,13 @@ class NativeHookHandoffTest(unittest.TestCase): # run here: 4, where a child can exec an aarch64 program, else the exec's 127 self.assertEqual("4" if self.child_status == "3" else "127", result.get("exit"), out.stdout) + def test_a_listed_program_a_shell_runs_as_a_job_control_job_is_not(self): + # listed, and no pressure needed: in a process group of its own under a shell, still counted + result, requests, out = self.run_parent("group", "4", as_shell=True) + self.assertEqual([], requests, out.stderr) + self.assertEqual("1", result.get("same"), out.stdout) + self.assertEqual("4" if self.child_status == "3" else "127", result.get("exit"), out.stdout) + def test_below_the_limit_a_shells_child_is_not(self): result, requests, out = self.run_parent("wait", "3", handoff="other", pressure=1000, as_shell=True) self.assertEqual([], requests)