nsprpub/pr/tests/semaerr.c

branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
equal deleted inserted replaced
-1:000000000000 0:84b1d1f814d3
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 NO_SUCH_SEM_NAME "c:\\data\\nosuchsem.sem"
13 #define SEM_NAME1 "c:\\data\\foo.sem"
14 #define EXE_NAME "nspr_tests_semaerr1.exe"
15 #else
16 #define NO_SUCH_SEM_NAME "/tmp/nosuchsem.sem"
17 #define SEM_NAME1 "/tmp/foo.sem"
18 #define EXE_NAME "semaerr1"
19 #endif
20 #define SEM_MODE 0666
21
22 static PRBool debug_mode = PR_FALSE;
23
24 static void Help(void)
25 {
26 fprintf(stderr, "semaerr test program usage:\n");
27 fprintf(stderr, "\t-d debug mode (FALSE)\n");
28 fprintf(stderr, "\t-h this message\n");
29 } /* Help */
30
31 int main(int argc, char **argv)
32 {
33 PLOptStatus os;
34 PLOptState *opt = PL_CreateOptState(argc, argv, "dh");
35 PRSem *sem;
36 char *child_argv[32];
37 char **child_arg;
38 PRProcess *proc;
39 PRInt32 exit_code;
40
41 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
42 if (PL_OPT_BAD == os) continue;
43 switch (opt->option) {
44 case 'd': /* debug mode */
45 debug_mode = PR_TRUE;
46 break;
47 case 'h':
48 default:
49 Help();
50 return 2;
51 }
52 }
53 PL_DestroyOptState(opt);
54
55 /*
56 * Open a nonexistent semaphore without the PR_SEM_CREATE
57 * flag should fail with PR_FILE_NOT_FOUND_ERROR.
58 */
59 (void) PR_DeleteSemaphore(NO_SUCH_SEM_NAME);
60 sem = PR_OpenSemaphore(NO_SUCH_SEM_NAME, 0, 0, 0);
61 if (NULL != sem) {
62 fprintf(stderr, "Opening nonexistent semaphore %s "
63 "without the PR_SEM_CREATE flag should fail "
64 "but succeeded\n", NO_SUCH_SEM_NAME);
65 exit(1);
66 }
67 if (PR_GetError() != PR_FILE_NOT_FOUND_ERROR) {
68 fprintf(stderr, "Expected error is %d but got (%d, %d)\n",
69 PR_FILE_NOT_FOUND_ERROR, PR_GetError(), PR_GetOSError());
70 exit(1);
71 }
72
73 /*
74 * Create a semaphore and let the another process
75 * try PR_SEM_CREATE and PR_SEM_CREATE|PR_SEM_EXCL.
76 */
77 if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
78 fprintf(stderr, "warning: deleted semaphore %s from previous "
79 "run of the test\n", SEM_NAME1);
80 }
81 sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0);
82 if (sem == NULL) {
83 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
84 PR_GetError(), PR_GetOSError());
85 exit(1);
86 }
87 child_arg = child_argv;
88 *child_arg++ = EXE_NAME;
89 if (debug_mode) {
90 *child_arg++ = "-d";
91 }
92 *child_arg = NULL;
93 proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
94 if (proc == NULL) {
95 fprintf(stderr, "PR_CreateProcess failed\n");
96 exit(1);
97 }
98 if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
99 fprintf(stderr, "PR_WaitProcess failed\n");
100 exit(1);
101 }
102 if (exit_code != 0) {
103 fprintf(stderr, "process semaerr1 failed\n");
104 exit(1);
105 }
106 if (PR_CloseSemaphore(sem) == PR_FAILURE) {
107 fprintf(stderr, "PR_CloseSemaphore failed\n");
108 exit(1);
109 }
110 if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
111 fprintf(stderr, "PR_DeleteSemaphore failed\n");
112 exit(1);
113 }
114
115 printf("PASS\n");
116 return 0;
117 }

mercurial