fix(nativeguest): let Steam end a handed-off game and keep process-group signals

This commit is contained in:
Kurt Himebauch committed 2026-11-27 15:27:40 -05:00
1 parent e418f99aa0
commit ae6863d016
4 files changed
+317 -20

No files matched your search

@@ -425,6 +425,8 @@ object GuestWorkers {
if (request.has("sigign")) env["DDH_SIGIGN"] = request.getLong("sigign").toString()
if (request.has("nofile")) env["DDH_NOFILE"] = request.getLong("nofile").toString()
if (request.has("umask")) env["DDH_UMASK"] = request.getLong("umask").toString()
if (request.has("pgid")) env["DDH_PGID"] = request.getLong("pgid").toString()
if (request.has("anchor")) env["DDH_ANCHOR"] = request.getLong("anchor").toString()
val exited = CountDownLatch(1)
var status = -1
val pid = start(app, command, env.map { (k, v) -> "$k=$v" }, programFds, targets, true) { code ->
+22 -1
View File
@@ -349,7 +349,28 @@ lingers in its worker; see below).
login, and the trace from a game's processes went to a pipe nobody read.
- **Finding the sender of a SIGKILL:** shell can enable the `signal/signal_generate` ftrace event
on the Thor (`/sys/kernel/tracing`), which names the sending thread; Perfetto recorded nothing
there. Quitting the game, stopping the session (no worker is left) and starting
there.
- **Ending a game from Steam** (`SteamClient.Apps.TerminateApp`, the overlay's Exit game) left Wine
running since the first round: Steam sends SIGINT to the launch's `reaper` and its parent, and the
reaper ends what is under it, which handed-off programs are not. Each handed-off program now
lists itself as `P<pid>` with the process that forked its child and, for when that process is
gone (Wine starts a process through a fork that exits at once), the worker's program it descended
from. A signal to `reaper SteamLaunch` from a hooked process also goes, as SIGTERM for SIGINT
(Wine takes SIGINT for a console's Ctrl+C), to the groups of every listed program under that
reaper, all found before any is signalled.
- **Process groups:** a handed-off program runs in a session of its own, outside the group its
forked parent's child was in. It lists itself as `GROUP.PID.START` for that group (`DDH_PGID`,
also for the processes it forks), and the hook's `kill(-GROUP)`, `kill(0)` and `killpg()` also
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
system calls in its child (no hook wrapper takes a lock there): one Steam fork hung on a lock
another thread held.
Quitting the game (also from Steam), stopping the session (no worker is left) and starting
again all work. Started from the front end with the limit on, Setup shows "Child-process limit
handled by DroidDeck" and Steam starts.
+42 -16
View File
@@ -1086,7 +1086,18 @@ struct ddh_spawn_action {
#define POSIX_SPAWN_SETSID 0x80
#endif
// What glibc's spawn child does before its exec (posix/spawni.c), as an errno.
static int ddh_spawn_errno(long ret) {
return ret < 0 ? errno : 0;
}
static int ddh_spawn_close_part(unsigned int first, unsigned int last, void *unused) {
(void)unused;
return (int)call_orig_syscall(SYS_close_range, first, last, 0, 0, 0, 0);
}
// What glibc's spawn child does before its exec (posix/spawni.c), as an errno. Its fd and
// directory work is done with system calls, not the hook's wrappers: those take locks that another
// thread of the parent may have held at fork() (Steam has many threads).
static int ddh_spawn_child_setup(const posix_spawn_file_actions_t *fa, const posix_spawnattr_t *attr) {
short flags = 0;
if (attr) posix_spawnattr_getflags(attr, &flags);
@@ -1112,36 +1123,51 @@ static int ddh_spawn_child_setup(const posix_spawn_file_actions_t *fa, const pos
if (r == -1) return errno;
}
const struct ddh_spawn_action *a = fa ? (const struct ddh_spawn_action *)fa->__actions : NULL;
char path[PATH_MAX];
int err;
for (int i = 0; a && i < fa->__used; i++) {
switch (a[i].tag) {
case DDH_DO_CLOSE:
close(a[i].action.close_action.fd);
// the handoff table and the worker's binder stay open (kept fds)
if (!is_kept_fd(a[i].action.close_action.fd))
call_orig_syscall(SYS_close, (unsigned long)a[i].action.close_action.fd, 0, 0, 0, 0, 0);
break;
case DDH_DO_OPEN: {
int fd = open(a[i].action.open_action.path, a[i].action.open_action.oflag, a[i].action.open_action.mode);
const char *real = rewrite_path(a[i].action.open_action.path, path, sizeof(path));
long fd = call_orig_syscall(SYS_openat, (unsigned long)AT_FDCWD, (unsigned long)real,
(unsigned long)a[i].action.open_action.oflag, a[i].action.open_action.mode, 0, 0);
if (fd < 0) return errno;
if (fd != a[i].action.open_action.fd) {
if (dup2(fd, a[i].action.open_action.fd) < 0) return errno;
close(fd);
if ((err = ddh_spawn_errno(call_orig_syscall(SYS_dup3, (unsigned long)fd, (unsigned long)a[i].action.open_action.fd, 0, 0, 0, 0))))
return err;
call_orig_syscall(SYS_close, (unsigned long)fd, 0, 0, 0, 0, 0);
}
break;
}
case DDH_DO_DUP2:
if (a[i].action.dup2_action.fd == a[i].action.dup2_action.newfd) {
int fdflags = fcntl(a[i].action.dup2_action.fd, F_GETFD);
if (fdflags < 0 || fcntl(a[i].action.dup2_action.fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) return errno;
} else if (dup2(a[i].action.dup2_action.fd, a[i].action.dup2_action.newfd) < 0) {
return errno;
case DDH_DO_DUP2: {
int from = a[i].action.dup2_action.fd, to = a[i].action.dup2_action.newfd;
if (from == to) {
long fdflags = call_orig_syscall(SYS_fcntl, (unsigned long)from, F_GETFD, 0, 0, 0, 0);
if (fdflags < 0) return errno;
if ((err = ddh_spawn_errno(call_orig_syscall(SYS_fcntl, (unsigned long)from, F_SETFD, (unsigned long)(fdflags & ~FD_CLOEXEC), 0, 0, 0))))
return err;
} else if ((err = ddh_spawn_errno(call_orig_syscall(SYS_dup3, (unsigned long)from, (unsigned long)to, 0, 0, 0, 0)))) {
return err;
}
break;
case DDH_DO_CHDIR:
if (chdir(a[i].action.chdir_action.path) != 0) return errno;
}
case DDH_DO_CHDIR: {
const char *real = rewrite_path_follow(a[i].action.chdir_action.path, path, sizeof(path));
if ((err = ddh_spawn_errno(call_orig_syscall(SYS_chdir, (unsigned long)real, 0, 0, 0, 0, 0)))) return err;
break;
}
case DDH_DO_FCHDIR:
if (fchdir(a[i].action.fchdir_action.fd) != 0) return errno;
if ((err = ddh_spawn_errno(call_orig_syscall(SYS_fchdir, (unsigned long)a[i].action.fchdir_action.fd, 0, 0, 0, 0, 0))))
return err;
break;
case DDH_DO_CLOSEFROM:
close_range((unsigned int)a[i].action.closefrom_action.from, ~0U, 0);
for_each_range_without((unsigned int)a[i].action.closefrom_action.from, ~0U, g_kept_fds, g_kept_fd_count,
ddh_spawn_close_part, NULL);
break;
case DDH_DO_TCSETPGRP:
if (tcsetpgrp(a[i].action.setpgrp_action.fd, getpgrp()) != 0) return errno;
@@ -1175,7 +1201,7 @@ static int ddh_spawn_forked(pid_t *pid, const char *path, const posix_spawn_file
return err;
}
if (child == 0) {
close(report[0]);
call_orig_syscall(SYS_close, (unsigned long)report[0], 0, 0, 0, 0, 0);
int err = ddh_spawn_child_setup(fa, attr);
if (!err) {
execve(path, argv, envp);
+251 -3
View File
@@ -330,6 +330,10 @@ static void ddh_linger(int flush) {
}
}
static void ddh_register_group(void);
static void ddh_unregister_group(void);
static pid_t ddh_logical_pgrp(void);
// From exit() (on_exit, arg 1) or _exit() (arg NULL) in a worker's own process.
// Only a handed-off child lingers: the app waits for the death of the programs it starts itself
// (the session's root) to tear everything down.
@@ -340,6 +344,7 @@ static void ddh_report_exit(int status, void *from_exit) {
__atomic_store_n(&e->state, DDH_EXITED, __ATOMIC_RELEASE);
if (g_ddh_report_parent > 0) syscall(SYS_kill, g_ddh_report_parent, SIGCHLD);
ddh_linger(from_exit != NULL);
ddh_unregister_group();
}
// A program that leaves with _exit() skips on_exit(): a daemon's first process does, once it has
@@ -372,6 +377,7 @@ static void ddh_setup_report(void) {
g_ddh_report_slot = slot;
g_ddh_report_parent = parent;
on_exit(ddh_report_exit, (void *)1);
ddh_register_group();
}
// In a program a worker started: the state its forked parent had, sent along with the exec.
@@ -543,11 +549,14 @@ static int ddh_open_fds(int *fds, int max) {
}
// The interpreter a script names on its "#!" line ("/usr/bin/env NAME" gives NAME), into buf.
// In a forked child: system calls only, as the hook's own open() and close() take locks another
// thread of the parent may have held at fork().
static const char *ddh_interpreter(const char *path, char *buf, size_t size) {
int fd = open(path, O_RDONLY | O_CLOEXEC);
char real[PATH_MAX];
int fd = (int)call_orig_syscall(SYS_openat, (unsigned long)AT_FDCWD, (unsigned long)rewrite_path(path, real, sizeof(real)), O_RDONLY | O_CLOEXEC, 0, 0, 0);
if (fd < 0) return NULL;
long n = read(fd, buf, size - 1);
close(fd);
long n = call_orig_syscall(SYS_read, (unsigned long)fd, (unsigned long)buf, size - 1, 0, 0, 0);
syscall(SYS_close, fd);
if (n < 3 || buf[0] != '#' || buf[1] != '!') return NULL;
buf[n] = 0;
char *p = buf + 2;
@@ -701,6 +710,10 @@ static void ddh_try(const char *filename, char *const argv[], char *const envp[]
ddh_putint(&b, (long)nofile.rlim_cur);
ddh_puts(&b, ",\"umask\":");
ddh_putint(&b, (long)mask);
ddh_puts(&b, ",\"pgid\":");
ddh_putint(&b, (long)ddh_logical_pgrp());
ddh_puts(&b, ",\"anchor\":");
ddh_putint(&b, getenv("DDH_WORKER_PID") ? (long)atoi(getenv("DDH_WORKER_PID")) : (long)parent);
ddh_puts(&b, "}");
if (pass == 0) {
b.cap = b.len + 4;
@@ -941,6 +954,230 @@ static int ddh_app_group(pid_t pgid) {
return n > 0 && strncmp(name, "zygote", 6) == 0;
}
// A handed-off program runs in a session of its own (the worker's setsid()), out of the process
// group its forked parent's child was in, so a signal to that group (Steam ends a game with one)
// would miss it. Each one is listed here as GROUP.PID.START, GROUP being the group it stands for,
// and a signal to GROUP also goes to the group the program leads.
#define DDH_GROUPS "/run/droiddeck-handoff-groups"
static long ddh_start_time(pid_t pid) {
char path[48], stat[512];
snprintf(path, sizeof(path), "/proc/%d/stat", (int)pid);
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return -1;
long n = syscall(SYS_read, fd, stat, sizeof(stat) - 1);
syscall(SYS_close, fd);
if (n <= 0) return -1;
stat[n] = 0;
char *p = strrchr(stat, ')');
// starttime is field 22; after ") " comes field 3
for (int field = 2; p && field < 22; field++) p = strchr(p + 1, ' ');
return p ? atol(p + 1) : -1;
}
// The group a process stands for: its worker's (DDH_PGID) while it is in the group its worker leads.
static pid_t ddh_logical_pgrp(void) {
pid_t real = getpgrp();
const char *worker = getenv("DDH_WORKER_PID"), *group = getenv("DDH_PGID");
if (worker && group && real == (pid_t)atoi(worker)) return (pid_t)atoi(group);
return real;
}
static char g_ddh_group_entry[96];
static int g_ddh_parent_entry;
static int ddh_open_groups(int create) {
char dir[PATH_MAX];
const char *real_dir = rewrite_path(DDH_GROUPS, dir, sizeof(dir));
if (create) call_orig_syscall(SYS_mkdirat, (unsigned long)AT_FDCWD, (unsigned long)real_dir, 0755, 0, 0, 0);
return (int)call_orig_syscall(SYS_openat, (unsigned long)AT_FDCWD, (unsigned long)real_dir,
O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0, 0);
}
// Also P<PID>, "START PARENT ANCHOR": the process that forked this program's child, the parent it
// has in place of the worker's (see ddh_signal_reaper_tree()), and the worker's program that process
// descends from (DDH_ANCHOR), for when it is gone: Wine starts a process through a fork that exits.
static void ddh_register_group(void) {
if (getpid() != g_ddh_worker) return;
int dir = ddh_open_groups(1);
if (dir < 0) return;
long start = ddh_start_time(getpid());
char name[96], text[48];
const char *group = getenv("DDH_PGID");
if (group) {
snprintf(name, sizeof(name), "%d.%d.%ld", atoi(group), (int)getpid(), start);
int fd = (int)call_orig_syscall(SYS_openat, (unsigned long)dir, (unsigned long)name, O_WRONLY | O_CREAT | O_CLOEXEC, 0644, 0, 0);
if (fd >= 0) {
syscall(SYS_close, fd);
snprintf(g_ddh_group_entry, sizeof(g_ddh_group_entry), "%s", name);
}
}
if (g_ddh_report_parent > 0) {
snprintf(name, sizeof(name), "P%d", (int)getpid());
const char *anchor = getenv("DDH_ANCHOR");
int len = snprintf(text, sizeof(text), "%ld %d %d\n", start, (int)g_ddh_report_parent,
anchor ? atoi(anchor) : (int)g_ddh_report_parent);
int fd = (int)call_orig_syscall(SYS_openat, (unsigned long)dir, (unsigned long)name,
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644, 0, 0);
if (fd >= 0) {
call_orig_syscall(SYS_write, (unsigned long)fd, (unsigned long)text, (unsigned long)len, 0, 0, 0);
syscall(SYS_close, fd);
g_ddh_parent_entry = 1;
}
}
syscall(SYS_close, dir);
}
static void ddh_unregister_group(void) {
if ((!g_ddh_group_entry[0] && !g_ddh_parent_entry) || getpid() != g_ddh_worker) return;
int dir = ddh_open_groups(0);
if (dir < 0) return;
if (g_ddh_group_entry[0]) call_orig_syscall(SYS_unlinkat, (unsigned long)dir, (unsigned long)g_ddh_group_entry, 0, 0, 0, 0);
if (g_ddh_parent_entry) {
char name[24];
snprintf(name, sizeof(name), "P%d", (int)getpid());
call_orig_syscall(SYS_unlinkat, (unsigned long)dir, (unsigned long)name, 0, 0, 0, 0);
}
syscall(SYS_close, dir);
g_ddh_group_entry[0] = 0;
g_ddh_parent_entry = 0;
}
// The process that forked pid's child (or, gone, the worker's program it descended from), when pid
// is a handed-off program; else 0.
static pid_t ddh_logical_parent(pid_t pid) {
char path[PATH_MAX], text[48];
const char *real_dir = rewrite_path(DDH_GROUPS, path, sizeof(path));
if (real_dir != path) snprintf(path, sizeof(path), "%s", real_dir);
size_t used = strlen(path);
if (used + 16 >= sizeof(path)) return 0;
snprintf(path + used, sizeof(path) - used, "/P%d", (int)pid);
int fd = (int)call_orig_syscall(SYS_openat, (unsigned long)AT_FDCWD, (unsigned long)path, O_RDONLY | O_CLOEXEC, 0, 0, 0);
if (fd < 0) return 0;
long n = call_orig_syscall(SYS_read, (unsigned long)fd, (unsigned long)text, sizeof(text) - 1, 0, 0, 0);
syscall(SYS_close, fd);
if (n <= 0) return 0;
text[n] = 0;
long start = 0;
int parent = 0, anchor = 0;
if (sscanf(text, "%ld %d %d", &start, &parent, &anchor) < 2 || parent <= 1 || ddh_start_time(pid) != start) return 0;
return (pid_t)(ddh_start_time(parent) < 0 && anchor > 1 ? anchor : parent);
}
// Signals the groups of the programs standing for group; returns how many took it.
static int ddh_signal_stand_ins(pid_t group, int sig, pid_t done) {
char dir[PATH_MAX];
const char *real_dir = rewrite_path(DDH_GROUPS, dir, sizeof(dir));
int fd = (int)call_orig_syscall(SYS_openat, (unsigned long)AT_FDCWD, (unsigned long)real_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0, 0);
if (fd < 0) return 0;
char prefix[24], entries[4096];
int plen = snprintf(prefix, sizeof(prefix), "%d.", (int)group);
int sent = 0;
long n;
while ((n = syscall(SYS_getdents64, fd, entries, sizeof(entries))) > 0) {
for (long off = 0; off < n;) {
unsigned short reclen;
memcpy(&reclen, entries + off + 16, sizeof(reclen));
const char *name = entries + off + 19;
off += reclen ? reclen : n;
if (strncmp(name, prefix, (size_t)plen) != 0) continue;
int real = 0;
long start = 0;
if (sscanf(name + plen, "%d.%ld", &real, &start) != 2 || real <= 1) continue;
if (ddh_start_time(real) != start) {
call_orig_syscall(SYS_unlinkat, (unsigned long)fd, (unsigned long)name, 0, 0, 0, 0); // gone, or its pid reused
continue;
}
if (real == done || ddh_app_group(real)) continue;
if (syscall(SYS_kill, -real, sig) == 0) sent++;
}
}
syscall(SYS_close, fd);
return sent;
}
static int ddh_kill_group(pid_t pid, int sig) {
pid_t group = pid == 0 ? ddh_logical_pgrp() : -pid;
pid_t own = pid == 0 ? getpgrp() : -pid;
long ret = syscall(SYS_kill, pid, sig);
int err = errno;
if (group != own && !ddh_app_group(group) && syscall(SYS_kill, -group, sig) == 0) ret = 0;
if (ddh_signal_stand_ins(group, sig, own) > 0) ret = 0;
if (ret != 0) errno = err;
return (int)ret;
}
// The kernel's parent of pid, from /proc/<pid>/stat; 0 when unknown.
static pid_t ddh_proc_parent(pid_t pid) {
char path[48], stat[512];
snprintf(path, sizeof(path), "/proc/%d/stat", (int)pid);
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return 0;
long n = syscall(SYS_read, fd, stat, sizeof(stat) - 1);
syscall(SYS_close, fd);
if (n <= 0) return 0;
stat[n] = 0;
char *p = strrchr(stat, ')');
return p && p[1] == ' ' && p[2] && p[3] == ' ' ? (pid_t)atoi(p + 4) : 0;
}
// Steam's "reaper SteamLaunch AppId=N -- ...", which runs a game's launch.
static int ddh_is_steam_reaper(pid_t pid) {
char path[48], cmd[128];
snprintf(path, sizeof(path), "/proc/%d/cmdline", (int)pid);
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return 0;
long n = syscall(SYS_read, fd, cmd, sizeof(cmd) - 1);
syscall(SYS_close, fd);
if (n <= 0) return 0;
cmd[n] = 0;
size_t first = strlen(cmd);
return (size_t)n > first + 1 && strcmp(ddh_base(cmd), "reaper") == 0 && strcmp(cmd + first + 1, "SteamLaunch") == 0;
}
static int ddh_descends_from(pid_t pid, pid_t ancestor) {
for (int depth = 0; pid > 1 && depth < 64; depth++) {
if (pid == ancestor) return 1;
pid_t up = ddh_logical_parent(pid);
pid = up > 0 ? up : ddh_proc_parent(pid);
}
return 0;
}
// Steam ends a game by signalling its launch's reaper, which ends every process under it; the
// handed-off ones are not under it, so they (and the groups they lead) get the signal here. All are
// found before any is signalled: one that exits takes its place in the chain of parents with it.
// SIGINT goes as SIGTERM: Wine takes SIGINT for a console's Ctrl+C, which windowed programs ignore.
static void ddh_signal_reaper_tree(pid_t reaper, int sig) {
int dir = ddh_open_groups(0);
if (dir < 0) return;
pid_t found[256];
int count = 0;
char entries[4096];
long n;
while ((n = syscall(SYS_getdents64, dir, entries, sizeof(entries))) > 0) {
for (long off = 0; off < n;) {
unsigned short reclen;
memcpy(&reclen, entries + off + 16, sizeof(reclen));
const char *name = entries + off + 19;
off += reclen ? reclen : n;
if (name[0] != 'P' || name[1] < '1' || name[1] > '9') continue;
pid_t pid = (pid_t)atoi(name + 1);
if (count < 256 && ddh_logical_parent(pid) > 0 && ddh_descends_from(pid, reaper)) found[count++] = pid;
}
}
syscall(SYS_close, dir);
for (int i = 0; i < count; i++) syscall(SYS_kill, -found[i], sig == SIGINT ? SIGTERM : sig);
}
int killpg(int pgrp, int sig) {
if (pgrp < 0) {
errno = EINVAL;
return -1;
}
return kill(-pgrp, sig);
}
int kill(pid_t pid, int sig) {
if (g_ddh && pid > 0) {
struct ddh_entry *e = ddh_find(pid);
@@ -951,6 +1188,17 @@ int kill(pid_t pid, int sig) {
errno = EPERM;
return -1;
}
if (pid < -1 || pid == 0) {
if (sig != 0 && getenv("DD_HANDOFF_DEBUG")) {
char line[96];
int len = snprintf(line, sizeof(line), "ddnatv handoff: %d kill group %d (stands for %d), %d\n", (int)getpid(),
pid == 0 ? (int)getpgrp() : (int)-pid, pid == 0 ? (int)ddh_logical_pgrp() : (int)-pid, sig);
if (len > 0) ddh_log(getenv("DD_HANDOFF_DEBUG"), line, (size_t)len);
}
return ddh_kill_group(pid, sig);
}
if (pid > 1 && (sig == SIGINT || sig == SIGTERM || sig == SIGKILL || sig == SIGHUP) && ddh_is_steam_reaper(pid))
ddh_signal_reaper_tree(pid, sig);
if (sig != 0 && getenv("DD_HANDOFF_DEBUG")) {
char line[160], comm[32] = "?";
int fd = (int)syscall(SYS_openat, AT_FDCWD, "/proc/self/comm", O_RDONLY | O_CLOEXEC);