| 1 | pppd: Watch out for time warps |
| 2 | |
| 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. |
| 5 | |
| 6 | When the clock suddenly jumps forward, the internal accounting (connect time) |
| 7 | is confused resulting in unreliable data. |
| 8 | |
| 9 | This patch implements periodic clock checking to look for time warps, if one |
| 10 | is detected, the internal counters are adjusted accordingly. |
| 11 | |
| 12 | Signed-off-by: Jo-Philipp Wich <jow@openwrt.org> |
| 13 | |
| 14 | --- a/pppd/main.c |
| 15 | +++ b/pppd/main.c |
| 16 | @@ -90,6 +90,7 @@ |
| 17 | #include <sys/socket.h> |
| 18 | #include <netinet/in.h> |
| 19 | #include <arpa/inet.h> |
| 20 | +#include <sys/sysinfo.h> |
| 21 | |
| 22 | #include "pppd.h" |
| 23 | #include "magic.h" |
| 24 | @@ -228,6 +229,7 @@ static struct subprocess *children; |
| 25 | |
| 26 | /* Prototypes for procedures local to this file. */ |
| 27 | |
| 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"); |
| 34 | } |
| 35 | |
| 36 | + check_time(); |
| 37 | gettimeofday(&start_time, NULL); |
| 38 | script_unsetenv("CONNECT_TIME"); |
| 39 | script_unsetenv("BYTES_SENT"); |
| 40 | @@ -1267,6 +1270,36 @@ struct callout { |
| 41 | |
| 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; |
| 46 | + |
| 47 | +static void check_time(void) |
| 48 | +{ |
| 49 | + long new_diff; |
| 50 | + struct timeval t; |
| 51 | + struct sysinfo i; |
| 52 | + struct callout *p; |
| 53 | + |
| 54 | + gettimeofday(&t, NULL); |
| 55 | + sysinfo(&i); |
| 56 | + new_diff = t.tv_sec - i.uptime; |
| 57 | + |
| 58 | + if (!uptime_diff_set) { |
| 59 | + uptime_diff = new_diff; |
| 60 | + uptime_diff_set = 1; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 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; |
| 68 | + |
| 69 | + for (p = callout; p != NULL; p = p->c_next) |
| 70 | + p->c_time.tv_sec += new_diff - uptime_diff; |
| 71 | + } |
| 72 | + uptime_diff = new_diff; |
| 73 | +} |
| 74 | |
| 75 | /* |
| 76 | * timeout - Schedule a timeout. |
| 77 | @@ -1337,6 +1370,8 @@ calltimeout() |
| 78 | { |
| 79 | struct callout *p; |
| 80 | |
| 81 | + check_time(); |
| 82 | + |
| 83 | while (callout != NULL) { |
| 84 | p = callout; |
| 85 | |
| 86 | @@ -1364,6 +1399,8 @@ timeleft(tvp) |
| 87 | { |
| 88 | if (callout == NULL) |
| 89 | return NULL; |
| 90 | + |
| 91 | + check_time(); |
| 92 | |
| 93 | gettimeofday(&timenow, NULL); |
| 94 | tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; |
| 95 | |