forked from iitis/tracedump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid.c
68 lines (56 loc) · 1.17 KB
/
pid.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
/*
* Copyright (C) 2011 IITiS PAN Gliwice <www.iitis.pl>
* Author: Paweł Foremski <[email protected]>
* Licensed under GNU GPL v. 3
*/
#include "tracedump.h"
struct pid *pid_get(struct tracedump *td, pid_t pid)
{
struct pid *sp;
/* speed-up cache */
if (td->sp && td->sp->pid == pid)
return td->sp;
sp = thash_uint_get(td->pids, pid);
if (!sp) {
dbg(7, "new pid %d\n", pid);
sp = mmatic_zalloc(td->mm, sizeof *sp);
sp->td = td;
sp->pid = pid;
thash_uint_set(td->pids, pid, sp);
}
return sp;
}
void pid_del(struct tracedump *td, pid_t pid)
{
dbg(7, "deleting pid %d\n", pid);
thash_uint_set(td->pids, pid, NULL);
}
void pid_detach_all(struct tracedump *td)
{
struct pid *sp;
thash_reset(td->pids);
while ((sp = thash_uint_iter(td->pids, NULL))) {
ptrace_detach(sp, 0);
pid_del(td, sp->pid);
}
}
int pid_tgid(pid_t pid)
{
char buf[256];
FILE *fp;
int ret = 0;
snprintf(buf, sizeof buf, "/proc/%d/status", pid);
fp = fopen(buf, "r");
if (!fp) {
dbg(1, "fopen(%s): %s\n", buf, strerror(errno));
return 0;
}
while (fgets(buf, sizeof buf, fp)) {
if (strncmp("Tgid:", buf, 5) == 0) {
ret = atoi(buf+6);
break;
}
}
fclose(fp);
return ret;
}