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 NO_SUCH_SEM_NAME "/tmp/nosuchsem.sem"
|
---|
44 | #define SEM_NAME1 "/tmp/foo.sem"
|
---|
45 | #define SEM_MODE 0666
|
---|
46 |
|
---|
47 | static PRBool debug_mode = PR_FALSE;
|
---|
48 |
|
---|
49 | static void Help(void)
|
---|
50 | {
|
---|
51 | fprintf(stderr, "semaerr 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 | char *child_argv[32];
|
---|
62 | char **child_arg;
|
---|
63 | PRProcess *proc;
|
---|
64 | PRInt32 exit_code;
|
---|
65 |
|
---|
66 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
|
---|
67 | if (PL_OPT_BAD == os) continue;
|
---|
68 | switch (opt->option) {
|
---|
69 | case 'd': /* debug mode */
|
---|
70 | debug_mode = PR_TRUE;
|
---|
71 | break;
|
---|
72 | case 'h':
|
---|
73 | default:
|
---|
74 | Help();
|
---|
75 | return 2;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | PL_DestroyOptState(opt);
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Open a nonexistent semaphore without the PR_SEM_CREATE
|
---|
82 | * flag should fail with PR_FILE_NOT_FOUND_ERROR.
|
---|
83 | */
|
---|
84 | (void) PR_DeleteSemaphore(NO_SUCH_SEM_NAME);
|
---|
85 | sem = PR_OpenSemaphore(NO_SUCH_SEM_NAME, 0, 0, 0);
|
---|
86 | if (NULL != sem) {
|
---|
87 | fprintf(stderr, "Opening nonexistent semaphore %s "
|
---|
88 | "without the PR_SEM_CREATE flag should fail "
|
---|
89 | "but succeeded\n", NO_SUCH_SEM_NAME);
|
---|
90 | exit(1);
|
---|
91 | }
|
---|
92 | if (PR_GetError() != PR_FILE_NOT_FOUND_ERROR) {
|
---|
93 | fprintf(stderr, "Expected error is %d but got (%d, %d)\n",
|
---|
94 | PR_FILE_NOT_FOUND_ERROR, PR_GetError(), PR_GetOSError());
|
---|
95 | exit(1);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Create a semaphore and let the another process
|
---|
100 | * try PR_SEM_CREATE and PR_SEM_CREATE|PR_SEM_EXCL.
|
---|
101 | */
|
---|
102 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
|
---|
103 | fprintf(stderr, "warning: deleted semaphore %s from previous "
|
---|
104 | "run of the test\n", SEM_NAME1);
|
---|
105 | }
|
---|
106 | sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0);
|
---|
107 | if (sem == NULL) {
|
---|
108 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
|
---|
109 | PR_GetError(), PR_GetOSError());
|
---|
110 | exit(1);
|
---|
111 | }
|
---|
112 | child_arg = child_argv;
|
---|
113 | *child_arg++ = "semaerr1";
|
---|
114 | if (debug_mode) {
|
---|
115 | *child_arg++ = "-d";
|
---|
116 | }
|
---|
117 | *child_arg = NULL;
|
---|
118 | proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
|
---|
119 | if (proc == NULL) {
|
---|
120 | fprintf(stderr, "PR_CreateProcess failed\n");
|
---|
121 | exit(1);
|
---|
122 | }
|
---|
123 | if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
|
---|
124 | fprintf(stderr, "PR_WaitProcess failed\n");
|
---|
125 | exit(1);
|
---|
126 | }
|
---|
127 | if (exit_code != 0) {
|
---|
128 | fprintf(stderr, "process semaerr1 failed\n");
|
---|
129 | exit(1);
|
---|
130 | }
|
---|
131 | if (PR_CloseSemaphore(sem) == PR_FAILURE) {
|
---|
132 | fprintf(stderr, "PR_CloseSemaphore failed\n");
|
---|
133 | exit(1);
|
---|
134 | }
|
---|
135 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
|
---|
136 | fprintf(stderr, "PR_DeleteSemaphore failed\n");
|
---|
137 | exit(1);
|
---|
138 | }
|
---|
139 |
|
---|
140 | printf("PASS\n");
|
---|
141 | return 0;
|
---|
142 | }
|
---|