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() will return ovalue.it_value = the previous
9 * amount of time before the timer would have expired.
10 * - set up a timer to expire in TIMERSEC seconds
11 * - sleep for TIMERSEC-TIMELEFT seconds
12 * - set up a new timer
13 * - compare TIMELEFT and the value in ovalue.it_value and ensure they
14 *   are within ACCEPTABLEDELTA of each other
15 *
16 * For this test, signal SIGCONT will be used so that the test will
17 * not abort.  Clock CLOCK_REALTIME 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 TIMERSEC 7
28#define TIMELEFT 5
29#define ACCEPTABLEDELTA 1
30
31int main(int argc, char *argv[])
32{
33	struct sigevent ev;
34	timer_t tid;
35	struct itimerspec its, oits;
36
37	ev.sigev_notify = SIGEV_SIGNAL;
38	ev.sigev_signo = SIGCONT;
39
40	its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0;
41	its.it_value.tv_sec = TIMERSEC;
42	its.it_value.tv_nsec = 0;
43
44	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
45		perror("timer_create() did not return success\n");
46		return PTS_UNRESOLVED;
47	}
48
49	if (timer_settime(tid, 0, &its, &oits) != 0) {
50		perror("timer_settime() did not return success\n");
51		return PTS_UNRESOLVED;
52	}
53
54	sleep(TIMERSEC-TIMELEFT);
55
56	if (timer_settime(tid, 0, &its, &oits) != 0) {
57		perror("timer_settime() did not return success\n");
58		return PTS_UNRESOLVED;
59	}
60
61	if (abs(oits.it_value.tv_sec-TIMELEFT) <= ACCEPTABLEDELTA) {
62		printf("Test PASSED\n");
63		return PTS_PASS;
64	} else {
65		printf("Test FAILED:  time left %d oits.it_value.tv_sec %d\n",
66				TIMELEFT, (int) oits.it_value.tv_sec);
67		return PTS_FAIL;
68	}
69
70	printf("This code should not be executed.\n");
71	return PTS_UNRESOLVED;
72}
73