1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1999-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nspr.h"
|
---|
39 | #include "plgetopt.h"
|
---|
40 |
|
---|
41 | #include <stdio.h>
|
---|
42 |
|
---|
43 | #define SEM_NAME1 "/tmp/foo.sem"
|
---|
44 | #define SEM_NAME2 "/tmp/bar.sem"
|
---|
45 | #define SEM_MODE 0666
|
---|
46 |
|
---|
47 | static PRBool debug_mode = PR_FALSE;
|
---|
48 |
|
---|
49 | static void Help(void)
|
---|
50 | {
|
---|
51 | fprintf(stderr, "semaerr1 test program usage:\n");
|
---|
52 | fprintf(stderr, "\t-d debug mode (FALSE)\n");
|
---|
53 | fprintf(stderr, "\t-h this message\n");
|
---|
54 | } /* Help */
|
---|
55 |
|
---|
56 | int main(int argc, char **argv)
|
---|
57 | {
|
---|
58 | PLOptStatus os;
|
---|
59 | PLOptState *opt = PL_CreateOptState(argc, argv, "dh");
|
---|
60 | PRSem *sem;
|
---|
61 |
|
---|
62 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
|
---|
63 | if (PL_OPT_BAD == os) continue;
|
---|
64 | switch (opt->option) {
|
---|
65 | case 'd': /* debug mode */
|
---|
66 | debug_mode = PR_TRUE;
|
---|
67 | break;
|
---|
68 | case 'h':
|
---|
69 | default:
|
---|
70 | Help();
|
---|
71 | return 2;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | PL_DestroyOptState(opt);
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * PR_SEM_CREATE|PR_SEM_EXCL should be able to
|
---|
78 | * create a nonexistent semaphore.
|
---|
79 | */
|
---|
80 | (void) PR_DeleteSemaphore(SEM_NAME2);
|
---|
81 | sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
|
---|
82 | if (sem == NULL) {
|
---|
83 | fprintf(stderr, "PR_OpenSemaphore failed\n");
|
---|
84 | exit(1);
|
---|
85 | }
|
---|
86 | if (PR_CloseSemaphore(sem) == PR_FAILURE) {
|
---|
87 | fprintf(stderr, "PR_CloseSemaphore failed\n");
|
---|
88 | exit(1);
|
---|
89 | }
|
---|
90 | if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
|
---|
91 | fprintf(stderr, "PR_DeleteSemaphore failed\n");
|
---|
92 | exit(1);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Opening an existing semaphore with PR_SEM_CREATE|PR_SEM_EXCL.
|
---|
97 | * should fail with PR_FILE_EXISTS_ERROR.
|
---|
98 | */
|
---|
99 | sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
|
---|
100 | if (sem != NULL) {
|
---|
101 | fprintf(stderr, "PR_OpenSemaphore should fail but succeeded\n");
|
---|
102 | exit(1);
|
---|
103 | }
|
---|
104 | if (PR_GetError() != PR_FILE_EXISTS_ERROR) {
|
---|
105 | fprintf(stderr, "Expect %d but got %d\n", PR_FILE_EXISTS_ERROR,
|
---|
106 | PR_GetError());
|
---|
107 | exit(1);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Try again, with just PR_SEM_CREATE. This should succeed.
|
---|
112 | */
|
---|
113 | sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0);
|
---|
114 | if (sem == NULL) {
|
---|
115 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
|
---|
116 | PR_GetError(), PR_GetOSError());
|
---|
117 | exit(1);
|
---|
118 | }
|
---|
119 | if (PR_CloseSemaphore(sem) == PR_FAILURE) {
|
---|
120 | fprintf(stderr, "PR_CloseSemaphore failed\n");
|
---|
121 | exit(1);
|
---|
122 | }
|
---|
123 |
|
---|
124 | sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
|
---|
125 | if (sem == NULL) {
|
---|
126 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
|
---|
127 | PR_GetError(), PR_GetOSError());
|
---|
128 | exit(1);
|
---|
129 | }
|
---|
130 | if (PR_CloseSemaphore(sem) == PR_FAILURE) {
|
---|
131 | fprintf(stderr, "PR_CloseSemaphore failed\n");
|
---|
132 | exit(1);
|
---|
133 | }
|
---|
134 |
|
---|
135 | printf("PASS\n");
|
---|
136 | return 0;
|
---|
137 | }
|
---|