raz / commit ae0cca00

commit
ae0cca005bb5a6f021ccc13f43d19ae21e73b114
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
451f1b61
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.

patch · log

5 files changed, +245 -19

doc/raz-lib.md (history) modified +28 -3

@@ -11,7 +11,7 @@
1111 | Module | What |
1212 |---|---|
1313 | `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 |
1515 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
1616 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
1717 | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike |
@@ -212,6 +212,7 @@
212212 proc close*(fd: INT);
213213 proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *)
214214 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 *)
215216 proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *)
216217 proc isTerminal*(fd: INT): BOOL;
217218 proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *)
@@ -258,9 +259,11 @@
258259 proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *)
259260 proc spawn*(p: Program): INT; (* a program started: its process id *)
260261 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 *)
261263 proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *)
262264 proc kill*(pid, signal: INT): INT;
263265 proc pid*: INT; (* this program's process id *)
266+proc group*: INT; (* the process group this program is in *)
264267 proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *)
265268 proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *)
266269 proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *)
@@ -274,6 +277,8 @@
274277 proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *)
275278 proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *)
276279 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 *)
277282
278283 proc clock*: I64; (* nanoseconds, on a clock that only goes forward *)
279284 proc now*: I64; (* nanoseconds since 1970-01-01, UTC *)
@@ -400,6 +405,15 @@
400405 `exec >log` and around a builtin: `dupTo(fd, into)` makes `into` the
401406 same open file as `fd`, closing what `into` was, and `dup` gives a new
402407 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.
403417 `access` says whether this user, by its real ids, may read, write or
404418 execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode`
405419 alone can't say; an empty `how` asks only whether it is there.
@@ -420,8 +434,9 @@
420434 one whose rights it has, 0 for root, as a prompt shows `#`; `hostName`
421435 is the machine's name.
422436
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
425440 its exit status; `killed`, with the number of the signal that ended
426441 it; or `stopped`, by Ctrl-Z or a signal, with the signal's number.
427442 Without `block` it doesn't wait, and is 0 and `running` if no program
@@ -439,6 +454,16 @@
439454 makes a signal do nothing, as a shell ignores `sigTtou` so that it can
440455 take its terminal back, and `ignore(sig, false)` gives it back what it
441456 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.
442467
443468 **Time.** `clock` is for measuring how long something takes: it never
444469 goes back, and says nothing of the date. `now` is the date and time,

doc/report-history.md (history) modified +4 -3

