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.
| @@ -62,11 +62,11 @@ | ||
| 62 | 62 | environment. `tools/Scan.raz` is theirs to use, as a tool, not part of |
| 63 | 63 | the library. Stage 4, diagnostics (steps 19-27), is done; so are |
| 64 | 64 | stage 6, the language of report 2026-10.3 (steps 42-47), and stage 8, |
| 65 | -one module to a name (report 2026-10.5, step 51). Stage 7, `Sys` | |
| 66 | -for rlX11 and rat (report 2026-10.4, approved): steps 49a | |
| 67 | -(`connect`, `readyOf`) and 49c (`send`, report 2026-10.7) are done, | |
| 68 | -49b (the pseudo-terminal and programs) waits. The compiler says it | |
| 69 | -follows report 2026-10.3, the last version all done. New work starts | |
| 65 | +one module to a name (report 2026-10.5, step 51), and stage 7, `Sys` | |
| 66 | +for rlX11 and rat (reports 2026-10.4 and 10.7, steps 49a-c), with | |
| 67 | +`Info`'s `file` and `symlink` for rloc (2026-10.6, step 49d). The | |
| 68 | +compiler says it follows report 2026-10.7, the last version all done. | |
| 69 | +New work starts | |
| 70 | 70 | only on the owner's word. Keep |
| 71 | 71 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 72 | 72 | compiler changes. Left for later at the owner's word: a rewrite of |
| @@ -482,7 +482,7 @@ | ||
| 482 | 482 | - [x] 49c. `send` (report 2026-10.7), at the owner's word: `write` to |
| 483 | 483 | a socket with `MSG_NOSIGNAL`, -32 (EPIPE) where `write` would |
| 484 | 484 | bring SIGPIPE. Both tests send to a connection closed. |
| 485 | -- [ ] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and | |
| 485 | +- [x] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and | |
| 486 | 486 | `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its size; |
| 487 | 487 | `fork`, then in the child `setsid`, the terminal opened as its |
| 488 | 488 | controlling one with `TIOCSCTTY`, `dup2` to 0, 1 and 2, |
| @@ -493,6 +493,12 @@ | ||
| 493 | 493 | file, as before. Both files' tests: a program spawned on a |
| 494 | 494 | pseudo-terminal that prints its size and its arguments and ends |
| 495 | 495 | with a status read back by `wait`; a program stopped by `kill`. |
| 496 | +- [x] 49d. `Info`'s `file` and `symlink` (report 2026-10.6, for rloc, | |
| 497 | + which walks a tree and must not open a pipe): on Linux `lstat`, | |
| 498 | + then `stat`; over C the same by POSIX's names, `struct stat` | |
| 499 | + glibc's. The report's `link` becomes `symlink`, since `link` is a | |
| 500 | + reserved word. Both tests: a file, a directory, `/dev/null`, and | |
| 501 | + a link to a directory. | |
| 496 | 502 | |
| 497 | 503 | Stage 8: one module to a name, report 2026-10.5 |
| 498 | 504 |
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | This report defines the language. It is short on purpose. Whatever it |
| 10 | 10 | doesn't allow, raz doesn't have. |
| 11 | 11 | |
| 12 | -This is version 2026-10.3 of the report; section 12 says what each | |
| 12 | +This is version 2026-10.7 of the report; section 12 says what each | |
| 13 | 13 | version changed. |
| 14 | 14 | |
| 15 | 15 | ## 1. A first look |
| @@ -957,7 +957,7 @@ | ||
| 957 | 957 | size*: I64; (* in bytes *) |
| 958 | 958 | dir*: BOOL; (* a directory *) |
| 959 | 959 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 960 | - link*: BOOL; (* the path itself is a symbolic link *) | |
| 960 | + symlink*: BOOL; (* the path itself is a symbolic link *) | |
| 961 | 961 | end; |
| 962 | 962 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| 963 | 963 | |
| @@ -990,7 +990,7 @@ | ||
| 990 | 990 | ``` |
| 991 | 991 | |
| 992 | 992 | `info` follows a symbolic link: `size`, `dir` and `file` are those of |
| 993 | -what it names, and `link` says the path was a link, so that a program walking | |
| 993 | +what it names, and `symlink` says the path was a link, so that a program walking | |
| 994 | 994 | a tree can choose not to follow one back up it. A link to nothing is |
| 995 | 995 | `-ENOENT`, as a missing file is. `file` lets a walk open only what |
| 996 | 996 | holds data: opening a pipe would wait for a program to write to it. |
| @@ -37,8 +37,23 @@ | ||
| 37 | 37 | extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 38 | 38 | extern proc getenv*(name: ^U8): ^U8; |
| 39 | 39 | extern proc getcwd*(buf: ^U8; n: U64): ^U8; |
| 40 | +extern proc stat*(path, st: ^U8): I32; | |
| 41 | +extern proc lstat*(path, st: ^U8): I32; | |
| 42 | +extern proc symlink*(target, path: ^U8): I32; | |
| 43 | +extern proc posix_openpt*(flags: I32): I32; | |
| 44 | +extern proc grantpt*(fd: I32): I32; | |
| 45 | +extern proc unlockpt*(fd: I32): I32; | |
| 46 | +extern proc ptsname*(fd: I32): ^U8; | |
| 47 | +extern proc fork*: I32; | |
| 48 | +extern proc setsid*: I32; | |
| 49 | +extern proc dup2*(fd, fd2: I32): I32; | |
| 50 | +extern proc close_range*(first, last: U32; flags: I32): I32; | |
| 51 | +extern proc execve*(path: ^U8; argv, envp: ^^U8): I32; | |
| 52 | +extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32; | |
| 53 | +extern proc kill*(pid, signal: I32): I32; | |
| 40 | 54 | extern proc printf*(format: ^U8; ...): I32; |
| 41 | 55 | extern proc fflush*(f: ^U8): I32; |
| 56 | +extern var environ*: ^^U8; | |
| 42 | 57 | extern var stdout*: ^U8; (* FILE * *) |
| 43 | 58 | extern var stderr*: ^U8; |
| 44 | 59 |
| @@ -12,6 +12,8 @@ | ||
| 12 | 12 | Info* = record |
| 13 | 13 | size*: I64; (* in bytes *) |
| 14 | 14 | dir*: BOOL; (* a directory *) |
| 15 | + file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) | |
| 16 | + symlink*: BOOL; (* the path itself is a symbolic link *) | |
| 15 | 17 | end; |
| 16 | 18 | |
| 17 | 19 | (* An open directory: getdents64's records, read a block at a time. *) |
| @@ -134,16 +136,21 @@ | ||
| 134 | 136 | return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *) |
| 135 | 137 | end; |
| 136 | 138 | |
| 137 | -(* The size of what path names, and whether it is a directory, | |
| 138 | - following a symbolic link; and 0, or minus an errno. *) | |
| 139 | +(* The size of what path names, whether it is a directory or a regular | |
| 140 | + file, following a symbolic link, and whether path is one; and 0, or | |
| 141 | + minus an errno. *) | |
| 139 | 142 | proc info*(path: []U8): (Info, I64); |
| 140 | 143 | var a: [4096]U8; st: [18]I64; i: Info; r: I64; (* struct stat: 144 bytes *) |
| 141 | 144 | begin |
| 142 | 145 | if not cstr(path, a) then return i, -36; end; |
| 146 | + r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *) | |
| 147 | + if r # 0 then return i, r; end; | |
| 148 | + i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) | |
| 143 | 149 | r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) |
| 144 | 150 | if r # 0 then return i, r; end; |
| 145 | 151 | i.size := st[6]; (* st_size *) |
| 146 | - i.dir := st[3] & 0F000H = 4000H; (* st_mode is S_IFDIR *) | |
| 152 | + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) | |
| 153 | + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) | |
| 147 | 154 | return i, 0; |
| 148 | 155 | end; |
| 149 | 156 | |
| @@ -302,6 +309,141 @@ | ||
| 302 | 309 | return r - 1; |
| 303 | 310 | end; |
| 304 | 311 | |
| 312 | +(* A pseudo-terminal: the file descriptor of its controlling side, or | |
| 313 | + minus an errno. Its other side, the terminal its programs run on, is | |
| 314 | + unlocked, for spawn to open. *) | |
| 315 | +proc openPty*: I64; | |
| 316 | +var a: [4096]U8; fd, unlock: I64; | |
| 317 | +begin | |
| 318 | + cstr("/dev/ptmx", a); | |
| 319 | + fd := SYSCALL(2, ADR(a), 80102H); (* open: O_RDWR, O_NOCTTY, O_CLOEXEC *) | |
| 320 | + if fd < 0 then return fd; end; | |
| 321 | + unlock := 0; | |
| 322 | + if SYSCALL(16, fd, 40045431H, ADR(unlock)) # 0 then (* TIOCSPTLCK *) | |
| 323 | + close(fd); | |
| 324 | + return -5; (* EIO *) | |
| 325 | + end; | |
| 326 | + return fd; | |
| 327 | +end; | |
| 328 | + | |
| 329 | +(* Tells the terminal of the pseudo-terminal fd that it has cols | |
| 330 | + columns and rows rows: 0, or minus an errno. *) | |
| 331 | +proc setTermSize*(fd, cols, rows: I64): I64; | |
| 332 | +var w: Winsize; | |
| 333 | +begin | |
| 334 | + w := {rows: U16(rows), cols: U16(cols)}; | |
| 335 | + return SYSCALL(16, fd, 5414H, ADR(w)); (* TIOCSWINSZ *) | |
| 336 | +end; | |
| 337 | + | |
| 338 | +(* Copies s and a zero to at: the byte after them. *) | |
| 339 | +proc put(s: []U8; at: ^U8): ^U8; | |
| 340 | +var i: I64; | |
| 341 | +begin | |
| 342 | + for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; | |
| 343 | + at[LEN(s)] := 0; | |
| 344 | + return at + (LEN(s) + 1); | |
| 345 | +end; | |
| 346 | + | |
| 347 | +(* Whether the NAME=value s, a C string, names a variable that one of | |
| 348 | + env gives. *) | |
| 349 | +proc replaced(s: ^U8; env: [][]U8): BOOL; | |
| 350 | +var i, n: I64; | |
| 351 | +begin | |
| 352 | + for i := 0 to LEN(env) - 1 do | |
| 353 | + n := 0; | |
| 354 | + while (n < LEN(env[i])) and (s[n] = env[i][n]) and (s[n] # '=') do INC(n); end; | |
| 355 | + if (n < LEN(env[i])) and (env[i][n] = '=') and (s[n] = '=') then return true; end; | |
| 356 | + end; | |
| 357 | + return false; | |
| 358 | +end; | |
| 359 | + | |
| 360 | +(* What execve takes, in fresh memory of the size returned: args as C | |
| 361 | + strings and a nil, then the environment old with each NAME=value of | |
| 362 | + env in place of its variable or added, and a nil; or nil. The | |
| 363 | + environment follows the arguments. *) | |
| 364 | +proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, I64); | |
| 365 | +var p: ^^U8; s: ^U8; size, n, i, k: I64; | |
| 366 | +begin | |
| 367 | + n := 0; | |
| 368 | + while old[n] # nil do INC(n); end; | |
| 369 | + size := (LEN(args) + n + LEN(env) + 2) * SIZE(^U8); | |
| 370 | + for i := 0 to LEN(args) - 1 do INC(size, LEN(args[i]) + 1); end; | |
| 371 | + for i := 0 to LEN(env) - 1 do INC(size, LEN(env[i]) + 1); end; | |
| 372 | + p := ^^U8(mmap(size)); | |
| 373 | + if p = nil then return nil, 0; end; | |
| 374 | + s := ^U8(p + (LEN(args) + n + LEN(env) + 2)); | |
| 375 | + k := 0; | |
| 376 | + for i := 0 to LEN(args) - 1 do p[k] := s; s := put(args[i], s); INC(k); end; | |
| 377 | + p[k] := nil; | |
| 378 | + INC(k); | |
| 379 | + for i := 0 to n - 1 do | |
| 380 | + if not replaced(old[i], env) then p[k] := old[i]; INC(k); end; | |
| 381 | + end; | |
| 382 | + for i := 0 to LEN(env) - 1 do p[k] := s; s := put(env[i], s); INC(k); end; | |
| 383 | + p[k] := nil; | |
| 384 | + return p, size; | |
| 385 | +end; | |
| 386 | + | |
| 387 | +(* Starts the program at path with args, the first its name, and this | |
| 388 | + program's environment changed by env: its process id, or minus an | |
| 389 | + errno. If tty is a pseudo-terminal's fd, the program runs on that | |
| 390 | + terminal, as its controlling terminal and its standard input, output | |
| 391 | + and error. It gets no other open file. A program that can't be | |
| 392 | + started ends with status 127. *) | |
| 393 | +proc spawn*(path: []U8; args, env: [][]U8; tty: I64): I64; | |
| 394 | +var a: [4096]U8; name: [32]U8; p: ^^U8; size, pid, n, i, fd: I64; | |
| 395 | +begin | |
| 396 | + if not cstr(path, a) then return -36; end; | |
| 397 | + if tty >= 0 then | |
| 398 | + (* its terminal: /dev/pts/ and the number TIOCGPTN gives *) | |
| 399 | + n := 0; | |
| 400 | + if SYSCALL(16, tty, 80045430H, ADR(n)) # 0 then return -25; end; (* ENOTTY *) | |
| 401 | + i := put("/dev/pts/", ADR(name[0])) - ADR(name[0]) - 1; (* at its zero *) | |
| 402 | + size := n; | |
| 403 | + repeat INC(i); size := size div 10; until size = 0; | |
| 404 | + name[i] := 0; | |
| 405 | + repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0; | |
| 406 | + end; | |
| 407 | + p, size := argsAndEnv(args, env, ARGV() + (ARGC() + 1)); | |
| 408 | + if p = nil then return -12; end; (* ENOMEM *) | |
| 409 | + pid := SYSCALL(57); (* fork *) | |
| 410 | + if pid = 0 then | |
| 411 | + if tty >= 0 then | |
| 412 | + SYSCALL(112); (* setsid *) | |
| 413 | + fd := SYSCALL(2, ADR(name), 2); (* open, O_RDWR *) | |
| 414 | + if (fd < 0) or (SYSCALL(16, fd, 540EH, 0) # 0) then SYSCALL(231, 127); end; (* TIOCSCTTY; exit *) | |
| 415 | + for i := 0 to 2 do SYSCALL(33, fd, i); end; (* dup2 *) | |
| 416 | + end; | |
| 417 | + SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *) | |
| 418 | + SYSCALL(59, ADR(a), p, p + (LEN(args) + 1)); (* execve *) | |
| 419 | + SYSCALL(231, 127); (* exit *) | |
| 420 | + end; | |
| 421 | + munmap(^U8(p), size); | |
| 422 | + return pid; | |
| 423 | +end; | |
| 424 | + | |
| 425 | +(* Whether the program pid has ended, waiting for it to if block: its | |
| 426 | + exit status, or 128 and the number of the signal that stopped it, | |
| 427 | + and 1; or 0 and 0 if it goes on; or minus an errno and 1, if there | |
| 428 | + is no such program to wait for. *) | |
| 429 | +proc wait*(pid: I64; block: BOOL): (I64, I64); | |
| 430 | +var status: I32; r, options: I64; | |
| 431 | +begin | |
| 432 | + options := 1; (* WNOHANG *) | |
| 433 | + if block then options := 0; end; | |
| 434 | + r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *) | |
| 435 | + if r < 0 then return r, 1; end; | |
| 436 | + if r = 0 then return 0, 0; end; | |
| 437 | + if status & 7FH = 0 then return I64(status) shr 8 & 0FFH, 1; end; | |
| 438 | + return 128 + I64(status) & 7FH, 1; | |
| 439 | +end; | |
| 440 | + | |
| 441 | +(* Sends the program pid the signal: 0, or minus an errno. *) | |
| 442 | +proc kill*(pid, signal: I64): I64; | |
| 443 | +begin | |
| 444 | + return SYSCALL(62, pid, signal); | |
| 445 | +end; | |
| 446 | + | |
| 305 | 447 | (* Gives the processor to another thread. *) |
| 306 | 448 | proc yield*; |
| 307 | 449 | begin |
| @@ -329,12 +471,46 @@ | ||
| 329 | 471 | return fd; |
| 330 | 472 | end; |
| 331 | 473 | |
| 474 | +(* The test of programs: one spawned on a pseudo-terminal of 100 by 30 | |
| 475 | + prints its size, its arguments and a variable put in place, and ends | |
| 476 | + with status 3; one with no terminal is stopped by kill; one that | |
| 477 | + isn't there ends with 127. *) | |
| 478 | +proc testPrograms; | |
| 479 | +var pty, pid, n, m, status, ended: I64; buf: [64]U8; | |
| 480 | +begin | |
| 481 | + pty := openPty(); | |
| 482 | + if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end; | |
| 483 | + pid := spawn("/bin/sh", ["sh", "-c", "stty size; echo $0 $1 $HOME; exit 3", "a", "b"], ["HOME=/h"], pty); | |
| 484 | + if pid <= 0 then exit(2); end; | |
| 485 | + n := 0; | |
| 486 | + repeat | |
| 487 | + if not ready(pty, 5000) then exit(2); end; | |
| 488 | + m := read(pty, buf[n..LEN(buf)]); | |
| 489 | + if m > 0 then INC(n, m); end; | |
| 490 | + until m <= 0; (* -5, EIO, once the terminal has no program *) | |
| 491 | + close(pty); | |
| 492 | + if buf[0..n] # "30 100\r\na b /h\r\n" then exit(2); end; | |
| 493 | + status, ended := wait(pid, true); | |
| 494 | + if (status # 3) or (ended # 1) then exit(2); end; | |
| 495 | + pid := spawn("/bin/sleep", ["sleep", "10"], [], -1); | |
| 496 | + status, ended := wait(pid, false); | |
| 497 | + if ended # 0 then exit(2); end; | |
| 498 | + if kill(pid, 15) # 0 then exit(2); end; | |
| 499 | + status, ended := wait(pid, true); | |
| 500 | + if (status # 128 + 15) or (ended # 1) then exit(2); end; | |
| 501 | + status, ended := wait(pid, true); | |
| 502 | + if (status # -10) or (ended # 1) then exit(2); end; (* ECHILD: waited for already *) | |
| 503 | + pid := spawn("/no/such/program", ["x"], [], -1); | |
| 504 | + status, ended := wait(pid, true); | |
| 505 | + if (status # 127) or (ended # 1) then exit(2); end; | |
| 506 | +end; | |
| 507 | + | |
| 332 | 508 | (* The module's test: a file written, read back and closed; a |
| 333 | 509 | directory made, listed and emptied; no terminal; a socket, sent to |
| 334 | 510 | after its end. *) |
| 335 | 511 | proc main*; |
| 336 | 512 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 337 | - dir: [4096]U8; | |
| 513 | + dir, name2: [4096]U8; | |
| 338 | 514 | begin |
| 339 | 515 | fd := create("/tmp/raz-sys-test"); |
| 340 | 516 | if fd < 0 then exit(2); end; |
| @@ -359,9 +535,18 @@ | ||
| 359 | 535 | close(fd); |
| 360 | 536 | if rename("/tmp/raz-sys-dir/f", "/tmp/raz-sys-dir/g") # 0 then exit(2); end; |
| 361 | 537 | i, err := info("/tmp/raz-sys-dir/g"); |
| 362 | - if (err # 0) or (i.size # 3) or i.dir then exit(2); end; | |
| 538 | + if (err # 0) or (i.size # 3) or i.dir or not i.file or i.symlink then exit(2); end; | |
| 363 | 539 | i, err := info("/tmp/raz-sys-dir"); |
| 364 | - if (err # 0) or not i.dir then exit(2); end; | |
| 540 | + if (err # 0) or not i.dir or i.file then exit(2); end; | |
| 541 | + i, err := info("/dev/null"); | |
| 542 | + if (err # 0) or i.dir or i.file then exit(2); end; | |
| 543 | + remove("/tmp/raz-sys-link"); | |
| 544 | + cstr("/tmp/raz-sys-dir", dir); | |
| 545 | + cstr("/tmp/raz-sys-link", name2); | |
| 546 | + if SYSCALL(88, ADR(dir), ADR(name2)) # 0 then exit(2); end; (* symlink *) | |
| 547 | + i, err := info("/tmp/raz-sys-link"); | |
| 548 | + if (err # 0) or not i.dir or not i.symlink then exit(2); end; | |
| 549 | + remove("/tmp/raz-sys-link"); | |
| 365 | 550 | i, err := info("/no/such/file"); |
| 366 | 551 | if err >= 0 then exit(2); end; |
| 367 | 552 | if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; |
| @@ -397,6 +582,7 @@ | ||
| 397 | 582 | close(c); |
| 398 | 583 | close(l); |
| 399 | 584 | remove("/tmp/raz-sys-socket"); |
| 585 | + testPrograms; | |
| 400 | 586 | end; |
| 401 | 587 | |
| 402 | 588 | end Sys. |
| @@ -2,12 +2,11 @@ | ||
| 2 | 2 | file of their own, with the same exports. A program that imports it |
| 3 | 3 | uses C. |
| 4 | 4 | |
| 5 | - It keeps to what POSIX gives: raw mode is cfmakeraw's, and a file's | |
| 6 | - size is where lseek finds its end, so that no struct stat is read. | |
| 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. *) | |
| 5 | + It keeps to POSIX's names, and raw mode is cfmakeraw's. What is | |
| 6 | + glibc's, on x86-64: where a name lies in struct dirent; struct stat; | |
| 7 | + the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and TIOCSCTTY; | |
| 8 | + close_range; and struct sockaddr_un, which on the BSDs starts with a | |
| 9 | + byte of length. A C library that differs needs a Sys of its own. *) | |
| 11 | 10 | module Sys; |
| 12 | 11 | import C; |
| 13 | 12 | |
| @@ -16,13 +15,17 @@ | ||
| 16 | 15 | stdout* = 1; |
| 17 | 16 | stderr* = 2; |
| 18 | 17 | DirentName = 19; (* the offset of d_name in glibc's struct dirent *) |
| 19 | - TIOCGWINSZ = 5413H; (* glibc's *) | |
| 18 | + TIOCGWINSZ = 5413H; (* glibc's, as the two below *) | |
| 19 | + TIOCSWINSZ = 5414H; | |
| 20 | + TIOCSCTTY = 540EH; | |
| 20 | 21 | |
| 21 | 22 | type |
| 22 | 23 | (* What info says of a file. *) |
| 23 | 24 | Info* = record |
| 24 | 25 | size*: I64; (* in bytes *) |
| 25 | 26 | dir*: BOOL; (* a directory *) |
| 27 | + file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) | |
| 28 | + symlink*: BOOL; (* the path itself is a symbolic link *) | |
| 26 | 29 | end; |
| 27 | 30 | |
| 28 | 31 | (* An open directory: C's DIR. *) |
| @@ -151,24 +154,19 @@ | ||
| 151 | 154 | return 0; |
| 152 | 155 | end; |
| 153 | 156 | |
| 154 | -(* The size of what path names, and whether it is a directory, | |
| 155 | - following a symbolic link; and 0, or minus an errno. A directory is | |
| 156 | - what opendir opens; a file's size is where lseek finds its end, so a | |
| 157 | - file that can't be read has none. *) | |
| 157 | +(* The size of what path names, whether it is a directory or a regular | |
| 158 | + file, following a symbolic link, and whether path is one; and 0, or | |
| 159 | + minus an errno. *) | |
| 158 | 160 | proc info*(path: []U8): (Info, I64); |
| 159 | -var a: [4096]U8; i: Info; d: ^U8; fd: I32; | |
| 161 | +var a: [4096]U8; st: [18]I64; i: Info; (* glibc's struct stat: 144 bytes *) | |
| 160 | 162 | begin |
| 161 | 163 | if not cstr(path, a) then return i, -36; end; |
| 162 | - d := C.opendir(ADR(a[0])); | |
| 163 | - if d # nil then | |
| 164 | - C.closedir(d); | |
| 165 | - i.dir := true; | |
| 166 | - return i, 0; | |
| 167 | - end; | |
| 168 | - fd := C.open(ADR(a[0]), 0); (* O_RDONLY *) | |
| 169 | - if fd < 0 then return i, failed(); end; | |
| 170 | - i.size := C.lseek(fd, 0, 2); (* SEEK_END *) | |
| 171 | - C.close(fd); | |
| 164 | + if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; | |
| 165 | + i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) | |
| 166 | + if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; | |
| 167 | + i.size := st[6]; (* st_size *) | |
| 168 | + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) | |
| 169 | + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) | |
| 172 | 170 | return i, 0; |
| 173 | 171 | end; |
| 174 | 172 | |
| @@ -307,8 +305,146 @@ | ||
| 307 | 305 | n := 0; |
| 308 | 306 | while buf[n] # 0 do INC(n); end; |
| 309 | 307 | return n; |
| 308 | +end; | |
| 309 | + | |
| 310 | +(* A pseudo-terminal: the file descriptor of its controlling side, or | |
| 311 | + minus an errno. Its other side, the terminal its programs run on, is | |
| 312 | + unlocked, for spawn to open. *) | |
| 313 | +proc openPty*: I64; | |
| 314 | +var fd: I32; err: I64; | |
| 315 | +begin | |
| 316 | + fd := C.posix_openpt(102H); (* O_RDWR, O_NOCTTY: glibc's *) | |
| 317 | + if fd < 0 then return failed(); end; | |
| 318 | + if (C.grantpt(fd) # 0) or (C.unlockpt(fd) # 0) then | |
| 319 | + err := failed(); | |
| 320 | + C.close(fd); | |
| 321 | + return err; | |
| 322 | + end; | |
| 323 | + return I64(fd); | |
| 310 | 324 | end; |
| 311 | 325 | |
| 326 | +(* Tells the terminal of the pseudo-terminal fd that it has cols | |
| 327 | + columns and rows rows: 0, or minus an errno. *) | |
| 328 | +proc setTermSize*(fd, cols, rows: I64): I64; | |
| 329 | +var w: Winsize; | |
| 330 | +begin | |
| 331 | + w := {rows: U16(rows), cols: U16(cols)}; | |
| 332 | + if C.ioctl(I32(fd), TIOCSWINSZ, ADR(w)) # 0 then return failed(); end; | |
| 333 | + return 0; | |
| 334 | +end; | |
| 335 | + | |
| 336 | +(* Copies s and a zero to at: the byte after them. *) | |
| 337 | +proc put(s: []U8; at: ^U8): ^U8; | |
| 338 | +var i: I64; | |
| 339 | +begin | |
| 340 | + for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; | |
| 341 | + at[LEN(s)] := 0; | |
| 342 | + return at + (LEN(s) + 1); | |
| 343 | +end; | |
| 344 | + | |
| 345 | +(* Whether the NAME=value s, a C string, names a variable that one of | |
| 346 | + env gives. *) | |
| 347 | +proc replaced(s: ^U8; env: [][]U8): BOOL; | |
| 348 | +var i, n: I64; | |
| 349 | +begin | |
| 350 | + for i := 0 to LEN(env) - 1 do | |
| 351 | + n := 0; | |
| 352 | + while (n < LEN(env[i])) and (s[n] = env[i][n]) and (s[n] # '=') do INC(n); end; | |
| 353 | + if (n < LEN(env[i])) and (env[i][n] = '=') and (s[n] = '=') then return true; end; | |
| 354 | + end; | |
| 355 | + return false; | |
| 356 | +end; | |
| 357 | + | |
| 358 | +(* What execve takes, in fresh memory of the size returned: args as C | |
| 359 | + strings and a nil, then the environment old with each NAME=value of | |
| 360 | + env in place of its variable or added, and a nil; or nil. The | |
| 361 | + environment follows the arguments. *) | |
| 362 | +proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, I64); | |
| 363 | +var p: ^^U8; s: ^U8; size, n, i, k: I64; | |
| 364 | +begin | |
| 365 | + n := 0; | |
| 366 | + while old[n] # nil do INC(n); end; | |
| 367 | + size := (LEN(args) + n + LEN(env) + 2) * SIZE(^U8); | |
| 368 | + for i := 0 to LEN(args) - 1 do INC(size, LEN(args[i]) + 1); end; | |
| 369 | + for i := 0 to LEN(env) - 1 do INC(size, LEN(env[i]) + 1); end; | |
| 370 | + p := ^^U8(mmap(size)); | |
| 371 | + if p = nil then return nil, 0; end; | |
| 372 | + s := ^U8(p + (LEN(args) + n + LEN(env) + 2)); | |
| 373 | + k := 0; | |
| 374 | + for i := 0 to LEN(args) - 1 do p[k] := s; s := put(args[i], s); INC(k); end; | |
| 375 | + p[k] := nil; | |
| 376 | + INC(k); | |
| 377 | + for i := 0 to n - 1 do | |
| 378 | + if not replaced(old[i], env) then p[k] := old[i]; INC(k); end; | |
| 379 | + end; | |
| 380 | + for i := 0 to LEN(env) - 1 do p[k] := s; s := put(env[i], s); INC(k); end; | |
| 381 | + p[k] := nil; | |
| 382 | + return p, size; | |
| 383 | +end; | |
| 384 | + | |
| 385 | +(* Starts the program at path with args, the first its name, and this | |
| 386 | + program's environment changed by env: its process id, or minus an | |
| 387 | + errno. If tty is a pseudo-terminal's fd, the program runs on that | |
| 388 | + terminal, as its controlling terminal and its standard input, output | |
| 389 | + and error. It gets no other open file. A program that can't be | |
| 390 | + started ends with status 127. *) | |
| 391 | +proc spawn*(path: []U8; args, env: [][]U8; tty: I64): I64; | |
| 392 | +var a, name: [4096]U8; p: ^^U8; s: ^U8; size, n, err: I64; pid, fd: I32; | |
| 393 | +begin | |
| 394 | + if not cstr(path, a) then return -36; end; | |
| 395 | + if tty >= 0 then | |
| 396 | + s := C.ptsname(I32(tty)); (* here, since it isn't safe after fork *) | |
| 397 | + if s = nil then return failed(); end; | |
| 398 | + n := 0; | |
| 399 | + while s[n] # 0 do INC(n); end; | |
| 400 | + if not cstr(s[0..n], name) then return -36; end; | |
| 401 | + end; | |
| 402 | + p, size := argsAndEnv(args, env, C.environ); | |
| 403 | + if p = nil then return -12; end; (* ENOMEM *) | |
| 404 | + pid := C.fork(); | |
| 405 | + if pid = 0 then | |
| 406 | + if tty >= 0 then | |
| 407 | + C.setsid(); | |
| 408 | + fd := C.open(ADR(name[0]), 2); (* O_RDWR *) | |
| 409 | + if (fd < 0) or (C.ioctl(fd, TIOCSCTTY, 0) # 0) then C._exit(127); end; | |
| 410 | + C.dup2(fd, 0); | |
| 411 | + C.dup2(fd, 1); | |
| 412 | + C.dup2(fd, 2); | |
| 413 | + end; | |
| 414 | + C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *) | |
| 415 | + C.execve(ADR(a[0]), p, p + (LEN(args) + 1)); | |
| 416 | + C._exit(127); | |
| 417 | + end; | |
| 418 | + err := failed(); | |
| 419 | + munmap(^U8(p), size); | |
| 420 | + if pid < 0 then return err; end; | |
| 421 | + return I64(pid); | |
| 422 | +end; | |
| 423 | + | |
| 424 | +(* Whether the program pid has ended, waiting for it to if block: its | |
| 425 | + exit status, or 128 and the number of the signal that stopped it, | |
| 426 | + and 1; or 0 and 0 if it goes on; or minus an errno and 1, if there | |
| 427 | + is no such program to wait for. The status is read as glibc's macros | |
| 428 | + read it. *) | |
| 429 | +proc wait*(pid: I64; block: BOOL): (I64, I64); | |
| 430 | +var status, options, r: I32; | |
| 431 | +begin | |
| 432 | + options := 1; (* WNOHANG *) | |
| 433 | + if block then options := 0; end; | |
| 434 | + r := C.waitpid(I32(pid), ADR(status), options); | |
| 435 | + if r < 0 then return failed(), 1; end; | |
| 436 | + if r = 0 then return 0, 0; end; | |
| 437 | + if status & 7FH = 0 then return I64(status) shr 8 & 0FFH, 1; end; | |
| 438 | + return 128 + I64(status) & 7FH, 1; | |
| 439 | +end; | |
| 440 | + | |
| 441 | +(* Sends the program pid the signal: 0, or minus an errno. *) | |
| 442 | +proc kill*(pid, signal: I64): I64; | |
| 443 | +begin | |
| 444 | + if C.kill(I32(pid), I32(signal)) # 0 then return failed(); end; | |
| 445 | + return 0; | |
| 446 | +end; | |
| 447 | + | |
| 312 | 448 | proc isTerminal*(fd: I64): BOOL; |
| 313 | 449 | begin |
| 314 | 450 | return C.isatty(I32(fd)) = 1; |
| @@ -339,12 +475,46 @@ | ||
| 339 | 475 | return I64(fd); |
| 340 | 476 | end; |
| 341 | 477 | |
| 478 | +(* The test of programs: one spawned on a pseudo-terminal of 100 by 30 | |
| 479 | + prints its size, its arguments and a variable put in place, and ends | |
| 480 | + with status 3; one with no terminal is stopped by kill; one that | |
| 481 | + isn't there ends with 127. *) | |
| 482 | +proc testPrograms; | |
| 483 | +var pty, pid, n, m, status, ended: I64; buf: [64]U8; | |
| 484 | +begin | |
| 485 | + pty := openPty(); | |
| 486 | + if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end; | |
| 487 | + pid := spawn("/bin/sh", ["sh", "-c", "stty size; echo $0 $1 $HOME; exit 3", "a", "b"], ["HOME=/h"], pty); | |
| 488 | + if pid <= 0 then exit(2); end; | |
| 489 | + n := 0; | |
| 490 | + repeat | |
| 491 | + if not ready(pty, 5000) then exit(2); end; | |
| 492 | + m := read(pty, buf[n..LEN(buf)]); | |
| 493 | + if m > 0 then INC(n, m); end; | |
| 494 | + until m <= 0; (* -5, EIO, once the terminal has no program *) | |
| 495 | + close(pty); | |
| 496 | + if buf[0..n] # "30 100\r\na b /h\r\n" then exit(2); end; | |
| 497 | + status, ended := wait(pid, true); | |
| 498 | + if (status # 3) or (ended # 1) then exit(2); end; | |
| 499 | + pid := spawn("/bin/sleep", ["sleep", "10"], [], -1); | |
| 500 | + status, ended := wait(pid, false); | |
| 501 | + if ended # 0 then exit(2); end; | |
| 502 | + if kill(pid, 15) # 0 then exit(2); end; | |
| 503 | + status, ended := wait(pid, true); | |
| 504 | + if (status # 128 + 15) or (ended # 1) then exit(2); end; | |
| 505 | + status, ended := wait(pid, true); | |
| 506 | + if (status # -10) or (ended # 1) then exit(2); end; (* ECHILD: waited for already *) | |
| 507 | + pid := spawn("/no/such/program", ["x"], [], -1); | |
| 508 | + status, ended := wait(pid, true); | |
| 509 | + if (status # 127) or (ended # 1) then exit(2); end; | |
| 510 | +end; | |
| 511 | + | |
| 342 | 512 | (* The module's test, as Linux's Sys has it: a file written, read back |
| 343 | 513 | and closed; a directory made, listed and emptied; no terminal; a |
| 344 | 514 | socket, sent to after its end. *) |
| 345 | 515 | proc main*; |
| 346 | 516 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 347 | - dir: [4096]U8; | |
| 517 | + dir, name2: [4096]U8; | |
| 348 | 518 | begin |
| 349 | 519 | fd := create("/tmp/raz-sys-test"); |
| 350 | 520 | if fd < 0 then exit(2); end; |
| @@ -369,9 +539,18 @@ | ||
| 369 | 539 | close(fd); |
| 370 | 540 | if rename("/tmp/raz-sys-dir/f", "/tmp/raz-sys-dir/g") # 0 then exit(2); end; |
| 371 | 541 | i, err := info("/tmp/raz-sys-dir/g"); |
| 372 | - if (err # 0) or (i.size # 3) or i.dir then exit(2); end; | |
| 542 | + if (err # 0) or (i.size # 3) or i.dir or not i.file or i.symlink then exit(2); end; | |
| 373 | 543 | i, err := info("/tmp/raz-sys-dir"); |
| 374 | - if (err # 0) or not i.dir then exit(2); end; | |
| 544 | + if (err # 0) or not i.dir or i.file then exit(2); end; | |
| 545 | + i, err := info("/dev/null"); | |
| 546 | + if (err # 0) or i.dir or i.file then exit(2); end; | |
| 547 | + remove("/tmp/raz-sys-link"); | |
| 548 | + cstr("/tmp/raz-sys-dir", dir); | |
| 549 | + cstr("/tmp/raz-sys-link", name2); | |
| 550 | + if C.symlink(ADR(dir[0]), ADR(name2[0])) # 0 then exit(2); end; | |
| 551 | + i, err := info("/tmp/raz-sys-link"); | |
| 552 | + if (err # 0) or not i.dir or not i.symlink then exit(2); end; | |
| 553 | + remove("/tmp/raz-sys-link"); | |
| 375 | 554 | i, err := info("/no/such/file"); |
| 376 | 555 | if err >= 0 then exit(2); end; |
| 377 | 556 | if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; |
| @@ -407,6 +586,7 @@ | ||
| 407 | 586 | close(c); |
| 408 | 587 | close(l); |
| 409 | 588 | remove("/tmp/raz-sys-socket"); |
| 589 | + testPrograms; | |
| 410 | 590 | end; |
| 411 | 591 | |
| 412 | 592 | end Sys. |
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 32 | 32 | 0.3.0-5-g38d29a8 five commits past it } |
| 33 | 33 | Version = {$I version.inc}; |
| 34 | - Report = '2026-10.3'; { the version of doc/raz.md it follows } | |
| 34 | + Report = '2026-10.7'; { the version of doc/raz.md it follows } | |
| 35 | 35 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 36 | 36 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 37 | 37 |
| @@ -1,5 +1,5 @@ | ||
| 1 | -lib/Sys.raz:12:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz | |
| 2 | - 12 | import C; | |
| 1 | +lib/Sys.raz:11:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz | |
| 2 | + 11 | import C; | |
| 3 | 3 | | ^ |
| 4 | 4 | help: a program has one module of each name; a symbolic link to one of these files chooses it |
| 5 | 5 | raz: 1 error |