raz / commit e4373052

commit
e4373052940699275cda01683fb4df980611d185
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
14be7cfb
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.

patch · log

4 files changed, +170 -21

PLAN.md (history) modified +17 -14

@@ -467,26 +467,29 @@
467467 rlX11, an X11 client library written in raz, and rat, a terminal
468468 emulator over it, are to need no C: they talk to the X server over a
469469 socket, and rat runs its programs on a pseudo-terminal. Sys gains what
470-that takes. Not started: each step waits for the owner's word.
470+that takes. Approved; under way, each step on the owner's word.
471471
472472 - [x] 48. The report, version 2026-10.4: `connect`, `readyOf`,
473473 `openPty`, `setTermSize`, `spawn`, `wait`, `kill` (section 11),
474474 for review before any code.
475-- [ ] 49. On Linux by system calls: `socket` and `connect` for a Unix
476- socket; `poll` over several fds; `/dev/ptmx` with `TIOCSPTLCK`
477- and `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its
478- size; `fork`, then in the child `setsid`, the terminal opened as
479- its controlling one with `TIOCSCTTY`, `dup2` to 0, 1 and 2,
475+- [x] 49a. The owner approves 2026-10.4. `connect` and `readyOf`, what
476+ rlX11 needs first: on Linux `socket` (close-on-exec) and
477+ `connect` for a Unix socket, `poll` over several fds; over C
478+ by POSIX's names, `struct sockaddr_un` named glibc's. Both
479+ files' tests: `connect` refused where nothing listens, and taken
480+ by a listener the test makes, which `readyOf` then finds ready;
481+ a byte across, and the end of the connection.
482+- [ ] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and
483+ `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its size;
484+ `fork`, then in the child `setsid`, the terminal opened as its
485+ controlling one with `TIOCSCTTY`, `dup2` to 0, 1 and 2,
480486 `close_range` for the rest, and `execve` with the environment
481487 made from this one's; `wait4`, `kill`. Over C by POSIX's names
482- (`socket`, `poll`, `posix_openpt`, `grantpt`, `unlockpt`,
483- `ptsname`, `fork`, `execve`, `waitpid`, `kill`), what is glibc's
484- named in the file, as before. Both files' tests: a program
485- spawned on a pseudo-terminal that prints its size and its
486- arguments and ends with a status read back by `wait`; a program
487- stopped by `kill`; `readyOf` on two pipes; `connect` refused on
488- a path with nothing at it, and taken by a listener the test
489- makes with Python, in `test/libc` and beside the library's own.
488+ (`posix_openpt`, `grantpt`, `unlockpt`, `ptsname`, `fork`,
489+ `execve`, `waitpid`, `kill`), what is glibc's named in the
490+ file, as before. Both files' tests: a program spawned on a
491+ pseudo-terminal that prints its size and its arguments and ends
492+ with a status read back by `wait`; a program stopped by `kill`.
490493
491494 Stage 8: one module to a name, report 2026-10.5
492495

lib/C.raz (history) modified +5 -0

@@ -29,6 +29,11 @@
2929 extern proc cfmakeraw*(t: ^U8);
3030 extern proc ioctl*(fd: I32; request: U64; ...): I32;
3131 extern proc poll*(fds: ^U8; n: U64; ms: I32): I32;
32+extern proc socket*(domain, kind, protocol: I32): I32;
33+extern proc connect*(fd: I32; addr: ^U8; len: U32): I32;
34+extern proc bind*(fd: I32; addr: ^U8; len: U32): I32;
35+extern proc listen*(fd, backlog: I32): I32;
36+extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32;
3237 extern proc getenv*(name: ^U8): ^U8;
3338 extern proc getcwd*(buf: ^U8; n: U64): ^U8;
3439 extern proc printf*(format: ^U8; ...): I32;

lib/Sys.linux-x86_64.raz (history) modified +71 -2

