From 7104bb23a94481b4b80bbd3e3a96e04282159826 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Wed, 30 Jan 2013 19:01:43 +0100 Subject: [PATCH] Upgrading kill Change-Id: I9ad71f08998ac17651d243afecbcc362e0f91b56 --- bin/Makefile | 2 +- bin/kill/Makefile | 6 + bin/kill/kill.1 | 151 +++++++++++++++++++++ bin/kill/kill.c | 253 ++++++++++++++++++++++++++++++++++++ commands/Makefile | 2 +- commands/kill/Makefile | 4 - commands/kill/kill.c | 102 --------------- distrib/sets/lists/minix/mi | 3 +- man/man1/Makefile | 2 +- man/man1/kill.1 | 47 ------- releasetools/nbsd_ports | 1 + 11 files changed, 416 insertions(+), 157 deletions(-) create mode 100644 bin/kill/Makefile create mode 100644 bin/kill/kill.1 create mode 100644 bin/kill/kill.c delete mode 100644 commands/kill/Makefile delete mode 100644 commands/kill/kill.c delete mode 100644 man/man1/kill.1 diff --git a/bin/Makefile b/bin/Makefile index e62fd47fe..18c762def 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,7 +1,7 @@ # $NetBSD: Makefile,v 1.22 2007/12/31 15:31:24 ad Exp $ # @(#)Makefile 8.1 (Berkeley) 5/31/93 -SUBDIR= cat date echo ed expr \ +SUBDIR= cat date echo ed expr kill \ mkdir pax pwd rm rmdir test .include diff --git a/bin/kill/Makefile b/bin/kill/Makefile new file mode 100644 index 000000000..c2b60979b --- /dev/null +++ b/bin/kill/Makefile @@ -0,0 +1,6 @@ +# $NetBSD: Makefile,v 1.8 1997/07/20 22:37:09 christos Exp $ +# @(#)Makefile 8.1 (Berkeley) 5/31/93 + +PROG= kill + +.include diff --git a/bin/kill/kill.1 b/bin/kill/kill.1 new file mode 100644 index 000000000..0d9f16790 --- /dev/null +++ b/bin/kill/kill.1 @@ -0,0 +1,151 @@ +.\" $NetBSD: kill.1,v 1.22 2012/03/22 07:58:17 wiz Exp $ +.\" +.\" Copyright (c) 1980, 1990, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the Institute of Electrical and Electronics Engineers, Inc. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)kill.1 8.2 (Berkeley) 4/28/95 +.\" +.Dd April 28, 1995 +.Dt KILL 1 +.Os +.Sh NAME +.Nm kill +.Nd terminate or signal a process +.Sh SYNOPSIS +.Nm +.Op Fl s Ar signal_name +.Ar pid +\&... +.Nm +.Fl l +.Op Ar exit_status +.Nm +.Fl signal_name +.Ar pid +\&... +.Nm +.Fl signal_number +.Ar pid +\&... +.Sh DESCRIPTION +The +.Nm +utility sends a signal to the process(es) specified +by the pid operand(s). +.Pp +Only the super-user may send signals to other users' processes. +.Pp +The options are as follows: +.Pp +.Bl -tag -width Ds +.It Fl s Ar signal_name +A symbolic signal name specifying the signal to be sent instead of the +default +.Dv TERM . +.It Fl l Op Ar exit_status +Display the name of the signal corresponding to +.Ar exit_status . +.Ar exit_status +may be the exit status of a command killed by a signal +(see the +special +.Xr sh 1 +parameter +.Sq ?\& ) +or a signal number. +.Pp +If no operand is given, display the names of all the signals. +.It Fl signal_name +A symbolic signal name specifying the signal to be sent instead of the +default +.Dv TERM . +.It Fl signal_number +A non-negative decimal integer, specifying the signal to be sent instead +of the default +.Dv TERM . +.El +.Pp +The following pids have special meanings: +.Bl -tag -width Ds -compact +.It -1 +If superuser, broadcast the signal to all processes; otherwise broadcast +to all processes belonging to the user. +.It 0 +Broadcast the signal to all processes in the current process group +belonging to the user. +.El +.Pp +Some of the more commonly used signals: +.Bl -tag -width Ds -compact +.It 1 +HUP (hang up) +.It 2 +INT (interrupt) +.It 3 +QUIT (quit) +.It 6 +ABRT (abort) +.It 9 +KILL (non-catchable, non-ignorable kill) +.It 14 +ALRM (alarm clock) +.It 15 +TERM (software termination signal) +.El +.Pp +.Nm +is a built-in to +.Xr csh 1 ; +it allows job specifiers of the form ``%...'' as arguments +so process id's are not as often used as +.Nm +arguments. +See +.Xr csh 1 +for details. +.Sh SEE ALSO +.Xr csh 1 , +.Xr pgrep 1 , +.Xr pkill 1 , +.Xr ps 1 , +.Xr kill 2 , +.Xr sigaction 2 , +.Xr signal 7 +.Sh STANDARDS +The +.Nm +function is expected to be +.St -p1003.2 +compatible. +.Sh HISTORY +A +.Nm +command appeared in +.At v6 . diff --git a/bin/kill/kill.c b/bin/kill/kill.c new file mode 100644 index 000000000..059257795 --- /dev/null +++ b/bin/kill/kill.c @@ -0,0 +1,253 @@ +/* $NetBSD: kill.c,v 1.27 2011/08/29 14:51:18 joerg Exp $ */ + +/* + * Copyright (c) 1988, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if !defined(lint) && !defined(SHELL) +__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\ + The Regents of the University of California. All rights reserved."); +#endif /* not lint */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95"; +#else +__RCSID("$NetBSD: kill.c,v 1.27 2011/08/29 14:51:18 joerg Exp $"); +#endif +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef SHELL /* sh (aka ash) builtin */ +int killcmd(int, char *argv[]); +#define main killcmd +#include "../../bin/sh/bltin/bltin.h" +#endif /* SHELL */ + +__dead static void nosig(char *); +static void printsignals(FILE *); +static int signame_to_signum(char *); +__dead static void usage(void); + +int +main(int argc, char *argv[]) +{ + int errors; + intmax_t numsig, pid; + char *ep; + + setprogname(argv[0]); + setlocale(LC_ALL, ""); + if (argc < 2) + usage(); + + numsig = SIGTERM; + + argc--, argv++; + if (strcmp(*argv, "-l") == 0) { + argc--, argv++; + if (argc > 1) + usage(); + if (argc == 1) { + if (isdigit((unsigned char)**argv) == 0) + usage(); + numsig = strtoimax(*argv, &ep, 10); + /* check for correctly parsed number */ + if (*ep != '\0' || numsig == INTMAX_MIN || numsig == INTMAX_MAX) { + errx(EXIT_FAILURE, "illegal signal number: %s", + *argv); + /* NOTREACHED */ + } + if (numsig >= 128) + numsig -= 128; + /* and whether it fits into signals range */ + if (numsig <= 0 || numsig >= NSIG) + nosig(*argv); + printf("%s\n", sys_signame[(int) numsig]); + exit(0); + } + printsignals(stdout); + exit(0); + } + + if (!strcmp(*argv, "-s")) { + argc--, argv++; + if (argc < 1) { + warnx("option requires an argument -- s"); + usage(); + } + if (strcmp(*argv, "0")) { + if ((numsig = signame_to_signum(*argv)) < 0) + nosig(*argv); + } else + numsig = 0; + argc--, argv++; + } else if (**argv == '-') { + char *sn = *argv + 1; + if (isalpha((unsigned char)*sn)) { + if ((numsig = signame_to_signum(sn)) < 0) + nosig(sn); + } else if (isdigit((unsigned char)*sn)) { + numsig = strtoimax(sn, &ep, 10); + /* check for correctly parsed number */ + if (*ep || numsig == INTMAX_MIN || numsig == INTMAX_MAX ) { + errx(EXIT_FAILURE, "illegal signal number: %s", + sn); + /* NOTREACHED */ + } + /* and whether it fits into signals range */ + if (numsig < 0 || numsig >= NSIG) + nosig(sn); + } else + nosig(sn); + argc--, argv++; + } + + if (argc == 0) + usage(); + + for (errors = 0; argc; argc--, argv++) { +#ifdef SHELL + extern int getjobpgrp(const char *); + if (*argv[0] == '%') { + pid = getjobpgrp(*argv); + if (pid == 0) { + warnx("illegal job id: %s", *argv); + errors = 1; + continue; + } + } else +#endif + { + pid = strtoimax(*argv, &ep, 10); + /* make sure the pid is a number and fits into pid_t */ + if (!**argv || *ep || pid == INTMAX_MIN || + pid == INTMAX_MAX || pid != (pid_t) pid) { + + warnx("illegal process id: %s", *argv); + errors = 1; + continue; + } + } + if (kill((pid_t) pid, (int) numsig) == -1) { + warn("%s", *argv); + errors = 1; + } +#ifdef SHELL + /* Wakeup the process if it was suspended, so it can + exit without an explicit 'fg'. */ + if (numsig == SIGTERM || numsig == SIGHUP) + kill((pid_t) pid, SIGCONT); +#endif + } + + exit(errors); + /* NOTREACHED */ +} + +static int +signame_to_signum(char *sig) +{ + int n; + + if (strncasecmp(sig, "sig", 3) == 0) + sig += 3; + for (n = 1; n < NSIG; n++) { + if (!strcasecmp(sys_signame[n], sig)) + return (n); + } + return (-1); +} + +static void +nosig(char *name) +{ + + warnx("unknown signal %s; valid signals:", name); + printsignals(stderr); + exit(1); + /* NOTREACHED */ +} + +static void +printsignals(FILE *fp) +{ + int sig; + int len, nl; + const char *name; + int termwidth = 80; + + if (isatty(fileno(fp))) { + struct winsize win; + if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0) + termwidth = win.ws_col; + } + + for (len = 0, sig = 1; sig < NSIG; sig++) { + name = sys_signame[sig]; + nl = 1 + strlen(name); + + if (len + nl >= termwidth) { + fprintf(fp, "\n"); + len = 0; + } else + if (len != 0) + fprintf(fp, " "); + len += nl; + fprintf(fp, "%s", name); + } + if (len != 0) + fprintf(fp, "\n"); +} + +static void +usage(void) +{ + + fprintf(stderr, "usage: %s [-s signal_name] pid ...\n" + " %s -l [exit_status]\n" + " %s -signal_name pid ...\n" + " %s -signal_number pid ...\n", + getprogname(), getprogname(), getprogname(), getprogname()); + exit(1); + /* NOTREACHED */ +} diff --git a/commands/Makefile b/commands/Makefile index 53bd42dad..0921cf12e 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -12,7 +12,7 @@ SUBDIR= add_route arp ash at backup banner basename btrace cal \ find finger fingerd fix fold format fortune fsck.mfs \ ftp101 gcore gcov-pull getty grep head hexdump host \ hostaddr id ifconfig ifdef \ - intr ipcrm ipcs irdpd isoread kill last \ + intr ipcrm ipcs irdpd isoread last \ less loadkeys loadramdisk logger look lp \ lpd ls lspci mail MAKEDEV \ mesg mined mkfifo \ diff --git a/commands/kill/Makefile b/commands/kill/Makefile deleted file mode 100644 index 509f5ef2d..000000000 --- a/commands/kill/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -PROG= kill -MAN= - -.include diff --git a/commands/kill/kill.c b/commands/kill/kill.c deleted file mode 100644 index bb957960c..000000000 --- a/commands/kill/kill.c +++ /dev/null @@ -1,102 +0,0 @@ -/* kill - send a signal to a process Author: Adri Koppes */ - -#include -#include -#include -#include -#include -#include - -int main(int argc, char **argv); -void usage(void); - -/* Table of signal names. */ -struct signames { - char *name; - int sig; -} signames[] = { - { "HUP", SIGHUP }, - { "INT", SIGINT }, - { "QUIT", SIGQUIT }, - { "ILL", SIGILL }, - { "TRAP", SIGTRAP }, - { "ABRT", SIGABRT }, - { "IOT", SIGIOT }, - { "FPE", SIGFPE }, - { "KILL", SIGKILL }, - { "USR1", SIGUSR1 }, - { "SEGV", SIGSEGV }, - { "USR2", SIGUSR2 }, - { "PIPE", SIGPIPE }, - { "ALRM", SIGALRM }, - { "TERM", SIGTERM }, - { "EMT", SIGEMT }, - { "BUS", SIGBUS }, - { "CHLD", SIGCHLD }, - { "CONT", SIGCONT }, - { "STOP", SIGSTOP }, - { "TSTP", SIGTSTP }, - { "TTIN", SIGTTIN }, - { "TTOU", SIGTTOU }, -#ifdef SIGWINCH - { "WINCH", SIGWINCH }, -#endif - { "VTALRM", SIGVTALRM }, - { "PROF", SIGPROF }, - { NULL, 0 } -}; - -int main(argc, argv) -int argc; -char **argv; -{ - pid_t proc; - int ex = 0, sig = SIGTERM; - char *end; - long l; - unsigned long ul; - struct sigaction sa; - int i, doit; - struct signames *snp; - - if (argc > 1 && argv[1][0] == '-') { - sig = -1; - for (snp = signames; snp->name != NULL; snp++) { /* symbolic? */ - if (strcmp(snp->name, argv[1] + 1) == 0) { - sig = snp->sig; - break; - } - } - if (sig < 0) { /* numeric? */ - ul = strtoul(argv[1] + 1, &end, 10); - if (end == argv[1] + 1 || *end != 0 || ul >= _NSIG) usage(); - sig = ul; - } - argv++; - argc--; - } - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); - sa.sa_handler = SIG_IGN; /* try not to kill yourself */ - (void) sigaction(sig, &sa, (struct sigaction *) NULL); - - for (doit = 0; doit <= 1; doit++) { - for (i = 1; i < argc; i++) { - l = strtoul(argv[i], &end, 10); - if (end == argv[i] || *end != 0 || (pid_t) l != l) usage(); - proc = l; - if (doit && kill(proc, sig) < 0) { - fprintf(stderr, "kill: %d: %s\n", - proc, strerror(errno)); - ex = 1; - } - } - } - return(ex); -} - -void usage() -{ - fprintf(stderr, "Usage: kill [-sig] pid\n"); - exit(1); -} diff --git a/distrib/sets/lists/minix/mi b/distrib/sets/lists/minix/mi index ea2d81068..69bc16829 100644 --- a/distrib/sets/lists/minix/mi +++ b/distrib/sets/lists/minix/mi @@ -16,6 +16,7 @@ ./bin/getty minix-sys ./bin/halt minix-sys ./bin/intr minix-sys +./bin/kill minix-sys ./bin/ln minix-sys ./bin/loadkeys minix-sys ./bin/ls minix-sys @@ -265,7 +266,7 @@ ./usr/bin/isoinfo minix-sys ./usr/bin/isoread minix-sys ./usr/bin/join minix-sys -./usr/bin/kill minix-sys +./usr/bin/kill minix-sys obsolete ./usr/bin/last minix-sys ./usr/bin/ldd minix-sys ./usr/bin/lessecho minix-sys diff --git a/man/man1/Makefile b/man/man1/Makefile index 01cc5b227..effbb4861 100644 --- a/man/man1/Makefile +++ b/man/man1/Makefile @@ -7,7 +7,7 @@ MAN= ash.1 at.1 banner.1 basename.1 \ env.1 expand.1 factor.1 \ finger.1 flexdoc.1 fold.1 format.1 fortune.1 \ fsck.mfs.1 head.1 host.1 hostaddr.1 ifdef.1 \ - isodir.1 isoinfo.1 isoread.1 kill.1 \ + isodir.1 isoinfo.1 isoread.1 \ last.1 loadfont.1 loadkeys.1 logger.1 \ look.1 lp.1 ls.1 lspci.1 mail.1 \ mesg.1 mixer.1 mkfs.mfs.1 \ diff --git a/man/man1/kill.1 b/man/man1/kill.1 deleted file mode 100644 index e232f21ec..000000000 --- a/man/man1/kill.1 +++ /dev/null @@ -1,47 +0,0 @@ -.TH KILL 1 -.SH NAME -kill \- send a signal to a process -.SH SYNOPSIS -\fBkill\fR [\fB\-\fIn\fR] \fIprocess\fR -.br -.de FL -.TP -\\fB\\$1\\fR -\\$2 -.. -.de EX -.TP 20 -\\fB\\$1\\fR -# \\$2 -.. -.SH OPTIONS -.TP 5 -.B \-\fIn\fR -# Signal number to send -.TP 5 -.B \-\fINAME\fR -# Named signal to send -.SH EXAMPLES -.TP 20 -.B kill 35 -# Send signal 15 to process 35 -.TP 20 -.B kill \-9 40 -# Send signal 9 to process 40 -.TP 20 -.B kill \-2 0 -# Send signal 2 to whole terminal process group -.TP 20 -.B kill \-HUP -123 -# Send a hangup to process group 123 -.SH DESCRIPTION -.PP -A signal is sent to a given process. -By default signal 15 (SIGTERM) is sent. -Process 0 means all the processes in the sender's process group. -A process group can be signalled by the negative value of the process -group ID. -Signals may be numerical, or the name of the signal without \fBSIG\fP. -.SH "SEE ALSO" -.BR kill (2), -.BR sigaction (2). diff --git a/releasetools/nbsd_ports b/releasetools/nbsd_ports index b22e92981..cac856969 100644 --- a/releasetools/nbsd_ports +++ b/releasetools/nbsd_ports @@ -11,6 +11,7 @@ 2012/10/17 12:00:00,bin/echo 2012/01/16 18:47:57,bin/ed 2012/10/17 12:00:00,bin/expr +2012/10/17 12:00:00,bin/kill 2012/10/17 12:00:00,bin/Makefile 2012/10/17 12:00:00,bin/Makefile.inc 2008/07/20 00:52:40,bin/mkdir