michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nspr.h" michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef SYMBIAN michael@0: #define NO_SUCH_SEM_NAME "c:\\data\\nosuchsem.sem" michael@0: #define SEM_NAME1 "c:\\data\\foo.sem" michael@0: #define EXE_NAME "nspr_tests_semaerr1.exe" michael@0: #else michael@0: #define NO_SUCH_SEM_NAME "/tmp/nosuchsem.sem" michael@0: #define SEM_NAME1 "/tmp/foo.sem" michael@0: #define EXE_NAME "semaerr1" michael@0: #endif michael@0: #define SEM_MODE 0666 michael@0: michael@0: static PRBool debug_mode = PR_FALSE; michael@0: michael@0: static void Help(void) michael@0: { michael@0: fprintf(stderr, "semaerr test program usage:\n"); michael@0: fprintf(stderr, "\t-d debug mode (FALSE)\n"); michael@0: fprintf(stderr, "\t-h this message\n"); michael@0: } /* Help */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dh"); michael@0: PRSem *sem; michael@0: char *child_argv[32]; michael@0: char **child_arg; michael@0: PRProcess *proc; michael@0: PRInt32 exit_code; michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = PR_TRUE; michael@0: break; michael@0: case 'h': michael@0: default: michael@0: Help(); michael@0: return 2; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: /* michael@0: * Open a nonexistent semaphore without the PR_SEM_CREATE michael@0: * flag should fail with PR_FILE_NOT_FOUND_ERROR. michael@0: */ michael@0: (void) PR_DeleteSemaphore(NO_SUCH_SEM_NAME); michael@0: sem = PR_OpenSemaphore(NO_SUCH_SEM_NAME, 0, 0, 0); michael@0: if (NULL != sem) { michael@0: fprintf(stderr, "Opening nonexistent semaphore %s " michael@0: "without the PR_SEM_CREATE flag should fail " michael@0: "but succeeded\n", NO_SUCH_SEM_NAME); michael@0: exit(1); michael@0: } michael@0: if (PR_GetError() != PR_FILE_NOT_FOUND_ERROR) { michael@0: fprintf(stderr, "Expected error is %d but got (%d, %d)\n", michael@0: PR_FILE_NOT_FOUND_ERROR, PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: michael@0: /* michael@0: * Create a semaphore and let the another process michael@0: * try PR_SEM_CREATE and PR_SEM_CREATE|PR_SEM_EXCL. michael@0: */ michael@0: if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) { michael@0: fprintf(stderr, "warning: deleted semaphore %s from previous " michael@0: "run of the test\n", SEM_NAME1); michael@0: } michael@0: sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0); michael@0: if (sem == NULL) { michael@0: fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: child_arg = child_argv; michael@0: *child_arg++ = EXE_NAME; michael@0: if (debug_mode) { michael@0: *child_arg++ = "-d"; michael@0: } michael@0: *child_arg = NULL; michael@0: proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL); michael@0: if (proc == NULL) { michael@0: fprintf(stderr, "PR_CreateProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_WaitProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: if (exit_code != 0) { michael@0: fprintf(stderr, "process semaerr1 failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_CloseSemaphore(sem) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CloseSemaphore failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_DeleteSemaphore failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }