tst-mqueue1.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* Test message queue passing.
  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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/wait.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include "tst-mqueue.h"
  26. #include "../test-skeleton.h"
  27. #ifdef __ARCH_USE_MMU__
  28. static int
  29. intcmp (const void *a, const void *b)
  30. {
  31. if (*(unsigned char *)a < *(unsigned char *)b)
  32. return 1;
  33. if (*(unsigned char *)a > *(unsigned char *)b)
  34. return -1;
  35. return 0;
  36. }
  37. static int
  38. check_attrs (struct mq_attr *attr, int nonblock, long cnt)
  39. {
  40. int result = 0;
  41. if (attr->mq_maxmsg != 10 || attr->mq_msgsize != 1)
  42. {
  43. printf ("attributes don't match those passed to mq_open\n"
  44. "mq_maxmsg %ld, mq_msgsize %ld\n",
  45. attr->mq_maxmsg, attr->mq_msgsize);
  46. result = 1;
  47. }
  48. if ((attr->mq_flags & O_NONBLOCK) != nonblock)
  49. {
  50. printf ("mq_flags %lx != %x\n", (attr->mq_flags & O_NONBLOCK), nonblock);
  51. result = 1;
  52. }
  53. if (attr->mq_curmsgs != cnt)
  54. {
  55. printf ("mq_curmsgs %ld != %ld\n", 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. 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, &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, &v[i], 1, v[i] >> 4);
  98. else
  99. ret = mq_timedsend (q, &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, &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, &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. char vr[11] = { };
  170. unsigned int prio;
  171. ssize_t rets;
  172. if (mq_send (q, &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, &vr[i], 1, &prio);
  186. else
  187. rets = mq_timedreceive (q, &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, &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, &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. printf ("mq_open failed with: %m\n");
  286. return result;
  287. }
  288. else
  289. add_temp_mq (name);
  290. result |= do_one_test (q, name, 0);
  291. mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
  292. if (q2 == (mqd_t) -1)
  293. {
  294. printf ("mq_open failed with: %m\n");
  295. q2 = q;
  296. result = 1;
  297. }
  298. else
  299. {
  300. if (mq_close (q) != 0)
  301. {
  302. printf ("mq_close in parent failed: %m\n");
  303. result = 1;
  304. }
  305. q = q2;
  306. result |= do_one_test (q, name, O_NONBLOCK);
  307. if (mq_getattr (q, &attr) != 0)
  308. {
  309. printf ("mq_getattr failed: %m\n");
  310. result = 1;
  311. }
  312. else
  313. {
  314. attr.mq_flags ^= O_NONBLOCK;
  315. struct mq_attr attr2;
  316. memset (&attr2, 0x55, sizeof (attr2));
  317. if (mq_setattr (q, &attr, &attr2) != 0)
  318. {
  319. printf ("mq_setattr failed: %m\n");
  320. result = 1;
  321. }
  322. else if (attr.mq_flags != (attr2.mq_flags ^ O_NONBLOCK)
  323. || attr.mq_maxmsg != attr2.mq_maxmsg
  324. || attr.mq_msgsize != attr2.mq_msgsize
  325. || attr.mq_curmsgs != 0
  326. || attr2.mq_curmsgs != 0)
  327. {
  328. puts ("mq_setattr returned unexpected values in *omqstat");
  329. result = 1;
  330. }
  331. else
  332. {
  333. result |= do_one_test (q, name, 0);
  334. if (mq_setattr (q, &attr2, NULL) != 0)
  335. {
  336. printf ("mq_setattr failed: %m\n");
  337. result = 1;
  338. }
  339. else
  340. result |= do_one_test (q, name, O_NONBLOCK);
  341. }
  342. }
  343. }
  344. if (mq_unlink (name) != 0)
  345. {
  346. printf ("mq_unlink failed: %m\n");
  347. result = 1;
  348. }
  349. if (mq_close (q) != 0)
  350. {
  351. printf ("mq_close in parent failed: %m\n");
  352. result = 1;
  353. }
  354. if (mq_close (q) != -1)
  355. {
  356. puts ("second mq_close did not fail");
  357. result = 1;
  358. }
  359. else if (errno != EBADF)
  360. {
  361. printf ("second mq_close did not fail with EBADF: %m\n");
  362. result = 1;
  363. }
  364. return result;
  365. }
  366. #include "../test-skeleton.c"
  367. #else
  368. int main(void)
  369. {
  370. printf("Skipping test on non-mmu host!\n");
  371. return 23;
  372. }
  373. #endif