Drop main() prototype. Syncs with NetBSD-8
[minix.git] / minix / usr.bin / trace / proc.c
blob665ea9f125bd54740109aaaa29e2207780e548fc
2 #include "inc.h"
4 static TAILQ_HEAD(, trace_proc) proc_root;
5 static unsigned int nr_procs;
7 /*
8 * Initialize the list of traced processes.
9 */
10 void
11 proc_init(void)
14 TAILQ_INIT(&proc_root);
15 nr_procs = 0;
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).
23 struct trace_proc *
24 proc_add(pid_t pid)
26 struct trace_proc *proc;
28 proc = (struct trace_proc *)malloc(sizeof(struct trace_proc));
30 if (proc == NULL)
31 return NULL;
33 memset(proc, 0, sizeof(*proc));
35 proc->pid = pid;
37 TAILQ_INSERT_TAIL(&proc_root, proc, next);
38 nr_procs++;
40 return proc;
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.
47 struct trace_proc *
48 proc_get(pid_t pid)
50 struct trace_proc *proc;
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;
58 return NULL;
62 * Remove a process from the list of traced processes.
64 void
65 proc_del(struct trace_proc * proc)
68 TAILQ_REMOVE(&proc_root, proc, next);
69 nr_procs--;
71 free(proc);
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.
79 struct trace_proc *
80 proc_next(struct trace_proc * proc)
83 if (proc == NULL)
84 return TAILQ_FIRST(&proc_root);
85 else
86 return TAILQ_NEXT(proc, next);
90 * Return the number of processes in the list of traced processes.
92 unsigned int
93 proc_count(void)
96 return nr_procs;