1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*============================================================= |
6 | ** |
7 | ** Source: helper.c |
8 | ** |
9 | ** Purpose: This helper process sets up signals to communicate |
10 | ** with the test thread in the parent process, and let the test |
11 | ** thread signal this process when to exit. |
12 | ** |
13 | ** |
14 | **============================================================*/ |
15 | |
16 | #include "commonconsts.h" |
17 | |
18 | #include <palsuite.h> |
19 | |
20 | HANDLE hProcessStartEvent; |
21 | HANDLE hProcessReadyEvent; |
22 | HANDLE hProcessFinishEvent; |
23 | HANDLE hProcessCleanupEvent; |
24 | |
25 | |
26 | int __cdecl main(int argc, char *argv[]) |
27 | { |
28 | |
29 | BOOL success = TRUE; /* assume success */ |
30 | DWORD dwRet; |
31 | DWORD dwProcessId; |
32 | char szEventName[MAX_LONGPATH]; |
33 | PWCHAR uniString; |
34 | |
35 | if(0 != (PAL_Initialize(argc, argv))) |
36 | { |
37 | return FAIL; |
38 | } |
39 | |
40 | /* Open the event to let test thread tell us to get started. */ |
41 | uniString = convert(szcHelperProcessStartEvName); |
42 | hProcessStartEvent = OpenEventW(EVENT_ALL_ACCESS, 0, uniString); |
43 | free(uniString); |
44 | if (!hProcessStartEvent) |
45 | { |
46 | Fail("helper.main: OpenEvent of '%S' failed (%u). " |
47 | "(the event should already exist!)\n" , |
48 | szcHelperProcessStartEvName, GetLastError()); |
49 | } |
50 | |
51 | /* Wait for signal from test thread. */ |
52 | dwRet = WaitForSingleObject(hProcessStartEvent, TIMEOUT); |
53 | if (dwRet != WAIT_OBJECT_0) |
54 | { |
55 | Fail("helper.main: WaitForSingleObject '%s' failed\n" |
56 | "LastError:(%u)\n" , szcHelperProcessStartEvName, GetLastError()); |
57 | } |
58 | |
59 | dwProcessId = GetCurrentProcessId(); |
60 | |
61 | if ( 0 >= dwProcessId ) |
62 | { |
63 | Fail ("helper.main: %s has invalid pid %d\n" , argv[0], dwProcessId ); |
64 | } |
65 | |
66 | /* Open the event to tell test thread we are ready. */ |
67 | if (sprintf_s(szEventName, MAX_LONGPATH-1, "%s%d" , szcHelperProcessReadyEvName, dwProcessId) < 0) |
68 | { |
69 | Fail ("helper.main: Insufficient event name string length for pid=%d\n" , dwProcessId); |
70 | } |
71 | |
72 | uniString = convert(szEventName); |
73 | |
74 | hProcessReadyEvent = OpenEventW(EVENT_ALL_ACCESS, 0, uniString); |
75 | free(uniString); |
76 | if (!hProcessReadyEvent) |
77 | { |
78 | Fail("helper.main: OpenEvent of '%s' failed (%u). " |
79 | "(the event should already exist!)\n" , |
80 | szEventName, GetLastError()); |
81 | } |
82 | |
83 | /* Open the event to let test thread tell us to exit. */ |
84 | if (sprintf_s(szEventName, MAX_LONGPATH-1, "%s%d" , szcHelperProcessFinishEvName, dwProcessId) < 0) |
85 | { |
86 | Fail ("helper.main: Insufficient event name string length for pid=%d\n" , dwProcessId); |
87 | } |
88 | |
89 | uniString = convert(szEventName); |
90 | |
91 | hProcessFinishEvent = OpenEventW(EVENT_ALL_ACCESS, 0, uniString); |
92 | free(uniString); |
93 | if (!hProcessFinishEvent) |
94 | { |
95 | Fail("helper.main: OpenEvent of '%s' failed LastError:(%u).\n" , |
96 | szEventName, GetLastError()); |
97 | } |
98 | |
99 | /* Tell the test thread we are ready. */ |
100 | if (!SetEvent(hProcessReadyEvent)) |
101 | { |
102 | Fail("helper.main: SetEvent '%s' failed LastError:(%u)\n" , |
103 | hProcessReadyEvent, GetLastError()); |
104 | } |
105 | |
106 | /* Wait for signal from test thread before exit. */ |
107 | dwRet = WaitForSingleObject(hProcessFinishEvent, TIMEOUT); |
108 | if (WAIT_OBJECT_0 != dwRet) |
109 | { |
110 | Fail("helper.main: WaitForSingleObject '%s' failed pid=%d\n" |
111 | "LastError:(%u)\n" , |
112 | szcHelperProcessFinishEvName, dwProcessId, GetLastError()); |
113 | } |
114 | |
115 | PEDANTIC(CloseHandle, (hProcessStartEvent)); |
116 | PEDANTIC(CloseHandle, (hProcessReadyEvent)); |
117 | PEDANTIC(CloseHandle, (hProcessFinishEvent)); |
118 | |
119 | PAL_Terminate(); |
120 | |
121 | return success ? PASS : FAIL; |
122 | } |
123 | |