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