nsprpub/pr/tests/nameshm1.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/nameshm1.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,576 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 +** File: nameshm1.c -- Test Named Shared Memory
    1.11 +**
    1.12 +** Description: 
    1.13 +** nameshm1 tests Named Shared Memory. nameshm1 performs two tests of
    1.14 +** named shared memory. 
    1.15 +** 
    1.16 +** The first test is a basic test. The basic test operates as a single
    1.17 +** process. The process exercises all the API elements of the facility.
    1.18 +** This test also attempts to write to all locations in the shared
    1.19 +** memory.
    1.20 +**
    1.21 +** The second test is a client-server test. The client-server test
    1.22 +** creates a new instance of nameshm1, passing the -C argument to the
    1.23 +** new process; this creates the client-side process. The server-side
    1.24 +** (the instance of nameshm1 created from the command line) and the
    1.25 +** client-side interact via inter-process semaphores to verify that the
    1.26 +** shared memory segment can be read and written by both sides in a
    1.27 +** synchronized maner.
    1.28 +**
    1.29 +** Note: Because this test runs in two processes, the log files created
    1.30 +** by the test are not in chronological sequence; makes it hard to read.
    1.31 +** As a temporary circumvention, I changed the definition(s) of the
    1.32 +** _PUT_LOG() macro in prlog.c to force a flushall(), or equivalent.
    1.33 +** This causes the log entries to be emitted in true chronological
    1.34 +** order.
    1.35 +**
    1.36 +** Synopsis: nameshm1 [options] [name]
    1.37 +** 
    1.38 +** Options:
    1.39 +** -d       Enables debug trace via PR_LOG()
    1.40 +** -v       Enables verbose mode debug trace via PR_LOG()
    1.41 +** -w       Causes the basic test to attempt to write to the segment
    1.42 +**          mapped as read-only. When this option is specified, the
    1.43 +**          test should crash with a seg-fault; this is a destructive
    1.44 +**          test and is considered successful when it seg-faults.
    1.45 +** 
    1.46 +** -C       Causes nameshm1 to start as the client-side of a
    1.47 +**          client-server pair of processes. Only the instance
    1.48 +**          of nameshm1 operating as the server-side process should
    1.49 +**          specify the -C option when creating the client-side process;
    1.50 +**          the -C option should not be specified at the command line.
    1.51 +**          The client-side uses the shared memory segment created by
    1.52 +**          the server-side to communicate with the server-side
    1.53 +**          process.
    1.54 +**          
    1.55 +** -p <n>   Specify the number of iterations the client-server tests
    1.56 +**          should perform. Default: 1000.
    1.57 +**
    1.58 +** -s <n>   Size, in KBytes (1024), of the shared memory segment.
    1.59 +**          Default: (10 * 1024)
    1.60 +**
    1.61 +** -i <n>   Number of client-side iterations. Default: 3
    1.62 +**
    1.63 +** name     specifies the name of the shared memory segment to be used.
    1.64 +**          Default: /tmp/xxxNSPRshm
    1.65 +**
    1.66 +**
    1.67 +** See also: prshm.h
    1.68 +**
    1.69 +** /lth. Aug-1999.
    1.70 +*/
    1.71 +
    1.72 +#include <plgetopt.h> 
    1.73 +#include <nspr.h>
    1.74 +#include <stdlib.h>
    1.75 +#include <string.h>
    1.76 +#include <private/primpl.h>
    1.77 +
    1.78 +#ifdef SYMBIAN
    1.79 +#define SEM_NAME1 "c:\\data\\nameshmSEM1"
    1.80 +#define SEM_NAME2 "c:\\data\\nameshmSEM2"
    1.81 +#define OPT_NAME "c:\\data\\xxxNSPRshm"
    1.82 +#define EXE_NAME "nspr_tests_nameshm1.exe"
    1.83 +#else
    1.84 +#define SEM_NAME1 "/tmp/nameshmSEM1"
    1.85 +#define SEM_NAME2 "/tmp/nameshmSEM2"
    1.86 +#define OPT_NAME "/tmp/xxxNSPRshm"
    1.87 +#define EXE_NAME "nameshm1"
    1.88 +#endif
    1.89 +#define SEM_MODE  0666
    1.90 +#define SHM_MODE  0666
    1.91 +
    1.92 +#define NameSize (1024)
    1.93 +
    1.94 +PRIntn  debug = 0;
    1.95 +PRIntn  failed_already = 0;
    1.96 +PRLogModuleLevel msgLevel = PR_LOG_NONE;
    1.97 +PRLogModuleInfo *lm;
    1.98 +
    1.99 +/* command line options */
   1.100 +PRIntn      optDebug = 0;
   1.101 +PRIntn      optVerbose = 0;
   1.102 +PRUint32    optWriteRO = 0;     /* test write to read-only memory. should crash  */
   1.103 +PRUint32    optClient = 0;
   1.104 +PRUint32    optCreate = 1;
   1.105 +PRUint32    optAttachRW = 1;
   1.106 +PRUint32    optAttachRO = 1;
   1.107 +PRUint32    optClose = 1;
   1.108 +PRUint32    optDelete = 1;
   1.109 +PRInt32     optPing = 1000;
   1.110 +PRUint32    optSize = (10 * 1024 );
   1.111 +PRInt32     optClientIterations = 3;
   1.112 +char        optName[NameSize] = OPT_NAME;
   1.113 +
   1.114 +char buf[1024] = "";
   1.115 +
   1.116 +
   1.117 +static void BasicTest( void ) 
   1.118 +{
   1.119 +    PRSharedMemory  *shm;
   1.120 +    char *addr; /* address of shared memory segment */
   1.121 +    PRUint32  i;
   1.122 +    PRInt32 rc;
   1.123 +
   1.124 +    PR_LOG( lm, msgLevel,
   1.125 +             ( "nameshm1: Begin BasicTest" ));
   1.126 +
   1.127 +    if ( PR_FAILURE == PR_DeleteSharedMemory( optName )) {
   1.128 +        PR_LOG( lm, msgLevel,
   1.129 +            ("nameshm1: Initial PR_DeleteSharedMemory() failed. No problem"));
   1.130 +    } else
   1.131 +        PR_LOG( lm, msgLevel,
   1.132 +            ("nameshm1: Initial PR_DeleteSharedMemory() success"));
   1.133 +
   1.134 +
   1.135 +    shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE );
   1.136 +    if ( NULL == shm )
   1.137 +    {
   1.138 +        PR_LOG( lm, msgLevel,
   1.139 +                 ( "nameshm1: RW Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.140 +        failed_already = 1;
   1.141 +        return;
   1.142 +    }
   1.143 +    PR_LOG( lm, msgLevel,
   1.144 +             ( "nameshm1: RW Create: success: %p", shm ));
   1.145 +
   1.146 +    addr = PR_AttachSharedMemory( shm , 0 );
   1.147 +    if ( NULL == addr ) 
   1.148 +    {
   1.149 +        PR_LOG( lm, msgLevel,
   1.150 +                 ( "nameshm1: RW Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.151 +        failed_already = 1;
   1.152 +        return;
   1.153 +    }
   1.154 +    PR_LOG( lm, msgLevel,
   1.155 +             ( "nameshm1: RW Attach: success: %p", addr ));
   1.156 +
   1.157 +    /* fill memory with i */
   1.158 +    for ( i = 0; i < optSize ;  i++ )
   1.159 +    {
   1.160 +         *(addr + i) = i;
   1.161 +    }
   1.162 +
   1.163 +    rc = PR_DetachSharedMemory( shm, addr );
   1.164 +    if ( PR_FAILURE == rc )
   1.165 +    {
   1.166 +        PR_LOG( lm, msgLevel,
   1.167 +                 ( "nameshm1: RW Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.168 +        failed_already = 1;
   1.169 +        return;
   1.170 +    }
   1.171 +    PR_LOG( lm, msgLevel,
   1.172 +             ( "nameshm1: RW Detach: success: " ));
   1.173 +
   1.174 +    rc = PR_CloseSharedMemory( shm );
   1.175 +    if ( PR_FAILURE == rc )
   1.176 +    {
   1.177 +        PR_LOG( lm, msgLevel,
   1.178 +                 ( "nameshm1: RW Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.179 +        failed_already = 1;
   1.180 +        return;
   1.181 +    }
   1.182 +    PR_LOG( lm, msgLevel,
   1.183 +             ( "nameshm1: RW Close: success: " ));
   1.184 +
   1.185 +    rc = PR_DeleteSharedMemory( optName );
   1.186 +    if ( PR_FAILURE == rc )
   1.187 +    {
   1.188 +        PR_LOG( lm, msgLevel,
   1.189 +                 ( "nameshm1: RW Delete: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.190 +        failed_already = 1;
   1.191 +        return;
   1.192 +    }
   1.193 +    PR_LOG( lm, msgLevel,
   1.194 +             ( "nameshm1: RW Delete: success: " ));
   1.195 +
   1.196 +    PR_LOG( lm, msgLevel,
   1.197 +            ("nameshm1: BasicTest(): Passed"));
   1.198 +
   1.199 +    return;
   1.200 +} /* end BasicTest() */
   1.201 +
   1.202 +static void ReadOnlyTest( void )
   1.203 +{
   1.204 +    PRSharedMemory  *shm;
   1.205 +    char *roAddr; /* read-only address of shared memory segment */
   1.206 +    PRInt32 rc;
   1.207 +
   1.208 +    PR_LOG( lm, msgLevel,
   1.209 +             ( "nameshm1: Begin ReadOnlyTest" ));
   1.210 +
   1.211 +    shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE);
   1.212 +    if ( NULL == shm )
   1.213 +    {
   1.214 +        PR_LOG( lm, msgLevel,
   1.215 +                 ( "nameshm1: RO Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.216 +        failed_already = 1;
   1.217 +        return;
   1.218 +    }
   1.219 +    PR_LOG( lm, msgLevel,
   1.220 +             ( "nameshm1: RO Create: success: %p", shm ));
   1.221 +
   1.222 +
   1.223 +    roAddr = PR_AttachSharedMemory( shm , PR_SHM_READONLY );
   1.224 +    if ( NULL == roAddr ) 
   1.225 +    {
   1.226 +        PR_LOG( lm, msgLevel,
   1.227 +                 ( "nameshm1: RO Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.228 +        failed_already = 1;
   1.229 +        return;
   1.230 +    }
   1.231 +    PR_LOG( lm, msgLevel,
   1.232 +             ( "nameshm1: RO Attach: success: %p", roAddr ));
   1.233 +
   1.234 +    if ( optWriteRO )
   1.235 +    {
   1.236 +        *roAddr = 0x00; /* write to read-only memory */
   1.237 +        failed_already = 1;
   1.238 +        PR_LOG( lm, msgLevel, ("nameshm1: Wrote to read-only memory segment!"));
   1.239 +        return;
   1.240 +    }
   1.241 +
   1.242 +    rc = PR_DetachSharedMemory( shm, roAddr );
   1.243 +    if ( PR_FAILURE == rc )
   1.244 +    {
   1.245 +        PR_LOG( lm, msgLevel,
   1.246 +                 ( "nameshm1: RO Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.247 +        failed_already = 1;
   1.248 +        return;
   1.249 +    }
   1.250 +    PR_LOG( lm, msgLevel,
   1.251 +             ( "nameshm1: RO Detach: success: " ));
   1.252 +
   1.253 +    rc = PR_CloseSharedMemory( shm );
   1.254 +    if ( PR_FAILURE == rc )
   1.255 +    {
   1.256 +        PR_LOG( lm, msgLevel,
   1.257 +                 ( "nameshm1: RO Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.258 +        failed_already = 1;
   1.259 +        return;
   1.260 +    }
   1.261 +    PR_LOG( lm, msgLevel,
   1.262 +             ( "nameshm1: RO Close: success: " ));
   1.263 +
   1.264 +    rc = PR_DeleteSharedMemory( optName );
   1.265 +    if ( PR_FAILURE == rc )
   1.266 +    {
   1.267 +        PR_LOG( lm, msgLevel,
   1.268 +                 ( "nameshm1: RO Destroy: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.269 +        failed_already = 1;
   1.270 +        return;
   1.271 +    }
   1.272 +    PR_LOG( lm, msgLevel,
   1.273 +             ( "nameshm1: RO Destroy: success: " ));
   1.274 +
   1.275 +    PR_LOG( lm, msgLevel,
   1.276 +        ("nameshm1: ReadOnlyTest(): Passed"));
   1.277 +
   1.278 +    return;
   1.279 +} /* end ReadOnlyTest() */
   1.280 +
   1.281 +static void DoClient( void )
   1.282 +{
   1.283 +    PRStatus rc;
   1.284 +    PRSem *sem1, *sem2;
   1.285 +    PRSharedMemory  *shm;
   1.286 +    PRUint32 *addr; 
   1.287 +    PRInt32 i;
   1.288 +
   1.289 +    PR_LOG( lm, msgLevel,
   1.290 +            ("nameshm1: DoClient(): Starting"));
   1.291 +
   1.292 +    sem1 = PR_OpenSemaphore( SEM_NAME1, 0, 0, 0 );
   1.293 +    PR_ASSERT( sem1 );
   1.294 +
   1.295 +    sem2 = PR_OpenSemaphore( SEM_NAME2, 0, 0, 0 );
   1.296 +    PR_ASSERT( sem1 );
   1.297 +
   1.298 +    shm = PR_OpenSharedMemory( optName, optSize, 0, SHM_MODE );
   1.299 +    if ( NULL == shm )
   1.300 +    {
   1.301 +        PR_LOG( lm, msgLevel,
   1.302 +            ( "nameshm1: DoClient(): Create: Error: %ld. OSError: %ld", 
   1.303 +                PR_GetError(), PR_GetOSError()));
   1.304 +        failed_already = 1;
   1.305 +        return;
   1.306 +    }
   1.307 +    PR_LOG( lm, msgLevel,
   1.308 +             ( "nameshm1: DoClient(): Create: success: %p", shm ));
   1.309 +
   1.310 +    addr = PR_AttachSharedMemory( shm , 0 );
   1.311 +    if ( NULL == addr ) 
   1.312 +    {
   1.313 +        PR_LOG( lm, msgLevel,
   1.314 +            ( "nameshm1: DoClient(): Attach: Error: %ld. OSError: %ld", 
   1.315 +                PR_GetError(), PR_GetOSError()));
   1.316 +        failed_already = 1;
   1.317 +        return;
   1.318 +    }
   1.319 +    PR_LOG( lm, msgLevel,
   1.320 +             ( "nameshm1: DoClient(): Attach: success: %p", addr ));
   1.321 +
   1.322 +    PR_LOG( lm, msgLevel,
   1.323 +        ( "Client found: %s", addr));
   1.324 +
   1.325 +    PR_Sleep(PR_SecondsToInterval(4));
   1.326 +    for ( i = 0 ; i < optPing ; i++ )
   1.327 +    {
   1.328 +        rc = PR_WaitSemaphore( sem2 );
   1.329 +        PR_ASSERT( PR_FAILURE != rc );
   1.330 +        
   1.331 +        (*addr)++;
   1.332 +        PR_ASSERT( (*addr % 2) == 0 );        
   1.333 +        if ( optVerbose )
   1.334 +            PR_LOG( lm, msgLevel,
   1.335 +                 ( "nameshm1: Client ping: %d, i: %d", *addr, i));
   1.336 +
   1.337 +        rc = PR_PostSemaphore( sem1 );
   1.338 +        PR_ASSERT( PR_FAILURE != rc );
   1.339 +    }
   1.340 +
   1.341 +    rc = PR_CloseSemaphore( sem1 );
   1.342 +    PR_ASSERT( PR_FAILURE != rc );
   1.343 +
   1.344 +    rc = PR_CloseSemaphore( sem2 );
   1.345 +    PR_ASSERT( PR_FAILURE != rc );
   1.346 +
   1.347 +    rc = PR_DetachSharedMemory( shm, addr );
   1.348 +    if ( PR_FAILURE == rc )
   1.349 +    {
   1.350 +        PR_LOG( lm, msgLevel,
   1.351 +            ( "nameshm1: DoClient(): Detach: Error: %ld. OSError: %ld", 
   1.352 +                PR_GetError(), PR_GetOSError()));
   1.353 +        failed_already = 1;
   1.354 +        return;
   1.355 +    }
   1.356 +    PR_LOG( lm, msgLevel,
   1.357 +             ( "nameshm1: DoClient(): Detach: success: " ));
   1.358 +
   1.359 +    rc = PR_CloseSharedMemory( shm );
   1.360 +    if ( PR_FAILURE == rc )
   1.361 +    {
   1.362 +        PR_LOG( lm, msgLevel,
   1.363 +            ( "nameshm1: DoClient(): Close: Error: %ld. OSError: %ld", 
   1.364 +                PR_GetError(), PR_GetOSError()));
   1.365 +        failed_already = 1;
   1.366 +        return;
   1.367 +    }
   1.368 +    PR_LOG( lm, msgLevel,
   1.369 +             ( "nameshm1: DoClient(): Close: success: " ));
   1.370 +
   1.371 +    return;
   1.372 +}    /* end DoClient() */
   1.373 +
   1.374 +static void ClientServerTest( void )
   1.375 +{
   1.376 +    PRStatus rc;
   1.377 +    PRSem *sem1, *sem2;
   1.378 +    PRProcess *proc;
   1.379 +    PRInt32 exit_status;
   1.380 +    PRSharedMemory  *shm;
   1.381 +    PRUint32 *addr; 
   1.382 +    PRInt32 i;
   1.383 +    char *child_argv[8];
   1.384 +    char buf[24];
   1.385 +
   1.386 +    PR_LOG( lm, msgLevel,
   1.387 +             ( "nameshm1: Begin ClientServerTest" ));
   1.388 +
   1.389 +    rc = PR_DeleteSharedMemory( optName );
   1.390 +    if ( PR_FAILURE == rc )
   1.391 +    {
   1.392 +        PR_LOG( lm, msgLevel,
   1.393 +            ( "nameshm1: Server: Destroy: failed. No problem"));
   1.394 +    } else
   1.395 +        PR_LOG( lm, msgLevel,
   1.396 +            ( "nameshm1: Server: Destroy: success" ));
   1.397 +
   1.398 +
   1.399 +    shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE);
   1.400 +    if ( NULL == shm )
   1.401 +    {
   1.402 +        PR_LOG( lm, msgLevel,
   1.403 +                 ( "nameshm1: Server: Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.404 +        failed_already = 1;
   1.405 +        return;
   1.406 +    }
   1.407 +    PR_LOG( lm, msgLevel,
   1.408 +             ( "nameshm1: Server: Create: success: %p", shm ));
   1.409 +
   1.410 +    addr = PR_AttachSharedMemory( shm , 0 );
   1.411 +    if ( NULL == addr ) 
   1.412 +    {
   1.413 +        PR_LOG( lm, msgLevel,
   1.414 +                 ( "nameshm1: Server: Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
   1.415 +        failed_already = 1;
   1.416 +        return;
   1.417 +    }
   1.418 +    PR_LOG( lm, msgLevel,
   1.419 +             ( "nameshm1: Server: Attach: success: %p", addr ));
   1.420 +
   1.421 +    sem1 = PR_OpenSemaphore( SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0 );
   1.422 +    PR_ASSERT( sem1 );
   1.423 +
   1.424 +    sem2 = PR_OpenSemaphore( SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 1 );
   1.425 +    PR_ASSERT( sem1 );
   1.426 +
   1.427 +    strcpy( (char*)addr, "FooBar" );
   1.428 +
   1.429 +    child_argv[0] = EXE_NAME;
   1.430 +    child_argv[1] = "-C";
   1.431 +    child_argv[2] = "-p";
   1.432 +    sprintf( buf, "%d", optPing );
   1.433 +    child_argv[3] = buf;
   1.434 +    child_argv[4] = optName;
   1.435 +    child_argv[5] = NULL;
   1.436 +
   1.437 +    proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
   1.438 +    PR_ASSERT( proc );
   1.439 +
   1.440 +    PR_Sleep( PR_SecondsToInterval(4));
   1.441 +
   1.442 +    *addr = 1;
   1.443 +    for ( i = 0 ; i < optPing ; i++ )
   1.444 +    { 
   1.445 +        rc = PR_WaitSemaphore( sem1 );
   1.446 +        PR_ASSERT( PR_FAILURE != rc );
   1.447 +
   1.448 +        (*addr)++;
   1.449 +        PR_ASSERT( (*addr % 2) == 1 );
   1.450 +        if ( optVerbose )
   1.451 +            PR_LOG( lm, msgLevel,
   1.452 +                 ( "nameshm1: Server pong: %d, i: %d", *addr, i));
   1.453 +
   1.454 +    
   1.455 +        rc = PR_PostSemaphore( sem2 );
   1.456 +        PR_ASSERT( PR_FAILURE != rc );
   1.457 +    }
   1.458 +
   1.459 +    rc = PR_WaitProcess( proc, &exit_status );
   1.460 +    PR_ASSERT( PR_FAILURE != rc );
   1.461 +
   1.462 +    rc = PR_CloseSemaphore( sem1 );
   1.463 +    PR_ASSERT( PR_FAILURE != rc );
   1.464 +
   1.465 +    rc = PR_CloseSemaphore( sem2 );
   1.466 +    PR_ASSERT( PR_FAILURE != rc );
   1.467 +
   1.468 +    rc = PR_DeleteSemaphore( SEM_NAME1 );
   1.469 +    PR_ASSERT( PR_FAILURE != rc );
   1.470 +
   1.471 +    rc = PR_DeleteSemaphore( SEM_NAME2 );
   1.472 +    PR_ASSERT( PR_FAILURE != rc );
   1.473 +
   1.474 +    rc = PR_DetachSharedMemory( shm, addr );
   1.475 +    if ( PR_FAILURE == rc )
   1.476 +    {
   1.477 +        PR_LOG( lm, msgLevel,
   1.478 +            ( "nameshm1: Server: Detach: Error: %ld. OSError: %ld", 
   1.479 +                PR_GetError(), PR_GetOSError()));
   1.480 +        failed_already = 1;
   1.481 +        return;
   1.482 +    }
   1.483 +    PR_LOG( lm, msgLevel,
   1.484 +             ( "nameshm1: Server: Detach: success: " ));
   1.485 +
   1.486 +    rc = PR_CloseSharedMemory( shm );
   1.487 +    if ( PR_FAILURE == rc )
   1.488 +    {
   1.489 +        PR_LOG( lm, msgLevel,
   1.490 +            ( "nameshm1: Server: Close: Error: %ld. OSError: %ld", 
   1.491 +                PR_GetError(), PR_GetOSError()));
   1.492 +        failed_already = 1;
   1.493 +        return;
   1.494 +    }
   1.495 +    PR_LOG( lm, msgLevel,
   1.496 +        ( "nameshm1: Server: Close: success: " ));
   1.497 +
   1.498 +    rc = PR_DeleteSharedMemory( optName );
   1.499 +    if ( PR_FAILURE == rc )
   1.500 +    {
   1.501 +        PR_LOG( lm, msgLevel,
   1.502 +            ( "nameshm1: Server: Destroy: Error: %ld. OSError: %ld", 
   1.503 +                PR_GetError(), PR_GetOSError()));
   1.504 +        failed_already = 1;
   1.505 +        return;
   1.506 +    }
   1.507 +    PR_LOG( lm, msgLevel,
   1.508 +        ( "nameshm1: Server: Destroy: success" ));
   1.509 +
   1.510 +    return;
   1.511 +} /* end ClientServerTest() */
   1.512 +
   1.513 +int main(int argc, char **argv)
   1.514 +{
   1.515 +    {
   1.516 +        /*
   1.517 +        ** Get command line options
   1.518 +        */
   1.519 +        PLOptStatus os;
   1.520 +        PLOptState *opt = PL_CreateOptState(argc, argv, "Cdvw:s:p:i:");
   1.521 +
   1.522 +	    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   1.523 +        {
   1.524 +		    if (PL_OPT_BAD == os) continue;
   1.525 +            switch (opt->option)
   1.526 +            {
   1.527 +            case 'v':  /* debug mode */
   1.528 +                optVerbose = 1;
   1.529 +                /* no break! fall into debug option */
   1.530 +            case 'd':  /* debug mode */
   1.531 +                debug = 1;
   1.532 +			    msgLevel = PR_LOG_DEBUG;
   1.533 +                break;
   1.534 +            case 'w':  /* try writing to memory mapped read-only */
   1.535 +                optWriteRO = 1;
   1.536 +                break;
   1.537 +            case 'C':
   1.538 +                optClient = 1;
   1.539 +                break;
   1.540 +            case 's':
   1.541 +                optSize = atol(opt->value) * 1024;
   1.542 +                break;
   1.543 +            case 'p':
   1.544 +                optPing = atol(opt->value);
   1.545 +                break;
   1.546 +            case 'i':
   1.547 +                optClientIterations = atol(opt->value);
   1.548 +                break;
   1.549 +            default:
   1.550 +                strcpy( optName, opt->value );
   1.551 +                break;
   1.552 +            }
   1.553 +        }
   1.554 +	    PL_DestroyOptState(opt);
   1.555 +    }
   1.556 +
   1.557 +    lm = PR_NewLogModule("Test");       /* Initialize logging */
   1.558 +    
   1.559 +    PR_LOG( lm, msgLevel,
   1.560 +             ( "nameshm1: Starting" ));
   1.561 +
   1.562 +    if ( optClient )
   1.563 +    {
   1.564 +        DoClient();
   1.565 +    } else {
   1.566 +        BasicTest();
   1.567 +        if ( failed_already != 0 )
   1.568 +            goto Finished;
   1.569 +        ReadOnlyTest();
   1.570 +        if ( failed_already != 0 )
   1.571 +            goto Finished;
   1.572 +        ClientServerTest();
   1.573 +    }
   1.574 +
   1.575 +Finished:
   1.576 +    if ( debug ) printf("%s\n", (failed_already)? "FAIL" : "PASS" );
   1.577 +    return( (failed_already)? 1 : 0 );
   1.578 +}  /* main() */
   1.579 +/* end instrumt.c */

mercurial