blob: ff620eef43d4d864d532a6a18dc1c10c739fc159 [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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <stdio.h>
26#include <stdlib.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000027#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <unistd.h>
29#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000030#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000031#include <string.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000032#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000033#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Eric Andersen8f4ef152003-08-06 07:43:58 +000035#define KILL 0
36#define KILLALL 1
Eric Andersencc8ed391999-10-05 16:24:54 +000037
Erik Andersene49d5ec2000-02-08 19:58:47 +000038extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Eric Andersena4fccce2004-08-19 18:26:26 +000040 int whichApp, signo = SIGTERM;
Eric Andersen34506362001-08-02 05:02:46 +000041 const char *name;
Eric Andersenc38678d2002-09-16 06:22:25 +000042 int errors = 0;
Erik Andersen246cc6d2000-03-07 07:41:42 +000043
Eric Andersenbdfd0d72001-10-24 05:00:29 +000044#ifdef CONFIG_KILLALL
Eric Andersena4fccce2004-08-19 18:26:26 +000045 int quiet=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +000046 /* Figure out what we are trying to do here */
Eric Andersen8f4ef152003-08-06 07:43:58 +000047 whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000048#else
49 whichApp = KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000050#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 /* Parse any options */
Eric Andersen7d72e792003-07-26 07:41:56 +000053 if (argc < 2)
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000055
Eric Andersen7d72e792003-07-26 07:41:56 +000056 if(argv[1][0] != '-'){
57 argv++;
58 argc--;
59 goto do_it_now;
60 }
61
62 /* The -l option, which prints out signal names. */
63 if(argv[1][1]=='l' && argv[1][2]=='\0'){
64 if(argc==2) {
65 /* Print the whole signal list */
66 int col = 0;
67 for(signo=1; signo < NSIG; signo++) {
68 name = u_signal_names(0, &signo, 1);
69 if(name==NULL) /* unnamed */
70 continue;
71 col += printf("%2d) %-16s", signo, name);
72 if (col > 60) {
73 printf("\n");
74 col = 0;
75 }
Eric Andersen80cd3cf2002-07-23 23:45:11 +000076 }
Eric Andersen7d72e792003-07-26 07:41:56 +000077 printf("\n");
78
79 } else {
80 for(argv++; *argv; argv++) {
81 name = u_signal_names(*argv, &signo, -1);
82 if(name!=NULL)
83 printf("%s\n", name);
84 }
85 }
86 /* If they specified -l, were all done */
87 return EXIT_SUCCESS;
88 }
89
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000090#ifdef CONFIG_KILLALL
Eric Andersen7d72e792003-07-26 07:41:56 +000091 /* The -q quiet option */
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +000092 if(whichApp != KILL && argv[1][1]=='q' && argv[1][2]=='\0'){
Eric Andersen7d72e792003-07-26 07:41:56 +000093 quiet++;
94 argv++;
95 argc--;
Eric Andersen8f4ef152003-08-06 07:43:58 +000096 if(argc<2 || argv[1][0] != '-'){
Eric Andersen7d72e792003-07-26 07:41:56 +000097 goto do_it_now;
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 }
Eric Andersencc8ed391999-10-05 16:24:54 +000099 }
Eric Andersena4fccce2004-08-19 18:26:26 +0000100#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000101
Eric Andersen7d72e792003-07-26 07:41:56 +0000102 if(!u_signal_names(argv[1]+1, &signo, 0))
103 bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
104 argv+=2;
105 argc-=2;
106
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000107do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000108
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +0000109 /* Pid or name required */
110 if (argc <= 0)
111 bb_show_usage();
112
Erik Andersen246cc6d2000-03-07 07:41:42 +0000113 if (whichApp == KILL) {
114 /* Looks like they want to do a kill. Do that */
115 while (--argc >= 0) {
116 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117
Mike Frysingerb9240552006-01-04 00:13:22 +0000118 if (!isdigit(**argv) && **argv != '-')
Eric Andersen7d72e792003-07-26 07:41:56 +0000119 bb_error_msg_and_die( "Bad PID '%s'", *argv);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000120 pid = strtol(*argv, NULL, 0);
Eric Andersen7d72e792003-07-26 07:41:56 +0000121 if (kill(pid, signo) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 bb_perror_msg( "Could not kill pid '%d'", pid);
Eric Andersenc38678d2002-09-16 06:22:25 +0000123 errors++;
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000124 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000125 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000127
Eric Andersen8f4ef152003-08-06 07:43:58 +0000128 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000129#ifdef CONFIG_KILLALL
Erik Andersen6273f652000-03-17 01:12:41 +0000130 else {
Erik Andersen0a64de92000-04-18 20:00:03 +0000131 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000132 /* Looks like they want to do a killall. Do that */
133 while (--argc >= 0) {
Eric Andersenb24d6562001-12-06 14:52:32 +0000134 long* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000135
Eric Andersenc38678d2002-09-16 06:22:25 +0000136 pidList = find_pid_by_name(*argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000137 if (!pidList || *pidList<=0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000138 errors++;
139 if (quiet==0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 bb_error_msg( "%s: no process killed", *argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000141 } else {
142 long *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000143
Eric Andersen7d72e792003-07-26 07:41:56 +0000144 for(pl = pidList; *pl !=0 ; pl++) {
145 if (*pl==myPid)
146 continue;
147 if (kill(*pl, signo) != 0) {
148 errors++;
149 if (quiet==0)
150 bb_perror_msg( "Could not kill pid '%ld'", *pl);
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000151 }
Eric Andersen7d72e792003-07-26 07:41:56 +0000152 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000153 }
Eric Andersen44608e92002-10-22 12:21:15 +0000154 free(pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000155 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000157 }
Erik Andersen6273f652000-03-17 01:12:41 +0000158#endif
Eric Andersenc38678d2002-09-16 06:22:25 +0000159 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000160}