semaerr1.c (3045B)
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 SEM_NAME1 "/tmp/foo.sem" SEM_D SEM_64 23 #define SEM_NAME2 "/tmp/bar.sem" SEM_D SEM_64 24 #define SEM_MODE 0666 25 26 static PRBool debug_mode = PR_FALSE; 27 28 static void Help(void) { 29 fprintf(stderr, "semaerr1 test program usage:\n"); 30 fprintf(stderr, "\t-d debug mode (FALSE)\n"); 31 fprintf(stderr, "\t-h this message\n"); 32 } /* Help */ 33 34 int main(int argc, char** argv) { 35 PLOptStatus os; 36 PLOptState* opt = PL_CreateOptState(argc, argv, "dh"); 37 PRSem* sem; 38 39 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { 40 if (PL_OPT_BAD == os) { 41 continue; 42 } 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 * PR_SEM_CREATE|PR_SEM_EXCL should be able to 57 * create a nonexistent semaphore. 58 */ 59 (void)PR_DeleteSemaphore(SEM_NAME2); 60 sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE | PR_SEM_EXCL, SEM_MODE, 0); 61 if (sem == NULL) { 62 fprintf(stderr, "PR_OpenSemaphore failed\n"); 63 exit(1); 64 } 65 if (PR_CloseSemaphore(sem) == PR_FAILURE) { 66 fprintf(stderr, "PR_CloseSemaphore failed\n"); 67 exit(1); 68 } 69 if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) { 70 fprintf(stderr, "PR_DeleteSemaphore failed\n"); 71 exit(1); 72 } 73 74 /* 75 * Opening an existing semaphore with PR_SEM_CREATE|PR_SEM_EXCL. 76 * should fail with PR_FILE_EXISTS_ERROR. 77 */ 78 sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE | PR_SEM_EXCL, SEM_MODE, 0); 79 if (sem != NULL) { 80 fprintf(stderr, "PR_OpenSemaphore should fail but succeeded\n"); 81 exit(1); 82 } 83 if (PR_GetError() != PR_FILE_EXISTS_ERROR) { 84 fprintf(stderr, "Expect %d but got %d\n", PR_FILE_EXISTS_ERROR, 85 PR_GetError()); 86 exit(1); 87 } 88 89 /* 90 * Try again, with just PR_SEM_CREATE. This should succeed. 91 */ 92 sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0); 93 if (sem == NULL) { 94 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", PR_GetError(), 95 PR_GetOSError()); 96 exit(1); 97 } 98 if (PR_CloseSemaphore(sem) == PR_FAILURE) { 99 fprintf(stderr, "PR_CloseSemaphore failed\n"); 100 exit(1); 101 } 102 103 sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE | PR_SEM_EXCL, SEM_MODE, 0); 104 if (sem == NULL) { 105 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", PR_GetError(), 106 PR_GetOSError()); 107 exit(1); 108 } 109 if (PR_CloseSemaphore(sem) == PR_FAILURE) { 110 fprintf(stderr, "PR_CloseSemaphore failed\n"); 111 exit(1); 112 } 113 114 printf("PASS\n"); 115 return 0; 116 }