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