tst-cancel19.c 6.9 KB

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