1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: julie.n.fleischer REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 * 8 * Test that if value.it_interval != 0, the timer is periodic and loaded 9 * according to value.it_interval. 10 * 11 * - Set up a timer with an it_value < it_interval. 12 * - Check that after it_value seconds, the timer expires. 13 * - Check that after it_interval more seconds, the timer expires again (2X) 14 * - Delete the timer 15 * 16 * For this test, signal SIGTOTEST will be used, clock CLOCK_REALTIME 17 * will be used. 18 */ 19 20 #include <time.h> 21 #include <signal.h> 22 #include <stdio.h> 23 #include <unistd.h> 24 #include <stdlib.h> 25 #include "posixtest.h" 26 27 #define SIGTOTEST SIGALRM 28 #define TIMERVALUESEC 2 29 #define TIMERINTERVALSEC 5 30 #define ACCEPTABLEDELTA 1 31 32 int main(void) 33 { 34 struct sigevent ev; 35 timer_t tid; 36 struct itimerspec its; 37 struct timespec tsbefore, tsafter; 38 sigset_t set; 39 int sig, i, delta, timeelapsed; 40 41 /* 42 * set up signal set containing SIGTOTEST that will be used 43 * in call to sigwait immediately after timer is set 44 */ 45 46 if (sigemptyset(&set) == -1) { 47 perror("sigemptyset() failed\n"); 48 return PTS_UNRESOLVED; 49 } 50 51 if (sigaddset(&set, SIGTOTEST) == -1) { 52 perror("sigaddset() failed\n"); 53 return PTS_UNRESOLVED; 54 } 55 56 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { 57 perror("sigprocmask() failed\n"); 58 return PTS_UNRESOLVED; 59 } 60 61 /* 62 * set up timer to perform action SIGTOTEST on expiration 63 */ 64 ev.sigev_notify = SIGEV_SIGNAL; 65 ev.sigev_signo = SIGTOTEST; 66 67 its.it_interval.tv_sec = TIMERINTERVALSEC; 68 its.it_interval.tv_nsec = 0; 69 its.it_value.tv_sec = TIMERVALUESEC; 70 its.it_value.tv_nsec = 0; 71 72 if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { 73 perror("timer_create() did not return success\n"); 74 return PTS_UNRESOLVED; 75 } 76 77 /* 78 * - get the time 79 * - call timer_settime() 80 * - wait for the signal 81 * - get the time again => value.it_value 82 * - wait for signal again 83 * - get the time again => value.it_interval 84 * - repeat previous 2 85 * - delete the timer 86 */ 87 88 if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) { 89 perror("clock_gettime() did not return success\n"); 90 return PTS_UNRESOLVED; 91 } 92 93 if (timer_settime(tid, 0, &its, NULL) != 0) { 94 perror("timer_settime() did not return success\n"); 95 return PTS_UNRESOLVED; 96 } 97 98 if (sigwait(&set, &sig) == -1) { 99 perror("sigwait() failed\n"); 100 return PTS_UNRESOLVED; 101 } 102 103 if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) { 104 perror("clock_gettime() did not return success\n"); 105 return PTS_UNRESOLVED; 106 } 107 108 timeelapsed = tsafter.tv_sec - tsbefore.tv_sec; 109 if (timeelapsed < 0) { 110 perror("clock_gettime inconsistent\n"); 111 return PTS_UNRESOLVED; 112 } 113 114 delta = timeelapsed - TIMERVALUESEC; 115 116 if ((delta > ACCEPTABLEDELTA) || (delta < 0)) { 117 perror("timer_settime() did not expire after value.it_value\n"); 118 return PTS_UNRESOLVED; 119 } 120 121 for (i = 0; i < 2; i++) { 122 tsbefore.tv_sec = tsafter.tv_sec; 123 tsbefore.tv_nsec = tsafter.tv_nsec; 124 if (sigwait(&set, &sig) == -1) { 125 perror("sigwait() failed\n"); 126 return PTS_UNRESOLVED; 127 } 128 129 if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) { 130 perror("clock_gettime() did not return success\n"); 131 return PTS_UNRESOLVED; 132 } 133 timeelapsed = tsafter.tv_sec - tsbefore.tv_sec; 134 if (timeelapsed < 0) { 135 perror("clock_gettime inconsistent\n"); 136 return PTS_UNRESOLVED; 137 } 138 139 delta = timeelapsed - TIMERINTERVALSEC; 140 141 if ((delta > ACCEPTABLEDELTA) || (delta < 0)) { 142 printf("timer did not wait for correct interval\n"); 143 return PTS_FAIL; 144 } 145 } 146 147 if (timer_delete(tid) != 0) { 148 perror("timer_delete() did not return success\n"); 149 return PTS_UNRESOLVED; 150 } 151 152 printf("Test PASSED\n"); 153 return PTS_PASS; 154 } 155