100-use-monotic-clock-instead-of-time-of-day.patch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. From d0e66e0719ae8eb549f7cc220fdc66575d3db332 Mon Sep 17 00:00:00 2001
  2. From: Jonas Gorski <jonas.gorski@gmail.com>
  3. Date: Thu, 29 Mar 2012 17:01:11 +0200
  4. Subject: [PATCH 4/4] use monotic clock instead of time of day
  5. The time of day might chance e.g. by daylight savings time during the
  6. runtime, which causes timers to fire repeatedly for a long time.
  7. Contributed by T-Labs, Deutsche Telekom Innovation Laboratories
  8. Signed-off-by: Jonas Gorski <jonas.gorski@gmail.com>
  9. ---
  10. configure.ac | 2 ++
  11. src/igmpproxy.c | 26 +++++++++++++-------------
  12. src/igmpproxy.h | 3 ++-
  13. 3 files changed, 17 insertions(+), 14 deletions(-)
  14. diff --git a/configure.ac b/configure.ac
  15. index 85beb08..bd84eba 100644
  16. --- a/configure.ac
  17. +++ b/configure.ac
  18. @@ -25,6 +25,8 @@ AC_CHECK_MEMBERS([struct sockaddr_in.sin_len], [], [], [[
  19. #include <netinet/in.h>
  20. ]])
  21. +AC_SEARCH_LIBS([clock_gettime],[rt])
  22. +
  23. AC_CONFIG_FILES([
  24. Makefile
  25. doc/Makefile
  26. diff --git a/src/igmpproxy.c b/src/igmpproxy.c
  27. index 35000c7..3a9ccad 100644
  28. --- a/src/igmpproxy.c
  29. +++ b/src/igmpproxy.c
  30. @@ -234,13 +234,13 @@ void igmpProxyRun() {
  31. int MaxFD, Rt, secs;
  32. fd_set ReadFDS;
  33. socklen_t dummy = 0;
  34. - struct timeval curtime, lasttime, difftime, tv;
  35. + struct timespec curtime, lasttime, difftime, tv;
  36. // The timeout is a pointer in order to set it to NULL if nessecary.
  37. - struct timeval *timeout = &tv;
  38. + struct timespec *timeout = &tv;
  39. // Initialize timer vars
  40. - difftime.tv_usec = 0;
  41. - gettimeofday(&curtime, NULL);
  42. + difftime.tv_nsec = 0;
  43. + clock_gettime(CLOCK_MONOTONIC, &curtime);
  44. lasttime = curtime;
  45. // First thing we send a membership query in downstream VIF's...
  46. @@ -263,7 +263,7 @@ void igmpProxyRun() {
  47. if(secs == -1) {
  48. timeout = NULL;
  49. } else {
  50. - timeout->tv_usec = 0;
  51. + timeout->tv_nsec = 0;
  52. timeout->tv_sec = secs;
  53. }
  54. @@ -274,7 +274,7 @@ void igmpProxyRun() {
  55. FD_SET( MRouterFD, &ReadFDS );
  56. // wait for input
  57. - Rt = select( MaxFD +1, &ReadFDS, NULL, NULL, timeout );
  58. + Rt = pselect( MaxFD +1, &ReadFDS, NULL, NULL, timeout, NULL );
  59. // log and ignore failures
  60. if( Rt < 0 ) {
  61. @@ -307,20 +307,20 @@ void igmpProxyRun() {
  62. */
  63. if (Rt == 0) {
  64. curtime.tv_sec = lasttime.tv_sec + secs;
  65. - curtime.tv_usec = lasttime.tv_usec;
  66. + curtime.tv_nsec = lasttime.tv_nsec;
  67. Rt = -1; /* don't do this next time through the loop */
  68. } else {
  69. - gettimeofday(&curtime, NULL);
  70. + clock_gettime(CLOCK_MONOTONIC, &curtime);
  71. }
  72. difftime.tv_sec = curtime.tv_sec - lasttime.tv_sec;
  73. - difftime.tv_usec += curtime.tv_usec - lasttime.tv_usec;
  74. - while (difftime.tv_usec > 1000000) {
  75. + difftime.tv_nsec += curtime.tv_nsec - lasttime.tv_nsec;
  76. + while (difftime.tv_nsec > 1000000000) {
  77. difftime.tv_sec++;
  78. - difftime.tv_usec -= 1000000;
  79. + difftime.tv_nsec -= 1000000000;
  80. }
  81. - if (difftime.tv_usec < 0) {
  82. + if (difftime.tv_nsec < 0) {
  83. difftime.tv_sec--;
  84. - difftime.tv_usec += 1000000;
  85. + difftime.tv_nsec += 1000000000;
  86. }
  87. lasttime = curtime;
  88. if (secs == 0 || difftime.tv_sec > 0)
  89. diff --git a/src/igmpproxy.h b/src/igmpproxy.h
  90. index 4df8a79..36a4f04 100644
  91. --- a/src/igmpproxy.h
  92. +++ b/src/igmpproxy.h
  93. @@ -44,12 +44,13 @@
  94. #include <string.h>
  95. #include <fcntl.h>
  96. #include <stdbool.h>
  97. +#include <time.h>
  98. #include <sys/socket.h>
  99. #include <sys/un.h>
  100. -#include <sys/time.h>
  101. #include <sys/ioctl.h>
  102. #include <sys/param.h>
  103. +#include <sys/select.h>
  104. #include <net/if.h>
  105. #include <netinet/in.h>
  106. --
  107. 1.7.2.5