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