]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
pidlib: Implement pidstr_is_ok()
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 2 Sep 2010 15:28:41 +0000 (18:28 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 2 Sep 2010 15:32:56 +0000 (18:32 +0300)
This can be used to check whether a numerical pid string represents an
actual pid numer on the system.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
pidlib.c
pidlib.h

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);
+}
index e095ecbf3777bf3189e907ab42465542b21ffcf0..f1704f25f3f9ca1b900350aa9e8d526eafca4766 100644 (file)
--- a/pidlib.h
+++ b/pidlib.h
@@ -9,5 +9,6 @@ int get_next_tid(int pid, DIR **dir);
 int get_next_pid(DIR **dir);
 int get_next_pid_by_name(DIR **dir, char *name);
 int read_cmdline(int pid, int tid, char *cmdline, size_t len);
+int pidstr_is_ok(const char *str);
 
 #endif