Simplified version checking.
[oweals/busybox.git] / 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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include "busybox.h"
33
34 static const int KILL = 0;
35 static const int KILLALL = 1;
36
37
38 extern int kill_main(int argc, char **argv)
39 {
40         int whichApp, sig = SIGTERM;
41         const char *name;
42
43 #ifdef BB_KILLALL
44         /* Figure out what we are trying to do here */
45         whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL; 
46 #else
47         whichApp = KILL;
48 #endif
49
50         argc--;
51         argv++;
52         /* Parse any options */
53         if (argc < 1)
54                 show_usage();
55
56         while (argc > 0 && **argv == '-') {
57                 while (*++(*argv)) {
58                         switch (**argv) {
59                         case 'l':
60                                 if(argc>1) {
61                                         for(argv++; *argv; argv++) {
62                                                 name = u_signal_names(*argv, &sig, -1);
63                                                 if(name!=NULL)
64                                                         printf("%s\n", name);
65                                         }
66                                 } else {
67                                         int col = 0;
68                                         for(sig=1; sig < NSIG; sig++) {
69                                                 name = u_signal_names(0, &sig, 1);
70                                                 if(name==NULL)  /* unnamed */
71                                                         continue;
72                                                 col += printf("%2d) %-16s", sig, name);
73                                                 if (col > 60) {
74                                                         printf("\n");
75                                                         col = 0;
76                                                 }
77                                         }
78                                         printf("\n");
79                                 }
80                                 return EXIT_SUCCESS;
81                         case '-':
82                                 show_usage();
83                         default:
84                                 name = u_signal_names(*argv, &sig, 0);
85                                 if(name==NULL)
86                                         error_msg_and_die( "bad signal name: %s", *argv);
87                                                                 argc--;
88                                                                 argv++;
89                                                                 goto do_it_now;
90                                                         }
91                         argc--;
92                         argv++;
93                 }
94         }
95
96   do_it_now:
97
98         if (whichApp == KILL) {
99                 /* Looks like they want to do a kill. Do that */
100                 while (--argc >= 0) {
101                         int pid;
102
103                         if (!isdigit(**argv))
104                                 perror_msg_and_die( "Bad PID");
105                         pid = strtol(*argv, NULL, 0);
106                         if (kill(pid, sig) != 0) 
107                                 perror_msg_and_die( "Could not kill pid '%d'", pid);
108                         argv++;
109                 }
110         } 
111 #ifdef BB_KILLALL
112         else {
113                 int all_found = TRUE;
114                 pid_t myPid=getpid();
115                 /* Looks like they want to do a killall.  Do that */
116                 while (--argc >= 0) {
117                         pid_t* pidList;
118
119                         pidList = find_pid_by_name( *argv);
120                         if (!pidList || *pidList<=0) {
121                                 all_found = FALSE;
122                                 error_msg_and_die( "%s: no process killed", *argv);
123                         }
124
125                         for(; pidList && *pidList!=0; pidList++) {
126                                 if (*pidList==myPid)
127                                         continue;
128                                 if (kill(*pidList, sig) != 0) 
129                                         perror_msg_and_die( "Could not kill pid '%d'", *pidList);
130                         }
131                         /* Note that we don't bother to free the memory
132                          * allocated in find_pid_by_name().  It will be freed
133                          * upon exit, so we can save a byte or two */
134                         argv++;
135                 }
136                 if (all_found == FALSE)
137                         return EXIT_FAILURE;
138         }
139 #endif
140
141         return EXIT_SUCCESS;
142 }