aboutsummaryrefslogtreecommitdiffstats
path: root/src/process_iterator.c
blob: e27ed4c4768a906acf20fd0b9a8684d8014e4b94 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/procfs.h>
#include <time.h>
#include "process_iterator.h"

//See this link to port to other systems: http://www.steve.org.uk/Reference/Unix/faq_8.html#SEC85

#ifdef __linux__

static int get_boot_time()
{
	int uptime;
	FILE *fp = fopen ("/proc/uptime", "r");
	if (fp != NULL)
	{
		char buf[BUFSIZ];
		char *b = fgets(buf, BUFSIZ, fp);
		if (b == buf)
		{
			char *end_ptr;
			double upsecs = strtod(buf, &end_ptr);
			uptime = (int)upsecs;
		}
		fclose (fp);
	}
	time_t now = time(NULL);
	return now - uptime;
}

int init_process_iterator(struct process_iterator *it, struct process_filter *filter)
{
	//open a directory stream to /proc directory
	if ((it->dip = opendir("/proc")) == NULL)
	{
		perror("opendir");
		return -1;
	}
	it->filter = filter;
	it->boot_time = get_boot_time();
	return 0;
}

static int read_process_info(pid_t pid, struct process *p)
{
	static char buffer[1024];
	static char statfile[32];
	static char exefile[1024];
	p->pid = pid;
	//read stat file
	sprintf(statfile, "/proc/%d/stat", p->pid);
	FILE *fd = fopen(statfile, "r");
	if (fd==NULL) return -1;
	if (fgets(buffer, sizeof(buffer), fd)==NULL) {
		fclose(fd);
		return -1;
	}
	fclose(fd);
	char *token = strtok(buffer, " ");
	int i;
	for (i=0; i<3; i++) token = strtok(NULL, " ");
	p->ppid = atoi(token);
	for (i=0; i<10; i++)
		token = strtok(NULL, " ");
	p->cputime = atoi(token) * 1000 / HZ;
	token = strtok(NULL, " ");
	p->cputime += atoi(token) * 1000 / HZ;
	for (i=0; i<7; i++)
		token = strtok(NULL, " ");
	p->starttime = atoi(token) / sysconf(_SC_CLK_TCK);
	//read command line
	sprintf(exefile,"/proc/%d/cmdline", p->pid);
	fd = fopen(statfile, "r");
	if (fgets(buffer, sizeof(buffer), fd)==NULL) {
		fclose(fd);
		return -1;
	}
	fclose(fd);
	sscanf(buffer, "%s", (char*)&p->command);
	return 0;
}

static pid_t getppid_of(pid_t pid)
{
	char statfile[20];
	char buffer[1024];
	sprintf(statfile, "/proc/%d/stat", pid);
	FILE *fd = fopen(statfile, "r");
	if (fd==NULL) return -1;
	if (fgets(buffer, sizeof(buffer), fd)==NULL) {
		fclose(fd);
		return -1;
	}
	fclose(fd);
	char *token = strtok(buffer, " ");
	int i;
	for (i=0; i<3; i++) token = strtok(NULL, " ");
	return atoi(token);
}

static int is_child_of(pid_t child_pid, pid_t parent_pid)
{
	int ppid = child_pid;
	while(ppid > 1 && ppid != parent_pid) {
		ppid = getppid_of(ppid);
	}
	return ppid == parent_pid;
}

int get_next_process(struct process_iterator *it, struct process *p)
{
	if (it->dip == NULL)
	{
		//end of processes
		return -1;
	}
	if (it->filter->pid > 0 && !it->filter->include_children)
	{
		read_process_info(it->filter->pid, p);
		p->starttime += it->boot_time;
		it->dip = NULL;
		return 0;
	}
	struct dirent *dit;
	//read in from /proc and seek for process dirs
	while ((dit = readdir(it->dip)) != NULL) {
		if(strtok(dit->d_name, "0123456789") != NULL)
			continue;
		p->pid = atoi(dit->d_name);
		if (it->filter->pid > 0 && it->filter->pid != p->pid && !is_child_of(p->pid, it->filter->pid)) continue;
		read_process_info(p->pid, p);
		p->starttime += it->boot_time;
		break;
	}
	if (dit == NULL)
	{
		//end of processes
		closedir(it->dip);
		it->dip = NULL;
		return -1;
	}
	return 0;
}

int close_process_iterator(struct process_iterator *it) {
	if (it->dip != 0 && closedir(it->dip) == -1) {
		perror("closedir");
		return 1;
	}
	it->dip = NULL;
	return 0;
}

#elif defined __FreeBSD__

#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;
}

#elif defined __APPLE__

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

int get_next_process(struct process_iterator *it, struct process *p) {
	return -1;
}

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

	// int err;
	// struct kinfo_proc *result = NULL;
	// size_t length;
	// int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};

	// /* We start by calling sysctl with result == NULL and length == 0.
	//    That will succeed, and set length to the appropriate length.
	//    We then allocate a buffer of that size and call sysctl again
	//    with that buffer.
	// */
	// length = 0;
	// err = sysctl(mib, 4, NULL, &length, NULL, 0);
	// if (err == -1) {
	// 	err = errno;
	// }
	// if (err == 0) {
	// 	result = malloc(length);
	// 	err = sysctl(mib, 4, result, &length, NULL, 0);
	// 	if (err == -1)
	// 		err = errno;
	// 	if (err == ENOMEM) {
	// 		free(result); /* clean up */
	// 		result = NULL;
	// 	}
	// }

	// i->proclist = result;
	// i->count = err == 0 ? length / sizeof *result : 0;
	// i->c = 0;

// int get_proc_info(struct process *p, pid_t pid) {
// 	int err;
// 	struct kinfo_proc *result = NULL;
// 	size_t length;
// 	int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};

// 	/* We start by calling sysctl with result == NULL and length == 0.
// 	   That will succeed, and set length to the appropriate length.
// 	   We then allocate a buffer of that size and call sysctl again
// 	   with that buffer.
// 	*/
// 	length = 0;
// 	err = sysctl(mib, 4, NULL, &length, NULL, 0);
// 	if (err == -1) {
// 		err = errno;
// 	}
// 	if (err == 0) {
// 		result = malloc(length);
// 		err = sysctl(mib, 4, result, &length, NULL, 0);
// 		if (err == -1)
// 			err = errno;
// 		if (err == ENOMEM) {
// 			free(result); /* clean up */
// 			result = NULL;
// 		}
// 	}

// 	p->pid = result->kp_proc.p_pid;
// 	p->ppid = result->kp_eproc.e_ppid;
// 	p->starttime = result->kp_proc.p_starttime.tv_sec;
// 	p->last_jiffies = result->kp_proc.p_cpticks;
// 	//p_pctcpu

// 	return 0;
// }

#elif defined __APPLE__

#endif
Un proyecto texto-plano.xyz