tst-cancel21.c 5.4 KB

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