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-apps.git

commit 07330be3d28801ae2eb214c94211fcad16157167
Author: daichuan <[email protected]>
AuthorDate: Thu Jun 11 11:30:31 2026 +0800

    netutils/rpmsg_tun: harden the client and server connection paths.
    
    - Let the client take an optional <ifname> and, like the server, only
      forward while that interface is running.
    - Connect the client RPMsg socket in non-blocking mode and wait for
      completion with poll(), so a missing server does not block the task.
    - Wait for incoming connections on the server with poll() before
      accept() instead of busy-looping on EAGAIN.
    - Treat ENOMEM from the TUN write as transient, like EAGAIN; the packet
      is retried once IOBs are available again instead of dropping the link.
    - Check the FIONBIO ioctl result.
    
    Signed-off-by: daichuan <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 netutils/rpmsg_tun/rpmsg_tun_client.c | 34 ++++++++++++++--
 netutils/rpmsg_tun/rpmsg_tun_server.c |  2 +-
 netutils/rpmsg_tun/rpmsg_tun_util.c   | 75 +++++++++++++++++++++++++++++++----
 3 files changed, 98 insertions(+), 13 deletions(-)

diff --git a/netutils/rpmsg_tun/rpmsg_tun_client.c 
b/netutils/rpmsg_tun/rpmsg_tun_client.c
index c417f112d..1708598ff 100644
--- a/netutils/rpmsg_tun/rpmsg_tun_client.c
+++ b/netutils/rpmsg_tun/rpmsg_tun_client.c
@@ -40,13 +40,16 @@
 
 int main(int argc, char *argv[])
 {
+  int nlfd = -1;
   int rpmsgfd;
   int tunfd;
   int ret;
 
-  if (argc != 5)
+  if (argc != 5 && argc != 6)
     {
-      fprintf(stderr, "Usage: %s <tunname> <ip> <mask> <cpu>\n", argv[0]);
+      fprintf(stderr, "Usage: %s <tunname> <ip> <mask> <cpu> <ifname> or\n"
+                      "Usage: %s <tunname> <ip> <mask> <cpu>\n",
+                      argv[0], argv[0]);
       return -EINVAL;
     }
 
@@ -56,16 +59,34 @@ int main(int argc, char *argv[])
       return tunfd;
     }
 
+  if (argc == 6)
+    {
+      nlfd = rpmsg_tun_connect_netlink();
+      if (nlfd < 0)
+        {
+          close(tunfd);
+          return nlfd;
+        }
+    }
+
   for (; ; )
     {
+      if (nlfd >= 0)
+        {
+          ret = rpmsg_tun_wait_running(nlfd, argv[5]);
+          if (ret < 0)
+            {
+              break;
+            }
+        }
+
       rpmsgfd = rpmsg_tun_connect(argv[4], argv[1]);
       if (rpmsgfd < 0)
         {
-          sleep(3);
           continue;
         }
 
-      ret = rpmsg_tun_loop(tunfd, rpmsgfd, -1, NULL);
+      ret = rpmsg_tun_loop(tunfd, rpmsgfd, nlfd, argc == 6 ? argv[5] : NULL);
       close(rpmsgfd);
       if (ret < 0)
         {
@@ -73,6 +94,11 @@ int main(int argc, char *argv[])
         }
     }
 
+  if (nlfd >= 0)
+    {
+      close(nlfd);
+    }
+
   close(tunfd);
   return ret;
 }
diff --git a/netutils/rpmsg_tun/rpmsg_tun_server.c 
b/netutils/rpmsg_tun/rpmsg_tun_server.c
index 98aea6a20..f6f37ac64 100644
--- a/netutils/rpmsg_tun/rpmsg_tun_server.c
+++ b/netutils/rpmsg_tun/rpmsg_tun_server.c
@@ -85,7 +85,7 @@ int main(int argc, char *argv[])
           continue;
         }
 
