blob: b7a2985c88147ebbecbe1b009c5422dd107d3ca9 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Erik Andersen5661fe02000-04-05 01:00:52 +00003 * Mini kill/killall implementation for busybox
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenc4996011999-10-20 22:08:37 +00009 */
10
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000011#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000012
Rob Landleydfba7412006-03-06 20:47:33 +000013int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000014{
Rob Landleyc9c1a412006-07-12 19:17:55 +000015 int killall, signo = SIGTERM, errors = 0, quiet=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +000016
Rob Landleyc9c1a412006-07-12 19:17:55 +000017 killall = (ENABLE_KILLALL && bb_applet_name[4]=='a') ? 1 : 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000018
Erik Andersene49d5ec2000-02-08 19:58:47 +000019 /* Parse any options */
Eric Andersen7d72e792003-07-26 07:41:56 +000020 if (argc < 2)
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000022
Eric Andersen7d72e792003-07-26 07:41:56 +000023 if(argv[1][0] != '-'){
24 argv++;
25 argc--;
26 goto do_it_now;
27 }
28
29 /* The -l option, which prints out signal names. */
30 if(argv[1][1]=='l' && argv[1][2]=='\0'){
31 if(argc==2) {
32 /* Print the whole signal list */
33 int col = 0;
Rob Landleyc9c1a412006-07-12 19:17:55 +000034
35 for(signo = 0;;) {
36 char *name = get_signame(++signo);
37 if (isdigit(*name)) break;
38
Eric Andersen7d72e792003-07-26 07:41:56 +000039 if (col > 60) {
40 printf("\n");
41 col = 0;
42 }
Rob Landleyc9c1a412006-07-12 19:17:55 +000043 col += printf("%2d) %-16s", signo, name);
Eric Andersen80cd3cf2002-07-23 23:45:11 +000044 }
Eric Andersen7d72e792003-07-26 07:41:56 +000045 printf("\n");
Eric Andersen7d72e792003-07-26 07:41:56 +000046 } else {
47 for(argv++; *argv; argv++) {
Rob Landleyc9c1a412006-07-12 19:17:55 +000048 char *name;
49
50 if (isdigit(**argv)) name = get_signame(atoi(*argv));
51 else {
52 int temp = get_signum(*argv);
53 if (temp<0)
54 bb_error_msg_and_die("unknown signal %s", *argv);
55 name = get_signame(temp);
56 }
57 puts(name);
Eric Andersen7d72e792003-07-26 07:41:56 +000058 }
59 }
60 /* If they specified -l, were all done */
61 return EXIT_SUCCESS;
62 }
63
64 /* The -q quiet option */
Rob Landleyc9c1a412006-07-12 19:17:55 +000065 if(killall && argv[1][1]=='q' && argv[1][2]=='\0'){
Eric Andersen7d72e792003-07-26 07:41:56 +000066 quiet++;
67 argv++;
68 argc--;
Eric Andersen8f4ef152003-08-06 07:43:58 +000069 if(argc<2 || argv[1][0] != '-'){
Eric Andersen7d72e792003-07-26 07:41:56 +000070 goto do_it_now;
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 }
Eric Andersencc8ed391999-10-05 16:24:54 +000072 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000073
Rob Landleyc9c1a412006-07-12 19:17:55 +000074 if(0>(signo = get_signum(argv[1]+1)))
Eric Andersen7d72e792003-07-26 07:41:56 +000075 bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
76 argv+=2;
77 argc-=2;
78
Eric Andersen80cd3cf2002-07-23 23:45:11 +000079do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +000080
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +000081 /* Pid or name required */
82 if (argc <= 0)
83 bb_show_usage();
84
Rob Landleyc9c1a412006-07-12 19:17:55 +000085 if (!killall) {
Erik Andersen246cc6d2000-03-07 07:41:42 +000086 /* Looks like they want to do a kill. Do that */
87 while (--argc >= 0) {
88 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +000089
Mike Frysingerb9240552006-01-04 00:13:22 +000090 if (!isdigit(**argv) && **argv != '-')
Eric Andersen7d72e792003-07-26 07:41:56 +000091 bb_error_msg_and_die( "Bad PID '%s'", *argv);
Erik Andersen246cc6d2000-03-07 07:41:42 +000092 pid = strtol(*argv, NULL, 0);
Eric Andersen7d72e792003-07-26 07:41:56 +000093 if (kill(pid, signo) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_perror_msg( "Could not kill pid '%d'", pid);
Eric Andersenc38678d2002-09-16 06:22:25 +000095 errors++;
Eric Andersen80cd3cf2002-07-23 23:45:11 +000096 }
Erik Andersen246cc6d2000-03-07 07:41:42 +000097 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 }
Eric Andersenc38678d2002-09-16 06:22:25 +000099
Rob Landleyc9c1a412006-07-12 19:17:55 +0000100 } else {
Erik Andersen0a64de92000-04-18 20:00:03 +0000101 pid_t myPid=getpid();
Rob Landleyc9c1a412006-07-12 19:17:55 +0000102
Erik Andersen246cc6d2000-03-07 07:41:42 +0000103 /* Looks like they want to do a killall. Do that */
104 while (--argc >= 0) {
Eric Andersenb24d6562001-12-06 14:52:32 +0000105 long* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000106
Eric Andersenc38678d2002-09-16 06:22:25 +0000107 pidList = find_pid_by_name(*argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000108 if (!pidList || *pidList<=0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000109 errors++;
110 if (quiet==0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 bb_error_msg( "%s: no process killed", *argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000112 } else {
113 long *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000114
Eric Andersen7d72e792003-07-26 07:41:56 +0000115 for(pl = pidList; *pl !=0 ; pl++) {
116 if (*pl==myPid)
117 continue;
118 if (kill(*pl, signo) != 0) {
119 errors++;
120 if (quiet==0)
121 bb_perror_msg( "Could not kill pid '%ld'", *pl);
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000122 }
Eric Andersen7d72e792003-07-26 07:41:56 +0000123 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000124 }
Eric Andersen44608e92002-10-22 12:21:15 +0000125 free(pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000126 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000128 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000129
Eric Andersenc38678d2002-09-16 06:22:25 +0000130 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000131}