| 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 | ||
| 97cb13ce | 28 | #include <assert.h> |
| 062bbe7c | 29 | #include <fcntl.h> |
| 97cb13ce SK |
30 | #include <mqueue.h> |
| 31 | #include <stdio.h> | |
| dff7c724 | 32 | #include <string.h> |
| 97cb13ce SK |
33 | #include <stdlib.h> |
| 34 | #include <unistd.h> /* fork() */ | |
| 232ab19b | 35 | #include <sys/wait.h> |
| 97cb13ce | 36 | |
| 17621d19 | 37 | #define MQNAME "/tmqpc" |
| 97cb13ce | 38 | |
| 290718e9 SK |
39 | mqd_t md; |
| 40 | ||
| 97cb13ce SK |
41 | static void diep(const char *s); |
| 42 | ||
| 43 | int main(void) | |
| 44 | { | |
| ff954369 | 45 | const char msg[] = "Parent says hello"; |
| 97cb13ce | 46 | int rv; |
| 97cb13ce SK |
47 | pid_t pid; |
| 48 | ||
| 03ff01d7 | 49 | /* Create a message queue for write only with default parameters. */ |
| b1b68f2f | 50 | md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, NULL); |
| 97cb13ce SK |
51 | if (md == -1) |
| 52 | diep("mq_open"); | |
| 53 | ||
| 03ff01d7 | 54 | /* Send message. */ |
| 97cb13ce SK |
55 | rv = mq_send(md, msg, sizeof(msg), /* priority */ 0); |
| 56 | if (rv == -1) | |
| 57 | diep("mq_send"); | |
| 58 | ||
| 03ff01d7 | 59 | /* Disassociate with message queue. */ |
| 97cb13ce SK |
60 | rv = mq_close(md); |
| 61 | if (rv == -1) | |
| 62 | diep("mq_close"); | |
| 63 | ||
| 03ff01d7 | 64 | /* Fork and have child read the message from parent. */ |
| 97cb13ce SK |
65 | pid = fork(); |
| 66 | if (pid == -1) { | |
| 67 | diep("fork"); | |
| 68 | } else if (pid == 0) { | |
| 03ff01d7 | 69 | /* We are inside the child. */ |
| 97cb13ce SK |
70 | md = mq_open(MQNAME, O_RDONLY); |
| 71 | if (md == -1) | |
| 72 | diep("child: mq_open"); | |
| 73 | ||
| 03ff01d7 | 74 | char msg_recvd[8192]; /* Implementation defined. */ |
| 97cb13ce SK |
75 | rv = mq_receive(md, msg_recvd, sizeof(msg_recvd), NULL); |
| 76 | if (rv == -1) | |
| 77 | diep("child: mq_receive"); | |
| 78 | ||
| dff7c724 | 79 | assert(strcmp(msg_recvd, msg) == 0); |
| 97cb13ce | 80 | |
| 03ff01d7 | 81 | /* Disassociate with message queue. */ |
| 97cb13ce SK |
82 | rv = mq_close(md); |
| 83 | if (rv == -1) | |
| 84 | diep("child: mq_close"); | |
| 85 | ||
| 03ff01d7 | 86 | /* Remove the message queue from the system. */ |
| 97cb13ce SK |
87 | rv = mq_unlink(MQNAME); |
| 88 | if (rv == -1) | |
| 89 | diep("mq_unlink"); | |
| dff7c724 SK |
90 | |
| 91 | printf("passed\n"); | |
| 97cb13ce | 92 | } else { |
| 03ff01d7 | 93 | /* We are inside the parent. */ |
| 232ab19b SK |
94 | int status; |
| 95 | wait(&status); | |
| 97cb13ce SK |
96 | } |
| 97 | ||
| 290718e9 | 98 | return EXIT_SUCCESS; |
| 97cb13ce SK |
99 | } |
| 100 | ||
| 101 | void diep(const char *s) | |
| 102 | { | |
| 103 | perror(s); | |
| 290718e9 SK |
104 | |
| 105 | /* | |
| 106 | * Message queues' name & resources are persistent, i.e., they live | |
| 107 | * even after the process dies. That's why, disassociate and destroy | |
| 108 | * the queue on failure, or else we might end up with zombie queues and | |
| 109 | * hit the limit of max open queues. | |
| 110 | * Also, we don't care about the return value of the following calls. | |
| 111 | */ | |
| 112 | mq_close(md); | |
| 113 | mq_unlink(MQNAME); | |
| 114 | ||
| 97cb13ce SK |
115 | exit(EXIT_FAILURE); |
| 116 | } |