Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:36

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2009.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #define CONFIGURE_INIT
0034 #include "system.h"
0035 #include <signal.h>
0036 #include <errno.h>
0037 
0038 const char rtems_test_name[] = "PSX 3";
0039 
0040 volatile int Signal_occurred;
0041 volatile int Signal_count;
0042 void Signal_handler( int signo );
0043 
0044 void Signal_handler(
0045   int signo
0046 )
0047 {
0048   Signal_count++;
0049   printf(
0050     "Signal: %d caught by 0x%" PRIxpthread_t " (%d)\n",
0051     signo,
0052     pthread_self(),
0053     Signal_count
0054   );
0055   Signal_occurred = 1;
0056 }
0057 
0058 void *POSIX_Init(
0059   void *argument
0060 )
0061 {
0062   int               status;
0063   struct timespec   timeout;
0064   struct sigaction  act;
0065   sigset_t          mask;
0066   sigset_t          waitset;
0067   int               signo;
0068   siginfo_t         siginfo;
0069 
0070   TEST_BEGIN();
0071 
0072   /* set the time of day, and print our buffer in multiple ways */
0073 
0074   set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
0075 
0076   /* get id of this thread */
0077 
0078   Init_id = pthread_self();
0079   printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );
0080 
0081   /* install a signal handler */
0082 
0083   status = sigemptyset( &act.sa_mask );
0084   rtems_test_assert( !status );
0085 
0086   act.sa_handler = Signal_handler;
0087   act.sa_flags   = 0;
0088 
0089   sigaction( SIGUSR1, &act, NULL );
0090 
0091   /* initialize signal handler variables */
0092 
0093   Signal_count = 0;
0094   Signal_occurred = 0;
0095 
0096   /*
0097    *  wait on SIGUSR1 for 3 seconds, will timeout
0098    */
0099 
0100   /* initialize the signal set we will wait for to SIGUSR1 */
0101 
0102   status = sigemptyset( &waitset );
0103   rtems_test_assert( !status );
0104 
0105   status = sigaddset( &waitset, SIGUSR1 );
0106   rtems_test_assert( !status );
0107 
0108   timeout.tv_sec = 3;
0109   timeout.tv_nsec = 0;
0110 
0111   puts( "Init: waiting on any signal for 3 seconds." );
0112   signo = sigtimedwait( &waitset, &siginfo, &timeout );
0113   rtems_test_assert( signo == -1 );
0114 
0115   if ( errno == EAGAIN )
0116     puts( "Init: correctly timed out waiting for SIGUSR1." );
0117   else
0118     printf( "sigtimedwait returned wrong errno - %d\n", errno );
0119 
0120   Signal_occurred = 0;
0121 
0122   /*
0123    *  wait on SIGUSR1 for 3 seconds, will timeout because Task_1 sends SIGUSR2
0124    */
0125 
0126   empty_line();
0127 
0128   /* initialize a mask to block SIGUSR2 */
0129 
0130   status = sigemptyset( &mask );
0131   rtems_test_assert( !status );
0132 
0133   status = sigaddset( &mask, SIGUSR2 );
0134   rtems_test_assert( !status );
0135 
0136   printf( "Init: Block SIGUSR2\n" );
0137   status = sigprocmask( SIG_BLOCK, &mask, NULL );
0138   rtems_test_assert( !status );
0139 
0140   /* create a thread */
0141 
0142   status = pthread_create( &Task_id, NULL, Task_1, NULL );
0143   rtems_test_assert( !status );
0144 
0145   /* signal handler is still installed, waitset is still set for SIGUSR1 */
0146 
0147   timeout.tv_sec = 3;
0148   timeout.tv_nsec = 0;
0149 
0150   puts( "Init: waiting on any signal for 3 seconds." );
0151   signo = sigtimedwait( &waitset, &siginfo, &timeout );
0152 
0153      /* switch to Task 1 */
0154 
0155   if ( errno == EAGAIN )
0156     puts( "Init: correctly timed out waiting for SIGUSR1." );
0157   else
0158     printf( "sigtimedwait returned wrong errno - %d\n", errno );
0159   rtems_test_assert( signo == -1 );
0160 
0161   /*
0162    *  wait on SIGUSR1 for 3 seconds, Task_2 will send it to us
0163    */
0164 
0165   empty_line();
0166 
0167   /* create a thread */
0168 
0169   status = pthread_create( &Task_id, NULL, Task_2, NULL );
0170   rtems_test_assert( !status );
0171 
0172   /* signal handler is still installed, waitset is still set for SIGUSR1 */
0173 
0174   /* wait on SIGUSR1 for 3 seconds, will receive SIGUSR1 from Task_2 */
0175 
0176   timeout.tv_sec = 3;
0177   timeout.tv_nsec = 0;
0178 
0179   /* just so we can check that these were altered */
0180 
0181   siginfo.si_code = -1;
0182   siginfo.si_signo = -1;
0183   siginfo.si_value.sival_int = -1;
0184 
0185   puts( "Init: waiting on any signal for 3 seconds." );
0186   signo = sigtimedwait( &waitset, &siginfo, &timeout );
0187   printf( "Init: received (%d) SIGUSR1=%d\n", siginfo.si_signo, SIGUSR1 );
0188   rtems_test_assert( signo == SIGUSR1 );
0189   rtems_test_assert( siginfo.si_signo == SIGUSR1 );
0190   rtems_test_assert( siginfo.si_code == SI_USER );
0191   rtems_test_assert( siginfo.si_value.sival_int != -1 );   /* rtems does always set this */
0192 
0193   /* try out a process signal */
0194 
0195   empty_line();
0196   puts( "Init: kill with SIGUSR2." );
0197   status = kill( getpid(), SIGUSR2 );
0198   rtems_test_assert( !status );
0199 
0200   siginfo.si_code = -1;
0201   siginfo.si_signo = -1;
0202   siginfo.si_value.sival_int = -1;
0203 
0204   status = sigemptyset( &waitset );
0205   rtems_test_assert( !status );
0206 
0207   status = sigaddset( &waitset, SIGUSR1 );
0208   rtems_test_assert( !status );
0209 
0210   status = sigaddset( &waitset, SIGUSR2 );
0211   rtems_test_assert( !status );
0212 
0213   puts( "Init: waiting on any signal for 3 seconds." );
0214   signo = sigtimedwait( &waitset, &siginfo, &timeout );
0215   printf( "Init: received (%d) SIGUSR2=%d\n", siginfo.si_signo, SIGUSR2 );
0216   rtems_test_assert( signo == SIGUSR2 );
0217   rtems_test_assert( siginfo.si_signo == SIGUSR2 );
0218   rtems_test_assert( siginfo.si_code == SI_USER );
0219   rtems_test_assert( siginfo.si_value.sival_int != -1 );   /* rtems does always set this */
0220 
0221   /* exit this thread */
0222 
0223   TEST_END();
0224   rtems_test_exit( 0 );
0225 
0226   return NULL; /* just so the compiler thinks we returned something */
0227 }