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*36442Sbostic static char sccsid[] = "@(#)syslog.c 5.18 (Berkeley) 12/19/88"; 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> 4312742Ssam #include <netdb.h> 4426870Skarels #include <strings.h> 45*36442Sbostic #include <varargs.h> 4636440Sbostic #include <stdio.h> 4712742Ssam 4836440Sbostic #define LOGNAME "/dev/log" 4936440Sbostic #define CONSOLE "/dev/console" 5012742Ssam 5116414Sralph static int LogFile = -1; /* fd for log */ 5234563Skarels static int connected; /* have done connect */ 5336440Sbostic static int LogStat = 0; /* status bits, set by openlog() */ 5424846Seric static char *LogTag = "syslog"; /* string to tag the entry with */ 5524846Seric static int LogFacility = LOG_USER; /* default facility code */ 5612742Ssam 5736440Sbostic syslog(pri, fmt, args) 5836440Sbostic int pri, args; 5912742Ssam char *fmt; 6012742Ssam { 61*36442Sbostic vsyslog(pri, fmt, &args); 62*36442Sbostic } 63*36442Sbostic 64*36442Sbostic vsyslog(pri, fmt, ap) 65*36442Sbostic int pri; 66*36442Sbostic char *fmt; 67*36442Sbostic va_list ap; 68*36442Sbostic { 6936440Sbostic register int cnt; 7036440Sbostic register char *p; 7136440Sbostic time_t now, time(); 7236440Sbostic int pid; 7336440Sbostic char tbuf[2048], *ctime(); 7412742Ssam 7512742Ssam /* see if we should just throw out this message */ 7636440Sbostic if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || 7736440Sbostic !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 7812742Ssam return; 7934563Skarels if (LogFile < 0 || !connected) 8025187Seric openlog(LogTag, LogStat | LOG_NDELAY, 0); 8124846Seric 8224846Seric /* set default facility if none specified */ 8324846Seric if ((pri & LOG_FACMASK) == 0) 8424846Seric pri |= LogFacility; 8524846Seric 8624846Seric /* build the message */ 8736440Sbostic (void)time(&now); 8836440Sbostic (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 8936440Sbostic for (p = tbuf; *p; ++p); 9016414Sralph if (LogTag) { 9136440Sbostic (void)strcpy(p, LogTag); 9236440Sbostic for (; *p; ++p); 9316414Sralph } 9416414Sralph if (LogStat & LOG_PID) { 9536440Sbostic (void)sprintf(p, "[%d]", getpid()); 9636440Sbostic for (; *p; ++p); 9716414Sralph } 9824846Seric if (LogTag) { 9936440Sbostic *p++ = ':'; 10036440Sbostic *p++ = ' '; 10124846Seric } 10212742Ssam 103*36442Sbostic (void)vsprintf(p, fmt, ap); 10424846Seric 10524846Seric /* output the message to the local logger */ 10636440Sbostic if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 || 10736440Sbostic !(LogStat&LOG_CONS)) 10816414Sralph return; 10924846Seric 11024846Seric /* output the message to the console */ 11126508Skarels pid = vfork(); 11216414Sralph if (pid == -1) 11316414Sralph return; 11416414Sralph if (pid == 0) { 11527795Skarels int fd; 11636440Sbostic long sigsetmask(); 11727795Skarels 11836440Sbostic (void)signal(SIGALRM, SIG_DFL); 11936440Sbostic sigsetmask((long)~sigmask(SIGALRM)); 12036440Sbostic (void)alarm((u_int)5); 12136440Sbostic if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0) 12236440Sbostic return; 12336440Sbostic (void)alarm((u_int)0); 12436440Sbostic (void)strcat(tbuf, "\r"); 12536440Sbostic p = index(tbuf, '>') + 1; 12636440Sbostic (void)write(fd, p, cnt + 1 - (p - tbuf)); 12736440Sbostic (void)close(fd); 12827795Skarels _exit(0); 12916414Sralph } 13027343Skarels if (!(LogStat & LOG_NOWAIT)) 13136440Sbostic while ((cnt = wait((int *)0)) > 0 && cnt != pid); 13212742Ssam } 13312742Ssam 13436440Sbostic static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 13512742Ssam /* 13612742Ssam * OPENLOG -- open system log 13712742Ssam */ 13824846Seric openlog(ident, logstat, logfac) 13912742Ssam char *ident; 14024846Seric int logstat, logfac; 14112742Ssam { 14224846Seric if (ident != NULL) 14324846Seric LogTag = ident; 14412742Ssam LogStat = logstat; 14533786Skarels if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 14633786Skarels LogFacility = logfac; 14734563Skarels if (LogFile == -1) { 14834563Skarels SyslogAddr.sa_family = AF_UNIX; 14936440Sbostic strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data); 15034563Skarels if (LogStat & LOG_NDELAY) { 15134563Skarels LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 15234563Skarels fcntl(LogFile, F_SETFD, 1); 15334563Skarels } 15417918Sralph } 15534563Skarels if (LogFile != -1 && !connected && 15634563Skarels connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 15734563Skarels connected = 1; 15812742Ssam } 15912742Ssam 16012742Ssam /* 16112742Ssam * CLOSELOG -- close the system log 16212742Ssam */ 16312742Ssam closelog() 16412742Ssam { 16512742Ssam (void) close(LogFile); 16612742Ssam LogFile = -1; 16734563Skarels connected = 0; 16812742Ssam } 16916414Sralph 17036440Sbostic static int LogMask = 0xff; /* mask of priorities to be logged */ 17116414Sralph /* 17216414Sralph * SETLOGMASK -- set the log mask level 17316414Sralph */ 17417526Sralph setlogmask(pmask) 17517526Sralph int pmask; 17616414Sralph { 17717526Sralph int omask; 17816414Sralph 17917526Sralph omask = LogMask; 18017526Sralph if (pmask != 0) 18117526Sralph LogMask = pmask; 18217526Sralph return (omask); 18316414Sralph } 184