xref: /freebsd/bin/kill/kill.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*
32  * Important: This file is used both as a standalone program /bin/kill and
33  * as a builtin for /bin/sh (#define SHELL).
34  */
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #ifdef SHELL
45 #define main killcmd
46 #include "bltin/bltin.h"
47 #endif
48 
49 static void nosig(const char *);
50 static void printsignals(FILE *);
51 static int signame_to_signum(const char *);
52 static void usage(void) __dead2;
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	long pidl;
58 	pid_t pid;
59 	int errors, numsig, ret;
60 	char *ep;
61 
62 	if (argc < 2)
63 		usage();
64 
65 	numsig = SIGTERM;
66 
67 	argc--, argv++;
68 	if (!strcmp(*argv, "-l")) {
69 		argc--, argv++;
70 		if (argc > 1)
71 			usage();
72 		if (argc == 1) {
73 			if (!isdigit(**argv))
74 				usage();
75 			numsig = strtol(*argv, &ep, 10);
76 			if (!**argv || *ep)
77 				errx(2, "illegal signal number: %s", *argv);
78 			if (numsig >= 128)
79 				numsig -= 128;
80 			if (numsig <= 0 || numsig >= sys_nsig)
81 				nosig(*argv);
82 			printf("%s\n", sys_signame[numsig]);
83 			return (0);
84 		}
85 		printsignals(stdout);
86 		return (0);
87 	}
88 
89 	if (!strcmp(*argv, "-s")) {
90 		argc--, argv++;
91 		if (argc < 1) {
92 			warnx("option requires an argument -- s");
93 			usage();
94 		}
95 		if (strcmp(*argv, "0")) {
96 			if ((numsig = signame_to_signum(*argv)) < 0)
97 				nosig(*argv);
98 		} else
99 			numsig = 0;
100 		argc--, argv++;
101 	} else if (**argv == '-' && *(*argv + 1) != '-') {
102 		++*argv;
103 		if (isalpha(**argv)) {
104 			if ((numsig = signame_to_signum(*argv)) < 0)
105 				nosig(*argv);
106 		} else if (isdigit(**argv)) {
107 			numsig = strtol(*argv, &ep, 10);
108 			if (!**argv || *ep)
109 				errx(2, "illegal signal number: %s", *argv);
110 			if (numsig < 0)
111 				nosig(*argv);
112 		} else
113 			nosig(*argv);
114 		argc--, argv++;
115 	}
116 
117 	if (argc > 0 && strncmp(*argv, "--", 2) == 0)
118 		argc--, argv++;
119 
120 	if (argc == 0)
121 		usage();
122 
123 	for (errors = 0; argc; argc--, argv++) {
124 #ifdef SHELL
125 		if (**argv == '%')
126 			ret = killjob(*argv, numsig);
127 		else
128 #endif
129 		{
130 			pidl = strtol(*argv, &ep, 10);
131 			/* Check for overflow of pid_t. */
132 			pid = (pid_t)pidl;
133 			if (!**argv || *ep || pid != pidl)
134 				errx(2, "illegal process id: %s", *argv);
135 			ret = kill(pid, numsig);
136 		}
137 		if (ret == -1) {
138 			warn("%s", *argv);
139 			errors = 1;
140 		}
141 	}
142 
143 	return (errors);
144 }
145 
146 static int
147 signame_to_signum(const char *sig)
148 {
149 	int n;
150 
151 	if (strncasecmp(sig, "SIG", 3) == 0)
152 		sig += 3;
153 	for (n = 1; n < sys_nsig; n++) {
154 		if (!strcasecmp(sys_signame[n], sig))
155 			return (n);
156 	}
157 	return (-1);
158 }
159 
160 static void
161 nosig(const char *name)
162 {
163 
164 	warnx("unknown signal %s; valid signals:", name);
165 	printsignals(stderr);
166 #ifdef SHELL
167 	error(NULL);
168 #else
169 	exit(2);
170 #endif
171 }
172 
173 static void
174 printsignals(FILE *fp)
175 {
176 	int n;
177 
178 	for (n = 1; n < sys_nsig; n++) {
179 		(void)fprintf(fp, "%s", sys_signame[n]);
180 		if (n == (sys_nsig / 2) || n == (sys_nsig - 1))
181 			(void)fprintf(fp, "\n");
182 		else
183 			(void)fprintf(fp, " ");
184 	}
185 }
186 
187 static void
188 usage(void)
189 {
190 
191 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
192 		"usage: kill [-s signal_name] pid ...",
193 		"       kill -l [exit_status]",
194 		"       kill -signal_name pid ...",
195 		"       kill -signal_number pid ...");
196 #ifdef SHELL
197 	error(NULL);
198 #else
199 	exit(2);
200 #endif
201 }
202