semaerr.c 3.5 KB

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