# brintos-consoled — start a shell on /dev/pts/ when the host asks. # # RUN IT THROUGH A SHELL (`sh brintos-consoled …`, `bash brintos-consoled …`), # never by path: a `#!` script ENOENTs on exec on this arch (binfmt_script -> # binfmt_wasm re-entry gap; see the busybox rig's inittab note), which is also # why this file carries no #! line. # # The console tabs past ttyS1 are kernel console rings 2 and up (pty ring ABI v3, # arch/wasm32/include/asm/tty_bridge.h). The kernel opens every ring's pty # master at boot and holds it, so /dev/pts/ exists from the first moment # and nothing is on it. Ring 1 — /dev/pts/1 — is this script's line to the # host: # # host -> here `open ` start a shell on /dev/pts/ (k >= 2) # `display ` # start display d's terminal (d >= 1): the # renderer /sbin/brintos-vtfb drawing tty # on /dev/fb and typing what arrives on # /dev/pts/ (k >= 2) into it, and a shell # on tty (docs/plans/PLAN-MACHINE- # DISPLAYS.md) # here -> host `ready` once, when this script is listening # `exited ` that console's shell is gone — by `exit`, or # by the hangup a tab's close button sends; for # a display, its shell exited or the host hung # ring up (the Screen was deleted), and # nothing holds /dev/fb open any more # # Until `ready` arrives the host offers no new-console button, so an image that # does not start this script shows ttyS1 alone rather than a button that does # nothing. The host is the one that knows which consoles are open; it only # asks for a it believes free, and waits for `exited ` before reusing it # — and, for a display, before unplugging it. # # Usage: brintos-consoled CMD [ARGS...] # # CMD runs once per console with stdin/stdout/stderr on /dev/pts/ — or, for # a display, on /dev/tty. An argument spelled @TTY@ is replaced with that # device's name, `pts/` or `tty` (getty wants the name). CMD must make # the tty its controlling terminal itself — `setsid -c bash -i`, or # `getty -n -l /bin/sh 38400 @TTY@` — so the shell has job control and the # hangup reaches it. ctl=/dev/pts/1 if [ ! -c "$ctl" ]; then # Said once, then idle rather than exit: under busybox init this runs # as `respawn`, and an immediate exit would restart it in a tight loop. echo "brintos-consoled: no $ctl — this kernel has no console rings" >&2 exec sleep 2147483647 fi [ $# -gt 0 ] || { echo "brintos-consoled: usage: brintos-consoled CMD [ARGS...]" >&2; exit 2; } # PS1 ARRIVES UNDER ANOTHER NAME. Run as a script, bash deletes PS1 and PS2 # at startup — a non-interactive shell has no prompt — so on the bash rigs # every console came up as "bash-5.2#" beside a ttyS1 showing user@host:cwd. # Measured on dev, that was the whole difference: each console's environment # held everything pid 1 was given (PATH, HOME, TERM, PWD, ENV) but PS1. The # caller passes it as BRINTOS_CONSOLE_PS1, which bash leaves alone, and it is # exported back here for every console. (/proc/1/environ cannot stand in: on # this kernel it reads empty — the host hands pid 1 its environment.) if [ -n "${BRINTOS_CONSOLE_PS1-}" ]; then PS1=$BRINTOS_CONSOLE_PS1 export PS1 fi unset BRINTOS_CONSOLE_PS1 cd / || : exec 3<>"$ctl" || exit 1 exec /dev/null # Echo is off so the host reads back only what this script says; canonical # mode stays on, which is what makes `read` below line-at-a-time. Best effort: # a host that sees its own `open ` echoed ignores it. stty -echo <&3 2>/dev/null || : # spawn TTY CMD...: CMD with stdio on /dev/TTY, @TTY@ spelled TTY. spawn() { tty=$1 shift n=$# while [ "$n" -gt 0 ]; do a=$1 shift [ "$a" = @TTY@ ] && a=$tty set -- "$@" "$a" n=$((n - 1)) done "$@" <>"/dev/$tty" >&0 2>&0 3>&- } # display K D FB CMD...: display D's terminal. The renderer draws tty on # /dev/fb and types ring K's input into it; CMD runs on tty. Either # end finishes both: the shell exiting stops the renderer below, and the host # hanging ring K up ends the renderer's input, so it hangs the VT up, which # ends the shell. The renderer is waited for before `exited ` is said — it # is what holds /dev/fb open, and the host unplugs the display on that. display() { k=$1 vt=$(($2 + 1)) fb=$3 shift 3 # A VT keeps its text and cursor after its shell has gone, so a Screen # made again on the same display showed the new prompt beside the old # one (owner, 2026-10-25). It starts reset (RIS), before the renderer # draws it and the shell writes to it. printf '\033c' >"/dev/tty$vt" 2>/dev/null || : /sbin/brintos-vtfb "/dev/fb$fb" "$vt" <>"/dev/pts/$k" >&0 2>&0 3>&- & r=$! # A VT is a Linux console, not the host's xterm.js that pid 1's TERM # names — the tty1 shells say TERM=linux for the same reason. Nor is it # described by the xterm entry that arrives inline beside it: GNU termcap # takes an inline TERMCAP for whatever name equals $TERM, so a Screen's # shell kept xterm's keys and End printed a ~. Unset, it reads # /etc/termcap (distros/common/guest/termcap). TERM=linux export TERM unset TERMCAP spawn "tty$vt" "$@" kill "$r" 2>/dev/null wait "$r" } echo ready >&3 while read -r verb k d fb rest <&3; do # A console is any /dev/pts/ from 2 up that the kernel made a ring for # (how many is the kernel's call — init.c); anything else is not asked. case "$verb:$k" in open:*[!0-9]* | open:) continue ;; open:*) ;; display:*[!0-9]* | display:) continue ;; display:*) # d >= 1 (display 0 is fbcon's, on tty1), spelled without a # leading 0, which $(( )) would read as octal. case $d in '' | *[!0-9]* | 0*) continue ;; esac case $fb in '' | *[!0-9]* | 0?*) continue ;; esac ;; *) continue ;; esac [ "$k" -ge 2 ] && [ -c "/dev/pts/$k" ] || continue if [ "$verb" = open ]; then ( spawn "pts/$k" "$@"; echo "exited $k" >&3 ) & else ( display "$k" "$d" "$fb" "$@"; echo "exited $k" >&3 ) & fi done echo "brintos-consoled: $ctl closed; no new consoles until this restarts" >&2 sleep 1 # a respawning init restarts this; never in a tight loop