Home | History | Annotate | Line # | Download | only in kill
kill.c revision 1.20
      1 /* $NetBSD: kill.c,v 1.20 2002/11/24 22:35:44 christos 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 #if !defined(lint) && !defined(SHELL)
     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.20 2002/11/24 22:35:44 christos 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 #include <termios.h>
     58 #include <unistd.h>
     59 #include <locale.h>
     60 #include <sys/ioctl.h>
     61 
     62 #ifdef SHELL            /* sh (aka ash) builtin */
     63 #define main killcmd
     64 #include "../../bin/sh/bltin/bltin.h"
     65 #endif /* SHELL */
     66 
     67 static void nosig(char *);
     68 static void printsignals(FILE *);
     69 static int signame_to_signum(char *);
     70 static void usage(void);
     71 int main(int, char *[]);
     72 
     73 int
     74 main(int argc, char *argv[])
     75 {
     76 	int errors, numsig, pid;
     77 	char *ep;
     78 
     79 	setprogname(argv[0]);
     80 	setlocale(LC_ALL, "");
     81 	if (argc < 2)
     82 		usage();
     83 
     84 	numsig = SIGTERM;
     85 
     86 	argc--, argv++;
     87 	if (strcmp(*argv, "-l") == 0) {
     88 		argc--, argv++;
     89 		if (argc > 1)
     90 			usage();
     91 		if (argc == 1) {
     92 			if (isdigit((unsigned char)**argv) == 0)
     93 				usage();
     94 			numsig = strtol(*argv, &ep, 10);
     95 			if (*ep != '\0')
     96 				errx(1, "illegal signal number: %s", *argv);
     97 			if (numsig >= 128)
     98 				numsig -= 128;
     99 			if (numsig <= 0 || numsig >= NSIG)
    100 				nosig(*argv);
    101 			printf("%s\n", sys_signame[numsig]);
    102 			exit(0);
    103 		}
    104 		printsignals(stdout);
    105 		exit(0);
    106 	}
    107 
    108 	if (!strcmp(*argv, "-s")) {
    109 		argc--, argv++;
    110 		if (argc < 1) {
    111 			warnx("option requires an argument -- s");
    112 			usage();
    113 		}
    114 		if (strcmp(*argv, "0")) {
    115 			if ((numsig = signame_to_signum(*argv)) < 0)
    116 				nosig(*argv);
    117 		} else
    118 			numsig = 0;
    119 		argc--, argv++;
    120 	} else if (**argv == '-') {
    121 		++*argv;
    122 		if (isalpha((unsigned char)**argv)) {
    123 			if ((numsig = signame_to_signum(*argv)) < 0)
    124 				nosig(*argv);
    125 		} else if (isdigit((unsigned char)**argv)) {
    126 			numsig = strtol(*argv, &ep, 10);
    127 			if (!*argv || *ep)
    128 				errx(1, "illegal signal number: %s", *argv);
    129 			if (numsig < 0 || numsig >= NSIG)
    130 				nosig(*argv);
    131 		} else
    132 			nosig(*argv);
    133 		argc--, argv++;
    134 	}
    135 
    136 	if (argc == 0)
    137 		usage();
    138 
    139 	for (errors = 0; argc; argc--, argv++) {
    140 #ifdef SHELL
    141 		extern int getjobpgrp(const char *);
    142 		if (*argv[0] == '%') {
    143 			pid = getjobpgrp(*argv);
    144 			if (pid == 0) {
    145 				warnx("illegal job id: %s", *argv);
    146 				errors = 1;
    147 				continue;
    148 			}
    149 		} else
    150 #endif
    151 		{
    152 			pid = strtol(*argv, &ep, 10);
    153 			if (!**argv || *ep) {
    154 				warnx("illegal process id: %s", *argv);
    155 				errors = 1;
    156 				continue;
    157 			}
    158 		}
    159 		if (kill(pid, numsig) == -1) {
    160 			warn("%s", *argv);
    161 			errors = 1;
    162 		}
    163 	}
    164 
    165 	exit(errors);
    166 	/* NOTREACHED */
    167 }
    168 
    169 static int
    170 signame_to_signum(char *sig)
    171 {
    172 	int n;
    173 
    174 	if (strncasecmp(sig, "sig", 3) == 0)
    175 		sig += 3;
    176 	for (n = 1; n < NSIG; n++) {
    177 		if (!strcasecmp(sys_signame[n], sig))
    178 			return (n);
    179 	}
    180 	return (-1);
    181 }
    182 
    183 static void
    184 nosig(char *name)
    185 {
    186 
    187 	warnx("unknown signal %s; valid signals:", name);
    188 	printsignals(stderr);
    189 	exit(1);
    190 	/* NOTREACHED */
    191 }
    192 
    193 static void
    194 printsignals(FILE *fp)
    195 {
    196 	int sig;
    197 	int len, nl;
    198 	const char *name;
    199 	int termwidth = 80;
    200 
    201 	if (isatty(fileno(fp))) {
    202 		struct winsize win;
    203 		if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
    204 			termwidth = win.ws_col;
    205 	}
    206 
    207 	for (len = 0, sig = 1; sig < NSIG; sig++) {
    208 		name = sys_signame[sig];
    209 		nl = 1 + strlen(name);
    210 
    211 		if (len + nl >= termwidth) {
    212 			fprintf(fp, "\n");
    213 			len = 0;
    214 		} else
    215 			if (len != 0)
    216 				fprintf(fp, " ");
    217 		len += nl;
    218 		fprintf(fp, "%s", name);
    219 	}
    220 	if (len != 0)
    221 		fprintf(fp, "\n");
    222 }
    223 
    224 static void
    225 usage(void)
    226 {
    227 
    228 	fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
    229 	    "       %s -l [exit_status]\n"
    230 	    "       %s -signal_name pid ...\n"
    231 	    "       %s -signal_number pid ...\n",
    232 	    getprogname(), getprogname(), getprogname(), getprogname());
    233 	exit(1);
    234 	/* NOTREACHED */
    235 }
    236