Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_notify_invsigno.c
CommitLineData
c1ed6a45 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>
3d36923e 29#include <errno.h>
c1ed6a45 30#include <limits.h> /* for INT_MAX */
31#include <mqueue.h>
32#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h> /* for sleep() */
36
37#define MQNAME "/t_mq_notify_simple_invsigno"
38
39int
40main(void)
41{
42 mqd_t md;
43
44 /* Create a new message queue. */
45 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0777, NULL);
46 assert(md != (mqd_t)-1);
47
48 /*
49 * At this point the message queue is empty.
50 * We register ourselves for notification, upon the arrival of a
51 * message.
52 */
53 struct sigevent sigev;
54
55 /*
56 * But we use an invalid (or so we hope!) signal number.
57 * This would have caused a panic on {Net, DragonFly}BSD, but it is now
58 * fixed.
59 */
60 sigev.sigev_signo = -INT_MAX;
61 sigev.sigev_notify = SIGEV_SIGNAL;
62 /* This is a temporary (famous last words) hack for DragonFly. */
63#ifndef __DragonFly__
64 sigev.sigev_value.sival_int = (int)md;
65#endif
66
3d36923e 67 /*
debd5a3a 68 * POSIX (up until issue 7) doesn't specify an error condition for
3d36923e 69 * invalid signal numbers. OpenSolaris and NetBSD (perhaps others too)
70 * return -1/EINVAL.
71 */
72 if (mq_notify(md, &sigev) == -1) {
73 assert(errno == EINVAL);
74 goto PASSED;
75 }
c1ed6a45 76
77 /* Give it a break (!) */
78 sleep(1);
79
80 /*
81 * We send a message to the queue so that the transition
82 * empty->non empty takes place and the signal is raised.
3d36923e 83 * This could pontentially crash the OS!
c1ed6a45 84 */
85 assert(mq_send(md, "foo", sizeof("foo"), 0) != -1);
86
3d36923e 87PASSED:
88 /*
89 * If we have reached so far (i.e., the system didn't panic), we
90 * consider the run as successful.
91 */
92
c1ed6a45 93 /* Disassociate with message queue. */
94 assert(mq_close(md) != -1);
95
96 /* Remove the message queue from the system. */
97 assert(mq_unlink(MQNAME) != -1);
98
c1ed6a45 99 printf("passed\n");
100
101 return (EXIT_SUCCESS);
102}