aboutsummaryrefslogtreecommitdiffstats
path: root/process.c
blob: 5d88a04fb8b983a421466844bcb387b217af5c66 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


static struct process head;
static struct process tail;


void
init_process_list(void)
{
	head.next = &tail;
	tail.prev = &head;
}


struct process *
find_process(pid_t pid)
{
	struct process *p;
	for (p = head.next; p->next; p = p->next)
		if (p->pid == pid)
			return p;
	return NULL;
}


struct process *
add_process(pid_t pid, pid_t leader, unsigned long int trace_options)
{
	struct process *proc;
	int status, sig;

	proc = calloc(1, sizeof(*proc));
	if (!proc)
		eprintf("calloc: %s\n");
	proc->pid = pid;
	proc->thread_leader = leader;
	proc->next = &tail;
	proc->prev = tail.prev;
	proc->prev->next = proc;
	tail.prev = proc;

	if (!leader)
		leader = pid;

	while (waitpid(pid, &status, WUNTRACED) != pid) {
		if (errno == EINTR)
			continue;
		eprintf_and_kill(pid, "waitpid %ju <buffer> WSTOPPED:", (uintmax_t)pid);
	}

	sig = WIFSTOPPED(status) ? WSTOPSIG(status) : 0;
	if (sig == SIGSTOP) {
		if (ptrace(PTRACE_SEIZE, pid, NULL, trace_options))
			eprintf_and_kill(pid, "ptrace PTRACE_SEIZE %ju NULL ...:", (uintmax_t)pid);
		if (ptrace(PTRACE_INTERRUPT, pid, NULL, 0))
			eprintf_and_kill(pid, "ptrace PTRACE_INTERRUPT %ju NULL 0:", (uintmax_t)pid);
		if (tgkill(leader, pid, SIGCONT) < 0)
			eprintf_and_kill(pid, "tgkill %ju %ju SIGCONT:", (uintmax_t)leader, (uintmax_t)pid);
	} else if (sig == SIGTRAP && status & PTRACE_EVENT_STOP << 16) {
		if (ptrace(PTRACE_SETOPTIONS, pid, NULL, trace_options))
			eprintf_and_kill(pid, "ptrace PTRACE_SETOPTIONS %ju NULL ...:", (uintmax_t)pid);
	} else {
		eprintf_and_kill(pid, "unexpected return of waitpid %ju <buffer> WSTOPPED: %#x\n", (uintmax_t)pid, status);
	}

	return proc;
}


void
remove_process(struct process *proc)
{
 	proc->prev->next = proc->next;
	proc->next->prev = proc->prev;
	if (proc->vfork_waiting_on)
		proc->vfork_waiting_on->continue_on_exit = NULL;
	if (proc->continue_on_exit)
		proc->continue_on_exit->vfork_waiting_on = NULL;
	free(proc);
}