summaryrefslogtreecommitdiffstats
path: root/nsprpub/pr/tests/nameshm1.c
blob: 3f7137e23e1713843817d0ba35873d85493977dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
** File: nameshm1.c -- Test Named Shared Memory
**
** Description:
** nameshm1 tests Named Shared Memory. nameshm1 performs two tests of
** named shared memory.
**
** The first test is a basic test. The basic test operates as a single
** process. The process exercises all the API elements of the facility.
** This test also attempts to write to all locations in the shared
** memory.
**
** The second test is a client-server test. The client-server test
** creates a new instance of nameshm1, passing the -C argument to the
** new process; this creates the client-side process. The server-side
** (the instance of nameshm1 created from the command line) and the
** client-side interact via inter-process semaphores to verify that the
** shared memory segment can be read and written by both sides in a
** synchronized maner.
**
** Note: Because this test runs in two processes, the log files created
** by the test are not in chronological sequence; makes it hard to read.
** As a temporary circumvention, I changed the definition(s) of the
** _PUT_LOG() macro in prlog.c to force a flushall(), or equivalent.
** This causes the log entries to be emitted in true chronological
** order.
**
** Synopsis: nameshm1 [options] [name]
**
** Options:
** -d       Enables debug trace via PR_LOG()
** -v       Enables verbose mode debug trace via PR_LOG()
** -w       Causes the basic test to attempt to write to the segment
**          mapped as read-only. When this option is specified, the
**          test should crash with a seg-fault; this is a destructive
**          test and is considered successful when it seg-faults.
**
** -C       Causes nameshm1 to start as the client-side of a
**          client-server pair of processes. Only the instance
**          of nameshm1 operating as the server-side process should
**          specify the -C option when creating the client-side process;
**          the -C option should not be specified at the command line.
**          The client-side uses the shared memory segment created by
**          the server-side to communicate with the server-side
**          process.
**
** -p <n>   Specify the number of iterations the client-server tests
**          should perform. Default: 1000.
**
** -s <n>   Size, in KBytes (1024), of the shared memory segment.
**          Default: (10 * 1024)
**
** -i <n>   Number of client-side iterations. Default: 3
**
** name     specifies the name of the shared memory segment to be used.
**          Default: /tmp/xxxNSPRshm
**
**
** See also: prshm.h
**
** /lth. Aug-1999.
*/

#include <plgetopt.h>
#include <nspr.h>
#include <stdlib.h>
#include <string.h>
#include <private/primpl.h>

#define SEM_NAME1 "/tmp/nameshmSEM1"
#define SEM_NAME2 "/tmp/nameshmSEM2"
#define OPT_NAME "/tmp/xxxNSPRshm"
#define EXE_NAME "nameshm1"
#define SEM_MODE  0666
#define SHM_MODE  0666

#define NameSize (1024)

PRIntn  debug = 0;
PRIntn  failed_already = 0;
PRLogModuleLevel msgLevel = PR_LOG_NONE;
PRLogModuleInfo *lm;

/* command line options */
PRIntn      optDebug = 0;
PRIntn      optVerbose = 0;
PRUint32    optWriteRO = 0;     /* test write to read-only memory. should crash  */
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] = OPT_NAME;

char buf[1024] = "";


static void BasicTest( void )
{
    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() */

static void ReadOnlyTest( void )
{
    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() */

static void DoClient( void )
{
    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() */

static void ClientServerTest( void )
{
    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] = EXE_NAME;
    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() */

int main(int argc, char **argv)
{
    {
        /*
        ** 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() */
/* end instrumt.c */