Splitting statements with #define's can cause trouble for cross
[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-2003 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, quiet = 0;
41         const char *name;
42         int errors = 0;
43
44 #ifdef CONFIG_KILLALL
45         /* Figure out what we are trying to do here */
46         whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
47 #else
48         whichApp = KILL;
49 #endif
50
51         /* Parse any options */
52         if (argc < 2)
53                 bb_show_usage();
54
55         if(argv[1][0] != '-'){
56                 argv++;
57                 argc--;
58                 goto do_it_now;
59         }
60
61         /* The -l option, which prints out signal names. */
62         if(argv[1][1]=='l' && argv[1][2]=='\0'){
63                 if(argc==2) {
64                         /* Print the whole signal list */
65                         int col = 0;
66                         for(signo=1; signo < NSIG; signo++) {
67                                 name = u_signal_names(0, &signo, 1);
68                                 if(name==NULL)  /* unnamed */
69                                         continue;
70                                 col += printf("%2d) %-16s", signo, name);
71                                 if (col > 60) {
72                                         printf("\n");
73                                         col = 0;
74                                 }
75                         }
76                         printf("\n");
77
78                 } else {
79                         for(argv++; *argv; argv++) {
80                                 name = u_signal_names(*argv, &signo, -1);
81                                 if(name!=NULL)
82                                         printf("%s\n", name);
83                         }
84                 }
85                 /* If they specified -l, were all done */
86                 return EXIT_SUCCESS;
87         }
88
89         /* The -q quiet option */
90         if(argv[1][1]=='q' && argv[1][2]=='\0'){
91                 quiet++;
92                 argv++;
93                 argc--;
94                 if(argc<2 || argv[1][0] != '-'){
95                         goto do_it_now;
96                 }
97         }
98
99         if(!u_signal_names(argv[1]+1, &signo, 0))
100                 bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
101         argv+=2;
102         argc-=2;
103
104 do_it_now:
105
106         if (whichApp == KILL) {
107                 /* Looks like they want to do a kill. Do that */
108                 while (--argc >= 0) {
109                         int pid;
110
111                         if (!isdigit(**argv))
112                                 bb_error_msg_and_die( "Bad PID '%s'", *argv);
113                         pid = strtol(*argv, NULL, 0);
114                         if (kill(pid, signo) != 0) {
115                                 bb_perror_msg( "Could not kill pid '%d'", pid);
116                                 errors++;
117                         }
118                         argv++;
119                 }
120
121         }
122 #ifdef CONFIG_KILLALL
123         else {
124                 pid_t myPid=getpid();
125                 /* Looks like they want to do a killall.  Do that */
126                 while (--argc >= 0) {
127                         long* pidList;
128
129                         pidList = find_pid_by_name(*argv);
130                         if (!pidList || *pidList<=0) {
131                                 errors++;
132                                 if (quiet==0)
133                                         bb_error_msg( "%s: no process killed", *argv);
134                         } else {
135                                 long *pl;
136
137                                 for(pl = pidList; *pl !=0 ; pl++) {
138                                         if (*pl==myPid)
139                                                 continue;
140                                         if (kill(*pl, signo) != 0) {
141                                                 errors++;
142                                                 if (quiet==0)
143                                                         bb_perror_msg( "Could not kill pid '%ld'", *pl);
144                                         }
145                                 }
146                         }
147                         free(pidList);
148                         argv++;
149                 }
150         }
151 #endif
152         return errors;
153 }