This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-13 21:27:42 -04:00
1 parent 5fd47421f9
commit aecd2ebe1d
852 files changed
+4844 -46144

No files matched your search

@@ -11,14 +11,6 @@ import com.droiddeck.launcher.core.FileUtils
import java.io.File
import java.io.RandomAccessFile
/**
* AppImages the user brings: picked from storage, checked, and extracted once into the runtime at
* /opt/appimages/user/<id>/app. proot has no FUSE to mount an image, and extracting it at every
* start would cost its whole size in /tmp each time. Each one gets a menu entry on the Linux
* desktop, and the front end starts it under gamescope through droiddeck-appimage-run.
*
* <id>/name holds the name to show, <id>/icon.png the icon when the image has a PNG one.
*/
object AppImageManager {
private const val TAG = "AppImageManager"
const val GUEST_DIR = "/opt/appimages/user"
@@ -29,7 +21,6 @@ object AppImageManager {
val guestDir: String get() = "$GUEST_DIR/$id"
}
/** Why a file cannot be imported: [text] with its [args]. */
internal class Problem(@StringRes val text: Int, vararg val args: Any) {
fun describe(context: Context): String = context.getString(text, *args)
}
@@ -47,10 +38,6 @@ object AppImageManager {
)
}?.sortedBy { it.name.lowercase() } ?: emptyList()
/**
* What is wrong with [file] as an AppImage for this device, or null when it can be imported.
* An AppImage is an ELF program (its runtime) with "AI" and the format version at byte 8.
*/
internal fun problem(file: File): Problem? {
if (!file.isFile) return Problem(R.string.user_apps_file_gone)
val head = ByteArray(20)
@@ -71,7 +58,6 @@ object AppImageManager {
}
}
/** A directory name from the image's file name: "Some_App-1.2-aarch64.AppImage" -> "some-app-1-2-aarch64". */
internal fun idFor(fileName: String, taken: Set<String>): String {
val base = fileName.removeSuffix(".AppImage").removeSuffix(".appimage").lowercase()
.replace(Regex("[^a-z0-9]+"), "-").trim('-').take(48).ifEmpty { "appimage" }
@@ -83,17 +69,12 @@ object AppImageManager {
fun dir(context: Context, id: String) = File(root(context), id)
/**
* Imports [file]; null on success, else what went wrong. [name] and [icon] replace the image's own;
* each of [meta] is written as a file beside them.
*/
fun import(
context: Context, file: File, name: String?, icon: File?, meta: Map<String, String> = emptyMap(), onProgress: (String) -> Unit,
): String? {
if (!LinuxRuntime.isInstalled(context)) return context.getString(R.string.user_apps_runtime_required)
problem(file)?.let { return it.describe(context) }
val base = root(context).apply { mkdirs() }
// Room for the image while it is extracted, and the extracted tree (about the same again).
if (StatFs(base.path).availableBytes < file.length() * 3) {
return context.getString(R.string.appimg_no_space_import, FileUtils.sizeToString(file.length() * 3))
}
@@ -114,7 +95,6 @@ object AppImageManager {
}
}
/** Swaps the program of imported [id] for [file], keeping its name and icon; the old one stays if this fails. */
fun replace(context: Context, id: String, file: File, meta: Map<String, String>, onProgress: (String) -> Unit): String? {
problem(file)?.let { return it.describe(context) }
val dir = dir(context, id)
@@ -134,14 +114,11 @@ object AppImageManager {
}
}
/** Extracts [file] to [dir]/app, replacing what is there only once the extraction has worked. */
private fun extract(context: Context, dir: File, file: File, onProgress: (String) -> Unit): String? {
val image = File(dir, "image.AppImage")
val links = File(dir, ".links").apply { mkdirs() }
try {
dir.mkdirs()
// Copied in, not run where it lies: shared storage is mounted noexec, and proot's loader
// maps a program executable.
onProgress(context.getString(R.string.user_apps_copying, file.name))
file.inputStream().use { input -> image.outputStream().use { FileUtils.copy(input, it) } }
image.setExecutable(true, false)
@@ -149,8 +126,6 @@ object AppImageManager {
val unpack = if (LinuxFex.isX86(LinuxFex.elfMachine(image))) "${LinuxFex.TOOL} extract ./image.AppImage"
else "./image.AppImage --appimage-extract || { rm -rf squashfs-root AppDir; ${LinuxFex.TOOL} extract ./image.AppImage; }"
val out = StringBuilder()
// uruntime's DwarFS images unpack into AppDir with squashfs-root a link to it, and keep
// one program under several names as hard links (sharun), which Android denies apps.
val status = GuestCommand.run(context, listOf(
"/bin/bash", "-c",
"cd \"$1\" && rm -rf squashfs-root AppDir && { $unpack; } >/dev/null && " +
@@ -170,10 +145,6 @@ object AppImageManager {
}
}
/**
* Each name proot's link2symlink left for a hard link becomes a file of its own: a program run
* through a link would see the shared file's name, and sharun picks what to start by its own.
*/
private fun copyLinkedFiles(app: File, links: File) {
val roots = setOf(links.path + "/", links.canonicalPath + "/")
app.walkTopDown().onEnter { !Files.isLink(it) }.forEach { f ->
@@ -187,7 +158,6 @@ object AppImageManager {
}
}
/** Name, comment and icon from the image's own desktop entry; the file name when it has none. */
private fun describe(dir: File, source: File) {
val app = File(dir, "app")
val entry = app.listFiles { f -> f.isFile && f.name.endsWith(".desktop") }?.firstOrNull()
@@ -200,11 +170,6 @@ object AppImageManager {
restoreIcon(dir)
}
/**
* The image's own icon as [dir]'s: of the files its entry names, beside it or anywhere in its
* icon theme, a PNG of at least [SHARP_ICON] px, else the SVG, else the largest PNG; .DirIcon
* without any.
*/
internal fun restoreIcon(dir: File) {
val app = File(dir, "app")
val entry = app.listFiles { f -> f.isFile && f.name.endsWith(".desktop") }?.firstOrNull()
@@ -229,7 +194,6 @@ object AppImageManager {
f.inputStream().use { val b = ByteArray(4); it.read(b) == 4 && b[1] == 'P'.code.toByte() && b[2] == 'N'.code.toByte() && b[3] == 'G'.code.toByte() }
} catch (e: Exception) { false }
/** A key of a desktop entry's main group, unlocalised. */
internal fun desktopKey(desktop: String, key: String): String? {
var inMain = false
for (line in desktop.lineSequence()) {
@@ -241,11 +205,8 @@ object AppImageManager {
}
private fun menuEntry(context: Context, id: String) =
// A system directory, not ~/.local/share/applications: the desktop copies game entries from
// here into the user's with a GPU launcher (droiddeck-desktop-gpu), since it composites in software.
File(LinuxRuntime.rootDir(context), "usr/local/share/applications/droiddeck-appimage-$id.desktop")
/** The Linux desktop's menu entry: the desktop's own Exec rules (and droiddeck-gpu) apply to it. */
internal fun writeMenuEntry(context: Context, dir: File) {
val id = dir.name
val name = FileUtils.readString(File(dir, "name"))?.trim() ?: id
@@ -256,7 +217,6 @@ object AppImageManager {
"Terminal=false\nCategories=$categories\nX-DroidDeck-AppImage=$id\n")
}
/** The desktop's import of [key]; older ones of the image at [path], since replaced, are removed. */
fun desktopImport(context: Context, key: String, path: String): String? {
var found: String? = null
root(context).listFiles()?.forEach { dir ->
@@ -18,26 +18,12 @@ import java.io.IOException
import java.nio.file.Files
import java.util.concurrent.atomic.AtomicInteger
/**
* Flatpak's sandboxes, started for the runtime's bwrap stand-in (droiddeck-bwrap).
*
* A Flatpak app runs with its runtime as /usr and itself as /app, which bwrap arranges with mount
* namespaces Android gives no app. proot can show the same picture, and started here - beside the
* session's proot rather than inside it - it traces the app once instead of twice. The stand-in
* sends the sandbox Flatpak asked for (its binds, directories, symlinks and files, in guest paths),
* this builds it under the cache and starts it, then streams the output, the pid and the exit
* status back until one side goes away: the stand-in ending takes the sandbox down with it.
*
* The socket is abstract, so anything on the device could connect: only this app's own uid - the
* uid every program in the runtime already runs as - is served.
*/
object BwrapSpawner {
private const val TAG = "BwrapSpawner"
fun socket(context: Context): String = "${context.packageName}.bwrap"
private val seq = AtomicInteger()
@Volatile private var server: LocalServerSocket? = null
/** Listens for sandboxes for the rest of the process's life; later calls do nothing. */
@Synchronized
fun start(context: Context) {
if (server != null) return
@@ -89,7 +75,6 @@ object BwrapSpawner {
}
}
/** What proot is started with: the sandbox root, its binds (host to guest), and the program. */
class Plan(val root: File, val binds: List<Bind>, val cwd: String, val argv: List<String>, val env: Map<String, String>)
data class Bind(val host: String, val guest: String) {
@@ -103,14 +88,9 @@ object BwrapSpawner {
}
}
/** True when [path] is [dir] or inside it. */
internal fun under(path: String, dir: String): Boolean =
dir == "/" || path == dir || path.startsWith(if (dir.endsWith("/")) dir else "$dir/")
/**
* Where a path the session's programs see lives on the host: through the session's own binds
* (the most specific one), or else inside the rootfs.
*/
internal fun hostPath(sessionBinds: List<Bind>, rootfs: String, guest: String): String {
val bind = sessionBinds.filter { under(guest, it.guest) }.maxByOrNull { it.guest.length }
?: return rootfs.trimEnd('/') + guest
@@ -118,11 +98,6 @@ object BwrapSpawner {
return if (rest.isEmpty()) bind.host else bind.host.trimEnd('/') + "/" + rest
}
/**
* Guest [src] bound at [dest]: the bind itself, then every session bind below [src] moved
* under [dest] - /dev brings the session's /dev/shm and GPU node, /proc its stand-ins for what
* Android keeps apps from reading.
*/
internal fun bindsFor(sessionBinds: List<Bind>, rootfs: String, src: String, dest: String): List<Bind> {
val out = ArrayList<Bind>()
out.add(Bind(hostPath(sessionBinds, rootfs, src), dest))
@@ -146,12 +121,6 @@ object BwrapSpawner {
}
fun coveringBind(dest: String) = binds.filter { under(dest, it.guest) }.maxByOrNull { it.guest.length }
fun newScratch(): File = File(dir, "x${scratch++}")
/**
* Where [dest] can be created on the host: inside the sandbox root, or inside a directory
* of ours bound into it (a --tmpfs under the runtime's /usr, which Flatpak fills with the
* GL extension's links). Null inside anything else bound in - the runtime itself, a host
* directory - which must not be written to.
*/
fun writable(dest: String): File? {
val cover = coveringBind(dest) ?: return inRoot(root, dest)
if (!under(cover.host, dir.path)) return null
@@ -199,8 +168,6 @@ object BwrapSpawner {
link.parentFile?.mkdirs()
if (!Files.isSymbolicLink(link.toPath()) && !link.exists()) Files.createSymbolicLink(link.toPath(), java.nio.file.Paths.get(target))
} else {
// A link inside the runtime's own tree cannot be made; what it points at is
// bound in its place.
val abs = if (target.startsWith("/")) target else normalize(dest.substringBeforeLast('/') + "/" + target)
val host = hostPath(binds, root.path, abs)
if (File(host).exists()) addBind(Bind(host, dest)) else Log.w(TAG, "symlink $dest -> $target: nothing there; left out")
@@ -218,24 +185,13 @@ object BwrapSpawner {
return Plan(root, binds, request.optString("cwd", "/").ifEmpty { "/" }, argv, env)
}
/** Where a sandbox finds the runtime's own GPU driver. */
private const val GPU_DIR = "/run/droiddeck-gpu"
/**
* The GPU, for an app that is given /dev. Flathub's Mesa has Turnip, but built for the DRM
* kernel driver: Adreno on Android is KGSL, so apps fell back to drawing on the CPU. The
* runtime's own Turnip has the KGSL backend and needs nothing the Freedesktop runtime lacks
* but three libraries, which come with it; Mesa's GL then runs on it through Zink. An app's
* own choice of driver (VK_ICD_FILENAMES, MESA_LOADER_DRIVER_OVERRIDE) is left alone.
*/
private fun gpu(rootfs: String, root: File, env: MutableMap<String, String>): List<Bind>? {
val lib = File(rootfs, "usr/lib")
val driver = File(lib, "libvulkan_freedreno.so")
if (!driver.isFile || !File("/dev/kgsl-3d0").exists()) return null
if (env.containsKey("VK_ICD_FILENAMES") || env.containsKey("VK_DRIVER_FILES")) return null
// A window on the desktop gets shared-memory buffers only (KWin composites in software);
// Zink cannot present there, so such an app keeps Flathub's own Mesa (llvmpipe). From the
// desktop, games go through droiddeck-gpu's gamescope and do get the GPU.
if (env["BL_DESKTOP"] == "1" && env["GAMESCOPE_WAYLAND_DISPLAY"] == null) return null
val binds = ArrayList<Bind>()
for (name in listOf("libvulkan_freedreno.so", "libdisplay-info.so.3", "libSPIRV-Tools.so", "libSPIRV-Tools-opt.so")) {
@@ -243,7 +199,6 @@ object BwrapSpawner {
if (!f.exists()) { Log.w(TAG, "gpu: the runtime has no $name; apps draw on the CPU"); return null }
binds.add(Bind(f.canonicalPath, "$GPU_DIR/$name"))
}
// Beside the sandbox root, in the sandbox's own directory: removed with it.
val icd = File(root.parentFile, "freedreno_icd.json")
icd.writeText("{\"file_format_version\": \"1.0.0\", \"ICD\": {\"library_path\": \"$GPU_DIR/libvulkan_freedreno.so\", \"api_version\": \"1.4.0\"}}\n")
binds.add(Bind(icd.path, "$GPU_DIR/freedreno_icd.json"))
@@ -253,18 +208,10 @@ object BwrapSpawner {
env["MESA_LOADER_DRIVER_OVERRIDE"] = "zink"
env["GALLIUM_DRIVER"] = "zink"
}
// The driver's own libraries, found beside it; nothing else is in that directory.
env["LD_LIBRARY_PATH"] = listOf(env["LD_LIBRARY_PATH"], GPU_DIR).filter { !it.isNullOrEmpty() }.joinToString(":")
return binds
}
/**
* What every program in the session preloads (SessionFiles writes /etc/ld.so.preload): the
* session shim - SysV shared memory for X11, the network calls Android refuses - and the
* controller reader. A sandbox's /etc is the runtime's, so the same libraries go in through
* LD_PRELOAD instead, with the directory the controllers' rings live in; without them an app
* saw no gamepad. Both are built against an older glibc than any Flathub runtime has.
*/
private fun preload(session: List<Bind>, rootfs: String, env: MutableMap<String, String>): List<Bind> {
val libs = FileUtils.readString(File(rootfs, "etc/ld.so.preload"))?.lines()
?.map { it.trim() }?.filter { it.startsWith("/") && File(rootfs + it).isFile } ?: return emptyList()
@@ -272,9 +219,6 @@ object BwrapSpawner {
val binds = ArrayList<Bind>()
val paths = libs.map { lib -> "$PRELOAD_DIR/${lib.substringAfterLast('/')}".also { binds.add(Bind(rootfs + lib, it)) } }
env["LD_PRELOAD"] = (paths + listOfNotNull(env["LD_PRELOAD"]?.takeIf { it.isNotBlank() })).joinToString(":")
// Not through ZYPAK_LD_PRELOAD to Chromium's children: the session shim's stat hooks then
// hide the setuid bit zypak fakes on chrome-sandbox, and Chromium refuses to start.
// The controllers: FAKE_EVDEV_DIR is <session>/dev/input, the rings beside it in <session>/dev.
env["FAKE_EVDEV_DIR"]?.let { File(it).parent }?.let { dev ->
binds.addAll(bindsFor(session, rootfs, dev, dev))
}
@@ -283,45 +227,22 @@ object BwrapSpawner {
private const val PRELOAD_DIR = "/run/droiddeck-preload"
/**
* Browsers sandbox their own children, and neither way works in here. Firefox wants
* namespaces and seccomp from the kernel; its content processes died without them (the
* desktop sets the same for the runtime's own Firefox). Chromium and Electron apps start
* their zygote through Flatpak's portal (zypak's spawn strategy), and the portal refuses:
* it knows a Flatpak by /proc/<pid>/root/.flatpak-info, and a proot sandbox's root is the
* host's. zypak's older strategy mimics the zygote inside the app's own sandbox instead.
*/
private fun browserSandboxes(env: MutableMap<String, String>) {
env.putIfAbsent("ZYPAK_ZYGOTE_STRATEGY_SPAWN", "0")
// Qt WebEngine is Chromium without zypak: it sets up its own namespace sandbox.
env.putIfAbsent("QTWEBENGINE_DISABLE_SANDBOX", "1")
// Chromium on Wayland asks the render node the compositor names for its DRM version;
// KGSL has none, and the GPU process aborts until Chromium gives up ("GPU process isn't
// usable"). Electron picks Wayland from XDG_SESSION_TYPE; with an X server there (every
// session has Xwayland) it stays on X11, which works. GTK and Qt go by their own settings.
if (env.containsKey("DISPLAY")) env["XDG_SESSION_TYPE"] = "x11"
for (name in listOf("MOZ_DISABLE_CONTENT_SANDBOX", "MOZ_DISABLE_GMP_SANDBOX", "MOZ_DISABLE_RDD_SANDBOX",
"MOZ_DISABLE_SOCKET_PROCESS_SANDBOX", "MOZ_DISABLE_UTILITY_SANDBOX",
// Android 16 refuses the size seal Firefox puts on a tab's shared
// memory, and the tab then dies on every page (droiddeck-desktop).
"MOZ_SHM_NO_SEALS")) {
env.putIfAbsent(name, "1")
}
}
/**
* /proc/cpuinfo without the cores' names. Snapdragon's ARMv9 cores (Cortex-A510/A715/X3) have
* SVE2 by architecture, and LLVM - llvmpipe's JIT in Flathub's Mesa, which is what apps draw
* with here - takes the CPU part to mean it may use it. Qualcomm leaves SVE off, so the first
* shader compiled killed the app with SIGILL. Without a part number LLVM targets generic
* ARMv8 plus the "Features" line, which lists what the kernel really enabled.
*/
private fun genericCpuinfo(context: Context): File? = try {
val out = File(context.cacheDir, "cpuinfo-generic")
val text = File("/proc/cpuinfo").readLines()
.filterNot { it.startsWith("CPU implementer") || it.startsWith("CPU part") || it.startsWith("CPU variant") || it.startsWith("CPU revision") }
.joinToString("\n", postfix = "\n")
// Sandboxes start side by side; one must never read another's half-written copy.
if (!out.isFile || out.readText() != text) {
val staged = File.createTempFile("cpuinfo", null, context.cacheDir)
staged.writeText(text)
@@ -332,7 +253,6 @@ object BwrapSpawner {
Log.w(TAG, "cpuinfo", e); null
}
/** [path] with "." and ".." resolved lexically. */
internal fun normalize(path: String): String {
val out = ArrayList<String>()
for (part in path.split('/')) when (part) {
@@ -343,10 +263,6 @@ object BwrapSpawner {
return "/" + out.joinToString("/")
}
/**
* [guest] inside the sandbox root, or null when the way there crosses a symlink: one of
* Flatpak's (var/run -> ../run) may point anywhere once followed on the host.
*/
private fun inRoot(root: File, guest: String): File? {
var f = root
for (part in guest.split('/').filter { it.isNotEmpty() }) {
@@ -358,17 +274,14 @@ object BwrapSpawner {
}
private fun chmod(f: File, mode: Int) {
try { Os.chmod(f.path, mode) } catch (e: Exception) { /* the default mode serves */ }
try { Os.chmod(f.path, mode) } catch (e: Exception) { }
}
private fun run(context: Context, plan: Plan, client: LocalSocket, out: DataOutputStream, id: Int) {
val cmd = LinuxRuntime.prootPrefix(context, plan.root, plan.cwd)
// DROIDDECK_PROOT_VERBOSE=N in the app's environment (flatpak run --env=...) has proot
// say what it does, to logcat in full: for a sandbox that dies without a word.
val verbose = plan.env["DROIDDECK_PROOT_VERBOSE"]?.toIntOrNull()
if (verbose != null) { cmd.add(1, "-v"); cmd.add(2, verbose.toString()) }
plan.binds.forEach { cmd.add("-b"); cmd.add(it.spec) }
// proot hands its own environment on; the sandbox gets exactly what Flatpak asked for.
cmd.add("/usr/bin/env")
cmd.add("-i")
plan.env.forEach { (k, v) -> cmd.add("$k=$v") }
@@ -383,23 +296,18 @@ object BwrapSpawner {
}
val process = builder.start()
val pid = HostProcess.pidOf(process)
// A sandbox lives exactly as long as its bwrap stand-in, which ends it; a session starting
// meanwhile (OrphanReaper) must not take it - an install's extra-data step, say.
com.droiddeck.launcher.session.OrphanReaper.keep(pid)
Log.i(TAG, "sandbox $id pid $pid: ${plan.argv.joinToString(" ")} (${plan.binds.size} binds)")
synchronized(out) { frame(out, 'P', pid.toString().toByteArray()) }
// The stand-in going away (Flatpak killed, the session over) ends the sandbox.
Thread({
try { while (client.inputStream.read() >= 0) { /* nothing is sent after the request */ } }
catch (e: IOException) { /* closed */ }
try { while (client.inputStream.read() >= 0) { } }
catch (e: IOException) { }
if (process.isAlive) {
Log.i(TAG, "sandbox $id: its bwrap is gone; stopping it")
process.destroy()
}
}, "bwrap-$id-watch").apply { isDaemon = true; start() }
val buffer = ByteArray(16384)
// The start of what the sandbox says goes to logcat too: a sandbox that fails at once
// (proot refusing a bind, a missing library) says why there even when nothing reads it.
var logged = 0
process.inputStream.use { stream ->
while (true) {
@@ -16,7 +16,6 @@ import java.util.zip.ZipInputStream
import org.json.JSONArray
import org.json.JSONObject
/** Optional Decky Loader installation for the ARM64 Linux guest. */
object DeckyManager {
private const val TAG = "DeckyManager"
const val REPOSITORY = "Droid-Deck/decky-loader"
@@ -25,7 +24,6 @@ object DeckyManager {
private const val VERSION = "root/homebrew/services/.droiddeck-decky-version"
private const val CEF_REMOTE_DEBUG_MARKER = "root/.local/share/Steam/.cef-enable-remote-debugging"
/** True while a plugin's remote binary downloads: what the UI branches on, not the stage's words. */
@Volatile var downloadingBinary = false
private set
@@ -41,7 +39,6 @@ object DeckyManager {
fun loader(context: Context) = File(LinuxRuntime.rootDir(context), PATH)
fun supervisorEnabled(context: Context) = File(LinuxRuntime.rootDir(context), "root/.droiddeck-decky-enabled").isFile
/** Remove the legacy install-time debugger marker unless the user opted into Decky. */
fun syncCefMarker(context: Context) {
val marker = File(LinuxRuntime.rootDir(context), CEF_REMOTE_DEBUG_MARKER)
if (!supervisorEnabled(context)) {
@@ -105,7 +102,6 @@ object DeckyManager {
} catch (e: Exception) { Log.w(TAG, "release metadata", e); ReleaseChannels(emptyList(), emptyList()) }
}
/** Downloads to a sibling temp file and atomically renames only after size, digest and ELF checks. */
fun install(context: Context, release: Release, progress: (String, Int) -> Unit): String? {
if (SessionState.running) return context.getString(R.string.deckymgr_stop_session_loader)
val target = loader(context)
@@ -124,7 +120,6 @@ object DeckyManager {
if (!validElf(temp, release.machine)) { temp.delete(); return context.getString(R.string.deckymgr_not_elf) }
if (SessionState.running) { temp.delete(); return context.getString(R.string.deckymgr_session_started_loader) }
if (!temp.setExecutable(true, false)) { temp.delete(); return context.getString(R.string.deckymgr_loader_not_executable) }
// Decky needs Steam's local CEF debugger only when its session supervisor is enabled.
val cefMarker = File(LinuxRuntime.rootDir(context), CEF_REMOTE_DEBUG_MARKER)
val cefReady = !supervisorEnabled(context) || cefMarker.isFile || runCatching {
cefMarker.parentFile?.mkdirs()
@@ -149,7 +144,6 @@ object DeckyManager {
return null
}
/** Imports a Decky distribution ZIP into homebrew/plugins for the next Steam session. */
fun installPluginZip(context: Context, archive: File, progress: (String, Int) -> Unit): String? {
if (SessionState.running) return context.getString(R.string.deckymgr_stop_session_plugin)
if (installed(context) == null) return context.getString(R.string.deckymgr_loader_required)
@@ -216,8 +210,6 @@ object DeckyManager {
out.write(buffer, 0, read)
}
}
// Decky distributions place plugin executables under bin/. Java's ZIP reader
// does not expose Unix mode bits, so restore execution on those payload files.
if (relative.split('/').dropLast(1).any { it == "bin" } && !output.setExecutable(true, false)) {
throw IllegalStateException(context.getString(R.string.deckymgr_bundled_not_executable))
}
@@ -6,9 +6,7 @@ import java.io.File
import org.json.JSONArray
import org.json.JSONObject
/** Offline equivalent of Decky's identity-based replacement and loader settings update. */
internal object DeckyPluginInstall {
/** A failure the user is told about: [text] is its string resource. */
class Failure(@StringRes val text: Int) : IllegalStateException()
fun activate(staged: File, plugins: File, name: String, settingsFile: File, backupSuffix: String) {
@@ -26,7 +24,6 @@ internal object DeckyPluginInstall {
.distinct())
if ((0 until order.length()).none { order.optString(it) == name }) order.put(name)
settings.put("pluginOrder", order)
// Decky's reinstall removes these flags while retaining the plugin's order and data.
for (key in listOf("frozenPlugins", "hiddenPlugins", "disabled_plugins")) {
val values = settings.optJSONArray(key) ?: continue
settings.put(key, JSONArray((0 until values.length()).map { values.get(it) }.filter { it != name }))
@@ -10,28 +10,15 @@ import com.droiddeck.launcher.core.FileUtils
import org.json.JSONObject
import java.io.File
/**
* The hosted packages that go into the runtime on request - the desktop, the emulators, and the
* builds we mirror or make ourselves - described by desktop.json beside the runtime's own catalog.
*
* Two kinds. A `tar` extracts over the rootfs like the runtime itself (the same extractor, links
* and modes preserved). An `appimage` is one file dropped in /opt/appimages with a .desktop entry
* written for it; proot has no FUSE, so it runs extracted (APPIMAGE_EXTRACT_AND_RUN).
*/
object DesktopCatalog {
private const val TAG = "DesktopCatalog"
const val CATALOG_URL = "https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/main/desktop.json"
/**
* Valve's Proton Experimental (ARM64) with its appmanifest, laid out as the client keeps it.
* In a catalog of its own so it never shows among the packages a user picks from.
*/
const val STEAM_SEED_URL = "https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/main/steam-seed.json"
const val PROTON_SEED_ID = "proton-arm64"
class Entry(
val id: String, val name: String, val tier: Int, val version: String, val kind: String,
val url: String, val sha256: String, val size: Long, val notes: String,
/** For an appimage: the icon name and menu category of its .desktop entry. */
val icon: String, val category: String,
)
@@ -40,8 +27,6 @@ object DesktopCatalog {
return try {
val json = JSONObject(body)
val arr = json.getJSONArray("packages")
// A package built per memory page size (ARMSX2: 4K and 16K kernels) names the others
// under "pages"; the device's own size picks one, and the top-level file is the 4K one.
val pageSize = try {
android.system.Os.sysconf(android.system.OsConstants._SC_PAGESIZE).toString()
} catch (e: Exception) { "4096" }
@@ -62,42 +47,21 @@ object DesktopCatalog {
private fun marker(context: Context, id: String) = File(LinuxRuntime.rootDir(context), ".droiddeck-pkg-$id")
/** The installed version of a package, or null. */
fun installed(context: Context, id: String): String? =
FileUtils.readString(marker(context, id))?.trim()?.takeIf { it.isNotEmpty() }
/**
* True until the ARM64 Proton has been placed once. Its appmanifest counts too, so an install
* the client fetched itself is never overwritten; after the first placement the client owns it
* and updates it like any other app, and a user who removes it is not given it back.
*/
fun protonSeedNeeded(context: Context): Boolean =
installed(context, PROTON_SEED_ID) == null &&
!File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps/appmanifest_4427310.acf").isFile
/**
* The desktop: KDE Plasma (tools/desktop-kde). It replaced an LXQt/labwc package of id
* "desktop", which a runtime may still carry; that one does not count, so the first desktop
* after the update installs this one over it.
*/
const val DESKTOP_ID = "desktop-kde"
/**
* The desktop package this apk installs: pinned in tools/desktop-kde/release.env and published
* in DroidDeck-Components, not read from the catalog, so the desktop scripts staged by this
* apk always get the package they were made for.
*/
fun desktopEntry(context: Context) = Entry(
DESKTOP_ID, context.getString(R.string.content_linux_desktop), 1, BuildConfig.DESKTOP_KDE_TAG, "tar",
BuildConfig.DESKTOP_KDE_URL, BuildConfig.DESKTOP_KDE_SHA256, BuildConfig.DESKTOP_KDE_SIZE, "",
"", "",
)
// The desktop package installed to the end: KWin is there and the marker install() writes once
// the whole package is extracted. An install cut short (the app killed, storage full) leaves
// KWin without the marker, and the next desktop installs it again. A newer pinned package also
// replaces the installed one, keeping the user's home and settings. The launcher is staged by
// the app at every session, so it cannot tell. SessionFiles uses the same test.
fun desktopInstalled(context: Context): Boolean = desktopInstalled(LinuxRuntime.rootDir(context))
fun desktopInstalled(root: File): Boolean =
@@ -110,7 +74,6 @@ object DesktopCatalog {
return File(root, DesktopRemoval.PENDING).exists() || marker(context, DESKTOP_ID).exists()
}
/** The installed desktop's size on disk; off the main thread. */
fun desktopSize(context: Context): Long = DesktopRemoval.installedSize(context)
fun cleanLegacy(context: Context, listener: LinuxRuntimeInstaller.ProgressListener? = null) =
@@ -119,11 +82,9 @@ object DesktopCatalog {
fun removeDesktop(context: Context, listener: LinuxRuntimeInstaller.ProgressListener): String? =
DesktopRemoval.remove(context, listener)
/** Downloads, verifies and installs one package. Returns null on success, else a message. */
fun install(context: Context, entry: Entry, listener: LinuxRuntimeInstaller.ProgressListener?): String? {
val root = LinuxRuntime.rootDir(context)
if (!root.isDirectory) return context.getString(R.string.deskpkg_runtime_missing)
// Every catalog row carries a sha256; one without is refused rather than trusted.
if (entry.sha256.isEmpty()) return context.getString(R.string.deskpkg_no_checksum, entry.name)
val download = File(context.cacheDir, "pkg-${entry.id}.download")
try {
@@ -141,7 +102,6 @@ object DesktopCatalog {
"appimage" -> {
val dir = File(root, "opt/appimages").apply { mkdirs() }
val target = File(dir, "${entry.id}.AppImage")
// Some projects zip the AppImage (melonDS); the one file inside is what we want.
val placed = if (entry.url.endsWith(".zip", ignoreCase = true)) unzipAppImage(download, target)
else download.renameTo(target)
if (!placed) return context.getString(R.string.deskpkg_place_failed)
@@ -152,8 +112,6 @@ object DesktopCatalog {
"Icon=${entry.icon}\nTerminal=false\nCategories=${entry.category};\n")
}
else -> {
// Not complete until written again at the end: a reinstall cut short must not
// leave the last install's marker saying it is.
marker(context, entry.id).delete()
if (!LinuxRuntimeInstaller.extract(context, download, root, listener)) return context.getString(R.string.deskpkg_extract_failed)
}
@@ -192,10 +150,6 @@ object DesktopCatalog {
return false
}
/**
* Removes an appimage package outright. A tar package is spread through the rootfs and is
* not tracked file by file; only its marker is dropped, which offers it again.
*/
fun remove(context: Context, entry: Entry) {
val root = LinuxRuntime.rootDir(context)
if (entry.kind == "appimage") {
@@ -7,7 +7,6 @@ import java.nio.file.LinkOption.NOFOLLOW_LINKS
import java.nio.file.attribute.BasicFileAttributes
import java.security.MessageDigest
/** Removes only unchanged, recorded package files. Never visits home or follows a symlink. */
internal object DesktopFileRemoval {
data class Entry(val kind: String, val size: Long, val identity: String, val path: String) {
companion object {
@@ -37,14 +36,13 @@ internal object DesktopFileRemoval {
else -> attrs.isRegularFile && attrs.size() == entry.size && sha(file, buffer) == entry.identity
}
if (!matches) { kept++; continue }
Files.delete(file.toPath()) // A failure propagates; the pending marker makes retry possible.
Files.delete(file.toPath())
removed++
bytes += if (attrs.isRegularFile) attrs.size() else 0
var parent = file.parentFile
while (parent != root && parent != null) { directories.add(parent); parent = parent.parentFile }
if (index % 200 == 0) progress(if (total > 0) index * 99 / total else -1)
}
// Only now-empty parents of removed files, never a recursive delete of a package directory.
for (dir in directories.sortedByDescending { it.path.length }) {
if (dir.list()?.isEmpty() == true && !dir.delete()) throw IOException("Cannot remove $dir")
}
@@ -56,7 +54,6 @@ internal object DesktopFileRemoval {
if (Files.isSymbolicLink(root.toPath())) return null
val parts = name.split('/')
if (parts.firstOrNull() !in setOf("usr", "etc") || parts.any { it.isEmpty() || it == "." || it == ".." }) return null
// Even an in-root symlink in a parent can redirect deletion into a user's data.
var parent = root
for (part in parts.dropLast(1)) {
parent = File(parent, part)
@@ -76,7 +73,6 @@ internal object DesktopFileRemoval {
}
}
/** The base runtime's pacman database is still present: desktop archives never replace it. */
fun runtimeFiles(root: File): Set<String> = buildSet {
val database = File(root, "var/lib/pacman/local")
if (!database.isDirectory || Files.isSymbolicLink(database.toPath())) throw IOException("Runtime package database missing")
@@ -7,7 +7,6 @@ import com.droiddeck.launcher.core.FileUtils
import java.io.File
import java.util.zip.GZIPInputStream
/** Inventories are generated from pinned archives by tools/desktop-kde/removal-manifests.py. */
internal object DesktopRemoval {
const val PENDING = ".droiddeck-desktop-removing"
private const val CLEANING = ".droiddeck-lxqt-cleaning-v1"
@@ -19,15 +18,9 @@ internal object DesktopRemoval {
private fun protected(context: Context, root: File) =
DesktopFileRemoval.runtimeFiles(root) + lines(context, "emulators-keep.txt.gzip")
/**
* What the desktop's own files take on disk, from the same inventory its removal uses (each
* file's size); the bundled apps' and emulators' shared files are not counted. Reads an asset:
* call it off the main thread.
*/
fun installedSize(context: Context): Long =
lines(context, "kde-remove.tsv.gzip").sumOf { line -> line.split('\t').getOrNull(1)?.toLongOrNull() ?: 0L }
/** Called before a guest starts or just after KDE extraction; retries after a process death. */
fun cleanLegacy(context: Context, listener: LinuxRuntimeInstaller.ProgressListener? = null) {
val root = LinuxRuntime.rootDir(context)
if (FileUtils.readString(File(root, CLEANED))?.trim() == "1") return
@@ -54,7 +47,6 @@ internal object DesktopRemoval {
val version = FileUtils.readString(pending)?.trim() ?: FileUtils.readString(marker)?.trim()
val versions = context.assets.open("desktop-removal/versions.txt").bufferedReader().use { it.readLines() }
check(version in versions) { context.getString(R.string.desktop_removal_unknown) }
// Validate all inventories and the runtime database before dropping the installed marker.
val entries = lines(context, "kde-remove.tsv.gzip").map(DesktopFileRemoval.Entry::parse)
val keep = protected(context, root)
cleanLegacy(context, listener)
@@ -63,7 +55,6 @@ internal object DesktopRemoval {
if (marker.exists() && !marker.delete()) error("Could not clear desktop marker")
val removing = context.getString(R.string.desktop_removing)
val result = DesktopFileRemoval.remove(root, entries, keep) { listener.onProgress(removing, it) }
// APK-staged desktop files are matched against the APK too; keep user-modified copies.
for ((asset, path) in com.droiddeck.launcher.session.SessionFiles.DESKTOP_FILES) {
val target = DesktopFileRemoval.safeFile(root, path) ?: continue
if (!isRegularFile(target)) continue
@@ -7,31 +7,20 @@ import com.droiddeck.launcher.core.FileUtils
import org.json.JSONObject
import java.io.File
/**
* Flatpak in the Linux runtime: putting it there (droiddeck-flatpak-setup), the apps installed
* from Flathub, and installing, removing and updating them (droiddeck-flatpak). One per-user
* installation at /root/.local/share/flatpak, so nothing needs the system helper or polkit.
*
* Every command runs in a proot of its own, outside any session: the store works whether or not
* Steam or the desktop is up, and a session starting meanwhile leaves it running (OrphanReaper).
*/
object FlatpakManager {
private const val TAG = "FlatpakManager"
const val FLATPAK = "/usr/bin/flatpak"
const val BWRAP = "/usr/local/bin/droiddeck-bwrap"
/** The program the front end starts a Flatpak app through, in a session of its own. */
const val LAUNCHER = "/usr/local/bin/droiddeck-flatpak-run"
private const val HELPER = "/usr/local/bin/droiddeck-flatpak"
private const val SETUP = "/usr/local/bin/droiddeck-flatpak-setup"
private const val USER_DIR = "root/.local/share/flatpak"
/** One store operation at a time: Flatpak locks its installation per transaction anyway. */
@Volatile var busy: String? = null
private set
class App(val id: String, val name: String, val icon: File?, val branch: String, val summary: String?)
/** Flatpak is in the runtime, with Flathub to install from. */
fun ready(context: Context): Boolean {
val root = LinuxRuntime.rootDir(context)
return File(root, FLATPAK.substring(1)).isFile &&
@@ -44,10 +33,6 @@ object FlatpakManager {
else -> "aarch64"
}
/**
* The installed apps, read from the installation itself: each app's deployed files carry the
* desktop entry and icons it exports, so listing them needs no process in the runtime.
*/
fun installedApps(context: Context): List<App> {
val apps = File(LinuxRuntime.rootDir(context), "$USER_DIR/app")
val arch = arch()
@@ -67,7 +52,6 @@ object FlatpakManager {
fun isInstalled(context: Context, id: String): Boolean =
File(LinuxRuntime.rootDir(context), "$USER_DIR/app/$id/${arch()}").listFiles()?.any { File(it, "active").exists() } == true
/** A key of the desktop entry's main group ([Desktop Entry]), unlocalised. */
private fun entry(desktop: String, key: String): String? {
var inMain = false
for (line in desktop.lineSequence()) {
@@ -78,7 +62,6 @@ object FlatpakManager {
return null
}
/** The largest PNG the app exports (the front end draws no SVG). */
private fun icon(export: File, id: String): File? {
val hicolor = File(export, "icons/hicolor")
return hicolor.listFiles()
@@ -87,13 +70,11 @@ object FlatpakManager {
?.maxByOrNull { it.first }?.second
}
/** Runs [argv] in the runtime, its output also in files/logs/tools/flatpak-<verb>.log. */
private fun runGuest(context: Context, argv: List<String>, fakeRoot: Boolean, onLine: (String) -> Unit): Int {
val name = "flatpak-" + (argv.getOrNull(2)?.takeIf { argv.getOrNull(1) == HELPER } ?: "setup")
return GuestCommand.run(context, argv, fakeRoot, name, onLine = onLine)
}
/** [block]'s result, or [ifBusy]'s while another operation has the installation. */
private inline fun <T> exclusive(what: String, ifBusy: () -> T, block: () -> T): T {
synchronized(this) {
if (busy != null) return ifBusy()
@@ -102,7 +83,6 @@ object FlatpakManager {
try { return block() } finally { busy = null }
}
/** Puts Flatpak into the runtime and adds Flathub. Null on success, else what went wrong. */
fun setup(context: Context, onProgress: (String, Int) -> Unit): String? {
if (!LinuxRuntime.isInstalled(context)) return context.getString(R.string.user_apps_runtime_required)
return exclusive("setup", { context.getString(R.string.flatpakmgr_busy) }) {
@@ -123,17 +103,12 @@ object FlatpakManager {
}
}
/**
* One store operation - install, uninstall or update - with its progress as a stage line and
* an overall percentage across the steps Flatpak plans (a runtime, its extensions, the app).
*/
private fun transaction(
context: Context, verb: String, id: String?, onProgress: (String, Int) -> Unit, onDone: (JSONObject) -> Unit = {},
): String? {
if (!ready(context)) return context.getString(R.string.flatpakmgr_not_ready)
return exclusive("$verb:${id ?: "all"}", { context.getString(R.string.flatpakmgr_busy) }) {
var error: String? = null
// Progress lines carry no ref; they belong to the step the last "op" line started.
var step = context.getString(R.string.flatpakmgr_starting)
val argv = listOfNotNull("/usr/bin/python3", HELPER, verb, id)
val status = runGuest(context, argv, fakeRoot = false) { line ->
@@ -169,10 +144,6 @@ object FlatpakManager {
}, name)
}
/**
* A ref in words: "app/org.supertuxproject.SuperTux/aarch64/stable" is "SuperTux". [text]
* resolves a string resource with its arguments.
*/
internal fun refLabel(ref: String, text: (Int, Array<out Any>) -> String): String {
val parts = ref.split('/')
val id = parts.getOrNull(1) ?: return ref
@@ -190,7 +161,6 @@ object FlatpakManager {
}
}
/** The whole transaction's percentage: steps done, plus this one's share. */
private fun overall(o: JSONObject, percent: Int): Int {
val n = o.optInt("n").coerceAtLeast(1)
val i = o.optInt("i").coerceIn(1, n)
@@ -200,7 +170,6 @@ object FlatpakManager {
fun install(context: Context, id: String, onProgress: (String, Int) -> Unit) = transaction(context, "install", id, onProgress)
fun uninstall(context: Context, id: String, onProgress: (String, Int) -> Unit) = transaction(context, "uninstall", id, onProgress)
/** Installs the .flatpak bundle at [guestPath], its runtimes from Flathub: the app's ID, and what went wrong if anything. */
fun installBundle(context: Context, guestPath: String, onProgress: (String, Int) -> Unit): Pair<String?, String?> {
var id: String? = null
val problem = transaction(context, "install-bundle", guestPath, onProgress) { id = it.optString("id").ifEmpty { null } }
@@ -208,7 +177,6 @@ object FlatpakManager {
}
fun update(context: Context, id: String?, onProgress: (String, Int) -> Unit) = transaction(context, "update", id, onProgress)
/** Installed apps with an update on Flathub, or null when that could not be checked. */
fun updates(context: Context): Set<String>? {
if (!ready(context)) return emptySet()
var apps: Set<String>? = null
@@ -9,18 +9,7 @@ import com.droiddeck.launcher.session.SessionPaths
import com.droiddeck.launcher.session.SessionPrefs
import java.io.File
/**
* One command in the Linux runtime outside any session - the store's Flatpak work, an AppImage
* import - in a proot of its own. A session starting meanwhile leaves it running (OrphanReaper).
*/
object GuestCommand {
/**
* Runs [argv] and hands each output line to [onLine]; returns the exit status. [fakeRoot] is
* for the package tools, which refuse any uid but 0. With [logName], everything the command
* says also goes to files/logs/tools/<logName>.log beside the session logs, so a problem can
* be handed over like a session's. With [linkDir], hard links (which Android denies apps)
* become symlinks to files proot keeps there (its link2symlink).
*/
fun run(context: Context, argv: List<String>, fakeRoot: Boolean = false, logName: String? = null,
linkDir: File? = null, onLine: (String) -> Unit): Int {
val log = logName?.let {
@@ -40,7 +29,6 @@ object GuestCommand {
val root = LinuxRuntime.rootDir(context)
LinuxRuntime.writeAccounts(context)
SessionFiles.stage(context, root)
// The resolver otherwise names the DNS of whatever network the last session was on.
LinuxNetworkLinkComponent(context, root).publish()
val runtimeDir = File(context.filesDir, ".flatpak-rt").apply { mkdirs() }
val cmd = LinuxRuntime.prootPrefix(context, root, "/root", fakeRoot)
@@ -26,18 +26,6 @@ import java.net.Inet6Address
import org.json.JSONArray
import org.json.JSONObject
/**
* The device's network link, published for the Linux runtime's processes.
*
* Android denies an app the rtnetlink dump behind getifaddrs() and if_nameindex(), the hardware
* address ioctl and every table under /proc/net. Wine builds its adapter, address, route and
* neighbour tables from those, so a game under the runtime saw no adapter, no address, no gateway
* and no MAC - and one that checks its adapters before going online waited for good. The link the
* app can see through ConnectivityManager is written here, kept current while the session runs,
* and the session shim answers whatever the kernel refuses from it (tools/linuxfs/preload/netif.c).
*
* Shape follows WinNative's LinuxNetworkLinkComponent (maxjivi05, f76a5c4a), GPL-3.0.
*/
class LinuxNetworkLinkComponent(
context: Context,
private val rootDir: File,
@@ -71,7 +59,6 @@ class LinuxNetworkLinkComponent(
}
}
/** Called before the session starts, so its first process already sees the link. */
fun publish() = synchronized(lock) {
currentNetwork = connectivity.activeNetwork
currentProperties = currentNetwork?.let(connectivity::getLinkProperties)
@@ -102,7 +89,6 @@ class LinuxNetworkLinkComponent(
}
override fun onLost(network: Network) = synchronized(lock) {
// A handover can report the old link lost after the new default arrived.
if (network == currentNetwork) {
currentNetwork = null
currentProperties = null
@@ -174,7 +160,6 @@ class LinuxNetworkLinkComponent(
}
}
/** NetworkManager's view of Android. Kept separate from netif.c's legacy adapter format. */
@Suppress("DEPRECATION")
private fun writeNetworkState(properties: LinkProperties?, capabilities: NetworkCapabilities?) {
val namesAllowed = canReadWifiNames()
@@ -238,8 +223,6 @@ class LinuxNetworkLinkComponent(
connectionInfo
}
if (info != null) {
// SSIDs can be redacted by Android. No location permission is needed to
// report the transport; a hidden name simply displays as Wi-Fi in Steam.
if (namesAllowed) {
val namedInfo = connectionInfo ?: info
namedInfo.ssid?.takeUnless { it == WifiManager.UNKNOWN_SSID || it.isEmpty() }
@@ -262,15 +245,6 @@ class LinuxNetworkLinkComponent(
}
}
/**
* The guest's resolver, from the network it is actually on. glibc reads /etc/resolv.conf,
* and the image ships two public servers there, which is why Steam resolves at all - but a
* captive portal, a private-DNS network or a v6-only carrier wants the network's own
* servers. Those come first, IPv4 before IPv6 and never a link-local one (Android lists
* fe80:: resolvers a guest cannot reach; handing Bannerlator's Wine guests exactly that was
* the Pale Moon "no internet"). The public ones stay as fallback so nothing is ever worse
* than the image was.
*/
private fun writeResolver(properties: LinkProperties?) {
val own = properties?.dnsServers.orEmpty()
.filter { !it.isLinkLocalAddress && !it.isLoopbackAddress && !it.isAnyLocalAddress }
@@ -321,10 +295,6 @@ class LinuxNetworkLinkComponent(
}
}
/**
* A stable, locally administered address for this device. Android hides the real one, and a
* title should see the same hardware identity every session.
*/
private fun seededMac(): String {
val id = Settings.Secure.getString(appContext.contentResolver, Settings.Secure.ANDROID_ID) ?: "bannerlator"
val bytes = java.security.MessageDigest.getInstance("SHA-1").digest(id.toByteArray())
@@ -16,47 +16,23 @@ import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
/**
* The glibc arm64 rootfs at {@code files/linuxfs} and the proot invocation that runs a program in
* it as this app's own uid. It is a second runtime beside the Wine imagefs, not a container: no
* Wine, no box64, no FEX. proot is packaged as {@code libproot.so} so the installer places it, with
* its loader, in the native library directory - the only place an app on targetSdk 28 may execute
* a file from.
*
* <p>Ported from WinNative's gamescope runtime (GPL-3.0).
*/
public final class LinuxRuntime {
public static final String DIR = "linuxfs";
public static final String SESSION_SCRIPT = "/usr/local/bin/droiddeck-session";
public static final String MODE_DESKTOP = "desktop";
public static final String MODE_STEAM = "steam";
public static final String MODE_RUN = "run";
/** Shortcut extra naming which of the modes above a Linux entry launches. */
public static final String EXTRA_LINUX_MODE = "linux_mode";
private static final String KGSL_DEVICE = "/dev/kgsl-3d0";
/**
* Where the guest sees a command's XDG_RUNTIME_DIR. A Unix socket's path must fit in 108 bytes,
* and libwayland checks the guest's path before proot ever sees it: with the app on an SD card
* adopted as internal storage the files directory is /mnt/expand/<uuid>/user/0/<package>/files,
* and gamescope-0 under it is one byte over, so gamescope could not open its socket and the
* session ended at once. A long host path behind it is proot's to shorten.
*/
public static final String GUEST_RUNTIME_DIR = "/run/droiddeck";
private static final String SYSTEM_FONTS = "/system/fonts";
private static final String GUEST_SYSTEM_FONTS = "/usr/local/share/fonts/android";
/** The folder under Downloads where builds before private logs wrote them; read only to move them. */
public static final String DEBUG_LOG_DIR = "DroidDeck";
/**
* Where every log lands: app-private, {@code files/logs}. Session folders, tools/ (one-off
* commands) and stores/ sit under it; the files directory is bound into every session at the
* same path, so the guest writes there too. Share logs is how a folder leaves the device.
*/
public static File logDir(Context context) {
return new File(context.getFilesDir(), "logs");
}
/** {@code Download/DroidDeck}, where earlier builds wrote logs. Only LogMigration reads it. */
public static File legacyLogDir() {
return new File(android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS), DEBUG_LOG_DIR);
@@ -68,16 +44,10 @@ public final class LinuxRuntime {
return new File(context.getFilesDir(), DIR);
}
/**
* The session's own small tree beside the rootfs: the fake evdev nodes and their rings. In
* Bannerlator these lived in the Wine imagefs; here there is no Wine, so the session owns them.
* Bound into the guest at its own host path, so nothing needs translating.
*/
public static File sessionRoot(Context context) {
return new File(context.getFilesDir(), "session");
}
/** Where the runtime carries the host-side proot; see tools/linuxfs/prebuilt/proot/README.md. */
private static final String HOST_DIR = "opt/android-host";
public static File prootBinary(Context context) {
@@ -92,13 +62,11 @@ public final class LinuxRuntime {
return new File(rootDir(context), HOST_DIR + "/loader");
}
/** proot links against libtalloc, which sits beside it; empty when the apk copy is in use. */
public static String prootLibraryPath(Context context) {
File dir = new File(rootDir(context), HOST_DIR);
return dir.equals(prootBinary(context).getParentFile()) ? dir.getPath() : "";
}
/** The rootfs is present with gamescope and the session script the launcher hands control to. */
public static boolean isInstalled(Context context) {
if (LinuxRuntimeInstaller.isRemoving() || LinuxRuntimeInstaller.hasRemovalPending(context)) return false;
File root = rootDir(context);
@@ -109,7 +77,6 @@ public final class LinuxRuntime {
&& prootLoader(context).isFile();
}
/** The Vulkan ICD manifest the rootfs ships for the device GPU, or null when it has none. */
public static File vulkanIcd(Context context) {
File icdDir = new File(rootDir(context), "usr/share/vulkan/icd.d");
File[] manifests = icdDir.listFiles((dir, name) -> name.endsWith(".json"));
@@ -120,25 +87,16 @@ public final class LinuxRuntime {
return manifests.length > 0 ? manifests[0] : null;
}
/**
* The proot command line running {@code guestCommand} inside the rootfs. Host paths the session
* needs - the app's files directory for the compositor and audio sockets, external storage for
* the user's games - are bound at their own paths, so nothing on either side needs translating
* and proot never touches the fds a dma-buf travels in. Android has no /dev/shm; a directory
* under the cache stands in, which glibc's shm_open and Chromium's shared memory accept.
*/
public static List<String> command(Context context, File sessionRoot, File runtimeDir,
File externalStorage, List<String> guestCommand) {
return command(context, sessionRoot, runtimeDir, externalStorage, null, guestCommand);
}
/** As above, plus {@code host:guest} bind specs - the installed games handed to Steam. */
public static List<String> command(Context context, File sessionRoot, File runtimeDir,
File externalStorage, List<String> extraBinds,
List<String> guestCommand) {
File root = rootDir(context);
List<String> binds = binds(context, sessionRoot, runtimeDir, externalStorage, extraBinds);
// A session's binds are the view its programs have; a one-off command's are not recorded.
if (sessionRoot != null) lastBinds = binds;
List<String> cmd = prootPrefix(context, root, "/root");
for (String spec : binds) bind(cmd, spec);
@@ -146,10 +104,6 @@ public final class LinuxRuntime {
return cmd;
}
/**
* The binds of the last command built: the guest's view of the host, which the Flatpak
* sandboxes started beside a session (BwrapSpawner) translate their paths through.
*/
private static volatile List<String> lastBinds;
public static List<String> lastBinds(Context context) {
@@ -158,25 +112,15 @@ public final class LinuxRuntime {
return binds(context, null, null, android.os.Environment.getExternalStorageDirectory(), null);
}
/** proot and its options up to the binds: the rootfs at {@code root}, starting in {@code cwd}. */
public static List<String> prootPrefix(Context context, File root, String cwd) {
return prootPrefix(context, root, cwd, false);
}
/**
* As above; {@code fakeRoot} has the guest see uid 0 (proot -0), for the package tools that
* refuse any other uid. Files it writes still belong to the app.
*/
public static List<String> prootPrefix(Context context, File root, String cwd, boolean fakeRoot) {
List<String> cmd = new ArrayList<>();
cmd.add(prootBinary(context).getPath());
cmd.add("--kill-on-exit");
// Preserve the host kernel identity while giving the guest the app's branded host name.
cmd.add("--kernel-release=" + guestUtsname(SessionPrefs.guestHostname(context)));
// Android's app seccomp policy traps the whole set*id family. Xwayland's Popen() calls
// setgid()/setuid() before it execs xkbcomp and _exit(127)s when they fail, so without
// this the keymap never compiles and Xwayland dies. -i makes proot answer those calls
// itself while still reporting our real ids, so nothing inside sees a different user.
int uid = Process.myUid();
if (fakeRoot) {
cmd.add("-0");
@@ -191,7 +135,6 @@ public final class LinuxRuntime {
return cmd;
}
/** The {@code host:guest} (or same-path) bind specs of a session; see {@link #command}. */
public static List<String> binds(Context context, File sessionRoot, File runtimeDir,
File externalStorage, List<String> extraBinds) {
File root = rootDir(context);
@@ -221,39 +164,20 @@ public final class LinuxRuntime {
bind(cmd, shm.getPath() + ":/dev/shm");
bindSystemFonts(cmd, root);
// Android denies apps these; glibc, Steam and libcap read them at startup.
File fakeProc = new File(root, "etc/droiddeck/proc");
// libpci picks its procfs backend on whether it can read the /proc/bus/pci directory, which
// the app can, then die()s - exit(1) on the calling process - on the devices file inside
// it, which the app cannot. Chromium loads libpci in its GPU process to name the video
// card, so that exit kills the process; after a few tries CEF gives up on hardware and
// draws the rest of the session on SwiftShader: the client's interface rendered on the
// CPU. An empty list is the truthful answer from in here - nothing the app can see is on
// a PCI bus. Created at session start rather than shipped in the rootfs so an installed
// runtime is fixed too, and the table's guard below binds it only when the real file
// cannot be read, so it can never stand in front of real data. (WinNative, maxjivi05,
// deff1ac6, via Bannerlator 8fb668d1: 44 -> 85 fps scrolling the Big Picture library on a
// OnePlus 15, GPU-process crashes 12 -> 0.)
File pciDevices = new File(fakeProc, "pci_devices");
if (!pciDevices.isFile()) {
try {
//noinspection ResultOfMethodCallIgnored
pciDevices.getParentFile().mkdirs();
//noinspection ResultOfMethodCallIgnored
pciDevices.createNewFile();
} catch (java.io.IOException e) {
// It then fails the isFile() test below and the session runs as it did before.
}
}
// protonfixes, which GE-Proton and proton-cachyos run before every game, opens this to
// check the esync file limit and dies on the PermissionError, so no game starts with them.
// The value is the 64-bit kernel default.
File fileMax = new File(fakeProc, "file-max");
if (!fileMax.isFile()) {
try {
Files.write(fileMax.toPath(), "9223372036854775807\n".getBytes(StandardCharsets.US_ASCII));
} catch (IOException e) {
// Left unbound, as above.
}
}
String[][] procFiles = {
@@ -270,7 +194,6 @@ public final class LinuxRuntime {
};
for (String[] entry : procFiles) {
File fake = new File(fakeProc, entry[0]);
// A live stand-in the session binds itself (CpuStatComponent's /proc/stat) wins.
boolean live = false;
if (extraBinds != null) for (String spec : extraBinds) live |= spec.endsWith(":" + entry[1]);
if (!live && fake.isFile() && !new File(entry[1]).canRead()) {
@@ -288,14 +211,6 @@ public final class LinuxRuntime {
return specs;
}
/**
* An app process may not open {@code /dev/dri} - the nodes exist but are labelled
* {@code graphics_device}, which stock policy grants surfaceflinger and not us - yet libdrm and
* everything built on it identify a GPU by its render node, and gamescope refuses to offer
* linux-dmabuf without one. The KGSL device Turnip actually drives ({@code gpu_device}, which we
* may open) stands in: it appears as a render node with the sysfs entries libdrm reads, and our
* Turnip build reports the same device numbers for it.
*/
private static void bindGpuNode(Context context, List<String> cmd) {
StructStat st;
try {
@@ -324,8 +239,6 @@ public final class LinuxRuntime {
if (!Files.isSymbolicLink(subsystem.toPath())) {
Os.symlink("/sys/bus/platform", subsystem.getPath());
}
// What MangoHud names a GPU's driver by; msm_drm is how an Adreno's render node reads
// on a mainline kernel, and the driver it reads the load of (bindAdrenoStats).
File driver = new File(device, "driver");
if (!Files.isSymbolicLink(driver.toPath())) {
Os.symlink("/sys/bus/platform/drivers/msm_drm", driver.getPath());
@@ -339,22 +252,11 @@ public final class LinuxRuntime {
bindDrmClass(base, cmd, node, major + ":" + minor);
}
/**
* MangoHud (Deck mode's performance overlay, mangoapp) finds the GPU by listing
* {@code /sys/class/drm}, and an exception from that listing ends the process: under an
* enforcing SELinux policy - every retail phone - an app may not list it, so mangoapp died on
* start, gamescope restarted it until the session gave up, and there was no overlay. Where the
* app cannot list it, the session's {@code /sys/class/drm} is ours: the render node above,
* named as an Adreno's is. MangoHud then reads the GPU's stats from KGSL's sysfs, which such a
* device refuses too - SessionService's GpuStatsComponent stands in for that. Where the
* listing is allowed it is left alone.
*/
private static void bindDrmClass(File base, List<String> cmd, String node, String devNumbers) {
if (new File("/sys/class/drm").list() != null) return;
File drmClass = new File(base, "class");
try {
if (!drmClass.isDirectory() && !drmClass.mkdirs()) return;
// Only symlinks are ever made here; deleting them must not follow one into its target.
File[] old = drmClass.listFiles();
if (old != null) for (File f : old) Files.deleteIfExists(f.toPath());
Os.symlink("/sys/dev/char/" + devNumbers, new File(drmClass, node).getPath());
@@ -363,15 +265,12 @@ public final class LinuxRuntime {
}
bind(cmd, drmClass.getPath() + ":/sys/class/drm");
android.util.Log.i("LinuxRuntime", "hud: /sys/class/drm is not listable here; the session's lists " + node);
// Also refused to list, also asked for by MangoHud twice a second - an error line each
// time in the session log - and holding nothing it reads on an Adreno phone.
if (new File("/sys/class/powercap").list() == null) {
File empty = new File(base, "empty-powercap");
if (empty.isDirectory() || empty.mkdirs()) bind(cmd, empty.getPath() + ":/sys/class/powercap");
}
}
/** The temperature of a CPU zone the app may read (the first by name), or null. */
public static String cpuTempSource() {
for (java.util.Map.Entry<String, File> e : new java.util.TreeMap<>(thermalZones()).entrySet()) {
File temp = new File(e.getValue(), "temp");
@@ -380,7 +279,6 @@ public final class LinuxRuntime {
return null;
}
/** The file the GPU's temperature reads from, or null where the app may read none. */
public static String gpuTempSource() {
String kgsl = "/sys/class/kgsl/kgsl-3d0/";
return firstReadable(kgsl + "temp", kgsl + "devfreq/temp", thermalZone("gpu"));
@@ -391,12 +289,6 @@ public final class LinuxRuntime {
return firstReadable(kgsl + "gpu_busy_percentage", kgsl + "devfreq/gpu_load");
}
/**
* Valve's mangoapp (Deck mode's performance overlay) reads an Adreno GPU's load, clock and
* temperatures from where they are on Valve's own hardware; every Adreno under Android keeps
* them in KGSL's sysfs, readable by the app, though which files a kernel has differs between
* Snapdragon generations.
*/
private static void bindAdrenoStats(List<String> cmd, List<String> extraBinds) {
String kgsl = "/sys/class/kgsl/kgsl-3d0/";
String gpuTemp = gpuTempSource();
@@ -409,13 +301,10 @@ public final class LinuxRuntime {
{thermalZone("ddr"), "/sys/class/thermal/thermal_zone22/temp"},
};
for (String[] stat : stats) {
// A value the session feeds itself (GpuStatsComponent, where KGSL is refused) wins.
boolean fed = false;
if (extraBinds != null) for (String spec : extraBinds) fed |= spec.endsWith(":" + stat[1]);
if (stat[0] != null && !fed) bind(cmd, stat[0] + ":" + stat[1]);
}
// Which of the overlay's GPU values this device lets the app read: a phone under an
// enforcing policy refuses some of them, and the overlay then has no line for that value.
android.util.Log.i("LinuxRuntime", "hud sources: gpu load " + orNone(stats[0][0]) + ", gpu clock "
+ orNone(stats[1][0]) + ", gpu temp " + orNone(gpuTemp) + ", ddr temp " + orNone(stats[4][0])
+ ", thermal zones " + thermalZones().size()
@@ -426,12 +315,6 @@ public final class LinuxRuntime {
return path != null ? path : "none";
}
/**
* mangoapp's CPU temperature is the mean of the thermal zones named cpuN-thermal or
* cpuN-top-thermal, as mainline kernels name them; Android kernels name the same sensors
* cpuss-0, cpu-1-0, cpu0-silver-usr, apc1-cpu0-usr and the like, so those zones are shown
* under the mainline name.
*/
private static void bindCpuTemps(List<String> cmd, File root) {
File name = new File(root, "etc/droiddeck/cpu-thermal-type");
try {
@@ -454,7 +337,6 @@ public final class LinuxRuntime {
return null;
}
/** The temp file of the thermal zone for a sensor: the one named so, else the first whose name has it. */
private static String thermalZone(String sensor) {
java.util.Map<String, File> zones = thermalZones();
File zone = zones.get(sensor);
@@ -464,7 +346,6 @@ public final class LinuxRuntime {
return zone != null ? new File(zone, "temp").getPath() : null;
}
/** The device's thermal zones by their sensor's name, in lower case. */
private static java.util.Map<String, File> thermalZones() {
java.util.Map<String, File> byType = new java.util.HashMap<>();
File[] zones = new File("/sys/class/thermal").listFiles((dir, n) -> n.startsWith("thermal_zone"));
@@ -492,14 +373,12 @@ public final class LinuxRuntime {
cmd.add(spec);
}
/** PRoot's complex -k format: sysname, nodename, release, version, machine, domain, HWCAP. */
static String guestUtsname(String hostname) {
StructUtsname host = Os.uname();
return "\\" + host.sysname + "\\" + hostname + "\\" + host.release
+ "\\" + host.version + "\\" + host.machine + "\\localdomain\\-1\\";
}
/** X access control and Steam look the session user up by uid: the app uid is root inside. */
public static void writeAccounts(Context context) throws IOException {
File root = rootDir(context);
int uid = Process.myUid();
@@ -29,30 +29,19 @@ import java.nio.file.StandardCopyOption;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
/**
* Downloads and unpacks the Linux runtime rootfs into {@code files/linuxfs}. It is opt-in: nothing
* fetches it until the user asks for it, and a gamescope session refuses to start without it.
*
* <p>The catalog row names the build, its URL and its sha256, so an interrupted or corrupted
* download is caught before anything is written into the app's files directory.
*/
public final class LinuxRuntimeInstaller {
private static final String TAG = "LinuxRuntimeInstaller";
/** Catalog row, beside the other component catalogs in winlator-contents. */
public static final String CATALOG_URL =
"https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/main/linuxfs.json";
private static final String VERSION_FILE = ".version";
/** What a stage is doing, for code that branches on it rather than on its (translated) words. */
public enum Step { DOWNLOADING, VERIFYING, OTHER }
public interface ProgressListener {
/** {@code percent} is -1 while the size is unknown. */
void onProgress(String stage, int percent);
/** The same with its {@link Step}; a listener that branches on the step overrides this one. */
default void onProgress(Step step, String stage, int percent) {
onProgress(stage, percent);
}
@@ -72,11 +61,6 @@ public final class LinuxRuntimeInstaller {
}
}
/**
* One runtime operation per process. The launcher and session can both install, and a
* launcher rebuilt mid-install (a pad plugged in, dark mode switched) forgets it had: a second
* install would share the first one's archive and staging directory and wreck both.
*/
private static final class Job {
final CopyOnWriteArrayList<ProgressListener> listeners = new CopyOnWriteArrayList<>();
final CountDownLatch done = new CountDownLatch(1);
@@ -95,7 +79,6 @@ public final class LinuxRuntimeInstaller {
private LinuxRuntimeInstaller() {}
/** The build currently unpacked, or null when the runtime is not installed. */
public static String installedVersion(Context context) {
File marker = new File(LinuxRuntime.rootDir(context), VERSION_FILE);
if (!marker.isFile() || !LinuxRuntime.isInstalled(context)) return null;
@@ -103,7 +86,6 @@ public final class LinuxRuntimeInstaller {
return v == null ? null : v.trim();
}
/** The catalog's current build, or null when it cannot be reached or read. */
public static Release fetchRelease() {
String body = Downloader.downloadString(CATALOG_URL);
if (body == null || body.isEmpty()) return null;
@@ -120,22 +102,8 @@ public final class LinuxRuntimeInstaller {
}
}
/**
* The guest's home directory, carried across an update rather than replaced with the tarball's
* empty one. Steam installs itself here - the client, the signed-in account and every
* downloaded game - so replacing the rootfs wholesale used to delete all three and leave the
* user re-downloading Steam and signing in again, with their library gone.
*/
private static final String USER_DATA = "root";
/**
* Downloads {@code release} and replaces whatever is installed with it. Returns false and
* leaves the existing runtime alone if the download or the checksum fails; the new rootfs is
* only moved into place once it has been unpacked whole.
*
* <p>{@link #USER_DATA} survives the swap: the system is replaced, what the user put in it is
* not. An update therefore keeps Steam, the login and the installed games.
*/
public static boolean install(Context context, Release release, ProgressListener listener) {
Job job;
boolean owner;
@@ -169,7 +137,6 @@ public final class LinuxRuntimeInstaller {
}
}
/** True while an install is running in this process, whoever started it. */
public static boolean isInstalling() {
synchronized (JOB_LOCK) {
return running != null && !running.removal && !running.maintenance;
@@ -188,7 +155,6 @@ public final class LinuxRuntimeInstaller {
synchronized (JOB_LOCK) { return running != null && running.maintenance; }
}
/** Reserve an in-place package operation with the same exclusion/progress as runtime installs. */
public static Maintenance beginMaintenance(Context context, String stage) {
if (com.droiddeck.launcher.session.SessionState.INSTANCE.getRunning()) return null;
com.droiddeck.launcher.session.SessionPhase phase = com.droiddeck.launcher.session.SessionState.INSTANCE.getPhase();
@@ -241,10 +207,6 @@ public final class LinuxRuntimeInstaller {
return new File(root.getParentFile(), root.getName() + ".removing");
}
/**
* Follows the runtime operation already running, including removal, after a screen rebuild.
* Blocks until it ends and returns its result, or null when there is no current operation.
*/
public static Boolean attach(ProgressListener listener) {
Job job;
synchronized (JOB_LOCK) {
@@ -271,15 +233,12 @@ public final class LinuxRuntimeInstaller {
private static boolean installOnce(Context context, Release release, ProgressListener listener) {
File archive = new File(context.getCacheDir(), "linuxfs.tar.zst");
try {
// A failed/interrupted explicit removal must finish before a fresh installation.
// Its quarantined home is not user data to carry into an update.
File pendingRemoval = removalDirectory(LinuxRuntime.rootDir(context));
if (pendingRemoval.exists() && listener != null) listener.onProgress(context.getString(R.string.rtinst_removing_leftovers), -1);
if (!discard(pendingRemoval, null)) throw new IOException("Cannot clear the previous runtime: " + pendingRemoval);
emptyTrash(LinuxRuntime.rootDir(context).getParentFile());
String downloading = context.getString(R.string.rtinst_downloading);
if (listener != null) listener.onProgress(Step.DOWNLOADING, downloading, 0);
// Downloader reports a 0..1 fraction, or -1 while the total size is unknown.
boolean ok = Downloader.downloadFile(release.url, archive, true, (fraction) -> {
if (listener != null) {
listener.onProgress(Step.DOWNLOADING, downloading,
@@ -298,8 +257,6 @@ public final class LinuxRuntimeInstaller {
return false;
}
// Unpack beside the live rootfs and swap, so a failure here cannot leave a half
// runtime that isInstalled() would happily launch.
File root = LinuxRuntime.rootDir(context);
File staging = new File(root.getParentFile(), LinuxRuntime.DIR + ".new");
File old = new File(root.getParentFile(), LinuxRuntime.DIR + ".old");
@@ -321,16 +278,10 @@ public final class LinuxRuntimeInstaller {
return false;
}
// Carry the user's home over before the new rootfs takes the name. A rename inside the
// same filesystem, so a 30 GB library costs nothing and cannot half-copy; the tarball's
// own empty /root is dropped first so the rename has somewhere to land. If this fails
// the update is abandoned and the previous runtime is put back untouched - shipping a
// working system with the user's games gone is the worse outcome.
try {
RuntimeFileTree.carryHome(old, staging);
} catch (IOException e) {
Log.w(TAG, "could not carry root across the update; rolling back", e);
// Restore the live name even if cleaning the failed staging tree is denied.
old.renameTo(root);
try { RuntimeFileTree.delete(staging, null); }
catch (IOException cleanup) { Log.w(TAG, "could not clear the failed staging tree", cleanup); }
@@ -366,15 +317,8 @@ public final class LinuxRuntimeInstaller {
}
}
/** Where entries the app cannot unlink are set aside, out of the way of the runtime's names. */
static final String TRASH = ".runtime-trash";
/**
* Gets {@code tree} out from under its name. What can be deleted is; if something inside
* cannot be (a file made by root or {@code su} carries another SELinux category, and the app
* may not unlink it), the remainder is moved aside into {@link #TRASH} instead of blocking
* every later install and removal on it. False only when even that move is refused.
*/
static boolean discard(File tree, java.util.function.LongConsumer progress) {
if (RuntimeFileTree.deleteWhatCan(tree, progress)) return true;
File trash = new File(tree.getParentFile(), TRASH);
@@ -388,7 +332,6 @@ public final class LinuxRuntimeInstaller {
return !tree.exists();
}
/** Another try at what {@link #discard} set aside. Whatever still cannot go stays there. */
static void emptyTrash(File parent) {
File trash = new File(parent, TRASH);
if (trash.exists() && !RuntimeFileTree.deleteWhatCan(trash, null))
@@ -400,7 +343,6 @@ public final class LinuxRuntimeInstaller {
return dir.isDirectory() && names != null && names.length == 0;
}
/** Reserve removal before returning to the UI, so Play cannot race the worker. */
public static Removal beginUninstall(Context context) {
if (com.droiddeck.launcher.session.SessionState.INSTANCE.getRunning()) return null;
com.droiddeck.launcher.session.SessionPhase phase = com.droiddeck.launcher.session.SessionState.INSTANCE.getPhase();
@@ -408,7 +350,6 @@ public final class LinuxRuntimeInstaller {
return beginUninstall(context.getApplicationContext(), LinuxRuntime.rootDir(context));
}
/** {@code app} words the progress; unit tests pass null and get none. */
static Removal beginUninstall(Context app, File root) {
synchronized (JOB_LOCK) {
if (running != null) return null;
@@ -446,17 +387,12 @@ public final class LinuxRuntimeInstaller {
}
};
File pending = removalDirectory(root);
// Invalidate the installation before traversing it. A process killed halfway
// through leaves a named removal to resume, never a launchable partial runtime.
if (root.exists()) {
if (!discard(pending, progress) || !root.renameTo(pending))
throw new IOException("Cannot prepare the runtime for removal");
} else if (!pending.isDirectory() && !pending.mkdirs()) {
throw new IOException("Cannot prepare the runtime for removal");
}
// Keep the quarantine until all leftovers are gone, so a failure stays retryable.
// An entry the app may not delete is set aside rather than failing the removal:
// a quarantine that can never clear would block reinstalling for good.
for (File leftover : new File[]{
new File(root.getParentFile(), root.getName() + ".new"),
new File(root.getParentFile(), root.getName() + ".old"),
@@ -484,12 +420,6 @@ public final class LinuxRuntimeInstaller {
}
}
/**
* A whole-rootfs tar, which is not the shape the shared extractor handles: a distribution
* rootfs is full of hard links (one binary under several names), and an entry written as an
* empty file instead of its link target is a rootfs that boots to nothing. Symlinks, hard
* links and the executable bit are all carried over here.
*/
public static boolean extract(Context context, File archive, File destination, ProgressListener listener) {
long entries = 0;
String extracting = context.getString(R.string.rtinst_extracting);
@@ -498,7 +428,6 @@ public final class LinuxRuntimeInstaller {
TarArchiveInputStream tar = new TarArchiveInputStream(in)) {
TarArchiveEntry entry;
while ((entry = tar.getNextTarEntry()) != null) {
// Refuse anything that would land outside the runtime directory.
File file = ArchivePaths.inside(destination, entry.getName());
if (file == null) {
Log.w(TAG, "skipping entry outside the rootfs: " + entry.getName());
@@ -515,10 +444,6 @@ public final class LinuxRuntimeInstaller {
file.delete();
FileUtils.symlink(entry.getLinkName(), file.getAbsolutePath());
} else if (entry.isLink()) {
// A hard link to an earlier entry. Link where the filesystem allows it and
// fall back to a copy, which costs space but always works.
// The target is checked too: a link to "../../shared_prefs/x" would pull a
// file from outside the rootfs into it.
File target = ArchivePaths.inside(destination, entry.getLinkName());
if (target == null) {
Log.w(TAG, "skipping hard link outside the rootfs: " + entry.getLinkName());
@@ -540,7 +465,7 @@ public final class LinuxRuntimeInstaller {
}
if ((entry.getMode() & 0111) != 0) file.setExecutable(true, false);
} else {
continue; // device nodes and fifos: the runtime binds the real ones
continue;
}
if (++entries % 2000 == 0 && listener != null) {
listener.onProgress(extracting, -1);
@@ -5,36 +5,14 @@ import com.droiddeck.launcher.session.SessionPrefs
import java.io.File
import java.security.MessageDigest
/**
* proot's fast path: the session's path lookups answered inside each process by libblfastpath
* (tools/proot/fastpath), which issues the translated call from a trampoline page proot's seccomp
* filter lets through (tools/proot/patches/0014). A stat or an open costs about a microsecond
* instead of a 15-60 us round trip through the tracer.
*
* The library maps guest paths to host paths itself, so it has to be told the rootfs and the
* binds exactly as proot was given them. Both sides carry a key derived from that view - proot
* PROOT_FASTPATH=<key>, the guest PROOT_FP_KEY=<key> - and the library answers nothing unless the
* process that traces it has the same key: a program that inherited the variables but runs under
* another proot (a Flatpak sandbox, a one-off command) leaves every call to its own proot.
*
* Needs proot's seccomp filter, so "Run proot without seccomp" turns it off as well.
*/
object ProotFastPath {
const val LIBRARY = "/usr/local/lib/libblfastpath.so"
/** MAX_BINDS in fastpath.c: past it the library turns itself off, so the session keeps it off. */
const val MAX_BINDS = 256
/**
* Only the app's own proot (libproot.so, built with patches/0014) lets the trampoline through.
* The runtime's fallback copy (LinuxRuntime.prootBinary) is Termux's, which traps those calls
* too and translates their host paths a second time: every lookup then fails inside the
* process and goes to proot anyway, paying for both.
*/
fun enabled(context: Context): Boolean =
SessionPrefs.prootFastPath(context) && !SessionPrefs.prootNoSeccomp(context) &&
LinuxRuntime.prootBinary(context).parent == context.applicationInfo.nativeLibraryDir
/** The key for this rootfs and these binds, or null when the library cannot be told them. */
fun key(root: File, binds: List<String>): String? {
if (binds.size > MAX_BINDS || binds.any { it.contains('|') } || root.path.contains('|')) return null
val digest = MessageDigest.getInstance("SHA-256")
@@ -42,10 +20,8 @@ object ProotFastPath {
return digest.take(12).joinToString("") { "%02x".format(it) }
}
/** What proot's own environment carries. */
fun hostEnv(key: String): Pair<String, String> = "PROOT_FASTPATH" to key
/** The guest's assignments (for `env -i`), describing the view proot was given. */
fun guestEnv(root: File, binds: List<String>, key: String): List<String> = listOf(
"PROOT_FP_ROOT=" + root.path,
"PROOT_FP_BINDS=" + binds.joinToString("|"),
@@ -12,11 +12,9 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.LongConsumer;
import java.util.function.Predicate;
/** Checked removal of an app-owned runtime tree, including read-only directories. */
final class RuntimeFileTree {
private RuntimeFileTree() {}
/** Unit tests stand in for an entry the app cannot unlink (a file another SELinux user made). */
static volatile Predicate<Path> refuseForTest;
static void delete(File root, LongConsumer progress) throws IOException {
@@ -28,8 +26,6 @@ final class RuntimeFileTree {
try { attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); }
catch (NoSuchFileException e) { return; }
if (attrs.isDirectory()) {
// Change directory permissions before opening it, so mode 000 also works. Links
// are inspected with lstat and unlinked directly; never chmod or visit their target.
makeWritable(path.toFile());
try (DirectoryStream<Path> children = Files.newDirectoryStream(path)) {
for (Path child : children) delete(child, progress, removed);
@@ -39,10 +35,6 @@ final class RuntimeFileTree {
if (progress != null) progress.accept(++removed[0]);
}
/**
* Removes what it can of {@code root} and carries on past entries it cannot, rather than
* stopping at the first. Returns true when nothing is left.
*/
static boolean deleteWhatCan(File root, LongConsumer progress) {
return deleteWhatCan(root.toPath(), progress, new long[]{0});
}
@@ -12,15 +12,6 @@ import android.os.IBinder
import com.droiddeck.launcher.MainActivity
import com.droiddeck.launcher.R
/**
* Keeps the process in the foreground while the Linux runtime downloads and unpacks. The runtime
* is several hundred megabytes; without a foreground service Android may kill the app the moment
* the user switches away, and the install starts over from the resumable part file.
*
* The install itself is [LinuxRuntimeInstaller.install], which runs one job per process: the
* screen that asked for it joins the same job to show progress, so it does not matter which of the
* two gets there first.
*/
class RuntimeInstallService : Service() {
override fun attachBaseContext(newBase: android.content.Context) =
super.attachBaseContext(com.droiddeck.launcher.core.AppLanguage.wrap(newBase))
@@ -39,7 +30,6 @@ class RuntimeInstallService : Service() {
val manager = getSystemService(NotificationManager::class.java)
var shown = ""
val progress = LinuxRuntimeInstaller.ProgressListener { stage, percent ->
// Install percentages and throttled removal counts, including stage transitions.
if ("$stage:$percent" != shown) {
shown = "$stage:$percent"
manager?.notify(NOTIFICATION_ID, notification(stage, percent))
@@ -87,7 +77,6 @@ class RuntimeInstallService : Service() {
private const val EXTRA_SHA256 = "sha256"
private const val EXTRA_SIZE = "size"
/** Starts the install of [release] in the foreground; a running install is joined instead. */
fun start(context: Context, release: LinuxRuntimeInstaller.Release) {
val intent = Intent(context, RuntimeInstallService::class.java)
.putExtra(EXTRA_VERSION, release.version)
@@ -19,20 +19,10 @@ import org.json.JSONObject
import java.io.File
import java.io.RandomAccessFile
/**
* The apps a user adds to the Desktop page: shell scripts, AppImages (from storage or a GitHub
* release) and Flatpak apps. Each opens full screen under gamescope, like an emulator.
*
* A script lives at /opt/droiddeck-apps/<id>: its folder at `files`, the script's name in `entry`.
* The folder is linked where it lies; shared storage is mounted noexec, so a folder holding Linux
* programs is copied in instead. A linked folder outside internal storage is bound into the
* session at its own path ([binds]), so the link resolves inside the guest.
*/
object UserApps {
private const val TAG = "UserApps"
const val GUEST_DIR = "/opt/droiddeck-apps"
const val SCRIPT_LAUNCHER = "/usr/local/bin/droiddeck-script-run"
/** An app the desktop asked for (droiddeck-open): its key, then the path it was started from there. */
const val DESKTOP_IMPORT = "desktop-import"
private const val FLATPAK_OVERRIDES = ".flatpak"
private const val ICON_SIZE = 256
@@ -47,10 +37,6 @@ object UserApps {
enum class Kind { SCRIPT, APPIMAGE, FLATPAK }
/**
* [copied]: a script whose folder was copied in rather than linked. [repo]: the GitHub repository
* an AppImage came from, [version] the release it is.
*/
class App(
val key: String, val kind: Kind, val name: String, val icon: Any?, val detail: String?,
val program: String, val args: List<String>, val copied: Boolean = false,
@@ -74,10 +60,8 @@ object UserApps {
class FlatpakBundle(val file: File) : Source()
}
/** [icon]: a picked file's path or a suggested icon's URL. [meta]: files written into the added app's folder. */
class Request(val source: Source, val name: String?, val icon: String?, val meta: Map<String, String> = emptyMap())
/** How a script's folder reaches the session: linked in place, or copied in ([bytes] to copy). */
class ScriptPlan(val folder: File, val copy: Boolean, val bytes: Long)
private fun root(context: Context) = File(LinuxRuntime.rootDir(context), GUEST_DIR.substring(1))
@@ -117,7 +101,6 @@ object UserApps {
private fun readName(dir: File) = FileUtils.readString(File(dir, "name"))?.trim()?.takeIf { it.isNotEmpty() }
/** Adds [request]; null on success, else what went wrong. */
fun add(context: Context, request: Request, onProgress: (String, Int) -> Unit): String? {
if (!LinuxRuntime.isInstalled(context)) return context.getString(R.string.user_apps_runtime_required)
val name = request.name?.trim()?.takeIf { it.isNotEmpty() }
@@ -132,7 +115,6 @@ object UserApps {
}
}
/** [icon] as a file for [use]: a path as it is, a URL downloaded for the while. */
private inline fun <T> withIcon(context: Context, icon: String?, use: (File?) -> T): T {
if (icon == null || !icon.startsWith("https://")) return use(icon?.let(::File))
val download = File(context.cacheDir, "icon-${System.nanoTime()}")
@@ -143,10 +125,6 @@ object UserApps {
}
}
/**
* Renames [app] and sets its icon: [icon] a path or URL, "" for its default, null to keep it.
* The Linux desktop's menu entry follows.
*/
fun edit(context: Context, app: App, name: String, icon: String?, fex: String? = null): String? {
val id = app.key.substringAfter(':')
if (id.isEmpty() || '/' in id || id.startsWith(".")) return null
@@ -177,7 +155,6 @@ object UserApps {
return when (app.kind) {
Kind.SCRIPT -> {
val dir = File(root(context), id)
// The link goes, never the folder it points to.
File(dir, "files").delete()
FileUtils.delete(dir)
menuEntry(context, id).delete()
@@ -190,7 +167,6 @@ object UserApps {
}
}
/** Folders linked from outside internal storage, bound into a session at their own paths. */
fun binds(context: Context): List<String> {
val internal = Environment.getExternalStorageDirectory().canonicalPath
return root(context).listFiles().orEmpty().mapNotNull { dir ->
@@ -199,7 +175,6 @@ object UserApps {
}.distinct()
}
/** The desktop's import of [key] as the session sees it: an added script's folder, or an AppImage's. */
fun desktopImport(context: Context, key: String, path: String): String? {
root(context).listFiles()?.firstOrNull { dir ->
File(dir, "entry").isFile && FileUtils.readString(File(dir, DESKTOP_IMPORT))?.lineSequence()?.firstOrNull() == key
@@ -263,7 +238,6 @@ object UserApps {
"Terminal=false\nCategories=Game;\nX-DroidDeck-App=$id\n")
}
/** Copies [from] into [to], keeping Linux programs and scripts executable. */
private fun copyTree(from: File, to: File) {
from.walkTopDown().forEach { src ->
val dst = File(to, src.relativeTo(from).path)
@@ -275,7 +249,6 @@ object UserApps {
}
}
/** [source] (PNG, JPEG, WebP or SVG) as the app's icon: a PNG no larger than [ICON_SIZE] on a side. */
internal fun saveIcon(source: File, target: File): Boolean {
val bitmap = (if (isSvg(source)) renderSvg(source) else decodeScaled(source)) ?: return false
return try {
@@ -306,7 +279,6 @@ object UserApps {
f.inputStream().use { val head = ByteArray(1024); val n = it.read(head); n > 0 && String(head, 0, n).contains("<svg") }
} catch (e: Exception) { false }
/** An SVG drawn at [ICON_SIZE], centred in its own proportions. */
private fun renderSvg(source: File): Bitmap? = try {
val svg = source.inputStream().use { SVG.getFromInputStream(it) }
if (svg.documentViewBox == null && svg.documentWidth > 0 && svg.documentHeight > 0) {
@@ -319,7 +291,6 @@ object UserApps {
Log.w(TAG, "svg ${source.path}", e); null
}
/** "owner/repo" from what was typed: any link into the repository, its clone address, or the short form. */
fun githubRepo(input: String): String? {
val t = input.trim()
val m = Regex("^(?:https?://)?(?:www\\.)?github\\.com[/:]([\\w.-]+)/([\\w.-]+)", RegexOption.IGNORE_CASE).find(t)
@@ -329,10 +300,6 @@ object UserApps {
return if (repo.isEmpty() || repo == "." || repo == "..") null else "${m.groupValues[1]}/$repo"
}
/**
* The AppImage among a release's files: named for aarch64/arm64, else for no other CPU, else the
* x86_64 one, which runs through FEX.
*/
internal fun pickAsset(names: List<String>): String? {
val images = names.filter { it.endsWith(".appimage", ignoreCase = true) }
return images.firstOrNull { Regex("aarch64|arm64", RegexOption.IGNORE_CASE).containsMatchIn(it) }
@@ -340,7 +307,6 @@ object UserApps {
?: images.firstOrNull { Regex("x86_64|x86-64|amd64|x64", RegexOption.IGNORE_CASE).containsMatchIn(it) }
}
/** The newest release of [repo] with an AppImage this device runs, preferring full releases; else what went wrong. */
private fun latestRelease(context: Context, repo: String): Pair<Release?, String?> {
val body = Downloader.downloadString("https://api.github.com/repos/$repo/releases?per_page=15")
val releases = body?.let { runCatching { JSONArray(it) }.getOrNull() }
@@ -358,7 +324,6 @@ object UserApps {
return pick to if (pick == null) context.getString(R.string.user_apps_github_no_appimage, repo) else null
}
/** Downloads [release] and hands it to [use]; null on success, else what went wrong. */
private fun withDownload(context: Context, release: Release, onProgress: (String, Int) -> Unit, use: (File) -> String?): String? {
val download = File(context.cacheDir, "github-${release.asset}")
try {
@@ -382,10 +347,6 @@ object UserApps {
}
}
/**
* Images in [repo] that look like its icon, best first, then its owner's picture: links to show
* as suggestions and to download once one is picked.
*/
fun githubIcons(repo: String): List<String> {
val info = Downloader.downloadString("https://api.github.com/repos/$repo")?.let { runCatching { JSONObject(it) }.getOrNull() } ?: return emptyList()
val branch = info.optString("default_branch").ifEmpty { return emptyList() }
@@ -410,13 +371,11 @@ object UserApps {
return urls + listOfNotNull(avatar)
}
/** Whether [app], added from GitHub, has a newer release than the one it is. */
fun checkUpdate(context: Context, app: App): UpdateCheck {
val repo = app.repo ?: return UpdateCheck.Failed(context.getString(R.string.user_apps_update_failed))
val (release, problem) = latestRelease(context, repo)
if (release == null) return UpdateCheck.Failed(problem ?: context.getString(R.string.user_apps_update_failed))
val published = FileUtils.readString(File(AppImageManager.dir(context, app.key.substringAfter(':')), GITHUB_PUBLISHED))?.trim().orEmpty()
// Releases are compared by date, so a newest one without an ARM64 build never reads as a downgrade.
val newer = if (published.isNotEmpty() && release.published.isNotEmpty()) release.published > published else release.tag != app.version
return if (newer) UpdateCheck.Available(release) else UpdateCheck.Current(app.version ?: release.tag)
}
@@ -428,7 +387,6 @@ object UserApps {
}
}
/** A Flathub app ID from what was typed: the ID itself or its Flathub page. */
fun flatpakId(input: String): String? {
val t = input.trim().removeSuffix("/").substringAfterLast('/')
return t.takeIf { Regex("^[A-Za-z][\\w-]*(\\.[A-Za-z0-9_][\\w-]*){2,}$").matches(it) }
@@ -444,7 +402,6 @@ object UserApps {
private fun addFlatpakBundle(context: Context, file: File, name: String?, icon: File?, onProgress: (String, Int) -> Unit): String? {
if (!file.isFile) return context.getString(R.string.user_apps_file_gone)
if (!FlatpakManager.ready(context)) FlatpakManager.setup(context, onProgress)?.let { return it }
// Shared storage is bound into the runtime at its own path; a file anywhere else is copied in.
val shared = Environment.getExternalStorageDirectory().canonicalPath
val copy = File(LinuxRuntime.rootDir(context), "tmp/droiddeck-bundle.flatpak").takeIf { !file.canonicalPath.startsWith("$shared/") }
try {