chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / rt / tst-mqueue5.c
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.
5
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.
10
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.
15
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.  */
20
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"
36
37 #define TIMEOUT 3
38
39 #if _POSIX_THREADS
40 # include <pthread.h>
41
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;
47
48 static void
49 rtmin_handler (int sig, siginfo_t *info, void *ctx)
50 {
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;
58 }
59
60 #define mqsend(q) (mqsend) (q, __LINE__)
61 static int
62 (mqsend) (mqd_t q, int line)
63 {
64   char c;
65   if (mq_send (q, &c, 1, 1) != 0)
66     {
67       printf ("mq_send on line %d failed with: %m\n", line);
68       return 1;
69     }
70   return 0;
71 }
72
73 #define mqrecv(q) (mqrecv) (q, __LINE__)
74 static int
75 (mqrecv) (mqd_t q, int line)
76 {
77   char c;
78   ssize_t rets = TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
79   if (rets != 1)
80     {
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;
87     }
88   return 0;
89 }
90
91 struct thr_data
92 {
93   const char *name;
94   pthread_barrier_t *b3;
95   mqd_t q;
96 };
97
98 static void *
99 thr (void *arg)
100 {
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;
105
106   result |= mqrecv (q);
107
108   (void) pthread_barrier_wait (b3);
109
110   /* Child verifies SIGRTMIN has not been sent.  */
111
112   (void) pthread_barrier_wait (b3);
113
114   /* Parent calls mqsend (q), which should trigger notification.  */
115
116   (void) pthread_barrier_wait (b3);
117
118   if (rtmin_cnt != 2)
119     {
120       puts ("SIGRTMIN signal in child did not arrive");
121       result = 1;
122     }
123   else if (rtmin_pid != getppid ()
124            || rtmin_uid != getuid ()
125            || rtmin_code != SI_MESGQ
126            || rtmin_sigval.sival_int != 0xdeadbeef)
127     {
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;
132     }
133
134   struct sigevent ev;
135   memset (&ev, 0x82, sizeof (ev));
136   ev.sigev_notify = SIGEV_NONE;
137   if (mq_notify (q, &ev) != 0)
138     {
139       printf ("mq_notify in thread (q, { SIGEV_NONE }) failed with: %m\n");
140       result = 1;
141     }
142
143   if (mq_notify (q, NULL) != 0)
144     {
145       printf ("mq_notify in thread (q, NULL) failed with: %m\n");
146       result = 1;
147     }
148
149   result |= mqrecv (q);
150
151   (void) pthread_barrier_wait (b3);
152
153   /* Child calls mq_notify (q, { SIGEV_SIGNAL }).  */
154
155   (void) pthread_barrier_wait (b3);
156
157   if (mq_notify (q, NULL) != 0)
158     {
159       printf ("second mq_notify in thread (q, NULL) failed with: %m\n");
160       result = 1;
161     }
162
163   (void) pthread_barrier_wait (b3);
164
165   /* Parent calls mqsend (q), which should not trigger notification.  */
166
167   (void) pthread_barrier_wait (b3);
168
169   /* Child verifies SIGRTMIN has not been received.  */
170   /* Child calls mq_notify (q, { SIGEV_SIGNAL }).  */
171
172   (void) pthread_barrier_wait (b3);
173
174   mqd_t q4 = mq_open (name, O_RDONLY);
175   if (q4 == (mqd_t) -1)
176     {
177       printf ("mq_open in thread failed with: %m\n");
178       result = 1;
179     }
180
181   if (mq_notify (q4, NULL) != 0)
182     {
183       printf ("mq_notify in thread (q4, NULL) failed with: %m\n");
184       result = 1;
185     }
186
187   if (mq_close (q4) != 0)
188     {
189       printf ("mq_close in thread failed with: %m\n");
190       result = 1;
191     }
192
193   (void) pthread_barrier_wait (b3);
194
195   /* Parent calls mqsend (q), which should not trigger notification.  */
196
197   (void) pthread_barrier_wait (b3);
198
199   /* Child verifies SIGRTMIN has not been received.  */
200   /* Child calls mq_notify (q, { SIGEV_SIGNAL }).  */
201
202   (void) pthread_barrier_wait (b3);
203
204   mqd_t q5 = mq_open (name, O_WRONLY);
205   if (q5 == (mqd_t) -1)
206     {
207       printf ("mq_open O_WRONLY in thread failed with: %m\n");
208       result = 1;
209     }
210
211   if (mq_notify (q5, NULL) != 0)
212     {
213       printf ("mq_notify in thread (q5, NULL) failed with: %m\n");
214       result = 1;
215     }
216
217   if (mq_close (q5) != 0)
218     {
219       printf ("mq_close in thread failed with: %m\n");
220       result = 1;
221     }
222
223   (void) pthread_barrier_wait (b3);
224
225   /* Parent calls mqsend (q), which should not trigger notification.  */
226
227   (void) pthread_barrier_wait (b3);
228
229   /* Child verifies SIGRTMIN has not been received.  */
230
231   return (void *) (long) result;
232 }
233
234 static void
235 do_child (const char *name, pthread_barrier_t *b2, pthread_barrier_t *b3,
236           mqd_t q)
237 {
238   int result = 0;
239
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)
246     {
247       puts ("first mq_notify in child (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
248       result = 1;
249     }
250   else if (errno != EBUSY)
251     {
252       printf ("first mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
253       result = 1;
254     }
255
256   (void) pthread_barrier_wait (b2);
257
258   /* Parent calls mqsend (q), which makes notification available.  */
259
260   (void) pthread_barrier_wait (b2);
261
262   rtmin_cnt = 0;
263
264   if (mq_notify (q, &ev) != 0)
265     {
266       printf ("second mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
267       result = 1;
268     }
269
270   if (rtmin_cnt != 0)
271     {
272       puts ("SIGRTMIN signal in child caught too early");
273       result = 1;
274     }
275
276   (void) pthread_barrier_wait (b2);
277
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.  */
282
283   (void) pthread_barrier_wait (b2);
284
285   if (rtmin_cnt != 1)
286     {
287       puts ("SIGRTMIN signal in child did not arrive");
288       result = 1;
289     }
290   else if (rtmin_pid != getppid ()
291            || rtmin_uid != getuid ()
292            || rtmin_code != SI_MESGQ
293            || rtmin_sigval.sival_ptr != &ev)
294     {
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;
299     }
300
301   result |= mqsend (q);
302
303   (void) pthread_barrier_wait (b2);
304
305   /* Parent verifies caught SIGRTMIN.  */
306
307   mqd_t q2 = mq_open (name, O_RDWR);
308   if (q2 == (mqd_t) -1)
309     {
310       printf ("mq_open in child failed with: %m\n");
311       result = 1;
312     }
313
314   (void) pthread_barrier_wait (b2);
315
316   /* Parent mq_open's another mqd_t for the same queue (q3).  */
317
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)
323     {
324       printf ("mq_notify in child (q2, { SIGEV_SIGNAL }) failed with: %m\n");
325       result = 1;
326     }
327
328   (void) pthread_barrier_wait (b2);
329
330   /* Parent unsuccessfully attempts to mq_notify { SIGEV_NONE } on q.  */
331
332   (void) pthread_barrier_wait (b2);
333
334   if (mq_close (q2) != 0)
335     {
336       printf ("mq_close failed: %m\n");
337       result = 1;
338     }
339
340   (void) pthread_barrier_wait (b2);
341
342   /* Parent successfully calls mq_notify { SIGEV_NONE } on q3.  */
343
344   (void) pthread_barrier_wait (b2);
345
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)
351     {
352       puts ("third mq_notify in child (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
353       result = 1;
354     }
355   else if (errno != EBUSY)
356     {
357       printf ("third mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
358       result = 1;
359     }
360
361   (void) pthread_barrier_wait (b2);
362
363   /* Parent calls mq_close on q3, which makes the queue available again for
364      notification.  */
365
366   (void) pthread_barrier_wait (b2);
367
368   memset (&ev, 0x13, sizeof (ev));
369   ev.sigev_notify = SIGEV_NONE;
370   if (mq_notify (q, &ev) != 0)
371     {
372       printf ("mq_notify in child (q, { SIGEV_NONE }) failed with: %m\n");
373       result = 1;
374     }
375
376   if (mq_notify (q, NULL) != 0)
377     {
378       printf ("mq_notify in child (q, NULL) failed with: %m\n");
379       result = 1;
380     }
381
382   (void) pthread_barrier_wait (b2);
383
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)
388     {
389       errno = ret;
390       printf ("pthread_created failed with: %m\n");
391       result = 1;
392     }
393
394   /* Wait till thr calls mq_receive on the empty queue q and blocks on it.  */
395   sleep (1);
396
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)
402     {
403       printf ("fourth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
404       result = 1;
405     }
406
407   (void) pthread_barrier_wait (b2);
408
409   /* Parent calls mqsend (q), which should wake up mqrecv (q)
410      in the thread but no notification should be sent.  */
411
412   (void) pthread_barrier_wait (b3);
413
414   if (rtmin_cnt != 1)
415     {
416       puts ("SIGRTMIN signal caught while thr was blocked on mq_receive");
417       result = 1;
418     }
419
420   (void) pthread_barrier_wait (b3);
421
422   /* Parent calls mqsend (q), which should trigger notification.  */
423
424   (void) pthread_barrier_wait (b3);
425
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).  */
430
431   (void) pthread_barrier_wait (b3);
432
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)
438     {
439       printf ("fifth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
440       result = 1;
441     }
442
443   (void) pthread_barrier_wait (b3);
444
445   /* Thread calls mq_notify (q, NULL), which should unregister the above
446      notification.  */
447
448   (void) pthread_barrier_wait (b3);
449
450   /* Parent calls mqsend (q), which should not trigger notification.  */
451
452   (void) pthread_barrier_wait (b3);
453
454   if (rtmin_cnt != 2)
455     {
456       puts ("SIGRTMIN signal caught while notification has been disabled");
457       result = 1;
458     }
459
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)
465     {
466       printf ("sixth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
467       result = 1;
468     }
469
470   (void) pthread_barrier_wait (b3);
471
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).  */
476
477   (void) pthread_barrier_wait (b3);
478
479   /* Parent calls mqsend (q), which should not trigger notification.  */
480
481   (void) pthread_barrier_wait (b3);
482
483   if (rtmin_cnt != 2)
484     {
485       puts ("SIGRTMIN signal caught while notification has been disabled");
486       result = 1;
487     }
488
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)
494     {
495       printf ("seventh mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
496       result = 1;
497     }
498
499   (void) pthread_barrier_wait (b3);
500
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).  */
505
506   (void) pthread_barrier_wait (b3);
507
508   /* Parent calls mqsend (q), which should not trigger notification.  */
509
510   (void) pthread_barrier_wait (b3);
511
512   if (rtmin_cnt != 2)
513     {
514       puts ("SIGRTMIN signal caught while notification has been disabled");
515       result = 1;
516     }
517
518  void *thr_ret;
519   ret = pthread_join (th, &thr_ret);
520   if (ret)
521     {
522       errno = ret;
523       printf ("pthread_join failed: %m\n");
524       result = 1;
525     }
526   else if (thr_ret)
527     result = 1;
528
529   if (mq_close (q) != 0)
530     {
531       printf ("mq_close failed: %m\n");
532       result = 1;
533     }
534
535   exit (result);
536 }
537
538 #define TEST_FUNCTION do_test ()
539 static int
540 do_test (void)
541 {
542   int result = 0;
543
544   char tmpfname[] = "/tmp/tst-mqueue5-barrier.XXXXXX";
545   int fd = mkstemp (tmpfname);
546   if (fd == -1)
547     {
548       printf ("cannot open temporary file: %m\n");
549       return 1;
550     }
551
552   /* Make sure it is always removed.  */
553   unlink (tmpfname);
554
555   /* Create one page of data.  */
556   size_t ps = sysconf (_SC_PAGESIZE);
557   char data[ps];
558   memset (data, '\0', ps);
559
560   /* Write the data to the file.  */
561   if (write (fd, data, ps) != (ssize_t) ps)
562     {
563       puts ("short write");
564       return 1;
565     }
566
567   void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
568   if (mem == MAP_FAILED)
569     {
570       printf ("mmap failed: %m\n");
571       return 1;
572     }
573
574   pthread_barrier_t *b2;
575   b2 = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
576                               & ~(__alignof (pthread_barrier_t) - 1));
577
578   pthread_barrier_t *b3;
579   b3 = b2 + 1;
580
581   pthread_barrierattr_t a;
582   if (pthread_barrierattr_init (&a) != 0)
583     {
584       puts ("barrierattr_init failed");
585       return 1;
586     }
587
588   if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
589     {
590       puts ("barrierattr_setpshared failed, could not test");
591       return 0;
592     }
593
594   if (pthread_barrier_init (b2, &a, 2) != 0)
595     {
596       puts ("barrier_init failed");
597       return 1;
598     }
599
600   if (pthread_barrier_init (b3, &a, 3) != 0)
601     {
602       puts ("barrier_init failed");
603       return 1;
604     }
605
606   if (pthread_barrierattr_destroy (&a) != 0)
607     {
608       puts ("barrierattr_destroy failed");
609       return 1;
610     }
611
612   char name[sizeof "/tst-mqueue5-" + sizeof (pid_t) * 3];
613   snprintf (name, sizeof (name), "/tst-mqueue5-%u", getpid ());
614
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);
617
618   if (q == (mqd_t) -1)
619     {
620       printf ("mq_open failed with: %m\n");
621       return result;
622     }
623   else
624     add_temp_mq (name);
625
626   struct sigevent ev;
627   memset (&ev, 0xaa, sizeof (ev));
628   ev.sigev_notify = SIGEV_NONE;
629   if (mq_notify (q, &ev) != 0)
630     {
631       printf ("mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
632       result = 1;
633     }
634
635   if (mq_notify (q, &ev) == 0)
636     {
637       puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
638       result = 1;
639     }
640   else if (errno != EBUSY)
641     {
642       printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
643       result = 1;
644     }
645
646   result |= mqsend (q);
647
648   if (mq_notify (q, &ev) != 0)
649     {
650       printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
651       result = 1;
652     }
653
654   result |= mqrecv (q);
655
656   if (mq_notify (q, NULL) != 0)
657     {
658       printf ("mq_notify (q, NULL) failed with: %m\n");
659       result = 1;
660     }
661
662   if (mq_notify (q, NULL) != 0)
663     {
664       /* Implementation-defined behaviour, so don't fail,
665          just inform.  */
666       printf ("second mq_notify (q, NULL) failed with: %m\n");
667     }
668
669   struct sigaction sa = { .sa_sigaction = rtmin_handler,
670                           .sa_flags = SA_SIGINFO };
671   sigemptyset (&sa.sa_mask);
672   sigaction (SIGRTMIN, &sa, NULL);
673
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)
679     {
680       printf ("mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
681       result = 1;
682     }
683
684   ev.sigev_value.sival_ptr = &ev;
685   if (mq_notify (q, &ev) == 0)
686     {
687       puts ("second mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
688       result = 1;
689     }
690   else if (errno != EBUSY)
691     {
692       printf ("second mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
693       result = 1;
694     }
695
696   if (rtmin_cnt != 0)
697     {
698       puts ("SIGRTMIN signal caught too early");
699       result = 1;
700     }
701
702   result |= mqsend (q);
703
704   if (rtmin_cnt != 1)
705     {
706       puts ("SIGRTMIN signal did not arrive");
707       result = 1;
708     }
709   else if (rtmin_pid != getpid ()
710            || rtmin_uid != getuid ()
711            || rtmin_code != SI_MESGQ
712            || rtmin_sigval.sival_int != 26)
713     {
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;
718     }
719
720   ev.sigev_value.sival_int = 75;
721   if (mq_notify (q, &ev) != 0)
722     {
723       printf ("third mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
724       result = 1;
725     }
726
727   result |= mqrecv (q);
728
729   if (mq_notify (q, NULL) != 0)
730     {
731       printf ("mq_notify (q, NULL) failed with: %m\n");
732       result = 1;
733     }
734
735   memset (&ev, 0x33, sizeof (ev));
736   ev.sigev_notify = SIGEV_NONE;
737   if (mq_notify (q, &ev) != 0)
738     {
739       printf ("fourth mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
740       result = 1;
741     }
742
743   pid_t pid = fork ();
744   if (pid == -1)
745     {
746       printf ("fork () failed: %m\n");
747       mq_unlink (name);
748       return 1;
749     }
750
751   if (pid == 0)
752     do_child (name, b2, b3, q);
753
754   /* Child unsuccessfully attempts to mq_notify.  */
755
756   (void) pthread_barrier_wait (b2);
757
758   result |= mqsend (q);
759
760   (void) pthread_barrier_wait (b2);
761
762   /* Child successfully calls mq_notify SIGEV_SIGNAL now.  */
763
764   result |= mqrecv (q);
765
766   (void) pthread_barrier_wait (b2);
767
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)
773     {
774       puts ("fourth mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
775       result = 1;
776     }
777   else if (errno != EBUSY)
778     {
779       printf ("fourth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
780       result = 1;
781     }
782
783   result |= mqsend (q);
784
785   if (mq_notify (q, &ev) != 0)
786     {
787       printf ("fifth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
788       result = 1;
789     }
790
791   if (rtmin_cnt != 1)
792     {
793       puts ("SIGRTMIN signal caught too early");
794       result = 1;
795     }
796
797   result |= mqrecv (q);
798
799   (void) pthread_barrier_wait (b2);
800
801   /* Child verifies caught SIGRTMIN signal.  */
802   /* Child calls mq_send (q) which triggers SIGRTMIN signal here.  */
803
804   (void) pthread_barrier_wait (b2);
805
806   /* Child mq_open's another mqd_t for the same queue (q2).  */
807
808   if (rtmin_cnt != 2)
809     {
810       puts ("SIGRTMIN signal did not arrive");
811       result = 1;
812     }
813   else if (rtmin_pid != pid
814            || rtmin_uid != getuid ()
815            || rtmin_code != SI_MESGQ
816            || rtmin_sigval.sival_int != 15)
817     {
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;
822     }
823
824   result |= mqrecv (q);
825
826   (void) pthread_barrier_wait (b2);
827
828   /* Child successfully calls mq_notify { SIGEV_SIGNAL } on q2.  */
829
830   (void) pthread_barrier_wait (b2);
831
832   memset (&ev, 0xbb, sizeof (ev));
833   ev.sigev_notify = SIGEV_NONE;
834   if (mq_notify (q, &ev) == 0)
835     {
836       puts ("fifth mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
837       result = 1;
838     }
839   else if (errno != EBUSY)
840     {
841       printf ("fifth mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
842       result = 1;
843     }
844
845   (void) pthread_barrier_wait (b2);
846
847   /* Child calls mq_close on q2, which makes the queue available again for
848      notification.  */
849
850   mqd_t q3 = mq_open (name, O_RDWR);
851   if (q3 == (mqd_t) -1)
852     {
853       printf ("mq_open q3 in parent failed with: %m\n");
854       result = 1;
855     }
856
857   (void) pthread_barrier_wait (b2);
858
859   memset (&ev, 0x12, sizeof (ev));
860   ev.sigev_notify = SIGEV_NONE;
861   if (mq_notify (q3, &ev) != 0)
862     {
863       printf ("mq_notify (q3, { SIGEV_NONE }) failed with: %m\n");
864       result = 1;
865     }
866
867   (void) pthread_barrier_wait (b2);
868
869   /* Child unsuccessfully attempts to mq_notify { SIGEV_SIGNAL } on q.  */
870
871   (void) pthread_barrier_wait (b2);
872
873   if (mq_close (q3) != 0)
874     {
875       printf ("mq_close failed: %m\n");
876       result = 1;
877     }
878
879   (void) pthread_barrier_wait (b2);
880
881   /* Child successfully calls mq_notify { SIGEV_NONE } on q.  */
882   /* Child successfully calls mq_notify NULL on q.  */
883
884   (void) pthread_barrier_wait (b2);
885
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.  */
890
891   (void) pthread_barrier_wait (b2);
892
893   result |= mqsend (q);
894
895   (void) pthread_barrier_wait (b3);
896
897   /* Child verifies SIGRTMIN has not been sent.  */
898
899   (void) pthread_barrier_wait (b3);
900
901   result |= mqsend (q);
902
903   (void) pthread_barrier_wait (b3);
904
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).  */
909
910   (void) pthread_barrier_wait (b3);
911
912   /* Child calls mq_notify (q, { SIGEV_SIGNAL }).  */
913
914   (void) pthread_barrier_wait (b3);
915
916   /* Thread calls mq_notify (q, NULL). */
917
918   (void) pthread_barrier_wait (b3);
919
920   result |= mqsend (q);
921   result |= mqrecv (q);
922
923   (void) pthread_barrier_wait (b3);
924
925   /* Child verifies SIGRTMIN has not been sent.  */
926   /* Child calls mq_notify (q, { SIGEV_SIGNAL }).  */
927
928   (void) pthread_barrier_wait (b3);
929
930   /* Thread opens a new O_RDONLY mqd_t (q4).  */
931   /* Thread calls mq_notify (q4, NULL). */
932   /* Thread calls mq_close (q4).  */
933
934   (void) pthread_barrier_wait (b3);
935
936   result |= mqsend (q);
937   result |= mqrecv (q);
938
939   (void) pthread_barrier_wait (b3);
940
941   /* Child verifies SIGRTMIN has not been sent.  */
942   /* Child calls mq_notify (q, { SIGEV_SIGNAL }).  */
943
944   (void) pthread_barrier_wait (b3);
945
946   /* Thread opens a new O_WRONLY mqd_t (q5).  */
947   /* Thread calls mq_notify (q5, NULL). */
948   /* Thread calls mq_close (q5).  */
949
950   (void) pthread_barrier_wait (b3);
951
952   result |= mqsend (q);
953   result |= mqrecv (q);
954
955   (void) pthread_barrier_wait (b3);
956
957   /* Child verifies SIGRTMIN has not been sent.  */
958
959   int status;
960   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
961     {
962       puts ("waitpid failed");
963       kill (pid, SIGKILL);
964       result = 1;
965     }
966   else if (!WIFEXITED (status) || WEXITSTATUS (status))
967     {
968       printf ("child failed with status %d\n", status);
969       result = 1;
970     }
971
972   if (mq_unlink (name) != 0)
973     {
974       printf ("mq_unlink failed: %m\n");
975       result = 1;
976     }
977
978   if (mq_close (q) != 0)
979     {
980       printf ("mq_close failed: %m\n");
981       result = 1;
982     }
983
984   if (mq_notify (q, NULL) == 0)
985     {
986       puts ("mq_notify on closed mqd_t unexpectedly succeeded");
987       result = 1;
988     }
989   else if (errno != EBADF)
990     {
991       printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
992       result = 1;
993     }
994
995   memset (&ev, 0x55, sizeof (ev));
996   ev.sigev_notify = SIGEV_NONE;
997   if (mq_notify (q, &ev) == 0)
998     {
999       puts ("mq_notify on closed mqd_t unexpectedly succeeded");
1000       result = 1;
1001     }
1002   else if (errno != EBADF)
1003     {
1004       printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
1005       result = 1;
1006     }
1007
1008   return result;
1009 }
1010 #else
1011 # define TEST_FUNCTION 0
1012 #endif
1013
1014 #include "../test-skeleton.c"