Use kill(pid, 0) to check if the process is alive
This commit is contained in:
parent
75670ba64a
commit
cf09102433
@ -51,7 +51,7 @@ typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
|
|||||||
/* src/general.c */
|
/* src/general.c */
|
||||||
char *skip_character(char *input, char character, int amount);
|
char *skip_character(char *input, char character, int amount);
|
||||||
void die(const char *fmt, ...);
|
void die(const char *fmt, ...);
|
||||||
bool slurp(char *filename, char *destination, int size);
|
bool slurp(const char *filename, char *destination, int size);
|
||||||
|
|
||||||
/* src/output.c */
|
/* src/output.c */
|
||||||
void print_seperator();
|
void print_seperator();
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
* Reads size bytes into the destination buffer from filename.
|
* Reads size bytes into the destination buffer from filename.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
bool slurp(char *filename, char *destination, int size) {
|
bool slurp(const char *filename, char *destination, int size) {
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if ((fd = open(filename, O_RDONLY)) == -1)
|
if ((fd = open(filename, O_RDONLY)) == -1)
|
||||||
|
@ -3,54 +3,31 @@
|
|||||||
#include <glob.h>
|
#include <glob.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
#ifndef LINUX
|
#include <signal.h>
|
||||||
/* TODO: correctly check for *BSD */
|
|
||||||
#include <sys/param.h>
|
|
||||||
#include <sys/sysctl.h>
|
|
||||||
#include <sys/resource.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "i3status.h"
|
#include "i3status.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Checks if the PID in path is still valid by checking:
|
* Checks if the PID in path is still valid by sending signal 0 (does not do
|
||||||
* (Linux) if /proc/<pid> exists
|
* anything). kill() will return ESRCH if the process does not exist and 0 or
|
||||||
* (NetBSD) if sysctl returns process infos for this pid
|
* EPERM (depending on the uid) if it exists.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
bool process_runs(const char *path) {
|
bool process_runs(const char *path) {
|
||||||
char pidbuf[16];
|
static char pidbuf[16];
|
||||||
static glob_t globbuf;
|
static glob_t globbuf;
|
||||||
int fd;
|
|
||||||
memset(pidbuf, 0, sizeof(pidbuf));
|
memset(pidbuf, 0, sizeof(pidbuf));
|
||||||
|
|
||||||
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
|
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
|
||||||
die("glob() failed\n");
|
die("glob() failed\n");
|
||||||
fd = open((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), O_RDONLY);
|
if (!slurp((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), pidbuf, sizeof(pidbuf))) {
|
||||||
globfree(&globbuf);
|
globfree(&globbuf);
|
||||||
if (fd < 0)
|
|
||||||
return false;
|
return false;
|
||||||
(void)read(fd, pidbuf, sizeof(pidbuf));
|
}
|
||||||
(void)close(fd);
|
globfree(&globbuf);
|
||||||
|
|
||||||
#if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
|
return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM);
|
||||||
struct stat statbuf;
|
|
||||||
char procbuf[512];
|
|
||||||
(void)snprintf(procbuf, sizeof(procbuf), "/proc/%ld", strtol(pidbuf, NULL, 10));
|
|
||||||
return (stat(procbuf, &statbuf) >= 0);
|
|
||||||
#else
|
|
||||||
/* TODO: correctly check for NetBSD. Evaluate if this runs on OpenBSD/FreeBSD */
|
|
||||||
struct kinfo_proc info;
|
|
||||||
size_t length = sizeof(struct kinfo_proc);
|
|
||||||
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, strtol(pidbuf, NULL, 10) };
|
|
||||||
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
|
|
||||||
return false;
|
|
||||||
return (length != 0);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user