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.26 (Berkeley) 05/15/90"; 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 <sys/uio.h> 44 #include <sys/wait.h> 45 #include <netdb.h> 46 #include <string.h> 47 #include <varargs.h> 48 #include <paths.h> 49 #include <stdio.h> 50 51 #define _PATH_LOGNAME "/dev/log" 52 53 static int LogFile = -1; /* fd for log */ 54 static int connected; /* have done connect */ 55 static int LogStat = 0; /* status bits, set by openlog() */ 56 static char *LogTag = "syslog"; /* string to tag the entry with */ 57 static int LogFacility = LOG_USER; /* default facility code */ 58 59 syslog(pri, fmt, args) 60 int pri, args; 61 char *fmt; 62 { 63 vsyslog(pri, fmt, &args); 64 } 65 66 vsyslog(pri, fmt, ap) 67 int pri; 68 register char *fmt; 69 va_list ap; 70 { 71 extern int errno; 72 register int cnt; 73 register char *p; 74 time_t now, time(); 75 int fd, saved_errno; 76 char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); 77 78 saved_errno = errno; 79 80 /* see if we should just throw out this message */ 81 if (!LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 82 return; 83 if (LogFile < 0 || !connected) 84 openlog(LogTag, LogStat | LOG_NDELAY, 0); 85 86 /* set default facility if none specified */ 87 if ((pri & LOG_FACMASK) == 0) 88 pri |= LogFacility; 89 90 /* build the message */ 91 (void)time(&now); 92 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 93 for (p = tbuf; *p; ++p); 94 if (LogStat & LOG_PERROR) 95 stdp = p; 96 if (LogTag) { 97 (void)strcpy(p, LogTag); 98 for (; *p; ++p); 99 } 100 if (LogStat & LOG_PID) { 101 (void)sprintf(p, "[%d]", getpid()); 102 for (; *p; ++p); 103 } 104 if (LogTag) { 105 *p++ = ':'; 106 *p++ = ' '; 107 } 108 109 /* substitute error message for %m */ 110 { 111 register char ch, *t1, *t2; 112 char *strerror(); 113 114 for (t1 = fmt_cpy; ch = *fmt; ++fmt) 115 if (ch == '%' && fmt[1] == 'm') { 116 ++fmt; 117 for (t2 = strerror(saved_errno); 118 *t1 = *t2++; ++t1); 119 } 120 else 121 *t1++ = ch; 122 *t1 = '\0'; 123 } 124 125 (void)vsprintf(p, fmt_cpy, ap); 126 127 cnt = strlen(tbuf); 128 129 /* output to stderr if requested */ 130 if (LogStat & LOG_PERROR) { 131 struct iovec iov[2]; 132 register struct iovec *v = iov; 133 134 v->iov_base = stdp; 135 v->iov_len = cnt - (stdp - tbuf); 136 ++v; 137 v->iov_base = "\n"; 138 v->iov_len = 1; 139 (void)writev(2, iov, 2); 140 } 141 142 /* output the message to the local logger */ 143 if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS)) 144 return; 145 146 /* 147 * output the message to the console; don't worry about 148 * blocking, if console blocks everything will. 149 */ 150 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) < 0) 151 return; 152 (void)strcat(tbuf, "\r\n"); 153 p = index(tbuf, '>') + 1; 154 (void)write(fd, p, cnt + 1 - (p - tbuf)); 155 (void)close(fd); 156 } 157 158 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 159 /* 160 * OPENLOG -- open system log 161 */ 162 openlog(ident, logstat, logfac) 163 char *ident; 164 int logstat, logfac; 165 { 166 if (ident != NULL) 167 LogTag = ident; 168 LogStat = logstat; 169 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 170 LogFacility = logfac; 171 if (LogFile == -1) { 172 SyslogAddr.sa_family = AF_UNIX; 173 strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, 174 sizeof(SyslogAddr.sa_data)); 175 if (LogStat & LOG_NDELAY) { 176 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 177 fcntl(LogFile, F_SETFD, 1); 178 } 179 } 180 if (LogFile != -1 && !connected && 181 connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 182 connected = 1; 183 } 184 185 /* 186 * CLOSELOG -- close the system log 187 */ 188 closelog() 189 { 190 (void) close(LogFile); 191 LogFile = -1; 192 connected = 0; 193 } 194 195 static int LogMask = 0xff; /* mask of priorities to be logged */ 196 /* 197 * SETLOGMASK -- set the log mask level 198 */ 199 setlogmask(pmask) 200 int pmask; 201 { 202 int omask; 203 204 omask = LogMask; 205 if (pmask != 0) 206 LogMask = pmask; 207 return (omask); 208 } 209