1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/semaerr.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nspr.h" 1.10 +#include "plgetopt.h" 1.11 + 1.12 +#include <stdio.h> 1.13 + 1.14 +#ifdef SYMBIAN 1.15 +#define NO_SUCH_SEM_NAME "c:\\data\\nosuchsem.sem" 1.16 +#define SEM_NAME1 "c:\\data\\foo.sem" 1.17 +#define EXE_NAME "nspr_tests_semaerr1.exe" 1.18 +#else 1.19 +#define NO_SUCH_SEM_NAME "/tmp/nosuchsem.sem" 1.20 +#define SEM_NAME1 "/tmp/foo.sem" 1.21 +#define EXE_NAME "semaerr1" 1.22 +#endif 1.23 +#define SEM_MODE 0666 1.24 + 1.25 +static PRBool debug_mode = PR_FALSE; 1.26 + 1.27 +static void Help(void) 1.28 +{ 1.29 + fprintf(stderr, "semaerr test program usage:\n"); 1.30 + fprintf(stderr, "\t-d debug mode (FALSE)\n"); 1.31 + fprintf(stderr, "\t-h this message\n"); 1.32 +} /* Help */ 1.33 + 1.34 +int main(int argc, char **argv) 1.35 +{ 1.36 + PLOptStatus os; 1.37 + PLOptState *opt = PL_CreateOptState(argc, argv, "dh"); 1.38 + PRSem *sem; 1.39 + char *child_argv[32]; 1.40 + char **child_arg; 1.41 + PRProcess *proc; 1.42 + PRInt32 exit_code; 1.43 + 1.44 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { 1.45 + if (PL_OPT_BAD == os) continue; 1.46 + switch (opt->option) { 1.47 + case 'd': /* debug mode */ 1.48 + debug_mode = PR_TRUE; 1.49 + break; 1.50 + case 'h': 1.51 + default: 1.52 + Help(); 1.53 + return 2; 1.54 + } 1.55 + } 1.56 + PL_DestroyOptState(opt); 1.57 + 1.58 + /* 1.59 + * Open a nonexistent semaphore without the PR_SEM_CREATE 1.60 + * flag should fail with PR_FILE_NOT_FOUND_ERROR. 1.61 + */ 1.62 + (void) PR_DeleteSemaphore(NO_SUCH_SEM_NAME); 1.63 + sem = PR_OpenSemaphore(NO_SUCH_SEM_NAME, 0, 0, 0); 1.64 + if (NULL != sem) { 1.65 + fprintf(stderr, "Opening nonexistent semaphore %s " 1.66 + "without the PR_SEM_CREATE flag should fail " 1.67 + "but succeeded\n", NO_SUCH_SEM_NAME); 1.68 + exit(1); 1.69 + } 1.70 + if (PR_GetError() != PR_FILE_NOT_FOUND_ERROR) { 1.71 + fprintf(stderr, "Expected error is %d but got (%d, %d)\n", 1.72 + PR_FILE_NOT_FOUND_ERROR, PR_GetError(), PR_GetOSError()); 1.73 + exit(1); 1.74 + } 1.75 + 1.76 + /* 1.77 + * Create a semaphore and let the another process 1.78 + * try PR_SEM_CREATE and PR_SEM_CREATE|PR_SEM_EXCL. 1.79 + */ 1.80 + if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) { 1.81 + fprintf(stderr, "warning: deleted semaphore %s from previous " 1.82 + "run of the test\n", SEM_NAME1); 1.83 + } 1.84 + sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0); 1.85 + if (sem == NULL) { 1.86 + fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", 1.87 + PR_GetError(), PR_GetOSError()); 1.88 + exit(1); 1.89 + } 1.90 + child_arg = child_argv; 1.91 + *child_arg++ = EXE_NAME; 1.92 + if (debug_mode) { 1.93 + *child_arg++ = "-d"; 1.94 + } 1.95 + *child_arg = NULL; 1.96 + proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL); 1.97 + if (proc == NULL) { 1.98 + fprintf(stderr, "PR_CreateProcess failed\n"); 1.99 + exit(1); 1.100 + } 1.101 + if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) { 1.102 + fprintf(stderr, "PR_WaitProcess failed\n"); 1.103 + exit(1); 1.104 + } 1.105 + if (exit_code != 0) { 1.106 + fprintf(stderr, "process semaerr1 failed\n"); 1.107 + exit(1); 1.108 + } 1.109 + if (PR_CloseSemaphore(sem) == PR_FAILURE) { 1.110 + fprintf(stderr, "PR_CloseSemaphore failed\n"); 1.111 + exit(1); 1.112 + } 1.113 + if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) { 1.114 + fprintf(stderr, "PR_DeleteSemaphore failed\n"); 1.115 + exit(1); 1.116 + } 1.117 + 1.118 + printf("PASS\n"); 1.119 + return 0; 1.120 +}