]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/kill.c
Merge branch 'meson-more-build-options' of https://github.com/jwillikers/util-linux
[thirdparty/util-linux.git] / misc-utils / kill.c
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.
37 *
38 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
39 * - added Native Language Support
40 *
41 * 1999-11-13 aeb Accept signal numbers 128+s.
42 *
43 * Copyright (C) 2014 Sami Kerola <kerolasa@iki.fi>
44 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
45 */
46
47 #include <ctype.h> /* for isdigit() */
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include "c.h"
55 #include "closestream.h"
56 #include "nls.h"
57 #include "procutils.h"
58 #include "signames.h"
59 #include "strutils.h"
60 #include "ttyutils.h"
61 #include "xalloc.h"
62
63 /* partial success, otherwise we return regular EXIT_{SUCCESS,FAILURE} */
64 #define KILL_EXIT_SOMEOK 64
65
66 enum {
67 KILL_FIELD_WIDTH = 11,
68 KILL_OUTPUT_WIDTH = 72
69 };
70
71 struct kill_control {
72 char *arg;
73 pid_t pid;
74 int numsig;
75 #ifdef HAVE_SIGQUEUE
76 union sigval sigdata;
77 #endif
78 unsigned int
79 check_all:1,
80 do_kill:1,
81 do_pid:1,
82 use_sigval:1,
83 verbose:1;
84 };
85
86 static void print_signal_name(int signum)
87 {
88 const char *name = signum_to_signame(signum);
89
90 if (name) {
91 printf("%s\n", name);
92 return;
93 }
94 #ifdef SIGRTMIN
95 if (SIGRTMIN <= signum && signum <= SIGRTMAX) {
96 printf("RT%d\n", signum - SIGRTMIN);
97 return;
98 }
99 #endif
100 printf("%d\n", signum);
101 }
102
103 static void pretty_print_signal(FILE *fp, size_t term_width, size_t *lpos,
104 int signum, const char *name)
105 {
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 }
113
114 static void print_all_signals(FILE *fp, int pretty)
115 {
116 size_t n, lth, lpos = 0, width;
117 const char *signame = NULL;
118 int signum = 0;
119
120 if (!pretty) {
121 for (n = 0; get_signame_by_idx(n, &signame, NULL) == 0; n++) {
122 lth = 1 + strlen(signame);
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(signame, fp);
130 }
131 #ifdef SIGRTMIN
132 fputs(" RT<N> RTMIN+<N> RTMAX-<N>", fp);
133 #endif
134 fputc('\n', fp);
135 return;
136 }
137
138 /* pretty print */
139 width = get_terminal_width(KILL_OUTPUT_WIDTH + 1) - 1;
140 for (n = 0; get_signame_by_idx(n, &signame, &signum) == 0; n++)
141 pretty_print_signal(fp, width, &lpos, signum, signame);
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 }
148
149 static void err_nosig(char *name)
150 {
151 warnx(_("unknown signal %s; valid signals:"), name);
152 print_all_signals(stderr, 1);
153 exit(EXIT_FAILURE);
154 }
155
156 static 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)
166 return -1;
167 return numsig;
168 }
169 return signame_to_signum(arg);
170 }
171
172 static void __attribute__((__noreturn__)) usage(void)
173 {
174 FILE *out = stdout;
175 fputs(USAGE_HEADER, out);
176 fprintf(out, _(" %s [options] <pid>|<name>...\n"), program_invocation_short_name);
177
178 fputs(USAGE_SEPARATOR, out);
179 fputs(_("Forcibly terminate a process.\n"), out);
180
181 fputs(USAGE_OPTIONS, out);
182 fputs(_(" -a, --all do not restrict the name-to-pid conversion to processes\n"
183 " with the same uid as the present process\n"), out);
184 fputs(_(" -s, --signal <signal> send this <signal> instead of SIGTERM\n"), out);
185 #ifdef HAVE_SIGQUEUE
186 fputs(_(" -q, --queue <value> use sigqueue(2), not kill(2), and pass <value> as data\n"), out);
187 #endif
188 fputs(_(" -p, --pid print pids without signaling them\n"), out);
189 fputs(_(" -l, --list[=<signal>] list signal names, or convert a signal number to a name\n"), out);
190 fputs(_(" -L, --table list signal names and numbers\n"), out);
191 fputs(_(" --verbose print pids that will be signaled\n"), out);
192
193 fputs(USAGE_SEPARATOR, out);
194 printf(USAGE_HELP_OPTIONS(24));
195 printf(USAGE_MAN_TAIL("kill(1)"));
196
197 exit(EXIT_SUCCESS);
198 }
199
200 static char **parse_arguments(int argc, char **argv, struct kill_control *ctl)
201 {
202 char *arg;
203
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. */
207 for (argc--, argv++; 0 < argc; argc--, argv++) {
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 print_version(EXIT_SUCCESS);
218 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
219 usage();
220 if (!strcmp(arg, "--verbose")) {
221 ctl->verbose = 1;
222 continue;
223 }
224 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
225 ctl->check_all = 1;
226 continue;
227 }
228 if (!strcmp(arg, "-l") || !strcmp(arg, "--list")) {
229 if (argc < 2) {
230 print_all_signals(stdout, 0);
231 exit(EXIT_SUCCESS);
232 }
233 if (2 < argc)
234 errx(EXIT_FAILURE, _("too many arguments"));
235 /* argc == 2, accept "kill -l $?" */
236 arg = argv[1];
237 if ((ctl->numsig = arg_to_signum(arg, 1)) < 0)
238 errx(EXIT_FAILURE, _("unknown signal: %s"),
239 arg);
240 print_signal_name(ctl->numsig);
241 exit(EXIT_SUCCESS);
242 }
243 /* for compatibility with procps kill(1) */
244 if (!strncmp(arg, "--list=", 7) || !strncmp(arg, "-l=", 3)) {
245 char *p = strchr(arg, '=') + 1;
246 if ((ctl->numsig = arg_to_signum(p, 1)) < 0)
247 errx(EXIT_FAILURE, _("unknown signal: %s"), p);
248 print_signal_name(ctl->numsig);
249 exit(EXIT_SUCCESS);
250 }
251 if (!strcmp(arg, "-L") || !strcmp(arg, "--table")) {
252 print_all_signals(stdout, 1);
253 exit(EXIT_SUCCESS);
254 }
255 if (!strcmp(arg, "-p") || !strcmp(arg, "--pid")) {
256 ctl->do_pid = 1;
257 if (ctl->do_kill)
258 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
259 #ifdef HAVE_SIGQUEUE
260 if (ctl->use_sigval)
261 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
262 #endif
263 continue;
264 }
265 if (!strcmp(arg, "-s") || !strcmp(arg, "--signal")) {
266 if (argc < 2)
267 errx(EXIT_FAILURE, _("not enough arguments"));
268 ctl->do_kill = 1;
269 if (ctl->do_pid)
270 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
271 argc--, argv++;
272 arg = *argv;
273 if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
274 err_nosig(arg);
275 continue;
276 }
277 #ifdef HAVE_SIGQUEUE
278 if (!strcmp(arg, "-q") || !strcmp(arg, "--queue")) {
279 if (argc < 2)
280 errx(EXIT_FAILURE, _("option '%s' requires an argument"), arg);
281 if (ctl->do_pid)
282 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
283 argc--, argv++;
284 arg = *argv;
285 ctl->sigdata.sival_int = strtos32_or_err(arg, _("argument error"));
286 ctl->use_sigval = 1;
287 continue;
288 }
289 #endif
290 /* 'arg' begins with a dash but is not a known option.
291 * So it's probably something like -HUP, or -1/-n try to
292 * deal with it.
293 *
294 * -n could be either signal n or pid -n (a process group
295 * number). In case of doubt, POSIX tells us to assume a
296 * signal. But if a signal has already been parsed, then
297 * assume it is a process group, so stop parsing options. */
298 if (ctl->do_kill)
299 break;
300 arg++;
301 if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
302 errx(EXIT_FAILURE, _("invalid signal name or number: %s"), arg);
303 ctl->do_kill = 1;
304 if (ctl->do_pid)
305 errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
306 continue;
307 }
308 if (!*argv)
309 errx(EXIT_FAILURE, _("not enough arguments"));
310 return argv;
311 }
312
313
314 static int kill_verbose(const struct kill_control *ctl)
315 {
316 int rc = 0;
317
318 if (ctl->verbose)
319 printf(_("sending signal %d to pid %d\n"), ctl->numsig, ctl->pid);
320 if (ctl->do_pid) {
321 printf("%ld\n", (long) ctl->pid);
322 return 0;
323 }
324 #ifdef HAVE_SIGQUEUE
325 if (ctl->use_sigval)
326 rc = sigqueue(ctl->pid, ctl->numsig, ctl->sigdata);
327 else
328 #endif
329 rc = kill(ctl->pid, ctl->numsig);
330
331 if (rc < 0)
332 warn(_("sending signal to %s failed"), ctl->arg);
333 return rc;
334 }
335
336 int main(int argc, char **argv)
337 {
338 struct kill_control ctl = { .numsig = SIGTERM };
339 int nerrs = 0, ct = 0;
340
341 setlocale(LC_ALL, "");
342 bindtextdomain(PACKAGE, LOCALEDIR);
343 textdomain(PACKAGE);
344 close_stdout_atexit();
345
346 argv = parse_arguments(argc, argv, &ctl);
347
348 /* The rest of the arguments should be process ids and names. */
349 for ( ; (ctl.arg = *argv) != NULL; argv++) {
350 char *ep = NULL;
351
352 errno = 0;
353 ctl.pid = strtol(ctl.arg, &ep, 10);
354 if (errno == 0 && ep && *ep == '\0' && ctl.arg < ep) {
355 if (kill_verbose(&ctl) != 0)
356 nerrs++;
357 ct++;
358 } else {
359 struct proc_processes *ps = proc_open_processes();
360 int found = 0;
361
362 if (!ps)
363 continue;
364 if (!ctl.check_all)
365 proc_processes_filter_by_uid(ps, getuid());
366
367 proc_processes_filter_by_name(ps, ctl.arg);
368 while (proc_next_pid(ps, &ctl.pid) == 0) {
369 if (kill_verbose(&ctl) != 0)
370 nerrs++;
371 ct++;
372 found = 1;
373 }
374 proc_close_processes(ps);
375
376 if (!found) {
377 nerrs++, ct++;
378 warnx(_("cannot find process \"%s\""), ctl.arg);
379 }
380 }
381 }
382
383 if (ct && nerrs == 0)
384 return EXIT_SUCCESS; /* full success */
385 else if (ct == nerrs)
386 return EXIT_FAILURE; /* all failed */
387
388 return KILL_EXIT_SOMEOK; /* partial success */
389 }
390