Skip to content

kernel: per-session controlling terminal (fg pgid, TOSTOP, winsize, SIGWINCH) - #172

Merged
ccclosed merged 7 commits into
devfrom
feat/term-pty-jobcontrol
Nov 22, 2026
Merged

ccclosed merged 7 commits into
devfrom
feat/term-pty-jobcontrol

Conversation

@ccclosed

@ccclosed ccclosed commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Per-session controlling terminal for every console session (boot console 0,
terminal window >= 1) instead of one global foreground pgid. Rebased onto
dev after #147/#169/#174 merged; no longer stacked.

What

  • drivers::tty — arrays per session: foreground pgid, shell pgid, TOSTOP,
    winsize; init_shell_for/foreground_pgid_for/set_foreground_pgid_for/
    restore_..._for/tostop_for/winsize_for/set_winsize_for/reset_session.
    Existing no-_for wrappers answer for session 0, so boot-console behaviour is
    unchanged.
  • TOSTOP defaults to off, like Linux. Default-on stopped the background
    desktop compositor with SIGTTOU (seen in live runs). Background reads
    still always get SIGTTIN.
  • Pure winsize model (arch/x86_64/linux/tty.rs): Winsize,
    encode/decode_winsize, surface_winsize, winsize_request,
    winsize_changed + 5 host properties. TIOCGWINSZ answers the session's own
    size; TIOCSWINSZ stores it and queues SIGWINCH to that session's foreground
    group only (no signal on an unchanged size). tty ioctls on /dev/vtinput1
    address session 1.
  • Per-session input: stdin queue, O_NONBLOCK, ^C/^Z latches and foreground
    pid are arrays; stdin reads re-check the queue inside the wait loop and consume
    their own session's scancode ring. shell::jobs/jobs/bg/fg are per
    session; foreground_wait drains foreign ^C/^Z while a window owns the
    keyboard (shell: full kernel-shell session works in the desktop window (stage 2, kernel half) #169 behaviour kept).
  • vtinput::write_session: cooked foreground gets ISIG
    (0x03/0x1a/0x1c -> SIGINT/SIGTSTP/SIGQUIT to that session's pgid) and
    the session's scancode ring; raw foreground gets verbatim bytes in the
    session's stdin queue. fork children inherit the session binding
    (session::inherit_bind, alongside dev's bound_session), exit drops it.
  • Keyboard ownership: opening the session devices / writing /dev/vtinput1
    claims the physical keyboard (vtinput::client_active); IRQ ^C/^Z tracking
    signals only the owner, so a chord typed in the window cannot kill/park the
    window client (the window session gets ISIG instead). reap_dead_client
    returns the keyboard after the client exits; boot-console job control without a
    window is unchanged.

Client: on every Configure (and startup) the terminal converts its client area
to kernel cells (8x18, winsize_cells) and issues TIOCSWINSZ on
/dev/vtinput1 (devfs::set_winsize). After a successful #174 respawn the
remembered size is reset and re-sent to the new kernel session, and a closed
input device is tolerated (FileInput::file() is Option<&File> now).

Evidence

Gates (rebased tree): cargo fmt --check OK; tools/build.py build linked OK;
tools/build.py build --release linked OK; tools/host_tests.py 871 passed /
0 failed
; tools/check_safety.py OK; tools/check_agents_md.py OK.
pagh-terminal crate: full cargo test green (incl. 19 kernel_session
tests).

In-QEMU selftest (tools/e2e.py selftest --cmd "selftest 2", release, BOOTID
verified): 126 routines, 0 failed, 1 skipped twice,
SELFTEST IDEMPOTENCY … diverged=false, green routines include
terminal-session::registry commands route into the window session (#169),
desktop-b::/dev/vtfb1 respawn after session exit (device path) (#174),
console::text grid matches the framebuffer resolution, and
linux-tty::per-session foreground groups + winsize + SIGWINCH.

Live QEMU evidence from the stage-3 run (before the rebase; kernel semantics
unchanged, #169 routing covered by the selftest above): 22/22 checks —
[tty] session 1: shell pgid 9 owns the terminal, lxrun ->
foreground pgid 9 -> 11; Ctrl+Z -> sig=20 stopping thread group 11, shell
regains 11 -> 9, jobs shows [1] Stopped pid=11 pgid=11; bg/fg ->
SIGCONT (resumed) twice with a 12.9 s tick gap while stopped; Ctrl+C ->
sig=2 default-terminate of the window job only; resize maximize
112x33 -> 159x40, restore 159x40 -> 112x33, grip drag 112x33 -> 125x36,
each with SIGWINCH to pgid 11, probe records all three sizes; background
writer: 10 tick lines, zero SIGTTOU, compositor not parked; window exit
returns the boot shell. Evidence: .cache/pagh-agent-notes/qterm3-live/,
notes .cache/pagh-agent-notes/qterm3.md.

Honest gaps

No PTY (/dev/ptmx+pts/N), setsid/TIOCSCTTY (terminal = kernel console
session; tcsetpgrp session rule approximated); only
ICANON/ECHO/ISIG/TOSTOP of termios; background access returns EINTR
rather than restarting after SIGCONT; winsize is the app-facing cell size
while the pixel surface stays screen-sized and is scaled by the client; one
drivers::vt for all sessions; keyboard focus is latched for the client's
lifetime (compositor has no focus signal to the kernel); /dev/vtfb1/vtinput1
address session 1; jobs does not mark a background job stopped on a later
SIGTTIN; TIOCSWINSZ sends SIGWINCH only on change. Version not bumped.

…IGWINCH)

Terminal job-control state was global: one foreground pgid, one TOSTOP bit,
one stdin queue, one set of ^C/^Z latches.  Boot console and every desktop
terminal window therefore fought over one controlling terminal, and the
compositor (a background job of the boot console) was parked by the
default-on TOSTOP the moment it wrote a diagnostic line.

Move the state to per-console-session arrays in drivers::tty (foreground
pgid, shell pgid, TOSTOP, winsize) and in the input layer (stdin queue,
O_NONBLOCK, ^C/^Z latches, foreground pid).  io_sys resolves the session of
the calling task (owner or compat bind) for read/write/ioctl, and the
/dev/vtinput1 fd explicitly addresses session 1.  TIOCGWINSZ now answers the
session's own size; TIOCSWINSZ stores it and queues SIGWINCH to that session's
foreground group only.  TOSTOP defaults to off, like Linux, so background
writers (notably the compositor) are not stopped; foreground-group handoff,
SIGTTIN on background reads and the boot-console behaviour are unchanged.

fork() children inherit the console-session binding (session::inherit_bind)
and finish_compat_exit drops it, so subprocesses of a window job keep their
terminal instead of leaking output to the boot console.  Winsize encoding is
a pure, host-tested model in arch/x86_64/linux/tty.rs.
jobs/fg/bg used one global table, so a window and the boot shell saw each
other's stopped jobs and could resume/inspect the wrong process group.  The
table now lives per console session, and every job-control path resolves the
state of the session it runs in: foreground wait consumes that session's ^C/^Z
latches, hands that session's terminal to the child pgid (tty::*_for), records
jobs in that session's table and restores that session's shell foreground.
lxrun also publishes the child's foreground pid per session before interrupts
are re-enabled, so the window's /dev/vtinput1 routes cooked input into the
job's terminal discipline from its first byte.

A window shell now calls tty::init_shell_for(session, pid) too: its foreground
group, TOSTOP and winsize are its own, and the boot console keeps exactly the
old behaviour.
Host property tty_job gains five properties over the pure winsize model:
struct winsize round trip/UAPI layout, surface->cells division with the
one-cell floor, classification of exactly TIOCGWINSZ/TIOCSWINSZ, fieldwise
change detection (the SIGWINCH condition), and independence of two terminals
(the pure restatement of per-session tty).

In-QEMU routine linux-tty::per-session foreground groups + winsize + SIGWINCH
proves on the real machine what the property cannot: sessions 0 and 1 keep
independent foreground groups, TOSTOP, winsize, foreground pid, ^C/^Z latches
and stdin queues; TIOCSWINSZ on session 1 queues SIGWINCH only to its
foreground group and not on an unchanged size; TIOCGWINSZ on fd 0 reports the
caller's session size in the Linux wire layout.

Docs state the new contract (per-session terminal, TOSTOP off by default, the
resize path window -> TIOCSWINSZ -> SIGWINCH) and the honest boundary that the
pixel surface is still screen-sized/scaled by the client while winsize is the
app-facing cell size.
The kernel now keeps winsize/SIGWINCH per console session, but the window client
never told it the window changed size: a TUI inside the session could not react
to a compositor resize.  On every Configure (and once at startup) the client
converts its client area to the kernel cell grid (8x18 px, integer division,
one-cell floor, u16 clamp) and issues TIOCSWINSZ on /dev/vtinput1.  The kernel
stores the session's size and queues SIGWINCH to its foreground group; an
unchanged size is a no-op on both sides.

The ioctl stays in the devfs unsafe module; the cell arithmetic is a pure
host-tested function (tests/kernel_session.rs).  The client documents the resize
contract and the honest boundary: winsize is the app-facing cell size, the
pixel surface is still screen-sized and scaled by the client.

Stacked on origin/feat/terminal-kernel-client (PR #147) so the live window can
be driven end to end before #147 merges; rebase once it lands in dev.
A QEMU/PS/2 keypress is seen twice by design: paging IRQ1 queues it for the boot
shell ring while evdev feeds the compositor.  For a kernel-session window the
same chord therefore reached TWO terminals: the compositor forwarded it to
session 1 (correct) and the IRQ-level ^C/^Z tracking acted on the boot shell's
foreground job (the terminal client itself), so Ctrl+Z parked the client and
Ctrl+C killed it; the window vanished mid-check (live-QEMU, run 2).

Opening /dev/vtfb1//dev/vtinput1 (and any write to /dev/vtinput1) now claims
the physical keyboard through the existing vtinput::client_active latch, and
IRQ tracking applies ^C/^Z terminal semantics only to the keyboard OWNER.  While
a window is alive the boot shell ring stays empty and the session's own ISIG
path owns the chords; reap_dead_client returns the keyboard after the client
exits.  Boot-console behaviour without a window is unchanged.

Verified live: Ctrl+Z stops only the window job (SIGTSTP to session 1's pgid),
Ctrl+C terminates only it, the client keeps painting, and the boot shell still
answers after the window session exits.
#174 boxes the kernel-session frame source and closes both devices between
ENODEV and respawn, so FileInput::file() is now Option<&File>.  The resize path
must not assume the input device is always open: TIOCSWINSZ is skipped while it
is closed, and a successful respawn resets the remembered size and re-sends the
window's winsize to the NEW kernel session (which starts with the default size
again).  Without this a TUI restarted in the same window (Enter after )
would keep a stale geometry.

Host tests: full pagh-terminal cargo test green.
@ccclosed
ccclosed force-pushed the feat/term-pty-jobcontrol branch from dbe3ec0 to 4b49af8 Compare November 22, 2026 07:05
@ccclosed
ccclosed merged commit 7b3e5a7 into dev Nov 22, 2026
8 checks passed
@ccclosed
ccclosed deleted the feat/term-pty-jobcontrol branch November 22, 2026 10:00
ccclosed pushed a commit that referenced this pull request Nov 22, 2026
Second sync in this round: dev moved from c1853ad to 7b3e5a7 with
#172 terminal PTY job control on top of #177 i8042/ppkg, #174 respawn and
the terminal docs. This merge closes the real DIRTY state: dev changed
io_sys (per-session tty ioctl) and test.rs while both files also carry
USB S1 hooks.

Conflicts:
- src/arch/x86_64/linux/io_sys.rs: kept the USB
  `/dev/uevent` ioctl arm and merged the dev per-session tty path —
  final order is fbdev, vtconsole, vtfb1, uevent, vtinput1, then
  Console/Stdin -> tty_ioctl(session::current(), ...).
- src/test.rs: kept BOTH the dev per-session-tty selftest routine
  ("linux-tty::per-session foreground groups + winsize + SIGWINCH") and
  the USB "usb::BOT/SCSI/uevent layouts and /usb root rule" routine.

USB S1 files are otherwise untouched; all dev i8042/ppkg/frame-stats/
PTY-job-control/respawn changes are preserved.

Post-merge evidence: gates 6/6 (host 893/0), QEMU usb-storage acceptance
46/46, release-kernel selftest 127 routines / 0 failed / 1 skipped
(NVMe absent), one flocked run.
ccclosed added a commit that referenced this pull request Nov 22, 2026
… tty

Conflicts (8 files) and their resolution:

* `src/drivers/session.rs` — `exit()` keeps our per-slot `OPEN_FDS`/`OPEN_CLIENT`
  reset AND #172's `jobs::clear_session` + `tty::reset_session`; added
  `open_client(session)` for the focus-aware keyboard latch.
* `src/drivers/vtinput.rs` — `write_session` takes #172's PTY path
  (`fg_pid(session)` + `deliver_to_job`: raw→stdin queue, cooked→ISIG +
  session scancode ring). The window keeps #172's physical-keyboard latch, but
  it is now **focus-aware**: `VTINPUT_SET_FOCUS(true)` re-claims it for the
  slot's client and `false` releases it, so clicking the desktop returns the
  keyboard to the boot shell instead of waiting for the window to close.
  `deliver_physical` still suppresses the boot ring while any focused session
  is live.
* `src/arch/x86_64/linux/io_sys.rs` — one dispatch for any `vtinput{N}`:
  our `VTINPUT_SET_FOCUS` first, otherwise #172's per-session
  `tty_ioctl(session, …)` (winsize/SIGWINCH, TIOCGPGRP/SPGRP). vtfb dispatch
  stays session-indexed (slots 1..3), console/stdin use #172's tty_ioctl with
  the task's session.
* `src/drivers/README.md` — our vtinput/vtmouse/session rows, with the latch
  described as focus-aware and #172's PTY/pass-through facts.
* `rust-apps/pagh-terminal/src/bin/pagh-terminal.rs` — our slot scan/EBUSY,
  `--vt-mouse`, `MouseForwarder`, pid-derived window id and focus ioctl kept;
  #172's `forward_winsize` (`TIOCSWINSZ` on the key sink, re-sent after
  respawn) merged into the same loop. A stray `KeyForwarder::new(key_sink)`
  from the merged side removed (devices already carry the forwarder), and
  winsize forwarding goes through `keys.sink()`, not the mouse option.
* `src/kernelsession.rs` — our `MouseForwarder` + #172's `file()`; duplicate
  `file()` impl removed (kept #172's, which returns `Option<&File>` for the
  close/respawn window).
* `src/tests/kernel_session.rs` — our pointer/EBUSY/window-id tests and
  #172's `winsize_cells` test both kept.
* `TERMINAL.md`, `docs/terminal-session.md` — #172's §1.5 (per-session
  foreground group, TOSTOP off by default, TIOCGWINSZ/TIOCSWINSZ/SIGWINCH,
  per-session jobs) documented as working; our vtmouse/focus/multi-slot
  sections kept; the old "no job control / no winsize / TOSTOP mandatory"
  claims dropped as outdated. Remaining gaps: no /dev/pts, no real pixel
  resize, DEC/alternate screen, session-scoped clipboard, max 3 windows.

Not broken: job control keeps its per-session fg pgid/TOSTOP/SIGWINCH; the
only changed interaction is that the keyboard latch now follows compositor
focus, which is what makes Ctrl+C/Ctrl+Z in a window hit that window only
while still returning the keyboard to the boot shell after a desktop click.
ccclosed added a commit that referenced this pull request Nov 22, 2026
…dow acceptance

Resolve the two conflicts by taking dev's versions of the tooling and
re-adding only what #171 still owns on top:

* tools/e2e.py — dev's file (termux stage-4 mode included) + the
  'termwindow' mode re-inserted on top of its helpers. _desktop_bins and
  _desktop_prepare_disk are reused; the latter gains the optional
  size_mb argument (64 MiB default keeps every existing mode unchanged),
  because ppkg install needs a 256 MiB volume.
* docs/terminal-session.md — dev's rewritten §6 kept; a short §7 documents
  the live acceptance and points at §6.10 (no alternate screen/DEC modes)
  instead of claiming ncurses interactivity.
* tools/README.md — dev's text kept, mode table lists termux and
  termwindow, the termwindow bullet matches what the mode now does
  (ncurses-base, -I, unverified ncurses interactivity).
* tools/qemu_shot.py — unchanged addition: dev does not have chord
  support, and the mode needs 'ctrl-s'/'ctrl-o' via HMP.

The Linux (ncurses) nano step is now recorded as info_check (unverified)
when the editor reaches read(2) but never accepts the chords: that is the
named §6.10 limitation of drivers::vt, not a stage-2 window-session claim.
ccclosed pushed a commit that referenced this pull request Nov 22, 2026
Sync with dev=f1453f55 (#167 multi-session terminal lifecycle on top of
#172 PTY job control): /dev/vtfbN, /dev/vtinputN and /dev/vtmouseN per
session instead of the single-session vtfb1/vtinput1 pair.

Conflicts:
- src/arch/x86_64/linux/io_sys.rs (4 stops): merged the dev per-session
  accessors with the USB hooks.
  * read: `/dev/uevent` record path stays before the dev
    `vtconsole || vtfb_session().is_some()` snapshot path.
  * close: dev `closing_session` (vtfb/vtinput/vtmouse) and USB
    `closing_uevent` both release their reader/session.
  * stat mode: dev session accessors plus `is_uevent()` stay S_IFCHR.
  * ioctl: fbdev -> vtconsole -> vtfb_session -> uevent -> vtinput_session
    (with the dev VTINPUT_SET_FOCUS/tty_ioctl split) -> Console/Stdin.
- src/vfs/mod.rs: dev terminal_devices loop (vtfb/vtinput/vtmouse per
  MAX_SESSIONS) is kept, and `/dev/block` + `/dev/uevent` are chained into
  the same DevDirectory children list.
- src/arch/x86_64/linux/errno.rs auto-merged with a duplicate `EBUSY=16`
  (USB needed it, #167 added its own); collapsed to one variant with both
  documented meanings.
- src/test.rs auto-merged; all three routines remain (console grid,
  linux-tty per-session, USB layout).

The dead `vtinput1_ioctl` wrapper superseded by the dev inline
session-aware branch is removed.

Post-merge evidence: gates 6/6 (host 903/0), QEMU usb-storage acceptance
46/46, release-kernel selftest 129 routines / 0 failed / 1 skipped
(NVMe absent), one flocked run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant