2000-02-18 03:38:33 +08:00
|
|
|
/* kill.cc
|
|
|
|
|
|
|
|
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>
|
2011-10-10 22:57:48 +08:00
|
|
|
#include <cygwin/version.h>
|
2002-05-13 13:13:58 +08:00
|
|
|
#include <getopt.h>
|
2004-11-14 00:30:19 +08:00
|
|
|
#include <limits.h>
|
2002-05-13 13:13:58 +08:00
|
|
|
|
2002-05-14 07:42:32 +08:00
|
|
|
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'},
|
2019-02-03 04:02:00 +08:00
|
|
|
{"winpid", no_argument, NULL, 'W'},
|
2011-10-10 22:57:48 +08:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
2002-05-13 13:13:58 +08:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2019-02-03 04:02:00 +08:00
|
|
|
static char opts[] = "hl::fs:WV";
|
2002-05-13 13:13:58 +08:00
|
|
|
|
2020-08-07 19:29:43 +08:00
|
|
|
static void __attribute__ ((__noreturn__))
|
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 , ""
|
2019-02-03 04:02:00 +08:00
|
|
|
"Usage: %1$s [-fW] [-signal] [-s signal] pid1 [pid2 ...]\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
" %1$s -l [signal]\n"
|
|
|
|
"\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"
|
2011-10-10 22:57:48 +08:00
|
|
|
" -s, --signal send signal (use %1$s --list for a list)\n"
|
2019-02-03 04:02:00 +08:00
|
|
|
" -W, --winpid specified pids are windows PIDs, not Cygwin PIDs\n"
|
2019-02-03 04:15:50 +08:00
|
|
|
" (use with extreme caution!)\n"
|
2002-05-14 07:42:32 +08:00
|
|
|
" -h, --help output usage information and exit\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
" -V, --version output version information and exit\n"
|
|
|
|
"\n", 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 ()
|
|
|
|
{
|
2011-10-10 22:57:48 +08:00
|
|
|
printf ("kill (cygwin) %d.%d.%d\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"Process Signaller\n"
|
2016-05-24 17:16:39 +08:00
|
|
|
"Copyright (C) 1996 - %s Cygwin Authors\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 07:39:47 +08:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2002-05-14 07:42:32 +08:00
|
|
|
}
|
|
|
|
|
2007-07-10 08:12:54 +08:00
|
|
|
static const char *
|
|
|
|
strsigno (int signo)
|
|
|
|
{
|
2015-11-27 05:30:43 +08:00
|
|
|
if (signo > 0 && signo < NSIG)
|
2007-07-10 08:12:54 +08:00
|
|
|
return sys_sigabbrev[signo];
|
|
|
|
static char buf[sizeof ("Unknown signal") + 32];
|
|
|
|
sprintf (buf, "Unknown signal %d", signo);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2005-03-08 12:06:26 +08:00
|
|
|
sprintf (buf, "SIG%-.20s", in_sig);
|
2002-03-12 02:33:02 +08:00
|
|
|
sig = buf;
|
|
|
|
}
|
2002-05-13 13:13:58 +08:00
|
|
|
intsig = strtosigno (sig) ?: atoi (in_sig);
|
|
|
|
char *p;
|
2016-11-24 23:00:28 +08:00
|
|
|
if (!intsig && (strcmp (sig, "SIG0") != 0 && (strtol (in_sig, &p, 10) != 0 || *p)))
|
2002-05-13 13:13:58 +08:00
|
|
|
intsig = -1;
|
|
|
|
return intsig;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_for_unknown_sig (int sig, const char *sigstr)
|
|
|
|
{
|
|
|
|
if (sig < 0 || sig > NSIG)
|
|
|
|
{
|
2011-10-10 22:57:48 +08:00
|
|
|
fprintf (stderr, "%1$s: unknown signal: %2$s\n"
|
|
|
|
"Try `%1$s --help' for more information.\n",
|
|
|
|
prog_name, sigstr);
|
2002-05-13 13:13:58 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
listsig (const char *in_sig)
|
|
|
|
{
|
|
|
|
int sig;
|
|
|
|
if (!in_sig)
|
2004-05-27 23:15:51 +08:00
|
|
|
for (sig = 1; sig < NSIG - 1; sig++)
|
2002-05-13 13:13:58 +08: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-27 05:30:43 +08:00
|
|
|
if (sig && atoi (in_sig) == sig)
|
2002-05-13 13:35:44 +08:00
|
|
|
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 (),
|
2011-12-18 07:39:47 +08:00
|
|
|
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tok))
|
2004-03-14 02:11:13 +08: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);
|
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
static void
|
2019-02-03 04:02:00 +08:00
|
|
|
forcekill (pid_t pid, DWORD winpid, int sig, int wait)
|
2002-03-12 02:33:02 +08:00
|
|
|
{
|
2019-02-03 04:02:00 +08:00
|
|
|
DWORD dwpid;
|
|
|
|
|
2019-02-02 22:00:39 +08:00
|
|
|
/* try to acquire SeDebugPrivilege */
|
2004-03-14 02:11:13 +08:00
|
|
|
get_debug_priv();
|
|
|
|
|
2019-02-03 04:02:00 +08:00
|
|
|
if (!winpid)
|
2019-02-02 22:00:39 +08:00
|
|
|
{
|
2019-02-03 04:02:00 +08:00
|
|
|
external_pinfo *p = (external_pinfo *)
|
|
|
|
cygwin_internal (CW_GETPINFO_FULL, pid);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: %d: No such process\n", prog_name, pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dwpid = p->dwProcessId;
|
2019-02-02 22:00:39 +08:00
|
|
|
}
|
2019-02-03 04:02:00 +08:00
|
|
|
else
|
|
|
|
/* pid is used for printing only after this point */
|
|
|
|
pid = dwpid = winpid;
|
|
|
|
|
2019-02-02 22:00:39 +08:00
|
|
|
HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, dwpid);
|
2002-03-12 02:33:02 +08:00
|
|
|
if (!h)
|
2003-09-01 09:57:44 +08:00
|
|
|
{
|
2011-12-18 07:39:47 +08:00
|
|
|
if (!wait || GetLastError () != ERROR_INVALID_PARAMETER)
|
2019-02-02 22:32:28 +08:00
|
|
|
fprintf (stderr, "%s: couldn't open pid %u\n",
|
|
|
|
prog_name, (unsigned int) dwpid);
|
2003-09-14 02:20:52 +08:00
|
|
|
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)
|
2004-11-14 00:30:19 +08:00
|
|
|
if (sig && !TerminateProcess (h, sig << 8)
|
2003-09-14 02:20:52 +08:00
|
|
|
&& WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "%s: couldn't kill pid %u, %u\n",
|
2019-02-02 22:32:28 +08:00
|
|
|
prog_name, (unsigned int) dwpid, (unsigned int) 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;
|
2019-02-03 04:02:00 +08:00
|
|
|
int winpids = 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
|
|
|
|
2011-10-10 22:57:48 +08:00
|
|
|
prog_name = program_invocation_short_name;
|
2002-05-14 07:42:32 +08:00
|
|
|
|
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;
|
|
|
|
|
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;
|
2019-02-03 04:02:00 +08:00
|
|
|
case 'W':
|
|
|
|
winpids = 1;
|
|
|
|
break;
|
2002-05-13 13:23:37 +08:00
|
|
|
case 'h':
|
|
|
|
usage (stdout);
|
|
|
|
break;
|
2011-10-10 22:57:48 +08:00
|
|
|
case 'V':
|
2002-05-14 07:42:32 +08:00
|
|
|
print_version ();
|
|
|
|
break;
|
2002-05-13 13:13:58 +08:00
|
|
|
case '?':
|
2019-02-02 22:00:39 +08:00
|
|
|
if (gotasig) /* this is a negative pid, go ahead */
|
2019-02-18 05:59:36 +08:00
|
|
|
{
|
|
|
|
--optind;
|
|
|
|
goto out;
|
|
|
|
}
|
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;
|
2019-02-02 22:00:39 +08:00
|
|
|
if (*argv == 0)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: not enough arguments\n", prog_name);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-02-03 04:02:00 +08:00
|
|
|
for (long long int pid = 0; *argv != NULL; argv++)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2019-02-03 04:02:00 +08:00
|
|
|
DWORD dwpid = 0;
|
|
|
|
|
|
|
|
pid = strtoll (*argv, &p, 10);
|
2019-02-02 22:00:39 +08:00
|
|
|
/* INT_MIN <= pid <= INT_MAX. -f only takes positive pids. */
|
|
|
|
if (*p != '\0' || pid < (force ? 1 : INT_MIN) || pid > INT_MAX)
|
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;
|
2019-02-03 04:02:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (winpids)
|
|
|
|
{
|
|
|
|
dwpid = pid;
|
|
|
|
pid = (pid_t) cygwin_internal (CW_WINPID_TO_CYGWIN_PID, dwpid);
|
2001-10-08 11:06:25 +08:00
|
|
|
}
|
2019-02-03 04:02:00 +08:00
|
|
|
if (kill ((pid_t) pid, sig) == 0)
|
2001-10-08 11:06:25 +08:00
|
|
|
{
|
|
|
|
if (force)
|
2019-02-03 04:02:00 +08:00
|
|
|
forcekill ((pid_t) pid, dwpid, sig, 1);
|
2001-10-08 11:06:25 +08:00
|
|
|
}
|
2004-11-14 00:30:19 +08:00
|
|
|
else if (force)
|
2019-02-03 04:02:00 +08:00
|
|
|
forcekill ((pid_t) pid, dwpid, sig, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
2001-10-08 11:06:25 +08:00
|
|
|
char buf[1000];
|
2019-02-03 04:02:00 +08:00
|
|
|
sprintf (buf, "%s: %lld", prog_name, dwpid ?: pid);
|
2001-10-08 11:06:25 +08:00
|
|
|
perror (buf);
|
|
|
|
ret = 1;
|
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
|
|
|
|