kill.c revision 1.9 1 /*
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. 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 static char copyright[] =
36 "@(#) 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 /*static char sccsid[] = "from: @(#)kill.c 8.3 (Berkeley) 4/2/94";*/
42 static char *rcsid = "$Id: kill.c,v 1.9 1995/03/17 05:48:48 jtc Exp $";
43 #endif /* not lint */
44
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 void nosig __P((char *));
54 void printsignals __P((FILE *));
55 int signame_to_signum __P((char *));
56 void usage __P((void));
57
58 int
59 main(argc, argv)
60 int argc;
61 char *argv[];
62 {
63 int errors, numsig, pid;
64 char *ep;
65
66 if (argc < 2)
67 usage();
68
69 numsig = SIGTERM;
70
71 argc--, argv++;
72 if (!strcmp(*argv, "-l")) {
73 argc--, argv++;
74 if (argc > 1)
75 usage();
76 if (argc == 1) {
77 if (!isdigit(**argv))
78 usage();
79 numsig = strtol(*argv, &ep, 10);
80 if (!*argv || *ep)
81 errx(1, "illegal signal number: %s", *argv);
82 if (numsig >= 128)
83 numsig -= 128;
84 if (numsig <= 0 || numsig >= NSIG)
85 nosig(*argv);
86 printf("%s\n", sys_signame[numsig]);
87 exit(0);
88 }
89 printsignals(stdout);
90 exit(0);
91 }
92
93 if (!strcmp(*argv, "-s")) {
94 argc--, argv++;
95 if (argc < 1) {
96 warnx("option requires an argument -- s");
97 usage();
98 }
99 if (strcmp(*argv, "0")) {
100 if ((numsig = signame_to_signum(*argv)) < 0)
101 nosig(*argv);
102 } else
103 numsig = 0;
104 argc--, argv++;
105 } else if (**argv == '-') {
106 ++*argv;
107 if (isalpha(**argv)) {
108 if ((numsig = signame_to_signum(*argv)) < 0)
109 nosig(*argv);
110 } else if (isdigit(**argv)) {
111 numsig = strtol(*argv, &ep, 10);
112 if (!*argv || *ep)
113 errx(1, "illegal signal number: %s", *argv);
114 if (numsig < 0 || numsig >= NSIG)
115 nosig(*argv);
116 } else
117 nosig(*argv);
118 argc--, argv++;
119 }
120
121 if (argc == 0)
122 usage();
123
124 for (errors = 0; argc; argc--, argv++) {
125 pid = strtol(*argv, &ep, 10);
126 if (!*argv || *ep) {
127 warnx("illegal process id: %s", *argv);
128 errors = 1;
129 } else if (kill(pid, numsig) == -1) {
130 warn("%s", *argv);
131 errors = 1;
132 }
133 }
134
135 exit(errors);
136 }
137
138 int
139 signame_to_signum(sig)
140 char *sig;
141 {
142 int n;
143
144 if (!strncasecmp(sig, "sig", 3))
145 sig += 3;
146 for (n = 1; n < NSIG; n++) {
147 if (!strcasecmp(sys_signame[n], sig))
148 return (n);
149 }
150 return (-1);
151 }
152
153 void
154 nosig(name)
155 char *name;
156 {
157
158 warnx("unknown signal %s; valid signals:", name);
159 printsignals(stderr);
160 exit(1);
161 }
162
163 void
164 printsignals(fp)
165 FILE *fp;
166 {
167 int n;
168
169 for (n = 1; n < NSIG; n++) {
170 (void)fprintf(fp, "%s", sys_signame[n]);
171 if (n == (NSIG / 2) || n == (NSIG - 1))
172 (void)fprintf(fp, "\n");
173 else
174 (void)fprintf(fp, " ");
175 }
176 }
177
178 void
179 usage()
180 {
181
182 (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n");
183 (void)fprintf(stderr, " kill -l [exit_status]\n");
184 (void)fprintf(stderr, " kill -signal_name pid ...\n");
185 (void)fprintf(stderr, " kill -signal_number pid ...\n");
186 exit(1);
187 }
188