source: rtems/testsuites/psxtests/psx04/init.c @ 698c2e50

4.115
Last change on this file since 698c2e50 was 698c2e50, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 07:06:16

tests/psxtests: Use <rtems/test.h>

  • Property mode set to 100644
File size: 15.8 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  rtems_test_assert( remaining == 2 );
285
286  /* test SIG_SETMASK case and returning oset of pthread_sigmask */
287
288  empty_line();
289
290  status = sigemptyset( &mask );
291  rtems_test_assert( !status );
292
293  status = sigaddset( &mask, SIGUSR1 );
294  rtems_test_assert( !status );
295
296  status = sigaddset( &mask, SIGUSR2 );
297  rtems_test_assert( !status );
298
299  puts( "Init: Block SIGUSR1 and SIGUSR2 only" );
300  status = pthread_sigmask( SIG_SETMASK, &mask, &oset );
301  printf( "Init: Previous blocked set was 0x%08x\n", (unsigned int) oset );
302  rtems_test_assert( !status );
303
304  /* test inquiry about current blocked set with pthread_sigmask */
305
306  status = pthread_sigmask( 0, NULL, &oset );
307  printf( "Init: Current blocked set is 0x%08x\n", (unsigned int) oset );
308  rtems_test_assert( !status );
309
310  /* return blocked mask to no signals blocked */
311
312  status = sigemptyset( &mask );
313  rtems_test_assert( !status );
314
315  puts( "Init: Unblock all signals" );
316  status = pthread_sigmask( SIG_SETMASK, &mask, &oset );
317  printf( "Init: Previous blocked set was 0x%08x\n", (unsigned int) oset );
318  rtems_test_assert( !status );
319
320  /* test sigsuspend */
321
322  empty_line();
323
324  puts( "Init: create a thread to send Init SIGUSR1" );
325  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
326  rtems_test_assert( !status );
327
328  status = sigemptyset( &mask );
329  rtems_test_assert( !status );
330
331  puts( "Init: sigsuspend for any signal" );
332  status = sigsuspend( &mask );
333  rtems_test_assert( status );
334  printf( "Init: awakended from sigsuspend status=%08d \n", status );
335
336  /* test a SIGINFO case, these are signals sent to a process only */
337
338  empty_line();
339
340  puts( "Init: create a thread to sent Process SIGUSR1 with SA_SIGINFO" );
341  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
342  rtems_test_assert( !status );
343
344  /* set action on SIGUSR1 to an info case */
345  act.sa_handler   = Signal_handler;
346  act.sa_flags     = SA_SIGINFO;
347  act.sa_sigaction = Signal_info_handler;
348
349  sigaction( SIGUSR1, &act, NULL );
350
351  puts( "Init: sleep so the Task_3 can sigqueue SIGUSR1" );
352  remaining = sleep( 1 );
353  rtems_test_assert( !status );
354
355     /* switch to task 1 */
356
357  puts( "Init: sigqueue occurred" );
358
359  /* Send SIGUSR1, Task_3 has issued a sigwaitinfo */
360
361  status = sigemptyset( &mask );
362  rtems_test_assert( !status );
363
364  status = sigaddset( &mask, SIGUSR1 );
365  rtems_test_assert( !status );
366
367  puts( "Init: Block SIGUSR1" );
368  status = sigprocmask( SIG_BLOCK, &mask, NULL );
369  rtems_test_assert( !status );
370
371  puts( "Init: send SIGUSR1 to process" );
372  status = kill( getpid(), SIGUSR1 );
373  rtems_test_assert( !status );
374
375  puts( "Init: sleep so the Task_3 can receive SIGUSR1" );
376  remaining = sleep( 1 );
377  rtems_test_assert( !status );
378
379  /* Send SIGUSR1, Task_3 has issued a sigwait */
380
381  status = sigemptyset( &mask );
382  rtems_test_assert( !status );
383
384  status = sigaddset( &mask, SIGUSR1 );
385  rtems_test_assert( !status );
386
387  puts( "Init: Block SIGUSR1" );
388  status = sigprocmask( SIG_BLOCK, &mask, NULL );
389  rtems_test_assert( !status );
390
391  puts( "Init: send SIGUSR1 to process" );
392  status = kill( getpid(), SIGUSR1 );
393  rtems_test_assert( !status );
394
395  puts( "Init: sleep so the Task_3 can receive SIGUSR1" );
396  remaining = sleep( 1 );
397  rtems_test_assert( !status );
398
399  /* Send SIGUSR1, Task_3 has issued a sigwaitinfo */
400
401  status = sigemptyset( &mask );
402  rtems_test_assert( !status );
403
404  status = sigaddset( &mask, SIGUSR2 );
405  rtems_test_assert( !status );
406
407  puts( "Init: Block SIGUSR2" );
408  status = sigprocmask( SIG_BLOCK, &mask, NULL );
409  rtems_test_assert( !status );
410
411  puts( "Init: send SIGUSR2 to process" );
412  status = kill( getpid(), SIGUSR2 );
413  rtems_test_assert( !status );
414
415  puts( "Init: sleep so the Task_3 can receive SIGUSR2" );
416  remaining = sleep( 1 );
417  rtems_test_assert( !status );
418
419  /* Suspend for signal that has already be sent */
420
421  status = sigemptyset( &mask );
422  rtems_test_assert( !status );
423
424  puts( "Init: sigsuspend for any signal" );
425  status = sigsuspend( &mask );
426  rtems_test_assert( status );
427  printf( "Init: awakended from sigsuspend status=%d \n", status );
428
429  /* generate error cases for psignal */
430
431  empty_line();
432
433  status = sigemptyset( NULL );
434  if ( status != -1 )
435    printf( "status = %d\n", status );
436  rtems_test_assert( errno == EINVAL );
437  puts( "Init: sigemptyset - EINVAL (set invalid)" );
438
439  status = sigfillset( NULL );
440  if ( status != -1 )
441    printf( "status = %d\n", status );
442  rtems_test_assert( errno == EINVAL );
443  puts( "Init: sigfillset - EINVAL (set invalid)" );
444
445  status = sigaddset( NULL, SIGUSR1 );
446  if ( status != -1 )
447    printf( "status = %d\n", status );
448  rtems_test_assert( errno == EINVAL );
449  puts( "Init: sigaddset - EINVAL (set invalid)" );
450
451  status = sigaddset( &mask, 0 );
452  if ( status != -1 )
453    printf( "status = %d\n", status );
454  rtems_test_assert( errno == EINVAL );
455  puts( "Init: sigaddset - EINVAL (signal = 0)" );
456
457  status = sigaddset( &mask, 999 );
458  if ( status != -1 )
459    printf( "status = %d\n", status );
460  rtems_test_assert( errno == EINVAL );
461  puts( "Init: sigaddset - EINVAL (set invalid)" );
462
463  status = sigdelset( NULL, SIGUSR1 );
464  if ( status != -1 )
465    printf( "status = %d\n", status );
466  rtems_test_assert( errno == EINVAL );
467  puts( "Init: sigdelset - EINVAL (set invalid)" );
468
469  status = sigdelset( &mask, 0 );
470  rtems_test_assert( !status );
471  puts( "Init: sigdelset - SUCCESSFUL (signal = 0)" );
472
473  status = sigdelset( &mask, 999 );
474  if ( status != -1 )
475    printf( "status = %d\n", status );
476  rtems_test_assert( errno == EINVAL );
477  puts( "Init: sigdelset - EINVAL (set invalid)" );
478
479  status = sigismember( NULL, SIGUSR1 );
480  if ( status != -1 )
481    printf( "status = %d\n", status );
482  rtems_test_assert( errno == EINVAL );
483  puts( "Init: sigismember - EINVAL (set invalid)" );
484
485  status = sigismember( &mask, 0 );
486  rtems_test_assert( !status );
487  puts( "Init: sigismember - SUCCESSFUL (signal = 0)" );
488
489  status = sigismember( &mask, 999 );
490  if ( status != -1 )
491    printf( "status = %d\n", status );
492  rtems_test_assert( errno == EINVAL );
493  puts( "Init: sigismember - EINVAL (signal invalid)" );
494
495  status = sigaction( 0, &act, 0 );
496  if ( status != -1 )
497    printf( "status = %d\n", status );
498  rtems_test_assert( errno == EINVAL );
499  puts( "Init: sigaction - EINVAL (signal = 0)" );
500
501  status = sigaction( 999, &act, NULL );
502  if ( status != -1 )
503    printf( "status = %d\n", status );
504  rtems_test_assert( errno == EINVAL );
505  puts( "Init: sigaction - EINVAL (signal invalid)" );
506
507  status = sigaction( SIGKILL, &act, NULL );
508  if ( status != -1 )
509    printf( "status = %d\n", status );
510  rtems_test_assert( errno == EINVAL );
511  puts( "Init: sigaction - EINVAL (SIGKILL)" );
512
513  status = pthread_sigmask( SIG_BLOCK, NULL, NULL );
514  if ( status != -1 )
515    printf( "status = %d\n", status );
516  rtems_test_assert( errno == EINVAL );
517  puts( "Init: pthread_sigmask - EINVAL (set and oset invalid)" );
518
519  status = pthread_sigmask( 999, &pending_set, NULL );
520  if ( status != -1 )
521    printf( "status = %d\n", status );
522  rtems_test_assert( errno == EINVAL );
523  puts( "Init: pthread_sigmask - EINVAL (how invalid)" );
524
525  status = sigpending( NULL );
526  if ( status != -1 )
527    printf( "status = %d\n", status );
528  rtems_test_assert( errno == EINVAL );
529  puts( "Init: sigpending - EINVAL (set invalid)" );
530
531  timeout.tv_nsec = -1;
532  status = sigtimedwait( &mask, &info, &timeout );
533  if ( status != -1 )
534    printf( "status = %d\n", status );
535  rtems_test_assert( errno == EINVAL );
536  puts( "Init: pthread_sigmask - EINVAL (timout->nsec invalid < 0)" );
537
538  timeout.tv_nsec = 0x7fffffff;
539  status = sigtimedwait( &mask, &info, &timeout );
540  if ( status != -1 )
541    printf( "status = %d\n", status );
542  rtems_test_assert( errno == EINVAL );
543  puts( "Init: pthread_sigmask - EINVAL (timout->nsec invalid to large)" );
544
545  status = pthread_kill( Init_id, 999 );
546  if ( status != -1 )
547    printf( "status = %d\n", status );
548  rtems_test_assert( errno == EINVAL );
549  puts( "Init: pthread_kill - EINVAL (sig invalid)" );
550
551  status = pthread_kill( Init_id, 0 );
552  if ( status != -1 )
553    printf( "status = %d\n", status );
554  rtems_test_assert( errno == EINVAL );
555  puts( "Init: pthread_kill - EINVAL (signal = 0)" );
556
557  act.sa_handler = SIG_IGN;
558  act.sa_flags = 0;
559  sigaction( SIGUSR2, &act, NULL );
560  status = pthread_kill( Init_id, SIGUSR2 );
561  rtems_test_assert( !status );
562  puts( "Init: pthread_kill - SUCCESSFUL (signal = SIG_IGN)" );
563
564  status = kill( INT_MAX, SIGUSR1 );
565  if ( status != -1 )
566    printf( "status = %d\n", status );
567  rtems_test_assert( errno == ESRCH );
568  puts( "Init: kill - ESRCH (pid invalid)" );
569
570  status = kill( getpid(), 0 );
571  if ( status != -1 )
572    printf( "status = %d\n", status );
573  rtems_test_assert( errno == EINVAL );
574  puts( "Init: kill - EINVAL (signal = 0)" );
575
576  status = kill( getpid(), 999 );
577  if ( status != -1 )
578    printf( "status = %d\n", status );
579  rtems_test_assert( errno == EINVAL );
580  puts( "Init: kill - EINVAL (sig invalid)" );
581
582  /* exit this thread */
583
584  TEST_END();
585  rtems_test_exit( 0 );
586
587  return NULL; /* just so the compiler thinks we returned something */
588}
Note: See TracBrowser for help on using the repository browser.