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() fails with EEXIST if O_CREAT and O_EXCL are set
11 * but the message queue already exists.
12 */
13
14#include <stdio.h>
15#include <mqueue.h>
16#include <fcntl.h>
17#include <sys/stat.h>
18#include <sys/types.h>
19#include <unistd.h>
20#include <string.h>
21#include <errno.h>
22#include "posixtest.h"
23
24#define NAMESIZE 50
25
26int main()
27{
28        char qname[NAMESIZE];
29        mqd_t queue, queue2;
30
31        sprintf(qname, "/mq_open_23-1_%d", getpid());
32
33        queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, NULL);
34        if (queue == (mqd_t)-1) {
35                perror("mq_open() did not return success");
36		printf("Test UNRESOLVED\n");
37                return PTS_UNRESOLVED;
38        }
39
40	/*
41	 * Open queue qname again with O_CREAT and O_EXCL set
42	 */
43	queue2 = mq_open(qname, O_CREAT |O_EXCL|O_RDWR,
44			S_IRUSR | S_IWUSR, NULL);
45        if (queue2 != (mqd_t)-1) {
46                printf("mq_open() should have failed with O_CREAT and \n");
47                printf("O_EXCL on an already opened queue.\n");
48		printf("Test FAILED\n");
49		mq_close(queue);
50		mq_close(queue2);
51		mq_unlink(qname);
52                return PTS_FAIL;
53        }
54
55#ifdef DEBUG
56	printf("mq_open() failed as expected\n");
57#endif
58
59	if (errno != EEXIST) {
60		printf("errno != EEXIST\n");
61		printf("Test FAILED\n");
62		mq_close(queue);
63		mq_unlink(qname);
64		return PTS_FAIL;
65	}
66
67#ifdef DEBUG
68	printf("errno == EEXIST\n");
69#endif
70
71	mq_close(queue);
72	mq_unlink(qname);
73
74        printf("Test PASSED\n");
75        return PTS_PASS;
76}
77
78