git: 4f8b31efcf95 - stable/12 - syslog(3): Send proper NILVALUE if gethostname(3) fails.

From: Eugene Grosbein <eugen_at_FreeBSD.org>
Date: Thu, 18 Aug 2022 15:21:31 UTC
The branch stable/12 has been updated by eugen:

URL: https://cgit.FreeBSD.org/src/commit/?id=4f8b31efcf95b43ed64bf6f9c895a27e8dffe158

commit 4f8b31efcf95b43ed64bf6f9c895a27e8dffe158
Author:     Bryan Drewery <bdrewery@FreeBSD.org>
AuthorDate: 2020-08-14 00:18:18 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2022-08-18 15:13:04 +0000

    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
    
    (cherry picked from commit 2933cd31845432dbaac67917e6babc516d2a57d1)
---
 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);