Mercurial > syslog2iptables
comparison src/syslogconfig.cpp @ 2:6e88da080f08
initial coding
author | carl |
---|---|
date | Thu, 24 Nov 2005 10:31:09 -0800 |
parents | 551433a01cab |
children | 8fe310e5cd44 |
comparison
equal
deleted
inserted
replaced
1:551433a01cab | 2:6e88da080f08 |
---|---|
17 * Free Software Foundation, Inc., * | 17 * Free Software Foundation, Inc., * |
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * | 18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 ***************************************************************************/ | 19 ***************************************************************************/ |
20 | 20 |
21 #include "includes.h" | 21 #include "includes.h" |
22 #include <fcntl.h> | |
22 | 23 |
23 static char* syslogconfig_version="$Id$"; | 24 static char* syslogconfig_version="$Id$"; |
24 | 25 |
25 char *token_cisco; | 26 char *token_cisco; |
26 char *token_file; | 27 char *token_file; |
61 c->dump(0); | 62 c->dump(0); |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 | 66 |
66 SYSLOGCONFIG::SYSLOGCONFIG(char *file_name_, parser_style parser_) { | 67 void CONFIG::read() { |
67 file_name = file_name_; | 68 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) { |
68 parser = parser_; | 69 SYSLOGCONFIGP c = *i; |
70 c->read(); | |
71 } | |
72 } | |
73 | |
74 | |
75 SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, char *file_name_, parser_style parser_) { | |
76 file_name = file_name_; | |
77 parser = parser_; | |
78 fd = open(file_name, O_RDONLY); | |
79 len = 0; | |
80 if (fd == -1) { | |
81 char buf[maxlen]; | |
82 snprintf(buf, sizeof(buf), "syslog file %s not readable", file_name); | |
83 tok.token_error(buf); | |
84 } | |
85 else { | |
86 lseek(fd, 0, SEEK_END); | |
87 } | |
69 } | 88 } |
70 | 89 |
71 | 90 |
72 SYSLOGCONFIG::~SYSLOGCONFIG() { | 91 SYSLOGCONFIG::~SYSLOGCONFIG() { |
92 if (fd != -1) close(fd); | |
93 fd = -1; | |
94 } | |
95 | |
96 | |
97 void SYSLOGCONFIG::read() { | |
98 if (failed()) return; | |
99 int n = ::read(fd, buf, buflen-len); | |
100 if (n > 0) { | |
101 len += n; | |
102 while (true) { | |
103 char *p = (char*)memchr(buf, '\n', len); | |
104 if (!p) break; | |
105 n = p-buf; | |
106 *p = '\0'; | |
107 process(); // process null terminated string | |
108 len -= n+1; | |
109 memmove(buf, p+1, len); | |
110 } | |
111 // no <lf> in a full buffer | |
112 if (len == buflen) len = 0; | |
113 } | |
114 } | |
115 | |
116 | |
117 void SYSLOGCONFIG::process() { | |
118 my_syslog(buf); | |
73 } | 119 } |
74 | 120 |
75 | 121 |
76 void SYSLOGCONFIG::dump(int level) { | 122 void SYSLOGCONFIG::dump(int level) { |
77 char indent[maxlen]; | 123 char indent[maxlen]; |
160 tok.token_error("file keyword", have); | 206 tok.token_error("file keyword", have); |
161 return false; | 207 return false; |
162 } | 208 } |
163 } | 209 } |
164 if (!tsa(tok, token_semi)) return false; | 210 if (!tsa(tok, token_semi)) return false; |
165 SYSLOGCONFIGP con = new SYSLOGCONFIG(name, parser); | 211 SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name, parser); |
212 if (con->failed()) { | |
213 delete con; | |
214 return false; | |
215 } | |
166 dc.add_syslogconfig(con); | 216 dc.add_syslogconfig(con); |
167 return true; | 217 return true; |
168 } | 218 } |
169 | 219 |
170 | 220 |