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, see
18 <http://www.gnu.org/licenses/>. */
29 #include "tst-mqueue.h"
32 intcmp (const void *a
, const void *b
)
34 if (*(unsigned char *)a
< *(unsigned char *)b
)
36 if (*(unsigned char *)a
> *(unsigned char *)b
)
42 check_attrs (struct mq_attr
*attr
, int nonblock
, long cnt
)
46 if (attr
->mq_maxmsg
!= 10 || attr
->mq_msgsize
!= 1)
48 printf ("attributes don't match those passed to mq_open\n"
49 "mq_maxmsg %ld, mq_msgsize %ld\n",
50 attr
->mq_maxmsg
, attr
->mq_msgsize
);
54 if ((attr
->mq_flags
& O_NONBLOCK
) != nonblock
)
56 printf ("mq_flags %lx != %x\n", (attr
->mq_flags
& O_NONBLOCK
), nonblock
);
60 if (attr
->mq_curmsgs
!= cnt
)
62 printf ("mq_curmsgs %ld != %ld\n", attr
->mq_curmsgs
, cnt
);
70 do_one_test (mqd_t q
, const char *name
, int nonblock
)
75 = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
78 memset (&attr
, 0xaa, sizeof (attr
));
79 if (mq_getattr (q
, &attr
) != 0)
81 printf ("mq_getattr failed: %m\n");
85 result
|= check_attrs (&attr
, nonblock
, 0);
87 if (mq_receive (q
, (char *) &v
[0], 1, NULL
) != -1)
89 puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
92 else if (errno
!= EBADF
)
94 printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
99 if (clock_gettime (CLOCK_REALTIME
, &ts
) == 0)
103 ts
.tv_sec
= time (NULL
) - 1;
108 for (int i
= 0; i
< 10; ++i
)
111 ret
= mq_send (q
, (char *) &v
[i
], 1, v
[i
] >> 4);
113 ret
= mq_timedsend (q
, (char *) &v
[i
], 1, v
[i
] >> 4, &ts
);
117 printf ("mq_%ssend failed: %m\n", (i
& 1) ? "" : "timed");
122 ret
= mq_timedsend (q
, (char *) &v
[10], 1, 8, &ts
);
125 puts ("mq_timedsend on full queue did not fail");
128 else if (errno
!= (nonblock
? EAGAIN
: ETIMEDOUT
))
130 printf ("mq_timedsend on full queue did not fail with %s: %m\n",
131 nonblock
? "EAGAIN" : "ETIMEDOUT");
137 ret
= mq_send (q
, (char *) &v
[10], 1, 8);
140 puts ("mq_send on full non-blocking queue did not fail");
143 else if (errno
!= EAGAIN
)
145 printf ("mq_send on full non-blocking queue did not fail"
146 "with EAGAIN: %m\n");
151 memset (&attr
, 0xaa, sizeof (attr
));
152 if (mq_getattr (q
, &attr
) != 0)
154 printf ("mq_getattr failed: %m\n");
158 result
|= check_attrs (&attr
, nonblock
, 10);
163 printf ("fork failed: %m\n");
170 if (mq_close (q
) != 0)
172 printf ("mq_close in child failed: %m\n");
176 q
= mq_open (name
, O_RDONLY
| nonblock
);
179 printf ("mq_open in child failed: %m\n");
183 memset (&attr
, 0xaa, sizeof (attr
));
184 if (mq_getattr (q
, &attr
) != 0)
186 printf ("mq_getattr failed: %m\n");
190 result
|= check_attrs (&attr
, nonblock
, 10);
192 unsigned char vr
[11] = { };
196 if (mq_send (q
, (char *) &v
[0], 1, 1) != -1)
198 puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
201 else if (errno
!= EBADF
)
203 printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
207 for (int i
= 0; i
< 10; ++i
)
210 rets
= mq_receive (q
, (char *) &vr
[i
], 1, &prio
);
212 rets
= mq_timedreceive (q
, (char *) &vr
[i
], 1, &prio
, &ts
);
217 printf ("mq_%sreceive failed: %m\n", (i
& 1) ? "" : "timed");
219 printf ("mq_%sreceive returned %zd != 1\n",
220 (i
& 1) ? "" : "timed", rets
);
223 else if (prio
!= (unsigned int) vr
[i
] >> 4)
225 printf ("unexpected priority %x for value %02x\n", prio
,
231 qsort (v
, 10, 1, intcmp
);
232 if (memcmp (v
, vr
, 10) != 0)
234 puts ("messages not received in expected order");
238 rets
= mq_timedreceive (q
, (char *) &vr
[10], 1, &prio
, &ts
);
241 puts ("mq_timedreceive on empty queue did not fail");
244 else if (errno
!= (nonblock
? EAGAIN
: ETIMEDOUT
))
246 printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
247 nonblock
? "EAGAIN" : "ETIMEDOUT");
253 ret
= mq_receive (q
, (char *) &vr
[10], 1, &prio
);
256 puts ("mq_receive on empty non-blocking queue did not fail");
259 else if (errno
!= EAGAIN
)
261 printf ("mq_receive on empty non-blocking queue did not fail"
262 "with EAGAIN: %m\n");
267 memset (&attr
, 0xaa, sizeof (attr
));
268 if (mq_getattr (q
, &attr
) != 0)
270 printf ("mq_getattr failed: %m\n");
274 result
|= check_attrs (&attr
, nonblock
, 0);
276 if (mq_close (q
) != 0)
278 printf ("mq_close in child failed: %m\n");
286 if (TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0)) != pid
)
288 printf ("waitpid failed: %m\n");
292 else if (!WIFEXITED (status
) || WEXITSTATUS (status
))
294 printf ("child failed: %d\n", status
);
298 memset (&attr
, 0xaa, sizeof (attr
));
299 if (mq_getattr (q
, &attr
) != 0)
301 printf ("mq_getattr failed: %m\n");
305 result
|= check_attrs (&attr
, nonblock
, 0);
310 #define TEST_FUNCTION do_test ()
316 char name
[sizeof "/tst-mqueue1-" + sizeof (pid_t
) * 3];
317 snprintf (name
, sizeof (name
), "/tst-mqueue1-%u", getpid ());
319 struct mq_attr attr
= { .mq_maxmsg
= 10, .mq_msgsize
= 1 };
320 mqd_t q
= mq_open (name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0600, &attr
);
324 printf ("mq_open failed with: %m\n");
330 result
|= do_one_test (q
, name
, 0);
332 mqd_t q2
= mq_open (name
, O_WRONLY
| O_NONBLOCK
);
333 if (q2
== (mqd_t
) -1)
335 printf ("mq_open failed with: %m\n");
341 if (mq_close (q
) != 0)
343 printf ("mq_close in parent failed: %m\n");
348 result
|= do_one_test (q
, name
, O_NONBLOCK
);
350 if (mq_getattr (q
, &attr
) != 0)
352 printf ("mq_getattr failed: %m\n");
357 attr
.mq_flags
^= O_NONBLOCK
;
359 struct mq_attr attr2
;
360 memset (&attr2
, 0x55, sizeof (attr2
));
361 if (mq_setattr (q
, &attr
, &attr2
) != 0)
363 printf ("mq_setattr failed: %m\n");
366 else if (attr
.mq_flags
!= (attr2
.mq_flags
^ O_NONBLOCK
)
367 || attr
.mq_maxmsg
!= attr2
.mq_maxmsg
368 || attr
.mq_msgsize
!= attr2
.mq_msgsize
369 || attr
.mq_curmsgs
!= 0
370 || attr2
.mq_curmsgs
!= 0)
372 puts ("mq_setattr returned unexpected values in *omqstat");
377 result
|= do_one_test (q
, name
, 0);
379 if (mq_setattr (q
, &attr2
, NULL
) != 0)
381 printf ("mq_setattr failed: %m\n");
385 result
|= do_one_test (q
, name
, O_NONBLOCK
);
390 if (mq_unlink (name
) != 0)
392 printf ("mq_unlink failed: %m\n");
396 if (mq_close (q
) != 0)
398 printf ("mq_close in parent failed: %m\n");
402 if (mq_close (q
) != -1)
404 puts ("second mq_close did not fail");
407 else if (errno
!= EBADF
)
409 printf ("second mq_close did not fail with EBADF: %m\n");
416 #include "../test-skeleton.c"