From fdcb312cf9e231ebbbd5b7eebd339cce1932fba9 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:22:00 +0100 Subject: [PATCH] Sys.send: writing to a socket without SIGPIPE Report 2026-10.7. On Linux sendto with MSG_NOSIGNAL; over C, send with glibc's MSG_NOSIGNAL. Both tests send to a connection whose other end has closed and get -32; write in its place dies of SIGPIPE. --- diff --git a/PLAN.md b/PLAN.md index 9d3b797..657794c 100644 --- a/PLAN.md +++ b/PLAN.md @@ -479,6 +479,9 @@ files' tests: `connect` refused where nothing listens, and taken by a listener the test makes, which `readyOf` then finds ready; a byte across, and the end of the connection. +- [x] 49c. `send` (report 2026-10.7), at the owner's word: `write` to + a socket with `MSG_NOSIGNAL`, -32 (EPIPE) where `write` would + bring SIGPIPE. Both tests send to a connection closed. - [ ] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its size; `fork`, then in the child `setsid`, the terminal opened as its diff --git a/lib/C.raz b/lib/C.raz index 48f05a2..cc4fd9b 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -31,6 +31,7 @@ extern proc poll*(fds: ^U8; n: U64; ms: I32): I32; extern proc socket*(domain, kind, protocol: I32): I32; extern proc connect*(fd: I32; addr: ^U8; len: U32): I32; +extern proc send*(fd: I32; buf: ^U8; n: U64; flags: I32): I64; extern proc bind*(fd: I32; addr: ^U8; len: U32): I32; extern proc listen*(fd, backlog: I32): I32; extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 1fc9b9e..c1334d0 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -247,6 +247,15 @@ return fd; end; +(* Writes s to the socket fd, as write does, but returns -32 (EPIPE) + where write would stop the program with SIGPIPE: when the other end + has closed it. *) +proc send*(fd: I64; s: []U8): I64; +begin + if LEN(s) = 0 then return 0; end; + return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *) +end; + (* The first of fds that has something to read within ms milliseconds, or that has reached its end or failed, so that a read won't wait; -1 if none has. ms < 0 waits as long as it takes. At most 64 fds. *) @@ -321,7 +330,8 @@ end; (* The module's test: a file written, read back and closed; a - directory made, listed and emptied; no terminal; a socket. *) + directory made, listed and emptied; no terminal; a socket, sent to + after its end. *) proc main*; var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; dir: [4096]U8; @@ -379,10 +389,11 @@ if readyOf([c], 0) # -1 then exit(2); end; if readyOf([c, l], 1000) # l then exit(2); end; a := SYSCALL(43, l, 0, 0); (* accept *) - if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; + if (a < 0) or (send(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; close(a); if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *) + if send(c, "x") # -32 then exit(2); end; (* EPIPE, and no SIGPIPE *) close(c); close(l); remove("/tmp/raz-sys-socket"); diff --git a/lib/Sys.raz b/lib/Sys.raz index be0686b..5e618f2 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -259,6 +259,18 @@ return I64(fd); end; +(* Writes s to the socket fd, as write does, but returns -32 (EPIPE) + where write would stop the program with SIGPIPE: when the other end + has closed it. *) +proc send*(fd: I64; s: []U8): I64; +var r: I64; +begin + if LEN(s) = 0 then return 0; end; + r := C.send(I32(fd), ADR(s[0]), U64(LEN(s)), 4000H); (* MSG_NOSIGNAL, glibc's *) + if r < 0 then return failed(); end; + return r; +end; + (* The first of fds that has something to read within ms milliseconds, or that has reached its end or failed, so that a read won't wait; -1 if none has. ms < 0 waits as long as it takes. At most 64 fds. *) @@ -329,7 +341,7 @@ (* The module's test, as Linux's Sys has it: a file written, read back and closed; a directory made, listed and emptied; no terminal; a - socket. *) + socket, sent to after its end. *) proc main*; var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; dir: [4096]U8; @@ -387,10 +399,11 @@ if readyOf([c], 0) # -1 then exit(2); end; if readyOf([c, l], 1000) # l then exit(2); end; a := I64(C.accept(I32(l), nil, nil)); - if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; + if (a < 0) or (send(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; close(a); if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *) + if send(c, "x") # -32 then exit(2); end; (* EPIPE, and no SIGPIPE *) close(c); close(l); remove("/tmp/raz-sys-socket");