tst-mqueue4.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Test message queue passing.
  2. Copyright (C) 2004-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <mqueue.h>
  18. #include <limits.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/time.h>
  25. #include <sys/wait.h>
  26. #include <time.h>
  27. #include <unistd.h>
  28. #include <support/check.h>
  29. #include "tst-mqueue.h"
  30. #define TEST_FUNCTION do_test ()
  31. static int
  32. do_test (void)
  33. {
  34. int result = 0;
  35. char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX];
  36. char *p;
  37. p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ());
  38. struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
  39. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  40. if (q == (mqd_t) -1)
  41. {
  42. if (errno == ENOSYS)
  43. FAIL_UNSUPPORTED ("mq_open not supported");
  44. printf ("mq_open failed with: %m\n");
  45. return 1;
  46. }
  47. add_temp_mq (name);
  48. *p = '.';
  49. memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
  50. name[NAME_MAX + 1] = '\0';
  51. mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  52. if (q2 == (mqd_t) -1)
  53. {
  54. printf ("mq_open with NAME_MAX long name component failed with: %m\n");
  55. result = 1;
  56. }
  57. if (mq_unlink (name) != 0)
  58. {
  59. printf ("mq_unlink failed: %m\n");
  60. result = 1;
  61. }
  62. if (mq_close (q2) != 0)
  63. {
  64. printf ("mq_close failed: %m\n");
  65. result = 1;
  66. }
  67. name[NAME_MAX + 1] = 'x';
  68. name[NAME_MAX + 2] = '\0';
  69. q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  70. if (q2 != (mqd_t) -1)
  71. {
  72. puts ("mq_open with too long name component unexpectedly succeeded");
  73. mq_unlink (name);
  74. mq_close (q2);
  75. result = 1;
  76. }
  77. else if (errno != ENAMETOOLONG)
  78. {
  79. printf ("mq_open with too long name component did not fail with "
  80. "ENAMETOOLONG: %m\n");
  81. result = 1;
  82. }
  83. if (mq_unlink (name) == 0)
  84. {
  85. puts ("mq_unlink with too long name component unexpectedly succeeded");
  86. result = 1;
  87. }
  88. else if (errno != ENAMETOOLONG)
  89. {
  90. printf ("mq_unlink with too long name component did not fail with "
  91. "ENAMETOOLONG: %m\n");
  92. result = 1;
  93. }
  94. *p = '\0';
  95. attr.mq_maxmsg = 1;
  96. attr.mq_msgsize = 3;
  97. q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
  98. if (q2 == (mqd_t) -1)
  99. {
  100. printf ("mq_open without O_EXCL failed with %m\n");
  101. result = 1;
  102. }
  103. char buf[3];
  104. strcpy (buf, "jk");
  105. if (mq_send (q, buf, 2, 4) != 0)
  106. {
  107. printf ("mq_send failed: %m\n");
  108. result = 1;
  109. }
  110. if (mq_send (q, buf + 1, 1, 5) != 0)
  111. {
  112. printf ("mq_send failed: %m\n");
  113. result = 1;
  114. }
  115. if (mq_getattr (q2, &attr) != 0)
  116. {
  117. printf ("mq_getattr failed: %m\n");
  118. result = 1;
  119. }
  120. if ((attr.mq_flags & O_NONBLOCK)
  121. || attr.mq_maxmsg != 2
  122. || attr.mq_msgsize != 2
  123. || attr.mq_curmsgs != 2)
  124. {
  125. printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
  126. ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
  127. (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
  128. (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
  129. result = 1;
  130. }
  131. struct timespec ts;
  132. if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
  133. ++ts.tv_sec;
  134. else
  135. {
  136. ts.tv_sec = time (NULL) + 1;
  137. ts.tv_nsec = 0;
  138. }
  139. if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
  140. {
  141. puts ("mq_timedsend unexpectedly succeeded");
  142. result = 1;
  143. }
  144. else if (errno != ETIMEDOUT)
  145. {
  146. printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
  147. result = 1;
  148. }
  149. if (mq_close (q2) != 0)
  150. {
  151. printf ("mq_close failed: %m\n");
  152. result = 1;
  153. }
  154. q2 = mq_open (name, O_RDONLY);
  155. if (q2 == (mqd_t) -1)
  156. {
  157. printf ("mq_open without O_CREAT failed with %m\n");
  158. result = 1;
  159. }
  160. mqd_t q3 = mq_open (name, O_RDONLY);
  161. if (q3 == (mqd_t) -1)
  162. {
  163. printf ("mq_open without O_CREAT failed with %m\n");
  164. result = 1;
  165. }
  166. memset (buf, ' ', sizeof (buf));
  167. unsigned int prio;
  168. ssize_t rets = mq_receive (q2, buf, 2, &prio);
  169. if (rets != 1)
  170. {
  171. if (rets == -1)
  172. printf ("mq_receive failed with: %m\n");
  173. else
  174. printf ("mq_receive returned %zd != 1\n", rets);
  175. result = 1;
  176. }
  177. else if (prio != 5 || memcmp (buf, "k ", 3) != 0)
  178. {
  179. printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k \")\n",
  180. prio, buf[0], buf[1], buf[2]);
  181. result = 1;
  182. }
  183. if (mq_getattr (q3, &attr) != 0)
  184. {
  185. printf ("mq_getattr failed: %m\n");
  186. result = 1;
  187. }
  188. if ((attr.mq_flags & O_NONBLOCK)
  189. || attr.mq_maxmsg != 2
  190. || attr.mq_msgsize != 2
  191. || attr.mq_curmsgs != 1)
  192. {
  193. printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
  194. ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
  195. (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
  196. (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
  197. result = 1;
  198. }
  199. rets = mq_receive (q3, buf, 2, NULL);
  200. if (rets != 2)
  201. {
  202. if (rets == -1)
  203. printf ("mq_receive failed with: %m\n");
  204. else
  205. printf ("mq_receive returned %zd != 2\n", rets);
  206. result = 1;
  207. }
  208. else if (memcmp (buf, "jk ", 3) != 0)
  209. {
  210. printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
  211. buf[0], buf[1], buf[2]);
  212. result = 1;
  213. }
  214. if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
  215. ++ts.tv_sec;
  216. else
  217. {
  218. ts.tv_sec = time (NULL) + 1;
  219. ts.tv_nsec = 0;
  220. }
  221. if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
  222. {
  223. puts ("mq_timedreceive on empty queue unexpectedly succeeded");
  224. result = 1;
  225. }
  226. else if (errno != ETIMEDOUT)
  227. {
  228. printf ("mq_timedreceive on empty queue did not fail with "
  229. "ETIMEDOUT: %m\n");
  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. if (mq_close (q2) != 0)
  243. {
  244. printf ("mq_close failed: %m\n");
  245. result = 1;
  246. }
  247. if (mq_close (q3) != 0)
  248. {
  249. printf ("mq_close failed: %m\n");
  250. result = 1;
  251. }
  252. return result;
  253. }
  254. #include "../test-skeleton.c"