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
This commit is contained in:
Bryan Drewery 2020-08-14 00:18:18 +00:00
parent 3bd8419597
commit 2933cd3184
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=364223

View File

@ -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);