mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
fix(nativeguest): address review findings in handoff waits, signals and spawn errors
This commit is contained in:
1 parent
ba9b0b0422
commit
924ad4e9a3
5 files changed
+77
-25
No files matched your search
@@ -490,6 +490,9 @@ object GuestWorkers {
|
||||
} else {
|
||||
take()
|
||||
}
|
||||
// Until start() has them: closed on every way out before that
|
||||
var handedOver = false
|
||||
try {
|
||||
val argv = request.getJSONArray("argv").let { a -> List(a.length()) { a.getString(it) } }
|
||||
val env = LinkedHashMap<String, String>()
|
||||
request.optJSONObject("env")?.let { o -> o.keys().forEach { env[it] = o.getString(it) } }
|
||||
@@ -500,7 +503,8 @@ object GuestWorkers {
|
||||
env.keys.removeAll { it.startsWith("DDH_") }
|
||||
val cwd = request.optString("cwd", "/")
|
||||
val wait = request.optBoolean("wait", false)
|
||||
val file = request.optString("file", "")
|
||||
// A path without a slash names a file in the cwd, as execve() takes it, not one on PATH
|
||||
val file = request.optString("file", "").let { if (it.isNotEmpty() && '/' !in it) "./$it" else it }
|
||||
// The handoff table (the hook's) comes last among the fds; the rest go to these numbers.
|
||||
val tableIndex = request.optInt("table", -1)
|
||||
val numbers = request.optJSONArray("fds")?.let { a -> IntArray(a.length()) { a.getInt(it) } } ?: IntArray(3) { it }
|
||||
@@ -522,7 +526,6 @@ object GuestWorkers {
|
||||
val out = client.outputStream
|
||||
if (root == null || argv.isEmpty()) {
|
||||
out.write("error no guest root or program\n".toByteArray())
|
||||
fds.forEach { runCatching { it.close() } }
|
||||
return
|
||||
}
|
||||
val shell = listOf("/usr/bin/bash", "/bin/bash").first { File(root, it.removePrefix("/")).isFile }
|
||||
@@ -551,15 +554,21 @@ object GuestWorkers {
|
||||
exited.countDown()
|
||||
}
|
||||
worker = pid
|
||||
handedOver = true
|
||||
programFds.filter { it !== table }.forEach { runCatching { it.close() } }
|
||||
if (pid <= 0) table?.let { runCatching { it.close() } }
|
||||
out.write(if (pid > 0) "pid $pid\n".toByteArray() else "error no worker\n".toByteArray())
|
||||
out.flush()
|
||||
if (pid > 0 && wait) {
|
||||
exited.await()
|
||||
out.write("exit $status\n".toByteArray())
|
||||
// droiddeck-spawn exits with this: an exit code, or 128 plus the signal
|
||||
val code = if (status < 0) 1 else if (status and 0x7f == 0) (status shr 8) and 0xff else 128 + (status and 0x7f)
|
||||
out.write("exit $code\n".toByteArray())
|
||||
out.flush()
|
||||
}
|
||||
} finally {
|
||||
if (!handedOver) fds.forEach { runCatching { it.close() } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,8 +45,9 @@ Source: `frameworks/base/services/core/java/com/android/server/am/PhantomProcess
|
||||
at `PROCESS_STATE_HOME` or below (backgrounded, no foreground service) and a phantom used
|
||||
"excessive" CPU, `killPhantomProcessGroupLocked` kills every phantom of that app **and the app**.
|
||||
|
||||
Both are switched off only by `settings_enable_monitor_phantom_procs=false` (the developer option)
|
||||
or by raising `max_phantom_processes` through `device_config` (shell only).
|
||||
`settings_enable_monitor_phantom_procs=false` (the developer option) switches off both. Raising
|
||||
`max_phantom_processes` through `device_config` (shell only) avoids only the first: the
|
||||
excessive-CPU check still runs while monitoring is on.
|
||||
|
||||
Today an idle Steam session is about 30 processes and a session with a game about 45 (#566, #575).
|
||||
|
||||
@@ -438,6 +439,16 @@ the old behavior and the old requirement.
|
||||
forks sleepers from the main process, a plain isolated service, an app-zygote isolated service and
|
||||
eight `:wN` workers, execs in place, and probes what a process may do.
|
||||
|
||||
The limit of 4 below is device-wide: a trim while it is set can kill other apps' child processes,
|
||||
which restoring the setting does not bring back. Use a test device or an emulator with nothing else
|
||||
running, and note the current values first, to put them back afterwards:
|
||||
|
||||
```
|
||||
adb shell settings get global settings_enable_monitor_phantom_procs
|
||||
adb shell device_config get activity_manager max_phantom_processes
|
||||
adb shell device_config get_sync_disabled_for_tests
|
||||
```
|
||||
|
||||
```
|
||||
tools/phantomlab/build.sh # build/phantomlab.apk, debug-signed
|
||||
adb install -r tools/phantomlab/build/phantomlab.apk
|
||||
@@ -452,6 +463,7 @@ adb logcat -s PLAB ActivityManager | grep -i 'PLAB\|Trimming'
|
||||
|
||||
Targets: `main`, `proc` (`:proc`), `isoplain`, `iso:<name>` (one app-zygote instance per name),
|
||||
`w0`…`w7`. Android scans only on CPU-stat refreshes, so switch apps a few times (5 s apart) before
|
||||
reading the phantom list. Restore afterwards with
|
||||
`device_config delete activity_manager max_phantom_processes` and
|
||||
`device_config set_sync_disabled_for_tests none`.
|
||||
reading the phantom list. Restore the noted values afterwards: `adb shell device_config put
|
||||
activity_manager max_phantom_processes <value>` (or `adb shell device_config delete activity_manager
|
||||
max_phantom_processes` if it was null), `adb shell device_config set_sync_disabled_for_tests <mode>`
|
||||
and `adb shell settings put global settings_enable_monitor_phantom_procs <value>`.
|
||||
@@ -1134,6 +1134,9 @@ static int ddh_spawn_child_setup(const posix_spawn_file_actions_t *fa, const pos
|
||||
break;
|
||||
case DDH_DO_OPEN: {
|
||||
const char *real = rewrite_path(a[i].action.open_action.path, path, sizeof(path));
|
||||
// as glibc: the target goes first, so a full fd table still has room for the open
|
||||
if (!is_kept_fd(a[i].action.open_action.fd))
|
||||
call_orig_syscall(SYS_close, (unsigned long)a[i].action.open_action.fd, 0, 0, 0, 0, 0);
|
||||
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;
|
||||
@@ -1202,12 +1205,20 @@ static int ddh_spawn_forked(pid_t *pid, const char *path, const posix_spawn_file
|
||||
}
|
||||
if (child == 0) {
|
||||
call_orig_syscall(SYS_close, (unsigned long)report[0], 0, 0, 0, 0, 0);
|
||||
// Out of the file actions' way: high, and one of the fds they leave open (closefrom(3) is common)
|
||||
int to_parent = (int)call_orig_syscall(SYS_fcntl, (unsigned long)report[1], F_DUPFD_CLOEXEC, 1000, 0, 0, 0);
|
||||
if (to_parent >= 0) {
|
||||
call_orig_syscall(SYS_close, (unsigned long)report[1], 0, 0, 0, 0, 0);
|
||||
ddh_keep(to_parent);
|
||||
} else {
|
||||
to_parent = report[1];
|
||||
}
|
||||
int err = ddh_spawn_child_setup(fa, attr);
|
||||
if (!err) {
|
||||
execve(path, argv, envp);
|
||||
err = errno;
|
||||
}
|
||||
syscall(SYS_write, report[1], &err, sizeof(err));
|
||||
syscall(SYS_write, to_parent, &err, sizeof(err));
|
||||
_exit(127);
|
||||
}
|
||||
close(report[1]);
|
||||
|
||||
@@ -477,7 +477,7 @@ struct ddh_buf {
|
||||
};
|
||||
|
||||
static void ddh_put(struct ddh_buf *b, const char *s, size_t n) {
|
||||
if (b->p && b->len + n <= b->cap) memcpy(b->p + b->len, s, n);
|
||||
if (b->p && b->len < b->cap) memcpy(b->p + b->len, s, n < b->cap - b->len ? n : b->cap - b->len);
|
||||
b->len += n;
|
||||
}
|
||||
|
||||
@@ -574,8 +574,15 @@ static int ddh_open_fds(int *fds, int max) {
|
||||
// thread of the parent may have held at fork().
|
||||
static const char *ddh_interpreter(const char *path, char *buf, size_t size) {
|
||||
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);
|
||||
// non-blocking, and only a regular file: the path could name a FIFO
|
||||
int fd = (int)call_orig_syscall(SYS_openat, (unsigned long)AT_FDCWD, (unsigned long)rewrite_path(path, real, sizeof(real)),
|
||||
O_RDONLY | O_CLOEXEC | O_NONBLOCK, 0, 0, 0);
|
||||
if (fd < 0) return NULL;
|
||||
struct stat st;
|
||||
if (call_orig_syscall(SYS_fstat, (unsigned long)fd, (unsigned long)&st, 0, 0, 0, 0) != 0 || !S_ISREG(st.st_mode)) {
|
||||
syscall(SYS_close, fd);
|
||||
return NULL;
|
||||
}
|
||||
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;
|
||||
@@ -977,7 +984,9 @@ int waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (!g_ddh || !(options & WEXITED) || (idtype != P_PID && idtype != P_ALL) || !ddh_any_live())
|
||||
// An ordinary child asked for by pid: the real call, with all its options (stops, WNOWAIT).
|
||||
if (!g_ddh || !(options & WEXITED) || (idtype != P_PID && idtype != P_ALL) || !ddh_any_live() ||
|
||||
(idtype == P_PID && !ddh_find((pid_t)id)))
|
||||
return real(idtype, id, info, options);
|
||||
// Only exits are asked for here: answer through ddh_wait and describe the result as waitid does.
|
||||
int status = 0;
|
||||
@@ -1055,7 +1064,7 @@ static void ddh_register_group(void) {
|
||||
int dir = ddh_open_groups(1);
|
||||
if (dir < 0) return;
|
||||
long start = ddh_start_time(getpid());
|
||||
char name[96], text[48];
|
||||
char name[96], text[96];
|
||||
const char *group = getenv("DDH_PGID");
|
||||
if (group) {
|
||||
snprintf(name, sizeof(name), "%d.%d.%ld", atoi(group), (int)getpid(), start);
|
||||
@@ -1067,9 +1076,11 @@ static void ddh_register_group(void) {
|
||||
}
|
||||
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);
|
||||
// with their start times: a pid followed later may have been reused by then
|
||||
const char *anchor_env = getenv("DDH_ANCHOR");
|
||||
pid_t anchor = anchor_env ? (pid_t)atoi(anchor_env) : g_ddh_report_parent;
|
||||
int len = snprintf(text, sizeof(text), "%ld %d %d %ld %ld\n", start, (int)g_ddh_report_parent, (int)anchor,
|
||||
ddh_start_time(g_ddh_report_parent), ddh_start_time(anchor));
|
||||
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) {
|
||||
@@ -1099,7 +1110,7 @@ static void ddh_unregister_group(void) {
|
||||
// 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];
|
||||
char path[PATH_MAX], text[96];
|
||||
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);
|
||||
@@ -1111,10 +1122,15 @@ static pid_t ddh_logical_parent(pid_t pid) {
|
||||
syscall(SYS_close, fd);
|
||||
if (n <= 0) return 0;
|
||||
text[n] = 0;
|
||||
long start = 0;
|
||||
long start = 0, parent_start = -1, anchor_start = -1;
|
||||
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);
|
||||
if (sscanf(text, "%ld %d %d %ld %ld", &start, &parent, &anchor, &parent_start, &anchor_start) < 5 ||
|
||||
ddh_start_time(pid) != start)
|
||||
return 0;
|
||||
// -1: it was gone already when the program started (Wine forks through one that exits at once)
|
||||
if (parent > 1 && parent_start >= 0 && ddh_start_time(parent) == parent_start) return (pid_t)parent;
|
||||
if (anchor > 1 && anchor_start >= 0 && ddh_start_time(anchor) == anchor_start) return (pid_t)anchor;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Signals the groups of the programs standing for group; returns how many took it.
|
||||
@@ -1152,10 +1168,13 @@ static int ddh_signal_stand_ins(pid_t group, int sig, pid_t done) {
|
||||
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;
|
||||
// The caller's own group last: the signal may end the caller.
|
||||
long ret = -1;
|
||||
int err = ESRCH;
|
||||
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 (syscall(SYS_kill, pid, sig) == 0) ret = 0;
|
||||
else err = errno;
|
||||
if (ret != 0) errno = err;
|
||||
return (int)ret;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import errno
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
@@ -192,10 +193,10 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.assertEqual("6", result.get("exit"), out.stdout)
|
||||
|
||||
def test_a_posix_spawn_of_a_missing_program_fails_as_it_would(self):
|
||||
listed, requests, out = self.run_parent("spawn", "6", "/bin/missing")
|
||||
unlisted, _, _ = self.run_parent("spawn", "6", "/bin/missing", handoff="other")
|
||||
# posix_spawn() returns the exec's error, even with closefrom(3) among the file actions
|
||||
result, requests, out = self.run_parent("spawn", "6", "/bin/missing")
|
||||
self.assertEqual([], requests)
|
||||
self.assertEqual((unlisted.get("spawned"), unlisted.get("exit")), (listed.get("spawned"), listed.get("exit")), out.stdout)
|
||||
self.assertEqual(str(errno.ENOENT), result.get("spawned"), out.stdout)
|
||||
|
||||
def test_kill_reaches_the_program_and_its_signal_is_reported(self):
|
||||
result, requests, out = self.run_parent("kill", "sleep")
|
||||
|
||||
Reference in new issue
Block a user