tst-mqueue4.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <limits.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. #define TIMEOUT 4
  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. printf ("mq_open failed with: %m\n");
  43. return result;
  44. }
  45. else
  46. add_temp_mq (name);
  47. *p = '.';
  48. memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
  49. name[NAME_MAX + 1] = '\0';
  50. mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  51. if (q2 == (mqd_t) -1)
  52. {
  53. printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
  54. result = 1;
  55. }
  56. if (mq_unlink (name) != 0)
  57. {
  58. printf ("mq_unlink failed: %m\n");
  59. result = 1;
  60. }
  61. if (mq_close (q2) != 0)
  62. {
  63. printf ("mq_close failed: %m\n");
  64. result = 1;
  65. }
  66. name[NAME_MAX + 1] = 'x';
  67. name[NAME_MAX + 2] = '\0';
  68. q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  69. if (q2 != (mqd_t) -1)
  70. {
  71. puts ("mq_open with too long name component unexpectedly succeeded");
  72. mq_unlink (name);
  73. mq_close (q2);
  74. result = 1;
  75. }
  76. else if (errno != ENAMETOOLONG)
  77. {
  78. printf ("mq_open with too long name component did not fail with "
  79. "ENAMETOOLONG: %m\n");
  80. result = 1;
  81. }
  82. if (mq_unlink (name) == 0)
  83. {
  84. puts ("mq_unlink with too long name component unexpectedly succeeded");
  85. result = 1;
  86. }
  87. else if (errno != ENAMETOOLONG)
  88. {
  89. printf ("mq_unlink with too long name component did not fail with "
  90. "ENAMETOOLONG: %m\n");
  91. result = 1;
  92. }
  93. *p = '\0';
  94. attr.mq_maxmsg = 1;
  95. attr.mq_msgsize = 3;
  96. q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
  97. if (q2 == (mqd_t) -1)
  98. {
  99. printf ("mq_open without O_EXCL failed with %m\n");
  100. result = 1;
  101. }
  102. char buf[3];
  103. strcpy (buf, "jk");
  104. if (mq_send (q, buf, 2, 4) != 0)
  105. {
  106. printf ("mq_send failed: %m\n");
  107. result = 1;
  108. }
  109. if (mq_send (q, buf + 1, 1, 5) != 0)
  110. {
  111. printf ("mq_send failed: %m\n");
  112. result = 1;
  113. }
  114. if (mq_getattr (q2, &attr) != 0)
  115. {
  116. printf ("mq_getattr failed: %m\n");
  117. result = 1;
  118. }
  119. if ((attr.mq_flags & O_NONBLOCK)
  120. || attr.mq_maxmsg != 2
  121. || attr.mq_msgsize != 2
  122. || attr.mq_curmsgs != 2)
  123. {
  124. printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
  125. ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
  126. attr.mq_flags, attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
  127. result = 1;
  128. }
  129. struct timespec ts;
  130. if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
  131. ++ts.tv_sec;
  132. else
  133. {
  134. ts.tv_sec = time (NULL) + 1;
  135. ts.tv_nsec = 0;
  136. }
  137. if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
  138. {
  139. puts ("mq_timedsend unexpectedly succeeded");
  140. result = 1;
  141. }
  142. else if (errno != ETIMEDOUT)
  143. {
  144. printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
  145. result = 1;
  146. }
  147. if (mq_close (q2) != 0)
  148. {
  149. printf ("mq_close failed: %m\n");
  150. result = 1;
  151. }
  152. q2 = mq_open (name, O_RDONLY, 0600, &attr);
  153. if (q2 == (mqd_t) -1)
  154. {
  155. printf ("mq_open without O_CREAT failed with %m\n");
  156. result = 1;
  157. }
  158. mqd_t q3 = mq_open (name, O_RDONLY, 0600, &attr);
  159. if (q3 == (mqd_t) -1)
  160. {
  161. printf ("mq_open without O_CREAT failed with %m\n");
  162. result = 1;
  163. }
  164. memset (buf, ' ', sizeof (buf));
  165. unsigned int prio;
  166. ssize_t rets = mq_receive (q2, buf, 2, &prio);
  167. if (rets != 1)
  168. {
  169. if (rets == -1)
  170. printf ("mq_receive failed with: %m\n");
  171. else
  172. printf ("mq_receive returned %zd != 1\n", rets);
  173. result = 1;
  174. }
  175. else if (prio != 5 || memcmp (buf, "k ", 3) != 0)
  176. {
  177. printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k \")\n",
  178. prio, buf[0], buf[1], buf[2]);
  179. result = 1;
  180. }
  181. if (mq_getattr (q3, &attr) != 0)
  182. {
  183. printf ("mq_getattr failed: %m\n");
  184. result = 1;
  185. }
  186. if ((attr.mq_flags & O_NONBLOCK)
  187. || attr.mq_maxmsg != 2
  188. || attr.mq_msgsize != 2
  189. || attr.mq_curmsgs != 1)
  190. {
  191. printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
  192. ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
  193. attr.mq_flags, attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
  194. result = 1;
  195. }
  196. rets = mq_receive (q3, buf, 2, NULL);
  197. if (rets != 2)
  198. {
  199. if (rets == -1)
  200. printf ("mq_receive failed with: %m\n");
  201. else
  202. printf ("mq_receive returned %zd != 2\n", rets);
  203. result = 1;
  204. }
  205. else if (memcmp (buf, "jk ", 3) != 0)
  206. {
  207. printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
  208. buf[0], buf[1], buf[2]);
  209. result = 1;
  210. }
  211. if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
  212. ++ts.tv_sec;
  213. else
  214. {
  215. ts.tv_sec = time (NULL) + 1;
  216. ts.tv_nsec = 0;
  217. }
  218. if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
  219. {
  220. puts ("mq_timedreceive on empty queue unexpectedly succeeded");
  221. result = 1;
  222. }
  223. else if (errno != ETIMEDOUT)
  224. {
  225. printf ("mq_timedreceive on empty queue did not fail with "
  226. "ETIMEDOUT: %m\n");
  227. result = 1;
  228. }
  229. if (mq_unlink (name) != 0)
  230. {
  231. printf ("mq_unlink failed: %m\n");
  232. result = 1;
  233. }
  234. if (mq_close (q) != 0)
  235. {
  236. printf ("mq_close failed: %m\n");
  237. result = 1;
  238. }
  239. if (mq_close (q2) != 0)
  240. {
  241. printf ("mq_close failed: %m\n");
  242. result = 1;
  243. }
  244. if (mq_close (q3) != 0)
  245. {
  246. printf ("mq_close failed: %m\n");
  247. result = 1;
  248. }
  249. return result;
  250. }
  251. #include "../test-skeleton.c"