1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 ** Description: Test anonymous file map
42 ** Synopsis: anonfm [options] [dirName]
45 ** -d enable debug mode
46 ** -h display a help message
47 ** -s <n> size of the anonymous memory map, in KBytes. default: 100KBytes.
48 ** -C 1 Operate this process as ClientOne()
49 ** -C 2 Operate this process as ClientTwo()
51 ** anonfn.c contains two tests, corresponding to the two protocols for
52 ** passing an anonymous file map to a child process.
54 ** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
55 ** PR_CreateProcess() [for portability of the test case] to create the
56 ** child process, but does not use the PRProcessAttr structure for
57 ** passing the file map data.
59 ** ServerTwo()/ClientTwo() tests the passing of the file map using the
60 ** PRProcessAttr structure.
65 #include <private/primpl.h>
71 ** Test harness infrastructure
74 PRLogModuleLevel msgLevel = PR_LOG_NONE;
75 PRUint32 failed_already = 0;
78 PRIntn client = 0; /* invoke client, style */
79 char dirName[512] = "."; /* directory name to contain anon mapped file */
80 PRSize fmSize = (100 * 1024 );
81 PRUint32 fmMode = 0600;
82 PRFileMapProtect fmProt = PR_PROT_READWRITE;
83 const char *fmEnvName = "nsprFileMapEnvVariable";
86 ** Emit help text for this test
88 static void Help( void )
90 printf("anonfm [options] [dirName]\n");
91 printf("-d -- enable debug mode\n");
92 printf("dirName is alternate directory name. Default: . (current directory)\n");
100 static void ClientOne( void )
108 ("ClientOne() starting"));
110 fmString = PR_GetEnv( fmEnvName );
111 if ( NULL == fmString ) {
114 ("ClientOne(): PR_Getenv() failed"));
118 ("ClientOne(): PR_Getenv(): found: %s", fmString));
120 fm = PR_ImportFileMapFromString( fmString );
124 ("ClientOne(): PR_ImportFileMapFromString() failed"));
128 ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
130 addr = PR_MemMap( fm, LL_ZERO, fmSize );
131 if ( NULL == addr ) {
134 ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
138 ("ClientOne(): PR_MemMap(): addr: %p", addr ));
140 /* write to memory map to release server */
143 rc = PR_MemUnmap( addr, fmSize );
144 PR_ASSERT( rc == PR_SUCCESS );
146 ("ClientOne(): PR_MemUnap(): success" ));
148 rc = PR_CloseFileMap( fm );
149 if ( PR_FAILURE == rc ) {
152 ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
156 ("ClientOne(): PR_CloseFileMap(): success" ));
159 } /* end ClientOne() */
164 static void ClientTwo( void )
167 } /* end ClientTwo() */
172 static void ServerOne( void )
185 ("ServerOne() starting"));
187 fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
191 ("PR_OpenAnonFileMap() failed"));
195 ("ServerOne(): FileMap: %p", fm ));
197 rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
198 if ( PR_FAILURE == rc ) {
201 ("PR_ExportFileMap() failed"));
206 ** put the string into the environment
208 PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
211 addr = PR_MemMap( fm, LL_ZERO, fmSize );
212 if ( NULL == addr ) {
215 ("PR_MemMap() failed"));
219 /* set initial value for client */
220 for (i = 0; i < (PRIntn)fmSize ; i++ )
224 ("ServerOne(): PR_MemMap(): addr: %p", addr ));
227 ** set arguments for child process
229 child_argv[0] = "anonfm";
230 child_argv[1] = "-C";
232 child_argv[3] = NULL;
234 proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
237 ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
240 ** ClientOne() will set the memory to 1
243 ("ServerOne(): waiting on Client, *addr: %x", *addr ));
244 while( *addr == 0x00 ) {
246 fprintf(stderr, ".");
247 PR_Sleep(PR_MillisecondsToInterval(300));
250 fprintf(stderr, "\n");
252 ("ServerOne(): Client responded" ));
254 rc = PR_WaitProcess( proc, &exit_status );
255 PR_ASSERT( PR_FAILURE != rc );
257 rc = PR_MemUnmap( addr, fmSize);
258 if ( PR_FAILURE == rc ) {
261 ("PR_MemUnmap() failed"));
265 ("ServerOne(): PR_MemUnmap(): success" ));
267 rc = PR_CloseFileMap(fm);
268 if ( PR_FAILURE == rc ) {
271 ("PR_CloseFileMap() failed"));
275 ("ServerOne(): PR_CloseFileMap() success" ));
278 } /* end ServerOne() */
283 static void ServerTwo( void )
286 ("ServerTwo(): Not implemented yet" ));
287 } /* end ServerTwo() */
290 int main(int argc, char **argv)
294 ** Get command line options
297 PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
299 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
301 if (PL_OPT_BAD == os) continue;
304 case 'C': /* Client style */
305 client = atol(opt->value);
307 case 's': /* file size */
308 fmSize = atol( opt->value ) * 1024;
310 case 'd': /* debug */
312 msgLevel = PR_LOG_DEBUG;
314 case 'h': /* help message */
318 strcpy(dirName, opt->value);
322 PL_DestroyOptState(opt);
325 lm = PR_NewLogModule("Test"); /* Initialize logging */
329 } else if ( client == 2 ) {
333 if ( failed_already ) goto Finished;
339 printf("%s\n", (failed_already)? "FAIL" : "PASS");
340 return( (failed_already == PR_TRUE )? 1 : 0 );