1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "nspr.h"
7 #include "plgetopt.h"
8 
9 #include <stdio.h>
10 
11 #ifdef DEBUG
12 #define SEM_D "D"
13 #else
14 #define SEM_D
15 #endif
16 #ifdef IS_64
17 #define SEM_64 "64"
18 #else
19 #define SEM_64
20 #endif
21 
22 #define SHM_NAME "/tmp/counter" SEM_D SEM_64
23 #define SEM_NAME1 "/tmp/foo.sem" SEM_D SEM_64
24 #define SEM_NAME2 "/tmp/bar.sem" SEM_D SEM_64
25 #define EXE_NAME "semapong"
26 #define SEM_MODE  0666
27 #define SHM_MODE  0666
28 #define ITERATIONS 1000
29 
30 static PRBool debug_mode = PR_FALSE;
31 static PRIntn iterations = ITERATIONS;
32 static PRSem *sem1, *sem2;
33 
Help(void)34 static void Help(void)
35 {
36     fprintf(stderr, "semaping test program usage:\n");
37     fprintf(stderr, "\t-d           debug mode         (FALSE)\n");
38     fprintf(stderr, "\t-c <count>   loop count         (%d)\n", ITERATIONS);
39     fprintf(stderr, "\t-h           this message\n");
40 }  /* Help */
41 
main(int argc,char ** argv)42 int main(int argc, char **argv)
43 {
44     PRProcess *proc;
45     PRIntn i;
46     char *child_argv[32];
47     char **child_arg;
48     char iterations_buf[32];
49     PRSharedMemory *shm;
50     PRIntn *counter_addr;
51     PRInt32 exit_code;
52     PLOptStatus os;
53     PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
54 
55     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
56         if (PL_OPT_BAD == os) {
57             continue;
58         }
59         switch (opt->option) {
60             case 'd':  /* debug mode */
61                 debug_mode = PR_TRUE;
62                 break;
63             case 'c':  /* loop count */
64                 iterations = atoi(opt->value);
65                 break;
66             case 'h':
67             default:
68                 Help();
69                 return 2;
70         }
71     }
72     PL_DestroyOptState(opt);
73 
74     if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) {
75         fprintf(stderr, "warning: removed shared memory %s left over "
76                 "from previous run\n", SHM_NAME);
77     }
78     if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
79         fprintf(stderr, "warning: removed semaphore %s left over "
80                 "from previous run\n", SEM_NAME1);
81     }
82     if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
83         fprintf(stderr, "warning: removed semaphore %s left over "
84                 "from previous run\n", SEM_NAME2);
85     }
86 
87     shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE);
88     if (NULL == shm) {
89         fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
90                 PR_GetError(), PR_GetOSError());
91         exit(1);
92     }
93     counter_addr = PR_AttachSharedMemory(shm, 0);
94     if (NULL == counter_addr) {
95         fprintf(stderr, "PR_AttachSharedMemory failed\n");
96         exit(1);
97     }
98     *counter_addr = 0;
99     sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
100     if (NULL == sem1) {
101         fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
102                 PR_GetError(), PR_GetOSError());
103         exit(1);
104     }
105     sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
106     if (NULL == sem2) {
107         fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
108                 PR_GetError(), PR_GetOSError());
109         exit(1);
110     }
111 
112     child_arg = &child_argv[0];
113     *child_arg++ = EXE_NAME;
114     if (debug_mode != PR_FALSE) {
115         *child_arg++ = "-d";
116     }
117     if (iterations != ITERATIONS) {
118         *child_arg++ = "-c";
119         PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations);
120         *child_arg++ = iterations_buf;
121     }
122     *child_arg = NULL;
123     proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
124     if (NULL == proc) {
125         fprintf(stderr, "PR_CreateProcess failed\n");
126         exit(1);
127     }
128 
129     /*
130      * Process 1 waits on semaphore 1 and posts to semaphore 2.
131      */
132     for (i = 0; i < iterations; i++) {
133         if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
134             fprintf(stderr, "PR_WaitSemaphore failed\n");
135             exit(1);
136         }
137         if (*counter_addr == 2*i) {
138             if (debug_mode) {
139                 printf("process 1: counter = %d\n", *counter_addr);
140             }
141         } else {
142             fprintf(stderr, "process 1: counter should be %d but is %d\n",
143                     2*i, *counter_addr);
144             exit(1);
145         }
146         (*counter_addr)++;
147         if (PR_PostSemaphore(sem2) == PR_FAILURE) {
148             fprintf(stderr, "PR_PostSemaphore failed\n");
149             exit(1);
150         }
151     }
152     if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
153         fprintf(stderr, "PR_DetachSharedMemory failed\n");
154         exit(1);
155     }
156     if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
157         fprintf(stderr, "PR_CloseSharedMemory failed\n");
158         exit(1);
159     }
160     if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
161         fprintf(stderr, "PR_CloseSemaphore failed\n");
162     }
163     if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
164         fprintf(stderr, "PR_CloseSemaphore failed\n");
165     }
166 
167     if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
168         fprintf(stderr, "PR_WaitProcess failed\n");
169         exit(1);
170     }
171     if (exit_code != 0) {
172         fprintf(stderr, "process 2 failed with exit code %d\n", exit_code);
173         exit(1);
174     }
175 
176     if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) {
177         fprintf(stderr, "PR_DeleteSharedMemory failed\n");
178     }
179     if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
180         fprintf(stderr, "PR_DeleteSemaphore failed\n");
181     }
182     if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
183         fprintf(stderr, "PR_DeleteSemaphore failed\n");
184     }
185     printf("PASS\n");
186     return 0;
187 }
188