tst-sem4.c 3.2 KB

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