tst-mqueue8.c 5.7 KB

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