1 /* Test message queue passing.
2 Copyright (C) 2004, 2007 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include "tst-mqueue.h"
33 intcmp (const void *a, const void *b)
35 if (*(unsigned char *)a < *(unsigned char *)b)
37 if (*(unsigned char *)a > *(unsigned char *)b)
43 check_attrs (struct mq_attr *attr, int nonblock, long cnt)
47 if (attr->mq_maxmsg != 10 || attr->mq_msgsize != 1)
49 printf ("attributes don't match those passed to mq_open\n"
50 "mq_maxmsg %ld, mq_msgsize %ld\n",
51 attr->mq_maxmsg, attr->mq_msgsize);
55 if ((attr->mq_flags & O_NONBLOCK) != nonblock)
57 printf ("mq_flags %lx != %x\n", (attr->mq_flags & O_NONBLOCK), nonblock);
61 if (attr->mq_curmsgs != cnt)
63 printf ("mq_curmsgs %ld != %ld\n", attr->mq_curmsgs, cnt);
71 do_one_test (mqd_t q, const char *name, int nonblock)
76 = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
79 memset (&attr, 0xaa, sizeof (attr));
80 if (mq_getattr (q, &attr) != 0)
82 printf ("mq_getattr failed: %m\n");
86 result |= check_attrs (&attr, nonblock, 0);
88 if (mq_receive (q, (char *) &v[0], 1, NULL) != -1)
90 puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
93 else if (errno != EBADF)
95 printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
100 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
104 ts.tv_sec = time (NULL) - 1;
109 for (int i = 0; i < 10; ++i)
112 ret = mq_send (q, (char *) &v[i], 1, v[i] >> 4);
114 ret = mq_timedsend (q, (char *) &v[i], 1, v[i] >> 4, &ts);
118 printf ("mq_%ssend failed: %m\n", (i & 1) ? "" : "timed");
123 ret = mq_timedsend (q, (char *) &v[10], 1, 8, &ts);
126 puts ("mq_timedsend on full queue did not fail");
129 else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
131 printf ("mq_timedsend on full queue did not fail with %s: %m\n",
132 nonblock ? "EAGAIN" : "ETIMEDOUT");
138 ret = mq_send (q, (char *) &v[10], 1, 8);
141 puts ("mq_send on full non-blocking queue did not fail");
144 else if (errno != EAGAIN)
146 printf ("mq_send on full non-blocking queue did not fail"
147 "with EAGAIN: %m\n");
152 memset (&attr, 0xaa, sizeof (attr));
153 if (mq_getattr (q, &attr) != 0)
155 printf ("mq_getattr failed: %m\n");
159 result |= check_attrs (&attr, nonblock, 10);
164 printf ("fork failed: %m\n");
171 if (mq_close (q) != 0)
173 printf ("mq_close in child failed: %m\n");
177 q = mq_open (name, O_RDONLY | nonblock);
180 printf ("mq_open in child failed: %m\n");
184 memset (&attr, 0xaa, sizeof (attr));
185 if (mq_getattr (q, &attr) != 0)
187 printf ("mq_getattr failed: %m\n");
191 result |= check_attrs (&attr, nonblock, 10);
193 unsigned char vr[11] = { };
197 if (mq_send (q, (char *) &v[0], 1, 1) != -1)
199 puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
202 else if (errno != EBADF)
204 printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
208 for (int i = 0; i < 10; ++i)
211 rets = mq_receive (q, (char *) &vr[i], 1, &prio);
213 rets = mq_timedreceive (q, (char *) &vr[i], 1, &prio, &ts);
218 printf ("mq_%sreceive failed: %m\n", (i & 1) ? "" : "timed");
220 printf ("mq_%sreceive returned %zd != 1\n",
221 (i & 1) ? "" : "timed", rets);
224 else if (prio != (unsigned int) vr[i] >> 4)
226 printf ("unexpected priority %x for value %02x\n", prio,
232 qsort (v, 10, 1, intcmp);
233 if (memcmp (v, vr, 10) != 0)
235 puts ("messages not received in expected order");
239 rets = mq_timedreceive (q, (char *) &vr[10], 1, &prio, &ts);
242 puts ("mq_timedreceive on empty queue did not fail");
245 else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
247 printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
248 nonblock ? "EAGAIN" : "ETIMEDOUT");
254 ret = mq_receive (q, (char *) &vr[10], 1, &prio);
257 puts ("mq_receive on empty non-blocking queue did not fail");
260 else if (errno != EAGAIN)
262 printf ("mq_receive on empty non-blocking queue did not fail"
263 "with EAGAIN: %m\n");
268 memset (&attr, 0xaa, sizeof (attr));
269 if (mq_getattr (q, &attr) != 0)
271 printf ("mq_getattr failed: %m\n");
275 result |= check_attrs (&attr, nonblock, 0);
277 if (mq_close (q) != 0)
279 printf ("mq_close in child failed: %m\n");
287 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
289 printf ("waitpid failed: %m\n");
293 else if (!WIFEXITED (status) || WEXITSTATUS (status))
295 printf ("child failed: %d\n", status);
299 memset (&attr, 0xaa, sizeof (attr));
300 if (mq_getattr (q, &attr) != 0)
302 printf ("mq_getattr failed: %m\n");
306 result |= check_attrs (&attr, nonblock, 0);
311 #define TEST_FUNCTION do_test ()
317 char name[sizeof "/tst-mqueue1-" + sizeof (pid_t) * 3];
318 snprintf (name, sizeof (name), "/tst-mqueue1-%u", getpid ());
320 struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
321 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
325 printf ("mq_open failed with: %m\n");
331 result |= do_one_test (q, name, 0);
333 mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
334 if (q2 == (mqd_t) -1)
336 printf ("mq_open failed with: %m\n");
342 if (mq_close (q) != 0)
344 printf ("mq_close in parent failed: %m\n");
349 result |= do_one_test (q, name, O_NONBLOCK);
351 if (mq_getattr (q, &attr) != 0)
353 printf ("mq_getattr failed: %m\n");
358 attr.mq_flags ^= O_NONBLOCK;
360 struct mq_attr attr2;
361 memset (&attr2, 0x55, sizeof (attr2));
362 if (mq_setattr (q, &attr, &attr2) != 0)
364 printf ("mq_setattr failed: %m\n");
367 else if (attr.mq_flags != (attr2.mq_flags ^ O_NONBLOCK)
368 || attr.mq_maxmsg != attr2.mq_maxmsg
369 || attr.mq_msgsize != attr2.mq_msgsize
370 || attr.mq_curmsgs != 0
371 || attr2.mq_curmsgs != 0)
373 puts ("mq_setattr returned unexpected values in *omqstat");
378 result |= do_one_test (q, name, 0);
380 if (mq_setattr (q, &attr2, NULL) != 0)
382 printf ("mq_setattr failed: %m\n");
386 result |= do_one_test (q, name, O_NONBLOCK);
391 if (mq_unlink (name) != 0)
393 printf ("mq_unlink failed: %m\n");
397 if (mq_close (q) != 0)
399 printf ("mq_close in parent failed: %m\n");
403 if (mq_close (q) != -1)
405 puts ("second mq_close did not fail");
408 else if (errno != EBADF)
410 printf ("second mq_close did not fail with EBADF: %m\n");
417 #include "../test-skeleton.c"