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, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 static char sccsid[] = "@(#)syslog.c 5.34 (Berkeley) 6/26/91"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/types.h> 39 #include <sys/socket.h> 40 #include <sys/file.h> 41 #include <sys/syslog.h> 42 #include <sys/uio.h> 43 #include <sys/errno.h> 44 #include <netdb.h> 45 #include <string.h> 46 #if __STDC__ 47 #include <stdarg.h> 48 #else 49 #include <varargs.h> 50 #endif 51 #include <time.h> 52 #include <unistd.h> 53 #include <paths.h> 54 #include <stdio.h> 55 56 static int LogFile = -1; /* fd for log */ 57 static int connected; /* have done connect */ 58 static int LogStat = 0; /* status bits, set by openlog() */ 59 static const char *LogTag = "syslog"; /* string to tag the entry with */ 60 static int LogFacility = LOG_USER; /* default facility code */ 61 static int LogMask = 0xff; /* mask of priorities to be logged */ 62 63 /* 64 * syslog, vsyslog -- 65 * print message on log file; output is intended for syslogd(8). 66 */ 67 void 68 #if __STDC__ 69 syslog(int pri, const char *fmt, ...) 70 #else 71 syslog(pri, fmt, va_alist) 72 int pri; 73 char *fmt; 74 va_dcl 75 #endif 76 { 77 va_list ap; 78 79 #if __STDC__ 80 va_start(ap, fmt); 81 #else 82 va_start(ap); 83 #endif 84 vsyslog(pri, fmt, ap); 85 va_end(ap); 86 } 87 88 void 89 vsyslog(pri, fmt, ap) 90 int pri; 91 register const char *fmt; 92 va_list ap; 93 { 94 register int cnt; 95 register char *p; 96 time_t now, time(); 97 int fd, saved_errno; 98 char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); 99 100 /* check for invalid bits or no priority set */ 101 if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)) || 102 !(LOG_MASK(pri) & LogMask)) 103 return; 104 105 saved_errno = errno; 106 107 /* set default facility if none specified */ 108 if ((pri & LOG_FACMASK) == 0) 109 pri |= LogFacility; 110 111 /* build the message */ 112 (void)time(&now); 113 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 114 for (p = tbuf; *p; ++p); 115 if (LogStat & LOG_PERROR) 116 stdp = p; 117 if (LogTag) { 118 (void)strcpy(p, LogTag); 119 for (; *p; ++p); 120 } 121 if (LogStat & LOG_PID) { 122 (void)sprintf(p, "[%d]", getpid()); 123 for (; *p; ++p); 124 } 125 if (LogTag) { 126 *p++ = ':'; 127 *p++ = ' '; 128 } 129 130 /* substitute error message for %m */ 131 { 132 register char ch, *t1, *t2; 133 char *strerror(); 134 135 for (t1 = fmt_cpy; ch = *fmt; ++fmt) 136 if (ch == '%' && fmt[1] == 'm') { 137 ++fmt; 138 for (t2 = strerror(saved_errno); 139 *t1 = *t2++; ++t1); 140 } 141 else 142 *t1++ = ch; 143 *t1 = '\0'; 144 } 145 146 (void)vsprintf(p, fmt_cpy, ap); 147 148 cnt = strlen(tbuf); 149 150 /* output to stderr if requested */ 151 if (LogStat & LOG_PERROR) { 152 struct iovec iov[2]; 153 register struct iovec *v = iov; 154 155 v->iov_base = stdp; 156 v->iov_len = cnt - (stdp - tbuf); 157 ++v; 158 v->iov_base = "\n"; 159 v->iov_len = 1; 160 (void)writev(STDERR_FILENO, iov, 2); 161 } 162 163 /* get connected, output the message to the local logger */ 164 if (!connected) 165 openlog(LogTag, LogStat | LOG_NDELAY, 0); 166 if (send(LogFile, tbuf, cnt, 0) >= 0) 167 return; 168 169 /* see if should attempt the console */ 170 if (!(LogStat&LOG_CONS)) 171 return; 172 173 /* 174 * Output the message to the console; don't worry about blocking, 175 * if console blocks everything will. Make sure the error reported 176 * is the one from the syslogd failure. 177 */ 178 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { 179 (void)strcat(tbuf, "\r\n"); 180 cnt += 2; 181 p = index(tbuf, '>') + 1; 182 (void)write(fd, p, cnt - (p - tbuf)); 183 (void)close(fd); 184 } 185 } 186 187 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 188 189 void 190 openlog(ident, logstat, logfac) 191 const char *ident; 192 int logstat, logfac; 193 { 194 if (ident != NULL) 195 LogTag = ident; 196 LogStat = logstat; 197 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 198 LogFacility = logfac; 199 200 if (LogFile == -1) { 201 SyslogAddr.sa_family = AF_UNIX; 202 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, 203 sizeof(SyslogAddr.sa_data)); 204 if (LogStat & LOG_NDELAY) { 205 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 206 return; 207 (void)fcntl(LogFile, F_SETFD, 1); 208 } 209 } 210 if (LogFile != -1 && !connected) 211 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { 212 (void)close(LogFile); 213 LogFile = -1; 214 } else 215 connected = 1; 216 } 217 218 void 219 closelog() 220 { 221 (void)close(LogFile); 222 LogFile = -1; 223 connected = 0; 224 } 225 226 /* setlogmask -- set the log mask level */ 227 setlogmask(pmask) 228 int pmask; 229 { 230 int omask; 231 232 omask = LogMask; 233 if (pmask != 0) 234 LogMask = pmask; 235 return (omask); 236 } 237