-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhwclock_sync.c
More file actions
167 lines (138 loc) · 5.39 KB
/
hwclock_sync.c
File metadata and controls
167 lines (138 loc) · 5.39 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <linux/rtc.h>
//check hardware clock and sys clock
//check if ntpdate -d will be a better fix command. then hwclock or date -s
//issue 1 what happens if the system time is completely wrong?
//check ntp status on old servers and chronyd on new.
int main() {
int hour[50], fixthis=0, htime , stime, n=0 , i;
char clean1[100] , precheck1[100];
//clean existing temp file
FILE *prep = popen("rm -rf /var/tmp/check_clock.txt", "r");
// check hardware clock and add stdout to array
// change verbose to debug for older systems
FILE *ls = popen("/usr/sbin/hwclock --debug | grep read", "r");
char buf[256];
while (fgets(buf, sizeof(buf), ls) != 0) {
//printf("System Time : %s", buf);
for (i=0; i < 52; i++) {
// printf("%c", buf[i]);
hour[n] = buf[i];
n = n + 1;
}
}
pclose(ls);
// validate is ntp or chronyd is running .
// need to fix this to check ntpd instead
FILE *pre1 = popen("/etc/init.d/ntpd status ", "r");
while (fgets(precheck1, sizeof(precheck1), ls) != 0) {
// printf("NTP is: %c%c%c%c%c%c%c\n", precheck1[20],precheck1[21],precheck1[22],precheck1[23],precheck1[24],precheck1[25],precheck1[26]);
char precheck2[50];
int live = snprintf(precheck2, sizeof(precheck2), "NTP is: %c%c%c%c%c%c%c", precheck1[20],precheck1[21],precheck1[22],precheck1[23],precheck1[24],precheck1[25],precheck1[26]);
// compare the output string
char out2[50];
char out3[50];
int rel;
strcpy(out2, "NTP is: running");
strcpy(out3, "running");
rel = strcmp(out2, precheck2);
if (rel == 0) {
printf("NTP is running. %d\n", rel);
}
}
pclose(pre1);
FILE *pre2 = popen("/etc/init.d/ntpd status ", "r");
while (fgets(precheck1, sizeof(precheck1), ls) != 0) {
// printf("NTP is: %c%c%c%c%c%c%c\n", precheck1[8],precheck1[9],precheck1[10],precheck1[11],precheck1[12], \
precheck1[13],precheck1[14]);
char precheck2[50];
int live = snprintf(precheck2, sizeof(precheck2), "NTP is in a %c%c%c%c%c%c%c state", precheck1[8],precheck1[9],precheck1[10],precheck1[11],precheck1[12], \
precheck1[13],precheck1[14]);
// compare the output string
char out2[50];
char out3[50];
int rel;
strcpy(out2, "NTP is in a stopped state");
strcpy(out3, "running");
rel = strcmp(out2, precheck2);
if (rel == 0) {
printf("NTP is not running.Terminating application %d\n", rel);
fixthis = fixthis + 1;
}
}
pclose(pre2);
if (fixthis == 1) {
FILE *auto_15 = popen("/usr/sbin/ntpd ", "r");
pclose(auto_15);
exit(0);
}
//for testing only.
//printf("Testing Sysclock with Char %c%c%c%c\n" , hour[42],hour[43],hour[45],hour[46]);
//converting char to int
int r = hour[42] - '0';
int x = hour[43] - '0';
int y = hour[45] - '0';
int z = hour[46] - '0';
//putting the int together . printing htime is for debugging only
htime= r * 1000 + x * 100 + y * 10 + z;
int htimeH = r * 10 + x;
int htimeM = y * 10 + z;
//printf("%i\n", htimeM);
//start comparision to os clock
FILE *os_time = popen("date -u +%H%M", "r");
char buf1[256];
while (fgets(buf1, sizeof(buf1), os_time) != 0) {
//print char fro array- not needed unless debugging
//printf("OS Time : %s", buf1);
}
pclose(os_time);
int a = buf1[0] - '0';
int b = buf1[1] - '0';
int c = buf1[2] - '0';
int d = buf1[3] - '0';
stime= a * 1000 + b * 100 + c * 10 + d;
int stimeH = a * 10 + b;
int stimeM = c * 10 + d;
//printf("%i",stimeM);
if (htime == stime) {
printf("%s", "The SYS clock and HWclock are in sync\n");
}
else if (stime != htime) {
printf("%s" , "The OS and HW clocks are not in sync.The sync is within the threshold\n");
//consider removing this check
// if (htimeH != stimeH) {
// printf("%s", "The hour is off and needed to be fixed. \n");
// FILE *temp = popen("touch /var/tmp/fixclock.txt" , "r");
// }
// if the hardware clock is higher. how do we verify if the clock is correct
if (htimeM > stimeM) {
int timeD = htimeM - stimeM;
//need time offset if the system time is not utc
int timeF = htimeH - 4;
if (timeD > 10) {
printf("%s%i%s", "The hwCLOCK ahead of the sysclock by ", timeD , " minutes.Self correcting\n");
int fix1 = snprintf(clean1, sizeof(clean1), "hwclock --systohc");
FILE *FIX2 = popen(clean1, "r");
FILE *temp = popen("touch /var/tmp/check_clock.txt" , "r");
FILE *temp2 = popen("echo $(date) The clock was off by more then 10 minutes >> /var/tmp/clock.log" , "r");
}
}
// possible patch for minutes in the 50 range (if htimeM < 50 && htimeM < stimeM)
// original line (htimeM < stimeM)
else if (htimeM < stimeM) {
int timeD = stimeM - htimeM;
int timeF = htimeH - 4;
if (timeD > 10) {
printf("%s%i%s", "The HWCLOCK behind the sysclock by ", timeD , " minutes.Self Correcting\n");
// int fix1 = snprintf(clean1, sizeof(clean1), "date -s \"%d:%d\"",htimeH,htimeM);
int fix1 = snprintf(clean1, sizeof(clean1), "hwclock --systohc");
FILE *FIX2 = popen(clean1, "r");
FILE *temp = popen("touch /var/tmp/check_clock.txt" , "r");
FILE *temp2 = popen("echo $(date) The clock was off by more then 10 minutes >> /var/tmp/clock.log" , "r");
}
}
}
}