tst-sem4.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <semaphore.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include "../test-skeleton.h"
  22. #ifdef __ARCH_USE_MMU__
  23. static void
  24. remove_sem (int status, void *arg)
  25. {
  26. sem_unlink (arg);
  27. }
  28. int
  29. do_test (void)
  30. {
  31. #if defined(__GLIBC__) || defined(__UCLIBC__)
  32. sem_t *s;
  33. sem_t *s2;
  34. pid_t pid;
  35. int val;
  36. s = sem_open ("/glibc-tst-sem4", O_CREAT, 0600, 1);
  37. if (s == SEM_FAILED)
  38. {
  39. if (errno == ENOSYS)
  40. {
  41. puts ("sem_open not supported. Oh well.");
  42. return 0;
  43. }
  44. /* Maybe the shm filesystem has strict permissions. */
  45. if (errno == EACCES)
  46. {
  47. puts ("sem_open not allowed. Oh well.");
  48. return 0;
  49. }
  50. printf ("sem_open: %m\n");
  51. return 1;
  52. }
  53. on_exit (remove_sem, (void *) "/glibc-tst-sem4");
  54. /* We have the semaphore object. Now try again with O_EXCL, this
  55. should fail. */
  56. s2 = sem_open ("/glibc-tst-sem4", O_CREAT | O_EXCL, 0600, 1);
  57. if (s2 != SEM_FAILED)
  58. {
  59. puts ("2nd sem_open didn't fail");
  60. return 1;
  61. }
  62. if (errno != EEXIST)
  63. {
  64. puts ("2nd sem_open returned wrong error");
  65. return 1;
  66. }
  67. /* Check the value. */
  68. if (sem_getvalue (s, &val) == -1)
  69. {
  70. puts ("getvalue failed");
  71. return 1;
  72. }
  73. if (val != 1)
  74. {
  75. printf ("initial value wrong: got %d, expected 1\n", val);
  76. return 1;
  77. }
  78. if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
  79. {
  80. puts ("1st sem_wait failed");
  81. return 1;
  82. }
  83. pid = fork ();
  84. if (pid == -1)
  85. {
  86. printf ("fork failed: %m\n");
  87. return 1;
  88. }
  89. if (pid == 0)
  90. {
  91. /* Child. */
  92. /* Check the value. */
  93. if (sem_getvalue (s, &val) == -1)
  94. {
  95. puts ("child: getvalue failed");
  96. return 1;
  97. }
  98. if (val != 0)
  99. {
  100. printf ("child: value wrong: got %d, expect 0\n", val);
  101. return 1;
  102. }
  103. if (sem_post (s) == -1)
  104. {
  105. puts ("child: post failed");
  106. return 1;
  107. }
  108. }
  109. else
  110. {
  111. if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
  112. {
  113. puts ("2nd sem_wait failed");
  114. return 1;
  115. }
  116. if (sem_getvalue (s, &val) == -1)
  117. {
  118. puts ("parent: 2nd getvalue failed");
  119. return 1;
  120. }
  121. if (val != 0)
  122. {
  123. printf ("parent: value wrong: got %d, expected 0\n", val);
  124. return 1;
  125. }
  126. }
  127. return 0;
  128. #else
  129. return 23;
  130. #endif
  131. }
  132. #define TEST_FUNCTION do_test ()
  133. #include "../test-skeleton.c"
  134. #else
  135. int main(void)
  136. {
  137. printf("Skipping test on non-mmu host!\n");
  138. return 23;
  139. }
  140. #endif