tst-mqueue2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/time.h>
  24. #include <sys/wait.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. #include "tst-mqueue.h"
  28. static void
  29. alrm_handler (int sig)
  30. {
  31. }
  32. #define TIMEOUT 10
  33. #define TEST_FUNCTION do_test ()
  34. static int
  35. do_test (void)
  36. {
  37. int result = 0;
  38. char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
  39. snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
  40. struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
  41. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  42. if (q == (mqd_t) -1)
  43. {
  44. printf ("mq_open failed with: %m\n");
  45. return result;
  46. }
  47. else
  48. add_temp_mq (name);
  49. mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  50. if (q2 != (mqd_t) -1)
  51. {
  52. puts ("mq_open with O_EXCL unexpectedly succeeded");
  53. result = 1;
  54. }
  55. else if (errno != EEXIST)
  56. {
  57. printf ("mq_open did not fail with EEXIST: %m\n");
  58. result = 1;
  59. }
  60. char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
  61. snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
  62. attr.mq_maxmsg = -2;
  63. q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  64. if (q2 != (mqd_t) -1)
  65. {
  66. puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
  67. add_temp_mq (name2);
  68. result = 1;
  69. }
  70. else if (errno != EINVAL)
  71. {
  72. printf ("mq_open with invalid mq_maxmsg did not fail with "
  73. "EINVAL: %m\n");
  74. result = 1;
  75. }
  76. attr.mq_maxmsg = 2;
  77. attr.mq_msgsize = -56;
  78. q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  79. if (q2 != (mqd_t) -1)
  80. {
  81. puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
  82. add_temp_mq (name2);
  83. result = 1;
  84. }
  85. else if (errno != EINVAL)
  86. {
  87. printf ("mq_open with invalid mq_msgsize did not fail with "
  88. "EINVAL: %m\n");
  89. result = 1;
  90. }
  91. char buf[3];
  92. struct timespec ts;
  93. if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
  94. ts.tv_sec += 10;
  95. else
  96. {
  97. ts.tv_sec = time (NULL) + 10;
  98. ts.tv_nsec = 0;
  99. }
  100. if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
  101. {
  102. puts ("mq_timedreceive with too small msg_len did not fail");
  103. result = 1;
  104. }
  105. else if (errno != EMSGSIZE)
  106. {
  107. printf ("mq_timedreceive with too small msg_len did not fail with "
  108. "EMSGSIZE: %m\n");
  109. result = 1;
  110. }
  111. ts.tv_nsec = -1;
  112. if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
  113. {
  114. puts ("mq_timedreceive with negative tv_nsec did not fail");
  115. result = 1;
  116. }
  117. else if (errno != EINVAL)
  118. {
  119. printf ("mq_timedreceive with negative tv_nsec did not fail with "
  120. "EINVAL: %m\n");
  121. result = 1;
  122. }
  123. ts.tv_nsec = 1000000000;
  124. if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
  125. {
  126. puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
  127. result = 1;
  128. }
  129. else if (errno != EINVAL)
  130. {
  131. printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
  132. "EINVAL: %m\n");
  133. result = 1;
  134. }
  135. struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
  136. sigemptyset (&sa.sa_mask);
  137. sigaction (SIGALRM, &sa, NULL);
  138. struct itimerval it = { .it_value = { .tv_sec = 1 } };
  139. setitimer (ITIMER_REAL, &it, NULL);
  140. if (mq_receive (q, buf, 2, NULL) == 0)
  141. {
  142. puts ("mq_receive on empty queue did not block");
  143. result = 1;
  144. }
  145. else if (errno != EINTR)
  146. {
  147. printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
  148. result = 1;
  149. }
  150. setitimer (ITIMER_REAL, &it, NULL);
  151. ts.tv_nsec = 0;
  152. if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
  153. {
  154. puts ("mq_timedreceive on empty queue did not block");
  155. result = 1;
  156. }
  157. else if (errno != EINTR)
  158. {
  159. printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
  160. result = 1;
  161. }
  162. buf[0] = '6';
  163. buf[1] = '7';
  164. if (mq_send (q, buf, 2, 3) != 0
  165. || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
  166. {
  167. printf ("mq_send failed: %m\n");
  168. result = 1;
  169. }
  170. memset (buf, ' ', sizeof (buf));
  171. unsigned int prio;
  172. ssize_t rets = mq_receive (q, buf, 3, &prio);
  173. if (rets != 1)
  174. {
  175. if (rets == -1)
  176. printf ("mq_receive failed: %m\n");
  177. else
  178. printf ("mq_receive returned %zd != 1\n", rets);
  179. result = 1;
  180. }
  181. else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
  182. {
  183. printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
  184. prio, buf[0], buf[1], buf[2]);
  185. result = 1;
  186. }
  187. rets = mq_receive (q, buf, 2, NULL);
  188. if (rets != 2)
  189. {
  190. if (rets == -1)
  191. printf ("mq_receive failed: %m\n");
  192. else
  193. printf ("mq_receive returned %zd != 2\n", rets);
  194. result = 1;
  195. }
  196. else if (memcmp (buf, "67 ", 3) != 0)
  197. {
  198. printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
  199. buf[0], buf[1], buf[2]);
  200. result = 1;
  201. }
  202. buf[0] = '2';
  203. buf[1] = '1';
  204. if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
  205. ts.tv_sec = time (NULL);
  206. ts.tv_nsec = -1000000001;
  207. if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
  208. && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
  209. || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
  210. (mq_timedsend (q, buf, 1, 4, &ts) != 0
  211. && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
  212. {
  213. printf ("mq_timedsend failed: %m\n");
  214. result = 1;
  215. }
  216. buf[0] = '-';
  217. ts.tv_nsec = 1000000001;
  218. if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
  219. {
  220. puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
  221. result = 1;
  222. }
  223. else if (errno != EINVAL)
  224. {
  225. printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
  226. "EINVAL: %m\n");
  227. result = 1;
  228. }
  229. ts.tv_nsec = -2;
  230. if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
  231. {
  232. puts ("mq_timedsend with negative tv_nsec did not fail");
  233. result = 1;
  234. }
  235. else if (errno != EINVAL)
  236. {
  237. printf ("mq_timedsend with megatove tv_nsec did not fail with "
  238. "EINVAL: %m\n");
  239. result = 1;
  240. }
  241. setitimer (ITIMER_REAL, &it, NULL);
  242. if (mq_send (q, buf, 2, 8) == 0)
  243. {
  244. puts ("mq_send on full queue did not block");
  245. result = 1;
  246. }
  247. else if (errno != EINTR)
  248. {
  249. printf ("mq_send on full queue did not fail with EINTR: %m\n");
  250. result = 1;
  251. }
  252. setitimer (ITIMER_REAL, &it, NULL);
  253. ts.tv_sec += 10;
  254. ts.tv_nsec = 0;
  255. if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
  256. {
  257. puts ("mq_timedsend on full queue did not block");
  258. result = 1;
  259. }
  260. else if (errno != EINTR)
  261. {
  262. printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
  263. result = 1;
  264. }
  265. memset (buf, ' ', sizeof (buf));
  266. if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
  267. ts.tv_sec = time (NULL);
  268. ts.tv_nsec = -1000000001;
  269. rets = mq_timedreceive (q, buf, 2, &prio, &ts);
  270. if (rets == -1 && errno == EINVAL)
  271. rets = mq_receive (q, buf, 2, &prio);
  272. if (rets != 2)
  273. {
  274. if (rets == -1)
  275. printf ("mq_timedreceive failed: %m\n");
  276. else
  277. printf ("mq_timedreceive returned %zd != 2\n", rets);
  278. result = 1;
  279. }
  280. else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
  281. {
  282. printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
  283. prio, buf[0], buf[1], buf[2]);
  284. result = 1;
  285. }
  286. if (mq_receive (q, buf, 1, NULL) == 0)
  287. {
  288. puts ("mq_receive with too small msg_len did not fail");
  289. result = 1;
  290. }
  291. else if (errno != EMSGSIZE)
  292. {
  293. printf ("mq_receive with too small msg_len did not fail with "
  294. "EMSGSIZE: %m\n");
  295. result = 1;
  296. }
  297. ts.tv_nsec = -ts.tv_nsec;
  298. rets = mq_timedreceive (q, buf, 2, NULL, &ts);
  299. if (rets == -1 && errno == EINVAL)
  300. rets = mq_receive (q, buf, 2, NULL);
  301. if (rets != 1)
  302. {
  303. if (rets == -1)
  304. printf ("mq_timedreceive failed: %m\n");
  305. else
  306. printf ("mq_timedreceive returned %zd != 1\n", rets);
  307. result = 1;
  308. }
  309. else if (memcmp (buf, "31 ", 3) != 0)
  310. {
  311. printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
  312. buf[0], buf[1], buf[2]);
  313. result = 1;
  314. }
  315. if (mq_send (q, "", 0, 2) != 0)
  316. {
  317. printf ("mq_send with msg_len 0 failed: %m\n");
  318. result = 1;
  319. }
  320. rets = mq_receive (q, buf, 2, &prio);
  321. if (rets)
  322. {
  323. if (rets == -1)
  324. printf ("mq_receive failed: %m\n");
  325. else
  326. printf ("mq_receive returned %zd != 0\n", rets);
  327. result = 1;
  328. }
  329. long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
  330. if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
  331. {
  332. if (mq_send (q, buf, 1, mq_prio_max) == 0)
  333. {
  334. puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
  335. result = 1;
  336. }
  337. else if (errno != EINVAL)
  338. {
  339. printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
  340. "EINVAL: %m\n");
  341. result = 1;
  342. }
  343. if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
  344. {
  345. printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
  346. result = 1;
  347. }
  348. }
  349. if (mq_unlink (name) != 0)
  350. {
  351. printf ("mq_unlink failed: %m\n");
  352. result = 1;
  353. }
  354. q2 = mq_open (name, O_RDWR);
  355. if (q2 != (mqd_t) -1)
  356. {
  357. printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
  358. "succeeded\n", name);
  359. result = 1;
  360. }
  361. else if (errno != ENOENT)
  362. {
  363. printf ("mq_open of unlinked %s without O_CREAT did not fail with "
  364. "ENOENT: %m\n", name);
  365. result = 1;
  366. }
  367. if (mq_close (q) != 0)
  368. {
  369. printf ("mq_close in parent failed: %m\n");
  370. result = 1;
  371. }
  372. if (mq_receive (q, buf, 2, NULL) == 0)
  373. {
  374. puts ("mq_receive on invalid mqd_t did not fail");
  375. result = 1;
  376. }
  377. else if (errno != EBADF)
  378. {
  379. printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
  380. result = 1;
  381. }
  382. if (mq_send (q, buf, 1, 2) == 0)
  383. {
  384. puts ("mq_send on invalid mqd_t did not fail");
  385. result = 1;
  386. }
  387. else if (errno != EBADF)
  388. {
  389. printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
  390. result = 1;
  391. }
  392. if (mq_getattr (q, &attr) == 0)
  393. {
  394. puts ("mq_getattr on invalid mqd_t did not fail");
  395. result = 1;
  396. }
  397. else if (errno != EBADF)
  398. {
  399. printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
  400. result = 1;
  401. }
  402. memset (&attr, 0, sizeof (attr));
  403. if (mq_setattr (q, &attr, NULL) == 0)
  404. {
  405. puts ("mq_setattr on invalid mqd_t did not fail");
  406. result = 1;
  407. }
  408. else if (errno != EBADF)
  409. {
  410. printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
  411. result = 1;
  412. }
  413. if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
  414. {
  415. puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
  416. result = 1;
  417. }
  418. else if (errno != ENOENT)
  419. {
  420. printf ("mq_unlink of non-existant message queue did not fail with "
  421. "ENOENT: %m\n");
  422. result = 1;
  423. }
  424. return result;
  425. }
  426. #include "../test-skeleton.c"