Home | History | Annotate | Line # | Download | only in kill
kill.c revision 1.25
      1  1.25     lukem /* $NetBSD: kill.c,v 1.25 2008/07/20 00:52:40 lukem Exp $ */
      2  1.10       cgd 
      3   1.1       cgd /*
      4   1.7   mycroft  * Copyright (c) 1988, 1993, 1994
      5   1.7   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.23       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.13  christos #include <sys/cdefs.h>
     33  1.20  christos #if !defined(lint) && !defined(SHELL)
     34  1.25     lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
     35  1.25     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39  1.10       cgd #if 0
     40  1.11       jtc static char sccsid[] = "@(#)kill.c	8.4 (Berkeley) 4/28/95";
     41  1.10       cgd #else
     42  1.25     lukem __RCSID("$NetBSD: kill.c,v 1.25 2008/07/20 00:52:40 lukem Exp $");
     43  1.10       cgd #endif
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.7   mycroft #include <ctype.h>
     47   1.7   mycroft #include <err.h>
     48   1.7   mycroft #include <errno.h>
     49   1.1       cgd #include <signal.h>
     50   1.1       cgd #include <stdio.h>
     51   1.1       cgd #include <stdlib.h>
     52   1.1       cgd #include <string.h>
     53  1.20  christos #include <termios.h>
     54  1.20  christos #include <unistd.h>
     55  1.20  christos #include <locale.h>
     56  1.20  christos #include <sys/ioctl.h>
     57  1.20  christos 
     58  1.20  christos #ifdef SHELL            /* sh (aka ash) builtin */
     59  1.20  christos #define main killcmd
     60  1.20  christos #include "../../bin/sh/bltin/bltin.h"
     61  1.20  christos #endif /* SHELL */
     62  1.20  christos 
     63  1.20  christos static void nosig(char *);
     64  1.20  christos static void printsignals(FILE *);
     65  1.20  christos static int signame_to_signum(char *);
     66  1.20  christos static void usage(void);
     67  1.18       wiz int main(int, char *[]);
     68   1.7   mycroft 
     69   1.7   mycroft int
     70  1.18       wiz main(int argc, char *argv[])
     71   1.1       cgd {
     72   1.7   mycroft 	int errors, numsig, pid;
     73   1.1       cgd 	char *ep;
     74   1.1       cgd 
     75  1.19       wiz 	setprogname(argv[0]);
     76  1.20  christos 	setlocale(LC_ALL, "");
     77   1.1       cgd 	if (argc < 2)
     78   1.1       cgd 		usage();
     79   1.1       cgd 
     80   1.4       jtc 	numsig = SIGTERM;
     81   1.7   mycroft 
     82   1.4       jtc 	argc--, argv++;
     83  1.18       wiz 	if (strcmp(*argv, "-l") == 0) {
     84   1.7   mycroft 		argc--, argv++;
     85   1.7   mycroft 		if (argc > 1)
     86   1.7   mycroft 			usage();
     87   1.7   mycroft 		if (argc == 1) {
     88  1.18       wiz 			if (isdigit((unsigned char)**argv) == 0)
     89   1.7   mycroft 				usage();
     90   1.7   mycroft 			numsig = strtol(*argv, &ep, 10);
     91  1.22  jschauma 			if (*ep != '\0') {
     92  1.22  jschauma 				errx(EXIT_FAILURE, "illegal signal number: %s",
     93  1.22  jschauma 						*argv);
     94  1.22  jschauma 				/* NOTREACHED */
     95  1.22  jschauma 			}
     96   1.7   mycroft 			if (numsig >= 128)
     97   1.7   mycroft 				numsig -= 128;
     98   1.7   mycroft 			if (numsig <= 0 || numsig >= NSIG)
     99   1.7   mycroft 				nosig(*argv);
    100  1.20  christos 			printf("%s\n", sys_signame[numsig]);
    101   1.7   mycroft 			exit(0);
    102   1.4       jtc 		}
    103   1.4       jtc 		printsignals(stdout);
    104   1.1       cgd 		exit(0);
    105   1.7   mycroft 	}
    106   1.7   mycroft 
    107   1.7   mycroft 	if (!strcmp(*argv, "-s")) {
    108   1.7   mycroft 		argc--, argv++;
    109   1.7   mycroft 		if (argc < 1) {
    110   1.7   mycroft 			warnx("option requires an argument -- s");
    111   1.4       jtc 			usage();
    112   1.4       jtc 		}
    113   1.7   mycroft 		if (strcmp(*argv, "0")) {
    114   1.7   mycroft 			if ((numsig = signame_to_signum(*argv)) < 0)
    115   1.7   mycroft 				nosig(*argv);
    116   1.7   mycroft 		} else
    117   1.4       jtc 			numsig = 0;
    118   1.7   mycroft 		argc--, argv++;
    119   1.4       jtc 	} else if (**argv == '-') {
    120  1.24  christos 		char *sn = *argv + 1;
    121  1.24  christos 		if (isalpha((unsigned char)*sn)) {
    122  1.24  christos 			if ((numsig = signame_to_signum(sn)) < 0)
    123  1.24  christos 				nosig(sn);
    124  1.24  christos 		} else if (isdigit((unsigned char)*sn)) {
    125  1.24  christos 			numsig = strtol(sn, &ep, 10);
    126  1.24  christos 			if (*ep) {
    127  1.22  jschauma 				errx(EXIT_FAILURE, "illegal signal number: %s",
    128  1.24  christos 						sn);
    129  1.22  jschauma 				/* NOTREACHED */
    130  1.22  jschauma 			}
    131   1.9       jtc 			if (numsig < 0 || numsig >= NSIG)
    132  1.24  christos 				nosig(sn);
    133   1.1       cgd 		} else
    134  1.24  christos 			nosig(sn);
    135   1.7   mycroft 		argc--, argv++;
    136   1.1       cgd 	}
    137   1.1       cgd 
    138   1.7   mycroft 	if (argc == 0)
    139   1.1       cgd 		usage();
    140   1.1       cgd 
    141   1.7   mycroft 	for (errors = 0; argc; argc--, argv++) {
    142  1.20  christos #ifdef SHELL
    143  1.20  christos 		extern int getjobpgrp(const char *);
    144  1.20  christos 		if (*argv[0] == '%') {
    145  1.20  christos 			pid = getjobpgrp(*argv);
    146  1.20  christos 			if (pid == 0) {
    147  1.20  christos 				warnx("illegal job id: %s", *argv);
    148  1.20  christos 				errors = 1;
    149  1.20  christos 				continue;
    150  1.20  christos 			}
    151  1.20  christos 		} else
    152  1.20  christos #endif
    153  1.20  christos 		{
    154  1.20  christos 			pid = strtol(*argv, &ep, 10);
    155  1.20  christos 			if (!**argv || *ep) {
    156  1.20  christos 				warnx("illegal process id: %s", *argv);
    157  1.20  christos 				errors = 1;
    158  1.20  christos 				continue;
    159  1.20  christos 			}
    160  1.20  christos 		}
    161  1.20  christos 		if (kill(pid, numsig) == -1) {
    162   1.7   mycroft 			warn("%s", *argv);
    163   1.1       cgd 			errors = 1;
    164   1.1       cgd 		}
    165  1.21  christos #ifdef SHELL
    166  1.21  christos 		/* Wakeup the process if it was suspended, so it can
    167  1.21  christos 		   exit without an explicit 'fg'. */
    168  1.21  christos 		if (numsig == SIGTERM || numsig == SIGHUP)
    169  1.21  christos 			kill(pid, SIGCONT);
    170  1.21  christos #endif
    171   1.1       cgd 	}
    172   1.7   mycroft 
    173   1.1       cgd 	exit(errors);
    174  1.14   mycroft 	/* NOTREACHED */
    175   1.1       cgd }
    176   1.1       cgd 
    177  1.20  christos static int
    178  1.18       wiz signame_to_signum(char *sig)
    179   1.4       jtc {
    180   1.6   mycroft 	int n;
    181   1.4       jtc 
    182  1.18       wiz 	if (strncasecmp(sig, "sig", 3) == 0)
    183   1.4       jtc 		sig += 3;
    184   1.6   mycroft 	for (n = 1; n < NSIG; n++) {
    185   1.6   mycroft 		if (!strcasecmp(sys_signame[n], sig))
    186   1.7   mycroft 			return (n);
    187   1.4       jtc 	}
    188   1.7   mycroft 	return (-1);
    189   1.4       jtc }
    190   1.4       jtc 
    191  1.20  christos static void
    192  1.18       wiz nosig(char *name)
    193   1.1       cgd {
    194   1.7   mycroft 
    195   1.7   mycroft 	warnx("unknown signal %s; valid signals:", name);
    196   1.4       jtc 	printsignals(stderr);
    197   1.1       cgd 	exit(1);
    198  1.15   mycroft 	/* NOTREACHED */
    199   1.1       cgd }
    200   1.1       cgd 
    201  1.20  christos static void
    202  1.18       wiz printsignals(FILE *fp)
    203   1.1       cgd {
    204  1.20  christos 	int sig;
    205  1.20  christos 	int len, nl;
    206  1.20  christos 	const char *name;
    207  1.20  christos 	int termwidth = 80;
    208  1.20  christos 
    209  1.20  christos 	if (isatty(fileno(fp))) {
    210  1.20  christos 		struct winsize win;
    211  1.20  christos 		if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
    212  1.20  christos 			termwidth = win.ws_col;
    213  1.20  christos 	}
    214   1.1       cgd 
    215  1.20  christos 	for (len = 0, sig = 1; sig < NSIG; sig++) {
    216  1.20  christos 		name = sys_signame[sig];
    217  1.20  christos 		nl = 1 + strlen(name);
    218  1.20  christos 
    219  1.20  christos 		if (len + nl >= termwidth) {
    220  1.20  christos 			fprintf(fp, "\n");
    221  1.20  christos 			len = 0;
    222  1.20  christos 		} else
    223  1.20  christos 			if (len != 0)
    224  1.20  christos 				fprintf(fp, " ");
    225  1.20  christos 		len += nl;
    226  1.20  christos 		fprintf(fp, "%s", name);
    227   1.7   mycroft 	}
    228  1.20  christos 	if (len != 0)
    229  1.20  christos 		fprintf(fp, "\n");
    230   1.1       cgd }
    231   1.1       cgd 
    232  1.20  christos static void
    233  1.18       wiz usage(void)
    234   1.1       cgd {
    235   1.7   mycroft 
    236  1.20  christos 	fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
    237  1.19       wiz 	    "       %s -l [exit_status]\n"
    238  1.19       wiz 	    "       %s -signal_name pid ...\n"
    239  1.19       wiz 	    "       %s -signal_number pid ...\n",
    240  1.19       wiz 	    getprogname(), getprogname(), getprogname(), getprogname());
    241   1.1       cgd 	exit(1);
    242  1.15   mycroft 	/* NOTREACHED */
    243   1.1       cgd }
    244