close bug 638
[oweals/busybox.git] / procps / kill.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini kill/killall implementation for busybox
4  *
5  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7  *
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
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <signal.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include "busybox.h"
34
35 #define KILL 0
36 #define KILLALL 1
37
38 extern int kill_main(int argc, char **argv)
39 {
40         int whichApp, signo = SIGTERM;
41         const char *name;
42         int errors = 0;
43
44 #ifdef CONFIG_KILLALL
45         int quiet=0;
46         /* Figure out what we are trying to do here */
47         whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
48 #else
49         whichApp = KILL;
50 #endif
51
52         /* Parse any options */
53         if (argc < 2)
54                 bb_show_usage();
55
56         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                                 }
76                         }
77                         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
90 #ifdef CONFIG_KILLALL
91         /* The -q quiet option */
92         if(whichApp != KILL && argv[1][1]=='q' && argv[1][2]=='\0'){
93                 quiet++;
94                 argv++;
95                 argc--;
96                 if(argc<2 || argv[1][0] != '-'){
97                         goto do_it_now;
98                 }
99         }
100 #endif
101
102         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
107 do_it_now:
108
109         /* Pid or name required */
110         if (argc <= 0)
111                 bb_show_usage();
112
113         if (whichApp == KILL) {
114                 /* Looks like they want to do a kill. Do that */
115                 while (--argc >= 0) {
116                         int pid;
117
118                         if (!isdigit(**argv) && **argv != '-')
119                                 bb_error_msg_and_die( "Bad PID '%s'", *argv);
120                         pid = strtol(*argv, NULL, 0);
121                         if (kill(pid, signo) != 0) {
122                                 bb_perror_msg( "Could not kill pid '%d'", pid);
123                                 errors++;
124                         }
125                         argv++;
126                 }
127
128         }
129 #ifdef CONFIG_KILLALL
130         else {
131                 pid_t myPid=getpid();
132                 /* Looks like they want to do a killall.  Do that */
133                 while (--argc >= 0) {
134                         long* pidList;
135
136                         pidList = find_pid_by_name(*argv);
137                         if (!pidList || *pidList<=0) {
138                                 errors++;
139                                 if (quiet==0)
140                                         bb_error_msg( "%s: no process killed", *argv);
141                         } else {
142                                 long *pl;
143
144                                 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);
151                                         }
152                                 }
153                         }
154                         free(pidList);
155                         argv++;
156                 }
157         }
158 #endif
159         return errors;
160 }