tst-mqueue6.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Test mq_notify.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <mqueue.h>
  19. #include <limits.h>
  20. #include <signal.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/mman.h>
  26. #include <sys/time.h>
  27. #include <sys/wait.h>
  28. #include <time.h>
  29. #include <unistd.h>
  30. #include "tst-mqueue.h"
  31. #if _POSIX_THREADS
  32. # include <pthread.h>
  33. # define mqsend(q) (mqsend) (q, __LINE__)
  34. static int
  35. (mqsend) (mqd_t q, int line)
  36. {
  37. char c;
  38. if (mq_send (q, &c, 1, 1) != 0)
  39. {
  40. printf ("mq_send on line %d failed with: %m\n", line);
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. # define mqrecv(q) (mqrecv) (q, __LINE__)
  46. static int
  47. (mqrecv) (mqd_t q, int line)
  48. {
  49. char c;
  50. ssize_t rets = TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
  51. if (rets != 1)
  52. {
  53. if (rets == -1)
  54. printf ("mq_receive on line %d failed with: %m\n", line);
  55. else
  56. printf ("mq_receive on line %d returned %zd != 1\n",
  57. line, rets);
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. volatile int fct_cnt, fct_err;
  63. size_t fct_guardsize;
  64. static void
  65. fct (union sigval s)
  66. {
  67. mqd_t q = *(mqd_t *) s.sival_ptr;
  68. pthread_attr_t nattr;
  69. int ret = pthread_getattr_np (pthread_self (), &nattr);
  70. if (ret)
  71. {
  72. errno = ret;
  73. printf ("pthread_getattr_np failed: %m\n");
  74. fct_err = 1;
  75. }
  76. else
  77. {
  78. ret = pthread_attr_getguardsize (&nattr, &fct_guardsize);
  79. if (ret)
  80. {
  81. errno = ret;
  82. printf ("pthread_attr_getguardsize failed: %m\n");
  83. fct_err = 1;
  84. }
  85. if (pthread_attr_destroy (&nattr) != 0)
  86. {
  87. puts ("pthread_attr_destroy failed");
  88. fct_err = 1;
  89. }
  90. }
  91. ++fct_cnt;
  92. fct_err |= mqsend (q);
  93. }
  94. # define TEST_FUNCTION do_test ()
  95. static int
  96. do_test (void)
  97. {
  98. int result = 0;
  99. char name[sizeof "/tst-mqueue6-" + sizeof (pid_t) * 3];
  100. snprintf (name, sizeof (name), "/tst-mqueue6-%u", getpid ());
  101. struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
  102. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  103. if (q == (mqd_t) -1)
  104. {
  105. printf ("mq_open failed with: %m\n");
  106. return result;
  107. }
  108. else
  109. add_temp_mq (name);
  110. pthread_attr_t nattr;
  111. if (pthread_attr_init (&nattr)
  112. || pthread_attr_setguardsize (&nattr, 0))
  113. {
  114. puts ("pthread_attr_t setup failed");
  115. result = 1;
  116. }
  117. fct_guardsize = 1;
  118. struct sigevent ev;
  119. memset (&ev, 0xaa, sizeof (ev));
  120. ev.sigev_notify = SIGEV_THREAD;
  121. ev.sigev_notify_function = fct;
  122. ev.sigev_notify_attributes = &nattr;
  123. ev.sigev_value.sival_ptr = &q;
  124. if (mq_notify (q, &ev) != 0)
  125. {
  126. printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
  127. result = 1;
  128. }
  129. size_t ps = sysconf (_SC_PAGESIZE);
  130. if (pthread_attr_setguardsize (&nattr, 32 * ps))
  131. {
  132. puts ("pthread_attr_t setup failed");
  133. result = 1;
  134. }
  135. if (mq_notify (q, &ev) == 0)
  136. {
  137. puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
  138. result = 1;
  139. }
  140. else if (errno != EBUSY)
  141. {
  142. printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  143. result = 1;
  144. }
  145. if (fct_cnt != 0)
  146. {
  147. printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__);
  148. result = 1;
  149. }
  150. result |= mqsend (q);
  151. result |= mqrecv (q);
  152. result |= mqrecv (q);
  153. if (fct_cnt != 1)
  154. {
  155. printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__);
  156. result = 1;
  157. }
  158. else if (fct_guardsize != 0)
  159. {
  160. printf ("fct_guardsize %zd != 0\n", fct_guardsize);
  161. result = 1;
  162. }
  163. if (mq_notify (q, &ev) != 0)
  164. {
  165. printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  166. result = 1;
  167. }
  168. if (mq_notify (q, NULL) != 0)
  169. {
  170. printf ("mq_notify (q, NULL) failed with: %m\n");
  171. result = 1;
  172. }
  173. memset (&ev, 0x11, sizeof (ev));
  174. ev.sigev_notify = SIGEV_THREAD;
  175. ev.sigev_notify_function = fct;
  176. ev.sigev_notify_attributes = &nattr;
  177. ev.sigev_value.sival_ptr = &q;
  178. if (mq_notify (q, &ev) != 0)
  179. {
  180. printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
  181. result = 1;
  182. }
  183. if (pthread_attr_setguardsize (&nattr, 0))
  184. {
  185. puts ("pthread_attr_t setup failed");
  186. result = 1;
  187. }
  188. if (mq_notify (q, &ev) == 0)
  189. {
  190. puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
  191. result = 1;
  192. }
  193. else if (errno != EBUSY)
  194. {
  195. printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  196. result = 1;
  197. }
  198. if (fct_cnt != 1)
  199. {
  200. printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__);
  201. result = 1;
  202. }
  203. result |= mqsend (q);
  204. result |= mqrecv (q);
  205. result |= mqrecv (q);
  206. if (fct_cnt != 2)
  207. {
  208. printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__);
  209. result = 1;
  210. }
  211. else if (fct_guardsize != 32 * ps)
  212. {
  213. printf ("fct_guardsize %zd != %zd\n", fct_guardsize, 32 * ps);
  214. result = 1;
  215. }
  216. if (mq_notify (q, &ev) != 0)
  217. {
  218. printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  219. result = 1;
  220. }
  221. if (mq_notify (q, NULL) != 0)
  222. {
  223. printf ("mq_notify (q, NULL) failed with: %m\n");
  224. result = 1;
  225. }
  226. if (pthread_attr_destroy (&nattr) != 0)
  227. {
  228. puts ("pthread_attr_destroy failed");
  229. result = 1;
  230. }
  231. if (mq_unlink (name) != 0)
  232. {
  233. printf ("mq_unlink failed: %m\n");
  234. result = 1;
  235. }
  236. if (mq_close (q) != 0)
  237. {
  238. printf ("mq_close failed: %m\n");
  239. result = 1;
  240. }
  241. memset (&ev, 0x55, sizeof (ev));
  242. ev.sigev_notify = SIGEV_THREAD;
  243. ev.sigev_notify_function = fct;
  244. ev.sigev_notify_attributes = NULL;
  245. ev.sigev_value.sival_int = 0;
  246. if (mq_notify (q, &ev) == 0)
  247. {
  248. puts ("mq_notify on closed mqd_t unexpectedly succeeded");
  249. result = 1;
  250. }
  251. else if (errno != EBADF)
  252. {
  253. printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
  254. result = 1;
  255. }
  256. if (fct_err)
  257. result = 1;
  258. return result;
  259. }
  260. #else
  261. # define TEST_FUNCTION 0
  262. #endif
  263. #include "../test-skeleton.c"