Mercurial > wflogs-daemon
annotate src/wflogs-config.cpp @ 5:efe0b291233a
Added tag stable-1-0-1 for changeset 37eace15ef87
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 17 May 2013 12:06:21 -0700 |
parents | 37eace15ef87 |
children | a043b7d7269c |
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); |
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
|
80 fdo = ::open(tempin, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
0 | 81 if (fdo == -1) { |
82 if (msg) { | |
83 char buf[maxlen]; | |
84 snprintf(buf, sizeof(buf), "wflogs tempin file %s not writeable", tempin); | |
85 tokp->token_error(buf); | |
86 } | |
87 } | |
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
|
88 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
|
89 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
|
90 } |
0 | 91 } |
92 | |
93 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
94 void CONTEXT::open(bool msg) { |
0 | 95 fd = ::open(fn, O_RDONLY); |
96 len = 0; | |
97 if (fd == -1) { | |
98 if (msg) { | |
99 char buf[maxlen]; | |
100 snprintf(buf, sizeof(buf), "syslog file %s not readable", fn); | |
101 tokp->token_error(buf); | |
102 } | |
103 } | |
104 else { | |
105 if (debug_syslog > 1) { | |
106 snprintf(buf, sizeof(buf), "syslog file %s opened", fn); | |
107 my_syslog(buf); | |
108 } | |
109 if (msg) lseek(fd, 0, SEEK_END); | |
110 if (fstat(fd, &openfdstat)) { | |
111 close(); | |
112 snprintf(buf, sizeof(buf), "syslog file %s cannot stat after open", fn); | |
113 tokp->token_error(buf); | |
114 } | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
115 // 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
|
116 // won't have access to it. |
0 | 117 int oldflags = fcntl(fd, F_GETFD, 0); |
118 if (oldflags >= 0) { | |
119 fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC); | |
120 } | |
121 } | |
122 } | |
123 | |
124 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
125 bool CONTEXT::write(char *p) { |
0 | 126 // p points to \0 at end of buf, may be destroyed |
127 if (failedo()) { | |
128 openo(false); | |
129 if (failedo()) return false; | |
130 } | |
131 *p = '\n'; | |
132 ::write(fdo, buf, p-buf+1); | |
133 } | |
134 | |
135 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
136 bool CONTEXT::read() { |
0 | 137 if (failed()) { |
138 open(false); | |
139 if (failed()) return false; | |
140 } | |
141 int n = ::read(fd, buf+len, buflen-len); | |
142 bool have = (n > 0); | |
143 if (have) { | |
144 len += n; | |
145 while (true) { | |
146 char *p = (char*)memchr(buf, '\n', len); | |
147 if (!p) break; | |
148 n = p-buf; | |
149 *p = '\0'; | |
150 process(p); // process null terminated string, but may destroy the null | |
151 len -= n+1; | |
152 memmove(buf, p+1, len); | |
153 } | |
154 // no <lf> in a full buffer | |
155 if (len == buflen) len = 0; | |
156 } | |
157 else { | |
158 // check for file close | |
159 struct stat filenamest; | |
160 if (0 == stat(fn, &filenamest)) { | |
161 if ((filenamest.st_dev != openfdstat.st_dev) || | |
162 (filenamest.st_ino != openfdstat.st_ino)) { | |
163 close(); | |
164 } | |
165 } | |
166 else { | |
167 // filename no longer exists | |
168 close(); | |
169 } | |
170 } | |
171 check_wflog(); | |
172 return have; | |
173 } | |
174 | |
175 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
176 void CONTEXT::closeo() { |
0 | 177 if (fdo != -1) ::close(fdo); |
178 fdo = -1; | |
179 } | |
180 | |
181 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
182 void CONTEXT::close() { |
0 | 183 if (debug_syslog > 1) { |
184 snprintf(buf, sizeof(buf), "syslog file %s closed", fn); | |
185 my_syslog(buf); | |
186 } | |
187 if (fd != -1) ::close(fd); | |
188 fd = -1; | |
189 } | |
190 | |
191 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
192 void CONTEXT::process(char *p) { |
0 | 193 // p points to \0 at end of buf, may be destroyed |
194 if (pattern) { | |
195 if (0 == regexec(&re, buf, 0, NULL, 0)) { | |
196 if (debug_syslog > 2) { | |
197 my_syslog(buf); // show lines with matches | |
198 } | |
199 write(p); | |
200 } | |
201 } | |
202 } | |
203 | |
204 | |
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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 ((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
|
211 ((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
|
212 ((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
|
213 } |
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 |
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 |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
216 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
|
217 if ((fdo != -1) && check_wflog_time()) { |
0 | 218 closeo(); |
219 // rename previous wflog html output files | |
220 char buf[maxlen]; | |
221 char fn1[maxlen]; | |
222 char fn2[maxlen]; | |
223 for (int i=1; i<versions; i++) { | |
224 int j = versions - 1 - i; | |
225 int k = j + 1; | |
226 snprintf(fn1, maxlen, output, j); | |
227 snprintf(fn2, maxlen, output, k); | |
228 snprintf(buf, maxlen, "mv \"%s\" \"%s\"", fn1, fn2); | |
229 system(buf); | |
230 } | |
231 snprintf(buf, maxlen, wflogs, fn1); | |
232 system(buf); | |
233 openo(false); | |
234 } | |
235 } | |
236 | |
237 | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
238 void CONTEXT::free_all() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
239 regfree(&re); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
240 close(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
241 closeo(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
242 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
243 |
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 CONFIG::CONFIG() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
248 reference_count = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
249 generation = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
250 load_time = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
251 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
252 |
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 CONFIG::~CONFIG() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
255 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
|
256 CONTEXT *c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
257 delete c; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
258 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
259 } |
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 |
0 | 262 void CONFIG::dump() { |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
263 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
|
264 CONTEXTP c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
265 c->dump(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
266 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
267 } |
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 bool CONFIG::read() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
271 bool rc = false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
272 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
|
273 CONTEXT *c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
274 rc |= c->read(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
275 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
276 } |
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 void CONFIG::sleep(int duration, time_t &previous) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
279 ::sleep(duration); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
280 time_t now = time(NULL); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
281 previous = now; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
282 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
283 |
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 void CONFIG::free_all() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
286 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
|
287 CONTEXT *c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
288 c->free_all(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
289 } |
0 | 290 } |
291 | |
292 | |
293 //////////////////////////////////////////////// | |
294 // helper to discard the strings held by a string_set | |
295 // | |
296 void discard(string_set &s) { | |
297 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
298 free((void*)*i); | |
299 } | |
300 s.clear(); | |
301 } | |
302 | |
303 | |
304 //////////////////////////////////////////////// | |
305 // helper to register a string in a string set | |
306 // | |
307 const char* register_string(string_set &s, const char *name) { | |
308 string_set::const_iterator i = s.find(name); | |
309 if (i != s.end()) return *i; | |
310 char *x = strdup(name); | |
311 s.insert(x); | |
312 return x; | |
313 } | |
314 | |
315 | |
316 //////////////////////////////////////////////// | |
317 // register a global string | |
318 // | |
319 const char* register_string(const char *name) { | |
320 return register_string(all_strings, name); | |
321 } | |
322 | |
323 | |
324 //////////////////////////////////////////////// | |
325 // clear all global strings, helper for valgrind checking | |
326 // | |
327 void clear_strings() { | |
328 discard(all_strings); | |
329 } | |
330 | |
331 | |
332 //////////////////////////////////////////////// | |
333 // | |
334 bool tsa(TOKEN &tok, const char *token); | |
335 bool tsa(TOKEN &tok, const char *token) { | |
336 const char *have = tok.next(); | |
337 if (have == token) return true; | |
338 tok.token_error(token, have); | |
339 return false; | |
340 } | |
341 | |
342 | |
343 //////////////////////////////////////////////// | |
344 // | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
345 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
|
346 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
|
347 const char *name = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
348 if (!tsa(tok, token_lbrace)) return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
349 CONTEXTP con = new CONTEXT(name); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
350 con->set_token(tok); |
0 | 351 while (true) { |
352 const char *have = tok.next(); | |
353 if (!have) break; | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
354 if (have == token_rbrace) break; // done |
0 | 355 if (have == token_period) { |
356 have = tok.next(); | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
357 con->set_period(atoi(have)); |
0 | 358 if (!tsa(tok, token_semi)) return false; |
359 } | |
360 else if (have == token_versions) { | |
361 have = tok.next(); | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
362 con->set_versions(atoi(have)); |
0 | 363 if (!tsa(tok, token_semi)) return false; |
364 } | |
365 else if (have == token_output) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
366 con->set_output(tok.next()); |
0 | 367 if (!tsa(tok, token_semi)) return false; |
368 } | |
369 else if (have == token_tempin) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
370 con->set_tempin(tok.next()); |
0 | 371 if (!tsa(tok, token_semi)) return false; |
372 } | |
373 else if (have == token_wflogs) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
374 con->set_wflogs(tok.next()); |
0 | 375 if (!tsa(tok, token_semi)) return false; |
376 } | |
377 else if (have == token_file) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
378 con->set_file(tok.next()); |
0 | 379 if (!tsa(tok, token_semi)) return false; |
380 } | |
381 else if (have == token_pattern) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
382 con->pattern = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
383 int rc = regcomp(&con->re, con->pattern, REG_ICASE | REG_EXTENDED); |
0 | 384 if (rc) { |
385 char bu[maxlen]; | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
386 regerror(rc, &con->re, bu, maxlen); |
0 | 387 char buf[maxlen]; |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
388 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", con->pattern, bu); |
0 | 389 tok.token_error(buf); |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
390 con->pattern = NULL; |
0 | 391 } |
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
|
392 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
|
393 } |
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 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
|
395 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
|
396 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
|
397 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
|
398 } |
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 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
|
400 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
|
401 } |
0 | 402 if (!tsa(tok, token_semi)) return false; |
403 } | |
404 else { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
405 tok.token_error("period/versions/output/tempin/wflogs/file/pattern", have); |
0 | 406 return false; |
407 } | |
408 } | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
409 if (!tsa(tok, token_semi)) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
410 delete con; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
411 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
412 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
413 dc.add_context(con); |
0 | 414 return true; |
415 } | |
416 | |
417 | |
418 //////////////////////////////////////////////// | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
419 // parse a config file |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
420 // |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
421 bool load_conf(CONFIG &dc, const char *fn) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
422 int count = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
423 TOKEN tok(fn, &dc.config_files); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
424 while (true) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
425 const char *have = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
426 if (!have) break; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
427 if (have == token_context) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
428 if (!parse_context(tok, dc, NULL)) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
429 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
|
430 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
431 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
432 else count++; |
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 { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
435 tok.token_error(token_context, have); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
436 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
437 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
438 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
439 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
|
440 return (!dc.contexts.empty()); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
441 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
442 |
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 //////////////////////////////////////////////// |
0 | 445 // init the tokens |
446 // | |
447 void token_init() { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
448 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
|
449 token_daily = register_string("daily"); |
0 | 450 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
|
451 token_hourly = register_string("hourly"); |
0 | 452 token_include = register_string("include"); |
453 token_lbrace = register_string("{"); | |
454 token_output = register_string("output"); | |
455 token_pattern = register_string("pattern"); | |
456 token_period = register_string("period"); | |
457 token_rbrace = register_string("}"); | |
458 token_semi = register_string(";"); | |
459 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
|
460 token_trigger = register_string("trigger"); |
0 | 461 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
|
462 token_weekly = register_string("weekly"); |
0 | 463 token_wflogs = register_string("wflogs"); |
464 } |