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