From ae0cca005bb5a6f021ccc13f43d19ae21e73b114 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 09:53:42 +0100 Subject: [PATCH] Sys: what a shell still needs, for concha dupAbove, group, fork, handle and came, and wait on a group (-g), over Linux and over C. handle notes a signal and nothing else runs in the handler; with no SA_RESTART a system call it comes during is -4. Over C, ready and readyFor retry EINTR only for catch's signals. Files.load and Files.write try again after -4, as Out does. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 0301858..4351132 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -11,7 +11,7 @@ | 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`, `close`, `dup`, `dupTo`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; 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 | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; 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 | | `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 | @@ -212,6 +212,7 @@ proc close*(fd: INT); proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) +proc dupAbove*(fd, least: INT): INT; (* a new fd for the same file, the lowest free from least on, closed on exec *) proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *) proc isTerminal*(fd: INT): BOOL; proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) @@ -258,9 +259,11 @@ proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) proc spawn*(p: Program): INT; (* a program started: its process id *) proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) +proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) proc kill*(pid, signal: INT): INT; proc pid*: INT; (* this program's process id *) +proc group*: INT; (* the process group this program is in *) proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *) proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *) proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *) @@ -274,6 +277,8 @@ 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 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 *) proc clock*: I64; (* nanoseconds, on a clock that only goes forward *) proc now*: I64; (* nanoseconds since 1970-01-01, UTC *) @@ -400,6 +405,15 @@ `exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the same open file as `fd`, closing what `into` was, and `dup` gives a new fd for it, the lowest free, as a shell saves a file to give it back. +`dupAbove(fd, 10)` gives one at 10 or above, closed on exec, out of +the way of the fds a script names (`3>log`), as a shell keeps its own. +`fork` copies this program, which goes on from there in both: it is 0 +in the copy and the copy's process id in this one, as a shell runs a +builtin or a procedure as a stage of a pipe. Only the thread that +calls it is copied, so a program with threads mustn't; and what `Out` +keeps is copied too, to be written twice, so `Out.flush` comes first. +`group` is the process group this program is in: a shell starting on a +terminal waits until it is `foregroundOf(0)` before it takes it. `access` says whether this user, by its real ids, may read, write or execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` alone can't say; an empty `how` asks only whether it is there. @@ -420,8 +434,9 @@ one whose rights it has, 0 for root, as a prompt shows `#`; `hostName` is the machine's name. -`wait` waits for the program `pid` to change, or for any of this -program's if `pid` is -1, and says which one, and how: `exited`, with +`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 +`-g`, as a shell waits for a job's programs, and says which one, and how: `exited`, with its exit status; `killed`, with the number of the signal that ended it; or `stopped`, by Ctrl-Z or a signal, with the signal's number. Without `block` it doesn't wait, and is 0 and `running` if no program @@ -439,6 +454,16 @@ 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. + +`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 +key, sees Ctrl-C: the signal is noted, and the call it comes during, +`wait`, `read`, `ready` and the rest, fails with -4 (EINTR) rather +than going on; `sleep` sleeps its time all the same. `came(sig)` says whether it came since the last time it +was asked. Nothing else runs when the signal comes, so the program +does what it does after it, in its own time; `Out` and `Files` try +again after -4. `ignore(sig, false)` gives the signal back what it does +at first, and the signals `catch` refuses, `handle` refuses too. **Time.** `clock` is for measuring how long something takes: it never goes back, and says nothing of the date. `now` is the date and time, diff --git a/doc/report-history.md b/doc/report-history.md index 1d54673..186faac 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -172,6 +172,7 @@ what a shell needs: `dup`, `dupTo`, `keepOnExec`, `Program.more`, `exec`, `access` and `Access`'s `executing`, `umask`, `envAt`, `getMode` and `setMode`, `cpuTime`, `limit` and `setLimit`, `uid`, - `euid`, `hostName`, and `sigUsr1`, `sigUsr2` and `sigAlrm`; `Map` - gains `done`, `Str` gains `free`, and `Files` gains `load` (volume - 2). + `euid`, `hostName`, and `sigUsr1`, `sigUsr2` and `sigAlrm`, then + `dupAbove`, `group`, `fork`, `handle` and `came`, and `wait` on a + group; `Map` gains `done`, `Str` gains `free`, and `Files` gains + `load`, and tries again after EINTR (volume 2). diff --git a/lib/Files.raz b/lib/Files.raz index 7b53932..73e0d8f 100644 --- a/lib/Files.raz +++ b/lib/Files.raz @@ -25,7 +25,7 @@ repeat n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *) if n > 0 then INC(b^.len, n); end; - until n <= 0; + until (n <= 0) and (n # -4); (* -EINTR: a signal Sys.handle took; read again *) Sys.close(fd); return n; end; @@ -45,8 +45,9 @@ at := 0; while at < LEN(data) do n := Sys.write(fd, data[at..LEN(data)]); - if n <= 0 then Sys.close(fd); return false; end; - at := at + n; + if n > 0 then at := at + n; + elsif n # -4 then Sys.close(fd); return false; (* -EINTR: write again *) + end; end; Sys.close(fd); return true; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index cedfc49..380e7e4 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -122,6 +122,13 @@ fds: [16]I32; end; Timespec = record sec, nsec: I64; end; + (* struct sigaction, as rt_sigaction takes it *) + Action = record + handler: proc(sig: INT); + flags: U64; + restorer: proc; + mask: U64; + end; var cwd: [4096]U8; (* what workDir gives *) @@ -129,6 +136,7 @@ saved: Termios; (* the terminal as rawMode found it *) caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *) sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) + noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *) uts: [390]U8; (* struct utsname, which hostName gives the nodename of *) (* Writes s to the file descriptor fd; the number of bytes written, or @@ -269,6 +277,15 @@ return SYSCALL(33, fd, into); (* dup2 *) end; +(* A new file descriptor for the file fd is open on, the lowest free + from least on, and closed on exec: it, or minus an errno. A shell + keeps its own at 10 and above, out of the way of those a script + names. *) +proc dupAbove*(fd, least: INT): INT; +begin + return SYSCALL(72, fd, 1030, least); (* fcntl: F_DUPFD_CLOEXEC *) +end; + (* Whether fd stays open in the program exec runs in this one's place: yes, or no, closed then, as pipe's fds are at first. 0, or minus an errno. *) @@ -1194,6 +1211,12 @@ return SYSCALL(109, pid, group); (* setpgid *) end; +(* The process group this program is in. *) +proc group*: INT; +begin + return SYSCALL(111); (* getpgrp *) +end; + (* Gives the keys of the terminal on fd, and its signals, to the process group group. *) proc foreground*(fd, group: INT): INT; @@ -1456,8 +1479,8 @@ return r; end; -(* Waits for the program pid, or any of this program's if pid is -1, to - change, if block, and says which one changed and how: exited, with +(* Waits for the program pid, any of this program's if pid is -1, or + any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with its exit status; killed or stopped, with the signal's number. Without block it doesn't wait: 0 and running if none has changed. Minus an errno, and running, if there is none to wait for. *) @@ -1522,6 +1545,56 @@ return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) end; +(* Returns from a signal handler, as the kernel asks: rt_sigreturn, + which sa_restorer names. *) +proc sigReturn; +code + mov eax, 15; + syscall; +end sigReturn; + +(* The handler of the signals handle takes: it notes that sig came, and + does nothing else, since it runs between any two instructions of the + program, perhaps while Out or Heap holds its lock. *) +proc onNoted(sig: INT); +begin + _ := XCHG(ADR(noted[sig]), 1); +end; + +(* Takes the signal sig away from what it would do: it is noted, for + came to see, and a system call it comes during fails with -4 (EINTR) + rather than going on, so that a program waiting for another, or for + a key, sees Ctrl-C at once. 0, or minus an errno. ignore(sig, false) + gives it back what it does at first. *) +proc handle*(sig: INT): INT; +var act: Action; +begin + if (sig < 1) or (sig > 64) or (sig = sigKill) or (sig = sigStop) + or (sig = 8) or (sig = 11) then (* SIGFPE and SIGSEGV, Rt's *) + return -22; (* EINVAL *) + end; + act := {handler: onNoted, flags: 4000000H, restorer: sigReturn}; (* SA_RESTORER, and no SA_RESTART *) + return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) +end; + +(* Whether the signal sig, which handle took, came since the last time + it was asked. *) +proc came*(sig: INT): BOOL; +begin + if (sig < 1) or (sig > 64) then return false; end; + return XCHG(ADR(noted[sig]), 0) = 1; +end; + +(* A copy of this program, which goes on from here: 0 in the copy, its + process id in this one, or minus an errno. Only the thread that + calls it is copied, so a program with threads mustn't; and what Out + keeps is copied too, to be written twice, so the program flushes it + first. *) +proc fork*: INT; +begin + return SYSCALL(57); (* fork *) +end; + (* Gives the processor to another thread. *) proc yield*; begin @@ -1634,7 +1707,7 @@ are caught still. *) proc testShell; var - r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; + r, w, fd, n, k, child, c, err, pty, sigs, mask, parent: INT; t: I64; st: Status; buf: [64]U8; m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; begin r, w := pipe(); @@ -1709,6 +1782,30 @@ if err # -22 then exit(2); end; (* EINVAL *) e, err := hostName(); if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; + + (* a copy of an fd out of the way; this program's group; a copy of + this program, waited for by the group; a signal noted, and a wait + it breaks into *) + r, w := pipe(); + fd := dupAbove(w, 10); + if (fd < 10) or (write(fd, "d") # 1) or (read(r, buf) # 1) or (dupAbove(-1, 10) # -9) then exit(2); end; + close(fd); + if group() <= 0 then exit(2); end; + child := fork(); + if child = 0 then exit(7); end; + k, st, c := wait(-group(), true); + if (k # child) or (st # exited) or (c # 7) then exit(2); end; + if (handle(sigUsr2) # 0) or came(sigUsr2) or (handle(sigKill) # -22) then exit(2); end; + if (kill(pid(), sigUsr2) # 0) or not came(sigUsr2) or came(sigUsr2) then exit(2); end; + parent := pid(); + child := fork(); + if child = 0 then sleep(100); _ := kill(parent, sigUsr2); exit(0); end; + t := clock(); + if ready(r, 5000) or not came(sigUsr2) or (clock() - t > 4000000000) then exit(2); end; + k, st, c := wait(child, true); + if (k # child) or (ignore(sigUsr2, false) # 0) then exit(2); end; + close(r); + close(w); sigs := catch([sigUsr1]); while caught(sigs) # 0 do end; (* the sigChld of the programs above *) diff --git a/lib/Sys.raz b/lib/Sys.raz index 1c8645f..fef5641 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -7,8 +7,8 @@ only the two struct timeval that begin struct rusage are read. What is glibc's, on x86-64: where a name lies in struct dirent; struct stat; open's flags; the numbers of the requests TIOCGWINSZ, - TIOCSWINSZ and TIOCSCTTY; close_range; SOCK_CLOEXEC, AF_INET6 and - SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs + TIOCSWINSZ and TIOCSCTTY, and of F_DUPFD_CLOEXEC; close_range; + SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs start with a byte of length; the numbers of the signals and of the limits, and RLIM_INFINITY, all ones. A C library that differs needs a Sys of its own. *) @@ -157,6 +157,8 @@ saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) host: [256]U8; (* what hostName gives *) sigRead, sigWrite: I32; (* the pipe the handler of caught signals writes their numbers to, once open *) + noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *) + broke: INT; (* 1 when one has come since a call that ready or readyFor makes again began *) (* minus errno, as a system call returns it *) proc failed: INT; @@ -164,6 +166,15 @@ begin e := C.__errno_location(); return -INT(e^); +end; + +(* Whether a call that failed is to be made again: when it failed with + EINTR because a signal catch took broke into it, since on Linux those + break into nothing; not when one handle took did, which the program + is to see. broke is set to 0 before the call. *) +proc again: BOOL; +begin + return (failed() = -4) and (XCHG(ADR(broke), 0) = 0); end; (* Writes s to the file descriptor fd; the number of bytes written, or @@ -333,6 +344,18 @@ return INT(r); end; +(* A new file descriptor for the file fd is open on, the lowest free + from least on, and closed on exec: it, or minus an errno. A shell + keeps its own at 10 and above, out of the way of those a script + names. *) +proc dupAbove*(fd, least: INT): INT; +var r: I32; +begin + r := C.fcntl(I32(fd), 1030, I32(least)); (* F_DUPFD_CLOEXEC *) + if r < 0 then return failed(); end; + return INT(r); +end; + (* Whether fd stays open in the program exec runs in this one's place: yes, or no, closed then, as pipe's fds are at first. 0, or minus an errno. *) @@ -644,7 +667,8 @@ var p: Pollfd; r: I32; begin p := {fd: I32(fd), events: 1}; (* POLLIN *) - repeat r := C.poll(^U8(ADR(p)), 1, I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR: a signal caught *) + _ := XCHG(ADR(broke), 0); + repeat r := C.poll(^U8(ADR(p)), 1, I32(ms)); until (r >= 0) or not again(); return r > 0; end; @@ -836,7 +860,8 @@ INC(n); end; end; - repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *) + _ := XCHG(ADR(broke), 0); + repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or not again(); if r <= 0 then return -1; end; for i := 0 to n - 1 do if p[i].revents # 0 then return INT(p[i].fd); end; @@ -1183,6 +1208,12 @@ return 0; end; +(* The process group this program is in. *) +proc group*: INT; +begin + return INT(C.getpgrp()); +end; + (* Gives the keys of the terminal on fd, and its signals, to the process group group. *) proc foreground*(fd, group: INT): INT; @@ -1450,8 +1481,8 @@ return r; end; -(* Waits for the program pid, or any of this program's if pid is -1, to - change, if block, and says which one changed and how: exited, with +(* Waits for the program pid, any of this program's if pid is -1, or + any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with its exit status; killed or stopped, with the signal's number. Without block it doesn't wait: 0 and running if none has changed. Minus an errno, and running, if there is none to wait for. The status is read @@ -1537,6 +1568,53 @@ return C.isatty(I32(fd)) = 1; end; +(* The handler of the signals handle takes: it notes that sig came, and + does nothing else, since it runs between any two instructions of the + program, perhaps while Out or Heap holds its lock. *) +proc onNoted(sig: I32); +begin + _ := XCHG(ADR(noted[sig]), 1); + _ := XCHG(ADR(broke), 1); +end; + +(* Takes the signal sig away from what it would do: it is noted, for + came to see, and a system call it comes during fails with -4 (EINTR) + rather than going on, so that a program waiting for another, or for + a key, sees Ctrl-C at once. 0, or minus an errno. ignore(sig, false) + gives it back what it does at first. *) +proc handle*(sig: INT): INT; +var act: Sigaction; +begin + if (sig < 1) or (sig > 64) or (sig = sigKill) or (sig = sigStop) + or (sig = 8) or (sig = 11) then (* SIGFPE and SIGSEGV, Rt's *) + return -22; (* EINVAL *) + end; + act := {handler: onNoted}; (* no SA_RESTART *) + if C.sigaction(I32(sig), ^U8(ADR(act)), nil) # 0 then return failed(); end; + return 0; +end; + +(* Whether the signal sig, which handle took, came since the last time + it was asked. *) +proc came*(sig: INT): BOOL; +begin + if (sig < 1) or (sig > 64) then return false; end; + return XCHG(ADR(noted[sig]), 0) = 1; +end; + +(* A copy of this program, which goes on from here: 0 in the copy, its + process id in this one, or minus an errno. Only the thread that + calls it is copied, so a program with threads mustn't; and what Out + keeps is copied too, to be written twice, so the program flushes it + first. *) +proc fork*: INT; +var r: I32; +begin + r := C.fork(); + if r < 0 then return failed(); end; + return INT(r); +end; + (* Gives the processor to another thread. *) proc yield*; begin @@ -1649,7 +1727,7 @@ are caught still. *) proc testShell; var - r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; + r, w, fd, n, k, child, c, err, pty, sigs, mask, parent: INT; t: I64; st: Status; buf: [64]U8; m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; begin r, w := pipe(); @@ -1724,6 +1802,30 @@ if err # -22 then exit(2); end; (* EINVAL *) e, err := hostName(); if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; + + (* a copy of an fd out of the way; this program's group; a copy of + this program, waited for by the group; a signal noted, and a wait + it breaks into *) + r, w := pipe(); + fd := dupAbove(w, 10); + if (fd < 10) or (write(fd, "d") # 1) or (read(r, buf) # 1) or (dupAbove(-1, 10) # -9) then exit(2); end; + close(fd); + if group() <= 0 then exit(2); end; + child := fork(); + if child = 0 then exit(7); end; + k, st, c := wait(-group(), true); + if (k # child) or (st # exited) or (c # 7) then exit(2); end; + if (handle(sigUsr2) # 0) or came(sigUsr2) or (handle(sigKill) # -22) then exit(2); end; + if (kill(pid(), sigUsr2) # 0) or not came(sigUsr2) or came(sigUsr2) then exit(2); end; + parent := pid(); + child := fork(); + if child = 0 then sleep(100); _ := kill(parent, sigUsr2); exit(0); end; + t := clock(); + if ready(r, 5000) or not came(sigUsr2) or (clock() - t > 4000000000) then exit(2); end; + k, st, c := wait(child, true); + if (k # child) or (ignore(sigUsr2, false) # 0) then exit(2); end; + close(r); + close(w); sigs := catch([sigUsr1]); while caught(sigs) # 0 do end; (* the sigChld of the programs above *)