tst-sem3.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <semaphore.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/mman.h>
  23. #include <sys/wait.h>
  24. #include "../test-skeleton.h"
  25. #ifdef __ARCH_USE_MMU__
  26. int
  27. do_test (void)
  28. {
  29. size_t ps = sysconf (_SC_PAGESIZE);
  30. char tmpfname[] = "/tmp/tst-sem3.XXXXXX";
  31. char data[ps];
  32. void *mem;
  33. int fd;
  34. sem_t *s;
  35. pid_t pid;
  36. char *p;
  37. fd = mkstemp (tmpfname);
  38. if (fd == -1)
  39. {
  40. printf ("cannot open temporary file: %m\n");
  41. return 1;
  42. }
  43. /* Make sure it is always removed. */
  44. unlink (tmpfname);
  45. /* Create one page of data. */
  46. memset (data, '\0', ps);
  47. /* Write the data to the file. */
  48. if (write (fd, data, ps) != (ssize_t) ps)
  49. {
  50. puts ("short write");
  51. return 1;
  52. }
  53. mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  54. if (mem == MAP_FAILED)
  55. {
  56. printf ("mmap failed: %m\n");
  57. return 1;
  58. }
  59. s = (sem_t *) (((uintptr_t) mem + __alignof (sem_t))
  60. & ~(__alignof (sem_t) - 1));
  61. p = (char *) (s + 1);
  62. if (sem_init (s, 1, 1) == -1)
  63. {
  64. puts ("init failed");
  65. return 1;
  66. }
  67. if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
  68. {
  69. puts ("1st wait failed");
  70. return 1;
  71. }
  72. errno = 0;
  73. if (TEMP_FAILURE_RETRY (sem_trywait (s)) != -1)
  74. {
  75. puts ("trywait succeeded");
  76. return 1;
  77. }
  78. else if (errno != EAGAIN)
  79. {
  80. puts ("trywait didn't return EAGAIN");
  81. return 1;
  82. }
  83. *p = 0;
  84. puts ("going to fork now");
  85. pid = fork ();
  86. if (pid == -1)
  87. {
  88. puts ("fork failed");
  89. return 1;
  90. }
  91. else if (pid == 0)
  92. {
  93. /* Play some lock ping-pong. It's our turn to unlock first. */
  94. if ((*p)++ != 0)
  95. {
  96. puts ("child: *p != 0");
  97. return 1;
  98. }
  99. if (sem_post (s) == -1)
  100. {
  101. puts ("child: 1st post failed");
  102. return 1;
  103. }
  104. puts ("child done");
  105. }
  106. else
  107. {
  108. if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
  109. {
  110. printf ("parent: 2nd wait failed: %m\n");
  111. return 1;
  112. }
  113. if (*p != 1)
  114. {
  115. puts ("*p != 1");
  116. return 1;
  117. }
  118. puts ("parent done");
  119. }
  120. return 0;
  121. }
  122. #define TEST_FUNCTION do_test ()
  123. #include "../test-skeleton.c"
  124. #else
  125. int main(void)
  126. {
  127. printf("Skipping test on non-mmu host!\n");
  128. return 23;
  129. }
  130. #endif