121362Sdist /* 233786Skarels * Copyright (c) 1983, 1988 Regents of the University of California. 333786Skarels * All rights reserved. 433786Skarels * 533786Skarels * Redistribution and use in source and binary forms are permitted 634821Sbostic * provided that the above copyright notice and this paragraph are 734821Sbostic * duplicated in all such forms and that any documentation, 834821Sbostic * advertising materials, and other materials related to such 934821Sbostic * distribution and use acknowledge that the software was developed 1034821Sbostic * by the University of California, Berkeley. The name of the 1134821Sbostic * University may not be used to endorse or promote products derived 1234821Sbostic * from this software without specific prior written permission. 1334821Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434821Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534821Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621362Sdist */ 1721362Sdist 1826602Sdonn #if defined(LIBC_SCCS) && !defined(lint) 19*38567Sbostic static char sccsid[] = "@(#)syslog.c 5.23 (Berkeley) 08/04/89"; 2033786Skarels #endif /* LIBC_SCCS and not lint */ 2112742Ssam 2212742Ssam /* 2312742Ssam * SYSLOG -- print message on log file 2412742Ssam * 2536440Sbostic * This routine looks a lot like printf, except that it outputs to the 2636440Sbostic * log file instead of the standard output. Also: 2716414Sralph * adds a timestamp, 2816414Sralph * prints the module name in front of the message, 2916414Sralph * has some other formatting types (or will sometime), 3016414Sralph * adds a newline on the end of the message. 3112742Ssam * 3236440Sbostic * The output of this routine is intended to be read by syslogd(8). 3316943Sralph * 3416943Sralph * Author: Eric Allman 3516943Sralph * Modified to use UNIX domain IPC by Ralph Campbell 3612742Ssam */ 3716414Sralph 3812742Ssam #include <sys/types.h> 3912742Ssam #include <sys/socket.h> 4016414Sralph #include <sys/file.h> 4126870Skarels #include <sys/signal.h> 4226870Skarels #include <sys/syslog.h> 4336792Sbostic #include <sys/uio.h> 4438557Sbostic #include <sys/wait.h> 4512742Ssam #include <netdb.h> 4626870Skarels #include <strings.h> 4736442Sbostic #include <varargs.h> 4838557Sbostic #include <paths.h> 4936440Sbostic #include <stdio.h> 5012742Ssam 5138557Sbostic #define _PATH_LOGNAME "/dev/log" 5212742Ssam 5316414Sralph static int LogFile = -1; /* fd for log */ 5434563Skarels static int connected; /* have done connect */ 5536440Sbostic static int LogStat = 0; /* status bits, set by openlog() */ 5624846Seric static char *LogTag = "syslog"; /* string to tag the entry with */ 5724846Seric static int LogFacility = LOG_USER; /* default facility code */ 5812742Ssam 5936440Sbostic syslog(pri, fmt, args) 6036440Sbostic int pri, args; 6112742Ssam char *fmt; 6212742Ssam { 6336442Sbostic vsyslog(pri, fmt, &args); 6436442Sbostic } 6536442Sbostic 6636442Sbostic vsyslog(pri, fmt, ap) 6736442Sbostic int pri; 6836443Sbostic register char *fmt; 6936442Sbostic va_list ap; 7036442Sbostic { 7136443Sbostic extern int errno; 7236440Sbostic register int cnt; 7336440Sbostic register char *p; 7436440Sbostic time_t now, time(); 75*38567Sbostic int fd, saved_errno; 7636792Sbostic char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); 7712742Ssam 7836443Sbostic saved_errno = errno; 7936443Sbostic 8012742Ssam /* see if we should just throw out this message */ 8136440Sbostic if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || 8236440Sbostic !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 8312742Ssam return; 8434563Skarels if (LogFile < 0 || !connected) 8525187Seric openlog(LogTag, LogStat | LOG_NDELAY, 0); 8624846Seric 8724846Seric /* set default facility if none specified */ 8824846Seric if ((pri & LOG_FACMASK) == 0) 8924846Seric pri |= LogFacility; 9024846Seric 9124846Seric /* build the message */ 9236440Sbostic (void)time(&now); 9336440Sbostic (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 9436440Sbostic for (p = tbuf; *p; ++p); 9536792Sbostic if (LogStat & LOG_PERROR) 9636792Sbostic stdp = p; 9716414Sralph if (LogTag) { 9836440Sbostic (void)strcpy(p, LogTag); 9936440Sbostic for (; *p; ++p); 10016414Sralph } 10116414Sralph if (LogStat & LOG_PID) { 10236440Sbostic (void)sprintf(p, "[%d]", getpid()); 10336440Sbostic for (; *p; ++p); 10416414Sralph } 10524846Seric if (LogTag) { 10636440Sbostic *p++ = ':'; 10736440Sbostic *p++ = ' '; 10824846Seric } 10912742Ssam 11036443Sbostic /* substitute error message for %m */ 11136443Sbostic { 11236443Sbostic register char ch, *t1, *t2; 11336443Sbostic char *strerror(); 11424846Seric 11536443Sbostic for (t1 = fmt_cpy; ch = *fmt; ++fmt) 11636443Sbostic if (ch == '%' && fmt[1] == 'm') { 11736443Sbostic ++fmt; 11836443Sbostic for (t2 = strerror(saved_errno); 11936443Sbostic *t1 = *t2++; ++t1); 12036443Sbostic } 12136443Sbostic else 12236443Sbostic *t1++ = ch; 12336583Sbostic *t1 = '\0'; 12436443Sbostic } 12536443Sbostic 12636443Sbostic (void)vsprintf(p, fmt_cpy, ap); 12736443Sbostic 12836792Sbostic cnt = strlen(tbuf); 12936792Sbostic 13036792Sbostic /* output to stderr if requested */ 13136792Sbostic if (LogStat & LOG_PERROR) { 13236792Sbostic struct iovec iov[2]; 13336792Sbostic register struct iovec *v = iov; 13436792Sbostic 13536792Sbostic v->iov_base = stdp; 13636792Sbostic v->iov_len = cnt - (stdp - tbuf); 13736792Sbostic ++v; 13836792Sbostic v->iov_base = "\n"; 13936792Sbostic v->iov_len = 1; 14036792Sbostic (void)writev(2, iov, 2); 14136792Sbostic } 14236792Sbostic 14324846Seric /* output the message to the local logger */ 14438557Sbostic if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS)) 14516414Sralph return; 14624846Seric 147*38567Sbostic /* 148*38567Sbostic * output the message to the console; don't worry about 149*38567Sbostic * blocking, if console blocks everything will. 150*38567Sbostic */ 151*38567Sbostic if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) < 0) 15216414Sralph return; 153*38567Sbostic (void)strcat(tbuf, "\r"); 154*38567Sbostic p = index(tbuf, '>') + 1; 155*38567Sbostic (void)write(fd, p, cnt + 1 - (p - tbuf)); 156*38567Sbostic (void)close(fd); 15712742Ssam } 15812742Ssam 15936440Sbostic static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 16012742Ssam /* 16112742Ssam * OPENLOG -- open system log 16212742Ssam */ 16324846Seric openlog(ident, logstat, logfac) 16412742Ssam char *ident; 16524846Seric int logstat, logfac; 16612742Ssam { 16724846Seric if (ident != NULL) 16824846Seric LogTag = ident; 16912742Ssam LogStat = logstat; 17033786Skarels if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 17133786Skarels LogFacility = logfac; 17234563Skarels if (LogFile == -1) { 17334563Skarels SyslogAddr.sa_family = AF_UNIX; 17438557Sbostic strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, 17538557Sbostic sizeof(SyslogAddr.sa_data)); 17634563Skarels if (LogStat & LOG_NDELAY) { 17734563Skarels LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 17834563Skarels fcntl(LogFile, F_SETFD, 1); 17934563Skarels } 18017918Sralph } 18134563Skarels if (LogFile != -1 && !connected && 18234563Skarels connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 18334563Skarels connected = 1; 18412742Ssam } 18512742Ssam 18612742Ssam /* 18712742Ssam * CLOSELOG -- close the system log 18812742Ssam */ 18912742Ssam closelog() 19012742Ssam { 19112742Ssam (void) close(LogFile); 19212742Ssam LogFile = -1; 19334563Skarels connected = 0; 19412742Ssam } 19516414Sralph 19636440Sbostic static int LogMask = 0xff; /* mask of priorities to be logged */ 19716414Sralph /* 19816414Sralph * SETLOGMASK -- set the log mask level 19916414Sralph */ 20017526Sralph setlogmask(pmask) 20117526Sralph int pmask; 20216414Sralph { 20317526Sralph int omask; 20416414Sralph 20517526Sralph omask = LogMask; 20617526Sralph if (pmask != 0) 20717526Sralph LogMask = pmask; 20817526Sralph return (omask); 20916414Sralph } 210