source: rtems/c/src/tests/psxtests/psx04/init.c @ e4cc4176

4.104.114.84.95
Last change on this file since e4cc4176 was e4cc4176, checked in by Mark Johannes <Mark.Johannes@…>, on Aug 23, 1996 at 3:58:24 PM

Changed all EFAULTS to EINVALs

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