From: Felix Fietkau Date: Mon, 21 Oct 2013 13:49:11 +0000 (+0200) Subject: utils: add a function for checking if a process given by pid is still alive X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=f80b9e4acc47cf454768b44142495fdb8a68a39f;p=oweals%2Fnetifd.git utils: add a function for checking if a process given by pid is still alive Signed-off-by: Felix Fietkau --- diff --git a/handler.c b/handler.c index 531b509..f4e27e1 100644 --- a/handler.c +++ b/handler.c @@ -16,7 +16,6 @@ #include #include #include -#include #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 #include #include -#include #include 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 #include #include -#include #include #include diff --git a/iprule.c b/iprule.c index a31db99..4e3dd15 100644 --- a/iprule.c +++ b/iprule.c @@ -15,7 +15,6 @@ #include #include #include -#include #include diff --git a/main.c b/main.c index c1f55e9..92c6f49 100644 --- a/main.c +++ b/main.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include 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 #include #include -#include #include #include 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 #include #include #include diff --git a/utils.c b/utils.c index 6b53c22..d202505 100644 --- a/utils.c +++ b/utils.c @@ -18,6 +18,10 @@ #include #include +#ifdef __APPLE__ +#include +#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 --- a/utils.h +++ b/utils.h @@ -14,6 +14,7 @@ #ifndef __NETIFD_UTILS_H #define __NETIFD_UTILS_H +#include #include #include #include @@ -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);