Skip to content

drivers/serial: Support TIOCGWINSZ and TIOCSWINSZ - #21795

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:tty-winsize-up
Nov 13, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:tty-winsize-up

Conversation

@casaroli

@casaroli casaroli commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

depends-on: /pull/21778
depends-on: /pull/21800

Summary

TIOCGWINSZ and TIOCSWINSZ worked only on the telnet driver. The serial driver and the pseudo-terminal driver returned ENOTTY, so a program could not get the terminal size, tcgetwinsize() failed on a serial console, and openpty() could not set the size.

This PR:

  • keeps a struct winsize in each serial device (struct uart_dev_s);
  • keeps one size for the two ends of a pseudo-terminal, because the size belongs to the terminal, not to one end;
  • sends SIGWINCH to the foreground PID when the size changes (for a pseudo-terminal: the foreground PID of the slave), if CONFIG_TTY_SIGINT or CONFIG_TTY_SIGTSTP keeps that PID. The same size again sends no signal. The pseudo-terminal sends the signal after it releases its lock: in a flat build the signal handler of the caller runs inside nxsig_kill(), and a handler usually calls TIOCGWINSZ again.

The size is 0 rows and 0 columns until a program sets it. The second commit describes the ioctls in Documentation/os/drivers/character/serial.rst.

Impact

New ioctls on serial and pseudo-terminal devices. struct uart_dev_s and the pseudo-terminal device pair get a struct winsize (8 bytes each). No new option. No change for programs that do not use the ioctls; SIGWINCH has no default action, so a program without a handler ignores it. All build modes: the drivers read and write the struct winsize of the caller as they do for TCGETS and TCSETS.

Testing

Test program winsize.c (the SIGWINCH handler reads the size again with TIOCGWINSZ on the same terminal), with CONFIG_PSEUDOTERM, CONFIG_PSEUDOTERM_SUSV1, CONFIG_SERIAL_TERMIOS, CONFIG_TTY_SIGINT, CONFIG_TTY_SIGTSTP and CONFIG_SIG_DEFAULT, on qemu-armv8a:knsh (kernel build, the program from /system/bin) and qemu-armv8a:nsh (flat build, the program as a builtin), QEMU with HVF:

Case master (knsh and nsh) this PR (knsh and nsh)
serial console: set + get, 1 SIGWINCH, the handler sees the new size ENOTTY PASS
serial console: the same size again, no SIGWINCH not run (ENOTTY above) PASS
openpty() size, read on the slave ENOTTY 24x80, PASS
the same size on the master ENOTTY 24x80, PASS
TIOCSWINSZ on the master: the slave sees it, 1 SIGWINCH, the handler sees the new size ENOTTY PASS
the same size again: no SIGWINCH ENOTTY PASS
TIOCSWINSZ on the slave: the master sees it, 1 SIGWINCH, the handler sees the new size ENOTTY PASS
SIGWINCH with the default action: the program goes on ENOTTY PASS
(knsh) a fork() child is the foreground process of the slave: TIOCSWINSZ on the master in the parent sends it SIGWINCH FAIL PASS

ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh with these options, on master and with this PR.

Builds with -Werror, each OK: qemu-armv8a:nsh (flat, arm64) without the options, with all of them, with CONFIG_PSEUDOTERM but without CONFIG_TTY_SIGINT, CONFIG_TTY_SIGTSTP and CONFIG_SERIAL_TERMIOS, and with CONFIG_TTY_SIGTSTP only; qemu-armv8a:knsh (kernel, arm64) without the options and with them but without CONFIG_SERIAL_TERMIOS; with all the options: mps2-an500:knsh (protected, Cortex-M7), rv-virt:pnsh64 (protected, rv64), rv-virt:knsh64 (kernel, rv64), sim:nsh and esp32s3-devkit:nsh (Xtensa). lm3s6965-ek:qemu-protected builds as it is; with the options its kernel flash overflows on master too (by 5984 bytes; 6200 with this PR).

tools/checkpatch.sh -c -u -m -g passes.

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]>
Describe TIOCGWINSZ, TIOCSWINSZ and SIGWINCH for the serial and
pseudo-terminal drivers.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@casaroli
casaroli marked this pull request as draft November 12, 2026 14:08
@casaroli
casaroli marked this pull request as ready for review November 12, 2026 14:08
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: M The size of the change in this PR is medium labels Nov 12, 2026
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/43217127171

@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .data +36 B, .text +230 B (+0.4%, 68,256 B / 262,144 B, total: 26% used)
  • sram: .data +36 B (+1.3%, 2,841 B / 8,192 B, total: 35% used)

esp32-devkitc

  • ROM: .dram0.data +12 B, .flash.rodata +72 B, .flash.text +364 B (+0.3%, 131,756 B / 4,194,272 B, total: 3% used)
  • dram0_0_seg: .dram0.data +12 B (+0.1%, 10,060 B / 180,736 B, total: 6% used)
  • drom0_0_seg: .flash.rodata +72 B (+0.5%, 13,908 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +364 B (+0.4%, 97,600 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .data +16 B, .text +64 B (+0.1%, 87,748 B / 4,194,304 B, total: 2% used)
  • sram: .bss +16 B, .data +16 B (+0.8%, 3,996 B / 16,384 B, total: 24% used)

mirtoo

  • kseg0_progmem: .data +16 B, .text +500 B (+0.7%, 70,660 B / 131,072 B, total: 54% used)
  • kseg1_datamem: .data +16 B (+0.6%, 2,616 B / 32,256 B, total: 8% used)

qemu-armv8a

  • Code: .rodata +24 B, .text.pl011_txint +20 B, .text.uart_close +20 B, .text.uart_ioctl +136 B, .text.uart_open +32 B, .text.uart_poll +28 B, .text.uart_poll_notify +12 B, .text.uart_putxmitbuf +64 B, .text.uart_readv +108 B, .text.uart_register +8 B, .text.uart_tcdrain.isra.0 +52 B, .text.uart_writev +12 B, .text.uart_xmitchars +36 B (+0.3%, 366,444 B)
  • Data: .data.g_uart1port +16 B, .data.g_version +2 B (+0.0%, 77,943 B)

qemu-intel64

  • Code: .text +676 B (+0.0%, 8,699,171 B)
  • Data: .rodata +128 B (+0.1%, 127,033 B)

rx65n-rsk2mb

  • RAM: .data +16 B (+0.2%, 6,740 B / 262,144 B, total: 3% used)
  • ROM: .rodata +20 B, .text +192 B (+0.2%, 93,112 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +1,120 B (+0.3%, 395,168 B)
  • Data: .data +48 B (+0.4%, 11,995 B)

stm32-nucleo-f103rb

  • flash: .data +16 B, .text +320 B (+0.9%, 36,240 B / 131,072 B, total: 28% used)
  • sram: .data +16 B (+0.6%, 2,484 B / 20,480 B, total: 12% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

@github-actions

Copy link
Copy Markdown

@xiaoxiang781216
xiaoxiang781216 merged commit ce05d0a into apache:master Nov 13, 2026
51 of 64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants