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_gettime() sets sets itimerspec.it_value to the
9 * amount of time remaining in the middle of a timer.
10 * - Create and arm a timer for TIMERSEC seconds.
11 * - Sleep for SLEEPSEC < TIMERSEC.
12 * - Call timer_gettime().
13 * - Ensure the value returned is within ACCEPTABLEDELTA less than
14 *   TIMERSEC-SLEEPSEC.
15 *
16 * Signal SIGCONT will be used so that it will not affect the test if
17 * the timer expires.
18 * Clock CLOCK_REALTIME 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 TIMERSEC 8
29#define SLEEPSEC 2
30#define ACCEPTABLEDELTA 1
31
32int main(void)
33{
34	struct sigevent ev;
35	timer_t tid;
36	struct itimerspec itsset, itsget;
37	int delta;
38
39	ev.sigev_notify = SIGEV_SIGNAL;
40	ev.sigev_signo = SIGCONT;
41
42	itsset.it_interval.tv_sec = 0;
43	itsset.it_interval.tv_nsec = 0;
44	itsset.it_value.tv_sec = TIMERSEC;
45	itsset.it_value.tv_nsec = 0;
46
47	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
48		perror("timer_create() did not return success\n");
49		return PTS_UNRESOLVED;
50	}
51
52	if (timer_settime(tid, 0, &itsset, NULL) != 0) {
53		perror("timer_settime() did not return success\n");
54		return PTS_UNRESOLVED;
55	}
56
57	if (sleep(SLEEPSEC) != 0) {
58		perror("sleep() did not return 0\n");
59		return PTS_UNRESOLVED;
60	}
61
62	if (timer_gettime(tid, &itsget) != 0) {
63		perror("timer_gettime() did not return success\n");
64		return PTS_UNRESOLVED;
65	}
66
67	delta = (itsset.it_value.tv_sec - SLEEPSEC) - itsget.it_value.tv_sec;
68
69	if (delta < 0) {
70		printf("FAIL:  timer_gettime() value > time expected left\n");
71		printf("%d > %d\n", (int)itsget.it_value.tv_sec,
72		       (int)itsset.it_value.tv_sec - SLEEPSEC);
73		return PTS_FAIL;
74	}
75
76	if (delta <= ACCEPTABLEDELTA) {
77		printf("Test PASSED\n");
78		return PTS_PASS;
79	}
80
81	printf("FAIL:  timer_gettime() value !~= time expected left\n");
82	printf("%d !~= %d\n", (int)itsget.it_value.tv_sec,
83	       (int)itsset.it_value.tv_sec - SLEEPSEC);
84	return PTS_FAIL;
85}
86