]>
Commit | Line | Data |
---|---|---|
d2a1ec20 | 1 | /* Basic tests for Linux pidfd interfaces. |
dff8da6b | 2 | Copyright (C) 2022-2024 Free Software Foundation, Inc. |
d2a1ec20 AZ |
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 | #include <errno.h> | |
20 | #include <fcntl.h> | |
e7190fc7 | 21 | #include <limits.h> |
d2a1ec20 AZ |
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> | |
e7190fc7 AZN |
31 | #include <stdlib.h> |
32 | #include <unistd.h> | |
d2a1ec20 AZ |
33 | |
34 | #define REMOTE_PATH "/dev/null" | |
35 | ||
36 | /* The pair of sockets used for coordination. The subprocess uses | |
37 | sockets[1]. */ | |
38 | static int sockets[2]; | |
39 | ||
40 | static pid_t ppid; | |
41 | static uid_t puid; | |
42 | ||
43 | static void | |
44 | sighandler (int sig) | |
45 | { | |
46 | } | |
47 | ||
48 | static void | |
49 | subprocess (void) | |
50 | { | |
51 | xsignal (SIGUSR1, sighandler); | |
52 | xsignal (SIGUSR2, sighandler); | |
53 | ||
54 | /* Check first pidfd_send_signal with default NULL siginfo_t argument. */ | |
55 | { | |
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); | |
66 | } | |
67 | ||
68 | /* Check second pidfd_send_signal with crafted siginfo_t argument. */ | |
69 | { | |
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); | |
80 | } | |
81 | ||
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); | |
85 | ||
86 | /* Wait for final pidfd_send_signal. */ | |
87 | pause (); | |
88 | ||
89 | _exit (0); | |
90 | } | |
91 | ||
92 | static int | |
93 | do_test (void) | |
94 | { | |
95 | { | |
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"); | |
103 | } | |
104 | ||
105 | ppid = getpid (); | |
106 | puid = getuid (); | |
107 | ||
e7190fc7 AZN |
108 | /* Sanity check for invalid inputs. */ |
109 | TEST_COMPARE (pidfd_getpid (-1), -1); | |
110 | TEST_COMPARE (errno, EBADF); | |
111 | ||
112 | { | |
113 | pid_t pid = pidfd_getpid (STDOUT_FILENO); | |
114 | TEST_COMPARE (pid, -1); | |
115 | TEST_COMPARE (errno, EBADF); | |
116 | } | |
117 | ||
118 | /* Check if pidfd_getpid returns ESRCH for exited subprocess. */ | |
119 | { | |
120 | pid_t pidfork = xfork (); | |
121 | if (pidfork == 0) | |
122 | _exit (EXIT_SUCCESS); | |
123 | int pidfork_pidfd = pidfd_open (pidfork, 0); | |
124 | ||
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); | |
131 | ||
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); | |
137 | ||
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); | |
142 | ||
143 | xclose (pidfork_pidfd); | |
144 | } | |
145 | ||
d2a1ec20 AZ |
146 | TEST_COMPARE (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets), 0); |
147 | ||
148 | pid_t pid = xfork (); | |
149 | if (pid == 0) | |
150 | { | |
151 | xclose (sockets[0]); | |
152 | subprocess (); | |
153 | } | |
154 | xclose (sockets[1]); | |
155 | ||
156 | TEST_COMPARE (pidfd_open (-1, 0), -1); | |
157 | TEST_COMPARE (errno, EINVAL); | |
158 | ||
159 | int pidfd = pidfd_open (pid, 0); | |
160 | TEST_VERIFY (pidfd != -1); | |
161 | ||
e7190fc7 AZN |
162 | TEST_COMPARE (pidfd_getpid (INT_MAX), -1); |
163 | { | |
164 | pid_t querypid = pidfd_getpid (pidfd); | |
165 | TEST_COMPARE (querypid, pid); | |
166 | } | |
167 | ||
d2a1ec20 AZ |
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); | |
171 | ||
172 | /* Wait for second sigtimedwait. */ | |
173 | support_process_state_wait (pid, support_process_state_sleeping); | |
174 | { | |
175 | siginfo_t info = | |
176 | { | |
177 | .si_signo = SIGUSR2, | |
178 | .si_errno = EAGAIN, | |
179 | .si_code = -10, | |
180 | .si_pid = ppid, | |
181 | .si_uid = puid | |
182 | }; | |
183 | TEST_COMPARE (pidfd_send_signal (pidfd, SIGUSR2, &info, 0), 0); | |
184 | } | |
185 | ||
186 | /* Get remote file descriptor to check for pidfd_getfd. */ | |
187 | { | |
188 | int remote_fd; | |
189 | xrecvfrom (sockets[0], &remote_fd, sizeof (remote_fd), 0, NULL, 0); | |
190 | ||
191 | int fd = pidfd_getfd (pidfd, remote_fd, 0); | |
325ba824 MW |
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) | |
f82e05eb FW |
197 | { |
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"); | |
201 | } | |
d2a1ec20 AZ |
202 | TEST_VERIFY (fd > 0); |
203 | ||
204 | char *path = xasprintf ("/proc/%d/fd/%d", pid, remote_fd); | |
205 | char *resolved = xreadlink (path); | |
206 | TEST_COMPARE_STRING (resolved, REMOTE_PATH); | |
207 | ||
208 | int remote_fd_mode = fcntl (fd, F_GETFL); | |
209 | TEST_VERIFY (remote_fd_mode != -1); | |
210 | TEST_VERIFY (remote_fd_mode & O_WRONLY); | |
211 | ||
212 | int remote_fd_flags = fcntl (fd, F_GETFD); | |
213 | TEST_VERIFY (remote_fd_flags != -1); | |
214 | TEST_VERIFY (remote_fd_flags & FD_CLOEXEC); | |
215 | } | |
216 | ||
217 | TEST_COMPARE (pidfd_send_signal (pidfd, SIGKILL, NULL, 0), 0); | |
218 | { | |
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); | |
224 | } | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
229 | #include <support/test-driver.c> |