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>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h>
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);
+}
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