Mercurial > wflogs-daemon
annotate src/wflogs-config.cpp @ 6:a043b7d7269c
truncate output files after processing them
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 17 May 2013 12:22:33 -0700 |
parents | 37eace15ef87 |
children | 306059d0f677 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
3 Copyright (c) 2013 Carl Byington - 510 Software Group, released under |
0 | 4 the GPL version 3 or any later version at your choice available at |
5 http://www.gnu.org/licenses/gpl-3.0.txt | |
6 | |
7 */ | |
8 | |
9 #include "includes.h" | |
10 #include <fcntl.h> | |
11 #include <sys/socket.h> | |
12 #include <netinet/in.h> | |
13 #include <arpa/inet.h> | |
14 #include <netdb.h> | |
15 #include <limits.h> | |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
16 #include <time.h> |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
17 |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
18 |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
19 string_set all_strings;// owns all the strings, only modified by the config loader thread |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
20 const int maxlen = 1000; // used for snprintf buffers |
0 | 21 |
22 const char *token_context; | |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
23 const char *token_daily; |
0 | 24 const char *token_file; |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
25 const char *token_hourly; |
0 | 26 const char *token_include; |
27 const char *token_lbrace; | |
28 const char *token_output; | |
29 const char *token_pattern; | |
30 const char *token_period; | |
31 const char *token_rbrace; | |
32 const char *token_semi; | |
33 const char *token_tempin; | |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
34 const char *token_trigger; |
0 | 35 const char *token_versions; |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
36 const char *token_weekly; |
0 | 37 const char *token_wflogs; |
38 | |
39 | |
40 //////////////////////////////////////////////// | |
41 // | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
42 CONTEXT::CONTEXT(const char *nam) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
43 name = nam; |
0 | 44 fd = -1; |
45 len = 0; | |
46 fdo = -1; | |
47 period = 120; | |
48 versions = 3; | |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
49 trigger = NULL; |
0 | 50 output = NULL; |
51 tempin = NULL; | |
52 wflogs = NULL; | |
53 fn = NULL; | |
54 pattern = NULL; | |
55 } | |
56 | |
57 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
58 CONTEXT::~CONTEXT() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
59 free_all(); |
0 | 60 } |
61 | |
62 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
63 void CONTEXT::dump() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
64 printf("context %s {\n", name); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
65 printf(" period %d; \n", period); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
66 printf(" versions %d; \n", versions); |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
67 if (trigger) printf(" trigger \"%s\";\n", trigger); |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
68 printf(" output \"%s\";\n", output); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
69 printf(" tempin \"%s\";\n", tempin); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
70 printf(" wflogs \"%s\";\n", wflogs); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
71 printf(" file \"%s\";\n", fn); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
72 printf(" pattern \"%s\";\n", pattern); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
73 printf("};\n\n"); |
0 | 74 } |
75 | |
76 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
77 void CONTEXT::openo(bool msg) { |
0 | 78 open_time = time(NULL); |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
79 localtime_r(&open_time, &open_tm); |
6
a043b7d7269c
truncate output files after processing them
Carl Byington <carl@five-ten-sg.com>
parents:
4
diff
changeset
|
80 int flags = O_CREAT | O_WRONLY; |
a043b7d7269c
truncate output files after processing them
Carl Byington <carl@five-ten-sg.com>
parents:
4
diff
changeset
|
81 if (!msg) flags |= O_TRUNC; |
a043b7d7269c
truncate output files after processing them
Carl Byington <carl@five-ten-sg.com>
parents:
4
diff
changeset
|
82 fdo = ::open(tempin, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
0 | 83 if (fdo == -1) { |
84 if (msg) { | |
85 char buf[maxlen]; | |
86 snprintf(buf, sizeof(buf), "wflogs tempin file %s not writeable", tempin); | |
87 tokp->token_error(buf); | |
88 } | |
89 } | |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
90 else { |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
91 lseek(fdo, 0, SEEK_END); |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
92 } |
0 | 93 } |
94 | |
95 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
96 void CONTEXT::open(bool msg) { |
0 | 97 fd = ::open(fn, O_RDONLY); |
98 len = 0; | |
99 if (fd == -1) { | |
100 if (msg) { | |
101 char buf[maxlen]; | |
102 snprintf(buf, sizeof(buf), "syslog file %s not readable", fn); | |
103 tokp->token_error(buf); | |
104 } | |
105 } | |
106 else { | |
107 if (debug_syslog > 1) { | |
108 snprintf(buf, sizeof(buf), "syslog file %s opened", fn); | |
109 my_syslog(buf); | |
110 } | |
111 if (msg) lseek(fd, 0, SEEK_END); | |
112 if (fstat(fd, &openfdstat)) { | |
113 close(); | |
114 snprintf(buf, sizeof(buf), "syslog file %s cannot stat after open", fn); | |
6
a043b7d7269c
truncate output files after processing them
Carl Byington <carl@five-ten-sg.com>
parents:
4
diff
changeset
|
115 if (msg) tokp->token_error(buf); |
0 | 116 } |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
117 // specify that this fd gets closed on exec, so that wflogs |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
118 // won't have access to it. |
0 | 119 int oldflags = fcntl(fd, F_GETFD, 0); |
120 if (oldflags >= 0) { | |
121 fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC); | |
122 } | |
123 } | |
124 } | |
125 | |
126 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
127 bool CONTEXT::write(char *p) { |
0 | 128 // p points to \0 at end of buf, may be destroyed |
129 if (failedo()) { | |
130 openo(false); | |
131 if (failedo()) return false; | |
132 } | |
133 *p = '\n'; | |
134 ::write(fdo, buf, p-buf+1); | |
135 } | |
136 | |
137 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
138 bool CONTEXT::read() { |
0 | 139 if (failed()) { |
140 open(false); | |
141 if (failed()) return false; | |
142 } | |
143 int n = ::read(fd, buf+len, buflen-len); | |
144 bool have = (n > 0); | |
145 if (have) { | |
146 len += n; | |
147 while (true) { | |
148 char *p = (char*)memchr(buf, '\n', len); | |
149 if (!p) break; | |
150 n = p-buf; | |
151 *p = '\0'; | |
152 process(p); // process null terminated string, but may destroy the null | |
153 len -= n+1; | |
154 memmove(buf, p+1, len); | |
155 } | |
156 // no <lf> in a full buffer | |
157 if (len == buflen) len = 0; | |
158 } | |
159 else { | |
160 // check for file close | |
161 struct stat filenamest; | |
162 if (0 == stat(fn, &filenamest)) { | |
163 if ((filenamest.st_dev != openfdstat.st_dev) || | |
164 (filenamest.st_ino != openfdstat.st_ino)) { | |
165 close(); | |
166 } | |
167 } | |
168 else { | |
169 // filename no longer exists | |
170 close(); | |
171 } | |
172 } | |
173 check_wflog(); | |
174 return have; | |
175 } | |
176 | |
177 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
178 void CONTEXT::closeo() { |
0 | 179 if (fdo != -1) ::close(fdo); |
180 fdo = -1; | |
181 } | |
182 | |
183 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
184 void CONTEXT::close() { |
0 | 185 if (debug_syslog > 1) { |
186 snprintf(buf, sizeof(buf), "syslog file %s closed", fn); | |
187 my_syslog(buf); | |
188 } | |
189 if (fd != -1) ::close(fd); | |
190 fd = -1; | |
191 } | |
192 | |
193 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
194 void CONTEXT::process(char *p) { |
0 | 195 // p points to \0 at end of buf, may be destroyed |
196 if (pattern) { | |
197 if (0 == regexec(&re, buf, 0, NULL, 0)) { | |
198 if (debug_syslog > 2) { | |
199 my_syslog(buf); // show lines with matches | |
200 } | |
201 write(p); | |
202 } | |
203 } | |
204 } | |
205 | |
206 | |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
207 bool CONTEXT::check_wflog_time() { |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
208 time_t now_time = time(NULL); |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
209 tm now_tm; |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
210 localtime_r(&now_time, &now_tm); |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
211 return (open_time + period < now_time) || \ |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
212 ((trigger == token_hourly) && (now_tm.tm_hour != open_tm.tm_hour)) || \ |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
213 ((trigger == token_daily) && (now_tm.tm_wday != open_tm.tm_wday)) || \ |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
214 ((trigger == token_weekly) && (now_tm.tm_wday != open_tm.tm_wday) && (now_tm.tm_wday == 0)); |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
215 } |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
216 |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
217 |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
218 void CONTEXT::check_wflog() { |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
219 if ((fdo != -1) && check_wflog_time()) { |
0 | 220 closeo(); |
221 // rename previous wflog html output files | |
222 char buf[maxlen]; | |
223 char fn1[maxlen]; | |
224 char fn2[maxlen]; | |
225 for (int i=1; i<versions; i++) { | |
226 int j = versions - 1 - i; | |
227 int k = j + 1; | |
228 snprintf(fn1, maxlen, output, j); | |
229 snprintf(fn2, maxlen, output, k); | |
230 snprintf(buf, maxlen, "mv \"%s\" \"%s\"", fn1, fn2); | |
231 system(buf); | |
232 } | |
233 snprintf(buf, maxlen, wflogs, fn1); | |
234 system(buf); | |
235 openo(false); | |
236 } | |
237 } | |
238 | |
239 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
240 void CONTEXT::free_all() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
241 regfree(&re); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
242 close(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
243 closeo(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
244 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
245 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
246 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
247 //////////////////////////////////////////////// |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
248 // |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
249 CONFIG::CONFIG() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
250 reference_count = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
251 generation = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
252 load_time = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
253 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
254 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
255 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
256 CONFIG::~CONFIG() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
257 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
258 CONTEXT *c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
259 delete c; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
260 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
261 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
262 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
263 |
0 | 264 void CONFIG::dump() { |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
265 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
266 CONTEXTP c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
267 c->dump(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
268 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
269 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
270 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
271 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
272 bool CONFIG::read() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
273 bool rc = false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
274 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
275 CONTEXT *c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
276 rc |= c->read(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
277 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
278 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
279 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
280 void CONFIG::sleep(int duration, time_t &previous) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
281 ::sleep(duration); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
282 time_t now = time(NULL); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
283 previous = now; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
284 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
285 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
286 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
287 void CONFIG::free_all() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
288 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
289 CONTEXT *c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
290 c->free_all(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
291 } |
0 | 292 } |
293 | |
294 | |
295 //////////////////////////////////////////////// | |
296 // helper to discard the strings held by a string_set | |
297 // | |
298 void discard(string_set &s) { | |
299 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
300 free((void*)*i); | |
301 } | |
302 s.clear(); | |
303 } | |
304 | |
305 | |
306 //////////////////////////////////////////////// | |
307 // helper to register a string in a string set | |
308 // | |
309 const char* register_string(string_set &s, const char *name) { | |
310 string_set::const_iterator i = s.find(name); | |
311 if (i != s.end()) return *i; | |
312 char *x = strdup(name); | |
313 s.insert(x); | |
314 return x; | |
315 } | |
316 | |
317 | |
318 //////////////////////////////////////////////// | |
319 // register a global string | |
320 // | |
321 const char* register_string(const char *name) { | |
322 return register_string(all_strings, name); | |
323 } | |
324 | |
325 | |
326 //////////////////////////////////////////////// | |
327 // clear all global strings, helper for valgrind checking | |
328 // | |
329 void clear_strings() { | |
330 discard(all_strings); | |
331 } | |
332 | |
333 | |
334 //////////////////////////////////////////////// | |
335 // | |
336 bool tsa(TOKEN &tok, const char *token); | |
337 bool tsa(TOKEN &tok, const char *token) { | |
338 const char *have = tok.next(); | |
339 if (have == token) return true; | |
340 tok.token_error(token, have); | |
341 return false; | |
342 } | |
343 | |
344 | |
345 //////////////////////////////////////////////// | |
346 // | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
347 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
348 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
349 const char *name = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
350 if (!tsa(tok, token_lbrace)) return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
351 CONTEXTP con = new CONTEXT(name); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
352 con->set_token(tok); |
0 | 353 while (true) { |
354 const char *have = tok.next(); | |
355 if (!have) break; | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
356 if (have == token_rbrace) break; // done |
0 | 357 if (have == token_period) { |
358 have = tok.next(); | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
359 con->set_period(atoi(have)); |
0 | 360 if (!tsa(tok, token_semi)) return false; |
361 } | |
362 else if (have == token_versions) { | |
363 have = tok.next(); | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
364 con->set_versions(atoi(have)); |
0 | 365 if (!tsa(tok, token_semi)) return false; |
366 } | |
367 else if (have == token_output) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
368 con->set_output(tok.next()); |
0 | 369 if (!tsa(tok, token_semi)) return false; |
370 } | |
371 else if (have == token_tempin) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
372 con->set_tempin(tok.next()); |
0 | 373 if (!tsa(tok, token_semi)) return false; |
374 } | |
375 else if (have == token_wflogs) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
376 con->set_wflogs(tok.next()); |
0 | 377 if (!tsa(tok, token_semi)) return false; |
378 } | |
379 else if (have == token_file) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
380 con->set_file(tok.next()); |
0 | 381 if (!tsa(tok, token_semi)) return false; |
382 } | |
383 else if (have == token_pattern) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
384 con->pattern = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
385 int rc = regcomp(&con->re, con->pattern, REG_ICASE | REG_EXTENDED); |
0 | 386 if (rc) { |
387 char bu[maxlen]; | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
388 regerror(rc, &con->re, bu, maxlen); |
0 | 389 char buf[maxlen]; |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
390 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", con->pattern, bu); |
0 | 391 tok.token_error(buf); |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
392 con->pattern = NULL; |
0 | 393 } |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
394 if (!tsa(tok, token_semi)) return false; |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
395 } |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
396 else if (have == token_trigger) { |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
397 have = tok.next(); |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
398 if ((have == token_hourly) || (have == token_daily) || (have == token_weekly)) { |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
399 con->trigger = have; |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
400 } |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
401 else { |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
402 tok.token_error("hourly/daily/weekly", have); |
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
403 } |
0 | 404 if (!tsa(tok, token_semi)) return false; |
405 } | |
406 else { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
407 tok.token_error("period/versions/output/tempin/wflogs/file/pattern", have); |
0 | 408 return false; |
409 } | |
410 } | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
411 if (!tsa(tok, token_semi)) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
412 delete con; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
413 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
414 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
415 dc.add_context(con); |
0 | 416 return true; |
417 } | |
418 | |
419 | |
420 //////////////////////////////////////////////// | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
421 // parse a config file |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
422 // |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
423 bool load_conf(CONFIG &dc, const char *fn) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
424 int count = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
425 TOKEN tok(fn, &dc.config_files); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
426 while (true) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
427 const char *have = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
428 if (!have) break; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
429 if (have == token_context) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
430 if (!parse_context(tok, dc, NULL)) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
431 tok.token_error("load_conf() failed to parse context"); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
432 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
433 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
434 else count++; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
435 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
436 else { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
437 tok.token_error(token_context, have); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
438 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
439 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
440 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
441 tok.token_error("load_conf() found %d contexts in %s", count, fn); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
442 return (!dc.contexts.empty()); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
443 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
444 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
445 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
446 //////////////////////////////////////////////// |
0 | 447 // init the tokens |
448 // | |
449 void token_init() { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
450 token_context = register_string("context"); |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
451 token_daily = register_string("daily"); |
0 | 452 token_file = register_string("file"); |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
453 token_hourly = register_string("hourly"); |
0 | 454 token_include = register_string("include"); |
455 token_lbrace = register_string("{"); | |
456 token_output = register_string("output"); | |
457 token_pattern = register_string("pattern"); | |
458 token_period = register_string("period"); | |
459 token_rbrace = register_string("}"); | |
460 token_semi = register_string(";"); | |
461 token_tempin = register_string("tempin"); | |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
462 token_trigger = register_string("trigger"); |
0 | 463 token_versions = register_string("versions"); |
4
37eace15ef87
allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
464 token_weekly = register_string("weekly"); |
0 | 465 token_wflogs = register_string("wflogs"); |
466 } |