From: Timo Kokkonen Date: Thu, 2 Sep 2010 15:28:41 +0000 (+0300) Subject: pidlib: Implement pidstr_is_ok() X-Git-Url: http://git.itanic.dy.fi/?p=scan-pagemap;a=commitdiff_plain;h=1e0e4e7d301c414549252f44eec2cb6544d78103 pidlib: Implement pidstr_is_ok() This can be used to check whether a numerical pid string represents an actual pid numer on the system. Signed-off-by: Timo Kokkonen --- diff --git a/pidlib.c b/pidlib.c index da36035..d432e9d 100644 --- a/pidlib.c +++ b/pidlib.c @@ -1,3 +1,6 @@ +#include +#include +#include #include #include #include @@ -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); +} diff --git a/pidlib.h b/pidlib.h index e095ecb..f1704f2 100644 --- 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