From: Michael Stapelberg Date: Sun, 28 Nov 2010 15:45:34 +0000 (+0100) Subject: Use kill(pid, 0) to check if the process is alive X-Git-Tag: 2.3~26 X-Git-Url: https://git.sur5r.net/?p=i3%2Fi3status;a=commitdiff_plain;h=cf091024335052e4774d44179e80e1948da8610f;ds=sidebyside Use kill(pid, 0) to check if the process is alive --- diff --git a/include/i3status.h b/include/i3status.h index a79be18..0ed0d1f 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -51,7 +51,7 @@ typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t; /* src/general.c */ char *skip_character(char *input, char character, int amount); void die(const char *fmt, ...); -bool slurp(char *filename, char *destination, int size); +bool slurp(const char *filename, char *destination, int size); /* src/output.c */ void print_seperator(); diff --git a/src/general.c b/src/general.c index a093c16..2ae90f1 100644 --- a/src/general.c +++ b/src/general.c @@ -13,7 +13,7 @@ * Reads size bytes into the destination buffer from filename. * */ -bool slurp(char *filename, char *destination, int size) { +bool slurp(const char *filename, char *destination, int size) { int fd; if ((fd = open(filename, O_RDONLY)) == -1) diff --git a/src/process_runs.c b/src/process_runs.c index d8513a5..88979f0 100644 --- a/src/process_runs.c +++ b/src/process_runs.c @@ -3,54 +3,31 @@ #include #include #include -#include -#include -#include #include #include - -#ifndef LINUX -/* TODO: correctly check for *BSD */ -#include -#include -#include -#endif +#include +#include #include "i3status.h" /* - * Checks if the PID in path is still valid by checking: - * (Linux) if /proc/ exists - * (NetBSD) if sysctl returns process infos for this pid + * Checks if the PID in path is still valid by sending signal 0 (does not do + * anything). kill() will return ESRCH if the process does not exist and 0 or + * EPERM (depending on the uid) if it exists. * */ bool process_runs(const char *path) { - char pidbuf[16]; + static char pidbuf[16]; static glob_t globbuf; - int fd; memset(pidbuf, 0, sizeof(pidbuf)); if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) die("glob() failed\n"); - fd = open((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), O_RDONLY); - globfree(&globbuf); - if (fd < 0) + if (!slurp((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), pidbuf, sizeof(pidbuf))) { + globfree(&globbuf); return false; - (void)read(fd, pidbuf, sizeof(pidbuf)); - (void)close(fd); + } + globfree(&globbuf); -#if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) - struct stat statbuf; - char procbuf[512]; - (void)snprintf(procbuf, sizeof(procbuf), "/proc/%ld", strtol(pidbuf, NULL, 10)); - return (stat(procbuf, &statbuf) >= 0); -#else - /* TODO: correctly check for NetBSD. Evaluate if this runs on OpenBSD/FreeBSD */ - struct kinfo_proc info; - size_t length = sizeof(struct kinfo_proc); - int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, strtol(pidbuf, NULL, 10) }; - if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) - return false; - return (length != 0); -#endif + return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM); }