From 7924cbb705b97f192d227f614c199a7660051ed3 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:57:53 +0100 Subject: [PATCH] Sys: ignored, findEnv and processors; Rt: exitStatus ignored says whether a signal does nothing now, findEnv tells an unset variable from an empty one, processors counts the cpus the program may run on, and Rt.exitStatus gives an atExit procedure the status exit was given. In both backends; tests in Sys.main and test/run. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 5e7d5a1..46175a8 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -15,8 +15,8 @@ | Module | What | |---|---| -| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | -| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `lockShared`, `close`, `closeChecked`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `makeDirMode`, `chmod`, `setTime`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others, which takes what each C library fixes from `CAbi` | +| `Rt` | ending the program, `exit`, `exitStatus` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `lockShared`, `close`, `closeChecked`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `makeDirMode`, `chmod`, `setTime`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `findEnv`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others, which takes what each C library fixes from `CAbi` | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | @@ -55,6 +55,7 @@ const Full*: ERROR = "too many procedures to run at exit"; proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *) proc exit*(status: INT); (* the atExit procedures, the last first, then the end *) +proc exitStatus*: INT; (* the status exit was given, in an atExit procedure; 0 before *) proc stop*(msg: []U8); (* msg, and a line feed, on standard error, then exit with status 2 *) proc trace*; (* the calls under way, and their variables, on standard error *) ``` @@ -64,6 +65,9 @@ takes each procedure off the list before it runs it, so it runs once even if it fails or calls `exit` itself; in a program that uses C, it ends through C's `exit`, so that C flushes its buffers too. +`exitStatus` is the status `exit` was given, which a procedure `atExit` +registered can read, to note a crash: a failed check and `stop` both end +with 2. It is 0 before `exit` is called. ``` module Mem; @@ -302,6 +306,7 @@ proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *) proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) +proc findEnv*(name: []U8): ([]U8, BOOL); (* the value and true; "" and false if unset, "" and true if empty *) proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *) proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) @@ -325,6 +330,7 @@ proc uid*: INT; (* the user this program runs as *) proc euid*: INT; (* the user whose rights it has: 0 is root *) proc hostName*: ([]U8, INT); (* this machine's name, good until the next call *) +proc processors*: INT; (* the processors this program may run on, at least 1 *) proc cpuTime*(children: BOOL): (I64, I64); (* ns used in the program and in the system; or by its children *) proc limit*(which: INT): (I64, I64, INT); (* the soft limit and the hard; -1 for none *) proc setLimit*(which: INT; soft, hard: I64): INT; @@ -332,6 +338,7 @@ proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *) proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *) proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *) +proc ignored*(sig: INT): BOOL; (* whether sig does nothing now *) proc handle*(sig: INT): INT; (* sig noted, for came; a system call it comes during is -4 *) proc came*(sig: INT): BOOL; (* whether sig came since the last time it was asked *) @@ -520,7 +527,9 @@ taken away from the mode of every file this program, and those it starts, make, and says what they were. `envAt` gives the environment one variable at a time, from 0, for a shell to list it and to pass it -on whole with `onlyEnv`. `setEnv` refuses a name that is empty or +on whole with `onlyEnv`. `env` gives "" for a variable that is unset +and for one set to nothing; `findEnv` tells them apart, as `TZ` needs, +unset meaning the local time and empty meaning UTC. `setEnv` refuses a name that is empty or holds an `=`, and a 0 in either, with -22; as C's `setenv`, it isn't safe while another thread reads the environment, and what `env` gave for a variable may be gone once it sets that variable again. `getMode` reads a terminal's modes, and @@ -534,7 +543,9 @@ `ulimit` does: a program may raise its soft limit up to the hard one, and lower either. `uid` is the user this program runs as, `euid` the one whose rights it has, 0 for root, as a prompt shows `#`; `hostName` -is the machine's name. +is the machine's name. `processors` counts the processors the program +may run on, as the system's affinity mask allows, for a program that +makes a thread to each; it is never less than 1. `wait` waits for the program `pid` to change, for any of this program's if `pid` is -1, or for any of the group `g` if `pid` is @@ -559,6 +570,10 @@ makes a signal do nothing, as a shell ignores `sigTtou` so that it can take its terminal back, and `ignore(sig, false)` gives it back what it does at first. A program that `spawn` starts begins with neither. +`ignored(sig)` says whether a signal does nothing now, because the +program ignored it or because the one that started this one left it +so, as a program in the background or under `nohup` finds `sigInt` and +`sigHup`: it should leave those alone, and `handle` or `catch` the rest. `handle` is for a program that must see a signal while it waits in a system call, as a shell waiting for its foreground program, or for a diff --git a/lib/C.raz b/lib/C.raz index c3825af..d524242 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -86,6 +86,7 @@ extern proc gethostname*(name: ^U8; n: UINT): I32; extern proc access*(path: ^U8; how: I32): I32; extern proc umask*(mask: U32): U32; +extern proc sched_getaffinity*(pid: I32; size: UINT; mask: ^U8): I32; extern proc getrusage*(who: I32; usage: ^U8): I32; extern proc getrlimit*(which: I32; limit: ^U8): I32; extern proc setrlimit*(which: I32; limit: ^U8): I32; diff --git a/lib/Rt.raz b/lib/Rt.raz index 2576481..186a623 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -70,6 +70,7 @@ var atExits: [MaxAtExit]proc; numAtExit: INT; + ending: INT; (* the status exit was given, once it has been *) cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *) (* Set first thing too: where the line table is in them, where the program's code starts and stops, the numbers of procedures and @@ -116,6 +117,13 @@ proc profile; forward; +(* The status exit was given, while the procedures atExit registered + run; 0 before. A failed check and stop both end with 2. *) +proc exitStatus*: INT; +begin + return ending; +end; + (* Runs the procedures registered with atExit, the last first, then ends the program with status: through C's exit in a program that uses C, so that C flushes its buffers too. Each procedure is taken @@ -124,6 +132,7 @@ proc exit*(status: INT); var p: proc; begin + ending := status; while numAtExit > 0 do DEC(numAtExit); p := atExits[numAtExit]; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 7cb966a..1d2b5a8 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -1241,11 +1241,12 @@ return (n = LEN(name)) and (s[n] = '='); end; -(* The value of the environment variable name; "" if it isn't set. - Every program calls it, from Rt, so it reads the environment itself +(* The value of the environment variable name and true, or "" and + false if it isn't set: "" and true if it is set to nothing. Every + program calls env, from Rt, so this reads the environment itself rather than through environment and named, which would add their code to every executable. *) -proc env*(name: []U8): []U8; +proc findEnv*(name: []U8): ([]U8, BOOL); var e: ^^U8; s: ^U8; i, n, m: INT; begin e := envp; @@ -1258,11 +1259,19 @@ if (n = LEN(name)) and (s[n] = '=') then m := n + 1; while s[m] # 0 do INC(m); end; - return s[n + 1..m]; + return s[n + 1..m], true; end; INC(i); end; - return ""; + return "", false; +end; + +(* The value of the environment variable name; "" if it isn't set. *) +proc env*(name: []U8): []U8; +var v: []U8; +begin + v, _ := findEnv(name); + return v; end; (* The environment's ith NAME=value, from 0, and true; "" and false @@ -1479,6 +1488,21 @@ return uts[65..65 + n], 0; end; +(* The number of processors this program may run on, at least 1. *) +proc processors*: INT; +var mask: [1024]U8; n, i, b, count: INT; +begin + n := SYSCALL(204, 0, LEN(mask), ADR(mask)); (* sched_getaffinity: this thread, the bytes it filled in *) + count := 0; + for i := 0 to n - 1 do + for b := 0 to 7 do + if (INT(mask[i]) shr b) & 1 = 1 then INC(count); end; + end; + end; + if count < 1 then return 1; end; (* n < 0: it failed *) + return count; +end; + (* The time on the processor, in ns, that this program has used, or, if children, the programs it started and waited for: in the program itself, and in the system for it. *) @@ -1695,8 +1719,9 @@ return 0; end; -(* Whether this program ignores the signal sig: SIG_IGN is 1. *) -proc ignored(sig: INT): BOOL; +(* Whether the signal sig does nothing now, as ignore(sig, true) or the + program that started this one left it: its handler is SIG_IGN, 1. *) +proc ignored*(sig: INT): BOOL; var act: [4]U64; (* struct sigaction *) begin _ := SYSCALL(13, sig, 0, ADR(act), 8); (* rt_sigaction *) @@ -2258,6 +2283,23 @@ close(l); end; +(* The test of what a program asks of itself: a signal ignored or not, + an environment variable unset or empty, and the processors. *) +proc testProcess; +var v: []U8; found: BOOL; +begin + if (ignore(sigUsr1, true) # 0) or not ignored(sigUsr1) then exit(2); end; + if (ignore(sigUsr1, false) # 0) or ignored(sigUsr1) then exit(2); end; + v, found := findEnv("RAZ_NO_SUCH_VARIABLE"); + if found or (LEN(v) # 0) then exit(2); end; + if setEnv("RAZ_SYS_TEST_EMPTY", "") # 0 then exit(2); end; + v, found := findEnv("RAZ_SYS_TEST_EMPTY"); + if not found or (LEN(v) # 0) then exit(2); end; + v, found := findEnv("PATH"); + if not found or (LEN(v) = 0) then exit(2); end; + if processors() < 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; programs; time and the network. *) @@ -2484,6 +2526,7 @@ testPrograms; testShell; testNet; + testProcess; end; end Sys. diff --git a/lib/Sys.raz b/lib/Sys.raz index 07ed15f..8e7fd00 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -1164,16 +1164,25 @@ return s[0..n]; end; -(* The value of the environment variable name; "" if it isn't set. *) -proc env*(name: []U8): []U8; +(* The value of the environment variable name and true, or "" and + false if it isn't set: "" and true if it is set to nothing. *) +proc findEnv*(name: []U8): ([]U8, BOOL); var a: [4096]U8; s: ^U8; n: INT; begin - if not cstr(name, a) then return ""; end; + if not cstr(name, a) then return "", false; end; s := C.getenv(ADR(a[0])); - if s = nil then return ""; end; + if s = nil then return "", false; end; n := 0; while s[n] # 0 do INC(n); end; - return s[0..n]; + return s[0..n], true; +end; + +(* The value of the environment variable name; "" if it isn't set. *) +proc env*(name: []U8): []U8; +var v: []U8; +begin + v, _ := findEnv(name); + return v; end; (* The environment's ith NAME=value, from 0, and true; "" and false @@ -1353,6 +1362,22 @@ return host[0..n], 0; end; +(* The number of processors this program may run on, at least 1. *) +proc processors*: INT; +var mask: [1024]U8; i, b, count: INT; +begin + for i := 0 to LEN(mask) - 1 do mask[i] := 0; end; (* the C library fills in as many bytes as the system has processors' bits *) + if C.sched_getaffinity(0, UINT(LEN(mask)), ADR(mask[0])) # 0 then return 1; end; + count := 0; + for i := 0 to LEN(mask) - 1 do + for b := 0 to 7 do + if (INT(mask[i]) shr b) & 1 = 1 then INC(count); end; + end; + end; + if count < 1 then return 1; end; + return count; +end; + (* The struct timeval at p, in ns *) proc ns(p: ^U8): I64; begin @@ -1588,8 +1613,9 @@ return h^ = 1; end; -(* Whether this program ignores the signal sig. *) -proc ignored(sig: INT): BOOL; +(* Whether the signal sig does nothing now, as ignore(sig, true) or the + program that started this one left it: its handler is SIG_IGN. *) +proc ignored*(sig: INT): BOOL; var a: Sigaction; begin _ := C.sigaction(I32(sig), nil, ^U8(ADR(a))); @@ -2199,6 +2225,23 @@ close(l); end; +(* The test of what a program asks of itself: a signal ignored or not, + an environment variable unset or empty, and the processors. *) +proc testProcess; +var v: []U8; found: BOOL; +begin + if (ignore(sigUsr1, true) # 0) or not ignored(sigUsr1) then exit(2); end; + if (ignore(sigUsr1, false) # 0) or ignored(sigUsr1) then exit(2); end; + v, found := findEnv("RAZ_NO_SUCH_VARIABLE"); + if found or (LEN(v) # 0) then exit(2); end; + if setEnv("RAZ_SYS_TEST_EMPTY", "") # 0 then exit(2); end; + v, found := findEnv("RAZ_SYS_TEST_EMPTY"); + if not found or (LEN(v) # 0) then exit(2); end; + v, found := findEnv("PATH"); + if not found or (LEN(v) = 0) then exit(2); end; + if processors() < 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; programs; time and the network. *) @@ -2424,6 +2467,7 @@ testPrograms; testShell; testNet; + testProcess; end; end Sys. diff --git a/test/run/ExitStatus.expect b/test/run/ExitStatus.expect new file mode 100644 index 0000000..acda1e9 --- /dev/null +++ b/test/run/ExitStatus.expect @@ -0,0 +1,2 @@ +0 in main +ending with 7 diff --git a/test/run/ExitStatus.raz b/test/run/ExitStatus.raz new file mode 100644 index 0000000..7281933 --- /dev/null +++ b/test/run/ExitStatus.raz @@ -0,0 +1,20 @@ +(* Rt.exitStatus is 0 in main and the status exit was given in the + procedures atExit registered, also when a check or stop ends the + program. *) +module ExitStatus; +import Out, Rt; + +proc report; +begin + Out.str("ending with "); Out.int(Rt.exitStatus()); Out.str("\n"); +end; + +proc main*: INT; +begin + _ := Rt.atExit(report); + Out.int(Rt.exitStatus()); Out.str(" in main\n"); + Rt.exit(7); + return 0; +end; + +end ExitStatus. diff --git a/test/run/ExitStatus.status b/test/run/ExitStatus.status new file mode 100644 index 0000000..7f8f011 --- /dev/null +++ b/test/run/ExitStatus.status @@ -0,0 +1 @@ +7 diff --git a/test/run/StopStatus.expect b/test/run/StopStatus.expect new file mode 100644 index 0000000..081a76c --- /dev/null +++ b/test/run/StopStatus.expect @@ -0,0 +1 @@ +ending with 2 diff --git a/test/run/StopStatus.raz b/test/run/StopStatus.raz new file mode 100644 index 0000000..b2a9cd2 --- /dev/null +++ b/test/run/StopStatus.raz @@ -0,0 +1,20 @@ +(* A failed check ends the program with 2, which the procedures atExit + registered can see. *) +module StopStatus; +import Out, Rt; + +proc report; +begin + Out.str("ending with "); Out.int(Rt.exitStatus()); Out.str("\n"); +end; + +proc main*: INT; +var a: [2]INT; i: INT; +begin + _ := Rt.atExit(report); + i := 5; + a[i] := 1; + return 0; +end; + +end StopStatus. diff --git a/test/run/StopStatus.status b/test/run/StopStatus.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/StopStatus.status @@ -0,0 +1 @@ +2 diff --git a/test/run/StopStatus.stderr b/test/run/StopStatus.stderr new file mode 100644 index 0000000..a6137fd --- /dev/null +++ b/test/run/StopStatus.stderr @@ -0,0 +1,2 @@ +test/run/StopStatus.raz:16:7: index 5 is out of range for a length of 2 + in StopStatus.main at test/run/StopStatus.raz:16:7: i = 5