tst-sem4.c 3.0 KB

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