From 0b79ce2bcb0011900751d1df125c9cdf0132db9b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:46:20 +0100 Subject: [PATCH] Sys: setsid, a session of a program's own, and blocking, whether a descriptor's reads and writes wait --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 6326796..4877ab3 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -311,6 +311,7 @@ proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) +proc blocking*(fd: INT): INT; (* 1 if reads and writes wait, 0 if they are -11 at once, or minus an errno *) proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *) @@ -328,6 +329,7 @@ proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *) proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) +proc setsid*: INT; (* a session of its own, led by this program: its process id, or minus an errno *) proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) proc waitExit*(pid: INT): (INT, Status, INT); (* as wait, but only for a program to end *) proc kill*(pid, signal: INT): INT; @@ -532,6 +534,7 @@ builtin or a procedure as a stage of a pipe. Only the thread that calls it is copied, so a program with threads mustn't; and what `Out` keeps is copied too, to be written twice, so `Out.flush` comes first. +`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. `group` is the process group this program is in: a shell starting on a terminal waits until it is `foregroundOf(0)` before it takes it. `access` says whether this user, by its real ids, may read, write or @@ -542,7 +545,8 @@ fifo opened to read waits for a writer, but with `nonblocking` the open returns at once (-6, ENXIO, for a write with no reader), and a read or write that would wait is -11 later, as `setBlocking(fd, false)` makes -it. A file `open`, `openMode`, `openRead` or `create` gives is closed in +it, and `blocking(fd)` says which: 1 if they wait, 0 if not. A file +`open`, `openMode`, `openRead` or `create` gives is closed in a program `exec` or `execve` starts, as `pipe`'s are, so that a fifo or a log one program holds doesn't reach every program it runs; `keepOnExec(fd, true)` keeps one open, and `spawn` is unchanged, since diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index d9fa843..15664d6 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -1060,6 +1060,17 @@ return SYSCALL(72, fd, 4, flags); (* F_SETFL *) end; +(* Whether reads and writes on fd wait, as they do at first: 1 if they + do, 0 if they return -11 (EAGAIN) at once, or minus an errno. *) +proc blocking*(fd: INT): INT; +var flags: INT; +begin + flags := SYSCALL(72, fd, 3, 0); (* fcntl F_GETFL *) + if flags < 0 then return flags; end; + if flags & 800H # 0 then return 0; end; (* O_NONBLOCK *) + return 1; +end; + (* A stream socket connected to port at addr, as connectTcp makes it, or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds. It starts to connect without waiting, then waits for the socket to @@ -2062,6 +2073,15 @@ return SYSCALL(57); (* fork *) end; +(* A new session and process group, led by this program, with no + controlling terminal: its process id, or minus an errno (-1, EPERM, + if it leads a process group already, as a program just forked + doesn't). *) +proc setsid*: INT; +begin + return SYSCALL(112); (* setsid *) +end; + (* Gives the processor to another thread. *) proc yield*; begin @@ -2132,6 +2152,30 @@ if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end; close(r); close(w); + + (* blocking: a fresh pipe waits, a non-blocking one doesn't, a closed fd is -9 *) + r, w := pipe(); + if (blocking(r) # 1) or (setBlocking(r, false) # 0) or (blocking(r) # 0) then exit(2); end; + if (setBlocking(r, true) # 0) or (blocking(r) # 1) then exit(2); end; + close(r); + close(w); + if blocking(r) # -9 then exit(2); end; + + (* setsid: a forked child leads a session of its own, once; a group leader can't *) + child := fork(); + if child = 0 then + if (setsid() # pid()) or (setsid() # -1) then exit(2); end; + exit(0); + end; + r, st, c := wait(child, true); + if (r # child) or (st # exited) or (c # 0) then exit(2); end; + child := fork(); + if child = 0 then + if (setGroup(0, 0) # 0) or (setsid() # -1) then exit(2); end; + exit(0); + end; + r, st, c := wait(child, true); + if (r # child) or (st # exited) or (c # 0) then exit(2); end; child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); r, st, c := wait(child, false); diff --git a/lib/Sys.raz b/lib/Sys.raz index ef47ab3..ce1e3ae 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -1110,6 +1110,17 @@ return 0; end; +(* Whether reads and writes on fd wait, as they do at first: 1 if they + do, 0 if they return -11 (EAGAIN) at once, or minus an errno. *) +proc blocking*(fd: INT): INT; +var flags: I32; +begin + flags := C.fcntl(I32(fd), CAbi.fGetfl, 0); + if flags < 0 then return failed(); end; + if flags & CAbi.oNonblock # 0 then return 0; end; + return 1; +end; + (* A stream socket connected to port at addr, as connectTcp makes it, or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds. It starts to connect without waiting, then waits for the socket to @@ -2003,6 +2014,18 @@ return INT(r); end; +(* A new session and process group, led by this program, with no + controlling terminal: its process id, or minus an errno (-1, EPERM, + if it leads a process group already, as a program just forked + doesn't). *) +proc setsid*: INT; +var r: I32; +begin + r := C.setsid(); + if r < 0 then return failed(); end; + return INT(r); +end; + (* Gives the processor to another thread. *) proc yield*; begin @@ -2073,6 +2096,30 @@ if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end; close(r); close(w); + + (* blocking: a fresh pipe waits, a non-blocking one doesn't, a closed fd is -9 *) + r, w := pipe(); + if (blocking(r) # 1) or (setBlocking(r, false) # 0) or (blocking(r) # 0) then exit(2); end; + if (setBlocking(r, true) # 0) or (blocking(r) # 1) then exit(2); end; + close(r); + close(w); + if blocking(r) # -9 then exit(2); end; + + (* setsid: a forked child leads a session of its own, once; a group leader can't *) + child := fork(); + if child = 0 then + if (setsid() # pid()) or (setsid() # -1) then exit(2); end; + exit(0); + end; + r, st, c := wait(child, true); + if (r # child) or (st # exited) or (c # 0) then exit(2); end; + child := fork(); + if child = 0 then + if (setGroup(0, 0) # 0) or (setsid() # -1) then exit(2); end; + exit(0); + end; + r, st, c := wait(child, true); + if (r # child) or (st # exited) or (c # 0) then exit(2); end; child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); r, st, c := wait(child, false);