glibc/sysdeps/unix/sysv/linux/bits
Adhemerval Zanella Netto 0d6f9f6265 posix: Add pidfd_spawn and pidfd_spawnp (BZ 30349)
Returning a pidfd allows a process to keep a race-free handle for a
child process, otherwise, the caller will need to either use pidfd_open
(which still might be subject to TOCTOU) or keep the old racy interface
base on pid_t.

To correct use pifd_spawn, the kernel must support not only returning
the pidfd with clone/clone3 but also waitid (P_PIDFD) (added on Linux
5.4).  If kernel does not support the waitid, pidfd return ENOSYS.
It avoids the need to racy workarounds, such as reading the procfs
fdinfo to get the pid to use along with other wait interfaces.

These interfaces are similar to the posix_spawn and posix_spawnp, with
the only difference being it returns a process file descriptor (int)
instead of a process ID (pid_t).  Their prototypes are:

  int pidfd_spawn (int *restrict pidfd,
                   const char *restrict file,
                   const posix_spawn_file_actions_t *restrict facts,
                   const posix_spawnattr_t *restrict attrp,
                   char *const argv[restrict],
                   char *const envp[restrict])

  int pidfd_spawnp (int *restrict pidfd,
                    const char *restrict path,
                    const posix_spawn_file_actions_t *restrict facts,
                    const posix_spawnattr_t *restrict attrp,
                    char *const argv[restrict_arr],
                    char *const envp[restrict_arr]);

A new symbol is used instead of a posix_spawn extension to avoid
possible issues with language bindings that might track the return
argument lifetime.  Although on Linux pid_t and int are interchangeable,
POSIX only states that pid_t should be a signed integer.

Both symbols reuse the posix_spawn posix_spawn_file_actions_t and
posix_spawnattr_t, to void rehash posix_spawn API or add a new one. It
also means that both interfaces support the same attribute and file
actions, and a new flag or file action on posix_spawn is also added
automatically for pidfd_spawn.

Also, using posix_spawn plumbing allows the reusing of most of the
current testing with some changes:

  - waitid is used instead of waitpid since it is a more generic
    interface.

  - tst-posix_spawn-setsid.c is adapted to take into consideration that
    the caller can check for session id directly.  The test now spawns
itself and writes the session id as a file instead.

  - tst-spawn3.c need to know where pidfd_spawn is used so it keeps an
    extra file description unused.

Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PIDFD or waitid
support), Linux 5.4 (full support), and Linux 6.2.
Reviewed-by: Florian Weimer <fweimer@redhat.com>
2023-09-05 13:08:59 -03:00
..
types Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
a.out.h
dirent_ext.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
dirent.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
epoll.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
errno.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
eventfd.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
fcntl-linux.h Add F_SEAL_EXEC from Linux 6.3 to bits/fcntl-linux.h. 2023-08-28 14:51:39 -03:00
fcntl.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
in.h Add IP_PROTOCOL from Linux 6.4 to bits/in.h 2023-08-01 17:22:12 +00:00
initspin.h
inotify.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
ioctl-types.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
ioctls.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
ipc-perm.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
local_lim.h Fix misspellings in sysdeps/unix -- BZ 25337 2023-05-23 11:59:23 +00:00
mman_ext.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
mman-linux.h Fix misspellings in sysdeps/unix -- BZ 25337 2023-05-23 11:59:23 +00:00
mman-map-flags-generic.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
mman-shared.h Add MFD_NOEXEC_SEAL, MFD_EXEC from Linux 6.3 to bits/mman-shared.h 2023-05-26 15:04:51 +00:00
mman.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
mqueue.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
msq.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
param.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
poll.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
posix_opt.h Fix misspellings in sysdeps/unix -- BZ 25337 2023-05-23 11:59:23 +00:00
procfs-extra.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
procfs-id.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
procfs-prregset.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
procfs.h
pthread_stack_min-dynamic.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
pthread_stack_min.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
ptrace-shared.h Add PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG etc. from Linux 6.4 to sys/ptrace.h 2023-08-08 14:38:22 +00:00
resource.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
rseq.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
sched.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
sem.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
semaphore.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
shm.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
shmlba.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
sigaction.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
sigcontext.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
sigevent-consts.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
siginfo-arch.h
siginfo-consts-arch.h
siginfo-consts.h Fix misspellings in sysdeps/unix -- BZ 25337 2023-05-23 11:59:23 +00:00
signal_ext.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
signalfd.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
signum-arch.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
sigstack.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
sigstksz.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
socket_type.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
socket-constants.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
socket.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
spawn_ext.h posix: Add pidfd_spawn and pidfd_spawnp (BZ 30349) 2023-09-05 13:08:59 -03:00
ss_flags.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
stat.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
statfs.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
statvfs.h linux: statvfs: allocate spare for f_type 2023-08-08 09:29:06 -03:00
statx.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
stdio_lim.h libio: Do not autogenerate stdio_lim.h 2023-03-27 13:57:55 -03:00
struct_stat_time64_helper.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
struct_stat.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-baud.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-c_cc.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-c_cflag.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-c_iflag.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-c_lflag.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-c_oflag.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-misc.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-struct.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
termios-tcflow.h Fix misspellings in sysdeps/unix -- BZ 25337 2023-05-23 11:59:23 +00:00
termios.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
time.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
timerfd.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
timex.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
typesizes.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
uio_lim.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
uio-ext.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
unistd_ext.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
utsname.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
waitflags.h Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00