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*36792Sbostic static char sccsid[] = "@(#)syslog.c 5.21 (Berkeley) 02/15/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> 43*36792Sbostic #include <sys/uio.h> 4412742Ssam #include <netdb.h> 4526870Skarels #include <strings.h> 4636442Sbostic #include <varargs.h> 4736440Sbostic #include <stdio.h> 4812742Ssam 4936440Sbostic #define LOGNAME "/dev/log" 5036440Sbostic #define CONSOLE "/dev/console" 5112742Ssam 5216414Sralph static int LogFile = -1; /* fd for log */ 5334563Skarels static int connected; /* have done connect */ 5436440Sbostic static int LogStat = 0; /* status bits, set by openlog() */ 5524846Seric static char *LogTag = "syslog"; /* string to tag the entry with */ 5624846Seric static int LogFacility = LOG_USER; /* default facility code */ 5712742Ssam 5836440Sbostic syslog(pri, fmt, args) 5936440Sbostic int pri, args; 6012742Ssam char *fmt; 6112742Ssam { 6236442Sbostic vsyslog(pri, fmt, &args); 6336442Sbostic } 6436442Sbostic 6536442Sbostic vsyslog(pri, fmt, ap) 6636442Sbostic int pri; 6736443Sbostic register char *fmt; 6836442Sbostic va_list ap; 6936442Sbostic { 7036443Sbostic extern int errno; 7136440Sbostic register int cnt; 7236440Sbostic register char *p; 7336440Sbostic time_t now, time(); 7436443Sbostic int pid, saved_errno; 75*36792Sbostic char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); 7612742Ssam 7736443Sbostic saved_errno = errno; 7836443Sbostic 7912742Ssam /* see if we should just throw out this message */ 8036440Sbostic if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || 8136440Sbostic !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 8212742Ssam return; 8334563Skarels if (LogFile < 0 || !connected) 8425187Seric openlog(LogTag, LogStat | LOG_NDELAY, 0); 8524846Seric 8624846Seric /* set default facility if none specified */ 8724846Seric if ((pri & LOG_FACMASK) == 0) 8824846Seric pri |= LogFacility; 8924846Seric 9024846Seric /* build the message */ 9136440Sbostic (void)time(&now); 9236440Sbostic (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 9336440Sbostic for (p = tbuf; *p; ++p); 94*36792Sbostic if (LogStat & LOG_PERROR) 95*36792Sbostic stdp = p; 9616414Sralph if (LogTag) { 9736440Sbostic (void)strcpy(p, LogTag); 9836440Sbostic for (; *p; ++p); 9916414Sralph } 10016414Sralph if (LogStat & LOG_PID) { 10136440Sbostic (void)sprintf(p, "[%d]", getpid()); 10236440Sbostic for (; *p; ++p); 10316414Sralph } 10424846Seric if (LogTag) { 10536440Sbostic *p++ = ':'; 10636440Sbostic *p++ = ' '; 10724846Seric } 10812742Ssam 10936443Sbostic /* substitute error message for %m */ 11036443Sbostic { 11136443Sbostic register char ch, *t1, *t2; 11236443Sbostic char *strerror(); 11324846Seric 11436443Sbostic for (t1 = fmt_cpy; ch = *fmt; ++fmt) 11536443Sbostic if (ch == '%' && fmt[1] == 'm') { 11636443Sbostic ++fmt; 11736443Sbostic for (t2 = strerror(saved_errno); 11836443Sbostic *t1 = *t2++; ++t1); 11936443Sbostic } 12036443Sbostic else 12136443Sbostic *t1++ = ch; 12236583Sbostic *t1 = '\0'; 12336443Sbostic } 12436443Sbostic 12536443Sbostic (void)vsprintf(p, fmt_cpy, ap); 12636443Sbostic 127*36792Sbostic cnt = strlen(tbuf); 128*36792Sbostic 129*36792Sbostic /* output to stderr if requested */ 130*36792Sbostic if (LogStat & LOG_PERROR) { 131*36792Sbostic struct iovec iov[2]; 132*36792Sbostic register struct iovec *v = iov; 133*36792Sbostic 134*36792Sbostic v->iov_base = stdp; 135*36792Sbostic v->iov_len = cnt - (stdp - tbuf); 136*36792Sbostic ++v; 137*36792Sbostic v->iov_base = "\n"; 138*36792Sbostic v->iov_len = 1; 139*36792Sbostic (void)writev(2, iov, 2); 140*36792Sbostic } 141*36792Sbostic 14224846Seric /* output the message to the local logger */ 143*36792Sbostic if (send(LogFile, tbuf, cnt, 0) >= 0 || 14436440Sbostic !(LogStat&LOG_CONS)) 14516414Sralph return; 14624846Seric 14724846Seric /* output the message to the console */ 14826508Skarels pid = vfork(); 14916414Sralph if (pid == -1) 15016414Sralph return; 15116414Sralph if (pid == 0) { 15227795Skarels int fd; 15336440Sbostic long sigsetmask(); 15427795Skarels 15536440Sbostic (void)signal(SIGALRM, SIG_DFL); 15636440Sbostic sigsetmask((long)~sigmask(SIGALRM)); 15736440Sbostic (void)alarm((u_int)5); 15836440Sbostic if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0) 15936440Sbostic return; 16036440Sbostic (void)alarm((u_int)0); 16136440Sbostic (void)strcat(tbuf, "\r"); 16236440Sbostic p = index(tbuf, '>') + 1; 16336440Sbostic (void)write(fd, p, cnt + 1 - (p - tbuf)); 16436440Sbostic (void)close(fd); 16527795Skarels _exit(0); 16616414Sralph } 16727343Skarels if (!(LogStat & LOG_NOWAIT)) 16836440Sbostic while ((cnt = wait((int *)0)) > 0 && cnt != pid); 16912742Ssam } 17012742Ssam 17136440Sbostic static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 17212742Ssam /* 17312742Ssam * OPENLOG -- open system log 17412742Ssam */ 17524846Seric openlog(ident, logstat, logfac) 17612742Ssam char *ident; 17724846Seric int logstat, logfac; 17812742Ssam { 17924846Seric if (ident != NULL) 18024846Seric LogTag = ident; 18112742Ssam LogStat = logstat; 18233786Skarels if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 18333786Skarels LogFacility = logfac; 18434563Skarels if (LogFile == -1) { 18534563Skarels SyslogAddr.sa_family = AF_UNIX; 18636440Sbostic strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data); 18734563Skarels if (LogStat & LOG_NDELAY) { 18834563Skarels LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 18934563Skarels fcntl(LogFile, F_SETFD, 1); 19034563Skarels } 19117918Sralph } 19234563Skarels if (LogFile != -1 && !connected && 19334563Skarels connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 19434563Skarels connected = 1; 19512742Ssam } 19612742Ssam 19712742Ssam /* 19812742Ssam * CLOSELOG -- close the system log 19912742Ssam */ 20012742Ssam closelog() 20112742Ssam { 20212742Ssam (void) close(LogFile); 20312742Ssam LogFile = -1; 20434563Skarels connected = 0; 20512742Ssam } 20616414Sralph 20736440Sbostic static int LogMask = 0xff; /* mask of priorities to be logged */ 20816414Sralph /* 20916414Sralph * SETLOGMASK -- set the log mask level 21016414Sralph */ 21117526Sralph setlogmask(pmask) 21217526Sralph int pmask; 21316414Sralph { 21417526Sralph int omask; 21516414Sralph 21617526Sralph omask = LogMask; 21717526Sralph if (pmask != 0) 21817526Sralph LogMask = pmask; 21917526Sralph return (omask); 22016414Sralph } 221