]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/kill.c
lsblk: add basic function to build devices tree
[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"
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
66enum {
67 KILL_FIELD_WIDTH = 11,
68 KILL_OUTPUT_WIDTH = 72
69};
70
2ab6683f
SK
71struct 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 86static void print_signal_name(int signum)
dff74deb 87{
c61e986f 88 const char *name = signum_to_signame(signum);
6dbe3af9 89
c61e986f
KZ
90 if (name) {
91 printf("%s\n", name);
92 return;
dff74deb
KZ
93 }
94#ifdef SIGRTMIN
6ca93313
KZ
95 if (SIGRTMIN <= signum && signum <= SIGRTMAX) {
96 printf("RT%d\n", signum - SIGRTMIN);
dff74deb
KZ
97 return;
98 }
99#endif
6ca93313 100 printf("%d\n", signum);
dff74deb 101}
a1504d8b 102
dff74deb
KZ
103static void pretty_print_signal(FILE *fp, size_t term_width, size_t *lpos,
104 int signum, const char *name)
6dbe3af9 105{
dff74deb
KZ
106 if (term_width < (*lpos + KILL_FIELD_WIDTH)) {
107 fputc('\n', fp);
108 *lpos = 0;
109 }
110 *lpos += KILL_FIELD_WIDTH;
111 fprintf(fp, "%2d %-8s", signum, name);
112}
d4ea8636 113
abc7d9bf 114static void print_all_signals(FILE *fp, int pretty)
dff74deb
KZ
115{
116 size_t n, lth, lpos = 0, width;
c61e986f
KZ
117 const char *signame = NULL;
118 int signum = 0;
d4ea8636 119
dff74deb 120 if (!pretty) {
c61e986f
KZ
121 for (n = 0; get_signame_by_idx(n, &signame, NULL) == 0; n++) {
122 lth = 1 + strlen(signame);
dff74deb
KZ
123 if (KILL_OUTPUT_WIDTH < lpos + lth) {
124 fputc('\n', fp);
125 lpos = 0;
126 } else if (lpos)
127 fputc(' ', fp);
128 lpos += lth;
c61e986f 129 fputs(signame, fp);
dff74deb
KZ
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;
c61e986f
KZ
140 for (n = 0; get_signame_by_idx(n, &signame, &signum) == 0; n++)
141 pretty_print_signal(fp, width, &lpos, signum, signame);
dff74deb
KZ
142#ifdef SIGRTMIN
143 pretty_print_signal(fp, width, &lpos, SIGRTMIN, "RTMIN");
144 pretty_print_signal(fp, width, &lpos, SIGRTMAX, "RTMAX");
145#endif
146 fputc('\n', fp);
147}
fd393d2a 148
e497557f 149static void err_nosig(char *name)
dff74deb
KZ
150{
151 warnx(_("unknown signal %s; valid signals:"), name);
abc7d9bf 152 print_all_signals(stderr, 1);
e497557f 153 exit(EXIT_FAILURE);
dff74deb 154}
fd393d2a 155
dff74deb
KZ
156static int arg_to_signum(char *arg, int maskbit)
157{
158 int numsig;
159 char *ep;
160
161 if (isdigit(*arg)) {
162 numsig = strtol(arg, &ep, 10);
163 if (NSIG <= numsig && maskbit && (numsig & 128) != 0)
164 numsig -= 128;
165 if (*ep != 0 || numsig < 0 || NSIG <= numsig)
e497557f
KZ
166 return -1;
167 return numsig;
dff74deb
KZ
168 }
169 return signame_to_signum(arg);
170}
171
86be6a32 172static void __attribute__((__noreturn__)) usage(void)
dff74deb 173{
86be6a32 174 FILE *out = stdout;
dff74deb 175 fputs(USAGE_HEADER, out);
36a3cd56 176 fprintf(out, _(" %s [options] <pid>|<name>...\n"), program_invocation_short_name);
e497557f 177
451dbcfa
BS
178 fputs(USAGE_SEPARATOR, out);
179 fputs(_("Forcibly terminate a process.\n"), out);
180
dff74deb
KZ
181 fputs(USAGE_OPTIONS, out);
182 fputs(_(" -a, --all do not restrict the name-to-pid conversion to processes\n"
36a3cd56
BS
183 " with the same uid as the present process\n"), out);
184 fputs(_(" -s, --signal <signal> send this <signal> instead of SIGTERM\n"), out);
dff74deb 185#ifdef HAVE_SIGQUEUE
36a3cd56 186 fputs(_(" -q, --queue <value> use sigqueue(2), not kill(2), and pass <value> as data\n"), out);
dff74deb
KZ
187#endif
188 fputs(_(" -p, --pid print pids without signaling them\n"), out);
36a3cd56 189 fputs(_(" -l, --list[=<signal>] list signal names, or convert a signal number to a name\n"), out);
dff74deb 190 fputs(_(" -L, --table list signal names and numbers\n"), out);
d1fd7742 191 fputs(_(" --verbose print pids that will be signaled\n"), out);
e497557f 192
dff74deb 193 fputs(USAGE_SEPARATOR, out);
f45f3ec3
RM
194 printf(USAGE_HELP_OPTIONS(24));
195 printf(USAGE_MAN_TAIL("kill(1)"));
e497557f 196
86be6a32 197 exit(EXIT_SUCCESS);
dff74deb
KZ
198}
199
fd393d2a
SK
200static char **parse_arguments(int argc, char **argv, struct kill_control *ctl)
201{
202 char *arg;
203
d4ea8636
SK
204 /* Loop through the arguments. Actually, -a is the only option
205 * can be used with other options. The 'kill' is basically a
206 * one-option-at-most program. */
80af5fb0 207 for (argc--, argv++; 0 < argc; argc--, argv++) {
d4ea8636
SK
208 arg = *argv;
209 if (*arg != '-')
210 break;
211 if (!strcmp(arg, "--")) {
212 argc--, argv++;
213 break;
214 }
215 if (!strcmp(arg, "-v") || !strcmp(arg, "-V") ||
216 !strcmp(arg, "--version")) {
217 printf(UTIL_LINUX_VERSION);
fd393d2a 218 exit(EXIT_SUCCESS);
d4ea8636
SK
219 }
220 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
86be6a32 221 usage();
d1fd7742
SK
222 if (!strcmp(arg, "--verbose")) {
223 ctl->verbose = 1;
224 continue;
225 }
d4ea8636 226 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
fd393d2a 227 ctl->check_all = 1;
d4ea8636
SK
228 continue;
229 }
230 if (!strcmp(arg, "-l") || !strcmp(arg, "--list")) {
231 if (argc < 2) {
abc7d9bf 232 print_all_signals(stdout, 0);
fd393d2a 233 exit(EXIT_SUCCESS);
d4ea8636 234 }
80af5fb0 235 if (2 < argc)
d40c87b0 236 errx(EXIT_FAILURE, _("too many arguments"));
d4ea8636
SK
237 /* argc == 2, accept "kill -l $?" */
238 arg = argv[1];
fd393d2a 239 if ((ctl->numsig = arg_to_signum(arg, 1)) < 0)
d4ea8636
SK
240 errx(EXIT_FAILURE, _("unknown signal: %s"),
241 arg);
6ca93313 242 print_signal_name(ctl->numsig);
fd393d2a 243 exit(EXIT_SUCCESS);
d4ea8636
SK
244 }
245 /* for compatibility with procps kill(1) */
246 if (!strncmp(arg, "--list=", 7) || !strncmp(arg, "-l=", 3)) {
247 char *p = strchr(arg, '=') + 1;
fd393d2a 248 if ((ctl->numsig = arg_to_signum(p, 1)) < 0)
d4ea8636 249 errx(EXIT_FAILURE, _("unknown signal: %s"), p);
6ca93313 250 print_signal_name(ctl->numsig);
fd393d2a 251 exit(EXIT_SUCCESS);
d4ea8636
SK
252 }
253 if (!strcmp(arg, "-L") || !strcmp(arg, "--table")) {
abc7d9bf 254 print_all_signals(stdout, 1);
fd393d2a 255 exit(EXIT_SUCCESS);
d4ea8636
SK
256 }
257 if (!strcmp(arg, "-p") || !strcmp(arg, "--pid")) {
fd393d2a
SK
258 ctl->do_pid = 1;
259 if (ctl->do_kill)
d40c87b0 260 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
fe6b5e32
SK
261#ifdef HAVE_SIGQUEUE
262 if (ctl->use_sigval)
263 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
264#endif
d4ea8636
SK
265 continue;
266 }
267 if (!strcmp(arg, "-s") || !strcmp(arg, "--signal")) {
268 if (argc < 2)
d40c87b0 269 errx(EXIT_FAILURE, _("not enough arguments"));
fd393d2a
SK
270 ctl->do_kill = 1;
271 if (ctl->do_pid)
d40c87b0 272 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
d4ea8636
SK
273 argc--, argv++;
274 arg = *argv;
e497557f
KZ
275 if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
276 err_nosig(arg);
d4ea8636
SK
277 continue;
278 }
9e8dffd5 279#ifdef HAVE_SIGQUEUE
d4ea8636
SK
280 if (!strcmp(arg, "-q") || !strcmp(arg, "--queue")) {
281 if (argc < 2)
d40c87b0 282 errx(EXIT_FAILURE, _("option '%s' requires an argument"), arg);
fe6b5e32
SK
283 if (ctl->do_pid)
284 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
d4ea8636
SK
285 argc--, argv++;
286 arg = *argv;
a3443c30 287 ctl->sigdata.sival_int = strtos32_or_err(arg, _("argument error"));
9e8dffd5 288 ctl->use_sigval = 1;
d4ea8636
SK
289 continue;
290 }
9e8dffd5 291#endif
d4ea8636
SK
292 /* 'arg' begins with a dash but is not a known option.
293 * So it's probably something like -HUP, or -1/-n try to
294 * deal with it.
295 *
36a3cd56
BS
296 * -n could be either signal n or pid -n (a process group
297 * number). In case of doubt, POSIX tells us to assume a
298 * signal. But if a signal has already been parsed, then
299 * assume it is a process group, so stop parsing options. */
fd393d2a 300 if (ctl->do_kill)
d4ea8636
SK
301 break;
302 arg++;
fd393d2a 303 if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
36a3cd56 304 errx(EXIT_FAILURE, _("invalid signal name or number: %s"), arg);
fd393d2a
SK
305 ctl->do_kill = 1;
306 if (ctl->do_pid)
d40c87b0 307 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
d4ea8636 308 continue;
6dbe3af9 309 }
d4ea8636 310 if (!*argv)
d40c87b0 311 errx(EXIT_FAILURE, _("not enough arguments"));
fd393d2a 312 return argv;
6dbe3af9
KZ
313}
314
6dbe3af9 315
2ab6683f 316static int kill_verbose(const struct kill_control *ctl)
6dbe3af9 317{
d4ea8636 318 int rc = 0;
a1504d8b 319
d1fd7742
SK
320 if (ctl->verbose)
321 printf(_("sending signal %d to pid %d\n"), ctl->numsig, ctl->pid);
3665b07c 322 if (ctl->do_pid) {
2ab6683f 323 printf("%ld\n", (long) ctl->pid);
d4ea8636
SK
324 return 0;
325 }
a1504d8b 326#ifdef HAVE_SIGQUEUE
9e8dffd5
SK
327 if (ctl->use_sigval)
328 rc = sigqueue(ctl->pid, ctl->numsig, ctl->sigdata);
d4ea8636 329 else
a1504d8b 330#endif
2ab6683f 331 rc = kill(ctl->pid, ctl->numsig);
a01f4d43
KZ
332
333 if (rc < 0)
2ab6683f 334 warn(_("sending signal to %s failed"), ctl->arg);
a01f4d43 335 return rc;
6dbe3af9 336}
dff74deb
KZ
337
338int main(int argc, char **argv)
339{
a01f4d43
KZ
340 struct kill_control ctl = { .numsig = SIGTERM };
341 int nerrs = 0, ct = 0;
dff74deb
KZ
342
343 setlocale(LC_ALL, "");
344 bindtextdomain(PACKAGE, LOCALEDIR);
345 textdomain(PACKAGE);
346 atexit(close_stdout);
dff74deb 347
dff74deb 348 argv = parse_arguments(argc, argv, &ctl);
dff74deb 349
a01f4d43
KZ
350 /* The rest of the arguments should be process ids and names. */
351 for ( ; (ctl.arg = *argv) != NULL; argv++) {
352 char *ep = NULL;
353
354 errno = 0;
dff74deb 355 ctl.pid = strtol(ctl.arg, &ep, 10);
a01f4d43
KZ
356 if (errno == 0 && ep && *ep == '\0' && ctl.arg < ep) {
357 if (kill_verbose(&ctl) != 0)
358 nerrs++;
359 ct++;
360 } else {
dff74deb 361 struct proc_processes *ps = proc_open_processes();
a01f4d43 362 int found = 0;
dff74deb
KZ
363
364 if (!ps)
365 continue;
e0d46757 366 if (!ctl.check_all)
dff74deb 367 proc_processes_filter_by_uid(ps, getuid());
a01f4d43 368
dff74deb 369 proc_processes_filter_by_name(ps, ctl.arg);
a01f4d43
KZ
370 while (proc_next_pid(ps, &ctl.pid) == 0) {
371 if (kill_verbose(&ctl) != 0)
372 nerrs++;
dff74deb 373 ct++;
a01f4d43 374 found = 1;
dff74deb
KZ
375 }
376 proc_close_processes(ps);
a01f4d43
KZ
377
378 if (!found) {
379 nerrs++, ct++;
09af3db4 380 warnx(_("cannot find process \"%s\""), ctl.arg);
a01f4d43 381 }
dff74deb
KZ
382 }
383 }
a01f4d43
KZ
384
385 if (ct && nerrs == 0)
386 return EXIT_SUCCESS; /* full success */
387 else if (ct == nerrs)
388 return EXIT_FAILURE; /* all failed */
389
390 return KILL_EXIT_SOMEOK; /* partial success */
dff74deb
KZ
391}
392