raz / commit 7924cbb7

commit
7924cbb705b97f192d227f614c199a7660051ed3
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
59f8ade1
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.

patch · log

12 files changed, +177 -18

doc/raz-lib.md (history) modified +19 -4

@@ -15,8 +15,8 @@
1515
1616 | Module | What |
1717 |---|---|
18-| `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 |
19-| `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` |
18+| `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 |
19+| `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` |
2020 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
2121 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
2222 | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike |
@@ -55,6 +55,7 @@
5555 const Full*: ERROR = "too many procedures to run at exit";
5656 proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *)
5757 proc exit*(status: INT); (* the atExit procedures, the last first, then the end *)
58+proc exitStatus*: INT; (* the status exit was given, in an atExit procedure; 0 before *)
5859 proc stop*(msg: []U8); (* msg, and a line feed, on standard error, then exit with status 2 *)
5960 proc trace*; (* the calls under way, and their variables, on standard error *)
6061 ```
@@ -64,6 +65,9 @@
6465 takes each procedure off the list before it runs it, so it runs once
6566 even if it fails or calls `exit` itself; in a program that uses C, it
6667 ends through C's `exit`, so that C flushes its buffers too.
68+`exitStatus` is the status `exit` was given, which a procedure `atExit`
69+registered can read, to note a crash: a failed check and `stop` both end
70+with 2. It is 0 before `exit` is called.
6771
6872 ```
6973 module Mem;
@@ -302,6 +306,7 @@
302306 proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *)
303307
304308 proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *)
309+proc findEnv*(name: []U8): ([]U8, BOOL); (* the value and true; "" and false if unset, "" and true if empty *)
305310 proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *)
306311 proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *)
307312 proc workDir*: ([]U8, INT); (* the working directory, good until the next call *)
@@ -325,6 +330,7 @@
325330 proc uid*: INT; (* the user this program runs as *)
326331 proc euid*: INT; (* the user whose rights it has: 0 is root *)
327332 proc hostName*: ([]U8, INT); (* this machine's name, good until the next call *)
333+proc processors*: INT; (* the processors this program may run on, at least 1 *)
328334 proc cpuTime*(children: BOOL): (I64, I64); (* ns used in the program and in the system; or by its children *)
329335 proc limit*(which: INT): (I64, I64, INT); (* the soft limit and the hard; -1 for none *)
330336 proc setLimit*(which: INT; soft, hard: I64): INT;
@@ -332,6 +338,7 @@
332338 proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *)
333339 proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *)
334340 proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *)
341+proc ignored*(sig: INT): BOOL; (* whether sig does nothing now *)
335342 proc handle*(sig: INT): INT; (* sig noted, for came; a system call it comes during is -4 *)
336343 proc came*(sig: INT): BOOL; (* whether sig came since the last time it was asked *)
337344
@@ -520,7 +527,9 @@
520527 taken away from the mode of every file this program, and those it
521528 starts, make, and says what they were. `envAt` gives the environment
522529 one variable at a time, from 0, for a shell to list it and to pass it
523-on whole with `onlyEnv`. `setEnv` refuses a name that is empty or
530+on whole with `onlyEnv`. `env` gives "" for a variable that is unset
531+and for one set to nothing; `findEnv` tells them apart, as `TZ` needs,
532+unset meaning the local time and empty meaning UTC. `setEnv` refuses a name that is empty or
524533 holds an `=`, and a 0 in either, with -22; as C's `setenv`, it isn't
525534 safe while another thread reads the environment, and what `env` gave
526535 for a variable may be gone once it sets that variable again. `getMode` reads a terminal's modes, and
@@ -534,7 +543,9 @@
534543 `ulimit` does: a program may raise its soft limit up to the hard one,
535544 and lower either. `uid` is the user this program runs as, `euid` the
536545 one whose rights it has, 0 for root, as a prompt shows `#`; `hostName`
537-is the machine's name.
546+is the machine's name. `processors` counts the processors the program
547+may run on, as the system's affinity mask allows, for a program that
548+makes a thread to each; it is never less than 1.
538549
539550 `wait` waits for the program `pid` to change, for any of this
540551 program's if `pid` is -1, or for any of the group `g` if `pid` is
@@ -559,6 +570,10 @@
559570 makes a signal do nothing, as a shell ignores `sigTtou` so that it can
560571 take its terminal back, and `ignore(sig, false)` gives it back what it
561572 does at first. A program that `spawn` starts begins with neither.
573+`ignored(sig)` says whether a signal does nothing now, because the
574+program ignored it or because the one that started this one left it
575+so, as a program in the background or under `nohup` finds `sigInt` and
576+`sigHup`: it should leave those alone, and `handle` or `catch` the rest.
562577
563578 `handle` is for a program that must see a signal while it waits in a
564579 system call, as a shell waiting for its foreground program, or for a

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

