kill.c revision 26465
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1988, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
323044Sdg *
3326465Scharnier *	$Id: kill.c,v 1.7 1997/06/03 06:24:50 charnier Exp $
341556Srgrimes */
351556Srgrimes
361556Srgrimes#ifndef lint
3720416Sstevestatic char const copyright[] =
381556Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\
391556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
401556Srgrimes#endif /* not lint */
411556Srgrimes
421556Srgrimes#ifndef lint
4320416Sstevestatic char const sccsid[] = "@(#)kill.c	8.4 (Berkeley) 4/28/95";
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <ctype.h>
471556Srgrimes#include <err.h>
481556Srgrimes#include <errno.h>
491556Srgrimes#include <signal.h>
501556Srgrimes#include <stdio.h>
511556Srgrimes#include <stdlib.h>
521556Srgrimes#include <string.h>
531556Srgrimes
541556Srgrimesvoid nosig __P((char *));
5520416Sstevevoid printsignals __P((FILE *));
5620416Ssteveint signame_to_signum __P((char *));
571556Srgrimesvoid usage __P((void));
581556Srgrimes
591556Srgrimesint
601556Srgrimesmain(argc, argv)
611556Srgrimes	int argc;
621556Srgrimes	char *argv[];
631556Srgrimes{
641556Srgrimes	int errors, numsig, pid;
651556Srgrimes	char *ep;
661556Srgrimes
671556Srgrimes	if (argc < 2)
681556Srgrimes		usage();
691556Srgrimes
7020416Ssteve	numsig = SIGTERM;
7120416Ssteve
7220416Ssteve	argc--, argv++;
7320416Ssteve	if (!strcmp(*argv, "-l")) {
7420416Ssteve		argc--, argv++;
7520416Ssteve		if (argc > 1)
7620416Ssteve			usage();
7720416Ssteve		if (argc == 1) {
7820416Ssteve			if (!isdigit(**argv))
7920416Ssteve				usage();
8020416Ssteve			numsig = strtol(*argv, &ep, 10);
8120416Ssteve			if (!*argv || *ep)
8220416Ssteve				errx(1, "illegal signal number: %s", *argv);
8320416Ssteve			if (numsig >= 128)
8420416Ssteve				numsig -= 128;
8520416Ssteve			if (numsig <= 0 || numsig >= NSIG)
8620416Ssteve				nosig(*argv);
8720416Ssteve			printf("%s\n", sys_signame[numsig]);
8820416Ssteve			exit(0);
8920416Ssteve		}
9020416Ssteve		printsignals(stdout);
911556Srgrimes		exit(0);
921556Srgrimes	}
931556Srgrimes
9420416Ssteve	if (!strcmp(*argv, "-s")) {
9520416Ssteve		argc--, argv++;
9620416Ssteve		if (argc < 1) {
9720416Ssteve			warnx("option requires an argument -- s");
9820416Ssteve			usage();
9920416Ssteve		}
10020416Ssteve		if (strcmp(*argv, "0")) {
10120416Ssteve			if ((numsig = signame_to_signum(*argv)) < 0)
10220416Ssteve				nosig(*argv);
10320416Ssteve		} else
10420416Ssteve			numsig = 0;
10520416Ssteve		argc--, argv++;
10620416Ssteve	} else if (**argv == '-') {
1071556Srgrimes		++*argv;
1081556Srgrimes		if (isalpha(**argv)) {
10920416Ssteve			if ((numsig = signame_to_signum(*argv)) < 0)
1101556Srgrimes				nosig(*argv);
1111556Srgrimes		} else if (isdigit(**argv)) {
1121556Srgrimes			numsig = strtol(*argv, &ep, 10);
1131556Srgrimes			if (!*argv || *ep)
1141556Srgrimes				errx(1, "illegal signal number: %s", *argv);
11526404Scharnier			if (numsig < 0 || numsig >= NSIG)
1161556Srgrimes				nosig(*argv);
1171556Srgrimes		} else
1181556Srgrimes			nosig(*argv);
11920416Ssteve		argc--, argv++;
1201556Srgrimes	}
1211556Srgrimes
12220416Ssteve	if (argc == 0)
1231556Srgrimes		usage();
1241556Srgrimes
12520416Ssteve	for (errors = 0; argc; argc--, argv++) {
1261556Srgrimes		pid = strtol(*argv, &ep, 10);
1271556Srgrimes		if (!*argv || *ep) {
1281556Srgrimes			warnx("illegal process id: %s", *argv);
1291556Srgrimes			errors = 1;
1301556Srgrimes		} else if (kill(pid, numsig) == -1) {
1311556Srgrimes			warn("%s", *argv);
1321556Srgrimes			errors = 1;
1331556Srgrimes		}
1341556Srgrimes	}
13520416Ssteve
1361556Srgrimes	exit(errors);
1371556Srgrimes}
1381556Srgrimes
13920416Ssteveint
14020416Sstevesigname_to_signum(sig)
14120416Ssteve	char *sig;
14220416Ssteve{
14320416Ssteve	int n;
14420416Ssteve
14520416Ssteve	if (!strncasecmp(sig, "sig", 3))
14620416Ssteve		sig += 3;
14720416Ssteve	for (n = 1; n < NSIG; n++) {
14820416Ssteve		if (!strcasecmp(sys_signame[n], sig))
14920416Ssteve			return (n);
15020416Ssteve	}
15120416Ssteve	return (-1);
15220416Ssteve}
15320416Ssteve
1541556Srgrimesvoid
1551556Srgrimesnosig(name)
1561556Srgrimes	char *name;
1571556Srgrimes{
1581556Srgrimes
1591556Srgrimes	warnx("unknown signal %s; valid signals:", name);
16020416Ssteve	printsignals(stderr);
1611556Srgrimes	exit(1);
1621556Srgrimes}
1631556Srgrimes
1641556Srgrimesvoid
16520416Ssteveprintsignals(fp)
1661556Srgrimes	FILE *fp;
1671556Srgrimes{
16820416Ssteve	int n;
1691556Srgrimes
17020416Ssteve	for (n = 1; n < NSIG; n++) {
17120416Ssteve		(void)fprintf(fp, "%s", sys_signame[n]);
17220416Ssteve		if (n == (NSIG / 2) || n == (NSIG - 1))
1731556Srgrimes			(void)fprintf(fp, "\n");
17420416Ssteve		else
17520416Ssteve			(void)fprintf(fp, " ");
1761556Srgrimes	}
1771556Srgrimes}
1781556Srgrimes
1791556Srgrimesvoid
1801556Srgrimesusage()
1811556Srgrimes{
1821556Srgrimes
18326465Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
18426465Scharnier		"usage: kill [-s signal_name] pid ...",
18526465Scharnier		"       kill -l [exit_status]",
18626465Scharnier		"       kill -signal_name pid ...",
18726465Scharnier		"       kill -signal_number pid ...");
1881556Srgrimes	exit(1);
1891556Srgrimes}
190