chiark / gitweb /
util: implement parse_pid() function
authorLennart Poettering <lennart@poettering.net>
Thu, 17 Jun 2010 20:50:35 +0000 (22:50 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 17 Jun 2010 20:50:35 +0000 (22:50 +0200)
src/util.c
src/util.h

index ed0991a68b45d1a4b699f6de08ca54dcf81797e5..8f70c0f28a143de8611a020f704c3caf81023fd5 100644 (file)
@@ -241,6 +241,29 @@ int parse_boolean(const char *v) {
         return -EINVAL;
 }
 
+int parse_pid(const char *s, pid_t* ret_pid) {
+        unsigned long ul;
+        pid_t pid;
+        int r;
+
+        assert(s);
+        assert(ret_pid);
+
+        if ((r = safe_atolu(s, &ul)) < 0)
+                return r;
+
+        pid = (pid_t) ul;
+
+        if ((unsigned long) pid != ul)
+                return -ERANGE;
+
+        if (pid <= 0)
+                return -ERANGE;
+
+        *ret_pid = pid;
+        return 0;
+}
+
 int safe_atou(const char *s, unsigned *ret_u) {
         char *x = NULL;
         unsigned long l;
index 1e5ee28eba5677195942feaeb4c2b07d30f0e38d..1ca8f90cf1bea5793be46400dfb19aaa5fa41905 100644 (file)
@@ -108,6 +108,7 @@ void close_many(const int fds[], unsigned n_fd);
 
 int parse_boolean(const char *v);
 int parse_usec(const char *t, usec_t *usec);
+int parse_pid(const char *s, pid_t* ret_pid);
 
 int safe_atou(const char *s, unsigned *ret_u);
 int safe_atoi(const char *s, int *ret_i);