summaryrefslogtreecommitdiff
path: root/src/chproc.c
blob: b4ebee9ee0d2c2d2f64f702c7c47843736e18171 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// SPDX-License-Identifier: GPL-2.0-only
/*
 * chproc.c - simple process manager
 *
 * Features: list and kill.
 */

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>

#define PROGRAM_NAME "chproc"
#define PROGRAM_VERSION "0.1"

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

#define DIR_PROC "/proc"


struct flag_options {
	int name;
	int all;
	int kill;
	int help;
	int version;
};


struct data_options {
	char *process_name;
	char *process_pid;
};


struct options {
	struct flag_options flags;
	struct data_options data;
};


struct flag_handler {
	int *flag;
	int id;
};


enum flag_index {
	F_NAME,
	F_ALL,
	F_KILL,
	F_HELP,
	F_VERSION
};


static int usage(void)
{
	fprintf(stderr,
		"Usage: %s <option> [process_name, pid]\n"
		" -n, --name <name>	list all pid for that process\n"
		" -a, --all		list all processes\n"
		" -k, --kill <PID>	kill a process give then PID\n"
		" -h, --help		show this help\n"
		" -v, --version		show version\n"
		, PROGRAM_NAME);

	return 2;
}


struct process_info {
	pid_t pid;
	char name[256];
};


struct process_info *get_process_list(size_t *count)
{
	DIR *dir;
	struct dirent *entry;
	struct process_info *list;
	size_t capacity;
	size_t n;
	long pid;
	char path[PATH_MAX];
	FILE *f;
	char buffer[256];
	char *endptr;
	size_t len;
	struct process_info *tmp;

	dir = opendir(DIR_PROC);

	capacity = 128;
	n = 0;
	list = malloc(capacity * (sizeof(*list)));

	if (!list) {
		closedir(dir);
		return NULL;
	}


	while ((entry = readdir(dir)) != NULL) {
		/* skip "." and ".." */
		if (strcmp(entry->d_name, ".") == 0)
			continue;
		if (strcmp(entry->d_name, "..") == 0)
			continue;

		pid = strtol(entry->d_name, &endptr, 10);

		/* not a pure number */
		if (*endptr != '\0')
			continue;

		snprintf(path, sizeof(path), "/proc/%ld/comm", pid);
		f = fopen(path, "r");

		if (!f)
			continue;

		if (!(fgets(buffer, sizeof(buffer), f)))
			continue;

		len = strlen(buffer);

		if (len > 0 && buffer[len-1] == '\n')
			buffer[len-1] = '\0';

		list[n].pid = (pid_t)pid;
		strncpy(list[n].name, buffer, sizeof(list[n].name));
		list[n].name[sizeof(list[n].name) - 1] = '\0';
		n++;

		/* Resize array if needed */
		if (n >= capacity) {
			capacity *= 2;
			tmp = realloc(list, capacity * sizeof(*list));

			if (!tmp) {
				free(list);
				fclose(f);
				closedir(dir);
				return NULL;
			}
			list = tmp;
		}


		fclose(f);
	}

	closedir(dir);

	*count = n;
	return list;
}


static int process_get_all(struct process_info *list,
			size_t count)
{
	size_t i;

	for (i = 0; i < count; i++) {
		printf("%d - %s\n",
			list[i].pid, list[i].name);
	}

	return 0;
}


static int process_get_name(char *pname,
				struct options *x,
				struct process_info *list,
				size_t count)
{
	char *proc_name;
	size_t i;

	proc_name = pname;


	for (i = 0; i < count; i++) {
		if (x->flags.name && strcmp(list[i].name, proc_name) == 0) {
			printf("%d - %s\n", list[i].pid, list[i].name);
			++count;
		}
	}
	return 0;
}


static int process_get_kill(const char *process_id)
{
	pid_t pid;

	pid = (pid_t) atoi(process_id);

	if (pid < 1) {
		fprintf(stderr, "'%s': not a pid or valid job\n", process_id);
		usage();
		return 1;
	}


	if (kill(pid, SIGTERM) == -1) {
		perror("kill failed");
		return 1;
	}

	printf("Process '%d' killed.\n", pid);
	return 0;
}


int proc_check(void)
{
	DIR *dir;

	/* /proc/ must exist */
	dir = opendir(DIR_PROC);

	if (dir == NULL) {
		fprintf(stderr, "/proc/ not found.\n");
		return 1;
	}

	return 0;
}


int parse_args(int argc, char *argv[],
		struct options *x)
{
	int opt;

	memset(x, 0, sizeof(*x));

	struct option long_options[] = {
		{"name",	required_argument,	0, 'n'},
		{"all",		no_argument,		0, 'a'},
		{"kill",	required_argument,	0, 'k'},
		{"help",	no_argument,		0, 'h'},
		{"version",	no_argument,		0, 'v'},
		{0, 0, 0, 0}
	};


	if (argc < 2)
		return 2;

	while ((opt = getopt_long(argc,
				argv, "n:ak:",
				long_options, NULL)) != -1) {
		switch (opt) {
		case 'n':
			x->flags.name = true;
			x->data.process_name = optarg;
		break;
		case 'a':
			x->flags.all = true;
		break;
		case 'k':
			x->flags.kill = true;
			x->data.process_pid = optarg;
		break;
		case 'h':
			x->flags.help = true;
		break;
		case 'v':
			x->flags.version = true;
		break;
		default:
			return 2;
		}
	}

	return 0;
}


int main(int argc, char *argv[])
{
	struct options x = {0};
	int ret;
	size_t count;
	struct process_info *procs;
	struct flag_handler handlers[] = {
		{ &x.flags.name, F_NAME },
		{ &x.flags.all, F_ALL },
		{ &x.flags.kill, F_KILL },
		{ &x.flags.help, F_HELP },
		{ &x.flags.version, F_VERSION },
	};
	size_t i;
	size_t n_flags;

	ret = parse_args(argc, argv, &x);

	if (proc_check() != 0)
		return 1;

	if (ret != 0) {
		usage();
		return ret;
	}

	procs = get_process_list(&count);

	if (!procs) {
		fprintf(stderr, "Failed to read process list\n");
		return 1;
	}


	n_flags = ARRAY_SIZE(handlers);

	for (i = 0; i < n_flags; i++) {
		if (*(handlers[i].flag)) {
			switch (handlers[i].id) {
			case F_NAME:
				if (process_get_name(x.data.process_name,
						&x, procs, count) == 1)
					return 1;
			break;
			case F_ALL:
				if (process_get_all(procs, count) == 1)
					return 1;
			break;
			case F_KILL:
				if (process_get_kill(x.data.process_pid) == 1)
					return 1;
			break;
			case F_HELP:
				usage();
				return 2;
			case F_VERSION:
				printf("%s: %s\n", PROGRAM_NAME,
						PROGRAM_VERSION);
				return 0;
			}
		}
	}


	free(procs);

	return 0;
}