diff options
author | Lennart Poettering <lennart@poettering.net> | 2025-01-13 11:06:27 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2025-01-16 11:55:21 +0100 |
commit | 8110b34b64bfdda0a82b04544a7be07b7f0a5c7a (patch) | |
tree | e2e6763bd6c954b44bab29a286918d580cb192ed | |
parent | tree-wide: drop support for kernels without pidfd_open() and pidfd_send_signa... (diff) | |
download | systemd-8110b34b64bfdda0a82b04544a7be07b7f0a5c7a.tar.xz systemd-8110b34b64bfdda0a82b04544a7be07b7f0a5c7a.zip |
pidref: various shortcuts to pidref_equal()
This adds some shortcuts to pidref_equal(), so that we don't have to
query the pidfs id if there's no need.
-rw-r--r-- | src/basic/pidref.c | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/basic/pidref.c b/src/basic/pidref.c index a275f77b56..ccfa2903b6 100644 --- a/src/basic/pidref.c +++ b/src/basic/pidref.c @@ -40,6 +40,10 @@ int pidref_acquire_pidfd_id(PidRef *pidref) { bool pidref_equal(PidRef *a, PidRef *b) { + /* If this is the very same structure, it definitely refers to the same process */ + if (a == b) + return true; + if (!pidref_is_set(a)) return !pidref_is_set(b); @@ -58,9 +62,15 @@ bool pidref_equal(PidRef *a, PidRef *b) { if (a->fd_id == 0 || b->fd_id == 0) return true; } else { + /* If the other side is remote, then this is not the same */ if (pidref_is_remote(b)) return false; + /* PID1 cannot exit, hence it cannot change pidfs ids, hence no point in comparing them, we + * can shortcut things */ + if (a->pid == 1) + return true; + /* Try to compare pidfds using their inode numbers. This way we can ensure that we * don't spuriously consider two PidRefs equal if the pid has been reused once. Note * that we ignore all errors here, not only EOPNOTSUPP, as fstat() might fail due to |