1/*
2* Copyright (c) 2005, Bull S.A..  All rights reserved.
3* Created by: Sebastien Decugis
4
5* This program is free software; you can redistribute it and/or modify it
6* under the terms of version 2 of the GNU General Public License as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it would be useful, but
10* WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*
13* You should have received a copy of the GNU General Public License along
14* with this program; if not, write the Free Software Foundation, Inc., 59
15* Temple Place - Suite 330, Boston MA 02111-1307, USA.
16
17
18* This sample test aims to check the following assertions:
19*
20* If SA_SIGINFO is not set in sa_flags, sa_handler is used as the signal
21* handling function.
22
23* The steps are:
24* -> register a handler for SIGBUS without SA_SIGINFO, and a known function
25*   as saèhandler
26* -> raise SIGBUS, and check the function has been called.
27
28* The test fails if the function is not called
29*/
30
31
32/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
33#define _POSIX_C_SOURCE 200112L
34
35/******************************************************************************/
36/*************************** standard includes ********************************/
37/******************************************************************************/
38#include <pthread.h>
39#include <stdarg.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include <signal.h>
46#include <errno.h>
47
48/******************************************************************************/
49/***************************   Test framework   *******************************/
50/******************************************************************************/
51#include "testfrmw.h"
52#include "testfrmw.c"
53/* This header is responsible for defining the following macros:
54 * UNRESOLVED(ret, descr);
55 *    where descr is a description of the error and ret is an int
56 *   (error code for example)
57 * FAILED(descr);
58 *    where descr is a short text saying why the test has failed.
59 * PASSED();
60 *    No parameter.
61 *
62 * Both three macros shall terminate the calling process.
63 * The testcase shall not terminate in any other maneer.
64 *
65 * The other file defines the functions
66 * void output_init()
67 * void output(char * string, ...)
68 *
69 * Those may be used to output information.
70 */
71
72/******************************************************************************/
73/**************************** Configuration ***********************************/
74/******************************************************************************/
75#ifndef VERBOSE
76#define VERBOSE 1
77#endif
78
79#define SIGNAL SIGBUS
80
81/******************************************************************************/
82/***************************    Test case   ***********************************/
83/******************************************************************************/
84
85int called = 0;
86void handler( int sig )
87{
88	called = 1;
89}
90
91/* main function */
92int main()
93{
94	int ret;
95
96	struct sigaction sa;
97
98	/* Initialize output */
99	output_init();
100
101	/* Set the signal handler */
102	sa.sa_flags = 0;
103	sa.sa_handler = handler;
104	ret = sigemptyset( &sa.sa_mask );
105
106	if ( ret != 0 )
107	{
108		UNRESOLVED( ret, "Failed to empty signal set" );
109	}
110
111	/* Install the signal handler for SIGBUS */
112	ret = sigaction( SIGNAL, &sa, 0 );
113
114	if ( ret != 0 )
115	{
116		UNRESOLVED( ret, "Failed to set signal handler" );
117	}
118
119	if ( called )
120	{
121		FAILED( "The signal handler has been called when no signal was raised" );
122	}
123
124	ret = raise( SIGNAL );
125
126	if ( ret != 0 )
127	{
128		UNRESOLVED( ret, "Failed to raise SIGBUS" );
129	}
130
131	if ( !called )
132	{
133		FAILED( "the sa_handler was not called whereas SA_SIGINFO was not set" );
134	}
135
136
137	/* Test passed */
138#if VERBOSE > 0
139
140	output( "Test passed\n" );
141
142#endif
143
144	PASSED;
145}
146