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 clock_settime() sets clock_id to tp.
9 *
10 * The clock_id chosen for this test is CLOCK_REALTIME.
11 * The date chosen is Nov 12, 2002 ~11:13am (date when test was first
12 * written).
13 */
14
15#include <stdio.h>
16#include <time.h>
17#include <unistd.h>
18#include <errno.h>
19#include "posixtest.h"
20#include "helpers.h"
21
22#ifndef PR_NSEC_PER_SEC
23#define PR_NSEC_PER_SEC 1000000000UL
24#endif
25
26#define TESTTIME 1037128358
27#define ACCEPTABLEDELTA 1
28
29int main(void)
30{
31	struct timespec tpset, tpget, tpreset, tpres;
32	int delta, nsdelta;
33
34	/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
35	if (getuid() != 0) {
36		printf("Run this test as ROOT, not as a Regular User\n");
37		return PTS_UNTESTED;
38	}
39	if (clock_getres(CLOCK_REALTIME, &tpres) != 0) {
40		printf("Time resolution is not provided\n");
41		tpres.tv_sec = 0;
42		tpres.tv_nsec = 10000000;
43	}
44
45	getBeforeTime(&tpreset);
46
47	tpset.tv_sec = TESTTIME;
48	tpset.tv_nsec = 0;
49	if (clock_settime(CLOCK_REALTIME, &tpset) == 0) {
50		if (clock_gettime(CLOCK_REALTIME, &tpget) == -1) {
51			printf("Error in clock_gettime()\n");
52			setBackTime(tpreset);
53			return PTS_UNRESOLVED;
54		}
55		delta = tpget.tv_sec - tpset.tv_sec;
56		nsdelta = PR_NSEC_PER_SEC - tpget.tv_nsec;
57		if ((delta <= ACCEPTABLEDELTA) && (delta >= 0)) {
58			printf("Test PASSED\n");
59			setBackTime(tpreset);
60			return PTS_PASS;
61		} else if ((nsdelta <= tpres.tv_nsec) && (delta == -1)) {
62			printf("Test PASSED\n");
63			setBackTime(tpreset);
64			return PTS_PASS;
65		} else {
66			printf("clock does not appear to be set\n");
67			setBackTime(tpreset);
68			return PTS_FAIL;
69		}
70	}
71
72	printf("clock_settime() failed\n");
73	setBackTime(tpreset);
74	return PTS_UNRESOLVED;
75}
76