aboutsummaryrefslogtreecommitdiffstats
path: root/src/process_iterator_freebsd.c
blob: fae671d7163ca44e35f3cd55ef4e9883a0347096 (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
83
84
85
86
87
88
89
90
91
92
#include <sys/sysctl.h>
#include <sys/user.h>
#include <fcntl.h>
#include <paths.h>

int init_process_iterator(struct process_iterator *it, struct process_filter *filter) {
	kvm_t *kd;
	char errbuf[_POSIX2_LINE_MAX];
	it->i = 0;
	/* Open the kvm interface, get a descriptor */
	if ((kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf)) == NULL) {
		/* fprintf(stderr, "kvm_open: %s\n", errbuf); */
		fprintf(stderr, "kvm_open: %s", errbuf);
		return -1;
	}
	/* Get the list of processes. */
	if ((it->procs = kvm_getprocs(kd, KERN_PROC_PROC, 0, &it->count)) == NULL) {
		kvm_close(kd);
		/* fprintf(stderr, "kvm_getprocs: %s\n", kvm_geterr(kd)); */
		fprintf(stderr, "kvm_getprocs: %s", kvm_geterr(kd));
		return -1;
	}
	kvm_close(kd);
	it->filter = filter;
	return 0;
}

static void kproc2proc(struct kinfo_proc *kproc, struct process *proc)
{
	proc->pid = kproc->ki_pid;
	proc->ppid = kproc->ki_ppid;
	proc->cputime = kproc->ki_runtime / 1000;
	proc->starttime = kproc->ki_start.tv_sec;
}

static int get_single_process(pid_t pid, struct process *process)
{
	kvm_t *kd;
	int count;
	char errbuf[_POSIX2_LINE_MAX];
	/* Open the kvm interface, get a descriptor */
	if ((kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf)) == NULL) {
		/* fprintf(stderr, "kvm_open: %s\n", errbuf); */
		fprintf(stderr, "kvm_open: %s", errbuf);
		return -1;
	}
	struct kinfo_proc *kproc = kvm_getprocs(kd, KERN_PROC_PID, pid, &count);
	kvm_close(kd);
	if (count == 0 || kproc == NULL)
	{
		fprintf(stderr, "kvm_getprocs: %s", kvm_geterr(kd));
		return -1;
	}
	kproc2proc(kproc, process);
	return 0;
}

int get_next_process(struct process_iterator *it, struct process *p) {
	if (it->i == it->count)
	{
		return -1;
	}
	if (it->filter->pid > 0 && !it->filter->include_children)
	{
		get_single_process(it->filter->pid, p);
		it->i = it->count = 1;
		return 0;
	}
	while (it->i < it->count)
	{
		if (it->filter->pid > 0 && it->filter->include_children)
		{
			kproc2proc(&(it->procs[it->i]), p);
			it->i++;
			if (p->pid != it->filter->pid && p->ppid != it->filter->pid)
				continue;
			return 0;
		}
		else if (it->filter->pid == 0)
		{
			kproc2proc(&(it->procs[it->i]), p);
			it->i++;
			return 0;
		}
	}
	return -1;
}

int close_process_iterator(struct process_iterator *it) {
	return 0;
}

Un proyecto texto-plano.xyz