1/* Wrapper for file descriptors that refers to a process functions.
2 Copyright (C) 2022-2026 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _PIDFD_H
20
21#include <fcntl.h>
22#include <bits/types/siginfo_t.h>
23#include <sys/ioctl.h>
24
25// zig patch: check target glibc version
26#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 36) || __GLIBC__ > 2
27
28#define PIDFD_NONBLOCK O_NONBLOCK
29#define PIDFD_THREAD O_EXCL
30
31#define PIDFD_SIGNAL_THREAD (1UL << 0)
32#define PIDFD_SIGNAL_THREAD_GROUP (1UL << 1)
33#define PIDFD_SIGNAL_PROCESS_GROUP (1UL << 2)
34
35#define PIDFS_IOCTL_MAGIC 0xFF
36
37#define PIDFD_GET_CGROUP_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 1)
38#define PIDFD_GET_IPC_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 2)
39#define PIDFD_GET_MNT_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 3)
40#define PIDFD_GET_NET_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 4)
41#define PIDFD_GET_PID_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 5)
42#define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 6)
43#define PIDFD_GET_TIME_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 7)
44#define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8)
45#define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9)
46#define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10)
47
48/* Sentinels to avoid allocating a file descriptor to refer to own process. */
49#define PIDFD_SELF_THREAD -10000
50#define PIDFD_SELF_THREAD_GROUP -10001
51#define PIDFD_SELF PIDFD_SELF_THREAD
52#define PIDFD_SELF_PROCESS PIDFD_SELF_THREAD_GROUP
53
54
55/* Flags for pidfd_info. */
56
57/* Always returned, even if not requested */
58#define PIDFD_INFO_PID (1UL << 0)
59/* Always returned, even if not requested */
60#define PIDFD_INFO_CREDS (1UL << 1)
61/* Always returned if available, even if not requested */
62#define PIDFD_INFO_CGROUPID (1UL << 2)
63/* Only returned if requested. */
64#define PIDFD_INFO_EXIT (1UL << 3)
65/* Only returned if requested. */
66#define PIDFD_INFO_COREDUMP (1UL << 4)
67// zig patch: check target glibc version
68#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 44) || __GLIBC__ > 2
69/* Want/got supported mask flags */
70#define PIDFD_INFO_SUPPORTED_MASK (1UL << 5)
71/* Always returned if PIDFD_INFO_COREDUMP is requested. */
72#define PIDFD_INFO_COREDUMP_SIGNAL (1UL << 6)
73/* Always returned if PIDFD_INFO_COREDUMP is requested. */
74#define PIDFD_INFO_COREDUMP_CODE (1UL << 7)
75#endif /* (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 44) || __GLIBC__ > 2 */
76
77
78/* Value for coredump_mask in pidfd_info. Only valid if PIDFD_INFO_COREDUMP
79 is set in mask. */
80
81/* Did crash and... */
82#define PIDFD_COREDUMPED (1U << 0)
83/* coredumping generation was skipped. */
84#define PIDFD_COREDUMP_SKIP (1U << 1)
85/* coredump was done as the user. */
86#define PIDFD_COREDUMP_USER (1U << 2)
87/* coredump was done as root. */
88#define PIDFD_COREDUMP_ROOT (1U << 3)
89
90struct pidfd_info
91{
92 __uint64_t mask;
93 __uint64_t cgroupid;
94 __uint32_t pid;
95 __uint32_t tgid;
96 __uint32_t ppid;
97 __uint32_t ruid;
98 __uint32_t rgid;
99 __uint32_t euid;
100 __uint32_t egid;
101 __uint32_t suid;
102 __uint32_t sgid;
103 __uint32_t fsuid;
104 __uint32_t fsgid;
105 __int32_t exit_code;
106 __uint32_t coredump_mask;
107// zig patch: check target glibc version
108#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 44) || __GLIBC__ > 2
109 __uint32_t coredump_signal;
110 __uint32_t coredump_code;
111 __uint32_t coredump_pad;
112 __uint64_t supported_mask;
113#else
114 __uint32_t __spare1;
115#endif /* (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 44) || __GLIBC__ > 2 */
116};
117
118/* sizeof first published struct */
119#define PIDFD_INFO_SIZE_VER0 64
120// zig patch: check target glibc version
121#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 44) || __GLIBC__ > 2
122/* sizeof second published struct */
123#define PIDFD_INFO_SIZE_VER1 72
124/* sizeof third published struct */
125#define PIDFD_INFO_SIZE_VER2 80
126/* sizeof fourth published struct */
127#define PIDFD_INFO_SIZE_VER3 88
128#endif /* (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 44) || __GLIBC__ > 2 */
129
130#define PIDFD_GET_INFO _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info)
131
132/* Returns a file descriptor that refers to the process PID. The
133 close-on-exec is set on the file descriptor. */
134extern int pidfd_open (__pid_t __pid, unsigned int __flags) __THROW;
135
136/* Duplicates an existing file descriptor TARGETFD in the process referred
137 by the PIDFD file descriptor PIDFD.
138
139 The FLAGS argument is reserved for future use, it must be specified
140 as 0. */
141extern int pidfd_getfd (int __pidfd, int __targetfd,
142 unsigned int __flags) __THROW;
143
144/* Sends the signal SIG to the target process referred by the PIDFD. If
145 INFO points to a siginfo_t buffer, it will be populated. */
146extern int pidfd_send_signal (int __pidfd, int __sig, siginfo_t *__info,
147 unsigned int __flags) __THROW;
148
149#endif /* (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 36) || __GLIBC__ > 2 */
150
151// zig patch: check target glibc version
152#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 39) || __GLIBC__ > 2
153
154/* Query the process ID (PID) from process descriptor FD. Return the PID
155 or -1 in case of an error. */
156extern pid_t pidfd_getpid (int __fd) __THROW;
157
158#endif /* (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 36) || __GLIBC__ > 2 */
159
160#endif /* _PIDFD_H */