Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
nameshm1.c File Reference
#include <plgetopt.h>
#include <nspr.h>
#include <stdlib.h>
#include <string.h>
#include <private/primpl.h>

Go to the source code of this file.

Defines

#define SEM_NAME1   "/tmp/nameshmSEM1"
#define SEM_NAME2   "/tmp/nameshmSEM2"
#define SEM_MODE   0666
#define SHM_MODE   0666
#define NameSize   (1024)

Functions

static void BasicTest (void)
static void ReadOnlyTest (void)
static void DoClient (void)
static void ClientServerTest (void)
PRIntn main (PRIntn argc, char *argv[])

Variables

PRIntn debug = 0
PRIntn failed_already = 0
PRLogModuleLevel msgLevel = PR_LOG_NONE
PRLogModuleInfolm
PRIntn optDebug = 0
PRIntn optVerbose = 0
PRUint32 optWriteRO = 0
PRUint32 optClient = 0
PRUint32 optCreate = 1
PRUint32 optAttachRW = 1
PRUint32 optAttachRO = 1
PRUint32 optClose = 1
PRUint32 optDelete = 1
PRInt32 optPing = 1000
PRUint32 optSize = (10 * 1024 )
PRInt32 optClientIterations = 3
char optName [NameSize] = "/tmp/xxxNSPRshm"
char buf [1024] = ""

Define Documentation

#define NameSize   (1024)

Definition at line 112 of file nameshm1.c.

#define SEM_MODE   0666

Definition at line 109 of file nameshm1.c.

#define SEM_NAME1   "/tmp/nameshmSEM1"

Definition at line 107 of file nameshm1.c.

#define SEM_NAME2   "/tmp/nameshmSEM2"

Definition at line 108 of file nameshm1.c.

#define SHM_MODE   0666

Definition at line 110 of file nameshm1.c.


Function Documentation

static void BasicTest ( void  ) [static]

Definition at line 137 of file nameshm1.c.

{
    PRSharedMemory  *shm;
    char *addr; /* address of shared memory segment */
    PRUint32  i;
    PRInt32 rc;

    PR_LOG( lm, msgLevel,
             ( "nameshm1: Begin BasicTest" ));

    if ( PR_FAILURE == PR_DeleteSharedMemory( optName )) {
        PR_LOG( lm, msgLevel,
            ("nameshm1: Initial PR_DeleteSharedMemory() failed. No problem"));
    } else
        PR_LOG( lm, msgLevel,
            ("nameshm1: Initial PR_DeleteSharedMemory() success"));


    shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE );
    if ( NULL == shm )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RW Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RW Create: success: %p", shm ));

    addr = PR_AttachSharedMemory( shm , 0 );
    if ( NULL == addr ) 
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RW Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RW Attach: success: %p", addr ));

    /* fill memory with i */
    for ( i = 0; i < optSize ;  i++ )
    {
         *(addr + i) = i;
    }

    rc = PR_DetachSharedMemory( shm, addr );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RW Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RW Detach: success: " ));

    rc = PR_CloseSharedMemory( shm );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RW Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RW Close: success: " ));

    rc = PR_DeleteSharedMemory( optName );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RW Delete: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RW Delete: success: " ));

    PR_LOG( lm, msgLevel,
            ("nameshm1: BasicTest(): Passed"));

    return;
} /* end BasicTest() */

Here is the call graph for this function:

Here is the caller graph for this function:

static void ClientServerTest ( void  ) [static]

Definition at line 394 of file nameshm1.c.

