Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(status): find the QEMU PID via /proc so the VM charts work

ExTvaa9c72a14084
diff --git a/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt b/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt
index 7a8325d..c8de52e 100644
--- a/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt
+++ b/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt
@@ -82,14 +82,28 @@ class QemuEngine @Inject constructor(
override val runningSinceMs: Long? get() = _runningSinceMs
override fun emulatorRssMb(): Long? {
- val proc = process?.takeIf { it.isAlive } ?: return null
- val pid = HostMetrics.processPid(proc) ?: return null
+ val pid = emulatorPid() ?: return null
return HostMetrics.processVmRssMb(pid)
}
+ // The PID found for the current QEMU process; reset when `process` changes.
+ @Volatile
+ private var pidCache: Pair<Process, Int>? = null
+
override fun emulatorPid(): Int? {
val proc = process?.takeIf { it.isAlive } ?: return null
- return HostMetrics.processPid(proc)
+ pidCache?.let { (p, pid) -> if (p === proc) return pid }
+ // libpodroid-launcher execv()s QEMU, so both names refer to the same PID.
+ val pid = HostMetrics.childPidByExecutable(
+ android.os.Process.myPid(),
+ setOf("libpodroid-launcher.so", "libqemu-system-aarch64.so"),
+ )
+ if (pid == null) {
+ Log.w(TAG, "QEMU is running but its PID was not found in /proc")
+ return null
+ }
+ pidCache = proc to pid
+ return pid
}
@Volatile
diff --git a/app/src/main/java/com/excp/podroid/util/HostMetrics.kt b/app/src/main/java/com/excp/podroid/util/HostMetrics.kt
index 977bd58..0a1997d 100644
--- a/app/src/main/java/com/excp/podroid/util/HostMetrics.kt
+++ b/app/src/main/java/com/excp/podroid/util/HostMetrics.kt
@@ -74,11 +74,30 @@ object HostMetrics {
file.length()
}
- fun processPid(process: Process): Int? = try {
- val m = Process::class.java.getMethod("pid")
- (m.invoke(process) as Int).takeIf { it > 0 }
- } catch (_: Exception) {
- null
+ /**
+ * PID of a direct child of [parentPid] whose argv[0] file name is one of
+ * [executableNames], or null. Android's java.lang.Process has no pid()
+ * (a Java 9 API absent from libcore), so the emulator's PID is found by
+ * scanning /proc instead. Entries that vanish or are unreadable mid-scan
+ * are skipped: processes exit while we walk the directory.
+ */
+ fun childPidByExecutable(
+ parentPid: Int,
+ executableNames: Set<String>,
+ procRoot: File = File("/proc"),
+ ): Int? {
+ val entries = procRoot.listFiles() ?: return null
+ for (dir in entries) {
+ val pid = dir.name.toIntOrNull() ?: continue
+ val stat = runCatching { File(dir, "stat").readText() }.getOrNull() ?: continue
+ // "pid (comm) state ppid ..."; comm may contain spaces or ')'.
+ val ppid = stat.substringAfterLast(')').trim().split(' ').getOrNull(1)?.toIntOrNull()
+ if (ppid != parentPid) continue
+ val argv0 = runCatching { File(dir, "cmdline").readText() }.getOrNull()
+ ?.substringBefore('\u0000') ?: continue
+ if (argv0.substringAfterLast('/') in executableNames) return pid
+ }
+ return null
}
/** VmRSS from /proc/[pid]/status, in megabytes. */
diff --git a/app/src/test/java/com/excp/podroid/util/HostMetricsChildPidTest.kt b/app/src/test/java/com/excp/podroid/util/HostMetricsChildPidTest.kt
new file mode 100644
index 0000000..1768be3
--- /dev/null
+++ b/app/src/test/java/com/excp/podroid/util/HostMetricsChildPidTest.kt
@@ -0,0 +1,57 @@
+package com.excp.podroid.util
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.TemporaryFolder
+import java.io.File
+
+class HostMetricsChildPidTest {
+
+ @get:Rule val tmp = TemporaryFolder()
+
+ private val names = setOf("libpodroid-launcher.so", "libqemu-system-aarch64.so")
+
+ private fun proc(pid: Int, ppid: Int, comm: String, argv0: String) {
+ val dir = File(tmp.root, pid.toString()).apply { mkdirs() }
+ File(dir, "stat").writeText("$pid ($comm) S $ppid $pid $pid 0 -1 4194560 0 0 0 0\n")
+ File(dir, "cmdline").writeText("$argv0\u0000-m\u00002048\u0000")
+ }
+
+ @Test
+ fun `finds the qemu child after exec`() {
+ proc(100, 1, "app_process", "com.excp.podroid")
+ proc(4321, 100, "libqemu-system-", "/data/app/x/lib/arm64/libqemu-system-aarch64.so")
+ assertEquals(4321, HostMetrics.childPidByExecutable(100, names, tmp.root))
+ }
+
+ @Test
+ fun `finds the launcher before it has exec'd qemu`() {
+ proc(4321, 100, "libpodroid-laun", "/data/app/x/lib/arm64/libpodroid-launcher.so")
+ assertEquals(4321, HostMetrics.childPidByExecutable(100, names, tmp.root))
+ }
+
+ @Test
+ fun `ignores other children and other parents`() {
+ // The terminal bridge is also a child of the app; a QEMU under another
+ // parent is not ours.
+ proc(200, 100, "libpodroid-brid", "/data/app/x/lib/arm64/libpodroid-bridge.so")
+ proc(300, 999, "libqemu-system-", "/data/app/x/lib/arm64/libqemu-system-aarch64.so")
+ assertNull(HostMetrics.childPidByExecutable(100, names, tmp.root))
+ }
+
+ @Test
+ fun `comm containing spaces and parens does not break ppid parsing`() {
+ proc(4321, 100, "odd ) name", "/data/app/x/lib/arm64/libqemu-system-aarch64.so")
+ assertEquals(4321, HostMetrics.childPidByExecutable(100, names, tmp.root))
+ }
+
+ @Test
+ fun `unreadable or vanished entries are skipped`() {
+ File(tmp.root, "555").mkdirs() // no stat/cmdline: process exited mid-scan
+ File(tmp.root, "self").mkdirs()
+ proc(4321, 100, "libqemu-system-", "/data/app/x/lib/arm64/libqemu-system-aarch64.so")
+ assertEquals(4321, HostMetrics.childPidByExecutable(100, names, tmp.root))
+ }
+}