casaroli opened a new pull request, #21900: URL: https://github.com/apache/nuttx/pull/21900
## Summary `poll()` and `epoll_wait()` on a socket whose peer closed or shut down a direction now give the events that Linux gives. Linux (`tcp_poll()`, `unix_poll()`, `pipe_poll()`) reports, per file type: - **Stream socket (TCP, `AF_UNIX` stream):** no more data comes in (the peer closed or shut down its writing half, or `shutdown(SHUT_RD)`): `POLLIN | POLLRDHUP`. Both directions shut down, or the connection ended: also `POLLHUP`. A socket error (a reset): also `POLLERR`. - **`AF_UNIX` datagram socket:** a close of the peer does not change the socket. - **Pipe and FIFO:** no writer: `POLLHUP` (and `POLLIN` while data is left), never `POLLRDHUP`. No reader: the writer gets `POLLERR`. Three commits: **include/sys/poll.h: Give POLLRDHUP the value of Linux.** `POLLRDHUP` had the value of `POLLHUP` (0x10), so a caller could not tell them apart. A program that treats `POLLHUP` as "read and write" and `POLLRDHUP` as "the peer closed" always took the second case. `POLLRDHUP` and `EPOLLRDHUP` are now 0x2000, the value of Linux. It fits the `short` of `struct pollfd`. **net/local: Report a hang-up of a stream socket as Linux does.** A connected stream socket polled its FIFOs with the pollfd of the caller, so it reported the FIFO events: `POLLHUP` alone after the peer closed, `POLLERR` after `shutdown(SHUT_RD)`, and nothing for a caller that asked only for `POLLRDHUP`. A program that reads only on `POLLIN` did not read: it called `poll()` again at once, used all the CPU time and did not see the end of the stream. The socket now keeps its shutdown state (`lc_shutdown`). `shutdown()` sets it on the socket and on the peer, and a close sets both directions on the peer before the FIFOs close. A poll waits on the FIFOs with pollfds of the socket, and their callback reports the FIFO data events with the events of the state. `shutdown()` stops the polls of a FIFO before it closes the FIFO (before, a set-up poll stayed in the FIFO of the closed file). **net/tcp: Report a hang-up of a socket as Linux does.** TCP gave `POLLIN` alone after the peer closed or shut down its writing half, no `POLLHUP` when both directions were shut down, and `POLLERR | POLLHUP` without `POLLIN` after a reset. It now gives `POLLIN | POLLRDHUP` for `SHUT_RD` in `conn->shutdown` and for `TCP_RXCLOSE`, `POLLHUP` when both directions are shut down, and `POLLIN | POLLRDHUP` with `POLLERR | POLLHUP` when the connection ended. A socket that never connected gives `POLLERR | POLLHUP`, as before. ## Impact - `POLLRDHUP` and `EPOLLRDHUP` change from 0x10 to 0x2000 in `<sys/poll.h>` and `<sys/epoll.h>`. On master `POLLRDHUP` is `POLLHUP`, so it means "a hang-up" for every file. Now it is the hang-up of the reading side of a stream socket only, as on Linux. A program that asks for or tests only `POLLRDHUP` no longer sees the hang-up of the drivers and sockets that report `POLLHUP` (about 20 files: pipes, FIFOs, serial, CAN, eventfd, UDP, ...); it must test `POLLHUP`, as on Linux. `POLLHUP` is always reported, also when the caller did not ask for it. No code in NuttX or nuttx-apps uses `POLLRDHUP` or `EPOLLRDHUP` (only the two headers define them). A program built with the old value asks for `POLLHUP` and gets `POLLHUP`, as before; it must be built again to tell the two apart. This is not a breaking change: no API of the flat build is removed or changes its type, and a program that tests `POLLHUP` works as before. - TCP: a caller that asked for `POLLIN` also gets `POLLHUP` when both directions are shut down; after a reset it also gets `POLLIN` (as on Linux; `read()` does not block). A caller that asked for `POLLRDHUP` gets it. - `AF_UNIX` stream: after the peer closed, `POLLIN | POLLHUP` (was `POLLHUP`); after the peer shut down its writing half, `POLLIN | POLLRDHUP` without `POLLHUP` (was `POLLHUP`); after `shutdown(SHUT_RD)`, `POLLIN` (was `POLLERR`); a caller that asked only for `POLLRDHUP` is now woken up. - `AF_UNIX` datagram sockets, pipes, FIFOs, pseudo-terminals, UDP and usrsock do not change (see the table). - A connected `AF_UNIX` stream socket has as many polls at the same time as its FIFO (`CONFIG_DEV_PIPE_NPOLLWAITERS`, default 4); before, 4 for `POLLIN` alone and 2 for `POLLIN | POLLOUT`. With the defaults, `struct local_conn_s` grows by 144 bytes on 32-bit targets (528 to 672, mps2-an500) and by 256 bytes on 64-bit targets (720 to 976, qemu-armv8a). The pollfds of a stream socket share their memory with the pollfds of a datagram socket pair (a union): a socket uses only the array of its type. - No new option. All build modes and architectures (common code). ## Testing Test program `pollhup.c`: it puts a descriptor in a state and calls `poll(POLLIN)`, `poll(POLLIN | POLLRDHUP)`, `poll(POLLRDHUP)` and `epoll_wait(EPOLLIN | EPOLLRDHUP)`; a case passes when the four give the mask of Linux. Linux: the same program on Linux 7.2 x86_64 (47 of 47 pass). NuttX: qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build, the program as a builtin), QEMU with HVF, with `CONFIG_NET_LOCAL` (stream and datagram), `CONFIG_NET_TCP`, `CONFIG_NET_LOOPBACK`, `CONFIG_NET_SOLINGER` and `CONFIG_PSEUDOTERM`. knsh and nsh give the same masks except where the table says so. The table shows `poll(POLLIN | POLLRDHUP)` (on master `POLLRDHUP` is `POLLHUP`, so this is `poll(POLLIN)`): | Case | Linux | master | this PR | |---|---|---|---| | socket pair, the peer closed | 0x2011 | 0x10 | 0x2011 | | socket pair, the peer closed, one byte left | 0x2011 | 0x1 | 0x2011 | | socket pair, the peer shut down `SHUT_WR` | 0x2001 | 0x10 | 0x2001 | | socket pair, the peer shut down `SHUT_RD` | none | none | none | | socket pair, `shutdown(SHUT_RD)` | 0x2001 | 0x8 | 0x2001 | | socket pair, `shutdown(SHUT_RDWR)` | 0x2011 | 0x8 | 0x2011 | | socket pair, `shutdown(SHUT_WR)` | none | none | none | | connected `AF_UNIX` stream socket, the same seven cases | as above | as above | as above | | TCP loopback, the peer closed (with and without data left) | 0x2001 | 0x1 | 0x2001 | | TCP, the peer shut down `SHUT_WR` | 0x2001 | 0x1 | 0x2001 | | TCP, the peer shut down `SHUT_RD` | none | none | none | | TCP, `shutdown(SHUT_RD)` | 0x2001 | 0x1 | 0x2001 | | TCP, `shutdown(SHUT_RDWR)` | 0x2011 | 0x1 | 0x2011 | | TCP, `shutdown(SHUT_WR)` | none | none | none | | TCP, `shutdown(SHUT_WR)`, then the peer closed | 0x2011 | 0x1 | 0x2011 | | TCP, the peer closed with `SO_LINGER` 0 (reset) | 0x2019 | nsh 0x18, knsh 0x1 | nsh 0x2019, knsh 0x2001 | | TCP, non-blocking `connect()` refused (reset) | 0x2019 | 0x18 | 0x2019 | | a loop that reads only on `POLLIN` (socket pair) | sees the end | never sees the end (1262128 rounds in 300 ms) | sees the end | | a loop that reads only on `POLLIN` (TCP) | sees the end | sees the end | sees the end | | datagram socket pair or bound socket, the peer closed | none | socket pair 0x10, bound none | not changed | | pipe and FIFO, the writer closed | 0x10 | 0x10 | 0x10 | | pipe and FIFO, the writer closed, one byte left | 0x11 | 0x1 | not changed | | pipe and FIFO, the reader closed: `poll(POLLOUT)` | 0xc | 0x8 | not changed | | pseudo-terminal, the master closed: slave | 0x19 | 0x10 | not changed | | pseudo-terminal, the slave closed: master | 0x10 | 0x10 | not changed | `poll(POLLIN)`, `poll(POLLRDHUP)` and `epoll_wait()` give the same result as Linux in each socket case that this PR changes. Not changed by this PR, and different from Linux: - `SO_LINGER` 0: NuttX sends a FIN and the reset only at a later timer tick, so the knsh run saw only the FIN within 1.5 s. - A stream socket that closes with data it did not read: Linux gives the peer `POLLERR` (`ECONNRESET`), NuttX does not. - `shutdown()` of an `AF_UNIX` datagram socket gives `EOPNOTSUPP`. - `POLLOUT` with `POLLHUP` or `POLLERR`: Linux keeps `POLLOUT`, `poll_notify()` drops it. ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh with the options above, on master and with this PR (master fab614ba83d, apps 41840d466). nuttx-apps cmocka tests on qemu-armv8a:nsh (flat, HVF) with the options above, master fab614ba83d and this PR, apps 41840d466: | Test | master | this PR | |---|---|---| | cmocka_net_local_dgram | 83 PASSED | 83 PASSED | | cmocka_net_local | 4 PASSED | 4 PASSED | | cmocka_net_tcp | 6 PASSED, `test_tcp_connect_ipv4` FAILED | the same | `test_tcp_connect_ipv4` fails on master too in this configuration: its loopback server also listens on IPv6, and `CONFIG_NET_IPv6` is off. On the previous base (master f76e63dbc36), sim on a Linux host (the CI image): usrsocktest (sim:usrsocktest) OK 17, FAILED 0 on master and with this PR. Builds with `-Werror` (CI image, gcc 12.3), each with the local and TCP options above: sim:nsh, sim:nsh without `NET_LOCAL_STREAM`, sim:nsh without `NET_LOCAL_DGRAM`, qemu-armv8a:nsh, qemu-armv8a:knsh, qemu-armv8a:netnsh, rv-virt:netnsh, rv-virt:pnsh64, rv-virt:knsh64, mps2-an500:nsh, esp32s3-devkit:nsh, qemu-intel64:nsh; lm3s6965-ek:qemu-protected with the local sockets only (with TCP too, `kflash` overflows on master as well). On this base: qemu-armv8a:nsh with `-Werror` with the local sockets only as stream or only as datagram, no warning. This PR was run on master 46066440966 (one commit after fab614ba83d, in drivers/rpmsg); the master columns are from fab614ba83d. `tools/checkpatch.sh -c -u -m -g` passes. No change in Documentation/ is needed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
