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