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.
| @@ -479,6 +479,9 @@ | ||
| 479 | 479 | files' tests: `connect` refused where nothing listens, and taken |
| 480 | 480 | by a listener the test makes, which `readyOf` then finds ready; |
| 481 | 481 | 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. | |
| 482 | 485 | - [ ] 49b. The rest, for rat: `/dev/ptmx` with `TIOCSPTLCK` and |
| 483 | 486 | `TIOCGPTN` for the pseudo-terminal, `TIOCSWINSZ` for its size; |
| 484 | 487 | `fork`, then in the child `setsid`, the terminal opened as its |
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | extern proc poll*(fds: ^U8; n: U64; ms: I32): I32; |
| 32 | 32 | extern proc socket*(domain, kind, protocol: I32): I32; |
| 33 | 33 | extern proc connect*(fd: I32; addr: ^U8; len: U32): I32; |
| 34 | +extern proc send*(fd: I32; buf: ^U8; n: U64; flags: I32): I64; | |
| 34 | 35 | extern proc bind*(fd: I32; addr: ^U8; len: U32): I32; |
| 35 | 36 | extern proc listen*(fd, backlog: I32): I32; |
| 36 | 37 | extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; |
| @@ -247,6 +247,15 @@ | ||
| 247 | 247 | return fd; |
| 248 | 248 | end; |
| 249 | 249 | |
| 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 | + | |
| 250 | 259 | (* The first of fds that has something to read within ms milliseconds, |
| 251 | 260 | or that has reached its end or failed, so that a read won't wait; -1 |
| 252 | 261 | if none has. ms < 0 waits as long as it takes. At most 64 fds. *) |
| @@ -321,7 +330,8 @@ | ||
| 321 | 330 | end; |
| 322 | 331 | |
| 323 | 332 | (* 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. *) | |
| 325 | 335 | proc main*; |
| 326 | 336 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 327 | 337 | dir: [4096]U8; |
| @@ -379,10 +389,11 @@ | ||
| 379 | 389 | if readyOf([c], 0) # -1 then exit(2); end; |
| 380 | 390 | if readyOf([c, l], 1000) # l then exit(2); end; |
| 381 | 391 | 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; | |
| 383 | 393 | if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; |
| 384 | 394 | close(a); |
| 385 | 395 | 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 *) | |
| 386 | 397 | close(c); |
| 387 | 398 | close(l); |
| 388 | 399 | remove("/tmp/raz-sys-socket"); |
| @@ -259,6 +259,18 @@ | ||
| 259 | 259 | return I64(fd); |
| 260 | 260 | end; |
| 261 | 261 | |
| 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 | + | |
| 262 | 274 | (* The first of fds that has something to read within ms milliseconds, |
| 263 | 275 | or that has reached its end or failed, so that a read won't wait; -1 |
| 264 | 276 | if none has. ms < 0 waits as long as it takes. At most 64 fds. *) |
| @@ -329,7 +341,7 @@ | ||
| 329 | 341 | |
| 330 | 342 | (* The module's test, as Linux's Sys has it: a file written, read back |
| 331 | 343 | and closed; a directory made, listed and emptied; no terminal; a |
| 332 | - socket. *) | |
| 344 | + socket, sent to after its end. *) | |
| 333 | 345 | proc main*; |
| 334 | 346 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 335 | 347 | dir: [4096]U8; |
| @@ -387,10 +399,11 @@ | ||
| 387 | 399 | if readyOf([c], 0) # -1 then exit(2); end; |
| 388 | 400 | if readyOf([c, l], 1000) # l then exit(2); end; |
| 389 | 401 | 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; | |
| 391 | 403 | if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; |
| 392 | 404 | close(a); |
| 393 | 405 | 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 *) | |
| 394 | 407 | close(c); |
| 395 | 408 | close(l); |
| 396 | 409 | remove("/tmp/raz-sys-socket"); |