Mercurial > wflogs-daemon
annotate src/wflogs-config.cpp @ 12:f6220c098155 default tip
Added tag stable-1-0-1 for changeset 1b7813f588ba
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 17 May 2013 13:35:21 -0700 |
parents | 13204eef1a0a |
children |
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 | |
8
306059d0f677
fix missing return statement
Carl Byington <carl@five-ten-sg.com>
parents:
6
diff
changeset
|
127 void CONTEXT::write(char *p) { |
0 | 128 // p points to \0 at end of buf, may be destroyed |
129 if (failedo()) { | |
130 openo(false); | |
9
13204eef1a0a
fix missing return statement
Carl Byington <carl@five-ten-sg.com>
parents:
8
diff
changeset
|
131 if (failedo()) return; |
0 | 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 } |
8
306059d0f677
fix missing return statement
Carl Byington <carl@five-ten-sg.com>
parents:
6
diff
changeset
|
278 return rc; |
2
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 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
281 void CONFIG::sleep(int duration, time_t &previous) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
282 ::sleep(duration); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
283 time_t now = time(NULL); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
284 previous = now; |
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 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
288 void CONFIG::free_all() { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
289 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
|
290 CONTEXT *c = *i; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
291 c->free_all(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
292 } |
0 | 293 } |
294 | |
295 | |
296 //////////////////////////////////////////////// | |
297 // helper to discard the strings held by a string_set | |
298 // | |
299 void discard(string_set &s) { | |
300 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
301 free((void*)*i); | |
302 } | |
303 s.clear(); | |
304 } | |
305 | |
306 | |
307 //////////////////////////////////////////////// | |
308 // helper to register a string in a string set | |
309 // | |
310 const char* register_string(string_set &s, const char *name) { | |
311 string_set::const_iterator i = s.find(name); | |
312 if (i != s.end()) return *i; | |
313 char *x = strdup(name); | |
314 s.insert(x); | |
315 return x; | |
316 } | |
317 | |
318 | |
319 //////////////////////////////////////////////// | |
320 // register a global string | |
321 // | |
322 const char* register_string(const char *name) { | |
323 return register_string(all_strings, name); | |
324 } | |
325 | |
326 | |
327 //////////////////////////////////////////////// | |
328 // clear all global strings, helper for valgrind checking | |
329 // | |
330 void clear_strings() { | |
331 discard(all_strings); | |
332 } | |
333 | |
334 | |
335 //////////////////////////////////////////////// | |
336 // | |
337 bool tsa(TOKEN &tok, const char *token); | |
338 bool tsa(TOKEN &tok, const char *token) { | |
339 const char *have = tok.next(); | |
340 if (have == token) return true; | |
341 tok.token_error(token, have); | |
342 return false; | |
343 } | |
344 | |
345 | |
346 //////////////////////////////////////////////// | |
347 // | |
2
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 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
|
350 const char *name = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
351 if (!tsa(tok, token_lbrace)) return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
352 CONTEXTP con = new CONTEXT(name); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
353 con->set_token(tok); |
0 | 354 while (true) { |
355 const char *have = tok.next(); | |
356 if (!have) break; | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
357 if (have == token_rbrace) break; // done |
0 | 358 if (have == token_period) { |
359 have = tok.next(); | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
360 con->set_period(atoi(have)); |
0 | 361 if (!tsa(tok, token_semi)) return false; |
362 } | |
363 else if (have == token_versions) { | |
364 have = tok.next(); | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
365 con->set_versions(atoi(have)); |
0 | 366 if (!tsa(tok, token_semi)) return false; |
367 } | |
368 else if (have == token_output) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
369 con->set_output(tok.next()); |
0 | 370 if (!tsa(tok, token_semi)) return false; |
371 } | |
372 else if (have == token_tempin) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
373 con->set_tempin(tok.next()); |
0 | 374 if (!tsa(tok, token_semi)) return false; |
375 } | |
376 else if (have == token_wflogs) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
377 con->set_wflogs(tok.next()); |
0 | 378 if (!tsa(tok, token_semi)) return false; |
379 } | |
380 else if (have == token_file) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
381 con->set_file(tok.next()); |
0 | 382 if (!tsa(tok, token_semi)) return false; |
383 } | |
384 else if (have == token_pattern) { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
385 con->pattern = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
386 int rc = regcomp(&con->re, con->pattern, REG_ICASE | REG_EXTENDED); |
0 | 387 if (rc) { |
388 char bu[maxlen]; | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
389 regerror(rc, &con->re, bu, maxlen); |
0 | 390 char buf[maxlen]; |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
391 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", con->pattern, bu); |
0 | 392 tok.token_error(buf); |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
393 con->pattern = NULL; |
0 | 394 } |
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
|
395 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
|
396 } |
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 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
|
398 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
|
399 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
|
400 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
|
401 } |
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 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
|
403 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
|
404 } |
0 | 405 if (!tsa(tok, token_semi)) return false; |
406 } | |
407 else { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
408 tok.token_error("period/versions/output/tempin/wflogs/file/pattern", have); |
0 | 409 return false; |
410 } | |
411 } | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
412 if (!tsa(tok, token_semi)) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
413 delete con; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
414 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
415 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
416 dc.add_context(con); |
0 | 417 return true; |
418 } | |
419 | |
420 | |
421 //////////////////////////////////////////////// | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
422 // parse a config file |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
423 // |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
424 bool load_conf(CONFIG &dc, const char *fn) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
425 int count = 0; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
426 TOKEN tok(fn, &dc.config_files); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
427 while (true) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
428 const char *have = tok.next(); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
429 if (!have) break; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
430 if (have == token_context) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
431 if (!parse_context(tok, dc, NULL)) { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
432 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
|
433 return false; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
434 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
435 else count++; |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
436 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
437 else { |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
438 tok.token_error(token_context, have); |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
439 return false; |
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 } |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
442 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
|
443 return (!dc.contexts.empty()); |
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 |
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
447 //////////////////////////////////////////////// |
0 | 448 // init the tokens |
449 // | |
450 void token_init() { | |
2
400b1de6e1c6
allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
451 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
|
452 token_daily = register_string("daily"); |
0 | 453 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
|
454 token_hourly = register_string("hourly"); |
0 | 455 token_include = register_string("include"); |
456 token_lbrace = register_string("{"); | |
457 token_output = register_string("output"); | |
458 token_pattern = register_string("pattern"); | |
459 token_period = register_string("period"); | |
460 token_rbrace = register_string("}"); | |
461 token_semi = register_string(";"); | |
462 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
|
463 token_trigger = register_string("trigger"); |
0 | 464 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
|
465 token_weekly = register_string("weekly"); |
0 | 466 token_wflogs = register_string("wflogs"); |
467 } |