1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 01:06:01 +02:00

2004-0813 Alex Chapiro

Fix for PR PR 71601

	* os/win32/x86/spawner.dll
	* os/win32/x86/starter.exe
	* library/Win32ProcessEx.c
	* library/starter/killer.cpp
	* library/starter/killer.h
	* library/starter/starter.cpp
	* library/starter/starter.dsp
	* library/starter/starter.dsw
This commit is contained in:
Alain Magloire 2004-08-13 20:23:05 +00:00
parent c2f1517d54
commit 11cdde2cd3
9 changed files with 581 additions and 5 deletions

View file

@ -1,3 +1,16 @@
2004-0813 Alex Chapiro
Fix for PR PR 71601
* os/win32/x86/spawner.dll
* os/win32/x86/starter.exe
* library/Win32ProcessEx.c
* library/starter/killer.cpp
* library/starter/killer.h
* library/starter/starter.cpp
* library/starter/starter.dsp
* library/starter/starter.dsw
2004-07-20 Alex Chapiro
Fix for PR 70359

View file

@ -508,6 +508,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
flags = CREATE_NEW_CONSOLE;
flags |= CREATE_UNICODE_ENVIRONMENT;
ret = CreateProcessW(0, /* executable name */
szCmdLine, /* command line */

View file

@ -0,0 +1,269 @@
/**********************************************************************
* Copyright (c) 2002-2004 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
*
* starter.cpp
*
* This is a helper function for the process killing
* Implementation based on the article "Terminating Windows Processes"
* see http://www.alexfedotov.com/articles/killproc.asp
***********************************************************************/
#define STRICT
#include <Windows.h>
#include <Tlhelp32.h>
#include <process.h>
#include <tchar.h>
#include <stdio.h>
#include "killer.h"
#define SystemProcessesAndThreadsInformation 5
#define MAX_CMD_LINE_LENGTH 512
//#define DEBUG_MONITOR
void DisplayErrorMessage();
BOOL KillProcessEx(
IN DWORD dwProcessId // Handle of the process
)
{
OSVERSIONINFO osvi;
DWORD dwError;
#ifdef DEBUG_MONITOR
_TCHAR buffer[MAX_CMD_LINE_LENGTH];
#endif
// determine operating system version
osvi.dwOSVersionInfoSize = sizeof(osvi);
GetVersionEx(&osvi);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
HINSTANCE hNtDll;
NTSTATUS (WINAPI * pZwQuerySystemInformation)(UINT, PVOID,
ULONG, PULONG);
// get NTDLL.DLL handle
hNtDll = GetModuleHandleW(_T("ntdll.dll"));
if(hNtDll == NULL) {
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Failed to get ntdll.dll handle"));
OutputDebugStringW(buffer);
#endif
return FALSE;
}
// find address of ZwQuerySystemInformation
*(FARPROC *)&pZwQuerySystemInformation =
GetProcAddress(hNtDll, "ZwQuerySystemInformation");
if (pZwQuerySystemInformation == NULL)
return SetLastError(ERROR_PROC_NOT_FOUND), NULL;
// get default process heap handle
HANDLE hHeap = GetProcessHeap();
NTSTATUS Status;
ULONG cbBuffer = 0x8000;
PVOID pBuffer = NULL;
// it is difficult to predict what buffer size will be
// enough, so we start with 32K buffer and increase its
// size as needed
do
{
pBuffer = HeapAlloc(hHeap, 0, cbBuffer);
if (pBuffer == NULL)
return SetLastError(ERROR_NOT_ENOUGH_MEMORY), FALSE;
Status = pZwQuerySystemInformation(
SystemProcessesAndThreadsInformation,
pBuffer, cbBuffer, NULL);
if (Status == STATUS_INFO_LENGTH_MISMATCH)
{
HeapFree(hHeap, 0, pBuffer);
cbBuffer *= 2;
}
else if (!NT_SUCCESS(Status))
{
HeapFree(hHeap, 0, pBuffer);
return SetLastError(Status), NULL;
}
}
while (Status == STATUS_INFO_LENGTH_MISMATCH);
// call the helper
dwError = KillProcessTreeNtHelper(
(PSYSTEM_PROCESS_INFORMATION)pBuffer,
dwProcessId);
HeapFree(hHeap, 0, pBuffer);
}
else
{
// call the helper
dwError = KillProcessTreeWinHelper(dwProcessId);
}
SetLastError(dwError);
return dwError == ERROR_SUCCESS;
}
// Heloer function for process killing
static BOOL KillProcess(
IN DWORD dwProcessId
)
{
// get process handle
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
if (hProcess == NULL)
return FALSE;
DWORD dwError = ERROR_SUCCESS;
// try to terminate the process
if (!TerminateProcess(hProcess, (DWORD)-1))
dwError = GetLastError();
// close process handle
CloseHandle(hProcess);
SetLastError(dwError);
#ifdef DEBUG_MONITOR
if(dwError != ERROR_SUCCESS) {
_stprintf(buffer, _T("Process %i killed"), dwProcessId);
OutputDebugStringW(buffer);
} else {
_stprintf(buffer, _T("Failed to kill process %i"), dwProcessId);
OutputDebugStringW(buffer);
DisplayMessage();
}
#endif
return dwError == ERROR_SUCCESS;
}
// a helper function that walks a process tree recursively
// on Windows NT and terminates all processes in the tree
static BOOL KillProcessTreeNtHelper(
IN PSYSTEM_PROCESS_INFORMATION pInfo,
IN DWORD dwProcessId
)
{
#ifdef DEBUG_MONITOR
_TCHAR buffer[MAX_CMD_LINE_LENGTH];
#endif
if(pInfo == NULL) {
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("KillProcessTreeNtHelper: wrong parameter"));
OutputDebugStringW(buffer);
#endif
return FALSE;
}
// terminate all children first
for (;;)
{
if (pInfo->InheritedFromProcessId == dwProcessId)
KillProcessTreeNtHelper(pInfo, pInfo->ProcessId);
if (pInfo->NextEntryDelta == 0)
break;
// find address of the next structure
pInfo = (PSYSTEM_PROCESS_INFORMATION)(((PUCHAR)pInfo)
+ pInfo->NextEntryDelta);
}
// terminate the specified process
if (!KillProcess(dwProcessId))
return GetLastError();
return ERROR_SUCCESS;
}
// a helper function that walks a process tree recursively
// on Windows 9x and terminates all processes in the tree
static BOOL KillProcessTreeWinHelper(
IN DWORD dwProcessId
)
{
#ifdef DEBUG_MONITOR
_TCHAR buffer[MAX_CMD_LINE_LENGTH];
#endif
HINSTANCE hKernel;
HANDLE (WINAPI * pCreateToolhelp32Snapshot)(DWORD, DWORD);
BOOL (WINAPI * pProcess32First)(HANDLE, PROCESSENTRY32 *);
BOOL (WINAPI * pProcess32Next)(HANDLE, PROCESSENTRY32 *);
// get KERNEL32.DLL handle
hKernel = GetModuleHandleW(_T("kernel32.dll"));
if(hKernel == NULL) {
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("KillProcessTreeNtHelper: wrong parameter"));
OutputDebugStringW(buffer);
#endif
return FALSE;
}
// find necessary entrypoints in KERNEL32.DLL
*(FARPROC *)&pCreateToolhelp32Snapshot =
GetProcAddress(hKernel, "CreateToolhelp32Snapshot");
*(FARPROC *)&pProcess32First =
GetProcAddress(hKernel, "Process32First");
*(FARPROC *)&pProcess32Next =
GetProcAddress(hKernel, "Process32Next");
if (pCreateToolhelp32Snapshot == NULL ||
pProcess32First == NULL ||
pProcess32Next == NULL)
return ERROR_PROC_NOT_FOUND;
HANDLE hSnapshot;
PROCESSENTRY32 Entry;
// create a snapshot of all processes
hSnapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
return GetLastError();
Entry.dwSize = sizeof(Entry);
if (!pProcess32First(hSnapshot, &Entry))
{
DWORD dwError = GetLastError();
CloseHandle(hSnapshot);
return dwError;
}
// terminate children first
do
{
if (Entry.th32ParentProcessID == dwProcessId)
KillProcessTreeWinHelper(Entry.th32ProcessID);
Entry.dwSize = sizeof(Entry);
}
while (pProcess32Next(hSnapshot, &Entry));
CloseHandle(hSnapshot);
// terminate the specified process
if (!KillProcess(dwProcessId))
return GetLastError();
return ERROR_SUCCESS;
}

