1 /* $NetBSD: syslog.c,v 1.54 2014/09/18 13:58:20 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95"; 36 #else 37 __RCSID("$NetBSD: syslog.c,v 1.54 2014/09/18 13:58:20 christos Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 #include "namespace.h" 42 #include <sys/types.h> 43 #include <sys/param.h> 44 #include <sys/socket.h> 45 #include <sys/syslog.h> 46 #include <sys/uio.h> 47 #include <sys/un.h> 48 #include <netdb.h> 49 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <paths.h> 53 #include <stdarg.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <time.h> 58 #include <unistd.h> 59 #include "reentrant.h" 60 #include "extern.h" 61 62 #if defined(__minix) 63 #include <sys/ioctl.h> 64 #endif /* defined(__minix) */ 65 66 #ifdef __weak_alias 67 __weak_alias(closelog,_closelog) 68 __weak_alias(openlog,_openlog) 69 __weak_alias(setlogmask,_setlogmask) 70 __weak_alias(syslog,_syslog) 71 __weak_alias(vsyslog,_vsyslog) 72 __weak_alias(syslogp,_syslogp) 73 __weak_alias(vsyslogp,_vsyslogp) 74 #endif 75 76 static struct syslog_data sdata = SYSLOG_DATA_INIT; 77 78 static void openlog_unlocked_r(const char *, int, int, 79 struct syslog_data *); 80 static void disconnectlog_r(struct syslog_data *); 81 static void connectlog_r(struct syslog_data *); 82 83 #define LOG_SIGNAL_SAFE (int)0x80000000 84 85 86 #ifdef _REENTRANT 87 static mutex_t syslog_mutex = MUTEX_INITIALIZER; 88 #endif 89 90 /* 91 * syslog, vsyslog -- 92 * print message on log file; output is intended for syslogd(8). 93 */ 94 void 95 syslog(int pri, const char *fmt, ...) 96 { 97 va_list ap; 98 99 va_start(ap, fmt); 100 vsyslog(pri, fmt, ap); 101 va_end(ap); 102 } 103 104 void 105 vsyslog(int pri, const char *fmt, va_list ap) 106 { 107 vsyslog_r(pri, &sdata, fmt, ap); 108 } 109 110 /* 111 * syslogp, vsyslogp -- 112 * like syslog but take additional arguments for MSGID and SD 113 */ 114 void 115 syslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, ...) 116 { 117 va_list ap; 118 119 va_start(ap, msgfmt); 120 vsyslogp(pri, msgid, sdfmt, msgfmt, ap); 121 va_end(ap); 122 } 123 124 void 125 vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, va_list ap) 126 { 127 vsyslogp_r(pri, &sdata, msgid, sdfmt, msgfmt, ap); 128 } 129 130 void 131 openlog(const char *ident, int logstat, int logfac) 132 { 133 openlog_r(ident, logstat, logfac, &sdata); 134 } 135 136 void 137 closelog(void) 138 { 139 closelog_r(&sdata); 140 } 141 142 /* setlogmask -- set the log mask level */ 143 int 144 setlogmask(int pmask) 145 { 146 return setlogmask_r(pmask, &sdata); 147 } 148 149 /* Reentrant version of syslog, i.e. syslog_r() */ 150 151 void 152 syslog_r(int pri, struct syslog_data *data, const char *fmt, ...) 153 { 154 va_list ap; 155 156 va_start(ap, fmt); 157 vsyslog_r(pri, data, fmt, ap); 158 va_end(ap); 159 } 160 161 void 162 syslogp_r(int pri, struct syslog_data *data, const char *msgid, 163 const char *sdfmt, const char *msgfmt, ...) 164 { 165 va_list ap; 166 167 va_start(ap, msgfmt); 168 vsyslogp_r(pri, data, msgid, sdfmt, msgfmt, ap); 169 va_end(ap); 170 } 171 172 void 173 syslog_ss(int pri, struct syslog_data *data, const char *fmt, ...) 174 { 175 va_list ap; 176 177 va_start(ap, fmt); 178 vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap); 179 va_end(ap); 180 } 181 182 void 183 syslogp_ss(int pri, struct syslog_data *data, const char *msgid, 184 const char *sdfmt, const char *msgfmt, ...) 185 { 186 va_list ap; 187 188 va_start(ap, msgfmt); 189 vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap); 190 va_end(ap); 191 } 192 193 void 194 vsyslog_ss(int pri, struct syslog_data *data, const char *fmt, va_list ap) 195 { 196 vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap); 197 } 198 199 void 200 vsyslogp_ss(int pri, struct syslog_data *data, const char *msgid, 201 const char *sdfmt, const char *msgfmt, va_list ap) 202 { 203 vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap); 204 } 205 206 207 void 208 vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap) 209 { 210 vsyslogp_r(pri, data, NULL, NULL, fmt, ap); 211 } 212 213 void 214 vsyslogp_r(int pri, struct syslog_data *data, const char *msgid, 215 const char *sdfmt, const char *msgfmt, va_list ap) 216 { 217 static const char BRCOSP[] = "]: "; 218 static const char CRLF[] = "\r\n"; 219 size_t cnt, prlen, tries; 220 char ch, *p, *t; 221 struct timeval tv; 222 struct tm tmnow; 223 time_t now; 224 int fd, saved_errno; 225 #define TBUF_LEN 2048 226 #define FMT_LEN 1024 227 #define MAXTRIES 10 228 char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN], fmt_cat[FMT_LEN] = ""; 229 size_t tbuf_left, fmt_left, msgsdlen; 230 char *fmt = fmt_cat; 231 int signal_safe = pri & LOG_SIGNAL_SAFE; 232 struct iovec iov[7]; /* prog + [ + pid + ]: + fmt + crlf */ 233 int opened, iovcnt; 234 235 pri &= ~LOG_SIGNAL_SAFE; 236 237 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 238 /* Check for invalid bits. */ 239 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 240 syslog_r(INTERNALLOG | signal_safe, data, 241 "syslog_r: unknown facility/priority: %x", pri); 242 pri &= LOG_PRIMASK|LOG_FACMASK; 243 } 244 245 /* Check priority against setlogmask values. */ 246 if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask)) 247 return; 248 249 saved_errno = errno; 250 251 /* Set default facility if none specified. */ 252 if ((pri & LOG_FACMASK) == 0) 253 pri |= data->log_fac; 254 255 /* Build the message. */ 256 p = tbuf; 257 tbuf_left = TBUF_LEN; 258 259 #define DEC() \ 260 do { \ 261 if (prlen >= tbuf_left) \ 262 prlen = tbuf_left - 1; \ 263 p += prlen; \ 264 tbuf_left -= prlen; \ 265 } while (/*CONSTCOND*/0) 266 267 prlen = snprintf_ss(p, tbuf_left, "<%d>1 ", pri); 268 DEC(); 269 270 if (!signal_safe && (gettimeofday(&tv, NULL) != -1)) { 271 /* strftime() implies tzset(), localtime_r() doesn't. */ 272 tzset(); 273 now = (time_t) tv.tv_sec; 274 localtime_r(&now, &tmnow); 275 276 prlen = strftime(p, tbuf_left, "%FT%T", &tmnow); 277 DEC(); 278 prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec); 279 DEC(); 280 prlen = strftime(p, tbuf_left-1, "%z", &tmnow); 281 /* strftime gives eg. "+0200", but we need "+02:00" */ 282 if (prlen == 5) { 283 p[prlen+1] = p[prlen]; 284 p[prlen] = p[prlen-1]; 285 p[prlen-1] = p[prlen-2]; 286 p[prlen-2] = ':'; 287 prlen += 1; 288 } 289 } else { 290 prlen = snprintf_ss(p, tbuf_left, "-"); 291 #if 0 292 /* 293 * if gmtime_r() was signal-safe we could output 294 * the UTC-time: 295 */ 296 gmtime_r(&now, &tmnow); 297 prlen = strftime(p, tbuf_left, "%FT%TZ", &tmnow); 298 #endif 299 } 300 301 #if !defined(__minix) 302 if (data == &sdata) 303 mutex_lock(&syslog_mutex); 304 #endif /* !defined(__minix) */ 305 306 if (data->log_hostname[0] == '\0' && gethostname(data->log_hostname, 307 sizeof(data->log_hostname)) == -1) { 308 /* can this really happen? */ 309 data->log_hostname[0] = '-'; 310 data->log_hostname[1] = '\0'; 311 } 312 313 DEC(); 314 prlen = snprintf_ss(p, tbuf_left, " %s ", data->log_hostname); 315 316 if (data->log_tag == NULL) 317 data->log_tag = getprogname(); 318 319 DEC(); 320 prlen = snprintf_ss(p, tbuf_left, "%s ", 321 data->log_tag ? data->log_tag : "-"); 322 323 #if !defined(__minix) 324 if (data == &sdata) 325 mutex_unlock(&syslog_mutex); 326 #endif /* !defined(__minix) */ 327 328 if (data->log_stat & (LOG_PERROR|LOG_CONS)) { 329 iovcnt = 0; 330 iov[iovcnt].iov_base = p; 331 iov[iovcnt].iov_len = prlen - 1; 332 iovcnt++; 333 } 334 DEC(); 335 336 if (data->log_stat & LOG_PID) { 337 prlen = snprintf_ss(p, tbuf_left, "%d ", getpid()); 338 if (data->log_stat & (LOG_PERROR|LOG_CONS)) { 339 iov[iovcnt].iov_base = __UNCONST("["); 340 iov[iovcnt].iov_len = 1; 341 iovcnt++; 342 iov[iovcnt].iov_base = p; 343 iov[iovcnt].iov_len = prlen - 1; 344 iovcnt++; 345 iov[iovcnt].iov_base = __UNCONST(BRCOSP); 346 iov[iovcnt].iov_len = 3; 347 iovcnt++; 348 } 349 } else { 350 prlen = snprintf_ss(p, tbuf_left, "- "); 351 if (data->log_stat & (LOG_PERROR|LOG_CONS)) { 352 iov[iovcnt].iov_base = __UNCONST(BRCOSP + 1); 353 iov[iovcnt].iov_len = 2; 354 iovcnt++; 355 } 356 } 357 DEC(); 358 359 /* 360 * concat the format strings, then use one vsnprintf() 361 */ 362 if (msgid != NULL && *msgid != '\0') { 363 strlcat(fmt_cat, msgid, FMT_LEN); 364 strlcat(fmt_cat, " ", FMT_LEN); 365 } else 366 strlcat(fmt_cat, "- ", FMT_LEN); 367 368 if (sdfmt != NULL && *sdfmt != '\0') { 369 strlcat(fmt_cat, sdfmt, FMT_LEN); 370 } else 371 strlcat(fmt_cat, "-", FMT_LEN); 372 373 if (data->log_stat & (LOG_PERROR|LOG_CONS)) 374 msgsdlen = strlen(fmt_cat) + 1; 375 else 376 msgsdlen = 0; /* XXX: GCC */ 377 378 if (msgfmt != NULL && *msgfmt != '\0') { 379 strlcat(fmt_cat, " ", FMT_LEN); 380 strlcat(fmt_cat, msgfmt, FMT_LEN); 381 } 382 383 /* 384 * We wouldn't need this mess if printf handled %m, or if 385 * strerror() had been invented before syslog(). 386 */ 387 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) { 388 if (ch == '%' && fmt[1] == 'm') { 389 char ebuf[128]; 390 ++fmt; 391 if (signal_safe || 392 strerror_r(saved_errno, ebuf, sizeof(ebuf))) 393 prlen = snprintf_ss(t, fmt_left, "Error %d", 394 saved_errno); 395 else 396 prlen = snprintf_ss(t, fmt_left, "%s", ebuf); 397 if (prlen >= fmt_left) 398 prlen = fmt_left - 1; 399 t += prlen; 400 fmt_left -= prlen; 401 } else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) { 402 *t++ = '%'; 403 *t++ = '%'; 404 fmt++; 405 fmt_left -= 2; 406 } else { 407 if (fmt_left > 1) { 408 *t++ = ch; 409 fmt_left--; 410 } 411 } 412 } 413 *t = '\0'; 414 415 if (signal_safe) 416 prlen = vsnprintf_ss(p, tbuf_left, fmt_cpy, ap); 417 else 418 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap); 419 420 if (data->log_stat & (LOG_PERROR|LOG_CONS)) { 421 iov[iovcnt].iov_base = p + msgsdlen; 422 iov[iovcnt].iov_len = prlen - msgsdlen; 423 iovcnt++; 424 } 425 426 DEC(); 427 cnt = p - tbuf; 428 429 /* Output to stderr if requested. */ 430 if (data->log_stat & LOG_PERROR) { 431 iov[iovcnt].iov_base = __UNCONST(CRLF + 1); 432 iov[iovcnt].iov_len = 1; 433 (void)writev(STDERR_FILENO, iov, iovcnt + 1); 434 } 435 436 /* Get connected, output the message to the local logger. */ 437 #if !defined(__minix) 438 if (data == &sdata) 439 mutex_lock(&syslog_mutex); 440 #endif /* !defined(__minix) */ 441 opened = !data->log_opened; 442 if (opened) 443 openlog_unlocked_r(data->log_tag, data->log_stat, 0, data); 444 connectlog_r(data); 445 446 /* 447 * If the send() failed, there are two likely scenarios: 448 * 1) syslogd was restarted 449 * 2) /dev/log is out of socket buffer space 450 * We attempt to reconnect to /dev/log to take care of 451 * case #1 and keep send()ing data to cover case #2 452 * to give syslogd a chance to empty its socket buffer. 453 */ 454 for (tries = 0; tries < MAXTRIES; tries++) { 455 #if defined(__minix) 456 if (write(data->log_file, tbuf, cnt) != -1) 457 #else 458 if (send(data->log_file, tbuf, cnt, 0) != -1) 459 #endif /* defined(__minix) */ 460 break; 461 if (errno != ENOBUFS) { 462 disconnectlog_r(data); 463 connectlog_r(data); 464 } else 465 (void)usleep(1); 466 } 467 468 /* 469 * Output the message to the console; try not to block 470 * as a blocking console should not stop other processes. 471 * Make sure the error reported is the one from the syslogd failure. 472 */ 473 if (tries == MAXTRIES && (data->log_stat & LOG_CONS) && 474 (fd = open(_PATH_CONSOLE, 475 O_WRONLY | O_NONBLOCK | O_CLOEXEC, 0)) >= 0) { 476 iov[iovcnt].iov_base = __UNCONST(CRLF); 477 iov[iovcnt].iov_len = 2; 478 (void)writev(fd, iov, iovcnt + 1); 479 (void)close(fd); 480 } 481 482 #if !defined(__minix) 483 if (data == &sdata) 484 mutex_unlock(&syslog_mutex); 485 #endif /* !defined(__minix) */ 486 487 if (data != &sdata && opened) { 488 /* preserve log tag */ 489 const char *ident = data->log_tag; 490 closelog_r(data); 491 data->log_tag = ident; 492 } 493 } 494 495 static void 496 disconnectlog_r(struct syslog_data *data) 497 { 498 /* 499 * If the user closed the FD and opened another in the same slot, 500 * that's their problem. They should close it before calling on 501 * system services. 502 */ 503 if (data->log_file != -1) { 504 (void)close(data->log_file); 505 data->log_file = -1; 506 } 507 data->log_connected = 0; /* retry connect */ 508 } 509 510 static void 511 connectlog_r(struct syslog_data *data) 512 { 513 /* AF_UNIX address of local logger */ 514 static const struct sockaddr_un sun = { 515 .sun_family = AF_LOCAL, 516 #if !defined(__minix) 517 .sun_len = sizeof(sun), 518 #endif /* !defined(__minix) */ 519 .sun_path = _PATH_LOG, 520 }; 521 522 if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) { 523 if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 524 0)) == -1) 525 return; 526 data->log_connected = 0; 527 } 528 if (!data->log_connected) { 529 #if defined(__minix) 530 if(ioctl(data->log_file, NWIOSUDSTADDR, __UNCONST(&sun)) < 0) 531 532 #else 533 if (connect(data->log_file, 534 (const struct sockaddr *)(const void *)&sun, 535 (socklen_t)sizeof(sun)) == -1) 536 #endif /* defined(__minix) */ 537 { 538 (void)close(data->log_file); 539 data->log_file = -1; 540 } else 541 data->log_connected = 1; 542 } 543 } 544 545 static void 546 openlog_unlocked_r(const char *ident, int logstat, int logfac, 547 struct syslog_data *data) 548 { 549 if (ident != NULL) 550 data->log_tag = ident; 551 data->log_stat = logstat; 552 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 553 data->log_fac = logfac; 554 555 if (data->log_stat & LOG_NDELAY) /* open immediately */ 556 connectlog_r(data); 557 558 data->log_opened = 1; 559 } 560 561 void 562 openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data) 563 { 564 #if !defined(__minix) 565 if (data == &sdata) 566 mutex_lock(&syslog_mutex); 567 #endif /* !defined(__minix) */ 568 openlog_unlocked_r(ident, logstat, logfac, data); 569 #if !defined(__minix) 570 if (data == &sdata) 571 mutex_unlock(&syslog_mutex); 572 #endif /* !defined(__minix) */ 573 } 574 575 void 576 closelog_r(struct syslog_data *data) 577 { 578 #if !defined(__minix) 579 if (data == &sdata) 580 mutex_lock(&syslog_mutex); 581 #endif /* !defined(__minix) */ 582 (void)close(data->log_file); 583 data->log_file = -1; 584 data->log_connected = 0; 585 data->log_tag = NULL; 586 #if !defined(__minix) 587 if (data == &sdata) 588 mutex_unlock(&syslog_mutex); 589 #endif /* !defined(__minix) */ 590 } 591 592 int 593 setlogmask_r(int pmask, struct syslog_data *data) 594 { 595 int omask; 596 597 omask = data->log_mask; 598 if (pmask != 0) 599 data->log_mask = pmask; 600 return omask; 601 } 602