OpenWrt – Diff between revs 2 and 3

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 2 Rev 3
1 pppd: Watch out for time warps 1 pppd: Watch out for time warps
2   2  
3 On many embedded systems there is no battery backed RTC and a proper system 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. 4 time only becomes available through NTP after establishing a connection.
5   5  
6 When the clock suddenly jumps forward, the internal accounting (connect time) 6 When the clock suddenly jumps forward, the internal accounting (connect time)
7 is confused resulting in unreliable data. 7 is confused resulting in unreliable data.
8   8  
9 This patch implements periodic clock checking to look for time warps, if one 9 This patch implements periodic clock checking to look for time warps, if one
10 is detected, the internal counters are adjusted accordingly. 10 is detected, the internal counters are adjusted accordingly.
11   11  
12 Signed-off-by: Jo-Philipp Wich <jo@mein.io> 12 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
13   13  
14 --- a/pppd/main.c 14 --- a/pppd/main.c
15 +++ b/pppd/main.c 15 +++ b/pppd/main.c
16 @@ -90,6 +90,7 @@ 16 @@ -90,6 +90,7 @@
17 #include <sys/socket.h> 17 #include <sys/socket.h>
18 #include <netinet/in.h> 18 #include <netinet/in.h>
19 #include <arpa/inet.h> 19 #include <arpa/inet.h>
20 +#include <sys/sysinfo.h> 20 +#include <sys/sysinfo.h>
21 21
22 #include "pppd.h" 22 #include "pppd.h"
23 #include "magic.h" 23 #include "magic.h"
24 @@ -228,6 +229,7 @@ static struct subprocess *children; 24 @@ -228,6 +229,7 @@ static struct subprocess *children;
25 25
26 /* Prototypes for procedures local to this file. */ 26 /* Prototypes for procedures local to this file. */
27 27
28 +static void check_time(void); 28 +static void check_time(void);
29 static void setup_signals __P((void)); 29 static void setup_signals __P((void));
30 static void create_pidfile __P((int pid)); 30 static void create_pidfile __P((int pid));
31 static void create_linkpidfile __P((int pid)); 31 static void create_linkpidfile __P((int pid));
32 @@ -527,6 +529,7 @@ main(argc, argv) 32 @@ -535,6 +537,7 @@ main(argc, argv)
33 info("Starting link"); 33 info("Starting link");
34 } 34 }
35 35
36 + check_time(); 36 + check_time();
37 gettimeofday(&start_time, NULL); 37 gettimeofday(&start_time, NULL);
38 script_unsetenv("CONNECT_TIME"); 38 script_unsetenv("CONNECT_TIME");
39 script_unsetenv("BYTES_SENT"); 39 script_unsetenv("BYTES_SENT");
40 @@ -1262,6 +1265,36 @@ struct callout { 40 @@ -1267,6 +1270,36 @@ struct callout {
41 41
42 static struct callout *callout = NULL; /* Callout list */ 42 static struct callout *callout = NULL; /* Callout list */
43 static struct timeval timenow; /* Current time */ 43 static struct timeval timenow; /* Current time */
44 +static long uptime_diff = 0; 44 +static long uptime_diff = 0;
45 +static int uptime_diff_set = 0; 45 +static int uptime_diff_set = 0;
46 + 46 +
47 +static void check_time(void) 47 +static void check_time(void)
48 +{ 48 +{
49 + long new_diff; 49 + long new_diff;
50 + struct timeval t; 50 + struct timeval t;
51 + struct sysinfo i; 51 + struct sysinfo i;
52 + struct callout *p; 52 + struct callout *p;
53 + 53 +
54 + gettimeofday(&t, NULL); 54 + gettimeofday(&t, NULL);
55 + sysinfo(&i); 55 + sysinfo(&i);
56 + new_diff = t.tv_sec - i.uptime; 56 + new_diff = t.tv_sec - i.uptime;
57 + 57 +
58 + if (!uptime_diff_set) { 58 + if (!uptime_diff_set) {
59 + uptime_diff = new_diff; 59 + uptime_diff = new_diff;
60 + uptime_diff_set = 1; 60 + uptime_diff_set = 1;
61 + return; 61 + return;
62 + } 62 + }
63 + 63 +
64 + if ((new_diff - 5 > uptime_diff) || (new_diff + 5 < uptime_diff)) { 64 + if ((new_diff - 5 > uptime_diff) || (new_diff + 5 < uptime_diff)) {
65 + /* system time has changed, update counters and timeouts */ 65 + /* system time has changed, update counters and timeouts */
66 + info("System time change detected."); 66 + info("System time change detected.");
67 + start_time.tv_sec += new_diff - uptime_diff; 67 + start_time.tv_sec += new_diff - uptime_diff;
68 + 68 +
69 + for (p = callout; p != NULL; p = p->c_next) 69 + for (p = callout; p != NULL; p = p->c_next)
70 + p->c_time.tv_sec += new_diff - uptime_diff; 70 + p->c_time.tv_sec += new_diff - uptime_diff;
71 + } 71 + }
72 + uptime_diff = new_diff; 72 + uptime_diff = new_diff;
73 +} 73 +}
74 74
75 /* 75 /*
76 * timeout - Schedule a timeout. 76 * timeout - Schedule a timeout.
77 @@ -1332,6 +1365,8 @@ calltimeout() 77 @@ -1337,6 +1370,8 @@ calltimeout()
78 { 78 {
79 struct callout *p; 79 struct callout *p;
80 80
81 + check_time(); 81 + check_time();
82 + 82 +
83 while (callout != NULL) { 83 while (callout != NULL) {
84 p = callout; 84 p = callout;
85 85
86 @@ -1359,6 +1394,8 @@ timeleft(tvp) 86 @@ -1364,6 +1399,8 @@ timeleft(tvp)
87 { 87 {
88 if (callout == NULL) 88 if (callout == NULL)
89 return NULL; 89 return NULL;
90 + 90 +
91 + check_time(); 91 + check_time();
92 92
93 gettimeofday(&timenow, NULL); 93 gettimeofday(&timenow, NULL);
94 tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; 94 tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
95   95