]> git.sur5r.net Git - i3/i3status/blob - src/process_runs.c
fix: use SYSCONFDIR in error message
[i3/i3status] / src / process_runs.c
1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include <stdbool.h>
4 #include <glob.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <errno.h>
10 #include <signal.h>
11
12 #include "i3status.h"
13
14 /*
15  * Checks if the PID in path is still valid by sending signal 0 (does not do
16  * anything). kill() will return ESRCH if the process does not exist and 0 or
17  * EPERM (depending on the uid) if it exists.
18  *
19  * If multiple files match the glob pattern, all of them will be checked until
20  * the first running process is found.
21  *
22  */
23 bool process_runs(const char *path) {
24     static char pidbuf[16];
25     static glob_t globbuf;
26     memset(pidbuf, 0, sizeof(pidbuf));
27
28     if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) != 0)
29         die("glob() failed\n");
30     if (globbuf.gl_pathc == 0) {
31         /* No glob matches, the specified path does not contain a wildcard. */
32         globfree(&globbuf);
33         if (!slurp(path, pidbuf, sizeof(pidbuf)))
34             return false;
35         return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM);
36     }
37     for (size_t i = 0; i < globbuf.gl_pathc; i++) {
38         if (!slurp(globbuf.gl_pathv[i], pidbuf, sizeof(pidbuf))) {
39             globfree(&globbuf);
40             return false;
41         }
42         if (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM) {
43             globfree(&globbuf);
44             return true;
45         }
46     }
47     globfree(&globbuf);
48
49     return false;
50 }