Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_open.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
1f8f48cc
SK
28#include <assert.h>
29#include <errno.h>
062bbe7c 30#include <fcntl.h>
f374f4af 31#include <limits.h> /* for LONG_MAX */
1f8f48cc
SK
32#include <mqueue.h>
33#include <stdio.h>
34#include <stdlib.h>
f58259c0 35#include <string.h> /* memset() */
1f8f48cc 36
a8c75017 37#define MQNAME "/t_mq_open"
1f8f48cc 38
f374f4af 39int
40main(void)
1f8f48cc 41{
f374f4af 42 mqd_t md;
1f8f48cc 43
f374f4af 44 /* ------------------------------------------------------------------ */
34386361 45 /* Empty message queue name. */
f374f4af 46 assert(mq_open("", O_CREAT | O_EXCL | O_RDWR, 0700, NULL) == (mqd_t)-1
34386361
SK
47 && errno == EINVAL);
48
f374f4af 49
50 /* ------------------------------------------------------------------ */
770609f1 51 /* O_CREAT is not set and the named mqueue doesn't exist. */
f374f4af 52 assert(mq_open("/t_mq_definitelydoesntexist", O_RDWR, 0700, NULL)
53 == (mqd_t)-1 && errno == ENOENT);
54
1f8f48cc 55
f374f4af 56 /* ------------------------------------------------------------------ */
d829a07b 57 /* Pathname is too long. XXX Probe host OS to report real PATH_MAX. */
f374f4af 58 char *pathname = malloc(65536);
1f8f48cc
SK
59 assert(pathname != NULL);
60
f374f4af 61 /* Make sure we don't terminate prematurely. */
62 memset(pathname, 0xFF, 65536);
63
73a642c2 64 assert(mq_open(pathname, O_CREAT | O_EXCL | O_RDWR, 0700, NULL)
f374f4af 65 == -1 && errno == ENAMETOOLONG);
1f8f48cc
SK
66 free(pathname);
67
f374f4af 68
69 /* ------------------------------------------------------------------ */
70 /* Message queue already exists and O_CREAT, O_EXCL are both set. */
73a642c2 71 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0700, NULL);
d90376ca 72 assert(md != (mqd_t)-1);
3c82ae0d 73
73a642c2 74 assert(mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0700, NULL)
d90376ca 75 == (mqd_t)-1 && errno == EEXIST);
3c82ae0d 76
f374f4af 77 assert(mq_close(md) != -1);
78 assert(mq_unlink(MQNAME) != -1);
79
80
81 /* ------------------------------------------------------------------ */
02ab775e 82 /* Try to open a read only message queue for write. */
bea7fbaa 83 md = mq_open(MQNAME, O_CREAT | O_EXCL, 0400, NULL);
d90376ca 84 assert(md != (mqd_t)-1);
02ab775e 85
f374f4af 86 assert(mq_open(MQNAME, O_RDWR) == (mqd_t)-1 && errno == EACCES);
87 assert(mq_open(MQNAME, O_WRONLY) == (mqd_t)-1 && errno == EACCES);
211ff952 88
f374f4af 89 assert(mq_close(md) != -1);
90 assert(mq_unlink(MQNAME) != -1);
6f5bd4bd 91
02ab775e 92
f374f4af 93 /* ------------------------------------------------------------------ */
3228c800
SK
94 /* Try to open a write only message queue for read. */
95 md = mq_open(MQNAME, O_CREAT | O_EXCL, 0200, NULL);
d90376ca 96 assert(md != (mqd_t)-1);
3228c800 97
f374f4af 98 assert(mq_open(MQNAME, O_RDWR) == (mqd_t)-1 && errno == EACCES);
99 assert(mq_open(MQNAME, O_RDONLY) == (mqd_t)-1 && errno == EACCES);
100
101 assert(mq_close(md) != -1);
102 assert(mq_unlink(MQNAME) != -1);
103
104
105 /* ------------------------------------------------------------------ */
106 /* Try to open a queue with enormous capacity. */
107 struct mq_attr attr;
3228c800 108
f374f4af 109 memset(&attr, 0, sizeof(attr));
110 attr.mq_maxmsg = LONG_MAX;
111 attr.mq_msgsize = 1024;
6f5bd4bd 112
73a642c2 113 assert(mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0700, &attr) == -1);
3228c800 114
03786e02 115 printf("passed\n");
7503ff3a
SK
116
117 return (EXIT_SUCCESS);
1f8f48cc 118}