2000-02-17 20:38:33 +01:00
|
|
|
/* kill.cc
|
|
|
|
|
2013-01-21 17:28:27 +01:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
|
2015-11-26 22:30:43 +01:00
|
|
|
2009, 2011, 2015 Red Hat, Inc.
|
2000-02-17 20:38:33 +01: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-29 18:26:37 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <windows.h>
|
2000-09-13 04:48:39 +02:00
|
|
|
#include <sys/cygwin.h>
|
2011-10-10 16:57:48 +02:00
|
|
|
#include <cygwin/version.h>
|
2002-05-13 07:13:58 +02:00
|
|
|
#include <getopt.h>
|
2004-11-13 17:30:19 +01:00
|
|
|
#include <limits.h>
|
2002-05-13 07:13:58 +02:00
|
|
|
|
2002-05-14 01:42:32 +02:00
|
|
|
static char *prog_name;
|
|
|
|
|
2002-05-13 07:13:58 +02: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'},
|
2011-10-10 16:57:48 +02:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
2002-05-13 07:13:58 +02:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2011-10-10 16:57:48 +02:00
|
|
|
static char opts[] = "hl::fs:V";
|
2002-05-13 07:13:58 +02:00
|
|
|
|
2002-03-11 19:33:02 +01:00
|
|
|
static void
|
2002-05-13 07:23:37 +02:00
|
|
|
usage (FILE *where = stderr)
|
2002-03-11 19:33:02 +01:00
|
|
|
{
|
2002-05-14 01:42:32 +02:00
|
|
|
fprintf (where , ""
|
2011-10-10 16:57:48 +02:00
|
|
|
"Usage: %1$s [-f] [-signal] [-s signal] pid1 [pid2 ...]\n"
|
|
|
|
" %1$s -l [signal]\n"
|
|
|
|
"\n"
|
2003-04-26 23:52:03 +02:00
|
|
|
"Send signals to processes\n"
|
|
|
|
"\n"
|
2002-05-14 01:42:32 +02:00
|
|
|
" -f, --force force, using win32 interface if necessary\n"
|
|
|
|
" -l, --list print a list of signal names\n"
|
2011-10-10 16:57:48 +02:00
|
|
|
" -s, --signal send signal (use %1$s --list for a list)\n"
|
2002-05-14 01:42:32 +02:00
|
|
|
" -h, --help output usage information and exit\n"
|
2011-10-10 16:57:48 +02:00
|
|
|
" -V, --version output version information and exit\n"
|
|
|
|
"\n", prog_name);
|
2002-05-13 07:23:37 +02:00
|
|
|
exit (where == stderr ? 1 : 0);
|
2002-03-11 19:33:02 +01:00
|
|
|
}
|
|
|
|
|
2002-05-14 01:42:32 +02:00
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
2011-10-10 16:57:48 +02:00
|
|
|
printf ("kill (cygwin) %d.%d.%d\n"
|
2011-12-18 00:39:47 +01:00
|
|
|
"Process Signaller\n"
|
|
|
|
"Copyright (C) 1996 - %s Red Hat, Inc.\n"
|
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 16:57:48 +02:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 00:39:47 +01:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2002-05-14 01:42:32 +02:00
|
|
|
}
|
|
|
|
|
2007-07-10 02:12:54 +02:00
|
|
|
static const char *
|
|
|
|
strsigno (int signo)
|
|
|
|
{
|
2015-11-26 22:30:43 +01:00
|
|
|
if (signo > 0 && signo < NSIG)
|
2007-07-10 02:12:54 +02:00
|
|
|
return sys_sigabbrev[signo];
|
|
|
|
static char buf[sizeof ("Unknown signal") + 32];
|
|
|
|
sprintf (buf, "Unknown signal %d", signo);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-03-11 19:33:02 +01:00
|
|
|
static int
|
2002-05-13 07:13:58 +02:00
|
|
|
getsig (const char *in_sig)
|
2002-03-11 19:33:02 +01:00
|
|
|
{
|
2002-05-13 07:13:58 +02:00
|
|
|
const char *sig;
|
2002-03-11 19:33:02 +01:00
|
|
|
char buf[80];
|
2002-05-13 07:13:58 +02:00
|
|
|
int intsig;
|
2002-03-11 19:33:02 +01:00
|
|
|
|
|
|
|
if (strncmp (in_sig, "SIG", 3) == 0)
|
|
|
|
sig = in_sig;
|
|
|
|
else
|
|
|
|
{
|
2005-03-08 05:06:26 +01:00
|
|
|
sprintf (buf, "SIG%-.20s", in_sig);
|
2002-03-11 19:33:02 +01:00
|
|
|
sig = buf;
|
|
|
|
}
|
2002-05-13 07:13:58 +02: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)
|
|
|
|
{
|
2011-10-10 16:57:48 +02:00
|
|
|
fprintf (stderr, "%1$s: unknown signal: %2$s\n"
|
|
|
|
"Try `%1$s --help' for more information.\n",
|
|
|
|
prog_name, sigstr);
|
2002-05-13 07:13:58 +02:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
listsig (const char *in_sig)
|
|
|
|
{
|
|
|
|
int sig;
|
|
|
|
if (!in_sig)
|
2004-05-27 17:15:51 +02:00
|
|
|
for (sig = 1; sig < NSIG - 1; sig++)
|
2002-05-13 07:13:58 +02:00
|
|
|
printf ("%s%c", strsigno (sig) + 3, (sig < NSIG - 1) ? ' ' : '\n');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sig = getsig (in_sig);
|
|
|
|
test_for_unknown_sig (sig, in_sig);
|
2015-11-26 22:30:43 +01:00
|
|
|
if (sig && atoi (in_sig) == sig)
|
2002-05-13 07:35:44 +02:00
|
|
|
puts (strsigno (sig) + 3);
|
|
|
|
else
|
|
|
|
printf ("%d\n", sig);
|
2002-05-13 07:13:58 +02:00
|
|
|
}
|
2002-03-11 19:33:02 +01:00
|
|
|
}
|
|
|
|
|
2004-03-13 19:11:13 +01:00
|
|
|
static void
|
|
|
|
get_debug_priv (void)
|
|
|
|
{
|
|
|
|
HANDLE tok;
|
|
|
|
LUID luid;
|
|
|
|
TOKEN_PRIVILEGES tkp;
|
|
|
|
|
|
|
|
if (!OpenProcessToken (GetCurrentProcess (),
|
2011-12-18 00:39:47 +01:00
|
|
|
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tok))
|
2004-03-13 19:11:13 +01:00
|
|
|
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-11 19:33:02 +01:00
|
|
|
static void __stdcall
|
|
|
|
forcekill (int pid, int sig, int wait)
|
|
|
|
{
|
2004-03-13 19:11:13 +01:00
|
|
|
// try to acquire SeDebugPrivilege
|
|
|
|
get_debug_priv();
|
|
|
|
|
2004-11-13 17:30:19 +01:00
|
|
|
external_pinfo *p = NULL;
|
|
|
|
/* cygwin_internal misinterprets negative pids (Win9x pids) */
|
|
|
|
if (pid > 0)
|
|
|
|
p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
|
2003-09-01 03:57:44 +02:00
|
|
|
DWORD dwpid = p ? p->dwProcessId : (DWORD) pid;
|
|
|
|
HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) dwpid);
|
2002-03-11 19:33:02 +01:00
|
|
|
if (!h)
|
2003-09-01 03:57:44 +02:00
|
|
|
{
|
2011-12-18 00:39:47 +01:00
|
|
|
if (!wait || GetLastError () != ERROR_INVALID_PARAMETER)
|
|
|
|
fprintf (stderr, "%s: couldn't open pid %u\n",
|
|
|
|
prog_name, (unsigned) dwpid);
|
2003-09-13 20:20:52 +02:00
|
|
|
return;
|
2003-09-01 03:57:44 +02:00
|
|
|
}
|
2002-03-11 19:33:02 +01:00
|
|
|
if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
|
2004-11-13 17:30:19 +01:00
|
|
|
if (sig && !TerminateProcess (h, sig << 8)
|
2003-09-13 20:20:52 +02:00
|
|
|
&& WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
|
2013-04-23 11:44:36 +02:00
|
|
|
fprintf (stderr, "%s: couldn't kill pid %u, %u\n",
|
|
|
|
prog_name, (unsigned) dwpid, (unsigned int) GetLastError ());
|
2002-03-11 19:33:02 +01:00
|
|
|
CloseHandle (h);
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
int
|
2000-07-29 18:26:37 +02:00
|
|
|
main (int argc, char **argv)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
int sig = SIGTERM;
|
2000-07-29 18:26:37 +02:00
|
|
|
int force = 0;
|
2000-11-02 02:03:00 +01:00
|
|
|
int ret = 0;
|
2003-09-18 03:46:18 +02:00
|
|
|
char *gotasig = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-10-10 16:57:48 +02:00
|
|
|
prog_name = program_invocation_short_name;
|
2002-05-14 01:42:32 +02:00
|
|
|
|
2000-07-29 18:26:37 +02:00
|
|
|
if (argc == 1)
|
2000-02-17 20:38:33 +01:00
|
|
|
usage ();
|
|
|
|
|
2002-05-13 07:13:58 +02:00
|
|
|
opterr = 0;
|
2003-09-20 22:24:33 +02:00
|
|
|
|
|
|
|
char *p;
|
2004-11-13 17:30:19 +01:00
|
|
|
long long int pid = 0;
|
2003-09-20 22:24:33 +02:00
|
|
|
|
2002-05-13 07:13:58 +02:00
|
|
|
for (;;)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-05-13 07:13:58 +02: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 03:46:18 +02:00
|
|
|
gotasig = optarg;
|
|
|
|
sig = getsig (gotasig);
|
2002-05-13 07:13:58 +02: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 07:23:37 +02:00
|
|
|
case 'h':
|
|
|
|
usage (stdout);
|
|
|
|
break;
|
2011-10-10 16:57:48 +02:00
|
|
|
case 'V':
|
2002-05-14 01:42:32 +02:00
|
|
|
print_version ();
|
|
|
|
break;
|
2002-05-13 07:13:58 +02:00
|
|
|
case '?':
|
2003-09-18 03:46:18 +02:00
|
|
|
if (gotasig)
|
2003-09-20 22:24:33 +02:00
|
|
|
{
|
2009-10-12 18:01:44 +02:00
|
|
|
--optind;
|
|
|
|
goto out;
|
2003-09-20 22:24:33 +02:00
|
|
|
}
|
2002-05-13 07:13:58 +02:00
|
|
|
optreset = 1;
|
|
|
|
optind = 1 + av - argv;
|
2003-09-18 03:46:18 +02:00
|
|
|
gotasig = *av + 1;
|
|
|
|
sig = getsig (gotasig);
|
2002-05-13 07:13:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2003-09-20 22:24:33 +02:00
|
|
|
out:
|
2003-09-18 03:46:18 +02:00
|
|
|
test_for_unknown_sig (sig, gotasig);
|
2002-05-13 07:13:58 +02:00
|
|
|
|
|
|
|
argv += optind;
|
2000-07-29 18:26:37 +02:00
|
|
|
while (*argv != NULL)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-09-20 22:24:33 +02:00
|
|
|
if (!pid)
|
2004-11-13 17:30:19 +01:00
|
|
|
pid = strtoll (*argv, &p, 10);
|
|
|
|
if (*p != '\0'
|
2013-04-23 11:44:36 +02:00
|
|
|
|| (!force && (pid < INT_MIN || pid > INT_MAX))
|
|
|
|
|| (force && (pid <= 0 || pid > UINT_MAX)))
|
2001-10-08 05:06:25 +02:00
|
|
|
{
|
2002-05-14 01:42:32 +02:00
|
|
|
fprintf (stderr, "%s: illegal pid: %s\n", prog_name, *argv);
|
2001-10-08 05:06:25 +02:00
|
|
|
ret = 1;
|
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
else if (pid <= INT_MAX && kill ((pid_t) pid, sig) == 0)
|
2001-10-08 05:06:25 +02:00
|
|
|
{
|
|
|
|
if (force)
|
2004-11-13 17:30:19 +01:00
|
|
|
forcekill ((pid_t) pid, sig, 1);
|
2001-10-08 05:06:25 +02:00
|
|
|
}
|
2004-11-13 17:30:19 +01:00
|
|
|
else if (force)
|
|
|
|
forcekill ((pid_t) pid, sig, 0);
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
|
|
|
{
|
2001-10-08 05:06:25 +02:00
|
|
|
char buf[1000];
|
2004-11-13 17:30:19 +01:00
|
|
|
sprintf (buf, "%s: %lld", prog_name, pid);
|
2001-10-08 05:06:25 +02:00
|
|
|
perror (buf);
|
|
|
|
ret = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-07-29 18:26:37 +02:00
|
|
|
argv++;
|
2003-09-20 22:24:33 +02:00
|
|
|
pid = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-11-02 02:03:00 +01:00
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2004-03-13 19:11:13 +01:00
|
|
|
|