tst-cancel20.c 4.8 KB

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