1 /* 2 * Copyright (c) 1983, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static char sccsid[] = "@(#)syslog.c 5.17 (Berkeley) 12/19/88"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 /* 23 * SYSLOG -- print message on log file 24 * 25 * This routine looks a lot like printf, except that it outputs to the 26 * log file instead of the standard output. Also: 27 * adds a timestamp, 28 * prints the module name in front of the message, 29 * has some other formatting types (or will sometime), 30 * adds a newline on the end of the message. 31 * 32 * The output of this routine is intended to be read by syslogd(8). 33 * 34 * Author: Eric Allman 35 * Modified to use UNIX domain IPC by Ralph Campbell 36 */ 37 38 #include <sys/types.h> 39 #include <sys/socket.h> 40 #include <sys/file.h> 41 #include <sys/signal.h> 42 #include <sys/syslog.h> 43 #include <netdb.h> 44 #include <strings.h> 45 #include <stdio.h> 46 47 #define LOGNAME "/dev/log" 48 #define CONSOLE "/dev/console" 49 50 static int LogFile = -1; /* fd for log */ 51 static int connected; /* have done connect */ 52 static int LogStat = 0; /* status bits, set by openlog() */ 53 static char *LogTag = "syslog"; /* string to tag the entry with */ 54 static int LogFacility = LOG_USER; /* default facility code */ 55 56 syslog(pri, fmt, args) 57 int pri, args; 58 char *fmt; 59 { 60 register int cnt; 61 register char *p; 62 time_t now, time(); 63 int pid; 64 char tbuf[2048], *ctime(); 65 66 /* see if we should just throw out this message */ 67 if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || 68 !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 69 return; 70 if (LogFile < 0 || !connected) 71 openlog(LogTag, LogStat | LOG_NDELAY, 0); 72 73 /* set default facility if none specified */ 74 if ((pri & LOG_FACMASK) == 0) 75 pri |= LogFacility; 76 77 /* build the message */ 78 (void)time(&now); 79 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 80 for (p = tbuf; *p; ++p); 81 if (LogTag) { 82 (void)strcpy(p, LogTag); 83 for (; *p; ++p); 84 } 85 if (LogStat & LOG_PID) { 86 (void)sprintf(p, "[%d]", getpid()); 87 for (; *p; ++p); 88 } 89 if (LogTag) { 90 *p++ = ':'; 91 *p++ = ' '; 92 } 93 94 (void)vsprintf(p, fmt, &args); 95 96 /* output the message to the local logger */ 97 if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 || 98 !(LogStat&LOG_CONS)) 99 return; 100 101 /* output the message to the console */ 102 pid = vfork(); 103 if (pid == -1) 104 return; 105 if (pid == 0) { 106 int fd; 107 long sigsetmask(); 108 109 (void)signal(SIGALRM, SIG_DFL); 110 sigsetmask((long)~sigmask(SIGALRM)); 111 (void)alarm((u_int)5); 112 if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0) 113 return; 114 (void)alarm((u_int)0); 115 (void)strcat(tbuf, "\r"); 116 p = index(tbuf, '>') + 1; 117 (void)write(fd, p, cnt + 1 - (p - tbuf)); 118 (void)close(fd); 119 _exit(0); 120 } 121 if (!(LogStat & LOG_NOWAIT)) 122 while ((cnt = wait((int *)0)) > 0 && cnt != pid); 123 } 124 125 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 126 /* 127 * OPENLOG -- open system log 128 */ 129 openlog(ident, logstat, logfac) 130 char *ident; 131 int logstat, logfac; 132 { 133 if (ident != NULL) 134 LogTag = ident; 135 LogStat = logstat; 136 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 137 LogFacility = logfac; 138 if (LogFile == -1) { 139 SyslogAddr.sa_family = AF_UNIX; 140 strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data); 141 if (LogStat & LOG_NDELAY) { 142 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 143 fcntl(LogFile, F_SETFD, 1); 144 } 145 } 146 if (LogFile != -1 && !connected && 147 connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 148 connected = 1; 149 } 150 151 /* 152 * CLOSELOG -- close the system log 153 */ 154 closelog() 155 { 156 (void) close(LogFile); 157 LogFile = -1; 158 connected = 0; 159 } 160 161 static int LogMask = 0xff; /* mask of priorities to be logged */ 162 /* 163 * SETLOGMASK -- set the log mask level 164 */ 165 setlogmask(pmask) 166 int pmask; 167 { 168 int omask; 169 170 omask = LogMask; 171 if (pmask != 0) 172 LogMask = pmask; 173 return (omask); 174 } 175