feat(session): run the session root, its jobs and the gamescope chain outside the child-process count

This commit is contained in:
Kurt Himebauch committed 2026-11-27 12:44:11 -05:00
1 parent 592d03f69a
commit bbb4d11bf0
13 files changed
+478 -71

No files matched your search

@@ -22,6 +22,8 @@ class App : Application() {
super.onCreate()
if (android.os.Build.VERSION.SDK_INT >= 28 &&
com.droiddeck.launcher.runtime.GuestWorker.isWorkerProcess(this, Application.getProcessName())) return
if (android.os.Build.VERSION.SDK_INT >= 28 && Application.getProcessName() == packageName)
com.droiddeck.launcher.runtime.GuestWorkers.attach(this)
com.droiddeck.launcher.runtime.NativeGuest.init(this)
com.droiddeck.launcher.session.Onboarding.attach(this)
Thread({ runCatching { EnhancedSessionLogShare.cleanupAbandonedStages(this) } }, "diagnostic-stage-cleanup").start()
@@ -89,7 +89,7 @@ public class DirectAudioRelayComponent extends SessionPart {
}
}
final java.io.PrintWriter log = out;
pid = HostProcess.start(command.toString(), env.toArray(new String[0]),
pid = HostProcess.startUncounted(command.toString(), env.toArray(new String[0]),
app().getFilesDir(), null,
line -> {
Log.i(TAG, line);
@@ -1,5 +1,6 @@
package com.droiddeck.launcher.core
import android.os.ParcelFileDescriptor
import android.util.Log
import java.io.File
import java.util.function.Consumer
@@ -7,6 +8,9 @@ import java.util.function.Consumer
object HostProcess {
private const val TAG = "HostProcess"
private const val LINKER = "/system/bin/linker64"
// sh -c WORKER_CD sh DIR NAME=VALUE... -- PROGRAM ARGS...
private const val WORKER_CD = "cd \"\$1\" || exit 126; shift; " +
"while [ \$# -gt 0 ] && [ \"\$1\" != -- ]; do export \"\$1\"; shift; done; shift; exec \"\$@\""
/** Android 10+ refuses to execute app data files for targetSdk 29+; its linker still loads them. */
@JvmStatic
@@ -65,6 +69,66 @@ object HostProcess {
}
}
/**
* As [start], but the program runs as the main process of one of the app's worker processes,
* which Android does not count against the app's child processes (docs/phantom-processes.md).
* Falls back to [start] when no worker is available. The exit status is the program's exit
* code, or 128 plus the signal that ended it, as Process.waitFor() reports it.
*/
@JvmStatic
@JvmOverloads
fun startUncounted(command: String, env: Array<String>?, dir: File?, onExit: Consumer<Int>?, onLine: Consumer<String>?,
outputFile: File? = null, onOutputComplete: Runnable? = null): Int {
val context = com.droiddeck.launcher.runtime.GuestWorkers.context
val argv = split(command)
if (context == null || argv.isEmpty()) return start(command, env, dir, onExit, onLine, outputFile, onOutputComplete)
val environment = LinkedHashMap(System.getenv())
env?.forEach { line ->
val eq = line.indexOf('=')
if (eq > 0) environment[line.substring(0, eq)] = line.substring(eq + 1)
}
val assignments = environment.map { (k, v) -> "$k=$v" }
// A worker cannot chdir before its exec, so Android's shell does it. The shell starts with an
// environment of its own (the program's LD_PRELOAD of glibc libraries would stop it loading)
// and exports the program's before the exec.
val launch = if (dir != null) {
listOf("/system/bin/sh", "-c", WORKER_CD, "sh", dir.path) + assignments + listOf("--") + argv
} else argv
val workerEnv = if (dir != null) listOf("PATH=/system/bin") else assignments
val input = ParcelFileDescriptor.open(File("/dev/null"), ParcelFileDescriptor.MODE_READ_ONLY)
var reader: ParcelFileDescriptor? = null
val output = when {
onLine != null -> ParcelFileDescriptor.createPipe().let { reader = it[0]; it[1] }
outputFile != null -> ParcelFileDescriptor.open(outputFile,
ParcelFileDescriptor.MODE_WRITE_ONLY or ParcelFileDescriptor.MODE_CREATE or ParcelFileDescriptor.MODE_APPEND)
else -> ParcelFileDescriptor.open(File("/dev/null"), ParcelFileDescriptor.MODE_WRITE_ONLY)
}
val pid = com.droiddeck.launcher.runtime.GuestWorkers.start(context, launch, workerEnv,
listOf(input, output, output)) { status ->
onExit?.accept(if (status < 0) -1 else if (status and 0x7f == 0) (status shr 8) and 0xff else 128 + (status and 0x7f))
}
input.close()
output.close()
if (pid <= 0) {
reader?.close()
return start(command, env, dir, onExit, onLine, outputFile, onOutputComplete)
}
Log.i(TAG, "started pid $pid outside the child-process count: ${argv.first()} (${argv.size - 1} arguments)")
reader?.let { pipe ->
Thread({
try {
ParcelFileDescriptor.AutoCloseInputStream(pipe).bufferedReader().useLines { lines ->
lines.forEach { onLine?.accept(it) }
}
} catch (e: Exception) {
} finally {
onOutputComplete?.run()
}
}, "proc-out-$pid").apply { priority = Thread.NORM_PRIORITY - 1; isDaemon = true; start() }
}
return pid
}
@JvmStatic
fun pidOf(process: Process): Int = try {
val field = process.javaClass.getDeclaredField("pid")
@@ -28,17 +28,18 @@ object SessionLogCapture {
out.flush()
writer = out
this.target = target
pid = HostProcess.start(
pid = HostProcess.startUncounted(
command(Process.myPid(), sinceMillis),
null, target.parentFile, null,
) { line ->
synchronized(this) {
try {
writer?.apply { write(line); newLine(); flush() }
} catch (e: Exception) {
onLine = { line ->
synchronized(this) {
try {
writer?.apply { write(line); newLine(); flush() }
} catch (e: Exception) {
}
}
}
}
},
)
if (pid == -1) {
out.write("logcat could not be started; this ROM may not hand an app its own entries.\n")
out.flush()
@@ -51,31 +51,45 @@ open class GuestWorker : Service() {
private fun exec(argv: Array<String>, env: Array<String>, fds: List<ParcelFileDescriptor?>, targets: IntArray) {
try {
keepBinderOpen()
val top = targets.maxOrNull() ?: 2
val binderTop = keepBinderOpen(maxOf(BINDER_FDS, top + 1))
// Out of the way first, so that placing one fd cannot overwrite another not yet placed.
val moved = fds.map { pfd -> pfd?.let { Os.fcntlInt(it.fileDescriptor, OsConstants.F_DUPFD, MOVED_FDS) } }
val above = maxOf(MOVED_FDS, top + 1, binderTop + 1)
val moved = fds.map { pfd -> pfd?.let { Os.fcntlInt(it.fileDescriptor, OsConstants.F_DUPFD, above) } }
moved.forEachIndexed { i, fd ->
if (fd == null || i >= targets.size) return@forEachIndexed
val source = ParcelFileDescriptor.adoptFd(fd).also { kept.add(it) }.fileDescriptor
Os.dup2(source, targets[i])
Os.fcntlInt(source, OsConstants.F_SETFD, OsConstants.FD_CLOEXEC)
}
Os.execve(argv[0], argv, env)
// A session of its own: in zygote's process group, a program signalling its group
// (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()}")
} catch (e: ErrnoException) {
Log.e(TAG, "could not exec ${argv.firstOrNull()}", e)
}
Process.killProcess(Process.myPid())
}
/** Binder fds stay open across the exec, above the numbers the program's fds go to. */
private fun keepBinderOpen() {
/**
* Binder fds stay open across the exec, at [from] or above, where no fd of the program goes.
* Returns the highest fd used.
*/
private fun keepBinderOpen(from: Int): Int {
var highest = from - 1
File("/proc/self/fd").listFiles()?.forEach { link ->
val target = runCatching { Os.readlink(link.path) }.getOrNull() ?: return@forEach
if (!target.contains("binder")) return@forEach
val fd = ParcelFileDescriptor.adoptFd(link.name.toIntOrNull() ?: return@forEach)
kept.add(fd)
Os.fcntlInt(fd.fileDescriptor, OsConstants.F_DUPFD, BINDER_FDS)
// 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))
copy.close()
}
return highest
}
companion object {
@@ -185,13 +199,17 @@ object GuestWorkers {
GuestWorker63::class.java,
)
private class Slot(val index: Int, val connection: ServiceConnection) {
private class Slot(val index: Int, val connection: ServiceConnection, val guest: Boolean) {
@Volatile var pid = -1
}
private val slots = arrayOfNulls<Slot>(classes.size)
@Volatile private var server: Thread? = null
/** The app, once [attach] or [serve] ran in its main process; null before. */
@Volatile var context: Context? = null
private set
fun socketName(context: Context): String = "droiddeck-spawn." + context.packageName + "." + Process.myUid()
/**
@@ -204,7 +222,11 @@ object GuestWorkers {
/** As [start], with the fds in [fds] placed at the numbers in [targets]. */
fun start(context: Context, argv: List<String>, env: List<String>, fds: List<ParcelFileDescriptor>, targets: IntArray,
onExit: (Int) -> Unit): Int {
onExit: (Int) -> Unit): Int = start(context, argv, env, fds, targets, false, onExit)
/** [guest]: one of the session's guest programs, which [guestPids] lists and [stopGuests] ends. */
private fun start(context: Context, argv: List<String>, env: List<String>, fds: List<ParcelFileDescriptor>, targets: IntArray,
guest: Boolean, onExit: (Int) -> Unit): Int {
val app = context.applicationContext
val connected = CountDownLatch(1)
var service: IBinder? = null
@@ -225,7 +247,7 @@ object GuestWorkers {
override fun onBindingDied(name: ComponentName) = release(app, index, onExit)
}
slot = Slot(index, connection)
slot = Slot(index, connection, guest)
slots[index] = slot
}
val intent = Intent(app, classes[slot.index])
@@ -277,17 +299,26 @@ object GuestWorkers {
return -1
}
/** Kills every worker's program; for session teardown. */
fun stopAll() {
val pids = synchronized(slots) { slots.mapNotNull { it?.pid?.takeIf { pid -> pid > 0 } } }
/** The pids of the session's guest programs running in workers. */
fun guestPids(): List<Int> = synchronized(slots) { slots.mapNotNull { s -> s?.takeIf { it.guest }?.pid?.takeIf { it > 0 } } }
/** Ends the session's guest programs that are still running; for session teardown. */
fun stopGuests() {
val pids = guestPids()
if (pids.isEmpty()) return
pids.forEach { Process.sendSignal(it, OsConstants.SIGTERM) }
Thread.sleep(500)
pids.forEach { Process.killProcess(it) }
}
/** Listens for `droiddeck-spawn` requests from the session's guests. */
fun attach(context: Context) {
this.context = context.applicationContext
}
fun serve(context: Context) {
val app = context.applicationContext
this.context = app
synchronized(this) {
if (server != null) return
server = Thread({
@@ -299,7 +330,13 @@ object GuestWorkers {
}
while (true) {
val client = runCatching { socket.accept() }.getOrNull() ?: continue
Thread({ handle(app, client) }, "guest-spawn").start()
Thread({
try {
handle(app, client)
} catch (e: Throwable) {
Log.w(TAG, "spawn request failed", e)
}
}, "guest-spawn").start()
}
}, "guest-spawn-server").apply { isDaemon = true; start() }
}
@@ -310,26 +347,51 @@ object GuestWorkers {
if (client.peerCredentials.uid != Process.myUid()) return
val input = DataInputStream(client.inputStream)
val payload = ByteArray(input.readInt()).also { input.readFully(it) }
val received = client.ancillaryFileDescriptors.orEmpty()
val fds = received.map { ParcelFileDescriptor.dup(it) }
received.forEach { runCatching { Os.close(it) } }
val request = JSONObject(String(payload))
val fds = ArrayList<ParcelFileDescriptor>()
fun take() {
val received = client.ancillaryFileDescriptors.orEmpty()
received.forEach { fds.add(ParcelFileDescriptor.dup(it)) }
received.forEach { runCatching { Os.close(it) } }
}
if (request.has("fdchunks")) {
// The hook sends its fds after the request, a chunk with each byte (hook/handoff.c)
val total = (request.optJSONArray("fds")?.length() ?: 0) + (if (request.has("table")) 1 else 0)
while (fds.size < total) {
if (input.read() < 0) throw java.io.EOFException("fds ended at ${fds.size} of $total")
take()
}
} else {
take()
}
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) } }
request.optJSONArray("envlist")?.let { a ->
for (i in 0 until a.length()) a.getString(i).split('=', limit = 2).let { if (it.size == 2) env[it[0]] = it[1] }
}
// What the hook told the program that asked (it may run in a worker itself) is not this one's
env.keys.removeAll { it.startsWith("DDH_") }
val cwd = request.optString("cwd", "/")
val wait = request.optBoolean("wait", false)
val file = request.optString("file", "")
// 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 }
val table = fds.getOrNull(tableIndex)
val table = fds.getOrNull(tableIndex)?.takeIf { pfd ->
runCatching { Os.readlink("/proc/self/fd/${pfd.fd}") }.getOrNull()?.contains("ddnatv-handoff") == true
}
val slot = request.optInt("slot", -1)
val parent = request.optInt("parent", -1)
val programFds = fds.filterIndexed { i, _ -> i != tableIndex }
var programFds = fds.filterIndexed { i, _ -> i != tableIndex }
var targets = numbers
if (table != null && slot >= 0) {
// The program reports its own exit status into the table (hook/handoff.c)
val tableFd = (numbers.maxOrNull() ?: 2) + 1
programFds = programFds + table
targets = numbers + tableFd
env["DDH_TABLE"] = "$tableFd:$slot:$parent"
}
val root = env["DDNATV_ROOT"]?.let(::File)
val out = client.outputStream
if (root == null || argv.isEmpty()) {
@@ -346,14 +408,18 @@ object GuestWorkers {
}
val command = NativeGuest.command(root, shell, args)
env["DDNATV_REAL_EXE"] = File(root, shell.removePrefix("/")).path
// The hook restores these in the program (tools/nativeguest/hook/handoff.c).
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()
val exited = CountDownLatch(1)
var status = -1
val pid = start(app, command, env.map { (k, v) -> "$k=$v" }, programFds, numbers) { code ->
val pid = start(app, command, env.map { (k, v) -> "$k=$v" }, programFds, targets, true) { code ->
status = code
if (table != null && slot >= 0) reportExit(table, slot, code, parent)
exited.countDown()
}
programFds.forEach { runCatching { it.close() } }
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()
@@ -371,7 +437,11 @@ object GuestWorkers {
runCatching {
val entry = 64L + slot * 32L
val word = java.nio.ByteBuffer.allocate(4).order(java.nio.ByteOrder.nativeOrder())
Os.pwrite(table.fileDescriptor, word.putInt(0, if (status < 0) 9 else status).also { it.rewind() }, entry + 16)
Os.pread(table.fileDescriptor, word, entry + 12)
if (word.getInt(0) == 2) return@runCatching // the program wrote its own status
word.clear()
// An exit Android has no record of is reported as a failure, not as a signal.
Os.pwrite(table.fileDescriptor, word.putInt(0, if (status < 0) 1 shl 8 else status).also { it.rewind() }, entry + 16)
Os.pwrite(table.fileDescriptor, word.putInt(0, 2).also { it.rewind() }, entry + 12)
if (parent > 0) Process.sendSignal(parent, OsConstants.SIGCHLD)
}.onFailure { Log.w(TAG, "could not report an exit to the guest's parent", it) }
@@ -130,6 +130,7 @@ object SessionFiles {
"usr/local/bin/steam-compatibility" to "usr/local/bin/steam-compatibility",
"usr/local/bin/droiddeck-clipboard" to "usr/local/bin/droiddeck-clipboard",
"usr/local/bin/droiddeck-spawn" to "usr/local/bin/droiddeck-spawn",
"usr/local/bin/droiddeck-log-writer" to "usr/local/bin/droiddeck-log-writer",
"usr/local/bin/droiddeck-desktop-clipboard" to "usr/local/bin/droiddeck-desktop-clipboard",
"usr/local/bin/droiddeck-gpu-window" to "usr/local/bin/droiddeck-gpu-window",
"usr/local/bin/droiddeck-steam-install" to "usr/local/bin/droiddeck-steam-install",
@@ -862,10 +862,10 @@ class SessionService : Service() {
watchLaunchRequests(sessionRoot)
watchSyncWanted(root)
EsyncPacks.fetchInBackground(this, root)
val pid = SessionEvents.step("guest-launch") { HostProcess.start(line, hostEnv.asArray(), root, { status ->
val pid = SessionEvents.step("guest-launch") { HostProcess.startUncounted(line, hostEnv.asArray(), root, { status ->
if (gen != sessionGen) {
Log.i(TAG, "an earlier session's process ended ($status); the current one carries on")
return@start
return@startUncounted
}
val program = programStatus(sessionRoot)
Log.i(TAG, "session ended: $status" + if (program != null) " (program $program)" else "")
@@ -1526,8 +1526,9 @@ class SessionService : Service() {
private fun teardown(prootPid: Int, expectedStartTime: Long? = null) {
fun stillSameProcess(): Boolean = expectedStartTime == null || readStat(prootPid)?.second == expectedStartTime
if (!stillSameProcess()) return
if (expectedStartTime == null) com.droiddeck.launcher.runtime.GuestWorkers.stopAll()
val tree = descendants(prootPid)
// The session's programs that run in worker processes are not descendants of its root.
val workers = if (expectedStartTime == null) com.droiddeck.launcher.runtime.GuestWorkers.guestPids() else emptyList()
val tree = descendants(prootPid) + workers.flatMap { pid -> listOfNotNull(readStat(pid)?.let { Pair(pid, it.second) }) + descendants(pid) }
if (!stillSameProcess()) return
saveWineRegistries(tree)
android.os.Process.sendSignal(prootPid, 15)
@@ -1544,6 +1545,7 @@ class SessionService : Service() {
killed++
}
if (killed > 0) Log.i(TAG, "swept $killed process(es) proot left behind")
if (expectedStartTime == null) com.droiddeck.launcher.runtime.GuestWorkers.stopGuests()
}
private fun clearCachedApps() {