kill.c revision 1556
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.
321556Srgrimes */
331556Srgrimes
341556Srgrimes#ifndef lint
351556Srgrimesstatic char copyright[] =
361556Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#ifndef lint
411556Srgrimesstatic char sccsid[] = "@(#)kill.c	8.3 (Berkeley) 4/2/94";
421556Srgrimes#endif /* not lint */
431556Srgrimes
441556Srgrimes#include <ctype.h>
451556Srgrimes#include <err.h>
461556Srgrimes#include <errno.h>
471556Srgrimes#include <signal.h>
481556Srgrimes#include <stdio.h>
491556Srgrimes#include <stdlib.h>
501556Srgrimes#include <string.h>
511556Srgrimes
521556Srgrimesvoid nosig __P((char *));
531556Srgrimesvoid printsig __P((FILE *));
541556Srgrimesvoid usage __P((void));
551556Srgrimes
561556Srgrimesint
571556Srgrimesmain(argc, argv)
581556Srgrimes	int argc;
591556Srgrimes	char *argv[];
601556Srgrimes{
611556Srgrimes	const char *const *p;
621556Srgrimes	int errors, numsig, pid;
631556Srgrimes	char *ep;
641556Srgrimes
651556Srgrimes	if (argc < 2)
661556Srgrimes		usage();
671556Srgrimes
681556Srgrimes	if (!strcmp(*++argv, "-l")) {
691556Srgrimes		printsig(stdout);
701556Srgrimes		exit(0);
711556Srgrimes	}
721556Srgrimes
731556Srgrimes	numsig = SIGTERM;
741556Srgrimes	if (**argv == '-') {
751556Srgrimes		++*argv;
761556Srgrimes		if (isalpha(**argv)) {
771556Srgrimes			if (!strncasecmp(*argv, "sig", 3))
781556Srgrimes				*argv += 3;
791556Srgrimes			for (numsig = NSIG, p = sys_signame + 1; --numsig; ++p)
801556Srgrimes				if (!strcasecmp(*p, *argv)) {
811556Srgrimes					numsig = p - sys_signame;
821556Srgrimes					break;
831556Srgrimes				}
841556Srgrimes			if (!numsig)
851556Srgrimes				nosig(*argv);
861556Srgrimes		} else if (isdigit(**argv)) {
871556Srgrimes			numsig = strtol(*argv, &ep, 10);
881556Srgrimes			if (!*argv || *ep)
891556Srgrimes				errx(1, "illegal signal number: %s", *argv);
901556Srgrimes			if (numsig <= 0 || numsig > NSIG)
911556Srgrimes				nosig(*argv);
921556Srgrimes		} else
931556Srgrimes			nosig(*argv);
941556Srgrimes		++argv;
951556Srgrimes	}
961556Srgrimes
971556Srgrimes	if (!*argv)
981556Srgrimes		usage();
991556Srgrimes
1001556Srgrimes	for (errors = 0; *argv; ++argv) {
1011556Srgrimes		pid = strtol(*argv, &ep, 10);
1021556Srgrimes		if (!*argv || *ep) {
1031556Srgrimes			warnx("illegal process id: %s", *argv);
1041556Srgrimes			errors = 1;
1051556Srgrimes		} else if (kill(pid, numsig) == -1) {
1061556Srgrimes			warn("%s", *argv);
1071556Srgrimes			errors = 1;
1081556Srgrimes		}
1091556Srgrimes	}
1101556Srgrimes	exit(errors);
1111556Srgrimes}
1121556Srgrimes
1131556Srgrimesvoid
1141556Srgrimesnosig(name)
1151556Srgrimes	char *name;
1161556Srgrimes{
1171556Srgrimes
1181556Srgrimes	warnx("unknown signal %s; valid signals:", name);
1191556Srgrimes	printsig(stderr);
1201556Srgrimes	exit(1);
1211556Srgrimes}
1221556Srgrimes
1231556Srgrimesvoid
1241556Srgrimesprintsig(fp)
1251556Srgrimes	FILE *fp;
1261556Srgrimes{
1271556Srgrimes	const char *const *p;
1281556Srgrimes	int cnt;
1291556Srgrimes
1301556Srgrimes	for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
1311556Srgrimes		(void)fprintf(fp, "%s ", *p);
1321556Srgrimes		if (cnt == NSIG / 2)
1331556Srgrimes			(void)fprintf(fp, "\n");
1341556Srgrimes	}
1351556Srgrimes	(void)fprintf(fp, "\n");
1361556Srgrimes}
1371556Srgrimes
1381556Srgrimesvoid
1391556Srgrimesusage()
1401556Srgrimes{
1411556Srgrimes
1421556Srgrimes	(void)fprintf(stderr, "usage: kill [-l] [-sig] pid ...\n");
1431556Srgrimes	exit(1);
1441556Srgrimes}
145