Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
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: nameshm1.c -- Test Named Shared Memory |
michael@0 | 8 | ** |
michael@0 | 9 | ** Description: |
michael@0 | 10 | ** nameshm1 tests Named Shared Memory. nameshm1 performs two tests of |
michael@0 | 11 | ** named shared memory. |
michael@0 | 12 | ** |
michael@0 | 13 | ** The first test is a basic test. The basic test operates as a single |
michael@0 | 14 | ** process. The process exercises all the API elements of the facility. |
michael@0 | 15 | ** This test also attempts to write to all locations in the shared |
michael@0 | 16 | ** memory. |
michael@0 | 17 | ** |
michael@0 | 18 | ** The second test is a client-server test. The client-server test |
michael@0 | 19 | ** creates a new instance of nameshm1, passing the -C argument to the |
michael@0 | 20 | ** new process; this creates the client-side process. The server-side |
michael@0 | 21 | ** (the instance of nameshm1 created from the command line) and the |
michael@0 | 22 | ** client-side interact via inter-process semaphores to verify that the |
michael@0 | 23 | ** shared memory segment can be read and written by both sides in a |
michael@0 | 24 | ** synchronized maner. |
michael@0 | 25 | ** |
michael@0 | 26 | ** Note: Because this test runs in two processes, the log files created |
michael@0 | 27 | ** by the test are not in chronological sequence; makes it hard to read. |
michael@0 | 28 | ** As a temporary circumvention, I changed the definition(s) of the |
michael@0 | 29 | ** _PUT_LOG() macro in prlog.c to force a flushall(), or equivalent. |
michael@0 | 30 | ** This causes the log entries to be emitted in true chronological |
michael@0 | 31 | ** order. |
michael@0 | 32 | ** |
michael@0 | 33 | ** Synopsis: nameshm1 [options] [name] |
michael@0 | 34 | ** |
michael@0 | 35 | ** Options: |
michael@0 | 36 | ** -d Enables debug trace via PR_LOG() |
michael@0 | 37 | ** -v Enables verbose mode debug trace via PR_LOG() |
michael@0 | 38 | ** -w Causes the basic test to attempt to write to the segment |
michael@0 | 39 | ** mapped as read-only. When this option is specified, the |
michael@0 | 40 | ** test should crash with a seg-fault; this is a destructive |
michael@0 | 41 | ** test and is considered successful when it seg-faults. |
michael@0 | 42 | ** |
michael@0 | 43 | ** -C Causes nameshm1 to start as the client-side of a |
michael@0 | 44 | ** client-server pair of processes. Only the instance |
michael@0 | 45 | ** of nameshm1 operating as the server-side process should |
michael@0 | 46 | ** specify the -C option when creating the client-side process; |
michael@0 | 47 | ** the -C option should not be specified at the command line. |
michael@0 | 48 | ** The client-side uses the shared memory segment created by |
michael@0 | 49 | ** the server-side to communicate with the server-side |
michael@0 | 50 | ** process. |
michael@0 | 51 | ** |
michael@0 | 52 | ** -p <n> Specify the number of iterations the client-server tests |
michael@0 | 53 | ** should perform. Default: 1000. |
michael@0 | 54 | ** |
michael@0 | 55 | ** -s <n> Size, in KBytes (1024), of the shared memory segment. |
michael@0 | 56 | ** Default: (10 * 1024) |
michael@0 | 57 | ** |
michael@0 | 58 | ** -i <n> Number of client-side iterations. Default: 3 |
michael@0 | 59 | ** |
michael@0 | 60 | ** name specifies the name of the shared memory segment to be used. |
michael@0 | 61 | ** Default: /tmp/xxxNSPRshm |
michael@0 | 62 | ** |
michael@0 | 63 | ** |
michael@0 | 64 | ** See also: prshm.h |
michael@0 | 65 | ** |
michael@0 | 66 | ** /lth. Aug-1999. |
michael@0 | 67 | */ |
michael@0 | 68 | |
michael@0 | 69 | #include <plgetopt.h> |
michael@0 | 70 | #include <nspr.h> |
michael@0 | 71 | #include <stdlib.h> |
michael@0 | 72 | #include <string.h> |
michael@0 | 73 | #include <private/primpl.h> |
michael@0 | 74 | |
michael@0 | 75 | #ifdef SYMBIAN |
michael@0 | 76 | #define SEM_NAME1 "c:\\data\\nameshmSEM1" |
michael@0 | 77 | #define SEM_NAME2 "c:\\data\\nameshmSEM2" |
michael@0 | 78 | #define OPT_NAME "c:\\data\\xxxNSPRshm" |
michael@0 | 79 | #define EXE_NAME "nspr_tests_nameshm1.exe" |
michael@0 | 80 | #else |
michael@0 | 81 | #define SEM_NAME1 "/tmp/nameshmSEM1" |
michael@0 | 82 | #define SEM_NAME2 "/tmp/nameshmSEM2" |
michael@0 | 83 | #define OPT_NAME "/tmp/xxxNSPRshm" |
michael@0 | 84 | #define EXE_NAME "nameshm1" |
michael@0 | 85 | #endif |
michael@0 | 86 | #define SEM_MODE 0666 |
michael@0 | 87 | #define SHM_MODE 0666 |
michael@0 | 88 | |
michael@0 | 89 | #define NameSize (1024) |
michael@0 | 90 | |
michael@0 | 91 | PRIntn debug = 0; |
michael@0 | 92 | PRIntn failed_already = 0; |
michael@0 | 93 | PRLogModuleLevel msgLevel = PR_LOG_NONE; |
michael@0 | 94 | PRLogModuleInfo *lm; |
michael@0 | 95 | |
michael@0 | 96 | /* command line options */ |
michael@0 | 97 | PRIntn optDebug = 0; |
michael@0 | 98 | PRIntn optVerbose = 0; |
michael@0 | 99 | PRUint32 optWriteRO = 0; /* test write to read-only memory. should crash */ |
michael@0 | 100 | PRUint32 optClient = 0; |
michael@0 | 101 | PRUint32 optCreate = 1; |
michael@0 | 102 | PRUint32 optAttachRW = 1; |
michael@0 | 103 | PRUint32 optAttachRO = 1; |
michael@0 | 104 | PRUint32 optClose = 1; |
michael@0 | 105 | PRUint32 optDelete = 1; |
michael@0 | 106 | PRInt32 optPing = 1000; |
michael@0 | 107 | PRUint32 optSize = (10 * 1024 ); |
michael@0 | 108 | PRInt32 optClientIterations = 3; |
michael@0 | 109 | char optName[NameSize] = OPT_NAME; |
michael@0 | 110 | |
michael@0 | 111 | char buf[1024] = ""; |
michael@0 | 112 | |
michael@0 | 113 | |
michael@0 | 114 | static void BasicTest( void ) |
michael@0 | 115 | { |
michael@0 | 116 | PRSharedMemory *shm; |
michael@0 | 117 | char *addr; /* address of shared memory segment */ |
michael@0 | 118 | PRUint32 i; |
michael@0 | 119 | PRInt32 rc; |
michael@0 | 120 | |
michael@0 | 121 | PR_LOG( lm, msgLevel, |
michael@0 | 122 | ( "nameshm1: Begin BasicTest" )); |
michael@0 | 123 | |
michael@0 | 124 | if ( PR_FAILURE == PR_DeleteSharedMemory( optName )) { |
michael@0 | 125 | PR_LOG( lm, msgLevel, |
michael@0 | 126 | ("nameshm1: Initial PR_DeleteSharedMemory() failed. No problem")); |
michael@0 | 127 | } else |
michael@0 | 128 | PR_LOG( lm, msgLevel, |
michael@0 | 129 | ("nameshm1: Initial PR_DeleteSharedMemory() success")); |
michael@0 | 130 | |
michael@0 | 131 | |
michael@0 | 132 | shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE ); |
michael@0 | 133 | if ( NULL == shm ) |
michael@0 | 134 | { |
michael@0 | 135 | PR_LOG( lm, msgLevel, |
michael@0 | 136 | ( "nameshm1: RW Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 137 | failed_already = 1; |
michael@0 | 138 | return; |
michael@0 | 139 | } |
michael@0 | 140 | PR_LOG( lm, msgLevel, |
michael@0 | 141 | ( "nameshm1: RW Create: success: %p", shm )); |
michael@0 | 142 | |
michael@0 | 143 | addr = PR_AttachSharedMemory( shm , 0 ); |
michael@0 | 144 | if ( NULL == addr ) |
michael@0 | 145 | { |
michael@0 | 146 | PR_LOG( lm, msgLevel, |
michael@0 | 147 | ( "nameshm1: RW Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 148 | failed_already = 1; |
michael@0 | 149 | return; |
michael@0 | 150 | } |
michael@0 | 151 | PR_LOG( lm, msgLevel, |
michael@0 | 152 | ( "nameshm1: RW Attach: success: %p", addr )); |
michael@0 | 153 | |
michael@0 | 154 | /* fill memory with i */ |
michael@0 | 155 | for ( i = 0; i < optSize ; i++ ) |
michael@0 | 156 | { |
michael@0 | 157 | *(addr + i) = i; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | rc = PR_DetachSharedMemory( shm, addr ); |
michael@0 | 161 | if ( PR_FAILURE == rc ) |
michael@0 | 162 | { |
michael@0 | 163 | PR_LOG( lm, msgLevel, |
michael@0 | 164 | ( "nameshm1: RW Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 165 | failed_already = 1; |
michael@0 | 166 | return; |
michael@0 | 167 | } |
michael@0 | 168 | PR_LOG( lm, msgLevel, |
michael@0 | 169 | ( "nameshm1: RW Detach: success: " )); |
michael@0 | 170 | |
michael@0 | 171 | rc = PR_CloseSharedMemory( shm ); |
michael@0 | 172 | if ( PR_FAILURE == rc ) |
michael@0 | 173 | { |
michael@0 | 174 | PR_LOG( lm, msgLevel, |
michael@0 | 175 | ( "nameshm1: RW Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 176 | failed_already = 1; |
michael@0 | 177 | return; |
michael@0 | 178 | } |
michael@0 | 179 | PR_LOG( lm, msgLevel, |
michael@0 | 180 | ( "nameshm1: RW Close: success: " )); |
michael@0 | 181 | |
michael@0 | 182 | rc = PR_DeleteSharedMemory( optName ); |
michael@0 | 183 | if ( PR_FAILURE == rc ) |
michael@0 | 184 | { |
michael@0 | 185 | PR_LOG( lm, msgLevel, |
michael@0 | 186 | ( "nameshm1: RW Delete: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 187 | failed_already = 1; |
michael@0 | 188 | return; |
michael@0 | 189 | } |
michael@0 | 190 | PR_LOG( lm, msgLevel, |
michael@0 | 191 | ( "nameshm1: RW Delete: success: " )); |
michael@0 | 192 | |
michael@0 | 193 | PR_LOG( lm, msgLevel, |
michael@0 | 194 | ("nameshm1: BasicTest(): Passed")); |
michael@0 | 195 | |
michael@0 | 196 | return; |
michael@0 | 197 | } /* end BasicTest() */ |
michael@0 | 198 | |
michael@0 | 199 | static void ReadOnlyTest( void ) |
michael@0 | 200 | { |
michael@0 | 201 | PRSharedMemory *shm; |
michael@0 | 202 | char *roAddr; /* read-only address of shared memory segment */ |
michael@0 | 203 | PRInt32 rc; |
michael@0 | 204 | |
michael@0 | 205 | PR_LOG( lm, msgLevel, |
michael@0 | 206 | ( "nameshm1: Begin ReadOnlyTest" )); |
michael@0 | 207 | |
michael@0 | 208 | shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE); |
michael@0 | 209 | if ( NULL == shm ) |
michael@0 | 210 | { |
michael@0 | 211 | PR_LOG( lm, msgLevel, |
michael@0 | 212 | ( "nameshm1: RO Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 213 | failed_already = 1; |
michael@0 | 214 | return; |
michael@0 | 215 | } |
michael@0 | 216 | PR_LOG( lm, msgLevel, |
michael@0 | 217 | ( "nameshm1: RO Create: success: %p", shm )); |
michael@0 | 218 | |
michael@0 | 219 | |
michael@0 | 220 | roAddr = PR_AttachSharedMemory( shm , PR_SHM_READONLY ); |
michael@0 | 221 | if ( NULL == roAddr ) |
michael@0 | 222 | { |
michael@0 | 223 | PR_LOG( lm, msgLevel, |
michael@0 | 224 | ( "nameshm1: RO Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 225 | failed_already = 1; |
michael@0 | 226 | return; |
michael@0 | 227 | } |
michael@0 | 228 | PR_LOG( lm, msgLevel, |
michael@0 | 229 | ( "nameshm1: RO Attach: success: %p", roAddr )); |
michael@0 | 230 | |
michael@0 | 231 | if ( optWriteRO ) |
michael@0 | 232 | { |
michael@0 | 233 | *roAddr = 0x00; /* write to read-only memory */ |
michael@0 | 234 | failed_already = 1; |
michael@0 | 235 | PR_LOG( lm, msgLevel, ("nameshm1: Wrote to read-only memory segment!")); |
michael@0 | 236 | return; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | rc = PR_DetachSharedMemory( shm, roAddr ); |
michael@0 | 240 | if ( PR_FAILURE == rc ) |
michael@0 | 241 | { |
michael@0 | 242 | PR_LOG( lm, msgLevel, |
michael@0 | 243 | ( "nameshm1: RO Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 244 | failed_already = 1; |
michael@0 | 245 | return; |
michael@0 | 246 | } |
michael@0 | 247 | PR_LOG( lm, msgLevel, |
michael@0 | 248 | ( "nameshm1: RO Detach: success: " )); |
michael@0 | 249 | |
michael@0 | 250 | rc = PR_CloseSharedMemory( shm ); |
michael@0 | 251 | if ( PR_FAILURE == rc ) |
michael@0 | 252 | { |
michael@0 | 253 | PR_LOG( lm, msgLevel, |
michael@0 | 254 | ( "nameshm1: RO Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 255 | failed_already = 1; |
michael@0 | 256 | return; |
michael@0 | 257 | } |
michael@0 | 258 | PR_LOG( lm, msgLevel, |
michael@0 | 259 | ( "nameshm1: RO Close: success: " )); |
michael@0 | 260 | |
michael@0 | 261 | rc = PR_DeleteSharedMemory( optName ); |
michael@0 | 262 | if ( PR_FAILURE == rc ) |
michael@0 | 263 | { |
michael@0 | 264 | PR_LOG( lm, msgLevel, |
michael@0 | 265 | ( "nameshm1: RO Destroy: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 266 | failed_already = 1; |
michael@0 | 267 | return; |
michael@0 | 268 | } |
michael@0 | 269 | PR_LOG( lm, msgLevel, |
michael@0 | 270 | ( "nameshm1: RO Destroy: success: " )); |
michael@0 | 271 | |
michael@0 | 272 | PR_LOG( lm, msgLevel, |
michael@0 | 273 | ("nameshm1: ReadOnlyTest(): Passed")); |
michael@0 | 274 | |
michael@0 | 275 | return; |
michael@0 | 276 | } /* end ReadOnlyTest() */ |
michael@0 | 277 | |
michael@0 | 278 | static void DoClient( void ) |
michael@0 | 279 | { |
michael@0 | 280 | PRStatus rc; |
michael@0 | 281 | PRSem *sem1, *sem2; |
michael@0 | 282 | PRSharedMemory *shm; |
michael@0 | 283 | PRUint32 *addr; |
michael@0 | 284 | PRInt32 i; |
michael@0 | 285 | |
michael@0 | 286 | PR_LOG( lm, msgLevel, |
michael@0 | 287 | ("nameshm1: DoClient(): Starting")); |
michael@0 | 288 | |
michael@0 | 289 | sem1 = PR_OpenSemaphore( SEM_NAME1, 0, 0, 0 ); |
michael@0 | 290 | PR_ASSERT( sem1 ); |
michael@0 | 291 | |
michael@0 | 292 | sem2 = PR_OpenSemaphore( SEM_NAME2, 0, 0, 0 ); |
michael@0 | 293 | PR_ASSERT( sem1 ); |
michael@0 | 294 | |
michael@0 | 295 | shm = PR_OpenSharedMemory( optName, optSize, 0, SHM_MODE ); |
michael@0 | 296 | if ( NULL == shm ) |
michael@0 | 297 | { |
michael@0 | 298 | PR_LOG( lm, msgLevel, |
michael@0 | 299 | ( "nameshm1: DoClient(): Create: Error: %ld. OSError: %ld", |
michael@0 | 300 | PR_GetError(), PR_GetOSError())); |
michael@0 | 301 | failed_already = 1; |
michael@0 | 302 | return; |
michael@0 | 303 | } |
michael@0 | 304 | PR_LOG( lm, msgLevel, |
michael@0 | 305 | ( "nameshm1: DoClient(): Create: success: %p", shm )); |
michael@0 | 306 | |
michael@0 | 307 | addr = PR_AttachSharedMemory( shm , 0 ); |
michael@0 | 308 | if ( NULL == addr ) |
michael@0 | 309 | { |
michael@0 | 310 | PR_LOG( lm, msgLevel, |
michael@0 | 311 | ( "nameshm1: DoClient(): Attach: Error: %ld. OSError: %ld", |
michael@0 | 312 | PR_GetError(), PR_GetOSError())); |
michael@0 | 313 | failed_already = 1; |
michael@0 | 314 | return; |
michael@0 | 315 | } |
michael@0 | 316 | PR_LOG( lm, msgLevel, |
michael@0 | 317 | ( "nameshm1: DoClient(): Attach: success: %p", addr )); |
michael@0 | 318 | |
michael@0 | 319 | PR_LOG( lm, msgLevel, |
michael@0 | 320 | ( "Client found: %s", addr)); |
michael@0 | 321 | |
michael@0 | 322 | PR_Sleep(PR_SecondsToInterval(4)); |
michael@0 | 323 | for ( i = 0 ; i < optPing ; i++ ) |
michael@0 | 324 | { |
michael@0 | 325 | rc = PR_WaitSemaphore( sem2 ); |
michael@0 | 326 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 327 | |
michael@0 | 328 | (*addr)++; |
michael@0 | 329 | PR_ASSERT( (*addr % 2) == 0 ); |
michael@0 | 330 | if ( optVerbose ) |
michael@0 | 331 | PR_LOG( lm, msgLevel, |
michael@0 | 332 | ( "nameshm1: Client ping: %d, i: %d", *addr, i)); |
michael@0 | 333 | |
michael@0 | 334 | rc = PR_PostSemaphore( sem1 ); |
michael@0 | 335 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | rc = PR_CloseSemaphore( sem1 ); |
michael@0 | 339 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 340 | |
michael@0 | 341 | rc = PR_CloseSemaphore( sem2 ); |
michael@0 | 342 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 343 | |
michael@0 | 344 | rc = PR_DetachSharedMemory( shm, addr ); |
michael@0 | 345 | if ( PR_FAILURE == rc ) |
michael@0 | 346 | { |
michael@0 | 347 | PR_LOG( lm, msgLevel, |
michael@0 | 348 | ( "nameshm1: DoClient(): Detach: Error: %ld. OSError: %ld", |
michael@0 | 349 | PR_GetError(), PR_GetOSError())); |
michael@0 | 350 | failed_already = 1; |
michael@0 | 351 | return; |
michael@0 | 352 | } |
michael@0 | 353 | PR_LOG( lm, msgLevel, |
michael@0 | 354 | ( "nameshm1: DoClient(): Detach: success: " )); |
michael@0 | 355 | |
michael@0 | 356 | rc = PR_CloseSharedMemory( shm ); |
michael@0 | 357 | if ( PR_FAILURE == rc ) |
michael@0 | 358 | { |
michael@0 | 359 | PR_LOG( lm, msgLevel, |
michael@0 | 360 | ( "nameshm1: DoClient(): Close: Error: %ld. OSError: %ld", |
michael@0 | 361 | PR_GetError(), PR_GetOSError())); |
michael@0 | 362 | failed_already = 1; |
michael@0 | 363 | return; |
michael@0 | 364 | } |
michael@0 | 365 | PR_LOG( lm, msgLevel, |
michael@0 | 366 | ( "nameshm1: DoClient(): Close: success: " )); |
michael@0 | 367 | |
michael@0 | 368 | return; |
michael@0 | 369 | } /* end DoClient() */ |
michael@0 | 370 | |
michael@0 | 371 | static void ClientServerTest( void ) |
michael@0 | 372 | { |
michael@0 | 373 | PRStatus rc; |
michael@0 | 374 | PRSem *sem1, *sem2; |
michael@0 | 375 | PRProcess *proc; |
michael@0 | 376 | PRInt32 exit_status; |
michael@0 | 377 | PRSharedMemory *shm; |
michael@0 | 378 | PRUint32 *addr; |
michael@0 | 379 | PRInt32 i; |
michael@0 | 380 | char *child_argv[8]; |
michael@0 | 381 | char buf[24]; |
michael@0 | 382 | |
michael@0 | 383 | PR_LOG( lm, msgLevel, |
michael@0 | 384 | ( "nameshm1: Begin ClientServerTest" )); |
michael@0 | 385 | |
michael@0 | 386 | rc = PR_DeleteSharedMemory( optName ); |
michael@0 | 387 | if ( PR_FAILURE == rc ) |
michael@0 | 388 | { |
michael@0 | 389 | PR_LOG( lm, msgLevel, |
michael@0 | 390 | ( "nameshm1: Server: Destroy: failed. No problem")); |
michael@0 | 391 | } else |
michael@0 | 392 | PR_LOG( lm, msgLevel, |
michael@0 | 393 | ( "nameshm1: Server: Destroy: success" )); |
michael@0 | 394 | |
michael@0 | 395 | |
michael@0 | 396 | shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE); |
michael@0 | 397 | if ( NULL == shm ) |
michael@0 | 398 | { |
michael@0 | 399 | PR_LOG( lm, msgLevel, |
michael@0 | 400 | ( "nameshm1: Server: Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 401 | failed_already = 1; |
michael@0 | 402 | return; |
michael@0 | 403 | } |
michael@0 | 404 | PR_LOG( lm, msgLevel, |
michael@0 | 405 | ( "nameshm1: Server: Create: success: %p", shm )); |
michael@0 | 406 | |
michael@0 | 407 | addr = PR_AttachSharedMemory( shm , 0 ); |
michael@0 | 408 | if ( NULL == addr ) |
michael@0 | 409 | { |
michael@0 | 410 | PR_LOG( lm, msgLevel, |
michael@0 | 411 | ( "nameshm1: Server: Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); |
michael@0 | 412 | failed_already = 1; |
michael@0 | 413 | return; |
michael@0 | 414 | } |
michael@0 | 415 | PR_LOG( lm, msgLevel, |
michael@0 | 416 | ( "nameshm1: Server: Attach: success: %p", addr )); |
michael@0 | 417 | |
michael@0 | 418 | sem1 = PR_OpenSemaphore( SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0 ); |
michael@0 | 419 | PR_ASSERT( sem1 ); |
michael@0 | 420 | |
michael@0 | 421 | sem2 = PR_OpenSemaphore( SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 1 ); |
michael@0 | 422 | PR_ASSERT( sem1 ); |
michael@0 | 423 | |
michael@0 | 424 | strcpy( (char*)addr, "FooBar" ); |
michael@0 | 425 | |
michael@0 | 426 | child_argv[0] = EXE_NAME; |
michael@0 | 427 | child_argv[1] = "-C"; |
michael@0 | 428 | child_argv[2] = "-p"; |
michael@0 | 429 | sprintf( buf, "%d", optPing ); |
michael@0 | 430 | child_argv[3] = buf; |
michael@0 | 431 | child_argv[4] = optName; |
michael@0 | 432 | child_argv[5] = NULL; |
michael@0 | 433 | |
michael@0 | 434 | proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL); |
michael@0 | 435 | PR_ASSERT( proc ); |
michael@0 | 436 | |
michael@0 | 437 | PR_Sleep( PR_SecondsToInterval(4)); |
michael@0 | 438 | |
michael@0 | 439 | *addr = 1; |
michael@0 | 440 | for ( i = 0 ; i < optPing ; i++ ) |
michael@0 | 441 | { |
michael@0 | 442 | rc = PR_WaitSemaphore( sem1 ); |
michael@0 | 443 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 444 | |
michael@0 | 445 | (*addr)++; |
michael@0 | 446 | PR_ASSERT( (*addr % 2) == 1 ); |
michael@0 | 447 | if ( optVerbose ) |
michael@0 | 448 | PR_LOG( lm, msgLevel, |
michael@0 | 449 | ( "nameshm1: Server pong: %d, i: %d", *addr, i)); |
michael@0 | 450 | |
michael@0 | 451 | |
michael@0 | 452 | rc = PR_PostSemaphore( sem2 ); |
michael@0 | 453 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | rc = PR_WaitProcess( proc, &exit_status ); |
michael@0 | 457 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 458 | |
michael@0 | 459 | rc = PR_CloseSemaphore( sem1 ); |
michael@0 | 460 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 461 | |
michael@0 | 462 | rc = PR_CloseSemaphore( sem2 ); |
michael@0 | 463 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 464 | |
michael@0 | 465 | rc = PR_DeleteSemaphore( SEM_NAME1 ); |
michael@0 | 466 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 467 | |
michael@0 | 468 | rc = PR_DeleteSemaphore( SEM_NAME2 ); |
michael@0 | 469 | PR_ASSERT( PR_FAILURE != rc ); |
michael@0 | 470 | |
michael@0 | 471 | rc = PR_DetachSharedMemory( shm, addr ); |
michael@0 | 472 | if ( PR_FAILURE == rc ) |
michael@0 | 473 | { |
michael@0 | 474 | PR_LOG( lm, msgLevel, |
michael@0 | 475 | ( "nameshm1: Server: Detach: Error: %ld. OSError: %ld", |
michael@0 | 476 | PR_GetError(), PR_GetOSError())); |
michael@0 | 477 | failed_already = 1; |
michael@0 | 478 | return; |
michael@0 | 479 | } |
michael@0 | 480 | PR_LOG( lm, msgLevel, |
michael@0 | 481 | ( "nameshm1: Server: Detach: success: " )); |
michael@0 | 482 | |
michael@0 | 483 | rc = PR_CloseSharedMemory( shm ); |
michael@0 | 484 | if ( PR_FAILURE == rc ) |
michael@0 | 485 | { |
michael@0 | 486 | PR_LOG( lm, msgLevel, |
michael@0 | 487 | ( "nameshm1: Server: Close: Error: %ld. OSError: %ld", |
michael@0 | 488 | PR_GetError(), PR_GetOSError())); |
michael@0 | 489 | failed_already = 1; |
michael@0 | 490 | return; |
michael@0 | 491 | } |
michael@0 | 492 | PR_LOG( lm, msgLevel, |
michael@0 | 493 | ( "nameshm1: Server: Close: success: " )); |
michael@0 | 494 | |
michael@0 | 495 | rc = PR_DeleteSharedMemory( optName ); |
michael@0 | 496 | if ( PR_FAILURE == rc ) |
michael@0 | 497 | { |
michael@0 | 498 | PR_LOG( lm, msgLevel, |
michael@0 | 499 | ( "nameshm1: Server: Destroy: Error: %ld. OSError: %ld", |
michael@0 | 500 | PR_GetError(), PR_GetOSError())); |
michael@0 | 501 | failed_already = 1; |
michael@0 | 502 | return; |
michael@0 | 503 | } |
michael@0 | 504 | PR_LOG( lm, msgLevel, |
michael@0 | 505 | ( "nameshm1: Server: Destroy: success" )); |
michael@0 | 506 | |
michael@0 | 507 | return; |
michael@0 | 508 | } /* end ClientServerTest() */ |
michael@0 | 509 | |
michael@0 | 510 | int main(int argc, char **argv) |
michael@0 | 511 | { |
michael@0 | 512 | { |
michael@0 | 513 | /* |
michael@0 | 514 | ** Get command line options |
michael@0 | 515 | */ |
michael@0 | 516 | PLOptStatus os; |
michael@0 | 517 | PLOptState *opt = PL_CreateOptState(argc, argv, "Cdvw:s:p:i:"); |
michael@0 | 518 | |
michael@0 | 519 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
michael@0 | 520 | { |
michael@0 | 521 | if (PL_OPT_BAD == os) continue; |
michael@0 | 522 | switch (opt->option) |
michael@0 | 523 | { |
michael@0 | 524 | case 'v': /* debug mode */ |
michael@0 | 525 | optVerbose = 1; |
michael@0 | 526 | /* no break! fall into debug option */ |
michael@0 | 527 | case 'd': /* debug mode */ |
michael@0 | 528 | debug = 1; |
michael@0 | 529 | msgLevel = PR_LOG_DEBUG; |
michael@0 | 530 | break; |
michael@0 | 531 | case 'w': /* try writing to memory mapped read-only */ |
michael@0 | 532 | optWriteRO = 1; |
michael@0 | 533 | break; |
michael@0 | 534 | case 'C': |
michael@0 | 535 | optClient = 1; |
michael@0 | 536 | break; |
michael@0 | 537 | case 's': |
michael@0 | 538 | optSize = atol(opt->value) * 1024; |
michael@0 | 539 | break; |
michael@0 | 540 | case 'p': |
michael@0 | 541 | optPing = atol(opt->value); |
michael@0 | 542 | break; |
michael@0 | 543 | case 'i': |
michael@0 | 544 | optClientIterations = atol(opt->value); |
michael@0 | 545 | break; |
michael@0 | 546 | default: |
michael@0 | 547 | strcpy( optName, opt->value ); |
michael@0 | 548 | break; |
michael@0 | 549 | } |
michael@0 | 550 | } |
michael@0 | 551 | PL_DestroyOptState(opt); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | lm = PR_NewLogModule("Test"); /* Initialize logging */ |
michael@0 | 555 | |
michael@0 | 556 | PR_LOG( lm, msgLevel, |
michael@0 | 557 | ( "nameshm1: Starting" )); |
michael@0 | 558 | |
michael@0 | 559 | if ( optClient ) |
michael@0 | 560 | { |
michael@0 | 561 | DoClient(); |
michael@0 | 562 | } else { |
michael@0 | 563 | BasicTest(); |
michael@0 | 564 | if ( failed_already != 0 ) |
michael@0 | 565 | goto Finished; |
michael@0 | 566 | ReadOnlyTest(); |
michael@0 | 567 | if ( failed_already != 0 ) |
michael@0 | 568 | goto Finished; |
michael@0 | 569 | ClientServerTest(); |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | Finished: |
michael@0 | 573 | if ( debug ) printf("%s\n", (failed_already)? "FAIL" : "PASS" ); |
michael@0 | 574 | return( (failed_already)? 1 : 0 ); |
michael@0 | 575 | } /* main() */ |
michael@0 | 576 | /* end instrumt.c */ |