xref: /minix/minix/usr.bin/trace/proc.c (revision 9f988b79)
1 
2 #include "inc.h"
3 
4 static TAILQ_HEAD(, trace_proc) proc_root;
5 static unsigned int nr_procs;
6 
7 /*
8  * Initialize the list of traced processes.
9  */
10 void
11 proc_init(void)
12 {
13 
14 	TAILQ_INIT(&proc_root);
15 	nr_procs = 0;
16 }
17 
18 /*
19  * Add a new process to the list of traced processes, allocating memory for it
20  * first.  Return the new process structure with its PID assigned and the rest
21  * zeroed out, or NULL upon allocation failure (with errno set appropriately).
22  */
23 struct trace_proc *
24 proc_add(pid_t pid)
25 {
26 	struct trace_proc *proc;
27 
28 	proc = (struct trace_proc *)malloc(sizeof(struct trace_proc));
29 
30 	if (proc == NULL)
31 		return NULL;
32 
33 	memset(proc, 0, sizeof(*proc));
34 
35 	proc->pid = pid;
36 
37 	TAILQ_INSERT_TAIL(&proc_root, proc, next);
38 	nr_procs++;
39 
40 	return proc;
41 }
42 
43 /*
44  * Retrieve the data structure for a traced process based on its PID.  Return
45  * a pointer to the structure, or NULL if no structure exists for this process.
46  */
47 struct trace_proc *
48 proc_get(pid_t pid)
49 {
50 	struct trace_proc *proc;
51 
52 	/* Linear search for now; se we can easily add a hashtable later.. */
53 	TAILQ_FOREACH(proc, &proc_root, next) {
54 		if (proc->pid == pid)
55 			return proc;
56 	}
57 
58 	return NULL;
59 }
60 
61 /*
62  * Remove a process from the list of traced processes.
63  */
64 void
65 proc_del(struct trace_proc * proc)
66 {
67 
68 	TAILQ_REMOVE(&proc_root, proc, next);
69 	nr_procs--;
70 
71 	free(proc);
72 }
73 
74 /*
75  * Iterator for the list of traced processes.  If a NULL pointer is given,
76  * return the first process in the list; otherwise, return the next process in
77  * the list.  Not stable with respect to list modifications.
78  */
79 struct trace_proc *
80 proc_next(struct trace_proc * proc)
81 {
82 
83 	if (proc == NULL)
84 		return TAILQ_FIRST(&proc_root);
85 	else
86 		return TAILQ_NEXT(proc, next);
87 }
88 
89 /*
90  * Return the number of processes in the list of traced processes.
91  */
92 unsigned int
93 proc_count(void)
94 {
95 
96 	return nr_procs;
97 }
98