1 /* $NetBSD: syslog.c,v 1.11 1997/07/13 19:34:16 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; 40 #else 41 __RCSID("$NetBSD: syslog.c,v 1.11 1997/07/13 19:34:16 christos Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include <sys/types.h> 46 #include <sys/socket.h> 47 #include <sys/syslog.h> 48 #include <sys/uio.h> 49 #include <netdb.h> 50 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <paths.h> 54 #include <stdio.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 59 #if __STDC__ 60 #include <stdarg.h> 61 #else 62 #include <varargs.h> 63 #endif 64 65 static int LogFile = -1; /* fd for log */ 66 static int connected; /* have done connect */ 67 static int LogStat = 0; /* status bits, set by openlog() */ 68 static const char *LogTag = NULL; /* string to tag the entry with */ 69 static int LogFacility = LOG_USER; /* default facility code */ 70 static int LogMask = 0xff; /* mask of priorities to be logged */ 71 extern char *__progname; /* Program name, from crt0. */ 72 73 /* 74 * syslog, vsyslog -- 75 * print message on log file; output is intended for syslogd(8). 76 */ 77 void 78 #if __STDC__ 79 syslog(int pri, const char *fmt, ...) 80 #else 81 syslog(pri, fmt, va_alist) 82 int pri; 83 char *fmt; 84 va_dcl 85 #endif 86 { 87 va_list ap; 88 89 #if __STDC__ 90 va_start(ap, fmt); 91 #else 92 va_start(ap); 93 #endif 94 vsyslog(pri, fmt, ap); 95 va_end(ap); 96 } 97 98 void 99 vsyslog(pri, fmt, ap) 100 int pri; 101 register const char *fmt; 102 va_list ap; 103 { 104 register int cnt; 105 register char ch, *p, *t; 106 time_t now; 107 int fd, saved_errno; 108 #define TBUF_LEN 2048 109 #define FMT_LEN 1024 110 char *stdp = NULL; /* pacify gcc */ 111 char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN]; 112 int tbuf_left, fmt_left, prlen; 113 114 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 115 /* Check for invalid bits. */ 116 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 117 syslog(INTERNALLOG, 118 "syslog: unknown facility/priority: %x", pri); 119 pri &= LOG_PRIMASK|LOG_FACMASK; 120 } 121 122 /* Check priority against setlogmask values. */ 123 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) 124 return; 125 126 saved_errno = errno; 127 128 /* Set default facility if none specified. */ 129 if ((pri & LOG_FACMASK) == 0) 130 pri |= LogFacility; 131 132 /* Build the message. */ 133 134 /* 135 * Although it's tempting, we can't ignore the possibility of 136 * overflowing the buffer when assembling the "fixed" portion 137 * of the message. Strftime's "%h" directive expands to the 138 * locale's abbreviated month name, but if the user has the 139 * ability to construct to his own locale files, it may be 140 * arbitrarily long. 141 */ 142 (void)time(&now); 143 144 p = tbuf; 145 tbuf_left = TBUF_LEN; 146 147 #define DEC() \ 148 do { \ 149 if (prlen >= tbuf_left) \ 150 prlen = tbuf_left - 1; \ 151 p += prlen; \ 152 tbuf_left -= prlen; \ 153 } while (0) 154 155 prlen = snprintf(p, tbuf_left, "<%d>", pri); 156 DEC(); 157 158 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now)); 159 DEC(); 160 161 if (LogStat & LOG_PERROR) 162 stdp = p; 163 if (LogTag == NULL) 164 LogTag = __progname; 165 if (LogTag != NULL) { 166 prlen = snprintf(p, tbuf_left, "%s", LogTag); 167 DEC(); 168 } 169 if (LogStat & LOG_PID) { 170 prlen = snprintf(p, tbuf_left, "[%d]", getpid()); 171 DEC(); 172 } 173 if (LogTag != NULL) { 174 if (tbuf_left > 1) { 175 *p++ = ':'; 176 tbuf_left--; 177 } 178 if (tbuf_left > 1) { 179 *p++ = ' '; 180 tbuf_left--; 181 } 182 } 183 184 /* 185 * We wouldn't need this mess if printf handled %m, or if 186 * strerror() had been invented before syslog(). 187 */ 188 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) { 189 if (ch == '%' && fmt[1] == 'm') { 190 ++fmt; 191 prlen = snprintf(t, fmt_left, "%s", 192 strerror(saved_errno)); 193 if (prlen >= fmt_left) 194 prlen = fmt_left - 1; 195 t += prlen; 196 fmt_left -= prlen; 197 } else { 198 if (fmt_left > 1) { 199 *t++ = ch; 200 fmt_left--; 201 } 202 } 203 } 204 *t = '\0'; 205 206 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap); 207 DEC(); 208 cnt = p - tbuf; 209 210 /* Output to stderr if requested. */ 211 if (LogStat & LOG_PERROR) { 212 struct iovec iov[2]; 213 214 iov[0].iov_base = stdp; 215 iov[0].iov_len = cnt - (stdp - tbuf); 216 iov[1].iov_base = "\n"; 217 iov[1].iov_len = 1; 218 (void)writev(STDERR_FILENO, iov, 2); 219 } 220 221 /* Get connected, output the message to the local logger. */ 222 if (!connected) 223 openlog(LogTag, LogStat | LOG_NDELAY, 0); 224 if (send(LogFile, tbuf, cnt, 0) >= 0) 225 return; 226 227 /* 228 * Output the message to the console; don't worry about blocking, 229 * if console blocks everything will. Make sure the error reported 230 * is the one from the syslogd failure. 231 */ 232 if (LogStat & LOG_CONS && 233 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { 234 struct iovec iov[2]; 235 236 p = strchr(tbuf, '>') + 1; 237 iov[0].iov_base = p; 238 iov[0].iov_len = cnt - (p - tbuf); 239 iov[1].iov_base = "\r\n"; 240 iov[1].iov_len = 2; 241 (void)writev(fd, iov, 2); 242 (void)close(fd); 243 } 244 } 245 246 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 247 248 void 249 openlog(ident, logstat, logfac) 250 const char *ident; 251 int logstat, logfac; 252 { 253 if (ident != NULL) 254 LogTag = ident; 255 LogStat = logstat; 256 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 257 LogFacility = logfac; 258 259 if (LogFile == -1) { 260 SyslogAddr.sa_family = AF_UNIX; 261 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, 262 sizeof(SyslogAddr.sa_data)); 263 if (LogStat & LOG_NDELAY) { 264 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 265 return; 266 (void)fcntl(LogFile, F_SETFD, 1); 267 } 268 } 269 if (LogFile != -1 && !connected) 270 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { 271 (void)close(LogFile); 272 LogFile = -1; 273 } else 274 connected = 1; 275 } 276 277 void 278 closelog() 279 { 280 (void)close(LogFile); 281 LogFile = -1; 282 connected = 0; 283 } 284 285 /* setlogmask -- set the log mask level */ 286 int 287 setlogmask(pmask) 288 int pmask; 289 { 290 int omask; 291 292 omask = LogMask; 293 if (pmask != 0) 294 LogMask = pmask; 295 return (omask); 296 } 297