VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/anonfm.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39** File: anonfm.c
40** Description: Test anonymous file map
41**
42** Synopsis: anonfm [options] [dirName]
43**
44** Options:
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()
50**
51** anonfn.c contains two tests, corresponding to the two protocols for
52** passing an anonymous file map to a child process.
53**
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.
58**
59** ServerTwo()/ClientTwo() tests the passing of the file map using the
60** PRProcessAttr structure.
61**
62*/
63#include <plgetopt.h>
64#include <nspr.h>
65#include <private/primpl.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69
70/*
71** Test harness infrastructure
72*/
73PRLogModuleInfo *lm;
74PRLogModuleLevel msgLevel = PR_LOG_NONE;
75PRUint32 failed_already = 0;
76
77PRIntn debug = 0;
78PRIntn client = 0; /* invoke client, style */
79char dirName[512] = "."; /* directory name to contain anon mapped file */
80PRSize fmSize = (100 * 1024 );
81PRUint32 fmMode = 0600;
82PRFileMapProtect fmProt = PR_PROT_READWRITE;
83const char *fmEnvName = "nsprFileMapEnvVariable";
84
85/*
86** Emit help text for this test
87*/
88static void Help( void )
89{
90 printf("anonfm [options] [dirName]\n");
91 printf("-d -- enable debug mode\n");
92 printf("dirName is alternate directory name. Default: . (current directory)\n");
93 exit(1);
94} /* end Help() */
95
96
97/*
98** ClientOne() --
99*/
100static void ClientOne( void )
101{
102 PRFileMap *fm;
103 char *fmString;
104 char *addr;
105 PRStatus rc;
106
107 PR_LOG(lm, msgLevel,
108 ("ClientOne() starting"));
109
110 fmString = PR_GetEnv( fmEnvName );
111 if ( NULL == fmString ) {
112 failed_already = 1;
113 PR_LOG(lm, msgLevel,
114 ("ClientOne(): PR_Getenv() failed"));
115 return;
116 }
117 PR_LOG(lm, msgLevel,
118 ("ClientOne(): PR_Getenv(): found: %s", fmString));
119
120 fm = PR_ImportFileMapFromString( fmString );
121 if ( NULL == fm ) {
122 failed_already = 1;
123 PR_LOG(lm, msgLevel,
124 ("ClientOne(): PR_ImportFileMapFromString() failed"));
125 return;
126 }
127 PR_LOG(lm, msgLevel,
128 ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
129
130 addr = PR_MemMap( fm, LL_ZERO, fmSize );
131 if ( NULL == addr ) {
132 failed_already = 1;
133 PR_LOG(lm, msgLevel,
134 ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
135 return;
136 }
137 PR_LOG(lm, msgLevel,
138 ("ClientOne(): PR_MemMap(): addr: %p", addr ));
139
140 /* write to memory map to release server */
141 *addr = 1;
142
143 rc = PR_MemUnmap( addr, fmSize );
144 PR_ASSERT( rc == PR_SUCCESS );
145 PR_LOG(lm, msgLevel,
146 ("ClientOne(): PR_MemUnap(): success" ));
147
148 rc = PR_CloseFileMap( fm );
149 if ( PR_FAILURE == rc ) {
150 failed_already = 1;
151 PR_LOG(lm, msgLevel,
152 ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
153 return;
154 }
155 PR_LOG(lm, msgLevel,
156 ("ClientOne(): PR_CloseFileMap(): success" ));
157
158 return;
159} /* end ClientOne() */
160
161/*
162** ClientTwo() --
163*/
164static void ClientTwo( void )
165{
166 failed_already = 1;
167} /* end ClientTwo() */
168
169/*
170** ServerOne() --
171*/
172static void ServerOne( void )
173{
174 PRFileMap *fm;
175 PRStatus rc;
176 PRIntn i;
177 char *addr;
178 char fmString[256];
179 char envBuf[256];
180 char *child_argv[8];
181 PRProcess *proc;
182 PRInt32 exit_status;
183
184 PR_LOG(lm, msgLevel,
185 ("ServerOne() starting"));
186
187 fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
188 if ( NULL == fm ) {
189 failed_already = 1;
190 PR_LOG(lm, msgLevel,
191 ("PR_OpenAnonFileMap() failed"));
192 return;
193 }
194 PR_LOG(lm, msgLevel,
195 ("ServerOne(): FileMap: %p", fm ));
196
197 rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
198 if ( PR_FAILURE == rc ) {
199 failed_already = 1;
200 PR_LOG(lm, msgLevel,
201 ("PR_ExportFileMap() failed"));
202 return;
203 }
204
205 /*
206 ** put the string into the environment
207 */
208 PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
209 putenv( envBuf );
210
211 addr = PR_MemMap( fm, LL_ZERO, fmSize );
212 if ( NULL == addr ) {
213 failed_already = 1;
214 PR_LOG(lm, msgLevel,
215 ("PR_MemMap() failed"));
216 return;
217 }
218
219 /* set initial value for client */
220 for (i = 0; i < (PRIntn)fmSize ; i++ )
221 *(addr+i) = 0x00;
222
223 PR_LOG(lm, msgLevel,
224 ("ServerOne(): PR_MemMap(): addr: %p", addr ));
225
226 /*
227 ** set arguments for child process
228 */
229 child_argv[0] = "anonfm";
230 child_argv[1] = "-C";
231 child_argv[2] = "1";
232 child_argv[3] = NULL;
233
234 proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
235 PR_ASSERT( proc );
236 PR_LOG(lm, msgLevel,
237 ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
238
239 /*
240 ** ClientOne() will set the memory to 1
241 */
242 PR_LOG(lm, msgLevel,
243 ("ServerOne(): waiting on Client, *addr: %x", *addr ));
244 while( *addr == 0x00 ) {
245 if ( debug )
246 fprintf(stderr, ".");
247 PR_Sleep(PR_MillisecondsToInterval(300));
248 }
249 if ( debug )
250 fprintf(stderr, "\n");
251 PR_LOG(lm, msgLevel,
252 ("ServerOne(): Client responded" ));
253
254 rc = PR_WaitProcess( proc, &exit_status );
255 PR_ASSERT( PR_FAILURE != rc );
256
257 rc = PR_MemUnmap( addr, fmSize);
258 if ( PR_FAILURE == rc ) {
259 failed_already = 1;
260 PR_LOG(lm, msgLevel,
261 ("PR_MemUnmap() failed"));
262 return;
263 }
264 PR_LOG(lm, msgLevel,
265 ("ServerOne(): PR_MemUnmap(): success" ));
266
267 rc = PR_CloseFileMap(fm);
268 if ( PR_FAILURE == rc ) {
269 failed_already = 1;
270 PR_LOG(lm, msgLevel,
271 ("PR_CloseFileMap() failed"));
272 return;
273 }
274 PR_LOG(lm, msgLevel,
275 ("ServerOne(): PR_CloseFileMap() success" ));
276
277 return;
278} /* end ServerOne() */
279
280/*
281** ServerTwo() --
282*/
283static void ServerTwo( void )
284{
285 PR_LOG(lm, msgLevel,
286 ("ServerTwo(): Not implemented yet" ));
287} /* end ServerTwo() */
288
289
290PRIntn main(PRIntn argc, char *argv[])
291{
292 {
293 /*
294 ** Get command line options
295 */
296 PLOptStatus os;
297 PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
298
299 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
300 {
301 if (PL_OPT_BAD == os) continue;
302 switch (opt->option)
303 {
304 case 'C': /* Client style */
305 client = atol(opt->value);
306 break;
307 case 's': /* file size */
308 fmSize = atol( opt->value ) * 1024;
309 break;
310 case 'd': /* debug */
311 debug = 1;
312 msgLevel = PR_LOG_DEBUG;
313 break;
314 case 'h': /* help message */
315 Help();
316 break;
317 default:
318 strcpy(dirName, opt->value);
319 break;
320 }
321 }
322 PL_DestroyOptState(opt);
323 }
324
325 lm = PR_NewLogModule("Test"); /* Initialize logging */
326
327 if ( client == 1 ) {
328 ClientOne();
329 } else if ( client == 2 ) {
330 ClientTwo();
331 } else {
332 ServerOne();
333 if ( failed_already ) goto Finished;
334 ServerTwo();
335 }
336
337Finished:
338 if ( debug )
339 printf("%s\n", (failed_already)? "FAIL" : "PASS");
340 return( (failed_already == PR_TRUE )? 1 : 0 );
341} /* main() */
342/* end anonfm.c */
343
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette