tst-cancel16.c 4.7 KB

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