]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidref: add PIDREF_MAKE_FROM_PID()
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Sep 2023 14:05:42 +0000 (16:05 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 28 Sep 2023 21:22:58 +0000 (23:22 +0200)
This helper truns a pid_t into a PidRef. It's different from
pidref_set_pid() in being "passive", i.e. it does not attempt to acquire
a pidfd for the pid.

This is useful when using the PidRef as a lookup key that shall also
work after a process is already dead, and hence no conversion to a pidfd
is possible anymore.

src/basic/pidref.c
src/basic/pidref.h

index e1ad697af0cc8309b1e2cab82d5bb33e60728290..05f1b60e0ef77aef9f67595920ad7a68ae56f9a5 100644 (file)
@@ -68,11 +68,7 @@ int pidref_set_pidfd(PidRef *pidref, int fd) {
                 if (r < 0)
                         return r;
 
-                *pidref = (PidRef) {
-                        .fd = -EBADF,
-                        .pid = pid,
-                };
-
+                *pidref = PIDREF_MAKE_FROM_PID(pid);
                 return 0;
         }
 
index 84f3dee12980f4145c9c2e0ed80fe765d1afb22a..0942d974b9becf4bcdabea676971d82a160bbc91 100644 (file)
@@ -11,6 +11,10 @@ typedef struct PidRef {
 
 #define PIDREF_NULL (PidRef) { .fd = -EBADF }
 
+/* Turns a pid_t into a PidRef structure on-the-fly *without* acquiring a pidfd for it. (As opposed to
+ * pidref_set_pid() which does so *with* acquiring one, see below) */
+#define PIDREF_MAKE_FROM_PID(x) (PidRef) { .pid = (x), .fd = -EBADF }
+
 static inline bool pidref_is_set(const PidRef *pidref) {
         return pidref && pidref->pid > 0;
 }
@@ -27,6 +31,8 @@ static inline bool pidref_equal(const PidRef *a, const PidRef *b) {
         return !pidref_is_set(b);
 }
 
+/* This turns a pid_t into a PidRef structure, and acquires a pidfd for it, if possible. (As opposed to
+ * PIDREF_MAKE_FROM_PID() above, which does not acquire a pidfd.) */
 int pidref_set_pid(PidRef *pidref, pid_t pid);
 int pidref_set_pidstr(PidRef *pidref, const char *pid);
 int pidref_set_pidfd(PidRef *pidref, int fd);