Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_ambig.c
CommitLineData
322a3d9c
SK
1/*
2 * Copyright (c) 2009, Stathis Kamperis
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
6b79e392
SK
28#include <assert.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <mqueue.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35
36#define MQNAME "/t_mq_ambig"
37
38int main(void)
39{
40 char buf[100];
41 mqd_t md;
42
43 /*
44 * In the creation of a message queue, there is an ambiguity between
45 * `oflag' and `mode_t' with respect to which one of them takes
46 * precedence over the other.
47 *
48 * It seems that `oflag' is checked first and then `mode_t'.
49 */
50
51 /*
52 * Create a READ only message queue, but with WRITE permission bits
53 * set, and then try to actually WRITE (i.e., mq_send() data to it).
54 */
55 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDONLY, 0777, NULL);
56 assert(md != (mqd_t)-1);
57
58 assert(mq_send(md, buf, sizeof(buf), /* priority */ 0) == -1
59 && errno == EBADF);
60
61 /* Close and remove the message queue. */
62 assert(mq_close(md) != (mqd_t)-1);
63 assert(mq_unlink(MQNAME) != (mqd_t)-1);
64
65 /*
66 * Create a WRITE only message queue, but with READ permission bits
67 * set, and then try to actually READ (i.e., mq_receive() data from it).
68 */
69 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0777, NULL);
70 assert(md != (mqd_t)-1);
71
72 assert(mq_receive(md, buf, sizeof(buf), /* priority */ 0) == -1
73 && errno == EBADF);
74
75 /* Close and remove the message queue. */
76 assert(mq_close(md) != (mqd_t)-1);
77 assert(mq_unlink(MQNAME) != (mqd_t)-1);
78
79 printf("passed\n");
80
81 return (EXIT_SUCCESS);
82}