@@ -86,6 +86,7 @@
8686 extern proc gethostname*(name: ^U8; n: UINT): I32;
8787 extern proc access*(path: ^U8; how: I32): I32;
8888 extern proc umask*(mask: U32): U32;
89+extern proc sched_getaffinity*(pid: I32; size: UINT; mask: ^U8): I32;
8990 extern proc getrusage*(who: I32; usage: ^U8): I32;
9091 extern proc getrlimit*(which: I32; limit: ^U8): I32;
9192 extern proc setrlimit*(which: I32; limit: ^U8): I32;

lib/Rt.raz (history) modified +9 -0

@@ -70,6 +70,7 @@
7070 var
7171 atExits: [MaxAtExit]proc;
7272 numAtExit: INT;
73+ ending: INT; (* the status exit was given, once it has been *)
7374 cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *)
7475 (* Set first thing too: where the line table is in them, where the
7576 program's code starts and stops, the numbers of procedures and
@@ -116,6 +117,13 @@
116117
117118 proc profile; forward;
118119
120+(* The status exit was given, while the procedures atExit registered
121+ run; 0 before. A failed check and stop both end with 2. *)
122+proc exitStatus*: INT;
123+begin
124+ return ending;
125+end;
126+
119127 (* Runs the procedures registered with atExit, the last first, then
120128 ends the program with status: through C's exit in a program that
121129 uses C, so that C flushes its buffers too. Each procedure is taken
@@ -124,6 +132,7 @@
124132 proc exit*(status: INT);
125133 var p: proc;
126134 begin
135+ ending := status;
127136 while numAtExit > 0 do
128137 DEC(numAtExit);
129138 p := atExits[numAtExit];

lib/Sys.linux-x86_64.raz (history) modified +50 -7

@@ -1241,11 +1241,12 @@
12411241 return (n = LEN(name)) and (s[n] = '=');
12421242 end;
12431243
1244-(* The value of the environment variable name; "" if it isn't set.
1245- Every program calls it, from Rt, so it reads the environment itself
1244+(* The value of the environment variable name and true, or "" and
1245+ false if it isn't set: "" and true if it is set to nothing. Every
1246+ program calls env, from Rt, so this reads the environment itself
12461247 rather than through environment and named, which would add their
12471248 code to every executable. *)
1248-proc env*(name: []U8): []U8;
1249+proc findEnv*(name: []U8): ([]U8, BOOL);
12491250 var e: ^^U8; s: ^U8; i, n, m: INT;
12501251 begin
12511252 e := envp;
@@ -1258,11 +1259,19 @@
12581259 if (n = LEN(name)) and (s[n] = '=') then
12591260 m := n + 1;
12601261 while s[m] # 0 do INC(m); end;
1261- return s[n + 1..m];
1262+ return s[n + 1..m], true;
12621263 end;
12631264 INC(i);
12641265 end;
1265- return "";
1266+ return "", false;
1267+end;
1268+
1269+(* The value of the environment variable name; "" if it isn't set. *)
1270+proc env*(name: []U8): []U8;
1271+var v: []U8;
1272+begin
1273+ v, _ := findEnv(name);
1274+ return v;
12661275 end;
12671276
12681277 (* The environment's ith NAME=value, from 0, and true; "" and false
@@ -1479,6 +1488,21 @@
14791488 return uts[65..65 + n], 0;
14801489 end;
14811490
1491+(* The number of processors this program may run on, at least 1. *)
1492+proc processors*: INT;
1493+var mask: [1024]U8; n, i, b, count: INT;
1494+begin
1495+ n := SYSCALL(204, 0, LEN(mask), ADR(mask)); (* sched_getaffinity: this thread, the bytes it filled in *)
1496+ count := 0;
1497+ for i := 0 to n - 1 do
1498+ for b := 0 to 7 do
1499+ if (INT(mask[i]) shr b) & 1 = 1 then INC(count); end;
1500+ end;
1501+ end;
1502+ if count < 1 then return 1; end; (* n < 0: it failed *)
1503+ return count;
1504+end;
1505+
14821506 (* The time on the processor, in ns, that this program has used, or,
14831507 if children, the programs it started and waited for: in the program
14841508 itself, and in the system for it. *)
@@ -1695,8 +1719,9 @@
16951719 return 0;
16961720 end;
16971721
1698-(* Whether this program ignores the signal sig: SIG_IGN is 1. *)
1699-proc ignored(sig: INT): BOOL;
1722+(* Whether the signal sig does nothing now, as ignore(sig, true) or the
1723+ program that started this one left it: its handler is SIG_IGN, 1. *)
1724+proc ignored*(sig: INT): BOOL;
17001725 var act: [4]U64; (* struct sigaction *)
17011726 begin
17021727 _ := SYSCALL(13, sig, 0, ADR(act), 8); (* rt_sigaction *)
@@ -2258,6 +2283,23 @@
22582283 close(l);
22592284 end;
22602285
2286+(* The test of what a program asks of itself: a signal ignored or not,
2287+ an environment variable unset or empty, and the processors. *)
2288+proc testProcess;
2289+var v: []U8; found: BOOL;
2290+begin
2291+ if (ignore(sigUsr1, true) # 0) or not ignored(sigUsr1) then exit(2); end;
2292+ if (ignore(sigUsr1, false) # 0) or ignored(sigUsr1) then exit(2); end;
2293+ v, found := findEnv("RAZ_NO_SUCH_VARIABLE");
2294+ if found or (LEN(v) # 0) then exit(2); end;
2295+ if setEnv("RAZ_SYS_TEST_EMPTY", "") # 0 then exit(2); end;
2296+ v, found := findEnv("RAZ_SYS_TEST_EMPTY");
2297+ if not found or (LEN(v) # 0) then exit(2); end;
2298+ v, found := findEnv("PATH");
2299+ if not found or (LEN(v) = 0) then exit(2); end;
2300+ if processors() < 1 then exit(2); end;
2301+end;
2302+
22612303 (* The module's test: a file written, read back and closed; a
22622304 directory made, listed and emptied; no terminal; a socket, sent to
22632305 after its end; programs; time and the network. *)
@@ -2484,6 +2526,7 @@
24842526 testPrograms;
24852527 testShell;
24862528 testNet;
2529+ testProcess;
24872530 end;
24882531
24892532 end Sys.

lib/Sys.raz (history) modified +51 -7

@@ -1164,16 +1164,25 @@
11641164 return s[0..n];
11651165 end;
11661166
1167-(* The value of the environment variable name; "" if it isn't set. *)
1168-proc env*(name: []U8): []U8;
1167+(* The value of the environment variable name and true, or "" and
1168+ false if it isn't set: "" and true if it is set to nothing. *)
1169+proc findEnv*(name: []U8): ([]U8, BOOL);
11691170 var a: [4096]U8; s: ^U8; n: INT;
11701171 begin
1171- if not cstr(name, a) then return ""; end;
1172+ if not cstr(name, a) then return "", false; end;
11721173 s := C.getenv(ADR(a[0]));
1173- if s = nil then return ""; end;
1174+ if s = nil then return "", false; end;
11741175 n := 0;
11751176 while s[n] # 0 do INC(n); end;
1176- return s[0..n];
1177+ return s[0..n], true;
1178+end;
1179+
1180+(* The value of the environment variable name; "" if it isn't set. *)
1181+proc env*(name: []U8): []U8;
1182+var v: []U8;
1183+begin
1184+ v, _ := findEnv(name);
1185+ return v;
11771186 end;
11781187
11791188 (* The environment's ith NAME=value, from 0, and true; "" and false
@@ -1353,6 +1362,22 @@
13531362 return host[0..n], 0;
13541363 end;
13551364
1365+(* The number of processors this program may run on, at least 1. *)
1366+proc processors*: INT;
1367+var mask: [1024]U8; i, b, count: INT;
1368+begin
1369+ 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 *)
1370+ if C.sched_getaffinity(0, UINT(LEN(mask)), ADR(mask[0])) # 0 then return 1; end;
1371+ count := 0;
1372+ for i := 0 to LEN(mask) - 1 do
1373+ for b := 0 to 7 do
1374+ if (INT(mask[i]) shr b) & 1 = 1 then INC(count); end;
1375+ end;
1376+ end;
1377+ if count < 1 then return 1; end;
1378+ return count;
1379+end;
1380+
13561381 (* The struct timeval at p, in ns *)
13571382 proc ns(p: ^U8): I64;
13581383 begin
@@ -1588,8 +1613,9 @@
15881613 return h^ = 1;
15891614 end;
15901615
1591-(* Whether this program ignores the signal sig. *)
1592-proc ignored(sig: INT): BOOL;
1616+(* Whether the signal sig does nothing now, as ignore(sig, true) or the
1617+ program that started this one left it: its handler is SIG_IGN. *)
1618+proc ignored*(sig: INT): BOOL;
15931619 var a: Sigaction;
15941620 begin
15951621 _ := C.sigaction(I32(sig), nil, ^U8(ADR(a)));
@@ -2199,6 +2225,23 @@
21992225 close(l);
22002226 end;
22012227
2228+(* The test of what a program asks of itself: a signal ignored or not,
2229+ an environment variable unset or empty, and the processors. *)
2230+proc testProcess;
2231+var v: []U8; found: BOOL;
2232+begin
2233+ if (ignore(sigUsr1, true) # 0) or not ignored(sigUsr1) then exit(2); end;
2234+ if (ignore(sigUsr1, false) # 0) or ignored(sigUsr1) then exit(2); end;
2235+ v, found := findEnv("RAZ_NO_SUCH_VARIABLE");
2236+ if found or (LEN(v) # 0) then exit(2); end;
2237+ if setEnv("RAZ_SYS_TEST_EMPTY", "") # 0 then exit(2); end;
2238+ v, found := findEnv("RAZ_SYS_TEST_EMPTY");
2239+ if not found or (LEN(v) # 0) then exit(2); end;
2240+ v, found := findEnv("PATH");
2241+ if not found or (LEN(v) = 0) then exit(2); end;
2242+ if processors() < 1 then exit(2); end;
2243+end;
2244+
22022245 (* The module's test, as Linux's Sys has it: a file written, read back
22032246 and closed; a directory made, listed and emptied; no terminal; a
22042247 socket, sent to after its end; programs; time and the network. *)
@@ -2424,6 +2467,7 @@
24242467 testPrograms;
24252468 testShell;
24262469 testNet;
2470+ testProcess;
24272471 end;
24282472
24292473 end Sys.

test/run/ExitStatus.expect (history) added +2 -0

@@ -0,0 +1,2 @@
1+0 in main
2+ending with 7

test/run/ExitStatus.raz (history) added +20 -0

@@ -0,0 +1,20 @@
1+(* Rt.exitStatus is 0 in main and the status exit was given in the
2+ procedures atExit registered, also when a check or stop ends the
3+ program. *)
4+module ExitStatus;
5+import Out, Rt;
6+
7+proc report;
8+begin
9+ Out.str("ending with "); Out.int(Rt.exitStatus()); Out.str("\n");
10+end;
11+
12+proc main*: INT;
13+begin
14+ _ := Rt.atExit(report);
15+ Out.int(Rt.exitStatus()); Out.str(" in main\n");
16+ Rt.exit(7);
17+ return 0;
18+end;
19+
20+end ExitStatus.

test/run/ExitStatus.status (history) added +1 -0

@@ -0,0 +1 @@
1+7

test/run/StopStatus.expect (history) added +1 -0

@@ -0,0 +1 @@
1+ending with 2

test/run/StopStatus.raz (history) added +20 -0

@@ -0,0 +1,20 @@
1+(* A failed check ends the program with 2, which the procedures atExit
2+ registered can see. *)
3+module StopStatus;
4+import Out, Rt;
5+
6+proc report;
7+begin
8+ Out.str("ending with "); Out.int(Rt.exitStatus()); Out.str("\n");
9+end;
10+
11+proc main*: INT;
12+var a: [2]INT; i: INT;
13+begin
14+ _ := Rt.atExit(report);
15+ i := 5;
16+ a[i] := 1;
17+ return 0;
18+end;
19+
20+end StopStatus.

test/run/StopStatus.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/StopStatus.stderr (history) added +2 -0

@@ -0,0 +1,2 @@
1+test/run/StopStatus.raz:16:7: index 5 is out of range for a length of 2
2+ in StopStatus.main at test/run/StopStatus.raz:16:7: i = 5