@@ -29,6 +29,12 @@
2929 Winsize = record rows, cols, xpixel, ypixel: U16; end;
3030 Pollfd = record fd: I32; events, revents: I16; end;
3131
32+ (* struct sockaddr_un: AF_UNIX, then the path and its zero *)
33+ SockaddrUn = record
34+ family: U16;
35+ path: [108]U8;
36+ end;
37+
3238 var
3339 saved: Termios; (* the terminal as rawMode found it *)
3440
@@ -225,6 +231,38 @@
225231 return SYSCALL(7, ADR(p), 1, ms) > 0; (* poll *)
226232 end;
227233
234+(* A stream socket connected to the Unix socket at path: its file
235+ descriptor, or minus an errno. It is closed in a program exec
236+ starts. *)
237+proc connect*(path: []U8): I64;
238+var a: SockaddrUn; fd, r, i: I64;
239+begin
240+ if LEN(path) >= LEN(a.path) then return -36; end; (* ENAMETOOLONG *)
241+ a.family := 1; (* AF_UNIX *)
242+ for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end;
243+ fd := SYSCALL(41, 1, 80001H, 0); (* socket: AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC *)
244+ if fd < 0 then return fd; end;
245+ r := SYSCALL(42, fd, ADR(a), SIZE(SockaddrUn)); (* connect *)
246+ if r < 0 then close(fd); return r; end;
247+ return fd;
248+end;
249+
250+(* The first of fds that has something to read within ms milliseconds,
251+ or that has reached its end or failed, so that a read won't wait; -1
252+ if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
253+proc readyOf*(fds: []I64; ms: I64): I64;
254+var p: [64]Pollfd; n, i: I64;
255+begin
256+ n := LEN(fds);
257+ if n > LEN(p) then n := LEN(p); end;
258+ for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *)
259+ if SYSCALL(7, ADR(p), n, ms) <= 0 then return -1; end; (* poll *)
260+ for i := 0 to n - 1 do
261+ if p[i].revents # 0 then return fds[i]; end;
262+ end;
263+ return -1;
264+end;
265+
228266 (* The value of the environment variable name; "" if it isn't set. The
229267 environment follows the arguments and their nil. *)
230268 proc env*(name: []U8): []U8;
@@ -267,10 +305,25 @@
267305 SYSCALL(231, status);
268306 end;
269307
308+(* A socket listening at path, for the test: its fd. *)
309+proc listen(path: []U8): I64;
310+var a: SockaddrUn; fd, i: I64;
311+begin
312+ remove(path);
313+ a.family := 1;
314+ for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end;
315+ fd := SYSCALL(41, 1, 1, 0); (* socket *)
316+ if (fd < 0) or (SYSCALL(49, fd, ADR(a), SIZE(SockaddrUn)) # 0) (* bind *)
317+ or (SYSCALL(50, fd, 1) # 0) then (* listen *)
318+ exit(2);
319+ end;
320+ return fd;
321+end;
322+
270323 (* The module's test: a file written, read back and closed; a
271- directory made, listed and emptied; no terminal. *)
324+ directory made, listed and emptied; no terminal; a socket. *)
272325 proc main*;
273-var fd, n, err, w, h: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
326+var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
274327 dir: [4096]U8;
275328 begin
276329 fd := create("/tmp/raz-sys-test");
@@ -317,6 +370,22 @@
317370 if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end;
318371 n := workDir(dir);
319372 if (n <= 0) or (dir[0] # '/') then exit(2); end;
373+ (* a socket: nothing at a path, then a listener that a connection
374+ makes ready, and a byte from it to the connection *)
375+ if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *)
376+ l := listen("/tmp/raz-sys-socket");
377+ c := connect("/tmp/raz-sys-socket");
378+ if c < 0 then exit(2); end;
379+ if readyOf([c], 0) # -1 then exit(2); end;
380+ if readyOf([c, l], 1000) # l then exit(2); end;
381+ a := SYSCALL(43, l, 0, 0); (* accept *)
382+ if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
383+ if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end;
384+ close(a);
385+ if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *)
386+ close(c);
387+ close(l);
388+ remove("/tmp/raz-sys-socket");
320389 end;
321390
322391 end Sys.

lib/Sys.raz (history) modified +77 -5

