| 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 | ||
| 7ea37136 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> | |
| 34 | #include <string.h> /* memset() */ | |
| 70886c1b | 35 | #include <time.h> |
| 5d7c09a5 SK |
36 | #include <unistd.h> /* fork() */ |
| 37 | #include <sys/wait.h> | |
| 7ea37136 | 38 | |
| 322a3d9c | 39 | #define MQNAME "/t_mq_timedsend" |
| 7ea37136 SK |
40 | |
| 41 | int main(void) | |
| 42 | { | |
| 58276b77 | 43 | mqd_t md2; |
| 44 | ||
| 45 | /* Create a message queue for with capacity `1'. */ | |
| 7ea37136 | 46 | struct mq_attr attr; |
| 58276b77 | 47 | mqd_t md; |
| 7ea37136 | 48 | |
| 70886c1b | 49 | memset(&attr, 0, sizeof(attr)); |
| 91f9d0b7 SK |
50 | attr.mq_maxmsg = 1; /* Maximum number of messages. */ |
| 51 | attr.mq_msgsize = 1024; /* Maximum message size. */ | |
| 70886c1b SK |
52 | |
| 53 | md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, &attr); | |
| 58276b77 | 54 | assert(md != (mqd_t)-1); |
| 7ea37136 | 55 | |
| 58276b77 | 56 | /* Send a message to fill in the queue. */ |
| 70886c1b SK |
57 | assert(mq_send(md, "foo", sizeof("foo"), /* priority */ 0) != -1); |
| 58 | ||
| 5d7c09a5 | 59 | /* |
| 58276b77 | 60 | * Fork and let the parent block on mq_timedsend(). |
| 61 | * Meanwhile, the child receives the old message from the queue, making | |
| 62 | * room for parent to send the new one! | |
| 5d7c09a5 | 63 | */ |
| 58276b77 | 64 | pid_t pid = fork(); |
| 5d7c09a5 SK |
65 | assert(pid != -1); |
| 66 | ||
| 67 | if (pid == 0) { | |
| 68 | /* We are inside the child. */ | |
| 69 | md2 = mq_open(MQNAME, O_RDONLY); | |
| 58276b77 | 70 | assert(md2 != (mqd_t)-1); |
| 5d7c09a5 SK |
71 | |
| 72 | char msg_recvd[8192]; /* Implementation defined. */ | |
| 73 | assert(mq_receive(md2, msg_recvd, sizeof(msg_recvd), | |
| 74 | /* priority */ NULL) != -1); | |
| 75 | ||
| 76 | /* Disassociate from message queue. */ | |
| b0ba42e4 | 77 | assert(mq_close(md2) != -1); |
| 5d7c09a5 SK |
78 | } else { |
| 79 | /* We are inside the parent. */ | |
| 80 | /* Make sure the child has completed. */ | |
| 81 | int status; | |
| 58276b77 | 82 | assert(wait(&status) == pid); |
| 83 | ||
| 84 | /* | |
| 838f228d | 85 | * Determine if the child exited normally, or due to a SIGABRT |
| 86 | * signal being delivered to it by a failed assertion. | |
| 87 | */ | |
| 88 | if (WIFSIGNALED(status)) { | |
| 89 | assert(WTERMSIG(status) == SIGABRT); | |
| 90 | return (EXIT_FAILURE); | |
| 91 | } | |
| 92 | ||
| 93 | /* | |
| 58276b77 | 94 | * This shouldn't timeout now, since the queue is or is about to |
| 95 | * become empty. It may block though for a while. | |
| 96 | */ | |
| 97 | struct timespec now, timeout; | |
| 98 | ||
| 99 | assert(clock_gettime(CLOCK_REALTIME, &now) == 0); | |
| 100 | timeout.tv_sec = now.tv_sec + 3; | |
| 101 | timeout.tv_nsec = now.tv_nsec; | |
| 5d7c09a5 | 102 | |
| 5d7c09a5 | 103 | assert(mq_timedsend(md, "foo", sizeof("foo"), /* priority */ 0, |
| 58276b77 | 104 | &timeout) != -1); |
| 5d7c09a5 SK |
105 | |
| 106 | /* Disassociate from message queue. */ | |
| 107 | mq_close(md); | |
| 4b426afd | 108 | |
| 5d7c09a5 SK |
109 | /* Remove message queue from the system. */ |
| 110 | mq_unlink(MQNAME); | |
| 7ea37136 | 111 | |
| 5d7c09a5 | 112 | printf("passed\n"); |
| 838f228d | 113 | |
| 114 | return (EXIT_SUCCESS); | |
| 5d7c09a5 | 115 | } |
| 7ea37136 | 116 | |
| 838f228d | 117 | /* Only reached by child upon success. */ |
| 7ea37136 SK |
118 | return (EXIT_SUCCESS); |
| 119 | } |