1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1988 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 41 /* 42 * SYSLOG -- print message on log file 43 * 44 * This routine looks a lot like printf, except that it 45 * outputs to the log file instead of the standard output. 46 * Also: 47 * adds a timestamp, 48 * prints the module name in front of the message, 49 * has some other formatting types (or will sometime), 50 * adds a newline on the end of the message. 51 * 52 * The output of this routine is intended to be read by /etc/syslogd. 53 */ 54 55 #pragma weak _syslog = syslog 56 57 #include "lint.h" 58 #include <sys/types.h> 59 #include <sys/types32.h> 60 #include <sys/mman.h> 61 #include <sys/stropts.h> 62 #include <sys/strlog.h> 63 #include <sys/log.h> /* for LOG_MAXPS */ 64 #include <stdlib.h> 65 #include <procfs.h> 66 #include <syslog.h> 67 #include <signal.h> 68 #include <fcntl.h> 69 #include <string.h> 70 #include <stdarg.h> 71 #include <unistd.h> 72 #include <wait.h> 73 #include <stdio.h> 74 #include <string.h> 75 #include <errno.h> 76 #include <thread.h> 77 #include <synch.h> 78 #include <sys/door.h> 79 #include <sys/stat.h> 80 #include <stropts.h> 81 #include <sys/fork.h> 82 #include <sys/wait.h> 83 #include "libc.h" 84 85 #define MAXLINE 1024 /* max message size (but see below) */ 86 87 #define PRIMASK(p) (1 << ((p) & LOG_PRIMASK)) 88 #define PRIFAC(p) (((p) & LOG_FACMASK) >> 3) 89 #define IMPORTANT LOG_ERR 90 91 #ifndef FALSE 92 #define FALSE 0 93 #endif 94 95 #ifndef TRUE 96 #define TRUE 1 97 #endif 98 99 #define logname "/dev/conslog" 100 #define ctty "/dev/syscon" 101 #define sysmsg "/dev/sysmsg" 102 103 #define DOORFILE "/var/run/syslog_door" 104 105 static struct __syslog { 106 int _LogFile; 107 int _LogStat; 108 const char *_LogTag; 109 int _LogMask; 110 char *_SyslogHost; 111 int _LogFacility; 112 int _LogFileInvalid; 113 int _OpenLogCalled; 114 dev_t _LogDev; 115 char _ProcName[PRFNSZ + 1]; 116 } __syslog = { 117 -1, /* fd for log */ 118 0, /* status bits, set by openlog() */ 119 "syslog", /* string to tag the entry with */ 120 0xff, /* mask of priorities to be logged */ 121 NULL, 122 LOG_USER, /* default facility code */ 123 FALSE, /* check for validity of fd for log */ 124 0, /* openlog has not yet been called */ 125 }; 126 127 #define LogFile (__syslog._LogFile) 128 #define LogStat (__syslog._LogStat) 129 #define LogTag (__syslog._LogTag) 130 #define LogMask (__syslog._LogMask) 131 #define SyslogHost (__syslog._SyslogHost) 132 #define LogFacility (__syslog._LogFacility) 133 #define LogFileInvalid (__syslog._LogFileInvalid) 134 #define OpenLogCalled (__syslog._OpenLogCalled) 135 #define LogDev (__syslog._LogDev) 136 #define ProcName (__syslog._ProcName) 137 138 static int syslogd_ok(void); 139 140 /* 141 * Regrettably, there are several instances inside libc where 142 * syslog() is called from the bottom of a deep call stack 143 * and a critical lock was acquired near the top of the stack. 144 * 145 * Because syslog() uses stdio (and it is called from within stdio) 146 * it runs the danger of deadlocking, perhaps with an interposed 147 * malloc() when fork() is occurring concurrently, perhaps with 148 * some other lock within libc. 149 * 150 * The only fix for this problem is to restructure libc not to do 151 * this thing and always to call syslog() with no locks held. 152 * This restructuring will require a substantial effort. 153 * 154 * Meanwhile, we just hope that on the rare occasion that syslog() 155 * is called from within libc (such occurrences should "never happen") 156 * that we don't get caught in a race condition deadlock. 157 */ 158 void 159 syslog(int pri, const char *fmt, ...) 160 { 161 va_list ap; 162 163 va_start(ap, fmt); 164 vsyslog(pri, fmt, ap); 165 va_end(ap); 166 } 167 168 169 void 170 vsyslog(int pri, const char *fmt, va_list ap) 171 { 172 char *b, *f, *o; 173 char c; 174 int clen; 175 char buf[MAXLINE + 2]; 176 char outline[MAXLINE + 256]; /* pad to allow date, system name... */ 177 time_t now; 178 pid_t pid; 179 struct log_ctl hdr; 180 struct strbuf dat; 181 struct strbuf ctl; 182 char timestr[26]; /* hardwired value 26 due to Posix */ 183 size_t taglen; 184 int olderrno = errno; 185 struct stat statbuff; 186 int procfd; 187 char procfile[32]; 188 psinfo_t p; 189 int showpid; 190 uint32_t msgid; 191 char *msgid_start, *msgid_end; 192 int nowait; 193 int ret; 194 195 /* 196 * Maximum tag length is 256 (the pad in outline) minus the size of the 197 * other things that can go in the pad. 198 */ 199 #define MAX_TAG 230 200 201 /* see if we should just throw out this message */ 202 if (pri < 0 || PRIFAC(pri) >= LOG_NFACILITIES || 203 (PRIMASK(pri) & LogMask) == 0) 204 return; 205 206 if (LogFileInvalid) 207 return; 208 209 /* 210 * if openlog() has not been called by the application, 211 * try to get the name of the application and set it 212 * as the ident string for messages. If unable to get 213 * it for any reason, fall back to using the default 214 * of syslog. If we succeed in getting the name, also 215 * turn on LOG_PID, to provide greater detail. 216 */ 217 showpid = 0; 218 if (OpenLogCalled == 0) { 219 (void) sprintf(procfile, "/proc/%d/psinfo", (int)getpid()); 220 if ((procfd = open(procfile, O_RDONLY)) >= 0) { 221 if (read(procfd, &p, sizeof (psinfo_t)) >= 0) { 222 (void) strncpy(ProcName, p.pr_fname, PRFNSZ); 223 LogTag = (const char *) &ProcName; 224 showpid = LOG_PID; 225 } 226 (void) close(procfd); 227 } 228 } 229 if (LogFile < 0) 230 openlog(LogTag, LogStat|LOG_NDELAY|showpid, 0); 231 232 if ((fstat(LogFile, &statbuff) != 0) || 233 (!S_ISCHR(statbuff.st_mode)) || (statbuff.st_rdev != LogDev)) { 234 LogFileInvalid = TRUE; 235 return; 236 } 237 238 /* set default facility if none specified */ 239 if ((pri & LOG_FACMASK) == 0) 240 pri |= LogFacility; 241 242 /* build the header */ 243 hdr.pri = pri; 244 hdr.flags = SL_CONSOLE; 245 hdr.level = 0; 246 247 /* build the message */ 248 /* 249 * To avoid potential security problems, bounds checking is done 250 * on outline and buf. 251 * The following code presumes that the header information will 252 * fit in 250-odd bytes, as was accounted for in the buffer size 253 * allocation. This is dependent on the assumption that the LogTag 254 * and the string returned by sprintf() for getpid() will return 255 * be less than 230-odd characters combined. 256 */ 257 o = outline; 258 (void) time(&now); 259 (void) sprintf(o, "%.15s ", ctime_r(&now, timestr, 26) + 4); 260 o += strlen(o); 261 262 if (LogTag) { 263 taglen = strlen(LogTag) < MAX_TAG ? strlen(LogTag) : MAX_TAG; 264 (void) strncpy(o, LogTag, taglen); 265 o[taglen] = '\0'; 266 o += strlen(o); 267 } 268 if (LogStat & LOG_PID) { 269 (void) sprintf(o, "[%d]", (int)getpid()); 270 o += strlen(o); 271 } 272 if (LogTag) { 273 (void) strcpy(o, ": "); 274 o += 2; 275 } 276 277 STRLOG_MAKE_MSGID(fmt, msgid); 278 (void) sprintf(o, "[ID %u FACILITY_AND_PRIORITY] ", msgid); 279 o += strlen(o); 280 281 b = buf; 282 f = (char *)fmt; 283 while ((c = *f++) != '\0' && b < &buf[MAXLINE]) { 284 char *errmsg; 285 if (c != '%') { 286 *b++ = c; 287 continue; 288 } 289 if ((c = *f++) != 'm') { 290 *b++ = '%'; 291 *b++ = c; 292 continue; 293 } 294 if ((errmsg = strerror(olderrno)) == NULL) 295 (void) snprintf(b, &buf[MAXLINE] - b, "error %d", 296 olderrno); 297 else { 298 while (*errmsg != '\0' && b < &buf[MAXLINE]) { 299 if (*errmsg == '%') { 300 (void) strcpy(b, "%%"); 301 b += 2; 302 } 303 else 304 *b++ = *errmsg; 305 errmsg++; 306 } 307 *b = '\0'; 308 } 309 b += strlen(b); 310 } 311 if (b > buf && *(b-1) != '\n') /* ensure at least one newline */ 312 *b++ = '\n'; 313 *b = '\0'; 314 /* LINTED variable format specifier */ 315 (void) vsnprintf(o, &outline[sizeof (outline)] - o, buf, ap); 316 clen = (int)strlen(outline) + 1; /* add one for NULL byte */ 317 if (clen > MAXLINE) { 318 clen = MAXLINE; 319 outline[MAXLINE-1] = '\0'; 320 } 321 322 /* 323 * 1136432 points out that the underlying log driver actually 324 * refuses to accept (ERANGE) messages longer than LOG_MAXPS 325 * bytes. So it really doesn't make much sense to putmsg a 326 * longer message.. 327 */ 328 if (clen > LOG_MAXPS) { 329 clen = LOG_MAXPS; 330 outline[LOG_MAXPS-1] = '\0'; 331 } 332 333 /* set up the strbufs */ 334 ctl.maxlen = sizeof (struct log_ctl); 335 ctl.len = sizeof (struct log_ctl); 336 ctl.buf = (caddr_t)&hdr; 337 dat.maxlen = sizeof (outline); 338 dat.len = clen; 339 dat.buf = outline; 340 341 /* output the message to the local logger */ 342 ret = putmsg(LogFile, &ctl, &dat, 0); 343 if (!(LogStat & LOG_CONS)) 344 return; 345 if ((ret >= 0) && syslogd_ok()) 346 return; 347 348 /* 349 * Output the message to the console directly. To reduce visual 350 * clutter, we strip out the message ID. 351 */ 352 if ((msgid_start = strstr(outline, "[ID ")) != NULL && 353 (msgid_end = strstr(msgid_start, "] ")) != NULL) 354 (void) strcpy(msgid_start, msgid_end + 2); 355 356 clen = strlen(outline) + 1; 357 358 nowait = (LogStat & LOG_NOWAIT); 359 pid = forkx(nowait? 0 : (FORK_NOSIGCHLD | FORK_WAITPID)); 360 if (pid == -1) 361 return; 362 363 if (pid == 0) { 364 sigset_t sigs; 365 int fd; 366 367 (void) sigset(SIGALRM, SIG_DFL); 368 (void) sigemptyset(&sigs); 369 (void) sigaddset(&sigs, SIGALRM); 370 (void) sigprocmask(SIG_UNBLOCK, &sigs, NULL); 371 (void) alarm(5); 372 if (((fd = open(sysmsg, O_WRONLY)) >= 0) || 373 (fd = open(ctty, O_WRONLY)) >= 0) { 374 (void) alarm(0); 375 outline[clen - 1] = '\r'; 376 (void) write(fd, outline, clen); 377 (void) close(fd); 378 } 379 _exit(0); 380 } 381 if (!nowait) 382 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) 383 continue; 384 } 385 386 /* 387 * Use a door call to syslogd to see if it's alive. 388 */ 389 static int 390 syslogd_ok(void) 391 { 392 int d; 393 int s; 394 door_arg_t darg; 395 door_info_t info; 396 397 if ((d = open(DOORFILE, O_RDONLY)) < 0) 398 return (0); 399 /* 400 * see if our pid matches the pid of the door server. 401 * If so, syslogd has called syslog(), probably as 402 * a result of some name service library error, and 403 * we don't want to let syslog continue and possibly 404 * fork here. 405 */ 406 info.di_target = 0; 407 if (__door_info(d, &info) < 0 || info.di_target == getpid()) { 408 (void) close(d); 409 return (0); 410 } 411 darg.data_ptr = NULL; 412 darg.data_size = 0; 413 darg.desc_ptr = NULL; 414 darg.desc_num = 0; 415 darg.rbuf = NULL; 416 darg.rsize = 0; 417 s = __door_call(d, &darg); 418 (void) close(d); 419 if (s < 0) 420 return (0); /* failure - syslogd dead */ 421 else 422 return (1); 423 } 424 425 /* 426 * OPENLOG -- open system log 427 */ 428 429 void 430 openlog(const char *ident, int logstat, int logfac) 431 { 432 struct stat statbuff; 433 434 OpenLogCalled = 1; 435 if (ident != NULL) 436 LogTag = ident; 437 LogStat = logstat; 438 if (logfac != 0) 439 LogFacility = logfac & LOG_FACMASK; 440 441 /* 442 * if the fstat(2) fails or the st_rdev has changed 443 * then we must open the file 444 */ 445 if ((fstat(LogFile, &statbuff) == 0) && 446 (S_ISCHR(statbuff.st_mode)) && (statbuff.st_rdev == LogDev)) 447 return; 448 449 if (LogStat & LOG_NDELAY) { 450 LogFile = open(logname, O_WRONLY); 451 (void) fcntl(LogFile, F_SETFD, 1); 452 (void) fstat(LogFile, &statbuff); 453 LogDev = statbuff.st_rdev; 454 } 455 } 456 457 /* 458 * CLOSELOG -- close the system log 459 */ 460 461 void 462 closelog(void) 463 { 464 struct stat statbuff; 465 466 OpenLogCalled = 0; 467 468 /* if the LogFile is invalid it can not be closed */ 469 if (LogFileInvalid) 470 return; 471 472 /* 473 * if the fstat(2) fails or the st_rdev has changed 474 * then we can not close the file 475 */ 476 if ((fstat(LogFile, &statbuff) == 0) && (statbuff.st_rdev == LogDev)) { 477 (void) close(LogFile); 478 LogFile = -1; 479 LogStat = 0; 480 } 481 } 482 483 /* 484 * SETLOGMASK -- set the log mask level 485 */ 486 int 487 setlogmask(int pmask) 488 { 489 int omask = 0; 490 491 omask = LogMask; 492 if (pmask != 0) 493 LogMask = pmask; 494 return (omask); 495 } 496