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

4.104.11
Last change on this file since 2819bd1 was 2819bd1, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 3, 2009 at 6:39:11 PM

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
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.com/license/LICENSE.
8 *
9 *  $Id$
10 */
11
12#define CONFIGURE_INIT
13#include "system.h"
14#include <signal.h>
15#include <errno.h>
16
17volatile int Signal_occurred;
18volatile int Signal_count;
19void Signal_handler( int signo );
20void Signal_info_handler(
21  int        signo,
22  siginfo_t *info,
23  void      *context
24);
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
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
59void *POSIX_Init(
60  void *argument
61)
62{
63  unsigned int      remaining;
64  int               status;
65  struct sigaction  act;
66  sigset_t          mask;
67  sigset_t          pending_set;
68  sigset_t          oset;
69  struct timespec   timeout;
70  siginfo_t         info;
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
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
97/* install a signal handler for SIGUSR1 */
98
99  status = sigemptyset( &act.sa_mask );
100  assert( !status );
101  printf( "Init: sigemptyset -  set= 0x%08x\n", (unsigned int) act.sa_mask );
102
103  /* test sigfillset following the above sigemptyset */
104
105  status = sigfillset( &act.sa_mask );
106  assert( !status );
107  printf( "Init: sigfillset -  set= 0x%08x\n", (unsigned int) act.sa_mask );
108
109  /* test sigdelset */
110
111  status = sigdelset( &act.sa_mask, SIGUSR1 );
112  assert( !status );
113  printf( "Init: sigdelset - delete SIGUSR1 set= 0x%08x\n",
114      (unsigned int) act.sa_mask );
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 */
129
130  act.sa_handler = Signal_handler;
131  act.sa_flags   = 0;
132
133  sigaction( SIGUSR1, &act, NULL );
134
135  /* simple signal to process */
136
137  Signal_count = 0;
138  Signal_occurred = 0;
139
140  puts( "Init: send SIGUSR1 to process" );
141  status = kill( getpid(), SIGUSR1 );
142  assert( !status );
143
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
158  puts( "Init: Block SIGUSR1" );
159  act.sa_handler = Signal_handler;
160  act.sa_flags   = 0;
161
162  sigaction( SIGUSR1, &act, NULL );
163
164  /* simple signal to process */
165
166  Signal_count = 0;
167  Signal_occurred = 0;
168
169  puts( "Init: send SIGUSR1 to process" );
170  status = kill( getpid(), SIGUSR1 );
171  assert( !status );
172
173  Signal_occurred = 0;
174
175  /* now block the signal, send it, see if it is pending, and unblock it */
176
177  empty_line();
178
179  status = sigemptyset( &mask );
180  assert( !status );
181
182  status = sigaddset( &mask, SIGUSR1 );
183  assert( !status );
184
185  puts( "Init: Block SIGUSR1" );
186  status = sigprocmask( SIG_BLOCK, &mask, NULL );
187  assert( !status );
188
189  status = sigpending( &pending_set );
190  assert( !status );
191  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
192
193  puts( "Init: send SIGUSR1 to process" );
194  status = kill( getpid(), SIGUSR1 );
195  assert( !status );
196
197  status = sigpending( &pending_set );
198  assert( !status );
199  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
200
201  puts( "Init: Unblock SIGUSR1" );
202  status = sigprocmask( SIG_UNBLOCK, &mask, NULL );
203  assert( !status );
204
205  /* now let another task get interrupted by a signal */
206
207  empty_line();
208
209  puts( "Init: create a thread interested in SIGUSR1" );
210  status = pthread_create( &Task1_id, NULL, Task_1, NULL );
211  assert( !status );
212
213  puts( "Init: Block SIGUSR1" );
214  status = sigprocmask( SIG_BLOCK, &mask, NULL );
215  assert( !status );
216
217  status = sigpending( &pending_set );
218  assert( !status );
219  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
220
221  puts( "Init: sleep so the other task can block" );
222  remaining = sleep( 1 );
223  assert( !status );
224
225     /* switch to task 1 */
226
227  puts( "Init: send SIGUSR1 to process" );
228  status = kill( getpid(), SIGUSR1 );
229  assert( !status );
230
231  status = sigpending( &pending_set );
232  assert( !status );
233  printf( "Init: Signals pending 0x%08x\n", (unsigned int) pending_set );
234
235  puts( "Init: sleep so the other task can catch signal" );
236  remaining = sleep( 1 );
237  assert( !status );
238
239     /* switch to task 1 */
240
241  /* test alarm */
242
243  empty_line();
244
245  /* install a signal handler for SIGALRM and unblock it */
246
247  status = sigemptyset( &act.sa_mask );
248  assert( !status );
249
250  act.sa_handler = Signal_handler;
251  act.sa_flags   = 0;
252
253  sigaction( SIGALRM, &act, NULL );
254
255  status = sigemptyset( &mask );
256  assert( !status );
257
258  status = sigaddset( &mask, SIGALRM );
259  assert( !status );
260
261  puts( "Init: Unblock SIGALRM" );
262  status = sigprocmask( SIG_UNBLOCK, &mask, NULL );
263  assert( !status );
264
265  /* schedule the alarm */
266
267  puts( "Init: Firing alarm in 5 seconds" );
268  remaining = alarm( 5 );
269  printf( "Init: %d seconds left on previous alarm\n", status );
270  assert( !status );
271
272  puts( "Init: Firing alarm in 2 seconds" );
273  remaining = alarm( 2 );
274  printf( "Init: %d seconds left on previous alarm\n", remaining );
275  assert( remaining == 5 );
276
277  puts( "Init: Wait 4 seconds for alarm" );
278  remaining = sleep( 4 );
279  printf( "Init: %d seconds left in sleep\n", remaining );
280  assert( remaining == 2 );
281
282  /* test SIG_SETMASK case and returning oset of pthread_sigmask */
283
284  empty_line();
285
286  status = sigemptyset( &mask );
287  assert( !status );
288
289  status = sigaddset( &mask, SIGUSR1 );
290  assert( !status );
291
292  status = sigaddset( &mask, SIGUSR2 );
293  assert( !status );
294
295  puts( "Init: Block SIGUSR1 and SIGUSR2 only" );
296  status = pthread_sigmask( SIG_SETMASK, &mask, &oset );
297  printf( "Init: Previous blocked set was 0x%08x\n", (unsigned int) oset );
298  assert( !status );
299
300  /* test inquiry about current blocked set with pthread_sigmask */
301
302  status = pthread_sigmask( 0, NULL, &oset );
303  printf( "Init: Current blocked set is 0x%08x\n", (unsigned int) oset );
304  assert( !status );
305
306  /* return blocked mask to no signals blocked */
307
308  status = sigemptyset( &mask );
309  assert( !status );
310
311  puts( "Init: Unblock all signals" );
312  status = pthread_sigmask( SIG_SETMASK, &mask, &oset );
313  printf( "Init: Previous blocked set was 0x%08x\n", (unsigned int) oset );
314  assert( !status );
315
316  /* test sigsuspend */
317
318  empty_line();
319
320  puts( "Init: create a thread to send Init SIGUSR1" );
321  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
322  assert( !status );
323
324  status = sigemptyset( &mask );
325  assert( !status );
326
327  puts( "Init: sigsuspend for any signal" );
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
336  puts( "Init: create a thread to sent Process SIGUSR1 with SA_SIGINFO" );
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;
344
345  sigaction( SIGUSR1, &act, NULL );
346
347  puts( "Init: sleep so the Task_3 can sigqueue SIGUSR1" );
348  remaining = sleep( 1 );
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
363  puts( "Init: Block SIGUSR1" );
364  status = sigprocmask( SIG_BLOCK, &mask, NULL );
365  assert( !status );
366
367  puts( "Init: send SIGUSR1 to process" );
368  status = kill( getpid(), SIGUSR1 );
369  assert( !status );
370
371  puts( "Init: sleep so the Task_3 can receive SIGUSR1" );
372  remaining = sleep( 1 );
373  assert( !status );
374
375  /* Send SIGUSR1, Task_3 has issued a sigwait */
376
377  status = sigemptyset( &mask );
378  assert( !status );
379
380  status = sigaddset( &mask, SIGUSR1 );
381  assert( !status );
382
383  puts( "Init: Block SIGUSR1" );
384  status = sigprocmask( SIG_BLOCK, &mask, NULL );
385  assert( !status );
386
387  puts( "Init: send SIGUSR1 to process" );
388  status = kill( getpid(), SIGUSR1 );
389  assert( !status );
390
391  puts( "Init: sleep so the Task_3 can receive SIGUSR1" );
392  remaining = sleep( 1 );
393  assert( !status );
394
395  /* Send SIGUSR1, Task_3 has issued a sigwaitinfo */
396
397  status = sigemptyset( &mask );
398  assert( !status );
399
400  status = sigaddset( &mask, SIGUSR2 );
401  assert( !status );
402
403  puts( "Init: Block SIGUSR2" );
404  status = sigprocmask( SIG_BLOCK, &mask, NULL );
405  assert( !status );
406
407  puts( "Init: send SIGUSR2 to process" );
408  status = kill( getpid(), SIGUSR2 );
409  assert( !status );
410
411  puts( "Init: sleep so the Task_3 can receive SIGUSR2" );
412  remaining = sleep( 1 );
413  assert( !status );
414
415  /* Suspend for signal that has already be sent */
416
417  puts( "Init: sigsuspend for any signal" );
418  status = sigsuspend( &mask );
419  assert( status );
420  printf( "Init: awakended from sigsuspend status=%d \n", status );
421
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 );
429  assert( errno == EINVAL );
430  puts( "Init: sigemptyset - EINVAL (set invalid)" );
431
432  status = sigfillset( NULL );
433  if ( status != -1 )
434    printf( "status = %d\n", status );
435  assert( errno == EINVAL );
436  puts( "Init: sigfillset - EINVAL (set invalid)" );
437
438  status = sigaddset( NULL, SIGUSR1 );
439  if ( status != -1 )
440    printf( "status = %d\n", status );
441  assert( errno == EINVAL );
442  puts( "Init: sigaddset - EINVAL (set invalid)" );
443
444  status = sigaddset( &mask, 0 );
445  if ( status != -1 )
446    printf( "status = %d\n", status );
447  assert( errno == EINVAL );
448  puts( "Init: sigaddset - EINVAL (signal = 0)" );
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 );
459  assert( errno == EINVAL );
460  puts( "Init: sigdelset - EINVAL (set invalid)" );
461
462  status = sigdelset( &mask, 0 );
463  assert( !status );
464  puts( "Init: sigdelset - SUCCESSFUL (signal = 0)" );
465
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 );
475  assert( errno == EINVAL );
476  puts( "Init: sigismember - EINVAL (set invalid)" );
477
478  status = sigismember( &mask, 0 );
479  assert( !status );
480  puts( "Init: sigismember - SUCCESSFUL (signal = 0)" );
481
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 );
489  if ( status != -1 )
490    printf( "status = %d\n", status );
491  assert( errno == EINVAL );
492  puts( "Init: sigaction - EINVAL (signal = 0)" );
493
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 );
509  assert( errno == EINVAL );
510  puts( "Init: pthread_sigmask - EINVAL (set and oset invalid)" );
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 );
521  assert( errno == EINVAL );
522  puts( "Init: sigpending - EINVAL (set invalid)" );
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 );
551  if ( status != -1 )
552    printf( "status = %d\n", status );
553  assert( errno == EINVAL );
554  puts( "Init: pthread_kill - EINVAL (signal = 0)" );
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 );
570  if ( status != -1 )
571    printf( "status = %d\n", status );
572  assert( errno == EINVAL );
573  puts( "Init: kill - EINVAL (signal = 0)" );
574
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
581  /* exit this thread */
582
583  puts( "*** END OF POSIX TEST 4 ***" );
584  rtems_test_exit( 0 );
585
586  return NULL; /* just so the compiler thinks we returned something */
587}
Note: See TracBrowser for help on using the repository browser.