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 | #include <errno.h> |
---|
17 | |
---|
18 | volatile int Signal_occurred; |
---|
19 | volatile int Signal_count; |
---|
20 | |
---|
21 | void Signal_handler( |
---|
22 | int signo |
---|
23 | ) |
---|
24 | { |
---|
25 | Signal_count++; |
---|
26 | printf( |
---|
27 | "Signal: %d caught by 0x%x (%d)\n", |
---|
28 | signo, |
---|
29 | pthread_self(), |
---|
30 | Signal_count |
---|
31 | ); |
---|
32 | Signal_occurred = 1; |
---|
33 | } |
---|
34 | |
---|
35 | void *POSIX_Init( |
---|
36 | void *argument |
---|
37 | ) |
---|
38 | { |
---|
39 | int status; |
---|
40 | struct timespec timeout; |
---|
41 | struct sigaction act; |
---|
42 | sigset_t mask; |
---|
43 | sigset_t waitset; |
---|
44 | int signo; |
---|
45 | siginfo_t siginfo; |
---|
46 | |
---|
47 | puts( "\n\n*** POSIX TEST 3 ***" ); |
---|
48 | |
---|
49 | /* set the time of day, and print our buffer in multiple ways */ |
---|
50 | |
---|
51 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
52 | |
---|
53 | /* get id of this thread */ |
---|
54 | |
---|
55 | Init_id = pthread_self(); |
---|
56 | printf( "Init's ID is 0x%08x\n", Init_id ); |
---|
57 | |
---|
58 | /* install a signal handler */ |
---|
59 | |
---|
60 | status = sigemptyset( &act.sa_mask ); |
---|
61 | assert( !status ); |
---|
62 | |
---|
63 | act.sa_handler = Signal_handler; |
---|
64 | act.sa_flags = 0; |
---|
65 | |
---|
66 | sigaction( SIGUSR1, &act, NULL ); |
---|
67 | |
---|
68 | /* initialize signal handler variables */ |
---|
69 | |
---|
70 | Signal_count = 0; |
---|
71 | Signal_occurred = 0; |
---|
72 | |
---|
73 | /* |
---|
74 | * wait on SIGUSR1 for 3 seconds, will timeout |
---|
75 | */ |
---|
76 | |
---|
77 | /* initialize the signal set we will wait for to SIGUSR1 */ |
---|
78 | |
---|
79 | status = sigemptyset( &waitset ); |
---|
80 | assert( !status ); |
---|
81 | |
---|
82 | status = sigaddset( &waitset, SIGUSR1 ); |
---|
83 | assert( !status ); |
---|
84 | |
---|
85 | timeout.tv_sec = 3; |
---|
86 | timeout.tv_nsec = 0; |
---|
87 | |
---|
88 | puts( "Init: waiting on any signal for 3 seconds." ); |
---|
89 | signo = sigtimedwait( &waitset, &siginfo, &timeout ); |
---|
90 | assert( signo == -1 ); |
---|
91 | |
---|
92 | if ( errno == EAGAIN ) |
---|
93 | puts( "Init: correctly timed out waiting for SIGUSR1." ); |
---|
94 | else |
---|
95 | printf( "sigtimedwait returned wrong errno - %d\n", errno ); |
---|
96 | |
---|
97 | Signal_occurred = 0; |
---|
98 | |
---|
99 | /* |
---|
100 | * wait on SIGUSR1 for 3 seconds, will timeout because Task_1 sends SIGUSR2 |
---|
101 | */ |
---|
102 | |
---|
103 | empty_line(); |
---|
104 | |
---|
105 | /* initialize a mask to block SIGUSR2 */ |
---|
106 | |
---|
107 | status = sigemptyset( &mask ); |
---|
108 | assert( !status ); |
---|
109 | |
---|
110 | status = sigaddset( &mask, SIGUSR2 ); |
---|
111 | assert( !status ); |
---|
112 | |
---|
113 | printf( "Init: Block SIGUSR2\n" ); |
---|
114 | status = sigprocmask( SIG_BLOCK, &mask, NULL ); |
---|
115 | assert( !status ); |
---|
116 | |
---|
117 | /* create a thread */ |
---|
118 | |
---|
119 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
120 | assert( !status ); |
---|
121 | |
---|
122 | /* signal handler is still installed, waitset is still set for SIGUSR1 */ |
---|
123 | |
---|
124 | timeout.tv_sec = 3; |
---|
125 | timeout.tv_nsec = 0; |
---|
126 | |
---|
127 | puts( "Init: waiting on any signal for 3 seconds." ); |
---|
128 | signo = sigtimedwait( &waitset, &siginfo, &timeout ); |
---|
129 | |
---|
130 | /* switch to Task 1 */ |
---|
131 | |
---|
132 | if ( errno == EAGAIN ) |
---|
133 | puts( "Init: correctly timed out waiting for SIGUSR1." ); |
---|
134 | else |
---|
135 | printf( "sigtimedwait returned wrong errno - %d\n", errno ); |
---|
136 | assert( signo == -1 ); |
---|
137 | |
---|
138 | /* |
---|
139 | * wait on SIGUSR1 for 3 seconds, Task_2 will send it to us |
---|
140 | */ |
---|
141 | |
---|
142 | empty_line(); |
---|
143 | |
---|
144 | /* create a thread */ |
---|
145 | |
---|
146 | status = pthread_create( &Task_id, NULL, Task_2, NULL ); |
---|
147 | assert( !status ); |
---|
148 | |
---|
149 | /* signal handler is still installed, waitset is still set for SIGUSR1 */ |
---|
150 | |
---|
151 | /* wait on SIGUSR1 for 3 seconds, will receive SIGUSR1 from Task_2 */ |
---|
152 | |
---|
153 | timeout.tv_sec = 3; |
---|
154 | timeout.tv_nsec = 0; |
---|
155 | |
---|
156 | /* just so we can check that these were altered */ |
---|
157 | |
---|
158 | siginfo.si_code = -1; |
---|
159 | siginfo.si_signo = -1; |
---|
160 | siginfo.si_value.sival_int = -1; |
---|
161 | |
---|
162 | puts( "Init: waiting on any signal for 3 seconds." ); |
---|
163 | signo = sigtimedwait( &waitset, &siginfo, &timeout ); |
---|
164 | printf( "Init: received (%d) SIGUSR1=%d\n", siginfo.si_signo, SIGUSR1 ); |
---|
165 | assert( signo == SIGUSR1 ); |
---|
166 | assert( siginfo.si_signo == SIGUSR1 ); |
---|
167 | assert( siginfo.si_code == SI_USER ); |
---|
168 | assert( siginfo.si_value.sival_int != -1 ); /* rtems does always set this */ |
---|
169 | |
---|
170 | /* exit this thread */ |
---|
171 | |
---|
172 | puts( "*** END OF POSIX TEST 3 ***" ); |
---|
173 | exit( 0 ); |
---|
174 | |
---|
175 | return NULL; /* just so the compiler thinks we returned something */ |
---|
176 | } |
---|