summaryrefslogtreecommitdiff
path: root/src/chproc.c
blob: 46876a5e188d597abcf3ef26a79265632406a7ce (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * chproc.c - simple process checker
 *
 * Iterate over /proc and print PIDs of processes matching the given name.
 */

#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"

struct options {
	int list;
	int all;
	int kill;
	char *process_name;
	char *process_pid;
};


static void options_init(struct options *x)
{
	x->list = false;
	x->all	= false;
	x->kill = false;
	x->process_pid = NULL;
	x->process_name = NULL;
}


static void usage(void)
{
	fprintf(stderr,
		"Usage: %s <option> [process_name, pid]\n"
		" -l, --list <name>	list all pid for that process\n"
		" -a, --all		list all processes\n"
		" -k, --kill <PID>	kill a proces give then PID\n"
		, PROGRAM_NAME);
}



static int process_list(DIR *dir,
		const char *proc_name,
		struct options *x)
{
	char path[PATH_MAX];
	char buffer[256];
	char *endptr;
	FILE *f;
	int count;
	long pid;
	struct dirent *entry;
	size_t len;

	count = 0;

	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);

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

		snprintf(path, sizeof(path), "/proc/%ld/comm", pid);

		f = fopen(path, "r");

		if (!f) {
			fprintf(stderr, "Failed to open %s: %s\n",
				path, strerror(errno));
			continue;
		}

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

		len = strlen(buffer);

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

		/* flag instead of re-writing a function */
		if (x->list) {
			if (strcmp(buffer, proc_name) == 0) {
				printf("%s - %s\n", entry->d_name, buffer);
				++count;
			}
		} else {
			printf("%s - %s\n", entry->d_name, buffer);
			++count;
		}
		fclose(f);
	}

	closedir(dir);

	if (count == 0)
		return 1;
	else
		return 0;
}


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

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

	pid = (pid_t) atoi(process_id);

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

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


int main(int argc, char *argv[])
{
	int opt;
	DIR *dir_proc;

	struct options x;

	struct option long_options[] = {
		{"list", required_argument, 0, 'l'},
		{"all", no_argument, 0, 'a'},
		{"kill", required_argument, 0, 'k'},
		{0, 0, 0, 0}
	};

	options_init(&x);

	/* /proc/ must to exist */
	dir_proc = opendir("/proc");
	if (dir_proc == NULL) {
		fprintf(stderr, "/proc/ not found.\n");
		return EXIT_FAILURE;
	}

	if (argc < 2) {
		usage();
		return 2;
	}

	while ((opt = getopt_long(argc, argv, "l:k:a",
		long_options, NULL)) != -1) {
		switch (opt) {
		case 'l':
			x.list = true;
			x.process_name = optarg;
			break;
		case 'a':
			x.all = true;
			break;
		case 'k':
			x.kill = true;
			x.process_pid = optarg;
			break;
		default:
			fprintf(stderr,
				"Unknow option.\n");
			usage();
			exit(2);
		}
	}

	if (x.list || x.all)
		if (process_list(dir_proc, x.process_name, &x) == 1)
			return 1;

	if (x.kill)
		if (process_kill(x.process_pid) == 1)
			return 1;

	return 0;
}