|
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 SYMBIAN |
|
12 #define SEM_NAME1 "c:\\data\\foo.sem" |
|
13 #define SEM_NAME2 "c:\\data\\bar.sem" |
|
14 #else |
|
15 #define SEM_NAME1 "/tmp/foo.sem" |
|
16 #define SEM_NAME2 "/tmp/bar.sem" |
|
17 #endif |
|
18 #define SEM_MODE 0666 |
|
19 |
|
20 static PRBool debug_mode = PR_FALSE; |
|
21 |
|
22 static void Help(void) |
|
23 { |
|
24 fprintf(stderr, "semaerr1 test program usage:\n"); |
|
25 fprintf(stderr, "\t-d debug mode (FALSE)\n"); |
|
26 fprintf(stderr, "\t-h this message\n"); |
|
27 } /* Help */ |
|
28 |
|
29 int main(int argc, char **argv) |
|
30 { |
|
31 PLOptStatus os; |
|
32 PLOptState *opt = PL_CreateOptState(argc, argv, "dh"); |
|
33 PRSem *sem; |
|
34 |
|
35 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
|
36 if (PL_OPT_BAD == os) continue; |
|
37 switch (opt->option) { |
|
38 case 'd': /* debug mode */ |
|
39 debug_mode = PR_TRUE; |
|
40 break; |
|
41 case 'h': |
|
42 default: |
|
43 Help(); |
|
44 return 2; |
|
45 } |
|
46 } |
|
47 PL_DestroyOptState(opt); |
|
48 |
|
49 /* |
|
50 * PR_SEM_CREATE|PR_SEM_EXCL should be able to |
|
51 * create a nonexistent semaphore. |
|
52 */ |
|
53 (void) PR_DeleteSemaphore(SEM_NAME2); |
|
54 sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0); |
|
55 if (sem == NULL) { |
|
56 fprintf(stderr, "PR_OpenSemaphore failed\n"); |
|
57 exit(1); |
|
58 } |
|
59 if (PR_CloseSemaphore(sem) == PR_FAILURE) { |
|
60 fprintf(stderr, "PR_CloseSemaphore failed\n"); |
|
61 exit(1); |
|
62 } |
|
63 if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) { |
|
64 fprintf(stderr, "PR_DeleteSemaphore failed\n"); |
|
65 exit(1); |
|
66 } |
|
67 |
|
68 /* |
|
69 * Opening an existing semaphore with PR_SEM_CREATE|PR_SEM_EXCL. |
|
70 * should fail with PR_FILE_EXISTS_ERROR. |
|
71 */ |
|
72 sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0); |
|
73 if (sem != NULL) { |
|
74 fprintf(stderr, "PR_OpenSemaphore should fail but succeeded\n"); |
|
75 exit(1); |
|
76 } |
|
77 if (PR_GetError() != PR_FILE_EXISTS_ERROR) { |
|
78 fprintf(stderr, "Expect %d but got %d\n", PR_FILE_EXISTS_ERROR, |
|
79 PR_GetError()); |
|
80 exit(1); |
|
81 } |
|
82 |
|
83 /* |
|
84 * Try again, with just PR_SEM_CREATE. This should succeed. |
|
85 */ |
|
86 sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0); |
|
87 if (sem == NULL) { |
|
88 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
|
89 PR_GetError(), PR_GetOSError()); |
|
90 exit(1); |
|
91 } |
|
92 if (PR_CloseSemaphore(sem) == PR_FAILURE) { |
|
93 fprintf(stderr, "PR_CloseSemaphore failed\n"); |
|
94 exit(1); |
|
95 } |
|
96 |
|
97 sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0); |
|
98 if (sem == NULL) { |
|
99 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
|
100 PR_GetError(), PR_GetOSError()); |
|
101 exit(1); |
|
102 } |
|
103 if (PR_CloseSemaphore(sem) == PR_FAILURE) { |
|
104 fprintf(stderr, "PR_CloseSemaphore failed\n"); |
|
105 exit(1); |
|
106 } |
|
107 |
|
108 printf("PASS\n"); |
|
109 return 0; |
|
110 } |