| Commit | Line | Data |
|---|---|---|
| 211b6a64 SK |
1 | /* |
| 2 | * Copyright (c) 2009, Stathis Kamperis | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 15 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 17 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 18 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 20 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 22 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 24 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 25 | * SUCH DAMAGE. | |
| 26 | */ | |
| 27 | ||
| af3f41db SK |
28 | #include <assert.h> |
| 29 | #include <mqueue.h> | |
| 30 | #include <signal.h> | |
| 31 | #include <stdio.h> | |
| 32 | #include <stdlib.h> | |
| 33 | #include <unistd.h> /* for sleep() */ | |
| 34 | ||
| d58890c5 | 35 | #define MQNAME "/t_mq_notify_simple" |
| af3f41db SK |
36 | |
| 37 | int notified = 0; /* Whether we were notified or not. */ | |
| 38 | ||
| 39 | /* Function prototypes. */ | |
| 40 | static void myhandler(int, siginfo_t *, void *); | |
| 41 | ||
| 42 | int main(void) | |
| 43 | { | |
| 44 | mqd_t md; | |
| 45 | ||
| 46 | /* Create a new message queue. */ | |
| 47 | md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0777, NULL); | |
| 48 | assert(md != (mqd_t)-1); | |
| 49 | ||
| 50 | /* | |
| 51 | * At this point the message queue is empty. | |
| d58890c5 | 52 | * We register ourselves for notification, upon the arrival of a |
| af3f41db SK |
53 | * message. |
| 54 | */ | |
| 55 | struct sigevent sigev; | |
| 56 | ||
| 57 | sigev.sigev_signo = SIGUSR1; | |
| 58 | sigev.sigev_notify = SIGEV_SIGNAL; | |
| 9573c1d2 SK |
59 | /* This is a temporary (famous last words) hack for DragonFly. */ |
| 60 | #ifndef __DragonFly__ | |
| 61 | sigev.sigev_value.sival_int = (int)md; | |
| 62 | #endif | |
| af3f41db SK |
63 | |
| 64 | assert(mq_notify(md, &sigev) != -1); | |
| 65 | ||
| 66 | /* We install a signal handler to catch SIGUSR1. */ | |
| 67 | struct sigaction sa; | |
| 68 | ||
| 69 | sa.sa_sigaction = myhandler; | |
| 70 | sa.sa_flags = SA_SIGINFO; | |
| 71 | ||
| 72 | assert(sigaction(SIGUSR1, &sa, NULL) != -1); | |
| 73 | ||
| 70c4500a SK |
74 | /* A little bit paranoid. */ |
| 75 | assert(notified == 0); | |
| 76 | ||
| af3f41db | 77 | /* |
| d58890c5 SK |
78 | * We send a message to the queue so that the transition |
| 79 | * empty->non empty takes place and the signal is raised. | |
| af3f41db SK |
80 | */ |
| 81 | assert(mq_send(md, "foo", sizeof("foo"), 0) != -1); | |
| 82 | ||
| 83 | /* | |
| d58890c5 SK |
84 | * Make sure we were notified, but first take a nap so that the |
| 85 | * implementation has enough time at its disposal. | |
| af3f41db SK |
86 | */ |
| 87 | sleep(1); | |
| 88 | assert(notified == 1); | |
| 89 | notified = 0; | |
| 90 | ||
| 91 | /* Disassociate with message queue. */ | |
| 92 | assert(mq_close(md) != -1); | |
| 93 | ||
| 94 | /* Remove the message queue from the system. */ | |
| 95 | assert(mq_unlink(MQNAME) != -1); | |
| 96 | ||
| 70c4500a | 97 | /* A little bit of paranoia again. We don't want spurious wakeups. */ |
| af3f41db SK |
98 | assert(notified == 0); |
| 99 | ||
| 100 | printf("passed\n"); | |
| 101 | ||
| 102 | return (EXIT_SUCCESS); | |
| 103 | } | |
| 104 | ||
| 105 | static void | |
| 106 | myhandler(int signo, siginfo_t *info, void *context) | |
| 107 | { | |
| 108 | notified = 1; | |
| 109 | } |