View file

@ -0,0 +1,122 @@
/**********************************************************************
* Copyright (c) 2002-2004 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
*
* StdAfx.h
*
* This is a header file for helper function for the process killing
* Implementation based on the article "Terminating Windows Processes"
* see http://www.alexfedotov.com/articles/killproc.asp and
* http://www.alexfedotov.com/samples/threads.asp
***********************************************************************/
#if !defined(_KILLER_H_INCLUDED_)
#define _KILLER_H_INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <Ntsecapi.h>
typedef LONG KPRIORITY; // From ntddk.h
//
// Process Virtual Memory Counters
// NtQueryInformationProcess using ProcessVmCounters
// From ntddk.h
typedef struct _VM_COUNTERS {
SIZE_T PeakVirtualSize;
SIZE_T VirtualSize;
ULONG PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
} VM_COUNTERS;
typedef VM_COUNTERS *PVM_COUNTERS;
//
// ClientId
//
typedef struct _CLIENT_ID {
HANDLE UniqueProcess;
HANDLE UniqueThread;
} CLIENT_ID;
typedef CLIENT_ID *PCLIENT_ID;
typedef struct _SYSTEM_THREAD_INFORMATION {
LARGE_INTEGER KernelTime; // time spent in kernel mode
LARGE_INTEGER UserTime; // time spent in user mode
LARGE_INTEGER CreateTime; // thread creation time
ULONG WaitTime; // wait time
PVOID StartAddress; // start address
CLIENT_ID ClientId; // thread and process IDs
KPRIORITY Priority; // dynamic priority
KPRIORITY BasePriority; // base priority
ULONG ContextSwitchCount; // number of context switches
LONG State; // current state
LONG WaitReason; // wait reason
} SYSTEM_THREAD_INFORMATION, * PSYSTEM_THREAD_INFORMATION;
typedef struct _SYSTEM_PROCESS_INFORMATION {
ULONG NextEntryDelta; // offset to the next entry
ULONG ThreadCount; // number of threads
ULONG Reserved1[6]; // reserved
LARGE_INTEGER CreateTime; // process creation time
LARGE_INTEGER UserTime; // time spent in user mode
LARGE_INTEGER KernelTime; // time spent in kernel mode
UNICODE_STRING ProcessName; // process name
KPRIORITY BasePriority; // base process priority
ULONG ProcessId; // process identifier
ULONG InheritedFromProcessId; // parent process identifier
ULONG HandleCount; // number of handles
ULONG Reserved2[2]; // reserved
VM_COUNTERS VmCounters; // virtual memory counters
#if _WIN32_WINNT >= 0x500
IO_COUNTERS IoCounters; // i/o counters
#endif
SYSTEM_THREAD_INFORMATION Threads[1]; // threads
} SYSTEM_PROCESS_INFORMATION, * PSYSTEM_PROCESS_INFORMATION;
static BOOL KillProcessTreeNtHelper(
IN PSYSTEM_PROCESS_INFORMATION pInfo,
IN DWORD dwProcessId);
static BOOL KillProcessTreeWinHelper(
IN DWORD dwProcessId);
// From ntstatus.h
// MessageId: STATUS_INFO_LENGTH_MISMATCH
//
// MessageText:
//
// The specified information record length does not match the length required for the specified information class.
//
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
// From ntstatus.h
// Generic test for success on any status value (non-negative numbers
// indicate success).
//
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
#endif // _KILLER_H_INCLUDED_

