Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
mxq
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
20
Pull requests
3
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
b636630
helper
manpages
mysql
web
.gitignore
.vimrc
Doxyfile
LICENSE
Makefile
README.md
keywordset.c
keywordset.h
mx_flock.c
mx_flock.h
mx_getopt.c
mx_getopt.h
mx_log.c
mx_log.h
mx_mysql.c
mx_mysql.h
mx_proc.c
mx_proc.h
mx_util.c
mx_util.h
mxq.h
mxq_daemon.c
mxq_daemon.h
mxq_group.c
mxq_group.h
mxq_job.c
mxq_job.h
mxq_log.c
mxq_reaper.c
mxqadmin.c
mxqd.c
mxqd.h
mxqd_control.c
mxqd_control.h
mxqdctl-hostconfig.sh
mxqdump.c
mxqkill.c
mxqps.c
mxqset.c
mxqsub.c
os-release
parser.y
ppidcache.c
ppidcache.h
test_keywordset.c
test_mx_log.c
test_mx_mysql.c
test_mx_util.c
test_mxqd_control.c
test_parser.c
xmalloc.h
Breadcrumbs
mxq
/
ppidcache.c
Blame
Blame
Latest commit
History
History
120 lines (109 loc) · 3.09 KB
Breadcrumbs
mxq
/
ppidcache.c
Top
File metadata and controls
Code
Blame
120 lines (109 loc) · 3.09 KB
Raw
#include <sys/types.h> #include <errno.h> #include "mx_util.h" #include "ppidcache.h" #include "mx_proc.h" #include "mx_log.h" struct entry { pid_t pid; pid_t parent; }; struct ppidcache { int count; int alloc; struct entry *entries; }; struct ppidcache *ppidcache_new(void) { struct ppidcache *ppidcache = mx_malloc_forever(sizeof(struct ppidcache)); ppidcache->count = 0; ppidcache->alloc = 500; ppidcache->entries = mx_malloc_forever(ppidcache->alloc*sizeof(struct entry)); return ppidcache; } void ppidcache_free(struct ppidcache *ppidcache) { free(ppidcache->entries); free(ppidcache); } static int _ppidcache_find(struct ppidcache *ppidcache, pid_t pid) { int i; for (i=0 ; i<ppidcache->count ; i++) { if (ppidcache->entries[i].pid == pid) return i; } return -1; } pid_t ppidcache_get_ppid(struct ppidcache *ppidcache, pid_t pid) { int i = _ppidcache_find(ppidcache, pid); if (i != -1) return ppidcache->entries[i].parent; if (ppidcache->count == ppidcache->alloc) { ppidcache->alloc += 100; struct entry *new = mx_malloc_forever(ppidcache->alloc * sizeof(struct entry)); memcpy(new, ppidcache->entries, ppidcache->count*sizeof(struct entry)); free(ppidcache->entries); ppidcache->entries = new; } pid_t parent = mx_proc_get_parent(pid); ppidcache->entries[ppidcache->count].pid = pid; ppidcache->entries[ppidcache->count++].parent = parent; return parent; } int ppidcache_is_descendant(struct ppidcache *ppidcache, pid_t ancestor, pid_t candidate) { int fuse=100; pid_t pid = candidate; do { pid = ppidcache_get_ppid(ppidcache, pid); if (pid == 0 || pid == -1) return 0; if (pid == ancestor) return 1; } while (--fuse > 0); return 0; } /* * Load the cache with all pids from the system. * Previous cached content, if any, is removed. */ void ppidcache_scan(struct ppidcache *ppidcache) { _mx_cleanup_closedir_ DIR *dir = NULL; pid_t pid; struct dirent *dirent; ppidcache->count = 0; dir = opendir("/proc"); if (dir == NULL) { mx_log_err("/proc: %m"); return; } while (1) { errno = 0; dirent = readdir(dir); if (dirent == NULL) { if (errno) mx_log_err("/proc: %m"); return; } if (strspn(dirent->d_name, "0123456789") != strlen(dirent->d_name)) continue; pid = atoi(dirent->d_name); ppidcache_get_ppid(ppidcache, pid); } } /* * call cb(data, pid) for all cached(!) descendants. * stop when no more descendants or when callback returns 0 */ void ppidcache_do_descendants( struct ppidcache *ppidcache, pid_t pid, int cb(void *data, pid_t pid), void *data) { int next; pid_t candidate; for (next=0 ; next<ppidcache->count; next++) { candidate = ppidcache->entries[next].pid; if (ppidcache_is_descendant(ppidcache, pid, candidate)) if ((*cb)(data, candidate) == 0) return; } }
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
You can’t perform that action at this time.