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. 1358f0484fSRodney W. Grimes * 4. 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 44*bf36cf8eSEitan Adler #include <assert.h> 4558f0484fSRodney W. Grimes #include <errno.h> 4658f0484fSRodney W. Grimes #include <fcntl.h> 4758f0484fSRodney W. Grimes #include <paths.h> 4896575206SGleb Smirnoff #include <pthread.h> 4958f0484fSRodney W. Grimes #include <stdio.h> 504cd01193SMark Murray #include <stdlib.h> 5158f0484fSRodney W. Grimes #include <string.h> 5258f0484fSRodney W. Grimes #include <time.h> 5358f0484fSRodney W. Grimes #include <unistd.h> 5458f0484fSRodney W. Grimes 5558f0484fSRodney W. Grimes #include <stdarg.h> 56d201fe46SDaniel Eischen #include "un-namespace.h" 5758f0484fSRodney W. Grimes 584cd01193SMark Murray #include "libc_private.h" 594cd01193SMark Murray 6058f0484fSRodney W. Grimes static int LogFile = -1; /* fd for log */ 61240d5a9bSGleb Smirnoff static int status; /* connection status */ 627e6ace85SPeter Wemm static int opened; /* have done openlog() */ 6358f0484fSRodney W. Grimes static int LogStat = 0; /* status bits, set by openlog() */ 6458f0484fSRodney W. Grimes static const char *LogTag = NULL; /* string to tag the entry with */ 6558f0484fSRodney W. Grimes static int LogFacility = LOG_USER; /* default facility code */ 6658f0484fSRodney W. Grimes static int LogMask = 0xff; /* mask of priorities to be logged */ 6796575206SGleb Smirnoff static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER; 6896575206SGleb Smirnoff 6996575206SGleb Smirnoff #define THREAD_LOCK() \ 7096575206SGleb Smirnoff do { \ 7196575206SGleb Smirnoff if (__isthreaded) _pthread_mutex_lock(&syslog_mutex); \ 7296575206SGleb Smirnoff } while(0) 7396575206SGleb Smirnoff #define THREAD_UNLOCK() \ 7496575206SGleb Smirnoff do { \ 7596575206SGleb Smirnoff if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex); \ 7696575206SGleb Smirnoff } while(0) 7758f0484fSRodney W. Grimes 78b231cb39SDavid E. O'Brien static void disconnectlog(void); /* disconnect from syslogd */ 79b231cb39SDavid E. O'Brien static void connectlog(void); /* (re)connect to syslogd */ 8096575206SGleb Smirnoff static void openlog_unlocked(const char *, int, int); 817e6ace85SPeter Wemm 82240d5a9bSGleb Smirnoff enum { 83240d5a9bSGleb Smirnoff NOCONN = 0, 84240d5a9bSGleb Smirnoff CONNDEF, 85240d5a9bSGleb Smirnoff CONNPRIV, 86240d5a9bSGleb Smirnoff }; 87240d5a9bSGleb Smirnoff 8858f0484fSRodney W. Grimes /* 897c8e2aa4SPeter Wemm * Format of the magic cookie passed through the stdio hook 907c8e2aa4SPeter Wemm */ 917c8e2aa4SPeter Wemm struct bufcookie { 927c8e2aa4SPeter Wemm char *base; /* start of buffer */ 937c8e2aa4SPeter Wemm int left; 947c8e2aa4SPeter Wemm }; 957c8e2aa4SPeter Wemm 967c8e2aa4SPeter Wemm /* 977c8e2aa4SPeter Wemm * stdio write hook for writing to a static string buffer 987c8e2aa4SPeter Wemm * XXX: Maybe one day, dynamically allocate it so that the line length 997c8e2aa4SPeter Wemm * is `unlimited'. 1007c8e2aa4SPeter Wemm */ 101fd42c4d8SStefan Farfeleder static int 102fd42c4d8SStefan Farfeleder writehook(void *cookie, const char *buf, int len) 1037c8e2aa4SPeter Wemm { 1047c8e2aa4SPeter Wemm struct bufcookie *h; /* private `handle' */ 1057c8e2aa4SPeter Wemm 1067c8e2aa4SPeter Wemm h = (struct bufcookie *)cookie; 1077c8e2aa4SPeter Wemm if (len > h->left) { 1087c8e2aa4SPeter Wemm /* clip in case of wraparound */ 1097c8e2aa4SPeter Wemm len = h->left; 1107c8e2aa4SPeter Wemm } 1117c8e2aa4SPeter Wemm if (len > 0) { 1127c8e2aa4SPeter Wemm (void)memcpy(h->base, buf, len); /* `write' it. */ 1137c8e2aa4SPeter Wemm h->base += len; 1147c8e2aa4SPeter Wemm h->left -= len; 1157c8e2aa4SPeter Wemm } 1165b1deb3cSPoul-Henning Kamp return len; 1177c8e2aa4SPeter Wemm } 1187c8e2aa4SPeter Wemm 1197c8e2aa4SPeter Wemm /* 12058f0484fSRodney W. Grimes * syslog, vsyslog -- 12158f0484fSRodney W. Grimes * print message on log file; output is intended for syslogd(8). 12258f0484fSRodney W. Grimes */ 12358f0484fSRodney W. Grimes void 12458f0484fSRodney W. Grimes syslog(int pri, const char *fmt, ...) 12558f0484fSRodney W. Grimes { 12658f0484fSRodney W. Grimes va_list ap; 12758f0484fSRodney W. Grimes 12858f0484fSRodney W. Grimes va_start(ap, fmt); 12958f0484fSRodney W. Grimes vsyslog(pri, fmt, ap); 13058f0484fSRodney W. Grimes va_end(ap); 13158f0484fSRodney W. Grimes } 13258f0484fSRodney W. Grimes 13358f0484fSRodney W. Grimes void 134fd42c4d8SStefan Farfeleder vsyslog(int pri, const char *fmt, va_list ap) 13558f0484fSRodney W. Grimes { 136b231cb39SDavid E. O'Brien int cnt; 137b231cb39SDavid E. O'Brien char ch, *p; 13858f0484fSRodney W. Grimes time_t now; 13958f0484fSRodney W. Grimes int fd, saved_errno; 14096575206SGleb Smirnoff char *stdp, tbuf[2048], fmt_cpy[1024], timbuf[26], errstr[64]; 1417c8e2aa4SPeter Wemm FILE *fp, *fmt_fp; 1427c8e2aa4SPeter Wemm struct bufcookie tbuf_cookie; 1437c8e2aa4SPeter Wemm struct bufcookie fmt_cookie; 14458f0484fSRodney W. Grimes 14558f0484fSRodney W. Grimes #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 14658f0484fSRodney W. Grimes /* Check for invalid bits. */ 14758f0484fSRodney W. Grimes if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 14858f0484fSRodney W. Grimes syslog(INTERNALLOG, 14958f0484fSRodney W. Grimes "syslog: unknown facility/priority: %x", pri); 15058f0484fSRodney W. Grimes pri &= LOG_PRIMASK|LOG_FACMASK; 15158f0484fSRodney W. Grimes } 15258f0484fSRodney W. Grimes 1533a31b448SDavid Xu saved_errno = errno; 1543a31b448SDavid Xu 15596575206SGleb Smirnoff THREAD_LOCK(); 15696575206SGleb Smirnoff 15758f0484fSRodney W. Grimes /* Check priority against setlogmask values. */ 15896575206SGleb Smirnoff if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) { 15996575206SGleb Smirnoff THREAD_UNLOCK(); 16058f0484fSRodney W. Grimes return; 16196575206SGleb Smirnoff } 16258f0484fSRodney W. Grimes 16358f0484fSRodney W. Grimes /* Set default facility if none specified. */ 16458f0484fSRodney W. Grimes if ((pri & LOG_FACMASK) == 0) 16558f0484fSRodney W. Grimes pri |= LogFacility; 16658f0484fSRodney W. Grimes 1677c8e2aa4SPeter Wemm /* Create the primary stdio hook */ 1687c8e2aa4SPeter Wemm tbuf_cookie.base = tbuf; 1697c8e2aa4SPeter Wemm tbuf_cookie.left = sizeof(tbuf); 1707c8e2aa4SPeter Wemm fp = fwopen(&tbuf_cookie, writehook); 17196575206SGleb Smirnoff if (fp == NULL) { 17296575206SGleb Smirnoff THREAD_UNLOCK(); 1737c8e2aa4SPeter Wemm return; 17496575206SGleb Smirnoff } 1757c8e2aa4SPeter Wemm 17658f0484fSRodney W. Grimes /* Build the message. */ 17758f0484fSRodney W. Grimes (void)time(&now); 1787c8e2aa4SPeter Wemm (void)fprintf(fp, "<%d>", pri); 179ab09ef00SDavid Malone (void)fprintf(fp, "%.15s ", ctime_r(&now, timbuf) + 4); 1807c8e2aa4SPeter Wemm if (LogStat & LOG_PERROR) { 1817c8e2aa4SPeter Wemm /* Transfer to string buffer */ 1827c8e2aa4SPeter Wemm (void)fflush(fp); 1837c8e2aa4SPeter Wemm stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left); 1847c8e2aa4SPeter Wemm } 18558f0484fSRodney W. Grimes if (LogTag == NULL) 1864cd01193SMark Murray LogTag = _getprogname(); 18758f0484fSRodney W. Grimes if (LogTag != NULL) 1887c8e2aa4SPeter Wemm (void)fprintf(fp, "%s", LogTag); 18958f0484fSRodney W. Grimes if (LogStat & LOG_PID) 1907c8e2aa4SPeter Wemm (void)fprintf(fp, "[%d]", getpid()); 19158f0484fSRodney W. Grimes if (LogTag != NULL) { 1927c8e2aa4SPeter Wemm (void)fprintf(fp, ": "); 1937c8e2aa4SPeter Wemm } 1947c8e2aa4SPeter Wemm 1957c8e2aa4SPeter Wemm /* Check to see if we can skip expanding the %m */ 1967c8e2aa4SPeter Wemm if (strstr(fmt, "%m")) { 1977c8e2aa4SPeter Wemm 1987c8e2aa4SPeter Wemm /* Create the second stdio hook */ 1997c8e2aa4SPeter Wemm fmt_cookie.base = fmt_cpy; 2007c8e2aa4SPeter Wemm fmt_cookie.left = sizeof(fmt_cpy) - 1; 2017c8e2aa4SPeter Wemm fmt_fp = fwopen(&fmt_cookie, writehook); 2027c8e2aa4SPeter Wemm if (fmt_fp == NULL) { 2037c8e2aa4SPeter Wemm fclose(fp); 20496575206SGleb Smirnoff THREAD_UNLOCK(); 2057c8e2aa4SPeter Wemm return; 20658f0484fSRodney W. Grimes } 20758f0484fSRodney W. Grimes 208e6cfb1ccSAlfred Perlstein /* 209e6cfb1ccSAlfred Perlstein * Substitute error message for %m. Be careful not to 210e6cfb1ccSAlfred Perlstein * molest an escaped percent "%%m". We want to pass it 211e6cfb1ccSAlfred Perlstein * on untouched as the format is later parsed by vfprintf. 212e6cfb1ccSAlfred Perlstein */ 213e6cfb1ccSAlfred Perlstein for ( ; (ch = *fmt); ++fmt) { 21458f0484fSRodney W. Grimes if (ch == '%' && fmt[1] == 'm') { 21558f0484fSRodney W. Grimes ++fmt; 21696575206SGleb Smirnoff strerror_r(saved_errno, errstr, sizeof(errstr)); 21796575206SGleb Smirnoff fputs(errstr, fmt_fp); 218e6cfb1ccSAlfred Perlstein } else if (ch == '%' && fmt[1] == '%') { 219e6cfb1ccSAlfred Perlstein ++fmt; 2207c8e2aa4SPeter Wemm fputc(ch, fmt_fp); 221e6cfb1ccSAlfred Perlstein fputc(ch, fmt_fp); 222e6cfb1ccSAlfred Perlstein } else { 223e6cfb1ccSAlfred Perlstein fputc(ch, fmt_fp); 224e6cfb1ccSAlfred Perlstein } 225e6cfb1ccSAlfred Perlstein } 22658f0484fSRodney W. Grimes 2277c8e2aa4SPeter Wemm /* Null terminate if room */ 2287c8e2aa4SPeter Wemm fputc(0, fmt_fp); 2297c8e2aa4SPeter Wemm fclose(fmt_fp); 2307c8e2aa4SPeter Wemm 2317c8e2aa4SPeter Wemm /* Guarantee null termination */ 2327c8e2aa4SPeter Wemm fmt_cpy[sizeof(fmt_cpy) - 1] = '\0'; 2337c8e2aa4SPeter Wemm 2347c8e2aa4SPeter Wemm fmt = fmt_cpy; 2357c8e2aa4SPeter Wemm } 2367c8e2aa4SPeter Wemm 2377c8e2aa4SPeter Wemm (void)vfprintf(fp, fmt, ap); 2387c8e2aa4SPeter Wemm (void)fclose(fp); 2397c8e2aa4SPeter Wemm 2407c8e2aa4SPeter Wemm cnt = sizeof(tbuf) - tbuf_cookie.left; 24158f0484fSRodney W. Grimes 242857b57eaSDiomidis Spinellis /* Remove a trailing newline */ 243857b57eaSDiomidis Spinellis if (tbuf[cnt - 1] == '\n') 244857b57eaSDiomidis Spinellis cnt--; 245857b57eaSDiomidis Spinellis 24658f0484fSRodney W. Grimes /* Output to stderr if requested. */ 24758f0484fSRodney W. Grimes if (LogStat & LOG_PERROR) { 24858f0484fSRodney W. Grimes struct iovec iov[2]; 249b231cb39SDavid E. O'Brien struct iovec *v = iov; 25058f0484fSRodney W. Grimes 25158f0484fSRodney W. Grimes v->iov_base = stdp; 25258f0484fSRodney W. Grimes v->iov_len = cnt - (stdp - tbuf); 25358f0484fSRodney W. Grimes ++v; 25458f0484fSRodney W. Grimes v->iov_base = "\n"; 25558f0484fSRodney W. Grimes v->iov_len = 1; 256d201fe46SDaniel Eischen (void)_writev(STDERR_FILENO, iov, 2); 25758f0484fSRodney W. Grimes } 25858f0484fSRodney W. Grimes 25958f0484fSRodney W. Grimes /* Get connected, output the message to the local logger. */ 2607e6ace85SPeter Wemm if (!opened) 26196575206SGleb Smirnoff openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0); 2627e6ace85SPeter Wemm connectlog(); 2637e6ace85SPeter Wemm 2647e6ace85SPeter Wemm /* 2652e89951bSGleb Smirnoff * If the send() failed, there are two likely scenarios: 2662e89951bSGleb Smirnoff * 1) syslogd was restarted 267240d5a9bSGleb Smirnoff * 2) /var/run/log is out of socket buffer space, which 268240d5a9bSGleb Smirnoff * in most cases means local DoS. 26905824745SDavid E. O'Brien * We attempt to reconnect to /var/run/log[priv] to take care of 2702e89951bSGleb Smirnoff * case #1 and keep send()ing data to cover case #2 2712e89951bSGleb Smirnoff * to give syslogd a chance to empty its socket buffer. 272240d5a9bSGleb Smirnoff * 273240d5a9bSGleb Smirnoff * If we are working with a priveleged socket, then take 274240d5a9bSGleb Smirnoff * only one attempt, because we don't want to freeze a 275240d5a9bSGleb Smirnoff * critical application like su(1) or sshd(8). 276240d5a9bSGleb Smirnoff * 2777e6ace85SPeter Wemm */ 2782e89951bSGleb Smirnoff 2792e89951bSGleb Smirnoff if (send(LogFile, tbuf, cnt, 0) < 0) { 2802e89951bSGleb Smirnoff if (errno != ENOBUFS) { 2817e6ace85SPeter Wemm disconnectlog(); 2827e6ace85SPeter Wemm connectlog(); 2832e89951bSGleb Smirnoff } 2842e89951bSGleb Smirnoff do { 28505824745SDavid E. O'Brien if (status == CONNPRIV) 28605824745SDavid E. O'Brien break; 287ed0b0abcSDaniel Eischen _usleep(1); 28896575206SGleb Smirnoff if (send(LogFile, tbuf, cnt, 0) >= 0) { 28996575206SGleb Smirnoff THREAD_UNLOCK(); 2908ba85e77SGleb Smirnoff return; 29196575206SGleb Smirnoff } 2922e89951bSGleb Smirnoff } while (errno == ENOBUFS); 29396575206SGleb Smirnoff } else { 29496575206SGleb Smirnoff THREAD_UNLOCK(); 2958ba85e77SGleb Smirnoff return; 29696575206SGleb Smirnoff } 29758f0484fSRodney W. Grimes 29858f0484fSRodney W. Grimes /* 29994998878SDavid Malone * Output the message to the console; try not to block 30094998878SDavid Malone * as a blocking console should not stop other processes. 30194998878SDavid Malone * Make sure the error reported is the one from the syslogd failure. 30258f0484fSRodney W. Grimes */ 30358f0484fSRodney W. Grimes if (LogStat & LOG_CONS && 30494998878SDavid Malone (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) { 30511e67a9fSPeter Wemm struct iovec iov[2]; 306b231cb39SDavid E. O'Brien struct iovec *v = iov; 30711e67a9fSPeter Wemm 30811e67a9fSPeter Wemm p = strchr(tbuf, '>') + 1; 30911e67a9fSPeter Wemm v->iov_base = p; 31011e67a9fSPeter Wemm v->iov_len = cnt - (p - tbuf); 31111e67a9fSPeter Wemm ++v; 31211e67a9fSPeter Wemm v->iov_base = "\r\n"; 31311e67a9fSPeter Wemm v->iov_len = 2; 314d201fe46SDaniel Eischen (void)_writev(fd, iov, 2); 3159233c4d9SJason Evans (void)_close(fd); 31658f0484fSRodney W. Grimes } 31796575206SGleb Smirnoff 31896575206SGleb Smirnoff THREAD_UNLOCK(); 31958f0484fSRodney W. Grimes } 32096575206SGleb Smirnoff 32196575206SGleb Smirnoff /* Should be called with mutex acquired */ 3227e6ace85SPeter Wemm static void 323fd42c4d8SStefan Farfeleder disconnectlog(void) 3247e6ace85SPeter Wemm { 3257e6ace85SPeter Wemm /* 3267e6ace85SPeter Wemm * If the user closed the FD and opened another in the same slot, 3277e6ace85SPeter Wemm * that's their problem. They should close it before calling on 3287e6ace85SPeter Wemm * system services. 3297e6ace85SPeter Wemm */ 3307e6ace85SPeter Wemm if (LogFile != -1) { 3319233c4d9SJason Evans _close(LogFile); 3327e6ace85SPeter Wemm LogFile = -1; 3337e6ace85SPeter Wemm } 334240d5a9bSGleb Smirnoff status = NOCONN; /* retry connect */ 3357e6ace85SPeter Wemm } 3367e6ace85SPeter Wemm 33796575206SGleb Smirnoff /* Should be called with mutex acquired */ 3387e6ace85SPeter Wemm static void 339fd42c4d8SStefan Farfeleder connectlog(void) 3407e6ace85SPeter Wemm { 341d584948eSBrian Somers struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ 342cf49f439SJohn Polstra 3437e6ace85SPeter Wemm if (LogFile == -1) { 344d201fe46SDaniel Eischen if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 3457e6ace85SPeter Wemm return; 346af478295SSergey Kandaurov (void)_fcntl(LogFile, F_SETFD, FD_CLOEXEC); 3477e6ace85SPeter Wemm } 348240d5a9bSGleb Smirnoff if (LogFile != -1 && status == NOCONN) { 349d584948eSBrian Somers SyslogAddr.sun_len = sizeof(SyslogAddr); 350d584948eSBrian Somers SyslogAddr.sun_family = AF_UNIX; 351240d5a9bSGleb Smirnoff 352240d5a9bSGleb Smirnoff /* 353240d5a9bSGleb Smirnoff * First try priveleged socket. If no success, 354240d5a9bSGleb Smirnoff * then try default socket. 355240d5a9bSGleb Smirnoff */ 356240d5a9bSGleb Smirnoff (void)strncpy(SyslogAddr.sun_path, _PATH_LOG_PRIV, 357240d5a9bSGleb Smirnoff sizeof SyslogAddr.sun_path); 358240d5a9bSGleb Smirnoff if (_connect(LogFile, (struct sockaddr *)&SyslogAddr, 359240d5a9bSGleb Smirnoff sizeof(SyslogAddr)) != -1) 360240d5a9bSGleb Smirnoff status = CONNPRIV; 361240d5a9bSGleb Smirnoff 362240d5a9bSGleb Smirnoff if (status == NOCONN) { 363d584948eSBrian Somers (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, 3640b3b961eSBrian Somers sizeof SyslogAddr.sun_path); 365240d5a9bSGleb Smirnoff if (_connect(LogFile, (struct sockaddr *)&SyslogAddr, 366240d5a9bSGleb Smirnoff sizeof(SyslogAddr)) != -1) 367240d5a9bSGleb Smirnoff status = CONNDEF; 368240d5a9bSGleb Smirnoff } 369cf49f439SJohn Polstra 370240d5a9bSGleb Smirnoff if (status == NOCONN) { 371cf49f439SJohn Polstra /* 372cf49f439SJohn Polstra * Try the old "/dev/log" path, for backward 373cf49f439SJohn Polstra * compatibility. 374cf49f439SJohn Polstra */ 375d584948eSBrian Somers (void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG, 3760b3b961eSBrian Somers sizeof SyslogAddr.sun_path); 377240d5a9bSGleb Smirnoff if (_connect(LogFile, (struct sockaddr *)&SyslogAddr, 378240d5a9bSGleb Smirnoff sizeof(SyslogAddr)) != -1) 379240d5a9bSGleb Smirnoff status = CONNDEF; 380cf49f439SJohn Polstra } 381cf49f439SJohn Polstra 382240d5a9bSGleb Smirnoff if (status == NOCONN) { 3839233c4d9SJason Evans (void)_close(LogFile); 3847e6ace85SPeter Wemm LogFile = -1; 385cf49f439SJohn Polstra } 3867e6ace85SPeter Wemm } 3877e6ace85SPeter Wemm } 3887e6ace85SPeter Wemm 38996575206SGleb Smirnoff static void 390fd42c4d8SStefan Farfeleder openlog_unlocked(const char *ident, int logstat, int logfac) 39158f0484fSRodney W. Grimes { 39258f0484fSRodney W. Grimes if (ident != NULL) 39358f0484fSRodney W. Grimes LogTag = ident; 39458f0484fSRodney W. Grimes LogStat = logstat; 39558f0484fSRodney W. Grimes if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 39658f0484fSRodney W. Grimes LogFacility = logfac; 39758f0484fSRodney W. Grimes 3987e6ace85SPeter Wemm if (LogStat & LOG_NDELAY) /* open immediately */ 3997e6ace85SPeter Wemm connectlog(); 4007e6ace85SPeter Wemm 4017e6ace85SPeter Wemm opened = 1; /* ident and facility has been set */ 40258f0484fSRodney W. Grimes } 40358f0484fSRodney W. Grimes 40458f0484fSRodney W. Grimes void 405fd42c4d8SStefan Farfeleder openlog(const char *ident, int logstat, int logfac) 40696575206SGleb Smirnoff { 40796575206SGleb Smirnoff THREAD_LOCK(); 40896575206SGleb Smirnoff openlog_unlocked(ident, logstat, logfac); 40996575206SGleb Smirnoff THREAD_UNLOCK(); 41096575206SGleb Smirnoff } 41196575206SGleb Smirnoff 41296575206SGleb Smirnoff 41396575206SGleb Smirnoff void 414fd42c4d8SStefan Farfeleder closelog(void) 41558f0484fSRodney W. Grimes { 41696575206SGleb Smirnoff THREAD_LOCK(); 417*bf36cf8eSEitan Adler assert(LogFile >= -1); 418*bf36cf8eSEitan Adler if (LogFile != -1) { 4199233c4d9SJason Evans (void)_close(LogFile); 42058f0484fSRodney W. Grimes LogFile = -1; 421*bf36cf8eSEitan Adler } 422bedff4e8SRuslan Ermilov LogTag = NULL; 423240d5a9bSGleb Smirnoff status = NOCONN; 42496575206SGleb Smirnoff THREAD_UNLOCK(); 42558f0484fSRodney W. Grimes } 42658f0484fSRodney W. Grimes 42758f0484fSRodney W. Grimes /* setlogmask -- set the log mask level */ 42858f0484fSRodney W. Grimes int 429fd42c4d8SStefan Farfeleder setlogmask(int pmask) 43058f0484fSRodney W. Grimes { 43158f0484fSRodney W. Grimes int omask; 43258f0484fSRodney W. Grimes 43396575206SGleb Smirnoff THREAD_LOCK(); 43458f0484fSRodney W. Grimes omask = LogMask; 43558f0484fSRodney W. Grimes if (pmask != 0) 43658f0484fSRodney W. Grimes LogMask = pmask; 43796575206SGleb Smirnoff THREAD_UNLOCK(); 43858f0484fSRodney W. Grimes return (omask); 43958f0484fSRodney W. Grimes } 440