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*36440Sbostic static char sccsid[] = "@(#)syslog.c 5.17 (Berkeley) 12/19/88"; 2033786Skarels #endif /* LIBC_SCCS and not lint */ 2112742Ssam 2212742Ssam /* 2312742Ssam * SYSLOG -- print message on log file 2412742Ssam * 25*36440Sbostic * This routine looks a lot like printf, except that it outputs to the 26*36440Sbostic * 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 * 32*36440Sbostic * 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*36440Sbostic #include <stdio.h> 4612742Ssam 47*36440Sbostic #define LOGNAME "/dev/log" 48*36440Sbostic #define CONSOLE "/dev/console" 4912742Ssam 5016414Sralph static int LogFile = -1; /* fd for log */ 5134563Skarels static int connected; /* have done connect */ 52*36440Sbostic static int LogStat = 0; /* status bits, set by openlog() */ 5324846Seric static char *LogTag = "syslog"; /* string to tag the entry with */ 5424846Seric static int LogFacility = LOG_USER; /* default facility code */ 5512742Ssam 56*36440Sbostic syslog(pri, fmt, args) 57*36440Sbostic int pri, args; 5812742Ssam char *fmt; 5912742Ssam { 60*36440Sbostic register int cnt; 61*36440Sbostic register char *p; 62*36440Sbostic time_t now, time(); 63*36440Sbostic int pid; 64*36440Sbostic char tbuf[2048], *ctime(); 6512742Ssam 6612742Ssam /* see if we should just throw out this message */ 67*36440Sbostic if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || 68*36440Sbostic !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 6912742Ssam return; 7034563Skarels if (LogFile < 0 || !connected) 7125187Seric openlog(LogTag, LogStat | LOG_NDELAY, 0); 7224846Seric 7324846Seric /* set default facility if none specified */ 7424846Seric if ((pri & LOG_FACMASK) == 0) 7524846Seric pri |= LogFacility; 7624846Seric 7724846Seric /* build the message */ 78*36440Sbostic (void)time(&now); 79*36440Sbostic (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 80*36440Sbostic for (p = tbuf; *p; ++p); 8116414Sralph if (LogTag) { 82*36440Sbostic (void)strcpy(p, LogTag); 83*36440Sbostic for (; *p; ++p); 8416414Sralph } 8516414Sralph if (LogStat & LOG_PID) { 86*36440Sbostic (void)sprintf(p, "[%d]", getpid()); 87*36440Sbostic for (; *p; ++p); 8816414Sralph } 8924846Seric if (LogTag) { 90*36440Sbostic *p++ = ':'; 91*36440Sbostic *p++ = ' '; 9224846Seric } 9312742Ssam 94*36440Sbostic (void)vsprintf(p, fmt, &args); 9524846Seric 9624846Seric /* output the message to the local logger */ 97*36440Sbostic if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 || 98*36440Sbostic !(LogStat&LOG_CONS)) 9916414Sralph return; 10024846Seric 10124846Seric /* output the message to the console */ 10226508Skarels pid = vfork(); 10316414Sralph if (pid == -1) 10416414Sralph return; 10516414Sralph if (pid == 0) { 10627795Skarels int fd; 107*36440Sbostic long sigsetmask(); 10827795Skarels 109*36440Sbostic (void)signal(SIGALRM, SIG_DFL); 110*36440Sbostic sigsetmask((long)~sigmask(SIGALRM)); 111*36440Sbostic (void)alarm((u_int)5); 112*36440Sbostic if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0) 113*36440Sbostic return; 114*36440Sbostic (void)alarm((u_int)0); 115*36440Sbostic (void)strcat(tbuf, "\r"); 116*36440Sbostic p = index(tbuf, '>') + 1; 117*36440Sbostic (void)write(fd, p, cnt + 1 - (p - tbuf)); 118*36440Sbostic (void)close(fd); 11927795Skarels _exit(0); 12016414Sralph } 12127343Skarels if (!(LogStat & LOG_NOWAIT)) 122*36440Sbostic while ((cnt = wait((int *)0)) > 0 && cnt != pid); 12312742Ssam } 12412742Ssam 125*36440Sbostic static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 12612742Ssam /* 12712742Ssam * OPENLOG -- open system log 12812742Ssam */ 12924846Seric openlog(ident, logstat, logfac) 13012742Ssam char *ident; 13124846Seric int logstat, logfac; 13212742Ssam { 13324846Seric if (ident != NULL) 13424846Seric LogTag = ident; 13512742Ssam LogStat = logstat; 13633786Skarels if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 13733786Skarels LogFacility = logfac; 13834563Skarels if (LogFile == -1) { 13934563Skarels SyslogAddr.sa_family = AF_UNIX; 140*36440Sbostic strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data); 14134563Skarels if (LogStat & LOG_NDELAY) { 14234563Skarels LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 14334563Skarels fcntl(LogFile, F_SETFD, 1); 14434563Skarels } 14517918Sralph } 14634563Skarels if (LogFile != -1 && !connected && 14734563Skarels connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 14834563Skarels connected = 1; 14912742Ssam } 15012742Ssam 15112742Ssam /* 15212742Ssam * CLOSELOG -- close the system log 15312742Ssam */ 15412742Ssam closelog() 15512742Ssam { 15612742Ssam (void) close(LogFile); 15712742Ssam LogFile = -1; 15834563Skarels connected = 0; 15912742Ssam } 16016414Sralph 161*36440Sbostic static int LogMask = 0xff; /* mask of priorities to be logged */ 16216414Sralph /* 16316414Sralph * SETLOGMASK -- set the log mask level 16416414Sralph */ 16517526Sralph setlogmask(pmask) 16617526Sralph int pmask; 16716414Sralph { 16817526Sralph int omask; 16916414Sralph 17017526Sralph omask = LogMask; 17117526Sralph if (pmask != 0) 17217526Sralph LogMask = pmask; 17317526Sralph return (omask); 17416414Sralph } 175