Home | History | Annotate | Line # | Download | only in kill
kill.c revision 1.18
      1 /*	$NetBSD: kill.c,v 1.18 2001/07/29 22:46:36 wiz Exp $	*/
      2 
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)kill.c	8.4 (Berkeley) 4/28/95";
     45 #else
     46 __RCSID("$NetBSD: kill.c,v 1.18 2001/07/29 22:46:36 wiz Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <ctype.h>
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <signal.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 
     58 void nosig(char *);
     59 void printsignals(FILE *);
     60 int signame_to_signum(char *);
     61 void usage(void);
     62 int main(int, char *[]);
     63 
     64 int
     65 main(int argc, char *argv[])
     66 {
     67 	int errors, numsig, pid;
     68 	char *ep;
     69 
     70 	if (argc < 2)
     71 		usage();
     72 
     73 	numsig = SIGTERM;
     74 
     75 	argc--, argv++;
     76 	if (strcmp(*argv, "-l") == 0) {
     77 		argc--, argv++;
     78 		if (argc > 1)
     79 			usage();
     80 		if (argc == 1) {
     81 			if (isdigit((unsigned char)**argv) == 0)
     82 				usage();
     83 			numsig = strtol(*argv, &ep, 10);
     84 			if (*ep != '\0')
     85 				errx(1, "illegal signal number: %s", *argv);
     86 			if (numsig >= 128)
     87 				numsig -= 128;
     88 			if (numsig <= 0 || numsig >= NSIG)
     89 				nosig(*argv);
     90 			(void)printf("%s\n", sys_signame[numsig]);
     91 			exit(0);
     92 		}
     93 		printsignals(stdout);
     94 		exit(0);
     95 	}
     96 
     97 	if (!strcmp(*argv, "-s")) {
     98 		argc--, argv++;
     99 		if (argc < 1) {
    100 			warnx("option requires an argument -- s");
    101 			usage();
    102 		}
    103 		if (strcmp(*argv, "0")) {
    104 			if ((numsig = signame_to_signum(*argv)) < 0)
    105 				nosig(*argv);
    106 		} else
    107 			numsig = 0;
    108 		argc--, argv++;
    109 	} else if (**argv == '-') {
    110 		++*argv;
    111 		if (isalpha((unsigned char)**argv)) {
    112 			if ((numsig = signame_to_signum(*argv)) < 0)
    113 				nosig(*argv);
    114 		} else if (isdigit((unsigned char)**argv)) {
    115 			numsig = strtol(*argv, &ep, 10);
    116 			if (!*argv || *ep)
    117 				errx(1, "illegal signal number: %s", *argv);
    118 			if (numsig < 0 || numsig >= NSIG)
    119 				nosig(*argv);
    120 		} else
    121 			nosig(*argv);
    122 		argc--, argv++;
    123 	}
    124 
    125 	if (argc == 0)
    126 		usage();
    127 
    128 	for (errors = 0; argc; argc--, argv++) {
    129 		pid = strtol(*argv, &ep, 10);
    130 		if (!**argv || *ep) {
    131 			warnx("illegal process id: %s", *argv);
    132 			errors = 1;
    133 		} else if (kill(pid, numsig) == -1) {
    134 			warn("%s", *argv);
    135 			errors = 1;
    136 		}
    137 	}
    138 
    139 	exit(errors);
    140 	/* NOTREACHED */
    141 }
    142 
    143 int
    144 signame_to_signum(char *sig)
    145 {
    146 	int n;
    147 
    148 	if (strncasecmp(sig, "sig", 3) == 0)
    149 		sig += 3;
    150 	for (n = 1; n < NSIG; n++) {
    151 		if (!strcasecmp(sys_signame[n], sig))
    152 			return (n);
    153 	}
    154 	return (-1);
    155 }
    156 
    157 void
    158 nosig(char *name)
    159 {
    160 
    161 	warnx("unknown signal %s; valid signals:", name);
    162 	printsignals(stderr);
    163 	exit(1);
    164 	/* NOTREACHED */
    165 }
    166 
    167 void
    168 printsignals(FILE *fp)
    169 {
    170 	int n;
    171 
    172 	for (n = 1; n < NSIG; n++) {
    173 		(void)fprintf(fp, "%s", sys_signame[n]);
    174 		if (n == (NSIG / 2) || n == (NSIG - 1))
    175 			(void)fprintf(fp, "\n");
    176 		else
    177 			(void)fprintf(fp, " ");
    178 	}
    179 }
    180 
    181 void
    182 usage(void)
    183 {
    184 
    185 	(void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n");
    186 	(void)fprintf(stderr, "       kill -l [exit_status]\n");
    187 	(void)fprintf(stderr, "       kill -signal_name pid ...\n");
    188 	(void)fprintf(stderr, "       kill -signal_number pid ...\n");
    189 	exit(1);
    190 	/* NOTREACHED */
    191 }
    192