utils: add a function for checking if a process given by pid is still alive
authorFelix Fietkau <nbd@openwrt.org>
Mon, 21 Oct 2013 13:49:11 +0000 (15:49 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 22 Oct 2013 12:10:33 +0000 (14:10 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
handler.c
interface-event.c
interface-ip.c
iprule.c
main.c
proto-shell.c
system-linux.c
utils.c
utils.h

index 531b509..f4e27e1 100644 (file)
--- a/handler.c
+++ b/handler.c
@@ -16,7 +16,6 @@
 #include <glob.h>
 #include <fcntl.h>
 #include <stdio.h>
-#include <unistd.h>
 
 #include "netifd.h"
 #include "system.h"
index 707764a..3b0d1fa 100644 (file)
@@ -14,7 +14,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 
 #include <libubox/uloop.h>
 
index 2280266..084688c 100644 (file)
@@ -15,7 +15,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <unistd.h>
 
 #include <limits.h>
 #include <arpa/inet.h>
index a31db99..4e3dd15 100644 (file)
--- a/iprule.c
+++ b/iprule.c
@@ -15,7 +15,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <unistd.h>
 
 #include <arpa/inet.h>
 
diff --git a/main.c b/main.c
index c1f55e9..92c6f49 100644 (file)
--- a/main.c
+++ b/main.c
@@ -15,7 +15,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <getopt.h>
-#include <unistd.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <syslog.h>
index aa638ad..629f43b 100644 (file)
@@ -16,7 +16,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <unistd.h>
 #include <signal.h>
 
 #include <arpa/inet.h>
index e5364e0..d01d7e3 100644 (file)
@@ -41,7 +41,6 @@
 #define RTN_FAILED_POLICY 12
 #endif
 
-#include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
 #include <glob.h>
diff --git a/utils.c b/utils.c
index 6b53c22..d202505 100644 (file)
--- a/utils.c
+++ b/utils.c
 #include <arpa/inet.h>
 #include <netinet/in.h>
 
+#ifdef __APPLE__
+#include <libproc.h>
+#endif
+
 void
 __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
 {
@@ -168,3 +172,26 @@ crc32_file(FILE *fp)
 
        return c ^ 0xFFFFFFFF;
 }
+
+bool check_pid_path(int pid, const char *exe)
+{
+       int proc_exe_len;
+       int exe_len = strlen(exe);
+
+#ifdef __APPLE__
+       char proc_exe_buf[PROC_PIDPATHINFO_SIZE];
+
+       proc_exe_len = proc_pidpath(pid, proc_exe_buf, sizeof(proc_exe_buf));
+#else
+       char proc_exe[32];
+       char *proc_exe_buf = alloca(exe_len);
+
+       sprintf(proc_exe, "/proc/%d/exe", pid);
+       proc_exe_len = readlink(proc_exe, proc_exe_buf, exe_len);
+#endif
+
+       if (proc_exe_len != exe_len)
+               return false;
+
+       return !memcmp(exe, proc_exe_buf, exe_len);
+}
diff --git a/utils.h b/utils.h
index 23795e5..b0a7d02 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -14,6 +14,7 @@
 #ifndef __NETIFD_UTILS_H
 #define __NETIFD_UTILS_H
 
+#include <unistd.h>
 #include <stdio.h>
 #include <libubox/list.h>
 #include <libubox/avl.h>
@@ -107,6 +108,7 @@ static inline int fls(int x)
 unsigned int parse_netmask_string(const char *str, bool v6);
 bool split_netmask(char *str, unsigned int *netmask, bool v6);
 int parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask);
+bool check_pid_path(int pid, const char *exe);
 
 char * format_macaddr(uint8_t *mac);