kill.c revision 127005
1229997Sken/*
2229997Sken * Copyright (c) 1988, 1993, 1994
3290776Smav *	The Regents of the University of California.  All rights reserved.
4229997Sken *
5229997Sken * Redistribution and use in source and binary forms, with or without
6229997Sken * modification, are permitted provided that the following conditions
7229997Sken * are met:
8229997Sken * 1. Redistributions of source code must retain the above copyright
9229997Sken *    notice, this list of conditions and the following disclaimer.
10229997Sken * 2. Redistributions in binary form must reproduce the above copyright
11229997Sken *    notice, this list of conditions and the following disclaimer in the
12229997Sken *    documentation and/or other materials provided with the distribution.
13229997Sken * 3. All advertising materials mentioning features or use of this software
14229997Sken *    must display the following acknowledgement:
15229997Sken *	This product includes software developed by the University of
16229997Sken *	California, Berkeley and its contributors.
17229997Sken * 4. Neither the name of the University nor the names of its contributors
18229997Sken *    may be used to endorse or promote products derived from this software
19229997Sken *    without specific prior written permission.
20229997Sken *
21229997Sken * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22229997Sken * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23229997Sken * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24229997Sken * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25229997Sken * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29229997Sken * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30229997Sken * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31229997Sken * SUCH DAMAGE.
32229997Sken */
33229997Sken
34229997Sken#if 0
35229997Sken#ifndef lint
36229997Skenstatic char const copyright[] =
37229997Sken"@(#) Copyright (c) 1988, 1993, 1994\n\
38229997Sken	The Regents of the University of California.  All rights reserved.\n";
39229997Sken#endif /* not lint */
40229997Sken
41229997Sken#ifndef lint
42229997Skenstatic char sccsid[] = "@(#)kill.c	8.4 (Berkeley) 4/28/95";
43229997Sken#endif /* not lint */
44229997Sken#endif
45229997Sken#include <sys/cdefs.h>
46229997Sken__FBSDID("$FreeBSD: head/bin/kill/kill.c 127005 2004-03-15 04:10:29Z jmallett $");
47229997Sken
48229997Sken#include <ctype.h>
49229997Sken#include <err.h>
50229997Sken#include <errno.h>
51229997Sken#include <signal.h>
52229997Sken#include <stdio.h>
53229997Sken#include <stdlib.h>
54229997Sken#include <string.h>
55229997Sken
56229997Skenstatic void nosig(const char *);
57229997Skenstatic void printsignals(FILE *);
58229997Skenstatic int signame_to_signum(const char *);
59229997Skenstatic void usage(void);
60229997Sken
61229997Skenint
62229997Skenmain(int argc, char *argv[])
63229997Sken{
64229997Sken	int errors, numsig, pid;
65229997Sken	char *ep;
66229997Sken
67229997Sken	if (argc < 2)
68229997Sken		usage();
69229997Sken
70229997Sken	numsig = SIGTERM;
71229997Sken
72229997Sken	argc--, argv++;
73229997Sken	if (!strcmp(*argv, "-l")) {
74229997Sken		argc--, argv++;
75229997Sken		if (argc > 1)
76229997Sken			usage();
77290776Smav		if (argc == 1) {
78290776Smav			if (!isdigit(**argv))
79290776Smav				usage();
80290776Smav			numsig = strtol(*argv, &ep, 10);
81290776Smav			if (!**argv || *ep)
82229997Sken				errx(1, "illegal signal number: %s", *argv);
83290776Smav			if (numsig >= 128)
84290776Smav				numsig -= 128;
85229997Sken			if (numsig <= 0 || numsig >= sys_nsig)
86229997Sken				nosig(*argv);
87229997Sken			printf("%s\n", sys_signame[numsig]);
88229997Sken			exit(0);
89229997Sken		}
90229997Sken		printsignals(stdout);
91229997Sken		exit(0);
92229997Sken	}
93229997Sken
94229997Sken	if (!strcmp(*argv, "-s")) {
95229997Sken		argc--, argv++;
96229997Sken		if (argc < 1) {
97229997Sken			warnx("option requires an argument -- s");
98229997Sken			usage();
99229997Sken		}
100229997Sken		if (strcmp(*argv, "0")) {
101229997Sken			if ((numsig = signame_to_signum(*argv)) < 0)
102229997Sken				nosig(*argv);
103229997Sken		} else
104229997Sken			numsig = 0;
105229997Sken		argc--, argv++;
106229997Sken	} else if (**argv == '-' && *(*argv + 1) != '-') {
107229997Sken		++*argv;
108229997Sken		if (isalpha(**argv)) {
109229997Sken			if ((numsig = signame_to_signum(*argv)) < 0)
110284793Smav				nosig(*argv);
111284793Smav		} else if (isdigit(**argv)) {
112284793Smav			numsig = strtol(*argv, &ep, 10);
113284793Smav			if (!**argv || *ep)
114229997Sken				errx(1, "illegal signal number: %s", *argv);
115229997Sken			if (numsig < 0 || numsig >= sys_nsig)
116229997Sken				nosig(*argv);
117229997Sken		} else
118229997Sken			nosig(*argv);
119229997Sken		argc--, argv++;
120229997Sken	}
121229997Sken
122229997Sken	if (argc > 0 && strncmp(*argv, "--", 2) == 0)
123229997Sken		argc--, argv++;
124288723Smav
125229997Sken	if (argc == 0)
126265641Smav		usage();
127229997Sken
128229997Sken	for (errors = 0; argc; argc--, argv++) {
129229997Sken		pid = strtol(*argv, &ep, 10);
130229997Sken		if (!**argv || *ep) {
131229997Sken			warnx("illegal process id: %s", *argv);
132229997Sken			errors = 1;
133265641Smav		} else if (kill(pid, numsig) == -1) {
134265641Smav			warn("%s", *argv);
135229997Sken			errors = 1;
136229997Sken		}
137229997Sken	}
138229997Sken
139229997Sken	exit(errors);
140229997Sken}
141229997Sken
142229997Skenstatic int
143229997Skensigname_to_signum(const char *sig)
144229997Sken{
145229997Sken	int n;
146229997Sken
147229997Sken	if (!strncasecmp(sig, "sig", (size_t)3))
148229997Sken		sig += 3;
149229997Sken	for (n = 1; n < sys_nsig; n++) {
150229997Sken		if (!strcasecmp(sys_signame[n], sig))
151229997Sken			return (n);
152229997Sken	}
153229997Sken	return (-1);
154229997Sken}
155229997Sken
156229997Skenstatic void
157229997Skennosig(const char *name)
158229997Sken{
159229997Sken
160229997Sken	warnx("unknown signal %s; valid signals:", name);
161229997Sken	printsignals(stderr);
162229997Sken	exit(1);
163229997Sken}
164229997Sken
165229997Skenstatic void
166229997Skenprintsignals(FILE *fp)
167229997Sken{
168229997Sken	int n;
169229997Sken
170229997Sken	for (n = 1; n < sys_nsig; n++) {
171229997Sken		(void)fprintf(fp, "%s", sys_signame[n]);
172229997Sken		if (n == (sys_nsig / 2) || n == (sys_nsig - 1))
173229997Sken			(void)fprintf(fp, "\n");
174229997Sken		else
175229997Sken			(void)fprintf(fp, " ");
176229997Sken	}
177229997Sken}
178229997Sken
179229997Skenstatic void
180229997Skenusage(void)
181229997Sken{
182229997Sken
183275878Smav	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
184229997Sken		"usage: kill [-s signal_name] pid ...",
185229997Sken		"       kill -l [exit_status]",
186229997Sken		"       kill -signal_name pid ...",
187229997Sken		"       kill -signal_number pid ...");
188229997Sken	exit(1);
189229997Sken}
190268677Smav