Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nspr.h" |
michael@0 | 7 | #include "plgetopt.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <stdio.h> |
michael@0 | 10 | |
michael@0 | 11 | #ifdef SYMBIAN |
michael@0 | 12 | #define NO_SUCH_SEM_NAME "c:\\data\\nosuchsem.sem" |
michael@0 | 13 | #define SEM_NAME1 "c:\\data\\foo.sem" |
michael@0 | 14 | #define EXE_NAME "nspr_tests_semaerr1.exe" |
michael@0 | 15 | #else |
michael@0 | 16 | #define NO_SUCH_SEM_NAME "/tmp/nosuchsem.sem" |
michael@0 | 17 | #define SEM_NAME1 "/tmp/foo.sem" |
michael@0 | 18 | #define EXE_NAME "semaerr1" |
michael@0 | 19 | #endif |
michael@0 | 20 | #define SEM_MODE 0666 |
michael@0 | 21 | |
michael@0 | 22 | static PRBool debug_mode = PR_FALSE; |
michael@0 | 23 | |
michael@0 | 24 | static void Help(void) |
michael@0 | 25 | { |
michael@0 | 26 | fprintf(stderr, "semaerr test program usage:\n"); |
michael@0 | 27 | fprintf(stderr, "\t-d debug mode (FALSE)\n"); |
michael@0 | 28 | fprintf(stderr, "\t-h this message\n"); |
michael@0 | 29 | } /* Help */ |
michael@0 | 30 | |
michael@0 | 31 | int main(int argc, char **argv) |
michael@0 | 32 | { |
michael@0 | 33 | PLOptStatus os; |
michael@0 | 34 | PLOptState *opt = PL_CreateOptState(argc, argv, "dh"); |
michael@0 | 35 | PRSem *sem; |
michael@0 | 36 | char *child_argv[32]; |
michael@0 | 37 | char **child_arg; |
michael@0 | 38 | PRProcess *proc; |
michael@0 | 39 | PRInt32 exit_code; |
michael@0 | 40 | |
michael@0 | 41 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
michael@0 | 42 | if (PL_OPT_BAD == os) continue; |
michael@0 | 43 | switch (opt->option) { |
michael@0 | 44 | case 'd': /* debug mode */ |
michael@0 | 45 | debug_mode = PR_TRUE; |
michael@0 | 46 | break; |
michael@0 | 47 | case 'h': |
michael@0 | 48 | default: |
michael@0 | 49 | Help(); |
michael@0 | 50 | return 2; |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | PL_DestroyOptState(opt); |
michael@0 | 54 | |
michael@0 | 55 | /* |
michael@0 | 56 | * Open a nonexistent semaphore without the PR_SEM_CREATE |
michael@0 | 57 | * flag should fail with PR_FILE_NOT_FOUND_ERROR. |
michael@0 | 58 | */ |
michael@0 | 59 | (void) PR_DeleteSemaphore(NO_SUCH_SEM_NAME); |
michael@0 | 60 | sem = PR_OpenSemaphore(NO_SUCH_SEM_NAME, 0, 0, 0); |
michael@0 | 61 | if (NULL != sem) { |
michael@0 | 62 | fprintf(stderr, "Opening nonexistent semaphore %s " |
michael@0 | 63 | "without the PR_SEM_CREATE flag should fail " |
michael@0 | 64 | "but succeeded\n", NO_SUCH_SEM_NAME); |
michael@0 | 65 | exit(1); |
michael@0 | 66 | } |
michael@0 | 67 | if (PR_GetError() != PR_FILE_NOT_FOUND_ERROR) { |
michael@0 | 68 | fprintf(stderr, "Expected error is %d but got (%d, %d)\n", |
michael@0 | 69 | PR_FILE_NOT_FOUND_ERROR, PR_GetError(), PR_GetOSError()); |
michael@0 | 70 | exit(1); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | * Create a semaphore and let the another process |
michael@0 | 75 | * try PR_SEM_CREATE and PR_SEM_CREATE|PR_SEM_EXCL. |
michael@0 | 76 | */ |
michael@0 | 77 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) { |
michael@0 | 78 | fprintf(stderr, "warning: deleted semaphore %s from previous " |
michael@0 | 79 | "run of the test\n", SEM_NAME1); |
michael@0 | 80 | } |
michael@0 | 81 | sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0); |
michael@0 | 82 | if (sem == NULL) { |
michael@0 | 83 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
michael@0 | 84 | PR_GetError(), PR_GetOSError()); |
michael@0 | 85 | exit(1); |
michael@0 | 86 | } |
michael@0 | 87 | child_arg = child_argv; |
michael@0 | 88 | *child_arg++ = EXE_NAME; |
michael@0 | 89 | if (debug_mode) { |
michael@0 | 90 | *child_arg++ = "-d"; |
michael@0 | 91 | } |
michael@0 | 92 | *child_arg = NULL; |
michael@0 | 93 | proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL); |
michael@0 | 94 | if (proc == NULL) { |
michael@0 | 95 | fprintf(stderr, "PR_CreateProcess failed\n"); |
michael@0 | 96 | exit(1); |
michael@0 | 97 | } |
michael@0 | 98 | if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) { |
michael@0 | 99 | fprintf(stderr, "PR_WaitProcess failed\n"); |
michael@0 | 100 | exit(1); |
michael@0 | 101 | } |
michael@0 | 102 | if (exit_code != 0) { |
michael@0 | 103 | fprintf(stderr, "process semaerr1 failed\n"); |
michael@0 | 104 | exit(1); |
michael@0 | 105 | } |
michael@0 | 106 | if (PR_CloseSemaphore(sem) == PR_FAILURE) { |
michael@0 | 107 | fprintf(stderr, "PR_CloseSemaphore failed\n"); |
michael@0 | 108 | exit(1); |
michael@0 | 109 | } |
michael@0 | 110 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) { |
michael@0 | 111 | fprintf(stderr, "PR_DeleteSemaphore failed\n"); |
michael@0 | 112 | exit(1); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | printf("PASS\n"); |
michael@0 | 116 | return 0; |
michael@0 | 117 | } |