View file

@ -28,6 +28,8 @@
int copyTo(_TCHAR * target, const _TCHAR * source, int cpyLength, int availSpace);
void DisplayErrorMessage();
BOOL KillProcessEx(DWORD dwProcessId); // Handle of the process
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI HandlerRoutine( DWORD dwCtrlType) // control signal type
{
@ -176,7 +178,16 @@ extern "C" int _tmain(int argc, _TCHAR * argv[]) {
_stprintf(buffer, _T("Starting: %s\n"), szCmdLine);
OutputDebugStringW(buffer);
#endif
// Create job object if it is possible
HMODULE hKernel = GetModuleHandle("kernel32.dll");
HANDLE hJob = NULL;
HANDLE (WINAPI * pCreateJobObject)(LPSECURITY_ATTRIBUTES lpJobAttributes,
char * lpName);
*(FARPROC *)&pCreateJobObject =
GetProcAddress(hKernel, "CreateJobObjectA");
if(NULL != pCreateJobObject)
hJob = pCreateJobObject(NULL, NULL);
// Spawn the other processes as part of this Process Group
BOOL f = CreateProcessW(NULL, szCmdLine, NULL, NULL, TRUE,
0, NULL, NULL, &si, &pi);
@ -192,6 +203,21 @@ extern "C" int _tmain(int argc, _TCHAR * argv[]) {
SetEvent(waitEvent); // Means thar process has been spawned
CloseHandle(pi.hThread);
h[1] = pi.hProcess;
if(NULL != hJob) {
HANDLE (WINAPI * pAssignProcessToJobObject)(HANDLE job, HANDLE process);
*(FARPROC *)&pAssignProcessToJobObject =
GetProcAddress(hKernel, "AssignProcessToJobObjectA");
if(NULL != pAssignProcessToJobObject)
if(!pAssignProcessToJobObject(hJob, pi.hProcess)) {
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Cannot assign process %i to a job\n"), pi.dwProcessId);
OutputDebugStringW(buffer);
DisplayErrorMessage();
#endif
}
}
while(!exitProc)
{
// Wait for the spawned-process to die or for the event
@ -223,7 +249,25 @@ extern "C" int _tmain(int argc, _TCHAR * argv[]) {
OutputDebugStringW(buffer);
#endif
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
TerminateProcess(h[1], 0);
if(NULL != hJob) {
HANDLE (WINAPI * pTerminateJobObject)(HANDLE job, UINT uExitCode);
*(FARPROC *)&pTerminateJobObject =
GetProcAddress(hKernel, "TerminateJobObjectA");
if(NULL != pTerminateJobObject) {
if(!pTerminateJobObject(hJob, -1)) {
#ifdef DEBUG_MONITOR
OutputDebugStringW(_T("Cannot terminate job\n"));
DisplayErrorMessage();
#endif
}
}
} else
if(!KillProcessEx(pi.dwProcessId)) {
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Cannot kill process (PID %i) tree\n"), pi.dwProcessId);
OutputDebugStringW(buffer);
#endif
}
exitProc = TRUE;
break;
default:
@ -333,19 +377,19 @@ int copyTo(_TCHAR * target, const _TCHAR * source, int cpyLength, int availSpace
void DisplayErrorMessage() {
char * lpMsgBuf;
FormatMessage(
_TCHAR * lpMsgBuf;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(char *) &lpMsgBuf,
(_TCHAR *) &lpMsgBuf,
0,
NULL
);
OutputDebugString(lpMsgBuf);
OutputDebugStringW(lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
}

View file

@ -0,0 +1,98 @@
# Microsoft Developer Studio Project File - Name="starter" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=starter - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "starter.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "starter.mak" CFG="starter - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "starter - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "starter - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "starter - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MD /W3 /GX /O2 /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_UNICODE" /FR /YX /FD /c
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "starter - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MD /W3 /Gm /GX /ZI /Od /D "_DEBUG" /D "WIN32" /D "_CONSOLE" /D "_UNICODE" /YX /FD /c
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "starter - Win32 Release"
# Name "starter - Win32 Debug"
# Begin Source File
SOURCE=.\killer.cpp
# End Source File
# Begin Source File
SOURCE=.\killer.h
# End Source File
# Begin Source File
SOURCE=.\starter.cpp
# End Source File
# End Target
# End Project

View file

@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "starter"=.\starter.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################