nsprpub/pr/tests/anonfm.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** File: anonfm.c
michael@0 8 ** Description: Test anonymous file map
michael@0 9 **
michael@0 10 ** Synopsis: anonfm [options] [dirName]
michael@0 11 **
michael@0 12 ** Options:
michael@0 13 ** -d enable debug mode
michael@0 14 ** -h display a help message
michael@0 15 ** -s <n> size of the anonymous memory map, in KBytes. default: 100KBytes.
michael@0 16 ** -C 1 Operate this process as ClientOne()
michael@0 17 ** -C 2 Operate this process as ClientTwo()
michael@0 18 **
michael@0 19 ** anonfn.c contains two tests, corresponding to the two protocols for
michael@0 20 ** passing an anonymous file map to a child process.
michael@0 21 **
michael@0 22 ** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
michael@0 23 ** PR_CreateProcess() [for portability of the test case] to create the
michael@0 24 ** child process, but does not use the PRProcessAttr structure for
michael@0 25 ** passing the file map data.
michael@0 26 **
michael@0 27 ** ServerTwo()/ClientTwo() tests the passing of the file map using the
michael@0 28 ** PRProcessAttr structure.
michael@0 29 **
michael@0 30 */
michael@0 31 #include <plgetopt.h>
michael@0 32 #include <nspr.h>
michael@0 33 #include <private/primpl.h>
michael@0 34 #include <stdio.h>
michael@0 35 #include <stdlib.h>
michael@0 36 #include <string.h>
michael@0 37
michael@0 38 /*
michael@0 39 ** Test harness infrastructure
michael@0 40 */
michael@0 41 PRLogModuleInfo *lm;
michael@0 42 PRLogModuleLevel msgLevel = PR_LOG_NONE;
michael@0 43 PRUint32 failed_already = 0;
michael@0 44
michael@0 45 PRIntn debug = 0;
michael@0 46 PRIntn client = 0; /* invoke client, style */
michael@0 47 char dirName[512] = "."; /* directory name to contain anon mapped file */
michael@0 48 PRSize fmSize = (100 * 1024 );
michael@0 49 PRUint32 fmMode = 0600;
michael@0 50 PRFileMapProtect fmProt = PR_PROT_READWRITE;
michael@0 51 const char *fmEnvName = "nsprFileMapEnvVariable";
michael@0 52
michael@0 53 /*
michael@0 54 ** Emit help text for this test
michael@0 55 */
michael@0 56 static void Help( void )
michael@0 57 {
michael@0 58 printf("anonfm [options] [dirName]\n");
michael@0 59 printf("-d -- enable debug mode\n");
michael@0 60 printf("dirName is alternate directory name. Default: . (current directory)\n");
michael@0 61 exit(1);
michael@0 62 } /* end Help() */
michael@0 63
michael@0 64
michael@0 65 /*
michael@0 66 ** ClientOne() --
michael@0 67 */
michael@0 68 static void ClientOne( void )
michael@0 69 {
michael@0 70 PRFileMap *fm;
michael@0 71 char *fmString;
michael@0 72 char *addr;
michael@0 73 PRStatus rc;
michael@0 74
michael@0 75 PR_LOG(lm, msgLevel,
michael@0 76 ("ClientOne() starting"));
michael@0 77
michael@0 78 fmString = PR_GetEnv( fmEnvName );
michael@0 79 if ( NULL == fmString ) {
michael@0 80 failed_already = 1;
michael@0 81 PR_LOG(lm, msgLevel,
michael@0 82 ("ClientOne(): PR_Getenv() failed"));
michael@0 83 return;
michael@0 84 }
michael@0 85 PR_LOG(lm, msgLevel,
michael@0 86 ("ClientOne(): PR_Getenv(): found: %s", fmString));
michael@0 87
michael@0 88 fm = PR_ImportFileMapFromString( fmString );
michael@0 89 if ( NULL == fm ) {
michael@0 90 failed_already = 1;
michael@0 91 PR_LOG(lm, msgLevel,
michael@0 92 ("ClientOne(): PR_ImportFileMapFromString() failed"));
michael@0 93 return;
michael@0 94 }
michael@0 95 PR_LOG(lm, msgLevel,
michael@0 96 ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
michael@0 97
michael@0 98 addr = PR_MemMap( fm, LL_ZERO, fmSize );
michael@0 99 if ( NULL == addr ) {
michael@0 100 failed_already = 1;
michael@0 101 PR_LOG(lm, msgLevel,
michael@0 102 ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
michael@0 103 return;
michael@0 104 }
michael@0 105 PR_LOG(lm, msgLevel,
michael@0 106 ("ClientOne(): PR_MemMap(): addr: %p", addr ));
michael@0 107
michael@0 108 /* write to memory map to release server */
michael@0 109 *addr = 1;
michael@0 110
michael@0 111 rc = PR_MemUnmap( addr, fmSize );
michael@0 112 PR_ASSERT( rc == PR_SUCCESS );
michael@0 113 PR_LOG(lm, msgLevel,
michael@0 114 ("ClientOne(): PR_MemUnap(): success" ));
michael@0 115
michael@0 116 rc = PR_CloseFileMap( fm );
michael@0 117 if ( PR_FAILURE == rc ) {
michael@0 118 failed_already = 1;
michael@0 119 PR_LOG(lm, msgLevel,
michael@0 120 ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
michael@0 121 return;
michael@0 122 }
michael@0 123 PR_LOG(lm, msgLevel,
michael@0 124 ("ClientOne(): PR_CloseFileMap(): success" ));
michael@0 125
michael@0 126 return;
michael@0 127 } /* end ClientOne() */
michael@0 128
michael@0 129 /*
michael@0 130 ** ClientTwo() --
michael@0 131 */
michael@0 132 static void ClientTwo( void )
michael@0 133 {
michael@0 134 failed_already = 1;
michael@0 135 } /* end ClientTwo() */
michael@0 136
michael@0 137 /*
michael@0 138 ** ServerOne() --
michael@0 139 */
michael@0 140 static void ServerOne( void )
michael@0 141 {
michael@0 142 PRFileMap *fm;
michael@0 143 PRStatus rc;
michael@0 144 PRIntn i;
michael@0 145 char *addr;
michael@0 146 char fmString[256];
michael@0 147 char envBuf[256];
michael@0 148 char *child_argv[8];
michael@0 149 PRProcess *proc;
michael@0 150 PRInt32 exit_status;
michael@0 151
michael@0 152 PR_LOG(lm, msgLevel,
michael@0 153 ("ServerOne() starting"));
michael@0 154
michael@0 155 fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
michael@0 156 if ( NULL == fm ) {
michael@0 157 failed_already = 1;
michael@0 158 PR_LOG(lm, msgLevel,
michael@0 159 ("PR_OpenAnonFileMap() failed"));
michael@0 160 return;
michael@0 161 }
michael@0 162 PR_LOG(lm, msgLevel,
michael@0 163 ("ServerOne(): FileMap: %p", fm ));
michael@0 164
michael@0 165 rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
michael@0 166 if ( PR_FAILURE == rc ) {
michael@0 167 failed_already = 1;
michael@0 168 PR_LOG(lm, msgLevel,
michael@0 169 ("PR_ExportFileMap() failed"));
michael@0 170 return;
michael@0 171 }
michael@0 172
michael@0 173 /*
michael@0 174 ** put the string into the environment
michael@0 175 */
michael@0 176 PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
michael@0 177 putenv( envBuf );
michael@0 178
michael@0 179 addr = PR_MemMap( fm, LL_ZERO, fmSize );
michael@0 180 if ( NULL == addr ) {
michael@0 181 failed_already = 1;
michael@0 182 PR_LOG(lm, msgLevel,
michael@0 183 ("PR_MemMap() failed"));
michael@0 184 return;
michael@0 185 }
michael@0 186
michael@0 187 /* set initial value for client */
michael@0 188 for (i = 0; i < (PRIntn)fmSize ; i++ )
michael@0 189 *(addr+i) = 0x00;
michael@0 190
michael@0 191 PR_LOG(lm, msgLevel,
michael@0 192 ("ServerOne(): PR_MemMap(): addr: %p", addr ));
michael@0 193
michael@0 194 /*
michael@0 195 ** set arguments for child process
michael@0 196 */
michael@0 197 child_argv[0] = "anonfm";
michael@0 198 child_argv[1] = "-C";
michael@0 199 child_argv[2] = "1";
michael@0 200 child_argv[3] = NULL;
michael@0 201
michael@0 202 proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
michael@0 203 PR_ASSERT( proc );
michael@0 204 PR_LOG(lm, msgLevel,
michael@0 205 ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
michael@0 206
michael@0 207 /*
michael@0 208 ** ClientOne() will set the memory to 1
michael@0 209 */
michael@0 210 PR_LOG(lm, msgLevel,
michael@0 211 ("ServerOne(): waiting on Client, *addr: %x", *addr ));
michael@0 212 while( *addr == 0x00 ) {
michael@0 213 if ( debug )
michael@0 214 fprintf(stderr, ".");
michael@0 215 PR_Sleep(PR_MillisecondsToInterval(300));
michael@0 216 }
michael@0 217 if ( debug )
michael@0 218 fprintf(stderr, "\n");
michael@0 219 PR_LOG(lm, msgLevel,
michael@0 220 ("ServerOne(): Client responded" ));
michael@0 221
michael@0 222 rc = PR_WaitProcess( proc, &exit_status );
michael@0 223 PR_ASSERT( PR_FAILURE != rc );
michael@0 224
michael@0 225 rc = PR_MemUnmap( addr, fmSize);
michael@0 226 if ( PR_FAILURE == rc ) {
michael@0 227 failed_already = 1;
michael@0 228 PR_LOG(lm, msgLevel,
michael@0 229 ("PR_MemUnmap() failed"));
michael@0 230 return;
michael@0 231 }
michael@0 232 PR_LOG(lm, msgLevel,
michael@0 233 ("ServerOne(): PR_MemUnmap(): success" ));
michael@0 234
michael@0 235 rc = PR_CloseFileMap(fm);
michael@0 236 if ( PR_FAILURE == rc ) {
michael@0 237 failed_already = 1;
michael@0 238 PR_LOG(lm, msgLevel,
michael@0 239 ("PR_CloseFileMap() failed"));
michael@0 240 return;
michael@0 241 }
michael@0 242 PR_LOG(lm, msgLevel,
michael@0 243 ("ServerOne(): PR_CloseFileMap() success" ));
michael@0 244
michael@0 245 return;
michael@0 246 } /* end ServerOne() */
michael@0 247
michael@0 248 /*
michael@0 249 ** ServerTwo() --
michael@0 250 */
michael@0 251 static void ServerTwo( void )
michael@0 252 {
michael@0 253 PR_LOG(lm, msgLevel,
michael@0 254 ("ServerTwo(): Not implemented yet" ));
michael@0 255 } /* end ServerTwo() */
michael@0 256
michael@0 257
michael@0 258 int main(int argc, char **argv)
michael@0 259 {
michael@0 260 {
michael@0 261 /*
michael@0 262 ** Get command line options
michael@0 263 */
michael@0 264 PLOptStatus os;
michael@0 265 PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
michael@0 266
michael@0 267 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 268 {
michael@0 269 if (PL_OPT_BAD == os) continue;
michael@0 270 switch (opt->option)
michael@0 271 {
michael@0 272 case 'C': /* Client style */
michael@0 273 client = atol(opt->value);
michael@0 274 break;
michael@0 275 case 's': /* file size */
michael@0 276 fmSize = atol( opt->value ) * 1024;
michael@0 277 break;
michael@0 278 case 'd': /* debug */
michael@0 279 debug = 1;
michael@0 280 msgLevel = PR_LOG_DEBUG;
michael@0 281 break;
michael@0 282 case 'h': /* help message */
michael@0 283 Help();
michael@0 284 break;
michael@0 285 default:
michael@0 286 strcpy(dirName, opt->value);
michael@0 287 break;
michael@0 288 }
michael@0 289 }
michael@0 290 PL_DestroyOptState(opt);
michael@0 291 }
michael@0 292
michael@0 293 lm = PR_NewLogModule("Test"); /* Initialize logging */
michael@0 294
michael@0 295 if ( client == 1 ) {
michael@0 296 ClientOne();
michael@0 297 } else if ( client == 2 ) {
michael@0 298 ClientTwo();
michael@0 299 } else {
michael@0 300 ServerOne();
michael@0 301 if ( failed_already ) goto Finished;
michael@0 302 ServerTwo();
michael@0 303 }
michael@0 304
michael@0 305 Finished:
michael@0 306 if ( debug )
michael@0 307 printf("%s\n", (failed_already)? "FAIL" : "PASS");
michael@0 308 return( (failed_already == PR_TRUE )? 1 : 0 );
michael@0 309 } /* main() */
michael@0 310 /* end anonfm.c */
michael@0 311

mercurial