Patch last_pach62 from vodz. This patch moves all the /proc parsing
[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         int errors = 0;
44
45 #ifdef CONFIG_KILLALL
46         /* Figure out what we are trying to do here */
47         whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL; 
48 #else
49         whichApp = KILL;
50 #endif
51
52         quiet=0;
53         argc--;
54         argv++;
55         /* Parse any options */
56         if (argc < 1)
57                 show_usage();
58
59         while (argc > 0 && **argv == '-') {
60                 while (*++(*argv)) {
61                         switch (**argv) {
62 #ifdef CONFIG_KILLALL
63                                 case 'q':
64                                         quiet++;
65                                         break;
66 #endif
67                                 case 'l':
68                                         if(argc>1) {
69                                                 for(argv++; *argv; argv++) {
70                                                         name = u_signal_names(*argv, &sig, -1);
71                                                         if(name!=NULL)
72                                                                 printf("%s\n", name);
73                                                 }
74                                         } else {
75                                                 int col = 0;
76                                                 for(sig=1; sig < NSIG; sig++) {
77                                                         name = u_signal_names(0, &sig, 1);
78                                                         if(name==NULL)  /* unnamed */
79                                                                 continue;
80                                                         col += printf("%2d) %-16s", sig, name);
81                                                         if (col > 60) {
82                                                                 printf("\n");
83                                                                 col = 0;
84                                                         }
85                                                 }
86                                                 printf("\n");
87                                         }
88                                         return EXIT_SUCCESS;
89                                 case '-':
90                                         show_usage();
91                                 default:
92                                         name = u_signal_names(*argv, &sig, 0);
93                                         if(name==NULL)
94                                                 error_msg_and_die( "bad signal name: %s", *argv);
95                                         argc--;
96                                         argv++;
97                                         goto do_it_now;
98                         }
99                         argc--;
100                         argv++;
101                 }
102         }
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                                 perror_msg_and_die( "Bad PID");
113                         pid = strtol(*argv, NULL, 0);
114                         if (kill(pid, sig) != 0) {
115                                 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 <= 0) {
131                                 errors++;
132                                 if (quiet==0)
133                                         error_msg( "%s: no process killed", *argv);
134                                 } else {
135                             for(; *pidList!=0; pidList++) {
136                                 if (*pidList==myPid)
137                                         continue;
138                                 if (kill(*pidList, sig) != 0) {
139                                         errors++;
140                                         if (quiet==0)
141                                                 perror_msg( "Could not kill pid '%d'", *pidList);
142                                         }
143                                 }
144                         }
145                         free(pidList);
146                         argv++;
147                 }
148         }
149 #endif
150         return errors;
151 }