source: rtems/testsuites/psxtests/psx04/init.c @ 2819bd1

4.104.115
Last change on this file since 2819bd1 was 2819bd1, checked in by Joel Sherrill <joel.sherrill@…>, on 07/03/09 at 18:39:11

2009-07-03 Joel Sherrill <joel.sherrill@…>

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