1 /* Test message queue passing.
2 Copyright (C) 2004-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
32 #include <support/check.h>
33 #include "tst-mqueue.h"
35 #define TEST_FUNCTION do_test ()
41 char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX];
43 p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ());
44 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
45 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
50 FAIL_UNSUPPORTED ("mq_open not supported");
52 printf ("mq_open failed with: %m\n");
59 memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
60 name[NAME_MAX + 1] = '\0';
62 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
65 printf ("mq_open with NAME_MAX long name component failed with: %m\n");
69 if (mq_unlink (name) != 0)
71 printf ("mq_unlink failed: %m\n");
75 if (mq_close (q2) != 0)
77 printf ("mq_close failed: %m\n");
81 name[NAME_MAX + 1] = 'x';
82 name[NAME_MAX + 2] = '\0';
83 q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
86 puts ("mq_open with too long name component unexpectedly succeeded");
91 else if (errno != ENAMETOOLONG)
93 printf ("mq_open with too long name component did not fail with "
94 "ENAMETOOLONG: %m\n");
98 if (mq_unlink (name) == 0)
100 puts ("mq_unlink with too long name component unexpectedly succeeded");
103 else if (errno != ENAMETOOLONG)
105 printf ("mq_unlink with too long name component did not fail with "
106 "ENAMETOOLONG: %m\n");
113 q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
114 if (q2 == (mqd_t) -1)
116 printf ("mq_open without O_EXCL failed with %m\n");
122 if (mq_send (q, buf, 2, 4) != 0)
124 printf ("mq_send failed: %m\n");
128 if (mq_send (q, buf + 1, 1, 5) != 0)
130 printf ("mq_send failed: %m\n");
134 if (mq_getattr (q2, &attr) != 0)
136 printf ("mq_getattr failed: %m\n");
140 if ((attr.mq_flags & O_NONBLOCK)
141 || attr.mq_maxmsg != 2
142 || attr.mq_msgsize != 2
143 || attr.mq_curmsgs != 2)
145 printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
146 ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
147 (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
148 (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
153 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
157 ts.tv_sec = time (NULL) + 1;
161 if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
163 puts ("mq_timedsend unexpectedly succeeded");
166 else if (errno != ETIMEDOUT)
168 printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
172 if (mq_close (q2) != 0)
174 printf ("mq_close failed: %m\n");
178 q2 = mq_open (name, O_RDONLY);
179 if (q2 == (mqd_t) -1)
181 printf ("mq_open without O_CREAT failed with %m\n");
185 mqd_t q3 = mq_open (name, O_RDONLY);
186 if (q3 == (mqd_t) -1)
188 printf ("mq_open without O_CREAT failed with %m\n");
192 memset (buf, ' ', sizeof (buf));
195 ssize_t rets = mq_receive (q2, buf, 2, &prio);
199 printf ("mq_receive failed with: %m\n");
201 printf ("mq_receive returned %zd != 1\n", rets);
204 else if (prio != 5 || memcmp (buf, "k ", 3) != 0)
206 printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k \")\n",
207 prio, buf[0], buf[1], buf[2]);
211 if (mq_getattr (q3, &attr) != 0)
213 printf ("mq_getattr failed: %m\n");
217 if ((attr.mq_flags & O_NONBLOCK)
218 || attr.mq_maxmsg != 2
219 || attr.mq_msgsize != 2
220 || attr.mq_curmsgs != 1)
222 printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
223 ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
224 (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
225 (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
229 rets = mq_receive (q3, buf, 2, NULL);
233 printf ("mq_receive failed with: %m\n");
235 printf ("mq_receive returned %zd != 2\n", rets);
238 else if (memcmp (buf, "jk ", 3) != 0)
240 printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
241 buf[0], buf[1], buf[2]);
245 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
249 ts.tv_sec = time (NULL) + 1;
253 if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
255 puts ("mq_timedreceive on empty queue unexpectedly succeeded");
258 else if (errno != ETIMEDOUT)
260 printf ("mq_timedreceive on empty queue did not fail with "
265 if (mq_unlink (name) != 0)
267 printf ("mq_unlink failed: %m\n");
271 if (mq_close (q) != 0)
273 printf ("mq_close failed: %m\n");
277 if (mq_close (q2) != 0)
279 printf ("mq_close failed: %m\n");
283 if (mq_close (q3) != 0)
285 printf ("mq_close failed: %m\n");
292 #include "../test-skeleton.c"