xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/timers/set-tai.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* Set tai offset
2*4882a593Smuzhiyun  *              by: John Stultz <john.stultz@linaro.org>
3*4882a593Smuzhiyun  *              (C) Copyright Linaro 2013
4*4882a593Smuzhiyun  *              Licensed under the GPLv2
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *   This program is free software: you can redistribute it and/or modify
7*4882a593Smuzhiyun  *   it under the terms of the GNU General Public License as published by
8*4882a593Smuzhiyun  *   the Free Software Foundation, either version 2 of the License, or
9*4882a593Smuzhiyun  *   (at your option) any later version.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *   This program is distributed in the hope that it will be useful,
12*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*4882a593Smuzhiyun  *   GNU General Public License for more details.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <stdio.h>
19*4882a593Smuzhiyun #include <stdlib.h>
20*4882a593Smuzhiyun #include <time.h>
21*4882a593Smuzhiyun #include <sys/time.h>
22*4882a593Smuzhiyun #include <sys/timex.h>
23*4882a593Smuzhiyun #include <string.h>
24*4882a593Smuzhiyun #include <signal.h>
25*4882a593Smuzhiyun #include <unistd.h>
26*4882a593Smuzhiyun #include "../kselftest.h"
27*4882a593Smuzhiyun 
set_tai(int offset)28*4882a593Smuzhiyun int set_tai(int offset)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	struct timex tx;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	memset(&tx, 0, sizeof(tx));
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	tx.modes = ADJ_TAI;
35*4882a593Smuzhiyun 	tx.constant = offset;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	return adjtimex(&tx);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
get_tai(void)40*4882a593Smuzhiyun int get_tai(void)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	struct timex tx;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	memset(&tx, 0, sizeof(tx));
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	adjtimex(&tx);
47*4882a593Smuzhiyun 	return tx.tai;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
main(int argc,char ** argv)50*4882a593Smuzhiyun int main(int argc, char **argv)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	int i, ret;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	ret = get_tai();
55*4882a593Smuzhiyun 	printf("tai offset started at %i\n", ret);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	printf("Checking tai offsets can be properly set: ");
58*4882a593Smuzhiyun 	fflush(stdout);
59*4882a593Smuzhiyun 	for (i = 1; i <= 60; i++) {
60*4882a593Smuzhiyun 		ret = set_tai(i);
61*4882a593Smuzhiyun 		ret = get_tai();
62*4882a593Smuzhiyun 		if (ret != i) {
63*4882a593Smuzhiyun 			printf("[FAILED] expected: %i got %i\n", i, ret);
64*4882a593Smuzhiyun 			return ksft_exit_fail();
65*4882a593Smuzhiyun 		}
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 	printf("[OK]\n");
68*4882a593Smuzhiyun 	return ksft_exit_pass();
69*4882a593Smuzhiyun }
70