source: rtems/testsuites/psxtests/psxsignal01/init.c @ 9a4eca5

5
Last change on this file since 9a4eca5 was 698c2e50, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 07:06:16

tests/psxtests: Use <rtems/test.h>

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#define CONFIGURE_INIT
15#include "system.h"
16#include <signal.h>
17#include <errno.h>
18#include <reent.h>
19
20const char rtems_test_name[] = "PSXSIGNAL 1";
21
22/* forward declarations to avoid warnings */
23int test_main(void);
24void Handler_1(int signo);
25void Signal_handler(int signo);
26void Signal_info_handler(int signo, siginfo_t *info, void *context);
27rtems_timer_service_routine Signal_duringISR_TSR(
28  rtems_id  ignored_id,
29  void     *ignored_address
30);
31
32typedef void (*sighandler_t)(int);
33sighandler_t signal(int signum, sighandler_t handler);
34extern void _POSIX_signals_Abnormal_termination_handler( int signo );
35
36volatile int Signal_occurred;
37volatile int Signal_count;
38
39void Handler_1(
40  int signo
41)
42{
43  Signal_count++;
44  printf(
45    "Handler_1: Signal: %d caught by 0x%" PRIxpthread_t " (%d)\n",
46    signo,
47    pthread_self(),
48    Signal_count
49  );
50  Signal_occurred = 1;
51}
52
53void Signal_handler(
54  int signo
55)
56{
57  Signal_count++;
58  printf(
59    "Signal: %d caught by 0x%"PRIxpthread_t " (%d)\n",
60    signo,
61    pthread_self(),
62    Signal_count
63  );
64  Signal_occurred = 1;
65}
66
67void Signal_info_handler(
68  int        signo,
69  siginfo_t *info,
70  void      *context
71)
72{
73  Signal_count++;
74  printf(
75    "Signal_info: %d caught by 0x%" PRIxpthread_t " (%d) si_signo= %d si_code= %d value= %d\n",
76    signo,
77    pthread_self(),
78    Signal_count,
79    info->si_signo,
80    info->si_code,
81    info->si_value.sival_int
82  );
83  Signal_occurred = 1;
84}
85
86rtems_timer_service_routine Signal_duringISR_TSR(
87  rtems_id  ignored_id,
88  void     *ignored_address
89)
90{
91  int  status;
92
93  status = pthread_kill( pthread_self(), SIGUSR1 );
94  rtems_test_assert( status == 0 );
95}
96
97
98void *POSIX_Init(
99  void *argument
100)
101{
102  int               status;
103  struct sigaction  act;
104  sigset_t          mask;
105  sighandler_t      oldHandler;
106  sighandler_t      newHandler;
107  rtems_interval start, end;
108
109  TEST_BEGIN();
110
111  /* set the time of day, and print our buffer in multiple ways */
112
113  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
114
115  /* get id of this thread */
116
117  Init_id = pthread_self();
118  printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );
119
120  Signal_occurred = 0;
121  Signal_count = 0;
122  act.sa_handler = Handler_1;
123  act.sa_flags   = 0;
124  sigaction( SIGUSR1, &act, NULL );
125  sigaction( SIGFPE, &act, NULL );
126  sigaction( SIGILL, &act, NULL );
127  sigaction( SIGSEGV, &act, NULL );
128
129
130  /*
131   * If we have the signal pending with default, we will die.
132   */
133  puts("Validate signal with SIG_DFL");
134  signal( SIGUSR1, SIG_DFL );
135  status = kill( getpid(), SIGUSR1 );
136  status = sleep( 1 );
137
138  puts("Validate signal with SIG_IGN");
139  signal( SIGUSR1, SIG_IGN );
140  status = kill( getpid(), SIGUSR1 );
141  status = sleep( 1 );
142
143/* unblock Signal and see if it happened */
144  status = sigemptyset( &mask );
145  rtems_test_assert( !status );
146
147  status = sigaddset( &mask, SIGUSR1 );
148  rtems_test_assert( !status );
149
150  status = sigaddset( &mask, SIGFPE );
151  rtems_test_assert( !status );
152
153  status = sigaddset( &mask, SIGILL );
154  rtems_test_assert( !status );
155
156  status = sigaddset( &mask, SIGSEGV );
157  rtems_test_assert( !status );
158
159  puts( "Init: Unblock SIGUSR1 SIGFPE SIGILL SIGSEGV" );
160  status = sigprocmask( SIG_UNBLOCK, &mask, NULL );
161  rtems_test_assert( !status );
162
163/* install a signal handler for SIGUSR1 */
164  Signal_occurred = 0;
165  Signal_count = 0;
166  act.sa_handler = Handler_1;
167  act.sa_flags   = 0;
168  sigaction( SIGUSR1, &act, NULL );
169
170  Signal_count = 0;
171  Signal_occurred = 0;
172
173  newHandler = Signal_handler;
174  oldHandler = signal( SIGUSR1, newHandler );
175  if (oldHandler == Handler_1 )
176    puts("Init: signal return value verified");
177  else
178    puts("Init: ERROR==> signal unexpected return value" );
179  status = sleep( 1 );
180
181  puts( "Init: send SIGUSR1 to process" );
182  status = kill( getpid(), SIGUSR1 );
183  status = sleep( 5 );
184
185  puts( "Init: send SIGFPE to process" );
186  status = _kill_r( NULL, getpid(), SIGFPE );
187  status = sleep(5);
188
189  puts( "Init: send SIGILL to process" );
190  status = _kill_r( NULL, getpid(), SIGILL );
191  status = sleep(5);
192
193  puts( "Init: send SIGSEGV to process" );
194  status = _kill_r( NULL, getpid(), SIGSEGV );
195  status = sleep(5);
196
197  Timer_name[0]= rtems_build_name( 'T', 'M', '1', ' ' );
198  status = rtems_timer_create( Timer_name[0], &Timer_id[0]);
199
200  Signal_count = 0;
201  Signal_occurred = 0;
202  puts( "Init: send SIGUSR1 to process from a TSR (interruptible sleep)" );
203  status = rtems_timer_fire_after(
204    Timer_id[ 0 ],
205    1,
206    Signal_duringISR_TSR,
207    NULL
208  );
209  sleep(5);
210  /* signal occurs during interruptible sleep */
211
212  /* now schedule another one to fire but do not sleep */
213
214  puts( "Init: send SIGUSR1 to process from a TSR (spin)" );
215  start = rtems_clock_get_ticks_since_boot();
216  Signal_count = 0;
217  Signal_occurred = 0;
218  status = rtems_timer_fire_after(
219    Timer_id[ 0 ],
220    10,
221    Signal_duringISR_TSR,
222    NULL
223  );
224  do {
225    end = rtems_clock_get_ticks_since_boot();
226  } while ( !Signal_occurred && ((end - start) <= 800));
227
228  if ( !Signal_occurred ) {
229    puts( "Signal did not occur" );
230    rtems_test_exit(0);
231  }
232
233/* end of install a signal handler for SIGUSR1 */
234
235  Signal_occurred = 0;
236
237  puts("*** Validate unexpected program termination ***");
238  TEST_END();
239  _POSIX_signals_Abnormal_termination_handler( SIGUSR1 );
240  status = sleep( 1 );
241
242  puts( "ERROR==> Expected program termination");
243  rtems_test_exit(0);
244  return NULL; /* just so the compiler thinks we returned something */
245}
Note: See TracBrowser for help on using the repository browser.