X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprocess_runs.c;h=b5e8f110c5ac2cd77e9be07ceea7ce2ecbe39c42;hb=d74e904bf4bdd7f1d767a8db09987b90de809579;hp=5e13de7f62f686ac9923db7818346a3e414dde00;hpb=6fda988f360b3145d5772b6964f336dd652357ea;p=i3%2Fi3status diff --git a/src/process_runs.c b/src/process_runs.c index 5e13de7..b5e8f11 100644 --- a/src/process_runs.c +++ b/src/process_runs.c @@ -1,55 +1,49 @@ +// vim:ts=4:sw=4:expandtab #include #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. + * + * If multiple files match the glob pattern, all of them will be checked until + * the first running process is found. * */ bool process_runs(const char *path) { - char pidbuf[16]; - static glob_t globbuf; - int fd; - memset(pidbuf, 0, sizeof(pidbuf)); + static char pidbuf[16]; + static glob_t globbuf; + 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); + if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) + die("glob() failed\n"); + if (globbuf.gl_pathc == 0) { + /* No glob matches, the specified path does not contain a wildcard. */ globfree(&globbuf); - if (fd < 0) - return false; - (void)read(fd, pidbuf, sizeof(pidbuf)); - (void)close(fd); + if (!slurp(path, pidbuf, sizeof(pidbuf))) + return false; + return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM); + } + for (size_t i = 0; i < globbuf.gl_pathc; i++) { + if (!slurp(globbuf.gl_pathv[i], pidbuf, sizeof(pidbuf))) { + globfree(&globbuf); + return false; + } + if (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM) { + globfree(&globbuf); + return true; + } + } + globfree(&globbuf); -#ifdef LINUX - 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 false; }