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 1ebffe4cdfc977ca065e654ed856ad207186f450
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 20 20:55:59 2026 +0200

    drivers/serial: Support TIOCGWINSZ and TIOCSWINSZ.
    
    The serial and pseudo-terminal drivers returned ENOTTY for TIOCGWINSZ
    and TIOCSWINSZ, so programs could not get the terminal size, and
    openpty() could not set it.
    
    The serial upper half keeps a struct winsize per device.  The PTY keeps
    one size for the master and the slave, as the size belongs to the
    terminal and not to one end.  The size is 0x0 until a program sets it.
    A change of the size sends SIGWINCH to the foreground PID (the slave's
    for a PTY) when CONFIG_TTY_SIGINT or CONFIG_TTY_SIGTSTP keeps that PID.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 drivers/serial/pty.c          | 56 +++++++++++++++++++++++++++++++++++++++++++
 drivers/serial/serial.c       | 40 +++++++++++++++++++++++++++++++
 include/nuttx/serial/serial.h |  4 ++++
 3 files changed, 100 insertions(+)

diff --git a/drivers/serial/pty.c b/drivers/serial/pty.c
index 8c808a61c13..54ac1a1aa75 100644
--- a/drivers/serial/pty.c
+++ b/drivers/serial/pty.c
@@ -125,6 +125,7 @@ struct pty_devpair_s
   bool pp_unlinked;             /* File has been unlinked */
   uint8_t pp_minor;             /* Minor device number */
   uint16_t pp_nopen;            /* Open file count */
+  struct winsize pp_winsize;    /* Window size, same for both ends */
   sem_t pp_slavesem;            /* Slave lock semaphore */
   mutex_t pp_lock;              /* Mutual exclusion */
 };
@@ -851,6 +852,9 @@ static int pty_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
   FAR struct inode *inode;
   FAR struct pty_dev_s *dev;
   FAR struct pty_devpair_s *devpair;
+#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
+  pid_t winchpid = INVALID_PROCESS_ID;
+#endif
   int ret;
 
   inode   = filep->f_inode;
@@ -1042,6 +1046,46 @@ static int pty_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
         }
         break;
 
+      case TIOCGWINSZ:
+        {
+          FAR struct winsize *ws = (FAR struct winsize *)arg;
+
+          if (ws == NULL)
+            {
+              ret = -EINVAL;
+              break;
+            }
+
+          *ws = devpair->pp_winsize;
+          ret = OK;
+        }
+        break;
+
+      /* A new size from either end sends SIGWINCH to the foreground
+       * process of the slave, after the lock is released.
+       */
+
+      case TIOCSWINSZ:
+        {
+          FAR const struct winsize *ws = (FAR const struct winsize *)arg;
+
+          if (ws == NULL)
+            {
+              ret = -EINVAL;
+              break;
+            }
+
+          ret = OK;
+          if (memcmp(&devpair->pp_winsize, ws, sizeof(*ws)) != 0)
+            {
+              devpair->pp_winsize = *ws;
+#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
+              winchpid = devpair->pp_slave.pd_pid;
+#endif
+            }
+        }
+        break;
+
 #if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
       /* Make the controlling terminal of the calling process */
 
@@ -1135,6 +1179,18 @@ static int pty_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
     }
 
   nxmutex_unlock(&devpair->pp_lock);
+
+#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
+  /* In a flat build, the signal handler of the caller runs in nxsig_kill()
+   * and can use this PTY.
+   */
+
+  if (winchpid > 0)
+    {
+      nxsig_kill(winchpid, SIGWINCH);
+    }
+#endif
+
   return ret;
 }
 
diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c
index e11096dc7a2..d4c9d1dc7fb 100644
--- a/drivers/serial/serial.c
+++ b/drivers/serial/serial.c
@@ -2105,6 +2105,46 @@ static int uart_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
             }
             break;
 
+          case TIOCGWINSZ:
+            {
+              FAR struct winsize *ws = (FAR struct winsize *)arg;
+
+              if (ws == NULL)
+                {
+                  ret = -EINVAL;
+                  break;
+                }
+
+              *ws = dev->winsize;
+              ret = 0;
+            }
+            break;
+
+          case TIOCSWINSZ:
+            {
+              FAR const struct winsize *ws =
+                (FAR const struct winsize *)arg;
+
+              if (ws == NULL)
+                {
+                  ret = -EINVAL;
+                  break;
+                }
+
+              ret = 0;
+              if (memcmp(&dev->winsize, ws, sizeof(*ws)) != 0)
+                {
+                  dev->winsize = *ws;
+#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
+                  if (dev->pid > 0)
+                    {
+                      nxsig_kill(dev->pid, SIGWINCH);
+                    }
+#endif
+                }
+            }
+            break;
+
 #if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP)
           /* Make the controlling terminal of the calling process */
 
diff --git a/include/nuttx/serial/serial.h b/include/nuttx/serial/serial.h
index 6d653b8b6b4..b3b9c97d248 100644
--- a/include/nuttx/serial/serial.h
+++ b/include/nuttx/serial/serial.h
@@ -362,6 +362,10 @@ struct uart_dev_s
   cc_t                 tc_vsusp;     /* c_cc[VSUSP] */
 #endif
 
+  /* Terminal window size (TIOCGWINSZ, TIOCSWINSZ) */
+
+  struct winsize       winsize;
+
   /* Semaphores & mutex */
 
   sem_t                xmitsem;      /* Wakeup user waiting for space in 
xmit.buffer */

Reply via email to