pidfd-util: extract pidfd_get_inode_id_impl() and make it thread safe

Preparation for later commits.
This commit is contained in:
Mike Yuan
2025-06-01 08:57:49 +02:00
parent e09b7ced65
commit a842c26be3
2 changed files with 21 additions and 11 deletions

View File

@@ -17,9 +17,9 @@
#include "stdio-util.h"
#include "string-util.h"
static int have_pidfs = -1;
static thread_local int have_pidfs = -1;
static int pidfd_check_pidfs(int pid_fd) {
int pidfd_check_pidfs(int pid_fd) {
/* NB: the passed fd *must* be acquired via pidfd_open(), i.e. must be a true pidfd! */
@@ -229,18 +229,12 @@ int pidfd_get_cgroupid(int fd, uint64_t *ret) {
return 0;
}
int pidfd_get_inode_id(int fd, uint64_t *ret) {
static bool file_handle_supported = true;
int pidfd_get_inode_id_impl(int fd, uint64_t *ret) {
static thread_local bool file_handle_supported = true;
int r;
assert(fd >= 0);
r = pidfd_check_pidfs(fd);
if (r < 0)
return r;
if (r == 0)
return -EOPNOTSUPP;
if (file_handle_supported) {
union {
struct file_handle file_handle;
@@ -284,6 +278,20 @@ int pidfd_get_inode_id(int fd, uint64_t *ret) {
#endif
}
int pidfd_get_inode_id(int fd, uint64_t *ret) {
int r;
assert(fd >= 0);
r = pidfd_check_pidfs(fd);
if (r < 0)
return r;
if (r == 0)
return -EOPNOTSUPP;
return pidfd_get_inode_id_impl(fd, ret);
}
int pidfd_get_inode_id_self_cached(uint64_t *ret) {
static thread_local uint64_t cached = 0;
static thread_local pid_t initialized = 0; /* < 0: cached error; == 0: invalid; > 0: valid and pid that was current */

View File

@@ -14,6 +14,8 @@ int pidfd_get_ppid(int fd, pid_t *ret);
int pidfd_get_uid(int fd, uid_t *ret);
int pidfd_get_cgroupid(int fd, uint64_t *ret);
int pidfd_get_inode_id_impl(int fd, uint64_t *ret);
int pidfd_get_inode_id(int fd, uint64_t *ret);
int pidfd_get_inode_id_self_cached(uint64_t *ret);
int pidfd_check_pidfs(int pid_fd);