tst-cancel19.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Copyright (C) 2003 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 <error.h>
  17. #include <fcntl.h>
  18. #include <pthread.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/select.h>
  24. #include <sys/time.h>
  25. #include <unistd.h>
  26. static void *
  27. tf (void *arg)
  28. {
  29. return NULL;
  30. }
  31. static void
  32. handler (int sig)
  33. {
  34. }
  35. static void __attribute__ ((noinline))
  36. clobber_lots_of_regs (void)
  37. {
  38. #define X1(n) long r##n = 10##n; __asm__ __volatile__ ("" : "+r" (r##n));
  39. #define X2(n) X1(n##0) X1(n##1) X1(n##2) X1(n##3) X1(n##4)
  40. #define X3(n) X2(n##0) X2(n##1) X2(n##2) X2(n##3) X2(n##4)
  41. X3(0) X3(1) X3(2) X3(3) X3(4)
  42. #undef X1
  43. #define X1(n) __asm__ __volatile__ ("" : : "r" (r##n));
  44. X3(0) X3(1) X3(2) X3(3) X3(4)
  45. #undef X1
  46. #undef X2
  47. #undef X3
  48. }
  49. static int
  50. do_test (void)
  51. {
  52. pthread_t th;
  53. int old, rc;
  54. int ret = 0;
  55. int fd[2];
  56. rc = pipe (fd);
  57. if (rc < 0)
  58. error (EXIT_FAILURE, errno, "couldn't create pipe");
  59. rc = pthread_create (&th, NULL, tf, NULL);
  60. if (rc)
  61. error (EXIT_FAILURE, rc, "couldn't create thread");
  62. rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
  63. if (rc)
  64. {
  65. error (0, rc, "1st pthread_setcanceltype failed");
  66. ret = 1;
  67. }
  68. if (old != PTHREAD_CANCEL_DEFERRED && old != PTHREAD_CANCEL_ASYNCHRONOUS)
  69. {
  70. error (0, 0, "1st pthread_setcanceltype returned invalid value %d",
  71. old);
  72. ret = 1;
  73. }
  74. clobber_lots_of_regs ();
  75. close (fd[0]);
  76. rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
  77. if (rc)
  78. {
  79. error (0, rc, "pthread_setcanceltype after close failed");
  80. ret = 1;
  81. }
  82. if (old != PTHREAD_CANCEL_DEFERRED)
  83. {
  84. error (0, 0, "pthread_setcanceltype after close returned invalid value %d",
  85. old);
  86. ret = 1;
  87. }
  88. clobber_lots_of_regs ();
  89. close (fd[1]);
  90. rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
  91. if (rc)
  92. {
  93. error (0, rc, "pthread_setcanceltype after 2nd close failed");
  94. ret = 1;
  95. }
  96. if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
  97. {
  98. error (0, 0, "pthread_setcanceltype after 2nd close returned invalid value %d",
  99. old);
  100. ret = 1;
  101. }
  102. struct sigaction sa = { .sa_handler = handler, .sa_flags = 0 };
  103. sigemptyset (&sa.sa_mask);
  104. sigaction (SIGALRM, &sa, NULL);
  105. struct itimerval it;
  106. it.it_value.tv_sec = 1;
  107. it.it_value.tv_usec = 0;
  108. it.it_interval = it.it_value;
  109. setitimer (ITIMER_REAL, &it, NULL);
  110. clobber_lots_of_regs ();
  111. pause ();
  112. memset (&it, 0, sizeof (it));
  113. setitimer (ITIMER_REAL, &it, NULL);
  114. rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
  115. if (rc)
  116. {
  117. error (0, rc, "pthread_setcanceltype after pause failed");
  118. ret = 1;
  119. }
  120. if (old != PTHREAD_CANCEL_DEFERRED)
  121. {
  122. error (0, 0, "pthread_setcanceltype after pause returned invalid value %d",
  123. old);
  124. ret = 1;
  125. }
  126. it.it_value.tv_sec = 1;
  127. it.it_value.tv_usec = 0;
  128. it.it_interval = it.it_value;
  129. setitimer (ITIMER_REAL, &it, NULL);
  130. clobber_lots_of_regs ();
  131. pause ();
  132. memset (&it, 0, sizeof (it));
  133. setitimer (ITIMER_REAL, &it, NULL);
  134. rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
  135. if (rc)
  136. {
  137. error (0, rc, "pthread_setcanceltype after 2nd pause failed");
  138. ret = 1;
  139. }
  140. if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
  141. {
  142. error (0, 0, "pthread_setcanceltype after 2nd pause returned invalid value %d",
  143. old);
  144. ret = 1;
  145. }
  146. char fname[] = "/tmp/tst-cancel19-dir-XXXXXX\0foo/bar";
  147. char *enddir = strchr (fname, '\0');
  148. if (mkdtemp (fname) == NULL)
  149. {
  150. error (0, errno, "mkdtemp failed");
  151. ret = 1;
  152. }
  153. *enddir = '/';
  154. clobber_lots_of_regs ();
  155. creat (fname, 0400);
  156. rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
  157. if (rc)
  158. {
  159. error (0, rc, "pthread_setcanceltype after creat failed");
  160. ret = 1;
  161. }
  162. if (old != PTHREAD_CANCEL_DEFERRED)
  163. {
  164. error (0, 0, "pthread_setcanceltype after creat returned invalid value %d",
  165. old);
  166. ret = 1;
  167. }
  168. clobber_lots_of_regs ();
  169. creat (fname, 0400);
  170. rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
  171. if (rc)
  172. {
  173. error (0, rc, "pthread_setcanceltype after 2nd creat failed");
  174. ret = 1;
  175. }
  176. if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
  177. {
  178. error (0, 0, "pthread_setcanceltype after 2nd creat returned invalid value %d",
  179. old);
  180. ret = 1;
  181. }
  182. clobber_lots_of_regs ();
  183. open (fname, O_CREAT, 0400);
  184. rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
  185. if (rc)
  186. {
  187. error (0, rc, "pthread_setcanceltype after open failed");
  188. ret = 1;
  189. }
  190. if (old != PTHREAD_CANCEL_DEFERRED)
  191. {
  192. error (0, 0, "pthread_setcanceltype after open returned invalid value %d",
  193. old);
  194. ret = 1;
  195. }
  196. clobber_lots_of_regs ();
  197. open (fname, O_CREAT, 0400);
  198. rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
  199. if (rc)
  200. {
  201. error (0, rc, "pthread_setcanceltype after 2nd open failed");
  202. ret = 1;
  203. }
  204. if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
  205. {
  206. error (0, 0, "pthread_setcanceltype after 2nd open returned invalid value %d",
  207. old);
  208. ret = 1;
  209. }
  210. *enddir = '\0';
  211. rmdir (fname);
  212. clobber_lots_of_regs ();
  213. select (-1, NULL, NULL, NULL, NULL);
  214. rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
  215. if (rc)
  216. {
  217. error (0, rc, "pthread_setcanceltype after select failed");
  218. ret = 1;
  219. }
  220. if (old != PTHREAD_CANCEL_DEFERRED)
  221. {
  222. error (0, 0, "pthread_setcanceltype after select returned invalid value %d",
  223. old);
  224. ret = 1;
  225. }
  226. clobber_lots_of_regs ();
  227. select (-1, NULL, NULL, NULL, NULL);
  228. rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
  229. if (rc)
  230. {
  231. error (0, rc, "pthread_setcanceltype after 2nd select failed");
  232. ret = 1;
  233. }
  234. if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
  235. {
  236. error (0, 0, "pthread_setcanceltype after 2nd select returned invalid value %d",
  237. old);
  238. ret = 1;
  239. }
  240. pthread_join (th, NULL);
  241. return ret;
  242. }
  243. #define TIMEOUT 20
  244. #define TEST_FUNCTION do_test ()
  245. #include "../test-skeleton.c"