From e411b8a63d4a44ac191b5ca032c4639b12d4b838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Wed, 18 Nov 2020 14:07:10 -0300 Subject: [PATCH] lib/procutils: add proc_is_procfs helper. Also add missing include in procutils.h for the definition of pid_t. --- include/procutils.h | 3 +++ lib/procutils.c | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/procutils.h b/include/procutils.h index 9f8dd76ec..c9f5bb552 100644 --- a/include/procutils.h +++ b/include/procutils.h @@ -2,6 +2,7 @@ #define UTIL_LINUX_PROCUTILS #include +#include struct proc_tasks { DIR *dir; @@ -31,4 +32,6 @@ extern int proc_next_pid(struct proc_processes *ps, pid_t *pid); extern char *proc_get_command(pid_t pid); extern char *proc_get_command_name(pid_t pid); +extern int proc_is_procfs(int fd); + #endif /* UTIL_LINUX_PROCUTILS */ diff --git a/lib/procutils.c b/lib/procutils.c index cf660ea5d..4436f38fa 100644 --- a/lib/procutils.c +++ b/lib/procutils.c @@ -13,11 +13,13 @@ #include #include #include +#include #include #include #include #include "procutils.h" +#include "statfs_magic.h" #include "fileutils.h" #include "all-io.h" #include "c.h" @@ -235,6 +237,23 @@ int proc_next_pid(struct proc_processes *ps, pid_t *pid) return 0; } +/* checks if fd is file in a procfs; + * returns 1 if true, 0 if false or couldn't determine */ +int proc_is_procfs(int fd) +{ + struct statfs st; + int ret; + + do { + ret = fstatfs(fd, &st); + } while (ret == -1 && errno == EINTR); + + if (ret == 0) + return st.f_type == STATFS_PROC_MAGIC; + else + return 0; +} + #ifdef TEST_PROGRAM_PROCUTILS static int test_tasks(int argc, char *argv[])