///| `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())), ), ] }