tst-mqueue8.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright (C) 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  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 <mqueue.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #if _POSIX_THREADS
  23. # include <pthread.h>
  24. static pthread_barrier_t b;
  25. /* Cleanup handling test. */
  26. static int cl_called;
  27. static void
  28. cl (void *arg)
  29. {
  30. ++cl_called;
  31. }
  32. #define TF_MQ_RECEIVE 0L
  33. #define TF_MQ_TIMEDRECEIVE 1L
  34. #define TF_MQ_SEND 2L
  35. #define TF_MQ_TIMEDSEND 3L
  36. static const char *names[]
  37. = { "mq_receive", "mq_timedreceive", "mq_send", "mq_timedsend" };
  38. static mqd_t q;
  39. static struct timespec never;
  40. static void *
  41. tf (void *arg)
  42. {
  43. int r = pthread_barrier_wait (&b);
  44. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  45. {
  46. puts ("tf: barrier_wait failed");
  47. exit (1);
  48. }
  49. pthread_cleanup_push (cl, NULL);
  50. char c = ' ';
  51. switch ((long) arg)
  52. {
  53. case TF_MQ_SEND:
  54. TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1));
  55. break;
  56. case TF_MQ_TIMEDSEND:
  57. TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never));
  58. break;
  59. case TF_MQ_RECEIVE:
  60. TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
  61. break;
  62. case TF_MQ_TIMEDRECEIVE:
  63. TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never));
  64. break;
  65. }
  66. pthread_cleanup_pop (0);
  67. printf ("tf: %s returned\n", names[(long) arg]);
  68. exit (1);
  69. }
  70. #define TEST_FUNCTION do_test ()
  71. static int
  72. do_test (void)
  73. {
  74. char name[sizeof "/tst-mqueue8-" + sizeof (pid_t) * 3];
  75. snprintf (name, sizeof (name), "/tst-mqueue8-%u", getpid ());
  76. struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
  77. q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  78. if (q == (mqd_t) -1)
  79. {
  80. printf ("mq_open failed with: %m\n");
  81. return 0;
  82. }
  83. if (mq_unlink (name) != 0)
  84. {
  85. printf ("mq_unlink failed with: %m\n");
  86. return 1;
  87. }
  88. if (pthread_barrier_init (&b, NULL, 2) != 0)
  89. {
  90. puts ("barrier_init failed");
  91. return 1;
  92. }
  93. if (clock_gettime (CLOCK_REALTIME, &never) == 0)
  94. never.tv_sec += 100;
  95. else
  96. {
  97. never.tv_sec = time (NULL) + 100;
  98. never.tv_nsec = 0;
  99. }
  100. int result = 0;
  101. for (long l = TF_MQ_RECEIVE; l <= TF_MQ_TIMEDSEND; ++l)
  102. {
  103. cl_called = 0;
  104. pthread_t th;
  105. if (pthread_create (&th, NULL, tf, (void *) l) != 0)
  106. {
  107. printf ("1st %s create failed\n", names[l]);
  108. result = 1;
  109. continue;
  110. }
  111. int r = pthread_barrier_wait (&b);
  112. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  113. {
  114. puts ("barrier_wait failed");
  115. result = 1;
  116. continue;
  117. }
  118. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  119. while (nanosleep (&ts, &ts) != 0)
  120. continue;
  121. printf ("going to cancel %s in-time\n", names[l]);
  122. if (pthread_cancel (th) != 0)
  123. {
  124. printf ("1st cancel of %s failed\n", names[l]);
  125. result = 1;
  126. continue;
  127. }
  128. void *status;
  129. if (pthread_join (th, &status) != 0)
  130. {
  131. printf ("1st join of %s failed\n", names[l]);
  132. result = 1;
  133. continue;
  134. }
  135. if (status != PTHREAD_CANCELED)
  136. {
  137. printf ("1st %s thread not canceled\n", names[l]);
  138. result = 1;
  139. continue;
  140. }
  141. if (cl_called == 0)
  142. {
  143. printf ("%s cleanup handler not called\n", names[l]);
  144. result = 1;
  145. continue;
  146. }
  147. if (cl_called > 1)
  148. {
  149. printf ("%s cleanup handler called more than once\n", names[l]);
  150. result = 1;
  151. continue;
  152. }
  153. printf ("in-time %s cancellation succeeded\n", names[l]);
  154. cl_called = 0;
  155. if (pthread_create (&th, NULL, tf, (void *) l) != 0)
  156. {
  157. printf ("2nd %s create failed\n", names[l]);
  158. result = 1;
  159. continue;
  160. }
  161. printf ("going to cancel %s early\n", names[l]);
  162. if (pthread_cancel (th) != 0)
  163. {
  164. printf ("2nd cancel of %s failed\n", names[l]);
  165. result = 1;
  166. continue;
  167. }
  168. r = pthread_barrier_wait (&b);
  169. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  170. {
  171. puts ("barrier_wait failed");
  172. result = 1;
  173. continue;
  174. }
  175. if (pthread_join (th, &status) != 0)
  176. {
  177. printf ("2nd join of %s failed\n", names[l]);
  178. result = 1;
  179. continue;
  180. }
  181. if (status != PTHREAD_CANCELED)
  182. {
  183. printf ("2nd %s thread not canceled\n", names[l]);
  184. result = 1;
  185. continue;
  186. }
  187. if (cl_called == 0)
  188. {
  189. printf ("%s cleanup handler not called\n", names[l]);
  190. result = 1;
  191. continue;
  192. }
  193. if (cl_called > 1)
  194. {
  195. printf ("%s cleanup handler called more than once\n", names[l]);
  196. result = 1;
  197. continue;
  198. }
  199. printf ("early %s cancellation succeeded\n", names[l]);
  200. if (l == TF_MQ_TIMEDRECEIVE)
  201. {
  202. /* mq_receive and mq_timedreceive are tested on empty mq.
  203. For mq_send and mq_timedsend we need to make it full.
  204. If this fails, there is no point in doing further testing. */
  205. char c = ' ';
  206. if (mq_send (q, &c, 1, 1) != 0)
  207. {
  208. printf ("mq_send failed: %m\n");
  209. result = 1;
  210. break;
  211. }
  212. }
  213. }
  214. if (mq_close (q) != 0)
  215. {
  216. printf ("mq_close failed: %m\n");
  217. result = 1;
  218. }
  219. return result;
  220. }
  221. #else
  222. # define TEST_FUNCTION 0
  223. #endif
  224. #include "../test-skeleton.c"