2 * Copyright (c) 2005, Bull S.A.. All rights reserved.
3 * Created by: Sebastien Decugis
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.
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.
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.
18 * This sample test aims to check the following assertions:
20 * If the signal action was set with the signal() function, getting it into oact
21 then reinstalling it with act must be valid.
24 * -> register a signal handler for SIGTTOU with signal().
25 * -> check this signal handler works.
26 * -> change the signal handler with sigaction, saving old handler in oact.
27 * -> check the new signal handler works.
28 * -> set the old signal handler back
29 * -> check the old signal handler still works.
31 * The test fails if a signal handler does not work as expected.
35 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
36 #define _POSIX_C_SOURCE 200112L
38 /******************************************************************************/
39 /*************************** standard includes ********************************/
40 /******************************************************************************/
50 /******************************************************************************/
51 /*************************** Test framework *******************************/
52 /******************************************************************************/
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)
58 * where descr is a short text saying why the test has failed.
62 * Both three macros shall terminate the calling process.
63 * The testcase shall not terminate in any other maneer.
65 * The other file defines the functions
67 * void output(char * string, ...)
69 * Those may be used to output information.
71 #include "posixtest.h"
73 #include <sys/types.h>
75 #ifdef __GNUC__ /* We are using GCC */
77 #define UNRESOLVED(x, s) \
78 { output("Test %s unresolved: got %i (%s) on line %i (%s)\n", __FILE__, x, strerror(x), __LINE__, s); \
80 exit(PTS_UNRESOLVED); }
83 { output("Test %s FAILED: %s\n", __FILE__, s); \
92 { output("File %s cannot test: %s\n", __FILE__, s); \
97 #else /* not using GCC */
99 #define UNRESOLVED(x, s) \
100 { output("Test unresolved: got %i (%s) on line %i (%s)\n", x, strerror(x), __LINE__, s); \
102 exit(PTS_UNRESOLVED); }
105 { output("Test FAILED: %s\n", s); \
113 #define UNTESTED(s) \
114 { output("Unable to test: %s\n", s); \
116 exit(PTS_UNTESTED); \
126 void output( char * string, ... )
130 char *ts = "[??:??:??]";
138 now = localtime( &nw );
143 printf( "[%2.2d:%2.2d:%2.2d]", now->tm_hour, now->tm_min, now->tm_sec );
146 va_start( ap, string );
148 vprintf( string, ap );
160 /******************************************************************************/
161 /**************************** Configuration ***********************************/
162 /******************************************************************************/
167 #define SIGNAL SIGTTOU
169 /******************************************************************************/
170 /*************************** Test case ***********************************/
171 /******************************************************************************/
173 sig_atomic_t called = 1;
175 void handler_1( int sig )
180 void handler_2( int sig )
190 struct sigaction sa, save;
192 /* Initialize output */
195 /* Register the signal handler with signal */
197 if ( SIG_ERR == signal( SIGNAL, handler_1 ) )
199 UNRESOLVED( errno, "Failed to register signal handler with signal()" );
202 /* As whether signal handler is restored to default when executed
203 is implementation defined, we cannot check it was registered here. */
205 /* Set the new signal handler with sigaction*/
208 sa.sa_handler = handler_2;
210 ret = sigemptyset( &sa.sa_mask );
214 UNRESOLVED( ret, "Failed to empty signal set" );
217 /* Install the signal handler for SIGTTOU */
218 ret = sigaction( SIGNAL, &sa, &save );
222 UNRESOLVED( ret, "Failed to set signal handler" );
225 /* Check the signal handler has been set up */
226 ret = raise( SIGNAL );
230 UNRESOLVED( ret , "Failed to raise the signal" );
235 FAILED( "handler not executed" );
238 /* Restore the first signal handler */
239 ret = sigaction( SIGNAL, &save, 0 );
243 UNRESOLVED( ret, "Failed to set signal handler" );
246 /* Check the signal handler has been set up */
247 ret = raise( SIGNAL );
251 UNRESOLVED( ret , "Failed to raise the signal" );
256 FAILED( "handler not executed" );
263 output( "Test passed\n" );