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