Upgrading kill

Change-Id: I9ad71f08998ac17651d243afecbcc362e0f91b56
This commit is contained in:
Lionel Sambuc 2013-01-30 19:01:43 +01:00
parent 06f3970b9e
commit 7104bb23a9
11 changed files with 416 additions and 157 deletions

View File

@ -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 <bsd.subdir.mk>

6
bin/kill/Makefile Normal file
View File

@ -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 <bsd.prog.mk>

151
bin/kill/kill.1 Normal file
View File

@ -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 .

253
bin/kill/kill.c Normal file
View File

@ -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 <sys/cdefs.h>
#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 <ctype.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <inttypes.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <locale.h>
#include <sys/ioctl.h>
#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 */
}

View File

@ -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 \

View File

@ -1,4 +0,0 @@
PROG= kill
MAN=
.include <bsd.prog.mk>

View File

@ -1,102 +0,0 @@
/* kill - send a signal to a process Author: Adri Koppes */
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
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);
}

View File

@ -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

View File

@ -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 \

View File

@ -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).

View File

@ -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