Author: bdrewery
Date: Fri Aug 14 00:18:18 2020
New Revision: 364223
URL: https://svnweb.freebsd.org/changeset/base/364223

Log:
  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

Modified:
  head/lib/libc/gen/syslog.c

Modified: head/lib/libc/gen/syslog.c
==============================================================================
--- head/lib/libc/gen/syslog.c  Thu Aug 13 23:13:05 2020        (r364222)
+++ head/lib/libc/gen/syslog.c  Fri Aug 14 00:18:18 2020        (r364223)
@@ -75,6 +75,9 @@ static pthread_mutex_t        syslog_mutex = PTHREAD_MUTEX_IN
                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);
 
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to