aboutsummaryrefslogtreecommitdiff
path: root/src/pid_proc.c
blob: 097c2ed1a389c799920ae318e66a11e1e57fb56e (plain)
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
332
333
334
335
336
337
338
339
340
341
/* This file is part of genrc
Copyryght (C) 2018 Sergey Poznyakoff
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
*/
#include "genrc.h"
#include <glob.h>
#include <sys/stat.h>


char *
catfile(char const *dir, char const *file)
{
	char *ret;
	size_t dlen, flen;

	dlen = strlen(dir);
	if (dlen > 0 && dir[dlen-1] == '/')
		dlen--;
	flen = strlen(file);
	ret = xmalloc(dlen + flen + 2);
	memcpy(ret, dir, dlen);
	ret[dlen] = '/';
	strcpy(ret + dlen + 1, file);
	return ret;
}

#define INITIAL_READLINK_SIZE 128

static int
areadlink(const char *name, char **pbuf)
{
	size_t status = 0;
	char *buf;
	size_t size;
	ssize_t linklen;

	size = INITIAL_READLINK_SIZE;
	buf = malloc(size);
	if (!buf)
		return -1;
	while (1) {
		char *p;
		size_t newsize = size << 1;
		if (newsize < size) {
			status = ENAMETOOLONG;
			break;
		}
		size = newsize;
		p = realloc(buf, size);
		if (!p)
			free(buf);
		buf = p;
		if (!buf) {
			status = ENOMEM;
			break;
		}
      
		linklen = readlink(name, buf, size);
		if (linklen < 0 && errno != ERANGE) {
			status = errno;
			break;
		}

		if ((size_t) linklen < size) {
			buf[linklen++] = '\0';
			status = 0;
			break;
		}
	}

	if (status) {
		free(buf);
		errno = status;
		return -1;
	}
	*pbuf = buf;
	return 0;
}

pid_t
strtopid(char const *str)
{
	unsigned long n;
	char *end;
	errno = 0;
	n = strtoul(str, &end, 10);
	if (errno || *end)
		return -1;
	return (pid_t) n;
}

struct proc_pid_closure {
	struct genrc_pid_closure generic;
	PROCSCANBUF buf;
};

pid_t
parent_pid(pid_t p)
{
	FILE *fp;
	char *filename = NULL;
	size_t s = 0;
	char *line = NULL;
	ssize_t n;
	struct wordsplit ws;
	pid_t pid;
	
	grecs_asprintf(&filename, &s, "/proc/%lu/stat", (unsigned long)p);
	fp = fopen(filename, "r");
	if (!fp) {
		system_error(errno, "can't open file %s", filename);
		free(filename);
		return -1;
	}
	s = 0;
	n = grecs_getline(&line, &s, fp);
	fclose(fp);
	if (n == -1) {
		system_error(errno, "error reading from %s", filename);
		free(filename);
		return -1;
	}
	free(filename);
	if (n == 0) 
		return 0;

	ws.ws_delim = " ";
	ws.ws_error = genrc_error;
	if (wordsplit(line, &ws, 
		      WRDSF_NOCMD|WRDSF_NOVAR|WRDSF_QUOTE|
		      WRDSF_DELIM|WRDSF_ENOMEMABRT|WRDSF_SHOWERR|WRDSF_ERROR))
		exit(1);
	pid = strtopid(ws.ws_wordv[3]);
	wordsplit_free(&ws);
	return pid;
}

static void
parent_pids_filter(PIDLIST *plist)
{
	int more;
	size_t i;
	
	do {
		more = 0;
		for (i = 0; i < plist->pidc; ) {
			pid_t ppid = parent_pid(plist->pidv[i]);
			if (ppid > 1 && pidlist_member(plist, ppid)) {
				pidlist_remove(plist, i);
				more = 1;
			} else
				i++;
		}
	} while (more);
}

static int
match_exe(struct proc_pid_closure *clos, char const *dir)
{
	char *fname = catfile(dir, "exe");
	char *exename;
	struct stat st;
	int rc = 0;
	
	if (lstat(fname, &st)) {
		if (errno != ENOENT)
			system_error(errno, "stat %s", fname);
		rc = -1;
	} else {
		if (!S_ISLNK(st.st_mode)) {
			rc = -1;
		} else if (areadlink(fname, &exename)) {
			if (errno != ENOENT)
				system_error(errno, "readlink %s", fname);
			rc = -1;
		}
	}
	free(fname);
	if (rc == 0) {
		rc = procscan_match(clos->buf, exename);
		free(exename);
	}
	return rc;
}
	
