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 4d6a1de6fb..b4843fc6df 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt @@ -395,9 +395,10 @@ object GuestWorkers { private set /** Whether the session runs its programs in workers: the native userland, and not turned off. */ - fun enabled(context: Context): Boolean = - android.os.Build.VERSION.SDK_INT >= 30 && com.droiddeck.launcher.session.SessionPrefs.childProcessHandoff(context) && - NativeGuest.enabled(context) + fun enabled(context: Context): Boolean = handoffAllowed(context) && NativeGuest.enabled(context) + + private fun handoffAllowed(context: Context): Boolean = + android.os.Build.VERSION.SDK_INT >= 30 && com.droiddeck.launcher.session.SessionPrefs.childProcessHandoff(context) /** How the running session's root was started: in a worker (true) or counted (false); null with none running. */ @Volatile var sessionRootUncounted: Boolean? = null @@ -408,7 +409,7 @@ object GuestWorkers { * the next one would start. */ fun limitHandled(context: Context): Boolean = - sessionRootUncounted ?: (NativeGuest.sessionMode != false && enabled(context)) + sessionRootUncounted ?: (NativeGuest.sessionMode != false && handoffAllowed(context) && NativeGuest.nextSession(context)) /** Whether pid is a program running in one of the workers. */ fun runsInWorker(pid: Int): Boolean = pid > 0 && synchronized(slots) { slots.any { it != null && it.started && it.pid == pid } } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt index babaae550a..95a660eaf4 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -24,6 +24,16 @@ object NativeGuest { fun enabled(context: Context): Boolean = SessionPrefs.nativeUserland(context) && available(LinuxRuntime.rootDir(context)) + /** + * Whether the next session will start natively. The session stages the hook from the APK before + * it starts, so only the setting and the rootfs's loader decide, and a runtime still to be + * installed will have one. + */ + fun nextSession(context: Context): Boolean = + SessionPrefs.nativeUserland(context) && willRun(LinuxRuntime.rootDir(context), LinuxRuntime.isInstalled(context)) + + internal fun willRun(root: File, installed: Boolean): Boolean = !installed || loader(root) != null + /** The mode the running session started in; null between sessions. */ @Volatile var sessionMode: Boolean? = null diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt index d8ea5ee185..563b09dd36 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt @@ -12,6 +12,14 @@ class NativeGuestTest { File(it, "usr/lib/ld-linux-aarch64.so.1").writeText("") } + @Test + fun aRuntimeStillToBeInstalledRunsTheNextSessionNatively() { + val empty = Files.createTempDirectory("native-root").toFile() + assertTrue(NativeGuest.willRun(empty, installed = false)) + assertEquals(false, NativeGuest.willRun(empty, installed = true)) + assertTrue(NativeGuest.willRun(rootWithLoader(), installed = true)) + } + @Test fun theSeccompSwitchNamesAHelperAndItsArgumentsInTheRootfs() { val root = Files.createTempDirectory("rootfs").toFile()