Files
chandra/wasmhost/bin/sys.mbt
T
Claude 68164ff75e
CI / test (push) Failing after 16s
Reload panel modules from outside the session: control socket, chandra-ctl, SIGUSR1
Until now a module reload was only reachable by clicking Settings > Panel inside the session.
Now:

- The compositor listens on $XDG_RUNTIME_DIR/chandra/<wayland socket name>.sock (the login
  session's is wayland-moon-0.sock) with a line protocol: ping, list, reload [NAME], help.
  Replies end with an "ok ..." or "error ..." line. It lives in compositor/host/control.mbt,
  runs from ChandraCompositor::step, and its fds join the idle loop's wait set, so an idle
  desktop still sleeps (measured ~0.4% CPU on wasmhost, ~0.1% on host-wasm) and wakes for a
  command. A path another live compositor answers on is left alone.
- DesktopShell::reload_modules(name) reloads every loaded module (or those whose module id,
  stem, slot or path is NAME) in place via replace_slot: same panel position, saved
  :module-config applied, a module that fails to load stays as it was and the error is
  reported to the caller and shown as a notice. The catalog is rescanned.
- SIGUSR1 to the host reloads every module (result in the log and the notice): new WIT
  function sys.take-sigusr1, implemented in compositor/sys (native build and wasmhost) and
  host-wasm; the handler has no SA_RESTART so it interrupts the idle poll.
- session/chandra-ctl (installed to /usr/local/bin), `just reload [NAME]`, docs/reloading.md,
  README.
- Tests: compositor/host/control_test.mbt (protocol, reload all / by name / by slot, order
  and settings kept, failing module kept, busy socket). Checked headless on both hosts with
  chandra-ctl and kill -USR1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjLJtvYcgW48okL6qQ
2026-11-25 20:57:40 +00:00

213 lines
5.2 KiB
MoonBit

///| `chandra:host/sys`: the OS surface, over compositor/sys's native implementation (the same
/// C shim the native compositor uses).
///|
#borrow(path, data)
extern "C" fn c_append_file(path : Bytes, data : Bytes) -> Int = "wh_append_file"
///| Fds beyond the first that arrived with the last `recvmsg` on a socket, for `take-fd`.
let extra_fds : Map[Int, Array[Int]] = {}
///|
fn drop_extra_fds(sock : Int) -> Unit {
match extra_fds.get(sock) {
Some(left) => {
for f in left {
@sys.close_fd(f)
}
extra_fds.remove(sock)
}
None => ()
}
}
///|
fn s32(v : Int) -> Array[@wt.Val] {
[S32(v)]
}
///|
fn boolean(b : Bool) -> Array[@wt.Val] {
[Bool(b)]
}
///|
let none : Array[@wt.Val] = []
///|
fn sys_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
[
("unix-listen", a => s32(@sys.unix_listen(a[0].to_string()))),
("unix-accept", a => s32(@sys.unix_accept(a[0].to_int()))),
("unix-connect", a => s32(@sys.unix_connect(a[0].to_string()))),
(
"close-fd",
a => {
let fd = a[0].to_int()
if fd >= 0 {
// A closed client socket's number is reused by the next one: drop what it left.
drop_extra_fds(fd)
@sys.close_fd(fd)
}
none
},
),
(
"unlink",
a => {
@sys.unlink(a[0].to_string())
none
},
),
("poll-read", a => s32(@sys.poll_read(a[0].to_int(), a[1].to_int()))),
(
"wait-readable",
a => {
let fds = a[0].to_list().map(v => v.to_int())
for f in display_wait_fds() {
fds.push(f)
}
s32(@sys.wait_readable(fds, a[1].to_int()))
},
),
(
"recvmsg",
a => {
let fd = a[0].to_int()
// Fds from the previous read nobody took would leak: close them.
drop_extra_fds(fd)
let max_len = a[1].to_int()
let received = fn(count : Int, got : Int, data : Bytes) -> Array[@wt.Val] {
[
Record([
("count", S32(count)),
("fd", S32(got)),
("data", @wt.Val::bytes(data)),
]),
]
}
if max_len <= 0 {
return received(0, -1, b"")
}
let buf = FixedArray::make(max_len, b'\x00')
let (n, fds) = @sys.recvmsg_fds(fd, buf, max_len)
if n < 0 {
for f in fds {
@sys.close_fd(f)
}
return received(n, -1, b"")
}
let got = if fds.length() > 0 { fds[0] } else { -1 }
if fds.length() > 1 {
extra_fds.set(fd, fds[1:].to_array())
}
received(n, got, Bytes::from_fixedarray(buf, len=n))
},
),
(
"take-fd",
a => {
let sock = a[0].to_int()
match extra_fds.get(sock) {
Some(q) if q.length() > 0 => {
let f = q.remove(0)
if q.length() == 0 {
extra_fds.remove(sock)
}
s32(f)
}
_ => s32(-1)
}
},
),
(
"sendmsg",
a => {
let data = a[1].to_bytes()
if data.length() == 0 {
return s32(0)
}
s32(@sys.sendmsg_fd(a[0].to_int(), data, a[2].to_int()))
},
),
("memfd-create", a => s32(@sys.memfd_create(a[0].to_string(), a[1].to_int()))),
(
"pwrite",
a => {
let fd = a[0].to_int()
let data = a[1].to_bytes()
let off = a[2].to_int()
s32(
if off < 0 {
@sys.write_nonblock(fd, data)
} else {
@sys.pwrite_bytes(fd, data, off)
},
)
},
),
(
"mmap-read",
a => [@wt.Val::bytes(@sys.mmap_read(a[0].to_int(), a[1].to_int()))],
),
(
"mmap-read-at",
a => {
let (fd, off, len) = (a[0].to_int(), a[1].to_int(), a[2].to_int())
if fd < 0 || off < 0 || len <= 0 {
return [@wt.Val::bytes(b"")]
}
[@wt.Val::bytes(@sys.mmap_read_at(fd, off, len))]
},
),
(
"read-file",
a => [@wt.Val::bytes(@sys.read_file_bytes(a[0].to_string()))],
),
(
"write-file",
a => s32(@sys.write_file_bytes(a[0].to_string(), a[1].to_bytes())),
),
(
"append-file",
a => s32(c_append_file(@utf8.encode(a[0].to_string()), a[1].to_bytes())),
),
(
"read-proc",
a => [@wt.Val::bytes(@sys.read_proc_bytes(a[0].to_string()))],
),
("mkdir-p", a => boolean(@sys.mkdir_p(a[0].to_string()))),
(
"list-desktop-files",
a => [@wt.Val::strings(@sys.list_desktop_files(a[0].to_string()))],
),
(
"sleep-ms",
a => {
let ms = a[0].to_int()
if ms > 0 {
@sys.sleep_ms(ms)
}
none
},
),
("monotonic-ms", _ => [U64(@sys.monotonic_ms())]),
(
"log",
a => {
eprint(a[0].to_string())
none
},
),
("getpid", _ => s32(@sys.getpid())),
("take-sigusr1", _ => boolean(@sys.take_sigusr1())),
("wall-time-field", a => s32(@sys.wall_time_field(a[0].to_int()))),
("getenv", a => [Str(@sys.getenv(a[0].to_string()))]),
("has-exe", a => boolean(@sys.has_exe(a[0].to_string()))),
(
"spawn",
a => boolean(@sys.spawn(a[0].to_string(), a[1].to_string())),
),
]
}