Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_send.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
a4c0a646
SK
28#include <assert.h>
29#include <errno.h>
062bbe7c 30#include <fcntl.h>
a4c0a646
SK
31#include <mqueue.h>
32#include <stdio.h>
33#include <stdlib.h>
aefea859 34#include <string.h> /* memset() */
a4c0a646 35
322a3d9c 36#define MQNAME "/t_mq_send"
a4c0a646
SK
37
38int main(void)
39{
40 struct mq_attr attr;
41 mqd_t md;
42 int rv;
43
44 /* Invalid message queue descriptor. */
45 rv = mq_send(-1, "", 0, 0);
46 assert (rv == -1 && errno == EBADF);
47
3df72c7a
SK
48 /*
49 * Priority is out of range 0...{MQ_PRIO_MAX}-1
50 * Mind that count starts from 0.
51 */
52 long maxprio = -1;
53#ifdef _SC_MQ_PRIO_MAX
54 maxprio = sysconf(_SC_MQ_PRIO_MAX);
55#endif
56 if (maxprio == -1) {
57 /*
58 * Use some ridiculously high value.
59 * Linux claims that supports 32768 priorities!
60 */
61 maxprio = 65536;
62 }
aefea859 63 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, NULL);
5618d109 64 assert(md != 1);
a4c0a646 65
3df72c7a 66 rv = mq_send(md, "foo", sizeof("foo"), maxprio);
a4c0a646
SK
67 assert(rv == -1 && errno == EINVAL);
68
69 mq_close(md);
aefea859
SK
70 mq_unlink(MQNAME);
71
72 /*
73 * Message size is larger than maximum allowed message size.
74 * The latter is implementation defined. In NetBSD happens to
5300ca9a
SK
75 * be 16*1024 as defined in sys/mqueue.h and kern/sys_mqueue.c.
76 * We use a "sufficiently" large value to cover other
77 * implemenetations as well.
aefea859
SK
78 */
79 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, NULL);
a4c0a646
SK
80 assert(md != -1);
81
82 char *p;
aefea859 83 rv = mq_send(md, p, 32768, /* priority */ 0);
a4c0a646
SK
84 assert(rv == -1 && errno == EMSGSIZE);
85
86 mq_close(md);
aefea859 87 mq_unlink(MQNAME);
a4c0a646 88
aa8574e2
SK
89 /* Same as previous but use a non-default maximum message size. */
90 memset(&attr, 0, sizeof(attr));
91 attr.mq_maxmsg = 1; /* Maximum number of messages. */
92 attr.mq_msgsize = 50; /* Maximum message size. */
93 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, &attr);
94 assert(md != -1);
95
5618d109
SK
96 p = malloc(2 * attr.mq_msgsize);
97 assert(p != NULL);
98 memset(p, 0xff, 2 * attr.mq_msgsize); /* Make sure we don't terminate prematurely */
99
100 rv = mq_send(md, p, 2 * attr.mq_msgsize, /* priority */ 0);
aa8574e2
SK
101 assert(rv == -1 && errno == EMSGSIZE);
102
5618d109
SK
103 free(p);
104
aa8574e2
SK
105 mq_close(md);
106 mq_unlink(MQNAME);
107
a4c0a646 108 printf("passed\n");
7503ff3a
SK
109
110 return (EXIT_SUCCESS);
a4c0a646 111}