ReactOS  0.4.15-dev-499-g1f31905
TerminateProcess.c File Reference
#include "precomp.h"
#include <ndk/obfuncs.h>
Include dependency graph for TerminateProcess.c:

Go to the source code of this file.

Macros

#define WaitExpectSuccess(h, ms)   WaitExpect_(h, ms, WAIT_OBJECT_0, __FILE__, __LINE__)
 
#define WaitExpectTimeout(h, ms)   WaitExpect_(h, ms, WAIT_TIMEOUT, __FILE__, __LINE__)
 
#define CloseProcessAndVerify(hp, pid, code)   CloseProcessAndVerify_(hp, pid, code, __FILE__, __LINE__)
 

Functions

static HANDLE StartChild (_In_ PCWSTR Argument, _In_ DWORD Flags, _Out_opt_ PDWORD ProcessId)
 
static VOID TraceHandleCount_ (_In_ HANDLE hObject, _In_ PCSTR File, _In_ INT Line)
 
static VOID WaitExpect_ (_In_ HANDLE hWait, _In_ DWORD Milliseconds, _In_ DWORD ExpectedError, _In_ PCSTR File, _In_ INT Line)
 
static VOID CloseProcessAndVerify_ (_In_ HANDLE hProcess, _In_ DWORD ProcessId, _In_ UINT ExpectedExitCode, _In_ PCSTR File, _In_ INT Line)
 
static VOID TestTerminateProcess (_In_ HANDLE hEvent)
 
 START_TEST (TerminateProcess)
 

Macro Definition Documentation

◆ CloseProcessAndVerify

#define CloseProcessAndVerify (   hp,
  pid,
  code 
)    CloseProcessAndVerify_(hp, pid, code, __FILE__, __LINE__)

Definition at line 105 of file TerminateProcess.c.

◆ WaitExpectSuccess

#define WaitExpectSuccess (   h,
  ms 
)    WaitExpect_(h, ms, WAIT_OBJECT_0, __FILE__, __LINE__)

Definition at line 88 of file TerminateProcess.c.

◆ WaitExpectTimeout

#define WaitExpectTimeout (   h,
  ms 
)    WaitExpect_(h, ms, WAIT_TIMEOUT, __FILE__, __LINE__)

Definition at line 89 of file TerminateProcess.c.

Function Documentation

◆ CloseProcessAndVerify_()

static VOID CloseProcessAndVerify_ ( _In_ HANDLE  hProcess,
_In_ DWORD  ProcessId,
_In_ UINT  ExpectedExitCode,
_In_ PCSTR  File,
_In_ INT  Line 
)
static

Definition at line 108 of file TerminateProcess.c.

