Debian Codegraph @

src/basic/pidref.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <poll.h>
4 #include <sys/wait.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "fiber-ops.h"
11 #include "format-util.h"
12 #include "hash-funcs.h"
13 #include "io-util.h"
14 #include "log.h"
15 #include "parse-util.h"
16 #include "pidfd-util.h"
17 #include "pidref.h"
18 #include "process-util.h"
19 #include "siphash24.h"
20 #include "time-util.h"
21
22 int pidref_acquire_pidfd_id(PidRef *pidref) {
23 int r;
24
25 assert(pidref);
26
27 if (!pidref_is_set(pidref))
28 return -ESRCH;
29
30 if (pidref_is_remote(pidref))
31 return -EREMOTE;
32
33 if (pidref->fd < 0)
34 return -ENOMEDIUM;
35
36 if (pidref->fd_id > 0)
37 return 0;
38
39 r = pidfd_get_inode_id(pidref->fd, &pidref->fd_id);
40 if (r < 0) {
41 if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
42 log_debug_errno(r, "Failed to get inode number of pidfd for pid " PID_FMT ": %m",
43 pidref->pid);
44 return r;
45 }
46
47 return 0;
48 }
49
50 bool pidref_equal(PidRef *a, PidRef *b) {
51
52 /* If this is the very same structure, it definitely refers to the same process */
53 if (a == b)
54 return true;
55
56 if (!pidref_is_set(a))
57 return !pidref_is_set(b);
58
59 if (!pidref_is_set(b))
60 return false;
61
62 if (a->pid != b->pid)
63 return false;
64
65 if (pidref_is_remote(a)) {
66 /* If one is remote and the other isn't, they are not the same */
67 if (!pidref_is_remote(b))
68 return false;
69
70 /* If both are remote, compare fd IDs if we have both, otherwise don't bother, and cut things short */
71 if (a->fd_id == 0 || b->fd_id == 0)
72 return true;
73 } else {
74 /* If the other side is remote, then this is not the same */
75 if (pidref_is_remote(b))
76 return false;
77
78 /* PID1 cannot exit, hence it cannot change pidfs ids, hence no point in comparing them, we
79 * can shortcut things */
80 if (a->pid == 1)
81 return true;
82
83 /* Try to compare pidfds using their inode numbers. This way we can ensure that we
84 * don't spuriously consider two PidRefs equal if the pid has been reused once. Note
85 * that we ignore all errors here, not only EOPNOTSUPP, as fstat() might fail due to
86 * many reasons. */
87 if (pidref_acquire_pidfd_id(a) < 0 || pidref_acquire_pidfd_id(b) < 0)
88 return true;
89 }
90
91 return a->fd_id == b->fd_id;
92 }
93
94 int pidref_set_pid(PidRef *pidref, pid_t pid) {
95 uint64_t pidfdid = 0;
96 int fd;
97
98 assert(pidref);
99
100 if (pid < 0)
101 return -ESRCH;
102 if (pid == 0) {
103 pid = getpid_cached();
104 (void) pidfd_get_inode_id_self_cached(&pidfdid);
105 }
106
107 fd = pidfd_open(pid, 0);
108 if (fd < 0) {
109 /* Graceful fallback in case the kernel is out of fds */
110 if (!ERRNO_IS_RESOURCE(errno))
111 return log_debug_errno(errno, "Failed to open pidfd for pid " PID_FMT ": %m", pid);
112
113 fd = -EBADF;
114 }
115
116 *pidref = (PidRef) {
117 .fd = fd,
118 .pid = pid,
119 .fd_id = pidfdid,
120 };
121
122 return 0;
123 }
124
125 int pidref_set_pid_and_pidfd_id(
126 PidRef *pidref,
127 pid_t pid,
128 uint64_t pidfd_id) {
129
130 int r;
131
132 assert(pidref);
133
134 _cleanup_(pidref_done) PidRef n = PIDREF_NULL;
135 r = pidref_set_pid(&n, pid);
136 if (r < 0)
137 return r;
138
139 if (pidfd_id > 0) {
140 r = pidref_acquire_pidfd_id(&n);
141 if (r < 0)
142 return r;
143
144 if (n.fd_id != pidfd_id)
145 return -ESRCH;
146 }
147
148 *pidref = TAKE_PIDREF(n);
149 return 0;
150 }
151
152 int pidref_set_pidstr(PidRef *pidref, const char *pid) {
153 pid_t nr;
154 int r;
155
156 assert(pidref);
157
158 r = parse_pid(pid, &nr);
159 if (r < 0)
160 return r;
161
162 return pidref_set_pid(pidref, nr);
163 }
164
165 int pidref_set_pidfd(PidRef *pidref, int fd) {
166 int r;
167
168 assert(pidref);
169
170 if (fd < 0)
171 return -EBADF;
172
173 int fd_copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
174 if (fd_copy < 0) {
175 pid_t pid;
176
177 if (!ERRNO_IS_RESOURCE(errno))
178 return -errno;
179
180 /* Graceful fallback if we are out of fds */
181 r = pidfd_get_pid(fd, &pid);
182 if (r < 0)
183 return r;
184
185 *pidref = PIDREF_MAKE_FROM_PID(pid);
186 return 0;
187 }
188
189 return pidref_set_pidfd_consume(pidref, fd_copy);
190 }
191
192 int pidref_set_pidfd_take(PidRef *pidref, int fd) {
193 pid_t pid;
194 int r;
195
196 assert(pidref);
197
198 if (fd < 0)
199 return -EBADF;
200
201 r = pidfd_get_pid(fd, &pid);
202 if (r < 0)
203 return r;
204
205 *pidref = (PidRef) {
206 .fd = fd,
207 .pid = pid,
208 };
209
210 return 0;
211 }
212
213 int pidref_set_pidfd_consume(PidRef *pidref, int fd) {
214 int r;
215
216 r = pidref_set_pidfd_take(pidref, fd);
217 if (r < 0)
218 safe_close(fd);
219
220 return r;
221 }
222
223 int pidref_set_parent(PidRef *ret) {
224 _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
225 pid_t ppid;
226 int r;
227
228 assert(ret);
229
230 /* Acquires a pidref to our parent process. Deals with the fact that parent processes might exit, and
231 * we get reparented to other processes, with our old parent's PID already being recycled. */
232
233 ppid = getppid();
234 for (;;) {
235 r = pidref_set_pid(&parent, ppid);
236 if (r < 0)
237 return r;
238
239 if (parent.fd < 0) /* If pidfds are not available, then we are done */
240 break;
241
242 pid_t now_ppid = getppid();
243 if (now_ppid == ppid) /* If our ppid is still the same, then we are done */
244 break;
245
246 /* Otherwise let's try again with the new ppid */
247 ppid = now_ppid;
248 pidref_done(&parent);
249 }
250
251 *ret = TAKE_PIDREF(parent);
252 return 0;
253 }
254
255 void pidref_done(PidRef *pidref) {
256 assert(pidref);
257
258 *pidref = (PidRef) {
259 .fd = safe_close(pidref->fd),
260 };
261 }
262
263 PidRef* pidref_free(PidRef *pidref) {
264 /* Regularly, this is an embedded structure. But sometimes we want it on the heap too */
265 if (!pidref)
266 return NULL;
267
268 pidref_done(pidref);
269 return mfree(pidref);
270 }
271
272 int pidref_copy(const PidRef *pidref, PidRef *ret) {
273 _cleanup_(pidref_done) PidRef copy = PIDREF_NULL;
274
275 /* If NULL is passed we'll generate a PidRef that refers to no process. This makes it easy to
276 * copy pidref fields that might or might not reference a process yet. */
277
278 assert(ret);
279
280 if (pidref) {
281 if (pidref_is_remote(pidref)) /* Propagate remote flag */
282 copy.fd = -EREMOTE;
283 else if (pidref->fd >= 0) {
284 copy.fd = fcntl(pidref->fd, F_DUPFD_CLOEXEC, 3);
285 if (copy.fd < 0) {
286 if (!ERRNO_IS_RESOURCE(errno))
287 return -errno;
288
289 copy.fd = -EBADF;
290 }
291 }
292
293 copy.pid = pidref->pid;
294 copy.fd_id = pidref->fd_id;
295 }
296
297 *ret = TAKE_PIDREF(copy);
298 return 0;
299 }
300
301 int pidref_dup(const PidRef *pidref, PidRef **ret) {
302 _cleanup_(pidref_freep) PidRef *dup_pidref = NULL;
303 int r;
304
305 /* Allocates a new PidRef on the heap, making it a copy of the specified pidref. This does not try to
306 * acquire a pidfd if we don't have one yet! */
307
308 assert(ret);
309
310 dup_pidref = newdup(PidRef, &PIDREF_NULL, 1);
311 if (!dup_pidref)
312 return -ENOMEM;
313
314 r = pidref_copy(pidref, dup_pidref);
315 if (r < 0)
316 return r;
317
318 *ret = TAKE_PTR(dup_pidref);
319 return 0;
320 }
321
322 int pidref_new_from_pid(pid_t pid, PidRef **ret) {
323 _cleanup_(pidref_freep) PidRef *n = NULL;
324 int r;
325
326 assert(ret);
327
328 if (pid < 0)
329 return -ESRCH;
330
331 n = new(PidRef, 1);
332 if (!n)
333 return -ENOMEM;
334
335 *n = PIDREF_NULL;
336
337 r = pidref_set_pid(n, pid);
338 if (r < 0)
339 return r;
340
341 *ret = TAKE_PTR(n);
342 return 0;
343 }
344
345 int pidref_kill(const PidRef *pidref, int sig) {
346
347 if (!pidref)
348 return -ESRCH;
349
350 if (pidref_is_remote(pidref))
351 return -EREMOTE;
352
353 if (pidref->fd >= 0)
354 return RET_NERRNO(pidfd_send_signal(pidref->fd, sig, NULL, 0));
355
356 if (pidref->pid > 0)
357 return RET_NERRNO(kill(pidref->pid, sig));
358
359 return -ESRCH;
360 }
361
362 int pidref_kill_and_sigcont(const PidRef *pidref, int sig) {
363 int r;
364
365 r = pidref_kill(pidref, sig);
366 if (r < 0)
367 return r;
368
369 if (!IN_SET(sig, SIGCONT, SIGKILL))
370 (void) pidref_kill(pidref, SIGCONT);
371
372 return 0;
373 }
374
375 int pidref_sigqueue(const PidRef *pidref, int sig, int value) {
376
377 if (!pidref)
378 return -ESRCH;
379
380 if (pidref_is_remote(pidref))
381 return -EREMOTE;
382
383 if (pidref->fd >= 0) {
384 siginfo_t si;
385
386 /* We can't use structured initialization here, since the structure contains various unions
387 * and these fields lie in overlapping (carefully aligned) unions that LLVM is allergic to
388 * allow assignments to */
389 zero(si);
390 si.si_signo = sig;
391 si.si_code = SI_QUEUE;
392 si.si_pid = getpid_cached();
393 si.si_uid = getuid();
394 si.si_value.sival_int = value;
395
396 return RET_NERRNO(pidfd_send_signal(pidref->fd, sig, &si, 0));
397 }
398
399 if (pidref->pid > 0)
400 return RET_NERRNO(sigqueue(pidref->pid, sig, (const union sigval) { .sival_int = value }));
401
402 return -ESRCH;
403 }
404
405 int pidref_verify(const PidRef *pidref) {
406 int r;
407
408 /* This is a helper that is supposed to be called after reading information from procfs via a
409 * PidRef. It ensures that the PID we track still matches the PIDFD we pin. If this value differs
410 * after a procfs read, we might have read the data from a recycled PID. */
411
412 if (!pidref_is_set(pidref))
413 return -ESRCH;
414
415 if (pidref_is_remote(pidref))
416 return -EREMOTE;
417
418 if (pidref->pid == 1)
419 return 1; /* PID 1 can never go away, hence never be recycled to a different process → return 1 */
420
421 if (pidref->fd < 0)
422 return 0; /* If we don't have a pidfd we cannot validate it, hence we assume it's all OK → return 0 */
423
424 r = pidfd_verify_pid(pidref->fd, pidref->pid);
425 if (r < 0)
426 return r;
427
428 return 1; /* We have a pidfd and it still points to the PID we have, hence all is *really* OK → return 1 */
429 }
430
431 bool pidref_is_self(PidRef *pidref) {
432 if (!pidref_is_set(pidref))
433 return false;
434
435 if (pidref_is_remote(pidref))
436 return false;
437
438 if (pidref->pid != getpid_cached())
439 return false;
440
441 /* PID1 cannot exit, hence no point in comparing pidfd IDs, they can never change */
442 if (pidref->pid == 1)
443 return true;
444
445 /* Also compare pidfd ID if we can get it */
446 if (pidref_acquire_pidfd_id(pidref) < 0)
447 return true;
448
449 uint64_t self_id;
450 if (pidfd_get_inode_id_self_cached(&self_id) < 0)
451 return true;
452
453 return pidref->fd_id == self_id;
454 }
455
456 int pidref_wait_for_terminate_full(PidRef *pidref, usec_t timeout, siginfo_t *ret_si) {
457 int r;
458
459 assert(timeout > 0);
460
461 if (!pidref_is_set(pidref))
462 return -ESRCH;
463
464 if (pidref_is_remote(pidref))
465 return -EREMOTE;
466
467 if (pidref->pid == 1 || pidref_is_self(pidref))
468 return -ECHILD;
469
470 if (pidref->fd < 0 && (timeout != USEC_INFINITY || fiber_ops_is_set()))
471 return -ENOMEDIUM;
472
473 usec_t ts = timeout == USEC_INFINITY ? USEC_INFINITY : usec_add(now(CLOCK_MONOTONIC), timeout);
474
475 /* Poll the pidfd before waitid() if either there's a finite timeout (so we can honor it) or
476 * we're on a fiber (so fd_wait_for_event() can suspend us instead of blocking the event loop
477 * inside waitid()). Otherwise let waitid() block directly. The precondition above guarantees
478 * pidref->fd >= 0 in both cases. */
479 bool poll_first = ts != USEC_INFINITY || fiber_ops_is_set();
480
481 for (;;) {
482 if (poll_first) {
483 usec_t left;
484
485 if (ts == USEC_INFINITY)
486 left = USEC_INFINITY;
487 else {
488 left = usec_sub_unsigned(ts, now(CLOCK_MONOTONIC));
489 if (left == 0)
490 return -ETIMEDOUT;
491 }
492
493 r = fd_wait_for_event(pidref->fd, POLLIN, left);
494 if (r == 0)
495 return -ETIMEDOUT;
496 if (r == -EINTR)
497 continue;
498 if (r < 0)
499 return r;
500 }
501
502 siginfo_t si = {};
503
504 if (pidref->fd >= 0)
505 r = RET_NERRNO(waitid(P_PIDFD, pidref->fd, &si, WEXITED));
506 else
507 r = RET_NERRNO(waitid(P_PID, pidref->pid, &si, WEXITED));
508 if (r >= 0) {
509 if (ret_si)
510 *ret_si = si;
511 return 0;
512 }
513 if (r != -EINTR)
514 return r;
515 }
516 }
517
518 bool pidref_is_automatic(const PidRef *pidref) {
519 return pidref && pid_is_automatic(pidref->pid);
520 }
521
522 void pidref_hash_func(const PidRef *pidref, struct siphash *state) {
523 siphash24_compress_typesafe(pidref->pid, state);
524 }
525
526 int pidref_compare_func(const PidRef *a, const PidRef *b) {
527 int r;
528
529 assert(a);
530 assert(b);
531
532 r = CMP(pidref_is_set(a), pidref_is_set(b));
533 if (r != 0)
534 return r;
535
536 r = CMP(pidref_is_automatic(a), pidref_is_automatic(b));
537 if (r != 0)
538 return r;
539
540 r = CMP(pidref_is_remote(a), pidref_is_remote(b));
541 if (r != 0)
542 return r;
543
544 r = CMP(a->pid, b->pid);
545 if (r != 0)
546 return r;
547
548 if (a->fd_id != 0 && b->fd_id != 0)
549 return CMP(a->fd_id, b->fd_id);
550
551 return 0;
552 }
553
554 DEFINE_HASH_OPS(pidref_hash_ops, PidRef, pidref_hash_func, pidref_compare_func);
555
556 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(pidref_hash_ops_free,
557 PidRef, pidref_hash_func, pidref_compare_func,
558 pidref_free);