tst-cancel20.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@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, 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 <pthread.h>
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. static int fd[4];
  23. static pthread_barrier_t b;
  24. volatile int in_sh_body;
  25. unsigned long cleanups;
  26. static void
  27. cl (void *arg)
  28. {
  29. cleanups = (cleanups << 4) | (long) arg;
  30. }
  31. static void __attribute__((noinline))
  32. sh_body (void)
  33. {
  34. char c;
  35. pthread_cleanup_push (cl, (void *) 1L);
  36. in_sh_body = 1;
  37. if (read (fd[2], &c, 1) == 1)
  38. {
  39. puts ("read succeeded");
  40. exit (1);
  41. }
  42. pthread_cleanup_pop (0);
  43. }
  44. static void
  45. sh (int sig)
  46. {
  47. pthread_cleanup_push (cl, (void *) 2L);
  48. sh_body ();
  49. in_sh_body = 0;
  50. pthread_cleanup_pop (0);
  51. }
  52. static void __attribute__((noinline))
  53. tf_body (void)
  54. {
  55. char c;
  56. pthread_cleanup_push (cl, (void *) 3L);
  57. int r = pthread_barrier_wait (&b);
  58. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  59. {
  60. puts ("child thread: barrier_wait failed");
  61. exit (1);
  62. }
  63. if (read (fd[0], &c, 1) == 1)
  64. {
  65. puts ("read succeeded");
  66. exit (1);
  67. }
  68. read (fd[0], &c, 1);
  69. pthread_cleanup_pop (0);
  70. }
  71. static void *
  72. tf (void *arg)
  73. {
  74. pthread_cleanup_push (cl, (void *) 4L);
  75. tf_body ();
  76. pthread_cleanup_pop (0);
  77. return NULL;
  78. }
  79. static int
  80. do_one_test (void)
  81. {
  82. in_sh_body = 0;
  83. cleanups = 0;
  84. if (pipe (fd) != 0 || pipe (fd + 2) != 0)
  85. {
  86. puts ("pipe failed");
  87. return 1;
  88. }
  89. pthread_t th;
  90. if (pthread_create (&th, NULL, tf, NULL) != 0)
  91. {
  92. puts ("create failed");
  93. return 1;
  94. }
  95. int r = pthread_barrier_wait (&b);
  96. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  97. {
  98. puts ("parent thread: barrier_wait failed");
  99. return 1;
  100. }
  101. sleep (1);
  102. r = pthread_kill (th, SIGHUP);
  103. if (r)
  104. {
  105. errno = r;
  106. printf ("pthread_kill failed %m\n");
  107. return 1;
  108. }
  109. while (in_sh_body == 0)
  110. sleep (1);
  111. if (pthread_cancel (th) != 0)
  112. {
  113. puts ("cancel failed");
  114. return 1;
  115. }
  116. /* This will cause the read in the child to return. */
  117. close (fd[0]);
  118. close (fd[1]);
  119. close (fd[2]);
  120. close (fd[3]);
  121. void *ret;
  122. if (pthread_join (th, &ret) != 0)
  123. {
  124. puts ("join failed");
  125. return 1;
  126. }
  127. if (ret != PTHREAD_CANCELED)
  128. {
  129. puts ("result is wrong");
  130. return 1;
  131. }
  132. if (cleanups != 0x1234L)
  133. {
  134. printf ("called cleanups %lx\n", cleanups);
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. static int
  140. do_test (void)
  141. {
  142. stack_t ss;
  143. ss.ss_sp = malloc (2 * SIGSTKSZ);
  144. if (ss.ss_sp == NULL)
  145. {
  146. puts ("failed to allocate alternate stack");
  147. return 1;
  148. }
  149. ss.ss_flags = 0;
  150. ss.ss_size = 2 * SIGSTKSZ;
  151. if (sigaltstack (&ss, NULL) < 0)
  152. {
  153. printf ("sigaltstack failed %m\n");
  154. return 1;
  155. }
  156. if (pthread_barrier_init (&b, NULL, 2) != 0)
  157. {
  158. puts ("barrier_init failed");
  159. return 1;
  160. }
  161. struct sigaction sa;
  162. sa.sa_handler = sh;
  163. sigemptyset (&sa.sa_mask);
  164. sa.sa_flags = 0;
  165. if (sigaction (SIGHUP, &sa, NULL) != 0)
  166. {
  167. puts ("sigaction failed");
  168. return 1;
  169. }
  170. puts ("sa_flags = 0 test");
  171. if (do_one_test ())
  172. return 1;
  173. sa.sa_handler = sh;
  174. sigemptyset (&sa.sa_mask);
  175. sa.sa_flags = SA_ONSTACK;
  176. if (sigaction (SIGHUP, &sa, NULL) != 0)
  177. {
  178. puts ("sigaction failed");
  179. return 1;
  180. }
  181. puts ("sa_flags = SA_ONSTACK test");
  182. if (do_one_test ())
  183. return 1;
  184. sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
  185. sigemptyset (&sa.sa_mask);
  186. sa.sa_flags = SA_SIGINFO;
  187. if (sigaction (SIGHUP, &sa, NULL) != 0)
  188. {
  189. puts ("sigaction failed");
  190. return 1;
  191. }
  192. puts ("sa_flags = SA_SIGINFO test");
  193. if (do_one_test ())
  194. return 1;
  195. sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
  196. sigemptyset (&sa.sa_mask);
  197. sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
  198. if (sigaction (SIGHUP, &sa, NULL) != 0)
  199. {
  200. puts ("sigaction failed");
  201. return 1;
  202. }
  203. puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
  204. if (do_one_test ())
  205. return 1;
  206. return 0;
  207. }
  208. #define TIMEOUT 40
  209. #define TEST_FUNCTION do_test ()
  210. #include "../test-skeleton.c"