From e4373052940699275cda01683fb4df980611d185 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:16:26 +0100 Subject: [PATCH] Sys: connect and readyOf, a Unix socket and waiting on several files Report 2026-10.4, approved. On Linux: socket (close-on-exec) and connect, poll over up to 64 fds; over C the same by POSIX's names, with socket, connect, bind, listen and accept in C. readyOf gives the fd itself, and counts an end or an error as ready, since a read then won't wait. Both tests listen on a socket of their own: refused where nothing listens, the listener ready when connected to, a byte across, the end seen. PLAN.md: step 49 split; 49a done, 49b (the pty and programs, for rat) to come. --- diff --git a/PLAN.md b/PLAN.md index 6c3e4da..9d3b797 100644 --- a/PLAN.md +++ b/PLAN.md @@ -467,26 +467,29 @@ rlX11, an X11 client library written in raz, and rat, a terminal emulator over it, are to need no C: they talk to the X server over a socket, and rat runs its programs on a pseudo-terminal. Sys gains what -that takes. Not started: each step waits for the owner's word. +that takes. Approved; under way, each step on the owner's word. - [x] 48. The report, version 2026-10.4: `connect`, `readyOf`, `openPty`, `setTermSize`, `spawn`, `wait`, `kill` (section 11), for review before any code. -- [ ] 49. On Linux by system calls: `socket` and `connect` for a Unix - socket; `poll` over several fds; `/dev/ptmx` with `TIOCSPTLCK` - and `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its - size; `fork`, then in the child `setsid`, the terminal opened as - its controlling one with `TIOCSCTTY`, `dup2` to 0, 1 and 2, +- [x] 49a. The owner approves 2026-10.4. `connect` and `readyOf`, what + rlX11 needs first: on Linux `socket` (close-on-exec) and + `connect` for a Unix socket, `poll` over several fds; over C + by POSIX's names, `struct sockaddr_un` named glibc's. Both + files' tests: `connect` refused where nothing listens, and taken + by a listener the test makes, which `readyOf` then finds ready; + a byte across, and the end of the connection. +- [ ] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and + `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its size; + `fork`, then in the child `setsid`, the terminal opened as its + controlling one with `TIOCSCTTY`, `dup2` to 0, 1 and 2, `close_range` for the rest, and `execve` with the environment made from this one's; `wait4`, `kill`. Over C by POSIX's names - (`socket`, `poll`, `posix_openpt`, `grantpt`, `unlockpt`, - `ptsname`, `fork`, `execve`, `waitpid`, `kill`), what is glibc's - named in the file, as before. Both files' tests: a program - spawned on a pseudo-terminal that prints its size and its - arguments and ends with a status read back by `wait`; a program - stopped by `kill`; `readyOf` on two pipes; `connect` refused on - a path with nothing at it, and taken by a listener the test - makes with Python, in `test/libc` and beside the library's own. + (`posix_openpt`, `grantpt`, `unlockpt`, `ptsname`, `fork`, + `execve`, `waitpid`, `kill`), what is glibc's named in the + file, as before. Both files' tests: a program spawned on a + pseudo-terminal that prints its size and its arguments and ends + with a status read back by `wait`; a program stopped by `kill`. Stage 8: one module to a name, report 2026-10.5 diff --git a/lib/C.raz b/lib/C.raz index 625278f..48f05a2 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -29,6 +29,11 @@ extern proc cfmakeraw*(t: ^U8); extern proc ioctl*(fd: I32; request: U64; ...): I32; extern proc poll*(fds: ^U8; n: U64; ms: I32): I32; +extern proc socket*(domain, kind, protocol: I32): I32; +extern proc connect*(fd: I32; addr: ^U8; len: U32): I32; +extern proc bind*(fd: I32; addr: ^U8; len: U32): I32; +extern proc listen*(fd, backlog: I32): I32; +extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; extern proc getenv*(name: ^U8): ^U8; extern proc getcwd*(buf: ^U8; n: U64): ^U8; extern proc printf*(format: ^U8; ...): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 61a062f..1fc9b9e 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -29,6 +29,12 @@ Winsize = record rows, cols, xpixel, ypixel: U16; end; Pollfd = record fd: I32; events, revents: I16; end; + (* struct sockaddr_un: AF_UNIX, then the path and its zero *) + SockaddrUn = record + family: U16; + path: [108]U8; + end; + var saved: Termios; (* the terminal as rawMode found it *) @@ -225,6 +231,38 @@ return SYSCALL(7, ADR(p), 1, ms) > 0; (* poll *) end; +(* A stream socket connected to the Unix socket at path: its file + descriptor, or minus an errno. It is closed in a program exec + starts. *) +proc connect*(path: []U8): I64; +var a: SockaddrUn; fd, r, i: I64; +begin + if LEN(path) >= LEN(a.path) then return -36; end; (* ENAMETOOLONG *) + a.family := 1; (* AF_UNIX *) + for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; + fd := SYSCALL(41, 1, 80001H, 0); (* socket: AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC *) + if fd < 0 then return fd; end; + r := SYSCALL(42, fd, ADR(a), SIZE(SockaddrUn)); (* connect *) + if r < 0 then close(fd); return r; end; + return fd; +end; + +(* The first of fds that has something to read within ms milliseconds, + or that has reached its end or failed, so that a read won't wait; -1 + if none has. ms < 0 waits as long as it takes. At most 64 fds. *) +proc readyOf*(fds: []I64; ms: I64): I64; +var p: [64]Pollfd; n, i: I64; +begin + n := LEN(fds); + if n > LEN(p) then n := LEN(p); end; + for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *) + if SYSCALL(7, ADR(p), n, ms) <= 0 then return -1; end; (* poll *) + for i := 0 to n - 1 do + if p[i].revents # 0 then return fds[i]; end; + end; + return -1; +end; + (* The value of the environment variable name; "" if it isn't set. The environment follows the arguments and their nil. *) proc env*(name: []U8): []U8; @@ -267,10 +305,25 @@ SYSCALL(231, status); end; +(* A socket listening at path, for the test: its fd. *) +proc listen(path: []U8): I64; +var a: SockaddrUn; fd, i: I64; +begin + remove(path); + a.family := 1; + for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; + fd := SYSCALL(41, 1, 1, 0); (* socket *) + if (fd < 0) or (SYSCALL(49, fd, ADR(a), SIZE(SockaddrUn)) # 0) (* bind *) + or (SYSCALL(50, fd, 1) # 0) then (* listen *) + exit(2); + end; + return fd; +end; + (* The module's test: a file written, read back and closed; a - directory made, listed and emptied; no terminal. *) + directory made, listed and emptied; no terminal; a socket. *) proc main*; -var fd, n, err, w, h: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; dir: [4096]U8; begin fd := create("/tmp/raz-sys-test"); @@ -317,6 +370,22 @@ if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; n := workDir(dir); if (n <= 0) or (dir[0] # '/') then exit(2); end; + (* a socket: nothing at a path, then a listener that a connection + makes ready, and a byte from it to the connection *) + if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *) + l := listen("/tmp/raz-sys-socket"); + c := connect("/tmp/raz-sys-socket"); + if c < 0 then exit(2); end; + if readyOf([c], 0) # -1 then exit(2); end; + if readyOf([c, l], 1000) # l then exit(2); end; + a := SYSCALL(43, l, 0, 0); (* accept *) + if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; + if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; + close(a); + if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *) + close(c); + close(l); + remove("/tmp/raz-sys-socket"); end; end Sys. diff --git a/lib/Sys.raz b/lib/Sys.raz index 2785efd..be0686b 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -4,9 +4,10 @@ It keeps to what POSIX gives: raw mode is cfmakeraw's, and a file's size is where lseek finds its end, so that no struct stat is read. - Two things are glibc's: where a name lies in struct dirent, and the - number of the TIOCGWINSZ request. A C library that differs needs a - Sys of its own. *) + Three things are glibc's: where a name lies in struct dirent, the + number of the TIOCGWINSZ request, and struct sockaddr_un, which on + the BSDs starts with a byte of length. A C library that differs + needs a Sys of its own. *) module Sys; import C; @@ -32,6 +33,12 @@ Winsize = record rows, cols, xpixel, ypixel: U16; end; Pollfd = record fd: I32; events, revents: I16; end; + (* glibc's struct sockaddr_un: AF_UNIX, then the path and its zero *) + SockaddrUn = record + family: U16; + path: [108]U8; + end; + var saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) @@ -234,6 +241,40 @@ return C.poll(^U8(ADR(p)), 1, I32(ms)) > 0; end; +(* A stream socket connected to the Unix socket at path: its file + descriptor, or minus an errno. *) +proc connect*(path: []U8): I64; +var a: SockaddrUn; fd: I32; i, err: I64; +begin + if LEN(path) >= LEN(a.path) then return -36; end; (* ENAMETOOLONG *) + a.family := 1; (* AF_UNIX *) + for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; + fd := C.socket(1, 1, 0); (* AF_UNIX, SOCK_STREAM *) + if fd < 0 then return failed(); end; + if C.connect(fd, ^U8(ADR(a)), U32(SIZE(SockaddrUn))) # 0 then + err := failed(); (* before close can change errno *) + C.close(fd); + return err; + end; + return I64(fd); +end; + +(* The first of fds that has something to read within ms milliseconds, + or that has reached its end or failed, so that a read won't wait; -1 + if none has. ms < 0 waits as long as it takes. At most 64 fds. *) +proc readyOf*(fds: []I64; ms: I64): I64; +var p: [64]Pollfd; n, i: I64; +begin + n := LEN(fds); + if n > LEN(p) then n := LEN(p); end; + for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *) + if C.poll(^U8(ADR(p)), U64(n), I32(ms)) <= 0 then return -1; end; + for i := 0 to n - 1 do + if p[i].revents # 0 then return fds[i]; end; + end; + return -1; +end; + (* The value of the environment variable name; "" if it isn't set. *) proc env*(name: []U8): []U8; var a: [4096]U8; s: ^U8; n: I64; @@ -272,10 +313,25 @@ C._exit(I32(status)); end; +(* A socket listening at path, for the test: its fd. *) +proc listen(path: []U8): I64; +var a: SockaddrUn; fd: I32; i: I64; +begin + remove(path); + a.family := 1; + for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; + fd := C.socket(1, 1, 0); + if (fd < 0) or (C.bind(fd, ^U8(ADR(a)), U32(SIZE(SockaddrUn))) # 0) or (C.listen(fd, 1) # 0) then + exit(2); + end; + return I64(fd); +end; + (* The module's test, as Linux's Sys has it: a file written, read back - and closed; a directory made, listed and emptied; no terminal. *) + and closed; a directory made, listed and emptied; no terminal; a + socket. *) proc main*; -var fd, n, err, w, h: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; dir: [4096]U8; begin fd := create("/tmp/raz-sys-test"); @@ -322,6 +378,22 @@ if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; n := workDir(dir); if (n <= 0) or (dir[0] # '/') then exit(2); end; + (* a socket: nothing at a path, then a listener that a connection + makes ready, and a byte from it to the connection *) + if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *) + l := listen("/tmp/raz-sys-socket"); + c := connect("/tmp/raz-sys-socket"); + if c < 0 then exit(2); end; + if readyOf([c], 0) # -1 then exit(2); end; + if readyOf([c, l], 1000) # l then exit(2); end; + a := I64(C.accept(I32(l), nil, nil)); + if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; + if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; + close(a); + if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *) + close(c); + close(l); + remove("/tmp/raz-sys-socket"); end; end Sys.