raz / commit 0b79ce2b

commit
0b79ce2bcb0011900751d1df125c9cdf0132db9b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
db22a346
Sys: setsid, a session of a program's own, and blocking, whether a descriptor's reads and writes wait

patch · log

3 files changed, +96 -1

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

@@ -311,6 +311,7 @@
311311 proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
312312 proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
313313 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 *)
314315
315316 proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *)
316317
@@ -328,6 +329,7 @@
328329 proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *)
329330 proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *)
330331 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 *)
331333 proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *)
332334 proc waitExit*(pid: INT): (INT, Status, INT); (* as wait, but only for a program to end *)
333335 proc kill*(pid, signal: INT): INT;
@@ -532,6 +534,7 @@
532534 builtin or a procedure as a stage of a pipe. Only the thread that
533535 calls it is copied, so a program with threads mustn't; and what `Out`
534536 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.
535538 `group` is the process group this program is in: a shell starting on a
536539 terminal waits until it is `foregroundOf(0)` before it takes it.
537540 `access` says whether this user, by its real ids, may read, write or
@@ -542,7 +545,8 @@
542545 fifo opened to read waits for a writer, but with `nonblocking` the open
543546 returns at once (-6, ENXIO, for a write with no reader), and a read or
544547 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
546550 a program `exec` or `execve` starts, as `pipe`'s are, so that a fifo or
547551 a log one program holds doesn't reach every program it runs;
548552 `keepOnExec(fd, true)` keeps one open, and `spawn` is unchanged, since

lib/Sys.linux-x86_64.raz (history) modified +44 -0

@@ -1060,6 +1060,17 @@
10601060 return SYSCALL(72, fd, 4, flags); (* F_SETFL *)
10611061 end;
10621062
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+
10631074 (* A stream socket connected to port at addr, as connectTcp makes it,
10641075 or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds.
10651076 It starts to connect without waiting, then waits for the socket to
@@ -2062,6 +2073,15 @@
20622073 return SYSCALL(57); (* fork *)
20632074 end;
20642075
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+
20652085 (* Gives the processor to another thread. *)
20662086 proc yield*;
20672087 begin
@@ -2132,6 +2152,30 @@
21322152 if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end;
21332153 close(r);
21342154 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;
21352179
21362180 child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1});
21372181 r, st, c := wait(child, false);

lib/Sys.raz (history) modified +47 -0

@@ -1110,6 +1110,17 @@
11101110 return 0;
11111111 end;
11121112
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+
11131124 (* A stream socket connected to port at addr, as connectTcp makes it,
11141125 or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds.
11151126 It starts to connect without waiting, then waits for the socket to
@@ -2003,6 +2014,18 @@
20032014 return INT(r);
20042015 end;
20052016
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+
20062029 (* Gives the processor to another thread. *)
20072030 proc yield*;
20082031 begin
@@ -2073,6 +2096,30 @@
20732096 if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end;
20742097 close(r);
20752098 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;
20762123
20772124 child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1});
20782125 r, st, c := wait(child, false);