1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/* kill.cc
Copyright 1996, 1997, 1998, 1999, 2000 Red Hat, Inc.
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>
#include <errno.h>
#include <windows.h>
#include <sys/cygwin.h>
static void usage (void);
static int __stdcall getsig (char *);
static void __stdcall forcekill (int, int, int);
int
main (int argc, char **argv)
{
int sig = SIGTERM;
int force = 0;
int gotsig = 0;
if (argc == 1)
usage ();
while (*++argv && **argv == '-')
if (strcmp (*argv + 1, "f") == 0)
force = 1;
else if (gotsig)
break;
else if (strcmp(*argv + 1, "0") != 0)
{
sig = getsig (*argv + 1);
gotsig = 1;
}
else
{
argv++;
sig = 0;
goto sig0;
}
if (sig <= 0 || sig > NSIG)
{
fprintf (stderr, "kill: unknown signal: %s\n", argv[-1]);
exit (1);
}
sig0:
while (*argv != NULL)
{
char *p;
int pid = strtol (*argv, &p, 10);
if (*p != '\0')
fprintf (stderr, "kill: illegal pid: %s\n", *argv);
else
{
#if 0
printf ("Sending %s(%d) signal to pid %d\n",
strsignal (sig), sig, pid);
#endif
if (kill (pid, sig) == 0)
{
if (force)
forcekill (pid, sig, 1);
}
else
{
if (errno == ESRCH && force && sig != 0)
forcekill (pid, sig, 0);
else
{
char buf[1000];
sprintf (buf, "kill %d", pid);
perror (buf);
}
}
}
argv++;
}
return 0;
}
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));
}
static void __stdcall
forcekill (int pid, int sig, int wait)
{
external_pinfo *p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
if (!p)
return;
HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) p->pid);
if (!h)
return;
if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
TerminateProcess (h, sig << 8);
CloseHandle (h);
}
|