Skip to content

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

fix(qemu): kill orphan VMs via PR_SET_PDEATHSIG launcher

ExTv961cb49d2bf2
diff --git a/Dockerfile b/Dockerfile
index d2b14c7..caa2ef9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -261,6 +261,10 @@ RUN cd ${QEMU_DIR} && ./configure --cc="${CC}" --cross-prefix="${LLVM}/bin/llvm-
COPY podroid-bridge.c /tmp/podroid-bridge.c
RUN ${CC} --sysroot=${LLVM}/sysroot -target aarch64-linux-android28 -fPIE -pie -Wl,-z,max-page-size=16384 /tmp/podroid-bridge.c -o /opt/qemu-out/libpodroid-bridge.so
+# Launcher (PR_SET_PDEATHSIG wrapper for QEMU — see podroid-launcher.c)
+COPY podroid-launcher.c /tmp/podroid-launcher.c
+RUN ${CC} --sysroot=${LLVM}/sysroot -target aarch64-linux-android28 -fPIE -pie -Wl,-z,max-page-size=16384 /tmp/podroid-launcher.c -o /opt/qemu-out/libpodroid-launcher.so
+
# Soname fix
RUN cp /opt/qemu-out/bin/qemu-system-aarch64 /opt/qemu-out/libqemu-system-aarch64.so \
&& cp /opt/qemu-out/lib/libslirp.so.0 /opt/qemu-out/libslirp.so \
@@ -279,5 +283,6 @@ COPY --from=packer /output/initrd.img /initrd.img
COPY --from=qemu-builder /opt/qemu-out/libqemu-system-aarch64.so /libqemu-system-aarch64.so
COPY --from=qemu-builder /opt/qemu-out/libslirp.so /libslirp.so
COPY --from=qemu-builder /opt/qemu-out/libpodroid-bridge.so /libpodroid-bridge.so
+COPY --from=qemu-builder /opt/qemu-out/libpodroid-launcher.so /libpodroid-launcher.so
COPY --from=qemu-builder /opt/qemu-out/share/qemu/efi-virtio.rom /qemu/efi-virtio.rom
COPY --from=qemu-builder /opt/qemu-out/share/qemu/keymaps/ /qemu/keymaps/
diff --git a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
index 58df3a1..ae3b056 100644
--- a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
+++ b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
@@ -574,7 +574,16 @@ class PodroidQemu @Inject constructor(
args += userQemuExtras.split(Regex("\\s+"))
}
- return listOf(qemuExe.absolutePath) + args
+ // Wrap QEMU in podroid-launcher when available — it sets PR_SET_PDEATHSIG
+ // so QEMU dies with the app on uninstall/OOM/force-stop instead of leaking
+ // as an orphan under PPID=1. If the launcher is missing (older deploys),
+ // fall back to spawning QEMU directly.
+ val launcher = File(context.applicationInfo.nativeLibraryDir, "libpodroid-launcher.so")
+ return if (launcher.exists()) {
+ listOf(launcher.absolutePath, qemuExe.absolutePath) + args
+ } else {
+ listOf(qemuExe.absolutePath) + args
+ }
}
private fun ensureStorageImage(storageSizeGb: Int) {
diff --git a/build-all.sh b/build-all.sh
index b9c35d8..46e90c9 100755
--- a/build-all.sh
+++ b/build-all.sh
@@ -144,6 +144,7 @@ build_qemu() {
docker cp podroid-qemu-extract:/libqemu-system-aarch64.so "$JNILIBS/"
docker cp podroid-qemu-extract:/libslirp.so "$JNILIBS/"
docker cp podroid-qemu-extract:/libpodroid-bridge.so "$JNILIBS/"
+ docker cp podroid-qemu-extract:/libpodroid-launcher.so "$JNILIBS/"
docker cp podroid-qemu-extract:/qemu/efi-virtio.rom "$ASSETS/qemu/"
docker cp podroid-qemu-extract:/qemu/keymaps/. "$ASSETS/qemu/keymaps/"
docker rm podroid-qemu-extract >/dev/null
diff --git a/podroid-launcher.c b/podroid-launcher.c
new file mode 100644
index 0000000..cd313b9
--- /dev/null
+++ b/podroid-launcher.c
@@ -0,0 +1,63 @@
+/*
+ * podroid-launcher — sets PR_SET_PDEATHSIG before exec'ing QEMU.
+ *
+ * Why: ProcessBuilder.start() spawns QEMU as a child of our app process, but
+ * Linux doesn't auto-kill the child when the parent dies. So when Android
+ * uninstalls/reinstalls the APK (parent SIGKILL'd by the installer), QEMU
+ * survives as an orphan under PPID=1 and keeps consuming RAM/swap until
+ * reboot or manual kill.
+ *
+ * Fix: a tiny wrapper that calls prctl(PR_SET_PDEATHSIG, SIGKILL) before
+ * exec'ing the real QEMU binary. The kernel then guarantees the child dies
+ * with the parent, regardless of the cause (uninstall, OOM, force-stop, crash).
+ *
+ * PR_SET_PDEATHSIG is a Linux syscall stable since Linux 2.6, supported on
+ * every Android version (9 through 17+). It is NOT cleared by a regular
+ * execve, so the flag carries through into QEMU.
+ *
+ * Args:
+ * argv[0] = launcher path (set by ProcessBuilder)
+ * argv[1] = real QEMU binary path
+ * argv[2..] = QEMU arguments
+ *
+ * Build (mirrors podroid-bridge):
+ * ${CC} --sysroot=${LLVM}/sysroot -target aarch64-linux-android28 \
+ * -fPIE -pie -Wl,-z,max-page-size=16384 \
+ * podroid-launcher.c -o libpodroid-launcher.so
+ */
+
+#include <signal.h>
+#include <stdio.h>
+#include <sys/prctl.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[]) {
+ if (argc < 2) {
+ fprintf(stderr, "podroid-launcher: usage: launcher <qemu-path> [args...]\n");
+ return 2;
+ }
+
+ /* Silence stderr for safety: this process inherits the app's stderr,
+ * which on Android typically goes to /dev/null. But just in case any
+ * deployment routes it elsewhere, keep noise out of the user's view. */
+
+ /* Tell the kernel to send us SIGKILL when our parent process exits.
+ * Failure here is non-fatal — we still launch QEMU, just without the
+ * orphan-cleanup guarantee (i.e. revert to today's behavior). */
+ (void)prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
+
+ /* Race guard: between ProcessBuilder.start() and our prctl() call, the
+ * parent may already have died. If so, we've already been reparented to
+ * init (PID 1). Bail immediately so we never become an orphan ourselves. */
+ if (getppid() == 1) {
+ return 1;
+ }
+
+ /* exec the real QEMU. PR_SET_PDEATHSIG is preserved across execve()
+ * (only cleared on setuid execs, which we never do). */
+ execv(argv[1], &argv[1]);
+
+ /* execv only returns on failure. */
+ perror("podroid-launcher: execv");
+ return 127;
+}