Mercurial > syslog2iptables
diff src/syslogconfig.cpp @ 2:6e88da080f08
initial coding
author | carl |
---|---|
date | Thu, 24 Nov 2005 10:31:09 -0800 |
parents | 551433a01cab |
children | 8fe310e5cd44 |
line wrap: on
line diff
--- a/src/syslogconfig.cpp Wed Nov 23 19:29:14 2005 -0800 +++ b/src/syslogconfig.cpp Thu Nov 24 10:31:09 2005 -0800 @@ -19,6 +19,7 @@ ***************************************************************************/ #include "includes.h" +#include <fcntl.h> static char* syslogconfig_version="$Id$"; @@ -63,13 +64,58 @@ } -SYSLOGCONFIG::SYSLOGCONFIG(char *file_name_, parser_style parser_) { +void CONFIG::read() { + for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) { + SYSLOGCONFIGP c = *i; + c->read(); + } +} + + +SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, char *file_name_, parser_style parser_) { file_name = file_name_; parser = parser_; + fd = open(file_name, O_RDONLY); + len = 0; + if (fd == -1) { + char buf[maxlen]; + snprintf(buf, sizeof(buf), "syslog file %s not readable", file_name); + tok.token_error(buf); + } + else { + lseek(fd, 0, SEEK_END); + } } SYSLOGCONFIG::~SYSLOGCONFIG() { + if (fd != -1) close(fd); + fd = -1; +} + + +void SYSLOGCONFIG::read() { + if (failed()) return; + int n = ::read(fd, buf, buflen-len); + if (n > 0) { + len += n; + while (true) { + char *p = (char*)memchr(buf, '\n', len); + if (!p) break; + n = p-buf; + *p = '\0'; + process(); // process null terminated string + len -= n+1; + memmove(buf, p+1, len); + } + // no <lf> in a full buffer + if (len == buflen) len = 0; + } +} + + +void SYSLOGCONFIG::process() { + my_syslog(buf); } @@ -162,7 +208,11 @@ } } if (!tsa(tok, token_semi)) return false; - SYSLOGCONFIGP con = new SYSLOGCONFIG(name, parser); + SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name, parser); + if (con->failed()) { + delete con; + return false; + } dc.add_syslogconfig(con); return true; }