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
get_driftfile_time(void)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
apply_driftfile_time(time_t t)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
RTC_Initialise(int initial_set)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       } else {
152         LOG(LOGS_ERR, "RTC driver could not be initialised");
153       }
154     } else {
155       LOG(LOGS_ERR, "RTC not supported on this operating system");
156     }
157   }
158 }
159 
160 /* ================================================== */
161 
162 void
RTC_Finalise(void)163 RTC_Finalise(void)
164 {
165   if (driver_initialised) {
166     (driver.fini)();
167   }
168 }
169 
170 /* ================================================== */
171 /* Start the processing to get a single measurement from the real time
172    clock, and use it to trim the system time, based on knowing the
173    drift rate of the RTC and the error the last time we set it.  If the
174    TimePreInit routine has succeeded, we can be sure that the trim required
175    is not *too* large.
176 
177    We are called with a hook to a function to be called after the
178    initialisation is complete.  We also call this if we cannot do the
179    initialisation. */
180 
181 void
RTC_TimeInit(void (* after_hook)(void *),void * anything)182 RTC_TimeInit(void (*after_hook)(void *), void *anything)
183 {
184   if (driver_initialised && driver_preinit_ok) {
185     (driver.time_init)(after_hook, anything);
186   } else {
187     (after_hook)(anything);
188   }
189 }
190 
191 /* ================================================== */
192 /* Start the RTC measurement process */
193 
194 void
RTC_StartMeasurements(void)195 RTC_StartMeasurements(void)
196 {
197   if (driver_initialised) {
198     (driver.start_measurements)();
199   }
200   /* Benign if driver not present */
201 }
202 
203 /* ================================================== */
204 /* Write RTC information out to RTC file.  Return 0 for success, 1 if
205    RTC driver not running, or 2 if the file cannot be written. */
206 
207 int
RTC_WriteParameters(void)208 RTC_WriteParameters(void)
209 {
210   if (driver_initialised) {
211     return (driver.write_parameters)();
212   } else {
213     return RTC_ST_NODRV;
214   }
215 }
216 
217 /* ================================================== */
218 
219 int
RTC_GetReport(RPT_RTC_Report * report)220 RTC_GetReport(RPT_RTC_Report *report)
221 {
222   if (driver_initialised) {
223     return (driver.get_report)(report);
224   } else {
225     return 0;
226   }
227 }
228 
229 /* ================================================== */
230 
231 int
RTC_Trim(void)232 RTC_Trim(void)
233 {
234   if (driver_initialised) {
235     return (driver.trim)();
236   } else {
237     return 0;
238   }
239 }
240 
241 /* ================================================== */
242 
243