raz / commit 62fab28b

commit
62fab28b4e632802e0a99458bda442d427a14f73
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
55f203f3
Sys: spawn finds its program first, keepIgnored, fds with tty, waitExit

patch · log

3 files changed, +190 -51

doc/raz-lib.md (history) modified +18 -5

@@ -229,6 +229,7 @@
229229 foreground*: BOOL; (* its group takes the terminal on standard input *)
230230 tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *)
231231 more*: []INT; (* fds above 2 passed on to it, at the same numbers *)
232+ keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *)
232233 end;
233234 Status* = (running, exited, killed, stopped);
234235 Mode* = record ... end; (* a terminal's modes; its fields are the target's *)
@@ -306,6 +307,7 @@
306307 proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *)
307308 proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *)
308309 proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *)
310+proc waitExit*(pid: INT): (INT, Status, INT); (* as wait, but only for a program to end *)
309311 proc kill*(pid, signal: INT): INT;
310312 proc pid*: INT; (* this program's process id *)
311313 proc group*: INT; (* the process group this program is in *)
@@ -438,7 +440,12 @@
438440 looked for as a shell looks for a command, and as C's `execvp` does:
439441 in each directory of this program's `PATH` in turn, an empty one being
440442 the working directory, or of `/bin:/usr/bin` if `PATH` is unset or
441-empty; the first that runs is the program. Its environment is this
443+empty; the first that runs is the program. If there is none, `spawn`
444+says so, -2 (ENOENT) or -13 (EACCES), before it makes a child: no
445+program is started, and no process id given for a program that was
446+never there. A `p.path` with a `/` is not looked at first, and a
447+program that is there but that the kernel then refuses to run (not a
448+program, a bad interpreter) is a child that ends with status 127. Its environment is this
442449 program's with each `NAME=value` of `p.env` added or put in place of
443450 the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in
444451 `p.dir`. Its standard input, output and error are `p.fds`; a 0 gives
@@ -446,10 +453,13 @@
446453 `ls` as a shell runs it, with every other field zero. It gets no
447454 other open file but those of `p.more`, each at its own number, as a
448455 shell passes `3<file` on, every signal does what it does at first, and
449-none is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a
450-session on that terminal: it is its controlling terminal, and its
451-standard input, output and error, and `p.fds` and `p.group` are not
452-used.
456+none is blocked; with `p.keepIgnored`, a signal this program ignores
457+stays ignored in it, as the kernel's `execve` leaves it, which is what
458+a program run under `nohup` wants of the programs it starts. If `p.tty`
459+is a pseudo-terminal's fd, the program starts a session on that
460+terminal: it is its controlling terminal, and its standard input,
461+output and error, but that a `p.fds[i]` that is not 0 is that file
462+instead, as for a job that reads `/dev/null`; `p.group` is not used.
453463
454464 `p.group` puts the program in a process group, the programs that a
455465 shell's job is made of: -1 makes it the first of a new group, whose
@@ -525,6 +535,9 @@
525535 it; or `stopped`, by Ctrl-Z or a signal, with the signal's number.
526536 Without `block` it doesn't wait, and is 0 and `running` if no program
527537 changed. A program that has nothing to wait for is -10 (ECHILD).
538+`waitExit(pid)` is `wait` that blocks until a program ends, and not
539+for one that is stopped, so that a program waiting for its editor
540+isn't woken by Ctrl-Z.
528541 `kill` sends a program a signal, `sigTerm` to end it; to a group, if
529542 `pid` is minus its number.
530543

lib/Sys.linux-x86_64.raz (history) modified +81 -23

@@ -81,6 +81,7 @@
8181 foreground*: BOOL; (* its group gets the terminal on standard input *)
8282 tty*: INT; (* a pseudo-terminal to start a session on, or 0 *)
8383 more*: []INT; (* fds above 2 passed on to it at the same numbers *)
84+ keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *)
8485 end;
8586 (* How a program that wait waited for changed. *)
8687 Status* = (running, exited, killed, stopped);
@@ -1668,9 +1669,9 @@
16681669
16691670 (* What p asks of the program before it runs, done to this one: a
16701671 session on the terminal name, if p.tty, or else its group, its
1671- terminal's foreground, and its standard input, output and error;
1672- then its working directory, the C string dir. 0, or minus an
1673- errno. *)
1672+ terminal's foreground; then its standard input, output and error,
1673+ each p.fds names over the terminal's; then its working directory,
1674+ the C string dir. 0, or minus an errno. *)
16741675 proc takeOn(p: Program; name, dir: []U8): INT;
16751676 var fd, i, r: INT;
16761677 begin
@@ -1686,23 +1687,32 @@
16861687 elsif p.group # 0 then _ := SYSCALL(109, 0, p.group);
16871688 end;
16881689 if p.foreground then _ := foreground(0, SYSCALL(111)); end; (* getpgrp *)
1689- for i := 0 to 2 do
1690- if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *)
1691- end;
1690+ end;
1691+ for i := 0 to 2 do
1692+ if p.fds[i] # 0 then _ := SYSCALL(33, p.fds[i], i); end; (* dup2 *)
16921693 end;
16931694 if LEN(p.dir) > 0 then return SYSCALL(80, ADR(dir[0])); end; (* chdir *)
16941695 return 0;
16951696 end;
16961697
1698+(* Whether this program ignores the signal sig: SIG_IGN is 1. *)
1699+proc ignored(sig: INT): BOOL;
1700+var act: [4]U64; (* struct sigaction *)
1701+begin
1702+ _ := SYSCALL(13, sig, 0, ADR(act), 8); (* rt_sigaction *)
1703+ return act[0] = 1;
1704+end;
1705+
16971706 (* Starts the program p describes, found as run says: its process id,
1698- or minus an errno. A program that can't be started ends with status
1699- 127. Every signal is blocked from the fork until the program has its
1700- own: its group, its terminal's foreground, and signals as they are
1701- at first. *)
1707+ or minus an errno: -2 (ENOENT) or -13 (EACCES) as exec says, before
1708+ the fork, if there is no program to run. A program found that
1709+ execve then refuses ends with status 127. Every signal is blocked
1710+ from the fork until the program has its own: its group, its
1711+ terminal's foreground, and signals as they are at first. *)
17021712 proc spawn*(p: Program): INT;
17031713 var
17041714 a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; (* struct sigaction: SIG_DFL *)
1705- argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, i, g: INT; search: []U8;
1715+ argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, i, g, r: INT; search: []U8;
17061716 begin
17071717 if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *)
17081718 if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *)
@@ -1715,13 +1725,17 @@
17151725 if argv = nil then return -12; end; (* ENOMEM *)
17161726 search := env("PATH"); (* read here, before the fork *)
17171727 if LEN(search) = 0 then search := defaultPath; end;
1728+ r := find(p.path, search, a);
1729+ if r < 0 then munmap(^U8(argv), size); return r; end;
17181730 all := 0FFFFFFFFFFFFFFFFH;
17191731 _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *)
17201732 pid := SYSCALL(57); (* fork *)
17211733 if pid = 0 then
17221734 if takeOn(p, name, dir) # 0 then _ := SYSCALL(231, 127); end; (* exit *)
17231735 closeAllBut(p.more);
1724- for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *)
1736+ for i := 1 to 64 do
1737+ if not (p.keepIgnored and ignored(i)) then _ := SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *)
1738+ end;
17251739 _ := SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *)
17261740 run(p.path, search, a, argv, argv + (LEN(p.args) + 1));
17271741 _ := SYSCALL(231, 127); (* exit *)
@@ -1773,7 +1787,10 @@
17731787 gives them, each kept to give back if execve fails *)
17741788 all := 0FFFFFFFFFFFFFFFFH;
17751789 _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *)
1776- for i := 1 to 64 do _ := SYSCALL(13, i, ADR(dfl), ADR(acts[i]), 8); end; (* rt_sigaction *)
1790+ for i := 1 to 64 do
1791+ _ := SYSCALL(13, i, 0, ADR(acts[i]), 8); (* rt_sigaction: as it is *)
1792+ if not (p.keepIgnored and (acts[i][0] = 1)) then _ := SYSCALL(13, i, ADR(dfl), 0, 8); end;
1793+ end;
17771794 _ := SYSCALL(14, 2, ADR(empty), 0, 8);
17781795 r := SYSCALL(59, ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *)
17791796 _ := SYSCALL(14, 2, ADR(all), 0, 8);
@@ -1801,23 +1818,38 @@
18011818 return r;
18021819 end;
18031820
1821+(* wait4 with options, its status read as C's macros read it. *)
1822+proc waitFor(pid, options: INT): (INT, Status, INT);
1823+var status: I32; r: INT;
1824+begin
1825+ r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *)
1826+ if r <= 0 then return r, running, 0; end;
1827+ if status & 7FH = 0 then return r, exited, INT(status) shr 8 & 0FFH; end;
1828+ if status & 0FFH = 7FH then return r, stopped, INT(status) shr 8 & 0FFH; end;
1829+ return r, killed, INT(status) & 7FH;
1830+end;
1831+
18041832 (* Waits for the program pid, any of this program's if pid is -1, or
18051833 any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with
18061834 its exit status; killed or stopped, with the signal's number. Without
18071835 block it doesn't wait: 0 and running if none has changed. Minus an
18081836 errno, and running, if there is none to wait for. *)
18091837 proc wait*(pid: INT; block: BOOL): (INT, Status, INT);
1810-var status: I32; r, options: INT;
1838+var options: INT;
18111839 begin
18121840 options := 3; (* WNOHANG, WUNTRACED *)
18131841 if block then options := 2; end;
1814- r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *)
1815- if r <= 0 then return r, running, 0; end;
1816- if status & 7FH = 0 then return r, exited, INT(status) shr 8 & 0FFH; end;
1817- if status & 0FFH = 7FH then return r, stopped, INT(status) shr 8 & 0FFH; end;
1818- return r, killed, INT(status) & 7FH;
1842+ return waitFor(pid, options);
18191843 end;
18201844
1845+(* Waits for the program pid as wait does, but only for one to end:
1846+ a stopped program is not waited for, so the status is exited or
1847+ killed, or running if there is none to wait for. *)
1848+proc waitExit*(pid: INT): (INT, Status, INT);
1849+begin
1850+ return waitFor(pid, 0);
1851+end;
1852+
18211853 (* Sends the program pid, or the group -pid, the signal: 0, or minus an
18221854 errno. *)
18231855 proc kill*(pid, signal: INT): INT;
@@ -1934,7 +1966,9 @@
19341966 terminal's foreground until it ends with status 3; one in a
19351967 directory and an environment of its own writes to a pipe; one in a
19361968 group of its own is stopped and killed through it; one that isn't
1937- there ends with 127. Then signals: caught as a program ends and as
1969+ there is no child, and one found that execve refuses ends with 127;
1970+ ignored signals stay so only with keepIgnored; fds replace a
1971+ terminal's files. Then signals: caught as a program ends and as
19381972 one is sent; and a program starts with them neither caught nor
19391973 ignored. *)
19401974 proc testPrograms;
@@ -2002,9 +2036,33 @@
20022036 child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]});
20032037 r, st, c := wait(child, true);
20042038 if (r # child) or (st # exited) or (c # 4) then exit(2); end;
2005- child := spawn({path: "no-such-program-on-path", args: ["x"]});
2006- r, st, c := wait(child, true);
2007- if (r # child) or (st # exited) or (c # 127) then exit(2); end;
2039+ (* one not on PATH is no child at all *)
2040+ if spawn({path: "no-such-program-on-path", args: ["x"]}) # -2 then exit(2); end;
2041+ r, st, c := wait(-1, false);
2042+ if r # -10 then exit(2); end; (* ECHILD: none was made *)
2043+ (* an ignored signal stays so only with keepIgnored *)
2044+ if ignore(sigInt, true) # 0 then exit(2); end;
2045+ child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"], keepIgnored: true});
2046+ r, st, c := waitExit(child);
2047+ if (r # child) or (st # exited) or (c # 6) then exit(2); end;
2048+ child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"]});
2049+ r, st, c := waitExit(child);
2050+ if (r # child) or (st # killed) or (c # sigInt) then exit(2); end;
2051+ if ignore(sigInt, false) # 0 then exit(2); end;
2052+ (* fds replace the terminal's files, with tty *)
2053+ pty := openPty();
2054+ r, w := pipe();
2055+ child := spawn({path: "sh", args: ["sh", "-c", "echo out; echo err >&2"], tty: pty, fds: [0, w, 0]});
2056+ close(w);
2057+ n := 0;
2058+ repeat
2059+ m := read(r, buf[n..LEN(buf)]);
2060+ if m > 0 then INC(n, m); end;
2061+ until m <= 0;
2062+ close(r);
2063+ r, st, c := waitExit(child);
2064+ close(pty);
2065+ if (buf[0..n] # "out\n") or (r # child) or (st # exited) or (c # 0) then exit(2); end;
20082066
20092067 sigs := catch([sigChld, sigHup]);
20102068 if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([11]) # -22) or (catch([sigKill]) # -22) then exit(2); end;

lib/Sys.raz (history) modified +91 -23

@@ -97,6 +97,7 @@
9797 foreground*: BOOL; (* its group gets the terminal on standard input *)
9898 tty*: INT; (* a pseudo-terminal to start a session on, or 0 *)
9999 more*: []INT; (* fds above 2 passed on to it at the same numbers *)
100+ keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *)
100101 end;
101102 (* How a program that wait waited for changed. *)
102103 Status* = (running, exited, killed, stopped);
@@ -1554,9 +1555,9 @@
15541555
15551556 (* What p asks of the program before it runs, done to this one: a
15561557 session on the terminal name, if p.tty, or else its group, its
1557- terminal's foreground, and its standard input, output and error;
1558- then its working directory, the C string dir. 0, or minus an
1559- errno. *)
1558+ terminal's foreground; then its standard input, output and error,
1559+ each p.fds names over the terminal's; then its working directory,
1560+ the C string dir. 0, or minus an errno. *)
15601561 proc takeOn(p: Program; name, dir: []U8): INT;
15611562 var fd: I32; i: INT;
15621563 begin
@@ -1571,19 +1572,36 @@
15711572 elsif p.group # 0 then _ := C.setpgid(0, I32(p.group));
15721573 end;
15731574 if p.foreground then _ := C.tcsetpgrp(0, C.getpgrp()); end;
1574- for i := 0 to 2 do
1575- if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end;
1576- end;
1575+ end;
1576+ for i := 0 to 2 do
1577+ if p.fds[i] # 0 then _ := C.dup2(I32(p.fds[i]), I32(i)); end;
15771578 end;
15781579 if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then return failed(); end;
15791580 return 0;
1581+end;
1582+
1583+(* Whether the struct sigaction a says SIG_IGN. *)
1584+proc ignoredIn(var a: Sigaction): BOOL;
1585+var h: ^UINT;
1586+begin
1587+ h := ^UINT(^U8(ADR(a)) + CAbi.saHandlerAt);
1588+ return h^ = 1;
1589+end;
1590+
1591+(* Whether this program ignores the signal sig. *)
1592+proc ignored(sig: INT): BOOL;
1593+var a: Sigaction;
1594+begin
1595+ _ := C.sigaction(I32(sig), nil, ^U8(ADR(a)));
1596+ return ignoredIn(a);
15801597 end;
15811598
15821599 (* Starts the program p describes, found as run says: its process id,
1583- or minus an errno. A program that can't be started ends with status
1584- 127. Every signal is blocked from the fork until the program has its
1585- own: its group, its terminal's foreground, and signals as they are
1586- at first. *)
1600+ or minus an errno: -2 (ENOENT) or -13 (EACCES) as exec says, before
1601+ the fork, if there is no program to run. A program found that
1602+ execve then refuses ends with status 127. Every signal is blocked
1603+ from the fork until the program has its own: its group, its
1604+ terminal's foreground, and signals as they are at first. *)
15871605 proc spawn*(p: Program): INT;
15881606 var
15891607 a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, err, i, g: INT; search: []U8;
@@ -1603,13 +1621,17 @@
16031621 if argv = nil then return -12; end; (* ENOMEM *)
16041622 search := env("PATH"); (* read here, before the fork *)
16051623 if LEN(search) = 0 then search := defaultPath; end;
1624+ err := find(p.path, search, a);
1625+ if err < 0 then munmap(^U8(argv), size); return err; end;
16061626 for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end;
16071627 _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), ^U8(ADR(old)));
16081628 pid := C.fork();
16091629 if pid = 0 then
16101630 if takeOn(p, name, dir) # 0 then C._exit(127); end;
16111631 closeAllBut(p.more);
1612- for i := 1 to CAbi.sigMax do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end;
1632+ for i := 1 to CAbi.sigMax do
1633+ if not (p.keepIgnored and ignored(i)) then _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end;
1634+ end;
16131635 _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil);
16141636 run(p.path, search, a, argv, argv + (LEN(p.args) + 1));
16151637 C._exit(127);
@@ -1666,7 +1688,12 @@
16661688 gives them, each kept to give back if execve fails *)
16671689 for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end;
16681690 _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), ^U8(ADR(old)));
1669- for i := 1 to CAbi.sigMax do _ := C.sigaction(I32(i), ^U8(ADR(dfl)), ^U8(ADR(acts[i]))); end;
1691+ for i := 1 to CAbi.sigMax do
1692+ _ := C.sigaction(I32(i), nil, ^U8(ADR(acts[i]))); (* as it is *)
1693+ if not (p.keepIgnored and ignoredIn(acts[i])) then
1694+ _ := C.sigaction(I32(i), ^U8(ADR(dfl)), nil);
1695+ end;
1696+ end;
16701697 _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil);
16711698 _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1));
16721699 r := failed();
@@ -1696,6 +1723,18 @@
16961723 return r;
16971724 end;
16981725
1726+(* waitpid with options, its status read as C's macros read it. *)
1727+proc waitFor(pid: INT; options: I32): (INT, Status, INT);
1728+var status, r: I32;
1729+begin
1730+ r := C.waitpid(I32(pid), ADR(status), options);
1731+ if r < 0 then return failed(), running, 0; end;
1732+ if r = 0 then return 0, running, 0; end;
1733+ if status & 7FH = 0 then return INT(r), exited, INT(status) shr 8 & 0FFH; end;
1734+ if status & 0FFH = 7FH then return INT(r), stopped, INT(status) shr 8 & 0FFH; end;
1735+ return INT(r), killed, INT(status) & 7FH;
1736+end;
1737+
16991738 (* Waits for the program pid, any of this program's if pid is -1, or
17001739 any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with
17011740 its exit status; killed or stopped, with the signal's number. Without
@@ -1703,16 +1742,19 @@
17031742 errno, and running, if there is none to wait for. The status is read
17041743 as C's macros read it, which every Unix encodes alike. *)
17051744 proc wait*(pid: INT; block: BOOL): (INT, Status, INT);
1706-var status, options, r: I32;
1745+var options: I32;
17071746 begin
17081747 options := CAbi.wNohang | CAbi.wUntraced;
17091748 if block then options := CAbi.wUntraced; end;
1710- r := C.waitpid(I32(pid), ADR(status), options);
1711- if r < 0 then return failed(), running, 0; end;
1712- if r = 0 then return 0, running, 0; end;
1713- if status & 7FH = 0 then return INT(r), exited, INT(status) shr 8 & 0FFH; end;
1714- if status & 0FFH = 7FH then return INT(r), stopped, INT(status) shr 8 & 0FFH; end;
1715- return INT(r), killed, INT(status) & 7FH;
1749+ return waitFor(pid, options);
1750+end;
1751+
1752+(* Waits for the program pid as wait does, but only for one to end:
1753+ a stopped program is not waited for, so the status is exited or
1754+ killed, or running if there is none to wait for. *)
1755+proc waitExit*(pid: INT): (INT, Status, INT);
1756+begin
1757+ return waitFor(pid, 0);
17161758 end;
17171759
17181760 (* Sends the program pid, or the group -pid, the signal: 0, or minus an
@@ -1860,7 +1902,9 @@
18601902 terminal's foreground until it ends with status 3; one in a
18611903 directory and an environment of its own writes to a pipe; one in a
18621904 group of its own is stopped and killed through it; one that isn't
1863- there ends with 127. Then signals: caught as a program ends and as
1905+ there is no child, and one found that execve refuses ends with 127;
1906+ ignored signals stay so only with keepIgnored; fds replace a
1907+ terminal's files. Then signals: caught as a program ends and as
18641908 one is sent; and a program starts with them neither caught nor
18651909 ignored. *)
18661910 proc testPrograms;
@@ -1928,9 +1972,33 @@
19281972 child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]});
19291973 r, st, c := wait(child, true);
19301974 if (r # child) or (st # exited) or (c # 4) then exit(2); end;
1931- child := spawn({path: "no-such-program-on-path", args: ["x"]});
1932- r, st, c := wait(child, true);
1933- if (r # child) or (st # exited) or (c # 127) then exit(2); end;
1975+ (* one not on PATH is no child at all *)
1976+ if spawn({path: "no-such-program-on-path", args: ["x"]}) # -2 then exit(2); end;
1977+ r, st, c := wait(-1, false);
1978+ if r # -10 then exit(2); end; (* ECHILD: none was made *)
1979+ (* an ignored signal stays so only with keepIgnored *)
1980+ if ignore(sigInt, true) # 0 then exit(2); end;
1981+ child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"], keepIgnored: true});
1982+ r, st, c := waitExit(child);
1983+ if (r # child) or (st # exited) or (c # 6) then exit(2); end;
1984+ child := spawn({path: "sh", args: ["sh", "-c", "kill -INT $$; exit 6"]});
1985+ r, st, c := waitExit(child);
1986+ if (r # child) or (st # killed) or (c # sigInt) then exit(2); end;
1987+ if ignore(sigInt, false) # 0 then exit(2); end;
1988+ (* fds replace the terminal's files, with tty *)
1989+ pty := openPty();
1990+ r, w := pipe();
1991+ child := spawn({path: "sh", args: ["sh", "-c", "echo out; echo err >&2"], tty: pty, fds: [0, w, 0]});
1992+ close(w);
1993+ n := 0;
1994+ repeat
1995+ m := read(r, buf[n..LEN(buf)]);
1996+ if m > 0 then INC(n, m); end;
1997+ until m <= 0;
1998+ close(r);
1999+ r, st, c := waitExit(child);
2000+ close(pty);
2001+ if (buf[0..n] # "out\n") or (r # child) or (st # exited) or (c # 0) then exit(2); end;
19342002
19352003 sigs := catch([sigChld, sigHup]);
19362004 if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([CAbi.sigSegv]) # -22) or (catch([sigKill]) # -22) then exit(2); end;