tst-mqueue2.c 12 KB

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