tst-cancel16.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  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 <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/mman.h>
  22. #include <sys/wait.h>
  23. #include "../test-skeleton.h"
  24. #ifdef __ARCH_USE_MMU__
  25. static pthread_barrier_t b2;
  26. static int fd;
  27. static int called;
  28. static void
  29. cl (void *arg)
  30. {
  31. called = 1;
  32. }
  33. static void *
  34. tf (void *arg)
  35. {
  36. int r = pthread_barrier_wait (&b2);
  37. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  38. {
  39. puts ("child thread: barrier_wait failed");
  40. exit (1);
  41. }
  42. pthread_cleanup_push (cl, NULL);
  43. /* This call should never return. */
  44. (void) lockf (fd, F_LOCK, 0);
  45. pthread_cleanup_pop (0);
  46. return NULL;
  47. }
  48. static int
  49. do_test (void)
  50. {
  51. char fname[] = "/tmp/cancel16XXXXXX";
  52. fd = mkstemp (fname);
  53. if (fd == -1)
  54. {
  55. puts ("mkstemp failed");
  56. return 1;
  57. }
  58. unlink (fname);
  59. char mem[sizeof (pthread_barrier_t)];
  60. memset (mem, '\0', sizeof (mem));
  61. if (TEMP_FAILURE_RETRY (pwrite (fd, mem, sizeof (mem), 0)) != sizeof (mem))
  62. {
  63. puts ("pwrite failed");
  64. return 1;
  65. }
  66. void *p = mmap (NULL, sizeof (mem), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  67. if (p == MAP_FAILED)
  68. {
  69. puts ("mmap failed");
  70. return 1;
  71. }
  72. pthread_barrier_t *b = (pthread_barrier_t *) p;
  73. pthread_barrierattr_t ba;
  74. if (pthread_barrierattr_init (&ba) != 0)
  75. {
  76. puts ("barrierattr_init failed");
  77. return 1;
  78. }
  79. if (pthread_barrierattr_setpshared (&ba, 1) != 0)
  80. {
  81. puts ("barrierattr_setshared failed");
  82. return 1;
  83. }
  84. if (pthread_barrier_init (b, &ba, 2) != 0)
  85. {
  86. puts ("1st barrier_init failed");
  87. return 1;
  88. }
  89. if (pthread_barrierattr_destroy (&ba) != 0)
  90. {
  91. puts ("barrier_destroy failed");
  92. return 1;
  93. }
  94. pid_t pid = fork ();
  95. if (pid == 0)
  96. {
  97. /* Child. Lock the file and wait. */
  98. if (lockf (fd, F_LOCK, 0) != 0)
  99. {
  100. puts ("child process: lockf failed");
  101. _exit (1);
  102. }
  103. int r = pthread_barrier_wait (b);
  104. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  105. {
  106. puts ("child process: 1st barrier_wait failed");
  107. _exit (1);
  108. }
  109. /* Make sure the process dies. */
  110. alarm (5);
  111. r = pthread_barrier_wait (b);
  112. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  113. {
  114. puts ("child process: 2nd barrier_wait failed");
  115. _exit (1);
  116. }
  117. _exit (0);
  118. }
  119. if (pid == -1)
  120. {
  121. puts ("fork failed");
  122. return 1;
  123. }
  124. int r = pthread_barrier_wait (b);
  125. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  126. {
  127. puts ("main: 1st barrier_wait failed");
  128. _exit (1);
  129. }
  130. if (pthread_barrier_init (&b2, NULL, 2) != 0)
  131. {
  132. puts ("2nd barrier_init failed");
  133. return 1;
  134. }
  135. pthread_t th;
  136. if (pthread_create (&th, NULL, tf, NULL) != 0)
  137. {
  138. puts ("create failed");
  139. return 1;
  140. }
  141. r = pthread_barrier_wait (&b2);
  142. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  143. {
  144. puts ("main: 2nd barrier_wait failed");
  145. return 1;
  146. }
  147. /* Delay. */
  148. sleep (1);
  149. if (pthread_cancel (th) != 0)
  150. {
  151. puts ("cancel failed");
  152. return 1;
  153. }
  154. void *result;
  155. if (pthread_join (th, &result) != 0)
  156. {
  157. puts ("join failed");
  158. return 1;
  159. }
  160. if (result != PTHREAD_CANCELED)
  161. {
  162. puts ("thread not canceled");
  163. return 1;
  164. }
  165. if (called == 0)
  166. {
  167. puts ("cleanup handler not called");
  168. return 1;
  169. }
  170. r = pthread_barrier_wait (b);
  171. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  172. {
  173. puts ("main: 3rd barrier_wait failed");
  174. return 1;
  175. }
  176. int status;
  177. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  178. {
  179. puts ("waitpid failed");
  180. return 1;
  181. }
  182. if (WEXITSTATUS (status) != 0)
  183. {
  184. printf ("child process exits with %d\n", WEXITSTATUS (status));
  185. return 1;
  186. }
  187. if (lockf (fd, F_LOCK, 0) != 0)
  188. {
  189. puts ("main: lockf failed");
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. #define TEST_FUNCTION do_test ()
  195. #include "../test-skeleton.c"
  196. #else
  197. int main(void)
  198. {
  199. printf("Skipping test on non-mmu host!\n");
  200. return 23;
  201. }
  202. #endif