@@ -172,6 +172,7 @@
172172 what a shell needs: `dup`, `dupTo`, `keepOnExec`, `Program.more`,
173173 `exec`, `access` and `Access`'s `executing`, `umask`, `envAt`,
174174 `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).

lib/Files.raz (history) modified +4 -3

@@ -25,7 +25,7 @@
2525 repeat
2626 n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *)
2727 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 *)
2929 Sys.close(fd);
3030 return n;
3131 end;
@@ -45,8 +45,9 @@
4545 at := 0;
4646 while at < LEN(data) do
4747 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;
5051 end;
5152 Sys.close(fd);
5253 return true;

lib/Sys.linux-x86_64.raz (history) modified +100 -3

@@ -122,6 +122,13 @@
122122 fds: [16]I32;
123123 end;
124124 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;
125132
126133 var
127134 cwd: [4096]U8; (* what workDir gives *)
@@ -129,6 +136,7 @@
129136 saved: Termios; (* the terminal as rawMode found it *)
130137 caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *)
131138 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 *)
132140 uts: [390]U8; (* struct utsname, which hostName gives the nodename of *)
133141
134142 (* Writes s to the file descriptor fd; the number of bytes written, or
@@ -269,6 +277,15 @@
269277 return SYSCALL(33, fd, into); (* dup2 *)
270278 end;
271279
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+
272289 (* Whether fd stays open in the program exec runs in this one's place:
273290 yes, or no, closed then, as pipe's fds are at first. 0, or minus an
274291 errno. *)
@@ -1194,6 +1211,12 @@
11941211 return SYSCALL(109, pid, group); (* setpgid *)
11951212 end;
11961213
1214+(* The process group this program is in. *)
1215+proc group*: INT;
1216+begin
1217+ return SYSCALL(111); (* getpgrp *)
1218+end;
1219+
11971220 (* Gives the keys of the terminal on fd, and its signals, to the process
11981221 group group. *)
11991222 proc foreground*(fd, group: INT): INT;
@@ -1456,8 +1479,8 @@
14561479 return r;
14571480 end;
14581481
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
14611484 its exit status; killed or stopped, with the signal's number. Without
14621485 block it doesn't wait: 0 and running if none has changed. Minus an
14631486 errno, and running, if there is none to wait for. *)
@@ -1522,6 +1545,56 @@
15221545 return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *)
15231546 end;
15241547
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+
15251598 (* Gives the processor to another thread. *)
15261599 proc yield*;
15271600 begin
@@ -1634,7 +1707,7 @@
16341707 are caught still. *)
16351708 proc testShell;
16361709 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;
16381711 m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL;
16391712 begin
16401713 r, w := pipe();
@@ -1709,6 +1782,30 @@
17091782 if err # -22 then exit(2); end; (* EINVAL *)
17101783 e, err := hostName();
17111784 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);
17121809
17131810 sigs := catch([sigUsr1]);
17141811 while caught(sigs) # 0 do end; (* the sigChld of the programs above *)

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

@@ -7,8 +7,8 @@
77 only the two struct timeval that begin struct rusage are read. What
88 is glibc's, on x86-64: where a name lies in struct dirent; struct
99 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
1212 start with a byte of length; the numbers of the signals and of the
1313 limits, and RLIM_INFINITY, all ones. A C library that differs needs
1414 a Sys of its own. *)
@@ -157,6 +157,8 @@
157157 saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *)
158158 host: [256]U8; (* what hostName gives *)
159159 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 *)
160162
161163 (* minus errno, as a system call returns it *)
162164 proc failed: INT;
@@ -164,6 +166,15 @@
164166 begin
165167 e := C.__errno_location();
166168 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);
167178 end;
168179
169180 (* Writes s to the file descriptor fd; the number of bytes written, or
@@ -333,6 +344,18 @@
333344 return INT(r);
334345 end;
335346
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+
336359 (* Whether fd stays open in the program exec runs in this one's place:
337360 yes, or no, closed then, as pipe's fds are at first. 0, or minus an
338361 errno. *)
@@ -644,7 +667,8 @@
644667 var p: Pollfd; r: I32;
645668 begin
646669 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();
648672 return r > 0;
649673 end;
650674
@@ -836,7 +860,8 @@
836860 INC(n);
837861 end;
838862 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();
840865 if r <= 0 then return -1; end;
841866 for i := 0 to n - 1 do
842867 if p[i].revents # 0 then return INT(p[i].fd); end;
@@ -1183,6 +1208,12 @@
11831208 return 0;
11841209 end;
11851210
1211+(* The process group this program is in. *)
1212+proc group*: INT;
1213+begin
1214+ return INT(C.getpgrp());
1215+end;
1216+
11861217 (* Gives the keys of the terminal on fd, and its signals, to the process
11871218 group group. *)
11881219 proc foreground*(fd, group: INT): INT;
@@ -1450,8 +1481,8 @@
14501481 return r;
14511482 end;
14521483
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
14551486 its exit status; killed or stopped, with the signal's number. Without
14561487 block it doesn't wait: 0 and running if none has changed. Minus an
14571488 errno, and running, if there is none to wait for. The status is read
@@ -1537,6 +1568,53 @@
15371568 return C.isatty(I32(fd)) = 1;
15381569 end;
15391570
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+
15401618 (* Gives the processor to another thread. *)
15411619 proc yield*;
15421620 begin
@@ -1649,7 +1727,7 @@
16491727 are caught still. *)
16501728 proc testShell;
16511729 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;
16531731 m, raw: Mode; soft, hard, s2, h2, user, system: I64; e: []U8; ok, path: BOOL;
16541732 begin
16551733 r, w := pipe();
@@ -1724,6 +1802,30 @@
17241802 if err # -22 then exit(2); end; (* EINVAL *)
17251803 e, err := hostName();
17261804 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);
17271829
17281830 sigs := catch([sigUsr1]);
17291831 while caught(sigs) # 0 do end; (* the sigChld of the programs above *)