aboutsummaryrefslogtreecommitdiff
path: root/toys/killall.c
blob: fb3e8cb32ad41d2c3453fb2435eef09a56c98014 (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
/* vi: set sw=4 ts=4:
 *
 * killall.c - Send a signal (default: TERM) to all processes with the given names.
 *
 * Copyright 2012 Andreas Heck <aheck@gmx.de>
 *
 * Not in SUSv4.
 * See http://opengroup.org/onlinepubs/9699919799/utilities/

USE_KILLALL(NEWTOY(killall, "?lq", TOYFLAG_USR|TOYFLAG_BIN))

config KILLALL
	bool "killall"
	default y
	help
	  usage: killall [-l] [-q] [-SIG] PROCESS_NAME...

	  Send a signal (default: TERM) to all processes with the given names.

		-l	print list of all available signals
		-q	don't print any warnings or error messages
*/

#include "toys.h"

#define FLAG_q	1
#define FLAG_l	2

DEFINE_GLOBALS(
		int matched;
		int signum;
)
#define TT this.killall

struct signame {
	int num;
	const char *name;
};

static struct signame signames[] = {
#ifdef SIGHUP
	{SIGHUP, "HUP"},
#endif
#ifdef SIGINT
	{SIGINT, "INT"},
#endif
#ifdef SIGQUIT
	{SIGQUIT, "QUIT"},
#endif
#ifdef SIGILL
	{SIGILL, "ILL"},
#endif
#ifdef SIGTRAP
	{SIGTRAP, "TRAP"},
#endif
#ifdef SIGTABRT
	{SIGABRT, "ABRT"},
#endif
#ifdef SIGTABRT
	{SIGIOT, "IOT"},
#endif
#ifdef SIGBUS
	{SIGBUS, "BUS"},
#endif
#ifdef SIGFPE
	{SIGFPE, "FPE"},
#endif
#ifdef SIGKILL
	{SIGKILL, "KILL"},
#endif
#ifdef SIGUSR1
	{SIGUSR1, "USR1"},
#endif
#ifdef SIGSEGV
	{SIGSEGV, "SEGV"},
#endif
#ifdef SIGUSR2
	{SIGUSR2, "USR2"},
#endif
#ifdef SIGPIPE
	{SIGPIPE, "PIPE"},
#endif
#ifdef SIGALRM
	{SIGALRM, "ALRM"},
#endif
#ifdef SIGTERM
	{SIGTERM, "TERM"},
#endif
#ifdef SIGSTKFLT
	{SIGSTKFLT, "STKFLT"},
#endif
#ifdef SIGCHLD
	{SIGCHLD, "CHLD"},
#endif
#ifdef SIGCONT
	{SIGCONT, "CONT"},
#endif
#ifdef SIGSTOP
	{SIGSTOP, "STOP"},
#endif
#ifdef SIGSTOP
	{SIGSTOP, "STOP"},
#endif
#ifdef SIGTSTP
	{SIGTSTP, "TSTP"},
#endif
#ifdef SIGTTIN
	{SIGTTIN, "TTIN"},
#endif
#ifdef SIGTTOU
	{SIGTTOU, "TTOU"},
#endif
#ifdef SIGURG
	{SIGURG, "URG"},
#endif
#ifdef SIGXCPU
	{SIGXCPU, "XCPU"},
#endif
#ifdef SIGXFSZ
	{SIGXFSZ, "XFSZ"},
#endif
#ifdef SIGVTALRM
	{SIGVTALRM, "VTALRM"},
#endif
#ifdef SIGVTALRM
	{SIGVTALRM, "VTALRM"},
#endif
#ifdef SIGPROF
	{SIGPROF, "PROF"},
#endif
#ifdef SIGWINCH
	{SIGWINCH, "WINCH"},
#endif
#ifdef SIGIO
	{SIGIO, "IO"},
#endif
#ifdef SIGPOLL
	{SIGPOLL, "POLL"},
#endif
#ifdef SIGPWR
	{SIGPWR, "PWR"},
#endif
#ifdef SIGSYS
	{SIGSYS, "SYS"},
#endif
#ifdef SIGUNUSED
	{SIGUNUSED, "UNUSED"},
#endif
	{0, NULL}
};

static int sig_to_num(const char *pidstr) {
	int i, num;

	if (isdigit(pidstr[0])) {
		num = atoi(pidstr);

		return num;
	}

	for (i = 0; signames[i].num; i++) {
		if (strcmp(pidstr, signames[i].name) == 0) {
			return signames[i].num;
		}
	}

	return -1;
}

static void print_signals() {
	int i;

	for (i = 0; signames[i].num; i++) {
		puts(signames[i].name);
	}
}

static void kill_process(const char *pidstr) {
	int ret;
	pid_t pid = atoi(pidstr);

	TT.matched = 1;
	ret = kill(pid, TT.signum);

	if (ret == -1) {
		if (toys.optflags & FLAG_q) perror("kill");
	}
}

void killall_main(void)
{
	char **names;

	TT.matched = 0;
	TT.signum = SIGTERM;

	if (toys.optflags & FLAG_l) {
		print_signals();
		exit(0);
	}

	if (!*toys.optargs) {
		toys.exithelp = 1;
		error_exit("Process name missing!");
	}

	names = toys.optargs;

	if ((*toys.optargs)[0] == '-') {
		TT.signum = sig_to_num(&(*toys.optargs)[1]);
		if (TT.signum <= 0) {
			if (toys.optflags & FLAG_q) fprintf(stderr, "Invalid signal\n");
			exit(1);
		}
		names = ++toys.optargs;
	}

	if (!*names) {
		toys.exithelp = 1;
		error_exit("Process name missing!");
	}

	for_each_pid_with_name_in(names, kill_process);

	if (!TT.matched) {
		if (!(toys.optflags & FLAG_q)) fprintf(stderr, "No such process\n");
		exit(1);
	}
}