tst-mqueue4.c 6.8 KB

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