tst-cancel16.c 4.8 KB

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