-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmap-3.c
142 lines (106 loc) · 2.9 KB
/
mmap-3.c
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
/*
* mmap( MAP_ANON | MAP_SHARED ) test program
*
* Copyright (C) 2014-2023 KO Myung-Hun <[email protected]>
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include "test.h"
#include "mmap.h"
#define P1 10
#define P2 20
#define EXIT(c) do { rc = (c); goto exit_munmap; } while( 0 )
int main( void )
{
int *p1;
int *p2;
int len = sizeof( *p1 );
int p2c[ 2 ];
int c2p[ 2 ];
int sig;
int pid;
const char *me;
int status;
int rc = 0;
printf("Testing mmap( MAP_ANON | MAP_SHARED )...\n");
p1 = mmap( NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1,
0 );
if( p1 == MAP_FAILED )
{
fprintf( stderr, "mmap() failed\n");
return 1;
}
p2 = mmap( NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1,
0 );
if( p2 == MAP_FAILED )
{
fprintf( stderr, "mmap() failed\n");
EXIT( 1 );
}
if( pipe( p2c ) == -1 )
{
fprintf( stderr, "pipe() failed\n");
EXIT( 1 );
}
if( pipe( c2p ) == -1 )
{
fprintf( stderr, "pipe() failed\n");
EXIT( 1 );
}
pid = fork();
switch( pid )
{
case -1 :
fprintf( stderr, "fork() failed\n");
EXIT( 1 );
break;
case 0 :
close( p2c[ 1 ]);
close( c2p[ 0 ]);
me = "CHILD ";
printf("%s: *p2 = %d\n", me, *p2 );
*p2 = P2;
printf("%s: Set *p2 to %d\n", me, *p2 );
read( p2c[ 0 ], &sig, sizeof( sig ));
write( c2p[ 1 ], &sig, sizeof( sig ));
close( p2c[ 0 ]);
close( c2p[ 1 ]);
break;
default :
close( p2c[ 0 ]);
close( c2p[ 1 ]);
me = "PARENT";
printf("%s: *p1 = %d\n", me, *p1 );
*p1 = P1;
printf("%s: Set *p1 to %d\n", me, *p1 );
write( p2c[ 1 ], &sig, sizeof( sig ));
read( c2p[ 0 ], &sig, sizeof( sig ));
close( p2c[ 1 ]);
close( c2p[ 0 ]);
break;
}
TEST_EQUAL_MSG( *p1, P1, me );
TEST_EQUAL_MSG( *p2, P2, me );
exit_munmap:
if( munmap( p1, len ))
{
fprintf( stderr, "%s: munmap() failed\n", me );
rc = 1;
}
if( munmap( p2, len ))
{
fprintf( stderr, "%s: munmap() failed\n", me);
rc = 1;
}
if( rc == 0 && pid != 0 && waitpid( pid, &status, 0 ) == pid
&& WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
printf("All tests PASSED\n");
return rc;
}