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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 the GPL version 3 or any later version at your choice available at
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 http://www.gnu.org/licenses/gpl-3.0.txt
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 */
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 #include "includes.h"
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 #include <fcntl.h>
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 #include <sys/socket.h>
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 #include <netinet/in.h>
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 #include <arpa/inet.h>
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 #include <netdb.h>
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 const char *token_include;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 const char *token_lbrace;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 const char *token_output;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 const char *token_pattern;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 const char *token_period;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 const char *token_rbrace;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 const char *token_semi;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 const char *token_wflogs;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 fd = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 len = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 fdo = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 period = 120;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 output = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 tempin = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 wflogs = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 fn = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 pattern = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 if (fdo == -1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 if (msg) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 char buf[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 snprintf(buf, sizeof(buf), "wflogs tempin file %s not writeable", tempin);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 tokp->token_error(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 fd = ::open(fn, O_RDONLY);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 len = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 if (fd == -1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 if (msg) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 char buf[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 snprintf(buf, sizeof(buf), "syslog file %s not readable", fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 tokp->token_error(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 else {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 if (debug_syslog > 1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 snprintf(buf, sizeof(buf), "syslog file %s opened", fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 my_syslog(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 if (msg) lseek(fd, 0, SEEK_END);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 if (fstat(fd, &openfdstat)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113 close();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119 int oldflags = fcntl(fd, F_GETFD, 0);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 if (oldflags >= 0) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121 fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 // p points to \0 at end of buf, may be destroyed
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129 if (failedo()) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133 *p = '\n';
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134 ::write(fdo, buf, p-buf+1);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
138 bool CONTEXT::read() {
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139 if (failed()) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140 open(false);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141 if (failed()) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143 int n = ::read(fd, buf+len, buflen-len);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
144 bool have = (n > 0);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
145 if (have) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146 len += n;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
147 while (true) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
148 char *p = (char*)memchr(buf, '\n', len);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
149 if (!p) break;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
150 n = p-buf;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
151 *p = '\0';
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
152 process(p); // process null terminated string, but may destroy the null
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
153 len -= n+1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
154 memmove(buf, p+1, len);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
155 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
156 // no <lf> in a full buffer
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
157 if (len == buflen) len = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
158 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
159 else {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
160 // check for file close
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
161 struct stat filenamest;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
162 if (0 == stat(fn, &filenamest)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
163 if ((filenamest.st_dev != openfdstat.st_dev) ||
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
164 (filenamest.st_ino != openfdstat.st_ino)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
165 close();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
166 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
167 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
168 else {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
169 // filename no longer exists
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
170 close();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
171 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
172 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
173 check_wflog();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
174 return have;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
175 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
176
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
177
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
178 void CONTEXT::closeo() {
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
179 if (fdo != -1) ::close(fdo);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
180 fdo = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
181 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
182
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
183
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
184 void CONTEXT::close() {
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
185 if (debug_syslog > 1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
186 snprintf(buf, sizeof(buf), "syslog file %s closed", fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
187 my_syslog(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
188 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
189 if (fd != -1) ::close(fd);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
190 fd = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
191 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
192
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
195 // p points to \0 at end of buf, may be destroyed
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
196 if (pattern) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
197 if (0 == regexec(&re, buf, 0, NULL, 0)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
198 if (debug_syslog > 2) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
199 my_syslog(buf); // show lines with matches
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
200 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
201 write(p);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
202 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
203 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
204 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
205
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
220 closeo();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
221 // rename previous wflog html output files
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
222 char buf[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
223 char fn1[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
224 char fn2[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
225 for (int i=1; i<versions; i++) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
226 int j = versions - 1 - i;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
227 int k = j + 1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
228 snprintf(fn1, maxlen, output, j);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
229 snprintf(fn2, maxlen, output, k);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
230 snprintf(buf, maxlen, "mv \"%s\" \"%s\"", fn1, fn2);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
231 system(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
232 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
233 snprintf(buf, maxlen, wflogs, fn1);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
234 system(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
235 openo(false);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
236 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
237 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
238
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
293 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
294
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
295
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
296 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
297 // helper to discard the strings held by a string_set
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
298 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
299 void discard(string_set &s) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
300 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
301 free((void*)*i);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
302 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
303 s.clear();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
304 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
305
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
306
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
307 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
308 // helper to register a string in a string set
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
309 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
310 const char* register_string(string_set &s, const char *name) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
311 string_set::const_iterator i = s.find(name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
312 if (i != s.end()) return *i;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
313 char *x = strdup(name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
314 s.insert(x);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
315 return x;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
316 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
317
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
318
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
319 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
320 // register a global string
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
321 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
322 const char* register_string(const char *name) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
323 return register_string(all_strings, name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
324 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
325
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
326
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
327 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
328 // clear all global strings, helper for valgrind checking
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
329 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
330 void clear_strings() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
331 discard(all_strings);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
332 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
333
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
334
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
335 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
336 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
337 bool tsa(TOKEN &tok, const char *token);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
338 bool tsa(TOKEN &tok, const char *token) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
339 const char *have = tok.next();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
340 if (have == token) return true;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
341 tok.token_error(token, have);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
342 return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
343 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
344
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
345
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
346 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
354 while (true) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
355 const char *have = tok.next();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
358 if (have == token_period) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
361 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
362 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
363 else if (have == token_versions) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
366 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
367 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
370 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
371 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
374 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
375 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
378 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
379 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
382 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
383 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
387 if (rc) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
405 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
406 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
409 return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
410 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
417 return true;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
418 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
419
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
420
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
448 // init the tokens
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
449 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
455 token_include = register_string("include");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
456 token_lbrace = register_string("{");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
457 token_output = register_string("output");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
458 token_pattern = register_string("pattern");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
459 token_period = register_string("period");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
460 token_rbrace = register_string("}");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
461 token_semi = register_string(";");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
466 token_wflogs = register_string("wflogs");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
467 }