1 | /* |
---|
2 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * All rights assigned to U.S. Government, 1994. |
---|
5 | * |
---|
6 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
7 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
8 | * notice must appear in all copies of this file and its derivatives. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #define CONFIGURE_INIT |
---|
14 | #include "system.h" |
---|
15 | #include <signal.h> |
---|
16 | |
---|
17 | volatile int Signal_occurred; |
---|
18 | volatile int Signal_count; |
---|
19 | |
---|
20 | void Signal_handler( |
---|
21 | int signo |
---|
22 | ) |
---|
23 | { |
---|
24 | Signal_count++; |
---|
25 | printf( |
---|
26 | "Signal: %d caught by 0x%x (%d)\n", |
---|
27 | signo, |
---|
28 | pthread_self(), |
---|
29 | Signal_count |
---|
30 | ); |
---|
31 | Signal_occurred = 1; |
---|
32 | } |
---|
33 | |
---|
34 | void Signal_info_handler( |
---|
35 | int signo, |
---|
36 | siginfo_t *info, |
---|
37 | void *context |
---|
38 | ) |
---|
39 | { |
---|
40 | Signal_count++; |
---|
41 | printf( |
---|
42 | "Signal_info: %d caught by 0x%x (%d) si_signo= %d si_code= %d value= %d\n", |
---|
43 | signo, |
---|
44 | pthread_self(), |
---|
45 | Signal_count, |
---|
46 | info->si_signo, |
---|
47 | info->si_code, |
---|
48 | info->si_value.sival_int |
---|
49 | ); |
---|
50 | Signal_occurred = 1; |
---|
51 | } |
---|
52 | |
---|
53 | void *POSIX_Init( |
---|
54 | void *argument |
---|
55 | ) |
---|
56 | { |
---|
57 | int status; |
---|
58 | struct sigaction act; |
---|
59 | sigset_t mask; |
---|
60 | sigset_t pending_set; |
---|
61 | sigset_t oset; |
---|
62 | |
---|
63 | puts( "\n\n*** POSIX TEST 4 ***" ); |
---|
64 | |
---|
65 | /* set the time of day, and print our buffer in multiple ways */ |
---|
66 | |
---|
67 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
68 | |
---|
69 | /* get id of this thread */ |
---|
70 | |
---|
71 | Init_id = pthread_self(); |
---|
72 | printf( "Init's ID is 0x%08x\n", Init_id ); |
---|
73 | |
---|
74 | /* install a signal handler for SIGUSR1 */ |
---|
75 | |
---|
76 | status = sigemptyset( &act.sa_mask ); |
---|
77 | assert( !status ); |
---|
78 | printf( "Init: sigemptyset - set= 0x%08x\n", act.sa_mask ); |
---|
79 | |
---|
80 | /* test sigfillset following the above sigemptyset */ |
---|
81 | |
---|
82 | status = sigfillset( &act.sa_mask ); |
---|
83 | assert( !status ); |
---|
84 | printf( "Init: sigfillset - set= 0x%08x\n", act.sa_mask ); |
---|
85 | |
---|
86 | /* test sigdelset */ |
---|
87 | |
---|
88 | status = sigdelset( &act.sa_mask, SIGUSR1 ); |
---|
89 | assert( !status ); |
---|
90 | printf( "Init: sigdelset - delete SIGUSR1 set= 0x%08x\n", act.sa_mask ); |
---|
91 | |
---|
92 | /* test sigismember - FALSE */ |
---|
93 | |
---|
94 | status = sigismember( &act.sa_mask, SIGUSR1 ); |
---|
95 | assert( !status ); |
---|
96 | puts( "Init: sigismember - FALSE since SIGUSR1 is not a member" ); |
---|
97 | |
---|
98 | /* test sigismember - TRUE */ |
---|
99 | |
---|
100 | status = sigismember( &act.sa_mask, SIGUSR2 ); |
---|
101 | assert( status ); |
---|
102 | puts( "Init: sigismember - TRUE since SIGUSR2 is a member" ); |
---|
103 | |
---|
104 | /* return the set to empty */ |
---|
105 | |
---|
106 | act.sa_handler = Signal_handler; |
---|
107 | act.sa_flags = 0; |
---|
108 | |
---|
109 | sigaction( SIGUSR1, &act, NULL ); |
---|
110 | |
---|
111 | /* simple signal to process */ |
---|
112 | |
---|
113 | Signal_count = 0; |
---|
114 | Signal_occurred = 0; |
---|
115 | |
---|
116 | printf( "Init: send SIGUSR1 to process\n" ); |
---|
117 | status = kill( getpid(), SIGUSR1 ); |
---|
118 | assert( !status ); |
---|
119 | |
---|
120 | /* end of install a signal handler for SIGUSR1 */ |
---|
121 | |
---|
122 | Signal_occurred = 0; |
---|
123 | |
---|
124 | /* now block the signal, send it, see if it is pending, and unblock it */ |
---|
125 | |
---|
126 | empty_line(); |
---|
127 | |
---|
128 | status = sigemptyset( &mask ); |
---|
129 | assert( !status ); |
---|
130 | |
---|
131 | status = sigaddset( &mask, SIGUSR1 ); |
---|
132 | assert( !status ); |
---|
133 | |
---|
134 | printf( "Init: Block SIGUSR1\n" ); |
---|
135 | act.sa_handler = Signal_handler; |
---|
136 | act.sa_flags = 0; |
---|
137 | |
---|
138 | sigaction( SIGUSR1, &act, NULL ); |
---|
139 | |
---|
140 | /* simple signal to process */ |
---|
141 | |
---|
142 | Signal_count = 0; |
---|
143 | Signal_occurred = 0; |
---|
144 | |
---|
145 | printf( "Init: send SIGUSR1 to process\n" ); |
---|
146 | status = kill( getpid(), SIGUSR1 ); |
---|
147 | assert( !status ); |
---|
148 | |
---|
149 | Signal_occurred = 0; |
---|
150 | |
---|
151 | /* now block the signal, send it, see if it is pending, and unblock it */ |
---|
152 | |
---|
153 | empty_line(); |
---|
154 | |
---|
155 | status = sigemptyset( &mask ); |
---|
156 | assert( !status ); |
---|
157 | |
---|
158 | status = sigaddset( &mask, SIGUSR1 ); |
---|
159 | assert( !status ); |
---|
160 | |
---|
161 | printf( "Init: Block SIGUSR1\n" ); |
---|
162 | status = sigprocmask( SIG_BLOCK, &mask, NULL ); |
---|
163 | assert( !status ); |
---|
164 | |
---|
165 | status = sigpending( &pending_set ); |
---|
166 | assert( !status ); |
---|
167 | printf( "Init: Signals pending 0x%08x\n", pending_set ); |
---|
168 | |
---|
169 | printf( "Init: send SIGUSR1 to process\n" ); |
---|
170 | status = kill( getpid(), SIGUSR1 ); |
---|
171 | assert( !status ); |
---|
172 | |
---|
173 | status = sigpending( &pending_set ); |
---|
174 | assert( !status ); |
---|
175 | printf( "Init: Signals pending 0x%08x\n", pending_set ); |
---|
176 | |
---|
177 | printf( "Init: Unblock SIGUSR1\n" ); |
---|
178 | status = sigprocmask( SIG_UNBLOCK, &mask, NULL ); |
---|
179 | assert( !status ); |
---|
180 | |
---|
181 | /* now let another task get interrupted by a signal */ |
---|
182 | |
---|
183 | empty_line(); |
---|
184 | |
---|
185 | printf( "Init: create a thread interested in SIGUSR1\n" ); |
---|
186 | status = pthread_create( &Task1_id, NULL, Task_1, NULL ); |
---|
187 | assert( !status ); |
---|
188 | |
---|
189 | printf( "Init: Block SIGUSR1\n" ); |
---|
190 | status = sigprocmask( SIG_BLOCK, &mask, NULL ); |
---|
191 | assert( !status ); |
---|
192 | |
---|
193 | status = sigpending( &pending_set ); |
---|
194 | assert( !status ); |
---|
195 | printf( "Init: Signals pending 0x%08x\n", pending_set ); |
---|
196 | |
---|
197 | printf( "Init: sleep so the other task can block\n" ); |
---|
198 | status = sleep( 1 ); |
---|
199 | assert( !status ); |
---|
200 | |
---|
201 | /* switch to task 1 */ |
---|
202 | |
---|
203 | printf( "Init: send SIGUSR1 to process\n" ); |
---|
204 | status = kill( getpid(), SIGUSR1 ); |
---|
205 | assert( !status ); |
---|
206 | |
---|
207 | status = sigpending( &pending_set ); |
---|
208 | assert( !status ); |
---|
209 | printf( "Init: Signals pending 0x%08x\n", pending_set ); |
---|
210 | |
---|
211 | printf( "Init: sleep so the other task can catch signal\n" ); |
---|
212 | status = sleep( 1 ); |
---|
213 | assert( !status ); |
---|
214 | |
---|
215 | /* switch to task 1 */ |
---|
216 | |
---|
217 | /* test alarm */ |
---|
218 | |
---|
219 | empty_line(); |
---|
220 | |
---|
221 | /* install a signal handler for SIGALRM and unblock it */ |
---|
222 | |
---|
223 | status = sigemptyset( &act.sa_mask ); |
---|
224 | assert( !status ); |
---|
225 | |
---|
226 | act.sa_handler = Signal_handler; |
---|
227 | act.sa_flags = 0; |
---|
228 | |
---|
229 | sigaction( SIGALRM, &act, NULL ); |
---|
230 | |
---|
231 | status = sigemptyset( &mask ); |
---|
232 | assert( !status ); |
---|
233 | |
---|
234 | status = sigaddset( &mask, SIGALRM ); |
---|
235 | assert( !status ); |
---|
236 | |
---|
237 | printf( "Init: Unblock SIGALRM\n" ); |
---|
238 | status = sigprocmask( SIG_UNBLOCK, &mask, NULL ); |
---|
239 | assert( !status ); |
---|
240 | |
---|
241 | /* schedule the alarm */ |
---|
242 | |
---|
243 | printf( "Init: Firing alarm in 5 seconds\n" ); |
---|
244 | status = alarm( 5 ); |
---|
245 | printf( "Init: %d seconds left on previous alarm\n", status ); |
---|
246 | assert( !status ); |
---|
247 | |
---|
248 | printf( "Init: Firing alarm in 2 seconds\n" ); |
---|
249 | status = alarm( 2 ); |
---|
250 | printf( "Init: %d seconds left on previous alarm\n", status ); |
---|
251 | assert( status ); |
---|
252 | |
---|
253 | printf( "Init: Wait 4 seconds for alarm\n" ); |
---|
254 | status = sleep( 4 ); |
---|
255 | printf( "Init: %d seconds left in sleep\n", status ); |
---|
256 | assert( status ); |
---|
257 | |
---|
258 | /* test SIG_SETMASK case and returning oset of pthread_sigmask */ |
---|
259 | |
---|
260 | empty_line(); |
---|
261 | |
---|
262 | status = sigemptyset( &mask ); |
---|
263 | assert( !status ); |
---|
264 | |
---|
265 | status = sigaddset( &mask, SIGUSR1 ); |
---|
266 | assert( !status ); |
---|
267 | |
---|
268 | status = sigaddset( &mask, SIGUSR2 ); |
---|
269 | assert( !status ); |
---|
270 | |
---|
271 | printf( "Init: Block SIGUSR1 and SIGUSR2 only\n" ); |
---|
272 | status = pthread_sigmask( SIG_SETMASK, &mask, &oset ); |
---|
273 | printf( "Init: Previous blocked set was 0x%08x\n", oset ); |
---|
274 | assert( !status ); |
---|
275 | |
---|
276 | /* test inquiry about current blocked set with pthread_sigmask */ |
---|
277 | |
---|
278 | status = pthread_sigmask( NULL, NULL, &oset ); |
---|
279 | printf( "Init: Current blocked set is 0x%08x\n", oset ); |
---|
280 | assert( !status ); |
---|
281 | |
---|
282 | /* return blocked mask to no signals blocked */ |
---|
283 | |
---|
284 | status = sigemptyset( &mask ); |
---|
285 | assert( !status ); |
---|
286 | |
---|
287 | printf( "Init: Unblock all signals\n" ); |
---|
288 | status = pthread_sigmask( SIG_SETMASK, &mask, &oset ); |
---|
289 | printf( "Init: Previous blocked set was 0x%08x\n", oset ); |
---|
290 | assert( !status ); |
---|
291 | |
---|
292 | /* test sigsuspend */ |
---|
293 | |
---|
294 | empty_line(); |
---|
295 | |
---|
296 | printf( "Init: create a thread to send Init SIGUSR1\n" ); |
---|
297 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
298 | assert( !status ); |
---|
299 | |
---|
300 | status = sigemptyset( &mask ); |
---|
301 | assert( !status ); |
---|
302 | |
---|
303 | printf( "Init: sigsuspend for any signal\n" ); |
---|
304 | status = sigsuspend( &mask ); |
---|
305 | assert( status ); |
---|
306 | printf( "Init: awakended from sigsuspend status=%08d \n", status ); |
---|
307 | |
---|
308 | /* test a SIGINFO case, these are signals sent to a process only */ |
---|
309 | |
---|
310 | empty_line(); |
---|
311 | |
---|
312 | printf( "Init: create a thread to sent Process SIGUSR1 with SA_SIGINFO\n" ); |
---|
313 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
314 | assert( !status ); |
---|
315 | |
---|
316 | /* set action on SIGUSR1 to an info case */ |
---|
317 | act.sa_handler = Signal_handler; |
---|
318 | act.sa_flags = SA_SIGINFO; |
---|
319 | act.sa_sigaction = Signal_info_handler; |
---|
320 | |
---|
321 | sigaction( SIGUSR1, &act, NULL ); |
---|
322 | |
---|
323 | printf( "Init: sleep so the Task_3 can sigqueue SIGUSR1\n" ); |
---|
324 | status = sleep( 1 ); |
---|
325 | assert( !status ); |
---|
326 | |
---|
327 | /* switch to task 1 */ |
---|
328 | |
---|
329 | puts( "Init: sigqueue occurred" ); |
---|
330 | |
---|
331 | /* Send SIGUSR1, Task_3 has issued a sigwaitinfo */ |
---|
332 | |
---|
333 | status = sigemptyset( &mask ); |
---|
334 | assert( !status ); |
---|
335 | |
---|
336 | status = sigaddset( &mask, SIGUSR1 ); |
---|
337 | assert( !status ); |
---|
338 | |
---|
339 | printf( "Init: Block SIGUSR1\n" ); |
---|
340 | status = sigprocmask( SIG_BLOCK, &mask, NULL ); |
---|
341 | assert( !status ); |
---|
342 | |
---|
343 | printf( "Init: send SIGUSR1 to process\n" ); |
---|
344 | status = kill( getpid(), SIGUSR1 ); |
---|
345 | assert( !status ); |
---|
346 | |
---|
347 | printf( "Init: sleep so the Task_3 can receive SIGUSR1\n" ); |
---|
348 | status = sleep( 1 ); |
---|
349 | assert( !status ); |
---|
350 | |
---|
351 | /* Send SIGUSR1, Task_3 has issued a sigwait */ |
---|
352 | |
---|
353 | status = sigemptyset( &mask ); |
---|
354 | assert( !status ); |
---|
355 | |
---|
356 | status = sigaddset( &mask, SIGUSR1 ); |
---|
357 | assert( !status ); |
---|
358 | |
---|
359 | printf( "Init: Block SIGUSR1\n" ); |
---|
360 | status = sigprocmask( SIG_BLOCK, &mask, NULL ); |
---|
361 | assert( !status ); |
---|
362 | |
---|
363 | printf( "Init: send SIGUSR1 to process\n" ); |
---|
364 | status = kill( getpid(), SIGUSR1 ); |
---|
365 | assert( !status ); |
---|
366 | |
---|
367 | printf( "Init: sleep so the Task_3 can receive SIGUSR1\n" ); |
---|
368 | status = sleep( 1 ); |
---|
369 | assert( !status ); |
---|
370 | |
---|
371 | /* Send SIGUSR1, Task_3 has issued a sigwaitinfo */ |
---|
372 | |
---|
373 | status = sigemptyset( &mask ); |
---|
374 | assert( !status ); |
---|
375 | |
---|
376 | status = sigaddset( &mask, SIGUSR2 ); |
---|
377 | assert( !status ); |
---|
378 | |
---|
379 | printf( "Init: Block SIGUSR2\n" ); |
---|
380 | status = sigprocmask( SIG_BLOCK, &mask, NULL ); |
---|
381 | assert( !status ); |
---|
382 | |
---|
383 | printf( "Init: send SIGUSR2 to process\n" ); |
---|
384 | status = kill( getpid(), SIGUSR2 ); |
---|
385 | assert( !status ); |
---|
386 | |
---|
387 | printf( "Init: sleep so the Task_3 can receive SIGUSR2\n" ); |
---|
388 | status = sleep( 1 ); |
---|
389 | assert( !status ); |
---|
390 | |
---|
391 | /* Suspend for signal that has already be sent */ |
---|
392 | |
---|
393 | printf( "Init: sigsuspend for any signal\n" ); |
---|
394 | status = sigsuspend( &mask ); |
---|
395 | assert( status ); |
---|
396 | printf( "Init: awakended from sigsuspend status=%d \n", status ); |
---|
397 | |
---|
398 | /* exit this thread */ |
---|
399 | |
---|
400 | puts( "*** END OF POSIX TEST 4 ***" ); |
---|
401 | exit( 0 ); |
---|
402 | |
---|
403 | return NULL; /* just so the compiler thinks we returned something */ |
---|
404 | } |
---|