Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1988 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | char copyright[] = | |
36 | "@(#) Copyright (c) 1988 Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)kill.c 5.3 (Berkeley) 7/1/91"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include <signal.h> | |
45 | #include <errno.h> | |
46 | #include <stdio.h> | |
47 | #include <stdlib.h> | |
48 | #include <string.h> | |
49 | #include <ctype.h> | |
50 | ||
51 | static char *signals[] = { | |
6a901187 C |
52 | "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", /* 1 - 6 */ |
53 | "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */ | |
54 | "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */ | |
55 | "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */ | |
56 | "XFSZ", "VTALRM", "PROF", "WINCH", "INFO", "USR1", /* 25 - 30 */ | |
57 | "USR2", NULL, /* 31 - 32 */ | |
15637ed4 RG |
58 | }; |
59 | ||
60 | main(argc, argv) | |
61 | int argc; | |
62 | char **argv; | |
63 | { | |
64 | register int errors, numsig, pid; | |
65 | register char **p; | |
66 | char *ep; | |
67 | ||
68 | if (argc < 2) | |
69 | usage(); | |
70 | ||
15637ed4 | 71 | numsig = SIGTERM; |
6a901187 C |
72 | argc--, argv++; |
73 | if (strcmp(*argv, "-l") == 0) { | |
74 | if (argc > 2) { | |
75 | usage (); | |
76 | /* NOTREACHED */ | |
77 | } | |
78 | if (argc == 2) { | |
79 | argv++; | |
80 | if (isdigit(**argv)) { | |
81 | numsig = strtol(*argv, &ep, 10); | |
82 | if (*argv && !*ep) { | |
83 | if (numsig > 0 && numsig < NSIG) { | |
84 | printsig (numsig); | |
85 | exit (0); | |
86 | } | |
87 | ||
88 | numsig -= 128; | |
89 | if (numsig > 0 && numsig < NSIG) { | |
90 | printsig (numsig); | |
91 | exit (0); | |
92 | } | |
93 | } | |
94 | (void)fprintf(stderr, | |
95 | "kill: illegal signal number %s\n", *argv); | |
96 | exit(1); | |
97 | } | |
98 | usage (); | |
99 | /* NOTREACHED */ | |
100 | } | |
101 | printsignals(stdout); | |
102 | exit(0); | |
103 | } else if (strcmp(*argv, "-s") == 0) { | |
104 | if (argc < 2) { | |
105 | (void)fprintf(stderr, | |
106 | "kill: option requires an argument -- s\n"); | |
107 | usage(); | |
108 | } | |
109 | argc--,argv++; | |
110 | if (strcmp (*argv, "0") == 0) { | |
111 | numsig = 0; | |
112 | } else { | |
113 | if ((numsig = signame_to_signum (*argv)) < 0) { | |
114 | nosig(*argv); | |
115 | /* NOTREACHED */ | |
116 | } | |
117 | } | |
118 | argc--,argv++; | |
119 | } else if (**argv == '-') { | |
15637ed4 RG |
120 | ++*argv; |
121 | if (isalpha(**argv)) { | |
6a901187 C |
122 | if ((numsig = signame_to_signum (*argv)) < 0) { |
123 | nosig(*argv); | |
124 | /* NOTREACHED */ | |
15637ed4 RG |
125 | } |
126 | } else if (isdigit(**argv)) { | |
127 | numsig = strtol(*argv, &ep, 10); | |
128 | if (!*argv || *ep) { | |
129 | (void)fprintf(stderr, | |
130 | "kill: illegal signal number %s\n", *argv); | |
131 | exit(1); | |
132 | } | |
6a901187 | 133 | if (numsig <= 0 || numsig >= NSIG) { |
15637ed4 | 134 | nosig(*argv); |
6a901187 C |
135 | /* NOTREACHED */ |
136 | } | |
15637ed4 RG |
137 | } else |
138 | nosig(*argv); | |
6a901187 | 139 | argc--,argv++; |
15637ed4 RG |
140 | } |
141 | ||
142 | if (!*argv) | |
143 | usage(); | |
144 | ||
145 | for (errors = 0; *argv; ++argv) { | |
146 | pid = strtol(*argv, &ep, 10); | |
147 | if (!*argv || *ep) { | |
148 | (void)fprintf(stderr, | |
149 | "kill: illegal process id %s\n", *argv); | |
150 | continue; | |
151 | } | |
152 | if (kill(pid, numsig) == -1) { | |
153 | (void)fprintf(stderr, | |
154 | "kill: %s: %s\n", *argv, strerror(errno)); | |
155 | errors = 1; | |
156 | } | |
157 | } | |
158 | exit(errors); | |
159 | } | |
160 | ||
6a901187 C |
161 | int |
162 | signame_to_signum (sig) | |
163 | char *sig; | |
164 | { | |
165 | char **p; | |
166 | ||
167 | if (!strncasecmp(sig, "sig", 3)) | |
168 | sig += 3; | |
169 | for (p = signals; *p; ++p) { | |
170 | if (!strcasecmp(*p, sig)) { | |
171 | return p - signals + 1; | |
172 | } | |
173 | } | |
174 | return -1; | |
175 | } | |
176 | ||
15637ed4 RG |
177 | nosig(name) |
178 | char *name; | |
179 | { | |
180 | (void)fprintf(stderr, | |
181 | "kill: unknown signal %s; valid signals:\n", name); | |
6a901187 | 182 | printsignals(stderr); |
15637ed4 RG |
183 | exit(1); |
184 | } | |
185 | ||
6a901187 C |
186 | printsig(sig) |
187 | int sig; | |
188 | { | |
189 | printf ("%s\n", signals[sig - 1]); | |
190 | } | |
191 | ||
192 | printsignals(fp) | |
15637ed4 RG |
193 | FILE *fp; |
194 | { | |
6a901187 | 195 | register char **p = signals;; |
15637ed4 | 196 | |
6a901187 C |
197 | /* From POSIX 1003.2, Draft 11.2: |
198 | When the -l option is specified, the symbolic name of each | |
199 | signal shall be written in the following format: | |
200 | "%s%c", <signal_name>, <separator> | |
201 | where the <signal_name> is in uppercase, without the SIG prefix, | |
202 | and the <separator> shall either be a <newline> or a <space>. | |
203 | For the last signal written, <separator> shall be a <newline> */ | |
204 | ||
205 | /* This looses if the signals array is empty; But, since it | |
206 | will "never happen", there is no need to add wrap this | |
207 | in a conditional that will always succeed. */ | |
208 | (void)fprintf(fp, "%s", *p); | |
209 | ||
210 | for (++p ; *p; ++p) { | |
211 | (void)fprintf(fp, " %s", *p); | |
15637ed4 RG |
212 | } |
213 | (void)fprintf(fp, "\n"); | |
214 | } | |
215 | ||
216 | usage() | |
217 | { | |
6a901187 C |
218 | (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n"); |
219 | (void)fprintf(stderr, " kill -l [exit_status]\n"); | |
220 | (void)fprintf(stderr, "obsolete usage:\n"); | |
221 | (void)fprintf(stderr, " kill -signal_name pid ...\n"); | |
222 | (void)fprintf(stderr, " kill -signal_number pid ...\n"); | |
15637ed4 RG |
223 | exit(1); |
224 | } |