Skip to content

drivers/serial: add job-control TTY ioctls#19184

Merged
acassis merged 1 commit into
apache:masterfrom
xiaoxiang781216:upstream-job-control-tty
Jun 23, 2026
Merged

drivers/serial: add job-control TTY ioctls#19184
acassis merged 1 commit into
apache:masterfrom
xiaoxiang781216:upstream-job-control-tty

Conversation

@xiaoxiang781216

@xiaoxiang781216 xiaoxiang781216 commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Add lightweight job-control/controlling-terminal support for serial and PTY drivers on systems without a real POSIX session/process-group model.

NuttX currently treats each task as its own process group. This patch keeps that model (pgrp == pid) and adds the missing APIs/ioctls needed by portable software such as dropbear/socat:

  • Add TIOCGPGRP, TIOCSPGRP, and TIOCGSID ioctl numbers.
  • Extend serial.c and pty.c to support:
    • TIOCSCTTY: arg > 0 preserves the existing NuttX convention of passing an explicit target PID (used by NSH for foreground commands), while arg == 0 follows POSIX-style flag semantics and selects the calling task.
    • TIOCGPGRP / TIOCGSID: return the current foreground PID.
    • TIOCSPGRP: set the foreground PID (or caller when pgrp is zero).
  • Add libc wrappers:
    • tcgetpgrp(), tcsetpgrp(), tcgetsid() over the new ioctls.
    • setsid(), getsid(), setpgid() stubs consistent with the existing single-member process-group model.
  • Document the behavior and limitations in Documentation/components/drivers/character/serial.rst.

Rationale

This avoids regressing existing NSH behavior where TIOCSCTTY is used to register a spawned foreground command as the Ctrl-C/Ctrl-Z signal target, while also supporting the POSIX-style ioctl(fd, TIOCSCTTY, 0) pattern used by dropbear and similar software.

Group-broadcast signaling (kill(-pgrp, signo)) remains unsupported, so TTY-generated signals still target a single foreground PID.

Testing

sim:nsh boot and ctrl+c work as before.

@github-actions github-actions Bot added Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Jun 21, 2026
@github-actions

github-actions Bot commented Jun 21, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@xiaoxiang781216
xiaoxiang781216 force-pushed the upstream-job-control-tty branch from 146ac2a to c776065 Compare June 21, 2026 03:20
NuttX has no real session/process-group abstraction, so the TTY layer
collapses the foreground process group onto the single dev->pid field
(pgrp == pid, one member per group).  Extend the controlling-terminal
support so portable software (e.g. dropbear, socat) that relies on
job-control primitives works without losing the existing NuttX-specific
behaviour.

Driver (serial.c, pty.c):
- TIOCSCTTY now accepts a flag: arg > 0 keeps the historical "target
  PID in arg" semantics (NSH registers the foreground command it just
  spawned), while arg == 0 selects the calling task via
  nxsched_getpid(), matching the POSIX flag convention used by
  dropbear/socat/apue.  This preserves all existing callers and makes
  the previously-dead arg==0 path deliver SIGINT correctly.
- Add TIOCGPGRP/TIOCGSID (return dev->pid) and TIOCSPGRP (set it).
- pty.c gains the same handlers against pd_pid and includes
  nuttx/sched.h for nxsched_getpid().

ioctl numbers (tioctl.h): TIOCGPGRP/TIOCSPGRP/TIOCGSID at 0x37-0x39.

libc wrappers:
- termios: tcgetpgrp(), tcsetpgrp(), tcgetsid() over the new ioctls.
- unistd: setsid()/getsid()/setpgid() stubs consistent with the
  existing getpgrp()/getpgid() single-session model (sid == pgid ==
  pid; setpgid only succeeds for pgid == pid).

Declare the new prototypes in unistd.h (tcgetsid was already in
termios.h) and register all sources in the Make.defs/CMakeLists.

Group-broadcast signalling (kill(-pgrp)) remains unsupported, so
tty signals still target the single dev->pid; a real session/process
group model is left as a follow-up.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
@xiaoxiang781216
xiaoxiang781216 force-pushed the upstream-job-control-tty branch from c776065 to 87222fd Compare June 21, 2026 04:08
@acassis

acassis commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

@xiaoxiang781216 so Ctrl+Z will not "freeZe" a process the same way it does on Linux?

For example on Linux:

$ sleep 10; echo "Finished"
^Z
[1]+  Stopped                 sleep 10
Finished
$ echo "I can do something else now"
I can do something else now
$

@xiaoxiang781216

Copy link
Copy Markdown
Contributor Author

@FelipeMdeO this patch may help you port dropbear.

@acassis
acassis merged commit 95063bd into apache:master Jun 23, 2026
54 checks passed
FelipeMdeO added a commit to FelipeMdeO/nuttx-apps that referenced this pull request Jun 23, 2026
Integrated SSH daemon authenticating against FSUTILS_PASSWD, with an
ECDSA P-256 host key and an NSH session over a PTY per connection. Built
from the upstream Dropbear tarball (pinned commit) and patched for
NuttX, using Dropbear's bundled libtomcrypt for all crypto. setsid() is
taken from NuttX libc (apache/nuttx#19184) instead of a local prototype.

Signed-off-by: Felipe Moura <moura.fmo@gmail.com>
FelipeMdeO added a commit to FelipeMdeO/nuttx-apps that referenced this pull request Jun 24, 2026
Integrated SSH daemon authenticating against FSUTILS_PASSWD, with an
ECDSA P-256 host key and an NSH session over a PTY per connection. Built
from the upstream Dropbear tarball (pinned commit) and patched for
NuttX, using Dropbear's bundled libtomcrypt for all crypto. setsid()
(apache/nuttx#19184) and link() now come from NuttX, not local stubs.

Signed-off-by: Felipe Moura <moura.fmo@gmail.com>
FelipeMdeO added a commit to FelipeMdeO/nuttx-apps that referenced this pull request Jun 24, 2026
Integrated SSH daemon authenticating against FSUTILS_PASSWD, with an
ECDSA P-256 host key and an NSH session over a PTY per connection. Built
from the upstream Dropbear tarball (pinned commit) and patched for
NuttX, using Dropbear's bundled libtomcrypt for all crypto. setsid()
(apache/nuttx#19184) and link() now come from NuttX, not local stubs.

Signed-off-by: Felipe Moura <moura.fmo@gmail.com>
FelipeMdeO added a commit to FelipeMdeO/nuttx-apps that referenced this pull request Jun 25, 2026
Integrated SSH daemon authenticating against FSUTILS_PASSWD, with an
ECDSA P-256 host key and an NSH session over a PTY per connection. Built
from the upstream Dropbear tarball (pinned commit) and patched for
NuttX, using Dropbear's bundled libtomcrypt for all crypto. setsid()
(apache/nuttx#19184) and link() now come from NuttX, not local stubs.

Signed-off-by: Felipe Moura <moura.fmo@gmail.com>
acassis pushed a commit to apache/nuttx-apps that referenced this pull request Jun 27, 2026
Integrated SSH daemon authenticating against FSUTILS_PASSWD, with an
ECDSA P-256 host key and an NSH session over a PTY per connection. Built
from the upstream Dropbear tarball (pinned commit) and patched for
NuttX, using Dropbear's bundled libtomcrypt for all crypto. setsid()
(apache/nuttx#19184) and link() now come from NuttX, not local stubs.

Signed-off-by: Felipe Moura <moura.fmo@gmail.com>
@xiaoxiang781216
xiaoxiang781216 deleted the upstream-job-control-tty branch June 28, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants