1 /* 2 * Copyright (c) 1983, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)syslog.c 5.27 (Berkeley) 06/01/90"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 /* 13 * SYSLOG -- print message on log file 14 * 15 * This routine looks a lot like printf, except that it outputs to the 16 * log file instead of the standard output. Also: 17 * adds a timestamp, 18 * prints the module name in front of the message, 19 * has some other formatting types (or will sometime), 20 * adds a newline on the end of the message. 21 * 22 * The output of this routine is intended to be read by syslogd(8). 23 * 24 * Author: Eric Allman 25 * Modified to use UNIX domain IPC by Ralph Campbell 26 */ 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <sys/file.h> 31 #include <sys/signal.h> 32 #include <sys/syslog.h> 33 #include <sys/uio.h> 34 #include <sys/wait.h> 35 #include <netdb.h> 36 #include <string.h> 37 #include <varargs.h> 38 #include <paths.h> 39 #include <stdio.h> 40 41 #define _PATH_LOGNAME "/dev/log" 42 43 static int LogFile = -1; /* fd for log */ 44 static int connected; /* have done connect */ 45 static int LogStat = 0; /* status bits, set by openlog() */ 46 static char *LogTag = "syslog"; /* string to tag the entry with */ 47 static int LogFacility = LOG_USER; /* default facility code */ 48 49 syslog(pri, fmt, args) 50 int pri, args; 51 char *fmt; 52 { 53 vsyslog(pri, fmt, &args); 54 } 55 56 vsyslog(pri, fmt, ap) 57 int pri; 58 register char *fmt; 59 va_list ap; 60 { 61 extern int errno; 62 register int cnt; 63 register char *p; 64 time_t now, time(); 65 int fd, saved_errno; 66 char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); 67 68 saved_errno = errno; 69 70 /* see if we should just throw out this message */ 71 if (!LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 72 return; 73 if (LogFile < 0 || !connected) 74 openlog(LogTag, LogStat | LOG_NDELAY, 0); 75 76 /* set default facility if none specified */ 77 if ((pri & LOG_FACMASK) == 0) 78 pri |= LogFacility; 79 80 /* build the message */ 81 (void)time(&now); 82 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 83 for (p = tbuf; *p; ++p); 84 if (LogStat & LOG_PERROR) 85 stdp = p; 86 if (LogTag) { 87 (void)strcpy(p, LogTag); 88 for (; *p; ++p); 89 } 90 if (LogStat & LOG_PID) { 91 (void)sprintf(p, "[%d]", getpid()); 92 for (; *p; ++p); 93 } 94 if (LogTag) { 95 *p++ = ':'; 96 *p++ = ' '; 97 } 98 99 /* substitute error message for %m */ 100 { 101 register char ch, *t1, *t2; 102 char *strerror(); 103 104 for (t1 = fmt_cpy; ch = *fmt; ++fmt) 105 if (ch == '%' && fmt[1] == 'm') { 106 ++fmt; 107 for (t2 = strerror(saved_errno); 108 *t1 = *t2++; ++t1); 109 } 110 else 111 *t1++ = ch; 112 *t1 = '\0'; 113 } 114 115 (void)vsprintf(p, fmt_cpy, ap); 116 117 cnt = strlen(tbuf); 118 119 /* output to stderr if requested */ 120 if (LogStat & LOG_PERROR) { 121 struct iovec iov[2]; 122 register struct iovec *v = iov; 123 124 v->iov_base = stdp; 125 v->iov_len = cnt - (stdp - tbuf); 126 ++v; 127 v->iov_base = "\n"; 128 v->iov_len = 1; 129 (void)writev(2, iov, 2); 130 } 131 132 /* output the message to the local logger */ 133 if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS)) 134 return; 135 136 /* 137 * output the message to the console; don't worry about 138 * blocking, if console blocks everything will. 139 */ 140 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) < 0) 141 return; 142 (void)strcat(tbuf, "\r\n"); 143 p = index(tbuf, '>') + 1; 144 (void)write(fd, p, cnt + 1 - (p - tbuf)); 145 (void)close(fd); 146 } 147 148 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 149 /* 150 * OPENLOG -- open system log 151 */ 152 openlog(ident, logstat, logfac) 153 char *ident; 154 int logstat, logfac; 155 { 156 if (ident != NULL) 157 LogTag = ident; 158 LogStat = logstat; 159 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 160 LogFacility = logfac; 161 if (LogFile == -1) { 162 SyslogAddr.sa_family = AF_UNIX; 163 strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, 164 sizeof(SyslogAddr.sa_data)); 165 if (LogStat & LOG_NDELAY) { 166 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 167 fcntl(LogFile, F_SETFD, 1); 168 } 169 } 170 if (LogFile != -1 && !connected && 171 connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 172 connected = 1; 173 } 174 175 /* 176 * CLOSELOG -- close the system log 177 */ 178 closelog() 179 { 180 (void) close(LogFile); 181 LogFile = -1; 182 connected = 0; 183 } 184 185 static int LogMask = 0xff; /* mask of priorities to be logged */ 186 /* 187 * SETLOGMASK -- set the log mask level 188 */ 189 setlogmask(pmask) 190 int pmask; 191 { 192 int omask; 193 194 omask = LogMask; 195 if (pmask != 0) 196 LogMask = pmask; 197 return (omask); 198 } 199