-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproc.c
466 lines (367 loc) · 8.68 KB
/
proc.c
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include "structs.h"
#include "proc.h"
#include "util.h"
#include "main.h"
#include "machine.h"
#define PROC_IS_KERNEL(p) ((p)->ppid == 0 || (p)->ppid == 2)
#define ITER_PROCS(i, p, ps) \
for(i = 0; i < HASH_TABLE_SIZE; i++) \
for(p = ps[i]; p; p = p->hash_next)
/* Processes are saved into a hash table with size HASH_TABLE_SIZE and
* key pid % HASH_TABLE_SIZE. If the key already exists, the new
* element is appended to the last one throught hash_next:
[ 0] = { NULL },
[ 1] = { { "vim", 4321 }, NULL },
[ 2] = { { "sh", 9821 }, { "xterm", 9801 }, NULL }
...
[NPROCS] = { NULL },
*/
static void proc_add_child(struct myproc *parent, struct myproc *child)
{
size_t n = 0;
struct myproc **i;
for(i = parent->children; i && *i; i++, n++);
n++; /* new */
parent->children = urealloc(parent->children, (n + 1) * sizeof *parent->children);
parent->children[n - 1] = child;
parent->children[n] = NULL;
}
static void proc_rm_child(struct myproc *parent, struct myproc *p)
{
size_t n, idx, found;
struct myproc **pi;
n = idx = found = 0;
for(pi = parent->children; pi && *pi; pi++, n++)
if(*pi == p)
found = 1;
else if(!found)
idx++;
if(!found)
return;
if(n <= 1){
free(parent->children);
parent->children = NULL;
}else{
n--; /* gone */
/*memmove(parent->children + idx,
parent->children + idx + 1,
n * sizeof *parent->children);*/
size_t i;
for(i = idx; i < n; i++)
parent->children[i] = parent->children[i + 1];
parent->children = urealloc(parent->children, (n + 1) * sizeof *parent->children);
parent->children[n] = NULL;
}
}
static void proc_free(struct myproc *p, struct myproc **procs)
{
struct myproc *i = proc_get(procs, p->ppid);
/* remove parent references */
if(i)
proc_rm_child(i, p);
/* remove hash-table references */
i = procs[p->pid % HASH_TABLE_SIZE];
if(i == p){
procs[p->pid % HASH_TABLE_SIZE] = p->hash_next;
}else{
while(i && i->hash_next != p)
i = i->hash_next;
if(i)
/* i->hash_next is p, reroute */
i->hash_next = p->hash_next;
}
free(p->unam);
free(p->gnam);
free(p->shell_cmd);
/*free(p->argv0_basename); - do not free*/
argv_free(p->argc, p->argv);
free(p->tty);
free(p);
}
struct myproc *proc_get(struct myproc **procs, pid_t pid)
{
struct myproc *p;
if(pid >= 0)
for(p = procs[pid % HASH_TABLE_SIZE]; p; p = p->hash_next)
if(p->pid == pid)
return p;
return NULL;
}
// Add a struct myproc pointer to the hash table
void proc_addto(struct myproc **procs, struct myproc *p)
{
struct myproc *last = procs[p->pid % HASH_TABLE_SIZE];
if(last){
while(last->hash_next)
last = last->hash_next;
last->hash_next = p;
}else{
procs[p->pid % HASH_TABLE_SIZE] = p;
}
p->hash_next = NULL;
struct myproc *parent = proc_get(procs, p->ppid);
if(parent)
proc_add_child(parent, p);
}
// initialize hash table
struct myproc **proc_init()
{
return umalloc(HASH_TABLE_SIZE * sizeof *proc_init());
}
const char *proc_state_str(struct myproc *p)
{
return (const char *[]){
"R",
"S",
"D",
"T",
"Z",
"X",
"t",
"?",
}[p->state];
}
void proc_create_shell_cmd(struct myproc *this)
{
char *cmd;
size_t cmd_len;
/* recreate this->shell_cmd from argv */
for(size_t i = cmd_len = 0; i < this->argc; i++)
cmd_len += strlen(this->argv[i]) + 1;
free(this->shell_cmd);
this->shell_cmd = umalloc(cmd_len + 1);
cmd = this->shell_cmd;
for(size_t i = 0; i < this->argc; i++)
cmd += sprintf(cmd, "%s ", this->argv[i]);
}
static void proc_update_single(
struct myproc *proc,
struct myproc **procs,
struct sysinfo *info)
{
if(machine_proc_exists(proc)){
const pid_t oldppid = proc->ppid;
machine_update_proc(proc);
info->count++;
if(PROC_IS_KERNEL(proc))
info->count_kernel++;
if(proc->uid == globals.uid)
info->owned++;
info->procs_in_state[proc->state]++;
if(oldppid != proc->ppid){
struct myproc *parent = proc_get(procs, proc->ppid);
if(parent){
proc_add_child(parent, proc);
}else{
/* TODO: reparent to init? */
}
}
proc_create_shell_cmd(proc);
}else{
proc_free(proc, procs);
}
}
enum proc_state proc_state_parse(char c)
{
switch(c){
case 'R': return PROC_STATE_RUN;
case 'I': /* very sleepy - "idle" */
case 'S': return PROC_STATE_SLEEP;
case 'U': /* uninterruptible wait */
case 'D': return PROC_STATE_DISK;
case 'T': return PROC_STATE_STOPPED;
case 'Z': return PROC_STATE_ZOMBIE;
case 'X': return PROC_STATE_DEAD;
case 't': return PROC_STATE_TRACE;
default:
return PROC_STATE_OTHER;
}
}
const char *proc_str(struct myproc *p)
{
static char buf[256];
snprintf(buf, sizeof buf,
"{ pid=%d, ppid=%d, state=%d, cmd=\"%s\"}",
p->pid, p->ppid, (int)p->state, p->shell_cmd);
return buf;
}
void proc_update(struct myproc **procs, struct sysinfo *info)
{
int i;
info->count = info->count_kernel = info->owned = 0;
memset(info->procs_in_state, 0, sizeof info->procs_in_state);
for(i = 0; i < HASH_TABLE_SIZE; i++){
struct myproc **change_me;
struct myproc *p;
change_me = &procs[i];
for(p = procs[i]; p; ){
if(p){
if(!machine_proc_exists(p)){
/* dead */
struct myproc *next = p->hash_next;
*change_me = next;
proc_free(p, procs);
p = next;
}else{
proc_update_single(p, procs, info);
change_me = &p->hash_next;
p = p->hash_next;
}
}
}
}
machine_proc_get_more(procs);
}
void proc_dump(struct myproc **ps, FILE *f)
{
struct myproc *p;
int i;
ITER_PROCS(i, p, ps)
fprintf(f, "%s\n", proc_str(p));
}
struct myproc *proc_find(const char *str, struct myproc **ps)
{
return proc_find_n(str, ps, 0);
}
static int proc_find_match(const char *shell_cmd, const char *search_str)
{
/* TODO: regex */
return !!ustrcasestr(shell_cmd, search_str);
}
static struct myproc *proc_find_n_child(const char *str, struct myproc *proc, int *n)
{
struct myproc **i;
if(proc->shell_cmd && proc_find_match(proc->shell_cmd, str) && --*n < 0)
return proc;
for(i = proc->children; i && *i; i++){
struct myproc *p = *i;
if((p = proc_find_n_child(str, p, n)))
return p;
}
return NULL;
}
struct myproc *proc_find_n(const char *str, struct myproc **ps, int n)
{
#ifdef HASH_TABLE_ORDER
struct myproc *p;
int i;
if(str)
ITER_PROCS(i, p, ps)
if(p->shell_cmd && proc_find_match(p->shell_cmd, str) && n-- <= 0)
return p;
return NULL;
#else
/* search in the same order the procs are displayed */
// TODO: multiple parents
return proc_find_n_child(str, proc_first(ps), &n);
#endif
}
static int proc_to_idx_nested(
struct myproc *head, struct myproc *const searchee,
int *py, int in_fold)
{
if(head == searchee)
return 1;
if(!in_fold)
++*py;
for(struct myproc **iter = head->children;
iter && *iter;
iter++)
{
if(searchee == *iter)
return 1;
if(proc_to_idx_nested(*iter, searchee, py, in_fold || head->folded))
return 1;
}
return 0;
}
int proc_to_idx(struct myproc **procs, struct myproc *searchee, int *py)
{
ITER_PROC_HEADS(struct myproc *, head, procs)
if(proc_to_idx_nested(head, searchee, py, head->folded))
return 1;
return 0;
}
static struct myproc *proc_from_idx_nested(struct myproc *head, int *idx)
{
if(*idx == 0)
return head;
for(struct myproc **iter = head->children;
iter && *iter;
iter++)
{
if(--*idx <= 0){
return *iter;
}else if(!(*iter)->folded){
struct myproc *sub = proc_from_idx_nested(*iter, idx);
if(sub)
return sub;
}
}
return NULL;
}
struct myproc *proc_from_idx(struct myproc **procs, int *idx)
{
// TODO: kernel threads
if(*idx < 0)
return NULL;
ITER_PROC_HEADS(struct myproc *, head, procs){
struct myproc *test = proc_from_idx_nested(head, idx);
if(test)
return test;
}
return NULL;
#undef RET
}
struct myproc *proc_first(struct myproc **procs)
{
struct myproc *p = proc_get(procs, 1); /* init */
int i;
if(p)
return p;
/* the most parent-y */
ITER_PROCS(i, p, procs)
if(p->ppid == 0)
return p;
ITER_PROCS(i, p, procs)
if(p->ppid == 1)
return p;
ITER_PROCS(i, p, procs)
if(!proc_get(procs, p->ppid))
return p;
return NULL;
}
struct myproc *proc_first_next(struct myproc **procs)
{
struct myproc *p;
int i;
ITER_PROCS(i, p, procs)
if(!p->mark && !proc_get(procs, p->ppid))
return p;
ITER_PROCS(i, p, procs)
if(!p->mark)
return p;
return NULL;
}
void proc_unmark(struct myproc **procs)
{
struct myproc *p;
int i;
ITER_PROCS(i, p, procs)
/* unmark everything except those whose ppids we don't have yet */
p->mark = p->ppid == -1;
}
void proc_mark_kernel(struct myproc **procs)
{
struct myproc *p;
int i;
ITER_PROCS(i, p, procs)
if(PROC_IS_KERNEL(p))
p->mark = 1;
}