From 2933cd31845432dbaac67917e6babc516d2a57d1 Mon Sep 17 00:00:00 2001 From: Bryan Drewery Date: Fri, 14 Aug 2020 00:18:18 +0000 Subject: [PATCH] syslog(3): Send proper NILVALUE if gethostname(3) fails. RFC5424 defines NILVALUE as '-'. Replace its usage with a macro and separate out the fields to be more clear. fputs(3) is used in some places to avoid hiding possible format string problems in a macro. Reviewed by: cem, vangyzen (earlier version) Sponsored by: Dell EMC --- lib/libc/gen/syslog.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/libc/gen/syslog.c b/lib/libc/gen/syslog.c index a0f1ddc97535..19d44db0075a 100644 --- a/lib/libc/gen/syslog.c +++ b/lib/libc/gen/syslog.c @@ -75,6 +75,9 @@ static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER; if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex); \ } while(0) +/* RFC5424 defined value. */ +#define NILVALUE "-" + static void disconnectlog(void); /* disconnect from syslogd */ static void connectlog(void); /* (re)connect to syslogd */ static void openlog_unlocked(const char *, int, int); @@ -190,25 +193,30 @@ vsyslog1(int pri, const char *fmt, va_list ap) tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec, tz_sign, tz_offset / 3600, (tz_offset % 3600) / 60); } else - (void)fprintf(fp, "- "); + (void)fputs(NILVALUE " ", fp); /* Hostname. */ (void)gethostname(hostname, sizeof(hostname)); - (void)fprintf(fp, "%s ", hostname); + (void)fprintf(fp, "%s ", + hostname[0] == '\0' ? NILVALUE : hostname); if (LogStat & LOG_PERROR) { /* Transfer to string buffer */ (void)fflush(fp); stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left); } + /* Application name. */ + if (LogTag == NULL) + LogTag = _getprogname(); + (void)fprintf(fp, "%s ", LogTag == NULL ? NILVALUE : LogTag); /* - * Application name, process ID, message ID and structured data. * Provide the process ID regardless of whether LOG_PID has been * specified, as it provides valuable information. Many * applications tend not to use this, even though they should. */ - if (LogTag == NULL) - LogTag = _getprogname(); - (void)fprintf(fp, "%s %d - - ", - LogTag == NULL ? "-" : LogTag, getpid()); + (void)fprintf(fp, "%d ", getpid()); + /* Message ID. */ + (void)fputs(NILVALUE " ", fp); + /* Structured data. */ + (void)fputs(NILVALUE " ", fp); /* Check to see if we can skip expanding the %m */ if (strstr(fmt, "%m")) { @@ -251,6 +259,7 @@ vsyslog1(int pri, const char *fmt, va_list ap) fmt = fmt_cpy; } + /* Message. */ (void)vfprintf(fp, fmt, ap); (void)fclose(fp);