xref: /freebsd/bin/kill/kill.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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 #if 0
35 #ifndef lint
36 static char const copyright[] =
37 "@(#) Copyright (c) 1988, 1993, 1994\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 static char sccsid[] = "@(#)kill.c	8.4 (Berkeley) 4/28/95";
43 #endif /* not lint */
44 #endif
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
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 == '-' && *(*argv + 1) != '-') {
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 && strncmp(*argv, "--", 2) == 0)
123 		argc--, argv++;
124 
125 	if (argc == 0)
126 		usage();
127 
128 	for (errors = 0; argc; argc--, argv++) {
129 		pid = strtol(*argv, &ep, 10);
130 		if (!**argv || *ep) {
131 			warnx("illegal process id: %s", *argv);
132 			errors = 1;
133 		} else if (kill(pid, numsig) == -1) {
134 			warn("%s", *argv);
135 			errors = 1;
136 		}
137 	}
138 
139 	exit(errors);
140 }
141 
142 int
143 signame_to_signum(char *sig)
144 {
145 	int n;
146 
147 	if (!strncasecmp(sig, "sig", (size_t)3))
148 		sig += 3;
149 	for (n = 1; n < NSIG; n++) {
150 		if (!strcasecmp(sys_signame[n], sig))
151 			return (n);
152 	}
153 	return (-1);
154 }
155 
156 void
157 nosig(char *name)
158 {
159 
160 	warnx("unknown signal %s; valid signals:", name);
161 	printsignals(stderr);
162 	exit(1);
163 }
164 
165 void
166 printsignals(FILE *fp)
167 {
168 	int n;
169 
170 	for (n = 1; n < NSIG; n++) {
171 		(void)fprintf(fp, "%s", sys_signame[n]);
172 		if (n == (NSIG / 2) || n == (NSIG - 1))
173 			(void)fprintf(fp, "\n");
174 		else
175 			(void)fprintf(fp, " ");
176 	}
177 }
178 
179 void
180 usage(void)
181 {
182 
183 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
184 		"usage: kill [-s signal_name] pid ...",
185 		"       kill -l [exit_status]",
186 		"       kill -signal_name pid ...",
187 		"       kill -signal_number pid ...");
188 	exit(1);
189 }
190