source: rtems/testsuites/psxtests/psx03/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

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