1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 ** Description: Test anonymous file map
10 ** Synopsis: anonfm [options] [dirName]
13 ** -d enable debug mode
14 ** -h display a help message
15 ** -s <n> size of the anonymous memory map, in KBytes. default: 100KBytes.
16 ** -C 1 Operate this process as ClientOne()
17 ** -C 2 Operate this process as ClientTwo()
19 ** anonfn.c contains two tests, corresponding to the two protocols for
20 ** passing an anonymous file map to a child process.
22 ** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
23 ** PR_CreateProcess() [for portability of the test case] to create the
24 ** child process, but does not use the PRProcessAttr structure for
25 ** passing the file map data.
27 ** ServerTwo()/ClientTwo() tests the passing of the file map using the
28 ** PRProcessAttr structure.
33 #include <private/primpl.h>
39 ** Test harness infrastructure
42 PRLogModuleLevel msgLevel
= PR_LOG_NONE
;
43 PRUint32 failed_already
= 0;
46 PRIntn client
= 0; /* invoke client, style */
47 char dirName
[512] = "."; /* directory name to contain anon mapped file */
48 PRSize fmSize
= (100 * 1024 );
49 PRUint32 fmMode
= 0600;
50 PRFileMapProtect fmProt
= PR_PROT_READWRITE
;
51 const char *fmEnvName
= "nsprFileMapEnvVariable";
54 ** Emit help text for this test
56 static void Help( void )
58 printf("anonfm [options] [dirName]\n");
59 printf("-d -- enable debug mode\n");
60 printf("dirName is alternate directory name. Default: . (current directory)\n");
68 static void ClientOne( void )
76 ("ClientOne() starting"));
78 fmString
= PR_GetEnv( fmEnvName
);
79 if ( NULL
== fmString
) {
82 ("ClientOne(): PR_Getenv() failed"));
86 ("ClientOne(): PR_Getenv(): found: %s", fmString
));
88 fm
= PR_ImportFileMapFromString( fmString
);
92 ("ClientOne(): PR_ImportFileMapFromString() failed"));
96 ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm
));
98 addr
= PR_MemMap( fm
, LL_ZERO
, fmSize
);
102 ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
106 ("ClientOne(): PR_MemMap(): addr: %p", addr
));
108 /* write to memory map to release server */
111 rc
= PR_MemUnmap( addr
, fmSize
);
112 PR_ASSERT( rc
== PR_SUCCESS
);
114 ("ClientOne(): PR_MemUnap(): success" ));
116 rc
= PR_CloseFileMap( fm
);
117 if ( PR_FAILURE
== rc
) {
120 ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
124 ("ClientOne(): PR_CloseFileMap(): success" ));
127 } /* end ClientOne() */
132 static void ClientTwo( void )
135 } /* end ClientTwo() */
140 static void ServerOne( void )
153 ("ServerOne() starting"));
155 fm
= PR_OpenAnonFileMap( dirName
, fmSize
, fmProt
);
159 ("PR_OpenAnonFileMap() failed"));
163 ("ServerOne(): FileMap: %p", fm
));
165 rc
= PR_ExportFileMapAsString( fm
, sizeof(fmString
), fmString
);
166 if ( PR_FAILURE
== rc
) {
169 ("PR_ExportFileMap() failed"));
174 ** put the string into the environment
176 PR_snprintf( envBuf
, sizeof(envBuf
), "%s=%s", fmEnvName
, fmString
);
179 addr
= PR_MemMap( fm
, LL_ZERO
, fmSize
);
180 if ( NULL
== addr
) {
183 ("PR_MemMap() failed"));
187 /* set initial value for client */
188 for (i
= 0; i
< (PRIntn
)fmSize
; i
++ )
192 ("ServerOne(): PR_MemMap(): addr: %p", addr
));
195 ** set arguments for child process
197 child_argv
[0] = "anonfm";
198 child_argv
[1] = "-C";
200 child_argv
[3] = NULL
;
202 proc
= PR_CreateProcess(child_argv
[0], child_argv
, NULL
, NULL
);
205 ("ServerOne(): PR_CreateProcess(): proc: %x", proc
));
208 ** ClientOne() will set the memory to 1
211 ("ServerOne(): waiting on Client, *addr: %x", *addr
));
212 while( *addr
== 0x00 ) {
214 fprintf(stderr
, ".");
215 PR_Sleep(PR_MillisecondsToInterval(300));
218 fprintf(stderr
, "\n");
220 ("ServerOne(): Client responded" ));
222 rc
= PR_WaitProcess( proc
, &exit_status
);
223 PR_ASSERT( PR_FAILURE
!= rc
);
225 rc
= PR_MemUnmap( addr
, fmSize
);
226 if ( PR_FAILURE
== rc
) {
229 ("PR_MemUnmap() failed"));
233 ("ServerOne(): PR_MemUnmap(): success" ));
235 rc
= PR_CloseFileMap(fm
);
236 if ( PR_FAILURE
== rc
) {
239 ("PR_CloseFileMap() failed"));
243 ("ServerOne(): PR_CloseFileMap() success" ));
246 } /* end ServerOne() */
251 static void ServerTwo( void )
254 ("ServerTwo(): Not implemented yet" ));
255 } /* end ServerTwo() */
258 int main(int argc
, char **argv
)
262 ** Get command line options
265 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "hdC:");
267 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
269 if (PL_OPT_BAD
== os
) continue;
272 case 'C': /* Client style */
273 client
= atol(opt
->value
);
275 case 's': /* file size */
276 fmSize
= atol( opt
->value
) * 1024;
278 case 'd': /* debug */
280 msgLevel
= PR_LOG_DEBUG
;
282 case 'h': /* help message */
286 strcpy(dirName
, opt
->value
);
290 PL_DestroyOptState(opt
);
293 lm
= PR_NewLogModule("Test"); /* Initialize logging */
297 } else if ( client
== 2 ) {
301 if ( failed_already
) goto Finished
;
307 printf("%s\n", (failed_already
)? "FAIL" : "PASS");
308 return( (failed_already
== PR_TRUE
)? 1 : 0 );