diff options
author | Dmitry V. Levin <ldv@strace.io> | 2023-04-29 08:00:00 +0000 |
---|---|---|
committer | Dmitry V. Levin <ldv@strace.io> | 2023-04-29 08:00:00 +0000 |
commit | 551870dc5cc2421bd960bb42fb25d5027af67efc (patch) | |
tree | 23bb8f08a828c0d0cb5e887fe0d951533fbf3b1f | |
parent | c7ea7bec297591c1d586620ef1cc86e4599b8ab1 (diff) | |
download | strace-551870dc5cc2421bd960bb42fb25d5027af67efc.tar.gz |
util: introduce scan_fdinfo() function
* src/util.c (scan_fdinfo_fn): New type.
(scan_fdinfo, parse_fdinfo_pid): New functions.
(pidfd_get_pid): Use them.
Suggested-by: Masatake YAMATO <yamato@redhat.com>
-rw-r--r-- | src/util.c | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/util.c b/src/util.c index 4fe45bf13..e43e4b725 100644 --- a/src/util.c +++ b/src/util.c @@ -709,37 +709,59 @@ printdev(struct tcb *tcp, int fd, const char *path, const struct finfo *finfo) return false; } -pid_t -pidfd_get_pid(pid_t pid_of_fd, int fd) +typedef bool (*scan_fdinfo_fn)(const char *value, void *data); + +static bool +scan_fdinfo(pid_t pid_of_fd, int fd, const char *search_pfx, + size_t search_pfx_len, scan_fdinfo_fn fn, void *data) { int proc_pid = 0; translate_pid(NULL, pid_of_fd, PT_TID, &proc_pid); if (!proc_pid) - return -1; + return false; char fdi_path[sizeof("/proc/%u/fdinfo/%u") + 2 * sizeof(int) * 3]; xsprintf(fdi_path, "/proc/%u/fdinfo/%u", proc_pid, fd); FILE *f = fopen_stream(fdi_path, "r"); if (!f) - return -1; + return false; - static const char pid_pfx[] = "Pid:\t"; char *line = NULL; size_t sz = 0; - pid_t pid = -1; - while (getline(&line, &sz, f) > 0) { - const char *pos = STR_STRIP_PREFIX(line, pid_pfx); - if (pos == line) - continue; + bool result = false; - pid = string_to_uint_ex(pos, NULL, INT_MAX, "\n"); - break; + while (getline(&line, &sz, f) > 0) { + const char *value = + str_strip_prefix_len(line, search_pfx, search_pfx_len); + if (value != line && fn(value, data)) { + result = true; + break; + } } free(line); fclose(f); + return result; +} + +static bool +parse_fdinfo_pid(const char *value, void *data) +{ + pid_t *pid = data; + *pid = string_to_uint_ex(value, NULL, INT_MAX, "\n"); + return true; +} + +pid_t +pidfd_get_pid(pid_t pid_of_fd, int fd) +{ + static const char pid_pfx[] = "Pid:\t"; + pid_t pid = -1; + + scan_fdinfo(pid_of_fd, fd, pid_pfx, sizeof(pid_pfx) - 1, + parse_fdinfo_pid, &pid); return pid; } |