Back to index

lightning-sunbird  0.9+nobinonly
anonfm.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File: anonfm.c
00040 ** Description: Test anonymous file map 
00041 **
00042 ** Synopsis: anonfm [options] [dirName]
00043 **
00044 ** Options:
00045 ** -d   enable debug mode
00046 ** -h   display a help message
00047 ** -s <n>  size of the anonymous memory map, in KBytes. default: 100KBytes.
00048 ** -C 1 Operate this process as ClientOne() 
00049 ** -C 2 Operate this process as ClientTwo()
00050 **
00051 ** anonfn.c contains two tests, corresponding to the two protocols for
00052 ** passing an anonymous file map to a child process.
00053 **
00054 ** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
00055 ** PR_CreateProcess() [for portability of the test case] to create the
00056 ** child process, but does not use the PRProcessAttr structure for
00057 ** passing the file map data.
00058 **
00059 ** ServerTwo()/ClientTwo() tests the passing of the file map using the
00060 ** PRProcessAttr structure.
00061 **
00062 */
00063 #include <plgetopt.h> 
00064 #include <nspr.h> 
00065 #include <private/primpl.h>
00066 #include <stdio.h>
00067 #include <stdlib.h>
00068 #include <string.h>
00069 
00070 /*
00071 ** Test harness infrastructure
00072 */
00073 PRLogModuleInfo *lm;
00074 PRLogModuleLevel msgLevel = PR_LOG_NONE;
00075 PRUint32  failed_already = 0;
00076 
00077 PRIntn  debug = 0;
00078 PRIntn  client = 0; /* invoke client, style */
00079 char    dirName[512] = "."; /* directory name to contain anon mapped file */
00080 PRSize  fmSize = (100 * 1024 );
00081 PRUint32 fmMode = 0600;
00082 PRFileMapProtect fmProt = PR_PROT_READWRITE;
00083 const char *fmEnvName = "nsprFileMapEnvVariable";
00084 
00085 /*
00086 ** Emit help text for this test
00087 */
00088 static void Help( void )
00089 {
00090     printf("anonfm [options] [dirName]\n");
00091     printf("-d -- enable debug mode\n");
00092     printf("dirName is alternate directory name. Default: . (current directory)\n");
00093     exit(1);
00094 } /* end Help() */
00095 
00096 
00097 /*
00098 ** ClientOne() --
00099 */
00100 static void ClientOne( void )
00101 {
00102     PRFileMap   *fm;
00103     char        *fmString;
00104     char        *addr;
00105     PRStatus    rc;
00106 
00107     PR_LOG(lm, msgLevel,
00108         ("ClientOne() starting"));
00109     
00110     fmString = PR_GetEnv( fmEnvName );
00111     if ( NULL == fmString ) {
00112         failed_already = 1;    
00113         PR_LOG(lm, msgLevel,
00114                 ("ClientOne(): PR_Getenv() failed"));
00115         return;
00116     }
00117     PR_LOG(lm, msgLevel,
00118         ("ClientOne(): PR_Getenv(): found: %s", fmString));
00119 
00120     fm = PR_ImportFileMapFromString( fmString );
00121     if ( NULL == fm ) {
00122         failed_already = 1;    
00123         PR_LOG(lm, msgLevel,
00124                 ("ClientOne(): PR_ImportFileMapFromString() failed"));
00125         return;
00126     }
00127     PR_LOG(lm, msgLevel,
00128         ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
00129 
00130     addr = PR_MemMap( fm, LL_ZERO, fmSize );
00131     if ( NULL == addr ) {
00132         failed_already = 1;    
00133         PR_LOG(lm, msgLevel,
00134             ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
00135         return;
00136     }
00137     PR_LOG(lm, msgLevel,
00138         ("ClientOne(): PR_MemMap(): addr: %p", addr ));
00139 
00140     /* write to memory map to release server */
00141     *addr = 1;
00142 
00143     rc = PR_MemUnmap( addr, fmSize );
00144     PR_ASSERT( rc == PR_SUCCESS );
00145     PR_LOG(lm, msgLevel,
00146         ("ClientOne(): PR_MemUnap(): success" ));
00147 
00148     rc = PR_CloseFileMap( fm );
00149     if ( PR_FAILURE == rc ) {
00150         failed_already = 1;    
00151         PR_LOG(lm, msgLevel,
00152             ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
00153         return;
00154     }
00155     PR_LOG(lm, msgLevel,
00156         ("ClientOne(): PR_CloseFileMap(): success" ));
00157 
00158     return;
00159 } /* end ClientOne() */
00160 
00161 /*
00162 ** ClientTwo() --
00163 */
00164 static void ClientTwo( void )
00165 {
00166     failed_already = 1;
00167 } /* end ClientTwo() */
00168 
00169 /*
00170 ** ServerOne() --
00171 */
00172 static void ServerOne( void )
00173 {
00174     PRFileMap   *fm;
00175     PRStatus    rc;
00176     PRIntn      i;
00177     char        *addr;
00178     char        fmString[256];
00179     char        envBuf[256];
00180     char        *child_argv[8];
00181     PRProcess   *proc;
00182     PRInt32     exit_status;
00183 
00184     PR_LOG(lm, msgLevel,
00185         ("ServerOne() starting"));
00186     
00187     fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
00188     if ( NULL == fm )      {
00189         failed_already = 1;    
00190         PR_LOG(lm, msgLevel,
00191                 ("PR_OpenAnonFileMap() failed"));
00192         return;
00193     }
00194     PR_LOG(lm, msgLevel,
00195         ("ServerOne(): FileMap: %p", fm ));
00196     
00197     rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
00198     if ( PR_FAILURE == rc )  {
00199         failed_already = 1;    
00200         PR_LOG(lm, msgLevel,
00201             ("PR_ExportFileMap() failed"));
00202         return;
00203     }
00204 
00205     /*
00206     ** put the string into the environment
00207     */
00208     PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
00209     putenv( envBuf );
00210     
00211     addr = PR_MemMap( fm, LL_ZERO, fmSize );
00212     if ( NULL == addr ) {
00213         failed_already = 1;    
00214         PR_LOG(lm, msgLevel,
00215             ("PR_MemMap() failed"));
00216         return;
00217     }
00218 
00219     /* set initial value for client */
00220     for (i = 0; i < (PRIntn)fmSize ; i++ )
00221         *(addr+i) = 0x00;  
00222 
00223     PR_LOG(lm, msgLevel,
00224         ("ServerOne(): PR_MemMap(): addr: %p", addr ));
00225     
00226     /*
00227     ** set arguments for child process
00228     */
00229     child_argv[0] = "anonfm";
00230     child_argv[1] = "-C";
00231     child_argv[2] = "1";
00232     child_argv[3] = NULL;
00233 
00234     proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
00235     PR_ASSERT( proc );
00236     PR_LOG(lm, msgLevel,
00237         ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
00238 
00239     /*
00240     ** ClientOne() will set the memory to 1
00241     */
00242     PR_LOG(lm, msgLevel,
00243         ("ServerOne(): waiting on Client, *addr: %x", *addr ));
00244     while( *addr == 0x00 ) {
00245         if ( debug )
00246             fprintf(stderr, ".");
00247         PR_Sleep(PR_MillisecondsToInterval(300));
00248     }
00249     if ( debug )
00250         fprintf(stderr, "\n");
00251     PR_LOG(lm, msgLevel,
00252         ("ServerOne(): Client responded" ));
00253 
00254     rc = PR_WaitProcess( proc, &exit_status );
00255     PR_ASSERT( PR_FAILURE != rc );
00256 
00257     rc = PR_MemUnmap( addr, fmSize);
00258     if ( PR_FAILURE == rc ) {
00259         failed_already = 1;    
00260         PR_LOG(lm, msgLevel,
00261             ("PR_MemUnmap() failed"));
00262         return;
00263     }
00264     PR_LOG(lm, msgLevel,
00265         ("ServerOne(): PR_MemUnmap(): success" ));
00266 
00267     rc = PR_CloseFileMap(fm);
00268     if ( PR_FAILURE == rc ) {
00269         failed_already = 1;    
00270         PR_LOG(lm, msgLevel,
00271             ("PR_CloseFileMap() failed"));
00272         return;
00273     }
00274     PR_LOG(lm, msgLevel,
00275         ("ServerOne(): PR_CloseFileMap() success" ));
00276 
00277     return;
00278 } /* end ServerOne() */
00279 
00280 /*
00281 ** ServerTwo() --
00282 */
00283 static void ServerTwo( void )
00284 {
00285     PR_LOG(lm, msgLevel,
00286         ("ServerTwo(): Not implemented yet" ));
00287 } /* end ServerTwo() */
00288 
00289 
00290 PRIntn main(PRIntn argc, char *argv[])
00291 {
00292     {
00293         /*
00294         ** Get command line options
00295         */
00296         PLOptStatus os;
00297         PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
00298 
00299            while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00300         {
00301                   if (PL_OPT_BAD == os) continue;
00302             switch (opt->option)
00303             {
00304             case 'C':  /* Client style */
00305                 client = atol(opt->value);
00306                 break;
00307             case 's':  /* file size */
00308                 fmSize = atol( opt->value ) * 1024;
00309                 break;
00310             case 'd':  /* debug */
00311                 debug = 1;
00312                          msgLevel = PR_LOG_DEBUG;
00313                 break;
00314             case 'h':  /* help message */
00315                          Help();
00316                 break;
00317              default:
00318                 strcpy(dirName, opt->value);
00319                 break;
00320             }
00321         }
00322            PL_DestroyOptState(opt);
00323     }
00324 
00325     lm = PR_NewLogModule("Test");       /* Initialize logging */
00326 
00327     if ( client == 1 ) {
00328         ClientOne();
00329     } else if ( client == 2 )  {
00330         ClientTwo();
00331     } else {
00332         ServerOne();
00333         if ( failed_already ) goto Finished;
00334         ServerTwo();
00335     }
00336 
00337 Finished:
00338     if ( debug )
00339         printf("%s\n", (failed_already)? "FAIL" : "PASS");
00340     return( (failed_already == PR_TRUE )? 1 : 0 );
00341 }  /* main() */
00342 /* end anonfm.c */
00343