source: rtems/testsuites/psxtests/psx04/init.c @ 03b900d

5
Last change on this file since 03b900d was 03b900d, checked in by Sebastian Huber <sebastian.huber@…>, on 02/18/16 at 07:36:26

score: Replace watchdog handler implementation

Use a red-black tree instead of delta chains.

Close #2344.
Update #2554.
Update #2555.
Close #2606.

  • Property mode set to 100644
File size: 15.9 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
19const char rtems_test_name[] = "PSX 4";
20
21volatile int Signal_occurred;
22volatile int Signal_count;
23void Signal_handler( int signo );
24void Signal_info_handler(
25  int        signo,
26  siginfo_t *info,
27  void      *context
28);
29
30void Signal_handler(
31  int signo
32)
33{
34  Signal_count++;
35  printf(
36    "Signal: %d caught by 0x%" PRIxpthread_t " (%d)\n",
37    signo,
38    pthread_self(),
39    Signal_count
40  );
41  Signal_occurred = 1;
42}
43
44void Signal_info_handler(
45  int        signo,
46  siginfo_t *info,
47  void      *context
48)
49{
50  Signal_count++;
51  printf(
52    "Signal_info: %d caught by 0x%" PRIxpthread_t " (%d) si_signo= %d si_code= %d value= %d\n",
53    signo,
54    pthread_self(),
55    Signal_count,
56    info->si_signo,
57    info->si_code,
58    info->si_value.sival_int
59  );
60  Signal_occurred = 1;
61}
62
63void *POSIX_Init(
64  void *argument
65)
66{
67  unsigned int      remaining;
68  int               status;
69  struct sigaction  act;
70  sigset_t          mask;
71  sigset_t          pending_set;
72  sigset_t          oset;
73  struct timespec   timeout;
74  siginfo_t         info;
75
76  TEST_BEGIN();
77
78  /* set the time of day, and print our buffer in multiple ways */
79
80  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
81
82  /* get id of this thread */
83
84  Init_id = pthread_self();
85  printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );
86
87  /* generate some easy error cases */
88
89  status = sigwait( NULL, NULL );
90  if ( status != EINVAL )
91    printf( "status = %d (%s)\n", status, strerror(status) );
92  rtems_test_assert( status == EINVAL );
93  puts( "Init: sigwait - EINVAL (NULL set)" );
94
95  status = sigtimedwait( NULL, NULL, NULL );
96  if ( status != -1 )
97    printf( "status = %d\n", status );
98  rtems_test_assert( errno == EINVAL );
99  puts( "Init: sigwait - EINVAL (NULL set)" );
100
101/* install a signal handler for SIGUSR1 */
102
103  status = sigemptyset( &act.sa_mask );
104  rtems_test_assert( !status );
105  printf( "Init: sigemptyset -  set= 0x%08x\n", (unsigned int) act.sa_mask );
106
107  /* test sigfillset following the above sigemptyset */
108
109  status = sigfillset( &act.sa_mask );
110  rtems_test_assert( !status );
111  printf( "Init: sigfillset -  set= 0x%08x\n", (unsigned int) act.sa_mask );
112
113  /* test sigdelset */
114
115  status = sigdelset( &act.sa_mask, SIGUSR1 );
116  rtems_test_assert( !status );
117  printf( "Init: sigdelset - delete SIGUSR1 set= 0x%08x\n",
118      (unsigned int) act.sa_mask );
119
120  /* test sigismember - FALSE */
121
122  status = sigismember( &act.sa_mask, SIGUSR1 );
123  rtems_test_assert( !status );
124  puts( "Init: sigismember - FALSE since SIGUSR1 is not a member" );
125
126  /* test sigismember - TRUE */
127
128  status = sigismember( &act.sa_mask, SIGUSR2 );
129  rtems_test_assert( status );
130  puts( "Init: sigismember - TRUE since SIGUSR2 is a member" );
131
132  /* return the set to empty */
133
134  act.sa_handler = Signal_handler;
135  act.sa_flags   = 0;
136
137  sigaction( SIGUSR1, &act, NULL );
138
139  /* simple signal to process */
140
141  Signal_count = 0;
142  Signal_occurred = 0;
143
144  puts( "Init: send SIGUSR1 to process" );
145  status = kill( getpid(), SIGUSR1 );
146  rtems_test_assert( !status );
147
148/* end of install a signal handler for SIGUSR1 */
149
150  Signal_occurred = 0;
151
152  /* now block the signal, send it, see if it is pending, and unblock it */
153
154  empty_line();
155
156  status = sigemptyset( &mask );
157  rtems_test_assert( !status );
158
159  status = sigaddset( &mask, SIGUSR1 );
160  rtems_test_assert( !status );
161
162  puts( "Init: Block SIGUSR1" );
163  act.sa_handler = Signal_handler;
164  act.sa_flags   = 0;
165
166  sigaction( SIGUSR1, &act, NULL );
167
168  /* simple signal to process */
169
170  Signal_count = 0;
171  Signal_occurred = 0;
172
173  puts( "Init: send SIGUSR1 to process" );
174  status = kill( getpid(), SIGUSR1 );
175  rtems_test_assert( !status );
176
177  Signal_occurred = 0;
178
179  /* now block the signal, send it, see if it is pending, and unblock it */
180
181  empty_line();
182
183  status = sigemptyset( &mask );
184  rtems_test_assert( !status );
185
186  status = sigaddset( &mask, SIGUSR1 );
187  rtems_test_assert( !status );
188
189  puts( "Init: Block SIGUSR1" );
190  status = sigprocmask( SIG_BLOCK, &mask, NULL );
191  rtems_test_assert( !status );
192
193  status = sigpending( &pending_set );
194  rtems_test_assert( !status );
195  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
196
197  puts( "Init: send SIGUSR1 to process" );
198  status = kill( getpid(), SIGUSR1 );
199  rtems_test_assert( !status );
200
201  status = sigpending( &pending_set );
202  rtems_test_assert( !status );
203  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
204
205  puts( "Init: Unblock SIGUSR1" );
206  status = sigprocmask( SIG_UNBLOCK, &mask, NULL );
207  rtems_test_assert( !status );
208
209  /* now let another task get interrupted by a signal */
210
211  empty_line();
212
213  puts( "Init: create a thread interested in SIGUSR1" );
214  status = pthread_create( &Task1_id, NULL, Task_1, NULL );
215  rtems_test_assert( !status );
216
217  puts( "Init: Block SIGUSR1" );
218  status = sigprocmask( SIG_BLOCK, &mask, NULL );
219  rtems_test_assert( !status );
220
221  status = sigpending( &pending_set );
222  rtems_test_assert( !status );
223  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
224
225  puts( "Init: sleep so the other task can block" );
226  remaining = sleep( 1 );
227  rtems_test_assert( !status );
228
229     /* switch to task 1 */
230
231  puts( "Init: send SIGUSR1 to process" );
232  status = kill( getpid(), SIGUSR1 );
233  rtems_test_assert( !status );
234
235  status = sigpending( &pending_set );
236  rtems_test_assert( !status );
237  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
238
239  puts( "Init: sleep so the other task can catch signal" );
240  remaining = sleep( 1 );
241  rtems_test_assert( !status );
242
243     /* switch to task 1 */
244
245  /* test alarm */
246
247  empty_line();
248
249  /* install a signal handler for SIGALRM and unblock it */
250
251  status = sigemptyset( &act.sa_mask );
252  rtems_test_assert( !status );
253
254  act.sa_handler = Signal_handler;
255  act.sa_flags   = 0;
256
257  sigaction( SIGALRM, &act, NULL );
258
259  status = sigemptyset( &mask );
260  rtems_test_assert( !status );
261
262  status = sigaddset( &mask, SIGALRM );
263  rtems_test_assert( !status );
264
265  puts( "Init: Unblock SIGALRM" );
266  status = sigprocmask( SIG_UNBLOCK, &mask, NULL );
267  rtems_test_assert( !status );
268
269  /* schedule the alarm */
270
271  puts( "Init: Firing alarm in 5 seconds" );
272  remaining = alarm( 5 );
273  printf( "Init: %d seconds left on previous alarm\n", status );
274  rtems_test_assert( !status );
275
276  puts( "Init: Firing alarm in 2 seconds" );
277  remaining = alarm( 2 );
278  printf( "Init: %d seconds left on previous alarm\n", remaining );
279  rtems_test_assert( remaining == 5 );
280
281  puts( "Init: Wait 4 seconds for alarm" );
282  remaining = sleep( 4 );
283  printf( "Init: %d seconds left in sleep\n", remaining );
284
285  /*
286   * sleep() uses nanosleep() internally which discards the nanoseconds part,
287   * e.g. 1.99s -> 1s
288   */
289  rtems_test_assert( remaining == 1 || remaining == 2 );
290
291  /* test SIG_SETMASK case and returning oset of pthread_sigmask */
292
293  empty_line();
294
295  status = sigemptyset( &mask );
296  rtems_test_assert( !status );
297
298  status = sigaddset( &mask, SIGUSR1 );
299  rtems_test_assert( !status );
300
301  status = sigaddset( &mask, SIGUSR2 );
302  rtems_test_assert( !status );
303
304  puts( "Init: Block SIGUSR1 and SIGUSR2 only" );
305  status = pthread_sigmask( SIG_SETMASK, &mask, &oset );
306  printf( "Init: Previous blocked set was 0x%08x\n", (unsigned int) oset );
307  rtems_test_assert( !status );
308
309  /* test inquiry about current blocked set with pthread_sigmask */
310
311  status = pthread_sigmask( 0, NULL, &oset );
312  printf( "Init: Current blocked set is 0x%08x\n", (unsigned int) oset );
313  rtems_test_assert( !status );
314
315  /* return blocked mask to no signals blocked */
316
317  status = sigemptyset( &mask );
318  rtems_test_assert( !status );
319
320  puts( "Init: Unblock all signals" );
321  status = pthread_sigmask( SIG_SETMASK, &mask, &oset );
322  printf( "Init: Previous blocked set was 0x%08x\n", (unsigned int) oset );
323  rtems_test_assert( !status );
324
325  /* test sigsuspend */
326
327  empty_line();
328
329  puts( "Init: create a thread to send Init SIGUSR1" );
330  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
331  rtems_test_assert( !status );
332
333  status = sigemptyset( &mask );
334  rtems_test_assert( !status );
335
336  puts( "Init: sigsuspend for any signal" );
337  status = sigsuspend( &mask );
338  rtems_test_assert( status );
339  printf( "Init: awakended from sigsuspend status=%08d \n", status );
340
341  /* test a SIGINFO case, these are signals sent to a process only */
342
343  empty_line();
344
345  puts( "Init: create a thread to sent Process SIGUSR1 with SA_SIGINFO" );
346  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
347  rtems_test_assert( !status );
348
349  /* set action on SIGUSR1 to an info case */
350  act.sa_handler   = Signal_handler;
351  act.sa_flags     = SA_SIGINFO;
352  act.sa_sigaction = Signal_info_handler;
353
354  sigaction( SIGUSR1, &act, NULL );
355
356  puts( "Init: sleep so the Task_3 can sigqueue SIGUSR1" );
357  remaining = sleep( 1 );
358  rtems_test_assert( !status );
359
360     /* switch to task 1 */
361
362  puts( "Init: sigqueue occurred" );
363
364  /* Send SIGUSR1, Task_3 has issued a sigwaitinfo */
365
366  status = sigemptyset( &mask );
367  rtems_test_assert( !status );
368
369  status = sigaddset( &mask, SIGUSR1 );
370  rtems_test_assert( !status );
371
372  puts( "Init: Block SIGUSR1" );
373  status = sigprocmask( SIG_BLOCK, &mask, NULL );
374  rtems_test_assert( !status );
375
376  puts( "Init: send SIGUSR1 to process" );
377  status = kill( getpid(), SIGUSR1 );
378  rtems_test_assert( !status );
379
380  puts( "Init: sleep so the Task_3 can receive SIGUSR1" );
381  remaining = sleep( 1 );
382  rtems_test_assert( !status );
383
384  /* Send SIGUSR1, Task_3 has issued a sigwait */
385
386  status = sigemptyset( &mask );
387  rtems_test_assert( !status );
388
389  status = sigaddset( &mask, SIGUSR1 );
390  rtems_test_assert( !status );
391
392  puts( "Init: Block SIGUSR1" );
393  status = sigprocmask( SIG_BLOCK, &mask, NULL );
394  rtems_test_assert( !status );
395
396  puts( "Init: send SIGUSR1 to process" );
397  status = kill( getpid(), SIGUSR1 );
398  rtems_test_assert( !status );
399
400  puts( "Init: sleep so the Task_3 can receive SIGUSR1" );
401  remaining = sleep( 1 );
402  rtems_test_assert( !status );
403
404  /* Send SIGUSR1, Task_3 has issued a sigwaitinfo */
405
406  status = sigemptyset( &mask );
407  rtems_test_assert( !status );
408
409  status = sigaddset( &mask, SIGUSR2 );
410  rtems_test_assert( !status );
411
412  puts( "Init: Block SIGUSR2" );
413  status = sigprocmask( SIG_BLOCK, &mask, NULL );
414  rtems_test_assert( !status );
415
416  puts( "Init: send SIGUSR2 to process" );
417  status = kill( getpid(), SIGUSR2 );
418  rtems_test_assert( !status );
419
420  puts( "Init: sleep so the Task_3 can receive SIGUSR2" );
421  remaining = sleep( 1 );
422  rtems_test_assert( !status );
423
424  /* Suspend for signal that has already be sent */
425
426  status = sigemptyset( &mask );
427  rtems_test_assert( !status );
428
429  puts( "Init: sigsuspend for any signal" );
430  status = sigsuspend( &mask );
431  rtems_test_assert( status );
432  printf( "Init: awakended from sigsuspend status=%d \n", status );
433
434  /* generate error cases for psignal */
435
436  empty_line();
437
438  status = sigemptyset( NULL );
439  if ( status != -1 )
440    printf( "status = %d\n", status );
441  rtems_test_assert( errno == EINVAL );
442  puts( "Init: sigemptyset - EINVAL (set invalid)" );
443
444  status = sigfillset( NULL );
445  if ( status != -1 )
446    printf( "status = %d\n", status );
447  rtems_test_assert( errno == EINVAL );
448  puts( "Init: sigfillset - EINVAL (set invalid)" );
449
450  status = sigaddset( NULL, SIGUSR1 );
451  if ( status != -1 )
452    printf( "status = %d\n", status );
453  rtems_test_assert( errno == EINVAL );
454  puts( "Init: sigaddset - EINVAL (set invalid)" );
455
456  status = sigaddset( &mask, 0 );
457  if ( status != -1 )
458    printf( "status = %d\n", status );
459  rtems_test_assert( errno == EINVAL );
460  puts( "Init: sigaddset - EINVAL (signal = 0)" );
461
462  status = sigaddset( &mask, 999 );
463  if ( status != -1 )
464    printf( "status = %d\n", status );
465  rtems_test_assert( errno == EINVAL );
466  puts( "Init: sigaddset - EINVAL (set invalid)" );
467
468  status = sigdelset( NULL, SIGUSR1 );
469  if ( status != -1 )
470    printf( "status = %d\n", status );
471  rtems_test_assert( errno == EINVAL );
472  puts( "Init: sigdelset - EINVAL (set invalid)" );
473
474  status = sigdelset( &mask, 0 );
475  rtems_test_assert( !status );
476  puts( "Init: sigdelset - SUCCESSFUL (signal = 0)" );
477
478  status = sigdelset( &mask, 999 );
479  if ( status != -1 )
480    printf( "status = %d\n", status );
481  rtems_test_assert( errno == EINVAL );
482  puts( "Init: sigdelset - EINVAL (set invalid)" );
483
484  status = sigismember( NULL, SIGUSR1 );
485  if ( status != -1 )
486    printf( "status = %d\n", status );
487  rtems_test_assert( errno == EINVAL );
488  puts( "Init: sigismember - EINVAL (set invalid)" );
489
490  status = sigismember( &mask, 0 );
491  rtems_test_assert( !status );
492  puts( "Init: sigismember - SUCCESSFUL (signal = 0)" );
493
494  status = sigismember( &mask, 999 );
495  if ( status != -1 )
496    printf( "status = %d\n", status );
497  rtems_test_assert( errno == EINVAL );
498  puts( "Init: sigismember - EINVAL (signal invalid)" );
499
500  status = sigaction( 0, &act, 0 );
501  if ( status != -1 )
502    printf( "status = %d\n", status );
503  rtems_test_assert( errno == EINVAL );
504  puts( "Init: sigaction - EINVAL (signal = 0)" );
505
506  status = sigaction( 999, &act, NULL );
507  if ( status != -1 )
508    printf( "status = %d\n", status );
509  rtems_test_assert( errno == EINVAL );
510  puts( "Init: sigaction - EINVAL (signal invalid)" );
511
512  status = sigaction( SIGKILL, &act, NULL );
513  if ( status != -1 )
514    printf( "status = %d\n", status );
515  rtems_test_assert( errno == EINVAL );
516  puts( "Init: sigaction - EINVAL (SIGKILL)" );
517
518  status = pthread_sigmask( SIG_BLOCK, NULL, NULL );
519  if ( status != -1 )
520    printf( "status = %d\n", status );
521  rtems_test_assert( errno == EINVAL );
522  puts( "Init: pthread_sigmask - EINVAL (set and oset invalid)" );
523
524  status = pthread_sigmask( 999, &pending_set, NULL );
525  if ( status != -1 )
526    printf( "status = %d\n", status );
527  rtems_test_assert( errno == EINVAL );
528  puts( "Init: pthread_sigmask - EINVAL (how invalid)" );
529
530  status = sigpending( NULL );
531  if ( status != -1 )
532    printf( "status = %d\n", status );
533  rtems_test_assert( errno == EINVAL );
534  puts( "Init: sigpending - EINVAL (set invalid)" );
535
536  timeout.tv_nsec = -1;
537  status = sigtimedwait( &mask, &info, &timeout );
538  if ( status != -1 )
539    printf( "status = %d\n", status );
540  rtems_test_assert( errno == EINVAL );
541  puts( "Init: pthread_sigmask - EINVAL (timout->nsec invalid < 0)" );
542
543  timeout.tv_nsec = 0x7fffffff;
544  status = sigtimedwait( &mask, &info, &timeout );
545  if ( status != -1 )
546    printf( "status = %d\n", status );
547  rtems_test_assert( errno == EINVAL );
548  puts( "Init: pthread_sigmask - EINVAL (timout->nsec invalid to large)" );
549
550  status = pthread_kill( Init_id, 999 );
551  if ( status != -1 )
552    printf( "status = %d\n", status );
553  rtems_test_assert( errno == EINVAL );
554  puts( "Init: pthread_kill - EINVAL (sig invalid)" );
555
556  status = pthread_kill( Init_id, 0 );
557  if ( status != -1 )
558    printf( "status = %d\n", status );
559  rtems_test_assert( errno == EINVAL );
560  puts( "Init: pthread_kill - EINVAL (signal = 0)" );
561
562  act.sa_handler = SIG_IGN;
563  act.sa_flags = 0;
564  sigaction( SIGUSR2, &act, NULL );
565  status = pthread_kill( Init_id, SIGUSR2 );
566  rtems_test_assert( !status );
567  puts( "Init: pthread_kill - SUCCESSFUL (signal = SIG_IGN)" );
568
569  status = kill( INT_MAX, SIGUSR1 );
570  if ( status != -1 )
571    printf( "status = %d\n", status );
572  rtems_test_assert( errno == ESRCH );
573  puts( "Init: kill - ESRCH (pid invalid)" );
574
575  status = kill( getpid(), 0 );
576  if ( status != -1 )
577    printf( "status = %d\n", status );
578  rtems_test_assert( errno == EINVAL );
579  puts( "Init: kill - EINVAL (signal = 0)" );
580
581  status = kill( getpid(), 999 );
582  if ( status != -1 )
583    printf( "status = %d\n", status );
584  rtems_test_assert( errno == EINVAL );
585  puts( "Init: kill - EINVAL (sig invalid)" );
586
587  /* exit this thread */
588
589  TEST_END();
590  rtems_test_exit( 0 );
591
592  return NULL; /* just so the compiler thinks we returned something */
593}
Note: See TracBrowser for help on using the repository browser.