2004-11-13 Pierre Humblet <pierre.humblet@ieee.org>

* kill.cc (forcekill): Do not pass negative pids to
	cygwin_internal. Check if sig == 0. Improve error messages.
	(main): Make pid a long long and distinguish between pids, gpids
	(i.e. negative pids) and Win9x pids.
This commit is contained in:
Pierre Humblet 2004-11-13 16:30:19 +00:00
parent 95e3512d53
commit 87b838398a
2 changed files with 29 additions and 14 deletions

View File

@ -1,3 +1,10 @@
2004-11-13 Pierre Humblet <pierre.humblet@ieee.org>
* kill.cc (forcekill): Do not pass negative pids to
cygwin_internal. Check if sig == 0. Improve error messages.
(main): Make pid a long long and distinguish between pids, gpids
(i.e. negative pids) and Win9x pids.
2004-11-11 Bas van Gompel <cygwin-patch.buzz@bavag.tmfweb.nl> 2004-11-11 Bas van Gompel <cygwin-patch.buzz@bavag.tmfweb.nl>
* cygcheck.cc: Change "keyeprint" to "display_error" throughout. * cygcheck.cc: Change "keyeprint" to "display_error" throughout.

View File

@ -17,6 +17,7 @@ details. */
#include <windows.h> #include <windows.h>
#include <sys/cygwin.h> #include <sys/cygwin.h>
#include <getopt.h> #include <getopt.h>
#include <limits.h>
static const char version[] = "$Revision$"; static const char version[] = "$Revision$";
static char *prog_name; static char *prog_name;
@ -157,19 +158,24 @@ forcekill (int pid, int sig, int wait)
// try to acquire SeDebugPrivilege // try to acquire SeDebugPrivilege
get_debug_priv(); get_debug_priv();
external_pinfo *p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid); external_pinfo *p = NULL;
/* cygwin_internal misinterprets negative pids (Win9x pids) */
if (pid > 0)
p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
DWORD dwpid = p ? p->dwProcessId : (DWORD) pid; DWORD dwpid = p ? p->dwProcessId : (DWORD) pid;
HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) dwpid); HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) dwpid);
if (!h) if (!h)
{ {
fprintf (stderr, "couldn't open pid %u\n", (unsigned) dwpid); if (!wait || GetLastError () != ERROR_INVALID_PARAMETER)
fprintf (stderr, "%s: couldn't open pid %u\n",
prog_name, (unsigned) dwpid);
return; return;
} }
if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0) if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
if (!TerminateProcess (h, sig << 8) if (sig && !TerminateProcess (h, sig << 8)
&& WaitForSingleObject (h, 200) != WAIT_OBJECT_0) && WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
fprintf (stderr, "couldn't kill pid %u, %u\n", (unsigned) dwpid, fprintf (stderr, "%s: couldn't kill pid %u, %lu\n",
(unsigned) GetLastError ()); prog_name, (unsigned) dwpid, GetLastError ());
CloseHandle (h); CloseHandle (h);
} }
@ -195,7 +201,7 @@ main (int argc, char **argv)
opterr = 0; opterr = 0;
char *p; char *p;
int pid = 0; long long int pid = 0;
for (;;) for (;;)
{ {
@ -235,7 +241,7 @@ main (int argc, char **argv)
case '?': case '?':
if (gotasig) if (gotasig)
{ {
pid = strtol (argv[optind], &p, 10); pid = strtoll (argv[optind], &p, 10);
if (pid < 0) if (pid < 0)
goto out; goto out;
usage (); usage ();
@ -258,23 +264,25 @@ out:
while (*argv != NULL) while (*argv != NULL)
{ {
if (!pid) if (!pid)
pid = strtol (*argv, &p, 10); pid = strtoll (*argv, &p, 10);
if (*p != '\0') if (*p != '\0'
|| (!force && (pid < LONG_MIN || pid > LONG_MAX))
|| (force && (pid <= 0 || pid > ULONG_MAX)))
{ {
fprintf (stderr, "%s: illegal pid: %s\n", prog_name, *argv); fprintf (stderr, "%s: illegal pid: %s\n", prog_name, *argv);
ret = 1; ret = 1;
} }
else if (kill (pid, sig) == 0) else if (pid <= LONG_MAX && kill ((pid_t) pid, sig) == 0)
{ {
if (force) if (force)
forcekill (pid, sig, 1); forcekill ((pid_t) pid, sig, 1);
} }
else if (force && sig != 0) else if (force)
forcekill (pid, sig, 0); forcekill ((pid_t) pid, sig, 0);
else else
{ {
char buf[1000]; char buf[1000];
sprintf (buf, "%s %d", prog_name, pid); sprintf (buf, "%s: %lld", prog_name, pid);
perror (buf); perror (buf);
ret = 1; ret = 1;
} }