1566b80c9d89d9e751a3332e48ede669217b61ed
[openwrt.git] / package / ppp / patches / 206-compensate_time_change.patch
1 diff -Naur ppp-2.4.4.orig/pppd/main.c ppp-2.4.4/pppd/main.c
2 --- ppp-2.4.4.orig/pppd/main.c  2009-05-07 22:25:24.000000000 -0400
3 +++ ppp-2.4.4/pppd/main.c       2009-05-07 22:22:40.000000000 -0400
4 @@ -90,6 +90,7 @@
5  #include <sys/socket.h>
6  #include <netinet/in.h>
7  #include <arpa/inet.h>
8 +#include <sys/sysinfo.h>
9  
10  #include "pppd.h"
11  #include "magic.h"
12 @@ -227,6 +228,7 @@
13  
14  /* Prototypes for procedures local to this file. */
15  
16 +static void check_time(void);
17  static void setup_signals __P((void));
18  static void create_pidfile __P((int pid));
19  static void create_linkpidfile __P((int pid));
20 @@ -532,6 +534,7 @@
21             info("Starting link");
22         }
23  
24 +       check_time();
25         gettimeofday(&start_time, NULL);
26         script_unsetenv("CONNECT_TIME");
27         script_unsetenv("BYTES_SENT");
28 @@ -1264,6 +1267,36 @@
29  
30  static struct callout *callout = NULL; /* Callout list */
31  static struct timeval timenow;         /* Current time */
32 +static long uptime_diff = 0;
33 +static int uptime_diff_set = 0;
34 +
35 +static void check_time(void)
36 +{
37 +       long new_diff;
38 +       struct timeval t;
39 +       struct sysinfo i;
40 +    struct callout *p;
41 +       
42 +       gettimeofday(&t, NULL);
43 +       sysinfo(&i);
44 +       new_diff = t.tv_sec - i.uptime;
45 +       
46 +       if (!uptime_diff_set) {
47 +               uptime_diff = new_diff;
48 +               uptime_diff_set = 1;
49 +               return;
50 +       }
51 +
52 +       if ((new_diff - 5 > uptime_diff) || (new_diff + 5 < uptime_diff)) {
53 +               /* system time has changed, update counters and timeouts */
54 +               info("System time change detected.");
55 +               start_time.tv_sec += new_diff - uptime_diff;
56 +               
57 +       for (p = callout; p != NULL; p = p->c_next)
58 +                       p->c_time.tv_sec += new_diff - uptime_diff;
59 +       }
60 +       uptime_diff = new_diff;
61 +}
62  
63  /*
64   * timeout - Schedule a timeout.
65 @@ -1334,6 +1367,8 @@
66  {
67      struct callout *p;
68  
69 +       check_time();
70 +       
71      while (callout != NULL) {
72         p = callout;
73  
74 @@ -1361,6 +1396,8 @@
75  {
76      if (callout == NULL)
77         return NULL;
78 +       
79 +       check_time();
80  
81      gettimeofday(&timenow, NULL);
82      tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
83 diff -Naur ppp-2.4.4.orig/pppd/main.c ppp-2.4.4/pppd/main.c
84 --- ppp-2.4.4.orig/pppd/main.c  2009-05-07 22:25:24.000000000 -0400
85 +++ ppp-2.4.4/pppd/main.c       2009-05-07 22:22:40.000000000 -0400
86 @@ -90,6 +90,7 @@
87  #include <sys/socket.h>
88  #include <netinet/in.h>
89  #include <arpa/inet.h>
90 +#include <sys/sysinfo.h>
91  
92  #include "pppd.h"
93  #include "magic.h"
94 @@ -227,6 +228,7 @@
95  
96  /* Prototypes for procedures local to this file. */
97  
98 +static void check_time(void);
99  static void setup_signals __P((void));
100  static void create_pidfile __P((int pid));
101  static void create_linkpidfile __P((int pid));
102 @@ -532,6 +534,7 @@
103             info("Starting link");
104         }
105  
106 +       check_time();
107         gettimeofday(&start_time, NULL);
108         script_unsetenv("CONNECT_TIME");
109         script_unsetenv("BYTES_SENT");
110 @@ -1264,6 +1267,36 @@
111  
112  static struct callout *callout = NULL; /* Callout list */
113  static struct timeval timenow;         /* Current time */
114 +static long uptime_diff = 0;
115 +static int uptime_diff_set = 0;
116 +
117 +static void check_time(void)
118 +{
119 +       long new_diff;
120 +       struct timeval t;
121 +       struct sysinfo i;
122 +    struct callout *p;
123 +       
124 +       gettimeofday(&t, NULL);
125 +       sysinfo(&i);
126 +       new_diff = t.tv_sec - i.uptime;
127 +       
128 +       if (!uptime_diff_set) {
129 +               uptime_diff = new_diff;
130 +               uptime_diff_set = 1;
131 +               return;
132 +       }
133 +
134 +       if ((new_diff - 5 > uptime_diff) || (new_diff + 5 < uptime_diff)) {
135 +               /* system time has changed, update counters and timeouts */
136 +               info("System time change detected.");
137 +               start_time.tv_sec += new_diff - uptime_diff;
138 +               
139 +       for (p = callout; p != NULL; p = p->c_next)
140 +                       p->c_time.tv_sec += new_diff - uptime_diff;
141 +       }
142 +       uptime_diff = new_diff;
143 +}
144  
145  /*
146   * timeout - Schedule a timeout.
147 @@ -1334,6 +1367,8 @@
148  {
149      struct callout *p;
150  
151 +       check_time();
152 +       
153      while (callout != NULL) {
154         p = callout;
155  
156 @@ -1361,6 +1396,8 @@
157  {
158      if (callout == NULL)
159         return NULL;
160 +       
161 +       check_time();
162  
163      gettimeofday(&timenow, NULL);
164      tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;