Patch from Bernhard Fischer to make a bunch of symbols static
[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(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         if (whichApp == KILL) {
110                 /* Looks like they want to do a kill. Do that */
111                 while (--argc >= 0) {
112                         int pid;
113
114                         if (!isdigit(**argv))
115                                 bb_error_msg_and_die( "Bad PID '%s'", *argv);
116                         pid = strtol(*argv, NULL, 0);
117                         if (kill(pid, signo) != 0) {
118                                 bb_perror_msg( "Could not kill pid '%d'", pid);
119                                 errors++;
120                         }
121                         argv++;
122                 }
123
124         }
125 #ifdef CONFIG_KILLALL
126         else {
127                 pid_t myPid=getpid();
128                 /* Looks like they want to do a killall.  Do that */
129                 while (--argc >= 0) {
130                         long* pidList;
131
132                         pidList = find_pid_by_name(*argv);
133                         if (!pidList || *pidList<=0) {
134                                 errors++;
135                                 if (quiet==0)
136                                         bb_error_msg( "%s: no process killed", *argv);
137                         } else {
138                                 long *pl;
139
140                                 for(pl = pidList; *pl !=0 ; pl++) {
141                                         if (*pl==myPid)
142                                                 continue;
143                                         if (kill(*pl, signo) != 0) {
144                                                 errors++;
145                                                 if (quiet==0)
146                                                         bb_perror_msg( "Could not kill pid '%ld'", *pl);
147                                         }
148                                 }
149                         }
150                         free(pidList);
151                         argv++;
152                 }
153         }
154 #endif
155         return errors;
156 }