114 {
115  int i = 0;
116  DWORD Error;
117  DWORD ExitCode;
118  BOOL Success;
119 
121  Success = GetExitCodeProcess(hProcess, &ExitCode);
122  ok_(File, Line)(Success, "GetExitCodeProcess failed with %lu\n", GetLastError());
125  {
126  if (++i >= 100)
127  {
130  break;
131  }
133  Sleep(100);
134  }
135  Error = GetLastError();
136  ok_(File, Line)(hProcess == NULL, "OpenProcess succeeded unexpectedly for pid 0x%lx\n", ProcessId);
137  ok_(File, Line)(Error == ERROR_INVALID_PARAMETER, "Error = %lu\n", Error);
138  ok_(File, Line)(ExitCode == ExpectedExitCode, "Exit code is %lu but expected %u\n", ExitCode, ExpectedExitCode);
139 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2706
#define CloseHandle
Definition: compat.h:407
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define PROCESS_QUERY_INFORMATION
Definition: pstypes.h:158
BOOL WINAPI GetExitCodeProcess(IN HANDLE hProcess, IN LPDWORD lpExitCode)
Definition: proc.c:1168
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
static VOID TraceHandleCount_(_In_ HANDLE hObject, _In_ PCSTR File, _In_ INT Line)
#define WAIT_OBJECT_0
Definition: winbase.h:387
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
Definition: ncftp.h:79
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1227
static VOID WaitExpect_(_In_ HANDLE hWait, _In_ DWORD Milliseconds, _In_ DWORD ExpectedError, _In_ PCSTR File, _In_ INT Line)
Definition: File.h:15
#define ok_(x1, x2)
Definition: atltest.h:61

◆ START_TEST()

START_TEST ( TerminateProcess  )

Definition at line 202 of file TerminateProcess.c.

203 {
204  HANDLE hEvent;
205  BOOL Success;
206  DWORD Error;
207  int argc;
208  char **argv;
209 
210  hEvent = CreateEventW(NULL, TRUE, FALSE, L"kernel32_apitest_TerminateProcess_event");
211  Error = GetLastError();
212  if (!hEvent)
213  {
214  skip("CreateEvent failed with error %lu\n", Error);
215  return;
216  }
218  if (argc >= 3)
219  {
220  ok(Error == ERROR_ALREADY_EXISTS, "Error = %lu\n", Error);
221  if (!strcmp(argv[2], "wait"))
222  {
223  WaitExpectSuccess(hEvent, 30000);
224  }
225  else
226  {
228  ok(Success, "SetEvent failed with return %d, error %lu\n", Success, GetLastError());
229  }
230  }
231  else
232  {
233  ok(Error == NO_ERROR, "Error = %lu\n", Error);
235  }
237  if (argc >= 5)
238  {
239  UINT ExitCode = strtol(argv[4], NULL, 10);
240 
241  fflush(stdout);
242  if (!strcmp(argv[3], "exit"))
243  ExitProcess(ExitCode);
244  else if (!strcmp(argv[3], "terminate"))
245  TerminateProcess(GetCurrentProcess(), ExitCode);
246  else if (!strcmp(argv[3], "terminate2"))
247  {
250  TerminateProcess(hProcess, ExitCode);
251  }
252  ok(0, "Should have terminated\n");
253  }
254 }
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:407
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define argv
Definition: mplay32.c:18
#define NO_ERROR
Definition: dderror.h:5
FILE * stdout
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:651
static HANDLE hEvent
Definition: comm.c:54
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
static VOID TestTerminateProcess(_In_ HANDLE hEvent)
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1138
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
int winetest_get_mainargs(char ***pargv)
static const WCHAR L[]
Definition: oid.c:1250
#define WaitExpectSuccess(h, ms)
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1227
#define PROCESS_TERMINATE
Definition: pstypes.h:149
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1532
#define ok(value,...)
Definition: atltest.h:57
unsigned int UINT
Definition: ndis.h:50
#define skip(...)
Definition: atltest.h:64
_Check_return_ long __cdecl strtol(_In_z_ const char *_Str, _Out_opt_ _Deref_post_z_ char **_EndPtr, _In_ int _Radix)
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1158

◆ StartChild()

static HANDLE StartChild ( _In_ PCWSTR  Argument,
_In_ DWORD  Flags,
_Out_opt_ PDWORD  ProcessId 
)
static

Definition at line 14 of file TerminateProcess.c.

18 {
19  BOOL Success;
21  WCHAR CommandLine[MAX_PATH];
22  STARTUPINFOW StartupInfo;
23  PROCESS_INFORMATION ProcessInfo;
24 
26  StringCbPrintfW(CommandLine,
27  sizeof(CommandLine),
28  L"\"%ls\" TerminateProcess %ls",
29  FileName,
30  Argument);
31 
32  RtlZeroMemory(&StartupInfo, sizeof(StartupInfo));
33  StartupInfo.cb = sizeof(StartupInfo);
34  /* HACK: running the test under rosautotest seems to keep another reference
35  * to the child process around until the test finishes (on both ROS and
36  * Windows)... I'm too lazy to investigate very much so let's just redirect
37  * the child std handles to nowhere. ok() is useless in half the child
38  * processes anyway.
39  */
40  StartupInfo.dwFlags = STARTF_USESTDHANDLES;
41 
43  CommandLine,
44  NULL,
45  NULL,
46  FALSE,
47  Flags,
48  NULL,
49  NULL,
50  &StartupInfo,
51  &ProcessInfo);
52  if (!Success)
53  {
54  skip("CreateProcess failed with %lu\n", GetLastError());
55  if (ProcessId)
56  *ProcessId = 0;
57  return NULL;
58  }
59  CloseHandle(ProcessInfo.hThread);
60  if (ProcessId)
61  *ProcessId = ProcessInfo.dwProcessId;
62  return ProcessInfo.hProcess;
63 }
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2706
#define CloseHandle
Definition: compat.h:407
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
STRSAFEAPI StringCbPrintfW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:557
BOOL WINAPI DECLSPEC_HOTPATCH CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
Definition: proc.c:4595
#define STARTF_USESTDHANDLES
Definition: winbase.h:480
DWORD cb
Definition: winbase.h:830
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define _countof(array)
Definition: sndvol32.h:68
#define MAX_PATH
Definition: compat.h:26
static const WCHAR L[]
Definition: oid.c:1250
#define skip(...)
Definition: atltest.h:64
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
DWORD dwFlags
Definition: winbase.h:841

