Home | History | Annotate | Line # | Download | only in kill
kill.c revision 1.22
      1 /* $NetBSD: kill.c,v 1.22 2003/08/04 22:31:24 jschauma 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.22 2003/08/04 22:31:24 jschauma 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(EXIT_FAILURE, "illegal signal number: %s",
     97 						*argv);
     98 				/* NOTREACHED */
     99 			}
    100 			if (numsig >= 128)
    101 				numsig -= 128;
    102 			if (numsig <= 0 || numsig >= NSIG)
    103 				nosig(*argv);
    104 			printf("%s\n", sys_signame[numsig]);
    105 			exit(0);
    106 		}
    107 		printsignals(stdout);
    108 		exit(0);
    109 	}
    110 
    111 	if (!strcmp(*argv, "-s")) {
    112 		argc--, argv++;
    113 		if (argc < 1) {
    114 			warnx("option requires an argument -- s");
    115 			usage();
    116 		}
    117 		if (strcmp(*argv, "0")) {
    118 			if ((numsig = signame_to_signum(*argv)) < 0)
    119 				nosig(*argv);
    120 		} else
    121 			numsig = 0;
    122 		argc--, argv++;
    123 	} else if (**argv == '-') {
    124 		++*argv;
    125 		if (isalpha((unsigned char)**argv)) {
    126 			if ((numsig = signame_to_signum(*argv)) < 0)
    127 				nosig(*argv);
    128 		} else if (isdigit((unsigned char)**argv)) {
    129 			numsig = strtol(*argv, &ep, 10);
    130 			if (!*argv || *ep) {
    131 				errx(EXIT_FAILURE, "illegal signal number: %s",
    132 						*argv);
    133 				/* NOTREACHED */
    134 			}
    135 			if (numsig < 0 || numsig >= NSIG)
    136 				nosig(*argv);
    137 		} else
    138 			nosig(*argv);
    139 		argc--, argv++;
    140 	}
    141 
    142 	if (argc == 0)
    143 		usage();
    144 
    145 	for (errors = 0; argc; argc--, argv++) {
    146 #ifdef SHELL
    147 		extern int getjobpgrp(const char *);
    148 		if (*argv[0] == '%') {
    149 			pid = getjobpgrp(*argv);
    150 			if (pid == 0) {
    151 				warnx("illegal job id: %s", *argv);
    152 				errors = 1;
    153 				continue;
    154 			}
    155 		} else
    156 #endif
    157 		{
    158 			pid = strtol(*argv, &ep, 10);
    159 			if (!**argv || *ep) {
    160 				warnx("illegal process id: %s", *argv);
    161 				errors = 1;
    162 				continue;
    163 			}
    164 		}
    165 		if (kill(pid, numsig) == -1) {
    166 			warn("%s", *argv);
    167 			errors = 1;
    168 		}
    169 #ifdef SHELL
    170 		/* Wakeup the process if it was suspended, so it can
    171 		   exit without an explicit 'fg'. */
    172 		if (numsig == SIGTERM || numsig == SIGHUP)
    173 			kill(pid, SIGCONT);
    174 #endif
    175 	}
    176 
    177 	exit(errors);
    178 	/* NOTREACHED */
    179 }
    180 
    181 static int
    182 signame_to_signum(char *sig)
    183 {
    184 	int n;
    185 
    186 	if (strncasecmp(sig, "sig", 3) == 0)
    187 		sig += 3;
    188 	for (n = 1; n < NSIG; n++) {
    189 		if (!strcasecmp(sys_signame[n], sig))
    190 			return (n);
    191 	}
    192 	return (-1);
    193 }
    194 
    195 static void
    196 nosig(char *name)
    197 {
    198 
    199 	warnx("unknown signal %s; valid signals:", name);
    200 	printsignals(stderr);
    201 	exit(1);
    202 	/* NOTREACHED */
    203 }
    204 
    205 static void
    206 printsignals(FILE *fp)
    207 {
    208 	int sig;
    209 	int len, nl;
    210 	const char *name;
    211 	int termwidth = 80;
    212 
    213 	if (isatty(fileno(fp))) {
    214 		struct winsize win;
    215 		if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
    216 			termwidth = win.ws_col;
    217 	}
    218 
    219 	for (len = 0, sig = 1; sig < NSIG; sig++) {
    220 		name = sys_signame[sig];
    221 		nl = 1 + strlen(name);
    222 
    223 		if (len + nl >= termwidth) {
    224 			fprintf(fp, "\n");
    225 			len = 0;
    226 		} else
    227 			if (len != 0)
    228 				fprintf(fp, " ");
    229 		len += nl;
    230 		fprintf(fp, "%s", name);
    231 	}
    232 	if (len != 0)
    233 		fprintf(fp, "\n");
    234 }
    235 
    236 static void
    237 usage(void)
    238 {
    239 
    240 	fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
    241 	    "       %s -l [exit_status]\n"
    242 	    "       %s -signal_name pid ...\n"
    243 	    "       %s -signal_number pid ...\n",
    244 	    getprogname(), getprogname(), getprogname(), getprogname());
    245 	exit(1);
    246 	/* NOTREACHED */
    247 }
    248