{
    PRStatus rc;
    PRSem *sem1, *sem2;
    PRProcess *proc;
    PRInt32 exit_status;
    PRSharedMemory  *shm;
    PRUint32 *addr; 
    PRInt32 i;
    char *child_argv[8];
    char buf[24];

    PR_LOG( lm, msgLevel,
             ( "nameshm1: Begin ClientServerTest" ));

    rc = PR_DeleteSharedMemory( optName );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: Server: Destroy: failed. No problem"));
    } else
        PR_LOG( lm, msgLevel,
            ( "nameshm1: Server: Destroy: success" ));


    shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE);
    if ( NULL == shm )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: Server: Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: Server: Create: success: %p", shm ));

    addr = PR_AttachSharedMemory( shm , 0 );
    if ( NULL == addr ) 
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: Server: Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: Server: Attach: success: %p", addr ));

    sem1 = PR_OpenSemaphore( SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0 );
    PR_ASSERT( sem1 );

    sem2 = PR_OpenSemaphore( SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 1 );
    PR_ASSERT( sem1 );

    strcpy( (char*)addr, "FooBar" );

    child_argv[0] = "nameshm1";
    child_argv[1] = "-C";
    child_argv[2] = "-p";
    sprintf( buf, "%d", optPing );
    child_argv[3] = buf;
    child_argv[4] = optName;
    child_argv[5] = NULL;

    proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
    PR_ASSERT( proc );

    PR_Sleep( PR_SecondsToInterval(4));

    *addr = 1;
    for ( i = 0 ; i < optPing ; i++ )
    { 
        rc = PR_WaitSemaphore( sem1 );
        PR_ASSERT( PR_FAILURE != rc );

        (*addr)++;
        PR_ASSERT( (*addr % 2) == 1 );
        if ( optVerbose )
            PR_LOG( lm, msgLevel,
                 ( "nameshm1: Server pong: %d, i: %d", *addr, i));

    
        rc = PR_PostSemaphore( sem2 );
        PR_ASSERT( PR_FAILURE != rc );
    }

    rc = PR_WaitProcess( proc, &exit_status );
    PR_ASSERT( PR_FAILURE != rc );

    rc = PR_CloseSemaphore( sem1 );
    PR_ASSERT( PR_FAILURE != rc );

    rc = PR_CloseSemaphore( sem2 );
    PR_ASSERT( PR_FAILURE != rc );

    rc = PR_DeleteSemaphore( SEM_NAME1 );
    PR_ASSERT( PR_FAILURE != rc );

    rc = PR_DeleteSemaphore( SEM_NAME2 );
    PR_ASSERT( PR_FAILURE != rc );

    rc = PR_DetachSharedMemory( shm, addr );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: Server: Detach: Error: %ld. OSError: %ld", 
                PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: Server: Detach: success: " ));

    rc = PR_CloseSharedMemory( shm );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: Server: Close: Error: %ld. OSError: %ld", 
                PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
        ( "nameshm1: Server: Close: success: " ));

    rc = PR_DeleteSharedMemory( optName );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: Server: Destroy: Error: %ld. OSError: %ld", 
                PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
        ( "nameshm1: Server: Destroy: success" ));

    return;
} /* end ClientServerTest() */

Here is the call graph for this function:

Here is the caller graph for this function:

static void DoClient ( void  ) [static]

Definition at line 301 of file nameshm1.c.

