2000-02-18 03:38:33 +08:00
|
|
|
/* kill.cc
|
|
|
|
|
2002-05-14 07:42:32 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2000-07-30 00:26:37 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <windows.h>
|
2000-09-13 10:48:39 +08:00
|
|
|
#include <sys/cygwin.h>
|
2002-05-13 13:13:58 +08:00
|
|
|
#include <getopt.h>
|
|
|
|
|
2002-05-14 07:42:32 +08:00
|
|
|
static const char version[] = "$Revision$";
|
|
|
|
static char *prog_name;
|
|
|
|
|
2002-05-13 13:13:58 +08:00
|
|
|
static struct option longopts[] =
|
|
|
|
{
|
|
|
|
{"help", no_argument, NULL, 'h' },
|
|
|
|
{"list", optional_argument, NULL, 'l'},
|
|
|
|
{"force", no_argument, NULL, 'f'},
|
|
|
|
{"signal", required_argument, NULL, 's'},
|
2002-05-14 07:42:32 +08:00
|
|
|
{"version", no_argument, NULL, 'v'},
|
2002-05-13 13:13:58 +08:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2002-05-14 07:42:32 +08:00
|
|
|
static char opts[] = "hl::fs:v";
|
2002-05-13 13:13:58 +08:00
|
|
|
|
|
|
|
extern "C" const char *strsigno (int);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-03-12 02:33:02 +08:00
|
|
|
static void
|
2002-05-13 13:23:37 +08:00
|
|
|
usage (FILE *where = stderr)
|
2002-03-12 02:33:02 +08:00
|
|
|
{
|
2002-05-14 07:42:32 +08:00
|
|
|
fprintf (where , ""
|
|
|
|
"Usage: %s [-f] [-signal] [-s signal] pid1 [pid2 ...]\n"
|
|
|
|
" %s -l [signal]\n"
|
2003-04-27 05:52:03 +08:00
|
|
|
"Send signals to processes\n"
|
|
|
|
"\n"
|
2002-05-14 07:42:32 +08:00
|
|
|
" -f, --force force, using win32 interface if necessary\n"
|
|
|
|
" -l, --list print a list of signal names\n"
|
|
|
|
" -s, --signal send signal (use %s --list for a list)\n"
|
|
|
|
" -h, --help output usage information and exit\n"
|
|
|
|
" -v, --version output version information and exit\n"
|
|
|
|
"", prog_name, prog_name, prog_name);
|
2002-05-13 13:23:37 +08:00
|
|
|
exit (where == stderr ? 1 : 0);
|
2002-03-12 02:33:02 +08:00
|
|
|
}
|
|
|
|
|
2002-05-14 07:42:32 +08:00
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
|
|
|
const char *v = strchr (version, ':');
|
|
|
|
int len;
|
|
|
|
if (!v)
|
|
|
|
{
|
|
|
|
v = "?";
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v += 2;
|
|
|
|
len = strchr (v, ' ') - v;
|
|
|
|
}
|
|
|
|
printf ("\
|
|
|
|
%s (cygwin) %.*s\n\
|
|
|
|
Process Signaller\n\
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.\n\
|
2002-09-16 03:24:37 +08:00
|
|
|
Compiled on %s\n\
|
|
|
|
", prog_name, len, v, __DATE__);
|
2002-05-14 07:42:32 +08:00
|
|
|
}
|
|
|
|
|
2002-03-12 02:33:02 +08:00
|
|
|
static int
|
2002-05-13 13:13:58 +08:00
|
|
|
getsig (const char *in_sig)
|
2002-03-12 02:33:02 +08:00
|
|
|
{
|
2002-05-13 13:13:58 +08:00
|
|
|
const char *sig;
|
2002-03-12 02:33:02 +08:00
|
|
|
char buf[80];
|
2002-05-13 13:13:58 +08:00
|
|
|
int intsig;
|
2002-03-12 02:33:02 +08:00
|
|
|
|
|
|
|
if (strncmp (in_sig, "SIG", 3) == 0)
|
|
|
|
sig = in_sig;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf (buf, "SIG%s", in_sig);
|
|
|
|
sig = buf;
|
|
|
|
}
|
2002-05-13 13:13:58 +08:00
|
|
|
intsig = strtosigno (sig) ?: atoi (in_sig);
|
|
|
|
char *p;
|
|
|
|
if (!intsig && (strcmp (buf, "SIG0") != 0 && (strtol (in_sig, &p, 10) != 0 || *p)))
|
|
|
|
intsig = -1;
|
|
|
|
return intsig;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_for_unknown_sig (int sig, const char *sigstr)
|
|
|
|
{
|
|
|
|
if (sig < 0 || sig > NSIG)
|
|
|
|
{
|
2002-05-14 07:42:32 +08:00
|
|
|
fprintf (stderr, "%s: unknown signal: %s\n", prog_name, sigstr);
|
2002-05-13 13:13:58 +08:00
|
|
|
usage ();
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
listsig (const char *in_sig)
|
|
|
|
{
|
|
|
|
int sig;
|
|
|
|
if (!in_sig)
|
|
|
|
for (sig = 1; sig < NSIG; sig++)
|
|
|
|
printf ("%s%c", strsigno (sig) + 3, (sig < NSIG - 1) ? ' ' : '\n');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sig = getsig (in_sig);
|
|
|
|
test_for_unknown_sig (sig, in_sig);
|
2002-05-13 13:35:44 +08:00
|
|
|
if (atoi (in_sig) == sig)
|
|
|
|
puts (strsigno (sig) + 3);
|
|
|
|
else
|
|
|
|
printf ("%d\n", sig);
|
2002-05-13 13:13:58 +08:00
|
|
|
}
|
2002-03-12 02:33:02 +08:00
|
|
|
}
|
|
|
|
|
2004-03-14 02:11:13 +08:00
|
|
|
static void
|
|
|
|
get_debug_priv (void)
|
|
|
|
{
|
|
|
|
HANDLE tok;
|
|
|
|
LUID luid;
|
|
|
|
TOKEN_PRIVILEGES tkp;
|
|
|
|
|
|
|
|
if (!OpenProcessToken (GetCurrentProcess (),
|
|
|
|
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tok))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!LookupPrivilegeValue (NULL, SE_DEBUG_NAME, &luid))
|
|
|
|
{
|
|
|
|
CloseHandle (tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tkp.PrivilegeCount = 1;
|
|
|
|
tkp.Privileges[0].Luid = luid;
|
|
|
|
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
|
|
|
|
|
|
|
AdjustTokenPrivileges (tok, FALSE, &tkp, sizeof tkp, NULL, NULL);
|
|
|
|
|
|
|
|
CloseHandle (tok);
|
|
|
|
}
|
|
|
|
|
2002-03-12 02:33:02 +08:00
|
|
|
static void __stdcall
|
|
|
|
forcekill (int pid, int sig, int wait)
|
|
|
|
{
|
2004-03-14 02:11:13 +08:00
|
|
|
// try to acquire SeDebugPrivilege
|
|
|
|
get_debug_priv();
|
|
|
|
|
2002-03-12 02:33:02 +08:00
|
|
|
external_pinfo *p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
|
2003-09-01 09:57:44 +08:00
|
|
|
DWORD dwpid = p ? p->dwProcessId : (DWORD) pid;
|
|
|
|
HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) dwpid);
|
2002-03-12 02:33:02 +08:00
|
|
|
if (!h)
|
2003-09-01 09:57:44 +08:00
|
|
|
{
|
2003-09-14 02:20:52 +08:00
|
|
|
fprintf (stderr, "couldn't open pid %u\n", (unsigned) dwpid);
|
|
|
|
return;
|
2003-09-01 09:57:44 +08:00
|
|
|
}
|
2002-03-12 02:33:02 +08:00
|
|
|
if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
|
2003-09-14 02:20:52 +08:00
|
|
|
if (!TerminateProcess (h, sig << 8)
|
|
|
|
&& WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
|
2003-09-01 10:40:16 +08:00
|
|
|
fprintf (stderr, "couldn't kill pid %u, %u\n", (unsigned) dwpid,
|
|
|
|
(unsigned) GetLastError ());
|
2002-03-12 02:33:02 +08:00
|
|
|
CloseHandle (h);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
int
|
2000-07-30 00:26:37 +08:00
|
|
|
main (int argc, char **argv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int sig = SIGTERM;
|
2000-07-30 00:26:37 +08:00
|
|
|
int force = 0;
|
2000-11-02 09:03:00 +08:00
|
|
|
int ret = 0;
|
2003-09-18 09:46:18 +08:00
|
|
|
char *gotasig = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-05-14 07:42:32 +08:00
|
|
|
prog_name = strrchr (argv[0], '/');
|
|
|
|
if (prog_name == NULL)
|
|
|
|
prog_name = strrchr (argv[0], '\\');
|
|
|
|
if (prog_name == NULL)
|
|
|
|
prog_name = argv[0];
|
|
|
|
else
|
|
|
|
prog_name++;
|
|
|
|
|
2000-07-30 00:26:37 +08:00
|
|
|
if (argc == 1)
|
2000-02-18 03:38:33 +08:00
|
|
|
usage ();
|
|
|
|
|
2002-05-13 13:13:58 +08:00
|
|
|
opterr = 0;
|
2003-09-21 04:24:33 +08:00
|
|
|
|
|
|
|
char *p;
|
|
|
|
int pid = 0;
|
|
|
|
|
2002-05-13 13:13:58 +08:00
|
|
|
for (;;)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-05-13 13:13:58 +08:00
|
|
|
int ch;
|
|
|
|
char **av = argv + optind;
|
|
|
|
if ((ch = getopt_long (argc, argv, opts, longopts, NULL)) == EOF)
|
|
|
|
break;
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 's':
|
2003-09-18 09:46:18 +08:00
|
|
|
gotasig = optarg;
|
|
|
|
sig = getsig (gotasig);
|
2002-05-13 13:13:58 +08:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (!optarg)
|
|
|
|
{
|
|
|
|
optarg = argv[optind];
|
|
|
|
if (optarg)
|
|
|
|
{
|
|
|
|
optind++;
|
|
|
|
optreset = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argv[optind])
|
|
|
|
usage ();
|
|
|
|
listsig (optarg);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
2002-05-13 13:23:37 +08:00
|
|
|
case 'h':
|
|
|
|
usage (stdout);
|
|
|
|
break;
|
2002-05-14 07:42:32 +08:00
|
|
|
case 'v':
|
|
|
|
print_version ();
|
|
|
|
break;
|
2002-05-13 13:13:58 +08:00
|
|
|
case '?':
|
2003-09-18 09:46:18 +08:00
|
|
|
if (gotasig)
|
2003-09-21 04:24:33 +08:00
|
|
|
{
|
|
|
|
pid = strtol (argv[optind], &p, 10);
|
|
|
|
if (pid < 0)
|
|
|
|
goto out;
|
|
|
|
usage ();
|
|
|
|
}
|
2002-05-13 13:13:58 +08:00
|
|
|
optreset = 1;
|
|
|
|
optind = 1 + av - argv;
|
2003-09-18 09:46:18 +08:00
|
|
|
gotasig = *av + 1;
|
|
|
|
sig = getsig (gotasig);
|
2002-05-13 13:13:58 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-09-21 04:24:33 +08:00
|
|
|
out:
|
2003-09-18 09:46:18 +08:00
|
|
|
test_for_unknown_sig (sig, gotasig);
|
2002-05-13 13:13:58 +08:00
|
|
|
|
|
|
|
argv += optind;
|
2000-07-30 00:26:37 +08:00
|
|
|
while (*argv != NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-21 04:24:33 +08:00
|
|
|
if (!pid)
|
|
|
|
pid = strtol (*argv, &p, 10);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (*p != '\0')
|
2001-10-08 11:06:25 +08:00
|
|
|
{
|
2002-05-14 07:42:32 +08:00
|
|
|
fprintf (stderr, "%s: illegal pid: %s\n", prog_name, *argv);
|
2001-10-08 11:06:25 +08:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else if (kill (pid, sig) == 0)
|
|
|
|
{
|
|
|
|
if (force)
|
|
|
|
forcekill (pid, sig, 1);
|
|
|
|
}
|
|
|
|
else if (force && sig != 0)
|
|
|
|
forcekill (pid, sig, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
2001-10-08 11:06:25 +08:00
|
|
|
char buf[1000];
|
2002-05-14 07:42:32 +08:00
|
|
|
sprintf (buf, "%s %d", prog_name, pid);
|
2001-10-08 11:06:25 +08:00
|
|
|
perror (buf);
|
|
|
|
ret = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-07-30 00:26:37 +08:00
|
|
|
argv++;
|
2003-09-21 04:24:33 +08:00
|
|
|
pid = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-11-02 09:03:00 +08:00
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2004-03-14 02:11:13 +08:00
|
|
|
|