diff options
author | Michael Stapelberg <michael@stapelberg.de> | 2014-05-15 21:51:01 +0200 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2014-05-15 21:51:01 +0200 |
commit | 653505a3bbaad141d9fb94458e2ffffe312a8e58 (patch) | |
tree | 4ac7194ffa58e1f7438f8808b3c8fe01a5ab9d9f | |
parent | b26b118d76ab32cbff7ca6e5def69ca7bfbf6aa0 (diff) |
run_watch: check all matching pid files (until first valid one) (Thanks brotbart)
fixes #1245
-rw-r--r-- | src/process_runs.c | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/process_runs.c b/src/process_runs.c index 88979f0..1836507 100644 --- a/src/process_runs.c +++ b/src/process_runs.c @@ -1,4 +1,4 @@ -// vim:ts=8:expandtab +// vim:ts=8:sw=8:expandtab #include <stdbool.h> #include <glob.h> #include <string.h> @@ -15,6 +15,9 @@ * 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) { static char pidbuf[16]; @@ -23,11 +26,24 @@ bool process_runs(const char *path) { if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) die("glob() failed\n"); - if (!slurp((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), pidbuf, sizeof(pidbuf))) { + if (globbuf.gl_pathc == 0) { + /* No glob matches, the specified path does not contain a wildcard. */ globfree(&globbuf); - return false; + 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); - return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM); + return false; } |