{
    PRStatus rc;
    PRSem *sem1, *sem2;
    PRSharedMemory  *shm;
    PRUint32 *addr; 
    PRInt32 i;

    PR_LOG( lm, msgLevel,
            ("nameshm1: DoClient(): Starting"));

    sem1 = PR_OpenSemaphore( SEM_NAME1, 0, 0, 0 );
    PR_ASSERT( sem1 );

    sem2 = PR_OpenSemaphore( SEM_NAME2, 0, 0, 0 );
    PR_ASSERT( sem1 );

    shm = PR_OpenSharedMemory( optName, optSize, 0, SHM_MODE );
    if ( NULL == shm )
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: DoClient(): Create: Error: %ld. OSError: %ld", 
                PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: DoClient(): Create: success: %p", shm ));

    addr = PR_AttachSharedMemory( shm , 0 );
    if ( NULL == addr ) 
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: DoClient(): Attach: Error: %ld. OSError: %ld", 
                PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: DoClient(): Attach: success: %p", addr ));

    PR_LOG( lm, msgLevel,
        ( "Client found: %s", addr));

    PR_Sleep(PR_SecondsToInterval(4));
    for ( i = 0 ; i < optPing ; i++ )
    {
        rc = PR_WaitSemaphore( sem2 );
        PR_ASSERT( PR_FAILURE != rc );
        
        (*addr)++;
        PR_ASSERT( (*addr % 2) == 0 );        
        if ( optVerbose )
            PR_LOG( lm, msgLevel,
                 ( "nameshm1: Client ping: %d, i: %d", *addr, i));

        rc = PR_PostSemaphore( sem1 );
        PR_ASSERT( PR_FAILURE != rc );
    }

    rc = PR_CloseSemaphore( sem1 );
    PR_ASSERT( PR_FAILURE != rc );

    rc = PR_CloseSemaphore( sem2 );
    PR_ASSERT( PR_FAILURE != rc );

    rc = PR_DetachSharedMemory( shm, addr );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: DoClient(): Detach: Error: %ld. OSError: %ld", 
                PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: DoClient(): Detach: success: " ));

    rc = PR_CloseSharedMemory( shm );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
            ( "nameshm1: DoClient(): Close: Error: %ld. OSError: %ld", 
                PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: DoClient(): Close: success: " ));

    return;
}    /* end DoClient() */

Here is the call graph for this function:

Here is the caller graph for this function:

PRIntn main ( PRIntn  argc,
char *  argv[] 
)

Definition at line 533 of file nameshm1.c.

{
    {
        /*
        ** Get command line options
        */
        PLOptStatus os;
        PLOptState *opt = PL_CreateOptState(argc, argv, "Cdvw:s:p:i:");

           while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
        {
                  if (PL_OPT_BAD == os) continue;
            switch (opt->option)
            {
            case 'v':  /* debug mode */
                optVerbose = 1;
                /* no break! fall into debug option */
            case 'd':  /* debug mode */
                debug = 1;
                         msgLevel = PR_LOG_DEBUG;
                break;
            case 'w':  /* try writing to memory mapped read-only */
                optWriteRO = 1;
                break;
            case 'C':
                optClient = 1;
                break;
            case 's':
                optSize = atol(opt->value) * 1024;
                break;
            case 'p':
                optPing = atol(opt->value);
                break;
            case 'i':
                optClientIterations = atol(opt->value);
                break;
            default:
                strcpy( optName, opt->value );
                break;
            }
        }
           PL_DestroyOptState(opt);
    }

    lm = PR_NewLogModule("Test");       /* Initialize logging */
    
    PR_LOG( lm, msgLevel,
             ( "nameshm1: Starting" ));

    if ( optClient )
    {
        DoClient();
    } else {
        BasicTest();
        if ( failed_already != 0 )
            goto Finished;
        ReadOnlyTest();
        if ( failed_already != 0 )
            goto Finished;
        ClientServerTest();
    }

Finished:
    if ( debug ) printf("%s\n", (failed_already)? "FAIL" : "PASS" );
    return( (failed_already)? 1 : 0 );
}  /* main() */

Here is the call graph for this function:

static void ReadOnlyTest ( void  ) [static]

Definition at line 222 of file nameshm1.c.

{
    PRSharedMemory  *shm;
    char *roAddr; /* read-only address of shared memory segment */
    PRInt32 rc;

    PR_LOG( lm, msgLevel,
             ( "nameshm1: Begin ReadOnlyTest" ));

    shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE);
    if ( NULL == shm )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RO Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RO Create: success: %p", shm ));


    roAddr = PR_AttachSharedMemory( shm , PR_SHM_READONLY );
    if ( NULL == roAddr ) 
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RO Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RO Attach: success: %p", roAddr ));

    if ( optWriteRO )
    {
        *roAddr = 0x00; /* write to read-only memory */
        failed_already = 1;
        PR_LOG( lm, msgLevel, ("nameshm1: Wrote to read-only memory segment!"));
        return;
    }

    rc = PR_DetachSharedMemory( shm, roAddr );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RO Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RO Detach: success: " ));

    rc = PR_CloseSharedMemory( shm );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RO Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RO Close: success: " ));

    rc = PR_DeleteSharedMemory( optName );
    if ( PR_FAILURE == rc )
    {
        PR_LOG( lm, msgLevel,
                 ( "nameshm1: RO Destroy: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError()));
        failed_already = 1;
        return;
    }
    PR_LOG( lm, msgLevel,
             ( "nameshm1: RO Destroy: success: " ));

    PR_LOG( lm, msgLevel,
        ("nameshm1: ReadOnlyTest(): Passed"));

    return;
} /* end ReadOnlyTest() */

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char buf[1024] = ""

Definition at line 134 of file nameshm1.c.

PRIntn debug = 0

Definition at line 114 of file nameshm1.c.

PRIntn failed_already = 0

Definition at line 115 of file nameshm1.c.

Definition at line 117 of file nameshm1.c.

Definition at line 116 of file nameshm1.c.

Definition at line 126 of file nameshm1.c.

Definition at line 125 of file nameshm1.c.

Definition at line 123 of file nameshm1.c.

Definition at line 131 of file nameshm1.c.

Definition at line 127 of file nameshm1.c.

Definition at line 124 of file nameshm1.c.

PRIntn optDebug = 0

Definition at line 120 of file nameshm1.c.

Definition at line 128 of file nameshm1.c.

char optName[NameSize] = "/tmp/xxxNSPRshm"

Definition at line 132 of file nameshm1.c.

PRInt32 optPing = 1000

Definition at line 129 of file nameshm1.c.

PRUint32 optSize = (10 * 1024 )

Definition at line 130 of file nameshm1.c.

PRIntn optVerbose = 0

Definition at line 121 of file nameshm1.c.

Definition at line 122 of file nameshm1.c.