tst-sem4.c 3.0 KB

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