1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Copyright (c) 2008, Novell Inc. All rights reserved.
4 *
5 * Created by: julie.n.fleischer REMOVE-THIS AT intel DOT com
6 * Major fixes by: Brandon Philips <bphilips@suse.de>
7 *
8 * This file is licensed under the GPL license. For the full content
9 * of this license, see the COPYING file at the top level of this
10 * source tree.
11 */
12
13 /*
14 * Test that if the message queue is full and O_NONBLOCK is not set, mq_send()
15 * will block until it can place the message in the queue.
16 *
17 * Test by sending messages in a child process until the message queue is full.
18 * At this point, the child should be blocking on sending. Then, have the
19 * parent receive the message and return pass when the next message is sent
20 * to the message queue.
21 *
22 * 3/13/03 - Added fix from Gregoire Pichon for specifying an attr
23 * with a mq_maxmsg >= BUFFER.
24 *
25 */
26
27 #include <stdio.h>
28 #include <mqueue.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include "posixtest.h"
40 #include "mq_send.h"
41
42 #define NAMESIZE 50
43 #define MSGSTR "0123456789"
44 #define BUFFER 40
45 #define MAXMSG 5
46
47 static char gqname[NAMESIZE];
48 static mqd_t gqueue;
49
50 static int sync_pipes[2];
51
cleanup_for_exit(int gqueue,char * gqname,int ret)52 static int cleanup_for_exit(int gqueue, char *gqname, int ret)
53 {
54 mq_close(gqueue);
55 mq_unlink(gqname);
56
57 if (ret == PTS_PASS)
58 printf("Test PASSED\n");
59 else if (ret == PTS_FAIL)
60 printf("Test FAILED\n");
61
62 return ret;
63 }
64
main(void)65 int main(void)
66 {
67 int pid;
68 char msgrcd[BUFFER];
69 const char *msgptr = MSGSTR;
70 struct mq_attr attr;
71 unsigned pri;
72
73 sprintf(gqname, "/mq_send_5-1_%d", getpid());
74
75 attr.mq_msgsize = BUFFER;
76 attr.mq_maxmsg = MAXMSG;
77
78 /* Use O_CREAT + O_EXCL to avoid using a previously created queue */
79 gqueue =
80 mq_open(gqname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR,
81 &attr);
82 if (gqueue == (mqd_t) - 1) {
83 perror("mq_open() did not return success");
84 return PTS_UNRESOLVED;
85 }
86
87 if (sync_pipe_create(sync_pipes) == -1) {
88 perror("sync_pipe_create() did not return success");
89 return cleanup_for_exit(gqueue, gqname, PTS_UNRESOLVED);
90 }
91
92 if ((pid = fork()) == 0) {
93 /* child here */
94 int i;
95
96 for (i = 0; i < MAXMSG + 1; i++) {
97 mq_send(gqueue, msgptr, strlen(msgptr), 1);
98
99 if (sync_pipe_notify(sync_pipes) < 0) {
100 perror
101 ("sync_pipe_notify() did not return success");
102 return cleanup_for_exit(gqueue, gqname,
103 PTS_UNRESOLVED);
104 }
105 }
106 } else {
107 /* parent here */
108 int j;
109
110 /* wait for child to reach MAXMSG */
111 for (j = 0; j < MAXMSG; j++) {
112 /* set a long timeout since we are expecting success */
113 if (sync_pipe_wait_select(sync_pipes, 60) != 0) {
114 printf("sync_pipe_wait\n");
115 return cleanup_for_exit(gqueue, gqname,
116 PTS_FAIL);
117 }
118 }
119
120 /* if we don't timeout here then we got too many messages, child never blocked */
121 if (sync_pipe_wait_select(sync_pipes, 1) != -ETIMEDOUT) {
122 printf("Child never blocked\n");
123 kill(pid, SIGKILL); //kill child
124 return cleanup_for_exit(gqueue, gqname, PTS_FAIL);
125 }
126
127 /* receive one message and allow child's mq_send to complete */
128 if (mq_receive(gqueue, msgrcd, BUFFER, &pri) == -1) {
129 perror("mq_receive() did not return success");
130 return cleanup_for_exit(gqueue, gqname, PTS_UNRESOLVED);
131 }
132
133 /* child has 5 seconds to call mq_send() again and notify us */
134 if (sync_pipe_wait_select(sync_pipes, 5) == -ETIMEDOUT) {
135 /*
136 * mq_send didn't unblock
137 */
138 kill(pid, SIGKILL); //kill child
139 printf("mq_send() didn't appear to complete\n");
140 return cleanup_for_exit(gqueue, gqname, PTS_FAIL);
141 }
142
143 return cleanup_for_exit(gqueue, gqname, PTS_PASS);
144 }
145
146 return PTS_UNRESOLVED;
147 }
148