1 /* Test message queue passing.
2 Copyright (C) 2004-2023 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/>. */
29 #include <support/check.h>
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 %jd, mq_msgsize %jd\n",
51 (intmax_t) attr
->mq_maxmsg
, (intmax_t) attr
->mq_msgsize
);
55 if ((attr
->mq_flags
& O_NONBLOCK
) != nonblock
)
57 printf ("mq_flags %jx != %x\n",
58 (intmax_t) (attr
->mq_flags
& O_NONBLOCK
), nonblock
);
62 if (attr
->mq_curmsgs
!= cnt
)
64 printf ("mq_curmsgs %jd != %ld\n", (intmax_t) attr
->mq_curmsgs
, cnt
);
72 do_one_test (mqd_t q
, const char *name
, int nonblock
)
77 = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
80 memset (&attr
, 0xaa, sizeof (attr
));
81 if (mq_getattr (q
, &attr
) != 0)
83 printf ("mq_getattr failed: %m\n");
87 result
|= check_attrs (&attr
, nonblock
, 0);
89 if (mq_receive (q
, (char *) &v
[0], 1, NULL
) != -1)
91 puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
94 else if (errno
!= EBADF
)
96 printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
101 if (clock_gettime (CLOCK_REALTIME
, &ts
) == 0)
105 ts
.tv_sec
= time (NULL
) - 1;
110 for (int i
= 0; i
< 10; ++i
)
113 ret
= mq_send (q
, (char *) &v
[i
], 1, v
[i
] >> 4);
115 ret
= mq_timedsend (q
, (char *) &v
[i
], 1, v
[i
] >> 4, &ts
);
119 printf ("mq_%ssend failed: %m\n", (i
& 1) ? "" : "timed");
124 ret
= mq_timedsend (q
, (char *) &v
[10], 1, 8, &ts
);
127 puts ("mq_timedsend on full queue did not fail");
130 else if (errno
!= (nonblock
? EAGAIN
: ETIMEDOUT
))
132 printf ("mq_timedsend on full queue did not fail with %s: %m\n",
133 nonblock
? "EAGAIN" : "ETIMEDOUT");
139 ret
= mq_send (q
, (char *) &v
[10], 1, 8);
142 puts ("mq_send on full non-blocking queue did not fail");
145 else if (errno
!= EAGAIN
)
147 printf ("mq_send on full non-blocking queue did not fail"
148 "with EAGAIN: %m\n");
153 memset (&attr
, 0xaa, sizeof (attr
));
154 if (mq_getattr (q
, &attr
) != 0)
156 printf ("mq_getattr failed: %m\n");
160 result
|= check_attrs (&attr
, nonblock
, 10);
165 printf ("fork failed: %m\n");
172 if (mq_close (q
) != 0)
174 printf ("mq_close in child failed: %m\n");
178 q
= mq_open (name
, O_RDONLY
| nonblock
);
181 printf ("mq_open in child failed: %m\n");
185 memset (&attr
, 0xaa, sizeof (attr
));
186 if (mq_getattr (q
, &attr
) != 0)
188 printf ("mq_getattr failed: %m\n");
192 result
|= check_attrs (&attr
, nonblock
, 10);
194 unsigned char vr
[11] = { };
198 if (mq_send (q
, (char *) &v
[0], 1, 1) != -1)
200 puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
203 else if (errno
!= EBADF
)
205 printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
209 for (int i
= 0; i
< 10; ++i
)
212 rets
= mq_receive (q
, (char *) &vr
[i
], 1, &prio
);
214 rets
= mq_timedreceive (q
, (char *) &vr
[i
], 1, &prio
, &ts
);
219 printf ("mq_%sreceive failed: %m\n", (i
& 1) ? "" : "timed");
221 printf ("mq_%sreceive returned %zd != 1\n",
222 (i
& 1) ? "" : "timed", rets
);
225 else if (prio
!= (unsigned int) vr
[i
] >> 4)
227 printf ("unexpected priority %x for value %02x\n", prio
,
233 qsort (v
, 10, 1, intcmp
);
234 if (memcmp (v
, vr
, 10) != 0)
236 puts ("messages not received in expected order");
240 rets
= mq_timedreceive (q
, (char *) &vr
[10], 1, &prio
, &ts
);
243 puts ("mq_timedreceive on empty queue did not fail");
246 else if (errno
!= (nonblock
? EAGAIN
: ETIMEDOUT
))
248 printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
249 nonblock
? "EAGAIN" : "ETIMEDOUT");
255 ret
= mq_receive (q
, (char *) &vr
[10], 1, &prio
);
258 puts ("mq_receive on empty non-blocking queue did not fail");
261 else if (errno
!= EAGAIN
)
263 printf ("mq_receive on empty non-blocking queue did not fail"
264 "with EAGAIN: %m\n");
269 memset (&attr
, 0xaa, sizeof (attr
));
270 if (mq_getattr (q
, &attr
) != 0)
272 printf ("mq_getattr failed: %m\n");
276 result
|= check_attrs (&attr
, nonblock
, 0);
278 if (mq_close (q
) != 0)
280 printf ("mq_close in child failed: %m\n");
288 if (TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0)) != pid
)
290 printf ("waitpid failed: %m\n");
294 else if (!WIFEXITED (status
) || WEXITSTATUS (status
))
296 printf ("child failed: %d\n", status
);
300 memset (&attr
, 0xaa, sizeof (attr
));
301 if (mq_getattr (q
, &attr
) != 0)
303 printf ("mq_getattr failed: %m\n");
307 result
|= check_attrs (&attr
, nonblock
, 0);
312 #define TEST_FUNCTION do_test ()
318 char name
[sizeof "/tst-mqueue1-" + sizeof (pid_t
) * 3];
319 snprintf (name
, sizeof (name
), "/tst-mqueue1-%u", getpid ());
321 struct mq_attr attr
= { .mq_maxmsg
= 10, .mq_msgsize
= 1 };
322 mqd_t q
= mq_open (name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0600, &attr
);
327 FAIL_UNSUPPORTED ("mq_open not supported");
329 printf ("mq_open failed with: %m\n");
335 result
|= do_one_test (q
, name
, 0);
337 mqd_t q2
= mq_open (name
, O_WRONLY
| O_NONBLOCK
);
338 if (q2
== (mqd_t
) -1)
340 printf ("mq_open failed with: %m\n");
346 if (mq_close (q
) != 0)
348 printf ("mq_close in parent failed: %m\n");
353 result
|= do_one_test (q
, name
, O_NONBLOCK
);
355 if (mq_getattr (q
, &attr
) != 0)
357 printf ("mq_getattr failed: %m\n");
362 attr
.mq_flags
^= O_NONBLOCK
;
364 struct mq_attr attr2
;
365 memset (&attr2
, 0x55, sizeof (attr2
));
366 if (mq_setattr (q
, &attr
, &attr2
) != 0)
368 printf ("mq_setattr failed: %m\n");
371 else if (attr
.mq_flags
!= (attr2
.mq_flags
^ O_NONBLOCK
)
372 || attr
.mq_maxmsg
!= attr2
.mq_maxmsg
373 || attr
.mq_msgsize
!= attr2
.mq_msgsize
374 || attr
.mq_curmsgs
!= 0
375 || attr2
.mq_curmsgs
!= 0)
377 puts ("mq_setattr returned unexpected values in *omqstat");
382 result
|= do_one_test (q
, name
, 0);
384 if (mq_setattr (q
, &attr2
, NULL
) != 0)
386 printf ("mq_setattr failed: %m\n");
390 result
|= do_one_test (q
, name
, O_NONBLOCK
);
395 if (mq_unlink (name
) != 0)
397 printf ("mq_unlink failed: %m\n");
401 if (mq_close (q
) != 0)
403 printf ("mq_close in parent failed: %m\n");
407 if (mq_close (q
) != -1)
409 puts ("second mq_close did not fail");
412 else if (errno
!= EBADF
)
414 printf ("second mq_close did not fail with EBADF: %m\n");
421 #include "../test-skeleton.c"