2000-02-18 03:38:33 +08:00
|
|
|
/* kill.cc
|
|
|
|
|
2000-07-30 00:26:37 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000 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>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
static void usage (void);
|
2000-07-30 00:26:37 +08:00
|
|
|
static int __stdcall getsig (char *);
|
2000-09-13 10:48:39 +08:00
|
|
|
static void __stdcall forcekill (int, int, int);
|
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;
|
|
|
|
int gotsig = 0;
|
2000-11-02 09:03:00 +08:00
|
|
|
int ret = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:26:37 +08:00
|
|
|
if (argc == 1)
|
2000-02-18 03:38:33 +08:00
|
|
|
usage ();
|
|
|
|
|
2000-08-25 03:03:12 +08:00
|
|
|
while (*++argv && **argv == '-')
|
2000-07-30 00:26:37 +08:00
|
|
|
if (strcmp (*argv + 1, "f") == 0)
|
|
|
|
force = 1;
|
|
|
|
else if (gotsig)
|
|
|
|
break;
|
|
|
|
else if (strcmp(*argv + 1, "0") != 0)
|
|
|
|
{
|
2000-08-29 09:58:00 +08:00
|
|
|
sig = getsig (*argv + 1);
|
2000-07-30 00:26:37 +08:00
|
|
|
gotsig = 1;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
2000-07-30 00:26:37 +08:00
|
|
|
argv++;
|
2000-02-18 03:38:33 +08:00
|
|
|
sig = 0;
|
|
|
|
goto sig0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig <= 0 || sig > NSIG)
|
|
|
|
{
|
2000-07-30 00:26:37 +08:00
|
|
|
fprintf (stderr, "kill: unknown signal: %s\n", argv[-1]);
|
2000-02-18 03:38:33 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sig0:
|
2000-07-30 00:26:37 +08:00
|
|
|
while (*argv != NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char *p;
|
2000-07-30 00:26:37 +08:00
|
|
|
int pid = strtol (*argv, &p, 10);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (*p != '\0')
|
2000-07-30 00:26:37 +08:00
|
|
|
fprintf (stderr, "kill: illegal pid: %s\n", *argv);
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
2000-07-30 00:26:37 +08:00
|
|
|
#if 0
|
2000-02-18 03:38:33 +08:00
|
|
|
printf ("Sending %s(%d) signal to pid %d\n",
|
|
|
|
strsignal (sig), sig, pid);
|
2000-07-30 00:26:37 +08:00
|
|
|
#endif
|
2000-09-13 10:48:39 +08:00
|
|
|
if (kill (pid, sig) == 0)
|
|
|
|
{
|
|
|
|
if (force)
|
|
|
|
forcekill (pid, sig, 1);
|
|
|
|
}
|
|
|
|
else
|
2000-07-30 00:26:37 +08:00
|
|
|
{
|
2000-10-09 11:31:10 +08:00
|
|
|
if (force && sig != 0)
|
2000-09-13 10:48:39 +08:00
|
|
|
forcekill (pid, sig, 0);
|
2000-07-30 00:26:37 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[1000];
|
|
|
|
sprintf (buf, "kill %d", pid);
|
|
|
|
perror (buf);
|
2000-11-02 09:03:00 +08:00
|
|
|
ret = 1;
|
2000-07-30 00:26:37 +08:00
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-07-30 00:26:37 +08:00
|
|
|
argv++;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage (void)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Usage: kill [-sigN] pid1 [pid2 ...]\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getsig (char *in_sig)
|
|
|
|
{
|
|
|
|
char *sig;
|
|
|
|
char buf[80];
|
|
|
|
|
|
|
|
if (strncmp (in_sig, "SIG", 3) == 0)
|
|
|
|
sig = in_sig;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf (buf, "SIG%s", in_sig);
|
|
|
|
sig = buf;
|
|
|
|
}
|
|
|
|
return (strtosigno (sig) ?: atoi (in_sig));
|
|
|
|
}
|
2000-07-30 00:26:37 +08:00
|
|
|
|
|
|
|
static void __stdcall
|
2000-09-13 10:48:39 +08:00
|
|
|
forcekill (int pid, int sig, int wait)
|
2000-07-30 00:26:37 +08:00
|
|
|
{
|
2000-09-13 10:48:39 +08:00
|
|
|
external_pinfo *p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) p->pid);
|
2000-07-30 00:26:37 +08:00
|
|
|
if (!h)
|
|
|
|
return;
|
2000-09-13 10:48:39 +08:00
|
|
|
if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
|
|
|
|
TerminateProcess (h, sig << 8);
|
2000-07-30 00:26:37 +08:00
|
|
|
CloseHandle (h);
|
|
|
|
}
|