Put in GPL v2 or later copyright notice
[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-2002 by Erik Andersen <andersee@debian.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 static const int KILL = 0;
36 static const int KILLALL = 1;
37
38
39 extern int kill_main(int argc, char **argv)
40 {
41         int whichApp, sig = SIGTERM, quiet;
42         const char *name;
43
44 #ifdef CONFIG_KILLALL
45         /* Figure out what we are trying to do here */
46         whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL; 
47 #else
48         whichApp = KILL;
49 #endif
50
51         quiet=0;
52         argc--;
53         argv++;
54         /* Parse any options */
55         if (argc < 1)
56                 show_usage();
57
58         while (argc > 0 && **argv == '-') {
59                 while (*++(*argv)) {
60                         switch (**argv) {
61 #ifdef CONFIG_KILLALL
62                                 case 'q':
63                                         quiet++;
64                                         break;
65 #endif
66                                 case 'l':
67                                         if(argc>1) {
68                                                 for(argv++; *argv; argv++) {
69                                                         name = u_signal_names(*argv, &sig, -1);
70                                                         if(name!=NULL)
71                                                                 printf("%s\n", name);
72                                                 }
73                                         } else {
74                                                 int col = 0;
75                                                 for(sig=1; sig < NSIG; sig++) {
76                                                         name = u_signal_names(0, &sig, 1);
77                                                         if(name==NULL)  /* unnamed */
78                                                                 continue;
79                                                         col += printf("%2d) %-16s", sig, name);
80                                                         if (col > 60) {
81                                                                 printf("\n");
82                                                                 col = 0;
83                                                         }
84                                                 }
85                                                 printf("\n");
86                                         }
87                                         return EXIT_SUCCESS;
88                                 case '-':
89                                         show_usage();
90                                 default:
91                                         name = u_signal_names(*argv, &sig, 0);
92                                         if(name==NULL)
93                                                 error_msg_and_die( "bad signal name: %s", *argv);
94                                         argc--;
95                                         argv++;
96                                         goto do_it_now;
97                         }
98                         argc--;
99                         argv++;
100                 }
101         }
102
103 do_it_now:
104
105         if (whichApp == KILL) {
106                 /* Looks like they want to do a kill. Do that */
107                 while (--argc >= 0) {
108                         int pid;
109
110                         if (!isdigit(**argv))
111                                 perror_msg_and_die( "Bad PID");
112                         pid = strtol(*argv, NULL, 0);
113                         if (kill(pid, sig) != 0) {
114                                 perror_msg_and_die( "Could not kill pid '%d'", pid);
115                         }
116                         argv++;
117                 }
118         } 
119 #ifdef CONFIG_KILLALL
120         else {
121                 int all_found = TRUE;
122                 pid_t myPid=getpid();
123                 /* Looks like they want to do a killall.  Do that */
124                 while (--argc >= 0) {
125                         long* pidList;
126
127                         pidList = find_pid_by_name( *argv);
128                         if (!pidList || *pidList<=0) {
129                                 all_found = FALSE;
130                                 if (quiet) {
131                                         exit(EXIT_FAILURE);
132                                 } else {
133                                         error_msg_and_die( "%s: no process killed", *argv);
134                                 }
135                         }
136
137                         for(; pidList && *pidList!=0; pidList++) {
138                                 if (*pidList==myPid)
139                                         continue;
140                                 if (kill(*pidList, sig) != 0) {
141                                         if (quiet) {
142                                                 exit(EXIT_FAILURE);
143                                         } else {
144                                                 perror_msg_and_die( "Could not kill pid '%d'", *pidList);
145                                         }
146                                 }
147                         }
148                         /* Note that we don't bother to free the memory
149                          * allocated in find_pid_by_name().  It will be freed
150                          * upon exit, so we can save a byte or two */
151                         argv++;
152                 }
153                 if (! all_found)
154                         return EXIT_FAILURE;
155         }
156 #endif
157
158         return EXIT_SUCCESS;
159 }