Skip to content

std/os ​

Typed libc bindings (memory, file descriptors, sockets, threads, processes) plus the green-aware fd wrappers the rest of the standard library is built on. Most programs only reach for it in FFI code; for files, sockets and processes use std/fs, std/net and std/process.

milo
from "std/os" import { malloc, free }

API reference ​

acceptFd ​

milo
pub fn acceptFd(fd: i32): i32

Accept one connection; returns the client fd (<0 on error). In a green task the client fd is left nonblocking so later readFd/writeFd calls skip a redundant fcntl round-trip.

acceptFdNb ​

milo
pub fn acceptFdNb(fd: i32): i32

Single-shot nonblocking accept: the client fd, or -1 when no connection is pending. For event loops that multiplex accept with other work and must not park on the listener — a blocking accept stops every other event source until the next connection arrives (milojs's cold-fetch hang).

bindIn ​

milo
pub fn bindIn(fd: i32, addr: &SockAddrIn): i32

bindIn6 ​

milo
pub fn bindIn6(fd: i32, addr: &SockAddrIn6): i32

bindUn ​

milo
pub fn bindUn(fd: i32, addr: &SockAddrUn): i32

connectFdIn ​

milo
pub fn connectFdIn(fd: i32, addr: &SockAddrIn): i32

connectFdIn6 ​

milo
pub fn connectFdIn6(fd: i32, addr: &SockAddrIn6): i32

connectFdIn6Wait ​

milo
pub fn connectFdIn6Wait(fd: i32, addr: &SockAddrIn6, wait: (bool) => bool): i32

connectFdInWait for an IPv6 address.

connectFdInWait ​

milo
pub fn connectFdInWait(fd: i32, addr: &SockAddrIn, wait: (bool) => bool): i32

connect(2) that waits through wait. 0 on success, -1 on failure, IO_GAVE_UP.

connectFdUn ​

milo
pub fn connectFdUn(fd: i32, addr: &SockAddrUn): i32

getSockAddr4 ​

milo
pub fn getSockAddr4(fd: i32): i64

The bound IPv4 address of fd in network order (as ip4 builds it), or -1 when it is not an IPv4 socket or getsockname fails. The family is read from the length the kernel reports (a sockaddr_in is 16 bytes) because the family field sits at a different offset on darwin (sin_len first) and linux. -1 under replay, where descriptors are synthetic; callers use it only for diagnostics.

getSockPort ​

milo
pub fn getSockPort(fd: i32): i32

The bound port, whatever family the socket is. getsockname writes into storage-sized space because the kernel picks the family; sin_port and sin6_port BOTH sit at offset 2 (same invariant as sun_path), so one read serves v4 and v6. Returns -1 on failure.

readFd ​

milo
pub fn readFd(fd: i32, buf: *u8, len: i64): i64

One read(2). Returns bytes read, 0 at EOF, <0 on error.

recvFd ​

milo
pub fn recvFd(fd: i32, buf: *u8, len: i64): i64

Green-aware SOCKET read/write. Identical shape to readFd/writeFd but through the sockRead/sockWrite seam: on POSIX that is read/write (a socket fd is a normal fd), but on Windows a SOCKET is NOT a CRT fd — the CRT _read/_write behind read/write fast-fail on one, so socket data must go through recv/send. Socket code (std/net, raw-socket programs) must use these, not readFd/writeFd, which stay bound to the CRT fd path for stdout/stdin/pipes.

recvFdWait ​

milo
pub fn recvFdWait(fd: i32, buf: *u8, len: i64, wait: (bool) => bool): i64

One socket read that waits through wait. Bytes read, 0 at EOF, <0 on error, IO_GAVE_UP.

sendFd ​

milo
pub fn sendFd(fd: i32, buf: *u8, len: i64): i64

sendFdWait ​

milo
pub fn sendFdWait(fd: i32, buf: *u8, len: i64, wait: (bool) => bool): i64

Write all len bytes, waiting through wait. len, <0 on error, or IO_GAVE_UP.

sysAccept ​

milo
pub fn sysAccept(fd: i32): i32

accept(2), discarding the peer address. Under replay the recorded client fd comes back as a synthetic socket.

sysClose ​

milo
pub fn sysClose(fd: i32): i32

close(2). Under replay a synthetic descriptor is forgotten, never passed to the OS.

sysDup ​

milo
pub fn sysDup(fd: i32): i32

dup(2). Under replay the duplicate of a descriptor is synthetic like everything else the trace hands out, and keeps the original's record kinds.

sysIsatty ​

milo
pub fn sysIsatty(fd: i32): bool

isatty(3) through the trace: whether a descriptor is a terminal is an input too.

sysKill ​

milo
pub fn sysKill(pid: i32, sig: i32): i32

kill(2) through the trace. Under replay no signal is sent: the pid is the recorded run's, and whatever has it now is not ours.

sysListen ​

milo
pub fn sysListen(fd: i32, backlog: i32): i32

listen(2).

sysLseek ​

milo
pub fn sysLseek(fd: i32, off: i64, whence: i32): i64

lseek(2): the new offset, or <0.

sysOpen ​

milo
pub fn sysOpen(path: &string, flags: i32, mode: i32): i32

open(2) with a creation mode (ignored unless flags creates). The fd, or <0.

sysRead ​

milo
pub fn sysRead(fd: i32, buf: *u8, len: i64): i64

One read(2): bytes read, 0 at EOF, <0 with errno set. At most 2147479552 bytes.

sysSocket ​

milo
pub fn sysSocket(domain: i32, sockType: i32, protocol: i32): i32

socket(2). Under replay the recorded number comes back as a synthetic socket.

sysWaitpid ​

milo
pub fn sysWaitpid(pid: i32, status: *u8, options: i32): i32

waitpid(2) through the trace: the return value and the status word the kernel wrote. Under replay no child exists; the recorded exit comes back.

sysWrite ​

milo
pub fn sysWrite(fd: i32, buf: *u8, len: i64): i64

One write(2): bytes written, <0 with errno set. At most 2147479552 bytes.

writeFd ​

milo
pub fn writeFd(fd: i32, buf: *u8, len: i64): i64

Write all len bytes (loops over partial writes). Returns len or <0 on error.