tst-mqueue6.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "../test-skeleton.h"
  32. #if _POSIX_THREADS
  33. # include <pthread.h>
  34. # define mqsend(q) (mqsend) (q, __LINE__)
  35. static int
  36. (mqsend) (mqd_t q, int line)
  37. {
  38. char c;
  39. if (mq_send (q, &c, 1, 1) != 0)
  40. {
  41. printf ("mq_send on line %d failed with: %m\n", line);
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. # define mqrecv(q) (mqrecv) (q, __LINE__)
  47. static int
  48. (mqrecv) (mqd_t q, int line)
  49. {
  50. char c;
  51. ssize_t rets = TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
  52. if (rets != 1)
  53. {
  54. if (rets == -1)
  55. printf ("mq_receive on line %d failed with: %m\n", line);
  56. else
  57. printf ("mq_receive on line %d returned %zd != 1\n",
  58. line, rets);
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. volatile int fct_cnt, fct_err;
  64. size_t fct_guardsize;
  65. static void
  66. fct (union sigval s)
  67. {
  68. mqd_t q = *(mqd_t *) s.sival_ptr;
  69. pthread_attr_t nattr;
  70. int ret = pthread_getattr_np (pthread_self (), &nattr);
  71. if (ret)
  72. {
  73. errno = ret;
  74. printf ("pthread_getattr_np failed: %m\n");
  75. fct_err = 1;
  76. }
  77. else
  78. {
  79. ret = pthread_attr_getguardsize (&nattr, &fct_guardsize);
  80. if (ret)
  81. {
  82. errno = ret;
  83. printf ("pthread_attr_getguardsize failed: %m\n");
  84. fct_err = 1;
  85. }
  86. if (pthread_attr_destroy (&nattr) != 0)
  87. {
  88. puts ("pthread_attr_destroy failed");
  89. fct_err = 1;
  90. }
  91. }
  92. ++fct_cnt;
  93. fct_err |= mqsend (q);
  94. }
  95. # define TEST_FUNCTION do_test ()
  96. static int
  97. do_test (void)
  98. {
  99. int result = 0;
  100. char name[sizeof "/tst-mqueue6-" + sizeof (pid_t) * 3];
  101. snprintf (name, sizeof (name), "/tst-mqueue6-%u", getpid ());
  102. struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
  103. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  104. if (q == (mqd_t) -1)
  105. {
  106. printf ("mq_open failed with: %m\n");
  107. return result;
  108. }
  109. else
  110. add_temp_mq (name);
  111. pthread_attr_t nattr;
  112. if (pthread_attr_init (&nattr)
  113. || pthread_attr_setguardsize (&nattr, 0))
  114. {
  115. puts ("pthread_attr_t setup failed");
  116. result = 1;
  117. }
  118. fct_guardsize = 1;
  119. struct sigevent ev;
  120. memset (&ev, 0xaa, sizeof (ev));
  121. ev.sigev_notify = SIGEV_THREAD;
  122. ev.sigev_notify_function = fct;
  123. ev.sigev_notify_attributes = &nattr;
  124. ev.sigev_value.sival_ptr = &q;
  125. if (mq_notify (q, &ev) != 0)
  126. {
  127. printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
  128. result = 1;
  129. }
  130. size_t ps = sysconf (_SC_PAGESIZE);
  131. if (pthread_attr_setguardsize (&nattr, 32 * ps))
  132. {
  133. puts ("pthread_attr_t setup failed");
  134. result = 1;
  135. }
  136. if (mq_notify (q, &ev) == 0)
  137. {
  138. puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
  139. result = 1;
  140. }
  141. else if (errno != EBUSY)
  142. {
  143. printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  144. result = 1;
  145. }
  146. if (fct_cnt != 0)
  147. {
  148. printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__);
  149. result = 1;
  150. }
  151. result |= mqsend (q);
  152. result |= mqrecv (q);
  153. result |= mqrecv (q);
  154. if (fct_cnt != 1)
  155. {
  156. printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__);
  157. result = 1;
  158. }
  159. else if (fct_guardsize != 0)
  160. {
  161. printf ("fct_guardsize %zd != 0\n", fct_guardsize);
  162. result = 1;
  163. }
  164. if (mq_notify (q, &ev) != 0)
  165. {
  166. printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  167. result = 1;
  168. }
  169. if (mq_notify (q, NULL) != 0)
  170. {
  171. printf ("mq_notify (q, NULL) failed with: %m\n");
  172. result = 1;
  173. }
  174. memset (&ev, 0x11, sizeof (ev));
  175. ev.sigev_notify = SIGEV_THREAD;
  176. ev.sigev_notify_function = fct;
  177. ev.sigev_notify_attributes = &nattr;
  178. ev.sigev_value.sival_ptr = &q;
  179. if (mq_notify (q, &ev) != 0)
  180. {
  181. printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
  182. result = 1;
  183. }
  184. if (pthread_attr_setguardsize (&nattr, 0))
  185. {
  186. puts ("pthread_attr_t setup failed");
  187. result = 1;
  188. }
  189. if (mq_notify (q, &ev) == 0)
  190. {
  191. puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
  192. result = 1;
  193. }
  194. else if (errno != EBUSY)
  195. {
  196. printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  197. result = 1;
  198. }
  199. if (fct_cnt != 1)
  200. {
  201. printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__);
  202. result = 1;
  203. }
  204. result |= mqsend (q);
  205. result |= mqrecv (q);
  206. result |= mqrecv (q);
  207. if (fct_cnt != 2)
  208. {
  209. printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__);
  210. result = 1;
  211. }
  212. else if (fct_guardsize != 32 * ps)
  213. {
  214. printf ("fct_guardsize %zd != %zd\n", fct_guardsize, 32 * ps);
  215. result = 1;
  216. }
  217. if (mq_notify (q, &ev) != 0)
  218. {
  219. printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  220. result = 1;
  221. }
  222. if (mq_notify (q, NULL) != 0)
  223. {
  224. printf ("mq_notify (q, NULL) failed with: %m\n");
  225. result = 1;
  226. }
  227. if (pthread_attr_destroy (&nattr) != 0)
  228. {
  229. puts ("pthread_attr_destroy failed");
  230. result = 1;
  231. }
  232. if (mq_unlink (name) != 0)
  233. {
  234. printf ("mq_unlink failed: %m\n");
  235. result = 1;
  236. }
  237. if (mq_close (q) != 0)
  238. {
  239. printf ("mq_close failed: %m\n");
  240. result = 1;
  241. }
  242. memset (&ev, 0x55, sizeof (ev));
  243. ev.sigev_notify = SIGEV_THREAD;
  244. ev.sigev_notify_function = fct;
  245. ev.sigev_notify_attributes = NULL;
  246. ev.sigev_value.sival_int = 0;
  247. if (mq_notify (q, &ev) == 0)
  248. {
  249. puts ("mq_notify on closed mqd_t unexpectedly succeeded");
  250. result = 1;
  251. }
  252. else if (errno != EBADF)
  253. {
  254. printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
  255. result = 1;
  256. }
  257. if (fct_err)
  258. result = 1;
  259. return result;
  260. }
  261. #else
  262. # define TEST_FUNCTION 0
  263. #endif
  264. #include "../test-skeleton.c"