]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * Copyright (c) 1988, 1993, 1994 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | /* | |
34 | * oct 5 1994 -- almost entirely re-written to allow for process names. | |
35 | * modifications (c) salvatore valente <svalente@mit.edu> | |
36 | * may be used / modified / distributed under the same terms as the original. | |
7eda085c | 37 | * |
b50945d4 | 38 | * 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
39 | * - added Native Language Support |
40 | * | |
9e930041 | 41 | * 1999-11-13 aeb Accept signal numbers 128+s. |
eb63b9b8 | 42 | * |
6853d664 KZ |
43 | * Copyright (C) 2014 Sami Kerola <kerolasa@iki.fi> |
44 | * Copyright (C) 2014 Karel Zak <kzak@redhat.com> | |
6dbe3af9 KZ |
45 | */ |
46 | ||
d4ea8636 SK |
47 | #include <ctype.h> /* for isdigit() */ |
48 | #include <signal.h> | |
6dbe3af9 KZ |
49 | #include <stdio.h> |
50 | #include <stdlib.h> | |
51 | #include <string.h> | |
6dbe3af9 | 52 | #include <unistd.h> |
bf969be7 KZ |
53 | |
54 | #include "c.h" | |
c05a80ca | 55 | #include "closestream.h" |
d4ea8636 | 56 | #include "nls.h" |
f1e7f7d5 | 57 | #include "procutils.h" |
6855f6e8 | 58 | #include "signames.h" |
a1504d8b | 59 | #include "strutils.h" |
fda84b66 SK |
60 | #include "ttyutils.h" |
61 | #include "xalloc.h" | |
6dbe3af9 | 62 | |
a01f4d43 KZ |
63 | /* partial success, otherwise we return regular EXIT_{SUCCESS,FAILURE} */ |
64 | #define KILL_EXIT_SOMEOK 64 | |
65 | ||
6e1bffa8 SK |
66 | enum { |
67 | KILL_FIELD_WIDTH = 11, | |
68 | KILL_OUTPUT_WIDTH = 72 | |
69 | }; | |
70 | ||
2ab6683f SK |
71 | struct kill_control { |
72 | char *arg; | |
73 | pid_t pid; | |
74 | int numsig; | |
9e8dffd5 SK |
75 | #ifdef HAVE_SIGQUEUE |
76 | union sigval sigdata; | |
77 | #endif | |
2ab6683f SK |
78 | unsigned int |
79 | check_all:1, | |
80 | do_kill:1, | |
9e8dffd5 | 81 | do_pid:1, |
d1fd7742 SK |
82 | use_sigval:1, |
83 | verbose:1; | |
2ab6683f SK |
84 | }; |
85 | ||
6ca93313 | 86 | static void print_signal_name(int signum) |
dff74deb KZ |
87 | { |
88 | size_t n; | |
6dbe3af9 | 89 | |
dff74deb | 90 | for (n = 0; n < ARRAY_SIZE(sys_signame); n++) { |
6ca93313 | 91 | if (sys_signame[n].val == signum) { |
dff74deb KZ |
92 | printf("%s\n", sys_signame[n].name); |
93 | return; | |
94 | } | |
95 | } | |
96 | #ifdef SIGRTMIN | |
6ca93313 KZ |
97 | if (SIGRTMIN <= signum && signum <= SIGRTMAX) { |
98 | printf("RT%d\n", signum - SIGRTMIN); | |
dff74deb KZ |
99 | return; |
100 | } | |
101 | #endif | |
6ca93313 | 102 | printf("%d\n", signum); |
dff74deb | 103 | } |
a1504d8b | 104 | |
dff74deb KZ |
105 | static void pretty_print_signal(FILE *fp, size_t term_width, size_t *lpos, |
106 | int signum, const char *name) | |
6dbe3af9 | 107 | { |
dff74deb KZ |
108 | if (term_width < (*lpos + KILL_FIELD_WIDTH)) { |
109 | fputc('\n', fp); | |
110 | *lpos = 0; | |
111 | } | |
112 | *lpos += KILL_FIELD_WIDTH; | |
113 | fprintf(fp, "%2d %-8s", signum, name); | |
114 | } | |
d4ea8636 | 115 | |
abc7d9bf | 116 | static void print_all_signals(FILE *fp, int pretty) |
dff74deb KZ |
117 | { |
118 | size_t n, lth, lpos = 0, width; | |
d4ea8636 | 119 | |
dff74deb KZ |
120 | if (!pretty) { |
121 | for (n = 0; n < ARRAY_SIZE(sys_signame); n++) { | |
122 | lth = 1 + strlen(sys_signame[n].name); | |
123 | if (KILL_OUTPUT_WIDTH < lpos + lth) { | |
124 | fputc('\n', fp); | |
125 | lpos = 0; | |
126 | } else if (lpos) | |
127 | fputc(' ', fp); | |
128 | lpos += lth; | |
129 | fputs(sys_signame[n].name, fp); | |
130 | } | |
131 | #ifdef SIGRTMIN | |
132 | fputs(" RT<N> RTMIN+<N> RTMAX-<N>", fp); | |
133 | #endif | |
134 | fputc('\n', fp); | |
135 | return; | |
136 | } | |
e497557f | 137 | |
dff74deb | 138 | /* pretty print */ |
43b4f7ea | 139 | width = get_terminal_width(KILL_OUTPUT_WIDTH + 1) - 1; |
dff74deb KZ |
140 | for (n = 0; n < ARRAY_SIZE(sys_signame); n++) |
141 | pretty_print_signal(fp, width, &lpos, | |
142 | sys_signame[n].val, sys_signame[n].name); | |
143 | #ifdef SIGRTMIN | |
144 | pretty_print_signal(fp, width, &lpos, SIGRTMIN, "RTMIN"); | |
145 | pretty_print_signal(fp, width, &lpos, SIGRTMAX, "RTMAX"); | |
146 | #endif | |
147 | fputc('\n', fp); | |
148 | } | |
fd393d2a | 149 | |
e497557f | 150 | static void err_nosig(char *name) |
dff74deb KZ |
151 | { |
152 | warnx(_("unknown signal %s; valid signals:"), name); | |
abc7d9bf | 153 | print_all_signals(stderr, 1); |
e497557f | 154 | exit(EXIT_FAILURE); |
dff74deb | 155 | } |
fd393d2a | 156 | |
dff74deb KZ |
157 | static int arg_to_signum(char *arg, int maskbit) |
158 | { | |
159 | int numsig; | |
160 | char *ep; | |
161 | ||
162 | if (isdigit(*arg)) { | |
163 | numsig = strtol(arg, &ep, 10); | |
164 | if (NSIG <= numsig && maskbit && (numsig & 128) != 0) | |
165 | numsig -= 128; | |
166 | if (*ep != 0 || numsig < 0 || NSIG <= numsig) | |
e497557f KZ |
167 | return -1; |
168 | return numsig; | |
dff74deb KZ |
169 | } |
170 | return signame_to_signum(arg); | |
171 | } | |
172 | ||
86be6a32 | 173 | static void __attribute__((__noreturn__)) usage(void) |
dff74deb | 174 | { |
86be6a32 | 175 | FILE *out = stdout; |
dff74deb | 176 | fputs(USAGE_HEADER, out); |
36a3cd56 | 177 | fprintf(out, _(" %s [options] <pid>|<name>...\n"), program_invocation_short_name); |
e497557f | 178 | |
451dbcfa BS |
179 | fputs(USAGE_SEPARATOR, out); |
180 | fputs(_("Forcibly terminate a process.\n"), out); | |
181 | ||
dff74deb KZ |
182 | fputs(USAGE_OPTIONS, out); |
183 | fputs(_(" -a, --all do not restrict the name-to-pid conversion to processes\n" | |
36a3cd56 BS |
184 | " with the same uid as the present process\n"), out); |
185 | fputs(_(" -s, --signal <signal> send this <signal> instead of SIGTERM\n"), out); | |
dff74deb | 186 | #ifdef HAVE_SIGQUEUE |
36a3cd56 | 187 | fputs(_(" -q, --queue <value> use sigqueue(2), not kill(2), and pass <value> as data\n"), out); |
dff74deb KZ |
188 | #endif |
189 | fputs(_(" -p, --pid print pids without signaling them\n"), out); | |
36a3cd56 | 190 | fputs(_(" -l, --list[=<signal>] list signal names, or convert a signal number to a name\n"), out); |
dff74deb | 191 | fputs(_(" -L, --table list signal names and numbers\n"), out); |
d1fd7742 | 192 | fputs(_(" --verbose print pids that will be signaled\n"), out); |
e497557f | 193 | |
dff74deb | 194 | fputs(USAGE_SEPARATOR, out); |
f45f3ec3 RM |
195 | printf(USAGE_HELP_OPTIONS(24)); |
196 | printf(USAGE_MAN_TAIL("kill(1)")); | |
e497557f | 197 | |
86be6a32 | 198 | exit(EXIT_SUCCESS); |
dff74deb KZ |
199 | } |
200 | ||
fd393d2a SK |
201 | static char **parse_arguments(int argc, char **argv, struct kill_control *ctl) |
202 | { | |
203 | char *arg; | |
204 | ||
d4ea8636 SK |
205 | /* Loop through the arguments. Actually, -a is the only option |
206 | * can be used with other options. The 'kill' is basically a | |
207 | * one-option-at-most program. */ | |
80af5fb0 | 208 | for (argc--, argv++; 0 < argc; argc--, argv++) { |
d4ea8636 SK |
209 | arg = *argv; |
210 | if (*arg != '-') | |
211 | break; | |
212 | if (!strcmp(arg, "--")) { | |
213 | argc--, argv++; | |
214 | break; | |
215 | } | |
216 | if (!strcmp(arg, "-v") || !strcmp(arg, "-V") || | |
217 | !strcmp(arg, "--version")) { | |
218 | printf(UTIL_LINUX_VERSION); | |
fd393d2a | 219 | exit(EXIT_SUCCESS); |
d4ea8636 SK |
220 | } |
221 | if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) | |
86be6a32 | 222 | usage(); |
d1fd7742 SK |
223 | if (!strcmp(arg, "--verbose")) { |
224 | ctl->verbose = 1; | |
225 | continue; | |
226 | } | |
d4ea8636 | 227 | if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) { |
fd393d2a | 228 | ctl->check_all = 1; |
d4ea8636 SK |
229 | continue; |
230 | } | |
231 | if (!strcmp(arg, "-l") || !strcmp(arg, "--list")) { | |
232 | if (argc < 2) { | |
abc7d9bf | 233 | print_all_signals(stdout, 0); |
fd393d2a | 234 | exit(EXIT_SUCCESS); |
d4ea8636 | 235 | } |
80af5fb0 | 236 | if (2 < argc) |
d40c87b0 | 237 | errx(EXIT_FAILURE, _("too many arguments")); |
d4ea8636 SK |
238 | /* argc == 2, accept "kill -l $?" */ |
239 | arg = argv[1]; | |
fd393d2a | 240 | if ((ctl->numsig = arg_to_signum(arg, 1)) < 0) |
d4ea8636 SK |
241 | errx(EXIT_FAILURE, _("unknown signal: %s"), |
242 | arg); | |
6ca93313 | 243 | print_signal_name(ctl->numsig); |
fd393d2a | 244 | exit(EXIT_SUCCESS); |
d4ea8636 SK |
245 | } |
246 | /* for compatibility with procps kill(1) */ | |
247 | if (!strncmp(arg, "--list=", 7) || !strncmp(arg, "-l=", 3)) { | |
248 | char *p = strchr(arg, '=') + 1; | |
fd393d2a | 249 | if ((ctl->numsig = arg_to_signum(p, 1)) < 0) |
d4ea8636 | 250 | errx(EXIT_FAILURE, _("unknown signal: %s"), p); |
6ca93313 | 251 | print_signal_name(ctl->numsig); |
fd393d2a | 252 | exit(EXIT_SUCCESS); |
d4ea8636 SK |
253 | } |
254 | if (!strcmp(arg, "-L") || !strcmp(arg, "--table")) { | |
abc7d9bf | 255 | print_all_signals(stdout, 1); |
fd393d2a | 256 | exit(EXIT_SUCCESS); |
d4ea8636 SK |
257 | } |
258 | if (!strcmp(arg, "-p") || !strcmp(arg, "--pid")) { | |
fd393d2a SK |
259 | ctl->do_pid = 1; |
260 | if (ctl->do_kill) | |
d40c87b0 | 261 | errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal"); |
fe6b5e32 SK |
262 | #ifdef HAVE_SIGQUEUE |
263 | if (ctl->use_sigval) | |
264 | errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue"); | |
265 | #endif | |
d4ea8636 SK |
266 | continue; |
267 | } | |
268 | if (!strcmp(arg, "-s") || !strcmp(arg, "--signal")) { | |
269 | if (argc < 2) | |
d40c87b0 | 270 | errx(EXIT_FAILURE, _("not enough arguments")); |
fd393d2a SK |
271 | ctl->do_kill = 1; |
272 | if (ctl->do_pid) | |
d40c87b0 | 273 | errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal"); |
d4ea8636 SK |
274 | argc--, argv++; |
275 | arg = *argv; | |
e497557f KZ |
276 | if ((ctl->numsig = arg_to_signum(arg, 0)) < 0) |
277 | err_nosig(arg); | |
d4ea8636 SK |
278 | continue; |
279 | } | |
9e8dffd5 | 280 | #ifdef HAVE_SIGQUEUE |
d4ea8636 SK |
281 | if (!strcmp(arg, "-q") || !strcmp(arg, "--queue")) { |
282 | if (argc < 2) | |
d40c87b0 | 283 | errx(EXIT_FAILURE, _("option '%s' requires an argument"), arg); |
fe6b5e32 SK |
284 | if (ctl->do_pid) |
285 | errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue"); | |
d4ea8636 SK |
286 | argc--, argv++; |
287 | arg = *argv; | |
a3443c30 | 288 | ctl->sigdata.sival_int = strtos32_or_err(arg, _("argument error")); |
9e8dffd5 | 289 | ctl->use_sigval = 1; |
d4ea8636 SK |
290 | continue; |
291 | } | |
9e8dffd5 | 292 | #endif |
d4ea8636 SK |
293 | /* 'arg' begins with a dash but is not a known option. |
294 | * So it's probably something like -HUP, or -1/-n try to | |
295 | * deal with it. | |
296 | * | |
36a3cd56 BS |
297 | * -n could be either signal n or pid -n (a process group |
298 | * number). In case of doubt, POSIX tells us to assume a | |
299 | * signal. But if a signal has already been parsed, then | |
300 | * assume it is a process group, so stop parsing options. */ | |
fd393d2a | 301 | if (ctl->do_kill) |
d4ea8636 SK |
302 | break; |
303 | arg++; | |
fd393d2a | 304 | if ((ctl->numsig = arg_to_signum(arg, 0)) < 0) |
36a3cd56 | 305 | errx(EXIT_FAILURE, _("invalid signal name or number: %s"), arg); |
fd393d2a SK |
306 | ctl->do_kill = 1; |
307 | if (ctl->do_pid) | |
d40c87b0 | 308 | errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal"); |
d4ea8636 | 309 | continue; |
6dbe3af9 | 310 | } |
d4ea8636 | 311 | if (!*argv) |
d40c87b0 | 312 | errx(EXIT_FAILURE, _("not enough arguments")); |
fd393d2a | 313 | return argv; |
6dbe3af9 KZ |
314 | } |
315 | ||
6dbe3af9 | 316 | |
2ab6683f | 317 | static int kill_verbose(const struct kill_control *ctl) |
6dbe3af9 | 318 | { |
d4ea8636 | 319 | int rc = 0; |
a1504d8b | 320 | |
d1fd7742 SK |
321 | if (ctl->verbose) |
322 | printf(_("sending signal %d to pid %d\n"), ctl->numsig, ctl->pid); | |
3665b07c | 323 | if (ctl->do_pid) { |
2ab6683f | 324 | printf("%ld\n", (long) ctl->pid); |
d4ea8636 SK |
325 | return 0; |
326 | } | |
a1504d8b | 327 | #ifdef HAVE_SIGQUEUE |
9e8dffd5 SK |
328 | if (ctl->use_sigval) |
329 | rc = sigqueue(ctl->pid, ctl->numsig, ctl->sigdata); | |
d4ea8636 | 330 | else |
a1504d8b | 331 | #endif |
2ab6683f | 332 | rc = kill(ctl->pid, ctl->numsig); |
a01f4d43 KZ |
333 | |
334 | if (rc < 0) | |
2ab6683f | 335 | warn(_("sending signal to %s failed"), ctl->arg); |
a01f4d43 | 336 | return rc; |
6dbe3af9 | 337 | } |
dff74deb KZ |
338 | |
339 | int main(int argc, char **argv) | |
340 | { | |
a01f4d43 KZ |
341 | struct kill_control ctl = { .numsig = SIGTERM }; |
342 | int nerrs = 0, ct = 0; | |
dff74deb KZ |
343 | |
344 | setlocale(LC_ALL, ""); | |
345 | bindtextdomain(PACKAGE, LOCALEDIR); | |
346 | textdomain(PACKAGE); | |
347 | atexit(close_stdout); | |
dff74deb | 348 | |
dff74deb | 349 | argv = parse_arguments(argc, argv, &ctl); |
dff74deb | 350 | |
a01f4d43 KZ |
351 | /* The rest of the arguments should be process ids and names. */ |
352 | for ( ; (ctl.arg = *argv) != NULL; argv++) { | |
353 | char *ep = NULL; | |
354 | ||
355 | errno = 0; | |
dff74deb | 356 | ctl.pid = strtol(ctl.arg, &ep, 10); |
a01f4d43 KZ |
357 | if (errno == 0 && ep && *ep == '\0' && ctl.arg < ep) { |
358 | if (kill_verbose(&ctl) != 0) | |
359 | nerrs++; | |
360 | ct++; | |
361 | } else { | |
dff74deb | 362 | struct proc_processes *ps = proc_open_processes(); |
a01f4d43 | 363 | int found = 0; |
dff74deb KZ |
364 | |
365 | if (!ps) | |
366 | continue; | |
e0d46757 | 367 | if (!ctl.check_all) |
dff74deb | 368 | proc_processes_filter_by_uid(ps, getuid()); |
a01f4d43 | 369 | |
dff74deb | 370 | proc_processes_filter_by_name(ps, ctl.arg); |
a01f4d43 KZ |
371 | while (proc_next_pid(ps, &ctl.pid) == 0) { |
372 | if (kill_verbose(&ctl) != 0) | |
373 | nerrs++; | |
dff74deb | 374 | ct++; |
a01f4d43 | 375 | found = 1; |
dff74deb KZ |
376 | } |
377 | proc_close_processes(ps); | |
a01f4d43 KZ |
378 | |
379 | if (!found) { | |
380 | nerrs++, ct++; | |
09af3db4 | 381 | warnx(_("cannot find process \"%s\""), ctl.arg); |
a01f4d43 | 382 | } |
dff74deb KZ |
383 | } |
384 | } | |
a01f4d43 KZ |
385 | |
386 | if (ct && nerrs == 0) | |
387 | return EXIT_SUCCESS; /* full success */ | |
388 | else if (ct == nerrs) | |
389 | return EXIT_FAILURE; /* all failed */ | |
390 | ||
391 | return KILL_EXIT_SOMEOK; /* partial success */ | |
dff74deb KZ |
392 | } |
393 |