1 | /* $NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Copyright (c) 1988, 1993, 1994
|
---|
5 | * The Regents of the University of California. All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * 3. Neither the name of the University nor the names of its contributors
|
---|
16 | * may be used to endorse or promote products derived from this software
|
---|
17 | * without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
29 | * SUCH DAMAGE.
|
---|
30 | */
|
---|
31 |
|
---|
32 | #ifdef HAVE_SYS_CDEFS_H
|
---|
33 | #include <sys/cdefs.h>
|
---|
34 | #endif
|
---|
35 | #if !defined(lint) && !defined(SHELL)
|
---|
36 | __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
|
---|
37 | The Regents of the University of California. All rights reserved.\n");
|
---|
38 | #endif /* not lint */
|
---|
39 |
|
---|
40 | #ifndef lint
|
---|
41 | #if 0
|
---|
42 | static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95";
|
---|
43 | #else
|
---|
44 | __RCSID("$NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $");
|
---|
45 | #endif
|
---|
46 | #endif /* not lint */
|
---|
47 |
|
---|
48 | #include <ctype.h>
|
---|
49 | #ifndef __sun__
|
---|
50 | #include <err.h>
|
---|
51 | #endif
|
---|
52 | #include <errno.h>
|
---|
53 | #include <signal.h>
|
---|
54 | #include <stdio.h>
|
---|
55 | #include <stdlib.h>
|
---|
56 | #include <string.h>
|
---|
57 | #include <termios.h>
|
---|
58 | #include <unistd.h>
|
---|
59 | #include <locale.h>
|
---|
60 | #include <sys/ioctl.h>
|
---|
61 |
|
---|
62 | #ifndef HAVE_SYS_SIGNAME
|
---|
63 | extern void init_sys_signame(void);
|
---|
64 | extern char sys_signame[NSIG][16];
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | #ifdef SHELL /* sh (aka ash) builtin */
|
---|
68 | #define main killcmd
|
---|
69 | #include "bltin/bltin.h"
|
---|
70 | #endif /* SHELL */
|
---|
71 |
|
---|
72 | static void nosig(char *);
|
---|
73 | static void printsignals(FILE *);
|
---|
74 | static int signame_to_signum(char *);
|
---|
75 | static void usage(void);
|
---|
76 | int main(int, char *[]);
|
---|
77 |
|
---|
78 | int
|
---|
79 | main(int argc, char *argv[])
|
---|
80 | {
|
---|
81 | int errors, numsig, pid;
|
---|
82 | char *ep;
|
---|
83 |
|
---|
84 | setprogname(argv[0]);
|
---|
85 | setlocale(LC_ALL, "");
|
---|
86 | if (argc < 2)
|
---|
87 | usage();
|
---|
88 |
|
---|
89 | numsig = SIGTERM;
|
---|
90 |
|
---|
91 | argc--, argv++;
|
---|
92 | if (strcmp(*argv, "-l") == 0) {
|
---|
93 | argc--, argv++;
|
---|
94 | if (argc > 1)
|
---|
95 | usage();
|
---|
96 | if (argc == 1) {
|
---|
97 | if (isdigit((unsigned char)**argv) == 0)
|
---|
98 | usage();
|
---|
99 | numsig = strtol(*argv, &ep, 10);
|
---|
100 | if (*ep != '\0') {
|
---|
101 | errx(EXIT_FAILURE, "illegal signal number: %s",
|
---|
102 | *argv);
|
---|
103 | /* NOTREACHED */
|
---|
104 | }
|
---|
105 | if (numsig >= 128)
|
---|
106 | numsig -= 128;
|
---|
107 | if (numsig <= 0 || numsig >= NSIG)
|
---|
108 | nosig(*argv);
|
---|
109 | #ifndef HAVE_SYS_SIGNAME
|
---|
110 | init_sys_signame();
|
---|
111 | #endif
|
---|
112 | printf("%s\n", sys_signame[numsig]);
|
---|
113 | exit(0);
|
---|
114 | }
|
---|
115 | printsignals(stdout);
|
---|
116 | exit(0);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (!strcmp(*argv, "-s")) {
|
---|
120 | argc--, argv++;
|
---|
121 | if (argc < 1) {
|
---|
122 | warnx("option requires an argument -- s");
|
---|
123 | usage();
|
---|
124 | }
|
---|
125 | if (strcmp(*argv, "0")) {
|
---|
126 | if ((numsig = signame_to_signum(*argv)) < 0)
|
---|
127 | nosig(*argv);
|
---|
128 | } else
|
---|
129 | numsig = 0;
|
---|
130 | argc--, argv++;
|
---|
131 | } else if (**argv == '-') {
|
---|
132 | ++*argv;
|
---|
133 | if (isalpha((unsigned char)**argv)) {
|
---|
134 | if ((numsig = signame_to_signum(*argv)) < 0)
|
---|
135 | nosig(*argv);
|
---|
136 | } else if (isdigit((unsigned char)**argv)) {
|
---|
137 | numsig = strtol(*argv, &ep, 10);
|
---|
138 | if (!*argv || *ep) {
|
---|
139 | errx(EXIT_FAILURE, "illegal signal number: %s",
|
---|
140 | *argv);
|
---|
141 | /* NOTREACHED */
|
---|
142 | }
|
---|
143 | if (numsig < 0 || numsig >= NSIG)
|
---|
144 | nosig(*argv);
|
---|
145 | } else
|
---|
146 | nosig(*argv);
|
---|
147 | argc--, argv++;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (argc == 0)
|
---|
151 | usage();
|
---|
152 |
|
---|
153 | for (errors = 0; argc; argc--, argv++) {
|
---|
154 | #ifdef SHELL
|
---|
155 | extern int getjobpgrp(const char *);
|
---|
156 | if (*argv[0] == '%') {
|
---|
157 | pid = getjobpgrp(*argv);
|
---|
158 | if (pid == 0) {
|
---|
159 | warnx("illegal job id: %s", *argv);
|
---|
160 | errors = 1;
|
---|
161 | continue;
|
---|
162 | }
|
---|
163 | } else
|
---|
164 | #endif
|
---|
165 | {
|
---|
166 | pid = strtol(*argv, &ep, 10);
|
---|
167 | if (!**argv || *ep) {
|
---|
168 | warnx("illegal process id: %s", *argv);
|
---|
169 | errors = 1;
|
---|
170 | continue;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | if (kill(pid, numsig) == -1) {
|
---|
174 | warn("%s", *argv);
|
---|
175 | errors = 1;
|
---|
176 | }
|
---|
177 | #ifdef SHELL
|
---|
178 | /* Wakeup the process if it was suspended, so it can
|
---|
179 | exit without an explicit 'fg'. */
|
---|
180 | if (numsig == SIGTERM || numsig == SIGHUP)
|
---|
181 | kill(pid, SIGCONT);
|
---|
182 | #endif
|
---|
183 | }
|
---|
184 |
|
---|
185 | exit(errors);
|
---|
186 | /* NOTREACHED */
|
---|
187 | }
|
---|
188 |
|
---|
189 | static int
|
---|
190 | signame_to_signum(char *sig)
|
---|
191 | {
|
---|
192 | int n;
|
---|
193 | #ifndef HAVE_SYS_SIGNAME
|
---|
194 | init_sys_signame();
|
---|
195 | #endif
|
---|
196 | if (strncasecmp(sig, "sig", 3) == 0)
|
---|
197 | sig += 3;
|
---|
198 | for (n = 1; n < NSIG; n++) {
|
---|
199 | if (!strcasecmp(sys_signame[n], sig))
|
---|
200 | return (n);
|
---|
201 | }
|
---|
202 | return (-1);
|
---|
203 | }
|
---|
204 |
|
---|
205 | static void
|
---|
206 | nosig(char *name)
|
---|
207 | {
|
---|
208 |
|
---|
209 | warnx("unknown signal %s; valid signals:", name);
|
---|
210 | printsignals(stderr);
|
---|
211 | exit(1);
|
---|
212 | /* NOTREACHED */
|
---|
213 | }
|
---|
214 |
|
---|
215 | static void
|
---|
216 | printsignals(FILE *fp)
|
---|
217 | {
|
---|
218 | int sig;
|
---|
219 | int len, nl;
|
---|
220 | const char *name;
|
---|
221 | int termwidth = 80;
|
---|
222 |
|
---|
223 | #ifdef TIOCGWINSZ
|
---|
224 | if (isatty(fileno(fp))) {
|
---|
225 | struct winsize win;
|
---|
226 | if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
|
---|
227 | termwidth = win.ws_col;
|
---|
228 | }
|
---|
229 | #else
|
---|
230 | #ifndef _MSC_VER
|
---|
231 | #warning TIOCGWINSZ is not present.
|
---|
232 | #endif
|
---|
233 | #endif
|
---|
234 | #ifndef HAVE_SYS_SIGNAME
|
---|
235 | init_sys_signame();
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | for (len = 0, sig = 1; sig < NSIG; sig++) {
|
---|
239 | name = sys_signame[sig];
|
---|
240 | nl = 1 + strlen(name);
|
---|
241 |
|
---|
242 | if (len + nl >= termwidth) {
|
---|
243 | fprintf(fp, "\n");
|
---|
244 | len = 0;
|
---|
245 | } else
|
---|
246 | if (len != 0)
|
---|
247 | fprintf(fp, " ");
|
---|
248 | len += nl;
|
---|
249 | fprintf(fp, "%s", name);
|
---|
250 | }
|
---|
251 | if (len != 0)
|
---|
252 | fprintf(fp, "\n");
|
---|
253 | }
|
---|
254 |
|
---|
255 | static void
|
---|
256 | usage(void)
|
---|
257 | {
|
---|
258 |
|
---|
259 | fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
|
---|
260 | " %s -l [exit_status]\n"
|
---|
261 | " %s -signal_name pid ...\n"
|
---|
262 | " %s -signal_number pid ...\n",
|
---|
263 | getprogname(), getprogname(), getprogname(), getprogname());
|
---|
264 | exit(1);
|
---|
265 | /* NOTREACHED */
|
---|
266 | }
|
---|