diff options
author | Felix Fietkau <nbd@openwrt.org> | 2013-10-21 15:49:11 +0200 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2013-10-22 14:10:33 +0200 |
commit | f80b9e4acc47cf454768b44142495fdb8a68a39f (patch) | |
tree | 799f4f70ae544067ba2135746a3265a907613ef4 | |
parent | 381f47c5af62cec173528f5f539558d7a18b6d30 (diff) |
utils: add a function for checking if a process given by pid is still alive
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
-rw-r--r-- | handler.c | 1 | ||||
-rw-r--r-- | interface-event.c | 1 | ||||
-rw-r--r-- | interface-ip.c | 1 | ||||
-rw-r--r-- | iprule.c | 1 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | proto-shell.c | 1 | ||||
-rw-r--r-- | system-linux.c | 1 | ||||
-rw-r--r-- | utils.c | 27 | ||||
-rw-r--r-- | utils.h | 2 |
9 files changed, 29 insertions, 7 deletions
@@ -16,7 +16,6 @@ #include <glob.h> #include <fcntl.h> #include <stdio.h> -#include <unistd.h> #include "netifd.h" #include "system.h" diff --git a/interface-event.c b/interface-event.c index 707764a..3b0d1fa 100644 --- a/interface-event.c +++ b/interface-event.c @@ -14,7 +14,6 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <libubox/uloop.h> diff --git a/interface-ip.c b/interface-ip.c index 2280266..084688c 100644 --- a/interface-ip.c +++ b/interface-ip.c @@ -15,7 +15,6 @@ #include <string.h> #include <stdlib.h> #include <stdio.h> -#include <unistd.h> #include <limits.h> #include <arpa/inet.h> @@ -15,7 +15,6 @@ #include <string.h> #include <stdlib.h> #include <stdio.h> -#include <unistd.h> #include <arpa/inet.h> @@ -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> diff --git a/proto-shell.c b/proto-shell.c index aa638ad..629f43b 100644 --- a/proto-shell.c +++ b/proto-shell.c @@ -16,7 +16,6 @@ #include <string.h> #include <stdlib.h> #include <stdio.h> -#include <unistd.h> #include <signal.h> #include <arpa/inet.h> diff --git a/system-linux.c b/system-linux.c index e5364e0..d01d7e3 100644 --- a/system-linux.c +++ b/system-linux.c @@ -41,7 +41,6 @@ #define RTN_FAILED_POLICY 12 #endif -#include <unistd.h> #include <string.h> #include <fcntl.h> #include <glob.h> @@ -18,6 +18,10 @@ #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); +} @@ -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); |