linux: Add pidfd_getpid
[glibc.git] / sysdeps / unix / sysv / linux / tst-pidfd.c
blob5f635a3feca2b253d0ecb9ec19a317a9b08bbacf
1 /* Basic tests for Linux pidfd interfaces.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <support/capture_subprocess.h>
23 #include <support/check.h>
24 #include <support/process_state.h>
25 #include <support/support.h>
26 #include <support/xsignal.h>
27 #include <support/xunistd.h>
28 #include <support/xsocket.h>
29 #include <sys/pidfd.h>
30 #include <sys/wait.h>
31 #include <stdlib.h>
32 #include <unistd.h>
34 #define REMOTE_PATH "/dev/null"
36 /* The pair of sockets used for coordination. The subprocess uses
37 sockets[1]. */
38 static int sockets[2];
40 static pid_t ppid;
41 static uid_t puid;
43 static void
44 sighandler (int sig)
48 static void
49 subprocess (void)
51 xsignal (SIGUSR1, sighandler);
52 xsignal (SIGUSR2, sighandler);
54 /* Check first pidfd_send_signal with default NULL siginfo_t argument. */
56 sigset_t set;
57 sigemptyset (&set);
58 sigaddset (&set, SIGUSR1);
59 siginfo_t info;
60 TEST_COMPARE (sigtimedwait (&set, &info, NULL), SIGUSR1);
61 TEST_COMPARE (info.si_signo, SIGUSR1);
62 TEST_COMPARE (info.si_errno, 0);
63 TEST_COMPARE (info.si_code, SI_USER);
64 TEST_COMPARE (info.si_pid, ppid);
65 TEST_COMPARE (info.si_uid, puid);
68 /* Check second pidfd_send_signal with crafted siginfo_t argument. */
70 sigset_t set;
71 sigemptyset (&set);
72 sigaddset (&set, SIGUSR2);
73 siginfo_t info;
74 TEST_COMPARE (sigtimedwait (&set, &info, NULL), SIGUSR2);
75 TEST_COMPARE (info.si_signo, SIGUSR2);
76 TEST_COMPARE (info.si_errno, EAGAIN);
77 TEST_COMPARE (info.si_code, -10);
78 TEST_COMPARE (info.si_pid, ppid);
79 TEST_COMPARE (info.si_uid, puid);
82 /* Send a local file descriptor value to check pidfd_getfd. */
83 int remote_fd = xopen (REMOTE_PATH, O_WRONLY | O_CLOEXEC, 0);
84 xsendto (sockets[1], &remote_fd, sizeof (remote_fd), 0, NULL, 0);
86 /* Wait for final pidfd_send_signal. */
87 pause ();
89 _exit (0);
92 static int
93 do_test (void)
96 /* The pidfd_getfd syscall was the last in the set of pidfd related
97 syscalls added to the kernel. Use pidfd_getfd to decide if this
98 kernel has pidfd support that we can test. */
99 int r = pidfd_getfd (0, 0, 1);
100 TEST_VERIFY_EXIT (r == -1);
101 if (errno == ENOSYS)
102 FAIL_UNSUPPORTED ("kernel does not support pidfd_getfd, skipping test");
105 ppid = getpid ();
106 puid = getuid ();
108 /* Sanity check for invalid inputs. */
109 TEST_COMPARE (pidfd_getpid (-1), -1);
110 TEST_COMPARE (errno, EBADF);
113 pid_t pid = pidfd_getpid (STDOUT_FILENO);
114 TEST_COMPARE (pid, -1);
115 TEST_COMPARE (errno, EBADF);
118 /* Check if pidfd_getpid returns ESRCH for exited subprocess. */
120 pid_t pidfork = xfork ();
121 if (pidfork == 0)
122 _exit (EXIT_SUCCESS);
123 int pidfork_pidfd = pidfd_open (pidfork, 0);
125 /* The process might be still running or already in zombie state, in
126 either case the PID is still allocated to the process. */
127 pid_t pid = pidfd_getpid (pidfork_pidfd);
128 TEST_COMPARE (pidfork, pid);
129 if (pid > 0)
130 support_process_state_wait (pid, support_process_state_zombie);
132 siginfo_t info;
133 TEST_COMPARE (waitid (P_PIDFD, pidfork_pidfd, &info, WEXITED), 0);
134 TEST_COMPARE (info.si_pid, pidfork);
135 TEST_COMPARE (info.si_status, 0);
136 TEST_COMPARE (info.si_code, CLD_EXITED);
138 /* Once the process is reaped the associated PID is not available. */
139 pid = pidfd_getpid (pidfork_pidfd);
140 TEST_COMPARE (pid, -1);
141 TEST_COMPARE (errno, ESRCH);
143 xclose (pidfork_pidfd);
146 TEST_COMPARE (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets), 0);
148 pid_t pid = xfork ();
149 if (pid == 0)
151 xclose (sockets[0]);
152 subprocess ();
154 xclose (sockets[1]);
156 TEST_COMPARE (pidfd_open (-1, 0), -1);
157 TEST_COMPARE (errno, EINVAL);
159 int pidfd = pidfd_open (pid, 0);
160 TEST_VERIFY (pidfd != -1);
162 TEST_COMPARE (pidfd_getpid (INT_MAX), -1);
164 pid_t querypid = pidfd_getpid (pidfd);
165 TEST_COMPARE (querypid, pid);
168 /* Wait for first sigtimedwait. */
169 support_process_state_wait (pid, support_process_state_sleeping);
170 TEST_COMPARE (pidfd_send_signal (pidfd, SIGUSR1, NULL, 0), 0);
172 /* Wait for second sigtimedwait. */
173 support_process_state_wait (pid, support_process_state_sleeping);
175 siginfo_t info =
177 .si_signo = SIGUSR2,
178 .si_errno = EAGAIN,
179 .si_code = -10,
180 .si_pid = ppid,
181 .si_uid = puid
183 TEST_COMPARE (pidfd_send_signal (pidfd, SIGUSR2, &info, 0), 0);
186 /* Get remote file descriptor to check for pidfd_getfd. */
188 int remote_fd;
189 xrecvfrom (sockets[0], &remote_fd, sizeof (remote_fd), 0, NULL, 0);
191 int fd = pidfd_getfd (pidfd, remote_fd, 0);
192 /* pidfd_getfd may fail with EPERM if the process does not have
193 PTRACE_MODE_ATTACH_REALCREDS permissions. This means the call
194 may be denied if the process doesn't have CAP_SYS_PTRACE or
195 if a LSM security_ptrace_access_check denies access. */
196 if (fd == -1 && errno == EPERM)
198 TEST_COMPARE (pidfd_send_signal (pidfd, SIGKILL, NULL, 0), 0);
199 FAIL_UNSUPPORTED ("don't have permission to use pidfd_getfd on pidfd, "
200 "skipping test");
202 TEST_VERIFY (fd > 0);
204 char *path = xasprintf ("/proc/%d/fd/%d", pid, remote_fd);
205 char *resolved = xreadlink (path);
206 TEST_COMPARE_STRING (resolved, REMOTE_PATH);
208 int remote_fd_mode = fcntl (fd, F_GETFL);
209 TEST_VERIFY (remote_fd_mode != -1);
210 TEST_VERIFY (remote_fd_mode & O_WRONLY);
212 int remote_fd_flags = fcntl (fd, F_GETFD);
213 TEST_VERIFY (remote_fd_flags != -1);
214 TEST_VERIFY (remote_fd_flags & FD_CLOEXEC);
217 TEST_COMPARE (pidfd_send_signal (pidfd, SIGKILL, NULL, 0), 0);
219 siginfo_t info;
220 int r = waitid (P_PIDFD, pidfd, &info, WEXITED);
221 TEST_COMPARE (r, 0);
222 TEST_COMPARE (info.si_status, SIGKILL);
223 TEST_COMPARE (info.si_code, CLD_KILLED);
226 return 0;
229 #include <support/test-driver.c>