Sys: spawn finds its program first, keepIgnored, fds with tty, waitExit
| @@ -229,6 +229,7 @@ | ||
| 229 | 229 | foreground*: BOOL; (* its group takes the terminal on standard input *) |
| 230 | 230 | tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) |
| 231 | 231 | more*: []INT; (* fds above 2 passed on to it, at the same numbers *) |
| 232 | + keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *) | |
| 232 | 233 | end; |
| 233 | 234 | Status* = (running, exited, killed, stopped); |
| 234 | 235 | Mode* = record ... end; (* a terminal's modes; its fields are the target's *) |
| @@ -306,6 +307,7 @@ | ||
| 306 | 307 | proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *) |
| 307 | 308 | proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) |
| 308 | 309 | 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 *) | |
| 309 | 311 | proc kill*(pid, signal: INT): INT; |
| 310 | 312 | proc pid*: INT; (* this program's process id *) |
| 311 | 313 | proc group*: INT; (* the process group this program is in *) |
| @@ -438,7 +440,12 @@ | ||
| 438 | 440 | looked for as a shell looks for a command, and as C's `execvp` does: |
| 439 | 441 | in each directory of this program's `PATH` in turn, an empty one being |
| 440 | 442 | 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 | |
| 442 | 449 | program's with each `NAME=value` of `p.env` added or put in place of |
| 443 | 450 | the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in |
| 444 | 451 | `p.dir`. Its standard input, output and error are `p.fds`; a 0 gives |
| @@ -446,10 +453,13 @@ | ||
| 446 | 453 | `ls` as a shell runs it, with every other field zero. It gets no |
| 447 | 454 | other open file but those of `p.more`, each at its own number, as a |
| 448 | 455 | 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. | |
| 453 | 463 | |
| 454 | 464 | `p.group` puts the program in a process group, the programs that a |
| 455 | 465 | shell's job is made of: -1 makes it the first of a new group, whose |
| @@ -525,6 +535,9 @@ | ||
| 525 | 535 | it; or `stopped`, by Ctrl-Z or a signal, with the signal's number. |
| 526 | 536 | Without `block` it doesn't wait, and is 0 and `running` if no program |
| 527 | 537 | 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. | |
| 528 | 541 | `kill` sends a program a signal, `sigTerm` to end it; to a group, if |
| 529 | 542 | `pid` is minus its number. |
| 530 | 543 |
| @@ -81,6 +81,7 @@ | ||
| 81 | 81 | foreground*: BOOL; (* its group gets the terminal on standard input *) |
| 82 | 82 | tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) |
| 83 | 83 | more*: []INT; (* fds above 2 passed on to it at the same numbers *) |
| 84 | + keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *) | |
| 84 | 85 | end; |
| 85 | 86 | (* How a program that wait waited for changed. *) |
| 86 | 87 | Status* = (running, exited, killed, stopped); |
| @@ -1668,9 +1669,9 @@ | ||
| 1668 | 1669 | |
| 1669 | 1670 | (* What p asks of the program before it runs, done to this one: a |
| 1670 | 1671 | 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. *) | |
| 1674 | 1675 | proc takeOn(p: Program; name, dir: []U8): INT; |
| 1675 | 1676 | var fd, i, r: INT; |
| 1676 | 1677 | begin |
| @@ -1686,23 +1687,32 @@ | ||
| 1686 | 1687 | elsif p.group # 0 then _ := SYSCALL(109, 0, p.group); |
| 1687 | 1688 | end; |
| 1688 | 1689 | 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 *) | |
| 1692 | 1693 | end; |
| 1693 | 1694 | if LEN(p.dir) > 0 then return SYSCALL(80, ADR(dir[0])); end; (* chdir *) |
| 1694 | 1695 | return 0; |
| 1695 | 1696 | end; |
| 1696 | 1697 | |
| 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 | + | |
| 1697 | 1706 | (* 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. *) | |
| 1702 | 1712 | proc spawn*(p: Program): INT; |
| 1703 | 1713 | var |
| 1704 | 1714 | 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; | |
| 1706 | 1716 | begin |
| 1707 | 1717 | if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) |
| 1708 | 1718 | if (p.tty # 0) and (ptyName(p.tty, name) # 0) then return -25; end; (* ENOTTY *) |
| @@ -1715,13 +1725,17 @@ | ||
| 1715 | 1725 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1716 | 1726 | search := env("PATH"); (* read here, before the fork *) |
| 1717 | 1727 | 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; | |
| 1718 | 1730 | all := 0FFFFFFFFFFFFFFFFH; |
| 1719 | 1731 | _ := SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *) |
| 1720 | 1732 | pid := SYSCALL(57); (* fork *) |
| 1721 | 1733 | if pid = 0 then |
| 1722 | 1734 | if takeOn(p, name, dir) # 0 then _ := SYSCALL(231, 127); end; (* exit *) |
| 1723 | 1735 | 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; | |
| 1725 | 1739 | _ := SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *) |
| 1726 | 1740 | run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); |
| 1727 | 1741 | _ := SYSCALL(231, 127); (* exit *) |
| @@ -1773,7 +1787,10 @@ | ||
| 1773 | 1787 | gives them, each kept to give back if execve fails *) |
| 1774 | 1788 | all := 0FFFFFFFFFFFFFFFFH; |
| 1775 | 1789 | _ := 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; | |
| 1777 | 1794 | _ := SYSCALL(14, 2, ADR(empty), 0, 8); |
| 1778 | 1795 | r := SYSCALL(59, ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *) |
| 1779 | 1796 | _ := SYSCALL(14, 2, ADR(all), 0, 8); |
| @@ -1801,23 +1818,38 @@ | ||
| 1801 | 1818 | return r; |
| 1802 | 1819 | end; |
| 1803 | 1820 | |
| 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 | + | |
| 1804 | 1832 | (* Waits for the program pid, any of this program's if pid is -1, or |
| 1805 | 1833 | any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with |
| 1806 | 1834 | its exit status; killed or stopped, with the signal's number. Without |
| 1807 | 1835 | block it doesn't wait: 0 and running if none has changed. Minus an |
| 1808 | 1836 | errno, and running, if there is none to wait for. *) |
| 1809 | 1837 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); |
| 1810 | -var status: I32; r, options: INT; | |
| 1838 | +var options: INT; | |
| 1811 | 1839 | begin |
| 1812 | 1840 | options := 3; (* WNOHANG, WUNTRACED *) |
| 1813 | 1841 | 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); | |
| 1819 | 1843 | end; |
| 1820 | 1844 | |
| 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 | + | |
| 1821 | 1853 | (* Sends the program pid, or the group -pid, the signal: 0, or minus an |
| 1822 | 1854 | errno. *) |
| 1823 | 1855 | proc kill*(pid, signal: INT): INT; |
| @@ -1934,7 +1966,9 @@ | ||
| 1934 | 1966 | terminal's foreground until it ends with status 3; one in a |
| 1935 | 1967 | directory and an environment of its own writes to a pipe; one in a |
| 1936 | 1968 | 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 | |
| 1938 | 1972 | one is sent; and a program starts with them neither caught nor |
| 1939 | 1973 | ignored. *) |
| 1940 | 1974 | proc testPrograms; |
| @@ -2002,9 +2036,33 @@ | ||
| 2002 | 2036 | child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]}); |
| 2003 | 2037 | r, st, c := wait(child, true); |
| 2004 | 2038 | 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; | |
| 2008 | 2066 | |
| 2009 | 2067 | sigs := catch([sigChld, sigHup]); |
| 2010 | 2068 | if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([11]) # -22) or (catch([sigKill]) # -22) then exit(2); end; |
| @@ -97,6 +97,7 @@ | ||
| 97 | 97 | foreground*: BOOL; (* its group gets the terminal on standard input *) |
| 98 | 98 | tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) |
| 99 | 99 | more*: []INT; (* fds above 2 passed on to it at the same numbers *) |
| 100 | + keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *) | |
| 100 | 101 | end; |
| 101 | 102 | (* How a program that wait waited for changed. *) |
| 102 | 103 | Status* = (running, exited, killed, stopped); |
| @@ -1554,9 +1555,9 @@ | ||
| 1554 | 1555 | |
| 1555 | 1556 | (* What p asks of the program before it runs, done to this one: a |
| 1556 | 1557 | 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. *) | |
| 1560 | 1561 | proc takeOn(p: Program; name, dir: []U8): INT; |
| 1561 | 1562 | var fd: I32; i: INT; |
| 1562 | 1563 | begin |
| @@ -1571,19 +1572,36 @@ | ||
| 1571 | 1572 | elsif p.group # 0 then _ := C.setpgid(0, I32(p.group)); |
| 1572 | 1573 | end; |
| 1573 | 1574 | 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; | |
| 1577 | 1578 | end; |
| 1578 | 1579 | if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then return failed(); end; |
| 1579 | 1580 | 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); | |
| 1580 | 1597 | end; |
| 1581 | 1598 | |
| 1582 | 1599 | (* 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. *) | |
| 1587 | 1605 | proc spawn*(p: Program): INT; |
| 1588 | 1606 | var |
| 1589 | 1607 | a, dir, name: [4096]U8; argv: ^^U8; none: ^U8; size, err, i, g: INT; search: []U8; |
| @@ -1603,13 +1621,17 @@ | ||
| 1603 | 1621 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1604 | 1622 | search := env("PATH"); (* read here, before the fork *) |
| 1605 | 1623 | 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; | |
| 1606 | 1626 | for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; |
| 1607 | 1627 | _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(all)), ^U8(ADR(old))); |
| 1608 | 1628 | pid := C.fork(); |
| 1609 | 1629 | if pid = 0 then |
| 1610 | 1630 | if takeOn(p, name, dir) # 0 then C._exit(127); end; |
| 1611 | 1631 | 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; | |
| 1613 | 1635 | _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil); |
| 1614 | 1636 | run(p.path, search, a, argv, argv + (LEN(p.args) + 1)); |
| 1615 | 1637 | C._exit(127); |
| @@ -1666,7 +1688,12 @@ | ||
| 1666 | 1688 | gives them, each kept to give back if execve fails *) |
| 1667 | 1689 | for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end; |
| 1668 | 1690 | _ := 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; | |
| 1670 | 1697 | _ := C.sigprocmask(CAbi.sigSetmask, ^U8(ADR(empty)), nil); |
| 1671 | 1698 | _ := C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1)); |
| 1672 | 1699 | r := failed(); |
| @@ -1696,6 +1723,18 @@ | ||
| 1696 | 1723 | return r; |
| 1697 | 1724 | end; |
| 1698 | 1725 | |
| 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 | + | |
| 1699 | 1738 | (* Waits for the program pid, any of this program's if pid is -1, or |
| 1700 | 1739 | any of the group g if pid is -g, to change, if block, and says which one changed and how: exited, with |
| 1701 | 1740 | its exit status; killed or stopped, with the signal's number. Without |
| @@ -1703,16 +1742,19 @@ | ||
| 1703 | 1742 | errno, and running, if there is none to wait for. The status is read |
| 1704 | 1743 | as C's macros read it, which every Unix encodes alike. *) |
| 1705 | 1744 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); |
| 1706 | -var status, options, r: I32; | |
| 1745 | +var options: I32; | |
| 1707 | 1746 | begin |
| 1708 | 1747 | options := CAbi.wNohang | CAbi.wUntraced; |
| 1709 | 1748 | 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); | |
| 1716 | 1758 | end; |
| 1717 | 1759 | |
| 1718 | 1760 | (* Sends the program pid, or the group -pid, the signal: 0, or minus an |
| @@ -1860,7 +1902,9 @@ | ||
| 1860 | 1902 | terminal's foreground until it ends with status 3; one in a |
| 1861 | 1903 | directory and an environment of its own writes to a pipe; one in a |
| 1862 | 1904 | 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 | |
| 1864 | 1908 | one is sent; and a program starts with them neither caught nor |
| 1865 | 1909 | ignored. *) |
| 1866 | 1910 | proc testPrograms; |
| @@ -1928,9 +1972,33 @@ | ||
| 1928 | 1972 | child := spawn({path: "sh", args: ["sh", "-c", "exit 4"]}); |
| 1929 | 1973 | r, st, c := wait(child, true); |
| 1930 | 1974 | 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; | |
| 1934 | 2002 | |
| 1935 | 2003 | sigs := catch([sigChld, sigHup]); |
| 1936 | 2004 | if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([CAbi.sigSegv]) # -22) or (catch([sigKill]) # -22) then exit(2); end; |