]> git.sur5r.net Git - i3/i3status/blob - src/process_runs.c
Teen ordinal numbers always use a 'th' suffix.
[i3/i3status] / src / process_runs.c
1 // vim:ts=8:expandtab
2 #include <stdbool.h>
3 #include <glob.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <signal.h>
10
11 #include "i3status.h"
12
13 /*
14  * Checks if the PID in path is still valid by sending signal 0 (does not do
15  * anything). kill() will return ESRCH if the process does not exist and 0 or
16  * EPERM (depending on the uid) if it exists.
17  *
18  */
19 bool process_runs(const char *path) {
20         static char pidbuf[16];
21         static glob_t globbuf;
22         memset(pidbuf, 0, sizeof(pidbuf));
23
24         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
25                 die("glob() failed\n");
26         if (!slurp((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), pidbuf, sizeof(pidbuf))) {
27                 globfree(&globbuf);
28                 return false;
29         }
30         globfree(&globbuf);
31
32         return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM);
33 }