fix(handoff): getppid() gives init for an exited or replaced parent

This commit is contained in:
Kurt Himebauch committed 2026-11-28 03:21:47 -05:00
1 parent 25d97a414b
commit 1dfefac571
2 files changed
+25 -2

No files matched your search

+11 -2
View File
@@ -1607,10 +1607,19 @@ pid_t getppid(void) {
p = p ? strchr(p + 1, ':') : NULL;
pid_t parent = p ? (pid_t)atoi(p + 1) : 0;
if (parent <= 0) return real;
char path[48], stat[512];
snprintf(path, sizeof(path), "/proc/%d/stat", (int)parent);
int saved = errno;
int gone = syscall(SYS_kill, parent, 0) != 0 && errno == ESRCH;
long n = ddh_read_proc(path, stat, sizeof(stat));
errno = saved;
return gone ? 1 : parent;
// Gone, or exited and not reaped yet: the kernel reparents at the exit, not at the reaping.
char *s = n > 0 ? strrchr(stat, ')') : NULL;
if (!s || s[1] != ' ' || s[2] == 'Z' || s[2] == 'X') return 1;
// A later process given the pid started after the forked child it never had.
for (int field = 2; s && field < 22; field++) s = strchr(s + 1, ' ');
long start = s ? atol(s + 1) : -1;
long child = g_ddh_report && g_ddh_report_slot >= 0 ? (long)g_ddh_report->e[g_ddh_report_slot].child_start : -1;
return start >= 0 && child >= 0 && start > child ? 1 : parent;
}
static char g_ddh_group_entry[96];
+14
View File
@@ -493,6 +493,20 @@ class NativeHookHandoffTest(unittest.TestCase):
def test_a_handed_off_program_whose_forking_process_is_gone_has_init_as_parent(self):
self.assertEqual("1", self.ppid_in_worker(4194303))
def test_a_handed_off_program_whose_forking_process_exited_unreaped_has_init_as_parent(self):
forker = subprocess.Popen(["true"])
self.addCleanup(forker.wait)
pause = threading.Event()
for _ in range(100):
pause.wait(0.05)
try:
with open("/proc/%d/stat" % forker.pid) as f:
if f.read().rsplit(")", 1)[1].split()[0] == "Z":
break
except OSError:
break
self.assertEqual("1", self.ppid_in_worker(forker.pid))
def test_any_other_program_has_its_real_parent(self):
self.assertEqual(str(os.getpid()), self.ppid_in_worker(None))