From 82f30b3cdbfde5385d9b8645deee860d0074f445 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:00:18 +0100 Subject: [PATCH] Sys: closeWrite, peer, socketPair, nonblocking and errConnAborted What a server needs: end this side's writing, say who is at the other end (Remote), make a pair of sockets, open a fifo without waiting, and name a client gone before accept. CAbi gains shutWr, soPeercred and struct ucred's fields, written again by tools/cabi.c. In both backends; tests in Sys.main. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 46175a8..bf2c478 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -208,12 +208,12 @@ errNotDir* = -20; errIsDir* = -21; errInvalid* = -22; errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32; errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40; - errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104; + errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104; errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113; errInProgress* = -115; type - Access* = (reading, writing, creating, exclusive, truncating, appending, executing); + Access* = (reading, writing, creating, exclusive, truncating, appending, executing, nonblocking); Accesses* = set of Access; Info* = record size*: I64; (* in bytes *) @@ -246,6 +246,13 @@ Status* = (running, exited, killed, stopped); Mode* = record ... end; (* a terminal's modes; its fields are the target's *) Peer* = record ... end; (* whom a datagram came from *) + Remote* = record (* who is at the other end of a connected socket *) + unix*: BOOL; (* a Unix socket: uid and pid say who *) + ipv6*: BOOL; (* addr is 16 bytes of IPv6, else 4 bytes of IPv4 then zeros *) + addr*: [16]U8; + port*: INT; + uid*, pid*: INT; + end; proc write*(fd: INT; s: []U8): INT; (* the bytes written *) proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) @@ -359,6 +366,9 @@ proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *) proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *) proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *) +proc closeWrite*(fd: INT): INT; (* no more writing to the socket fd; the other end reads an end *) +proc peer*(fd: INT): (Remote, INT); (* who is at the other end of the connected socket fd *) +proc socketPair*: (INT, INT, INT); (* two connected stream sockets, and 0 *) proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *) proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *) @@ -523,7 +533,11 @@ `access` says whether this user, by its real ids, may read, write or execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` alone can't say; an empty `how` asks only whether it is there. -`open` refuses `executing`, with -22 (EINVAL). `umask` sets the bits +`open` refuses `executing`, with -22 (EINVAL). `nonblocking` opens a file without waiting: a fifo opened to write +waits for a reader and a 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. `umask` sets the bits taken away from the mode of every file this program, and those it starts, make, and says what they were. `envAt` gives the environment one variable at a time, from 0, for a shell to list it and to pass it @@ -615,6 +629,18 @@ datagrams from anyone, as a name server has. `receive` gives the next datagram, at most `LEN(buf)` bytes of it, the rest lost, and a `Peer` that `reply` sends an answer to. + +`closeWrite` ends this side's writing to a socket, so that the other +end reads an end of input and can still answer on the same socket: a +client sends its request, says it is done, and reads the reply. `peer` +says who is at the other end of a connected socket: for a TCP one its +address and port; for a Unix one the user and process that made the +connection, as the system recorded them, so that a server can check +that its client is the same user instead of trusting a directory's +mode. `socketPair` makes two connected stream sockets at once, as a +`listen`, `connect` and `accept` would, with no name on disk. Each is +closed in a program exec starts. `accept` gives -103 (`errConnAborted`) +for a client that went before it was taken. **Passing files and sharing memory.** A program can give another an open file over a Unix socket: `sendFds` sends `s`, at least one byte, diff --git a/lib/C.raz b/lib/C.raz index d524242..5ad2cdf 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -44,6 +44,9 @@ extern proc accept4*(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32; extern proc setsockopt*(fd, level, name: I32; value: ^U8; len: U32): I32; extern proc getsockopt*(fd, level, name: I32; value: ^U8; len: ^U32): I32; +extern proc getpeername*(fd: I32; addr: ^U8; len: ^U32): I32; +extern proc shutdown*(fd, how: I32): I32; +extern proc socketpair*(domain, kind, protocol: I32; fds: ^I32): I32; extern proc getsockname*(fd: I32; addr: ^U8; len: ^U32): I32; extern proc recvfrom*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: ^U32): INT; extern proc sendto*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: U32): INT; diff --git a/lib/CAbi.linux-x86_64.raz b/lib/CAbi.linux-x86_64.raz index e974473..177f7c8 100644 --- a/lib/CAbi.linux-x86_64.raz +++ b/lib/CAbi.linux-x86_64.raz @@ -97,6 +97,8 @@ solSocket* = 1; soReuseaddr* = 2; soError* = 4; + soPeercred* = 17; + shutWr* = 1; msgNosignal* = 16384; msgCmsgCloexec* = 1073741824; scmRights* = 1; @@ -113,6 +115,10 @@ sockaddrIn6Len* = 28; sin6PortAt* = 2; sin6AddrAt* = 8; + ucPidAt* = 0; + ucPidLen* = 4; + ucUidAt* = 4; + ucUidLen* = 4; msghdrLen* = 56; msgIovAt* = 16; msgIovlenAt* = 24; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 1d2b5a8..f56711e 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -30,15 +30,16 @@ errNotDir* = -20; errIsDir* = -21; errInvalid* = -22; errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32; errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40; - errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104; + errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104; errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113; errInProgress* = -115; type (* What open is asked for: reading, writing or both, and what else - it does to the file. *) + it does to the file; nonblocking, that open doesn't wait (for a + fifo's other end) and reads and writes on the file don't either. *) Access* = (reading, writing, creating, exclusive, truncating, - appending, executing); + appending, executing, nonblocking); Accesses* = set of Access; (* what open is given *) (* What info says of a file. *) @@ -118,6 +119,14 @@ a: Sockaddr; size: INT; end; + (* Who is at the other end of a connected socket, as peer says. *) + Remote* = record + unix*: BOOL; (* a Unix socket: uid and pid say who; no addr or port *) + ipv6*: BOOL; (* addr is an IPv6 address, 16 bytes; else an IPv4 one, 4 bytes then zeros *) + addr*: [16]U8; (* an IP peer's address *) + port*: INT; (* an IP peer's port *) + uid*, pid*: INT; (* a Unix peer's user and process, as the system recorded them when it connected *) + end; (* struct msghdr, what sendmsg and recvmsg take: no address, one buffer, and a control message; the sizes and flags are ints with room after them, kept zero *) @@ -197,6 +206,7 @@ if exclusive in how then f := f | 80H; end; if truncating in how then f := f | 200H; end; if appending in how then f := f | 400H; end; + if nonblocking in how then f := f | 800H; end; (* O_NONBLOCK *) return f; end; @@ -920,6 +930,53 @@ return SYSCALL(288, fd, 0, 0, 80000H); (* accept4, SOCK_CLOEXEC *) end; +(* Ends this side's writing to the socket fd, so that the other end + reads an end of input and can still answer on it. 0, or minus an + errno. *) +proc closeWrite*(fd: INT): INT; +begin + return SYSCALL(48, fd, 1); (* shutdown, SHUT_WR *) +end; + +(* Who is at the other end of the connected socket fd, and 0; or minus + an errno. *) +proc peer*(fd: INT): (Remote, INT); +var r: Remote; a: Sockaddr; size, credSize: I32; cred: [3]I32; e: INT; +begin + r := {unix: false, ipv6: false}; + size := I32(SIZE(Sockaddr)); + e := SYSCALL(52, fd, ADR(a), ADR(size)); (* getpeername *) + if e < 0 then return r, e; end; + if a.family = 1 then (* AF_UNIX: ask who made the connection *) + credSize := I32(4 * LEN(cred)); + e := SYSCALL(55, fd, 1, 17, ADR(cred), ADR(credSize)); (* getsockopt: SOL_SOCKET, SO_PEERCRED *) + if e < 0 then return r, e; end; + r.unix := true; + r.pid := INT(cred[0]); + r.uid := INT(cred[1]); + else + r.port := (INT(a.port[0]) shl 8) | INT(a.port[1]); + if a.family = 10 then (* AF_INET6 *) + r.ipv6 := true; + COPY(r.addr[0..16], a.data[4..20]); + else + COPY(r.addr[0..4], a.data[0..4]); + end; + end; + return r, 0; +end; + +(* Two connected stream sockets, as a Unix socket and the one that + connects to it make: their fds and 0, closed in a program exec + starts; or -1, -1 and minus an errno. *) +proc socketPair*: (INT, INT, INT); +var fds: [2]I32; r: INT; +begin + r := SYSCALL(53, 1, 1 | 80000H, 0, ADR(fds)); (* socketpair: AF_UNIX, SOCK_STREAM, SOCK_CLOEXEC *) + if r < 0 then return -1, -1, r; end; + return INT(fds[0]), INT(fds[1]), 0; +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. *) @@ -2300,6 +2357,47 @@ if processors() < 1 then exit(2); end; end; +(* The test of what a server needs: a pair of sockets, one end told + it has no more to read, who is at the other end of a Unix socket and + of a TCP one, and a fifo opened without waiting. *) +proc testServer; +var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; +begin + a, b, r := socketPair(); + if (r # 0) or (send(a, "hi") # 2) or (read(b, buf) # 2) or (buf[0..2] # "hi") then exit(2); end; + if (closeWrite(a) # 0) or (read(b, buf) # 0) then exit(2); end; (* b reads an end, *) + if (send(b, "yo") # 2) or (read(a, buf) # 2) or (buf[0..2] # "yo") then exit(2); end; (* and can still answer *) + w, r := peer(a); + if (r # 0) or not w.unix or (w.pid # pid()) or (w.uid # uid()) or (w.port # 0) or (w.addr[0] # 0) then exit(2); end; + close(a); + close(b); + _, r := peer(-1); + if r # errBadFd then exit(2); end; + l := listenTcp("127.0.0.1", 0); + port := localPort(l); + c := connectTcp("127.0.0.1", port); + s := accept(l); + if (l < 0) or (c < 0) or (s < 0) then exit(2); end; + w, r := peer(c); + if (r # 0) or w.unix or w.ipv6 or (w.port # port) or (w.addr[0..4] # "\x7f\x00\x00\x01") then exit(2); end; + w, r := peer(s); + if (r # 0) or (w.port = port) or (w.addr[3] # 1) or (w.addr[4] # 0) then exit(2); end; + close(s); + close(c); + close(l); + _ := remove("/tmp/raz-sys-fifo"); + child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); + r, st, how := waitExit(child); + if (st # exited) or (how # 0) then exit(2); end; + if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) + a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); + b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); + if (a < 0) or (b < 0) or (read(a, buf) # errAgain) then exit(2); end; (* nothing to read, and it didn't wait *) + close(a); + close(b); + _ := remove("/tmp/raz-sys-fifo"); +end; + (* The module's test: a file written, read back and closed; a directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) @@ -2527,6 +2625,7 @@ testShell; testNet; testProcess; + testServer; end; end Sys. diff --git a/lib/Sys.raz b/lib/Sys.raz index 8e7fd00..a8417b2 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -47,15 +47,16 @@ errNotDir* = -20; errIsDir* = -21; errInvalid* = -22; errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32; errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40; - errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104; + errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104; errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113; errInProgress* = -115; type (* What open is asked for: reading, writing or both, and what else - it does to the file. *) + it does to the file; nonblocking, that open doesn't wait (for a + fifo's other end) and reads and writes on the file don't either. *) Access* = (reading, writing, creating, exclusive, truncating, - appending, executing); + appending, executing, nonblocking); Accesses* = set of Access; (* what open is given *) (* What info says of a file. *) @@ -127,6 +128,14 @@ a: Sockaddr; size: INT; end; + (* Who is at the other end of a connected socket, as peer says. *) + Remote* = record + unix*: BOOL; (* a Unix socket: uid and pid say who; no addr or port *) + ipv6*: BOOL; (* addr is an IPv6 address, 16 bytes; else an IPv4 one, 4 bytes then zeros *) + addr*: [16]U8; (* an IP peer's address *) + port*: INT; (* an IP peer's port *) + uid*, pid*: INT; (* a Unix peer's user and process, as the system recorded them when it connected *) + end; (* struct msghdr, what sendmsg and recvmsg take, as bytes: no address, one buffer, and a control message, a struct cmsghdr and the fds sent, at most 16 *) @@ -238,6 +247,7 @@ if exclusive in how then f := f | CAbi.oExcl; end; if truncating in how then f := f | CAbi.oTrunc; end; if appending in how then f := f | CAbi.oAppend; end; + if nonblocking in how then f := f | CAbi.oNonblock; end; return f; end; @@ -958,8 +968,58 @@ r := C.accept4(I32(fd), nil, nil, CAbi.sockCloexec); if r < 0 then return failed(); end; return INT(r); +end; + +(* Ends this side's writing to the socket fd, so that the other end + reads an end of input and can still answer on it. 0, or minus an + errno. *) +proc closeWrite*(fd: INT): INT; +begin + if C.shutdown(I32(fd), CAbi.shutWr) # 0 then return failed(); end; + return 0; +end; + +(* Who is at the other end of the connected socket fd, and 0; or minus + an errno. *) +proc peer*(fd: INT): (Remote, INT); +var r: Remote; a: Sockaddr; size, credSize: U32; cred: [CAbi.ucUidAt + CAbi.ucUidLen]U8; p: ^U8; +begin + r := {unix: false, ipv6: false}; + size := U32(SIZE(Sockaddr)); + p := ^U8(ADR(a)); + if C.getpeername(I32(fd), p, ADR(size)) # 0 then return r, failed(); end; + if familyOf(p) = CAbi.afUnix then (* ask who made the connection *) + credSize := U32(LEN(cred)); + if C.getsockopt(I32(fd), CAbi.solSocket, CAbi.soPeercred, ADR(cred[0]), ADR(credSize)) # 0 then + return r, failed(); + end; + r.unix := true; + r.pid := INT(field(ADR(cred[0]), CAbi.ucPidAt, CAbi.ucPidLen)); + r.uid := INT(field(ADR(cred[0]), CAbi.ucUidAt, CAbi.ucUidLen)); + else + r.port := portOf(p); + if familyOf(p) = CAbi.afInet6 then + r.ipv6 := true; + COPY(r.addr[0..16], p[CAbi.sin6AddrAt..CAbi.sin6AddrAt + 16]); + else + COPY(r.addr[0..4], p[CAbi.sinAddrAt..CAbi.sinAddrAt + 4]); + end; + end; + return r, 0; end; +(* Two connected stream sockets, as a Unix socket and the one that + connects to it make: their fds and 0, closed in a program exec + starts; or -1, -1 and minus an errno. *) +proc socketPair*: (INT, INT, INT); +var fds: [2]I32; +begin + if C.socketpair(CAbi.afUnix, CAbi.sockStream | CAbi.sockCloexec, 0, ADR(fds[0])) # 0 then + return -1, -1, failed(); + end; + return INT(fds[0]), INT(fds[1]), 0; +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. *) @@ -2242,6 +2302,47 @@ if processors() < 1 then exit(2); end; end; +(* The test of what a server needs: a pair of sockets, one end told + it has no more to read, who is at the other end of a Unix socket and + of a TCP one, and a fifo opened without waiting. *) +proc testServer; +var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; +begin + a, b, r := socketPair(); + if (r # 0) or (send(a, "hi") # 2) or (read(b, buf) # 2) or (buf[0..2] # "hi") then exit(2); end; + if (closeWrite(a) # 0) or (read(b, buf) # 0) then exit(2); end; (* b reads an end, *) + if (send(b, "yo") # 2) or (read(a, buf) # 2) or (buf[0..2] # "yo") then exit(2); end; (* and can still answer *) + w, r := peer(a); + if (r # 0) or not w.unix or (w.pid # pid()) or (w.uid # uid()) or (w.port # 0) or (w.addr[0] # 0) then exit(2); end; + close(a); + close(b); + _, r := peer(-1); + if r # errBadFd then exit(2); end; + l := listenTcp("127.0.0.1", 0); + port := localPort(l); + c := connectTcp("127.0.0.1", port); + s := accept(l); + if (l < 0) or (c < 0) or (s < 0) then exit(2); end; + w, r := peer(c); + if (r # 0) or w.unix or w.ipv6 or (w.port # port) or (w.addr[0..4] # "\x7f\x00\x00\x01") then exit(2); end; + w, r := peer(s); + if (r # 0) or (w.port = port) or (w.addr[3] # 1) or (w.addr[4] # 0) then exit(2); end; + close(s); + close(c); + close(l); + _ := remove("/tmp/raz-sys-fifo"); + child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); + r, st, how := waitExit(child); + if (st # exited) or (how # 0) then exit(2); end; + if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) + a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); + b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); + if (a < 0) or (b < 0) or (read(a, buf) # errAgain) then exit(2); end; (* nothing to read, and it didn't wait *) + close(a); + close(b); + _ := remove("/tmp/raz-sys-fifo"); +end; + (* 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, sent to after its end; programs; time and the network. *) @@ -2468,6 +2569,7 @@ testShell; testNet; testProcess; + testServer; end; end Sys. diff --git a/tools/cabi.c b/tools/cabi.c index 3d1ae34..a3591ac 100644 --- a/tools/cabi.c +++ b/tools/cabi.c @@ -151,7 +151,8 @@ K("sockStream", SOCK_STREAM); K("sockDgram", SOCK_DGRAM); K("sockCloexec", SOCK_CLOEXEC); K("sockNonblock", SOCK_NONBLOCK); K("solSocket", SOL_SOCKET); K("soReuseaddr", SO_REUSEADDR); - K("soError", SO_ERROR); K("msgNosignal", MSG_NOSIGNAL); + K("soError", SO_ERROR); K("soPeercred", SO_PEERCRED); /* Linux's */ + K("shutWr", SHUT_WR); K("msgNosignal", MSG_NOSIGNAL); K("msgCmsgCloexec", MSG_CMSG_CLOEXEC); K("scmRights", SCM_RIGHTS); K("sockaddrLen", sizeof(struct sockaddr_storage)); READ("saFamily", struct sockaddr, sa_family); @@ -168,6 +169,7 @@ K("sockaddrIn6Len", sizeof(struct sockaddr_in6)); AT("sin6Port", struct sockaddr_in6, sin6_port); AT("sin6Addr", struct sockaddr_in6, sin6_addr); + READ("ucPid", struct ucred, pid); READ("ucUid", struct ucred, uid); K("msghdrLen", sizeof(struct msghdr)); AT("msgIov", struct msghdr, msg_iov); WRITE("msgIovlen", struct msghdr, msg_iovlen, msg_control);