vnstat  2.9
About: vnStat is a console-based network traffic monitor (using the /proc filesystem).
  Fossies Dox: vnstat-2.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

common.c
Go to the documentation of this file.
1#include "common.h"
2
3/* global variables */
6char errorstring[1024];
8int debug;
9int noexit; /* = running as daemon if 2 */
13
14int printe(const PrintType type)
15{
16 int result = 1;
17 char timestamp[22];
18 time_t current;
19
20 if (disableprints) {
21 return 1;
22
23 /* daemon running but log not enabled */
24 } else if (noexit == 2 && cfg.uselogging == 0) {
25 return 1;
26
27 /* daemon running, log enabled */
28 } else if (noexit == 2) {
29
30 switch (type) {
31 case PT_Multiline:
32 break;
33 case PT_Info:
34 case PT_Infoless:
35 case PT_Warning:
36 case PT_Error:
37 case PT_Config:
39 result = logprint(type);
40 break;
41 }
42
43 /* daemon isn't running or is running attached to a terminal */
44 } else {
45
46 if (cfg.timestampprints && type != PT_ShortMultiline) {
47 current = time(NULL);
48 strftime(timestamp, 22, DATETIMEFORMAT, localtime(&current));
49 printf("[%s] ", timestamp);
50 }
51
52 switch (type) {
53 case PT_Info:
54 printf("Info: %s\n", errorstring);
55 break;
56 case PT_Infoless:
57 printf("%s\n", errorstring);
58 break;
59 case PT_Warning:
60 printf("Warning: %s\n", errorstring);
61 break;
62 case PT_Error:
63 printf("Error: %s\n", errorstring);
64 break;
65 case PT_Config:
66 printf("Config: %s\n", errorstring);
67 break;
68 case PT_Multiline:
69 printf("%s\n", errorstring);
70 break;
72 break;
73 }
74 fflush(stdout);
75 }
76
77 return result;
78}
79
80int logprint(const PrintType type)
81{
82 /* buffer needs some extra space for timestamp + info compared to errorstring */
83 char timestamp[22], buffer[1060];
84 time_t current;
85 FILE *logfile;
86
87 buffer[0] = '\0';
88
89 /* logfile */
90 if (cfg.uselogging == 1) {
91
92 if (type == PT_Multiline) {
93 return 0;
94 }
95
96 if ((logfile = fopen(cfg.logfile, "a")) == NULL) {
97 return 0;
98 }
99
100 current = time(NULL);
101 strftime(timestamp, 22, DATETIMEFORMAT, localtime(&current));
102
103 switch (type) {
104 case PT_Info:
105 case PT_Infoless:
106 snprintf(buffer, 1060, "[%s] %s\n", timestamp, errorstring);
107 break;
108 case PT_Warning:
109 snprintf(buffer, 1060, "[%s] Warning: %s\n", timestamp, errorstring);
110 break;
111 case PT_Error:
112 snprintf(buffer, 1060, "[%s] Error: %s\n", timestamp, errorstring);
113 break;
114 case PT_Config:
115 snprintf(buffer, 1060, "[%s] Config: %s\n", timestamp, errorstring);
116 break;
117 case PT_Multiline:
118 break;
120 snprintf(buffer, 1060, "[%s] %s\n", timestamp, errorstring);
121 break;
122 }
123
124 if (fwrite(buffer, strlen(buffer), 1, logfile) != 1) {
125 fclose(logfile);
126 return 0;
127 }
128
129 fclose(logfile);
130 return 1;
131
132 /* syslog */
133 } else if (cfg.uselogging == 2) {
134
135 openlog("vnstatd", LOG_PID, LOG_DAEMON);
136
137 switch (type) {
138 case PT_Multiline:
139 break;
140 case PT_Warning:
141 syslog(LOG_WARNING, "Warning: %s", errorstring);
142 break;
143 case PT_Error:
144 syslog(LOG_ERR, "Error: %s", errorstring);
145 break;
146 case PT_Config:
147 syslog(LOG_ERR, "Config: %s", errorstring);
148 break;
149 case PT_Info:
150 case PT_Infoless:
152 syslog(LOG_NOTICE, "%s", errorstring);
153 break;
154 }
155
156 closelog();
157 return 1;
158 }
159
160 return 0;
161}
162
164{
165 FILE *logfile;
166
167 /* only logfile logging can be verified */
168 if (cfg.uselogging == 1) {
169 if ((logfile = fopen(cfg.logfile, "a")) == NULL) {
170 return 0;
171 }
172 fclose(logfile);
173 }
174 return 1;
175}
176
177int dmonth(const int month)
178{
179 static int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
180 int year;
181 time_t current;
182
183 /* handle leap years */
184 if (month == 1) {
185 current = time(NULL);
186 year = localtime(&current)->tm_year + 1900;
187 if (isleapyear(year)) {
188 return 29;
189 } else {
190 return 28;
191 }
192 } else {
193 return dmon[month];
194 }
195}
196
197int isleapyear(const int year)
198{
199 if (year % 4 != 0) {
200 return 0;
201 } else if (year % 100 != 0) {
202 return 1;
203 } else if (year % 400 != 0) {
204 return 0;
205 }
206 return 1;
207}
208
209time_t mosecs(time_t month, time_t updated)
210{
211 struct tm d;
212
213 if (localtime_r(&month, &d) == NULL) {
214 return 1;
215 }
216
217 d.tm_mday = cfg.monthrotate;
218 d.tm_hour = d.tm_min = d.tm_sec = 0;
219
220 if ((updated - month) > 0) {
221 return updated - mktime(&d);
222 } else {
223 return 1;
224 }
225}
226
227uint64_t countercalc(const uint64_t *a, const uint64_t *b, const short is64bit)
228{
229 /* no rollover */
230 if (*b >= *a) {
231 if (debug)
232 printf("cc (%d): %" PRIu64 " - %" PRIu64 " = %" PRIu64 "\n", is64bit, *b, *a, *b - *a);
233 return *b - *a;
234
235 /* rollover exists */
236 } else {
237 /* counter is 64bit */
238 if (*a > MAX32 || *b > MAX32 || is64bit == 1) {
239 if (debug)
240 printf("cc64 (%d): uint64 - %" PRIu64 " + %" PRIu64 " = %" PRIu64 "\n", is64bit, *a, *b, (uint64_t)MAX64 - *a + *b);
241 return MAX64 - *a + *b;
242
243 /* counter is 32bit */
244 } else {
245 if (debug)
246 printf("cc32 (%d): uint32 - %" PRIu64 " + %" PRIu64 " = %" PRIu64 "\n", is64bit, *a, *b, (uint64_t)MAX32 - *a + *b);
247 return MAX32 - *a + *b;
248 }
249 }
250}
251
252/* strncpy with ensured null termination */
253char *strncpy_nt(char *dest, const char *src, size_t n)
254{
255 strncpy(dest, src, n);
256 dest[n - 1] = '\0';
257 return dest;
258}
259
260int isnumeric(const char *s)
261{
262 size_t i, len = strlen(s);
263
264 if (!len) {
265 return 0;
266 }
267
268 for (i = 0; i < len; i++) {
269 if (!isdigit(s[i])) {
270 return 0;
271 }
272 }
273
274 return 1;
275}
276
277__attribute__((noreturn)) void panicexit(const char *sourcefile, const int sourceline)
278{
279 snprintf(errorstring, 1024, "Unexpected error (%s), exiting. (%s:%d)", strerror(errno), sourcefile, sourceline);
280 fprintf(stderr, "%s\n", errorstring);
282 exit(EXIT_FAILURE);
283}
284
285char *getversion(void)
286{
287 int i;
288 static char versionbuffer[16];
289 strncpy_nt(versionbuffer, VERSION, 16);
290 for (i = 0; i < (int)strlen(versionbuffer); i++) {
291 if (versionbuffer[i] == '_') {
292 versionbuffer[i] = ' ';
293 }
294 }
295 return versionbuffer;
296}
297
298double timeused(const char *func, const int reset)
299{
300 static struct timeval starttime;
301 struct timeval endtime;
302 double used_secs;
303
304 if (reset) {
305 gettimeofday(&starttime, NULL);
306 return 0.0;
307 }
308
309 if (gettimeofday(&endtime, NULL) != 0) {
310 return 0.0;
311 }
312
313 used_secs = (double)(endtime.tv_usec - starttime.tv_usec) / 1000000 + (double)(endtime.tv_sec - starttime.tv_sec);
314
315 if (debug) {
316 printf("%s() in %f s\n", func, used_secs);
317 }
318
319 return used_secs;
320}
321
322void timeused_debug(const char *func, const int reset)
323{
324 if (!debug) {
325 return;
326 }
327
328 timeused(func, reset);
329}
ibwnode * ifacebw
Definition: common.c:7
int noexit
Definition: common.c:9
double timeused(const char *func, const int reset)
Definition: common.c:298
time_t mosecs(time_t month, time_t updated)
Definition: common.c:209
int intsignal
Definition: common.c:10
int disableprints
Definition: common.c:12
int isleapyear(const int year)
Definition: common.c:197
int dmonth(const int month)
Definition: common.c:177
uint64_t countercalc(const uint64_t *a, const uint64_t *b, const short is64bit)
Definition: common.c:227
IFINFO ifinfo
Definition: common.c:5
void panicexit(const char *sourcefile, const int sourceline)
Definition: common.c:277
int verifylogaccess(void)
Definition: common.c:163
void timeused_debug(const char *func, const int reset)
Definition: common.c:322
int isnumeric(const char *s)
Definition: common.c:260
int logprint(const PrintType type)
Definition: common.c:80
char errorstring[1024]
Definition: common.c:6
int printe(const PrintType type)
Definition: common.c:14
int debug
Definition: common.c:8
char * getversion(void)
Definition: common.c:285
CFG cfg
Definition: common.c:4
char * strncpy_nt(char *dest, const char *src, size_t n)
Definition: common.c:253
int pidfile
Definition: common.c:11
#define MAX32
Definition: common.h:214
#define MAX64
Definition: common.h:215
#define DATETIMEFORMAT
Definition: common.h:89
PrintType
Definition: common.h:349
@ PT_ShortMultiline
Definition: common.h:356
@ PT_Info
Definition: common.h:350
@ PT_Error
Definition: common.h:353
@ PT_Config
Definition: common.h:354
@ PT_Multiline
Definition: common.h:355
@ PT_Infoless
Definition: common.h:351
@ PT_Warning
Definition: common.h:352
Definition: common.h:306
int32_t monthrotate
Definition: common.h:315
int32_t uselogging
Definition: common.h:322
int32_t timestampprints
Definition: common.h:325
char logfile[512]
Definition: common.h:319
Definition: common.h:329