@@ -4,9 +4,10 @@
44
55 It keeps to what POSIX gives: raw mode is cfmakeraw's, and a file's
66 size is where lseek finds its end, so that no struct stat is read.
7- Two things are glibc's: where a name lies in struct dirent, and the
8- number of the TIOCGWINSZ request. A C library that differs needs a
9- Sys of its own. *)
7+ Three things are glibc's: where a name lies in struct dirent, the
8+ number of the TIOCGWINSZ request, and struct sockaddr_un, which on
9+ the BSDs starts with a byte of length. A C library that differs
10+ needs a Sys of its own. *)
1011 module Sys;
1112 import C;
1213
@@ -32,6 +33,12 @@
3233 Winsize = record rows, cols, xpixel, ypixel: U16; end;
3334 Pollfd = record fd: I32; events, revents: I16; end;
3435
36+ (* glibc's struct sockaddr_un: AF_UNIX, then the path and its zero *)
37+ SockaddrUn = record
38+ family: U16;
39+ path: [108]U8;
40+ end;
41+
3542 var
3643 saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *)
3744
@@ -234,6 +241,40 @@
234241 return C.poll(^U8(ADR(p)), 1, I32(ms)) > 0;
235242 end;
236243
244+(* A stream socket connected to the Unix socket at path: its file
245+ descriptor, or minus an errno. *)
246+proc connect*(path: []U8): I64;
247+var a: SockaddrUn; fd: I32; i, err: I64;
248+begin
249+ if LEN(path) >= LEN(a.path) then return -36; end; (* ENAMETOOLONG *)
250+ a.family := 1; (* AF_UNIX *)
251+ for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end;
252+ fd := C.socket(1, 1, 0); (* AF_UNIX, SOCK_STREAM *)
253+ if fd < 0 then return failed(); end;
254+ if C.connect(fd, ^U8(ADR(a)), U32(SIZE(SockaddrUn))) # 0 then
255+ err := failed(); (* before close can change errno *)
256+ C.close(fd);
257+ return err;
258+ end;
259+ return I64(fd);
260+end;
261+
262+(* The first of fds that has something to read within ms milliseconds,
263+ or that has reached its end or failed, so that a read won't wait; -1
264+ if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
265+proc readyOf*(fds: []I64; ms: I64): I64;
266+var p: [64]Pollfd; n, i: I64;
267+begin
268+ n := LEN(fds);
269+ if n > LEN(p) then n := LEN(p); end;
270+ for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *)
271+ if C.poll(^U8(ADR(p)), U64(n), I32(ms)) <= 0 then return -1; end;
272+ for i := 0 to n - 1 do
273+ if p[i].revents # 0 then return fds[i]; end;
274+ end;
275+ return -1;
276+end;
277+
237278 (* The value of the environment variable name; "" if it isn't set. *)
238279 proc env*(name: []U8): []U8;
239280 var a: [4096]U8; s: ^U8; n: I64;
@@ -272,10 +313,25 @@
272313 C._exit(I32(status));
273314 end;
274315
316+(* A socket listening at path, for the test: its fd. *)
317+proc listen(path: []U8): I64;
318+var a: SockaddrUn; fd: I32; i: I64;
319+begin
320+ remove(path);
321+ a.family := 1;
322+ for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end;
323+ fd := C.socket(1, 1, 0);
324+ if (fd < 0) or (C.bind(fd, ^U8(ADR(a)), U32(SIZE(SockaddrUn))) # 0) or (C.listen(fd, 1) # 0) then
325+ exit(2);
326+ end;
327+ return I64(fd);
328+end;
329+
275330 (* The module's test, as Linux's Sys has it: a file written, read back
276- and closed; a directory made, listed and emptied; no terminal. *)
331+ and closed; a directory made, listed and emptied; no terminal; a
332+ socket. *)
277333 proc main*;
278-var fd, n, err, w, h: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
334+var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
279335 dir: [4096]U8;
280336 begin
281337 fd := create("/tmp/raz-sys-test");
@@ -322,6 +378,22 @@
322378 if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end;
323379 n := workDir(dir);
324380 if (n <= 0) or (dir[0] # '/') then exit(2); end;
381+ (* a socket: nothing at a path, then a listener that a connection
382+ makes ready, and a byte from it to the connection *)
383+ if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *)
384+ l := listen("/tmp/raz-sys-socket");
385+ c := connect("/tmp/raz-sys-socket");
386+ if c < 0 then exit(2); end;
387+ if readyOf([c], 0) # -1 then exit(2); end;
388+ if readyOf([c, l], 1000) # l then exit(2); end;
389+ a := I64(C.accept(I32(l), nil, nil));
390+ if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
391+ if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end;
392+ close(a);
393+ if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *)
394+ close(c);
395+ close(l);
396+ remove("/tmp/raz-sys-socket");
325397 end;
326398
327399 end Sys.