Forgot this.
[oweals/busybox.git] / kill.c
1 /*
2  * Mini kill implementation for busybox
3  *
4  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21
22
23 #include "internal.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <ctype.h>
29
30 const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
31
32 struct signal_name {
33     const char *name;
34     int number;
35 };
36
37 const struct signal_name signames[] = {
38     {"HUP", SIGHUP},
39     {"INT", SIGINT},
40     {"QUIT", SIGQUIT},
41     {"ILL", SIGILL},
42     {"TRAP", SIGTRAP},
43     {"ABRT", SIGABRT},
44 #ifndef __alpha__
45     {"IOT", SIGIOT},
46 #endif
47 #if defined(__sparc__) || defined(__alpha__)
48     {"EMT", SIGEMT},
49 #else
50     {"BUS", SIGBUS},
51 #endif
52     {"FPE", SIGFPE},
53     {"KILL", SIGKILL},
54 #if defined(__sparc__) || defined(__alpha__)
55     {"BUS", SIGBUS},
56 #else
57     {"USR1", SIGUSR1},
58 #endif
59     {"SEGV", SIGSEGV},
60 #if defined(__sparc__) || defined(__alpha__)
61     {"SYS", SIGSYS},
62 #else
63     {"USR2", SIGUSR2},
64 #endif
65     {"PIPE", SIGPIPE},
66     {"ALRM", SIGALRM},
67     {"TERM", SIGTERM},
68 #if defined(__sparc__) || defined(__alpha__)
69     {"URG", SIGURG},
70     {"STOP", SIGSTOP},
71     {"TSTP", SIGTSTP},
72     {"CONT", SIGCONT},
73     {"CHLD", SIGCHLD},
74     {"TTIN", SIGTTIN},
75     {"TTOU", SIGTTOU},
76     {"IO", SIGIO},
77 # ifndef __alpha__
78     {"POLL", SIGIO},
79 # endif
80     {"XCPU", SIGXCPU},
81     {"XFSZ", SIGXFSZ},
82     {"VTALRM", SIGVTALRM},
83     {"PROF", SIGPROF},
84     {"WINCH", SIGWINCH},
85 # ifdef __alpha__
86     {"INFO", SIGINFO},
87 # else
88     {"LOST", SIGLOST},
89 # endif
90     {"USR1", SIGUSR1},
91     {"USR2", SIGUSR2},
92 #else
93     {"STKFLT", SIGSTKFLT},
94     {"CHLD", SIGCHLD},
95     {"CONT", SIGCONT},
96     {"STOP", SIGSTOP},
97     {"TSTP", SIGTSTP},
98     {"TTIN", SIGTTIN},
99     {"TTOU", SIGTTOU},
100     {"URG", SIGURG},
101     {"XCPU", SIGXCPU},
102     {"XFSZ", SIGXFSZ},
103     {"VTALRM", SIGVTALRM},
104     {"PROF", SIGPROF},
105     {"WINCH", SIGWINCH},
106     {"IO", SIGIO},
107     {"POLL", SIGPOLL},
108     {"PWR", SIGPWR},
109     {"UNUSED", SIGUNUSED},
110 #endif
111     {0, 0}
112 };
113
114 extern int kill_main (int argc, char **argv)
115 {
116     int sig = SIGTERM;
117
118     if ( argc < 2 )
119         usage (kill_usage);
120
121     if ( **(argv+1) == '-' ) {
122         if (isdigit( *(*(++argv)+1) )) {
123             sig = atoi (*argv);
124             if (sig < 0 || sig >= NSIG)
125                 goto end;
126         }
127         else {
128             const struct signal_name *s = signames;
129             while (s->name != 0) {
130                 if (strcasecmp (s->name, *argv+1) == 0) {
131                     sig = s->number;
132                     break;
133                 }
134                 s++;
135             }
136             if (s->name == 0)
137                 goto end;
138         }
139     }
140
141     while (--argc > 1) {
142         int pid;
143         if (! isdigit( **(++argv))) {
144             fprintf(stderr, "bad PID: %s\n", *argv);
145             exit( FALSE);
146         }
147         pid = atoi (*argv);
148         if (kill (pid, sig) != 0) {
149             perror (*argv);
150             exit ( FALSE);
151         }
152     }
153
154 end:
155     fprintf(stderr, "bad signal name: %s\n", *argv);
156     exit (TRUE);
157 }
158
159