annotate src/wflogs-config.cpp @ 0:0aa1171aebd2 stable-1-0-0

initial version
author Carl Byington <carl@five-ten-sg.com>
date Wed, 15 May 2013 13:15:59 -0700
parents
children 400b1de6e1c6
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
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>
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 const char *token_context;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 const char *token_file;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 const char *token_include;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 const char *token_lbrace;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 const char *token_output;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 const char *token_pattern;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 const char *token_period;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 const char *token_rbrace;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 const char *token_semi;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 const char *token_tempin;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 const char *token_versions;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 const char *token_wflogs;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 string_set all_strings;// owns all the strings, only modified by the config loader thread
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 const int maxlen = 1000; // used for snprintf buffers
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 CONFIG::CONFIG() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 reference_count = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 fd = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 len = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 fdo = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 generation = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 load_time = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 period = 120;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 versions = 3;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 output = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 tempin = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 wflogs = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 fn = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 pattern = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 CONFIG::~CONFIG() {
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 void CONFIG::sleep(int duration, time_t &previous) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 ::sleep(duration);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 time_t now = time(NULL);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 previous = now;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 void CONFIG::free_all() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 regfree(&re);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 close();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 closeo();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 void CONFIG::openo(bool msg) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 open_time = time(NULL);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 fdo = ::creat(tempin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 if (fdo == -1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 if (msg) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 char buf[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78 snprintf(buf, sizeof(buf), "wflogs tempin file %s not writeable", tempin);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 tokp->token_error(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 void CONFIG::open(bool msg) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 fd = ::open(fn, O_RDONLY);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 len = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 if (fd == -1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 if (msg) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 char buf[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 snprintf(buf, sizeof(buf), "syslog file %s not readable", fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 tokp->token_error(buf);
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 else {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 if (debug_syslog > 1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 snprintf(buf, sizeof(buf), "syslog file %s opened", fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 my_syslog(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 if (msg) lseek(fd, 0, SEEK_END);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 if (fstat(fd, &openfdstat)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 close();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 snprintf(buf, sizeof(buf), "syslog file %s cannot stat after open", fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 tokp->token_error(buf);
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 // specify that this fd gets closed on exec, so that selinux
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 // won't complain about iptables trying to read log files.
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 int oldflags = fcntl(fd, F_GETFD, 0);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 if (oldflags >= 0) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 bool CONFIG::write(char *p) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 // p points to \0 at end of buf, may be destroyed
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 if (failedo()) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119 openo(false);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 if (failedo()) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122 *p = '\n';
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123 ::write(fdo, buf, p-buf+1);
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
127 bool CONFIG::read() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 if (failed()) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129 open(false);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130 if (failed()) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 int n = ::read(fd, buf+len, buflen-len);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133 bool have = (n > 0);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134 if (have) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135 len += n;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136 while (true) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137 char *p = (char*)memchr(buf, '\n', len);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
138 if (!p) break;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139 n = p-buf;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140 *p = '\0';
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141 process(p); // process null terminated string, but may destroy the null
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142 len -= n+1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143 memmove(buf, p+1, len);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
144 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
145 // no <lf> in a full buffer
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146 if (len == buflen) len = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
147 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
148 else {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
149 // check for file close
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
150 struct stat filenamest;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
151 if (0 == stat(fn, &filenamest)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
152 if ((filenamest.st_dev != openfdstat.st_dev) ||
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
153 (filenamest.st_ino != openfdstat.st_ino)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
154 close();
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 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
157 else {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
158 // filename no longer exists
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
159 close();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
160 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
161 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
162 check_wflog();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
163 return have;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
164 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
165
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 void CONFIG::closeo() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
168 if (fdo != -1) ::close(fdo);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
169 fdo = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
170 }
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 void CONFIG::close() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
174 if (debug_syslog > 1) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
175 snprintf(buf, sizeof(buf), "syslog file %s closed", fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
176 my_syslog(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
177 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
178 if (fd != -1) ::close(fd);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
179 fd = -1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
180 }
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 void CONFIG::process(char *p) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
184 // p points to \0 at end of buf, may be destroyed
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
185 if (pattern) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
186 if (0 == regexec(&re, buf, 0, NULL, 0)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
187 if (debug_syslog > 2) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
188 my_syslog(buf); // show lines with matches
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
189 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
190 write(p);
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 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
194
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
195
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
196 void CONFIG::check_wflog() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
197 time_t now = time(NULL);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
198 if ((fdo != -1) && (open_time + period < now)) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
199 closeo();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
200 // rename previous wflog html output files
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
201 char buf[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
202 char fn1[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
203 char fn2[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
204 for (int i=1; i<versions; i++) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
205 int j = versions - 1 - i;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
206 int k = j + 1;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
207 snprintf(fn1, maxlen, output, j);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
208 snprintf(fn2, maxlen, output, k);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
209 snprintf(buf, maxlen, "mv \"%s\" \"%s\"", fn1, fn2);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
210 system(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
211 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
212 snprintf(buf, maxlen, wflogs, fn1);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
213 system(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
214 openo(false);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
215 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
216 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
217
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
218
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
219 void CONFIG::dump() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
220 int level = 0;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
221 char indent[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
222 int i = min(maxlen-1, level*4);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
223 memset(indent, ' ', i);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
224 indent[i] = '\0';
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
225 printf("%s period %d; \n", indent, period);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
226 printf("%s versions %d; \n", indent, versions);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
227 printf("%s output \"%s\";\n", indent, output);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
228 printf("%s tempin \"%s\";\n", indent, tempin);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
229 printf("%s wflogs \"%s\";\n", indent, wflogs);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
230 printf("%s file \"%s\";\n", indent, fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
231 printf("%s pattern \"%s\";\n", indent, pattern);
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
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
234
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
235 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
236 // helper to discard the strings held by a string_set
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 void discard(string_set &s) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
239 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
240 free((void*)*i);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
241 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
242 s.clear();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
243 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
244
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
245
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
246 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
247 // helper to register a string in a string set
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
248 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
249 const char* register_string(string_set &s, const char *name) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
250 string_set::const_iterator i = s.find(name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
251 if (i != s.end()) return *i;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
252 char *x = strdup(name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
253 s.insert(x);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
254 return x;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
255 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
256
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
257
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
258 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
259 // register a global string
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
260 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
261 const char* register_string(const char *name) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
262 return register_string(all_strings, name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
263 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
264
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
265
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
266 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
267 // clear all global strings, helper for valgrind checking
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
268 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
269 void clear_strings() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
270 discard(all_strings);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
271 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
272
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
273
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
274 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
275 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
276 bool tsa(TOKEN &tok, const char *token);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
277 bool tsa(TOKEN &tok, const char *token) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
278 const char *have = tok.next();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
279 if (have == token) return true;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
280 tok.token_error(token, have);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
281 return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
282 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
283
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
284
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
285 ////////////////////////////////////////////////
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
286 // parse a config file
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
287 //
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
288 bool load_conf(CONFIG &dc, const char *fn) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
289 TOKEN tok(fn, &dc.config_files);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
290 dc.set_token(tok);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
291 while (true) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
292 const char *have = tok.next();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
293 if (!have) break;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
294 if (have == token_period) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
295 have = tok.next();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
296 dc.set_period(atoi(have));
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
297 if (!tsa(tok, token_semi)) return false;
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 else if (have == token_versions) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
300 have = tok.next();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
301 dc.set_versions(atoi(have));
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
302 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
303 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
304 else if (have == token_output) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
305 dc.set_output(tok.next());
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
306 if (!tsa(tok, token_semi)) return false;
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 else if (have == token_tempin) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
309 dc.set_tempin(tok.next());
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
310 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
311 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
312 else if (have == token_wflogs) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
313 dc.set_wflogs(tok.next());
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
314 if (!tsa(tok, token_semi)) return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
315 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
316 else if (have == token_file) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
317 dc.set_file(tok.next());
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
318 if (!tsa(tok, token_semi)) return false;
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 else if (have == token_pattern) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
321 dc.pattern = tok.next();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
322 int rc = regcomp(&dc.re, dc.pattern, REG_ICASE | REG_EXTENDED);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
323 if (rc) {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
324 char bu[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
325 regerror(rc, &dc.re, bu, maxlen);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
326 char buf[maxlen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
327 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", dc.pattern, bu);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
328 tok.token_error(buf);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
329 dc.pattern = NULL;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
330 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
331
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
332 if (!tsa(tok, token_semi)) return false;
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 else {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
335 tok.token_error("statement", have);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
336 return false;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
337 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
338 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
339 return true;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
340 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
341
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
342
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 // init the tokens
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 void token_init() {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
347 token_file = register_string("file");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
348 token_include = register_string("include");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
349 token_lbrace = register_string("{");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
350 token_output = register_string("output");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
351 token_pattern = register_string("pattern");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
352 token_period = register_string("period");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
353 token_rbrace = register_string("}");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
354 token_semi = register_string(";");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
355 token_tempin = register_string("tempin");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
356 token_versions = register_string("versions");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
357 token_wflogs = register_string("wflogs");
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
358 }