drivers/serial: add job-control TTY ioctls and libc wrappers

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>
This commit is contained in:
Xiang Xiao 2026-06-21 10:38:30 +08:00 committed by Alan C. Assis
parent 970f2f226a
commit 95063bde15
16 changed files with 523 additions and 9 deletions

View file

@ -335,7 +335,10 @@ struct uart_dev_s
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGTSTP) || \
defined(CONFIG_TTY_FORCE_PANIC) || defined(CONFIG_TTY_LAUNCH)
pid_t pid; /* Thread PID to receive signals (-1 if none) */
pid_t pid; /* Foreground process group / controlling-tty
* owner that receives tty signals. With no
* process-group support, pgrp == pid.
* (-1 if none) */
#endif
#ifdef CONFIG_TTY_FORCE_PANIC

View file

@ -87,7 +87,7 @@
/* Controlling TTY */
#define TIOCSCTTY _TIOC(0x0018) /* Make controlling TTY: int */
#define TIOCSCTTY _TIOC(0x0018) /* Make controlling TTY: int (0 == caller) */
#define TIOCNOTTY _TIOC(0x0019) /* Give up controllinog TTY: void */
/* Exclusive mode */
@ -200,6 +200,12 @@
#define SER_SWAP_ENABLED (1 << 0) /* Enable/disable RX/TX swap */
/* Process group / session (job control) */
#define TIOCGPGRP _TIOC(0x0037) /* Get foreground process group: FAR pid_t* */
#define TIOCSPGRP _TIOC(0x0038) /* Set foreground process group: FAR const pid_t* */
#define TIOCGSID _TIOC(0x0039) /* Get session leader: FAR pid_t* */
/****************************************************************************
* Public Type Definitions
****************************************************************************/

View file

@ -354,6 +354,11 @@ pid_t getpgid(pid_t pid);
pid_t getpgrp(void);
pid_t gettid(void);
pid_t getppid(void);
pid_t getsid(pid_t pid);
int setpgid(pid_t pid, pid_t pgid);
pid_t setsid(void);
pid_t tcgetpgrp(int fd);
int tcsetpgrp(int fd, pid_t pgrp);
void _exit(int status) noreturn_function;
unsigned int sleep(unsigned int seconds);
int usleep(useconds_t usec);