This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 808c1d448110e8ea4f345559165ee892321bd923
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 12 07:54:00 2026 +0100

    net/local: Send a datagram of a socket pair as one frame.
    
    local_pair_send() wrote the header of a datagram and its data with two
    writes to the FIFO.  A reader that does not wait could get the header
    before the data was in the FIFO.  Then it lost the data and the
    boundaries of the next datagrams.  When the FIFO was full, a blocking
    send waited inside the datagram, so a signal could not end it.
    
    A bound socket sends a datagram as one frame with local_send_frame(),
    into a FIFO in the atomic mode (PIPEIOC_ATOMICWRITE).  A socket pair now
    does the same:
    
    - local_send_frame() gets the address of the sender as an argument.  A
      socket pair gives "", so recvfrom() on the peer gives no address, as
      before.
    - local_socketpair() puts the two FIFOs of a datagram pair in the atomic
      mode.
    - local_pair_send() sends with local_send_frame().  The size of the FIFO
      limits the frame (EMSGSIZE).  A blocking send waits for space for the
      whole frame, and a signal ends this wait with EINTR.  A socket with
      O_NONBLOCK gets EAGAIN from the FIFO.  The FIFO does not see
      MSG_DONTWAIT, so for this flag local_pair_send() checks the space
      first, as before.
    
    The format of the frame does not change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/local/local.h            |  2 ++
 net/local/local_sendmsg.c    | 59 +++++++++++++++++---------------------------
 net/local/local_sendpacket.c |  8 +++---
 net/local/local_sockif.c     |  8 ++++++
 4 files changed, 38 insertions(+), 39 deletions(-)

diff --git a/net/local/local.h b/net/local/local.h
index 45757ef0fd3..4d9acdb040d 100644
--- a/net/local/local.h
+++ b/net/local/local.h
@@ -525,6 +525,7 @@ ssize_t local_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
  *
  * Input Parameters:
  *   conn     A reference to local connection structure
+ *   path     The address of the sender ("" for none)
  *   buf      Payload to send
  *   len      Number of payload iovec segments
  *   rcvsize  Receive buffer size the peer advertised, preamble room
@@ -538,6 +539,7 @@ ssize_t local_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
  ****************************************************************************/
 
 ssize_t local_send_frame(FAR struct local_conn_s *conn,
+                         FAR const char *path,
                          FAR const struct iovec *buf, size_t len,
                          size_t rcvsize);
 
diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c
index 3494b74917d..ea3638de62d 100644
--- a/net/local/local_sendmsg.c
+++ b/net/local/local_sendmsg.c
@@ -144,8 +144,8 @@ fail:
  *
  * Description:
  *   Send one datagram, whole or not at all, to the peer of a SOCK_DGRAM
- *   socket pair: the header that psock_dgram_recvfrom() reads (address
- *   length 0, datagram length), then the data.
+ *   socket pair.  The frame has no address, so recvfrom() on the peer
+ *   gives none.
  *
  * Input Parameters:
  *   conn     The connection of the sender
@@ -170,61 +170,47 @@ static ssize_t local_pair_send(FAR struct local_conn_s 
*conn,
 {
   FAR const struct iovec *end = buf + len;
   FAR const struct iovec *iov;
-  lc_size_t header[2];
-  struct iovec hdriov;
-  size_t pktlen = 0;
+  size_t rcvsize;
+  size_t need;
   int space;
   int ret;
 
-  for (iov = buf; iov != end; iov++)
-    {
-      pktlen += iov->iov_len;
-    }
-
   ret = file_ioctl(&conn->lc_outfile, PIPEIOC_GETSIZE, 0);
   if (ret < 0)
     {
       return ret;
     }
 
-  if (pktlen + sizeof(header) > ret)
-    {
-      return -EMSGSIZE;
-    }
+  rcvsize = ret;
 
-  if ((flags & MSG_DONTWAIT) != 0 ||
-      (conn->lc_outfile.f_oflags & O_NONBLOCK) != 0)
+  /* The FIFO is in atomic mode, but it fails with -EAGAIN only when it is
+   * open with O_NONBLOCK.  For MSG_DONTWAIT, check the space here.  Only
+   * this socket writes to the FIFO and the caller holds lc_sendlock, so
+   * the space cannot become smaller before the write.
+   */
+
+  if ((flags & MSG_DONTWAIT) != 0 &&
+      (conn->lc_outfile.f_oflags & O_NONBLOCK) == 0)
     {
+      need = 2 * sizeof(lc_size_t);
+      for (iov = buf; iov != end; iov++)
+        {
+          need += iov->iov_len;
+        }
+
       ret = file_ioctl(&conn->lc_outfile, FIONSPACE, &space);
       if (ret < 0)
         {
           return ret;
         }
 
-      if (space < pktlen + sizeof(header))
+      if (need <= rcvsize && space < need)
         {
           return -EAGAIN;
         }
     }
 
-  header[0]       = 0;
-  header[1]       = pktlen;
-  hdriov.iov_base = header;
-  hdriov.iov_len  = sizeof(header);
-
-  ret = local_send_packet(&conn->lc_outfile, &hdriov, 1);
-  if (ret == sizeof(header) && pktlen > 0)
-    {
-      ret = local_send_packet(&conn->lc_outfile, buf, len);
-    }
-  else if (ret == sizeof(header))
-    {
-      ret = 0;
-    }
-  else if (ret >= 0)
-    {
-      ret = -EPIPE;
-    }
+  ret = local_send_frame(conn, "", buf, len, rcvsize);
 
   /* -EPIPE: the peer was closed.  A datagram socket reports that as
    * Linux does, and without SIGPIPE.
@@ -397,7 +383,8 @@ static ssize_t local_sendto(FAR struct socket *psock,
 
   /* Send preamble and payload as one atomic frame */
 
-  ret = local_send_frame(conn, buf, len, server->lc_rcvsize);
+  ret = local_send_frame(conn, conn->lc_path, buf, len,
+                         server->lc_rcvsize);
   if (ret < 0)
     {
       nerr("ERROR: Failed to send the frame: %zd\n", ret);
diff --git a/net/local/local_sendpacket.c b/net/local/local_sendpacket.c
index 3aa5e21b548..6a6e3c54c38 100644
--- a/net/local/local_sendpacket.c
+++ b/net/local/local_sendpacket.c
@@ -118,6 +118,7 @@ static ssize_t local_fifo_write(FAR struct file *filep,
  *
  * Input Parameters:
  *   conn     A reference to local connection structure
+ *   path     The address of the sender ("" for none)
  *   buf      Payload to send
  *   len      Number of payload iovec segments
  *   rcvsize  Receive buffer size the peer advertised, preamble room
@@ -131,6 +132,7 @@ static ssize_t local_fifo_write(FAR struct file *filep,
  ****************************************************************************/
 
 ssize_t local_send_frame(FAR struct local_conn_s *conn,
+                         FAR const char *path,
                          FAR const struct iovec *buf, size_t len,
                          size_t rcvsize)
 {
@@ -143,7 +145,7 @@ ssize_t local_send_frame(FAR struct local_conn_s *conn,
   size_t i;
   ssize_t ret;
 
-  pathlen = strlen(conn->lc_path);
+  pathlen = strlen(path);
 
   /* Hold the frame to the capacity the receiver advertised.  The driver
    * refuses only what its FIFO can never hold, which is a different limit:
@@ -175,7 +177,7 @@ ssize_t local_send_frame(FAR struct local_conn_s *conn,
     {
       stackiov[0].iov_base = preamble;
       stackiov[0].iov_len  = sizeof(preamble);
-      stackiov[1].iov_base = conn->lc_path;
+      stackiov[1].iov_base = (FAR char *)path;
       stackiov[1].iov_len  = pathlen;
       memcpy(&stackiov[2], buf, len * sizeof(struct iovec));
     }
@@ -195,7 +197,7 @@ ssize_t local_send_frame(FAR struct local_conn_s *conn,
 
       memcpy(frame, preamble, sizeof(preamble));
       ptr = frame + sizeof(preamble);
-      memcpy(ptr, conn->lc_path, pathlen);
+      memcpy(ptr, path, pathlen);
       ptr += pathlen;
       for (i = 0; i < len; i++)
         {
diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c
index 706e93244a9..df79100b9fd 100644
--- a/net/local/local_sockif.c
+++ b/net/local/local_sockif.c
@@ -1264,6 +1264,14 @@ static int local_socketpair(FAR struct socket *psocks[2])
             {
               goto errout;
             }
+
+          /* Each datagram goes whole into the FIFO, or not at all */
+
+          ret = file_ioctl(&conns[i]->lc_outfile, PIPEIOC_ATOMICWRITE, 1);
+          if (ret < 0)
+            {
+              goto errout;
+            }
         }
     }
 #endif

Reply via email to