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.16 2002/02/19 19:39:36 millert 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 #include <stdarg.h> 53 54 static struct syslog_data sdata = SYSLOG_DATA_INIT; 55 56 extern char *__progname; /* Program name, from crt0. */ 57 58 static void disconnectlog(void); /* disconnect from syslogd */ 59 static void connectlog(void); /* (re)connect to syslogd */ 60 static void disconnectlog_r(struct syslog_data *); 61 static void connectlog_r(struct syslog_data *); 62 63 /* 64 * syslog, vsyslog -- 65 * print message on log file; output is intended for syslogd(8). 66 */ 67 void 68 syslog(int pri, const char *fmt, ...) 69 { 70 va_list ap; 71 72 va_start(ap, fmt); 73 vsyslog(pri, fmt, ap); 74 va_end(ap); 75 } 76 77 void 78 vsyslog(pri, fmt, ap) 79 int pri; 80 register const char *fmt; 81 va_list ap; 82 { 83 vsyslog_r(pri, &sdata, fmt, ap); 84 } 85 86 static void 87 disconnectlog() 88 { 89 disconnectlog_r(&sdata); 90 } 91 92 static void 93 connectlog() 94 { 95 connectlog_r(&sdata); 96 } 97 98 void 99 openlog(ident, logstat, logfac) 100 const char *ident; 101 int logstat, logfac; 102 { 103 openlog_r(ident, logstat, logfac, &sdata); 104 } 105 106 void 107 closelog() 108 { 109 closelog_r(&sdata); 110 } 111 112 /* setlogmask -- set the log mask level */ 113 int 114 setlogmask(pmask) 115 int pmask; 116 { 117 return setlogmask_r(pmask, &sdata); 118 } 119 120 /* Reentrant version of syslog, i.e. syslog_r() */ 121 122 void 123 syslog_r(int pri, struct syslog_data *data, const char *fmt, ...) 124 { 125 va_list ap; 126 127 va_start(ap, fmt); 128 vsyslog_r(pri, data, fmt, ap); 129 va_end(ap); 130 } 131 132 void 133 vsyslog_r(pri, data, fmt, ap) 134 int pri; 135 struct syslog_data *data; 136 const char *fmt; 137 va_list ap; 138 { 139 int cnt; 140 char ch, *p, *t; 141 time_t now; 142 int fd, saved_errno; 143 #define TBUF_LEN 2048 144 #define FMT_LEN 1024 145 char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN]; 146 int tbuf_left, fmt_left, prlen; 147 148 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 149 /* Check for invalid bits. */ 150 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 151 if (data == &sdata) { 152 syslog(INTERNALLOG, 153 "syslog: unknown facility/priority: %x", pri); 154 } else { 155 syslog_r(INTERNALLOG, data, 156 "syslog_r: unknown facility/priority: %x", pri); 157 } 158 pri &= LOG_PRIMASK|LOG_FACMASK; 159 } 160 161 /* Check priority against setlogmask values. */ 162 if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask)) 163 return; 164 165 saved_errno = errno; 166 167 /* Set default facility if none specified. */ 168 if ((pri & LOG_FACMASK) == 0) 169 pri |= data->log_fac; 170 171 /* If we have been called through syslog(), no need for reentrancy. */ 172 if (data == &sdata) 173 (void)time(&now); 174 175 p = tbuf; 176 tbuf_left = TBUF_LEN; 177 178 #define DEC() \ 179 do { \ 180 if (prlen < 0) \ 181 prlen = 0; \ 182 if (prlen >= tbuf_left) \ 183 prlen = tbuf_left - 1; \ 184 p += prlen; \ 185 tbuf_left -= prlen; \ 186 } while (0) 187 188 prlen = snprintf(p, tbuf_left, "<%d>", pri); 189 DEC(); 190 191 /* 192 * syslogd will expand time automagically for reentrant case, and 193 * for normal case, just do like before 194 */ 195 if (data == &sdata) { 196 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now)); 197 DEC(); 198 } 199 200 if (data->log_stat & LOG_PERROR) 201 stdp = p; 202 if (data->log_tag == NULL) 203 data->log_tag = __progname; 204 if (data->log_tag != NULL) { 205 prlen = snprintf(p, tbuf_left, "%s", data->log_tag); 206 DEC(); 207 } 208 if (data->log_stat & LOG_PID) { 209 prlen = snprintf(p, tbuf_left, "[%d]", getpid()); 210 DEC(); 211 } 212 if (data->log_tag != NULL) { 213 if (tbuf_left > 1) { 214 *p++ = ':'; 215 tbuf_left--; 216 } 217 if (tbuf_left > 1) { 218 *p++ = ' '; 219 tbuf_left--; 220 } 221 } 222 223 /* strerror() is not reentrant */ 224 225 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) { 226 if (ch == '%' && fmt[1] == 'm') { 227 ++fmt; 228 if (data == &sdata) { 229 prlen = snprintf(t, fmt_left, "%s", 230 strerror(saved_errno)); 231 } else { 232 prlen = snprintf(t, fmt_left, "Error %d", 233 saved_errno); 234 } 235 if (prlen >= fmt_left) 236 prlen = fmt_left - 1; 237 t += prlen; 238 fmt_left -= prlen; 239 } else { 240 if (fmt_left > 1) { 241 *t++ = ch; 242 fmt_left--; 243 } 244 } 245 } 246 *t = '\0'; 247 248 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap); 249 DEC(); 250 cnt = p - tbuf; 251 252 /* Output to stderr if requested. */ 253 if (data->log_stat & LOG_PERROR) { 254 struct iovec iov[2]; 255 256 iov[0].iov_base = stdp; 257 iov[0].iov_len = cnt - (stdp - tbuf); 258 iov[1].iov_base = "\n"; 259 iov[1].iov_len = 1; 260 (void)writev(STDERR_FILENO, iov, 2); 261 } 262 263 /* Get connected, output the message to the local logger. */ 264 if (!data->opened) 265 openlog_r(data->log_tag, data->log_stat, 0, data); 266 connectlog_r(data); 267 if (send(data->log_file, tbuf, cnt, 0) >= 0) 268 return; 269 270 /* 271 * If the send() failed, the odds are syslogd was restarted. 272 * Make one (only) attempt to reconnect to /dev/log. 273 */ 274 disconnectlog_r(data); 275 connectlog_r(data); 276 if (send(data->log_file, tbuf, cnt, 0) >= 0) 277 return; 278 279 /* 280 * Output the message to the console; try not to block 281 * as a blocking console should not stop other processes. 282 * Make sure the error reported is the one from the syslogd failure. 283 */ 284 if (data->log_stat & LOG_CONS && 285 (fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) { 286 struct iovec iov[2]; 287 288 p = strchr(tbuf, '>') + 1; 289 iov[0].iov_base = p; 290 iov[0].iov_len = cnt - (p - tbuf); 291 iov[1].iov_base = "\r\n"; 292 iov[1].iov_len = 2; 293 (void)writev(fd, iov, 2); 294 (void)close(fd); 295 } 296 } 297 298 static void 299 disconnectlog_r(data) 300 struct syslog_data *data; 301 { 302 /* 303 * If the user closed the FD and opened another in the same slot, 304 * that's their problem. They should close it before calling on 305 * system services. 306 */ 307 if (data->log_file != -1) { 308 close(data->log_file); 309 data->log_file = -1; 310 } 311 data->connected = 0; /* retry connect */ 312 } 313 314 static void 315 connectlog_r(data) 316 struct syslog_data *data; 317 { 318 struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ 319 320 if (data->log_file == -1) { 321 if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 322 return; 323 (void)fcntl(data->log_file, F_SETFD, 1); 324 } 325 if (data->log_file != -1 && !data->connected) { 326 memset(&SyslogAddr, '\0', sizeof(SyslogAddr)); 327 SyslogAddr.sun_len = sizeof(SyslogAddr); 328 SyslogAddr.sun_family = AF_UNIX; 329 strlcpy(SyslogAddr.sun_path, _PATH_LOG, 330 sizeof(SyslogAddr.sun_path)); 331 if (connect(data->log_file, (struct sockaddr *)&SyslogAddr, 332 sizeof(SyslogAddr)) == -1) { 333 (void)close(data->log_file); 334 data->log_file = -1; 335 } else 336 data->connected = 1; 337 } 338 } 339 340 void 341 openlog_r(ident, logstat, logfac, data) 342 const char *ident; 343 int logstat, logfac; 344 struct syslog_data *data; 345 { 346 if (ident != NULL) 347 data->log_tag = ident; 348 data->log_stat = logstat; 349 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 350 data->log_fac = logfac; 351 352 if (data->log_stat & LOG_NDELAY) /* open immediately */ 353 connectlog_r(data); 354 355 data->opened = 1; /* ident and facility has been set */ 356 } 357 358 void 359 closelog_r(data) 360 struct syslog_data *data; 361 { 362 (void)close(data->log_file); 363 data->log_file = -1; 364 data->connected = 0; 365 } 366 367 /* setlogmask -- set the log mask level */ 368 int 369 setlogmask_r(pmask, data) 370 int pmask; 371 struct syslog_data *data; 372 { 373 int omask; 374 375 omask = data->log_mask; 376 if (pmask != 0) 377 data->log_mask = pmask; 378 return (omask); 379 } 380