-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathproc.cpy
More file actions
352 lines (288 loc) · 8.4 KB
/
proc.cpy
File metadata and controls
352 lines (288 loc) · 8.4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <dirent.h>
#include <libgen.h>
#include "string.h"
#include <ctype.h>
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <unordered_set>
#include <thread>
#include <set>
#define PID 1
#define PGROUP 4
// {{{ from https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
```
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include <fstream>
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
```
// }}}
namespace proc:
struct Proc:
int pid
string cmdline
vector<string> stat
;
struct MemInfo:
int total = 0
int available = 0
int used = 0
;
std::set<string> known_interpreters {"python", "python3", "bash", "sh"}
string join_path(vector<string> v):
ostringstream s;
for const auto& i : v:
if &i != &v[0]:
s << "/"
s << i
return s.str();
vector<string> read_dir(string bin_dir, bool set_path=false):
DIR *dir
struct dirent *ent
vector<string> filenames
if ((dir = opendir (bin_dir.c_str())) != NULL):
while ((ent = readdir (dir)) != NULL):
str_d_name := string(ent->d_name)
if str_d_name != "." and str_d_name != "..":
if set_path:
str_d_name = join_path({bin_dir, ent->d_name})
filenames.push_back(str_d_name)
closedir (dir)
else:
perror ("")
return filenames
vector<string> read_pids():
ret := vector<string>()
files := read_dir("/proc")
for auto file : files:
allnumbers := true
for auto c : file:
if not std::isdigit(c):
allnumbers = false
break
if allnumbers:
ret.push_back(file)
return ret
// checks that cmdline contains a value in args
// BUT it skips any tokens in cmdline that start with '-'
bool check_args(string cmdline, vector<string> &args):
found := false
cmd_tokens := str_utils::split(cmdline, 0)
for auto cmd_token : cmd_tokens:
if cmd_token.size() > 0 && cmd_token[0] == '-':
continue
base := cmd_token.c_str()
full_str := string(base)
base = basename((char *) base)
base_str := string(base)
auto search_interpreter = known_interpreters.find(base_str)
if search_interpreter != known_interpreters.end():
continue
for auto needle : args:
if base_str == needle || full_str == needle:
found = true
break
break
return found
vector<Proc> list_procs(vector<string> &args):
vector<Proc> ret;
needle := string("")
if len(args) > 1:
needle = args[1]
string cmdline
string stat
pids := read_pids()
mypid := to_string(getpid())
for auto p : pids:
if p == mypid:
continue
ifstream f(join_path({"/proc", p, "cmdline"}))
getline(f, cmdline)
if cmdline == "":
continue
if not check_args(cmdline, args):
continue
f = ifstream(join_path({"/proc", p, "stat"}))
getline(f, stat)
fields := str_utils::split(stat, ' ')
ret.push_back(Proc{std::stoi(p), cmdline, fields})
return ret
void groupkill(int signal, vector<string> &args):
procs := list_procs(args)
unordered_set<string> tokill
for auto proc : procs:
tokill.insert(proc.stat[PGROUP])
for auto p: tokill:
pid := std::stoi(p)
ret := kill(-pid, signal)
debug "SENDING", signal, "TO GROUP", -pid, "RET", ret
bool is_running(string bin):
vector<string> bins = { bin }
procs := list_procs(bins)
if procs.size() == 0:
return false
pid := procs[0].pid
fname := join_path({"/proc/", to_string(pid), "/wchan"})
ifstream f(fname)
string status
getline(f, status)
return status != "do_signal_stop"
map<string, bool> is_running(vector<string> bins, vector<Proc> &procs):
map<string, bool> ret;
for auto proc : procs:
for auto bin : bins:
args := vector<string> { bin }
if check_args(proc.cmdline, args):
ret[bin] = true
return ret
map<string, bool> is_running(vector<string> bins):
procs := list_procs(bins)
return is_running(bins, procs)
MemInfo read_mem_total():
mem_info := MemInfo{}
fname := "/proc/meminfo"
string line
int val = 0
ifstream f(fname)
while getline(f, line):
tokens := str_utils::split(line, ':')
if tokens.size() > 1:
try:
val = stoi(tokens[1])
catch(...):
continue
if tokens[0] == "MemTotal":
mem_info.total = val
if tokens[0] == "MemAvailable":
mem_info.available = val
mem_info.used = mem_info.total - mem_info.available
return mem_info
int read_mem_from_status(int pid):
val := -1
fname := join_path({"/proc/", to_string(pid), "/status"})
ifstream f(fname)
string line
while getline(f, line):
tokens := str_utils::split(line, ':')
if tokens.size() > 1 and tokens[0] == "VmSize":
val = stoi(tokens[1])
return val
int read_mem_from_smaps(string fname):
shared := 0
priv := 0
pss := 0
swap := 0
swap_pss := 0
ifstream f(fname)
string line
while getline(f, line):
tokens := str_utils::split(line, ':')
if tokens.size() > 1:
val_tokens := str_utils::split(tokens[1], ' ')
if tokens[0].find("Shared") == 0:
val := stoi(val_tokens[0])
shared += val
if tokens[0].find("Private") == 0:
val := stoi(val_tokens[0])
priv += val
if tokens[0] == "Pss":
val := stoi(val_tokens[0])
pss += val
if tokens[0] == "Swap":
val := stoi(val_tokens[0])
swap += val
if tokens[0] == "SwapPss":
val := stoi(val_tokens[0])
swap_pss += val
if pss > 0:
shared = pss - priv
return priv
// collect per process memory usage
map<int, int> collect_mem(vector<Proc> pids):
int val
map<int, int> mem_usage;
for auto p: pids:
fname := join_path({"/proc/", to_string(p.pid), "/smaps_rollup"})
try:
val = mem_usage[p.pid] = read_mem_from_smaps(fname)
catch(...):
pass
if val == 0:
fname = join_path({"/proc/", to_string(p.pid), "/smaps"})
try:
val = mem_usage[p.pid] = read_mem_from_smaps(fname)
catch(...):
pass
if val == 0:
mem_usage[p.pid] = read_mem_from_status(p.pid)
return mem_usage
void stop_programs(vector<string> programs, string signal=""):
for auto s : programs:
#ifdef REMARKABLE
cmd := "killall" + signal + " " + s + " 2> /dev/null"
if system(cmd.c_str()) == 0:
if signal != "":
debug "SENT", signal, "TO", s
else:
debug "KILLED", s
#endif
pass
return
void stop_xochitl():
#ifdef REMARKABLE
if system("systemctl stop xochitl 2> /dev/null") == 0:
debug "STOPPED XOCHITL"
#endif
return
void start_xochitl():
#ifdef REMARKABLE
if system("systemctl restart xochitl 2> /dev/null") == 0:
debug "STARTING XOCHITL"
#endif
return
bool exe_exists(string name):
char command[PATH_MAX]
sprintf(command, "test -x %s", name.c_str());
return 0 == system(command);
bool check_process(string name):
args := vector<string>{name}
procs := list_procs(args)
debug "CHECKING PROCESS", name, procs.size()
return procs.size() > 0
void launch_process(string name, bool check_running=false, background=false):
tokens := str_utils::split(name, ' ')
cstr := tokens[0].c_str()
base := string(basename((char *) cstr))
if check_running && check_process(base):
debug base, "IS ALREADY RUNNING, RESUMING"
term := vector<string> { base }
groupkill(SIGCONT, term)
return
proc := name
if background:
proc = "setsid " + proc
auto *th = new thread([=]() {
c_str := proc.c_str()
_ := system(c_str)
debug "PROGRAM EXITED"
})