Sys: setsid, a session of a program's own, and blocking, whether a descriptor's reads and writes wait
| @@ -311,6 +311,7 @@ | ||
| 311 | 311 | proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) |
| 312 | 312 | proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) |
| 313 | 313 | proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) |
| 314 | +proc blocking*(fd: INT): INT; (* 1 if reads and writes wait, 0 if they are -11 at once, or minus an errno *) | |
| 314 | 315 | |
| 315 | 316 | proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *) |
| 316 | 317 | |
| @@ -328,6 +329,7 @@ | ||
| 328 | 329 | proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) |
| 329 | 330 | proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *) |
| 330 | 331 | proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) |
| 332 | +proc setsid*: INT; (* a session of its own, led by this program: its process id, or minus an errno *) | |
| 331 | 333 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) |
| 332 | 334 | proc waitExit*(pid: INT): (INT, Status, INT); (* as wait, but only for a program to end *) |
| 333 | 335 | proc kill*(pid, signal: INT): INT; |
| @@ -532,6 +534,7 @@ | ||
| 532 | 534 | builtin or a procedure as a stage of a pipe. Only the thread that |
| 533 | 535 | calls it is copied, so a program with threads mustn't; and what `Out` |
| 534 | 536 | keeps is copied too, to be written twice, so `Out.flush` comes first. |
| 537 | +`setsid` gives the copy a session of its own, with no controlling terminal, as a daemon does so that closing its terminal doesn't reach it; it is -1 (EPERM) for a program that leads a process group already, which the copy doesn't. | |
| 535 | 538 | `group` is the process group this program is in: a shell starting on a |
| 536 | 539 | terminal waits until it is `foregroundOf(0)` before it takes it. |
| 537 | 540 | `access` says whether this user, by its real ids, may read, write or |
| @@ -542,7 +545,8 @@ | ||
| 542 | 545 | fifo opened to read waits for a writer, but with `nonblocking` the open |
| 543 | 546 | returns at once (-6, ENXIO, for a write with no reader), and a read or |
| 544 | 547 | write that would wait is -11 later, as `setBlocking(fd, false)` makes |
| 545 | -it. A file `open`, `openMode`, `openRead` or `create` gives is closed in | |
| 548 | +it, and `blocking(fd)` says which: 1 if they wait, 0 if not. A file | |
| 549 | +`open`, `openMode`, `openRead` or `create` gives is closed in | |
| 546 | 550 | a program `exec` or `execve` starts, as `pipe`'s are, so that a fifo or |
| 547 | 551 | a log one program holds doesn't reach every program it runs; |
| 548 | 552 | `keepOnExec(fd, true)` keeps one open, and `spawn` is unchanged, since |
| @@ -1060,6 +1060,17 @@ | ||
| 1060 | 1060 | return SYSCALL(72, fd, 4, flags); (* F_SETFL *) |
| 1061 | 1061 | end; |
| 1062 | 1062 | |
| 1063 | +(* Whether reads and writes on fd wait, as they do at first: 1 if they | |
| 1064 | + do, 0 if they return -11 (EAGAIN) at once, or minus an errno. *) | |
| 1065 | +proc blocking*(fd: INT): INT; | |
| 1066 | +var flags: INT; | |
| 1067 | +begin | |
| 1068 | + flags := SYSCALL(72, fd, 3, 0); (* fcntl F_GETFL *) | |
| 1069 | + if flags < 0 then return flags; end; | |
| 1070 | + if flags & 800H # 0 then return 0; end; (* O_NONBLOCK *) | |
| 1071 | + return 1; | |
| 1072 | +end; | |
| 1073 | + | |
| 1063 | 1074 | (* A stream socket connected to port at addr, as connectTcp makes it, |
| 1064 | 1075 | or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds. |
| 1065 | 1076 | It starts to connect without waiting, then waits for the socket to |
| @@ -2062,6 +2073,15 @@ | ||
| 2062 | 2073 | return SYSCALL(57); (* fork *) |
| 2063 | 2074 | end; |
| 2064 | 2075 | |
| 2076 | +(* A new session and process group, led by this program, with no | |
| 2077 | + controlling terminal: its process id, or minus an errno (-1, EPERM, | |
| 2078 | + if it leads a process group already, as a program just forked | |
| 2079 | + doesn't). *) | |
| 2080 | +proc setsid*: INT; | |
| 2081 | +begin | |
| 2082 | + return SYSCALL(112); (* setsid *) | |
| 2083 | +end; | |
| 2084 | + | |
| 2065 | 2085 | (* Gives the processor to another thread. *) |
| 2066 | 2086 | proc yield*; |
| 2067 | 2087 | begin |
| @@ -2132,6 +2152,30 @@ | ||
| 2132 | 2152 | if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end; |
| 2133 | 2153 | close(r); |
| 2134 | 2154 | close(w); |
| 2155 | + | |
| 2156 | + (* blocking: a fresh pipe waits, a non-blocking one doesn't, a closed fd is -9 *) | |
| 2157 | + r, w := pipe(); | |
| 2158 | + if (blocking(r) # 1) or (setBlocking(r, false) # 0) or (blocking(r) # 0) then exit(2); end; | |
| 2159 | + if (setBlocking(r, true) # 0) or (blocking(r) # 1) then exit(2); end; | |
| 2160 | + close(r); | |
| 2161 | + close(w); | |
| 2162 | + if blocking(r) # -9 then exit(2); end; | |
| 2163 | + | |
| 2164 | + (* setsid: a forked child leads a session of its own, once; a group leader can't *) | |
| 2165 | + child := fork(); | |
| 2166 | + if child = 0 then | |
| 2167 | + if (setsid() # pid()) or (setsid() # -1) then exit(2); end; | |
| 2168 | + exit(0); | |
| 2169 | + end; | |
| 2170 | + r, st, c := wait(child, true); | |
| 2171 | + if (r # child) or (st # exited) or (c # 0) then exit(2); end; | |
| 2172 | + child := fork(); | |
| 2173 | + if child = 0 then | |
| 2174 | + if (setGroup(0, 0) # 0) or (setsid() # -1) then exit(2); end; | |
| 2175 | + exit(0); | |
| 2176 | + end; | |
| 2177 | + r, st, c := wait(child, true); | |
| 2178 | + if (r # child) or (st # exited) or (c # 0) then exit(2); end; | |
| 2135 | 2179 | |
| 2136 | 2180 | child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); |
| 2137 | 2181 | r, st, c := wait(child, false); |
| @@ -1110,6 +1110,17 @@ | ||
| 1110 | 1110 | return 0; |
| 1111 | 1111 | end; |
| 1112 | 1112 | |
| 1113 | +(* Whether reads and writes on fd wait, as they do at first: 1 if they | |
| 1114 | + do, 0 if they return -11 (EAGAIN) at once, or minus an errno. *) | |
| 1115 | +proc blocking*(fd: INT): INT; | |
| 1116 | +var flags: I32; | |
| 1117 | +begin | |
| 1118 | + flags := C.fcntl(I32(fd), CAbi.fGetfl, 0); | |
| 1119 | + if flags < 0 then return failed(); end; | |
| 1120 | + if flags & CAbi.oNonblock # 0 then return 0; end; | |
| 1121 | + return 1; | |
| 1122 | +end; | |
| 1123 | + | |
| 1113 | 1124 | (* A stream socket connected to port at addr, as connectTcp makes it, |
| 1114 | 1125 | or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds. |
| 1115 | 1126 | It starts to connect without waiting, then waits for the socket to |
| @@ -2003,6 +2014,18 @@ | ||
| 2003 | 2014 | return INT(r); |
| 2004 | 2015 | end; |
| 2005 | 2016 | |
| 2017 | +(* A new session and process group, led by this program, with no | |
| 2018 | + controlling terminal: its process id, or minus an errno (-1, EPERM, | |
| 2019 | + if it leads a process group already, as a program just forked | |
| 2020 | + doesn't). *) | |
| 2021 | +proc setsid*: INT; | |
| 2022 | +var r: I32; | |
| 2023 | +begin | |
| 2024 | + r := C.setsid(); | |
| 2025 | + if r < 0 then return failed(); end; | |
| 2026 | + return INT(r); | |
| 2027 | +end; | |
| 2028 | + | |
| 2006 | 2029 | (* Gives the processor to another thread. *) |
| 2007 | 2030 | proc yield*; |
| 2008 | 2031 | begin |
| @@ -2073,6 +2096,30 @@ | ||
| 2073 | 2096 | if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end; |
| 2074 | 2097 | close(r); |
| 2075 | 2098 | close(w); |
| 2099 | + | |
| 2100 | + (* blocking: a fresh pipe waits, a non-blocking one doesn't, a closed fd is -9 *) | |
| 2101 | + r, w := pipe(); | |
| 2102 | + if (blocking(r) # 1) or (setBlocking(r, false) # 0) or (blocking(r) # 0) then exit(2); end; | |
| 2103 | + if (setBlocking(r, true) # 0) or (blocking(r) # 1) then exit(2); end; | |
| 2104 | + close(r); | |
| 2105 | + close(w); | |
| 2106 | + if blocking(r) # -9 then exit(2); end; | |
| 2107 | + | |
| 2108 | + (* setsid: a forked child leads a session of its own, once; a group leader can't *) | |
| 2109 | + child := fork(); | |
| 2110 | + if child = 0 then | |
| 2111 | + if (setsid() # pid()) or (setsid() # -1) then exit(2); end; | |
| 2112 | + exit(0); | |
| 2113 | + end; | |
| 2114 | + r, st, c := wait(child, true); | |
| 2115 | + if (r # child) or (st # exited) or (c # 0) then exit(2); end; | |
| 2116 | + child := fork(); | |
| 2117 | + if child = 0 then | |
| 2118 | + if (setGroup(0, 0) # 0) or (setsid() # -1) then exit(2); end; | |
| 2119 | + exit(0); | |
| 2120 | + end; | |
| 2121 | + r, st, c := wait(child, true); | |
| 2122 | + if (r # child) or (st # exited) or (c # 0) then exit(2); end; | |
| 2076 | 2123 | |
| 2077 | 2124 | child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); |
| 2078 | 2125 | r, st, c := wait(child, false); |