13 /* TODO: correctly check for *BSD */
14 #include <sys/param.h>
15 #include <sys/sysctl.h>
16 #include <sys/resource.h>
22 * Checks if the PID in path is still valid by checking:
23 * (Linux) if /proc/<pid> exists
24 * (NetBSD) if sysctl returns process infos for this pid
27 bool process_runs(const char *path) {
29 static glob_t globbuf;
31 memset(pidbuf, 0, sizeof(pidbuf));
33 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
34 die("glob() failed\n");
35 fd = open((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), O_RDONLY);
39 (void)read(fd, pidbuf, sizeof(pidbuf));
42 #if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
45 (void)snprintf(procbuf, sizeof(procbuf), "/proc/%ld", strtol(pidbuf, NULL, 10));
46 return (stat(procbuf, &statbuf) >= 0);
48 /* TODO: correctly check for NetBSD. Evaluate if this runs on OpenBSD/FreeBSD */
49 struct kinfo_proc info;
50 size_t length = sizeof(struct kinfo_proc);
51 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, strtol(pidbuf, NULL, 10) };
52 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)