File: tasks.c

package info (click to toggle)
bootchart2 0.14.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 5,036 kB
  • sloc: ansic: 1,805; python: 1,764; sh: 155; makefile: 88
file content (240 lines) | stat: -rw-r--r-- 5,262 bytes parent folder | download
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
/*
 * tasks - code to provide a view of what processes are available
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Michael Meeks <michael.meeks@novell.com>
 * Copyright (C) 2010 Novell, Inc.
 */

#include "common.h"

static int
was_known_pid (PidMap *map, pid_t p)
{
	int bit = p & 0x7;
	int offset = p >> 3;
	int was_known;

	if (map->len <= offset) {
		map->len += 512;
		map->pids = realloc (map->pids, map->len);
		memset (map->pids + map->len - 512, 0, 512);
	}

	was_known = map->pids[offset] & (1 << bit);
	map->pids[offset] |= (1 << bit);

	return was_known;
}

typedef struct {
	PidScanner     parent;

	/* fields for /proc polling */
	DIR           *proc;
	struct dirent *cur_ent;
	pid_t	         cur_pid;

	/* fields for /proc/task polling */
	DIR		*proc_task;
} ProcPidScanner;

PidScanner *
pid_scanner_alloc (int derived_size, PidScanEventFn event_fn, void *user_data)
{
	PidScanner *scanner;

	scanner = calloc (1, derived_size);
	scanner->event_fn = event_fn;
	scanner->user_data = user_data;

	return scanner;
}

static int
proc_pid_scanner_free (PidScanner *scanner)
{
	int ret = 0;
	ProcPidScanner *ps = (ProcPidScanner *)scanner;

	if (scanner) {
		if (closedir (ps->proc) < 0)
			{
				perror ("close /proc");
				ret = 1;
			}
		free (scanner);
	}
	return ret;
}

static void
proc_pid_scanner_restart (PidScanner *scanner)
{
	ProcPidScanner *ps = (ProcPidScanner *)scanner;
	rewinddir (ps->proc);
}

static pid_t
proc_pid_scanner_next (PidScanner *scanner)
{
	pid_t pid;
	ProcPidScanner *ps = (ProcPidScanner *)scanner;

	do {
		if (!(ps->cur_ent = readdir (ps->proc)))
			return 0;
	} while (!isdigit (ps->cur_ent->d_name[0]));

	pid = atoi (ps->cur_ent->d_name);
	ps->cur_pid = pid;

	pid_scanner_emit_exec (scanner, pid);

	return pid;
}

static pid_t
proc_pid_scanner_get_cur_pid (PidScanner *scanner)
{
	ProcPidScanner *ps = (ProcPidScanner *)scanner;
	return ps->cur_pid;
}

static pid_t
proc_pid_scanner_get_cur_ppid (PidScanner *scanner)
{
	return 0;
}

static void
proc_pid_scanner_get_tasks_start (PidScanner *scanner)
{
	// use dirfd and 'openat' to accelerate task reading & path concstruction [!]
	int dfd;
	ProcPidScanner *ps = (ProcPidScanner *)scanner;
	char *buffer = alloca (ps->cur_ent->d_reclen + 10);

	strcpy (buffer, ps->cur_ent->d_name);
	strcat (buffer, "/task");

	dfd = openat (dirfd (ps->proc), buffer, O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY);
	if (dfd < 0) {
		ps->proc_task = NULL;
/*		fprintf (stderr, "error: failed to open '%s'\n", buffer); */
	} else
		ps->proc_task = fdopendir (dfd);
}

static void
proc_pid_scanner_get_tasks_stop (PidScanner *scanner)
{
	ProcPidScanner *ps = (ProcPidScanner *)scanner;

	if (ps->proc_task) {
		closedir (ps->proc_task);
		ps->proc_task = NULL;
	}
}

/*
 * Return all tasks that are not the current pid.
 */
static pid_t
proc_pid_scanner_get_tasks_next (PidScanner *scanner)
{
	struct dirent *tent;
	ProcPidScanner *ps = (ProcPidScanner *)scanner;

	if (!ps->proc_task)
		return 0;

	for (;;) {
		pid_t tpid;

		if ((tent = readdir (ps->proc_task)) == NULL)
			return 0;
		if (!isdigit (tent->d_name[0]))
			continue;
		if ((tpid = atoi (tent->d_name)) != ps->cur_pid) {
/*			fprintf (stderr, "pid %d has tpid %d\n", ps->cur_pid, tpid); */
			return tpid;
		}
	}
}

PidScanner *
pid_scanner_new_proc (const char *proc_path, PidScanEventFn event_fn, void *user_data)
{
	ProcPidScanner *ps;

	ps = (ProcPidScanner *) pid_scanner_alloc (sizeof (ProcPidScanner),
						   event_fn, user_data);
	ps->proc = opendir (proc_path);
	if (!ps->proc) {
		fprintf (stderr, "Failed to open " PROC_PATH ": %s\n", strerror(errno));
		return NULL;
	}
	ps->cur_ent = NULL;

	/* vtable land-fill */
#define INIT(name) ps->parent.name = proc_pid_scanner_##name
	INIT(free);
	INIT(restart);
	INIT(next);
	INIT(get_cur_pid);
	INIT(get_cur_ppid);
	INIT(get_tasks_start);
	INIT(get_tasks_next);
	INIT(get_tasks_stop);
#undef INIT

	return (PidScanner *)ps;
}

void
pid_scanner_emit_exec (PidScanner *scanner, pid_t new_pid)
{
	PidScanEvent ev = { PID_SCAN_EVENT_EXEC, 0 };

	if (!scanner->event_fn)
		return;
  
	if (was_known_pid (&scanner->map, new_pid))
		return;

	ev.pid = new_pid;
	scanner->event_fn (&ev, scanner->user_data);
}

/*
 *    After extensive testing, processes have been
 * determined to be male:
 */
void
pid_scanner_emit_paternity (PidScanner *scanner,
			    pid_t       new_pid,
			    pid_t       parent)
{
	PidScanEvent ev = { PID_SCAN_EVENT_CREATED, 0 };

	if (!scanner->event_fn)
		return;
  
	ev.pid = new_pid;
	ev.u.ppid = parent;
	scanner->event_fn (&ev, scanner->user_data);
}