2006-12-06 17:25:32 -06:00
|
|
|
|
/*
|
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
|
* 4. 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.
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* oct 5 1994 -- almost entirely re-written to allow for process names.
|
|
|
|
|
* modifications (c) salvatore valente <svalente@mit.edu>
|
|
|
|
|
* may be used / modified / distributed under the same terms as the original.
|
2006-12-06 17:25:39 -06:00
|
|
|
|
*
|
2006-12-06 17:25:46 -06:00
|
|
|
|
* 1999-02-22 Arkadiusz Mi<EFBFBD>kiewicz <misiek@pld.ORG.PL>
|
2006-12-06 17:25:39 -06:00
|
|
|
|
* - added Native Language Support
|
|
|
|
|
*
|
2006-12-06 17:25:41 -06:00
|
|
|
|
* 1999-11-13 aeb Accept signal numers 128+s.
|
|
|
|
|
*
|
2006-12-06 17:25:32 -06:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <signal.h>
|
2006-12-06 17:25:43 -06:00
|
|
|
|
#include "kill.h"
|
2006-12-06 17:25:39 -06:00
|
|
|
|
#include "nls.h"
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
|
#define SIZE(a) (sizeof(a)/sizeof(a[0]))
|
|
|
|
|
|
|
|
|
|
struct signv {
|
|
|
|
|
char *name;
|
|
|
|
|
int val;
|
|
|
|
|
} sys_signame[] = {
|
|
|
|
|
/* POSIX signals */
|
|
|
|
|
{ "HUP", SIGHUP }, /* 1 */
|
|
|
|
|
{ "INT", SIGINT }, /* 2 */
|
|
|
|
|
{ "QUIT", SIGQUIT }, /* 3 */
|
|
|
|
|
{ "ILL", SIGILL }, /* 4 */
|
|
|
|
|
{ "ABRT", SIGABRT }, /* 6 */
|
|
|
|
|
{ "FPE", SIGFPE }, /* 8 */
|
|
|
|
|
{ "KILL", SIGKILL }, /* 9 */
|
|
|
|
|
{ "SEGV", SIGSEGV }, /* 11 */
|
|
|
|
|
{ "PIPE", SIGPIPE }, /* 13 */
|
|
|
|
|
{ "ALRM", SIGALRM }, /* 14 */
|
|
|
|
|
{ "TERM", SIGTERM }, /* 15 */
|
|
|
|
|
{ "USR1", SIGUSR1 }, /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
|
|
|
|
|
{ "USR2", SIGUSR2 }, /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
|
|
|
|
|
{ "CHLD", SIGCHLD }, /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
|
|
|
|
|
{ "CONT", SIGCONT }, /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
|
|
|
|
|
{ "STOP", SIGSTOP }, /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
|
|
|
|
|
{ "TSTP", SIGTSTP }, /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
|
|
|
|
|
{ "TTIN", SIGTTIN }, /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
|
|
|
|
|
{ "TTOU", SIGTTOU }, /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
|
|
|
|
|
/* Miscellaneous other signals */
|
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
|
{ "TRAP", SIGTRAP }, /* 5 */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGIOT
|
|
|
|
|
{ "IOT", SIGIOT }, /* 6, same as SIGABRT */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGEMT
|
|
|
|
|
{ "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGBUS
|
|
|
|
|
{ "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGSYS
|
|
|
|
|
{ "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGSTKFLT
|
|
|
|
|
{ "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGURG
|
|
|
|
|
{ "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGIO
|
|
|
|
|
{ "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGPOLL
|
|
|
|
|
{ "POLL", SIGPOLL }, /* same as SIGIO */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGCLD
|
|
|
|
|
{ "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
|
{ "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
|
2006-12-06 17:25:32 -06:00
|
|
|
|
#endif
|
2006-12-06 17:25:37 -06:00
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
|
{ "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGVTALRM
|
|
|
|
|
{ "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGPROF
|
|
|
|
|
{ "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGPWR
|
|
|
|
|
{ "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
|
{ "INFO", SIGINFO }, /* 29 (alpha) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGLOST
|
|
|
|
|
{ "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
|
{ "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGUNUSED
|
|
|
|
|
{ "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
|
|
|
|
|
#endif
|
|
|
|
|
};
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
extern char *mybasename(char *);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
int signame_to_signum (char *sig);
|
2006-12-06 17:25:41 -06:00
|
|
|
|
int arg_to_signum (char *arg, int mask);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
void nosig (char *name);
|
|
|
|
|
void printsig (int sig);
|
|
|
|
|
void printsignals (FILE *fp);
|
|
|
|
|
int usage (int status);
|
|
|
|
|
int kill_verbose (char *procname, int pid, int sig);
|
|
|
|
|
|
|
|
|
|
extern int *get_pids (char *, int);
|
|
|
|
|
|
2006-12-06 17:25:46 -06:00
|
|
|
|
static char *progname;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int errors, numsig, pid;
|
2006-12-06 17:25:46 -06:00
|
|
|
|
char *ep, *arg, *p;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
int do_pid, do_kill, check_all;
|
|
|
|
|
int *pids, *ip;
|
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
|
progname = argv[0];
|
|
|
|
|
if ((p = strrchr(progname, '/')) != NULL)
|
|
|
|
|
progname = p+1;
|
|
|
|
|
|
2006-12-06 17:25:39 -06:00
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
|
numsig = SIGTERM;
|
2006-12-06 17:25:46 -06:00
|
|
|
|
do_pid = (! strcmp (progname, "pid")); /* Yecch */
|
2006-12-06 17:25:32 -06:00
|
|
|
|
do_kill = 0;
|
|
|
|
|
check_all = 0;
|
|
|
|
|
|
|
|
|
|
/* loop through the arguments.
|
|
|
|
|
actually, -a is the only option can be used with other options.
|
|
|
|
|
`kill' is basically a one-option-at-most program. */
|
|
|
|
|
for (argc--, argv++; argc > 0; argc--, argv++) {
|
|
|
|
|
arg = *argv;
|
|
|
|
|
if (*arg != '-') {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
|
if (! strcmp (arg, "--")) {
|
|
|
|
|
argc--, argv++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (! strcmp (arg, "-v") || ! strcmp (arg, "-V") ||
|
|
|
|
|
! strcmp (arg, "--version")) {
|
|
|
|
|
printf(_("%s from %s\n"), progname, util_linux_version);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (! strcmp (arg, "-a")) {
|
|
|
|
|
check_all++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (! strcmp (arg, "-l")) {
|
|
|
|
|
if (argc < 2) {
|
|
|
|
|
printsignals (stdout);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (argc > 2) {
|
|
|
|
|
return usage (1);
|
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
|
/* argc == 2, accept "kill -l $?" */
|
2006-12-06 17:25:32 -06:00
|
|
|
|
arg = argv[1];
|
2006-12-06 17:25:41 -06:00
|
|
|
|
if ((numsig = arg_to_signum (arg, 1)) < 0) {
|
2006-12-06 17:25:46 -06:00
|
|
|
|
fprintf (stderr, _("%s: unknown signal %s\n"), progname, arg);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
printsig (numsig);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (! strcmp (arg, "-p")) {
|
|
|
|
|
do_pid++;
|
|
|
|
|
if (do_kill)
|
|
|
|
|
return usage (1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (! strcmp (arg, "-s")) {
|
|
|
|
|
if (argc < 2) {
|
|
|
|
|
return usage (1);
|
|
|
|
|
}
|
|
|
|
|
do_kill++;
|
|
|
|
|
if (do_pid)
|
|
|
|
|
return usage (1);
|
|
|
|
|
argc--, argv++;
|
|
|
|
|
arg = *argv;
|
2006-12-06 17:25:41 -06:00
|
|
|
|
if ((numsig = arg_to_signum (arg, 0)) < 0) {
|
2006-12-06 17:25:32 -06:00
|
|
|
|
nosig (arg);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* `arg' begins with a dash but is not a known option.
|
2006-12-06 17:25:34 -06:00
|
|
|
|
so it's probably something like -HUP, or -1/-n
|
|
|
|
|
try to deal with it.
|
|
|
|
|
-n could be signal n, or pid -n (i.e. process group n).
|
2006-12-06 17:25:46 -06:00
|
|
|
|
In case of doubt POSIX tells us to assume a signal.
|
2006-12-06 17:25:34 -06:00
|
|
|
|
If a signal has been parsed, assume it's a pid, break */
|
|
|
|
|
if (do_kill)
|
|
|
|
|
break;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
arg++;
|
2006-12-06 17:25:41 -06:00
|
|
|
|
if ((numsig = arg_to_signum (arg, 0)) < 0) {
|
2006-12-06 17:25:32 -06:00
|
|
|
|
return usage (1);
|
|
|
|
|
}
|
|
|
|
|
do_kill++;
|
|
|
|
|
if (do_pid)
|
|
|
|
|
return usage (1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! *argv) {
|
|
|
|
|
return usage (1);
|
|
|
|
|
}
|
|
|
|
|
if (do_pid) {
|
|
|
|
|
numsig = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* we're done with the options.
|
|
|
|
|
the rest of the arguments should be process ids and names.
|
|
|
|
|
kill them. */
|
|
|
|
|
for (errors = 0; (arg = *argv) != NULL; argv++) {
|
|
|
|
|
pid = strtol (arg, &ep, 10);
|
|
|
|
|
if (! *ep)
|
|
|
|
|
errors += kill_verbose (arg, pid, numsig);
|
|
|
|
|
else {
|
|
|
|
|
pids = get_pids (arg, check_all);
|
|
|
|
|
if (! pids) {
|
|
|
|
|
errors++;
|
2006-12-06 17:25:39 -06:00
|
|
|
|
fprintf (stderr, _("%s: can't find process \"%s\"\n"),
|
2006-12-06 17:25:46 -06:00
|
|
|
|
progname, arg);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (ip = pids; *ip >= 0; ip++)
|
|
|
|
|
errors += kill_verbose (arg, *ip, numsig);
|
|
|
|
|
free (pids);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (errors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int signame_to_signum (char *sig)
|
|
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
if (! strncasecmp (sig, "sig", 3))
|
|
|
|
|
sig += 3;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
for (n = 0; n < SIZE(sys_signame); n++) {
|
|
|
|
|
if (! strcasecmp (sys_signame[n].name, sig))
|
|
|
|
|
return sys_signame[n].val;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
}
|
|
|
|
|
return (-1);
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
|
int arg_to_signum (char *arg, int maskbit)
|
2006-12-06 17:25:32 -06:00
|
|
|
|
{
|
|
|
|
|
int numsig;
|
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
|
|
if (isdigit (*arg)) {
|
|
|
|
|
numsig = strtol (arg, &ep, 10);
|
2006-12-06 17:25:41 -06:00
|
|
|
|
if (numsig >= NSIG && maskbit && (numsig & 128) != 0)
|
|
|
|
|
numsig -= 128;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
if (*ep != 0 || numsig < 0 || numsig >= NSIG)
|
|
|
|
|
return (-1);
|
|
|
|
|
return (numsig);
|
|
|
|
|
}
|
|
|
|
|
return (signame_to_signum (arg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nosig (char *name)
|
|
|
|
|
{
|
2006-12-06 17:25:46 -06:00
|
|
|
|
fprintf (stderr, _("%s: unknown signal %s; valid signals:\n"), progname, name);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
printsignals (stderr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void printsig (int sig)
|
|
|
|
|
{
|
2006-12-06 17:25:37 -06:00
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
for (n = 0; n < SIZE(sys_signame); n++) {
|
|
|
|
|
if (sys_signame[n].val == sig) {
|
|
|
|
|
printf ("%s\n", sys_signame[n].name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("%d\n", sig);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void printsignals (FILE *fp)
|
|
|
|
|
{
|
2006-12-06 17:25:37 -06:00
|
|
|
|
int n, lth;
|
|
|
|
|
int lpos = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
|
for (n = 0; n < SIZE(sys_signame); n++) {
|
|
|
|
|
lth = 1+strlen(sys_signame[n].name);
|
|
|
|
|
if (lpos+lth > 72) {
|
2006-12-06 17:25:32 -06:00
|
|
|
|
fputc ('\n', fp);
|
2006-12-06 17:25:37 -06:00
|
|
|
|
lpos = 0;
|
|
|
|
|
} else if (lpos)
|
2006-12-06 17:25:32 -06:00
|
|
|
|
fputc (' ', fp);
|
2006-12-06 17:25:37 -06:00
|
|
|
|
lpos += lth;
|
|
|
|
|
fputs (sys_signame[n].name, fp);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
}
|
2006-12-06 17:25:37 -06:00
|
|
|
|
fputc ('\n', fp);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int usage (int status)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
fp = (status == 0 ? stdout : stderr);
|
2006-12-06 17:25:46 -06:00
|
|
|
|
fprintf (fp, _("usage: %s [ -s signal | -p ] [ -a ] pid ...\n"), progname);
|
|
|
|
|
fprintf (fp, _(" %s -l [ signal ]\n"), progname);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int kill_verbose (char *procname, int pid, int sig)
|
|
|
|
|
{
|
|
|
|
|
if (sig < 0) {
|
|
|
|
|
printf ("%d\n", pid);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (kill (pid, sig) < 0) {
|
2006-12-06 17:25:46 -06:00
|
|
|
|
fprintf (stderr, "%s ", progname);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
perror (procname);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|