[vhffs-dev] [2178] syslogger now truncates messages bigger than buffer |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 2178
Author: gradator
Date: 2012-05-07 21:19:35 +0200 (Mon, 07 May 2012)
Log Message:
-----------
syslogger now truncates messages bigger than buffer
Modified Paths:
--------------
trunk/vhffs-syslogger/syslogger.c
Modified: trunk/vhffs-syslogger/syslogger.c
===================================================================
--- trunk/vhffs-syslogger/syslogger.c 2012-05-06 13:02:25 UTC (rev 2177)
+++ trunk/vhffs-syslogger/syslogger.c 2012-05-07 19:19:35 UTC (rev 2178)
@@ -37,6 +37,12 @@
int val;
};
+enum {
+ SYSLOGGER_STATE_NORMALFLOW, // Flow is normally flowing from standard input to syslog
+ SYSLOGGER_STATE_WAITFORNEWLINE // We received a message which exceeded maxsize,
+ // we have to discard any input until we found a newline
+};
+
static void usage_exit(int ret_code, char *progname);
int syslog_facility_from_str(const char *facility);
int syslog_priority_from_str(const char *priority);
@@ -45,8 +51,10 @@
int main(int argc, char *argv[]) {
char *buffer, *ident = "httpd";
int facility = DEFAULT_FACILITY, priority = DEFAULT_PRIORITY, option = DEFAULT_OPTION;
- size_t bufsiz = 32768;
+ size_t bufsize = 32768;
FILE *tee = NULL;
+ int state;
+ size_t overflowsize;
struct option long_options[] = {
{ "ident", required_argument, NULL, 'i' },
@@ -72,7 +80,7 @@
break;
case 's':
- bufsiz = (size_t)(atoi(optarg));
+ bufsize = (size_t)(atoi(optarg));
break;
case 'f':
@@ -137,21 +145,62 @@
if(optind != argc)
usage_exit(1, argv[0]);
- if(bufsiz < 1024)
- bufsiz = 1024;
- buffer = malloc(bufsiz);
+ if(bufsize < 1024)
+ bufsize = 1024;
+
+ buffer = malloc(bufsize+2); // + '\n' + '\0'
if(!buffer) {
fprintf(stderr, "Failed to allocated buffer, exiting.\n");
return -1;
}
+ state = SYSLOGGER_STATE_NORMALFLOW;
openlog(ident, option, facility);
- while( fgets(buffer, bufsiz, stdin) ) {
- syslog(priority, "%s", buffer);
+ while( fgets(buffer, bufsize+2, stdin) ) {
+ size_t len = strlen(buffer);
+
if(tee) {
- fwrite(buffer, strlen(buffer), 1, tee);
+ fwrite(buffer, len, 1, tee);
fflush(tee);
}
+
+ switch( *(buffer+len-1) ) {
+
+ case '\n':
+ switch(state) {
+
+ // everything's good
+ case SYSLOGGER_STATE_NORMALFLOW:
+ syslog(priority, "%s", buffer);
+ break;
+
+ // overflow recovered
+ case SYSLOGGER_STATE_WAITFORNEWLINE:
+ overflowsize += len-1;
+ fprintf(stderr, "%s: too big message received: %d bytes long, truncated to %d bytes\n", argv[0], overflowsize, bufsize);
+ state = SYSLOGGER_STATE_NORMALFLOW;
+ break;
+ }
+ break;
+
+ default:
+ switch(state) {
+
+ // new overflow
+ case SYSLOGGER_STATE_NORMALFLOW:
+ *(buffer+len-1) = '\n';
+ overflowsize = len;
+ syslog(priority, "%s", buffer);
+ state = SYSLOGGER_STATE_WAITFORNEWLINE;
+ break;
+
+ // overflow continue
+ case SYSLOGGER_STATE_WAITFORNEWLINE:
+ overflowsize += len;
+ break;
+ }
+ break;
+ }
}
closelog();
if(tee) fclose(tee);