From 30dd2da2a3e030eb19a4d80219618240b034c8f2 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Sun, 8 Jul 2018 10:08:24 +0000 Subject: [PATCH] Use the FQDN in the newsyslog log message when RFC 5424 is enabled. The RFC 5424 spec mentions that logging FQDNs over short hostnames is preferred. Alter this code, so that the hostname doesn't get truncated on startup. Keep track of the length of the short hostname, so that fprintf() can do the truncation where necessary. MFC after: 1 month --- usr.sbin/newsyslog/newsyslog.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/usr.sbin/newsyslog/newsyslog.c b/usr.sbin/newsyslog/newsyslog.c index ede2a73e2024..c45c89f0f727 100644 --- a/usr.sbin/newsyslog/newsyslog.c +++ b/usr.sbin/newsyslog/newsyslog.c @@ -271,6 +271,7 @@ static char daytime[DAYTIME_LEN];/* The current time in human readable form, static char daytime_rfc5424[DAYTIME_RFC5424_LEN]; static char hostname[MAXHOSTNAMELEN]; /* hostname */ +static size_t hostname_shortlen; static const char *path_syslogpid = _PATH_SYSLOGPID; @@ -657,10 +658,7 @@ parse_args(int argc, char **argv) /* Let's get our hostname */ (void)gethostname(hostname, sizeof(hostname)); - - /* Truncate domain */ - if ((p = strchr(hostname, '.')) != NULL) - *p = '\0'; + hostname_shortlen = strcspn(hostname, "."); /* Parse command line options. */ while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1) @@ -2349,14 +2347,20 @@ log_trim(const char *logname, const struct conf_entry *log_ent) } } else { if (log_ent->firstcreate) - fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", - daytime, hostname, getpid(), xtra); + fprintf(f, + "%s %.*s newsyslog[%d]: logfile first created%s\n", + daytime, (int)hostname_shortlen, hostname, getpid(), + xtra); else if (log_ent->r_reason != NULL) - fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", - daytime, hostname, getpid(), log_ent->r_reason, xtra); + fprintf(f, + "%s %.*s newsyslog[%d]: logfile turned over%s%s\n", + daytime, (int)hostname_shortlen, hostname, getpid(), + log_ent->r_reason, xtra); else - fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", - daytime, hostname, getpid(), xtra); + fprintf(f, + "%s %.*s newsyslog[%d]: logfile turned over%s\n", + daytime, (int)hostname_shortlen, hostname, getpid(), + xtra); } if (fclose(f) == EOF) err(1, "log_trim: fclose");