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 time for CLOCK_REALTIME,
9 * then any threads blocked on clock_nanosleep() for the CLOCK_REALTIME
10 * clock that would now have expired in the past will expire immediately.
11 *
12 * Steps:
13 * - get time T0
14 * - in child:  set clock_nanosleep() to sleep until time
15 *              T1 = T0 + SLEEPOFFSET
16 * - in parent:  set time forward to T2 = T1 + SMALLTIME
17 * - in child:  ensure clock_nanosleep() expires within ACCEPTABLEDELTA of
18 *              T2
19 */
20#include <stdio.h>
21#include <time.h>
22#include <signal.h>
23#include <unistd.h>
24#include <sys/wait.h>
25#include "posixtest.h"
26#include "helpers.h"
27
28#define SLEEPOFFSET 5
29#define SMALLTIME 2
30#define ACCEPTABLEDELTA 1
31
32#define CHILDPASS 1
33#define CHILDFAIL 0
34
35int main(int argc, char *argv[])
36{
37	struct timespec tsT0, tsT1, tsT2;
38	int pid;
39
40	/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
41	if(getuid() != 0)
42	{
43		printf("Run this test as ROOT, not as a Regular User\n");
44		return PTS_UNTESTED;
45	}
46
47	if (clock_gettime(CLOCK_REALTIME, &tsT0) != 0) {
48		perror("clock_gettime() did not return success\n");
49		return PTS_UNRESOLVED;
50	}
51
52	tsT1.tv_sec=tsT0.tv_sec + SLEEPOFFSET;
53	tsT1.tv_nsec=tsT0.tv_nsec;
54
55	tsT2.tv_sec = tsT1.tv_sec + SMALLTIME;
56	tsT2.tv_nsec = tsT1.tv_nsec;
57
58	if ((pid = fork()) == 0) {
59		/* child here */
60		int flags = 0;
61		struct timespec tsT3;
62
63		flags |= TIMER_ABSTIME;
64		if (clock_nanosleep(CLOCK_REALTIME, flags, &tsT1, NULL) != 0) {
65			printf("clock_nanosleep() did not return success\n");
66			return CHILDFAIL;
67		}
68
69		if (clock_gettime(CLOCK_REALTIME, &tsT3) != 0) {
70			perror("clock_gettime() did not return success\n");
71			return CHILDFAIL;
72		}
73
74		if (tsT3.tv_sec >= tsT2.tv_sec) {
75			if ( (tsT3.tv_sec-tsT2.tv_sec) <= ACCEPTABLEDELTA) {
76				return CHILDPASS;
77			} else {
78				printf("Ended too late.  %d >> %d\n",
79						(int) tsT3.tv_sec,
80						(int) tsT2.tv_sec);
81				return CHILDFAIL;
82			}
83		} else {
84			printf("Did not sleep for long enough %d < %d\n",
85					(int) tsT3.tv_sec, (int) tsT2.tv_sec);
86			return CHILDFAIL;
87		}
88
89		return CHILDFAIL;
90	} else {
91		/* parent here */
92		int i;
93		struct timespec tsreset;
94
95		sleep(1);
96
97		getBeforeTime(&tsreset);
98		if (clock_settime(CLOCK_REALTIME, &tsT2) != 0) {
99			printf("clock_settime() did not return success\n");
100			return PTS_UNRESOLVED;
101		}
102
103		if (wait(&i) == -1) {
104			perror("Error waiting for child to exit\n");
105			return PTS_UNRESOLVED;
106		}
107
108		setBackTime(tsreset); //should be ~= before time
109
110		if (WIFEXITED(i) && WEXITSTATUS(i)) {
111			printf("Test PASSED\n");
112			return PTS_PASS;
113		} else {
114			printf("Test FAILED\n");
115			return PTS_FAIL;
116		}
117	}
118
119	return PTS_UNRESOLVED;
120}
121