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