nuttx/libs/libc/unistd/lib_getsid.c
Xiang Xiao 95063bde15 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>
2026-06-23 16:26:53 -03:00

75 lines
2.4 KiB
C

/****************************************************************************
* libs/libc/unistd/lib_getsid.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <unistd.h>
#include <errno.h>
#include <nuttx/sched.h>
#include <nuttx/signal.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: getsid
*
* Description:
* Get the session ID of the process whose process ID is pid. NuttX does
* not implement sessions, so the session ID is the same as the process
* ID, mirroring getpgid().
*
* Input Parameters:
* pid - The process ID whose session ID is requested. A value of zero
* refers to the calling process.
*
* Returned Value:
* The session ID is returned on success. Otherwise, (pid_t)-1 is
* returned and errno is set: EINVAL if pid is negative, ESRCH if pid
* does not refer to an existing process.
*
****************************************************************************/
pid_t getsid(pid_t pid)
{
if (pid < 0)
{
set_errno(EINVAL);
return (pid_t)-1;
}
if (pid == 0)
{
return _SCHED_GETPID();
}
if (_SIG_KILL(pid, 0) < 0)
{
return (pid_t)-1;
}
return pid;
}