semaerr1.c 3.2 KB

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