ab112c8e96148e75ddf54e9adff371ac3170bde0
[reactos.git] / reactos / apps / tests / map_dup_inherit / map_dup_inherit.c
1 #include <stdio.h>
2 #include <windows.h>
3
4 /* This tests the ability of the target win32 to create an anonymous file
5 * mapping, create a mapping view with MapViewOfFile, and then realize the
6 * pages with VirtualAlloc.
7 */
8
9 int main( int argc, char **argv ) {
10 HANDLE file_view;
11 void *file_map;
12 int *x;
13
14 fprintf( stderr, "%d: Starting\n", GetCurrentProcessId() );
15
16 if( argc == 2 ) {
17 file_map = atoi(argv[1]);
18 } else {
19 file_map = CreateFileMapping( INVALID_HANDLE_VALUE,
20 NULL,
21 PAGE_READWRITE | SEC_RESERVE,
22 0, 0x1000, NULL );
23 if( !SetHandleInformation( file_map,
24 HANDLE_FLAG_INHERIT,
25 HANDLE_FLAG_INHERIT ) ) {
26 fprintf( stderr, "%d: Could not make handle inheritable.\n",
27 GetCurrentProcessId() );
28 return 100;
29 }
30 }
31
32 if( !file_map ) {
33 fprintf( stderr, "%d: Could not create anonymous file map.\n",
34 GetCurrentProcessId() );
35 return 1;
36 }
37
38 file_view = MapViewOfFile( file_map,
39 FILE_MAP_WRITE,
40 0,
41 0,
42 0x1000 );
43
44 if( !file_view ) {
45 fprintf( stderr, "%d: Could not map view of file.\n",
46 GetCurrentProcessId() );
47 return 2;
48 }
49
50 if( !VirtualAlloc( file_view, 0x1000, MEM_COMMIT, PAGE_READWRITE ) ) {
51 fprintf( stderr, "%d: VirtualAlloc failed to realize the page.\n",
52 GetCurrentProcessId() );
53 return 3;
54 }
55
56 x = (int *)file_view;
57 x[0] = 0x12345678;
58
59 if( x[0] != 0x12345678 ) {
60 fprintf( stderr, "%d: Can't write to the memory (%08x != 0x12345678)\n",
61 GetCurrentProcessId() );
62 return 4;
63 }
64
65 if( argc == 1 ) {
66 STARTUPINFO si;
67 PROCESS_INFORMATION pi;
68 char cmdline[1000];
69
70 memset( &si, 0, sizeof( si ) );
71 memset( &pi, 0, sizeof( pi ) );
72
73 sprintf(cmdline,"%s %d", argv[0], (int)file_map);
74 if( !CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL,
75 &si, &pi ) ) {
76 fprintf( stderr, "%d: Could not create child process.\n",
77 GetCurrentProcessId() );
78 return 5;
79 }
80
81 if( WaitForSingleObject( pi.hThread, INFINITE ) != WAIT_OBJECT_0 ) {
82 fprintf( stderr, "%d: Failed to wait for child process to terminate.\n",
83 GetCurrentProcessId() );
84 return 6;
85 }
86 }
87
88 return 0;
89 }