]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/kill.c
include/pidfd-utils: small cleanup
[thirdparty/util-linux.git] / misc-utils / kill.c
CommitLineData
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"
6e6b9a1d 57#include "pidfd-utils.h"
f1e7f7d5 58#include "procutils.h"
6855f6e8 59#include "signames.h"
a1504d8b 60#include "strutils.h"
fda84b66
SK
61#include "ttyutils.h"
62#include "xalloc.h"
6dbe3af9 63
a01f4d43
KZ
64/* partial success, otherwise we return regular EXIT_{SUCCESS,FAILURE} */
65#define KILL_EXIT_SOMEOK 64
66
6e1bffa8
SK
67enum {
68 KILL_FIELD_WIDTH = 11,
69 KILL_OUTPUT_WIDTH = 72
70};
71
6e6b9a1d
SK
72#ifdef UL_HAVE_PIDFD
73# include <poll.h>
74# include "list.h"
75struct timeouts {
76 int period;
77 int sig;
78 struct list_head follow_ups;
79};
80#endif
81
2ab6683f
SK
82struct kill_control {
83 char *arg;
84 pid_t pid;
85 int numsig;
9e8dffd5
SK
86#ifdef HAVE_SIGQUEUE
87 union sigval sigdata;
6e6b9a1d
SK
88#endif
89#ifdef UL_HAVE_PIDFD
90 struct list_head follow_ups;
9e8dffd5 91#endif
2ab6683f
SK
92 unsigned int
93 check_all:1,
94 do_kill:1,
9e8dffd5 95 do_pid:1,
d1fd7742 96 use_sigval:1,
6e6b9a1d
SK
97#ifdef UL_HAVE_PIDFD
98 timeout:1,
99#endif
d1fd7742 100 verbose:1;
2ab6683f
SK
101};
102
6ca93313 103static void print_signal_name(int signum)
dff74deb 104{
c61e986f 105 const char *name = signum_to_signame(signum);
6dbe3af9 106
c61e986f
KZ
107 if (name) {
108 printf("%s\n", name);
109 return;
dff74deb
KZ
110 }
111#ifdef SIGRTMIN
6ca93313
KZ
112 if (SIGRTMIN <= signum && signum <= SIGRTMAX) {
113 printf("RT%d\n", signum - SIGRTMIN);
dff74deb
KZ
114 return;
115 }
116#endif
6ca93313 117 printf("%d\n", signum);
dff74deb 118}
a1504d8b 119
dff74deb
KZ
120static void pretty_print_signal(FILE *fp, size_t term_width, size_t *lpos,
121 int signum, const char *name)
6dbe3af9 122{
dff74deb
KZ
123 if (term_width < (*lpos + KILL_FIELD_WIDTH)) {
124 fputc('\n', fp);
125 *lpos = 0;
126 }
127 *lpos += KILL_FIELD_WIDTH;
128 fprintf(fp, "%2d %-8s", signum, name);
129}
d4ea8636 130
abc7d9bf 131static void print_all_signals(FILE *fp, int pretty)
dff74deb
KZ
132{
133 size_t n, lth, lpos = 0, width;
c61e986f
KZ
134 const char *signame = NULL;
135 int signum = 0;
d4ea8636 136
dff74deb 137 if (!pretty) {
c61e986f
KZ
138 for (n = 0; get_signame_by_idx(n, &signame, NULL) == 0; n++) {
139 lth = 1 + strlen(signame);
dff74deb
KZ
140 if (KILL_OUTPUT_WIDTH < lpos + lth) {
141 fputc('\n', fp);
142 lpos = 0;
143 } else if (lpos)
144 fputc(' ', fp);
145 lpos += lth;
c61e986f 146 fputs(signame, fp);
dff74deb
KZ
147 }
148#ifdef SIGRTMIN
149 fputs(" RT<N> RTMIN+<N> RTMAX-<N>", fp);
150#endif
151 fputc('\n', fp);
152 return;
153 }
e497557f 154
dff74deb 155 /* pretty print */
43b4f7ea 156 width = get_terminal_width(KILL_OUTPUT_WIDTH + 1) - 1;
c61e986f
KZ
157 for (n = 0; get_signame_by_idx(n, &signame, &signum) == 0; n++)
158 pretty_print_signal(fp, width, &lpos, signum, signame);
dff74deb
KZ
159#ifdef SIGRTMIN
160 pretty_print_signal(fp, width, &lpos, SIGRTMIN, "RTMIN");
161 pretty_print_signal(fp, width, &lpos, SIGRTMAX, "RTMAX");
162#endif
163 fputc('\n', fp);
164}
fd393d2a 165
e497557f 166static void err_nosig(char *name)
dff74deb
KZ
167{
168 warnx(_("unknown signal %s; valid signals:"), name);
abc7d9bf 169 print_all_signals(stderr, 1);
e497557f 170 exit(EXIT_FAILURE);
dff74deb 171}
fd393d2a 172
dff74deb
KZ
173static int arg_to_signum(char *arg, int maskbit)
174{
175 int numsig;
176 char *ep;
177
178 if (isdigit(*arg)) {
179 numsig = strtol(arg, &ep, 10);
180 if (NSIG <= numsig && maskbit && (numsig & 128) != 0)
181 numsig -= 128;
182 if (*ep != 0 || numsig < 0 || NSIG <= numsig)
e497557f
KZ
183 return -1;
184 return numsig;
dff74deb
KZ
185 }
186 return signame_to_signum(arg);
187}
188
86be6a32 189static void __attribute__((__noreturn__)) usage(void)
dff74deb 190{
86be6a32 191 FILE *out = stdout;
dff74deb 192 fputs(USAGE_HEADER, out);
36a3cd56 193 fprintf(out, _(" %s [options] <pid>|<name>...\n"), program_invocation_short_name);
e497557f 194
451dbcfa
BS
195 fputs(USAGE_SEPARATOR, out);
196 fputs(_("Forcibly terminate a process.\n"), out);
197
dff74deb
KZ
198 fputs(USAGE_OPTIONS, out);
199 fputs(_(" -a, --all do not restrict the name-to-pid conversion to processes\n"
36a3cd56
BS
200 " with the same uid as the present process\n"), out);
201 fputs(_(" -s, --signal <signal> send this <signal> instead of SIGTERM\n"), out);
dff74deb 202#ifdef HAVE_SIGQUEUE
36a3cd56 203 fputs(_(" -q, --queue <value> use sigqueue(2), not kill(2), and pass <value> as data\n"), out);
6e6b9a1d
SK
204#endif
205#ifdef UL_HAVE_PIDFD
206 fputs(_(" --timeout <milliseconds> <follow-up signal>\n"
207 " wait up to timeout and send follow-up signal\n"), out);
dff74deb
KZ
208#endif
209 fputs(_(" -p, --pid print pids without signaling them\n"), out);
36a3cd56 210 fputs(_(" -l, --list[=<signal>] list signal names, or convert a signal number to a name\n"), out);
dff74deb 211 fputs(_(" -L, --table list signal names and numbers\n"), out);
d1fd7742 212 fputs(_(" --verbose print pids that will be signaled\n"), out);
e497557f 213
dff74deb 214 fputs(USAGE_SEPARATOR, out);
f45f3ec3
RM
215 printf(USAGE_HELP_OPTIONS(24));
216 printf(USAGE_MAN_TAIL("kill(1)"));
e497557f 217
86be6a32 218 exit(EXIT_SUCCESS);
dff74deb
KZ
219}
220
fd393d2a
SK
221static char **parse_arguments(int argc, char **argv, struct kill_control *ctl)
222{
223 char *arg;
224
d4ea8636
SK
225 /* Loop through the arguments. Actually, -a is the only option
226 * can be used with other options. The 'kill' is basically a
227 * one-option-at-most program. */
80af5fb0 228 for (argc--, argv++; 0 < argc; argc--, argv++) {
d4ea8636
SK
229 arg = *argv;
230 if (*arg != '-')
231 break;
232 if (!strcmp(arg, "--")) {
233 argc--, argv++;
234 break;
235 }
236 if (!strcmp(arg, "-v") || !strcmp(arg, "-V") ||
2c308875
KZ
237 !strcmp(arg, "--version"))
238 print_version(EXIT_SUCCESS);
d4ea8636 239 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
86be6a32 240 usage();
d1fd7742
SK
241 if (!strcmp(arg, "--verbose")) {
242 ctl->verbose = 1;
243 continue;
244 }
d4ea8636 245 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
fd393d2a 246 ctl->check_all = 1;
d4ea8636
SK
247 continue;
248 }
249 if (!strcmp(arg, "-l") || !strcmp(arg, "--list")) {
250 if (argc < 2) {
abc7d9bf 251 print_all_signals(stdout, 0);
fd393d2a 252 exit(EXIT_SUCCESS);
d4ea8636 253 }
80af5fb0 254 if (2 < argc)
d40c87b0 255 errx(EXIT_FAILURE, _("too many arguments"));
d4ea8636
SK
256 /* argc == 2, accept "kill -l $?" */
257 arg = argv[1];
fd393d2a 258 if ((ctl->numsig = arg_to_signum(arg, 1)) < 0)
d4ea8636
SK
259 errx(EXIT_FAILURE, _("unknown signal: %s"),
260 arg);
6ca93313 261 print_signal_name(ctl->numsig);
fd393d2a 262 exit(EXIT_SUCCESS);
d4ea8636
SK
263 }
264 /* for compatibility with procps kill(1) */
265 if (!strncmp(arg, "--list=", 7) || !strncmp(arg, "-l=", 3)) {
266 char *p = strchr(arg, '=') + 1;
fd393d2a 267 if ((ctl->numsig = arg_to_signum(p, 1)) < 0)
d4ea8636 268 errx(EXIT_FAILURE, _("unknown signal: %s"), p);
6ca93313 269 print_signal_name(ctl->numsig);
fd393d2a 270 exit(EXIT_SUCCESS);
d4ea8636
SK
271 }
272 if (!strcmp(arg, "-L") || !strcmp(arg, "--table")) {
abc7d9bf 273 print_all_signals(stdout, 1);
fd393d2a 274 exit(EXIT_SUCCESS);
d4ea8636
SK
275 }
276 if (!strcmp(arg, "-p") || !strcmp(arg, "--pid")) {
fd393d2a
SK
277 ctl->do_pid = 1;
278 if (ctl->do_kill)
d40c87b0 279 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
fe6b5e32
SK
280#ifdef HAVE_SIGQUEUE
281 if (ctl->use_sigval)
282 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
283#endif
d4ea8636
SK
284 continue;
285 }
286 if (!strcmp(arg, "-s") || !strcmp(arg, "--signal")) {
287 if (argc < 2)
d40c87b0 288 errx(EXIT_FAILURE, _("not enough arguments"));
fd393d2a
SK
289 ctl->do_kill = 1;
290 if (ctl->do_pid)
d40c87b0 291 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
d4ea8636
SK
292 argc--, argv++;
293 arg = *argv;
e497557f
KZ
294 if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
295 err_nosig(arg);
d4ea8636
SK
296 continue;
297 }
9e8dffd5 298#ifdef HAVE_SIGQUEUE
d4ea8636
SK
299 if (!strcmp(arg, "-q") || !strcmp(arg, "--queue")) {
300 if (argc < 2)
d40c87b0 301 errx(EXIT_FAILURE, _("option '%s' requires an argument"), arg);
fe6b5e32
SK
302 if (ctl->do_pid)
303 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
d4ea8636
SK
304 argc--, argv++;
305 arg = *argv;
a3443c30 306 ctl->sigdata.sival_int = strtos32_or_err(arg, _("argument error"));
9e8dffd5 307 ctl->use_sigval = 1;
d4ea8636
SK
308 continue;
309 }
6e6b9a1d
SK
310#endif
311#ifdef UL_HAVE_PIDFD
312 if (!strcmp(arg, "--timeout")) {
313 struct timeouts *next;
314
315 ctl->timeout = 1;
316 if (argc < 2)
317 errx(EXIT_FAILURE, _("option '%s' requires an argument"), arg);
318 argc--, argv++;
319 arg = *argv;
320 next = xcalloc(1, sizeof(*next));
321 next->period = strtos32_or_err(arg, _("argument error"));
322 argc--, argv++;
323 arg = *argv;
324 if ((next->sig = arg_to_signum(arg, 0)) < 0)
325 err_nosig(arg);
326 list_add_tail(&next->follow_ups, &ctl->follow_ups);
327 continue;
328 }
9e8dffd5 329#endif
d4ea8636
SK
330 /* 'arg' begins with a dash but is not a known option.
331 * So it's probably something like -HUP, or -1/-n try to
332 * deal with it.
333 *
36a3cd56
BS
334 * -n could be either signal n or pid -n (a process group
335 * number). In case of doubt, POSIX tells us to assume a
336 * signal. But if a signal has already been parsed, then
337 * assume it is a process group, so stop parsing options. */
fd393d2a 338 if (ctl->do_kill)
d4ea8636
SK
339 break;
340 arg++;
fd393d2a 341 if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
36a3cd56 342 errx(EXIT_FAILURE, _("invalid signal name or number: %s"), arg);
fd393d2a
SK
343 ctl->do_kill = 1;
344 if (ctl->do_pid)
d40c87b0 345 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
d4ea8636 346 continue;
6dbe3af9 347 }
d4ea8636 348 if (!*argv)
d40c87b0 349 errx(EXIT_FAILURE, _("not enough arguments"));
fd393d2a 350 return argv;
6dbe3af9
KZ
351}
352
6e6b9a1d
SK
353#ifdef UL_HAVE_PIDFD
354static int kill_with_timeout(const struct kill_control *ctl)
355{
356 int pfd, n;
357 struct pollfd p = { 0 };
358 siginfo_t info = { 0 };
359 struct list_head *entry;
360
361 info.si_code = SI_QUEUE;
362 info.si_signo = ctl->numsig;
363 info.si_uid = getuid();
364 info.si_pid = getpid();
365 info.si_value.sival_int =
366 ctl->use_sigval != 0 ? ctl->use_sigval : ctl->numsig;
367
368 if ((pfd = pidfd_open(ctl->pid, 0)) < 0)
369 err(EXIT_FAILURE, _("pidfd_open() failed: %d"), ctl->pid);
370 p.fd = pfd;
371 p.events = POLLIN;
372
373 if (pidfd_send_signal(pfd, ctl->numsig, &info, 0) < 0)
374 err(EXIT_FAILURE, _("pidfd_send_signal() failed"));
375 list_for_each(entry, &ctl->follow_ups) {
376 struct timeouts *timeout;
377
378 timeout = list_entry(entry, struct timeouts, follow_ups);
379 n = poll(&p, 1, timeout->period);
380 if (n < 0)
381 err(EXIT_FAILURE, _("poll() failed"));
382 if (n == 0) {
383 info.si_signo = timeout->sig;
384 if (ctl->verbose)
385 printf(_("timeout, sending signal %d to pid %d\n"),
386 timeout->sig, ctl->pid);
387 if (pidfd_send_signal(pfd, timeout->sig, &info, 0) < 0)
388 err(EXIT_FAILURE, _("pidfd_send_signal() failed"));
389 }
390 }
391 return 0;
392}
393#endif
6dbe3af9 394
2ab6683f 395static int kill_verbose(const struct kill_control *ctl)
6dbe3af9 396{
d4ea8636 397 int rc = 0;
a1504d8b 398
d1fd7742
SK
399 if (ctl->verbose)
400 printf(_("sending signal %d to pid %d\n"), ctl->numsig, ctl->pid);
3665b07c 401 if (ctl->do_pid) {
2ab6683f 402 printf("%ld\n", (long) ctl->pid);
d4ea8636
SK
403 return 0;
404 }
6e6b9a1d
SK
405#ifdef UL_HAVE_PIDFD
406 if (ctl->timeout) {
407 rc = kill_with_timeout(ctl);
408 } else
409#endif
a1504d8b 410#ifdef HAVE_SIGQUEUE
9e8dffd5
SK
411 if (ctl->use_sigval)
412 rc = sigqueue(ctl->pid, ctl->numsig, ctl->sigdata);
d4ea8636 413 else
a1504d8b 414#endif
2ab6683f 415 rc = kill(ctl->pid, ctl->numsig);
a01f4d43
KZ
416
417 if (rc < 0)
2ab6683f 418 warn(_("sending signal to %s failed"), ctl->arg);
a01f4d43 419 return rc;
6dbe3af9 420}
dff74deb
KZ
421
422int main(int argc, char **argv)
423{
a01f4d43
KZ
424 struct kill_control ctl = { .numsig = SIGTERM };
425 int nerrs = 0, ct = 0;
dff74deb
KZ
426
427 setlocale(LC_ALL, "");
428 bindtextdomain(PACKAGE, LOCALEDIR);
429 textdomain(PACKAGE);
2c308875 430 close_stdout_atexit();
dff74deb 431
6e6b9a1d 432 INIT_LIST_HEAD(&ctl.follow_ups);
dff74deb 433 argv = parse_arguments(argc, argv, &ctl);
dff74deb 434
a01f4d43
KZ
435 /* The rest of the arguments should be process ids and names. */
436 for ( ; (ctl.arg = *argv) != NULL; argv++) {
437 char *ep = NULL;
438
439 errno = 0;
dff74deb 440 ctl.pid = strtol(ctl.arg, &ep, 10);
a01f4d43
KZ
441 if (errno == 0 && ep && *ep == '\0' && ctl.arg < ep) {
442 if (kill_verbose(&ctl) != 0)
443 nerrs++;
444 ct++;
445 } else {
dff74deb 446 struct proc_processes *ps = proc_open_processes();
a01f4d43 447 int found = 0;
dff74deb
KZ
448
449 if (!ps)
450 continue;
e0d46757 451 if (!ctl.check_all)
dff74deb 452 proc_processes_filter_by_uid(ps, getuid());
a01f4d43 453
dff74deb 454 proc_processes_filter_by_name(ps, ctl.arg);
a01f4d43
KZ
455 while (proc_next_pid(ps, &ctl.pid) == 0) {
456 if (kill_verbose(&ctl) != 0)
457 nerrs++;
dff74deb 458 ct++;
a01f4d43 459 found = 1;
dff74deb
KZ
460 }
461 proc_close_processes(ps);
a01f4d43
KZ
462
463 if (!found) {
464 nerrs++, ct++;
09af3db4 465 warnx(_("cannot find process \"%s\""), ctl.arg);
a01f4d43 466 }
dff74deb
KZ
467 }
468 }
a01f4d43
KZ
469
470 if (ct && nerrs == 0)
471 return EXIT_SUCCESS; /* full success */
472 else if (ct == nerrs)
473 return EXIT_FAILURE; /* all failed */
474
475 return KILL_EXIT_SOMEOK; /* partial success */
dff74deb
KZ
476}
477