diff --git a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java index 53a0a6aacf..1ff48a77cd 100644 --- a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java +++ b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java @@ -186,13 +186,16 @@ public class PulseAudioComponent extends SessionPart { env.add("LD_LIBRARY_PATH=/system/lib64:" + modules + ":" + workingDir.getAbsolutePath()); env.add("HOME=" + workingDir); env.add("TMPDIR=" + workingDir); + // The daemon closes every fd at start but this one; in a worker process it is the binder + // fd whose closing Android takes for the process's death. + env.add("PULSE_PASSED_FD=" + com.droiddeck.launcher.runtime.GuestWorker.BINDER_FD_TOKEN); String command = String.join(" ", HostProcess.dataExecutable(workingDir.getAbsolutePath() + "/libpulseaudio.so")) + " --system=false --disable-shm=true --fail=false" + " -n --file=default.pa --daemonize=false --use-pid-file=false --exit-idle-time=-1" + " --log-level=info"; final java.io.PrintWriter out = openLog(); - if (!host.launch(generation, () -> HostProcess.start(command, env.toArray(new String[0]), workingDir, status -> { + if (!host.launch(generation, () -> HostProcess.startUncounted(command, env.toArray(new String[0]), workingDir, status -> { if (host.exited(generation)) { Log.w(TAG, "audio host exited with status " + status + "; restarting"); start(generation); diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt b/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt index a75b669574..2c3cfe3c88 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt @@ -52,7 +52,7 @@ open class GuestWorker : Service() { private fun exec(argv: Array, env: Array, fds: List, targets: IntArray) { try { val top = targets.maxOrNull() ?: 2 - val binderTop = keepBinderOpen(maxOf(BINDER_FDS, top + 1)) + val (binderTop, binderFd) = keepBinderOpen(maxOf(BINDER_FDS, top + 1)) // Out of the way first, so that placing one fd cannot overwrite another not yet placed. val above = maxOf(MOVED_FDS, top + 1, binderTop + 1) val moved = fds.map { pfd -> pfd?.let { Os.fcntlInt(it.fileDescriptor, OsConstants.F_DUPFD, above) } } @@ -66,8 +66,10 @@ open class GuestWorker : Service() { // (kill(0, sig)) would reach every process of the app. runCatching { Os.setsid() } // The native hook closes the binder fds in every other process (hook/handoff.c). - Os.execve(argv[0], argv, env.filterNot { it.startsWith("DDH_WORKER_PID=") }.toTypedArray() + - "DDH_WORKER_PID=${Process.myPid()}") + val fill = { arg: String -> arg.replace(BINDER_FD_TOKEN, binderFd.toString()) } + Os.execve(argv[0], argv.map(fill).toTypedArray(), + env.filterNot { it.startsWith("DDH_WORKER_PID=") }.map(fill).toTypedArray() + + "DDH_WORKER_PID=${Process.myPid()}") } catch (e: ErrnoException) { Log.e(TAG, "could not exec ${argv.firstOrNull()}", e) } @@ -76,24 +78,30 @@ open class GuestWorker : Service() { /** * Binder fds stay open across the exec, at [from] or above, where no fd of the program goes. - * Returns the highest fd used. + * Returns the highest fd used and the one on /dev/binder, whose closing Android takes for + * the death of this process. */ - private fun keepBinderOpen(from: Int): Int { + private fun keepBinderOpen(from: Int): Pair { var highest = from - 1 + var binder = -1 File("/proc/self/fd").listFiles()?.forEach { link -> val target = runCatching { Os.readlink(link.path) }.getOrNull() ?: return@forEach if (!target.contains("binder")) return@forEach // A copy, not adoptFd(): some of these fds are owned (fdsan) by a ParcelFileDescriptor. val copy = runCatching { ParcelFileDescriptor.fromFd(link.name.toIntOrNull() ?: return@forEach) }.getOrNull() ?: return@forEach - highest = maxOf(highest, Os.fcntlInt(copy.fileDescriptor, OsConstants.F_DUPFD, from)) + val kept = Os.fcntlInt(copy.fileDescriptor, OsConstants.F_DUPFD, from) + highest = maxOf(highest, kept) + if (target.substringAfterLast('/') == "binder") binder = kept copy.close() } - return highest + return highest to binder } companion object { const val RUN = IBinder.FIRST_CALL_TRANSACTION + /** In a program's arguments or environment, replaced by the number of its kept binder fd. */ + const val BINDER_FD_TOKEN = "@DDH_WORKER_BINDER_FD@" private const val EXEC_DELAY_MS = 100L private const val BINDER_FDS = 900 private const val MOVED_FDS = 1200 diff --git a/docs/phantom-processes.md b/docs/phantom-processes.md index 24bd88997a..20112ba2b9 100644 --- a/docs/phantom-processes.md +++ b/docs/phantom-processes.md @@ -261,10 +261,15 @@ A second round moved most of what was still counted: - **The app's own processes:** `HostProcess.startUncounted` runs a host command in a worker with the same contract as `HostProcess.start` (output lines, exit code, working directory). The session - root, the app log's logcat and the DirectAudio relay use it. A worker cannot `chdir` before its - exec, so Android's `sh` does it from an environment of its own and exports the program's - (`env -i` cannot be used: the APK's library path contains `==`). PulseAudio stays a normal child: - it was SIGKILLed right after start in a worker, for a reason not yet found. + root, the app log's logcat, PulseAudio and the DirectAudio relay use it. A worker cannot `chdir` + before its exec, so Android's `sh` does it from an environment of its own and exports the + program's (`env -i` cannot be used: the APK's library path contains `==`). +- **PulseAudio** closes every inherited fd at start, the worker's binder fd included, and Android + then killed the worker as dead (the SIGKILL came from system_server's `ActivityManager` thread, + seen with the `signal_generate` ftrace event, which shell can enable on the Thor). The daemon + spares the fd named in `PULSE_PASSED_FD`; the worker replaces `@DDH_WORKER_BINDER_FD@` in a + program's arguments and environment with its `/dev/binder` fd number, so PulseAudio is given it. + Started as a normal child the token reads as fd 0, which the daemon ignores. - **The script's long-lived subshells:** `dd_job FUNCTION` runs a function in a new `bash` that first loads the shell's variables, functions and options from a state file, so it is an exec'd program the hook can hand off (`bash+dd-job`). `$!`, `kill` and `wait` keep working. The zRAM scanner, @@ -303,8 +308,9 @@ Measured on the Thor, Bully under Proton, Android's limit on (default 32), a sca | main (no handoff) | 48 | 63 | 33, session failed | | first round | 26 | 29 | 0 | | **second round** | **9** | **14** | **0** | +| + PulseAudio in a worker | 8 | | | -Still counted at idle: PulseAudio, `run_gamescope`'s subshell, Steam, its web helper's browser and +Still counted at idle: `run_gamescope`'s subshell, Steam, its web helper's browser and two utility processes, the session bus's activation babysitter and Decky's sound-effects worker (forks without an exec). Quitting the game, 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 @@ -325,7 +331,7 @@ the old behavior and the old requirement. 4. ~~The session root, its jobs, the log pipeline, the gamescope chain~~ done: 9 idle, 14 in game. 5. ~~Setup no longer requires the developer option while the session runs in workers~~ done. 6. Open: - - PulseAudio in a worker (SIGKILLed at start there); + - ~~PulseAudio in a worker~~ done; - `/proc//stat` showing the virtual parent, so that Steam and its web helper can be handed off too; - pre-started workers, so that a handoff costs no zygote fork (about 100 ms now);