1 pppd: Watch out for time warps
3 On many embedded systems there is no battery backed RTC and a proper system
4 time only becomes available through NTP after establishing a connection.
6 When the clock suddenly jumps forward, the internal accounting (connect time)
7 is confused resulting in unreliable data.
9 This patch implements periodic clock checking to look for time warps, if one
10 is detected, the internal counters are adjusted accordingly.
12 Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 +#include <sys/sysinfo.h>
24 @@ -228,6 +229,7 @@ static struct subprocess *children;
26 /* Prototypes for procedures local to this file. */
28 +static void check_time(void);
29 static void setup_signals __P((void));
30 static void create_pidfile __P((int pid));
31 static void create_linkpidfile __P((int pid));
32 @@ -535,6 +537,7 @@ main(argc, argv)
33 info("Starting link");
37 gettimeofday(&start_time, NULL);
38 script_unsetenv("CONNECT_TIME");
39 script_unsetenv("BYTES_SENT");
40 @@ -1267,6 +1270,36 @@ struct callout {
42 static struct callout *callout = NULL; /* Callout list */
43 static struct timeval timenow; /* Current time */
44 +static long uptime_diff = 0;
45 +static int uptime_diff_set = 0;
47 +static void check_time(void)
54 + gettimeofday(&t, NULL);
56 + new_diff = t.tv_sec - i.uptime;
58 + if (!uptime_diff_set) {
59 + uptime_diff = new_diff;
60 + uptime_diff_set = 1;
64 + if ((new_diff - 5 > uptime_diff) || (new_diff + 5 < uptime_diff)) {
65 + /* system time has changed, update counters and timeouts */
66 + info("System time change detected.");
67 + start_time.tv_sec += new_diff - uptime_diff;
69 + for (p = callout; p != NULL; p = p->c_next)
70 + p->c_time.tv_sec += new_diff - uptime_diff;
72 + uptime_diff = new_diff;
76 * timeout - Schedule a timeout.
77 @@ -1337,6 +1370,8 @@ calltimeout()
83 while (callout != NULL) {
86 @@ -1364,6 +1399,8 @@ timeleft(tvp)
93 gettimeofday(&timenow, NULL);
94 tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;