libc: Move out locking from vsyslog() core.
authorzrj <rimvydas.jasinskas@gmail.com>
Tue, 23 Apr 2019 16:16:12 +0000 (19:16 +0300)
committerzrj <zrj@dragonflybsd.org>
Tue, 23 Apr 2019 18:32:22 +0000 (21:32 +0300)
For consistency with openlog() and closelog().

While there, use SOCK_CLOEXEC directly and do not attempt to closelog()
several times for badly written programs (LogFile is handled internally).

lib/libc/gen/syslog.c

index e4f7d11..7cd7112 100644 (file)
@@ -126,8 +126,8 @@ syslog(int pri, const char *fmt, ...)
        va_end(ap);
 }
 
        va_end(ap);
 }
 
-void
-vsyslog(int pri, const char *fmt, va_list ap)
+static void
+vsyslog_unlocked(int pri, const char *fmt, va_list ap)
 {
        char ch, *p;
        time_t now;
 {
        char ch, *p;
        time_t now;
@@ -149,13 +149,9 @@ vsyslog(int pri, const char *fmt, va_list ap)
 
        saved_errno = errno;
 
 
        saved_errno = errno;
 
-       THREAD_LOCK();
-
        /* Check priority against setlogmask values. */
        /* Check priority against setlogmask values. */
-       if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) {
-               THREAD_UNLOCK();
+       if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
                return;
                return;
-       }
 
        /* Set default facility if none specified. */
        if ((pri & LOG_FACMASK) == 0)
 
        /* Set default facility if none specified. */
        if ((pri & LOG_FACMASK) == 0)
@@ -165,10 +161,8 @@ vsyslog(int pri, const char *fmt, va_list ap)
        tbuf_cookie.base = tbuf;
        tbuf_cookie.left = sizeof(tbuf);
        fp = fwopen(&tbuf_cookie, writehook);
        tbuf_cookie.base = tbuf;
        tbuf_cookie.left = sizeof(tbuf);
        fp = fwopen(&tbuf_cookie, writehook);
-       if (fp == NULL) {
-               THREAD_UNLOCK();
+       if (fp == NULL)
                return;
                return;
-       }
 
        /* Build the message. */
        time(&now);
 
        /* Build the message. */
        time(&now);
@@ -197,7 +191,6 @@ vsyslog(int pri, const char *fmt, va_list ap)
                fmt_fp = fwopen(&fmt_cookie, writehook);
                if (fmt_fp == NULL) {
                        fclose(fp);
                fmt_fp = fwopen(&fmt_cookie, writehook);
                if (fmt_fp == NULL) {
                        fclose(fp);
-                       THREAD_UNLOCK();
                        return;
                }
 
                        return;
                }
 
@@ -283,10 +276,8 @@ vsyslog(int pri, const char *fmt, va_list ap)
                        connectlog();
                }
                for (maxtries = 10; maxtries; --maxtries) {
                        connectlog();
                }
                for (maxtries = 10; maxtries; --maxtries) {
-                       if (send(LogFile, tbuf, cnt, 0) >= 0) {
-                               THREAD_UNLOCK();
+                       if (send(LogFile, tbuf, cnt, 0) >= 0)
                                return;
                                return;
-                       }
                        if (status == CONNPRIV)
                                break;
                        if (errno != ENOBUFS)
                        if (status == CONNPRIV)
                                break;
                        if (errno != ENOBUFS)
@@ -294,7 +285,6 @@ vsyslog(int pri, const char *fmt, va_list ap)
                        _usleep(1000000 / 10);
                }
        } else {
                        _usleep(1000000 / 10);
                }
        } else {
-               THREAD_UNLOCK();
                return;
        }
 
                return;
        }
 
@@ -317,7 +307,13 @@ vsyslog(int pri, const char *fmt, va_list ap)
                _writev(fd, iov, 2);
                _close(fd);
        }
                _writev(fd, iov, 2);
                _close(fd);
        }
+}
 
 
+void
+vsyslog(int pri, const char *fmt, va_list ap)
+{
+       THREAD_LOCK();
+       vsyslog_unlocked(pri, fmt, ap);
        THREAD_UNLOCK();
 }
 
        THREAD_UNLOCK();
 }
 
@@ -344,9 +340,9 @@ connectlog(void)
        struct sockaddr_un SyslogAddr;  /* AF_UNIX address of local logger */
 
        if (LogFile == -1) {
        struct sockaddr_un SyslogAddr;  /* AF_UNIX address of local logger */
 
        if (LogFile == -1) {
-               if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
+               if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
+                   0)) == -1)
                        return;
                        return;
-               _fcntl(LogFile, F_SETFD, 1);
        }
        if (LogFile != -1 && status == NOCONN) {
                SyslogAddr.sun_len = sizeof(SyslogAddr);
        }
        if (LogFile != -1 && status == NOCONN) {
                SyslogAddr.sun_len = sizeof(SyslogAddr);
@@ -417,8 +413,10 @@ void
 closelog(void)
 {
        THREAD_LOCK();
 closelog(void)
 {
        THREAD_LOCK();
-       _close(LogFile);
-       LogFile = -1;
+       if (LogFile != -1) {
+               _close(LogFile);
+               LogFile = -1;
+       }
        LogTag = NULL;
        status = NOCONN;
        THREAD_UNLOCK();
        LogTag = NULL;
        status = NOCONN;
        THREAD_UNLOCK();