1
0

206-compensate_time_change.patch 2.3 KB

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