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  * @file POSIX Timer Test #2
0005  *
0006  *  This is a simple real-time applications which contains 3 periodic tasks.
0007  *
0008  *  Task A is an independent task.
0009  *
0010  *  Task B and C share a data.
0011  *
0012  *  Tasks are implemented as POSIX threads.
0013  *
0014  *  The share data is protected with a POSIX mutex.
0015  *
0016  *  Other POSIX facilities such as timers, condition, .. are also used.
0017  */
0018 
0019 /*
0020  *  COPYRIGHT (c) 1989-2009.
0021  *  On-Line Applications Research Corporation (OAR).
0022  *
0023  * Redistribution and use in source and binary forms, with or without
0024  * modification, are permitted provided that the following conditions
0025  * are met:
0026  * 1. Redistributions of source code must retain the above copyright
0027  *    notice, this list of conditions and the following disclaimer.
0028  * 2. Redistributions in binary form must reproduce the above copyright
0029  *    notice, this list of conditions and the following disclaimer in the
0030  *    documentation and/or other materials provided with the distribution.
0031  *
0032  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0033  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0034  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0035  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0036  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0037  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0038  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0039  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0040  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0041  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0042  * POSSIBILITY OF SUCH DAMAGE.
0043  */
0044 
0045 #ifdef HAVE_CONFIG_H
0046 #include "config.h"
0047 #endif
0048 
0049 #define CONFIGURE_INIT
0050 #include "system.h"
0051 #include "tmacros.h"
0052 #include <signal.h>   /* signal facilities */
0053 #include <unistd.h>   /* sleep facilities */
0054 #include <time.h>     /* time facilities */
0055 #include <stdio.h>    /* console facilities */
0056 
0057 const char rtems_test_name[] = "PSXTIMER 2";
0058 
0059 void *POSIX_Init (
0060   void *argument
0061 )
0062 
0063 {
0064   struct timespec   now;
0065   struct sigevent   event;
0066   int               status;
0067   timer_t           timer;
0068   timer_t           timer1;
0069   struct itimerspec itimer;
0070 
0071   /*
0072    *  If these are not filled in correctly, we don't pass its error checking.
0073    */
0074   event.sigev_notify = SIGEV_SIGNAL;
0075   event.sigev_signo = SIGUSR1;
0076 
0077   TEST_BEGIN();
0078 
0079   puts( "timer_create - bad clock id - EINVAL" );
0080   status = timer_create( (clockid_t) -1, &event, &timer );
0081   fatal_posix_service_status_errno( status, EINVAL, "bad clock id" );
0082 
0083   puts( "timer_create - bad timer id pointer - EINVAL" );
0084   status = timer_create( CLOCK_REALTIME, &event, NULL );
0085   fatal_posix_service_status_errno( status, EINVAL, "bad timer id" );
0086 
0087   puts( "timer_create - OK" );
0088   status = timer_create( CLOCK_REALTIME, NULL, &timer );
0089   posix_service_failed( status, "timer_create OK" );
0090 
0091   puts( "timer_create - too many - EAGAIN" );
0092   status = timer_create( CLOCK_REALTIME, NULL, &timer1 );
0093   fatal_posix_service_status_errno( status, EAGAIN, "too many" );
0094 
0095   puts( "timer_delete - bad id - EINVAL" );
0096   status = timer_delete( timer1 + 1 );
0097   fatal_posix_service_status_errno( status, EINVAL, "bad id" );
0098 
0099   puts( "timer_getoverrun - bad id - EINVAL" );
0100   status = timer_getoverrun( timer1 + 1 );
0101   fatal_posix_service_status_errno( status, EINVAL, "bad id" );
0102 
0103   puts( "timer_gettime - bad itimer - EINVAL" );
0104   status = timer_gettime( timer1, NULL );
0105   fatal_posix_service_status_errno( status, EINVAL, "bad id" );
0106 
0107   puts( "timer_gettime - bad id - EINVAL" );
0108   status = timer_gettime( timer1 + 1, &itimer );
0109   fatal_posix_service_status_errno( status, EINVAL, "bad id" );
0110 
0111   puts( "timer_settime - bad itimer pointer - EINVAL" );
0112   status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
0113   fatal_posix_service_status_errno( status, EINVAL, "bad itimer pointer" );
0114 
0115   itimer.it_value.tv_nsec = 2000000000;
0116   puts( "timer_settime - bad itimer value - too many nanosecond - EINVAL" );
0117   status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
0118   fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #1" );
0119 
0120   itimer.it_value.tv_nsec = -1;
0121   puts( "timer_settime - bad itimer value - negative nanosecond - EINVAL" );
0122   status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
0123   fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #2" );
0124 
0125   clock_gettime( CLOCK_REALTIME, &now );
0126   itimer.it_value = now;
0127   itimer.it_value.tv_sec = itimer.it_value.tv_sec - 1;
0128   puts( "timer_settime - bad itimer value - previous time - EINVAL" );
0129   status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
0130   fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #3" );
0131 
0132   clock_gettime( CLOCK_REALTIME, &now );
0133   itimer.it_value = now;
0134   itimer.it_value.tv_sec = itimer.it_value.tv_sec + 1;
0135   puts( "timer_settime - bad id - EINVAL" );
0136   status = timer_settime( timer1, TIMER_ABSTIME, &itimer, NULL );
0137   fatal_posix_service_status_errno( status, EINVAL, "bad id" );
0138 
0139   itimer.it_value.tv_nsec = 0;
0140   puts( "timer_settime - bad clock value - EINVAL" );
0141   status = timer_settime( timer, 0x80, &itimer, NULL );
0142   fatal_posix_service_status_errno( status, EINVAL, "bad clock value" );
0143 
0144   puts( "timer_delete - OK" );
0145   status = timer_delete( timer );
0146   posix_service_failed( status, "timer_delete OK" );
0147 
0148   puts( "timer_delete - bad id - EINVAL" );
0149   status = timer_delete( timer );
0150   fatal_posix_service_status_errno( status, EINVAL, "bad id" );
0151 
0152   puts( "timer_create (monotonic) - bad timer id pointer - EINVAL" );
0153   status = timer_create( CLOCK_MONOTONIC, &event, NULL );
0154   fatal_posix_service_status_errno( status, EINVAL, "bad timer id" );
0155 
0156   puts( "timer_create (monotonic) - OK" );
0157   status = timer_create( CLOCK_MONOTONIC, NULL, &timer );
0158   posix_service_failed( status, "timer_create OK" );
0159 
0160   puts( "timer_create (monotonic) - too many - EAGAIN" );
0161   status = timer_create( CLOCK_MONOTONIC, NULL, &timer1 );
0162   fatal_posix_service_status_errno( status, EAGAIN, "too many" );
0163 
0164   clock_gettime( CLOCK_MONOTONIC, &now );
0165   itimer.it_value = now;
0166   itimer.it_value.tv_sec = itimer.it_value.tv_sec - 1;
0167   puts( "timer_settime (monotonic) - bad itimer value - previous time - EINVAL" );
0168   status = timer_settime( timer, TIMER_ABSTIME, &itimer, NULL );
0169   fatal_posix_service_status_errno( status, EINVAL, "bad itimer value #3" );
0170 
0171   clock_gettime( CLOCK_MONOTONIC, &now );
0172   itimer.it_value = now;
0173   itimer.it_value.tv_sec = itimer.it_value.tv_sec + 1;
0174   puts( "timer_settime (monotonic) - bad id - EINVAL" );
0175   status = timer_settime( timer1, TIMER_ABSTIME, &itimer, NULL );
0176   fatal_posix_service_status_errno( status, EINVAL, "bad id" );
0177 
0178   TEST_END();
0179   rtems_test_exit (0);
0180 }