From 46ec3b9fc6cf690e3c569d9343db0f312b487828 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 12:15:41 +0100 Subject: [PATCH] Sys: pseudo-terminals and programs; Info tells a file and a link Step 49b: openPty, setTermSize, spawn, wait, kill, natively by system calls and over C by POSIX's names. spawn builds argv and envp in fresh memory before fork, so the child calls no allocator. Step 49d (report 2026-10.6): Info's file and symlink, by lstat then stat; over C, struct stat is glibc's. The report's field was link, a reserved word: it is symlink. Both Sys tests: a shell on a pty prints its size, args and a replaced var and exits 3; sleep stopped by kill; a missing program ends 127. The compiler follows report 2026-10.7. --- diff --git a/CLAUDE.md b/CLAUDE.md index 4f1bda4..e13a0f2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -62,11 +62,11 @@ environment. `tools/Scan.raz` is theirs to use, as a tool, not part of the library. Stage 4, diagnostics (steps 19-27), is done; so are stage 6, the language of report 2026-10.3 (steps 42-47), and stage 8, -one module to a name (report 2026-10.5, step 51). Stage 7, `Sys` -for rlX11 and rat (report 2026-10.4, approved): steps 49a -(`connect`, `readyOf`) and 49c (`send`, report 2026-10.7) are done, -49b (the pseudo-terminal and programs) waits. The compiler says it -follows report 2026-10.3, the last version all done. New work starts +one module to a name (report 2026-10.5, step 51), and stage 7, `Sys` +for rlX11 and rat (reports 2026-10.4 and 10.7, steps 49a-c), with +`Info`'s `file` and `symlink` for rloc (2026-10.6, step 49d). The +compiler says it follows report 2026-10.7, the last version all done. +New work starts only on the owner's word. Keep `doc/compiler.md`'s table of lines and its routine names true when the compiler changes. Left for later at the owner's word: a rewrite of diff --git a/PLAN.md b/PLAN.md index 17e0539..a905200 100644 --- a/PLAN.md +++ b/PLAN.md @@ -482,7 +482,7 @@ - [x] 49c. `send` (report 2026-10.7), at the owner's word: `write` to a socket with `MSG_NOSIGNAL`, -32 (EPIPE) where `write` would bring SIGPIPE. Both tests send to a connection closed. -- [ ] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and +- [x] 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, @@ -493,6 +493,12 @@ 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`. +- [x] 49d. `Info`'s `file` and `symlink` (report 2026-10.6, for rloc, + which walks a tree and must not open a pipe): on Linux `lstat`, + then `stat`; over C the same by POSIX's names, `struct stat` + glibc's. The report's `link` becomes `symlink`, since `link` is a + reserved word. Both tests: a file, a directory, `/dev/null`, and + a link to a directory. Stage 8: one module to a name, report 2026-10.5 diff --git a/doc/raz.md b/doc/raz.md index 2f41cdf..e273a43 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -9,7 +9,7 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.3 of the report; section 12 says what each +This is version 2026-10.7 of the report; section 12 says what each version changed. ## 1. A first look @@ -957,7 +957,7 @@ size*: I64; (* in bytes *) dir*: BOOL; (* a directory *) file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) - link*: BOOL; (* the path itself is a symbolic link *) + symlink*: BOOL; (* the path itself is a symbolic link *) end; Dir* = record ... end; (* an open directory; its fields are the target's *) @@ -990,7 +990,7 @@ ``` `info` follows a symbolic link: `size`, `dir` and `file` are those of -what it names, and `link` says the path was a link, so that a program walking +what it names, and `symlink` says the path was a link, so that a program walking a tree can choose not to follow one back up it. A link to nothing is `-ENOENT`, as a missing file is. `file` lets a walk open only what holds data: opening a pipe would wait for a program to write to it. diff --git a/lib/C.raz b/lib/C.raz index cc4fd9b..04c45dd 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -37,8 +37,23 @@ 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 stat*(path, st: ^U8): I32; +extern proc lstat*(path, st: ^U8): I32; +extern proc symlink*(target, path: ^U8): I32; +extern proc posix_openpt*(flags: I32): I32; +extern proc grantpt*(fd: I32): I32; +extern proc unlockpt*(fd: I32): I32; +extern proc ptsname*(fd: I32): ^U8; +extern proc fork*: I32; +extern proc setsid*: I32; +extern proc dup2*(fd, fd2: I32): I32; +extern proc close_range*(first, last: U32; flags: I32): I32; +extern proc execve*(path: ^U8; argv, envp: ^^U8): I32; +extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32; +extern proc kill*(pid, signal: I32): I32; extern proc printf*(format: ^U8; ...): I32; extern proc fflush*(f: ^U8): I32; +extern var environ*: ^^U8; extern var stdout*: ^U8; (* FILE * *) extern var stderr*: ^U8; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 5e9063d..d655acf 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -12,6 +12,8 @@ Info* = record size*: I64; (* in bytes *) dir*: BOOL; (* a directory *) + file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) + symlink*: BOOL; (* the path itself is a symbolic link *) end; (* An open directory: getdents64's records, read a block at a time. *) @@ -134,16 +136,21 @@ return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *) end; -(* The size of what path names, and whether it is a directory, - following a symbolic link; and 0, or minus an errno. *) +(* The size of what path names, whether it is a directory or a regular + file, following a symbolic link, and whether path is one; and 0, or + minus an errno. *) proc info*(path: []U8): (Info, I64); var a: [4096]U8; st: [18]I64; i: Info; r: I64; (* struct stat: 144 bytes *) begin if not cstr(path, a) then return i, -36; end; + r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *) + if r # 0 then return i, r; end; + i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) if r # 0 then return i, r; end; i.size := st[6]; (* st_size *) - i.dir := st[3] & 0F000H = 4000H; (* st_mode is S_IFDIR *) + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) return i, 0; end; @@ -302,6 +309,141 @@ return r - 1; end; +(* A pseudo-terminal: the file descriptor of its controlling side, or + minus an errno. Its other side, the terminal its programs run on, is + unlocked, for spawn to open. *) +proc openPty*: I64; +var a: [4096]U8; fd, unlock: I64; +begin + cstr("/dev/ptmx", a); + fd := SYSCALL(2, ADR(a), 80102H); (* open: O_RDWR, O_NOCTTY, O_CLOEXEC *) + if fd < 0 then return fd; end; + unlock := 0; + if SYSCALL(16, fd, 40045431H, ADR(unlock)) # 0 then (* TIOCSPTLCK *) + close(fd); + return -5; (* EIO *) + end; + return fd; +end; + +(* Tells the terminal of the pseudo-terminal fd that it has cols + columns and rows rows: 0, or minus an errno. *) +proc setTermSize*(fd, cols, rows: I64): I64; +var w: Winsize; +begin + w := {rows: U16(rows), cols: U16(cols)}; + return SYSCALL(16, fd, 5414H, ADR(w)); (* TIOCSWINSZ *) +end; + +(* Copies s and a zero to at: the byte after them. *) +proc put(s: []U8; at: ^U8): ^U8; +var i: I64; +begin + for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; + at[LEN(s)] := 0; + return at + (LEN(s) + 1); +end; + +(* Whether the NAME=value s, a C string, names a variable that one of + env gives. *) +proc replaced(s: ^U8; env: [][]U8): BOOL; +var i, n: I64; +begin + for i := 0 to LEN(env) - 1 do + n := 0; + while (n < LEN(env[i])) and (s[n] = env[i][n]) and (s[n] # '=') do INC(n); end; + if (n < LEN(env[i])) and (env[i][n] = '=') and (s[n] = '=') then return true; end; + end; + return false; +end; + +(* What execve takes, in fresh memory of the size returned: args as C + strings and a nil, then the environment old with each NAME=value of + env in place of its variable or added, and a nil; or nil. The + environment follows the arguments. *) +proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, I64); +var p: ^^U8; s: ^U8; size, n, i, k: I64; +begin + n := 0; + while old[n] # nil do INC(n); end; + size := (LEN(args) + n + LEN(env) + 2) * SIZE(^U8); + for i := 0 to LEN(args) - 1 do INC(size, LEN(args[i]) + 1); end; + for i := 0 to LEN(env) - 1 do INC(size, LEN(env[i]) + 1); end; + p := ^^U8(mmap(size)); + if p = nil then return nil, 0; end; + s := ^U8(p + (LEN(args) + n + LEN(env) + 2)); + k := 0; + for i := 0 to LEN(args) - 1 do p[k] := s; s := put(args[i], s); INC(k); end; + p[k] := nil; + INC(k); + for i := 0 to n - 1 do + if not replaced(old[i], env) then p[k] := old[i]; INC(k); end; + end; + for i := 0 to LEN(env) - 1 do p[k] := s; s := put(env[i], s); INC(k); end; + p[k] := nil; + return p, size; +end; + +(* Starts the program at path with args, the first its name, and this + program's environment changed by env: its process id, or minus an + errno. If tty is a pseudo-terminal's fd, the program runs on that + terminal, as its controlling terminal and its standard input, output + and error. It gets no other open file. A program that can't be + started ends with status 127. *) +proc spawn*(path: []U8; args, env: [][]U8; tty: I64): I64; +var a: [4096]U8; name: [32]U8; p: ^^U8; size, pid, n, i, fd: I64; +begin + if not cstr(path, a) then return -36; end; + if tty >= 0 then + (* its terminal: /dev/pts/ and the number TIOCGPTN gives *) + n := 0; + if SYSCALL(16, tty, 80045430H, ADR(n)) # 0 then return -25; end; (* ENOTTY *) + i := put("/dev/pts/", ADR(name[0])) - ADR(name[0]) - 1; (* at its zero *) + size := n; + repeat INC(i); size := size div 10; until size = 0; + name[i] := 0; + repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0; + end; + p, size := argsAndEnv(args, env, ARGV() + (ARGC() + 1)); + if p = nil then return -12; end; (* ENOMEM *) + pid := SYSCALL(57); (* fork *) + if pid = 0 then + if tty >= 0 then + SYSCALL(112); (* setsid *) + fd := SYSCALL(2, ADR(name), 2); (* open, O_RDWR *) + if (fd < 0) or (SYSCALL(16, fd, 540EH, 0) # 0) then SYSCALL(231, 127); end; (* TIOCSCTTY; exit *) + for i := 0 to 2 do SYSCALL(33, fd, i); end; (* dup2 *) + end; + SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *) + SYSCALL(59, ADR(a), p, p + (LEN(args) + 1)); (* execve *) + SYSCALL(231, 127); (* exit *) + end; + munmap(^U8(p), size); + return pid; +end; + +(* Whether the program pid has ended, waiting for it to if block: its + exit status, or 128 and the number of the signal that stopped it, + and 1; or 0 and 0 if it goes on; or minus an errno and 1, if there + is no such program to wait for. *) +proc wait*(pid: I64; block: BOOL): (I64, I64); +var status: I32; r, options: I64; +begin + options := 1; (* WNOHANG *) + if block then options := 0; end; + r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *) + if r < 0 then return r, 1; end; + if r = 0 then return 0, 0; end; + if status & 7FH = 0 then return I64(status) shr 8 & 0FFH, 1; end; + return 128 + I64(status) & 7FH, 1; +end; + +(* Sends the program pid the signal: 0, or minus an errno. *) +proc kill*(pid, signal: I64): I64; +begin + return SYSCALL(62, pid, signal); +end; + (* Gives the processor to another thread. *) proc yield*; begin @@ -329,12 +471,46 @@ return fd; end; +(* The test of programs: one spawned on a pseudo-terminal of 100 by 30 + prints its size, its arguments and a variable put in place, and ends + with status 3; one with no terminal is stopped by kill; one that + isn't there ends with 127. *) +proc testPrograms; +var pty, pid, n, m, status, ended: I64; buf: [64]U8; +begin + pty := openPty(); + if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end; + pid := spawn("/bin/sh", ["sh", "-c", "stty size; echo $0 $1 $HOME; exit 3", "a", "b"], ["HOME=/h"], pty); + if pid <= 0 then exit(2); end; + n := 0; + repeat + if not ready(pty, 5000) then exit(2); end; + m := read(pty, buf[n..LEN(buf)]); + if m > 0 then INC(n, m); end; + until m <= 0; (* -5, EIO, once the terminal has no program *) + close(pty); + if buf[0..n] # "30 100\r\na b /h\r\n" then exit(2); end; + status, ended := wait(pid, true); + if (status # 3) or (ended # 1) then exit(2); end; + pid := spawn("/bin/sleep", ["sleep", "10"], [], -1); + status, ended := wait(pid, false); + if ended # 0 then exit(2); end; + if kill(pid, 15) # 0 then exit(2); end; + status, ended := wait(pid, true); + if (status # 128 + 15) or (ended # 1) then exit(2); end; + status, ended := wait(pid, true); + if (status # -10) or (ended # 1) then exit(2); end; (* ECHILD: waited for already *) + pid := spawn("/no/such/program", ["x"], [], -1); + status, ended := wait(pid, true); + if (status # 127) or (ended # 1) then exit(2); end; +end; + (* The module's test: a file written, read back and closed; a directory made, listed and emptied; no terminal; a socket, sent to after its end. *) proc main*; 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; + dir, name2: [4096]U8; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; @@ -359,9 +535,18 @@ close(fd); if rename("/tmp/raz-sys-dir/f", "/tmp/raz-sys-dir/g") # 0 then exit(2); end; i, err := info("/tmp/raz-sys-dir/g"); - if (err # 0) or (i.size # 3) or i.dir then exit(2); end; + if (err # 0) or (i.size # 3) or i.dir or not i.file or i.symlink then exit(2); end; i, err := info("/tmp/raz-sys-dir"); - if (err # 0) or not i.dir then exit(2); end; + if (err # 0) or not i.dir or i.file then exit(2); end; + i, err := info("/dev/null"); + if (err # 0) or i.dir or i.file then exit(2); end; + remove("/tmp/raz-sys-link"); + cstr("/tmp/raz-sys-dir", dir); + cstr("/tmp/raz-sys-link", name2); + if SYSCALL(88, ADR(dir), ADR(name2)) # 0 then exit(2); end; (* symlink *) + i, err := info("/tmp/raz-sys-link"); + if (err # 0) or not i.dir or not i.symlink then exit(2); end; + remove("/tmp/raz-sys-link"); i, err := info("/no/such/file"); if err >= 0 then exit(2); end; if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; @@ -397,6 +582,7 @@ close(c); close(l); remove("/tmp/raz-sys-socket"); + testPrograms; end; end Sys. diff --git a/lib/Sys.raz b/lib/Sys.raz index 5e618f2..81c3f55 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -2,12 +2,11 @@ file of their own, with the same exports. A program that imports it uses C. - 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. - 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. *) + It keeps to POSIX's names, and raw mode is cfmakeraw's. What is + glibc's, on x86-64: where a name lies in struct dirent; struct stat; + the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and TIOCSCTTY; + close_range; 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; @@ -16,13 +15,17 @@ stdout* = 1; stderr* = 2; DirentName = 19; (* the offset of d_name in glibc's struct dirent *) - TIOCGWINSZ = 5413H; (* glibc's *) + TIOCGWINSZ = 5413H; (* glibc's, as the two below *) + TIOCSWINSZ = 5414H; + TIOCSCTTY = 540EH; type (* What info says of a file. *) Info* = record size*: I64; (* in bytes *) dir*: BOOL; (* a directory *) + file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) + symlink*: BOOL; (* the path itself is a symbolic link *) end; (* An open directory: C's DIR. *) @@ -151,24 +154,19 @@ return 0; end; -(* The size of what path names, and whether it is a directory, - following a symbolic link; and 0, or minus an errno. A directory is - what opendir opens; a file's size is where lseek finds its end, so a - file that can't be read has none. *) +(* The size of what path names, whether it is a directory or a regular + file, following a symbolic link, and whether path is one; and 0, or + minus an errno. *) proc info*(path: []U8): (Info, I64); -var a: [4096]U8; i: Info; d: ^U8; fd: I32; +var a: [4096]U8; st: [18]I64; i: Info; (* glibc's struct stat: 144 bytes *) begin if not cstr(path, a) then return i, -36; end; - d := C.opendir(ADR(a[0])); - if d # nil then - C.closedir(d); - i.dir := true; - return i, 0; - end; - fd := C.open(ADR(a[0]), 0); (* O_RDONLY *) - if fd < 0 then return i, failed(); end; - i.size := C.lseek(fd, 0, 2); (* SEEK_END *) - C.close(fd); + if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; + i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) + if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; + i.size := st[6]; (* st_size *) + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) return i, 0; end; @@ -307,8 +305,146 @@ n := 0; while buf[n] # 0 do INC(n); end; return n; +end; + +(* A pseudo-terminal: the file descriptor of its controlling side, or + minus an errno. Its other side, the terminal its programs run on, is + unlocked, for spawn to open. *) +proc openPty*: I64; +var fd: I32; err: I64; +begin + fd := C.posix_openpt(102H); (* O_RDWR, O_NOCTTY: glibc's *) + if fd < 0 then return failed(); end; + if (C.grantpt(fd) # 0) or (C.unlockpt(fd) # 0) then + err := failed(); + C.close(fd); + return err; + end; + return I64(fd); end; +(* Tells the terminal of the pseudo-terminal fd that it has cols + columns and rows rows: 0, or minus an errno. *) +proc setTermSize*(fd, cols, rows: I64): I64; +var w: Winsize; +begin + w := {rows: U16(rows), cols: U16(cols)}; + if C.ioctl(I32(fd), TIOCSWINSZ, ADR(w)) # 0 then return failed(); end; + return 0; +end; + +(* Copies s and a zero to at: the byte after them. *) +proc put(s: []U8; at: ^U8): ^U8; +var i: I64; +begin + for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; + at[LEN(s)] := 0; + return at + (LEN(s) + 1); +end; + +(* Whether the NAME=value s, a C string, names a variable that one of + env gives. *) +proc replaced(s: ^U8; env: [][]U8): BOOL; +var i, n: I64; +begin + for i := 0 to LEN(env) - 1 do + n := 0; + while (n < LEN(env[i])) and (s[n] = env[i][n]) and (s[n] # '=') do INC(n); end; + if (n < LEN(env[i])) and (env[i][n] = '=') and (s[n] = '=') then return true; end; + end; + return false; +end; + +(* What execve takes, in fresh memory of the size returned: args as C + strings and a nil, then the environment old with each NAME=value of + env in place of its variable or added, and a nil; or nil. The + environment follows the arguments. *) +proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, I64); +var p: ^^U8; s: ^U8; size, n, i, k: I64; +begin + n := 0; + while old[n] # nil do INC(n); end; + size := (LEN(args) + n + LEN(env) + 2) * SIZE(^U8); + for i := 0 to LEN(args) - 1 do INC(size, LEN(args[i]) + 1); end; + for i := 0 to LEN(env) - 1 do INC(size, LEN(env[i]) + 1); end; + p := ^^U8(mmap(size)); + if p = nil then return nil, 0; end; + s := ^U8(p + (LEN(args) + n + LEN(env) + 2)); + k := 0; + for i := 0 to LEN(args) - 1 do p[k] := s; s := put(args[i], s); INC(k); end; + p[k] := nil; + INC(k); + for i := 0 to n - 1 do + if not replaced(old[i], env) then p[k] := old[i]; INC(k); end; + end; + for i := 0 to LEN(env) - 1 do p[k] := s; s := put(env[i], s); INC(k); end; + p[k] := nil; + return p, size; +end; + +(* Starts the program at path with args, the first its name, and this + program's environment changed by env: its process id, or minus an + errno. If tty is a pseudo-terminal's fd, the program runs on that + terminal, as its controlling terminal and its standard input, output + and error. It gets no other open file. A program that can't be + started ends with status 127. *) +proc spawn*(path: []U8; args, env: [][]U8; tty: I64): I64; +var a, name: [4096]U8; p: ^^U8; s: ^U8; size, n, err: I64; pid, fd: I32; +begin + if not cstr(path, a) then return -36; end; + if tty >= 0 then + s := C.ptsname(I32(tty)); (* here, since it isn't safe after fork *) + if s = nil then return failed(); end; + n := 0; + while s[n] # 0 do INC(n); end; + if not cstr(s[0..n], name) then return -36; end; + end; + p, size := argsAndEnv(args, env, C.environ); + if p = nil then return -12; end; (* ENOMEM *) + pid := C.fork(); + if pid = 0 then + if tty >= 0 then + C.setsid(); + fd := C.open(ADR(name[0]), 2); (* O_RDWR *) + if (fd < 0) or (C.ioctl(fd, TIOCSCTTY, 0) # 0) then C._exit(127); end; + C.dup2(fd, 0); + C.dup2(fd, 1); + C.dup2(fd, 2); + end; + C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *) + C.execve(ADR(a[0]), p, p + (LEN(args) + 1)); + C._exit(127); + end; + err := failed(); + munmap(^U8(p), size); + if pid < 0 then return err; end; + return I64(pid); +end; + +(* Whether the program pid has ended, waiting for it to if block: its + exit status, or 128 and the number of the signal that stopped it, + and 1; or 0 and 0 if it goes on; or minus an errno and 1, if there + is no such program to wait for. The status is read as glibc's macros + read it. *) +proc wait*(pid: I64; block: BOOL): (I64, I64); +var status, options, r: I32; +begin + options := 1; (* WNOHANG *) + if block then options := 0; end; + r := C.waitpid(I32(pid), ADR(status), options); + if r < 0 then return failed(), 1; end; + if r = 0 then return 0, 0; end; + if status & 7FH = 0 then return I64(status) shr 8 & 0FFH, 1; end; + return 128 + I64(status) & 7FH, 1; +end; + +(* Sends the program pid the signal: 0, or minus an errno. *) +proc kill*(pid, signal: I64): I64; +begin + if C.kill(I32(pid), I32(signal)) # 0 then return failed(); end; + return 0; +end; + proc isTerminal*(fd: I64): BOOL; begin return C.isatty(I32(fd)) = 1; @@ -339,12 +475,46 @@ return I64(fd); end; +(* The test of programs: one spawned on a pseudo-terminal of 100 by 30 + prints its size, its arguments and a variable put in place, and ends + with status 3; one with no terminal is stopped by kill; one that + isn't there ends with 127. *) +proc testPrograms; +var pty, pid, n, m, status, ended: I64; buf: [64]U8; +begin + pty := openPty(); + if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end; + pid := spawn("/bin/sh", ["sh", "-c", "stty size; echo $0 $1 $HOME; exit 3", "a", "b"], ["HOME=/h"], pty); + if pid <= 0 then exit(2); end; + n := 0; + repeat + if not ready(pty, 5000) then exit(2); end; + m := read(pty, buf[n..LEN(buf)]); + if m > 0 then INC(n, m); end; + until m <= 0; (* -5, EIO, once the terminal has no program *) + close(pty); + if buf[0..n] # "30 100\r\na b /h\r\n" then exit(2); end; + status, ended := wait(pid, true); + if (status # 3) or (ended # 1) then exit(2); end; + pid := spawn("/bin/sleep", ["sleep", "10"], [], -1); + status, ended := wait(pid, false); + if ended # 0 then exit(2); end; + if kill(pid, 15) # 0 then exit(2); end; + status, ended := wait(pid, true); + if (status # 128 + 15) or (ended # 1) then exit(2); end; + status, ended := wait(pid, true); + if (status # -10) or (ended # 1) then exit(2); end; (* ECHILD: waited for already *) + pid := spawn("/no/such/program", ["x"], [], -1); + status, ended := wait(pid, true); + if (status # 127) or (ended # 1) then exit(2); end; +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; a socket, sent to after its end. *) proc main*; 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; + dir, name2: [4096]U8; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; @@ -369,9 +539,18 @@ close(fd); if rename("/tmp/raz-sys-dir/f", "/tmp/raz-sys-dir/g") # 0 then exit(2); end; i, err := info("/tmp/raz-sys-dir/g"); - if (err # 0) or (i.size # 3) or i.dir then exit(2); end; + if (err # 0) or (i.size # 3) or i.dir or not i.file or i.symlink then exit(2); end; i, err := info("/tmp/raz-sys-dir"); - if (err # 0) or not i.dir then exit(2); end; + if (err # 0) or not i.dir or i.file then exit(2); end; + i, err := info("/dev/null"); + if (err # 0) or i.dir or i.file then exit(2); end; + remove("/tmp/raz-sys-link"); + cstr("/tmp/raz-sys-dir", dir); + cstr("/tmp/raz-sys-link", name2); + if C.symlink(ADR(dir[0]), ADR(name2[0])) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-link"); + if (err # 0) or not i.dir or not i.symlink then exit(2); end; + remove("/tmp/raz-sys-link"); i, err := info("/no/such/file"); if err >= 0 then exit(2); end; if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; @@ -407,6 +586,7 @@ close(c); close(l); remove("/tmp/raz-sys-socket"); + testPrograms; end; end Sys. diff --git a/src/raz.pas b/src/raz.pas index 5ea6af5..24452f7 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -31,7 +31,7 @@ { what git describe says, made by make: 0.3.0 at the tag v0.3.0, 0.3.0-5-g38d29a8 five commits past it } Version = {$I version.inc}; - Report = '2026-10.3'; { the version of doc/raz.md it follows } + Report = '2026-10.7'; { the version of doc/raz.md it follows } MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } MfdCloexec = 1; { an ELF still executes: the kernel opens it first } diff --git a/test/err/Clash.err b/test/err/Clash.err index ce01459..c90e32e 100644 --- a/test/err/Clash.err +++ b/test/err/Clash.err @@ -1,5 +1,5 @@ -lib/Sys.raz:12:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz - 12 | import C; +lib/Sys.raz:11:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz + 11 | import C; | ^ help: a program has one module of each name; a symbolic link to one of these files chooses it raz: 1 error