tst-cancel21.c 5.3 KB

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