This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit a36e921ebecb7f31bd8302a0b796a753744d4078 Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 11 23:09:07 2026 +0100 net/local: Report a hang-up of a stream socket as Linux does. A connected stream socket polls its FIFOs with the pollfd of the caller, so it reports the events of a FIFO. When the peer closes, the FIFO gives POLLHUP alone. read() gives the data that is left or end of file, but a program that reads only on POLLIN does not read, and it calls poll() again at once. It uses all the CPU time and it does not see the end of the stream. After shutdown(SHUT_RD) poll() gives POLLERR, and a caller that asks only for POLLRDHUP gets nothing. Linux reports the state of the socket (unix_poll()): - no more data comes in (the peer closed or shut down its writing half, or shutdown(SHUT_RD)): POLLIN | POLLRDHUP - no data goes out either: also POLLHUP The socket now keeps its shutdown state in lc_shutdown. shutdown() sets it on the socket and on the peer, and a close sets both directions on the peer before it closes the FIFOs. A poll waits on the FIFOs with pollfds of the socket (as many as the FIFO has slots). They share the memory of the pollfds of a datagram socket pair, which a stream socket does not use, so the connection does not grow by a full array. Their callback reports the FIFO data events with the events of the state; a FIFO without a reader gives POLLOUT, because send() does not block (it fails). shutdown() stops the polls of a FIFO before it closes the FIFO. Before, the poll stayed in the FIFO of the closed file. Datagram sockets, pipes and FIFOs do not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/local/local.h | 49 +++++++- net/local/local_conn.c | 9 ++ net/local/local_netpoll.c | 276 +++++++++++++++++++++++++++++++++++++++++++++- net/local/local_sockif.c | 25 +++++ 4 files changed, 351 insertions(+), 8 deletions(-) diff --git a/net/local/local.h b/net/local/local.h index 60f9a5bcd4d..ee0be0ba861 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -109,6 +109,20 @@ enum local_state_s */ struct devif_callback_s; /* Forward reference */ +struct local_conn_s; /* Forward reference */ + +/* A poll of a connected stream socket. It waits on the FIFOs of the + * socket, and the socket reports their events with the events of its own + * state (see local_netpoll.c). + */ + +struct local_pollfd_s +{ + FAR struct local_conn_s *lp_conn; /* The socket; NULL: the entry is free */ + FAR struct pollfd_s *lp_fds; /* The poll of the caller */ + struct pollfd_s lp_in; /* The poll of lc_infile */ + struct pollfd_s lp_out; /* The poll of lc_outfile */ +}; struct local_conn_s { @@ -125,6 +139,8 @@ struct local_conn_s uint8_t lc_type; /* See enum local_type_e */ uint8_t lc_state; /* See enum local_state_e */ bool lc_pair; /* One end of a socketpair() */ + uint8_t lc_shutdown; /* SHUT_RD: no more data comes in, SHUT_WR: + * no more data goes out */ struct file lc_infile; /* File for read-only FIFO (peers) */ struct file lc_outfile; /* File descriptor of write-only FIFO (peers) */ char lc_path[UNIX_PATH_MAX]; /* Path assigned by bind() */ @@ -153,11 +169,19 @@ struct local_conn_s */ #endif - /* The poll structures for the two FIFOs of a connected stream socket or - * of a socket pair, for poll() of POLLIN and POLLOUT together. + /* The polls of the FIFOs: a socket of a datagram socket pair uses pair + * (for POLLIN and POLLOUT together), a connected stream socket uses + * stream (each poll takes a slot of its FIFOs, so as many as a FIFO + * has). A socket uses only the array of its type. */ - struct pollfd_s lc_inout_fds[2*LOCAL_NPOLLWAITERS]; + union + { + struct pollfd_s pair[2*LOCAL_NPOLLWAITERS]; +#ifdef CONFIG_NET_LOCAL_STREAM + struct local_pollfd_s stream[CONFIG_DEV_PIPE_NPOLLWAITERS]; +#endif + } lc_pfds; #ifdef CONFIG_NET_LOCAL_STREAM /* SOCK_STREAM fields common to both client and server */ @@ -762,6 +786,25 @@ int local_open_sender(FAR struct local_conn_s *conn, FAR const char *path, void local_event_pollnotify(FAR struct local_conn_s *conn, pollevent_t eventset); +/**************************************************************************** + * Name: local_shutdown_pollnotify + * + * Description: + * shutdown() changed the state of a stream socket and is about to close + * one of its FIFOs. Stop the polls of that FIFO and tell all polls of + * the socket the new state. + * + * Input Parameters: + * conn - The socket + * filep - lc_infile or lc_outfile, which shutdown() closes next + * + ****************************************************************************/ + +#ifdef CONFIG_NET_LOCAL_STREAM +void local_shutdown_pollnotify(FAR struct local_conn_s *conn, + FAR struct file *filep); +#endif + /**************************************************************************** * Name: local_pollsetup * diff --git a/net/local/local_conn.c b/net/local/local_conn.c index 3baa869f07c..d7d1661bc8d 100644 --- a/net/local/local_conn.c +++ b/net/local/local_conn.c @@ -341,6 +341,15 @@ void local_free(FAR struct local_conn_s *conn) if (conn->lc_peer) { + /* A stream peer gets no more data and cannot send. Set this before + * the FIFOs close: their events wake up the polls of the peer. + */ + + if (conn->lc_proto == SOCK_STREAM) + { + conn->lc_peer->lc_shutdown = SHUT_RDWR; + } + conn->lc_peer->lc_peer = NULL; conn->lc_peer = NULL; } diff --git a/net/local/local_netpoll.c b/net/local/local_netpoll.c index c0646ee0846..2d4c18cd094 100644 --- a/net/local/local_netpoll.c +++ b/net/local/local_netpoll.c @@ -31,6 +31,8 @@ #include <errno.h> #include <nuttx/debug.h> #include <poll.h> +#include <string.h> +#include <sys/socket.h> #include <nuttx/semaphore.h> #include <nuttx/net/net.h> @@ -39,6 +41,10 @@ #include "socket/socket.h" #include "local/local.h" +/**************************************************************************** + * Private Functions + ****************************************************************************/ + /**************************************************************************** * Name: local_event_pollsetup ****************************************************************************/ @@ -107,6 +113,187 @@ static int local_event_pollsetup(FAR struct local_conn_s *conn, return OK; } + +/**************************************************************************** + * Name: local_pollstate + * + * Description: + * The events of the shutdown state of a socket, as on Linux: when no more + * data comes in, read() does not block (POLLIN) and the peer is gone for + * reading (POLLRDHUP); when no data goes out either, POLLHUP. + * + ****************************************************************************/ + +static pollevent_t local_pollstate(FAR struct local_conn_s *conn) +{ + pollevent_t eventset = 0; + + if ((conn->lc_shutdown & SHUT_RD) != 0) + { + eventset |= POLLIN | POLLRDHUP; + } + + if ((conn->lc_shutdown & SHUT_RDWR) == SHUT_RDWR) + { + eventset |= POLLHUP; + } + + return eventset; +} + +/**************************************************************************** + * Name: local_fifo_poll_cb + * + * Description: + * An event on a FIFO of the socket. A FIFO reports POLLHUP when it has + * no writer and POLLERR when it has no reader. For the socket only its + * shutdown state counts, which the peer sets before it closes a FIFO. + * + ****************************************************************************/ + +static void local_fifo_poll_cb(FAR struct pollfd_s *fds) +{ + FAR struct local_pollfd_s *lp = fds->arg; + pollevent_t eventset = fds->revents & (POLLIN | POLLOUT); + + /* The output FIFO has no reader: send() does not block, it fails */ + + if (fds == &lp->lp_out && (fds->revents & POLLERR) != 0) + { + eventset |= POLLOUT; + } + + poll_notify(&lp->lp_fds, 1, eventset | local_pollstate(lp->lp_conn)); +} + +/**************************************************************************** + * Name: local_fifo_pollsetup + * + * Description: + * Set up the poll of one FIFO of the socket. + * + ****************************************************************************/ + +static int local_fifo_pollsetup(FAR struct local_pollfd_s *lp, + FAR struct file *filep, + FAR struct pollfd_s *fds, + pollevent_t events) +{ + fds->events = events; + fds->arg = lp; + fds->cb = local_fifo_poll_cb; + + return file_poll(filep, fds, true); +} + +/**************************************************************************** + * Name: local_fifo_pollteardown + ****************************************************************************/ + +static int local_fifo_pollteardown(FAR struct local_conn_s *conn, + FAR struct local_pollfd_s *lp) +{ + int ret = OK; + int ret2; + + /* A FIFO that shutdown() closed has no poll left */ + + if (lp->lp_in.priv != NULL) + { + ret = file_poll(&conn->lc_infile, &lp->lp_in, false); + } + + if (lp->lp_out.priv != NULL) + { + ret2 = file_poll(&conn->lc_outfile, &lp->lp_out, false); + if (ret2 < 0) + { + ret = ret2; + } + } + + lp->lp_conn = NULL; + return ret; +} + +/**************************************************************************** + * Name: local_stream_pollsetup + * + * Description: + * Set up the poll of a connected stream socket: the poll of the FIFOs + * for the events that the caller asked for, and the events of the + * socket state now. + * + ****************************************************************************/ + +static int local_stream_pollsetup(FAR struct local_conn_s *conn, + FAR struct pollfd_s *fds) +{ + FAR struct local_pollfd_s *lp; + pollevent_t eventset = 0; + int ret = OK; + int i; + + nxmutex_lock(&conn->lc_polllock); + + for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++) + { + if (conn->lc_pfds.stream[i].lp_conn == NULL) + { + break; + } + } + + if (i >= CONFIG_DEV_PIPE_NPOLLWAITERS) + { + nxmutex_unlock(&conn->lc_polllock); + fds->priv = NULL; + return -EBUSY; + } + + lp = &conn->lc_pfds.stream[i]; + lp->lp_conn = conn; + lp->lp_fds = fds; + memset(&lp->lp_in, 0, sizeof(lp->lp_in)); + memset(&lp->lp_out, 0, sizeof(lp->lp_out)); + + /* A FIFO that shutdown() closed has nothing to wait for: the state gives + * POLLIN, and send() does not block (it fails). + */ + + if ((fds->events & (POLLIN | POLLRDHUP)) != 0 && + conn->lc_infile.f_inode != NULL) + { + ret = local_fifo_pollsetup(lp, &conn->lc_infile, &lp->lp_in, POLLIN); + } + + if ((fds->events & POLLOUT) != 0) + { + if (conn->lc_outfile.f_inode == NULL) + { + eventset |= POLLOUT; + } + else if (ret >= 0) + { + ret = local_fifo_pollsetup(lp, &conn->lc_outfile, &lp->lp_out, + POLLOUT); + } + } + + if (ret < 0) + { + local_fifo_pollteardown(conn, lp); + fds->priv = NULL; + } + else + { + fds->priv = lp; + poll_notify(&fds, 1, eventset | local_pollstate(conn)); + } + + nxmutex_unlock(&conn->lc_polllock); + return ret; +} #endif /* CONFIG_NET_LOCAL_STREAM */ /**************************************************************************** @@ -138,6 +325,52 @@ void local_event_pollnotify(FAR struct local_conn_s *conn, #endif } +/**************************************************************************** + * Name: local_shutdown_pollnotify + * + * Description: + * shutdown() changed the state of a stream socket and is about to close + * one of its FIFOs. Stop the polls of that FIFO and tell all polls of + * the socket the new state. + * + * Input Parameters: + * conn - The socket + * filep - lc_infile or lc_outfile, which shutdown() closes next + * + ****************************************************************************/ + +#ifdef CONFIG_NET_LOCAL_STREAM +void local_shutdown_pollnotify(FAR struct local_conn_s *conn, + FAR struct file *filep) +{ + FAR struct local_pollfd_s *lp; + FAR struct pollfd_s *fds; + int i; + + nxmutex_lock(&conn->lc_polllock); + + for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++) + { + lp = &conn->lc_pfds.stream[i]; + if (lp->lp_conn == NULL) + { + continue; + } + + fds = filep == &conn->lc_infile ? &lp->lp_in : &lp->lp_out; + if (fds->priv != NULL) + { + file_poll(filep, fds, false); + } + + poll_notify(&lp->lp_fds, 1, local_pollstate(conn) | + (filep == &conn->lc_outfile ? POLLOUT : 0)); + } + + nxmutex_unlock(&conn->lc_polllock); +} +#endif + /**************************************************************************** * Name: local_pollsetup * @@ -191,11 +424,29 @@ int local_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) { return local_event_pollsetup(conn, fds, true); } + + if (conn->lc_proto == SOCK_STREAM) + { + if (conn->lc_state == LOCAL_STATE_CONNECTED) + { + return local_stream_pollsetup(conn, fds); + } + + /* Not connected, or the connection was lost */ + + fds->priv = NULL; + if (conn->lc_state == LOCAL_STATE_DISCONNECTED || + (fds->events & (POLLIN | POLLOUT)) != 0) + { + poll_notify(&fds, 1, POLLERR); + } + + return OK; + } #endif - /* A connected stream socket or a socket of a socket pair (stream or - * datagram): POLLIN from the FIFO that it reads, POLLOUT from the FIFO - * that it writes. + /* A socket of a datagram socket pair: POLLIN from the FIFO that it + * reads, POLLOUT from the FIFO that it writes. */ if (conn->lc_state == LOCAL_STATE_DISCONNECTED) @@ -223,11 +474,11 @@ int local_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) nxmutex_lock(&conn->lc_polllock); - shadowfds = conn->lc_inout_fds; + shadowfds = conn->lc_pfds.pair; while (shadowfds->fd != 0) { shadowfds += 2; - if (shadowfds >= &conn->lc_inout_fds[2*LOCAL_NPOLLWAITERS]) + if (shadowfds >= &conn->lc_pfds.pair[2*LOCAL_NPOLLWAITERS]) { nxmutex_unlock(&conn->lc_polllock); return -ENOMEM; @@ -353,6 +604,21 @@ int local_pollteardown(FAR struct socket *psock, FAR struct pollfd_s *fds) { return local_event_pollsetup(conn, fds, false); } + + if (conn->lc_proto == SOCK_STREAM) + { + /* Only a poll that the setup registered needs a teardown */ + + if (fds->priv != NULL) + { + nxmutex_lock(&conn->lc_polllock); + ret = local_fifo_pollteardown(conn, fds->priv); + fds->priv = NULL; + nxmutex_unlock(&conn->lc_polllock); + } + + return ret; + } #endif if (conn->lc_state == LOCAL_STATE_DISCONNECTED) diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index df79100b9fd..fab37936877 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -1314,11 +1314,35 @@ static int local_shutdown(FAR struct socket *psock, int how) case SOCK_STREAM: { FAR struct local_conn_s *conn = psock->s_conn; + FAR struct local_conn_s *peer; + + /* The peer sends no more data when this socket stops reading, + * and it gets no more data when this socket stops writing. + */ + + local_lock(); + conn->lc_shutdown |= how & SHUT_RDWR; + peer = conn->lc_peer; + if (peer != NULL) + { + if (how & SHUT_RD) + { + peer->lc_shutdown |= SHUT_WR; + } + + if (how & SHUT_WR) + { + peer->lc_shutdown |= SHUT_RD; + } + } + + local_unlock(); if (how & SHUT_RD) { if (conn->lc_infile.f_inode != NULL) { + local_shutdown_pollnotify(conn, &conn->lc_infile); file_close(&conn->lc_infile); conn->lc_infile.f_inode = NULL; } @@ -1328,6 +1352,7 @@ static int local_shutdown(FAR struct socket *psock, int how) { if (conn->lc_outfile.f_inode != NULL) { + local_shutdown_pollnotify(conn, &conn->lc_outfile); file_close(&conn->lc_outfile); conn->lc_outfile.f_inode = NULL; }
