1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
37
** Description: Test anonymous file map
39
** Synopsis: anonfm [options] [dirName]
42
** -d enable debug mode
43
** -h display a help message
44
** -s <n> size of the anonymous memory map, in KBytes. default: 100KBytes.
45
** -C 1 Operate this process as ClientOne()
46
** -C 2 Operate this process as ClientTwo()
48
** anonfn.c contains two tests, corresponding to the two protocols for
49
** passing an anonymous file map to a child process.
51
** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
52
** PR_CreateProcess() [for portability of the test case] to create the
53
** child process, but does not use the PRProcessAttr structure for
54
** passing the file map data.
56
** ServerTwo()/ClientTwo() tests the passing of the file map using the
57
** PRProcessAttr structure.
62
#include <private/primpl.h>
68
** Test harness infrastructure
71
PRLogModuleLevel msgLevel = PR_LOG_NONE;
72
PRUint32 failed_already = 0;
75
PRIntn client = 0; /* invoke client, style */
76
char dirName[512] = "."; /* directory name to contain anon mapped file */
77
PRSize fmSize = (100 * 1024 );
78
PRUint32 fmMode = 0600;
79
PRFileMapProtect fmProt = PR_PROT_READWRITE;
80
const char *fmEnvName = "nsprFileMapEnvVariable";
83
** Emit help text for this test
85
static void Help( void )
87
printf("anonfm [options] [dirName]\n");
88
printf("-d -- enable debug mode\n");
89
printf("dirName is alternate directory name. Default: . (current directory)\n");
97
static void ClientOne( void )
105
("ClientOne() starting"));
107
fmString = PR_GetEnv( fmEnvName );
108
if ( NULL == fmString ) {
111
("ClientOne(): PR_Getenv() failed"));
115
("ClientOne(): PR_Getenv(): found: %s", fmString));
117
fm = PR_ImportFileMapFromString( fmString );
121
("ClientOne(): PR_ImportFileMapFromString() failed"));
125
("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
127
addr = PR_MemMap( fm, LL_ZERO, fmSize );
128
if ( NULL == addr ) {
131
("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
135
("ClientOne(): PR_MemMap(): addr: %p", addr ));
137
/* write to memory map to release server */
140
rc = PR_MemUnmap( addr, fmSize );
141
PR_ASSERT( rc == PR_SUCCESS );
143
("ClientOne(): PR_MemUnap(): success" ));
145
rc = PR_CloseFileMap( fm );
146
if ( PR_FAILURE == rc ) {
149
("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
153
("ClientOne(): PR_CloseFileMap(): success" ));
156
} /* end ClientOne() */
161
static void ClientTwo( void )
164
} /* end ClientTwo() */
169
static void ServerOne( void )
182
("ServerOne() starting"));
184
fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
188
("PR_OpenAnonFileMap() failed"));
192
("ServerOne(): FileMap: %p", fm ));
194
rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
195
if ( PR_FAILURE == rc ) {
198
("PR_ExportFileMap() failed"));
203
** put the string into the environment
205
PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
208
addr = PR_MemMap( fm, LL_ZERO, fmSize );
209
if ( NULL == addr ) {
212
("PR_MemMap() failed"));
216
/* set initial value for client */
217
for (i = 0; i < (PRIntn)fmSize ; i++ )
221
("ServerOne(): PR_MemMap(): addr: %p", addr ));
224
** set arguments for child process
226
child_argv[0] = "anonfm";
227
child_argv[1] = "-C";
229
child_argv[3] = NULL;
231
proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
234
("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
237
** ClientOne() will set the memory to 1
240
("ServerOne(): waiting on Client, *addr: %x", *addr ));
241
while( *addr == 0x00 ) {
243
fprintf(stderr, ".");
244
PR_Sleep(PR_MillisecondsToInterval(300));
247
fprintf(stderr, "\n");
249
("ServerOne(): Client responded" ));
251
rc = PR_WaitProcess( proc, &exit_status );
252
PR_ASSERT( PR_FAILURE != rc );
254
rc = PR_MemUnmap( addr, fmSize);
255
if ( PR_FAILURE == rc ) {
258
("PR_MemUnmap() failed"));
262
("ServerOne(): PR_MemUnmap(): success" ));
264
rc = PR_CloseFileMap(fm);
265
if ( PR_FAILURE == rc ) {
268
("PR_CloseFileMap() failed"));
272
("ServerOne(): PR_CloseFileMap() success" ));
275
} /* end ServerOne() */
280
static void ServerTwo( void )
283
("ServerTwo(): Not implemented yet" ));
284
} /* end ServerTwo() */
287
PRIntn main(PRIntn argc, char *argv[])
291
** Get command line options
294
PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
296
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
298
if (PL_OPT_BAD == os) continue;
301
case 'C': /* Client style */
302
client = atol(opt->value);
304
case 's': /* file size */
305
fmSize = atol( opt->value ) * 1024;
307
case 'd': /* debug */
309
msgLevel = PR_LOG_DEBUG;
311
case 'h': /* help message */
315
strcpy(dirName, opt->value);
319
PL_DestroyOptState(opt);
322
lm = PR_NewLogModule("Test"); /* Initialize logging */
326
} else if ( client == 2 ) {
330
if ( failed_already ) goto Finished;
336
printf("%s\n", (failed_already)? "FAIL" : "PASS");
337
return( (failed_already == PR_TRUE )? 1 : 0 );