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/>. */
30 #include <support/check.h>
31 #include "tst-mqueue.h"
34 alrm_handler (int sig)
38 #define TEST_FUNCTION do_test ()
44 char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
45 snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
47 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
48 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
53 FAIL_UNSUPPORTED ("mq_open not supported");
55 printf ("mq_open failed with: %m\n");
61 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
64 puts ("mq_open with O_EXCL unexpectedly succeeded");
67 else if (errno != EEXIST)
69 printf ("mq_open did not fail with EEXIST: %m\n");
73 char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
74 snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
77 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
80 puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
84 else if (errno != EINVAL)
86 printf ("mq_open with invalid mq_maxmsg did not fail with "
92 attr.mq_msgsize = -56;
93 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
96 puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
100 else if (errno != EINVAL)
102 printf ("mq_open with invalid mq_msgsize did not fail with "
109 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
113 ts.tv_sec = time (NULL) + 10;
117 if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
119 puts ("mq_timedreceive with too small msg_len did not fail");
122 else if (errno != EMSGSIZE)
124 printf ("mq_timedreceive with too small msg_len did not fail with "
130 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
132 puts ("mq_timedreceive with negative tv_nsec did not fail");
135 else if (errno != EINVAL)
137 printf ("mq_timedreceive with negative tv_nsec did not fail with "
142 ts.tv_nsec = 1000000000;
143 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
145 puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
148 else if (errno != EINVAL)
150 printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
155 struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
156 sigemptyset (&sa.sa_mask);
157 sigaction (SIGALRM, &sa, NULL);
159 struct itimerval it = { .it_value = { .tv_sec = 1 } };
160 setitimer (ITIMER_REAL, &it, NULL);
162 if (mq_receive (q, buf, 2, NULL) == 0)
164 puts ("mq_receive on empty queue did not block");
167 else if (errno != EINTR)
169 printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
173 setitimer (ITIMER_REAL, &it, NULL);
176 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
178 puts ("mq_timedreceive on empty queue did not block");
181 else if (errno != EINTR)
183 printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
189 if (mq_send (q, buf, 2, 3) != 0
190 || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
192 printf ("mq_send failed: %m\n");
196 memset (buf, ' ', sizeof (buf));
199 ssize_t rets = mq_receive (q, buf, 3, &prio);
203 printf ("mq_receive failed: %m\n");
205 printf ("mq_receive returned %zd != 1\n", rets);
208 else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
210 printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
211 prio, buf[0], buf[1], buf[2]);
215 rets = mq_receive (q, buf, 2, NULL);
219 printf ("mq_receive failed: %m\n");
221 printf ("mq_receive returned %zd != 2\n", rets);
224 else if (memcmp (buf, "67 ", 3) != 0)
226 printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
227 buf[0], buf[1], buf[2]);
233 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
234 ts.tv_sec = time (NULL);
235 ts.tv_nsec = -1000000001;
236 if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
237 && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
238 || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
239 (mq_timedsend (q, buf, 1, 4, &ts) != 0
240 && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
242 printf ("mq_timedsend failed: %m\n");
247 ts.tv_nsec = 1000000001;
248 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
250 puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
253 else if (errno != EINVAL)
255 printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
261 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
263 puts ("mq_timedsend with negative tv_nsec did not fail");
266 else if (errno != EINVAL)
268 printf ("mq_timedsend with megatove tv_nsec did not fail with "
273 setitimer (ITIMER_REAL, &it, NULL);
275 if (mq_send (q, buf, 2, 8) == 0)
277 puts ("mq_send on full queue did not block");
280 else if (errno != EINTR)
282 printf ("mq_send on full queue did not fail with EINTR: %m\n");
286 setitimer (ITIMER_REAL, &it, NULL);
290 if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
292 puts ("mq_timedsend on full queue did not block");
295 else if (errno != EINTR)
297 printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
301 memset (buf, ' ', sizeof (buf));
303 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
304 ts.tv_sec = time (NULL);
305 ts.tv_nsec = -1000000001;
306 rets = mq_timedreceive (q, buf, 2, &prio, &ts);
307 if (rets == -1 && errno == EINVAL)
308 rets = mq_receive (q, buf, 2, &prio);
312 printf ("mq_timedreceive failed: %m\n");
314 printf ("mq_timedreceive returned %zd != 2\n", rets);
317 else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
319 printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
320 prio, buf[0], buf[1], buf[2]);
324 if (mq_receive (q, buf, 1, NULL) == 0)
326 puts ("mq_receive with too small msg_len did not fail");
329 else if (errno != EMSGSIZE)
331 printf ("mq_receive with too small msg_len did not fail with "
336 ts.tv_nsec = -ts.tv_nsec;
337 rets = mq_timedreceive (q, buf, 2, NULL, &ts);
338 if (rets == -1 && errno == EINVAL)
339 rets = mq_receive (q, buf, 2, NULL);
343 printf ("mq_timedreceive failed: %m\n");
345 printf ("mq_timedreceive returned %zd != 1\n", rets);
348 else if (memcmp (buf, "31 ", 3) != 0)
350 printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
351 buf[0], buf[1], buf[2]);
355 if (mq_send (q, "", 0, 2) != 0)
357 printf ("mq_send with msg_len 0 failed: %m\n");
361 rets = mq_receive (q, buf, 2, &prio);
365 printf ("mq_receive failed: %m\n");
367 printf ("mq_receive returned %zd != 0\n", rets);
371 long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
372 if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
374 if (mq_send (q, buf, 1, mq_prio_max) == 0)
376 puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
379 else if (errno != EINVAL)
381 printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
386 if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
388 printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
393 if (mq_unlink (name) != 0)
395 printf ("mq_unlink failed: %m\n");
399 q2 = mq_open (name, O_RDWR);
400 if (q2 != (mqd_t) -1)
402 printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
403 "succeeded\n", name);
406 else if (errno != ENOENT)
408 printf ("mq_open of unlinked %s without O_CREAT did not fail with "
409 "ENOENT: %m\n", name);
413 if (mq_close (q) != 0)
415 printf ("mq_close in parent failed: %m\n");
419 if (mq_receive (q, buf, 2, NULL) == 0)
421 puts ("mq_receive on invalid mqd_t did not fail");
424 else if (errno != EBADF)
426 printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
430 if (mq_send (q, buf, 1, 2) == 0)
432 puts ("mq_send on invalid mqd_t did not fail");
435 else if (errno != EBADF)
437 printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
441 if (mq_getattr (q, &attr) == 0)
443 puts ("mq_getattr on invalid mqd_t did not fail");
446 else if (errno != EBADF)
448 printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
452 memset (&attr, 0, sizeof (attr));
453 if (mq_setattr (q, &attr, NULL) == 0)
455 puts ("mq_setattr on invalid mqd_t did not fail");
458 else if (errno != EBADF)
460 printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
464 if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
466 puts ("mq_unlink of non-existent message queue unexpectedly succeeded");
469 else if (errno != ENOENT)
471 printf ("mq_unlink of non-existent message queue did not fail with "
478 #include "../test-skeleton.c"