]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/pidref.h
Merge pull request #28919 from fbuihuu/custom-config-file-install-path
[thirdparty/systemd.git] / src / basic / pidref.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "macro.h"
5
6 /* An embeddable structure carrying a reference to a process. Supposed to be used when tracking processes continuously. */
7 typedef struct PidRef {
8 pid_t pid; /* always valid */
9 int fd; /* only valid if pidfd are available in the kernel, and we manage to get an fd */
10 } PidRef;
11
12 #define PIDREF_NULL (PidRef) { .fd = -EBADF }
13
14 static inline bool pidref_is_set(const PidRef *pidref) {
15 return pidref && pidref->pid > 0;
16 }
17
18 static inline bool pidref_equal(const PidRef *a, const PidRef *b) {
19
20 if (pidref_is_set(a)) {
21 if (!pidref_is_set(b))
22 return false;
23
24 return a->pid == b->pid;
25 }
26
27 return !pidref_is_set(b);
28 }
29
30 int pidref_set_pid(PidRef *pidref, pid_t pid);
31 int pidref_set_pidstr(PidRef *pidref, const char *pid);
32 int pidref_set_pidfd(PidRef *pidref, int fd);
33 int pidref_set_pidfd_take(PidRef *pidref, int fd); /* takes ownership of the passed pidfd on success*/
34 int pidref_set_pidfd_consume(PidRef *pidref, int fd); /* takes ownership of the passed pidfd in both success and failure */
35
36 void pidref_done(PidRef *pidref);
37
38 int pidref_kill(PidRef *pidref, int sig);
39 int pidref_kill_and_sigcont(PidRef *pidref, int sig);
40 int pidref_sigqueue(PidRef *pidfref, int sig, int value);
41
42 #define TAKE_PIDREF(p) TAKE_GENERIC((p), PidRef, PIDREF_NULL)