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 clock_settime() changes the value for CLOCK_REALTIME,
9 * then any absolute timers will use the new time for expiration.
10 *
11 * Steps:
12 * - get time T0
13 * - create/enable a timer to expire at T1 = T0 + TIMEROFFSET
14 * - sleep SLEEPTIME seconds (SLEEPTIME should be < TIMEROFFSET,
15 *				but > ACCEPTABLEDELTA)
16 * - set time back to T0
17 * - wait for the timer to expire
18 * - get time T2
19 * - ensure that:  T2 >= T1 and (T2-T1) <= ACCEPTABLEDELTA
20 *
21 * signal SIGTOTEST is used.
22 *
23 * adam.li: I think should check that (abs(T2-T1) <= ACCEPTABLEDELTA)
24 * 2004-04-30
25 */
26#include <stdio.h>
27#include <time.h>
28#include <signal.h>
29#include <unistd.h>
30#include "posixtest.h"
31#include "helpers.h"
32
33// SLEEPTIME < TIMEROFFSET
34// SLEEPTIME > ACCEPTABLEDELTA
35#define SLEEPTIME 3
36#define TIMEROFFSET 9
37#define ACCEPTABLEDELTA 1
38
39#define SIGTOTEST SIGALRM
40
41int main(void)
42{
43	struct sigevent ev;
44	struct timespec tpT0, tpT2, tpreset;
45	struct itimerspec its;
46	timer_t tid;
47	int delta;
48	int sig;
49	sigset_t set;
50	int flags = 0;
51
52	/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
53	if (geteuid() != 0) {
54		printf("Run this test as ROOT, not as a Regular User\n");
55		return PTS_UNTESTED;
56	}
57
58	/*
59	 * set up sigevent for timer
60	 * set up signal set for sigwait
61	 */
62	ev.sigev_notify = SIGEV_SIGNAL;
63	ev.sigev_signo = SIGTOTEST;
64
65	if (sigemptyset(&set) != 0) {
66		perror("sigemptyset() was not successful\n");
67		return PTS_UNRESOLVED;
68	}
69
70	if (sigaddset(&set, SIGTOTEST) != 0) {
71		perror("sigaddset() was not successful\n");
72		return PTS_UNRESOLVED;
73	}
74
75	if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
76		perror("sigprocmask() failed\n");
77		return PTS_UNRESOLVED;
78	}
79
80	if (clock_gettime(CLOCK_REALTIME, &tpT0) != 0) {
81		perror("clock_gettime() was not successful\n");
82		return PTS_UNRESOLVED;
83	}
84
85	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
86		perror("timer_create() did not return success\n");
87		return PTS_UNRESOLVED;
88	}
89
90	flags |= TIMER_ABSTIME;
91	its.it_interval.tv_sec = 0;
92	its.it_interval.tv_nsec = 0;
93	its.it_value.tv_sec = tpT0.tv_sec + TIMEROFFSET;
94	its.it_value.tv_nsec = tpT0.tv_nsec;
95	if (timer_settime(tid, flags, &its, NULL) != 0) {
96		perror("timer_settime() did not return success\n");
97		return PTS_UNRESOLVED;
98	}
99
100	sleep(SLEEPTIME);
101	getBeforeTime(&tpreset);
102	if (clock_settime(CLOCK_REALTIME, &tpT0) != 0) {
103		perror("clock_settime() was not successful");
104		return PTS_UNRESOLVED;
105	}
106
107	if (sigwait(&set, &sig) == -1) {
108		perror("sigwait() was not successful\n");
109		return PTS_UNRESOLVED;
110	}
111
112	if (clock_gettime(CLOCK_REALTIME, &tpT2) != 0) {
113		printf("clock_gettime() was not successful\n");
114		return PTS_UNRESOLVED;
115	}
116
117	delta = tpT2.tv_sec - its.it_value.tv_sec;
118
119	// add back time waited to reset value and reset time
120	tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec;
121	setBackTime(tpreset);
122
123	printf("delta: %d\n", delta);
124	if ((delta <= ACCEPTABLEDELTA) && (delta >= 0)) {
125		printf("Test PASSED\n");
126		return PTS_PASS;
127	}
128
129	printf("FAIL:  Ended %d, not %d\n",
130	       (int)tpT2.tv_sec, (int)its.it_value.tv_sec);
131	return PTS_FAIL;
132}
133