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 timer_settime() resets the time until the next timer
9 * expiration to value.it_value if the timer was already armed.
10 *
11 * - set up a timer with value.it_value = V and value.it_interval > 0
12 * - ensure that timer expires correctly the first time
13 * - change time value to value.it_value = V'
14 * - ensure that this new timer expires correctly
15 * - repeat above 2 steps NUMREPS times
16 *
17 * For this test, signal SIGTOTEST will be used, clock CLOCK_REALTIME
18 * will be used.
19 */
20
21#include <time.h>
22#include <signal.h>
23#include <stdio.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include "posixtest.h"
27
28#define SIGTOTEST SIGALRM
29#define TIMERVALUESEC 2
30#define TIMERINTERVALSEC 5
31#define INCREMENT 2
32#define ACCEPTABLEDELTA 1
33
34#define NUMREPS 5
35
36int main(int argc, char *argv[])
37{
38	struct sigevent ev;
39	timer_t tid;
40	struct itimerspec its;
41	struct timespec tsbefore, tsafter;
42	sigset_t set;
43	int sig, i, failure = 0, timeelapsed, delta;
44
45	/*
46	 * set up signal set containing SIGTOTEST that will be used
47	 * in call to sigwait immediately after timer is set
48	 */
49
50	if (sigemptyset(&set) == -1 ) {
51		perror("sigemptyset() failed\n");
52		return PTS_UNRESOLVED;
53	}
54
55	if (sigaddset(&set, SIGTOTEST) == -1) {
56		perror("sigaddset() failed\n");
57		return PTS_UNRESOLVED;
58	}
59
60	if (sigprocmask (SIG_BLOCK, &set, NULL) == -1) {
61		perror("sigprocmask() failed\n");
62		return PTS_UNRESOLVED;
63	}
64
65	/*
66	 * set up timer to perform action SIGTOTEST on expiration
67	 */
68	ev.sigev_notify = SIGEV_SIGNAL;
69	ev.sigev_signo = SIGTOTEST;
70
71	its.it_interval.tv_sec = TIMERINTERVALSEC;
72	its.it_interval.tv_nsec = 0;
73	its.it_value.tv_sec = TIMERVALUESEC;
74	its.it_value.tv_nsec = 0;
75
76	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
77		perror("timer_create() did not return success\n");
78		return PTS_UNRESOLVED;
79	}
80
81	/*
82	 * - for i = 1 to NUMREPS
83	 *    - get the time
84	 *    - call timer_settime()
85	 *    - wait for the signal
86	 *    - get the time again => value.it_value
87	 *    - set new value.it_value and value.it_interval to old + INVERVAL
88	 * - delete the timer
89	 */
90
91	for (i = 0; i < NUMREPS; i++) {
92		printf("Test for value %d\n", (int) its.it_value.tv_sec);
93		if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
94			perror("clock_gettime() did not return success\n");
95			return PTS_UNRESOLVED;
96		}
97
98		if (timer_settime(tid, 0, &its, NULL) != 0) {
99			perror("timer_settime() did not return success\n");
100			return PTS_UNRESOLVED;
101		}
102
103		if (sigwait(&set, &sig) == -1) {
104			perror("sigwait() failed\n");
105			return PTS_UNRESOLVED;
106		}
107
108		if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) {
109			perror("clock_gettime() did not return success\n");
110			return PTS_UNRESOLVED;
111		}
112
113		timeelapsed = tsafter.tv_sec-tsbefore.tv_sec;
114
115		if (timeelapsed < 0) {
116			perror("clock_gettime inconsistent\n");
117			return PTS_UNRESOLVED;
118		}
119
120		delta = timeelapsed-its.it_value.tv_sec;
121		if ( (delta > ACCEPTABLEDELTA) || (delta < 0) ) {
122			printf("FAIL:  timer_settime() invalid on %d\n",
123					(int) its.it_value.tv_sec);
124			failure = 1;
125		}
126
127		its.it_interval.tv_sec += INCREMENT;
128		its.it_value.tv_sec += INCREMENT;
129	}
130
131	if (timer_delete(tid) != 0) {
132		perror("timer_delete() did not return success\n");
133		return PTS_UNRESOLVED;
134	}
135
136	if (failure) {
137		printf("timer_settime() failed on at least one value\n");
138		return PTS_FAIL;
139	} else {
140		printf("Test PASSED\n");
141		return PTS_PASS;
142	}
143}
144