ca6f4203ad71aba3e0357daab3c87d64d7d15a78
[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  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9  */
10
11 #include "busybox.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <signal.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #define KILL 0
22 #define KILLALL 1
23
24 int kill_main(int argc, char **argv)
25 {
26         int whichApp, signo = SIGTERM;
27         const char *name;
28         int errors = 0;
29
30 #ifdef CONFIG_KILLALL
31         int quiet=0;
32         /* Figure out what we are trying to do here */
33         whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
34 #else
35         whichApp = KILL;
36 #endif
37
38         /* Parse any options */
39         if (argc < 2)
40                 bb_show_usage();
41
42         if(argv[1][0] != '-'){
43                 argv++;
44                 argc--;
45                 goto do_it_now;
46         }
47
48         /* The -l option, which prints out signal names. */
49         if(argv[1][1]=='l' && argv[1][2]=='\0'){
50                 if(argc==2) {
51                         /* Print the whole signal list */
52                         int col = 0;
53                         for(signo=1; signo < NSIG; signo++) {
54                                 name = u_signal_names(0, &signo, 1);
55                                 if(name==NULL)  /* unnamed */
56                                         continue;
57                                 col += printf("%2d) %-16s", signo, name);
58                                 if (col > 60) {
59                                         printf("\n");
60                                         col = 0;
61                                 }
62                         }
63                         printf("\n");
64
65                 } else {
66                         for(argv++; *argv; argv++) {
67                                 name = u_signal_names(*argv, &signo, -1);
68                                 if(name!=NULL)
69                                         printf("%s\n", name);
70                         }
71                 }
72                 /* If they specified -l, were all done */
73                 return EXIT_SUCCESS;
74         }
75
76 #ifdef CONFIG_KILLALL
77         /* The -q quiet option */
78         if(whichApp != KILL && argv[1][1]=='q' && argv[1][2]=='\0'){
79                 quiet++;
80                 argv++;
81                 argc--;
82                 if(argc<2 || argv[1][0] != '-'){
83                         goto do_it_now;
84                 }
85         }
86 #endif
87
88         if(!u_signal_names(argv[1]+1, &signo, 0))
89                 bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
90         argv+=2;
91         argc-=2;
92
93 do_it_now:
94
95         /* Pid or name required */
96         if (argc <= 0)
97                 bb_show_usage();
98
99         if (whichApp == KILL) {
100                 /* Looks like they want to do a kill. Do that */
101                 while (--argc >= 0) {
102                         int pid;
103
104                         if (!isdigit(**argv) && **argv != '-')
105                                 bb_error_msg_and_die( "Bad PID '%s'", *argv);
106                         pid = strtol(*argv, NULL, 0);
107                         if (kill(pid, signo) != 0) {
108                                 bb_perror_msg( "Could not kill pid '%d'", pid);
109                                 errors++;
110                         }
111                         argv++;
112                 }
113
114         }
115 #ifdef CONFIG_KILLALL
116         else {
117                 pid_t myPid=getpid();
118                 /* Looks like they want to do a killall.  Do that */
119                 while (--argc >= 0) {
120                         long* pidList;
121
122                         pidList = find_pid_by_name(*argv);
123                         if (!pidList || *pidList<=0) {
124                                 errors++;
125                                 if (quiet==0)
126                                         bb_error_msg( "%s: no process killed", *argv);
127                         } else {
128                                 long *pl;
129
130                                 for(pl = pidList; *pl !=0 ; pl++) {
131                                         if (*pl==myPid)
132                                                 continue;
133                                         if (kill(*pl, signo) != 0) {
134                                                 errors++;
135                                                 if (quiet==0)
136                                                         bb_perror_msg( "Could not kill pid '%ld'", *pl);
137                                         }
138                                 }
139                         }
140                         free(pidList);
141                         argv++;
142                 }
143         }
144 #endif
145         return errors;
146 }