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