kill.c revision 1.15 1 /* $NetBSD: kill.c,v 1.15 1998/07/28 05:31:24 mycroft 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 #ifndef lint
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.15 1998/07/28 05:31:24 mycroft 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
58 void nosig __P((char *));
59 void printsignals __P((FILE *));
60 int signame_to_signum __P((char *));
61 void usage __P((void));
62 int main __P((int, char *[]));
63
64 int
65 main(argc, argv)
66 int argc;
67 char *argv[];
68 {
69 int errors, numsig, pid;
70 char *ep;
71
72 if (argc < 2)
73 usage();
74
75 numsig = SIGTERM;
76
77 argc--, argv++;
78 if (!strcmp(*argv, "-l")) {
79 argc--, argv++;
80 if (argc > 1)
81 usage();
82 if (argc == 1) {
83 if (!isdigit(**argv))
84 usage();
85 numsig = strtol(*argv, &ep, 10);
86 if (*ep)
87 errx(1, "illegal signal number: %s", *argv);
88 if (numsig >= 128)
89 numsig -= 128;
90 if (numsig <= 0 || numsig >= NSIG)
91 nosig(*argv);
92 (void)printf("%s\n", sys_signame[numsig]);
93 exit(0);
94 /* NOTREACHED */
95 }
96 printsignals(stdout);
97 exit(0);
98 /* NOTREACHED */
99 }
100
101 if (!strcmp(*argv, "-s")) {
102 argc--, argv++;
103 if (argc < 1) {
104 warnx("option requires an argument -- s");
105 usage();
106 }
107 if (strcmp(*argv, "0")) {
108 if ((numsig = signame_to_signum(*argv)) < 0)
109 nosig(*argv);
110 } else
111 numsig = 0;
112 argc--, argv++;
113 } else if (**argv == '-') {
114 ++*argv;
115 if (isalpha(**argv)) {
116 if ((numsig = signame_to_signum(*argv)) < 0)
117 nosig(*argv);
118 } else if (isdigit(**argv)) {
119 numsig = strtol(*argv, &ep, 10);
120 if (!*argv || *ep)
121 errx(1, "illegal signal number: %s", *argv);
122 if (numsig < 0 || numsig >= NSIG)
123 nosig(*argv);
124 } else
125 nosig(*argv);
126 argc--, argv++;
127 }
128
129 if (argc == 0)
130 usage();
131
132 for (errors = 0; argc; argc--, argv++) {
133 pid = strtol(*argv, &ep, 10);
134 if (!**argv || *ep) {
135 warnx("illegal process id: %s", *argv);
136 errors = 1;
137 } else if (kill(pid, numsig) == -1) {
138 warn("%s", *argv);
139 errors = 1;
140 }
141 }
142
143 exit(errors);
144 /* NOTREACHED */
145 }
146
147 int
148 signame_to_signum(sig)
149 char *sig;
150 {
151 int n;
152
153 if (!strncasecmp(sig, "sig", 3))
154 sig += 3;
155 for (n = 1; n < NSIG; n++) {
156 if (!strcasecmp(sys_signame[n], sig))
157 return (n);
158 }
159 return (-1);
160 }
161
162 void
163 nosig(name)
164 char *name;
165 {
166
167 warnx("unknown signal %s; valid signals:", name);
168 printsignals(stderr);
169 exit(1);
170 /* NOTREACHED */
171 }
172
173 void
174 printsignals(fp)
175 FILE *fp;
176 {
177 int n;
178
179 for (n = 1; n < NSIG; n++) {
180 (void)fprintf(fp, "%s", sys_signame[n]);
181 if (n == (NSIG / 2) || n == (NSIG - 1))
182 (void)fprintf(fp, "\n");
183 else
184 (void)fprintf(fp, " ");
185 }
186 }
187
188 void
189 usage()
190 {
191
192 (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n");
193 (void)fprintf(stderr, " kill -l [exit_status]\n");
194 (void)fprintf(stderr, " kill -signal_name pid ...\n");
195 (void)fprintf(stderr, " kill -signal_number pid ...\n");
196 exit(1);
197 /* NOTREACHED */
198 }
199