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