158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1983, 1988, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 1458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1558f0484fSRodney W. Grimes * without specific prior written permission. 1658f0484fSRodney W. Grimes * 1758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2758f0484fSRodney W. Grimes * SUCH DAMAGE. 2858f0484fSRodney W. Grimes */ 2958f0484fSRodney W. Grimes 3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 31adf6ad9eSPeter Wemm static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95"; 3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 33b231cb39SDavid E. O'Brien #include <sys/cdefs.h> 34b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3558f0484fSRodney W. Grimes 36d201fe46SDaniel Eischen #include "namespace.h" 3758f0484fSRodney W. Grimes #include <sys/types.h> 3858f0484fSRodney W. Grimes #include <sys/socket.h> 3958f0484fSRodney W. Grimes #include <sys/syslog.h> 4058f0484fSRodney W. Grimes #include <sys/uio.h> 41d584948eSBrian Somers #include <sys/un.h> 4258f0484fSRodney W. Grimes #include <netdb.h> 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes #include <errno.h> 4558f0484fSRodney W. Grimes #include <fcntl.h> 4658f0484fSRodney W. Grimes #include <paths.h> 4796575206SGleb Smirnoff #include <pthread.h> 4858f0484fSRodney W. Grimes #include <stdio.h> 494cd01193SMark Murray #include <stdlib.h> 5058f0484fSRodney W. Grimes #include <string.h> 5158f0484fSRodney W. Grimes #include <time.h> 5258f0484fSRodney W. Grimes #include <unistd.h> 5358f0484fSRodney W. Grimes 5458f0484fSRodney W. Grimes #include <stdarg.h> 55d201fe46SDaniel Eischen #include "un-namespace.h" 5658f0484fSRodney W. Grimes 574cd01193SMark Murray #include "libc_private.h" 584cd01193SMark Murray 5958f0484fSRodney W. Grimes static int LogFile = -1; /* fd for log */ 60240d5a9bSGleb Smirnoff static int status; /* connection status */ 617e6ace85SPeter Wemm static int opened; /* have done openlog() */ 6258f0484fSRodney W. Grimes static int LogStat = 0; /* status bits, set by openlog() */ 6358f0484fSRodney W. Grimes static const char *LogTag = NULL; /* string to tag the entry with */ 6458f0484fSRodney W. Grimes static int LogFacility = LOG_USER; /* default facility code */ 6558f0484fSRodney W. Grimes static int LogMask = 0xff; /* mask of priorities to be logged */ 6696575206SGleb Smirnoff static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER; 6796575206SGleb Smirnoff 6896575206SGleb Smirnoff #define THREAD_LOCK() \ 6996575206SGleb Smirnoff do { \ 7096575206SGleb Smirnoff if (__isthreaded) _pthread_mutex_lock(&syslog_mutex); \ 7196575206SGleb Smirnoff } while(0) 7296575206SGleb Smirnoff #define THREAD_UNLOCK() \ 7396575206SGleb Smirnoff do { \ 7496575206SGleb Smirnoff if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex); \ 7596575206SGleb Smirnoff } while(0) 7658f0484fSRodney W. Grimes 77b231cb39SDavid E. O'Brien static void disconnectlog(void); /* disconnect from syslogd */ 78b231cb39SDavid E. O'Brien static void connectlog(void); /* (re)connect to syslogd */ 7996575206SGleb Smirnoff static void openlog_unlocked(const char *, int, int); 807e6ace85SPeter Wemm 81240d5a9bSGleb Smirnoff enum { 82240d5a9bSGleb Smirnoff NOCONN = 0, 83240d5a9bSGleb Smirnoff CONNDEF, 84240d5a9bSGleb Smirnoff CONNPRIV, 85240d5a9bSGleb Smirnoff }; 86240d5a9bSGleb Smirnoff 8758f0484fSRodney W. Grimes /* 887c8e2aa4SPeter Wemm * Format of the magic cookie passed through the stdio hook 897c8e2aa4SPeter Wemm */ 907c8e2aa4SPeter Wemm struct bufcookie { 917c8e2aa4SPeter Wemm char *base; /* start of buffer */ 927c8e2aa4SPeter Wemm int left; 937c8e2aa4SPeter Wemm }; 947c8e2aa4SPeter Wemm 957c8e2aa4SPeter Wemm /* 967c8e2aa4SPeter Wemm * stdio write hook for writing to a static string buffer 977c8e2aa4SPeter Wemm * XXX: Maybe one day, dynamically allocate it so that the line length 987c8e2aa4SPeter Wemm * is `unlimited'. 997c8e2aa4SPeter Wemm */ 100fd42c4d8SStefan Farfeleder static int 101fd42c4d8SStefan Farfeleder writehook(void *cookie, const char *buf, int len) 1027c8e2aa4SPeter Wemm { 1037c8e2aa4SPeter Wemm struct bufcookie *h; /* private `handle' */ 1047c8e2aa4SPeter Wemm 1057c8e2aa4SPeter Wemm h = (struct bufcookie *)cookie; 1067c8e2aa4SPeter Wemm if (len > h->left) { 1077c8e2aa4SPeter Wemm /* clip in case of wraparound */ 1087c8e2aa4SPeter Wemm len = h->left; 1097c8e2aa4SPeter Wemm } 1107c8e2aa4SPeter Wemm if (len > 0) { 1117c8e2aa4SPeter Wemm (void)memcpy(h->base, buf, len); /* `write' it. */ 1127c8e2aa4SPeter Wemm h->base += len; 1137c8e2aa4SPeter Wemm h->left -= len; 1147c8e2aa4SPeter Wemm } 1155b1deb3cSPoul-Henning Kamp return len; 1167c8e2aa4SPeter Wemm } 1177c8e2aa4SPeter Wemm 1187c8e2aa4SPeter Wemm /* 11958f0484fSRodney W. Grimes * syslog, vsyslog -- 12058f0484fSRodney W. Grimes * print message on log file; output is intended for syslogd(8). 12158f0484fSRodney W. Grimes */ 12258f0484fSRodney W. Grimes void 12358f0484fSRodney W. Grimes syslog(int pri, const char *fmt, ...) 12458f0484fSRodney W. Grimes { 12558f0484fSRodney W. Grimes va_list ap; 12658f0484fSRodney W. Grimes 12758f0484fSRodney W. Grimes va_start(ap, fmt); 12858f0484fSRodney W. Grimes vsyslog(pri, fmt, ap); 12958f0484fSRodney W. Grimes va_end(ap); 13058f0484fSRodney W. Grimes } 13158f0484fSRodney W. Grimes 13258f0484fSRodney W. Grimes void 133fd42c4d8SStefan Farfeleder vsyslog(int pri, const char *fmt, va_list ap) 13458f0484fSRodney W. Grimes { 135b231cb39SDavid E. O'Brien int cnt; 136b231cb39SDavid E. O'Brien char ch, *p; 13758f0484fSRodney W. Grimes time_t now; 13858f0484fSRodney W. Grimes int fd, saved_errno; 13996575206SGleb Smirnoff char *stdp, tbuf[2048], fmt_cpy[1024], timbuf[26], errstr[64]; 1407c8e2aa4SPeter Wemm FILE *fp, *fmt_fp; 1417c8e2aa4SPeter Wemm struct bufcookie tbuf_cookie; 1427c8e2aa4SPeter Wemm struct bufcookie fmt_cookie; 14358f0484fSRodney W. Grimes 14458f0484fSRodney W. Grimes #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 14558f0484fSRodney W. Grimes /* Check for invalid bits. */ 14658f0484fSRodney W. Grimes if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 14758f0484fSRodney W. Grimes syslog(INTERNALLOG, 14858f0484fSRodney W. Grimes "syslog: unknown facility/priority: %x", pri); 14958f0484fSRodney W. Grimes pri &= LOG_PRIMASK|LOG_FACMASK; 15058f0484fSRodney W. Grimes } 15158f0484fSRodney W. Grimes 1523a31b448SDavid Xu saved_errno = errno; 1533a31b448SDavid Xu 15496575206SGleb Smirnoff THREAD_LOCK(); 15596575206SGleb Smirnoff 15658f0484fSRodney W. Grimes /* Check priority against setlogmask values. */ 15796575206SGleb Smirnoff if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) { 15896575206SGleb Smirnoff THREAD_UNLOCK(); 15958f0484fSRodney W. Grimes return; 16096575206SGleb Smirnoff } 16158f0484fSRodney W. Grimes 16258f0484fSRodney W. Grimes /* Set default facility if none specified. */ 16358f0484fSRodney W. Grimes if ((pri & LOG_FACMASK) == 0) 16458f0484fSRodney W. Grimes pri |= LogFacility; 16558f0484fSRodney W. Grimes 1667c8e2aa4SPeter Wemm /* Create the primary stdio hook */ 1677c8e2aa4SPeter Wemm tbuf_cookie.base = tbuf; 1687c8e2aa4SPeter Wemm tbuf_cookie.left = sizeof(tbuf); 1697c8e2aa4SPeter Wemm fp = fwopen(&tbuf_cookie, writehook); 17096575206SGleb Smirnoff if (fp == NULL) { 17196575206SGleb Smirnoff THREAD_UNLOCK(); 1727c8e2aa4SPeter Wemm return; 17396575206SGleb Smirnoff } 1747c8e2aa4SPeter Wemm 17558f0484fSRodney W. Grimes /* Build the message. */ 17658f0484fSRodney W. Grimes (void)time(&now); 1777c8e2aa4SPeter Wemm (void)fprintf(fp, "<%d>", pri); 178ab09ef00SDavid Malone (void)fprintf(fp, "%.15s ", ctime_r(&now, timbuf) + 4); 1797c8e2aa4SPeter Wemm if (LogStat & LOG_PERROR) { 1807c8e2aa4SPeter Wemm /* Transfer to string buffer */ 1817c8e2aa4SPeter Wemm (void)fflush(fp); 1827c8e2aa4SPeter Wemm stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left); 1837c8e2aa4SPeter Wemm } 18458f0484fSRodney W. Grimes if (LogTag == NULL) 1854cd01193SMark Murray LogTag = _getprogname(); 18658f0484fSRodney W. Grimes if (LogTag != NULL) 1877c8e2aa4SPeter Wemm (void)fprintf(fp, "%s", LogTag); 18858f0484fSRodney W. Grimes if (LogStat & LOG_PID) 1897c8e2aa4SPeter Wemm (void)fprintf(fp, "[%d]", getpid()); 19058f0484fSRodney W. Grimes if (LogTag != NULL) { 1917c8e2aa4SPeter Wemm (void)fprintf(fp, ": "); 1927c8e2aa4SPeter Wemm } 1937c8e2aa4SPeter Wemm 1947c8e2aa4SPeter Wemm /* Check to see if we can skip expanding the %m */ 1957c8e2aa4SPeter Wemm if (strstr(fmt, "%m")) { 1967c8e2aa4SPeter Wemm 1977c8e2aa4SPeter Wemm /* Create the second stdio hook */ 1987c8e2aa4SPeter Wemm fmt_cookie.base = fmt_cpy; 1997c8e2aa4SPeter Wemm fmt_cookie.left = sizeof(fmt_cpy) - 1; 2007c8e2aa4SPeter Wemm fmt_fp = fwopen(&fmt_cookie, writehook); 2017c8e2aa4SPeter Wemm if (fmt_fp == NULL) { 2027c8e2aa4SPeter Wemm fclose(fp); 20396575206SGleb Smirnoff THREAD_UNLOCK(); 2047c8e2aa4SPeter Wemm return; 20558f0484fSRodney W. Grimes } 20658f0484fSRodney W. Grimes 207e6cfb1ccSAlfred Perlstein /* 208e6cfb1ccSAlfred Perlstein * Substitute error message for %m. Be careful not to 209e6cfb1ccSAlfred Perlstein * molest an escaped percent "%%m". We want to pass it 210e6cfb1ccSAlfred Perlstein * on untouched as the format is later parsed by vfprintf. 211e6cfb1ccSAlfred Perlstein */ 212e6cfb1ccSAlfred Perlstein for ( ; (ch = *fmt); ++fmt) { 21358f0484fSRodney W. Grimes if (ch == '%' && fmt[1] == 'm') { 21458f0484fSRodney W. Grimes ++fmt; 21596575206SGleb Smirnoff strerror_r(saved_errno, errstr, sizeof(errstr)); 21696575206SGleb Smirnoff fputs(errstr, fmt_fp); 217e6cfb1ccSAlfred Perlstein } else if (ch == '%' && fmt[1] == '%') { 218e6cfb1ccSAlfred Perlstein ++fmt; 2197c8e2aa4SPeter Wemm fputc(ch, fmt_fp); 220e6cfb1ccSAlfred Perlstein fputc(ch, fmt_fp); 221e6cfb1ccSAlfred Perlstein } else { 222e6cfb1ccSAlfred Perlstein fputc(ch, fmt_fp); 223e6cfb1ccSAlfred Perlstein } 224e6cfb1ccSAlfred Perlstein } 22558f0484fSRodney W. Grimes 2267c8e2aa4SPeter Wemm /* Null terminate if room */ 2277c8e2aa4SPeter Wemm fputc(0, fmt_fp); 2287c8e2aa4SPeter Wemm fclose(fmt_fp); 2297c8e2aa4SPeter Wemm 2307c8e2aa4SPeter Wemm /* Guarantee null termination */ 2317c8e2aa4SPeter Wemm fmt_cpy[sizeof(fmt_cpy) - 1] = '\0'; 2327c8e2aa4SPeter Wemm 2337c8e2aa4SPeter Wemm fmt = fmt_cpy; 2347c8e2aa4SPeter Wemm } 2357c8e2aa4SPeter Wemm 2367c8e2aa4SPeter Wemm (void)vfprintf(fp, fmt, ap); 2377c8e2aa4SPeter Wemm (void)fclose(fp); 2387c8e2aa4SPeter Wemm 2397c8e2aa4SPeter Wemm cnt = sizeof(tbuf) - tbuf_cookie.left; 24058f0484fSRodney W. Grimes 241857b57eaSDiomidis Spinellis /* Remove a trailing newline */ 242857b57eaSDiomidis Spinellis if (tbuf[cnt - 1] == '\n') 243857b57eaSDiomidis Spinellis cnt--; 244857b57eaSDiomidis Spinellis 24558f0484fSRodney W. Grimes /* Output to stderr if requested. */ 24658f0484fSRodney W. Grimes if (LogStat & LOG_PERROR) { 24758f0484fSRodney W. Grimes struct iovec iov[2]; 248b231cb39SDavid E. O'Brien struct iovec *v = iov; 24958f0484fSRodney W. Grimes 25058f0484fSRodney W. Grimes v->iov_base = stdp; 25158f0484fSRodney W. Grimes v->iov_len = cnt - (stdp - tbuf); 25258f0484fSRodney W. Grimes ++v; 25358f0484fSRodney W. Grimes v->iov_base = "\n"; 25458f0484fSRodney W. Grimes v->iov_len = 1; 255d201fe46SDaniel Eischen (void)_writev(STDERR_FILENO, iov, 2); 25658f0484fSRodney W. Grimes } 25758f0484fSRodney W. Grimes 25858f0484fSRodney W. Grimes /* Get connected, output the message to the local logger. */ 2597e6ace85SPeter Wemm if (!opened) 26096575206SGleb Smirnoff openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0); 2617e6ace85SPeter Wemm connectlog(); 2627e6ace85SPeter Wemm 2637e6ace85SPeter Wemm /* 26452e05d9aSXin LI * If the send() fails, there are two likely scenarios: 2652e89951bSGleb Smirnoff * 1) syslogd was restarted 266240d5a9bSGleb Smirnoff * 2) /var/run/log is out of socket buffer space, which 267240d5a9bSGleb Smirnoff * in most cases means local DoS. 26852e05d9aSXin LI * If the error does not indicate a full buffer, we address 26952e05d9aSXin LI * case #1 by attempting to reconnect to /var/run/log[priv] 27052e05d9aSXin LI * and resending the message once. 271240d5a9bSGleb Smirnoff * 27252e05d9aSXin LI * If we are working with a privileged socket, the retry 27352e05d9aSXin LI * attempts end there, because we don't want to freeze a 274240d5a9bSGleb Smirnoff * critical application like su(1) or sshd(8). 275240d5a9bSGleb Smirnoff * 27652e05d9aSXin LI * Otherwise, we address case #2 by repeatedly retrying the 27752e05d9aSXin LI * send() to give syslogd a chance to empty its socket buffer. 2787e6ace85SPeter Wemm */ 2792e89951bSGleb Smirnoff 2802e89951bSGleb Smirnoff if (send(LogFile, tbuf, cnt, 0) < 0) { 2812e89951bSGleb Smirnoff if (errno != ENOBUFS) { 28252e05d9aSXin LI /* 28352e05d9aSXin LI * Scenario 1: syslogd was restarted 28452e05d9aSXin LI * reconnect and resend once 28552e05d9aSXin LI */ 2867e6ace85SPeter Wemm disconnectlog(); 2877e6ace85SPeter Wemm connectlog(); 28852e05d9aSXin LI if (send(LogFile, tbuf, cnt, 0) >= 0) { 28952e05d9aSXin LI THREAD_UNLOCK(); 29052e05d9aSXin LI return; 2912e89951bSGleb Smirnoff } 29252e05d9aSXin LI /* 29352e05d9aSXin LI * if the resend failed, fall through to 29452e05d9aSXin LI * possible scenario 2 29552e05d9aSXin LI */ 29652e05d9aSXin LI } 29752e05d9aSXin LI while (errno == ENOBUFS) { 29852e05d9aSXin LI /* 29952e05d9aSXin LI * Scenario 2: out of socket buffer space 30052e05d9aSXin LI * possible DoS, fail fast on a privileged 30152e05d9aSXin LI * socket 30252e05d9aSXin LI */ 30305824745SDavid E. O'Brien if (status == CONNPRIV) 30405824745SDavid E. O'Brien break; 305ed0b0abcSDaniel Eischen _usleep(1); 30696575206SGleb Smirnoff if (send(LogFile, tbuf, cnt, 0) >= 0) { 30796575206SGleb Smirnoff THREAD_UNLOCK(); 3088ba85e77SGleb Smirnoff return; 30996575206SGleb Smirnoff } 31052e05d9aSXin LI } 31196575206SGleb Smirnoff } else { 31296575206SGleb Smirnoff THREAD_UNLOCK(); 3138ba85e77SGleb Smirnoff return; 31496575206SGleb Smirnoff } 31558f0484fSRodney W. Grimes 31658f0484fSRodney W. Grimes /* 31794998878SDavid Malone * Output the message to the console; try not to block 31894998878SDavid Malone * as a blocking console should not stop other processes. 31994998878SDavid Malone * Make sure the error reported is the one from the syslogd failure. 32058f0484fSRodney W. Grimes */ 32158f0484fSRodney W. Grimes if (LogStat & LOG_CONS && 32205eb11cbSJilles Tjoelker (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK|O_CLOEXEC, 0)) >= 32305eb11cbSJilles Tjoelker 0) { 32411e67a9fSPeter Wemm struct iovec iov[2]; 325b231cb39SDavid E. O'Brien struct iovec *v = iov; 32611e67a9fSPeter Wemm 32711e67a9fSPeter Wemm p = strchr(tbuf, '>') + 1; 32811e67a9fSPeter Wemm v->iov_base = p; 32911e67a9fSPeter Wemm v->iov_len = cnt - (p - tbuf); 33011e67a9fSPeter Wemm ++v; 33111e67a9fSPeter Wemm v->iov_base = "\r\n"; 33211e67a9fSPeter Wemm v->iov_len = 2; 333d201fe46SDaniel Eischen (void)_writev(fd, iov, 2); 3349233c4d9SJason Evans (void)_close(fd); 33558f0484fSRodney W. Grimes } 33696575206SGleb Smirnoff 33796575206SGleb Smirnoff THREAD_UNLOCK(); 33858f0484fSRodney W. Grimes } 33996575206SGleb Smirnoff 34096575206SGleb Smirnoff /* Should be called with mutex acquired */ 3417e6ace85SPeter Wemm static void 342fd42c4d8SStefan Farfeleder disconnectlog(void) 3437e6ace85SPeter Wemm { 3447e6ace85SPeter Wemm /* 3457e6ace85SPeter Wemm * If the user closed the FD and opened another in the same slot, 3467e6ace85SPeter Wemm * that's their problem. They should close it before calling on 3477e6ace85SPeter Wemm * system services. 3487e6ace85SPeter Wemm */ 3497e6ace85SPeter Wemm if (LogFile != -1) { 3509233c4d9SJason Evans _close(LogFile); 3517e6ace85SPeter Wemm LogFile = -1; 3527e6ace85SPeter Wemm } 353240d5a9bSGleb Smirnoff status = NOCONN; /* retry connect */ 3547e6ace85SPeter Wemm } 3557e6ace85SPeter Wemm 35696575206SGleb Smirnoff /* Should be called with mutex acquired */ 3577e6ace85SPeter Wemm static void 358fd42c4d8SStefan Farfeleder connectlog(void) 3597e6ace85SPeter Wemm { 360d584948eSBrian Somers struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ 361cf49f439SJohn Polstra 3627e6ace85SPeter Wemm if (LogFile == -1) { 3630b89df4aSJilles Tjoelker if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 3640b89df4aSJilles Tjoelker 0)) == -1) 3657e6ace85SPeter Wemm return; 3667e6ace85SPeter Wemm } 367240d5a9bSGleb Smirnoff if (LogFile != -1 && status == NOCONN) { 368d584948eSBrian Somers SyslogAddr.sun_len = sizeof(SyslogAddr); 369d584948eSBrian Somers SyslogAddr.sun_family = AF_UNIX; 370240d5a9bSGleb Smirnoff 371240d5a9bSGleb Smirnoff /* 37252e05d9aSXin LI * First try privileged socket. If no success, 373240d5a9bSGleb Smirnoff * then try default socket. 374240d5a9bSGleb Smirnoff */ 375240d5a9bSGleb Smirnoff (void)strncpy(SyslogAddr.sun_path, _PATH_LOG_PRIV, 376240d5a9bSGleb Smirnoff sizeof SyslogAddr.sun_path); 377240d5a9bSGleb Smirnoff if (_connect(LogFile, (struct sockaddr *)&SyslogAddr, 378240d5a9bSGleb Smirnoff sizeof(SyslogAddr)) != -1) 379240d5a9bSGleb Smirnoff status = CONNPRIV; 380240d5a9bSGleb Smirnoff 381240d5a9bSGleb Smirnoff if (status == NOCONN) { 382d584948eSBrian Somers (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, 3830b3b961eSBrian Somers sizeof SyslogAddr.sun_path); 384240d5a9bSGleb Smirnoff if (_connect(LogFile, (struct sockaddr *)&SyslogAddr, 385240d5a9bSGleb Smirnoff sizeof(SyslogAddr)) != -1) 386240d5a9bSGleb Smirnoff status = CONNDEF; 387240d5a9bSGleb Smirnoff } 388cf49f439SJohn Polstra 389240d5a9bSGleb Smirnoff if (status == NOCONN) { 390cf49f439SJohn Polstra /* 391cf49f439SJohn Polstra * Try the old "/dev/log" path, for backward 392cf49f439SJohn Polstra * compatibility. 393cf49f439SJohn Polstra */ 394d584948eSBrian Somers (void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG, 3950b3b961eSBrian Somers sizeof SyslogAddr.sun_path); 396240d5a9bSGleb Smirnoff if (_connect(LogFile, (struct sockaddr *)&SyslogAddr, 397240d5a9bSGleb Smirnoff sizeof(SyslogAddr)) != -1) 398240d5a9bSGleb Smirnoff status = CONNDEF; 399cf49f439SJohn Polstra } 400cf49f439SJohn Polstra 401240d5a9bSGleb Smirnoff if (status == NOCONN) { 4029233c4d9SJason Evans (void)_close(LogFile); 4037e6ace85SPeter Wemm LogFile = -1; 404cf49f439SJohn Polstra } 4057e6ace85SPeter Wemm } 4067e6ace85SPeter Wemm } 4077e6ace85SPeter Wemm 40896575206SGleb Smirnoff static void 409fd42c4d8SStefan Farfeleder openlog_unlocked(const char *ident, int logstat, int logfac) 41058f0484fSRodney W. Grimes { 41158f0484fSRodney W. Grimes if (ident != NULL) 41258f0484fSRodney W. Grimes LogTag = ident; 41358f0484fSRodney W. Grimes LogStat = logstat; 41458f0484fSRodney W. Grimes if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 41558f0484fSRodney W. Grimes LogFacility = logfac; 41658f0484fSRodney W. Grimes 4177e6ace85SPeter Wemm if (LogStat & LOG_NDELAY) /* open immediately */ 4187e6ace85SPeter Wemm connectlog(); 4197e6ace85SPeter Wemm 4207e6ace85SPeter Wemm opened = 1; /* ident and facility has been set */ 42158f0484fSRodney W. Grimes } 42258f0484fSRodney W. Grimes 42358f0484fSRodney W. Grimes void 424fd42c4d8SStefan Farfeleder openlog(const char *ident, int logstat, int logfac) 42596575206SGleb Smirnoff { 42696575206SGleb Smirnoff THREAD_LOCK(); 42796575206SGleb Smirnoff openlog_unlocked(ident, logstat, logfac); 42896575206SGleb Smirnoff THREAD_UNLOCK(); 42996575206SGleb Smirnoff } 43096575206SGleb Smirnoff 43196575206SGleb Smirnoff 43296575206SGleb Smirnoff void 433fd42c4d8SStefan Farfeleder closelog(void) 43458f0484fSRodney W. Grimes { 43596575206SGleb Smirnoff THREAD_LOCK(); 436bf36cf8eSEitan Adler if (LogFile != -1) { 4379233c4d9SJason Evans (void)_close(LogFile); 43858f0484fSRodney W. Grimes LogFile = -1; 439bf36cf8eSEitan Adler } 440bedff4e8SRuslan Ermilov LogTag = NULL; 441240d5a9bSGleb Smirnoff status = NOCONN; 44296575206SGleb Smirnoff THREAD_UNLOCK(); 44358f0484fSRodney W. Grimes } 44458f0484fSRodney W. Grimes 44558f0484fSRodney W. Grimes /* setlogmask -- set the log mask level */ 44658f0484fSRodney W. Grimes int 447fd42c4d8SStefan Farfeleder setlogmask(int pmask) 44858f0484fSRodney W. Grimes { 44958f0484fSRodney W. Grimes int omask; 45058f0484fSRodney W. Grimes 45196575206SGleb Smirnoff THREAD_LOCK(); 45258f0484fSRodney W. Grimes omask = LogMask; 45358f0484fSRodney W. Grimes if (pmask != 0) 45458f0484fSRodney W. Grimes LogMask = pmask; 45596575206SGleb Smirnoff THREAD_UNLOCK(); 45658f0484fSRodney W. Grimes return (omask); 45758f0484fSRodney W. Grimes } 458