Back to index

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

Go to the source code of this file.

Functions

static void Help (void)
static void ClientOne (void)
static void ClientTwo (void)
static void ServerOne (void)
static void ServerTwo (void)
PRIntn main (PRIntn argc, char *argv[])

Variables

PRLogModuleInfolm
PRLogModuleLevel msgLevel = PR_LOG_NONE
PRUint32 failed_already = 0
PRIntn debug = 0
PRIntn client = 0
char dirName [512] = "."
PRSize fmSize = (100 * 1024 )
PRUint32 fmMode = 0600
PRFileMapProtect fmProt = PR_PROT_READWRITE
const char * fmEnvName = "nsprFileMapEnvVariable"

Function Documentation

static void ClientOne ( void  ) [static]

Definition at line 100 of file anonfm.c.

{
    PRFileMap   *fm;
    char        *fmString;
    char        *addr;
    PRStatus    rc;

    PR_LOG(lm, msgLevel,
        ("ClientOne() starting"));
    
    fmString = PR_GetEnv( fmEnvName );
    if ( NULL == fmString ) {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
                ("ClientOne(): PR_Getenv() failed"));
        return;
    }
    PR_LOG(lm, msgLevel,
        ("ClientOne(): PR_Getenv(): found: %s", fmString));

    fm = PR_ImportFileMapFromString( fmString );
    if ( NULL == fm ) {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
                ("ClientOne(): PR_ImportFileMapFromString() failed"));
        return;
    }
    PR_LOG(lm, msgLevel,
        ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));

    addr = PR_MemMap( fm, LL_ZERO, fmSize );
    if ( NULL == addr ) {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
            ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
        return;
    }
    PR_LOG(lm, msgLevel,
        ("ClientOne(): PR_MemMap(): addr: %p", addr ));

    /* write to memory map to release server */
    *addr = 1;

    rc = PR_MemUnmap( addr, fmSize );
    PR_ASSERT( rc == PR_SUCCESS );
    PR_LOG(lm, msgLevel,
        ("ClientOne(): PR_MemUnap(): success" ));

    rc = PR_CloseFileMap( fm );
    if ( PR_FAILURE == rc ) {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
            ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
        return;
    }
    PR_LOG(lm, msgLevel,
        ("ClientOne(): PR_CloseFileMap(): success" ));

    return;
} /* end ClientOne() */

Here is the call graph for this function:

Here is the caller graph for this function:

static void ClientTwo ( void  ) [static]

Definition at line 164 of file anonfm.c.

{
    failed_already = 1;
} /* end ClientTwo() */

Here is the caller graph for this function:

static void Help ( void  ) [static]

Definition at line 88 of file anonfm.c.

{
    printf("anonfm [options] [dirName]\n");
    printf("-d -- enable debug mode\n");
    printf("dirName is alternate directory name. Default: . (current directory)\n");
    exit(1);
} /* end Help() */

Here is the call graph for this function:

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

Definition at line 290 of file anonfm.c.

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

           while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
        {
                  if (PL_OPT_BAD == os) continue;
            switch (opt->option)
            {
            case 'C':  /* Client style */
                client = atol(opt->value);
                break;
            case 's':  /* file size */
                fmSize = atol( opt->value ) * 1024;
                break;
            case 'd':  /* debug */
                debug = 1;
                         msgLevel = PR_LOG_DEBUG;
                break;
            case 'h':  /* help message */
                         Help();
                break;
             default:
                strcpy(dirName, opt->value);
                break;
            }
        }
           PL_DestroyOptState(opt);
    }

    lm = PR_NewLogModule("Test");       /* Initialize logging */

    if ( client == 1 ) {
        ClientOne();
    } else if ( client == 2 )  {
        ClientTwo();
    } else {
        ServerOne();
        if ( failed_already ) goto Finished;
        ServerTwo();
    }

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

Here is the call graph for this function:

static void ServerOne ( void  ) [static]

Definition at line 172 of file anonfm.c.

{
    PRFileMap   *fm;
    PRStatus    rc;
    PRIntn      i;
    char        *addr;
    char        fmString[256];
    char        envBuf[256];
    char        *child_argv[8];
    PRProcess   *proc;
    PRInt32     exit_status;

    PR_LOG(lm, msgLevel,
        ("ServerOne() starting"));
    
    fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
    if ( NULL == fm )      {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
                ("PR_OpenAnonFileMap() failed"));
        return;
    }
    PR_LOG(lm, msgLevel,
        ("ServerOne(): FileMap: %p", fm ));
    
    rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
    if ( PR_FAILURE == rc )  {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
            ("PR_ExportFileMap() failed"));
        return;
    }

    /*
    ** put the string into the environment
    */
    PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
    putenv( envBuf );
    
    addr = PR_MemMap( fm, LL_ZERO, fmSize );
    if ( NULL == addr ) {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
            ("PR_MemMap() failed"));
        return;
    }

    /* set initial value for client */
    for (i = 0; i < (PRIntn)fmSize ; i++ )
        *(addr+i) = 0x00;  

    PR_LOG(lm, msgLevel,
        ("ServerOne(): PR_MemMap(): addr: %p", addr ));
    
    /*
    ** set arguments for child process
    */
    child_argv[0] = "anonfm";
    child_argv[1] = "-C";
    child_argv[2] = "1";
    child_argv[3] = NULL;

    proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
    PR_ASSERT( proc );
    PR_LOG(lm, msgLevel,
        ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));

    /*
    ** ClientOne() will set the memory to 1
    */
    PR_LOG(lm, msgLevel,
        ("ServerOne(): waiting on Client, *addr: %x", *addr ));
    while( *addr == 0x00 ) {
        if ( debug )
            fprintf(stderr, ".");
        PR_Sleep(PR_MillisecondsToInterval(300));
    }
    if ( debug )
        fprintf(stderr, "\n");
    PR_LOG(lm, msgLevel,
        ("ServerOne(): Client responded" ));

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

    rc = PR_MemUnmap( addr, fmSize);
    if ( PR_FAILURE == rc ) {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
            ("PR_MemUnmap() failed"));
        return;
    }
    PR_LOG(lm, msgLevel,
        ("ServerOne(): PR_MemUnmap(): success" ));

    rc = PR_CloseFileMap(fm);
    if ( PR_FAILURE == rc ) {
        failed_already = 1;    
        PR_LOG(lm, msgLevel,
            ("PR_CloseFileMap() failed"));
        return;
    }
    PR_LOG(lm, msgLevel,
        ("ServerOne(): PR_CloseFileMap() success" ));

    return;
} /* end ServerOne() */

Here is the call graph for this function:

Here is the caller graph for this function:

static void ServerTwo ( void  ) [static]

Definition at line 283 of file anonfm.c.

{
    PR_LOG(lm, msgLevel,
        ("ServerTwo(): Not implemented yet" ));
} /* end ServerTwo() */

Here is the caller graph for this function:


Variable Documentation

PRIntn client = 0

Definition at line 78 of file anonfm.c.

PRIntn debug = 0

Definition at line 77 of file anonfm.c.

char dirName[512] = "."

Definition at line 79 of file anonfm.c.

Definition at line 75 of file anonfm.c.

const char* fmEnvName = "nsprFileMapEnvVariable"

Definition at line 83 of file anonfm.c.

PRUint32 fmMode = 0600

Definition at line 81 of file anonfm.c.

Definition at line 82 of file anonfm.c.

PRSize fmSize = (100 * 1024 )

Definition at line 80 of file anonfm.c.

Definition at line 73 of file anonfm.c.

Definition at line 74 of file anonfm.c.