mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
fix(handoff): a handed-off program's getppid() is its forking process
This commit is contained in:
1 parent
d629f83b8c
commit
12a15a5526
3 files changed
+61
-9
No files matched your search
@@ -390,9 +390,12 @@ lingers in its worker; see below).
|
||||
signal the groups those programs lead.
|
||||
- **Tried and dropped:** handing off Steam's launch chain (`sh`, `steam-launch-wrapper`, `reaper`):
|
||||
the client tracks the process it forked, which then exits at once, and called the game closed.
|
||||
Showing a handed-off program's forking process as its parent in `/proc/<pid>/stat`, `status` and
|
||||
`getppid()` left Steam's UI at "Loading library data": the client checks the parents of the
|
||||
processes on its IPC connections with `lsof -F R`. And `posix_spawn()` emulation must make only
|
||||
Showing a handed-off program's forking process as its parent in `/proc/<pid>/stat` and `status`
|
||||
left Steam's UI at "Loading library data": the client checks the parents of the processes on its
|
||||
IPC connections with `lsof -F R`. Only the program's own `getppid()` answers with that process
|
||||
(while it lives, then 1): the GOG launch leases the game folder for as long as the launcher, its
|
||||
parent, runs, and took zygote, which the guest cannot see, for a launcher already gone. And
|
||||
`posix_spawn()` emulation must make only
|
||||
system calls in its child (no hook wrapper takes a lock there): one Steam fork hung on a lock
|
||||
another thread held.
|
||||
|
||||
@@ -448,7 +451,8 @@ the old behavior and the old requirement.
|
||||
Bully on GE-Proton, Proton Experimental ARM64 and DroidDeck-Proton 11 (11–14 counted), Bomb
|
||||
Rush Cyberfunk (14), MGSV Ground Zeroes (13), Sekiro (13), zero-copy off (11), the x86-64
|
||||
Steam client under FEX (14, against 55 without the handoff), Splinter Cell through Ubisoft
|
||||
Connect (14, against 65), the desktop at rest (17–20) and
|
||||
Connect (14, against 65), Star Wars: Galaxy of Heroes (EA, 14), 9 Years of Shadows (GOG, 14),
|
||||
run mode with 40 background jobs (1), the desktop at rest (17–20) and
|
||||
with a terminal's 40 background jobs (20). PRoot sessions keep the old behavior and still need
|
||||
the developer option: with the limit on, Android killed 30 of their processes and the session
|
||||
failed;
|
||||
|
||||
@@ -185,7 +185,7 @@ static int ddh_list_fds(int *fds, int max) {
|
||||
// A table inherited across exec: the parent's if its owner is still our parent, else closed.
|
||||
static void ddh_find_parent_table(void) {
|
||||
char path[64], target[64];
|
||||
pid_t parent = getppid();
|
||||
pid_t parent = (pid_t)syscall(SYS_getppid);
|
||||
// The table a worker's program reports its exit to (DDH_TABLE), also for what it execs
|
||||
int report = -1;
|
||||
const char *spec = getenv("DDH_TABLE");
|
||||
@@ -850,7 +850,7 @@ static int ddh_waiter(pid_t pid) {
|
||||
}
|
||||
|
||||
static int ddh_pressed(const char *filename, char *const envp[]) {
|
||||
if (ddh_exec_stub(filename) || !ddh_waiter(getppid())) return 0;
|
||||
if (ddh_exec_stub(filename) || !ddh_waiter((pid_t)syscall(SYS_getppid))) return 0;
|
||||
// Not one that reads the terminal it is in the foreground of: a handed-off program has no
|
||||
// controlling terminal (the worker's setsid()), so /dev/tty, ^C and job control would miss it.
|
||||
// A job started with & reads /dev/null, or runs in a background group.
|
||||
@@ -886,14 +886,14 @@ static void ddh_try(const char *filename, char *const argv[], char *const envp[]
|
||||
if (own && getpid() != own_owner) {
|
||||
table = own;
|
||||
table_fd = g_ddh_fd;
|
||||
} else if (g_ddh_up && getppid() == g_ddh_up->pad[0]) {
|
||||
} else if (g_ddh_up && (pid_t)syscall(SYS_getppid) == g_ddh_up->pad[0]) {
|
||||
table = g_ddh_up;
|
||||
table_fd = g_ddh_up_fd;
|
||||
} else {
|
||||
if (ddh_env(envp, "DD_HANDOFF_DEBUG")) {
|
||||
char why[160];
|
||||
snprintf(why, sizeof(why), "not a child its parent can track (table %d owner %d up %d up-owner %d ppid %d):",
|
||||
own != NULL, (int)own_owner, g_ddh_up != NULL, g_ddh_up ? g_ddh_up->pad[0] : 0, (int)getppid());
|
||||
own != NULL, (int)own_owner, g_ddh_up != NULL, g_ddh_up ? g_ddh_up->pad[0] : 0, (int)(pid_t)syscall(SYS_getppid));
|
||||
ddh_debug(envp, why, filename);
|
||||
}
|
||||
return;
|
||||
@@ -938,7 +938,7 @@ static void ddh_try(const char *filename, char *const argv[], char *const envp[]
|
||||
}
|
||||
if (slot < 0) return;
|
||||
struct ddh_entry *e = &table->e[slot];
|
||||
pid_t me = getpid(), parent = getppid();
|
||||
pid_t me = getpid(), parent = (pid_t)syscall(SYS_getppid);
|
||||
// Before the request: the app checks them before it reports an exit into this entry, which a
|
||||
// later handoff may have taken over by then.
|
||||
e->child = me;
|
||||
@@ -1551,6 +1551,25 @@ static pid_t ddh_logical_group(pid_t real) {
|
||||
|
||||
static pid_t ddh_logical_pgrp(void) { return ddh_logical_group(getpgrp()); }
|
||||
|
||||
// A handed-off program's parent is zygote, which the guest cannot see: a launcher that watches its
|
||||
// parent through /proc/<ppid> took it for gone at once. getppid() answers with the process that
|
||||
// forked it (DDH_TABLE's parent) while that one lives, and init once it is gone, as after the
|
||||
// kernel's reparenting. /proc/<pid>/stat keeps the real parent (see docs, "Tried and dropped").
|
||||
pid_t getppid(void) {
|
||||
pid_t real = (pid_t)syscall(SYS_getppid);
|
||||
const char *worker = getenv("DDH_WORKER_PID"), *spec = getenv("DDH_TABLE");
|
||||
if (!worker || !spec || (pid_t)atoi(worker) != getpid()) return real;
|
||||
// fd:slot:parent:child
|
||||
const char *p = strchr(spec, ':');
|
||||
p = p ? strchr(p + 1, ':') : NULL;
|
||||
pid_t parent = p ? (pid_t)atoi(p + 1) : 0;
|
||||
if (parent <= 0) return real;
|
||||
int saved = errno;
|
||||
int gone = syscall(SYS_kill, parent, 0) != 0 && errno == ESRCH;
|
||||
errno = saved;
|
||||
return gone ? 1 : parent;
|
||||
}
|
||||
|
||||
static char g_ddh_group_entry[96];
|
||||
static int g_ddh_parent_entry;
|
||||
|
||||
|
||||
@@ -403,6 +403,35 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.assertEqual([], requests)
|
||||
self.assertEqual(self.child_status, result.get("exit"), out.stdout)
|
||||
|
||||
def ppid_in_worker(self, parent):
|
||||
# as a worker's own program: DDH_WORKER_PID is its pid, DDH_TABLE names the process that forked it
|
||||
tmp = Path(self.tmp.name)
|
||||
probe = tmp / "ppid"
|
||||
if not probe.exists():
|
||||
(tmp / "ppid.c").write_text('#include <stdio.h>\n#include <unistd.h>\nint main(void) { printf("%d", (int)getppid()); return 0; }\n')
|
||||
subprocess.run([self.cc, "-O1", "-o", str(probe), str(tmp / "ppid.c")], check=True)
|
||||
env = {"LD_PRELOAD": str(self.hook), "DDNATV_ROOT": str(self.root)}
|
||||
if parent is not None:
|
||||
env["DDH_TABLE"] = "3:0:%d:0" % parent
|
||||
settings = " ".join("-E %s=%s" % item for item in env.items()) if self.prefix else \
|
||||
" ".join("%s=%s" % item for item in env.items())
|
||||
worker = "-E DDH_WORKER_PID=$$" if self.prefix else "DDH_WORKER_PID=$$"
|
||||
runner = " ".join(self.prefix) + " " + settings + " " + worker if self.prefix else "env " + settings + " " + worker
|
||||
out = subprocess.run(["sh", "-c", "exec %s %s" % (runner, probe)], capture_output=True, text=True, timeout=60)
|
||||
return out.stdout.strip()
|
||||
|
||||
def test_a_handed_off_programs_parent_is_the_process_that_forked_it(self):
|
||||
forker = subprocess.Popen(["sleep", "60"])
|
||||
self.addCleanup(forker.wait)
|
||||
self.addCleanup(forker.kill)
|
||||
self.assertEqual(str(forker.pid), self.ppid_in_worker(forker.pid))
|
||||
|
||||
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_any_other_program_has_its_real_parent(self):
|
||||
self.assertEqual(str(os.getpid()), self.ppid_in_worker(None))
|
||||
|
||||
def test_a_program_not_listed_is_a_normal_child(self):
|
||||
result, requests, out = self.run_parent("wait", "3", handoff="other")
|
||||
self.assertEqual([], requests)
|
||||
|
||||
Reference in new issue
Block a user