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_RDONLY, the message queue can
11 * receive messages but not send.
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 rdwrqueue, roqueue;
37	struct mq_attr attr;
38	unsigned pri;
39
40        sprintf(qname, "/mq_open_7-1_%d", getpid());
41
42	attr.mq_msgsize = BUFFER;
43	attr.mq_maxmsg = BUFFER;
44        rdwrqueue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr);
45        if (rdwrqueue == (mqd_t)-1) {
46                perror("mq_open() did not return success");
47		printf("Test UNRESOLVED\n");
48                return PTS_UNRESOLVED;
49        }
50
51        if (mq_send(rdwrqueue, 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(rdwrqueue);
56		mq_unlink(qname);
57		return PTS_UNRESOLVED;
58        }
59#ifdef DEBUG
60	printf("Message %s sent\n", msgptr);
61#endif
62
63        roqueue = mq_open(qname, O_RDONLY, S_IRUSR | S_IWUSR, &attr);
64        if (roqueue == (mqd_t)-1) {
65                perror("mq_open() for read-only queue did not return success");
66		printf("Test UNRESOLVED\n");
67		/* close read-write queue and exit */
68		mq_close(rdwrqueue);
69		mq_unlink(qname);
70                return PTS_UNRESOLVED;
71        }
72#ifdef DEBUG
73	printf("read-only message queue opened\n");
74#endif
75
76        if (mq_receive(roqueue, msgrcd, BUFFER, &pri) == -1) {
77		perror("mq_receive() failed on read-only queue");
78		printf("Test FAILED\n");
79		/* close queues and exit */
80		mq_close(roqueue);
81		mq_close(rdwrqueue);
82		mq_unlink(qname);
83		return PTS_FAIL;
84	}
85#ifdef DEBUG
86	printf("Message received\n");
87#endif
88
89        if (mq_send(roqueue, msgptr, strlen(msgptr), 1) == 0) {
90		printf("mq_send() succeeded on read-only queue\n");
91		printf("Test FAILED\n");
92		/* close queues and exit */
93		mq_close(roqueue);
94		mq_close(rdwrqueue);
95		mq_unlink(qname);
96		return PTS_FAIL;
97	}
98#ifdef DEBUG
99	printf("Message could not be sent, as expected\n");
100#endif
101
102	mq_close(rdwrqueue);
103	mq_close(roqueue);
104	mq_unlink(qname);
105
106        printf("Test PASSED\n");
107        return PTS_PASS;
108}
109
110