tst-mqueue1.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/wait.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <stdint.h>
  25. #include <support/check.h>
  26. #include "tst-mqueue.h"
  27. static int
  28. intcmp (const void *a, const void *b)
  29. {
  30. if (*(unsigned char *)a < *(unsigned char *)b)
  31. return 1;
  32. if (*(unsigned char *)a > *(unsigned char *)b)
  33. return -1;
  34. return 0;
  35. }
  36. static int
  37. check_attrs (struct mq_attr *attr, int nonblock, long cnt)
  38. {
  39. int result = 0;
  40. if (attr->mq_maxmsg != 10 || attr->mq_msgsize != 1)
  41. {
  42. printf ("attributes don't match those passed to mq_open\n"
  43. "mq_maxmsg %jd, mq_msgsize %jd\n",
  44. (intmax_t) attr->mq_maxmsg, (intmax_t) attr->mq_msgsize);
  45. result = 1;
  46. }
  47. if ((attr->mq_flags & O_NONBLOCK) != nonblock)
  48. {
  49. printf ("mq_flags %jx != %x\n",
  50. (intmax_t) (attr->mq_flags & O_NONBLOCK), nonblock);
  51. result = 1;
  52. }
  53. if (attr->mq_curmsgs != cnt)
  54. {
  55. printf ("mq_curmsgs %jd != %ld\n", (intmax_t) attr->mq_curmsgs, cnt);
  56. result = 1;
  57. }
  58. return result;
  59. }
  60. static int
  61. do_one_test (mqd_t q, const char *name, int nonblock)
  62. {
  63. int result = 0;
  64. unsigned char v []
  65. = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
  66. struct mq_attr attr;
  67. memset (&attr, 0xaa, sizeof (attr));
  68. if (mq_getattr (q, &attr) != 0)
  69. {
  70. printf ("mq_getattr failed: %m\n");
  71. result = 1;
  72. }
  73. else
  74. result |= check_attrs (&attr, nonblock, 0);
  75. if (mq_receive (q, (char *) &v[0], 1, NULL) != -1)
  76. {
  77. puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
  78. result = 1;
  79. }
  80. else if (errno != EBADF)
  81. {
  82. printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
  83. result = 1;
  84. }
  85. struct timespec ts;
  86. if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
  87. --ts.tv_sec;
  88. else
  89. {
  90. ts.tv_sec = time (NULL) - 1;
  91. ts.tv_nsec = 0;
  92. }
  93. int ret;
  94. for (int i = 0; i < 10; ++i)
  95. {
  96. if (i & 1)
  97. ret = mq_send (q, (char *) &v[i], 1, v[i] >> 4);
  98. else
  99. ret = mq_timedsend (q, (char *) &v[i], 1, v[i] >> 4, &ts);
  100. if (ret)
  101. {
  102. printf ("mq_%ssend failed: %m\n", (i & 1) ? "" : "timed");
  103. result = 1;
  104. }
  105. }
  106. ret = mq_timedsend (q, (char *) &v[10], 1, 8, &ts);
  107. if (ret != -1)
  108. {
  109. puts ("mq_timedsend on full queue did not fail");
  110. result = 1;
  111. }
  112. else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
  113. {
  114. printf ("mq_timedsend on full queue did not fail with %s: %m\n",
  115. nonblock ? "EAGAIN" : "ETIMEDOUT");
  116. result = 1;
  117. }
  118. if (nonblock)
  119. {
  120. ret = mq_send (q, (char *) &v[10], 1, 8);
  121. if (ret != -1)
  122. {
  123. puts ("mq_send on full non-blocking queue did not fail");
  124. result = 1;
  125. }
  126. else if (errno != EAGAIN)
  127. {
  128. printf ("mq_send on full non-blocking queue did not fail"
  129. "with EAGAIN: %m\n");
  130. result = 1;
  131. }
  132. }
  133. memset (&attr, 0xaa, sizeof (attr));
  134. if (mq_getattr (q, &attr) != 0)
  135. {
  136. printf ("mq_getattr failed: %m\n");
  137. result = 1;
  138. }
  139. else
  140. result |= check_attrs (&attr, nonblock, 10);
  141. pid_t pid = fork ();
  142. if (pid == -1)
  143. {
  144. printf ("fork failed: %m\n");
  145. result = 1;
  146. }
  147. else if (pid == 0)
  148. {
  149. result = 0;
  150. if (mq_close (q) != 0)
  151. {
  152. printf ("mq_close in child failed: %m\n");
  153. result = 1;
  154. }
  155. q = mq_open (name, O_RDONLY | nonblock);
  156. if (q == (mqd_t) -1)
  157. {
  158. printf ("mq_open in child failed: %m\n");
  159. exit (1);
  160. }
  161. memset (&attr, 0xaa, sizeof (attr));
  162. if (mq_getattr (q, &attr) != 0)
  163. {
  164. printf ("mq_getattr failed: %m\n");
  165. result = 1;
  166. }
  167. else
  168. result |= check_attrs (&attr, nonblock, 10);
  169. unsigned char vr[11] = { };
  170. unsigned int prio;
  171. ssize_t rets;
  172. if (mq_send (q, (char *) &v[0], 1, 1) != -1)
  173. {
  174. puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
  175. result = 1;
  176. }
  177. else if (errno != EBADF)
  178. {
  179. printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
  180. result = 1;
  181. }
  182. for (int i = 0; i < 10; ++i)
  183. {
  184. if (i & 1)
  185. rets = mq_receive (q, (char *) &vr[i], 1, &prio);
  186. else
  187. rets = mq_timedreceive (q, (char *) &vr[i], 1, &prio, &ts);
  188. if (rets != 1)
  189. {
  190. if (rets == -1)
  191. printf ("mq_%sreceive failed: %m\n", (i & 1) ? "" : "timed");
  192. else
  193. printf ("mq_%sreceive returned %zd != 1\n",
  194. (i & 1) ? "" : "timed", rets);
  195. result = 1;
  196. }
  197. else if (prio != (unsigned int) vr[i] >> 4)
  198. {
  199. printf ("unexpected priority %x for value %02x\n", prio,
  200. vr[i]);
  201. result = 1;
  202. }
  203. }
  204. qsort (v, 10, 1, intcmp);
  205. if (memcmp (v, vr, 10) != 0)
  206. {
  207. puts ("messages not received in expected order");
  208. result = 1;
  209. }
  210. rets = mq_timedreceive (q, (char *) &vr[10], 1, &prio, &ts);
  211. if (rets != -1)
  212. {
  213. puts ("mq_timedreceive on empty queue did not fail");
  214. result = 1;
  215. }
  216. else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
  217. {
  218. printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
  219. nonblock ? "EAGAIN" : "ETIMEDOUT");
  220. result = 1;
  221. }
  222. if (nonblock)
  223. {
  224. ret = mq_receive (q, (char *) &vr[10], 1, &prio);
  225. if (ret != -1)
  226. {
  227. puts ("mq_receive on empty non-blocking queue did not fail");
  228. result = 1;
  229. }
  230. else if (errno != EAGAIN)
  231. {
  232. printf ("mq_receive on empty non-blocking queue did not fail"
  233. "with EAGAIN: %m\n");
  234. result = 1;
  235. }
  236. }
  237. memset (&attr, 0xaa, sizeof (attr));
  238. if (mq_getattr (q, &attr) != 0)
  239. {
  240. printf ("mq_getattr failed: %m\n");
  241. result = 1;
  242. }
  243. else
  244. result |= check_attrs (&attr, nonblock, 0);
  245. if (mq_close (q) != 0)
  246. {
  247. printf ("mq_close in child failed: %m\n");
  248. result = 1;
  249. }
  250. exit (result);
  251. }
  252. int status;
  253. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  254. {
  255. printf ("waitpid failed: %m\n");
  256. kill (pid, SIGKILL);
  257. result = 1;
  258. }
  259. else if (!WIFEXITED (status) || WEXITSTATUS (status))
  260. {
  261. printf ("child failed: %d\n", status);
  262. result = 1;
  263. }
  264. memset (&attr, 0xaa, sizeof (attr));
  265. if (mq_getattr (q, &attr) != 0)
  266. {
  267. printf ("mq_getattr failed: %m\n");
  268. result = 1;
  269. }
  270. else
  271. result |= check_attrs (&attr, nonblock, 0);
  272. return result;
  273. }
  274. #define TEST_FUNCTION do_test ()
  275. static int
  276. do_test (void)
  277. {
  278. int result = 0;
  279. char name[sizeof "/tst-mqueue1-" + sizeof (pid_t) * 3];
  280. snprintf (name, sizeof (name), "/tst-mqueue1-%u", getpid ());
  281. struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
  282. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
  283. if (q == (mqd_t) -1)
  284. {
  285. if (errno == ENOSYS)
  286. FAIL_UNSUPPORTED ("mq_open not supported");
  287. printf ("mq_open failed with: %m\n");
  288. return 1;
  289. }
  290. add_temp_mq (name);
  291. result |= do_one_test (q, name, 0);
  292. mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
  293. if (q2 == (mqd_t) -1)
  294. {
  295. printf ("mq_open failed with: %m\n");
  296. q2 = q;
  297. result = 1;
  298. }
  299. else
  300. {
  301. if (mq_close (q) != 0)
  302. {
  303. printf ("mq_close in parent failed: %m\n");
  304. result = 1;
  305. }
  306. q = q2;
  307. result |= do_one_test (q, name, O_NONBLOCK);
  308. if (mq_getattr (q, &attr) != 0)
  309. {
  310. printf ("mq_getattr failed: %m\n");
  311. result = 1;
  312. }
  313. else
  314. {
  315. attr.mq_flags ^= O_NONBLOCK;
  316. struct mq_attr attr2;
  317. memset (&attr2, 0x55, sizeof (attr2));
  318. if (mq_setattr (q, &attr, &attr2) != 0)
  319. {
  320. printf ("mq_setattr failed: %m\n");
  321. result = 1;
  322. }
  323. else if (attr.mq_flags != (attr2.mq_flags ^ O_NONBLOCK)
  324. || attr.mq_maxmsg != attr2.mq_maxmsg
  325. || attr.mq_msgsize != attr2.mq_msgsize
  326. || attr.mq_curmsgs != 0
  327. || attr2.mq_curmsgs != 0)
  328. {
  329. puts ("mq_setattr returned unexpected values in *omqstat");
  330. result = 1;
  331. }
  332. else
  333. {
  334. result |= do_one_test (q, name, 0);
  335. if (mq_setattr (q, &attr2, NULL) != 0)
  336. {
  337. printf ("mq_setattr failed: %m\n");
  338. result = 1;
  339. }
  340. else
  341. result |= do_one_test (q, name, O_NONBLOCK);
  342. }
  343. }
  344. }
  345. if (mq_unlink (name) != 0)
  346. {
  347. printf ("mq_unlink failed: %m\n");
  348. result = 1;
  349. }
  350. if (mq_close (q) != 0)
  351. {
  352. printf ("mq_close in parent failed: %m\n");
  353. result = 1;
  354. }
  355. if (mq_close (q) != -1)
  356. {
  357. puts ("second mq_close did not fail");
  358. result = 1;
  359. }
  360. else if (errno != EBADF)
  361. {
  362. printf ("second mq_close did not fail with EBADF: %m\n");
  363. result = 1;
  364. }
  365. return result;
  366. }
  367. #include "../test-skeleton.c"