xref: /dragonfly/bin/kill/kill.c (revision dff616f7)
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  * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California.  All rights reserved.
32  * @(#)kill.c	8.4 (Berkeley) 4/28/95
33  * $FreeBSD: src/bin/kill/kill.c,v 1.24 2011/02/04 16:40:50 jilles Exp $
34  */
35 /*
36  * Important: This file is used both as a standalone program /bin/kill and
37  * as a builtin for /bin/sh (#define SHELL).
38  */
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #ifdef SHELL
49 #define main killcmd
50 #include "bltin/bltin.h"
51 #include "error.h"
52 #endif
53 
54 static void	nosig(const char *);
55 static void	printsignals(FILE *);
56 static int	signame_to_signum(const char *);
57 static void	usage(void);
58 
59 int
60 main(int argc, char **argv)
61 {
62 	long pidl;
63 	pid_t pid;
64 	int errors, numsig, ret;
65 	char *ep;
66 
67 	if (argc < 2)
68 		usage();
69 
70 	numsig = SIGTERM;
71 
72 	argc--, argv++;
73 	if (strcmp(*argv, "-l") == 0) {
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 == '\0' || *ep != '\0')
82 				errx(2, "illegal signal number: %s", *argv);
83 			if (numsig >= 128)
84 				numsig -= 128;
85 			if (numsig <= 0 || numsig >= sys_nsig)
86 				nosig(*argv);
87 			printf("%s\n", sys_signame[numsig]);
88 			return (0);
89 		}
90 		printsignals(stdout);
91 		return (0);
92 	}
93 
94 	if (strcmp(*argv, "-s") == 0) {
95 		argc--, argv++;
96 		if (argc < 1) {
97 			warnx("option requires an argument -- s");
98 			usage();
99 		}
100 		if (strcmp(*argv, "0") != 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 == '\0' || *ep != '\0')
114 				errx(2, "illegal signal number: %s", *argv);
115 			if (numsig < 0)
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 #ifdef SHELL
130 		if (**argv == '%')
131 			ret = killjob(*argv, numsig);
132 		else
133 #endif
134 		{
135 			pidl = strtol(*argv, &ep, 10);
136 			/* Check for overflow of pid_t. */
137 			pid = (pid_t)pidl;
138 			if (!**argv || *ep || pid != pidl)
139 				errx(2, "illegal process id: %s", *argv);
140 			ret = kill(pid, numsig);
141 		}
142 		if (ret == -1) {
143 			warn("%s", *argv);
144 			errors = 1;
145 		}
146 	}
147 
148 	return (errors);
149 }
150 
151 static int
152 signame_to_signum(const char *sig)
153 {
154 	int n;
155 
156 	if (strncasecmp(sig, "SIG", 3) == 0)
157 		sig += 3;
158 	for (n = 1; n < sys_nsig; n++) {
159 		if (strcasecmp(sys_signame[n], sig) == 0)
160 			return(n);
161 	}
162 	return (-1);
163 }
164 
165 static void
166 nosig(const char *name)
167 {
168 	warnx("unknown signal %s; valid signals:", name);
169 	printsignals(stderr);
170 #ifdef SHELL
171 	error(NULL);
172 #else
173 	exit(2);
174 #endif
175 }
176 
177 static void
178 printsignals(FILE *fp)
179 {
180 	int n;
181 
182 	for (n = 1; n < sys_nsig; n++) {
183 		fprintf(fp, "%s", sys_signame[n]);
184 		if (n == (sys_nsig / 2) || n == (sys_nsig - 1))
185 			fprintf(fp, "\n");
186 		else
187 			fprintf(fp, " ");
188 	}
189 }
190 
191 static void
192 usage(void)
193 {
194 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
195 		"usage: kill [-s signal_name] pid ...",
196 		"       kill -l [exit_status]",
197 		"       kill -signal_name pid ...",
198 		"       kill -signal_number pid ...");
199 #ifdef SHELL
200 	error(NULL);
201 #else
202 	exit(2);
203 #endif
204 }
205