-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessTree.hxx
63 lines (45 loc) · 1.11 KB
/
ProcessTree.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef __ProcessTree_hxx__
#define __ProcessTree_hxx__
#include <unordered_map>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
static pid_t _ppid_from_proc_stat(const pid_t pid)
{
char buf[256] = {};
sprintf(buf, "/proc/%d/stat", pid);
FILE* fp = fopen(buf, "r");
if (fp == NULL)
return -errno;
// the stat pseudofile under /proc/#procid/ contains
// pid{%d} command{%s} state{%c} ppid{%d} ...
char* _ = buf;
pid_t ppid = -1;
fscanf(fp, "%s %s %s %d", _, _, _, &ppid);
fclose(fp);
return ppid;
}
class ProcessTree
{
public:
const pid_t ppidof(const pid_t &pid) const noexcept
{
try {
if (cache_.count(pid) == 0)
cache_[pid] = _ppid_from_proc_stat(pid);
return cache_[pid];
}
catch (...) {
return -1;
}
}
bool is_ancestor(const pid_t &aid, pid_t pid) const
{
while (pid != 0 && pid != aid)
pid = ppidof(pid);
return pid == aid;
}
private:
mutable std::unordered_map<pid_t, pid_t> cache_;
};
#endif // include guard