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.9 2000/01/02 23:35:58 hugh 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 >= tbuf_left) \ 146 prlen = tbuf_left - 1; \ 147 p += prlen; \ 148 tbuf_left -= prlen; \ 149 } while (0) 150 151 prlen = snprintf(p, tbuf_left, "<%d>", pri); 152 DEC(); 153 154 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now)); 155 DEC(); 156 157 if (LogStat & LOG_PERROR) 158 stdp = p; 159 if (LogTag == NULL) 160 LogTag = __progname; 161 if (LogTag != NULL) { 162 prlen = snprintf(p, tbuf_left, "%s", LogTag); 163 DEC(); 164 } 165 if (LogStat & LOG_PID) { 166 prlen = snprintf(p, tbuf_left, "[%d]", getpid()); 167 DEC(); 168 } 169 if (LogTag != NULL) { 170 if (tbuf_left > 1) { 171 *p++ = ':'; 172 tbuf_left--; 173 } 174 if (tbuf_left > 1) { 175 *p++ = ' '; 176 tbuf_left--; 177 } 178 } 179 180 /* 181 * We wouldn't need this mess if printf handled %m, or if 182 * strerror() had been invented before syslog(). 183 */ 184 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) { 185 if (ch == '%' && fmt[1] == 'm') { 186 ++fmt; 187 prlen = snprintf(t, fmt_left, "%s", 188 strerror(saved_errno)); 189 if (prlen >= fmt_left) 190 prlen = fmt_left - 1; 191 t += prlen; 192 fmt_left -= prlen; 193 } else { 194 if (fmt_left > 1) { 195 *t++ = ch; 196 fmt_left--; 197 } 198 } 199 } 200 *t = '\0'; 201 202 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap); 203 DEC(); 204 cnt = p - tbuf; 205 206 /* Output to stderr if requested. */ 207 if (LogStat & LOG_PERROR) { 208 struct iovec iov[2]; 209 210 iov[0].iov_base = stdp; 211 iov[0].iov_len = cnt - (stdp - tbuf); 212 iov[1].iov_base = "\n"; 213 iov[1].iov_len = 1; 214 (void)writev(STDERR_FILENO, iov, 2); 215 } 216 217 /* Get connected, output the message to the local logger. */ 218 if (!opened) 219 openlog(LogTag, LogStat, 0); 220 connectlog(); 221 if (send(LogFile, tbuf, cnt, 0) >= 0) 222 return; 223 224 /* 225 * If the send() failed, the odds are syslogd was restarted. 226 * Make one (only) attempt to reconnect to /dev/log. 227 */ 228 disconnectlog(); 229 connectlog(); 230 if (send(LogFile, tbuf, cnt, 0) >= 0) 231 return; 232 233 /* 234 * Output the message to the console; don't worry about blocking, 235 * if console blocks everything will. Make sure the error reported 236 * is the one from the syslogd failure. 237 */ 238 if (LogStat & LOG_CONS && 239 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { 240 struct iovec iov[2]; 241 242 p = strchr(tbuf, '>') + 1; 243 iov[0].iov_base = p; 244 iov[0].iov_len = cnt - (p - tbuf); 245 iov[1].iov_base = "\r\n"; 246 iov[1].iov_len = 2; 247 (void)writev(fd, iov, 2); 248 (void)close(fd); 249 } 250 } 251 252 static void 253 disconnectlog() 254 { 255 /* 256 * If the user closed the FD and opened another in the same slot, 257 * that's their problem. They should close it before calling on 258 * system services. 259 */ 260 if (LogFile != -1) { 261 close(LogFile); 262 LogFile = -1; 263 } 264 connected = 0; /* retry connect */ 265 } 266 267 static void 268 connectlog() 269 { 270 struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ 271 272 if (LogFile == -1) { 273 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 274 return; 275 (void)fcntl(LogFile, F_SETFD, 1); 276 } 277 if (LogFile != -1 && !connected) { 278 memset(&SyslogAddr, '\0', sizeof(SyslogAddr)); 279 SyslogAddr.sun_len = sizeof(SyslogAddr); 280 SyslogAddr.sun_family = AF_UNIX; 281 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, 282 sizeof(SyslogAddr.sun_path) - 1); 283 SyslogAddr.sun_path[sizeof(SyslogAddr.sun_path) - 1] = '\0'; 284 if (connect(LogFile, (struct sockaddr *)&SyslogAddr, 285 sizeof(SyslogAddr)) == -1) { 286 (void)close(LogFile); 287 LogFile = -1; 288 } else 289 connected = 1; 290 } 291 } 292 293 void 294 openlog(ident, logstat, logfac) 295 const char *ident; 296 int logstat, logfac; 297 { 298 if (ident != NULL) 299 LogTag = ident; 300 LogStat = logstat; 301 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 302 LogFacility = logfac; 303 304 if (LogStat & LOG_NDELAY) /* open immediately */ 305 connectlog(); 306 307 opened = 1; /* ident and facility has been set */ 308 } 309 310 void 311 closelog() 312 { 313 (void)close(LogFile); 314 LogFile = -1; 315 connected = 0; 316 } 317 318 /* setlogmask -- set the log mask level */ 319 int 320 setlogmask(pmask) 321 int pmask; 322 { 323 int omask; 324 325 omask = LogMask; 326 if (pmask != 0) 327 LogMask = pmask; 328 return (omask); 329 } 330