-      ret = rpmsg_tun_loop(tunfd, rpmsgfd, nlfd, argv[4]);
+      ret = rpmsg_tun_loop(tunfd, rpmsgfd, nlfd, argc == 5 ? argv[4] : NULL);
       close(rpmsgfd);
       if (ret < 0)
         {
diff --git a/netutils/rpmsg_tun/rpmsg_tun_util.c 
b/netutils/rpmsg_tun/rpmsg_tun_util.c
index 0c56fe74d..f778ae437 100644
--- a/netutils/rpmsg_tun/rpmsg_tun_util.c
+++ b/netutils/rpmsg_tun/rpmsg_tun_util.c
@@ -411,7 +411,7 @@ static int rpmsg_tun_from_socket(int tunfd, int rpmsgfd,
       ret = write(tunfd, buf->data, buf->len);
       if (ret < 0)
         {
-          return errno == EAGAIN ? 1 : -errno;
+          return (errno == EAGAIN || errno == ENOMEM) ? 1 : -errno;
         }
       else if (ret != buf->len)
         {
@@ -549,10 +549,11 @@ int rpmsg_tun_connect(const char *cpu, const char *name)
   struct sockaddr_rpmsg addr;
   int on = 1;
   int fd;
+  int ret;
 
   /* Create rpmsg socket */
 
-  fd = socket(AF_RPMSG, SOCK_STREAM | SOCK_CLOEXEC, 0);
+  fd = socket(AF_RPMSG, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
   if (fd < 0)
     {
       return -errno;
@@ -567,14 +568,44 @@ int rpmsg_tun_connect(const char *cpu, const char *name)
 
   /* Connect to remote server */
 
-  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
+  ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
+  if (ret < 0)
+    {
+      if (errno == EINPROGRESS)
+        {
+          /* Non-blocking socket, wait for connection completion */
+
+          struct pollfd pfd;
+
+          memset(&pfd, 0, sizeof(pfd));
+          pfd.fd = fd;
+          pfd.events = POLLOUT;
+
+          /* Wait for POLLOUT event (connection ready) */
+
+          ret = poll(&pfd, 1, -1);
+          if (ret <= 0 || (pfd.revents & (POLLERR | POLLHUP)))
+            {
+              goto error;
+            }
+        }
+      else if (errno != EISCONN)
+        {
+          goto error;
+        }
+    }
+
+  if (ioctl(fd, FIONBIO, (uintptr_t)&on) < 0)
     {
       close(fd);
       return -errno;
     }
 
-  ioctl(fd, FIONBIO, (uintptr_t)&on);
   return fd;
+
+error:
+  close(fd);
+  return -errno;
 }
 
 /****************************************************************************
@@ -624,12 +655,40 @@ int rpmsg_tun_accept(const char *name)
       goto out;
     }
 
-  /* Accept the connection */
+  /* Wait for an incoming connection using poll(), then accept it.  Using
+   * poll() blocks efficiently in the kernel until a connection is pending,
+   * which avoids repeatedly calling into the rpmsg driver via accept() and
+   * busy-looping on EAGAIN.  A spurious POLLIN that yields EAGAIN simply
+   * loops back to poll().
+   */
 
-  ret = accept4(fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
-  if (ret < 0)
+  for (; ; )
     {
-      goto out;
+      struct pollfd pfd;
+
+      memset(&pfd, 0, sizeof(pfd));
+      pfd.fd = fd;
+      pfd.events = POLLIN;
+
+      if (poll(&pfd, 1, -1) < 0)
+        {
+          if (errno == EINTR)
+            {
+              continue;
+            }
+
+          goto out;
+        }
+
+      ret = accept4(fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
+      if (ret >= 0)
+        {
+          break;
+        }
+      else if (errno != EAGAIN && errno != EINTR)
+        {
+          goto out;
+        }
     }
 
   close(fd);

Reply via email to