tst-mqueue8.c 5.8 KB

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