A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
refactor: route terminal over virtio-console instead of PL011 UART
ExTv617a94a13546
diff --git a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.ktindex ce2224a..fa1ae8a 100644--- a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt+++ b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt@@ -64,6 +64,7 @@ class PodroidQemu @Inject constructor(/** Unix socket paths exposed to TerminalViewModel for the bridge binary. */val serialSockPath: String get() = "${context.filesDir.absolutePath}/serial.sock"+ val terminalSockPath: String get() = "${context.filesDir.absolutePath}/terminal.sock"val ctrlSockPath: String get() = "${context.filesDir.absolutePath}/ctrl.sock"/** Boot-monitoring socket connection. Closed when the bridge takes over. */@@ -148,32 +149,24 @@ class PodroidQemu @Inject constructor(android.os.Handler(android.os.Looper.getMainLooper()).post {if (_terminalSession != null) return@postif (_state.value !is VmState.Running && _state.value !is VmState.Starting) return@post-- // Release the boot monitor connection.- releaseSerial()-- // IMPORTANT: QEMU serial socket ('server,nowait') takes a beat to- // re-listen after a client disconnects. If the bridge tries to- // connect immediately, it may hit ECONNREFUSED.- android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({- if (_terminalSession != null) return@postDelayed- val bridgeExe = File(context.applicationInfo.nativeLibraryDir, "libpodroid-bridge.so")- if (!bridgeExe.exists()) return@postDelayed-- val sess = TerminalSession(- bridgeExe.absolutePath,- context.filesDir.absolutePath,- arrayOf(bridgeExe.absolutePath, serialSockPath, ctrlSockPath),- null,- 2000,- proxySessionClient,- )- sess.updateSize(80, 24)- replayBootOutput(sess)- _terminalSession = sess- _terminalSessionAttached = false- Log.d(TAG, "Bridge auto-started (after 500ms delay)")- }, 500)++ // Bridge connects to terminal.sock (virtio-console, separate from serial).+ // No handoff with the boot monitor needed — they use different sockets.+ val bridgeExe = File(context.applicationInfo.nativeLibraryDir, "libpodroid-bridge.so")+ if (!bridgeExe.exists()) return@post++ val sess = TerminalSession(+ bridgeExe.absolutePath,+ context.filesDir.absolutePath,+ arrayOf(bridgeExe.absolutePath, terminalSockPath, ctrlSockPath),+ null,+ 2000,+ proxySessionClient,+ )+ sess.updateSize(80, 24)+ _terminalSession = sess+ _terminalSessionAttached = false+ Log.d(TAG, "Bridge auto-started on terminal.sock")}}@@ -187,9 +180,7 @@ class PodroidQemu @Inject constructor(return _terminalSession!!}- // Fallback: create session now (boot monitor may not have auto-started it)- releaseSerial()-+ // Fallback: create session nowval bridgeExe = File(context.applicationInfo.nativeLibraryDir, "libpodroid-bridge.so")if (!bridgeExe.exists()) {throw IllegalStateException("podroid-bridge not found at ${bridgeExe.absolutePath}")@@ -198,7 +189,7 @@ class PodroidQemu @Inject constructor(val sess = TerminalSession(bridgeExe.absolutePath,context.filesDir.absolutePath,- arrayOf(bridgeExe.absolutePath, serialSockPath, ctrlSockPath),+ arrayOf(bridgeExe.absolutePath, terminalSockPath, ctrlSockPath),null,2000,proxySessionClient,@@ -247,6 +238,7 @@ class PodroidQemu @Inject constructor(// included — a leftover file from a crashed QEMU prevents the new// process from binding its QMP server socket.File(serialSockPath).delete()+ File(terminalSockPath).delete()File(ctrlSockPath).delete()File(qmpSocketPath).delete()@@ -451,6 +443,7 @@ class PodroidQemu @Inject constructor(consoleBuilder.clear()_consoleText.value = ""File(serialSockPath).delete()+ File(terminalSockPath).delete()File(ctrlSockPath).delete()_bootStage.value = ""}@@ -529,12 +522,16 @@ class PodroidQemu @Inject constructor(args += "-netdev"; args += netdevArgargs += "-device"; args += "virtio-net-pci,netdev=net0,romfile="- // ── Serial console (ttyAMA0) → unix socket for podroid-bridge ─────────+ // ── Serial (ttyAMA0) → boot log sink only; kernel msgs + init boot stages ─args += "-serial"; args += "unix:$serialSockPath,server,nowait"- // ── Control channel (hvc0) → unix socket for resize signalling ────────- args += "-chardev"; args += "socket,id=ctrl0,path=$ctrlSockPath,server=on,wait=off"+ // ── virtio-console bus ────────────────────────────────────────────────+ // hvc0 = primary terminal (getty runs here; bridge connects to terminal.sock)+ // hvc1 = control channel (init daemon reads RESIZE messages from ctrl.sock)args += "-device"; args += "virtio-serial-pci"+ args += "-chardev"; args += "socket,id=term0,path=$terminalSockPath,server=on,wait=off"+ args += "-device"; args += "virtconsole,chardev=term0,name=org.podroid.term"+ args += "-chardev"; args += "socket,id=ctrl0,path=$ctrlSockPath,server=on,wait=off"args += "-device"; args += "virtconsole,chardev=ctrl0,name=org.podroid.ctrl"args += "-display"; args += "none"diff --git a/init-podroid b/init-podroidindex ed83696..d934ab8 100755--- a/init-podroid+++ b/init-podroid@@ -191,15 +191,15 @@ dmesg -n 1 2>/dev/nullstty rows 24 cols 80 -ixon -ixoff -iexten 2>/dev/nulltrap 'podman stop -a -t 5 2>/dev/null; podman network prune -f 2>/dev/null; sync' TERM INT HUP-# ── Resize daemon ─────────────────────────────────────────────────────────────+# ── Resize daemon (reads RESIZE msgs from hvc1, applies to hvc0 terminal) ─────(- for _i in $(seq 1 30); do [ -c /dev/hvc0 ] && break; sleep 1; done- [ -c /dev/hvc0 ] || exit 0+ for _i in $(seq 1 30); do [ -c /dev/hvc1 ] && break; sleep 1; done+ [ -c /dev/hvc1 ] || exit 0while IFS= read -r _line; docase "$_line" in- RESIZE\ *) set -- $_line; stty rows "$2" cols "$3" </dev/ttyAMA0 2>/dev/null ;;+ RESIZE\ *) set -- $_line; stty rows "$2" cols "$3" </dev/hvc0 2>/dev/null ;;esac- done </dev/hvc0+ done </dev/hvc1) &# ── MOTD + profile ────────────────────────────────────────────────────────────@@ -277,12 +277,10 @@ grep -q '^root:' /etc/passwd 2>/dev/null || \boot_stage "Ready!"rm -f /run/boot_stage-# Hand off serial.sock to bridge — monitor reads "Ready!" then disconnects;-# single-client socket requires this gap before getty takes over.-sleep 0.5-+# getty runs on /dev/hvc0 (virtio-console, primary terminal). The serial port+# (ttyAMA0) stays attached to the boot-monitor socket for console.log.while true; do- /sbin/getty -n -l /usr/local/bin/podroid-login 0 ttyAMA0 xterm-256color+ /sbin/getty -n -l /usr/local/bin/podroid-login 0 hvc0 xterm-256colorsleep 1donefidiff --git a/podroid-bridge.c b/podroid-bridge.cindex 742f486..f592f77 100644--- a/podroid-bridge.c+++ b/podroid-bridge.c@@ -1,16 +1,18 @@/*- * podroid-bridge — Serial socket ↔ PTY relay for Podroid+ * podroid-bridge — virtio-console socket ↔ PTY relay for Podroid** Bidirectional relay between:* stdin/stdout — PTY slave (Termux TerminalSession)- * serial.sock — QEMU unix socket serial (ttyAMA0 in VM)+ * terminal.sock — QEMU virtio-console chardev (/dev/hvc0 in VM; primary terminal)** Architecture:- * - PTY → VM: keystrokes pass through raw; ttyAMA0 handles echo/editing- * - VM → PTY: output filtered for ESC[18t/ESC[19t queries (answered locally)+ * - PTY → VM: keystrokes pass through raw; the VM's getty/bash handles echo/editing.+ * - VM → PTY: output forwarded as-is. virtio-console reports real terminal+ * size via TIOCGWINSZ inside the guest, so we don't need to intercept+ * ESC[18t queries (the old PL011-serial hack).* - SIGWINCH: TerminalSession calls ioctl(TIOCSWINSZ) on PTY master →- * we write "RESIZE rows cols\n" to ctrl.sock → VM daemon applies it- * to ttyAMA0 → foreground process gets SIGWINCH (nvim/vim/htop resize)+ * we write "RESIZE rows cols\n" to ctrl.sock (/dev/hvc1 in VM) → init+ * daemon stty's hvc0 → foreground TUI gets SIGWINCH.** Signals:* SIGPIPE — ignored; EPIPE on write = EOF, handled in main loop@@ -18,7 +20,7 @@* SIGTERM — graceful shutdown* SIGWINCH — async flag, processed in select() loop; fires send_resize() immediately*- * Args: <serial.sock> <ctrl.sock>+ * Args: <terminal.sock> <ctrl.sock>*/#include <errno.h>@@ -36,7 +38,7 @@static volatile sig_atomic_t g_winch = 0;static volatile sig_atomic_t g_shutdown = 0;static int g_ctrl_fd = -1;-static int g_serial_fd = -1;+static int g_term_fd = -1;static void on_winch(int sig) { (void)sig; g_winch = 1; }static void on_term(int sig) { (void)sig; g_shutdown = 1; }@@ -91,42 +93,9 @@ static void send_resize(void) {}}-/* Intercept ESC[18t / ESC[19t (xterm window size queries) from the VM.- * Busybox less sends these when ioctl(TIOCGWINSZ) returns a suspicious size.- * We respond immediately with the real PTY size and strip the query bytes- * so they never reach the Android TerminalEmulator (which ignores them). */-static int filter_queries(char *buf, int n) {- int out = 0;- int respond = 0;- int code = 8;-- for (int i = 0; i < n; ) {- if (i + 4 < n && buf[i] == '\033' && buf[i+1] == '['- && buf[i+2] == '1' && buf[i+4] == 't'- && (buf[i+3] == '8' || buf[i+3] == '9')) {- respond = 1;- code = (buf[i+3] == '8') ? 8 : 9;- i += 5;- continue;- }- buf[out++] = buf[i++];- }-- if (respond) {- struct winsize ws;- memset(&ws, 0, sizeof(ws));- if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_row > 0) {- char resp[32];- int len = snprintf(resp, sizeof(resp), "\033[%d;%d;%dt", code, ws.ws_row, ws.ws_col);- write_all(g_serial_fd, resp, len);- }- }- return out;-}-static void cleanup(void) {- if (g_serial_fd >= 0) { close(g_serial_fd); g_serial_fd = -1; }- if (g_ctrl_fd >= 0) { close(g_ctrl_fd); g_ctrl_fd = -1; }+ if (g_term_fd >= 0) { close(g_term_fd); g_term_fd = -1; }+ if (g_ctrl_fd >= 0) { close(g_ctrl_fd); g_ctrl_fd = -1; }}int main(int argc, char *argv[]) {@@ -142,8 +111,8 @@ int main(int argc, char *argv[]) {}if (argc < 3) return 1;- g_serial_fd = connect_unix(argv[1], 50, 200000);- if (g_serial_fd < 0) {+ g_term_fd = connect_unix(argv[1], 50, 200000);+ if (g_term_fd < 0) {fprintf(stderr, "podroid-bridge: cannot connect to %s\n", argv[1]);return 1;}@@ -175,9 +144,9 @@ int main(int argc, char *argv[]) {fd_set rfds;FD_ZERO(&rfds);- FD_SET(STDIN_FILENO, &rfds);- FD_SET(g_serial_fd, &rfds);- int nfds = (g_serial_fd > STDIN_FILENO ? g_serial_fd : STDIN_FILENO) + 1;+ FD_SET(STDIN_FILENO, &rfds);+ FD_SET(g_term_fd, &rfds);+ int nfds = (g_term_fd > STDIN_FILENO ? g_term_fd : STDIN_FILENO) + 1;struct timeval tv = { .tv_sec = 0, .tv_usec = 50000 };int ret = select(nfds, &rfds, NULL, NULL, &tv);if (ret < 0) { if (errno == EINTR) continue; break; }@@ -185,13 +154,12 @@ int main(int argc, char *argv[]) {if (FD_ISSET(STDIN_FILENO, &rfds)) {int n = read(STDIN_FILENO, buf, sizeof(buf));if (n <= 0) break;- if (write_all(g_serial_fd, buf, n) < 0) break;+ if (write_all(g_term_fd, buf, n) < 0) break;}- if (FD_ISSET(g_serial_fd, &rfds)) {- int n = read(g_serial_fd, buf, sizeof(buf));+ if (FD_ISSET(g_term_fd, &rfds)) {+ int n = read(g_term_fd, buf, sizeof(buf));if (n <= 0) break;- n = filter_queries(buf, n);- if (n > 0 && write_all(STDOUT_FILENO, buf, n) < 0) break;+ if (write_all(STDOUT_FILENO, buf, n) < 0) break;}}