| Commit | Line | Data |
|---|---|---|
| 322a3d9c 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 | ||
| 18fd27d7 SK |
28 | #include <assert.h> |
| 29 | #include <errno.h> | |
| 30 | #include <fcntl.h> | |
| 31 | #include <mqueue.h> | |
| 32 | #include <stdio.h> | |
| 33 | #include <stdlib.h> | |
| 7c6a8521 SK |
34 | #include <string.h> |
| 35 | #include <unistd.h> /* fork() */ | |
| 18fd27d7 | 36 | |
| 322a3d9c | 37 | #define MQNAME "/t_mq_select" |
| 18fd27d7 SK |
38 | |
| 39 | int main(void) | |
| 40 | { | |
| 41 | const char msg[] = "Child says hello to parent"; | |
| 42 | struct timeval abs_timeout; | |
| 43 | mqd_t md; | |
| 44 | fd_set ms; | |
| 45 | pid_t pid; | |
| 46 | ||
| 47 | /* Create a message queue for write only with default parameters. */ | |
| 01345d7d | 48 | md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0777, NULL); |
| 18fd27d7 SK |
49 | assert(md != -1); |
| 50 | ||
| 51 | /* Initialize descriptor set to null set. */ | |
| 52 | FD_ZERO(&ms); | |
| 53 | ||
| 54 | /* Add message queue descriptor to set. */ | |
| 55 | FD_SET(md, &ms); | |
| 5cae5d63 | 56 | assert(FD_ISSET(md, &ms)); /* Just a sanity check. */ |
| 18fd27d7 | 57 | |
| 71977616 SK |
58 | /* Wait for 2 seconds. */ |
| 59 | abs_timeout.tv_sec = 2; | |
| 18fd27d7 SK |
60 | abs_timeout.tv_usec = 0; |
| 61 | ||
| 62 | /* This should time out. */ | |
| 63 | assert(select(md + 1, &ms, NULL, NULL, &abs_timeout) == 0); | |
| 64 | ||
| 71977616 SK |
65 | FD_SET(md, &ms); |
| 66 | assert(FD_ISSET(md, &ms)); /* Just a sanity check. */ | |
| 67 | ||
| 18fd27d7 SK |
68 | /* Fork. */ |
| 69 | pid = fork(); | |
| 70 | assert(pid != -1); | |
| 71 | ||
| 72 | if (pid == 0) { | |
| 73 | /* We are inside the child. */ | |
| 5cae5d63 SK |
74 | /* Wait for parent to block on select(). */ |
| 75 | sleep(1); | |
| 18fd27d7 | 76 | |
| 5cae5d63 SK |
77 | /* Send message. */ |
| 78 | assert(mq_send(md, msg, sizeof(msg), /* priority */ 0) != -1); | |
| 18fd27d7 SK |
79 | } else { |
| 80 | /* We are inside the parent. */ | |
| 01345d7d | 81 | assert(select(md + 1, &ms, NULL, NULL, &abs_timeout) > 0); |
| 5cae5d63 | 82 | assert(FD_ISSET(md, &ms)); |
| 18fd27d7 | 83 | |
| 5cae5d63 SK |
84 | char msg_recvd[8192]; /* Implementation defined. */ |
| 85 | assert(mq_receive(md, msg_recvd, sizeof(msg_recvd), NULL) != -1); | |
| 86 | assert(strcmp(msg_recvd, msg) == 0); | |
| 18fd27d7 | 87 | |
| 5cae5d63 SK |
88 | /* Remove message queue descriptor from the set. */ |
| 89 | FD_CLR(md, &ms); | |
| 18fd27d7 SK |
90 | |
| 91 | /* | |
| 92 | * At this point we know for sure that the child has completed, | |
| 93 | * otherwise we would still be blocked by select(). | |
| 94 | */ | |
| 95 | ||
| 96 | /* Disassociate with message queue. */ | |
| 2c16f5cb | 97 | assert(mq_close(md) != -1); |
| 18fd27d7 SK |
98 | |
| 99 | /* Remove the message queue from the system. */ | |
| 2c16f5cb | 100 | assert(mq_unlink(MQNAME) != -1); |
| 18fd27d7 SK |
101 | |
| 102 | printf("passed\n"); | |
| 103 | } | |
| 104 | ||
| 105 | return (EXIT_SUCCESS); | |
| 106 | } |