-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbumpclock.c
73 lines (61 loc) · 1.6 KB
/
bumpclock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Hack to bump the system time for testing ntpd response.
* Must be run as root.
* Arg is microseconds.
*/
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include "ntpfrob.h"
#define NS_PER_S 1000000000
void bumpclock(int64_t bump)
{
struct timespec was = {0, 0}, now = {0, 0}, set;
int rc1, rc2, rc3;
int er1, er2, er3;
printf("Bumping clock by %" PRId64 " microseconds.\n", bump);
rc1 = clock_gettime(CLOCK_REALTIME, &was);
er1 = errno;
set = was;
if (bump > 1000000 || bump < -1000000) {
int64_t sec = bump/1000000;
bump -= sec*1000000;
set.tv_sec += sec;
}
bump *= 1000;
/* coverity[tainted_data] */
set.tv_nsec += bump;
while (set.tv_nsec >= NS_PER_S ) {
set.tv_nsec -= NS_PER_S;
set.tv_sec +=1;
}
while (set.tv_nsec <= 0 ) {
set.tv_nsec += NS_PER_S;
set.tv_sec -=1;
}
rc2 = clock_settime(CLOCK_REALTIME, &set);
er2 = errno;
rc3 = clock_gettime(CLOCK_REALTIME, &now);
er3 = errno;
/* Defer printing so it doesn't distort timing. */
if (rc1) {
printf("Couldn't get old time: %s\n", strerror(er1));
} else {
printf("Was: %ld. ld\n", (long)was.tv_sec, was.tv_nsec);
}
if (rc2) {
printf("Couldn't set time: %s\n", strerror(er2));
printf("Try: %ld. ld\n", (long)set.tv_sec, set.tv_nsec);
} else {
printf("Set: %ld. ld\n", (long)set.tv_sec, set.tv_nsec);
}
if (rc3) {
printf("Couldn't get new time: %s\n", strerror(er3));
} else {
printf("Now: %ld. ld\n", (long)now.tv_sec, now.tv_nsec);
}
}