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.
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | | Module | What | |
| 12 | 12 | |---|---| |
| 13 | 13 | | `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 | |
| 14 | -| `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 | | |
| 14 | +| `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 | | |
| 15 | 15 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 16 | 16 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 17 | 17 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| @@ -212,6 +212,7 @@ | ||
| 212 | 212 | proc close*(fd: INT); |
| 213 | 213 | proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) |
| 214 | 214 | proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) |
| 215 | +proc dupAbove*(fd, least: INT): INT; (* a new fd for the same file, the lowest free from least on, closed on exec *) | |
| 215 | 216 | proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *) |
| 216 | 217 | proc isTerminal*(fd: INT): BOOL; |
| 217 | 218 | proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| @@ -258,9 +259,11 @@ | ||
| 258 | 259 | proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) |
| 259 | 260 | proc spawn*(p: Program): INT; (* a program started: its process id *) |
| 260 | 261 | proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) |
| 262 | +proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) | |
| 261 | 263 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) |
| 262 | 264 | proc kill*(pid, signal: INT): INT; |
| 263 | 265 | proc pid*: INT; (* this program's process id *) |
| 266 | +proc group*: INT; (* the process group this program is in *) | |
| 264 | 267 | proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *) |
| 265 | 268 | proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *) |
| 266 | 269 | proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *) |
| @@ -274,6 +277,8 @@ | ||
| 274 | 277 | proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *) |
| 275 | 278 | proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *) |
| 276 | 279 | proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *) |
| 280 | +proc handle*(sig: INT): INT; (* sig noted, for came; a system call it comes during is -4 *) | |
| 281 | +proc came*(sig: INT): BOOL; (* whether sig came since the last time it was asked *) | |
| 277 | 282 | |
| 278 | 283 | proc clock*: I64; (* nanoseconds, on a clock that only goes forward *) |
| 279 | 284 | proc now*: I64; (* nanoseconds since 1970-01-01, UTC *) |
| @@ -400,6 +405,15 @@ | ||
| 400 | 405 | `exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the |
| 401 | 406 | same open file as `fd`, closing what `into` was, and `dup` gives a new |
| 402 | 407 | fd for it, the lowest free, as a shell saves a file to give it back. |
| 408 | +`dupAbove(fd, 10)` gives one at 10 or above, closed on exec, out of | |
| 409 | +the way of the fds a script names (`3>log`), as a shell keeps its own. | |
| 410 | +`fork` copies this program, which goes on from there in both: it is 0 | |
| 411 | +in the copy and the copy's process id in this one, as a shell runs a | |
| 412 | +builtin or a procedure as a stage of a pipe. Only the thread that | |
| 413 | +calls it is copied, so a program with threads mustn't; and what `Out` | |
| 414 | +keeps is copied too, to be written twice, so `Out.flush` comes first. | |
| 415 | +`group` is the process group this program is in: a shell starting on a | |
| 416 | +terminal waits until it is `foregroundOf(0)` before it takes it. | |
| 403 | 417 | `access` says whether this user, by its real ids, may read, write or |
| 404 | 418 | execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` |
| 405 | 419 | alone can't say; an empty `how` asks only whether it is there. |
| @@ -420,8 +434,9 @@ | ||
| 420 | 434 | one whose rights it has, 0 for root, as a prompt shows `#`; `hostName` |
| 421 | 435 | is the machine's name. |
| 422 | 436 | |
| 423 | -`wait` waits for the program `pid` to change, or for any of this | |
| 424 | -program's if `pid` is -1, and says which one, and how: `exited`, with | |
| 437 | +`wait` waits for the program `pid` to change, for any of this | |
| 438 | +program's if `pid` is -1, or for any of the group `g` if `pid` is | |
| 439 | +`-g`, as a shell waits for a job's programs, and says which one, and how: `exited`, with | |
| 425 | 440 | its exit status; `killed`, with the number of the signal that ended |
| 426 | 441 | it; or `stopped`, by Ctrl-Z or a signal, with the signal's number. |
| 427 | 442 | Without `block` it doesn't wait, and is 0 and `running` if no program |
| @@ -439,6 +454,16 @@ | ||
| 439 | 454 | makes a signal do nothing, as a shell ignores `sigTtou` so that it can |
| 440 | 455 | take its terminal back, and `ignore(sig, false)` gives it back what it |
| 441 | 456 | does at first. A program that `spawn` starts begins with neither. |
| 457 | + | |
| 458 | +`handle` is for a program that must see a signal while it waits in a | |
| 459 | +system call, as a shell waiting for its foreground program, or for a | |
| 460 | +key, sees Ctrl-C: the signal is noted, and the call it comes during, | |
| 461 | +`wait`, `read`, `ready` and the rest, fails with -4 (EINTR) rather | |
| 462 | +than going on; `sleep` sleeps its time all the same. `came(sig)` says whether it came since the last time it | |
| 463 | +was asked. Nothing else runs when the signal comes, so the program | |
| 464 | +does what it does after it, in its own time; `Out` and `Files` try | |
| 465 | +again after -4. `ignore(sig, false)` gives the signal back what it does | |
| 466 | +at first, and the signals `catch` refuses, `handle` refuses too. | |
| 442 | 467 | |
| 443 | 468 | **Time.** `clock` is for measuring how long something takes: it never |
| 444 | 469 | goes back, and says nothing of the date. `now` is the date and time, |
| @@ -172,6 +172,7 @@ | ||
| 172 | 172 | what a shell needs: `dup`, `dupTo`, `keepOnExec`, `Program.more`, |
| 173 | 173 | `exec`, `access` and `Access`'s `executing`, `umask`, `envAt`, |
| 174 | 174 | `getMode` and `setMode`, `cpuTime`, `limit` and `setLimit`, `uid`, |
| 175 | - `euid`, `hostName`, and `sigUsr1`, `sigUsr2` and `sigAlrm`; `Map` | |
| 176 | - gains `done`, `Str` gains `free`, and `Files` gains `load` (volume | |
| 177 | - 2). | |
| 175 | + `euid`, `hostName`, and `sigUsr1`, `sigUsr2` and `sigAlrm`, then | |
| 176 | + `dupAbove`, `group`, `fork`, `handle` and `came`, and `wait` on a | |
| 177 | + group; `Map` gains `done`, `Str` gains `free`, and `Files` gains | |
| 178 | + `load`, and tries again after EINTR (volume 2). |
| @@ -25,7 +25,7 @@ | ||
| 25 | 25 | repeat |
| 26 | 26 | n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *) |
| 27 | 27 | if n > 0 then INC(b^.len, n); end; |
| 28 | - until n <= 0; | |
| 28 | + until (n <= 0) and (n # -4); (* -EINTR: a signal Sys.handle took; read again *) | |
| 29 | 29 | Sys.close(fd); |
| 30 | 30 | return n; |
| 31 | 31 | end; |
| @@ -45,8 +45,9 @@ | ||
| 45 | 45 | at := 0; |
| 46 | 46 | while at < LEN(data) do |
| 47 | 47 | n := Sys.write(fd, data[at..LEN(data)]); |
| 48 | - if n <= 0 then Sys.close(fd); return false; end; | |
| 49 | - at := at + n; | |
| 48 | + if n > 0 then at := at + n; | |
| 49 | + elsif n # -4 then Sys.close(fd); return false; (* -EINTR: write again *) | |
| 50 | + end; | |
| 50 | 51 | end; |
| 51 | 52 | Sys.close(fd); |
| 52 | 53 | return true; |
| @@ -122,6 +122,13 @@ | ||
| 122 | 122 | fds: [16]I32; |
| 123 | 123 | end; |
| 124 | 124 | Timespec = record sec, nsec: I64; end; |
| 125 | + (* struct sigaction, as rt_sigaction takes it *) | |
| 126 | + Action = record | |
| 127 | + handler: proc(sig: INT); | |
| 128 | + flags: U64; | |
| 129 | + restorer: proc; | |
| 130 | + mask: U64; | |
| 131 | + end; | |
| 125 | 132 | |
| 126 | 133 | var |
| 127 | 134 | cwd: [4096]U8; (* what workDir gives *) |
| @@ -129,6 +136,7 @@ | ||
| 129 | 136 | saved: Termios; (* the terminal as rawMode found it *) |
| 130 | 137 | caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *) |
| 131 | 138 | sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) |
| 139 | + noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *) | |
| 132 | 140 | uts: [390]U8; (* struct utsname, which hostName gives the nodename of *) |
| 133 | 141 | |
| 134 | 142 | (* Writes s to the file descriptor fd; the number of bytes written, or |
| @@ -269,6 +277,15 @@ | ||
| 269 | 277 | return SYSCALL(33, fd, into); (* dup2 *) |
| 270 | 278 | end; |
| 271 | 279 | |
| 280 | +(* A new file descriptor for the file fd is open on, the lowest free | |
| 281 | + from least on, and closed on exec: it, or minus an errno. A shell | |
| 282 | + keeps its own at 10 and above, out of the way of those a script | |
| 283 | + names. *) | |
| 284 | +proc dupAbove*(fd, least: INT): INT; | |
| 285 | +begin | |
| 286 | + return SYSCALL(72, fd, 1030, least); (* fcntl: F_DUPFD_CLOEXEC *) | |
| 287 | +end; | |
| 288 | + | |
| 272 | 289 | (* Whether fd stays open in the program exec runs in this one's place: |
| 273 | 290 | yes, or no, closed then, as pipe's fds are at first. 0, or minus an |
| 274 | 291 | errno. *) |
| @@ -1194,6 +1211,12 @@ | ||
| 1194 | 1211 | return SYSCALL(109, pid, group); (* setpgid *) |
| 1195 | 1212 | end; |
| 1196 | 1213 | |
| 1214 | +(* The process group this program is in. *) | |
| 1215 | +proc group*: INT; | |
| 1216 | +begin | |
| 1217 | + return SYSCALL(111); (* getpgrp *) | |
| 1218 | +end; | |
| 1219 | + | |
| 1197 | 1220 | (* Gives the keys of the terminal on fd, and its signals, to the process |
| 1198 | 1221 | group group. *) |
| 1199 | 1222 | proc foreground*(fd, group: INT): INT; |
| @@ -1456,8 +1479,8 @@ | ||
| 1456 | 1479 | return r; |
| 1457 | 1480 | end; |
| 1458 | 1481 | |
| 1459 | -(* Waits for the program pid, or any of this program's if pid is -1, to | |
| 1460 | - change, if block, and says which one changed and how: exited, with | |
| 1482 | +(* Waits for the program pid, any of this program's if pid is -1, or | |
| 1483 | + any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with | |
| 1461 | 1484 | its exit status; killed or stopped, with the signal's number. Without |
| 1462 | 1485 | block it doesn't wait: 0 and running if none has changed. Minus an |
| 1463 | 1486 | errno, and running, if there is none to wait for. *) |
| @@ -1522,6 +1545,56 @@ | ||
| 1522 | 1545 | return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) |
| 1523 | 1546 | end; |
| 1524 | 1547 | |
| 1548 | +(* Returns from a signal handler, as the kernel asks: rt_sigreturn, | |
| 1549 | + which sa_restorer names. *) | |
| 1550 | +proc sigReturn; | |
| 1551 | +code | |
| 1552 | + mov eax, 15; | |
| 1553 | + syscall; | |
| 1554 | +end sigReturn; | |
| 1555 | + | |
| 1556 | +(* The handler of the signals handle takes: it notes that sig came, and | |
| 1557 | + does nothing else, since it runs between any two instructions of the | |
| 1558 | + program, perhaps while Out or Heap holds its lock. *) | |
| 1559 | +proc onNoted(sig: INT); | |
| 1560 | +begin | |
| 1561 | + _ := XCHG(ADR(noted[sig]), 1); | |
| 1562 | +end; | |
| 1563 | + | |
| 1564 | +(* Takes the signal sig away from what it would do: it is noted, for | |
| 1565 | + came to see, and a system call it comes during fails with -4 (EINTR) | |
| 1566 | + rather than going on, so that a program waiting for another, or for | |
| 1567 | + a key, sees Ctrl-C at once. 0, or minus an errno. ignore(sig, false) | |
| 1568 | + gives it back what it does at first. *) | |
| 1569 | +proc handle*(sig: INT): INT; | |
| 1570 | +var act: Action; | |
| 1571 | +begin | |
| 1572 | + if (sig < 1) or (sig > 64) or (sig = sigKill) or (sig = sigStop) | |
| 1573 | + or (sig = 8) or (sig = 11) then (* SIGFPE and SIGSEGV, Rt's *) | |
| 1574 | + return -22; (* EINVAL *) | |
| 1575 | + end; | |
| 1576 | + act := {handler: onNoted, flags: 4000000H, restorer: sigReturn}; (* SA_RESTORER, and no SA_RESTART *) | |
| 1577 | + return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) | |
| 1578 | +end; | |
| 1579 | + | |
| 1580 | +(* Whether the signal sig, which handle took, came since the last time | |
| 1581 | + it was asked. *) | |
| 1582 | +proc came*(sig: INT): BOOL; | |
| 1583 | +begin | |
| 1584 | + if (sig < 1) or (sig > 64) then return false; end; | |
| 1585 | + return XCHG(ADR(noted[sig]), 0) = 1; | |
| 1586 | +end; | |
| 1587 | + | |
| 1588 | +(* A copy of this program, which goes on from here: 0 in the copy, its | |
| 1589 | + process id in this one, or minus an errno. Only the thread that | |
| 1590 | + calls it is copied, so a program with threads mustn't; and what Out | |
| 1591 | + keeps is copied too, to be written twice, so the program flushes it | |
| 1592 | + first. *) | |
| 1593 | +proc fork*: INT; | |
| 1594 | +begin | |
| 1595 | + return SYSCALL(57); (* fork *) | |
| 1596 | +end; | |
| 1597 | + | |
| 1525 | 1598 | (* Gives the processor to another thread. *) |
| 1526 | 1599 | proc yield*; |
| 1527 | 1600 | begin |
| @@ -1634,7 +1707,7 @@ | ||
| 1634 | 1707 | are caught still. *) |
| 1635 | 1708 | proc testShell; |
| 1636 | 1709 | var |
| 1637 | - r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; | |
| 1710 | + r, w, fd, n, k, child, c, err, pty, sigs, mask, parent: INT; t: I64; st: Status; buf: [64]U8; | |
| 1638 | 1711 | m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; |
| 1639 | 1712 | begin |
| 1640 | 1713 | r, w := pipe(); |
| @@ -1709,6 +1782,30 @@ | ||
| 1709 | 1782 | if err # -22 then exit(2); end; (* EINVAL *) |
| 1710 | 1783 | e, err := hostName(); |
| 1711 | 1784 | if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; |
| 1785 | + | |
| 1786 | + (* a copy of an fd out of the way; this program's group; a copy of | |
| 1787 | + this program, waited for by the group; a signal noted, and a wait | |
| 1788 | + it breaks into *) | |
| 1789 | + r, w := pipe(); | |
| 1790 | + fd := dupAbove(w, 10); | |
| 1791 | + if (fd < 10) or (write(fd, "d") # 1) or (read(r, buf) # 1) or (dupAbove(-1, 10) # -9) then exit(2); end; | |
| 1792 | + close(fd); | |
| 1793 | + if group() <= 0 then exit(2); end; | |
| 1794 | + child := fork(); | |
| 1795 | + if child = 0 then exit(7); end; | |
| 1796 | + k, st, c := wait(-group(), true); | |
| 1797 | + if (k # child) or (st # exited) or (c # 7) then exit(2); end; | |
| 1798 | + if (handle(sigUsr2) # 0) or came(sigUsr2) or (handle(sigKill) # -22) then exit(2); end; | |
| 1799 | + if (kill(pid(), sigUsr2) # 0) or not came(sigUsr2) or came(sigUsr2) then exit(2); end; | |
| 1800 | + parent := pid(); | |
| 1801 | + child := fork(); | |
| 1802 | + if child = 0 then sleep(100); _ := kill(parent, sigUsr2); exit(0); end; | |
| 1803 | + t := clock(); | |
| 1804 | + if ready(r, 5000) or not came(sigUsr2) or (clock() - t > 4000000000) then exit(2); end; | |
| 1805 | + k, st, c := wait(child, true); | |
| 1806 | + if (k # child) or (ignore(sigUsr2, false) # 0) then exit(2); end; | |
| 1807 | + close(r); | |
| 1808 | + close(w); | |
| 1712 | 1809 | |
| 1713 | 1810 | sigs := catch([sigUsr1]); |
| 1714 | 1811 | while caught(sigs) # 0 do end; (* the sigChld of the programs above *) |
| @@ -7,8 +7,8 @@ | ||
| 7 | 7 | only the two struct timeval that begin struct rusage are read. What |
| 8 | 8 | is glibc's, on x86-64: where a name lies in struct dirent; struct |
| 9 | 9 | stat; open's flags; the numbers of the requests TIOCGWINSZ, |
| 10 | - TIOCSWINSZ and TIOCSCTTY; close_range; SOCK_CLOEXEC, AF_INET6 and | |
| 11 | - SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs | |
| 10 | + TIOCSWINSZ and TIOCSCTTY, and of F_DUPFD_CLOEXEC; close_range; | |
| 11 | + SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; struct sockaddr_un and sockaddr_in, which on the BSDs | |
| 12 | 12 | start with a byte of length; the numbers of the signals and of the |
| 13 | 13 | limits, and RLIM_INFINITY, all ones. A C library that differs needs |
| 14 | 14 | a Sys of its own. *) |
| @@ -157,6 +157,8 @@ | ||
| 157 | 157 | saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) |
| 158 | 158 | host: [256]U8; (* what hostName gives *) |
| 159 | 159 | sigRead, sigWrite: I32; (* the pipe the handler of caught signals writes their numbers to, once open *) |
| 160 | + noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *) | |
| 161 | + broke: INT; (* 1 when one has come since a call that ready or readyFor makes again began *) | |
| 160 | 162 | |
| 161 | 163 | (* minus errno, as a system call returns it *) |
| 162 | 164 | proc failed: INT; |
| @@ -164,6 +166,15 @@ | ||
| 164 | 166 | begin |
| 165 | 167 | e := C.__errno_location(); |
| 166 | 168 | return -INT(e^); |
| 169 | +end; | |
| 170 | + | |
| 171 | +(* Whether a call that failed is to be made again: when it failed with | |
| 172 | + EINTR because a signal catch took broke into it, since on Linux those | |
| 173 | + break into nothing; not when one handle took did, which the program | |
| 174 | + is to see. broke is set to 0 before the call. *) | |
| 175 | +proc again: BOOL; | |
| 176 | +begin | |
| 177 | + return (failed() = -4) and (XCHG(ADR(broke), 0) = 0); | |
| 167 | 178 | end; |
| 168 | 179 | |
| 169 | 180 | (* Writes s to the file descriptor fd; the number of bytes written, or |
| @@ -333,6 +344,18 @@ | ||
| 333 | 344 | return INT(r); |
| 334 | 345 | end; |
| 335 | 346 | |
| 347 | +(* A new file descriptor for the file fd is open on, the lowest free | |
| 348 | + from least on, and closed on exec: it, or minus an errno. A shell | |
| 349 | + keeps its own at 10 and above, out of the way of those a script | |
| 350 | + names. *) | |
| 351 | +proc dupAbove*(fd, least: INT): INT; | |
| 352 | +var r: I32; | |
| 353 | +begin | |
| 354 | + r := C.fcntl(I32(fd), 1030, I32(least)); (* F_DUPFD_CLOEXEC *) | |
| 355 | + if r < 0 then return failed(); end; | |
| 356 | + return INT(r); | |
| 357 | +end; | |
| 358 | + | |
| 336 | 359 | (* Whether fd stays open in the program exec runs in this one's place: |
| 337 | 360 | yes, or no, closed then, as pipe's fds are at first. 0, or minus an |
| 338 | 361 | errno. *) |
| @@ -644,7 +667,8 @@ | ||
| 644 | 667 | var p: Pollfd; r: I32; |
| 645 | 668 | begin |
| 646 | 669 | p := {fd: I32(fd), events: 1}; (* POLLIN *) |
| 647 | - repeat r := C.poll(^U8(ADR(p)), 1, I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR: a signal caught *) | |
| 670 | + _ := XCHG(ADR(broke), 0); | |
| 671 | + repeat r := C.poll(^U8(ADR(p)), 1, I32(ms)); until (r >= 0) or not again(); | |
| 648 | 672 | return r > 0; |
| 649 | 673 | end; |
| 650 | 674 | |
| @@ -836,7 +860,8 @@ | ||
| 836 | 860 | INC(n); |
| 837 | 861 | end; |
| 838 | 862 | end; |
| 839 | - repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *) | |
| 863 | + _ := XCHG(ADR(broke), 0); | |
| 864 | + repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or not again(); | |
| 840 | 865 | if r <= 0 then return -1; end; |
| 841 | 866 | for i := 0 to n - 1 do |
| 842 | 867 | if p[i].revents # 0 then return INT(p[i].fd); end; |
| @@ -1183,6 +1208,12 @@ | ||
| 1183 | 1208 | return 0; |
| 1184 | 1209 | end; |
| 1185 | 1210 | |
| 1211 | +(* The process group this program is in. *) | |
| 1212 | +proc group*: INT; | |
| 1213 | +begin | |
| 1214 | + return INT(C.getpgrp()); | |
| 1215 | +end; | |
| 1216 | + | |
| 1186 | 1217 | (* Gives the keys of the terminal on fd, and its signals, to the process |
| 1187 | 1218 | group group. *) |
| 1188 | 1219 | proc foreground*(fd, group: INT): INT; |
| @@ -1450,8 +1481,8 @@ | ||
| 1450 | 1481 | return r; |
| 1451 | 1482 | end; |
| 1452 | 1483 | |
| 1453 | -(* Waits for the program pid, or any of this program's if pid is -1, to | |
| 1454 | - change, if block, and says which one changed and how: exited, with | |
| 1484 | +(* Waits for the program pid, any of this program's if pid is -1, or | |
| 1485 | + any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with | |
| 1455 | 1486 | its exit status; killed or stopped, with the signal's number. Without |
| 1456 | 1487 | block it doesn't wait: 0 and running if none has changed. Minus an |
| 1457 | 1488 | errno, and running, if there is none to wait for. The status is read |
| @@ -1537,6 +1568,53 @@ | ||
| 1537 | 1568 | return C.isatty(I32(fd)) = 1; |
| 1538 | 1569 | end; |
| 1539 | 1570 | |
| 1571 | +(* The handler of the signals handle takes: it notes that sig came, and | |
| 1572 | + does nothing else, since it runs between any two instructions of the | |
| 1573 | + program, perhaps while Out or Heap holds its lock. *) | |
| 1574 | +proc onNoted(sig: I32); | |
| 1575 | +begin | |
| 1576 | + _ := XCHG(ADR(noted[sig]), 1); | |
| 1577 | + _ := XCHG(ADR(broke), 1); | |
| 1578 | +end; | |
| 1579 | + | |
| 1580 | +(* Takes the signal sig away from what it would do: it is noted, for | |
| 1581 | + came to see, and a system call it comes during fails with -4 (EINTR) | |
| 1582 | + rather than going on, so that a program waiting for another, or for | |
| 1583 | + a key, sees Ctrl-C at once. 0, or minus an errno. ignore(sig, false) | |
| 1584 | + gives it back what it does at first. *) | |
| 1585 | +proc handle*(sig: INT): INT; | |
| 1586 | +var act: Sigaction; | |
| 1587 | +begin | |
| 1588 | + if (sig < 1) or (sig > 64) or (sig = sigKill) or (sig = sigStop) | |
| 1589 | + or (sig = 8) or (sig = 11) then (* SIGFPE and SIGSEGV, Rt's *) | |
| 1590 | + return -22; (* EINVAL *) | |
| 1591 | + end; | |
| 1592 | + act := {handler: onNoted}; (* no SA_RESTART *) | |
| 1593 | + if C.sigaction(I32(sig), ^U8(ADR(act)), nil) # 0 then return failed(); end; | |
| 1594 | + return 0; | |
| 1595 | +end; | |
| 1596 | + | |
| 1597 | +(* Whether the signal sig, which handle took, came since the last time | |
| 1598 | + it was asked. *) | |
| 1599 | +proc came*(sig: INT): BOOL; | |
| 1600 | +begin | |
| 1601 | + if (sig < 1) or (sig > 64) then return false; end; | |
| 1602 | + return XCHG(ADR(noted[sig]), 0) = 1; | |
| 1603 | +end; | |
| 1604 | + | |
| 1605 | +(* A copy of this program, which goes on from here: 0 in the copy, its | |
| 1606 | + process id in this one, or minus an errno. Only the thread that | |
| 1607 | + calls it is copied, so a program with threads mustn't; and what Out | |
| 1608 | + keeps is copied too, to be written twice, so the program flushes it | |
| 1609 | + first. *) | |
| 1610 | +proc fork*: INT; | |
| 1611 | +var r: I32; | |
| 1612 | +begin | |
| 1613 | + r := C.fork(); | |
| 1614 | + if r < 0 then return failed(); end; | |
| 1615 | + return INT(r); | |
| 1616 | +end; | |
| 1617 | + | |
| 1540 | 1618 | (* Gives the processor to another thread. *) |
| 1541 | 1619 | proc yield*; |
| 1542 | 1620 | begin |
| @@ -1649,7 +1727,7 @@ | ||
| 1649 | 1727 | are caught still. *) |
| 1650 | 1728 | proc testShell; |
| 1651 | 1729 | var |
| 1652 | - r, w, fd, n, k, child, c, err, pty, sigs, mask: INT; st: Status; buf: [64]U8; | |
| 1730 | + r, w, fd, n, k, child, c, err, pty, sigs, mask, parent: INT; t: I64; st: Status; buf: [64]U8; | |
| 1653 | 1731 | m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL; |
| 1654 | 1732 | begin |
| 1655 | 1733 | r, w := pipe(); |
| @@ -1724,6 +1802,30 @@ | ||
| 1724 | 1802 | if err # -22 then exit(2); end; (* EINVAL *) |
| 1725 | 1803 | e, err := hostName(); |
| 1726 | 1804 | if (err # 0) or (LEN(e) = 0) or (uid() < 0) or (euid() < 0) then exit(2); end; |
| 1805 | + | |
| 1806 | + (* a copy of an fd out of the way; this program's group; a copy of | |
| 1807 | + this program, waited for by the group; a signal noted, and a wait | |
| 1808 | + it breaks into *) | |
| 1809 | + r, w := pipe(); | |
| 1810 | + fd := dupAbove(w, 10); | |
| 1811 | + if (fd < 10) or (write(fd, "d") # 1) or (read(r, buf) # 1) or (dupAbove(-1, 10) # -9) then exit(2); end; | |
| 1812 | + close(fd); | |
| 1813 | + if group() <= 0 then exit(2); end; | |
| 1814 | + child := fork(); | |
| 1815 | + if child = 0 then exit(7); end; | |
| 1816 | + k, st, c := wait(-group(), true); | |
| 1817 | + if (k # child) or (st # exited) or (c # 7) then exit(2); end; | |
| 1818 | + if (handle(sigUsr2) # 0) or came(sigUsr2) or (handle(sigKill) # -22) then exit(2); end; | |
| 1819 | + if (kill(pid(), sigUsr2) # 0) or not came(sigUsr2) or came(sigUsr2) then exit(2); end; | |
| 1820 | + parent := pid(); | |
| 1821 | + child := fork(); | |
| 1822 | + if child = 0 then sleep(100); _ := kill(parent, sigUsr2); exit(0); end; | |
| 1823 | + t := clock(); | |
| 1824 | + if ready(r, 5000) or not came(sigUsr2) or (clock() - t > 4000000000) then exit(2); end; | |
| 1825 | + k, st, c := wait(child, true); | |
| 1826 | + if (k # child) or (ignore(sigUsr2, false) # 0) then exit(2); end; | |
| 1827 | + close(r); | |
| 1828 | + close(w); | |
| 1727 | 1829 | |
| 1728 | 1830 | sigs := catch([sigUsr1]); |
| 1729 | 1831 | while caught(sigs) # 0 do end; (* the sigChld of the programs above *) |