Referenced by TestTerminateProcess().

◆ TestTerminateProcess()

static VOID TestTerminateProcess ( _In_ HANDLE  hEvent)
static

Definition at line 143 of file TerminateProcess.c.

145 {
148 
149  /* Regular child process that returns from the test function */
150  /* HACK: These two tests don't work if stdout is a pipe. See StartChild */
152  hProcess = StartChild(L"child", 0, &ProcessId);
153  WaitExpectSuccess(hEvent, 5000);
156 
158  hProcess = StartChild(L"child", 0, &ProcessId);
162 
163  /* Suspended process -- never gets a chance to initialize */
171 
172  /* Waiting process -- we have to terminate it */
174  hProcess = StartChild(L"wait", 0, &ProcessId);
179 
180  /* Process calls ExitProcess */
182  hProcess = StartChild(L"child exit 456", 0, &ProcessId);
183  WaitExpectSuccess(hEvent, 5000);
186 
187  /* Process calls TerminateProcess with GetCurrentProcess */
189  hProcess = StartChild(L"child terminate 456", 0, &ProcessId);
190  WaitExpectSuccess(hEvent, 5000);
193 
194  /* Process calls TerminateProcess with real handle to itself */
196  hProcess = StartChild(L"child terminate2 456", 0, &ProcessId);
197  WaitExpectSuccess(hEvent, 5000);
200 }
IN PLARGE_INTEGER IN PLARGE_INTEGER PEPROCESS ProcessId
Definition: fatprocs.h:2706
#define CloseProcessAndVerify(hp, pid, code)
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
static HANDLE StartChild(_In_ PCWSTR Argument, _In_ DWORD Flags, _Out_opt_ PDWORD ProcessId)
static HANDLE hEvent
Definition: comm.c:54
#define CREATE_SUSPENDED
Definition: winbase.h:178
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
#define WaitExpectSuccess(h, ms)
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1532
BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent(IN HANDLE hEvent)
Definition: synch.c:714
#define WaitExpectTimeout(h, ms)

Referenced by START_TEST().

◆ TraceHandleCount_()

static VOID TraceHandleCount_ ( _In_ HANDLE  hObject,
_In_ PCSTR  File,
_In_ INT  Line 
)
static

Definition at line 67 of file TerminateProcess.c.

71 {
73  OBJECT_BASIC_INFORMATION BasicInfo;
74 
75  Status = NtQueryObject(hObject,
77  &BasicInfo,
78  sizeof(BasicInfo),
79  NULL);
80  if (!NT_SUCCESS(Status))
81  {
82  ok_(File, Line)(0, "NtQueryObject failed with status 0x%lx\n", Status);
83  return;
84  }
85  ok_(File, Line)(0, "Handle %p still has %lu open handles, %lu references\n", hObject, BasicInfo.HandleCount, BasicInfo.PointerCount);
86 }
LONG NTSTATUS
Definition: precomp.h:26
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NtQueryObject(IN HANDLE Handle, IN OBJECT_INFO_CLASS ObjectInformationClass, OUT PVOID ObjectInformation, IN ULONG ObjectInformationLength, OUT PULONG ReturnLength)
Definition: ncftp.h:79
Status
Definition: gdiplustypes.h:24
Definition: File.h:15
#define ok_(x1, x2)
Definition: atltest.h:61

Referenced by CloseProcessAndVerify_().

◆ WaitExpect_()

static VOID WaitExpect_ ( _In_ HANDLE  hWait,
_In_ DWORD  Milliseconds,
_In_ DWORD  ExpectedError,
_In_ PCSTR  File,
_In_ INT  Line 
)
static

Definition at line 92 of file TerminateProcess.c.

98 {
99  DWORD Error;
100 
101  Error = WaitForSingleObject(hWait, Milliseconds);
102  ok_(File, Line)(Error == ExpectedError, "Wait for %p return %lu\n", hWait, Error);
103 }
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
Definition: ncftp.h:79
Definition: File.h:15
#define ok_(x1, x2)
Definition: atltest.h:61

Referenced by CloseProcessAndVerify_().