2 chronyd/chronyc - Programs for keeping computer clocks accurate.
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
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.
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.
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.
20 **********************************************************************
22 =======================================================================
35 #if defined LINUX && defined FEAT_RTC
36 #include "rtc_linux.h"
37 #endif /* defined LINUX */
39 /* ================================================== */
41 static int driver_initialised
= 0;
42 static int driver_preinit_ok
= 0;
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
);
55 #if defined LINUX && defined FEAT_RTC
58 RTC_Linux_TimePreInit
,
60 RTC_Linux_StartMeasurements
,
61 RTC_Linux_WriteParameters
,
76 /* ================================================== */
77 /* Get the last modification time of the driftfile */
80 get_driftfile_time(void)
85 drift_file
= CNF_GetDriftFile();
89 if (stat(drift_file
, &buf
))
95 /* ================================================== */
96 /* Set the system time to the driftfile time if it's in the future */
99 apply_driftfile_time(time_t t
)
103 LCL_ReadCookedTime(&now
, NULL
);
105 if (now
.tv_sec
< t
) {
106 if (LCL_ApplyStepOffset(now
.tv_sec
- t
))
107 LOG(LOGS_INFO
, "System time restored from driftfile");
111 /* ================================================== */
114 RTC_Initialise(int initial_set
)
116 time_t driftfile_time
;
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. */
126 driftfile_time
= get_driftfile_time();
128 if (driver
.time_pre_init
&& driver
.time_pre_init(driftfile_time
)) {
129 driver_preinit_ok
= 1;
131 driver_preinit_ok
= 0;
133 apply_driftfile_time(driftfile_time
);
137 driver_initialised
= 0;
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();
144 if (CNF_GetRtcSync()) {
145 LOG_FATAL("rtcfile directive cannot be used with rtcsync");
149 if ((driver
.init
)()) {
150 driver_initialised
= 1;
153 LOG(LOGS_ERR
, "RTC not supported on this operating system");
158 /* ================================================== */
163 if (driver_initialised
) {
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
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
180 RTC_TimeInit(void (*after_hook
)(void *), void *anything
)
182 if (driver_initialised
&& driver_preinit_ok
) {
183 (driver
.time_init
)(after_hook
, anything
);
185 (after_hook
)(anything
);
189 /* ================================================== */
190 /* Start the RTC measurement process */
193 RTC_StartMeasurements(void)
195 if (driver_initialised
) {
196 (driver
.start_measurements
)();
198 /* Benign if driver not present */
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. */
206 RTC_WriteParameters(void)
208 if (driver_initialised
) {
209 return (driver
.write_parameters
)();
215 /* ================================================== */
218 RTC_GetReport(RPT_RTC_Report
*report
)
220 if (driver_initialised
) {
221 return (driver
.get_report
)(report
);
227 /* ================================================== */
232 if (driver_initialised
) {
233 return (driver
.trim
)();
239 /* ================================================== */