]> git.sur5r.net Git - i3/i3status/blob - src/process_runs.c
Use own files for each function, add get_ipv6_addr.c
[i3/i3status] / src / process_runs.c
1 #include <stdbool.h>
2 #include <glob.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #ifndef LINUX
12 /* TODO: correctly check for *BSD */
13 #include <sys/param.h>
14 #include <sys/sysctl.h>
15 #include <sys/resource.h>
16 #endif
17
18 #include "i3status.h"
19
20 /*
21  * Checks if the PID in path is still valid by checking:
22  *  (Linux) if /proc/<pid> exists
23  *  (NetBSD) if sysctl returns process infos for this pid
24  *
25  */
26 bool process_runs(const char *path) {
27         char pidbuf[16];
28         static glob_t globbuf;
29         int fd;
30         memset(pidbuf, 0, sizeof(pidbuf));
31
32         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
33                 die("glob() failed\n");
34         fd = open((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), O_RDONLY);
35         globfree(&globbuf);
36         if (fd < 0)
37                 return false;
38         (void)read(fd, pidbuf, sizeof(pidbuf));
39         (void)close(fd);
40
41 #ifdef LINUX
42         struct stat statbuf;
43         char procbuf[512];
44         (void)snprintf(procbuf, sizeof(procbuf), "/proc/%ld", strtol(pidbuf, NULL, 10));
45         return (stat(procbuf, &statbuf) >= 0);
46 #else
47         /* TODO: correctly check for NetBSD. Evaluate if this runs on OpenBSD/FreeBSD */
48         struct kinfo_proc info;
49         size_t length = sizeof(struct kinfo_proc);
50         int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, strtol(pidbuf, NULL, 10) };
51         if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
52                 return false;
53         return (length != 0);
54 #endif
55 }