]> git.itanic.dy.fi Git - scan-pagemap/blobdiff - pidlib.c
pidlib: Implement pidstr_is_ok()
[scan-pagemap] / pidlib.c
index da3603590cdac20558aa9754a05df15a64a5c201..d432e9dc907065dd1ecc73198112af2af83e08f7 100644 (file)
--- a/pidlib.c
+++ b/pidlib.c
@@ -1,3 +1,6 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <libgen.h>
@@ -133,3 +136,33 @@ int read_cmdline(int pid, int tid, char *cmdline, size_t len)
        return ret > 0 ? 0 : -1;
 }
 
+static int pid_is_ok(int pid)
+{
+       struct stat statt;
+       char path[64];
+
+       snprintf(path, sizeof(path), "/proc/%d/", pid);
+
+       if (stat(path, &statt))
+               return 0;
+
+       return pid;
+}
+
+/*
+ * Test whether a pid string number exists in the system as a pid
+ * number
+ */
+int pidstr_is_ok(const char *str)
+{
+       int i, pid;
+
+       /* Test that it is all digits */
+       for (i = 0; i < strlen(str); i++)
+               if (str[i] < '0' || str[i] > '9')
+                       return 0;
+
+       pid = atoi(str);
+
+       return pid_is_ok(pid);
+}