1 /* 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. 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 rcsid[] = "$OpenBSD: syslog.c,v 1.2 1996/08/19 08:26:33 tholo Exp $"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/types.h> 39 #include <sys/socket.h> 40 #include <sys/syslog.h> 41 #include <sys/uio.h> 42 #include <netdb.h> 43 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <paths.h> 47 #include <stdio.h> 48 #include <string.h> 49 #include <time.h> 50 #include <unistd.h> 51 52 #if __STDC__ 53 #include <stdarg.h> 54 #else 55 #include <varargs.h> 56 #endif 57 58 static int LogFile = -1; /* fd for log */ 59 static int connected; /* have done connect */ 60 static int LogStat = 0; /* status bits, set by openlog() */ 61 static const char *LogTag = NULL; /* string to tag the entry with */ 62 static int LogFacility = LOG_USER; /* default facility code */ 63 static int LogMask = 0xff; /* mask of priorities to be logged */ 64 extern char *__progname; /* Program name, from crt0. */ 65 66 /* 67 * syslog, vsyslog -- 68 * print message on log file; output is intended for syslogd(8). 69 */ 70 void 71 #if __STDC__ 72 syslog(int pri, const char *fmt, ...) 73 #else 74 syslog(pri, fmt, va_alist) 75 int pri; 76 char *fmt; 77 va_dcl 78 #endif 79 { 80 va_list ap; 81 82 #if __STDC__ 83 va_start(ap, fmt); 84 #else 85 va_start(ap); 86 #endif 87 vsyslog(pri, fmt, ap); 88 va_end(ap); 89 } 90 91 void 92 vsyslog(pri, fmt, ap) 93 int pri; 94 register const char *fmt; 95 va_list ap; 96 { 97 register int cnt; 98 register char ch, *p, *t; 99 time_t now; 100 int fd, saved_errno; 101 #define TBUF_LEN 2048 102 #define FMT_LEN 1024 103 char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN]; 104 int tbuf_left, fmt_left, prlen; 105 106 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 107 /* Check for invalid bits. */ 108 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 109 syslog(INTERNALLOG, 110 "syslog: unknown facility/priority: %x", pri); 111 pri &= LOG_PRIMASK|LOG_FACMASK; 112 } 113 114 /* Check priority against setlogmask values. */ 115 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) 116 return; 117 118 saved_errno = errno; 119 120 /* Set default facility if none specified. */ 121 if ((pri & LOG_FACMASK) == 0) 122 pri |= LogFacility; 123 124 /* Build the message. */ 125 126 /* 127 * Although it's tempting, we can't ignore the possibility of 128 * overflowing the buffer when assembling the "fixed" portion 129 * of the message. Strftime's "%h" directive expands to the 130 * locale's abbreviated month name, but if the user has the 131 * ability to construct to his own locale files, it may be 132 * arbitrarily long. 133 */ 134 (void)time(&now); 135 136 p = tbuf; 137 tbuf_left = TBUF_LEN; 138 139 #define DEC() \ 140 do { \ 141 if (prlen >= tbuf_left) \ 142 prlen = tbuf_left - 1; \ 143 p += prlen; \ 144 tbuf_left -= prlen; \ 145 } while (0) 146 147 prlen = snprintf(p, tbuf_left, "<%d>", pri); 148 DEC(); 149 150 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now)); 151 DEC(); 152 153 if (LogStat & LOG_PERROR) 154 stdp = p; 155 if (LogTag == NULL) 156 LogTag = __progname; 157 if (LogTag != NULL) { 158 prlen = snprintf(p, tbuf_left, "%s", LogTag); 159 DEC(); 160 } 161 if (LogStat & LOG_PID) { 162 prlen = snprintf(p, tbuf_left, "[%d]", getpid()); 163 DEC(); 164 } 165 if (LogTag != NULL) { 166 if (tbuf_left > 1) { 167 *p++ = ':'; 168 tbuf_left--; 169 } 170 if (tbuf_left > 1) { 171 *p++ = ' '; 172 tbuf_left--; 173 } 174 } 175 176 /* 177 * We wouldn't need this mess if printf handled %m, or if 178 * strerror() had been invented before syslog(). 179 */ 180 for (t = fmt_cpy, fmt_left = FMT_LEN; ch = *fmt; ++fmt) { 181 if (ch == '%' && fmt[1] == 'm') { 182 ++fmt; 183 prlen = snprintf(t, fmt_left, "%s", 184 strerror(saved_errno)); 185 if (prlen >= fmt_left) 186 prlen = fmt_left - 1; 187 t += prlen; 188 fmt_left -= prlen; 189 } else { 190 if (fmt_left > 1) { 191 *t++ = ch; 192 fmt_left--; 193 } 194 } 195 } 196 *t = '\0'; 197 198 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap); 199 DEC(); 200 cnt = p - tbuf; 201 202 /* Output to stderr if requested. */ 203 if (LogStat & LOG_PERROR) { 204 struct iovec iov[2]; 205 206 iov[0].iov_base = stdp; 207 iov[0].iov_len = cnt - (stdp - tbuf); 208 iov[1].iov_base = "\n"; 209 iov[1].iov_len = 1; 210 (void)writev(STDERR_FILENO, iov, 2); 211 } 212 213 /* Get connected, output the message to the local logger. */ 214 if (!connected) 215 openlog(LogTag, LogStat | LOG_NDELAY, 0); 216 if (send(LogFile, tbuf, cnt, 0) >= 0) 217 return; 218 219 /* 220 * Output the message to the console; don't worry about blocking, 221 * if console blocks everything will. Make sure the error reported 222 * is the one from the syslogd failure. 223 */ 224 if (LogStat & LOG_CONS && 225 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { 226 struct iovec iov[2]; 227 228 p = strchr(tbuf, '>') + 1; 229 iov[0].iov_base = p; 230 iov[0].iov_len = cnt - (p - tbuf); 231 iov[1].iov_base = "\r\n"; 232 iov[1].iov_len = 2; 233 (void)writev(fd, iov, 2); 234 (void)close(fd); 235 } 236 } 237 238 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 239 240 void 241 openlog(ident, logstat, logfac) 242 const char *ident; 243 int logstat, logfac; 244 { 245 if (ident != NULL) 246 LogTag = ident; 247 LogStat = logstat; 248 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 249 LogFacility = logfac; 250 251 if (LogFile == -1) { 252 SyslogAddr.sa_family = AF_UNIX; 253 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, 254 sizeof(SyslogAddr.sa_data)); 255 if (LogStat & LOG_NDELAY) { 256 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 257 return; 258 (void)fcntl(LogFile, F_SETFD, 1); 259 } 260 } 261 if (LogFile != -1 && !connected) 262 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { 263 (void)close(LogFile); 264 LogFile = -1; 265 } else 266 connected = 1; 267 } 268 269 void 270 closelog() 271 { 272 (void)close(LogFile); 273 LogFile = -1; 274 connected = 0; 275 } 276 277 /* setlogmask -- set the log mask level */ 278 int 279 setlogmask(pmask) 280 int pmask; 281 { 282 int omask; 283 284 omask = LogMask; 285 if (pmask != 0) 286 LogMask = pmask; 287 return (omask); 288 } 289