Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2012.
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 #include <reent.h>
0038 
0039 const char rtems_test_name[] = "PSXSIGNAL 1";
0040 
0041 /* forward declarations to avoid warnings */
0042 int test_main(void);
0043 void Handler_1(int signo);
0044 void Signal_handler(int signo);
0045 void Signal_info_handler(int signo, siginfo_t *info, void *context);
0046 rtems_timer_service_routine Signal_duringISR_TSR(
0047   rtems_id  ignored_id,
0048   void     *ignored_address
0049 );
0050 
0051 typedef void (*sighandler_t)(int);
0052 sighandler_t signal(int signum, sighandler_t handler);
0053 extern void _POSIX_signals_Abnormal_termination_handler( int signo );
0054 
0055 volatile int Signal_occurred;
0056 volatile int Signal_count;
0057 
0058 static void block_all_signals(void)
0059 {
0060   int               sc;
0061   sigset_t          mask;
0062 
0063   sc = sigfillset( &mask );
0064   rtems_test_assert( !sc );
0065 
0066   sc = pthread_sigmask( SIG_BLOCK, &mask, NULL );
0067   rtems_test_assert( !sc );
0068 }
0069 
0070 void Handler_1(
0071   int signo
0072 )
0073 {
0074   Signal_count++;
0075   printf(
0076     "Handler_1: Signal: %d caught by 0x%" PRIxpthread_t " (%d)\n",
0077     signo,
0078     pthread_self(),
0079     Signal_count
0080   );
0081   Signal_occurred = 1;
0082 }
0083 
0084 void Signal_handler(
0085   int signo
0086 )
0087 {
0088   Signal_count++;
0089   printf(
0090     "Signal: %d caught by 0x%"PRIxpthread_t " (%d)\n",
0091     signo,
0092     pthread_self(),
0093     Signal_count
0094   );
0095   Signal_occurred = 1;
0096 }
0097 
0098 void Signal_info_handler(
0099   int        signo,
0100   siginfo_t *info,
0101   void      *context
0102 )
0103 {
0104   Signal_count++;
0105   printf(
0106     "Signal_info: %d caught by 0x%" PRIxpthread_t " (%d) si_signo= %d si_code= %d value= %d\n",
0107     signo,
0108     pthread_self(),
0109     Signal_count,
0110     info->si_signo,
0111     info->si_code,
0112     info->si_value.sival_int
0113   );
0114   Signal_occurred = 1;
0115 }
0116 
0117 rtems_timer_service_routine Signal_duringISR_TSR(
0118   rtems_id  ignored_id,
0119   void     *ignored_address
0120 )
0121 {
0122   int  status;
0123 
0124   status = pthread_kill( pthread_self(), SIGUSR1 );
0125   rtems_test_assert( status == 0 );
0126 }
0127 
0128 
0129 void *POSIX_Init(
0130   void *argument
0131 )
0132 {
0133   int               status;
0134   struct sigaction  act;
0135   sigset_t          mask;
0136   sighandler_t      oldHandler;
0137   sighandler_t      newHandler;
0138   rtems_interval start, end;
0139 
0140   TEST_BEGIN();
0141 
0142   block_all_signals();
0143 
0144   /* set the time of day, and print our buffer in multiple ways */
0145 
0146   set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
0147 
0148   /* get id of this thread */
0149 
0150   Init_id = pthread_self();
0151   printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );
0152 
0153   Signal_occurred = 0;
0154   Signal_count = 0;
0155   act.sa_handler = Handler_1;
0156   act.sa_flags   = 0;
0157   sigaction( SIGUSR1, &act, NULL );
0158   sigaction( SIGFPE, &act, NULL );
0159   sigaction( SIGILL, &act, NULL );
0160   sigaction( SIGSEGV, &act, NULL );
0161 
0162 
0163   /*
0164    * If we have the signal pending with default, we will die.
0165    */
0166   puts("Validate signal with SIG_DFL");
0167   signal( SIGUSR1, SIG_DFL );
0168   status = kill( getpid(), SIGUSR1 );
0169   status = sleep( 1 );
0170 
0171   puts("Validate signal with SIG_IGN");
0172   signal( SIGUSR1, SIG_IGN );
0173   status = kill( getpid(), SIGUSR1 );
0174   status = sleep( 1 );
0175 
0176 /* unblock Signal and see if it happened */
0177   status = sigemptyset( &mask );
0178   rtems_test_assert( !status );
0179 
0180   status = sigaddset( &mask, SIGUSR1 );
0181   rtems_test_assert( !status );
0182 
0183   status = sigaddset( &mask, SIGFPE );
0184   rtems_test_assert( !status );
0185 
0186   status = sigaddset( &mask, SIGILL );
0187   rtems_test_assert( !status );
0188 
0189   status = sigaddset( &mask, SIGSEGV );
0190   rtems_test_assert( !status );
0191 
0192   puts( "Init: Unblock SIGUSR1 SIGFPE SIGILL SIGSEGV" );
0193   status = sigprocmask( SIG_UNBLOCK, &mask, NULL );
0194   rtems_test_assert( !status );
0195 
0196 /* install a signal handler for SIGUSR1 */
0197   Signal_occurred = 0;
0198   Signal_count = 0;
0199   act.sa_handler = Handler_1;
0200   act.sa_flags   = 0;
0201   sigaction( SIGUSR1, &act, NULL );
0202 
0203   Signal_count = 0;
0204   Signal_occurred = 0;
0205 
0206   newHandler = Signal_handler;
0207   oldHandler = signal( SIGUSR1, newHandler );
0208   if (oldHandler == Handler_1 )
0209     puts("Init: signal return value verified");
0210   else
0211     puts("Init: ERROR==> signal unexpected return value" );
0212   status = sleep( 1 );
0213 
0214   puts( "Init: send SIGUSR1 to process" );
0215   status = kill( getpid(), SIGUSR1 );
0216   status = sleep( 5 );
0217 
0218   puts( "Init: send SIGFPE to process" );
0219   status = _kill_r( NULL, getpid(), SIGFPE );
0220   status = sleep(5);
0221 
0222   puts( "Init: send SIGILL to process" );
0223   status = _kill_r( NULL, getpid(), SIGILL );
0224   status = sleep(5);
0225 
0226   puts( "Init: send SIGSEGV to process" );
0227   status = _kill_r( NULL, getpid(), SIGSEGV );
0228   status = sleep(5);
0229 
0230   Timer_name[0]= rtems_build_name( 'T', 'M', '1', ' ' );
0231   status = rtems_timer_create( Timer_name[0], &Timer_id[0]);
0232 
0233   Signal_count = 0;
0234   Signal_occurred = 0;
0235   puts( "Init: send SIGUSR1 to process from a TSR (interruptible sleep)" );
0236   status = rtems_timer_fire_after(
0237     Timer_id[ 0 ],
0238     1,
0239     Signal_duringISR_TSR,
0240     NULL
0241   );
0242   sleep(5);
0243   /* signal occurs during interruptible sleep */
0244 
0245   /* now schedule another one to fire but do not sleep */
0246 
0247   puts( "Init: send SIGUSR1 to process from a TSR (spin)" );
0248   start = rtems_clock_get_ticks_since_boot();
0249   Signal_count = 0;
0250   Signal_occurred = 0;
0251   status = rtems_timer_fire_after(
0252     Timer_id[ 0 ],
0253     10,
0254     Signal_duringISR_TSR,
0255     NULL
0256   );
0257   do {
0258     end = rtems_clock_get_ticks_since_boot();
0259   } while ( !Signal_occurred && ((end - start) <= 800));
0260 
0261   if ( !Signal_occurred ) {
0262     puts( "Signal did not occur" );
0263     rtems_test_exit(0);
0264   }
0265 
0266 /* end of install a signal handler for SIGUSR1 */
0267 
0268   Signal_occurred = 0;
0269 
0270   puts("*** Validate unexpected program termination ***");
0271   TEST_END();
0272   _POSIX_signals_Abnormal_termination_handler( SIGUSR1 );
0273   status = sleep( 1 );
0274 
0275   puts( "ERROR==> Expected program termination");
0276   rtems_test_exit(0);
0277   return NULL; /* just so the compiler thinks we returned something */
0278 }