summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/OpenEventW/test3/test3.c
blob: 10a678a107935bb762620cabea98dd8411e877fb (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=============================================================================
**
** Source: test3.c
**
** Purpose: Test to ensure that OpenEventW() works when
** opening an event created by another process. This test
** program launches a child process which creates a
** named, initially-unset event. The child waits up to
** 10 seconds for the parent process to open that event
** and set it, and returns PASS if the event was set or FAIL
** otherwise. The parent process checks the return value
** from the child to verify that the opened event was
** properly used across processes.
**
** Dependencies: PAL_Initialize
**               PAL_Terminate
**               Fail
**               ZeroMemory
**               GetCurrentDirectoryW
**               CreateProcessW
**               WaitForSingleObject
**               GetExitCodeProcess
**               GetLastError
**               strlen
**               strncpy
**
**
**===========================================================================*/
#include <palsuite.h>

#define TIMEOUT 60000

int __cdecl main( int argc, char **argv )
{
    BOOL ret = FAIL;
    LPSECURITY_ATTRIBUTES   lpEventAttributes = NULL;

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    DWORD dwExitCode;

    DWORD  dwRet = 0;
    HANDLE hEvent = NULL;
    WCHAR  wcName[] = {'P','A','L','R','o','c','k','s','\0'};
    LPWSTR lpName = wcName;
    char lpCommandLine[MAX_PATH] = "";

    /* initialize the PAL */
    if( PAL_Initialize(argc, argv) != 0 )
    {
	    return( FAIL );
    }

    /* zero our process and startup info structures */
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof( si );
    ZeroMemory( &pi, sizeof(pi) );

    /* create an event which we can use with SetEvent */
    hEvent = CreateEventW(  lpEventAttributes,
                            TRUE,               /* manual reset */
                            FALSE,              /* unsignalled  */
                            lpName );

    if( hEvent == NULL )
    {
        /* ERROR */
        Fail(   "ERROR:%lu:CreateEventW() call failed in child\n",
                GetLastError());
    }

    ZeroMemory( lpCommandLine, MAX_PATH );
    if ( _snprintf( lpCommandLine, MAX_PATH-1, "childprocess ") < 0 )
    {
        Fail ("Error: Insufficient lpCommandline for\n");
    }

    /* launch the child process */
    if( !CreateProcess(     NULL,               /* module name to execute */
                            lpCommandLine,    /* command line */
                            NULL,               /* process handle not */
                                                /* inheritable */
                            NULL,               /* thread handle not */
                                                /* inheritable */
                            FALSE,              /* handle inheritance */
                            CREATE_NEW_CONSOLE, /* dwCreationFlags */
                            NULL,               /* use parent's environment */
                            NULL,               /* use parent's starting */
                                                /* directory */
                            &si,                /* startup info struct */
                            &pi )               /* process info struct */
        )
    {
        Fail( "ERROR:%lu:CreateProcess call failed\n",
              GetLastError() );
    }

    /* verify that the event is signalled by the child process */
    dwRet = WaitForSingleObject( hEvent, TIMEOUT );
    if( dwRet != WAIT_OBJECT_0 )
    {
        ret = FAIL;
        /* ERROR */
        Trace( "ERROR:WaitForSingleObject() call returned %lu, "
                "expected WAIT_TIMEOUT\n",
                "expected WAIT_OBJECT_0\n",
                dwRet );

        goto cleanup;

        if( !CloseHandle( hEvent ) )
        {
            Trace(   "ERROR:%lu:CloseHandle() call failed in child\n",
                    GetLastError());
        }
        goto cleanup;
    }

    /* wait for the child process to complete */
    dwRet = WaitForSingleObject ( pi.hProcess, TIMEOUT );
    if( dwRet != WAIT_OBJECT_0 )
    {
        ret = FAIL;
        Trace( "ERROR:WaitForSingleObject() returned %lu, "
                "expected %lu\n",
                dwRet,
                WAIT_OBJECT_0 );
        goto cleanup;
    }

    /* check the exit code from the process */
    if( ! GetExitCodeProcess( pi.hProcess, &dwExitCode ) )
    {
        ret = FAIL;
        Trace( "ERROR:%lu:GetExitCodeProcess call failed\n",
              GetLastError() );
        goto cleanup;
    }

    /* check for success */
    ret = (dwExitCode == PASS) ? PASS : FAIL;

cleanup:
    if( hEvent != NULL )
    {
        if( ! CloseHandle ( hEvent ) )
        {
            Trace( "ERROR:%lu:CloseHandle call failed on event handle\n",
                  GetLastError() );
            ret = FAIL;
        }
    }


    /* close process and thread handle */
    if( ! CloseHandle ( pi.hProcess ) )
    {
        Trace( "ERROR:%lu:CloseHandle call failed on process handle\n",
              GetLastError() );
        ret = FAIL;
    }

    if( ! CloseHandle ( pi.hThread ) )
    {
        Trace( "ERROR:%lu:CloseHandle call failed on thread handle\n",
              GetLastError() );
        ret = FAIL;
    }

    /* output a convenient error message and exit if we failed */
    if( ret == FAIL )
    {
        Fail( "test failed\n" );
    }


    /* terminate the PAL */
    PAL_Terminate();

    /* return success */
    return ret;
}