Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_send2.c
CommitLineData
7e4966ee 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
28#include <assert.h>
29#include <errno.h>
7e4966ee 30#include <mqueue.h>
31#include <signal.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h> /* memset() */
7e4966ee 35#include <unistd.h> /* fork() */
36#include <sys/wait.h>
37
cc1baa99 38#define MQNAME "/t_mq_send2"
7e4966ee 39
40int
41main(void)
42{
43 /* Create a message queue with capacity `1'. */
44 struct mq_attr attr;
7e4966ee 45
46 memset(&attr, 0, sizeof(attr));
47 attr.mq_maxmsg = 1; /* Maximum number of messages. */
48 attr.mq_msgsize = 1024; /* Maximum message size. */
49
50 /*
51 * We intentionally omit the O_NONBLOCK flag as we want our queue to
52 * operate on blocking mode.
53 */
cc1baa99 54 mqd_t md;
55
7e4966ee 56 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0777, &attr);
57 assert(md != (mqd_t)-1);
58
59 /* Send a message to fill in the queue. */
60 assert(mq_send(md, "foo", sizeof("foo"), /* priority */ 0) != -1);
61
62 /*
63 * Fork and let the child block indefinitely on mq_send().
64 * This would have caused a panic on {Net, DragonFly}BSD, but it is now
65 * fixed.
66 */
67 pid_t pid = fork();
68 assert(pid != -1);
69
70 if (pid == 0) {
71 /* We are inside the child. */
72 mqd_t md2;
73
74 md2 = mq_open(MQNAME, O_WRONLY);
75 assert(md2 != (mqd_t)-1);
76
77 assert(mq_send(md2, "foo", sizeof("foo"),
78 /* priority */ NULL) != -1);
79
80 /*
cc1baa99 81 * There is no point in calling mq_close() here, as (normally)
7e4966ee 82 * this code block will never be reached. Instead we do it
83 * inside the parent.
84 */
85 } else {
86 /* We are inside the parent. */
87 /* Sleep for a while so that child manages to block on
88 * mq_send().
89 */
90 assert(sleep(3) == 0);
91
92 /*
93 * Kill the child. We can't use wait() here, because if let
94 * alone, the child will (normally) live for ever, waiting
95 * on mq_send().
96 */
97 assert(kill(pid, SIGKILL) != -1);
98
99 /*
100 * We now probe child's exit status to make sure that everything
101 * went as planned.
102 */
103 int status;
104 assert(wait(&status) == pid);
105
106 /*
107 * It MUST have exited due to a signal been delivered to it.
108 */
109 assert(WIFSIGNALED(status));
110
111 /*
112 * And the signal MUST be the one _we_ send to it (SIGKILL).
113 */
114 if (WTERMSIG(status) != SIGKILL) {
115 return (EXIT_FAILURE);
116 }
117
118 /* Disassociate from message queue. */
119 assert(mq_close(md) != -1);
120
121 /* Remove message queue from the system. */
122 assert(mq_unlink(MQNAME) != -1);
123
124 printf("passed\n");
125
126 return (EXIT_SUCCESS);
127 }
128
129 /* Child will (normally) never reach here. Nor parent. */
130 return (EXIT_SUCCESS);
131}