wasmhost leaked the arguments of every host call. wt_stub.c handed them to the MoonBit dispatcher expecting it to own them, but the compiler borrowed the parameter, so nothing released them (about 600 kB/s in a test with one busy terminal; a long session reached over 16 GB resident plus swap). MoonBit now allocates the buffer and C copies into it. Also: - Unannotated pointer FFI parameters default to #owned, and none of these C functions released theirs, including the native KMS present (a whole frame each). Annotated #borrow; the wasmhost build now fails on that warning, which --warn-list -a had hidden. - Watched launches forked the whole compositor, and each monitor kept a snapshot of its heap for the app's lifetime. It now posix_spawns a fresh copy of the binary that takes over before main. - The host logs memory, window buffers and images once a minute. - chandra-session keeps the previous log as session.log.1. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hj5m35Lj6Rt9vwwLGZ
380 lines
10 KiB
MoonBit
380 lines
10 KiB
MoonBit
///| Minimal POSIX OS primitives for the pure-MoonBit Wayland compositor.
|
|
|
|
///|
|
|
#borrow(path)
|
|
extern "C" fn c_unix_listen(path : Bytes) -> Int = "mp_unix_listen"
|
|
|
|
///|
|
|
extern "C" fn c_unix_accept(listen_fd : Int) -> Int = "mp_unix_accept"
|
|
|
|
///|
|
|
#borrow(path)
|
|
extern "C" fn c_unix_connect(path : Bytes) -> Int = "mp_unix_connect"
|
|
|
|
///|
|
|
extern "C" fn c_close_fd(fd : Int) -> Int = "mp_close_fd"
|
|
|
|
///|
|
|
#borrow(path)
|
|
extern "C" fn c_unlink(path : Bytes) -> Int = "mp_unlink"
|
|
|
|
///|
|
|
extern "C" fn c_poll_read(fd : Int, timeout_ms : Int) -> Int = "mp_poll_read"
|
|
|
|
///|
|
|
#borrow(buf, out_fd)
|
|
extern "C" fn c_recvmsg_fd(
|
|
fd : Int,
|
|
buf : FixedArray[Byte],
|
|
max_len : Int,
|
|
out_fd : FixedArray[Int],
|
|
) -> Int = "mp_recvmsg_fd"
|
|
|
|
///|
|
|
#borrow(buf, fds)
|
|
extern "C" fn c_sendmsg_fds(
|
|
fd : Int,
|
|
buf : Bytes,
|
|
len : Int,
|
|
fds : FixedArray[Int],
|
|
n_fds : Int,
|
|
) -> Int = "mp_sendmsg_fds"
|
|
|
|
///|
|
|
#borrow(buf)
|
|
extern "C" fn c_sendmsg_fd(
|
|
fd : Int,
|
|
buf : Bytes,
|
|
len : Int,
|
|
send_fd : Int,
|
|
) -> Int = "mp_sendmsg_fd"
|
|
|
|
///|
|
|
#borrow(name)
|
|
extern "C" fn c_memfd_create(name : Bytes, size : Int) -> Int = "mp_memfd_create"
|
|
|
|
///|
|
|
#borrow(buf)
|
|
extern "C" fn c_pwrite_bytes(
|
|
fd : Int,
|
|
buf : Bytes,
|
|
len : Int,
|
|
offset : Int,
|
|
) -> Int = "mp_pwrite_bytes"
|
|
|
|
///|
|
|
extern "C" fn c_mmap_read(fd : Int, size : Int) -> Bytes = "mp_mmap_read"
|
|
|
|
///|
|
|
#borrow(path)
|
|
extern "C" fn c_read_file(path : Bytes) -> Bytes = "mp_read_file"
|
|
|
|
///|
|
|
#borrow(path, buf)
|
|
extern "C" fn c_write_file(path : Bytes, buf : Bytes, len : Int) -> Int = "mp_write_file"
|
|
|
|
///|
|
|
extern "C" fn c_sleep_ms(ms : Int) -> Unit = "mp_sleep_ms"
|
|
|
|
///|
|
|
extern "C" fn c_getpid() -> Int = "mp_getpid"
|
|
|
|
///| Encode ASCII/UTF-8 String into Bytes for C paths/names.
|
|
pub fn str_to_utf8_bytes(s : String) -> Bytes {
|
|
let buf : Array[Byte] = []
|
|
for ch in s {
|
|
let code = ch.to_int()
|
|
if code < 0x80 {
|
|
buf.push(code.to_byte())
|
|
} else if code < 0x800 {
|
|
buf.push((0xC0 | (code >> 6)).to_byte())
|
|
buf.push((0x80 | (code & 0x3F)).to_byte())
|
|
} else if code < 0x10000 {
|
|
buf.push((0xE0 | (code >> 12)).to_byte())
|
|
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
|
|
buf.push((0x80 | (code & 0x3F)).to_byte())
|
|
} else {
|
|
buf.push((0xF0 | (code >> 18)).to_byte())
|
|
buf.push((0x80 | ((code >> 12) & 0x3F)).to_byte())
|
|
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
|
|
buf.push((0x80 | (code & 0x3F)).to_byte())
|
|
}
|
|
}
|
|
Bytes::from_array(buf)
|
|
}
|
|
|
|
///|
|
|
pub fn unix_listen(path : String) -> Int {
|
|
c_unix_listen(str_to_utf8_bytes(path))
|
|
}
|
|
|
|
///|
|
|
pub fn unix_accept(listen_fd : Int) -> Int {
|
|
c_unix_accept(listen_fd)
|
|
}
|
|
|
|
///|
|
|
pub fn unix_connect(path : String) -> Int {
|
|
c_unix_connect(str_to_utf8_bytes(path))
|
|
}
|
|
|
|
///|
|
|
pub fn close_fd(fd : Int) -> Unit {
|
|
ignore(c_close_fd(fd))
|
|
}
|
|
|
|
///|
|
|
pub fn unlink(path : String) -> Unit {
|
|
ignore(c_unlink(str_to_utf8_bytes(path)))
|
|
}
|
|
|
|
///|
|
|
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
|
|
c_poll_read(fd, timeout_ms)
|
|
}
|
|
|
|
///|
|
|
#borrow(fds)
|
|
extern "C" fn c_wait_readable(fds : FixedArray[Int], n : Int, timeout_ms : Int) -> Int = "mp_wait_readable"
|
|
|
|
///| Block until one of `fds` is readable or `timeout_ms` passes (how an idle loop sleeps). The
|
|
/// number of ready fds, 0 on timeout.
|
|
pub fn wait_readable(fds : Array[Int], timeout_ms : Int) -> Int {
|
|
c_wait_readable(FixedArray::from_array(fds), fds.length(), timeout_ms)
|
|
}
|
|
|
|
///| Receive bytes and every SCM_RIGHTS fd that came with them, in order. Returns
|
|
/// (bytes_read, fds); bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
|
|
pub fn recvmsg_fds(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Array[Int]) {
|
|
let out_fd : FixedArray[Int] = FixedArray::make(33, -1)
|
|
let n = c_recvmsg_fd(fd, buf, max_len, out_fd)
|
|
let fds : Array[Int] = []
|
|
for i = 0; i < out_fd[0]; i = i + 1 {
|
|
fds.push(out_fd[1 + i])
|
|
}
|
|
(n, fds)
|
|
}
|
|
|
|
///| Receive bytes and an optional SCM_RIGHTS fd. Returns (bytes_read, received_fd); any further
|
|
/// fds are closed. bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF.
|
|
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
|
|
let (n, fds) = recvmsg_fds(fd, buf, max_len)
|
|
for i = 1; i < fds.length(); i = i + 1 {
|
|
close_fd(fds[i])
|
|
}
|
|
(n, if fds.length() > 0 { fds[0] } else { -1 })
|
|
}
|
|
|
|
///|
|
|
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
|
|
c_sendmsg_fd(fd, buf, buf.length(), send_fd)
|
|
}
|
|
|
|
///| Send `buf` with several fds in one SCM_RIGHTS message, the way libwayland flushes (tests).
|
|
pub fn sendmsg_fds(fd : Int, buf : Bytes, fds : Array[Int]) -> Int {
|
|
c_sendmsg_fds(fd, buf, buf.length(), FixedArray::from_array(fds), fds.length())
|
|
}
|
|
|
|
///|
|
|
pub fn memfd_create(name : String, size : Int) -> Int {
|
|
c_memfd_create(str_to_utf8_bytes(name), size)
|
|
}
|
|
|
|
///| Write as much of `buf` as a pipe takes without blocking (the fd is made non-blocking):
|
|
/// the byte count, 0 when the pipe is full, -1 on an error (the reader went away).
|
|
pub fn write_nonblock(fd : Int, buf : Bytes) -> Int {
|
|
c_pwrite_bytes(fd, buf, buf.length(), -1)
|
|
}
|
|
|
|
///|
|
|
pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
|
|
c_pwrite_bytes(fd, buf, buf.length(), offset)
|
|
}
|
|
|
|
///|
|
|
pub fn mmap_read_at(fd : Int, offset : Int, len : Int) -> Bytes {
|
|
let raw = c_mmap_read(fd, offset + len)
|
|
if raw.length() < offset + len {
|
|
return Bytes::make(0, b'\x00')
|
|
}
|
|
if offset == 0 && raw.length() == len {
|
|
raw
|
|
} else {
|
|
raw[offset:offset + len].to_bytes()
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn mmap_read(fd : Int, size : Int) -> Bytes {
|
|
c_mmap_read(fd, size)
|
|
}
|
|
|
|
///|
|
|
pub fn read_file_bytes(path : String) -> Bytes {
|
|
c_read_file(str_to_utf8_bytes(path))
|
|
}
|
|
|
|
///|
|
|
pub fn write_file_bytes(path : String, data : Bytes) -> Int {
|
|
c_write_file(str_to_utf8_bytes(path), data, data.length())
|
|
}
|
|
|
|
///|
|
|
pub fn sleep_ms(ms : Int) -> Unit {
|
|
c_sleep_ms(ms)
|
|
}
|
|
|
|
///|
|
|
pub fn getpid() -> Int {
|
|
c_getpid()
|
|
}
|
|
|
|
///|
|
|
extern "C" fn c_wall_time_field(which : Int) -> Int = "mp_wall_time_field"
|
|
|
|
///| Local wall clock: 0 year, 1 month (1-12), 2 day, 3 hour, 4 minute, 5 second, 6 weekday (0 = Sunday).
|
|
pub fn wall_time_field(which : Int) -> Int {
|
|
c_wall_time_field(which)
|
|
}
|
|
|
|
///|
|
|
#borrow(name)
|
|
extern "C" fn c_has_exe(name : Bytes) -> Int = "mp_has_exe"
|
|
|
|
///|
|
|
#borrow(cmd, display)
|
|
extern "C" fn c_spawn(cmd : Bytes, display : Bytes) -> Int = "mp_spawn"
|
|
|
|
///| Whether `name` is an executable on PATH.
|
|
pub fn has_exe(name : String) -> Bool {
|
|
c_has_exe(str_to_utf8_bytes(name)) != 0
|
|
}
|
|
|
|
///| Launch `cmd` detached with WAYLAND_DISPLAY set to `display`.
|
|
pub fn spawn(cmd : String, display : String) -> Bool {
|
|
c_spawn(str_to_utf8_bytes(cmd), str_to_utf8_bytes(display)) == 0
|
|
}
|
|
|
|
///|
|
|
#borrow(argv)
|
|
extern "C" fn c_spawn_socket(argv : Bytes) -> Int = "mp_spawn_socket"
|
|
|
|
///| Run `argv` (an absolute program path, then its arguments) with its stdin and stdout on a
|
|
/// socket and return our end of it (-1 on failure). For polkit's authentication helper.
|
|
pub fn spawn_socket(argv : Array[String]) -> Int {
|
|
c_spawn_socket(str_to_utf8_bytes(argv.join("\n")))
|
|
}
|
|
|
|
///|
|
|
extern "C" fn c_take_sigusr1() -> Int = "mp_take_sigusr1"
|
|
|
|
///| True once after the process got SIGUSR1 (reload the panel modules). The first call installs
|
|
/// the handler; a signal interrupts `wait_readable`, so an idle loop wakes for it.
|
|
pub fn take_sigusr1() -> Bool {
|
|
c_take_sigusr1() != 0
|
|
}
|
|
|
|
///|
|
|
#borrow(dir)
|
|
extern "C" fn c_list_desktop(dir : Bytes) -> Bytes = "mp_list_desktop"
|
|
|
|
///|
|
|
#borrow(name)
|
|
extern "C" fn c_getenv(name : Bytes) -> Bytes = "mp_getenv"
|
|
|
|
///| Newline-separated names of the `*.desktop` files directly in `dir`.
|
|
pub fn list_desktop_files(dir : String) -> Array[String] {
|
|
let raw = c_list_desktop(str_to_utf8_bytes(dir))
|
|
let out : Array[String] = []
|
|
let sb = StringBuilder::new()
|
|
let mut i = 0
|
|
while i < raw.length() {
|
|
if raw[i] == b'\n' {
|
|
out.push(sb.to_string())
|
|
sb.reset()
|
|
} else {
|
|
sb.write_char(Int::unsafe_to_char(raw[i].to_int()))
|
|
}
|
|
i = i + 1
|
|
}
|
|
out
|
|
}
|
|
|
|
///| Environment variable, or "" when unset.
|
|
pub fn getenv(name : String) -> String {
|
|
let raw = c_getenv(str_to_utf8_bytes(name))
|
|
let sb = StringBuilder::new()
|
|
for i = 0; i < raw.length(); i = i + 1 {
|
|
sb.write_char(Int::unsafe_to_char(raw[i].to_int()))
|
|
}
|
|
sb.to_string()
|
|
}
|
|
|
|
///|
|
|
#borrow(path)
|
|
extern "C" fn c_mkdir_p(path : Bytes) -> Int = "mp_mkdir_p"
|
|
|
|
///| Create `path` and any missing parents; true when it exists afterwards.
|
|
pub fn mkdir_p(path : String) -> Bool {
|
|
c_mkdir_p(str_to_utf8_bytes(path)) == 0
|
|
}
|
|
|
|
///|
|
|
#borrow(path)
|
|
extern "C" fn c_read_proc(path : Bytes) -> Bytes = "mp_read_proc"
|
|
|
|
///| Contents of a small /proc or /sys file ("" when unreadable). Unlike `read_file_bytes`
|
|
/// this works on files that report size 0.
|
|
pub fn read_proc_bytes(path : String) -> Bytes {
|
|
c_read_proc(str_to_utf8_bytes(path))
|
|
}
|
|
|
|
///| Milliseconds on a monotonic clock.
|
|
pub fn monotonic_ms() -> UInt64 {
|
|
@env.now()
|
|
}
|
|
|
|
///| Print a line to stderr (stdout on native).
|
|
pub fn log(message : String) -> Unit {
|
|
println(message)
|
|
}
|
|
|
|
///| Whether the host draws the screen (the wasm compositor): then client buffers are captured in
|
|
/// the host and the pixels never enter this program. Never on the native build.
|
|
pub fn host_raster() -> Bool {
|
|
false
|
|
}
|
|
|
|
///| Copy a client buffer into a host surface; 0 (none) on the native build.
|
|
pub fn surface_capture(_fd : Int, _offset : Int, _stride : Int, _width : Int, _height : Int, _format : Int) -> UInt {
|
|
0U
|
|
}
|
|
|
|
///|
|
|
pub fn surface_release(_id : UInt) -> Unit {
|
|
()
|
|
}
|
|
|
|
///| A scaled copy of a host surface as an image handle; 0 (none) on the native build, which
|
|
/// scales the snapshot's own pixels instead (`@renderer.shm_thumbnail`).
|
|
pub fn surface_thumbnail(_id : UInt, _w : Int, _h : Int, _reuse : UInt) -> UInt {
|
|
0U
|
|
}
|
|
|
|
///| A memfd with the host framebuffer as ARGB8888; -1 on the native build.
|
|
pub fn frame_memfd() -> Int {
|
|
-1
|
|
}
|
|
|
|
///|
|
|
#borrow(cmd, display)
|
|
extern "C" fn c_spawn_watch(cmd : Bytes, display : Bytes) -> Int = "mp_spawn_watch"
|
|
|
|
///| Launch `cmd` like `spawn`, watched: returns a non-blocking socket (-1 when nothing could
|
|
/// start) that reports, once `sh -c cmd` exits, "exit CODE MS\n" or "signal NUM MS\n" (MS = how
|
|
/// long it ran) followed by the last 2 KiB of its output, then closes. Its output still goes to
|
|
/// our stderr (the session log). See `@shell` launch.mbt.
|
|
pub fn spawn_watch(cmd : String, display : String) -> Int {
|
|
c_spawn_watch(str_to_utf8_bytes(cmd), str_to_utf8_bytes(display))
|
|
}
|