]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/procutils: add proc_is_procfs helper.
authorÉrico Rolim <erico.erc@gmail.com>
Wed, 18 Nov 2020 17:07:10 +0000 (14:07 -0300)
committerEmil Velikov <emil.l.velikov@gmail.com>
Tue, 9 Feb 2021 23:40:13 +0000 (23:40 +0000)
Also add missing include in procutils.h for the definition of pid_t.

(cherry picked from commit e411b8a63d4a44ac191b5ca032c4639b12d4b838)

include/procutils.h
lib/procutils.c

index 9f8dd76ec1d05bba403e2bbef7020dffe5afeeb3..c9f5bb5528c31c7afd4716f3a299a5d1a83aebc4 100644 (file)
@@ -2,6 +2,7 @@
 #define UTIL_LINUX_PROCUTILS
 
 #include <dirent.h>
+#include <sys/types.h>
 
 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 */
index cf660ea5dea636a0dc94eb603991eebb1a9e445b..4436f38fa402dc87abd0d1a1e8889d7a9f586642 100644 (file)
 #include <string.h>
 #include <errno.h>
 #include <sys/stat.h>
+#include <sys/vfs.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <ctype.h>
 
 #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[])