tagfs/src/proc.h

50 lines
721 B
C

#ifndef H_PROC
#define H_PROC
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <alloca.h>
#include <limits.h>
static inline int
is_pid_alive(unsigned pid)
{
char path[PATH_MAX];
snprintf(path, sizeof path, "/proc/%u", pid);
return access(path, F_OK) == 0;
}
static inline int
proc_get_ppid(unsigned pid)
{
char path[PATH_MAX];
snprintf(path, sizeof path, "/proc/%u/stat", pid);
FILE* fp;
if ((fp = fopen(path, "r")) == NULL)
goto err;
char stat[4096];
if (fread(stat, sizeof(char), sizeof stat, fp) == 0)
goto err;
fclose(fp);
char unused;
int ppid;
sscanf(strrchr(stat, ')') + 1, " %c %d", &unused, &ppid);
return ppid;
err:
if (fp) fclose(fp);
return -1;
}
#endif