Merge pull request #4853 from StevenHessing/noddos
[feed/packages.git] / utils / collectd / patches / 500-fix-uptime-reading.patch
1 From af01dd6fa3eb458e2fbb272703b0cae37ea54a9b Mon Sep 17 00:00:00 2001
2 From: Marcin Jurkowski <marcin1j@gmail.com>
3 Date: Tue, 11 Jul 2017 15:00:25 +0200
4 Subject: [PATCH] uptime plugin: don't cache boot time and simplify Linux code
5
6 Caching boottime on startup yields incorrect uptime values if system
7 date changes after the daemon is started.
8 This is almost certain to happen on embedded systems without RTC, where
9 clock is set from NTP server at some point after boot process.
10
11 On Linux, we can retrieve uptime directly by either reading /proc/uptime
12 (it's sufficient to read a few bytes) or calling sysinfo() function.
13 Use the latter since it's the most efficient way in speed, memory
14 requirements and code simplicity terms.
15 ---
16 src/uptime.c | 71 ++++++++++++++++--------------------------------------------
17 1 file changed, 19 insertions(+), 52 deletions(-)
18
19 --- a/src/uptime.c
20 +++ b/src/uptime.c
21 @@ -25,8 +25,7 @@
22 #include "plugin.h"
23
24 #if KERNEL_LINUX
25 -#define STAT_FILE "/proc/stat"
26 -/* Using /proc filesystem to retrieve the boot time, Linux only. */
27 +#include <sys/sysinfo.h>
28 /* #endif KERNEL_LINUX */
29
30 #elif HAVE_LIBKSTAT
31 @@ -53,8 +52,6 @@
32 /*
33 * Global variables
34 */
35 -/* boottime always used, no OS distinction */
36 -static time_t boottime;
37
38 #if HAVE_LIBKSTAT
39 extern kstat_ctl_t *kc;
40 @@ -72,8 +69,6 @@ static void uptime_submit(gauge_t value)
41 plugin_dispatch_values(&vl);
42 }
43
44 -static int uptime_init(void) /* {{{ */
45 -{
46 /*
47 * On most unix systems the uptime is calculated by looking at the boot
48 * time (stored in unix time, since epoch) and the current one. We are
49 @@ -84,48 +79,21 @@ static int uptime_init(void) /* {{{ */
50 * the boot time, the plugin is unregistered and there is no chance to
51 * try again later. Nevertheless, this is very unlikely to happen.
52 */
53 -
54 +static time_t uptime_get_sys(void) { /* {{{ */
55 + time_t result;
56 #if KERNEL_LINUX
57 - unsigned long starttime;
58 - char buffer[1024];
59 - int ret;
60 - FILE *fh;
61 -
62 - ret = 0;
63 -
64 - fh = fopen(STAT_FILE, "r");
65 + struct sysinfo info;
66 + int status;
67
68 - if (fh == NULL) {
69 + status = sysinfo(&info);
70 + if (status != 0) {
71 char errbuf[1024];
72 - ERROR("uptime plugin: Cannot open " STAT_FILE ": %s",
73 + ERROR("uptime plugin: Error calling sysinfo: %s",
74 sstrerror(errno, errbuf, sizeof(errbuf)));
75 return (-1);
76 }
77
78 - while (fgets(buffer, 1024, fh) != NULL) {
79 - /* look for the btime string and read the value */
80 - ret = sscanf(buffer, "btime %lu", &starttime);
81 - /* avoid further loops if btime has been found and read
82 - * correctly (hopefully) */
83 - if (ret == 1)
84 - break;
85 - }
86 -
87 - fclose(fh);
88 -
89 - /* loop done, check if no value has been found/read */
90 - if (ret != 1) {
91 - ERROR("uptime plugin: No value read from " STAT_FILE "");
92 - return (-1);
93 - }
94 -
95 - boottime = (time_t)starttime;
96 -
97 - if (boottime == 0) {
98 - ERROR("uptime plugin: btime read from " STAT_FILE ", "
99 - "but `boottime' is zero!");
100 - return (-1);
101 - }
102 + result = (time_t)info.uptime;
103 /* #endif KERNEL_LINUX */
104
105 #elif HAVE_LIBKSTAT
106 @@ -159,13 +127,13 @@ static int uptime_init(void) /* {{{ */
107 return (-1);
108 }
109
110 - boottime = (time_t)knp->value.ui32;
111 -
112 - if (boottime == 0) {
113 + if (knp->value.ui32 == 0) {
114 ERROR("uptime plugin: kstat_data_lookup returned success, "
115 "but `boottime' is zero!");
116 return (-1);
117 }
118 +
119 + result = time(NULL) - (time_t)knp->value.ui32;
120 /* #endif HAVE_LIBKSTAT */
121
122 #elif HAVE_SYS_SYSCTL_H
123 @@ -186,13 +154,13 @@ static int uptime_init(void) /* {{{ */
124 return (-1);
125 }
126
127 - boottime = boottv.tv_sec;
128 -
129 - if (boottime == 0) {
130 + if (boottv.tv_sec == 0) {
131 ERROR("uptime plugin: sysctl(3) returned success, "
132 "but `boottime' is zero!");
133 return (-1);
134 }
135 +
136 + result = time(NULL) - boottv.tv_sec;
137 /* #endif HAVE_SYS_SYSCTL_H */
138
139 #elif HAVE_PERFSTAT
140 @@ -212,18 +180,18 @@ static int uptime_init(void) /* {{{ */
141 if (hertz <= 0)
142 hertz = HZ;
143
144 - boottime = time(NULL) - cputotal.lbolt / hertz;
145 + result = cputotal.lbolt / hertz;
146 #endif /* HAVE_PERFSTAT */
147
148 - return (0);
149 -} /* }}} int uptime_init */
150 + return result;
151 +} /* }}} int uptime_get_sys */
152
153 static int uptime_read(void) {
154 gauge_t uptime;
155 time_t elapsed;
156
157 /* calculate the amount of time elapsed since boot, AKA uptime */
158 - elapsed = time(NULL) - boottime;
159 + elapsed = uptime_get_sys();
160
161 uptime = (gauge_t)elapsed;
162
163 @@ -233,6 +201,5 @@ static int uptime_read(void) {
164 }
165
166 void module_register(void) {
167 - plugin_register_init("uptime", uptime_init);
168 plugin_register_read("uptime", uptime_read);
169 } /* void module_register */