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.
| @@ -208,12 +208,12 @@ | ||
| 208 | 208 | errNotDir* = -20; errIsDir* = -21; errInvalid* = -22; |
| 209 | 209 | errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32; |
| 210 | 210 | errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40; |
| 211 | - errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104; | |
| 211 | + errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104; | |
| 212 | 212 | errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113; |
| 213 | 213 | errInProgress* = -115; |
| 214 | 214 | |
| 215 | 215 | type |
| 216 | - Access* = (reading, writing, creating, exclusive, truncating, appending, executing); | |
| 216 | + Access* = (reading, writing, creating, exclusive, truncating, appending, executing, nonblocking); | |
| 217 | 217 | Accesses* = set of Access; |
| 218 | 218 | Info* = record |
| 219 | 219 | size*: I64; (* in bytes *) |
| @@ -246,6 +246,13 @@ | ||
| 246 | 246 | Status* = (running, exited, killed, stopped); |
| 247 | 247 | Mode* = record ... end; (* a terminal's modes; its fields are the target's *) |
| 248 | 248 | Peer* = record ... end; (* whom a datagram came from *) |
| 249 | + Remote* = record (* who is at the other end of a connected socket *) | |
| 250 | + unix*: BOOL; (* a Unix socket: uid and pid say who *) | |
| 251 | + ipv6*: BOOL; (* addr is 16 bytes of IPv6, else 4 bytes of IPv4 then zeros *) | |
| 252 | + addr*: [16]U8; | |
| 253 | + port*: INT; | |
| 254 | + uid*, pid*: INT; | |
| 255 | + end; | |
| 249 | 256 | |
| 250 | 257 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 251 | 258 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| @@ -359,6 +366,9 @@ | ||
| 359 | 366 | proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *) |
| 360 | 367 | proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *) |
| 361 | 368 | proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *) |
| 369 | +proc closeWrite*(fd: INT): INT; (* no more writing to the socket fd; the other end reads an end *) | |
| 370 | +proc peer*(fd: INT): (Remote, INT); (* who is at the other end of the connected socket fd *) | |
| 371 | +proc socketPair*: (INT, INT, INT); (* two connected stream sockets, and 0 *) | |
| 362 | 372 | proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *) |
| 363 | 373 | proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *) |
| 364 | 374 | |
| @@ -523,7 +533,11 @@ | ||
| 523 | 533 | `access` says whether this user, by its real ids, may read, write or |
| 524 | 534 | execute a file, as `test -r`, `-w` and `-x` ask, which `Info.mode` |
| 525 | 535 | alone can't say; an empty `how` asks only whether it is there. |
| 526 | -`open` refuses `executing`, with -22 (EINVAL). `umask` sets the bits | |
| 536 | +`open` refuses `executing`, with -22 (EINVAL). `nonblocking` opens a file without waiting: a fifo opened to write | |
| 537 | +waits for a reader and a fifo opened to read waits for a writer, but | |
| 538 | +with `nonblocking` the open returns at once (-6, ENXIO, for a write | |
| 539 | +with no reader), and a read or write that would wait is -11 later, as | |
| 540 | +`setBlocking(fd, false)` makes it. `umask` sets the bits | |
| 527 | 541 | taken away from the mode of every file this program, and those it |
| 528 | 542 | starts, make, and says what they were. `envAt` gives the environment |
| 529 | 543 | one variable at a time, from 0, for a shell to list it and to pass it |
| @@ -615,6 +629,18 @@ | ||
| 615 | 629 | datagrams from anyone, as a name server has. `receive` gives the next |
| 616 | 630 | datagram, at most `LEN(buf)` bytes of it, the rest lost, and a `Peer` |
| 617 | 631 | that `reply` sends an answer to. |
| 632 | + | |
| 633 | +`closeWrite` ends this side's writing to a socket, so that the other | |
| 634 | +end reads an end of input and can still answer on the same socket: a | |
| 635 | +client sends its request, says it is done, and reads the reply. `peer` | |
| 636 | +says who is at the other end of a connected socket: for a TCP one its | |
| 637 | +address and port; for a Unix one the user and process that made the | |
| 638 | +connection, as the system recorded them, so that a server can check | |
| 639 | +that its client is the same user instead of trusting a directory's | |
| 640 | +mode. `socketPair` makes two connected stream sockets at once, as a | |
| 641 | +`listen`, `connect` and `accept` would, with no name on disk. Each is | |
| 642 | +closed in a program exec starts. `accept` gives -103 (`errConnAborted`) | |
| 643 | +for a client that went before it was taken. | |
| 618 | 644 | |
| 619 | 645 | **Passing files and sharing memory.** A program can give another an |
| 620 | 646 | open file over a Unix socket: `sendFds` sends `s`, at least one byte, |
| @@ -44,6 +44,9 @@ | ||
| 44 | 44 | extern proc accept4*(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32; |
| 45 | 45 | extern proc setsockopt*(fd, level, name: I32; value: ^U8; len: U32): I32; |
| 46 | 46 | extern proc getsockopt*(fd, level, name: I32; value: ^U8; len: ^U32): I32; |
| 47 | +extern proc getpeername*(fd: I32; addr: ^U8; len: ^U32): I32; | |
| 48 | +extern proc shutdown*(fd, how: I32): I32; | |
| 49 | +extern proc socketpair*(domain, kind, protocol: I32; fds: ^I32): I32; | |
| 47 | 50 | extern proc getsockname*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 48 | 51 | extern proc recvfrom*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: ^U32): INT; |
| 49 | 52 | extern proc sendto*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: U32): INT; |
| @@ -97,6 +97,8 @@ | ||
| 97 | 97 | solSocket* = 1; |
| 98 | 98 | soReuseaddr* = 2; |
| 99 | 99 | soError* = 4; |
| 100 | + soPeercred* = 17; | |
| 101 | + shutWr* = 1; | |
| 100 | 102 | msgNosignal* = 16384; |
| 101 | 103 | msgCmsgCloexec* = 1073741824; |
| 102 | 104 | scmRights* = 1; |
| @@ -113,6 +115,10 @@ | ||
| 113 | 115 | sockaddrIn6Len* = 28; |
| 114 | 116 | sin6PortAt* = 2; |
| 115 | 117 | sin6AddrAt* = 8; |
| 118 | + ucPidAt* = 0; | |
| 119 | + ucPidLen* = 4; | |
| 120 | + ucUidAt* = 4; | |
| 121 | + ucUidLen* = 4; | |
| 116 | 122 | msghdrLen* = 56; |
| 117 | 123 | msgIovAt* = 16; |
| 118 | 124 | msgIovlenAt* = 24; |
| @@ -30,15 +30,16 @@ | ||
| 30 | 30 | errNotDir* = -20; errIsDir* = -21; errInvalid* = -22; |
| 31 | 31 | errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32; |
| 32 | 32 | errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40; |
| 33 | - errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104; | |
| 33 | + errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104; | |
| 34 | 34 | errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113; |
| 35 | 35 | errInProgress* = -115; |
| 36 | 36 | |
| 37 | 37 | type |
| 38 | 38 | (* What open is asked for: reading, writing or both, and what else |
| 39 | - it does to the file. *) | |
| 39 | + it does to the file; nonblocking, that open doesn't wait (for a | |
| 40 | + fifo's other end) and reads and writes on the file don't either. *) | |
| 40 | 41 | Access* = (reading, writing, creating, exclusive, truncating, |
| 41 | - appending, executing); | |
| 42 | + appending, executing, nonblocking); | |
| 42 | 43 | Accesses* = set of Access; (* what open is given *) |
| 43 | 44 | |
| 44 | 45 | (* What info says of a file. *) |
| @@ -118,6 +119,14 @@ | ||
| 118 | 119 | a: Sockaddr; |
| 119 | 120 | size: INT; |
| 120 | 121 | end; |
| 122 | + (* Who is at the other end of a connected socket, as peer says. *) | |
| 123 | + Remote* = record | |
| 124 | + unix*: BOOL; (* a Unix socket: uid and pid say who; no addr or port *) | |
| 125 | + ipv6*: BOOL; (* addr is an IPv6 address, 16 bytes; else an IPv4 one, 4 bytes then zeros *) | |
| 126 | + addr*: [16]U8; (* an IP peer's address *) | |
| 127 | + port*: INT; (* an IP peer's port *) | |
| 128 | + uid*, pid*: INT; (* a Unix peer's user and process, as the system recorded them when it connected *) | |
| 129 | + end; | |
| 121 | 130 | (* struct msghdr, what sendmsg and recvmsg take: no address, one |
| 122 | 131 | buffer, and a control message; the sizes and flags are ints |
| 123 | 132 | with room after them, kept zero *) |
| @@ -197,6 +206,7 @@ | ||
| 197 | 206 | if exclusive in how then f := f | 80H; end; |
| 198 | 207 | if truncating in how then f := f | 200H; end; |
| 199 | 208 | if appending in how then f := f | 400H; end; |
| 209 | + if nonblocking in how then f := f | 800H; end; (* O_NONBLOCK *) | |
| 200 | 210 | return f; |
| 201 | 211 | end; |
| 202 | 212 | |
| @@ -920,6 +930,53 @@ | ||
| 920 | 930 | return SYSCALL(288, fd, 0, 0, 80000H); (* accept4, SOCK_CLOEXEC *) |
| 921 | 931 | end; |
| 922 | 932 | |
| 933 | +(* Ends this side's writing to the socket fd, so that the other end | |
| 934 | + reads an end of input and can still answer on it. 0, or minus an | |
| 935 | + errno. *) | |
| 936 | +proc closeWrite*(fd: INT): INT; | |
| 937 | +begin | |
| 938 | + return SYSCALL(48, fd, 1); (* shutdown, SHUT_WR *) | |
| 939 | +end; | |
| 940 | + | |
| 941 | +(* Who is at the other end of the connected socket fd, and 0; or minus | |
| 942 | + an errno. *) | |
| 943 | +proc peer*(fd: INT): (Remote, INT); | |
| 944 | +var r: Remote; a: Sockaddr; size, credSize: I32; cred: [3]I32; e: INT; | |
| 945 | +begin | |
| 946 | + r := {unix: false, ipv6: false}; | |
| 947 | + size := I32(SIZE(Sockaddr)); | |
| 948 | + e := SYSCALL(52, fd, ADR(a), ADR(size)); (* getpeername *) | |
| 949 | + if e < 0 then return r, e; end; | |
| 950 | + if a.family = 1 then (* AF_UNIX: ask who made the connection *) | |
| 951 | + credSize := I32(4 * LEN(cred)); | |
| 952 | + e := SYSCALL(55, fd, 1, 17, ADR(cred), ADR(credSize)); (* getsockopt: SOL_SOCKET, SO_PEERCRED *) | |
| 953 | + if e < 0 then return r, e; end; | |
| 954 | + r.unix := true; | |
| 955 | + r.pid := INT(cred[0]); | |
| 956 | + r.uid := INT(cred[1]); | |
| 957 | + else | |
| 958 | + r.port := (INT(a.port[0]) shl 8) | INT(a.port[1]); | |
| 959 | + if a.family = 10 then (* AF_INET6 *) | |
| 960 | + r.ipv6 := true; | |
| 961 | + COPY(r.addr[0..16], a.data[4..20]); | |
| 962 | + else | |
| 963 | + COPY(r.addr[0..4], a.data[0..4]); | |
| 964 | + end; | |
| 965 | + end; | |
| 966 | + return r, 0; | |
| 967 | +end; | |
| 968 | + | |
| 969 | +(* Two connected stream sockets, as a Unix socket and the one that | |
| 970 | + connects to it make: their fds and 0, closed in a program exec | |
| 971 | + starts; or -1, -1 and minus an errno. *) | |
| 972 | +proc socketPair*: (INT, INT, INT); | |
| 973 | +var fds: [2]I32; r: INT; | |
| 974 | +begin | |
| 975 | + r := SYSCALL(53, 1, 1 | 80000H, 0, ADR(fds)); (* socketpair: AF_UNIX, SOCK_STREAM, SOCK_CLOEXEC *) | |
| 976 | + if r < 0 then return -1, -1, r; end; | |
| 977 | + return INT(fds[0]), INT(fds[1]), 0; | |
| 978 | +end; | |
| 979 | + | |
| 923 | 980 | (* Writes s to the socket fd, as write does, but returns -32 (EPIPE) |
| 924 | 981 | where write would stop the program with SIGPIPE: when the other end |
| 925 | 982 | has closed it. *) |
| @@ -2300,6 +2357,47 @@ | ||
| 2300 | 2357 | if processors() < 1 then exit(2); end; |
| 2301 | 2358 | end; |
| 2302 | 2359 | |
| 2360 | +(* The test of what a server needs: a pair of sockets, one end told | |
| 2361 | + it has no more to read, who is at the other end of a Unix socket and | |
| 2362 | + of a TCP one, and a fifo opened without waiting. *) | |
| 2363 | +proc testServer; | |
| 2364 | +var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; | |
| 2365 | +begin | |
| 2366 | + a, b, r := socketPair(); | |
| 2367 | + if (r # 0) or (send(a, "hi") # 2) or (read(b, buf) # 2) or (buf[0..2] # "hi") then exit(2); end; | |
| 2368 | + if (closeWrite(a) # 0) or (read(b, buf) # 0) then exit(2); end; (* b reads an end, *) | |
| 2369 | + if (send(b, "yo") # 2) or (read(a, buf) # 2) or (buf[0..2] # "yo") then exit(2); end; (* and can still answer *) | |
| 2370 | + w, r := peer(a); | |
| 2371 | + 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; | |
| 2372 | + close(a); | |
| 2373 | + close(b); | |
| 2374 | + _, r := peer(-1); | |
| 2375 | + if r # errBadFd then exit(2); end; | |
| 2376 | + l := listenTcp("127.0.0.1", 0); | |
| 2377 | + port := localPort(l); | |
| 2378 | + c := connectTcp("127.0.0.1", port); | |
| 2379 | + s := accept(l); | |
| 2380 | + if (l < 0) or (c < 0) or (s < 0) then exit(2); end; | |
| 2381 | + w, r := peer(c); | |
| 2382 | + 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; | |
| 2383 | + w, r := peer(s); | |
| 2384 | + if (r # 0) or (w.port = port) or (w.addr[3] # 1) or (w.addr[4] # 0) then exit(2); end; | |
| 2385 | + close(s); | |
| 2386 | + close(c); | |
| 2387 | + close(l); | |
| 2388 | + _ := remove("/tmp/raz-sys-fifo"); | |
| 2389 | + child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); | |
| 2390 | + r, st, how := waitExit(child); | |
| 2391 | + if (st # exited) or (how # 0) then exit(2); end; | |
| 2392 | + if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) | |
| 2393 | + a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); | |
| 2394 | + b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); | |
| 2395 | + if (a < 0) or (b < 0) or (read(a, buf) # errAgain) then exit(2); end; (* nothing to read, and it didn't wait *) | |
| 2396 | + close(a); | |
| 2397 | + close(b); | |
| 2398 | + _ := remove("/tmp/raz-sys-fifo"); | |
| 2399 | +end; | |
| 2400 | + | |
| 2303 | 2401 | (* The module's test: a file written, read back and closed; a |
| 2304 | 2402 | directory made, listed and emptied; no terminal; a socket, sent to |
| 2305 | 2403 | after its end; programs; time and the network. *) |
| @@ -2527,6 +2625,7 @@ | ||
| 2527 | 2625 | testShell; |
| 2528 | 2626 | testNet; |
| 2529 | 2627 | testProcess; |
| 2628 | + testServer; | |
| 2530 | 2629 | end; |
| 2531 | 2630 | |
| 2532 | 2631 | end Sys. |
| @@ -47,15 +47,16 @@ | ||
| 47 | 47 | errNotDir* = -20; errIsDir* = -21; errInvalid* = -22; |
| 48 | 48 | errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32; |
| 49 | 49 | errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40; |
| 50 | - errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104; | |
| 50 | + errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104; | |
| 51 | 51 | errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113; |
| 52 | 52 | errInProgress* = -115; |
| 53 | 53 | |
| 54 | 54 | type |
| 55 | 55 | (* What open is asked for: reading, writing or both, and what else |
| 56 | - it does to the file. *) | |
| 56 | + it does to the file; nonblocking, that open doesn't wait (for a | |
| 57 | + fifo's other end) and reads and writes on the file don't either. *) | |
| 57 | 58 | Access* = (reading, writing, creating, exclusive, truncating, |
| 58 | - appending, executing); | |
| 59 | + appending, executing, nonblocking); | |
| 59 | 60 | Accesses* = set of Access; (* what open is given *) |
| 60 | 61 | |
| 61 | 62 | (* What info says of a file. *) |
| @@ -127,6 +128,14 @@ | ||
| 127 | 128 | a: Sockaddr; |
| 128 | 129 | size: INT; |
| 129 | 130 | end; |
| 131 | + (* Who is at the other end of a connected socket, as peer says. *) | |
| 132 | + Remote* = record | |
| 133 | + unix*: BOOL; (* a Unix socket: uid and pid say who; no addr or port *) | |
| 134 | + ipv6*: BOOL; (* addr is an IPv6 address, 16 bytes; else an IPv4 one, 4 bytes then zeros *) | |
| 135 | + addr*: [16]U8; (* an IP peer's address *) | |
| 136 | + port*: INT; (* an IP peer's port *) | |
| 137 | + uid*, pid*: INT; (* a Unix peer's user and process, as the system recorded them when it connected *) | |
| 138 | + end; | |
| 130 | 139 | (* struct msghdr, what sendmsg and recvmsg take, as bytes: no |
| 131 | 140 | address, one buffer, and a control message, a struct cmsghdr |
| 132 | 141 | and the fds sent, at most 16 *) |
| @@ -238,6 +247,7 @@ | ||
| 238 | 247 | if exclusive in how then f := f | CAbi.oExcl; end; |
| 239 | 248 | if truncating in how then f := f | CAbi.oTrunc; end; |
| 240 | 249 | if appending in how then f := f | CAbi.oAppend; end; |
| 250 | + if nonblocking in how then f := f | CAbi.oNonblock; end; | |
| 241 | 251 | return f; |
| 242 | 252 | end; |
| 243 | 253 | |
| @@ -958,8 +968,58 @@ | ||
| 958 | 968 | r := C.accept4(I32(fd), nil, nil, CAbi.sockCloexec); |
| 959 | 969 | if r < 0 then return failed(); end; |
| 960 | 970 | return INT(r); |
| 971 | +end; | |
| 972 | + | |
| 973 | +(* Ends this side's writing to the socket fd, so that the other end | |
| 974 | + reads an end of input and can still answer on it. 0, or minus an | |
| 975 | + errno. *) | |
| 976 | +proc closeWrite*(fd: INT): INT; | |
| 977 | +begin | |
| 978 | + if C.shutdown(I32(fd), CAbi.shutWr) # 0 then return failed(); end; | |
| 979 | + return 0; | |
| 980 | +end; | |
| 981 | + | |
| 982 | +(* Who is at the other end of the connected socket fd, and 0; or minus | |
| 983 | + an errno. *) | |
| 984 | +proc peer*(fd: INT): (Remote, INT); | |
| 985 | +var r: Remote; a: Sockaddr; size, credSize: U32; cred: [CAbi.ucUidAt + CAbi.ucUidLen]U8; p: ^U8; | |
| 986 | +begin | |
| 987 | + r := {unix: false, ipv6: false}; | |
| 988 | + size := U32(SIZE(Sockaddr)); | |
| 989 | + p := ^U8(ADR(a)); | |
| 990 | + if C.getpeername(I32(fd), p, ADR(size)) # 0 then return r, failed(); end; | |
| 991 | + if familyOf(p) = CAbi.afUnix then (* ask who made the connection *) | |
| 992 | + credSize := U32(LEN(cred)); | |
| 993 | + if C.getsockopt(I32(fd), CAbi.solSocket, CAbi.soPeercred, ADR(cred[0]), ADR(credSize)) # 0 then | |
| 994 | + return r, failed(); | |
| 995 | + end; | |
| 996 | + r.unix := true; | |
| 997 | + r.pid := INT(field(ADR(cred[0]), CAbi.ucPidAt, CAbi.ucPidLen)); | |
| 998 | + r.uid := INT(field(ADR(cred[0]), CAbi.ucUidAt, CAbi.ucUidLen)); | |
| 999 | + else | |
| 1000 | + r.port := portOf(p); | |
| 1001 | + if familyOf(p) = CAbi.afInet6 then | |
| 1002 | + r.ipv6 := true; | |
| 1003 | + COPY(r.addr[0..16], p[CAbi.sin6AddrAt..CAbi.sin6AddrAt + 16]); | |
| 1004 | + else | |
| 1005 | + COPY(r.addr[0..4], p[CAbi.sinAddrAt..CAbi.sinAddrAt + 4]); | |
| 1006 | + end; | |
| 1007 | + end; | |
| 1008 | + return r, 0; | |
| 961 | 1009 | end; |
| 962 | 1010 | |
| 1011 | +(* Two connected stream sockets, as a Unix socket and the one that | |
| 1012 | + connects to it make: their fds and 0, closed in a program exec | |
| 1013 | + starts; or -1, -1 and minus an errno. *) | |
| 1014 | +proc socketPair*: (INT, INT, INT); | |
| 1015 | +var fds: [2]I32; | |
| 1016 | +begin | |
| 1017 | + if C.socketpair(CAbi.afUnix, CAbi.sockStream | CAbi.sockCloexec, 0, ADR(fds[0])) # 0 then | |
| 1018 | + return -1, -1, failed(); | |
| 1019 | + end; | |
| 1020 | + return INT(fds[0]), INT(fds[1]), 0; | |
| 1021 | +end; | |
| 1022 | + | |
| 963 | 1023 | (* Writes s to the socket fd, as write does, but returns -32 (EPIPE) |
| 964 | 1024 | where write would stop the program with SIGPIPE: when the other end |
| 965 | 1025 | has closed it. *) |
| @@ -2242,6 +2302,47 @@ | ||
| 2242 | 2302 | if processors() < 1 then exit(2); end; |
| 2243 | 2303 | end; |
| 2244 | 2304 | |
| 2305 | +(* The test of what a server needs: a pair of sockets, one end told | |
| 2306 | + it has no more to read, who is at the other end of a Unix socket and | |
| 2307 | + of a TCP one, and a fifo opened without waiting. *) | |
| 2308 | +proc testServer; | |
| 2309 | +var a, b, l, c, s, port, child, r, how: INT; buf: [8]U8; w: Remote; st: Status; | |
| 2310 | +begin | |
| 2311 | + a, b, r := socketPair(); | |
| 2312 | + if (r # 0) or (send(a, "hi") # 2) or (read(b, buf) # 2) or (buf[0..2] # "hi") then exit(2); end; | |
| 2313 | + if (closeWrite(a) # 0) or (read(b, buf) # 0) then exit(2); end; (* b reads an end, *) | |
| 2314 | + if (send(b, "yo") # 2) or (read(a, buf) # 2) or (buf[0..2] # "yo") then exit(2); end; (* and can still answer *) | |
| 2315 | + w, r := peer(a); | |
| 2316 | + 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; | |
| 2317 | + close(a); | |
| 2318 | + close(b); | |
| 2319 | + _, r := peer(-1); | |
| 2320 | + if r # errBadFd then exit(2); end; | |
| 2321 | + l := listenTcp("127.0.0.1", 0); | |
| 2322 | + port := localPort(l); | |
| 2323 | + c := connectTcp("127.0.0.1", port); | |
| 2324 | + s := accept(l); | |
| 2325 | + if (l < 0) or (c < 0) or (s < 0) then exit(2); end; | |
| 2326 | + w, r := peer(c); | |
| 2327 | + 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; | |
| 2328 | + w, r := peer(s); | |
| 2329 | + if (r # 0) or (w.port = port) or (w.addr[3] # 1) or (w.addr[4] # 0) then exit(2); end; | |
| 2330 | + close(s); | |
| 2331 | + close(c); | |
| 2332 | + close(l); | |
| 2333 | + _ := remove("/tmp/raz-sys-fifo"); | |
| 2334 | + child := spawn({path: "mkfifo", args: ["mkfifo", "/tmp/raz-sys-fifo"]}); | |
| 2335 | + r, st, how := waitExit(child); | |
| 2336 | + if (st # exited) or (how # 0) then exit(2); end; | |
| 2337 | + if open("/tmp/raz-sys-fifo", {writing, nonblocking}) # -6 then exit(2); end; (* ENXIO: no reader, and it didn't wait *) | |
| 2338 | + a := open("/tmp/raz-sys-fifo", {reading, nonblocking}); | |
| 2339 | + b := open("/tmp/raz-sys-fifo", {writing, nonblocking}); | |
| 2340 | + if (a < 0) or (b < 0) or (read(a, buf) # errAgain) then exit(2); end; (* nothing to read, and it didn't wait *) | |
| 2341 | + close(a); | |
| 2342 | + close(b); | |
| 2343 | + _ := remove("/tmp/raz-sys-fifo"); | |
| 2344 | +end; | |
| 2345 | + | |
| 2245 | 2346 | (* The module's test, as Linux's Sys has it: a file written, read back |
| 2246 | 2347 | and closed; a directory made, listed and emptied; no terminal; a |
| 2247 | 2348 | socket, sent to after its end; programs; time and the network. *) |
| @@ -2468,6 +2569,7 @@ | ||
| 2468 | 2569 | testShell; |
| 2469 | 2570 | testNet; |
| 2470 | 2571 | testProcess; |
| 2572 | + testServer; | |
| 2471 | 2573 | end; |
| 2472 | 2574 | |
| 2473 | 2575 | end Sys. |
| @@ -151,7 +151,8 @@ | ||
| 151 | 151 | K("sockStream", SOCK_STREAM); K("sockDgram", SOCK_DGRAM); |
| 152 | 152 | K("sockCloexec", SOCK_CLOEXEC); K("sockNonblock", SOCK_NONBLOCK); |
| 153 | 153 | K("solSocket", SOL_SOCKET); K("soReuseaddr", SO_REUSEADDR); |
| 154 | - K("soError", SO_ERROR); K("msgNosignal", MSG_NOSIGNAL); | |
| 154 | + K("soError", SO_ERROR); K("soPeercred", SO_PEERCRED); /* Linux's */ | |
| 155 | + K("shutWr", SHUT_WR); K("msgNosignal", MSG_NOSIGNAL); | |
| 155 | 156 | K("msgCmsgCloexec", MSG_CMSG_CLOEXEC); K("scmRights", SCM_RIGHTS); |
| 156 | 157 | K("sockaddrLen", sizeof(struct sockaddr_storage)); |
| 157 | 158 | READ("saFamily", struct sockaddr, sa_family); |
| @@ -168,6 +169,7 @@ | ||
| 168 | 169 | K("sockaddrIn6Len", sizeof(struct sockaddr_in6)); |
| 169 | 170 | AT("sin6Port", struct sockaddr_in6, sin6_port); |
| 170 | 171 | AT("sin6Addr", struct sockaddr_in6, sin6_addr); |
| 172 | + READ("ucPid", struct ucred, pid); READ("ucUid", struct ucred, uid); | |
| 171 | 173 | K("msghdrLen", sizeof(struct msghdr)); |
| 172 | 174 | AT("msgIov", struct msghdr, msg_iov); |
| 173 | 175 | WRITE("msgIovlen", struct msghdr, msg_iovlen, msg_control); |