raz / commit fdcb312c

commit
fdcb312cf9e231ebbbd5b7eebd339cce1932fba9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
24955285
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.

patch · log

4 files changed, +32 -4

PLAN.md (history) modified +3 -0

@@ -479,6 +479,9 @@
479479 files' tests: `connect` refused where nothing listens, and taken
480480 by a listener the test makes, which `readyOf` then finds ready;
481481 a byte across, and the end of the connection.
482+- [x] 49c. `send` (report 2026-10.7), at the owner's word: `write` to
483+ a socket with `MSG_NOSIGNAL`, -32 (EPIPE) where `write` would
484+ bring SIGPIPE. Both tests send to a connection closed.
482485 - [ ] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and
483486 `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its size;
484487 `fork`, then in the child `setsid`, the terminal opened as its

lib/C.raz (history) modified +1 -0

@@ -31,6 +31,7 @@
3131 extern proc poll*(fds: ^U8; n: U64; ms: I32): I32;
3232 extern proc socket*(domain, kind, protocol: I32): I32;
3333 extern proc connect*(fd: I32; addr: ^U8; len: U32): I32;
34+extern proc send*(fd: I32; buf: ^U8; n: U64; flags: I32): I64;
3435 extern proc bind*(fd: I32; addr: ^U8; len: U32): I32;
3536 extern proc listen*(fd, backlog: I32): I32;
3637 extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32;

lib/Sys.linux-x86_64.raz (history) modified +13 -2

@@ -247,6 +247,15 @@
247247 return fd;
248248 end;
249249
250+(* Writes s to the socket fd, as write does, but returns -32 (EPIPE)
251+ where write would stop the program with SIGPIPE: when the other end
252+ has closed it. *)
253+proc send*(fd: I64; s: []U8): I64;
254+begin
255+ if LEN(s) = 0 then return 0; end;
256+ return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *)
257+end;
258+
250259 (* The first of fds that has something to read within ms milliseconds,
251260 or that has reached its end or failed, so that a read won't wait; -1
252261 if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
@@ -321,7 +330,8 @@
321330 end;
322331
323332 (* The module's test: a file written, read back and closed; a
324- directory made, listed and emptied; no terminal; a socket. *)
333+ directory made, listed and emptied; no terminal; a socket, sent to
334+ after its end. *)
325335 proc main*;
326336 var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
327337 dir: [4096]U8;
@@ -379,10 +389,11 @@
379389 if readyOf([c], 0) # -1 then exit(2); end;
380390 if readyOf([c, l], 1000) # l then exit(2); end;
381391 a := SYSCALL(43, l, 0, 0); (* accept *)
382- if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
392+ if (a < 0) or (send(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
383393 if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end;
384394 close(a);
385395 if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *)
396+ if send(c, "x") # -32 then exit(2); end; (* EPIPE, and no SIGPIPE *)
386397 close(c);
387398 close(l);
388399 remove("/tmp/raz-sys-socket");

lib/Sys.raz (history) modified +15 -2

@@ -259,6 +259,18 @@
259259 return I64(fd);
260260 end;
261261
262+(* Writes s to the socket fd, as write does, but returns -32 (EPIPE)
263+ where write would stop the program with SIGPIPE: when the other end
264+ has closed it. *)
265+proc send*(fd: I64; s: []U8): I64;
266+var r: I64;
267+begin
268+ if LEN(s) = 0 then return 0; end;
269+ r := C.send(I32(fd), ADR(s[0]), U64(LEN(s)), 4000H); (* MSG_NOSIGNAL, glibc's *)
270+ if r < 0 then return failed(); end;
271+ return r;
272+end;
273+
262274 (* The first of fds that has something to read within ms milliseconds,
263275 or that has reached its end or failed, so that a read won't wait; -1
264276 if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
@@ -329,7 +341,7 @@
329341
330342 (* The module's test, as Linux's Sys has it: a file written, read back
331343 and closed; a directory made, listed and emptied; no terminal; a
332- socket. *)
344+ socket, sent to after its end. *)
333345 proc main*;
334346 var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
335347 dir: [4096]U8;
@@ -387,10 +399,11 @@
387399 if readyOf([c], 0) # -1 then exit(2); end;
388400 if readyOf([c, l], 1000) # l then exit(2); end;
389401 a := I64(C.accept(I32(l), nil, nil));
390- if (a < 0) or (write(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
402+ if (a < 0) or (send(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end;
391403 if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end;
392404 close(a);
393405 if (readyOf([c], 1000) # c) or (read(c, buf) # 0) then exit(2); end; (* its end *)
406+ if send(c, "x") # -32 then exit(2); end; (* EPIPE, and no SIGPIPE *)
394407 close(c);
395408 close(l);
396409 remove("/tmp/raz-sys-socket");