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.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
31 #include "tst-mqueue.h"
34 alrm_handler (int sig)
39 #define TEST_FUNCTION do_test ()
45 char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
46 snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
48 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
49 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
53 printf ("mq_open failed with: %m\n");
59 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
62 puts ("mq_open with O_EXCL unexpectedly succeeded");
65 else if (errno != EEXIST)
67 printf ("mq_open did not fail with EEXIST: %m\n");
71 char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
72 snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
75 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
78 puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
82 else if (errno != EINVAL)
84 printf ("mq_open with invalid mq_maxmsg did not fail with "
90 attr.mq_msgsize = -56;
91 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
94 puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
98 else if (errno != EINVAL)
100 printf ("mq_open with invalid mq_msgsize did not fail with "
107 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
111 ts.tv_sec = time (NULL) + 10;
115 if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
117 puts ("mq_timedreceive with too small msg_len did not fail");
120 else if (errno != EMSGSIZE)
122 printf ("mq_timedreceive with too small msg_len did not fail with "
128 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
130 puts ("mq_timedreceive with negative tv_nsec did not fail");
133 else if (errno != EINVAL)
135 printf ("mq_timedreceive with negative tv_nsec did not fail with "
140 ts.tv_nsec = 1000000000;
141 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
143 puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
146 else if (errno != EINVAL)
148 printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
153 struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
154 sigemptyset (&sa.sa_mask);
155 sigaction (SIGALRM, &sa, NULL);
157 struct itimerval it = { .it_value = { .tv_sec = 1 } };
158 setitimer (ITIMER_REAL, &it, NULL);
160 if (mq_receive (q, buf, 2, NULL) == 0)
162 puts ("mq_receive on empty queue did not block");
165 else if (errno != EINTR)
167 printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
171 setitimer (ITIMER_REAL, &it, NULL);
174 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
176 puts ("mq_timedreceive on empty queue did not block");
179 else if (errno != EINTR)
181 printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
187 if (mq_send (q, buf, 2, 3) != 0
188 || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
190 printf ("mq_send failed: %m\n");
194 memset (buf, ' ', sizeof (buf));
197 ssize_t rets = mq_receive (q, buf, 3, &prio);
201 printf ("mq_receive failed: %m\n");
203 printf ("mq_receive returned %zd != 1\n", rets);
206 else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
208 printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
209 prio, buf[0], buf[1], buf[2]);
213 rets = mq_receive (q, buf, 2, NULL);
217 printf ("mq_receive failed: %m\n");
219 printf ("mq_receive returned %zd != 2\n", rets);
222 else if (memcmp (buf, "67 ", 3) != 0)
224 printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
225 buf[0], buf[1], buf[2]);
231 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
232 ts.tv_sec = time (NULL);
233 ts.tv_nsec = -1000000001;
234 if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
235 && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
236 || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
237 (mq_timedsend (q, buf, 1, 4, &ts) != 0
238 && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
240 printf ("mq_timedsend failed: %m\n");
245 ts.tv_nsec = 1000000001;
246 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
248 puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
251 else if (errno != EINVAL)
253 printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
259 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
261 puts ("mq_timedsend with negative tv_nsec did not fail");
264 else if (errno != EINVAL)
266 printf ("mq_timedsend with megatove tv_nsec did not fail with "
271 setitimer (ITIMER_REAL, &it, NULL);
273 if (mq_send (q, buf, 2, 8) == 0)
275 puts ("mq_send on full queue did not block");
278 else if (errno != EINTR)
280 printf ("mq_send on full queue did not fail with EINTR: %m\n");
284 setitimer (ITIMER_REAL, &it, NULL);
288 if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
290 puts ("mq_timedsend on full queue did not block");
293 else if (errno != EINTR)
295 printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
299 memset (buf, ' ', sizeof (buf));
301 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
302 ts.tv_sec = time (NULL);
303 ts.tv_nsec = -1000000001;
304 rets = mq_timedreceive (q, buf, 2, &prio, &ts);
305 if (rets == -1 && errno == EINVAL)
306 rets = mq_receive (q, buf, 2, &prio);
310 printf ("mq_timedreceive failed: %m\n");
312 printf ("mq_timedreceive returned %zd != 2\n", rets);
315 else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
317 printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
318 prio, buf[0], buf[1], buf[2]);
322 if (mq_receive (q, buf, 1, NULL) == 0)
324 puts ("mq_receive with too small msg_len did not fail");
327 else if (errno != EMSGSIZE)
329 printf ("mq_receive with too small msg_len did not fail with "
334 ts.tv_nsec = -ts.tv_nsec;
335 rets = mq_timedreceive (q, buf, 2, NULL, &ts);
336 if (rets == -1 && errno == EINVAL)
337 rets = mq_receive (q, buf, 2, NULL);
341 printf ("mq_timedreceive failed: %m\n");
343 printf ("mq_timedreceive returned %zd != 1\n", rets);
346 else if (memcmp (buf, "31 ", 3) != 0)
348 printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
349 buf[0], buf[1], buf[2]);
353 if (mq_send (q, "", 0, 2) != 0)
355 printf ("mq_send with msg_len 0 failed: %m\n");
359 rets = mq_receive (q, buf, 2, &prio);
363 printf ("mq_receive failed: %m\n");
365 printf ("mq_receive returned %zd != 0\n", rets);
369 long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
370 if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
372 if (mq_send (q, buf, 1, mq_prio_max) == 0)
374 puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
377 else if (errno != EINVAL)
379 printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
384 if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
386 printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
391 if (mq_unlink (name) != 0)
393 printf ("mq_unlink failed: %m\n");
397 q2 = mq_open (name, O_RDWR);
398 if (q2 != (mqd_t) -1)
400 printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
401 "succeeded\n", name);
404 else if (errno != ENOENT)
406 printf ("mq_open of unlinked %s without O_CREAT did not fail with "
407 "ENOENT: %m\n", name);
411 if (mq_close (q) != 0)
413 printf ("mq_close in parent failed: %m\n");
417 if (mq_receive (q, buf, 2, NULL) == 0)
419 puts ("mq_receive on invalid mqd_t did not fail");
422 else if (errno != EBADF)
424 printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
428 if (mq_send (q, buf, 1, 2) == 0)
430 puts ("mq_send on invalid mqd_t did not fail");
433 else if (errno != EBADF)
435 printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
439 if (mq_getattr (q, &attr) == 0)
441 puts ("mq_getattr on invalid mqd_t did not fail");
444 else if (errno != EBADF)
446 printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
450 memset (&attr, 0, sizeof (attr));
451 if (mq_setattr (q, &attr, NULL) == 0)
453 puts ("mq_setattr on invalid mqd_t did not fail");
456 else if (errno != EBADF)
458 printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
462 if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
464 puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
467 else if (errno != ENOENT)
469 printf ("mq_unlink of non-existant message queue did not fail with "
476 #include "../test-skeleton.c"