tst-sem4.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. static void
  23. remove_sem (int status, void *arg)
  24. {
  25. sem_unlink (arg);
  26. }
  27. int
  28. do_test (void)
  29. {
  30. #if defined(__GLIBC__) || defined(__UCLIBC__)
  31. sem_t *s;
  32. sem_t *s2;
  33. pid_t pid;
  34. int val;
  35. s = sem_open ("/glibc-tst-sem4", O_CREAT, 0600, 1);
  36. if (s == SEM_FAILED)
  37. {
  38. if (errno == ENOSYS)
  39. {
  40. puts ("sem_open not supported. Oh well.");
  41. return 0;
  42. }
  43. /* Maybe the shm filesystem has strict permissions. */
  44. if (errno == EACCES)
  45. {
  46. puts ("sem_open not allowed. Oh well.");
  47. return 0;
  48. }
  49. printf ("sem_open: %m\n");
  50. return 1;
  51. }
  52. on_exit (remove_sem, (void *) "/glibc-tst-sem4");
  53. /* We have the semaphore object. Now try again with O_EXCL, this
  54. should fail. */
  55. s2 = sem_open ("/glibc-tst-sem4", O_CREAT | O_EXCL, 0600, 1);
  56. if (s2 != SEM_FAILED)
  57. {
  58. puts ("2nd sem_open didn't fail");
  59. return 1;
  60. }
  61. if (errno != EEXIST)
  62. {
  63. puts ("2nd sem_open returned wrong error");
  64. return 1;
  65. }
  66. /* Check the value. */
  67. if (sem_getvalue (s, &val) == -1)
  68. {
  69. puts ("getvalue failed");
  70. return 1;
  71. }
  72. if (val != 1)
  73. {
  74. printf ("initial value wrong: got %d, expected 1\n", val);
  75. return 1;
  76. }
  77. if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
  78. {
  79. puts ("1st sem_wait failed");
  80. return 1;
  81. }
  82. pid = fork ();
  83. if (pid == -1)
  84. {
  85. printf ("fork failed: %m\n");
  86. return 1;
  87. }
  88. if (pid == 0)
  89. {
  90. /* Child. */
  91. /* Check the value. */
  92. if (sem_getvalue (s, &val) == -1)
  93. {
  94. puts ("child: getvalue failed");
  95. return 1;
  96. }
  97. if (val != 0)
  98. {
  99. printf ("child: value wrong: got %d, expect 0\n", val);
  100. return 1;
  101. }
  102. if (sem_post (s) == -1)
  103. {
  104. puts ("child: post failed");
  105. return 1;
  106. }
  107. }
  108. else
  109. {
  110. if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
  111. {
  112. puts ("2nd sem_wait failed");
  113. return 1;
  114. }
  115. if (sem_getvalue (s, &val) == -1)
  116. {
  117. puts ("parent: 2nd getvalue failed");
  118. return 1;
  119. }
  120. if (val != 0)
  121. {
  122. printf ("parent: value wrong: got %d, expected 0\n", val);
  123. return 1;
  124. }
  125. }
  126. return 0;
  127. #else
  128. return 23;
  129. #endif
  130. }
  131. #define TEST_FUNCTION do_test ()
  132. #include "../test-skeleton.c"