sysklogd: add timestamp option to ignore message timestamps

Some syslog producers provide inconsistent timestamps, so provide an option
to ignore the message timestamps and always locally timestamp.  In order to
implement this, invert the valid-timestamp check, but only use the timestamp
if this option is not enabled.

This is in line with what what other syslogd implementations do:

From sysklogd syslogd.c:
 * Sun Nov  7 12:28:47 CET 2004: Martin Schulze <joey@infodrom.org>
 *      Discard any timestamp information found in received syslog
 *      messages.  This will affect local messages sent from a
 *      different timezone.

rsyslog's imuxsock module similary has an (enabled by default)
IgnoreTimestamp option:

https://www.rsyslog.com/doc/v8-stable/configuration/modules/imuxsock.html

function                                             old     new   delta
packed_usage                                       32877   32912     +35
timestamp_and_log                                    363     376     +13
syslogd_main                                        1638    1641      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 51/0)               Total: 51 bytes

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Peter Korsgaard 2018-08-09 11:25:22 +02:00 committed by Denys Vlasenko
parent 6791140123
commit 9d539f9fbd

View File

@ -140,6 +140,7 @@
//usage: ) //usage: )
//usage: "\n -l N Log only messages more urgent than prio N (1-8)" //usage: "\n -l N Log only messages more urgent than prio N (1-8)"
//usage: "\n -S Smaller output" //usage: "\n -S Smaller output"
//usage: "\n -t Strip client-generated timestamps"
//usage: IF_FEATURE_SYSLOGD_DUP( //usage: IF_FEATURE_SYSLOGD_DUP(
//usage: "\n -D Drop duplicates" //usage: "\n -D Drop duplicates"
//usage: ) //usage: )
@ -316,6 +317,7 @@ enum {
OPTBIT_outfile, // -O OPTBIT_outfile, // -O
OPTBIT_loglevel, // -l OPTBIT_loglevel, // -l
OPTBIT_small, // -S OPTBIT_small, // -S
OPTBIT_timestamp, // -t
IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
@ -330,6 +332,7 @@ enum {
OPT_outfile = 1 << OPTBIT_outfile , OPT_outfile = 1 << OPTBIT_outfile ,
OPT_loglevel = 1 << OPTBIT_loglevel, OPT_loglevel = 1 << OPTBIT_loglevel,
OPT_small = 1 << OPTBIT_small , OPT_small = 1 << OPTBIT_small ,
OPT_timestamp = 1 << OPTBIT_timestamp,
OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0, OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
@ -339,7 +342,7 @@ enum {
OPT_cfg = IF_FEATURE_SYSLOGD_CFG( (1 << OPTBIT_cfg )) + 0, OPT_cfg = IF_FEATURE_SYSLOGD_CFG( (1 << OPTBIT_cfg )) + 0,
OPT_kmsg = IF_FEATURE_KMSG_SYSLOG( (1 << OPTBIT_kmsg )) + 0, OPT_kmsg = IF_FEATURE_KMSG_SYSLOG( (1 << OPTBIT_kmsg )) + 0,
}; };
#define OPTION_STR "m:nO:l:S" \ #define OPTION_STR "m:nO:l:St" \
IF_FEATURE_ROTATE_LOGFILE("s:" ) \ IF_FEATURE_ROTATE_LOGFILE("s:" ) \
IF_FEATURE_ROTATE_LOGFILE("b:" ) \ IF_FEATURE_ROTATE_LOGFILE("b:" ) \
IF_FEATURE_REMOTE_LOG( "R:*") \ IF_FEATURE_REMOTE_LOG( "R:*") \
@ -813,21 +816,27 @@ static void parse_fac_prio_20(int pri, char *res20)
* that there is no timestamp, short-circuiting the test. */ * that there is no timestamp, short-circuiting the test. */
static void timestamp_and_log(int pri, char *msg, int len) static void timestamp_and_log(int pri, char *msg, int len)
{ {
char *timestamp; char *timestamp = NULL;
time_t now; time_t now;
/* Jan 18 00:11:22 msg... */ /* Jan 18 00:11:22 msg... */
/* 01234567890123456 */ /* 01234567890123456 */
if (len < 16 || msg[3] != ' ' || msg[6] != ' ' if (len >= 16 && msg[3] == ' ' && msg[6] == ' '
|| msg[9] != ':' || msg[12] != ':' || msg[15] != ' ' && msg[9] == ':' && msg[12] == ':' && msg[15] == ' '
) { ) {
time(&now); if (!(option_mask32 & OPT_timestamp)) {
timestamp = ctime(&now) + 4; /* skip day of week */ /* use message timestamp */
} else { timestamp = msg;
now = 0; now = 0;
timestamp = msg; }
msg += 16; msg += 16;
} }
if (!timestamp) {
time(&now);
timestamp = ctime(&now) + 4; /* skip day of week */
}
timestamp[15] = '\0'; timestamp[15] = '\0';
if (option_mask32 & OPT_kmsg) { if (option_mask32 & OPT_kmsg) {