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