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 c175932e5c76c793d32f9d3549f645be8c69fa1a Author: Marco Casaroli <[email protected]> AuthorDate: Fri Oct 30 09:58:37 2026 +0100 net/local: Let a signal end a blocked recv() of a local socket. local_fifo_read() read the FIFO again after -EINTR. Thus a signal did not end recv(), recvfrom(), recvmsg() or read() of a local socket that waits for data. In a kernel or protected build, the signal action runs only when the system call returns, so the signal handler did not run, and a default action that ends the process (SIGTERM, SIGKILL) did not end it. A program could not use alarm() to stop a recv() that waits too long. POSIX: recv() fails with EINTR when a signal interrupts it before any data is available. Now local_fifo_read() returns -EINTR when the signal comes before the first byte and the caller lets it (new argument intr). A stream socket lets it. A datagram socket lets it only for the wait for a new datagram: after the first byte of a datagram, the read goes on to the end of the datagram, so the boundaries of the next datagrams stay correct. The writer puts a datagram whole into the FIFO. recvfrom() of a datagram socket that opens its FIFO for the call now closes the FIFO also when the first read fails (-EAGAIN, -EINTR). Also the indentation of a break in local_fifo_read() (nxstyle). The pipe driver already returns -EINTR. The same code runs in flat, protected and kernel builds. In a flat build the handler ran during the wait already, but recv() did not return. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/local/local.h | 8 ++++++-- net/local/local_recvmsg.c | 37 +++++++++++++++++++++++++------------ net/local/local_recvutils.c | 16 ++++++++++------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/net/local/local.h b/net/local/local.h index 9fce2db14fb..45757ef0fd3 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -602,17 +602,21 @@ ssize_t local_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, * len - Length of data to receive [in] * Length of data actually received [out] * once - Flag to indicate the buf may only be read once + * intr - A signal can end the wait for the first byte. Use false for + * the parts of a datagram after its start: a datagram must be + * read whole. * * Returned Value: * Zero is returned on success; a negated errno value is returned on any * failure. If -ECONNRESET is received, then the sending side has closed * the FIFO. In this case, the returned data may still be valid (if the - * returned len > 0). + * returned len > 0). -EINTR: a signal came before the first byte (only + * if intr is true). * ****************************************************************************/ int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, - size_t *len, bool once); + size_t *len, bool once, bool intr); /**************************************************************************** * Name: local_getaddr diff --git a/net/local/local_recvmsg.c b/net/local/local_recvmsg.c index 3f703e3df2b..72b19ebc794 100644 --- a/net/local/local_recvmsg.c +++ b/net/local/local_recvmsg.c @@ -53,13 +53,14 @@ * * Description: * A thin layer around local_fifo_read that handles socket-related loss-of- - * connection events. + * connection events. intr: a signal can end the wait for the first + * byte (the start of a message, not the rest of a datagram). * ****************************************************************************/ static int psock_fifo_read(FAR struct socket *psock, FAR void *buf, size_t offset, FAR size_t *readlen, - int flags, bool once) + int flags, bool once, bool intr) { FAR struct local_conn_s *conn = psock->s_conn; int ret; @@ -78,7 +79,7 @@ static int psock_fifo_read(FAR struct socket *psock, FAR void *buf, } else { - ret = local_fifo_read(&conn->lc_infile, buf, readlen, once); + ret = local_fifo_read(&conn->lc_infile, buf, readlen, once, intr); } if (ret < 0) @@ -112,7 +113,7 @@ static int psock_fifo_read(FAR struct socket *psock, FAR void *buf, } else { - if (ret != -EAGAIN) + if (ret != -EAGAIN && ret != -EINTR) { nerr("ERROR: Failed to read packet: %d\n", ret); } @@ -327,7 +328,7 @@ psock_stream_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg, ret = psock_fifo_read(psock, (FAR uint8_t *)msg->msg_iov[i].iov_base + iov_recv, - peek_offset, &readlen, flags, true); + peek_offset, &readlen, flags, true, true); if (ret < 0) { break; @@ -428,7 +429,8 @@ static int psock_fifo_discard(FAR struct socket *psock, size_t len, /* Read 256 bytes into the bit bucket */ tmplen = MIN(len, sizeof(bitbucket)); - ret = psock_fifo_read(psock, bitbucket, 0, &tmplen, flags, false); + ret = psock_fifo_read(psock, bitbucket, 0, &tmplen, flags, false, + false); if (ret < 0) { nerr("ERROR: Failed to get bitbucket : ret %d\n", ret); @@ -536,12 +538,21 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, } } + /* A signal ends the wait for a datagram (-EINTR). When the first byte + * is in, the datagram is read whole. + */ + readlen = sizeof(addrlen); - ret = psock_fifo_read(psock, &addrlen, offset, &readlen, flags, false); + ret = psock_fifo_read(psock, &addrlen, offset, &readlen, flags, false, + true); if (ret < 0) { - nerr("ERROR: Failed to get path length: ret %d\n", ret); - return ret; + if (ret != -EAGAIN && ret != -EINTR) + { + nerr("ERROR: Failed to get path length: ret %d\n", ret); + } + + goto errout_with_infd; } /* No data: the peer of a socket pair was closed. Return 0, as for the @@ -560,7 +571,8 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, readlen = sizeof(pktlen); offset += sizeof(addrlen); - ret = psock_fifo_read(psock, &pktlen, offset, &readlen, flags, false); + ret = psock_fifo_read(psock, &pktlen, offset, &readlen, flags, false, + false); if (ret < 0) { nerr("ERROR: Failed to get packet length: ret %d\n", ret); @@ -578,7 +590,7 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, { pathlen = MIN(*fromlen - sizeof(sa_family_t) - 1, readlen); ret = psock_fifo_read(psock, from->sa_data, offset, - &pathlen, flags, false); + &pathlen, flags, false, false); if (ret < 0) { nerr("ERROR: Failed to get path : ret %d\n", ret); @@ -610,7 +622,8 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, readlen = MIN(pktlen, len); offset += addrlen; - ret = psock_fifo_read(psock, buf, offset, &readlen, flags, false); + ret = psock_fifo_read(psock, buf, offset, &readlen, flags, false, + false); if (ret < 0) { nerr("ERROR: Failed to get packet : ret %d\n", ret); diff --git a/net/local/local_recvutils.c b/net/local/local_recvutils.c index 5de1f930e9e..4a208619a9c 100644 --- a/net/local/local_recvutils.c +++ b/net/local/local_recvutils.c @@ -56,15 +56,16 @@ * Zero means *len[in] is zero, * or the sending side has closed the FIFO * once - Flag to indicate the buf may only be read once + * intr - A signal can end the wait for the first byte * * Returned Value: * Zero is returned on success; a negated errno value is returned on any - * failure. + * failure. -EINTR: a signal came before the first byte. * ****************************************************************************/ int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, - size_t *len, bool once) + size_t *len, bool once, bool intr) { ssize_t remaining; ssize_t nread; @@ -80,12 +81,15 @@ int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, { ret = (int)nread; - if (ret == -EINTR) + /* A signal ends the wait only before the first byte, and only if + * the caller lets it: the rest of a datagram is on its way. + */ + + if (ret == -EINTR && (!intr || remaining < *len)) { - ninfo("Ignoring signal\n"); continue; } - else if (ret == -EAGAIN) + else if (ret == -EAGAIN || ret == -EINTR) { goto errout; } @@ -101,7 +105,7 @@ int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, * has closed the FIFO. */ - break; + break; } else {
