Fix warnings in __bswap_16.
[glibc.git] / rt / tst-mqueue5.c
blob97571da8abd89f8a6416c4f976856031d53db504
1 /* Test mq_notify.
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <mqueue.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/mman.h>
31 #include <sys/time.h>
32 #include <sys/wait.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include "tst-mqueue.h"
37 #define TIMEOUT 3
39 #if _POSIX_THREADS
40 # include <pthread.h>
42 volatile int rtmin_cnt;
43 volatile pid_t rtmin_pid;
44 volatile uid_t rtmin_uid;
45 volatile int rtmin_code;
46 volatile union sigval rtmin_sigval;
48 static void
49 rtmin_handler (int sig, siginfo_t *info, void *ctx)
51 if (sig != SIGRTMIN)
52 abort ();
53 ++rtmin_cnt;
54 rtmin_pid = info->si_pid;
55 rtmin_uid = info->si_uid;
56 rtmin_code = info->si_code;
57 rtmin_sigval = info->si_value;
60 #define mqsend(q) (mqsend) (q, __LINE__)
61 static int
62 (mqsend) (mqd_t q, int line)
64 char c;
65 if (mq_send (q, &c, 1, 1) != 0)
67 printf ("mq_send on line %d failed with: %m\n", line);
68 return 1;
70 return 0;
73 #define mqrecv(q) (mqrecv) (q, __LINE__)
74 static int
75 (mqrecv) (mqd_t q, int line)
77 char c;
78 ssize_t rets = TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
79 if (rets != 1)
81 if (rets == -1)
82 printf ("mq_receive on line %d failed with: %m\n", line);
83 else
84 printf ("mq_receive on line %d returned %zd != 1\n",
85 line, rets);
86 return 1;
88 return 0;
91 struct thr_data
93 const char *name;
94 pthread_barrier_t *b3;
95 mqd_t q;
98 static void *
99 thr (void *arg)
101 pthread_barrier_t *b3 = ((struct thr_data *)arg)->b3;
102 mqd_t q = ((struct thr_data *)arg)->q;
103 const char *name = ((struct thr_data *)arg)->name;
104 int result = 0;
106 result |= mqrecv (q);
108 (void) pthread_barrier_wait (b3);
110 /* Child verifies SIGRTMIN has not been sent. */
112 (void) pthread_barrier_wait (b3);
114 /* Parent calls mqsend (q), which should trigger notification. */
116 (void) pthread_barrier_wait (b3);
118 if (rtmin_cnt != 2)
120 puts ("SIGRTMIN signal in child did not arrive");
121 result = 1;
123 else if (rtmin_pid != getppid ()
124 || rtmin_uid != getuid ()
125 || rtmin_code != SI_MESGQ
126 || rtmin_sigval.sival_int != 0xdeadbeef)
128 printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (%d)\n",
129 rtmin_pid, getppid (), rtmin_uid, getuid (),
130 rtmin_code, SI_MESGQ, rtmin_sigval.sival_int, 0xdeadbeef);
131 result = 1;
134 struct sigevent ev;
135 memset (&ev, 0x82, sizeof (ev));
136 ev.sigev_notify = SIGEV_NONE;
137 if (mq_notify (q, &ev) != 0)
139 printf ("mq_notify in thread (q, { SIGEV_NONE }) failed with: %m\n");
140 result = 1;
143 if (mq_notify (q, NULL) != 0)
145 printf ("mq_notify in thread (q, NULL) failed with: %m\n");
146 result = 1;
149 result |= mqrecv (q);
151 (void) pthread_barrier_wait (b3);
153 /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
155 (void) pthread_barrier_wait (b3);
157 if (mq_notify (q, NULL) != 0)
159 printf ("second mq_notify in thread (q, NULL) failed with: %m\n");
160 result = 1;
163 (void) pthread_barrier_wait (b3);
165 /* Parent calls mqsend (q), which should not trigger notification. */
167 (void) pthread_barrier_wait (b3);
169 /* Child verifies SIGRTMIN has not been received. */
170 /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
172 (void) pthread_barrier_wait (b3);
174 mqd_t q4 = mq_open (name, O_RDONLY);
175 if (q4 == (mqd_t) -1)
177 printf ("mq_open in thread failed with: %m\n");
178 result = 1;
181 if (mq_notify (q4, NULL) != 0)
183 printf ("mq_notify in thread (q4, NULL) failed with: %m\n");
184 result = 1;
187 if (mq_close (q4) != 0)
189 printf ("mq_close in thread failed with: %m\n");
190 result = 1;
193 (void) pthread_barrier_wait (b3);
195 /* Parent calls mqsend (q), which should not trigger notification. */
197 (void) pthread_barrier_wait (b3);
199 /* Child verifies SIGRTMIN has not been received. */
200 /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
202 (void) pthread_barrier_wait (b3);
204 mqd_t q5 = mq_open (name, O_WRONLY);
205 if (q5 == (mqd_t) -1)
207 printf ("mq_open O_WRONLY in thread failed with: %m\n");
208 result = 1;
211 if (mq_notify (q5, NULL) != 0)
213 printf ("mq_notify in thread (q5, NULL) failed with: %m\n");
214 result = 1;
217 if (mq_close (q5) != 0)
219 printf ("mq_close in thread failed with: %m\n");
220 result = 1;
223 (void) pthread_barrier_wait (b3);
225 /* Parent calls mqsend (q), which should not trigger notification. */
227 (void) pthread_barrier_wait (b3);
229 /* Child verifies SIGRTMIN has not been received. */
231 return (void *) (long) result;
234 static void
235 do_child (const char *name, pthread_barrier_t *b2, pthread_barrier_t *b3,
236 mqd_t q)
238 int result = 0;
240 struct sigevent ev;
241 memset (&ev, 0x55, sizeof (ev));
242 ev.sigev_notify = SIGEV_SIGNAL;
243 ev.sigev_signo = SIGRTMIN;
244 ev.sigev_value.sival_ptr = &ev;
245 if (mq_notify (q, &ev) == 0)
247 puts ("first mq_notify in child (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
248 result = 1;
250 else if (errno != EBUSY)
252 printf ("first mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
253 result = 1;
256 (void) pthread_barrier_wait (b2);
258 /* Parent calls mqsend (q), which makes notification available. */
260 (void) pthread_barrier_wait (b2);
262 rtmin_cnt = 0;
264 if (mq_notify (q, &ev) != 0)
266 printf ("second mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
267 result = 1;
270 if (rtmin_cnt != 0)
272 puts ("SIGRTMIN signal in child caught too early");
273 result = 1;
276 (void) pthread_barrier_wait (b2);
278 /* Parent unsuccessfully attempts to mq_notify. */
279 /* Parent calls mqsend (q), which makes notification available
280 and triggers a signal in the child. */
281 /* Parent successfully calls mq_notify SIGEV_SIGNAL. */
283 (void) pthread_barrier_wait (b2);
285 if (rtmin_cnt != 1)
287 puts ("SIGRTMIN signal in child did not arrive");
288 result = 1;
290 else if (rtmin_pid != getppid ()
291 || rtmin_uid != getuid ()
292 || rtmin_code != SI_MESGQ
293 || rtmin_sigval.sival_ptr != &ev)
295 printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_ptr %p (%p)\n",
296 rtmin_pid, getppid (), rtmin_uid, getuid (),
297 rtmin_code, SI_MESGQ, rtmin_sigval.sival_ptr, &ev);
298 result = 1;
301 result |= mqsend (q);
303 (void) pthread_barrier_wait (b2);
305 /* Parent verifies caught SIGRTMIN. */
307 mqd_t q2 = mq_open (name, O_RDWR);
308 if (q2 == (mqd_t) -1)
310 printf ("mq_open in child failed with: %m\n");
311 result = 1;
314 (void) pthread_barrier_wait (b2);
316 /* Parent mq_open's another mqd_t for the same queue (q3). */
318 memset (&ev, 0x11, sizeof (ev));
319 ev.sigev_notify = SIGEV_SIGNAL;
320 ev.sigev_signo = SIGRTMIN;
321 ev.sigev_value.sival_ptr = &ev;
322 if (mq_notify (q2, &ev) != 0)
324 printf ("mq_notify in child (q2, { SIGEV_SIGNAL }) failed with: %m\n");
325 result = 1;
328 (void) pthread_barrier_wait (b2);
330 /* Parent unsuccessfully attempts to mq_notify { SIGEV_NONE } on q. */
332 (void) pthread_barrier_wait (b2);
334 if (mq_close (q2) != 0)
336 printf ("mq_close failed: %m\n");
337 result = 1;
340 (void) pthread_barrier_wait (b2);
342 /* Parent successfully calls mq_notify { SIGEV_NONE } on q3. */
344 (void) pthread_barrier_wait (b2);
346 memset (&ev, 0xbb, sizeof (ev));
347 ev.sigev_notify = SIGEV_SIGNAL;
348 ev.sigev_signo = SIGRTMIN;
349 ev.sigev_value.sival_ptr = &b2;
350 if (mq_notify (q, &ev) == 0)
352 puts ("third mq_notify in child (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
353 result = 1;
355 else if (errno != EBUSY)
357 printf ("third mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
358 result = 1;
361 (void) pthread_barrier_wait (b2);
363 /* Parent calls mq_close on q3, which makes the queue available again for
364 notification. */
366 (void) pthread_barrier_wait (b2);
368 memset (&ev, 0x13, sizeof (ev));
369 ev.sigev_notify = SIGEV_NONE;
370 if (mq_notify (q, &ev) != 0)
372 printf ("mq_notify in child (q, { SIGEV_NONE }) failed with: %m\n");
373 result = 1;
376 if (mq_notify (q, NULL) != 0)
378 printf ("mq_notify in child (q, NULL) failed with: %m\n");
379 result = 1;
382 (void) pthread_barrier_wait (b2);
384 struct thr_data thr_data = { .name = name, .b3 = b3, .q = q };
385 pthread_t th;
386 int ret = pthread_create (&th, NULL, thr, &thr_data);
387 if (ret)
389 errno = ret;
390 printf ("pthread_created failed with: %m\n");
391 result = 1;
394 /* Wait till thr calls mq_receive on the empty queue q and blocks on it. */
395 sleep (1);
397 memset (&ev, 0x5f, sizeof (ev));
398 ev.sigev_notify = SIGEV_SIGNAL;
399 ev.sigev_signo = SIGRTMIN;
400 ev.sigev_value.sival_int = 0xdeadbeef;
401 if (mq_notify (q, &ev) != 0)
403 printf ("fourth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
404 result = 1;
407 (void) pthread_barrier_wait (b2);
409 /* Parent calls mqsend (q), which should wake up mqrecv (q)
410 in the thread but no notification should be sent. */
412 (void) pthread_barrier_wait (b3);
414 if (rtmin_cnt != 1)
416 puts ("SIGRTMIN signal caught while thr was blocked on mq_receive");
417 result = 1;
420 (void) pthread_barrier_wait (b3);
422 /* Parent calls mqsend (q), which should trigger notification. */
424 (void) pthread_barrier_wait (b3);
426 /* Thread verifies SIGRTMIN has been received. */
427 /* Thread calls mq_notify (q, { SIGEV_NONE }) to verify notification is now
428 available for registration. */
429 /* Thread calls mq_notify (q, NULL). */
431 (void) pthread_barrier_wait (b3);
433 memset (&ev, 0x6a, sizeof (ev));
434 ev.sigev_notify = SIGEV_SIGNAL;
435 ev.sigev_signo = SIGRTMIN;
436 ev.sigev_value.sival_ptr = do_child;
437 if (mq_notify (q, &ev) != 0)
439 printf ("fifth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
440 result = 1;
443 (void) pthread_barrier_wait (b3);
445 /* Thread calls mq_notify (q, NULL), which should unregister the above
446 notification. */
448 (void) pthread_barrier_wait (b3);
450 /* Parent calls mqsend (q), which should not trigger notification. */
452 (void) pthread_barrier_wait (b3);
454 if (rtmin_cnt != 2)
456 puts ("SIGRTMIN signal caught while notification has been disabled");
457 result = 1;
460 memset (&ev, 0x7b, sizeof (ev));
461 ev.sigev_notify = SIGEV_SIGNAL;
462 ev.sigev_signo = SIGRTMIN;
463 ev.sigev_value.sival_ptr = thr;
464 if (mq_notify (q, &ev) != 0)
466 printf ("sixth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
467 result = 1;
470 (void) pthread_barrier_wait (b3);
472 /* Thread opens a new O_RDONLY mqd_t (q4). */
473 /* Thread calls mq_notify (q4, NULL), which should unregister the above
474 notification. */
475 /* Thread calls mq_close (q4). */
477 (void) pthread_barrier_wait (b3);
479 /* Parent calls mqsend (q), which should not trigger notification. */
481 (void) pthread_barrier_wait (b3);
483 if (rtmin_cnt != 2)
485 puts ("SIGRTMIN signal caught while notification has been disabled");
486 result = 1;
489 memset (&ev, 0xe1, sizeof (ev));
490 ev.sigev_notify = SIGEV_SIGNAL;
491 ev.sigev_signo = SIGRTMIN;
492 ev.sigev_value.sival_int = 127;
493 if (mq_notify (q, &ev) != 0)
495 printf ("seventh mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
496 result = 1;
499 (void) pthread_barrier_wait (b3);
501 /* Thread opens a new O_WRONLY mqd_t (q5). */
502 /* Thread calls mq_notify (q5, NULL), which should unregister the above
503 notification. */
504 /* Thread calls mq_close (q5). */
506 (void) pthread_barrier_wait (b3);
508 /* Parent calls mqsend (q), which should not trigger notification. */
510 (void) pthread_barrier_wait (b3);
512 if (rtmin_cnt != 2)
514 puts ("SIGRTMIN signal caught while notification has been disabled");
515 result = 1;
518 void *thr_ret;
519 ret = pthread_join (th, &thr_ret);
520 if (ret)
522 errno = ret;
523 printf ("pthread_join failed: %m\n");
524 result = 1;
526 else if (thr_ret)
527 result = 1;
529 if (mq_close (q) != 0)
531 printf ("mq_close failed: %m\n");
532 result = 1;
535 exit (result);
538 #define TEST_FUNCTION do_test ()
539 static int
540 do_test (void)
542 int result = 0;
544 char tmpfname[] = "/tmp/tst-mqueue5-barrier.XXXXXX";
545 int fd = mkstemp (tmpfname);
546 if (fd == -1)
548 printf ("cannot open temporary file: %m\n");
549 return 1;
552 /* Make sure it is always removed. */
553 unlink (tmpfname);
555 /* Create one page of data. */
556 size_t ps = sysconf (_SC_PAGESIZE);
557 char data[ps];
558 memset (data, '\0', ps);
560 /* Write the data to the file. */
561 if (write (fd, data, ps) != (ssize_t) ps)
563 puts ("short write");
564 return 1;
567 void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
568 if (mem == MAP_FAILED)
570 printf ("mmap failed: %m\n");
571 return 1;
574 pthread_barrier_t *b2;
575 b2 = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
576 & ~(__alignof (pthread_barrier_t) - 1));
578 pthread_barrier_t *b3;
579 b3 = b2 + 1;
581 pthread_barrierattr_t a;
582 if (pthread_barrierattr_init (&a) != 0)
584 puts ("barrierattr_init failed");
585 return 1;
588 if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
590 puts ("barrierattr_setpshared failed, could not test");
591 return 0;
594 if (pthread_barrier_init (b2, &a, 2) != 0)
596 puts ("barrier_init failed");
597 return 1;
600 if (pthread_barrier_init (b3, &a, 3) != 0)
602 puts ("barrier_init failed");
603 return 1;
606 if (pthread_barrierattr_destroy (&a) != 0)
608 puts ("barrierattr_destroy failed");
609 return 1;
612 char name[sizeof "/tst-mqueue5-" + sizeof (pid_t) * 3];
613 snprintf (name, sizeof (name), "/tst-mqueue5-%u", getpid ());
615 struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
616 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
618 if (q == (mqd_t) -1)
620 printf ("mq_open failed with: %m\n");
621 return result;
623 else
624 add_temp_mq (name);
626 struct sigevent ev;
627 memset (&ev, 0xaa, sizeof (ev));
628 ev.sigev_notify = SIGEV_NONE;
629 if (mq_notify (q, &ev) != 0)
631 printf ("mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
632 result = 1;
635 if (mq_notify (q, &ev) == 0)
637 puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
638 result = 1;
640 else if (errno != EBUSY)
642 printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
643 result = 1;
646 result |= mqsend (q);
648 if (mq_notify (q, &ev) != 0)
650 printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
651 result = 1;
654 result |= mqrecv (q);
656 if (mq_notify (q, NULL) != 0)
658 printf ("mq_notify (q, NULL) failed with: %m\n");
659 result = 1;
662 if (mq_notify (q, NULL) != 0)
664 /* Implementation-defined behaviour, so don't fail,
665 just inform. */
666 printf ("second mq_notify (q, NULL) failed with: %m\n");
669 struct sigaction sa = { .sa_sigaction = rtmin_handler,
670 .sa_flags = SA_SIGINFO };
671 sigemptyset (&sa.sa_mask);
672 sigaction (SIGRTMIN, &sa, NULL);
674 memset (&ev, 0x55, sizeof (ev));
675 ev.sigev_notify = SIGEV_SIGNAL;
676 ev.sigev_signo = SIGRTMIN;
677 ev.sigev_value.sival_int = 26;
678 if (mq_notify (q, &ev) != 0)
680 printf ("mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
681 result = 1;
684 ev.sigev_value.sival_ptr = &ev;
685 if (mq_notify (q, &ev) == 0)
687 puts ("second mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
688 result = 1;
690 else if (errno != EBUSY)
692 printf ("second mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
693 result = 1;
696 if (rtmin_cnt != 0)
698 puts ("SIGRTMIN signal caught too early");
699 result = 1;
702 result |= mqsend (q);
704 if (rtmin_cnt != 1)
706 puts ("SIGRTMIN signal did not arrive");
707 result = 1;
709 else if (rtmin_pid != getpid ()
710 || rtmin_uid != getuid ()
711 || rtmin_code != SI_MESGQ
712 || rtmin_sigval.sival_int != 26)
714 printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (26)\n",
715 rtmin_pid, getpid (), rtmin_uid, getuid (),
716 rtmin_code, SI_MESGQ, rtmin_sigval.sival_int);
717 result = 1;
720 ev.sigev_value.sival_int = 75;
721 if (mq_notify (q, &ev) != 0)
723 printf ("third mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
724 result = 1;
727 result |= mqrecv (q);
729 if (mq_notify (q, NULL) != 0)
731 printf ("mq_notify (q, NULL) failed with: %m\n");
732 result = 1;
735 memset (&ev, 0x33, sizeof (ev));
736 ev.sigev_notify = SIGEV_NONE;
737 if (mq_notify (q, &ev) != 0)
739 printf ("fourth mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
740 result = 1;
743 pid_t pid = fork ();
744 if (pid == -1)
746 printf ("fork () failed: %m\n");
747 mq_unlink (name);
748 return 1;
751 if (pid == 0)
752 do_child (name, b2, b3, q);
754 /* Child unsuccessfully attempts to mq_notify. */
756 (void) pthread_barrier_wait (b2);
758 result |= mqsend (q);
760 (void) pthread_barrier_wait (b2);
762 /* Child successfully calls mq_notify SIGEV_SIGNAL now. */
764 result |= mqrecv (q);
766 (void) pthread_barrier_wait (b2);
768 memset (&ev, 0xbb, sizeof (ev));
769 ev.sigev_notify = SIGEV_SIGNAL;
770 ev.sigev_signo = SIGRTMIN;
771 ev.sigev_value.sival_int = 15;
772 if (mq_notify (q, &ev) == 0)
774 puts ("fourth mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
775 result = 1;
777 else if (errno != EBUSY)
779 printf ("fourth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
780 result = 1;
783 result |= mqsend (q);
785 if (mq_notify (q, &ev) != 0)
787 printf ("fifth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
788 result = 1;
791 if (rtmin_cnt != 1)
793 puts ("SIGRTMIN signal caught too early");
794 result = 1;
797 result |= mqrecv (q);
799 (void) pthread_barrier_wait (b2);
801 /* Child verifies caught SIGRTMIN signal. */
802 /* Child calls mq_send (q) which triggers SIGRTMIN signal here. */
804 (void) pthread_barrier_wait (b2);
806 /* Child mq_open's another mqd_t for the same queue (q2). */
808 if (rtmin_cnt != 2)
810 puts ("SIGRTMIN signal did not arrive");
811 result = 1;
813 else if (rtmin_pid != pid
814 || rtmin_uid != getuid ()
815 || rtmin_code != SI_MESGQ
816 || rtmin_sigval.sival_int != 15)
818 printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (15)\n",
819 rtmin_pid, pid, rtmin_uid, getuid (),
820 rtmin_code, SI_MESGQ, rtmin_sigval.sival_int);
821 result = 1;
824 result |= mqrecv (q);
826 (void) pthread_barrier_wait (b2);
828 /* Child successfully calls mq_notify { SIGEV_SIGNAL } on q2. */
830 (void) pthread_barrier_wait (b2);
832 memset (&ev, 0xbb, sizeof (ev));
833 ev.sigev_notify = SIGEV_NONE;
834 if (mq_notify (q, &ev) == 0)
836 puts ("fifth mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
837 result = 1;
839 else if (errno != EBUSY)
841 printf ("fifth mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
842 result = 1;
845 (void) pthread_barrier_wait (b2);
847 /* Child calls mq_close on q2, which makes the queue available again for
848 notification. */
850 mqd_t q3 = mq_open (name, O_RDWR);
851 if (q3 == (mqd_t) -1)
853 printf ("mq_open q3 in parent failed with: %m\n");
854 result = 1;
857 (void) pthread_barrier_wait (b2);
859 memset (&ev, 0x12, sizeof (ev));
860 ev.sigev_notify = SIGEV_NONE;
861 if (mq_notify (q3, &ev) != 0)
863 printf ("mq_notify (q3, { SIGEV_NONE }) failed with: %m\n");
864 result = 1;
867 (void) pthread_barrier_wait (b2);
869 /* Child unsuccessfully attempts to mq_notify { SIGEV_SIGNAL } on q. */
871 (void) pthread_barrier_wait (b2);
873 if (mq_close (q3) != 0)
875 printf ("mq_close failed: %m\n");
876 result = 1;
879 (void) pthread_barrier_wait (b2);
881 /* Child successfully calls mq_notify { SIGEV_NONE } on q. */
882 /* Child successfully calls mq_notify NULL on q. */
884 (void) pthread_barrier_wait (b2);
886 /* Child creates new thread. */
887 /* Thread blocks on mqrecv (q). */
888 /* Child sleeps for 1sec so that thread has time to reach that point. */
889 /* Child successfully calls mq_notify { SIGEV_SIGNAL } on q. */
891 (void) pthread_barrier_wait (b2);
893 result |= mqsend (q);
895 (void) pthread_barrier_wait (b3);
897 /* Child verifies SIGRTMIN has not been sent. */
899 (void) pthread_barrier_wait (b3);
901 result |= mqsend (q);
903 (void) pthread_barrier_wait (b3);
905 /* Thread verifies SIGRTMIN has been caught. */
906 /* Thread calls mq_notify (q, { SIGEV_NONE }) to verify notification is now
907 available for registration. */
908 /* Thread calls mq_notify (q, NULL). */
910 (void) pthread_barrier_wait (b3);
912 /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
914 (void) pthread_barrier_wait (b3);
916 /* Thread calls mq_notify (q, NULL). */
918 (void) pthread_barrier_wait (b3);
920 result |= mqsend (q);
921 result |= mqrecv (q);
923 (void) pthread_barrier_wait (b3);
925 /* Child verifies SIGRTMIN has not been sent. */
926 /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
928 (void) pthread_barrier_wait (b3);
930 /* Thread opens a new O_RDONLY mqd_t (q4). */
931 /* Thread calls mq_notify (q4, NULL). */
932 /* Thread calls mq_close (q4). */
934 (void) pthread_barrier_wait (b3);
936 result |= mqsend (q);
937 result |= mqrecv (q);
939 (void) pthread_barrier_wait (b3);
941 /* Child verifies SIGRTMIN has not been sent. */
942 /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
944 (void) pthread_barrier_wait (b3);
946 /* Thread opens a new O_WRONLY mqd_t (q5). */
947 /* Thread calls mq_notify (q5, NULL). */
948 /* Thread calls mq_close (q5). */
950 (void) pthread_barrier_wait (b3);
952 result |= mqsend (q);
953 result |= mqrecv (q);
955 (void) pthread_barrier_wait (b3);
957 /* Child verifies SIGRTMIN has not been sent. */
959 int status;
960 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
962 puts ("waitpid failed");
963 kill (pid, SIGKILL);
964 result = 1;
966 else if (!WIFEXITED (status) || WEXITSTATUS (status))
968 printf ("child failed with status %d\n", status);
969 result = 1;
972 if (mq_unlink (name) != 0)
974 printf ("mq_unlink failed: %m\n");
975 result = 1;
978 if (mq_close (q) != 0)
980 printf ("mq_close failed: %m\n");
981 result = 1;
984 if (mq_notify (q, NULL) == 0)
986 puts ("mq_notify on closed mqd_t unexpectedly succeeded");
987 result = 1;
989 else if (errno != EBADF)
991 printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
992 result = 1;
995 memset (&ev, 0x55, sizeof (ev));
996 ev.sigev_notify = SIGEV_NONE;
997 if (mq_notify (q, &ev) == 0)
999 puts ("mq_notify on closed mqd_t unexpectedly succeeded");
1000 result = 1;
1002 else if (errno != EBADF)
1004 printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
1005 result = 1;
1008 return result;
1010 #else
1011 # define TEST_FUNCTION 0
1012 #endif
1014 #include "../test-skeleton.c"