static int
match_cmdline(struct proc_pid_closure *clos, char const *dir)
{
	char *fname = catfile(dir, "cmdline");
	FILE *fp;
	int c;
	int nul = 0;
	grecs_txtacc_t acc;
	int rc;
	char *cmdline;
	
	fp = fopen(fname, "r");
	if (!fp) {
		system_error(errno, "fopen %s", fname);
		free(fname);
		return -1;
	}
	acc = grecs_txtacc_create();
	
	while ((c = fgetc(fp)) != EOF) {
		if (nul) {
			grecs_txtacc_grow_char(acc, ' ');
			nul = 0;
		}
		if (c == 0)
			nul = 1;
		else
			grecs_txtacc_grow_char(acc, c);
	}
	fclose(fp);
	free(fname);

	grecs_txtacc_grow_char(acc, 0);
	cmdline = grecs_txtacc_finish(acc, 0);

	rc = procscan_match(clos->buf, cmdline);
	grecs_txtacc_free(acc);
	return rc;
}

static int
match_argv0(struct proc_pid_closure *clos, char const *dir)
{
	char *fname = catfile(dir, "cmdline");
	FILE *fp;
	int c;
	grecs_txtacc_t acc;
	int rc;
	char *argv0;
	
	fp = fopen(fname, "r");
	if (!fp) {
		system_error(errno, "fopen %s", fname);
		free(fname);
		return -1;
	}
	acc = grecs_txtacc_create();
	
	while ((c = fgetc(fp)) != EOF) {
		grecs_txtacc_grow_char(acc, c);
		if (c == 0)
			break;
	}
	fclose(fp);
	free(fname);

	argv0 = grecs_txtacc_finish(acc, 0);
	
	rc = procscan_match(clos->buf, argv0);
	grecs_txtacc_free(acc);
	return rc;
}

static void
scandir(struct proc_pid_closure *clos, char const *dir, PIDLIST *plist)
{
	int rc;
	struct stat st;

	if (stat(dir, &st)) {
		if (errno != ENOENT)
			system_error(errno, "stat %s", dir);
		return;
	}
	if (!S_ISDIR(st.st_mode))
		return;
	
	if (clos->buf->flags & PROCF_CMDLINE) {
		rc = match_cmdline(clos, dir);
	} else if (clos->buf->flags & PROCF_EXE) {
		rc = match_exe(clos, dir);
	} else {
		rc = match_argv0(clos, dir);
	}
	
	if (rc == 0) {
		pidlist_add(plist, strtopid(dir + sizeof("/proc")));
	}
}					  
	

static int
globerr(const char *epath, int ec)
{
	system_error(ec, "%s", epath);
	return 0;
}

static int
pid_proc_get(GENRC_PID_CLOSURE *clos, PIDLIST *plist)
{
	struct proc_pid_closure *proc_clos = (struct proc_pid_closure *)clos;
	glob_t gl;
	char const *dirpat = "/proc/[0-9]*";
	int i;

	switch (glob(dirpat, GLOB_NOSORT|GLOB_ONLYDIR, globerr, &gl)) {
	case GLOB_NOSPACE:
		grecs_alloc_die();
	case GLOB_ABORTED:
		genrc_error("read error during glob scan");
		exit(1);
	case GLOB_NOMATCH:
		return 0;
	}

	for (i = 0; i < gl.gl_pathc; i++) {
		scandir(proc_clos, gl.gl_pathv[i], plist);
	}
	globfree(&gl);

	if (plist->pidc > 1 && !(proc_clos->buf->flags & PROCF_ALL))
		parent_pids_filter(plist);
	
	return 0;
}

GENRC_PID_CLOSURE *
genrc_pid_proc_init(int argc, char **argv)
{
	struct proc_pid_closure *clos;

	if (argc > 3)
		usage_error("expected format: PROC[:[<EXE>][:<FLAGS>]]");

	clos = xzalloc(sizeof *clos);
	
	clos->buf = procscan_init((argc >= 2 && argv[1][0] != 0)
				  ? argv[1] : NULL,
				  argc > 2 ? argv[2] : NULL);
	clos->generic.pid = pid_proc_get;
	
	return (GENRC_PID_CLOSURE *)clos;
}

Return to:

Send suggestions and report system problems to the System administrator.