]>
git.ipfire.org Git - thirdparty/glibc.git/blob - rt/tst-mqueue2.c
1 /* Test message queue passing.
2 Copyright (C) 2004-2021 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 "tst-mqueue.h"
33 alrm_handler (int sig
)
37 #define TEST_FUNCTION do_test ()
43 char name
[sizeof "/tst-mqueue2-" + sizeof (pid_t
) * 3];
44 snprintf (name
, sizeof (name
), "/tst-mqueue2-%u", getpid ());
46 struct mq_attr attr
= { .mq_maxmsg
= 2, .mq_msgsize
= 2 };
47 mqd_t q
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
51 printf ("mq_open failed with: %m\n");
57 mqd_t q2
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
60 puts ("mq_open with O_EXCL unexpectedly succeeded");
63 else if (errno
!= EEXIST
)
65 printf ("mq_open did not fail with EEXIST: %m\n");
69 char name2
[sizeof "/tst-mqueue2-2-" + sizeof (pid_t
) * 3];
70 snprintf (name2
, sizeof (name2
), "/tst-mqueue2-2-%u", getpid ());
73 q2
= mq_open (name2
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
76 puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
80 else if (errno
!= EINVAL
)
82 printf ("mq_open with invalid mq_maxmsg did not fail with "
88 attr
.mq_msgsize
= -56;
89 q2
= mq_open (name2
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
92 puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
96 else if (errno
!= EINVAL
)
98 printf ("mq_open with invalid mq_msgsize did not fail with "
105 if (clock_gettime (CLOCK_REALTIME
, &ts
) == 0)
109 ts
.tv_sec
= time (NULL
) + 10;
113 if (mq_timedreceive (q
, buf
, 1, NULL
, &ts
) == 0)
115 puts ("mq_timedreceive with too small msg_len did not fail");
118 else if (errno
!= EMSGSIZE
)
120 printf ("mq_timedreceive with too small msg_len did not fail with "
126 if (mq_timedreceive (q
, buf
, 2, NULL
, &ts
) == 0)
128 puts ("mq_timedreceive with negative tv_nsec did not fail");
131 else if (errno
!= EINVAL
)
133 printf ("mq_timedreceive with negative tv_nsec did not fail with "
138 ts
.tv_nsec
= 1000000000;
139 if (mq_timedreceive (q
, buf
, 2, NULL
, &ts
) == 0)
141 puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
144 else if (errno
!= EINVAL
)
146 printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
151 struct sigaction sa
= { .sa_handler
= alrm_handler
, .sa_flags
= 0 };
152 sigemptyset (&sa
.sa_mask
);
153 sigaction (SIGALRM
, &sa
, NULL
);
155 struct itimerval it
= { .it_value
= { .tv_sec
= 1 } };
156 setitimer (ITIMER_REAL
, &it
, NULL
);
158 if (mq_receive (q
, buf
, 2, NULL
) == 0)
160 puts ("mq_receive on empty queue did not block");
163 else if (errno
!= EINTR
)
165 printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
169 setitimer (ITIMER_REAL
, &it
, NULL
);
172 if (mq_timedreceive (q
, buf
, 2, NULL
, &ts
) == 0)
174 puts ("mq_timedreceive on empty queue did not block");
177 else if (errno
!= EINTR
)
179 printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
185 if (mq_send (q
, buf
, 2, 3) != 0
186 || (buf
[0] = '8', mq_send (q
, buf
, 1, 4) != 0))
188 printf ("mq_send failed: %m\n");
192 memset (buf
, ' ', sizeof (buf
));
195 ssize_t rets
= mq_receive (q
, buf
, 3, &prio
);
199 printf ("mq_receive failed: %m\n");
201 printf ("mq_receive returned %zd != 1\n", rets
);
204 else if (prio
!= 4 || memcmp (buf
, "8 ", 3) != 0)
206 printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
207 prio
, buf
[0], buf
[1], buf
[2]);
211 rets
= mq_receive (q
, buf
, 2, NULL
);
215 printf ("mq_receive failed: %m\n");
217 printf ("mq_receive returned %zd != 2\n", rets
);
220 else if (memcmp (buf
, "67 ", 3) != 0)
222 printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
223 buf
[0], buf
[1], buf
[2]);
229 if (clock_gettime (CLOCK_REALTIME
, &ts
) != 0)
230 ts
.tv_sec
= time (NULL
);
231 ts
.tv_nsec
= -1000000001;
232 if ((mq_timedsend (q
, buf
, 2, 5, &ts
) != 0
233 && (errno
!= EINVAL
|| mq_send (q
, buf
, 2, 5) != 0))
234 || (buf
[0] = '3', ts
.tv_nsec
= -ts
.tv_nsec
,
235 (mq_timedsend (q
, buf
, 1, 4, &ts
) != 0
236 && (errno
!= EINVAL
|| mq_send (q
, buf
, 1, 4) != 0))))
238 printf ("mq_timedsend failed: %m\n");
243 ts
.tv_nsec
= 1000000001;
244 if (mq_timedsend (q
, buf
, 1, 6, &ts
) == 0)
246 puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
249 else if (errno
!= EINVAL
)
251 printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
257 if (mq_timedsend (q
, buf
, 1, 6, &ts
) == 0)
259 puts ("mq_timedsend with negative tv_nsec did not fail");
262 else if (errno
!= EINVAL
)
264 printf ("mq_timedsend with megatove tv_nsec did not fail with "
269 setitimer (ITIMER_REAL
, &it
, NULL
);
271 if (mq_send (q
, buf
, 2, 8) == 0)
273 puts ("mq_send on full queue did not block");
276 else if (errno
!= EINTR
)
278 printf ("mq_send on full queue did not fail with EINTR: %m\n");
282 setitimer (ITIMER_REAL
, &it
, NULL
);
286 if (mq_timedsend (q
, buf
, 2, 7, &ts
) == 0)
288 puts ("mq_timedsend on full queue did not block");
291 else if (errno
!= EINTR
)
293 printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
297 memset (buf
, ' ', sizeof (buf
));
299 if (clock_gettime (CLOCK_REALTIME
, &ts
) != 0)
300 ts
.tv_sec
= time (NULL
);
301 ts
.tv_nsec
= -1000000001;
302 rets
= mq_timedreceive (q
, buf
, 2, &prio
, &ts
);
303 if (rets
== -1 && errno
== EINVAL
)
304 rets
= mq_receive (q
, buf
, 2, &prio
);
308 printf ("mq_timedreceive failed: %m\n");
310 printf ("mq_timedreceive returned %zd != 2\n", rets
);
313 else if (prio
!= 5 || memcmp (buf
, "21 ", 3) != 0)
315 printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
316 prio
, buf
[0], buf
[1], buf
[2]);
320 if (mq_receive (q
, buf
, 1, NULL
) == 0)
322 puts ("mq_receive with too small msg_len did not fail");
325 else if (errno
!= EMSGSIZE
)
327 printf ("mq_receive with too small msg_len did not fail with "
332 ts
.tv_nsec
= -ts
.tv_nsec
;
333 rets
= mq_timedreceive (q
, buf
, 2, NULL
, &ts
);
334 if (rets
== -1 && errno
== EINVAL
)
335 rets
= mq_receive (q
, buf
, 2, NULL
);
339 printf ("mq_timedreceive failed: %m\n");
341 printf ("mq_timedreceive returned %zd != 1\n", rets
);
344 else if (memcmp (buf
, "31 ", 3) != 0)
346 printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
347 buf
[0], buf
[1], buf
[2]);
351 if (mq_send (q
, "", 0, 2) != 0)
353 printf ("mq_send with msg_len 0 failed: %m\n");
357 rets
= mq_receive (q
, buf
, 2, &prio
);
361 printf ("mq_receive failed: %m\n");
363 printf ("mq_receive returned %zd != 0\n", rets
);
367 long mq_prio_max
= sysconf (_SC_MQ_PRIO_MAX
);
368 if (mq_prio_max
> 0 && (unsigned int) mq_prio_max
== mq_prio_max
)
370 if (mq_send (q
, buf
, 1, mq_prio_max
) == 0)
372 puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
375 else if (errno
!= EINVAL
)
377 printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
382 if (mq_send (q
, buf
, 1, mq_prio_max
- 1) != 0)
384 printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
389 if (mq_unlink (name
) != 0)
391 printf ("mq_unlink failed: %m\n");
395 q2
= mq_open (name
, O_RDWR
);
396 if (q2
!= (mqd_t
) -1)
398 printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
399 "succeeded\n", name
);
402 else if (errno
!= ENOENT
)
404 printf ("mq_open of unlinked %s without O_CREAT did not fail with "
405 "ENOENT: %m\n", name
);
409 if (mq_close (q
) != 0)
411 printf ("mq_close in parent failed: %m\n");
415 if (mq_receive (q
, buf
, 2, NULL
) == 0)
417 puts ("mq_receive on invalid mqd_t did not fail");
420 else if (errno
!= EBADF
)
422 printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
426 if (mq_send (q
, buf
, 1, 2) == 0)
428 puts ("mq_send on invalid mqd_t did not fail");
431 else if (errno
!= EBADF
)
433 printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
437 if (mq_getattr (q
, &attr
) == 0)
439 puts ("mq_getattr on invalid mqd_t did not fail");
442 else if (errno
!= EBADF
)
444 printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
448 memset (&attr
, 0, sizeof (attr
));
449 if (mq_setattr (q
, &attr
, NULL
) == 0)
451 puts ("mq_setattr on invalid mqd_t did not fail");
454 else if (errno
!= EBADF
)
456 printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
460 if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
462 puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
465 else if (errno
!= ENOENT
)
467 printf ("mq_unlink of non-existant message queue did not fail with "
474 #include "../test-skeleton.c"