1/*
2 * Copyright (c) 2003, Intel Corporation. All rights reserved.
3 * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license.  For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7 */
8
9/*
10 * Test that if the access mode is O_WRONLY, the message queue can
11 * send messages but not receive.
12 *
13 * Test for a message queue opened twice in the same process.
14 *
15 * 3/13/03 - Added fix from Gregoire Pichon for specifying an attr
16 *           with a mq_maxmsg >= BUFFER.
17 */
18
19#include <stdio.h>
20#include <mqueue.h>
21#include <fcntl.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25#include <string.h>
26#include "posixtest.h"
27
28#define NAMESIZE 50
29#define MSGSTR "0123456789"
30#define BUFFER 40
31
32int main()
33{
34        char qname[NAMESIZE], msgrcd[BUFFER];
35        const char *msgptr = MSGSTR;
36        mqd_t woqueue, woqueue2;
37	struct mq_attr attr;
38	unsigned pri;
39
40        sprintf(qname, "/mq_open_8-1_%d", getpid());
41
42	attr.mq_msgsize = BUFFER;
43	attr.mq_maxmsg = BUFFER;
44        woqueue = mq_open(qname, O_CREAT |O_WRONLY, S_IRUSR | S_IWUSR, &attr);
45        if (woqueue == (mqd_t)-1) {
46                perror("mq_open() for write-only queue did not return success");
47		printf("Test UNRESOLVED\n");
48                return PTS_UNRESOLVED;
49        }
50
51        if (mq_send(woqueue, msgptr, strlen(msgptr), 1) != 0) {
52                perror("mq_send() did not return success");
53		printf("Test UNRESOLVED\n");
54		/* close queue and exit */
55		mq_close(woqueue);
56		mq_unlink(qname);
57		return PTS_UNRESOLVED;
58        }
59#ifdef DEBUG
60	printf("Message %s sent\n", msgptr);
61#endif
62
63        if (mq_receive(woqueue, msgrcd, BUFFER, &pri) != -1) {
64		printf("mq_receive() returned success on write only queue\n");
65		printf("Test FAILED\n");
66		/* close queue and exit */
67		mq_close(woqueue);
68		mq_unlink(qname);
69		return PTS_FAIL;
70	}
71#ifdef DEBUG
72	printf("Message receive failed, as expected\n");
73#endif
74
75        woqueue2 = mq_open(qname, O_WRONLY, S_IRUSR | S_IWUSR, &attr);
76        if (woqueue2 == (mqd_t)-1) {
77                perror("mq_open() did not return success");
78		printf("Test UNRESOLVED\n");
79		/* close woqueue and exit */
80		mq_close(woqueue);
81		mq_unlink(qname);
82                return PTS_UNRESOLVED;
83        }
84
85        if (mq_send(woqueue2, msgptr, strlen(msgptr), 1) != 0) {
86                perror("mq_send() did not return success");
87		printf("Test UNRESOLVED\n");
88		/* close queues and exit */
89		mq_close(woqueue);
90		mq_close(woqueue2);
91		mq_unlink(qname);
92		return PTS_UNRESOLVED;
93        }
94#ifdef DEBUG
95	printf("Message %s sent to second queue\n", msgptr);
96#endif
97
98        if (mq_receive(woqueue2, msgrcd, BUFFER, &pri) != -1) {
99		printf("mq_receive() returned success on write only queue\n");
100		printf("Test FAILED\n");
101		/* close queues and exit */
102		mq_close(woqueue);
103		mq_close(woqueue2);
104		mq_unlink(qname);
105		return PTS_FAIL;
106	}
107#ifdef DEBUG
108	printf("Message receive failed, as expected, on second queue\n");
109#endif
110
111	mq_close(woqueue);
112	mq_close(woqueue2);
113	mq_unlink(qname);
114
115        printf("Test PASSED\n");
116        return PTS_PASS;
117}
118
119