]> git.ipfire.org Git - thirdparty/chrony.git/blob - rtc.c
ntp: fix log message for replaced source
[thirdparty/chrony.git] / rtc.c
1 /*
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 **********************************************************************
21
22 =======================================================================
23
24 */
25
26 #include "config.h"
27
28 #include "sysincl.h"
29
30 #include "rtc.h"
31 #include "local.h"
32 #include "logging.h"
33 #include "conf.h"
34
35 #if defined LINUX && defined FEAT_RTC
36 #include "rtc_linux.h"
37 #endif /* defined LINUX */
38
39 /* ================================================== */
40
41 static int driver_initialised = 0;
42 static int driver_preinit_ok = 0;
43
44 static struct {
45 int (*init)(void);
46 void (*fini)(void);
47 int (*time_pre_init)(time_t driftfile_time);
48 void (*time_init)(void (*after_hook)(void*), void *anything);
49 void (*start_measurements)(void);
50 int (*write_parameters)(void);
51 int (*get_report)(RPT_RTC_Report *report);
52 int (*trim)(void);
53 } driver =
54 {
55 #if defined LINUX && defined FEAT_RTC
56 RTC_Linux_Initialise,
57 RTC_Linux_Finalise,
58 RTC_Linux_TimePreInit,
59 RTC_Linux_TimeInit,
60 RTC_Linux_StartMeasurements,
61 RTC_Linux_WriteParameters,
62 RTC_Linux_GetReport,
63 RTC_Linux_Trim
64 #else
65 NULL,
66 NULL,
67 NULL,
68 NULL,
69 NULL,
70 NULL,
71 NULL,
72 NULL
73 #endif
74 };
75
76 /* ================================================== */
77 /* Get the last modification time of the driftfile */
78
79 static time_t
80 get_driftfile_time(void)
81 {
82 struct stat buf;
83 char *drift_file;
84
85 drift_file = CNF_GetDriftFile();
86 if (!drift_file)
87 return 0;
88
89 if (stat(drift_file, &buf))
90 return 0;
91
92 return buf.st_mtime;
93 }
94
95 /* ================================================== */
96 /* Set the system time to the driftfile time if it's in the future */
97
98 static void
99 apply_driftfile_time(time_t t)
100 {
101 struct timespec now;
102
103 LCL_ReadCookedTime(&now, NULL);
104
105 if (now.tv_sec < t) {
106 if (LCL_ApplyStepOffset(now.tv_sec - t))
107 LOG(LOGS_INFO, "System time restored from driftfile");
108 }
109 }
110
111 /* ================================================== */
112
113 void
114 RTC_Initialise(int initial_set)
115 {
116 time_t driftfile_time;
117 char *file_name;
118
119 /* If the -s option was specified, try to do an initial read of the RTC and
120 set the system time to it. Also, read the last modification time of the
121 driftfile (i.e. system time when chronyd was previously stopped) and set
122 the system time to it if it's in the future to bring the clock closer to
123 the true time when the RTC is broken (e.g. it has no battery), is missing,
124 or there is no RTC driver. */
125 if (initial_set) {
126 driftfile_time = get_driftfile_time();
127
128 if (driver.time_pre_init && driver.time_pre_init(driftfile_time)) {
129 driver_preinit_ok = 1;
130 } else {
131 driver_preinit_ok = 0;
132 if (driftfile_time)
133 apply_driftfile_time(driftfile_time);
134 }
135 }
136
137 driver_initialised = 0;
138
139 /* This is how we tell whether the user wants to load the RTC
140 driver, if he is on a machine where it is an option. */
141 file_name = CNF_GetRtcFile();
142
143 if (file_name) {
144 if (CNF_GetRtcSync()) {
145 LOG_FATAL("rtcfile directive cannot be used with rtcsync");
146 }
147
148 if (driver.init) {
149 if ((driver.init)()) {
150 driver_initialised = 1;
151 }
152 } else {
153 LOG(LOGS_ERR, "RTC not supported on this operating system");
154 }
155 }
156 }
157
158 /* ================================================== */
159
160 void
161 RTC_Finalise(void)
162 {
163 if (driver_initialised) {
164 (driver.fini)();
165 }
166 }
167
168 /* ================================================== */
169 /* Start the processing to get a single measurement from the real time
170 clock, and use it to trim the system time, based on knowing the
171 drift rate of the RTC and the error the last time we set it. If the
172 TimePreInit routine has succeeded, we can be sure that the trim required
173 is not *too* large.
174
175 We are called with a hook to a function to be called after the
176 initialisation is complete. We also call this if we cannot do the
177 initialisation. */
178
179 void
180 RTC_TimeInit(void (*after_hook)(void *), void *anything)
181 {
182 if (driver_initialised && driver_preinit_ok) {
183 (driver.time_init)(after_hook, anything);
184 } else {
185 (after_hook)(anything);
186 }
187 }
188
189 /* ================================================== */
190 /* Start the RTC measurement process */
191
192 void
193 RTC_StartMeasurements(void)
194 {
195 if (driver_initialised) {
196 (driver.start_measurements)();
197 }
198 /* Benign if driver not present */
199 }
200
201 /* ================================================== */
202 /* Write RTC information out to RTC file. Return 0 for success, 1 if
203 RTC driver not running, or 2 if the file cannot be written. */
204
205 int
206 RTC_WriteParameters(void)
207 {
208 if (driver_initialised) {
209 return (driver.write_parameters)();
210 } else {
211 return RTC_ST_NODRV;
212 }
213 }
214
215 /* ================================================== */
216
217 int
218 RTC_GetReport(RPT_RTC_Report *report)
219 {
220 if (driver_initialised) {
221 return (driver.get_report)(report);
222 } else {
223 return 0;
224 }
225 }
226
227 /* ================================================== */
228
229 int
230 RTC_Trim(void)
231 {
232 if (driver_initialised) {
233 return (driver.trim)();
234 } else {
235 return 0;
236 }
237 }
238
239 /* ================================================== */
240