Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_timedsend2.c
CommitLineData
eb61dd57 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
28#include <assert.h>
29#include <errno.h>
30#include <limits.h>
31#include <mqueue.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h> /* memset() */
35#include <time.h>
36#include <sys/wait.h>
37
38#define MQNAME "/t_mq_timedsend24"
39
40struct {
41 time_t tv_sec;
42 long tv_nsec;
43} tstable[] = {
44 { 0, -1 },
45 { 0, -LONG_MAX },
46 { 0, 1000000000 },
47 { 0, 1000000001 },
e24002d3 48 { 0, +LONG_MAX },
eb61dd57 49
50 { 1, -1 },
51 { 1, -LONG_MAX },
52 { 1, 1000000000 },
53 { 1, 1000000001 },
54 { 1, +LONG_MAX }
55};
56
57int
58main(void)
59{
60 /* Create a message queue with capacity `1'. */
61 struct mq_attr attr;
62 mqd_t md;
63
64 memset(&attr, 0, sizeof(attr));
65 attr.mq_maxmsg = 1; /* Maximum number of messages. */
66 attr.mq_msgsize = 1024; /* Maximum message size. */
67
68 /*
69 * We intentionally omit the O_NONBLOCK flag, as we _want_ the queue to
70 * operate on blocking mode.
71 */
72 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, &attr);
73 assert(md != (mqd_t)-1);
74
75 /* Send a message to fill in the queue. */
76 assert(mq_send(md, "foo", sizeof("foo"), /* priority */ 0) != -1);
77
78 /*
79 * Since we are on blocking mode, any invalid `nsec' field in the
54c03783 80 * timeout structure we provide to mq_timedsend(), shall result in an
eb61dd57 81 * EINVAL error.
82 */
e24002d3 83 struct timespec timeout;
eb61dd57 84 size_t i;
85
86 for (i = 0; i < sizeof(tstable) / sizeof(tstable[0]); i++) {
87 timeout.tv_sec = tstable[i].tv_sec;
88 timeout.tv_nsec = tstable[i].tv_nsec;
89
90 assert(mq_timedsend(md, "foo", sizeof("foo"), /* priority */ 0,
91 &timeout) == -1 && errno == EINVAL);
92 }
93
94 /* Disassociate from message queue. */
54c03783 95 assert(mq_close(md) != -1);
eb61dd57 96
97 /* Remove message queue from the system. */
54c03783 98 assert(mq_unlink(MQNAME) != -1);
eb61dd57 99
100 printf("passed\n");
101
102 return (EXIT_SUCCESS);
103}