Skip to content

Commit

Permalink
pidref: use fd_inode_same to compare pidfds
Browse files Browse the repository at this point in the history
  • Loading branch information
YHNdnzj committed Mar 11, 2024
1 parent 95668ce commit f83833f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
22 changes: 22 additions & 0 deletions src/basic/pidref.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
#include "pidref.h"
#include "process-util.h"
#include "signal-util.h"
#include "stat-util.h"

bool pidref_equal(const PidRef *a, const PidRef *b) {

if (pidref_is_set(a)) {
if (!pidref_is_set(b))
return false;

if (a->pid != b->pid)
return false;

/* pidfds live in their own pidfs and each process comes with a unique inode number since
* kernel 6.8. We can safely do this on older kernels too though, as previously anonymous
* inode was used and inode number was the same for all pidfds. */
if (a->fd >= 0 && b->fd >= 0 && fd_inode_same(a->fd, b->fd) <= 0)
return false;

return true;
}

return !pidref_is_set(b);
}

int pidref_set_pid(PidRef *pidref, pid_t pid) {
int fd;
Expand Down
12 changes: 1 addition & 11 deletions src/basic/pidref.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,7 @@ static inline bool pidref_is_set(const PidRef *pidref) {
return pidref && pidref->pid > 0;
}

static inline bool pidref_equal(const PidRef *a, const PidRef *b) {

if (pidref_is_set(a)) {
if (!pidref_is_set(b))
return false;

return a->pid == b->pid;
}

return !pidref_is_set(b);
}
bool pidref_equal(const PidRef *a, const PidRef *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.) */
Expand Down

0 comments on commit f83833f

Please sign in to comment.