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 mq_open() does not add messages to the queue or remove
11 * messages from the queue.
12 *
13 * Test using mq_send and mq_receive:
14 * - Call mq_open() for non-blocking queue
15 * - Verify mq_receive() fails (because nothing should be in the queue yet)
16 * - Call mq_send() to put something in the queue
17 * - Call mq_open() again for non-blocking queue
18 * - Verify mq_receive() now succeeded (because the sent message should
19 *   still be in the queue).
20 *
21 *   3/13/03 - Added fix from Gregoire Pichon for specifying an attr
22 *             with a mq_maxmsg >= BUFFER.
23 */
24
25#include <stdio.h>
26#include <mqueue.h>
27#include <fcntl.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
31#include <string.h>
32#include "posixtest.h"
33
34#define NAMESIZE 50
35#define MSGSTR "0123456789"
36#define BUFFER 40
37
38int main()
39{
40        char qname[NAMESIZE], msgrcd[BUFFER];
41        const char *msgptr = MSGSTR;
42        mqd_t queue;
43	struct mq_attr attr;
44	int failure=0;
45	unsigned pri;
46
47        sprintf(qname, "/mq_open_19-1_%d", getpid());
48
49	attr.mq_msgsize = BUFFER;
50	attr.mq_maxmsg = BUFFER;
51
52        queue = mq_open(qname, O_CREAT |O_RDWR|O_NONBLOCK,
53			S_IRUSR | S_IWUSR, &attr);
54        if (queue == (mqd_t)-1) {
55                perror("mq_open() did not return success");
56		printf("Test UNRESOLVED\n");
57                return PTS_UNRESOLVED;
58        }
59
60        if (mq_receive(queue, msgrcd, BUFFER, &pri) != -1) {
61		printf("mq_receive() succeded\n");
62		printf("mq_open() may have placed a message in the queue\n");
63		failure=1;
64	}
65
66        if (mq_send(queue, msgptr, strlen(msgptr), 1) == -1) {
67                perror("mq_send() did not return success");
68		printf("Test UNRESOLVED\n");
69		/* close queue and exit */
70		mq_close(queue);
71		mq_unlink(qname);
72                return PTS_UNRESOLVED;
73        }
74
75        queue = mq_open(qname, O_RDWR|O_NONBLOCK, S_IRUSR | S_IWUSR, NULL);
76        if (queue == (mqd_t)-1) {
77                perror("mq_open() second time did not return success");
78		printf("Test UNRESOLVED\n");
79		/* close queue and exit */
80		mq_close(queue);
81		mq_unlink(qname);
82                return PTS_UNRESOLVED;
83        }
84
85        if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
86		perror("mq_receive() failed");
87		printf("mq_open() may have removed a msg from the queue\n");
88		failure=1;
89	}
90
91	mq_close(queue);
92	mq_unlink(qname);
93
94	if (failure==1) {
95		printf("Test FAILED\n");
96		return PTS_FAIL;
97	}
98
99        printf("Test PASSED\n");
100        return PTS_PASS;
101}
102
103