VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/nameshm1.c@ 25803

Last change on this file since 25803 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: 17.8 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: nameshm1.c -- Test Named Shared Memory
40**
41** Description:
42** nameshm1 tests Named Shared Memory. nameshm1 performs two tests of
43** named shared memory.
44**
45** The first test is a basic test. The basic test operates as a single
46** process. The process exercises all the API elements of the facility.
47** This test also attempts to write to all locations in the shared
48** memory.
49**
50** The second test is a client-server test. The client-server test
51** creates a new instance of nameshm1, passing the -C argument to the
52** new process; this creates the client-side process. The server-side
53** (the instance of nameshm1 created from the command line) and the
54** client-side interact via inter-process semaphores to verify that the
55** shared memory segment can be read and written by both sides in a
56** synchronized maner.
57**
58** Note: Because this test runs in two processes, the log files created
59** by the test are not in chronological sequence; makes it hard to read.
60** As a temporary circumvention, I changed the definition(s) of the
61** _PUT_LOG() macro in prlog.c to force a flushall(), or equivalent.
62** This causes the log entries to be emitted in true chronological
63** order.
64**
65** Synopsis: nameshm1 [options] [name]
66**
67** Options:
68** -d Enables debug trace via PR_LOG()
69** -v Enables verbose mode debug trace via PR_LOG()
70** -w Causes the basic test to attempt to write to the segment
71** mapped as read-only. When this option is specified, the
72** test should crash with a seg-fault; this is a destructive
73** test and is considered successful when it seg-faults.
74**
75** -C Causes nameshm1 to start as the client-side of a
76** client-server pair of processes. Only the instance
77** of nameshm1 operating as the server-side process should
78** specify the -C option when creating the client-side process;
79** the -C option should not be specified at the command line.
80** The client-side uses the shared memory segment created by
81** the server-side to communicate with the server-side
82** process.
83**
84** -p <n> Specify the number of iterations the client-server tests
85** should perform. Default: 1000.
86**
87** -s <n> Size, in KBytes (1024), of the shared memory segment.
88** Default: (10 * 1024)
89**
90** -i <n> Number of client-side iterations. Default: 3
91**
92** name specifies the name of the shared memory segment to be used.
93** Default: /tmp/xxxNSPRshm
94**
95**
96** See also: prshm.h
97**
98** /lth. Aug-1999.
99*/
100
101#include <plgetopt.h>
102#include <nspr.h>
103#include <stdlib.h>
104#include <string.h>
105#include <private/primpl.h>
106
107#define SEM_NAME1 "/tmp/nameshmSEM1"
108#define SEM_NAME2 "/tmp/nameshmSEM2"
109#define SEM_MODE 0666
110#define SHM_MODE 0666
111
112#define NameSize (1024)
113
114PRIntn debug = 0;
115PRIntn failed_already = 0;
116PRLogModuleLevel msgLevel = PR_LOG_NONE;
117PRLogModuleInfo *lm;
118
119/* command line options */
120PRIntn optDebug = 0;
121PRIntn optVerbose = 0;
122PRUint32 optWriteRO = 0; /* test write to read-only memory. should crash */
123PRUint32 optClient = 0;
124PRUint32 optCreate = 1;
125PRUint32 optAttachRW = 1;
126PRUint32 optAttachRO = 1;
127PRUint32 optClose = 1;
128PRUint32 optDelete = 1;
129PRInt32 optPing = 1000;
130PRUint32 optSize = (10 * 1024 );
131PRInt32 optClientIterations = 3;
132char optName[NameSize] = "/tmp/xxxNSPRshm";
133
134char buf[1024] = "";
135
136
137static void BasicTest( void )
138{
139 PRSharedMemory *shm;
140 char *addr; /* address of shared memory segment */
141 PRUint32 i;
142 PRInt32 rc;
143
144 PR_LOG( lm, msgLevel,
145 ( "nameshm1: Begin BasicTest" ));
146
147 if ( PR_FAILURE == PR_DeleteSharedMemory( optName )) {
148 PR_LOG( lm, msgLevel,
149 ("nameshm1: Initial PR_DeleteSharedMemory() failed. No problem"));
150 } else
151 PR_LOG( lm, msgLevel,
152 ("nameshm1: Initial PR_DeleteSharedMemory() success"));
153
154
155 shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE );
156 if ( NULL == shm )
157 {
158 PR_LOG( lm, msgLevel,
159 ( "nameshm1: RW Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
160 failed_already = 1;
161 return;
162 }
163 PR_LOG( lm, msgLevel,
164 ( "nameshm1: RW Create: success: %p", shm ));
165
166 addr = PR_AttachSharedMemory( shm , 0 );
167 if ( NULL == addr )
168 {
169 PR_LOG( lm, msgLevel,
170 ( "nameshm1: RW Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
171 failed_already = 1;
172 return;
173 }
174 PR_LOG( lm, msgLevel,
175 ( "nameshm1: RW Attach: success: %p", addr ));
176
177 /* fill memory with i */
178 for ( i = 0; i < optSize ; i++ )
179 {
180 *(addr + i) = i;
181 }
182
183 rc = PR_DetachSharedMemory( shm, addr );
184 if ( PR_FAILURE == rc )
185 {
186 PR_LOG( lm, msgLevel,
187 ( "nameshm1: RW Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
188 failed_already = 1;
189 return;
190 }
191 PR_LOG( lm, msgLevel,
192 ( "nameshm1: RW Detach: success: " ));
193
194 rc = PR_CloseSharedMemory( shm );
195 if ( PR_FAILURE == rc )
196 {
197 PR_LOG( lm, msgLevel,
198 ( "nameshm1: RW Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
199 failed_already = 1;
200 return;
201 }
202 PR_LOG( lm, msgLevel,
203 ( "nameshm1: RW Close: success: " ));
204
205 rc = PR_DeleteSharedMemory( optName );
206 if ( PR_FAILURE == rc )
207 {
208 PR_LOG( lm, msgLevel,
209 ( "nameshm1: RW Delete: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
210 failed_already = 1;
211 return;
212 }
213 PR_LOG( lm, msgLevel,
214 ( "nameshm1: RW Delete: success: " ));
215
216 PR_LOG( lm, msgLevel,
217 ("nameshm1: BasicTest(): Passed"));
218
219 return;
220} /* end BasicTest() */
221
222static void ReadOnlyTest( void )
223{
224 PRSharedMemory *shm;
225 char *roAddr; /* read-only address of shared memory segment */
226 PRInt32 rc;
227
228 PR_LOG( lm, msgLevel,
229 ( "nameshm1: Begin ReadOnlyTest" ));
230
231 shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE);
232 if ( NULL == shm )
233 {
234 PR_LOG( lm, msgLevel,
235 ( "nameshm1: RO Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
236 failed_already = 1;
237 return;
238 }
239 PR_LOG( lm, msgLevel,
240 ( "nameshm1: RO Create: success: %p", shm ));
241
242
243 roAddr = PR_AttachSharedMemory( shm , PR_SHM_READONLY );
244 if ( NULL == roAddr )
245 {
246 PR_LOG( lm, msgLevel,
247 ( "nameshm1: RO Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
248 failed_already = 1;
249 return;
250 }
251 PR_LOG( lm, msgLevel,
252 ( "nameshm1: RO Attach: success: %p", roAddr ));
253
254 if ( optWriteRO )
255 {
256 *roAddr = 0x00; /* write to read-only memory */
257 failed_already = 1;
258 PR_LOG( lm, msgLevel, ("nameshm1: Wrote to read-only memory segment!"));
259 return;
260 }
261
262 rc = PR_DetachSharedMemory( shm, roAddr );
263 if ( PR_FAILURE == rc )
264 {
265 PR_LOG( lm, msgLevel,
266 ( "nameshm1: RO Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
267 failed_already = 1;
268 return;
269 }
270 PR_LOG( lm, msgLevel,
271 ( "nameshm1: RO Detach: success: " ));
272
273 rc = PR_CloseSharedMemory( shm );
274 if ( PR_FAILURE == rc )
275 {
276 PR_LOG( lm, msgLevel,
277 ( "nameshm1: RO Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
278 failed_already = 1;
279 return;
280 }
281 PR_LOG( lm, msgLevel,
282 ( "nameshm1: RO Close: success: " ));
283
284 rc = PR_DeleteSharedMemory( optName );
285 if ( PR_FAILURE == rc )
286 {
287 PR_LOG( lm, msgLevel,
288 ( "nameshm1: RO Destroy: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
289 failed_already = 1;
290 return;
291 }
292 PR_LOG( lm, msgLevel,
293 ( "nameshm1: RO Destroy: success: " ));
294
295 PR_LOG( lm, msgLevel,
296 ("nameshm1: ReadOnlyTest(): Passed"));
297
298 return;
299} /* end ReadOnlyTest() */
300
301static void DoClient( void )
302{
303 PRStatus rc;
304 PRSem *sem1, *sem2;
305 PRSharedMemory *shm;
306 PRUint32 *addr;
307 PRInt32 i;
308
309 PR_LOG( lm, msgLevel,
310 ("nameshm1: DoClient(): Starting"));
311
312 sem1 = PR_OpenSemaphore( SEM_NAME1, 0, 0, 0 );
313 PR_ASSERT( sem1 );
314
315 sem2 = PR_OpenSemaphore( SEM_NAME2, 0, 0, 0 );
316 PR_ASSERT( sem1 );
317
318 shm = PR_OpenSharedMemory( optName, optSize, 0, SHM_MODE );
319 if ( NULL == shm )
320 {
321 PR_LOG( lm, msgLevel,
322 ( "nameshm1: DoClient(): Create: Error: %ld. OSError: %ld",
323 PR_GetError(), PR_GetOSError()));
324 failed_already = 1;
325 return;
326 }
327 PR_LOG( lm, msgLevel,
328 ( "nameshm1: DoClient(): Create: success: %p", shm ));
329
330 addr = PR_AttachSharedMemory( shm , 0 );
331 if ( NULL == addr )
332 {
333 PR_LOG( lm, msgLevel,
334 ( "nameshm1: DoClient(): Attach: Error: %ld. OSError: %ld",
335 PR_GetError(), PR_GetOSError()));
336 failed_already = 1;
337 return;
338 }
339 PR_LOG( lm, msgLevel,
340 ( "nameshm1: DoClient(): Attach: success: %p", addr ));
341
342 PR_LOG( lm, msgLevel,
343 ( "Client found: %s", addr));
344
345 PR_Sleep(PR_SecondsToInterval(4));
346 for ( i = 0 ; i < optPing ; i++ )
347 {
348 rc = PR_WaitSemaphore( sem2 );
349 PR_ASSERT( PR_FAILURE != rc );
350
351 (*addr)++;
352 PR_ASSERT( (*addr % 2) == 0 );
353 if ( optVerbose )
354 PR_LOG( lm, msgLevel,
355 ( "nameshm1: Client ping: %d, i: %d", *addr, i));
356
357 rc = PR_PostSemaphore( sem1 );
358 PR_ASSERT( PR_FAILURE != rc );
359 }
360
361 rc = PR_CloseSemaphore( sem1 );
362 PR_ASSERT( PR_FAILURE != rc );
363
364 rc = PR_CloseSemaphore( sem2 );
365 PR_ASSERT( PR_FAILURE != rc );
366
367 rc = PR_DetachSharedMemory( shm, addr );
368 if ( PR_FAILURE == rc )
369 {
370 PR_LOG( lm, msgLevel,
371 ( "nameshm1: DoClient(): Detach: Error: %ld. OSError: %ld",
372 PR_GetError(), PR_GetOSError()));
373 failed_already = 1;
374 return;
375 }
376 PR_LOG( lm, msgLevel,
377 ( "nameshm1: DoClient(): Detach: success: " ));
378
379 rc = PR_CloseSharedMemory( shm );
380 if ( PR_FAILURE == rc )
381 {
382 PR_LOG( lm, msgLevel,
383 ( "nameshm1: DoClient(): Close: Error: %ld. OSError: %ld",
384 PR_GetError(), PR_GetOSError()));
385 failed_already = 1;
386 return;
387 }
388 PR_LOG( lm, msgLevel,
389 ( "nameshm1: DoClient(): Close: success: " ));
390
391 return;
392} /* end DoClient() */
393
394static void ClientServerTest( void )
395{
396 PRStatus rc;
397 PRSem *sem1, *sem2;
398 PRProcess *proc;
399 PRInt32 exit_status;
400 PRSharedMemory *shm;
401 PRUint32 *addr;
402 PRInt32 i;
403 char *child_argv[8];
404 char buf[24];
405
406 PR_LOG( lm, msgLevel,
407 ( "nameshm1: Begin ClientServerTest" ));
408
409 rc = PR_DeleteSharedMemory( optName );
410 if ( PR_FAILURE == rc )
411 {
412 PR_LOG( lm, msgLevel,
413 ( "nameshm1: Server: Destroy: failed. No problem"));
414 } else
415 PR_LOG( lm, msgLevel,
416 ( "nameshm1: Server: Destroy: success" ));
417
418
419 shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE);
420 if ( NULL == shm )
421 {
422 PR_LOG( lm, msgLevel,
423 ( "nameshm1: Server: Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
424 failed_already = 1;
425 return;
426 }
427 PR_LOG( lm, msgLevel,
428 ( "nameshm1: Server: Create: success: %p", shm ));
429
430 addr = PR_AttachSharedMemory( shm , 0 );
431 if ( NULL == addr )
432 {
433 PR_LOG( lm, msgLevel,
434 ( "nameshm1: Server: Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
435 failed_already = 1;
436 return;
437 }
438 PR_LOG( lm, msgLevel,
439 ( "nameshm1: Server: Attach: success: %p", addr ));
440
441 sem1 = PR_OpenSemaphore( SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0 );
442 PR_ASSERT( sem1 );
443
444 sem2 = PR_OpenSemaphore( SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 1 );
445 PR_ASSERT( sem1 );
446
447 strcpy( (char*)addr, "FooBar" );
448
449 child_argv[0] = "nameshm1";
450 child_argv[1] = "-C";
451 child_argv[2] = "-p";
452 sprintf( buf, "%d", optPing );
453 child_argv[3] = buf;
454 child_argv[4] = optName;
455 child_argv[5] = NULL;
456
457 proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
458 PR_ASSERT( proc );
459
460 PR_Sleep( PR_SecondsToInterval(4));
461
462 *addr = 1;
463 for ( i = 0 ; i < optPing ; i++ )
464 {
465 rc = PR_WaitSemaphore( sem1 );
466 PR_ASSERT( PR_FAILURE != rc );
467
468 (*addr)++;
469 PR_ASSERT( (*addr % 2) == 1 );
470 if ( optVerbose )
471 PR_LOG( lm, msgLevel,
472 ( "nameshm1: Server pong: %d, i: %d", *addr, i));
473
474
475 rc = PR_PostSemaphore( sem2 );
476 PR_ASSERT( PR_FAILURE != rc );
477 }
478
479 rc = PR_WaitProcess( proc, &exit_status );
480 PR_ASSERT( PR_FAILURE != rc );
481
482 rc = PR_CloseSemaphore( sem1 );
483 PR_ASSERT( PR_FAILURE != rc );
484
485 rc = PR_CloseSemaphore( sem2 );
486 PR_ASSERT( PR_FAILURE != rc );
487
488 rc = PR_DeleteSemaphore( SEM_NAME1 );
489 PR_ASSERT( PR_FAILURE != rc );
490
491 rc = PR_DeleteSemaphore( SEM_NAME2 );
492 PR_ASSERT( PR_FAILURE != rc );
493
494 rc = PR_DetachSharedMemory( shm, addr );
495 if ( PR_FAILURE == rc )
496 {
497 PR_LOG( lm, msgLevel,
498 ( "nameshm1: Server: Detach: Error: %ld. OSError: %ld",
499 PR_GetError(), PR_GetOSError()));
500 failed_already = 1;
501 return;
502 }
503 PR_LOG( lm, msgLevel,
504 ( "nameshm1: Server: Detach: success: " ));
505
506 rc = PR_CloseSharedMemory( shm );
507 if ( PR_FAILURE == rc )
508 {
509 PR_LOG( lm, msgLevel,
510 ( "nameshm1: Server: Close: Error: %ld. OSError: %ld",
511 PR_GetError(), PR_GetOSError()));
512 failed_already = 1;
513 return;
514 }
515 PR_LOG( lm, msgLevel,
516 ( "nameshm1: Server: Close: success: " ));
517
518 rc = PR_DeleteSharedMemory( optName );
519 if ( PR_FAILURE == rc )
520 {
521 PR_LOG( lm, msgLevel,
522 ( "nameshm1: Server: Destroy: Error: %ld. OSError: %ld",
523 PR_GetError(), PR_GetOSError()));
524 failed_already = 1;
525 return;
526 }
527 PR_LOG( lm, msgLevel,
528 ( "nameshm1: Server: Destroy: success" ));
529
530 return;
531} /* end ClientServerTest() */
532
533PRIntn main(PRIntn argc, char *argv[])
534{
535 {
536 /*
537 ** Get command line options
538 */
539 PLOptStatus os;
540 PLOptState *opt = PL_CreateOptState(argc, argv, "Cdvw:s:p:i:");
541
542 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
543 {
544 if (PL_OPT_BAD == os) continue;
545 switch (opt->option)
546 {
547 case 'v': /* debug mode */
548 optVerbose = 1;
549 /* no break! fall into debug option */
550 case 'd': /* debug mode */
551 debug = 1;
552 msgLevel = PR_LOG_DEBUG;
553 break;
554 case 'w': /* try writing to memory mapped read-only */
555 optWriteRO = 1;
556 break;
557 case 'C':
558 optClient = 1;
559 break;
560 case 's':
561 optSize = atol(opt->value) * 1024;
562 break;
563 case 'p':
564 optPing = atol(opt->value);
565 break;
566 case 'i':
567 optClientIterations = atol(opt->value);
568 break;
569 default:
570 strcpy( optName, opt->value );
571 break;
572 }
573 }
574 PL_DestroyOptState(opt);
575 }
576
577 lm = PR_NewLogModule("Test"); /* Initialize logging */
578
579 PR_LOG( lm, msgLevel,
580 ( "nameshm1: Starting" ));
581
582 if ( optClient )
583 {
584 DoClient();
585 } else {
586 BasicTest();
587 if ( failed_already != 0 )
588 goto Finished;
589 ReadOnlyTest();
590 if ( failed_already != 0 )
591 goto Finished;
592 ClientServerTest();
593 }
594
595Finished:
596 if ( debug ) printf("%s\n", (failed_already)? "FAIL" : "PASS" );
597 return( (failed_already)? 1 : 0 );
598} /* main() */
599/* end instrumt.c */
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