xref: /minix3/bin/kill/kill.c (revision 7104bb23a94481b4b80bbd3e3a96e04282159826)
1*7104bb23SLionel Sambuc /* $NetBSD: kill.c,v 1.27 2011/08/29 14:51:18 joerg Exp $ */
2*7104bb23SLionel Sambuc 
3*7104bb23SLionel Sambuc /*
4*7104bb23SLionel Sambuc  * Copyright (c) 1988, 1993, 1994
5*7104bb23SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*7104bb23SLionel Sambuc  *
7*7104bb23SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*7104bb23SLionel Sambuc  * modification, are permitted provided that the following conditions
9*7104bb23SLionel Sambuc  * are met:
10*7104bb23SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*7104bb23SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*7104bb23SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*7104bb23SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*7104bb23SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*7104bb23SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
16*7104bb23SLionel Sambuc  *    may be used to endorse or promote products derived from this software
17*7104bb23SLionel Sambuc  *    without specific prior written permission.
18*7104bb23SLionel Sambuc  *
19*7104bb23SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*7104bb23SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*7104bb23SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*7104bb23SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*7104bb23SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*7104bb23SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*7104bb23SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*7104bb23SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*7104bb23SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*7104bb23SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*7104bb23SLionel Sambuc  * SUCH DAMAGE.
30*7104bb23SLionel Sambuc  */
31*7104bb23SLionel Sambuc 
32*7104bb23SLionel Sambuc #include <sys/cdefs.h>
33*7104bb23SLionel Sambuc #if !defined(lint) && !defined(SHELL)
34*7104bb23SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
35*7104bb23SLionel Sambuc  The Regents of the University of California.  All rights reserved.");
36*7104bb23SLionel Sambuc #endif /* not lint */
37*7104bb23SLionel Sambuc 
38*7104bb23SLionel Sambuc #ifndef lint
39*7104bb23SLionel Sambuc #if 0
40*7104bb23SLionel Sambuc static char sccsid[] = "@(#)kill.c	8.4 (Berkeley) 4/28/95";
41*7104bb23SLionel Sambuc #else
42*7104bb23SLionel Sambuc __RCSID("$NetBSD: kill.c,v 1.27 2011/08/29 14:51:18 joerg Exp $");
43*7104bb23SLionel Sambuc #endif
44*7104bb23SLionel Sambuc #endif /* not lint */
45*7104bb23SLionel Sambuc 
46*7104bb23SLionel Sambuc #include <ctype.h>
47*7104bb23SLionel Sambuc #include <err.h>
48*7104bb23SLionel Sambuc #include <errno.h>
49*7104bb23SLionel Sambuc #include <signal.h>
50*7104bb23SLionel Sambuc #include <stdio.h>
51*7104bb23SLionel Sambuc #include <stdlib.h>
52*7104bb23SLionel Sambuc #include <limits.h>
53*7104bb23SLionel Sambuc #include <inttypes.h>
54*7104bb23SLionel Sambuc #include <string.h>
55*7104bb23SLionel Sambuc #include <termios.h>
56*7104bb23SLionel Sambuc #include <unistd.h>
57*7104bb23SLionel Sambuc #include <locale.h>
58*7104bb23SLionel Sambuc #include <sys/ioctl.h>
59*7104bb23SLionel Sambuc 
60*7104bb23SLionel Sambuc #ifdef SHELL            /* sh (aka ash) builtin */
61*7104bb23SLionel Sambuc int killcmd(int, char *argv[]);
62*7104bb23SLionel Sambuc #define main killcmd
63*7104bb23SLionel Sambuc #include "../../bin/sh/bltin/bltin.h"
64*7104bb23SLionel Sambuc #endif /* SHELL */
65*7104bb23SLionel Sambuc 
66*7104bb23SLionel Sambuc __dead static void nosig(char *);
67*7104bb23SLionel Sambuc static void printsignals(FILE *);
68*7104bb23SLionel Sambuc static int signame_to_signum(char *);
69*7104bb23SLionel Sambuc __dead static void usage(void);
70*7104bb23SLionel Sambuc 
71*7104bb23SLionel Sambuc int
main(int argc,char * argv[])72*7104bb23SLionel Sambuc main(int argc, char *argv[])
73*7104bb23SLionel Sambuc {
74*7104bb23SLionel Sambuc 	int errors;
75*7104bb23SLionel Sambuc 	intmax_t numsig, pid;
76*7104bb23SLionel Sambuc 	char *ep;
77*7104bb23SLionel Sambuc 
78*7104bb23SLionel Sambuc 	setprogname(argv[0]);
79*7104bb23SLionel Sambuc 	setlocale(LC_ALL, "");
80*7104bb23SLionel Sambuc 	if (argc < 2)
81*7104bb23SLionel Sambuc 		usage();
82*7104bb23SLionel Sambuc 
83*7104bb23SLionel Sambuc 	numsig = SIGTERM;
84*7104bb23SLionel Sambuc 
85*7104bb23SLionel Sambuc 	argc--, argv++;
86*7104bb23SLionel Sambuc 	if (strcmp(*argv, "-l") == 0) {
87*7104bb23SLionel Sambuc 		argc--, argv++;
88*7104bb23SLionel Sambuc 		if (argc > 1)
89*7104bb23SLionel Sambuc 			usage();
90*7104bb23SLionel Sambuc 		if (argc == 1) {
91*7104bb23SLionel Sambuc 			if (isdigit((unsigned char)**argv) == 0)
92*7104bb23SLionel Sambuc 				usage();
93*7104bb23SLionel Sambuc 			numsig = strtoimax(*argv, &ep, 10);
94*7104bb23SLionel Sambuc 			/* check for correctly parsed number */
95*7104bb23SLionel Sambuc 			if (*ep != '\0' || numsig == INTMAX_MIN || numsig == INTMAX_MAX) {
96*7104bb23SLionel Sambuc 				errx(EXIT_FAILURE, "illegal signal number: %s",
97*7104bb23SLionel Sambuc 						*argv);
98*7104bb23SLionel Sambuc 				/* NOTREACHED */
99*7104bb23SLionel Sambuc 			}
100*7104bb23SLionel Sambuc 			if (numsig >= 128)
101*7104bb23SLionel Sambuc 				numsig -= 128;
102*7104bb23SLionel Sambuc 			/* and whether it fits into signals range */
103*7104bb23SLionel Sambuc 			if (numsig <= 0 || numsig >= NSIG)
104*7104bb23SLionel Sambuc 				nosig(*argv);
105*7104bb23SLionel Sambuc 			printf("%s\n", sys_signame[(int) numsig]);
106*7104bb23SLionel Sambuc 			exit(0);
107*7104bb23SLionel Sambuc 		}
108*7104bb23SLionel Sambuc 		printsignals(stdout);
109*7104bb23SLionel Sambuc 		exit(0);
110*7104bb23SLionel Sambuc 	}
111*7104bb23SLionel Sambuc 
112*7104bb23SLionel Sambuc 	if (!strcmp(*argv, "-s")) {
113*7104bb23SLionel Sambuc 		argc--, argv++;
114*7104bb23SLionel Sambuc 		if (argc < 1) {
115*7104bb23SLionel Sambuc 			warnx("option requires an argument -- s");
116*7104bb23SLionel Sambuc 			usage();
117*7104bb23SLionel Sambuc 		}
118*7104bb23SLionel Sambuc 		if (strcmp(*argv, "0")) {
119*7104bb23SLionel Sambuc 			if ((numsig = signame_to_signum(*argv)) < 0)
120*7104bb23SLionel Sambuc 				nosig(*argv);
121*7104bb23SLionel Sambuc 		} else
122*7104bb23SLionel Sambuc 			numsig = 0;
123*7104bb23SLionel Sambuc 		argc--, argv++;
124*7104bb23SLionel Sambuc 	} else if (**argv == '-') {
125*7104bb23SLionel Sambuc 		char *sn = *argv + 1;
126*7104bb23SLionel Sambuc 		if (isalpha((unsigned char)*sn)) {
127*7104bb23SLionel Sambuc 			if ((numsig = signame_to_signum(sn)) < 0)
128*7104bb23SLionel Sambuc 				nosig(sn);
129*7104bb23SLionel Sambuc 		} else if (isdigit((unsigned char)*sn)) {
130*7104bb23SLionel Sambuc 			numsig = strtoimax(sn, &ep, 10);
131*7104bb23SLionel Sambuc 			/* check for correctly parsed number */
132*7104bb23SLionel Sambuc 			if (*ep || numsig == INTMAX_MIN || numsig == INTMAX_MAX ) {
133*7104bb23SLionel Sambuc 				errx(EXIT_FAILURE, "illegal signal number: %s",
134*7104bb23SLionel Sambuc 						sn);
135*7104bb23SLionel Sambuc 				/* NOTREACHED */
136*7104bb23SLionel Sambuc 			}
137*7104bb23SLionel Sambuc 			/* and whether it fits into signals range */
138*7104bb23SLionel Sambuc 			if (numsig < 0 || numsig >= NSIG)
139*7104bb23SLionel Sambuc 				nosig(sn);
140*7104bb23SLionel Sambuc 		} else
141*7104bb23SLionel Sambuc 			nosig(sn);
142*7104bb23SLionel Sambuc 		argc--, argv++;
143*7104bb23SLionel Sambuc 	}
144*7104bb23SLionel Sambuc 
145*7104bb23SLionel Sambuc 	if (argc == 0)
146*7104bb23SLionel Sambuc 		usage();
147*7104bb23SLionel Sambuc 
148*7104bb23SLionel Sambuc 	for (errors = 0; argc; argc--, argv++) {
149*7104bb23SLionel Sambuc #ifdef SHELL
150*7104bb23SLionel Sambuc 		extern int getjobpgrp(const char *);
151*7104bb23SLionel Sambuc 		if (*argv[0] == '%') {
152*7104bb23SLionel Sambuc 			pid = getjobpgrp(*argv);
153*7104bb23SLionel Sambuc 			if (pid == 0) {
154*7104bb23SLionel Sambuc 				warnx("illegal job id: %s", *argv);
155*7104bb23SLionel Sambuc 				errors = 1;
156*7104bb23SLionel Sambuc 				continue;
157*7104bb23SLionel Sambuc 			}
158*7104bb23SLionel Sambuc 		} else
159*7104bb23SLionel Sambuc #endif
160*7104bb23SLionel Sambuc 		{
161*7104bb23SLionel Sambuc 			pid = strtoimax(*argv, &ep, 10);
162*7104bb23SLionel Sambuc 			/* make sure the pid is a number and fits into pid_t */
163*7104bb23SLionel Sambuc 			if (!**argv || *ep || pid == INTMAX_MIN ||
164*7104bb23SLionel Sambuc 				pid == INTMAX_MAX || pid != (pid_t) pid) {
165*7104bb23SLionel Sambuc 
166*7104bb23SLionel Sambuc 				warnx("illegal process id: %s", *argv);
167*7104bb23SLionel Sambuc 				errors = 1;
168*7104bb23SLionel Sambuc 				continue;
169*7104bb23SLionel Sambuc 			}
170*7104bb23SLionel Sambuc 		}
171*7104bb23SLionel Sambuc 		if (kill((pid_t) pid, (int) numsig) == -1) {
172*7104bb23SLionel Sambuc 			warn("%s", *argv);
173*7104bb23SLionel Sambuc 			errors = 1;
174*7104bb23SLionel Sambuc 		}
175*7104bb23SLionel Sambuc #ifdef SHELL
176*7104bb23SLionel Sambuc 		/* Wakeup the process if it was suspended, so it can
177*7104bb23SLionel Sambuc 		   exit without an explicit 'fg'. */
178*7104bb23SLionel Sambuc 		if (numsig == SIGTERM || numsig == SIGHUP)
179*7104bb23SLionel Sambuc 			kill((pid_t) pid, SIGCONT);
180*7104bb23SLionel Sambuc #endif
181*7104bb23SLionel Sambuc 	}
182*7104bb23SLionel Sambuc 
183*7104bb23SLionel Sambuc 	exit(errors);
184*7104bb23SLionel Sambuc 	/* NOTREACHED */
185*7104bb23SLionel Sambuc }
186*7104bb23SLionel Sambuc 
187*7104bb23SLionel Sambuc static int
signame_to_signum(char * sig)188*7104bb23SLionel Sambuc signame_to_signum(char *sig)
189*7104bb23SLionel Sambuc {
190*7104bb23SLionel Sambuc 	int n;
191*7104bb23SLionel Sambuc 
192*7104bb23SLionel Sambuc 	if (strncasecmp(sig, "sig", 3) == 0)
193*7104bb23SLionel Sambuc 		sig += 3;
194*7104bb23SLionel Sambuc 	for (n = 1; n < NSIG; n++) {
195*7104bb23SLionel Sambuc 		if (!strcasecmp(sys_signame[n], sig))
196*7104bb23SLionel Sambuc 			return (n);
197*7104bb23SLionel Sambuc 	}
198*7104bb23SLionel Sambuc 	return (-1);
199*7104bb23SLionel Sambuc }
200*7104bb23SLionel Sambuc 
201*7104bb23SLionel Sambuc static void
nosig(char * name)202*7104bb23SLionel Sambuc nosig(char *name)
203*7104bb23SLionel Sambuc {
204*7104bb23SLionel Sambuc 
205*7104bb23SLionel Sambuc 	warnx("unknown signal %s; valid signals:", name);
206*7104bb23SLionel Sambuc 	printsignals(stderr);
207*7104bb23SLionel Sambuc 	exit(1);
208*7104bb23SLionel Sambuc 	/* NOTREACHED */
209*7104bb23SLionel Sambuc }
210*7104bb23SLionel Sambuc 
211*7104bb23SLionel Sambuc static void
printsignals(FILE * fp)212*7104bb23SLionel Sambuc printsignals(FILE *fp)
213*7104bb23SLionel Sambuc {
214*7104bb23SLionel Sambuc 	int sig;
215*7104bb23SLionel Sambuc 	int len, nl;
216*7104bb23SLionel Sambuc 	const char *name;
217*7104bb23SLionel Sambuc 	int termwidth = 80;
218*7104bb23SLionel Sambuc 
219*7104bb23SLionel Sambuc 	if (isatty(fileno(fp))) {
220*7104bb23SLionel Sambuc 		struct winsize win;
221*7104bb23SLionel Sambuc 		if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
222*7104bb23SLionel Sambuc 			termwidth = win.ws_col;
223*7104bb23SLionel Sambuc 	}
224*7104bb23SLionel Sambuc 
225*7104bb23SLionel Sambuc 	for (len = 0, sig = 1; sig < NSIG; sig++) {
226*7104bb23SLionel Sambuc 		name = sys_signame[sig];
227*7104bb23SLionel Sambuc 		nl = 1 + strlen(name);
228*7104bb23SLionel Sambuc 
229*7104bb23SLionel Sambuc 		if (len + nl >= termwidth) {
230*7104bb23SLionel Sambuc 			fprintf(fp, "\n");
231*7104bb23SLionel Sambuc 			len = 0;
232*7104bb23SLionel Sambuc 		} else
233*7104bb23SLionel Sambuc 			if (len != 0)
234*7104bb23SLionel Sambuc 				fprintf(fp, " ");
235*7104bb23SLionel Sambuc 		len += nl;
236*7104bb23SLionel Sambuc 		fprintf(fp, "%s", name);
237*7104bb23SLionel Sambuc 	}
238*7104bb23SLionel Sambuc 	if (len != 0)
239*7104bb23SLionel Sambuc 		fprintf(fp, "\n");
240*7104bb23SLionel Sambuc }
241*7104bb23SLionel Sambuc 
242*7104bb23SLionel Sambuc static void
usage(void)243*7104bb23SLionel Sambuc usage(void)
244*7104bb23SLionel Sambuc {
245*7104bb23SLionel Sambuc 
246*7104bb23SLionel Sambuc 	fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
247*7104bb23SLionel Sambuc 	    "       %s -l [exit_status]\n"
248*7104bb23SLionel Sambuc 	    "       %s -signal_name pid ...\n"
249*7104bb23SLionel Sambuc 	    "       %s -signal_number pid ...\n",
250*7104bb23SLionel Sambuc 	    getprogname(), getprogname(), getprogname(), getprogname());
251*7104bb23SLionel Sambuc 	exit(1);
252*7104bb23SLionel Sambuc 	/* NOTREACHED */
253*7104bb23SLionel Sambuc }
254