1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/param.h> 31*0Sstevel@tonic-gate #include <sys/varargs.h> 32*0Sstevel@tonic-gate #include <sys/systm.h> 33*0Sstevel@tonic-gate #include <sys/cmn_err.h> 34*0Sstevel@tonic-gate #include <sys/stream.h> 35*0Sstevel@tonic-gate #include <sys/strsubr.h> 36*0Sstevel@tonic-gate #include <sys/strsun.h> 37*0Sstevel@tonic-gate #include <sys/sysmacros.h> 38*0Sstevel@tonic-gate #include <sys/kmem.h> 39*0Sstevel@tonic-gate #include <sys/log.h> 40*0Sstevel@tonic-gate #include <sys/spl.h> 41*0Sstevel@tonic-gate #include <sys/syslog.h> 42*0Sstevel@tonic-gate #include <sys/console.h> 43*0Sstevel@tonic-gate #include <sys/debug.h> 44*0Sstevel@tonic-gate #include <sys/utsname.h> 45*0Sstevel@tonic-gate #include <sys/id_space.h> 46*0Sstevel@tonic-gate #include <sys/zone.h> 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate log_zone_t log_global; 49*0Sstevel@tonic-gate queue_t *log_consq; 50*0Sstevel@tonic-gate queue_t *log_backlogq; 51*0Sstevel@tonic-gate queue_t *log_intrq; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #define LOG_PRISIZE 8 /* max priority size: 7 characters + null */ 54*0Sstevel@tonic-gate #define LOG_FACSIZE 9 /* max priority size: 8 characters + null */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate static krwlock_t log_rwlock; 57*0Sstevel@tonic-gate static int log_rwlock_depth; 58*0Sstevel@tonic-gate static int log_seq_no[SL_CONSOLE + 1]; 59*0Sstevel@tonic-gate static stdata_t log_fakestr; 60*0Sstevel@tonic-gate static id_space_t *log_minorspace; 61*0Sstevel@tonic-gate static log_t log_backlog; 62*0Sstevel@tonic-gate static log_t log_conslog; 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate static queue_t *log_recentq; 65*0Sstevel@tonic-gate static queue_t *log_freeq; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate static zone_key_t log_zone_key; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate static char log_overflow_msg[] = "message overflow on /dev/log minor #%d%s\n"; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate static char log_pri[LOG_PRIMASK + 1][LOG_PRISIZE] = { 72*0Sstevel@tonic-gate "emerg", "alert", "crit", "error", 73*0Sstevel@tonic-gate "warning", "notice", "info", "debug" 74*0Sstevel@tonic-gate }; 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate static char log_fac[LOG_NFACILITIES + 1][LOG_FACSIZE] = { 77*0Sstevel@tonic-gate "kern", "user", "mail", "daemon", 78*0Sstevel@tonic-gate "auth", "syslog", "lpr", "news", 79*0Sstevel@tonic-gate "uucp", "resv9", "resv10", "resv11", 80*0Sstevel@tonic-gate "resv12", "audit", "resv14", "cron", 81*0Sstevel@tonic-gate "local0", "local1", "local2", "local3", 82*0Sstevel@tonic-gate "local4", "local5", "local6", "local7", 83*0Sstevel@tonic-gate "unknown" 84*0Sstevel@tonic-gate }; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate /* 87*0Sstevel@tonic-gate * Get exclusive access to the logging system; this includes all minor 88*0Sstevel@tonic-gate * devices. We use an rwlock rather than a mutex because hold times 89*0Sstevel@tonic-gate * are potentially long, so we don't want to waste cycles in adaptive mutex 90*0Sstevel@tonic-gate * spin (rwlocks always block when contended). Note that we explicitly 91*0Sstevel@tonic-gate * support recursive calls (e.g. printf() calls foo() calls printf()). 92*0Sstevel@tonic-gate * 93*0Sstevel@tonic-gate * Clients may use log_enter() / log_exit() to guarantee that a group 94*0Sstevel@tonic-gate * of messages is treated atomically (i.e. they appear in order and are 95*0Sstevel@tonic-gate * not interspersed with any other messages), e.g. for multiline printf(). 96*0Sstevel@tonic-gate * 97*0Sstevel@tonic-gate * This could probably be changed to a per-zone lock if contention becomes 98*0Sstevel@tonic-gate * an issue. 99*0Sstevel@tonic-gate */ 100*0Sstevel@tonic-gate void 101*0Sstevel@tonic-gate log_enter(void) 102*0Sstevel@tonic-gate { 103*0Sstevel@tonic-gate if (rw_owner(&log_rwlock) != curthread) 104*0Sstevel@tonic-gate rw_enter(&log_rwlock, RW_WRITER); 105*0Sstevel@tonic-gate log_rwlock_depth++; 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate void 109*0Sstevel@tonic-gate log_exit(void) 110*0Sstevel@tonic-gate { 111*0Sstevel@tonic-gate if (--log_rwlock_depth == 0) 112*0Sstevel@tonic-gate rw_exit(&log_rwlock); 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate void 116*0Sstevel@tonic-gate log_flushq(queue_t *q) 117*0Sstevel@tonic-gate { 118*0Sstevel@tonic-gate mblk_t *mp; 119*0Sstevel@tonic-gate log_t *lp = (log_t *)q->q_ptr; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* lp will be NULL if the queue was created via log_makeq */ 122*0Sstevel@tonic-gate while ((mp = getq_noenab(q)) != NULL) 123*0Sstevel@tonic-gate log_sendmsg(mp, lp == NULL ? GLOBAL_ZONEID : lp->log_zoneid); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate /* 127*0Sstevel@tonic-gate * Create a minimal queue with just enough fields filled in to support 128*0Sstevel@tonic-gate * canput(9F), putq(9F), and getq_noenab(9F). We set QNOENB to ensure 129*0Sstevel@tonic-gate * that the queue will never be enabled. 130*0Sstevel@tonic-gate */ 131*0Sstevel@tonic-gate static queue_t * 132*0Sstevel@tonic-gate log_makeq(size_t lowat, size_t hiwat, void *ibc) 133*0Sstevel@tonic-gate { 134*0Sstevel@tonic-gate queue_t *q; 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate q = kmem_zalloc(sizeof (queue_t), KM_SLEEP); 137*0Sstevel@tonic-gate q->q_stream = &log_fakestr; 138*0Sstevel@tonic-gate q->q_flag = QISDRV | QMTSAFE | QNOENB | QREADR | QUSE; 139*0Sstevel@tonic-gate q->q_nfsrv = q; 140*0Sstevel@tonic-gate q->q_lowat = lowat; 141*0Sstevel@tonic-gate q->q_hiwat = hiwat; 142*0Sstevel@tonic-gate mutex_init(QLOCK(q), NULL, MUTEX_DRIVER, ibc); 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate return (q); 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate /* 148*0Sstevel@tonic-gate * Initialize the log structure for a new zone. 149*0Sstevel@tonic-gate */ 150*0Sstevel@tonic-gate static void * 151*0Sstevel@tonic-gate log_zoneinit(zoneid_t zoneid) 152*0Sstevel@tonic-gate { 153*0Sstevel@tonic-gate int i; 154*0Sstevel@tonic-gate log_zone_t *lzp; 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate if (zoneid == GLOBAL_ZONEID) 157*0Sstevel@tonic-gate lzp = &log_global; /* use statically allocated struct */ 158*0Sstevel@tonic-gate else 159*0Sstevel@tonic-gate lzp = kmem_zalloc(sizeof (log_zone_t), KM_SLEEP); 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate for (i = 0; i < LOG_NUMCLONES; i++) { 162*0Sstevel@tonic-gate lzp->lz_clones[i].log_minor = 163*0Sstevel@tonic-gate (minor_t)id_alloc(log_minorspace); 164*0Sstevel@tonic-gate lzp->lz_clones[i].log_zoneid = zoneid; 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate return (lzp); 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate /*ARGSUSED*/ 170*0Sstevel@tonic-gate static void 171*0Sstevel@tonic-gate log_zonefree(zoneid_t zoneid, void *arg) 172*0Sstevel@tonic-gate { 173*0Sstevel@tonic-gate log_zone_t *lzp = arg; 174*0Sstevel@tonic-gate int i; 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate ASSERT(lzp != &log_global && zoneid != GLOBAL_ZONEID); 177*0Sstevel@tonic-gate if (lzp == NULL) 178*0Sstevel@tonic-gate return; 179*0Sstevel@tonic-gate for (i = 0; i < LOG_NUMCLONES; i++) 180*0Sstevel@tonic-gate id_free(log_minorspace, lzp->lz_clones[i].log_minor); 181*0Sstevel@tonic-gate kmem_free(lzp, sizeof (log_zone_t)); 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate void 185*0Sstevel@tonic-gate log_init(void) 186*0Sstevel@tonic-gate { 187*0Sstevel@tonic-gate int log_maxzones; 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate /* 190*0Sstevel@tonic-gate * Create a backlog queue to consume console messages during periods 191*0Sstevel@tonic-gate * when there is no console reader (e.g. before syslogd(1M) starts). 192*0Sstevel@tonic-gate */ 193*0Sstevel@tonic-gate log_backlogq = log_consq = log_makeq(0, LOG_HIWAT, NULL); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate /* 196*0Sstevel@tonic-gate * Create a queue to hold free message of size <= LOG_MSGSIZE. 197*0Sstevel@tonic-gate * Calls from high-level interrupt handlers will do a getq_noenab() 198*0Sstevel@tonic-gate * from this queue, so its q_lock must be a maximum SPL spin lock. 199*0Sstevel@tonic-gate */ 200*0Sstevel@tonic-gate log_freeq = log_makeq(LOG_MINFREE, LOG_MAXFREE, (void *)ipltospl(SPL8)); 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate /* 203*0Sstevel@tonic-gate * Create a queue for messages from high-level interrupt context. 204*0Sstevel@tonic-gate * These messages are drained via softcall, or explicitly by panic(). 205*0Sstevel@tonic-gate */ 206*0Sstevel@tonic-gate log_intrq = log_makeq(0, LOG_HIWAT, (void *)ipltospl(SPL8)); 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate /* 209*0Sstevel@tonic-gate * Create a queue to hold the most recent 8K of console messages. 210*0Sstevel@tonic-gate * Useful for debugging. Required by the "$<msgbuf" adb macro. 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate log_recentq = log_makeq(0, LOG_RECENTSIZE, NULL); 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* 215*0Sstevel@tonic-gate * Create an id space for clone devices opened via /dev/log. 216*0Sstevel@tonic-gate * Need to limit the number of zones to avoid exceeding the 217*0Sstevel@tonic-gate * available minor number space. 218*0Sstevel@tonic-gate */ 219*0Sstevel@tonic-gate log_maxzones = (L_MAXMIN32 - LOG_LOGMIN) / LOG_NUMCLONES - 1; 220*0Sstevel@tonic-gate if (log_maxzones < maxzones) 221*0Sstevel@tonic-gate maxzones = log_maxzones; 222*0Sstevel@tonic-gate log_minorspace = id_space_create("logminor_space", LOG_LOGMIN + 1, 223*0Sstevel@tonic-gate L_MAXMIN32); 224*0Sstevel@tonic-gate /* 225*0Sstevel@tonic-gate * Put ourselves on the ZSD list. Note that zones have not been 226*0Sstevel@tonic-gate * initialized yet, but our constructor will be called on the global 227*0Sstevel@tonic-gate * zone when they are. 228*0Sstevel@tonic-gate */ 229*0Sstevel@tonic-gate zone_key_create(&log_zone_key, log_zoneinit, NULL, log_zonefree); 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate /* 232*0Sstevel@tonic-gate * Initialize backlog structure. 233*0Sstevel@tonic-gate */ 234*0Sstevel@tonic-gate log_backlog.log_zoneid = GLOBAL_ZONEID; 235*0Sstevel@tonic-gate log_backlog.log_minor = LOG_BACKLOG; 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate /* 238*0Sstevel@tonic-gate * Initialize conslog structure. 239*0Sstevel@tonic-gate */ 240*0Sstevel@tonic-gate log_conslog.log_zoneid = GLOBAL_ZONEID; 241*0Sstevel@tonic-gate log_conslog.log_minor = LOG_CONSMIN; 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate /* 244*0Sstevel@tonic-gate * Let the logging begin. 245*0Sstevel@tonic-gate */ 246*0Sstevel@tonic-gate log_update(&log_backlog, log_backlogq, SL_CONSOLE, log_console); 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate /* 249*0Sstevel@tonic-gate * Now that logging is enabled, emit the SunOS banner. 250*0Sstevel@tonic-gate */ 251*0Sstevel@tonic-gate printf("\rSunOS Release %s Version %s %u-bit\n", 252*0Sstevel@tonic-gate utsname.release, utsname.version, NBBY * (uint_t)sizeof (void *)); 253*0Sstevel@tonic-gate printf("Copyright 1983-2005 Sun Microsystems, Inc. " 254*0Sstevel@tonic-gate "All rights reserved.\nUse is subject to license terms.\n"); 255*0Sstevel@tonic-gate #ifdef DEBUG 256*0Sstevel@tonic-gate printf("DEBUG enabled\n"); 257*0Sstevel@tonic-gate #endif 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate /* 261*0Sstevel@tonic-gate * Allocate a log device corresponding to supplied device type. All 262*0Sstevel@tonic-gate * processes within a given zone that open /dev/conslog share the same 263*0Sstevel@tonic-gate * device; processes opening /dev/log get distinct devices (if 264*0Sstevel@tonic-gate * available). 265*0Sstevel@tonic-gate */ 266*0Sstevel@tonic-gate log_t * 267*0Sstevel@tonic-gate log_alloc(minor_t type) 268*0Sstevel@tonic-gate { 269*0Sstevel@tonic-gate zone_t *zptr = curproc->p_zone; 270*0Sstevel@tonic-gate log_zone_t *lzp; 271*0Sstevel@tonic-gate log_t *lp; 272*0Sstevel@tonic-gate int i; 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate if (type == LOG_CONSMIN) { 275*0Sstevel@tonic-gate /* return the dedicated /dev/conslog device */ 276*0Sstevel@tonic-gate return (&log_conslog); 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate ASSERT(type == LOG_LOGMIN); 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate lzp = zone_getspecific(log_zone_key, zptr); 282*0Sstevel@tonic-gate ASSERT(lzp != NULL); 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate /* search for an available /dev/log device for the zone */ 285*0Sstevel@tonic-gate for (i = LOG_LOGMINIDX; i <= LOG_LOGMAXIDX; i++) { 286*0Sstevel@tonic-gate lp = &lzp->lz_clones[i]; 287*0Sstevel@tonic-gate if (lp->log_inuse == 0) 288*0Sstevel@tonic-gate break; 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate if (i > LOG_LOGMAXIDX) 291*0Sstevel@tonic-gate lp = NULL; 292*0Sstevel@tonic-gate return (lp); 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate /* 296*0Sstevel@tonic-gate * Move console messages from src to dst. The time of day isn't known 297*0Sstevel@tonic-gate * early in boot, so fix up the message timestamps if necessary. 298*0Sstevel@tonic-gate */ 299*0Sstevel@tonic-gate static void 300*0Sstevel@tonic-gate log_conswitch(log_t *src, log_t *dst) 301*0Sstevel@tonic-gate { 302*0Sstevel@tonic-gate mblk_t *mp; 303*0Sstevel@tonic-gate mblk_t *hmp = NULL; 304*0Sstevel@tonic-gate mblk_t *tmp = NULL; 305*0Sstevel@tonic-gate log_ctl_t *hlc; 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate while ((mp = getq_noenab(src->log_q)) != NULL) { 308*0Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr; 309*0Sstevel@tonic-gate lc->flags |= SL_LOGONLY; 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate /* 312*0Sstevel@tonic-gate * In the early boot phase hrestime is invalid. 313*0Sstevel@tonic-gate * hrestime becomes valid when clock() runs for the first time. 314*0Sstevel@tonic-gate * At this time is lbolt == 1. log_sendmsg() saves the lbolt 315*0Sstevel@tonic-gate * value in ltime. 316*0Sstevel@tonic-gate */ 317*0Sstevel@tonic-gate if (lc->ltime < 2) { 318*0Sstevel@tonic-gate /* 319*0Sstevel@tonic-gate * Look ahead to first early boot message with time. 320*0Sstevel@tonic-gate */ 321*0Sstevel@tonic-gate if (hmp) { 322*0Sstevel@tonic-gate tmp->b_next = mp; 323*0Sstevel@tonic-gate tmp = mp; 324*0Sstevel@tonic-gate } else 325*0Sstevel@tonic-gate hmp = tmp = mp; 326*0Sstevel@tonic-gate continue; 327*0Sstevel@tonic-gate } 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate while (hmp) { 330*0Sstevel@tonic-gate tmp = hmp->b_next; 331*0Sstevel@tonic-gate hmp->b_next = NULL; 332*0Sstevel@tonic-gate hlc = (log_ctl_t *)hmp->b_rptr; 333*0Sstevel@tonic-gate /* 334*0Sstevel@tonic-gate * Calculate hrestime for an early log message with 335*0Sstevel@tonic-gate * an invalid time stamp. We know: 336*0Sstevel@tonic-gate * - the lbolt of the invalid time stamp. 337*0Sstevel@tonic-gate * - the hrestime and lbolt of the first valid 338*0Sstevel@tonic-gate * time stamp. 339*0Sstevel@tonic-gate */ 340*0Sstevel@tonic-gate hlc->ttime = lc->ttime - (lc->ltime - hlc->ltime) / hz; 341*0Sstevel@tonic-gate (void) putq(dst->log_q, hmp); 342*0Sstevel@tonic-gate hmp = tmp; 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate (void) putq(dst->log_q, mp); 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate while (hmp) { 347*0Sstevel@tonic-gate tmp = hmp->b_next; 348*0Sstevel@tonic-gate hmp->b_next = NULL; 349*0Sstevel@tonic-gate hlc = (log_ctl_t *)hmp->b_rptr; 350*0Sstevel@tonic-gate hlc->ttime = gethrestime_sec() - (lbolt - hlc->ltime) / hz; 351*0Sstevel@tonic-gate (void) putq(dst->log_q, hmp); 352*0Sstevel@tonic-gate hmp = tmp; 353*0Sstevel@tonic-gate } 354*0Sstevel@tonic-gate dst->log_overflow = src->log_overflow; 355*0Sstevel@tonic-gate src->log_flags = 0; 356*0Sstevel@tonic-gate dst->log_flags = SL_CONSOLE; 357*0Sstevel@tonic-gate log_consq = dst->log_q; 358*0Sstevel@tonic-gate } 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate /* 361*0Sstevel@tonic-gate * Set the fields in the 'target' clone to the specified values. 362*0Sstevel@tonic-gate * Then, look at all clones to determine which message types are 363*0Sstevel@tonic-gate * currently active and which clone is the primary console queue. 364*0Sstevel@tonic-gate * If the primary console queue changes to or from the backlog 365*0Sstevel@tonic-gate * queue, copy all messages from backlog to primary or vice versa. 366*0Sstevel@tonic-gate */ 367*0Sstevel@tonic-gate void 368*0Sstevel@tonic-gate log_update(log_t *target, queue_t *q, short flags, log_filter_t *filter) 369*0Sstevel@tonic-gate { 370*0Sstevel@tonic-gate log_t *lp; 371*0Sstevel@tonic-gate short active = SL_CONSOLE; 372*0Sstevel@tonic-gate zone_t *zptr = NULL; 373*0Sstevel@tonic-gate log_zone_t *lzp; 374*0Sstevel@tonic-gate zoneid_t zoneid = target->log_zoneid; 375*0Sstevel@tonic-gate int i; 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate log_enter(); 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate if (q != NULL) 380*0Sstevel@tonic-gate target->log_q = q; 381*0Sstevel@tonic-gate target->log_wanted = filter; 382*0Sstevel@tonic-gate target->log_flags = flags; 383*0Sstevel@tonic-gate target->log_overflow = 0; 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate /* 386*0Sstevel@tonic-gate * Need to special case the global zone here since this may be 387*0Sstevel@tonic-gate * called before zone_init. 388*0Sstevel@tonic-gate */ 389*0Sstevel@tonic-gate if (zoneid == GLOBAL_ZONEID) { 390*0Sstevel@tonic-gate lzp = &log_global; 391*0Sstevel@tonic-gate } else if ((zptr = zone_find_by_id(zoneid)) == NULL) { 392*0Sstevel@tonic-gate log_exit(); 393*0Sstevel@tonic-gate return; /* zone is being destroyed, ignore update */ 394*0Sstevel@tonic-gate } else { 395*0Sstevel@tonic-gate lzp = zone_getspecific(log_zone_key, zptr); 396*0Sstevel@tonic-gate } 397*0Sstevel@tonic-gate ASSERT(lzp != NULL); 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate for (i = LOG_LOGMAXIDX; i >= LOG_LOGMINIDX; i--) { 400*0Sstevel@tonic-gate lp = &lzp->lz_clones[i]; 401*0Sstevel@tonic-gate if (zoneid == GLOBAL_ZONEID && (lp->log_flags & SL_CONSOLE)) 402*0Sstevel@tonic-gate log_consq = lp->log_q; 403*0Sstevel@tonic-gate active |= lp->log_flags; 404*0Sstevel@tonic-gate } 405*0Sstevel@tonic-gate lzp->lz_active = active; 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gate if (zptr) 408*0Sstevel@tonic-gate zone_rele(zptr); 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate if (log_consq == target->log_q) { 411*0Sstevel@tonic-gate if (flags & SL_CONSOLE) 412*0Sstevel@tonic-gate log_conswitch(&log_backlog, target); 413*0Sstevel@tonic-gate else 414*0Sstevel@tonic-gate log_conswitch(target, &log_backlog); 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate target->log_q = q; 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate log_exit(); 419*0Sstevel@tonic-gate } 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate /*ARGSUSED*/ 422*0Sstevel@tonic-gate int 423*0Sstevel@tonic-gate log_error(log_t *lp, log_ctl_t *lc) 424*0Sstevel@tonic-gate { 425*0Sstevel@tonic-gate if ((lc->pri & LOG_FACMASK) == LOG_KERN) 426*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_ERR; 427*0Sstevel@tonic-gate return (1); 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate int 431*0Sstevel@tonic-gate log_trace(log_t *lp, log_ctl_t *lc) 432*0Sstevel@tonic-gate { 433*0Sstevel@tonic-gate trace_ids_t *tid = (trace_ids_t *)lp->log_data->b_rptr; 434*0Sstevel@tonic-gate trace_ids_t *tidend = (trace_ids_t *)lp->log_data->b_wptr; 435*0Sstevel@tonic-gate 436*0Sstevel@tonic-gate /* 437*0Sstevel@tonic-gate * We use `tid + 1 <= tidend' here rather than the more traditional 438*0Sstevel@tonic-gate * `tid < tidend', since the former ensures that there's at least 439*0Sstevel@tonic-gate * `sizeof (trace_ids_t)' bytes available before executing the 440*0Sstevel@tonic-gate * loop, whereas the latter only ensures that there's a single byte. 441*0Sstevel@tonic-gate */ 442*0Sstevel@tonic-gate for (; tid + 1 <= tidend; tid++) { 443*0Sstevel@tonic-gate if (tid->ti_level < lc->level && tid->ti_level >= 0) 444*0Sstevel@tonic-gate continue; 445*0Sstevel@tonic-gate if (tid->ti_mid != lc->mid && tid->ti_mid >= 0) 446*0Sstevel@tonic-gate continue; 447*0Sstevel@tonic-gate if (tid->ti_sid != lc->sid && tid->ti_sid >= 0) 448*0Sstevel@tonic-gate continue; 449*0Sstevel@tonic-gate if ((lc->pri & LOG_FACMASK) == LOG_KERN) 450*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_DEBUG; 451*0Sstevel@tonic-gate return (1); 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate return (0); 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate /*ARGSUSED*/ 457*0Sstevel@tonic-gate int 458*0Sstevel@tonic-gate log_console(log_t *lp, log_ctl_t *lc) 459*0Sstevel@tonic-gate { 460*0Sstevel@tonic-gate if ((lc->pri & LOG_FACMASK) == LOG_KERN) { 461*0Sstevel@tonic-gate if (lc->flags & SL_FATAL) 462*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_CRIT; 463*0Sstevel@tonic-gate else if (lc->flags & SL_ERROR) 464*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_ERR; 465*0Sstevel@tonic-gate else if (lc->flags & SL_WARN) 466*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_WARNING; 467*0Sstevel@tonic-gate else if (lc->flags & SL_NOTE) 468*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_NOTICE; 469*0Sstevel@tonic-gate else if (lc->flags & SL_TRACE) 470*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_DEBUG; 471*0Sstevel@tonic-gate else 472*0Sstevel@tonic-gate lc->pri = LOG_KERN | LOG_INFO; 473*0Sstevel@tonic-gate } 474*0Sstevel@tonic-gate return (1); 475*0Sstevel@tonic-gate } 476*0Sstevel@tonic-gate 477*0Sstevel@tonic-gate mblk_t * 478*0Sstevel@tonic-gate log_makemsg(int mid, int sid, int level, int sl, int pri, void *msg, 479*0Sstevel@tonic-gate size_t size, int on_intr) 480*0Sstevel@tonic-gate { 481*0Sstevel@tonic-gate mblk_t *mp = NULL; 482*0Sstevel@tonic-gate mblk_t *mp2; 483*0Sstevel@tonic-gate log_ctl_t *lc; 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate if (size <= LOG_MSGSIZE && 486*0Sstevel@tonic-gate (on_intr || log_freeq->q_count > log_freeq->q_lowat)) 487*0Sstevel@tonic-gate mp = getq_noenab(log_freeq); 488*0Sstevel@tonic-gate 489*0Sstevel@tonic-gate if (mp == NULL) { 490*0Sstevel@tonic-gate if (on_intr || 491*0Sstevel@tonic-gate (mp = allocb(sizeof (log_ctl_t), BPRI_HI)) == NULL || 492*0Sstevel@tonic-gate (mp2 = allocb(MAX(size, LOG_MSGSIZE), BPRI_HI)) == NULL) { 493*0Sstevel@tonic-gate freemsg(mp); 494*0Sstevel@tonic-gate return (NULL); 495*0Sstevel@tonic-gate } 496*0Sstevel@tonic-gate DB_TYPE(mp) = M_PROTO; 497*0Sstevel@tonic-gate mp->b_wptr += sizeof (log_ctl_t); 498*0Sstevel@tonic-gate mp->b_cont = mp2; 499*0Sstevel@tonic-gate } else { 500*0Sstevel@tonic-gate mp2 = mp->b_cont; 501*0Sstevel@tonic-gate mp2->b_wptr = mp2->b_rptr; 502*0Sstevel@tonic-gate } 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate lc = (log_ctl_t *)mp->b_rptr; 505*0Sstevel@tonic-gate lc->mid = mid; 506*0Sstevel@tonic-gate lc->sid = sid; 507*0Sstevel@tonic-gate lc->level = level; 508*0Sstevel@tonic-gate lc->flags = sl; 509*0Sstevel@tonic-gate lc->pri = pri; 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate bcopy(msg, mp2->b_wptr, size - 1); 512*0Sstevel@tonic-gate mp2->b_wptr[size - 1] = '\0'; 513*0Sstevel@tonic-gate mp2->b_wptr += strlen((char *)mp2->b_wptr) + 1; 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gate return (mp); 516*0Sstevel@tonic-gate } 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate void 519*0Sstevel@tonic-gate log_freemsg(mblk_t *mp) 520*0Sstevel@tonic-gate { 521*0Sstevel@tonic-gate mblk_t *mp2 = mp->b_cont; 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate ASSERT(MBLKL(mp) == sizeof (log_ctl_t)); 524*0Sstevel@tonic-gate ASSERT(mp2->b_rptr == mp2->b_datap->db_base); 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gate if ((log_freeq->q_flag & QFULL) == 0 && 527*0Sstevel@tonic-gate MBLKL(mp2) <= LOG_MSGSIZE && MBLKSIZE(mp2) >= LOG_MSGSIZE) 528*0Sstevel@tonic-gate (void) putq(log_freeq, mp); 529*0Sstevel@tonic-gate else 530*0Sstevel@tonic-gate freemsg(mp); 531*0Sstevel@tonic-gate } 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gate void 534*0Sstevel@tonic-gate log_sendmsg(mblk_t *mp, zoneid_t zoneid) 535*0Sstevel@tonic-gate { 536*0Sstevel@tonic-gate log_t *lp; 537*0Sstevel@tonic-gate char *src, *dst; 538*0Sstevel@tonic-gate mblk_t *mp2 = mp->b_cont; 539*0Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr; 540*0Sstevel@tonic-gate int flags, fac; 541*0Sstevel@tonic-gate off_t facility = 0; 542*0Sstevel@tonic-gate off_t body = 0; 543*0Sstevel@tonic-gate zone_t *zptr = NULL; 544*0Sstevel@tonic-gate log_zone_t *lzp; 545*0Sstevel@tonic-gate int i; 546*0Sstevel@tonic-gate int backlog; 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gate /* 549*0Sstevel@tonic-gate * Need to special case the global zone here since this may be 550*0Sstevel@tonic-gate * called before zone_init. 551*0Sstevel@tonic-gate */ 552*0Sstevel@tonic-gate if (zoneid == GLOBAL_ZONEID) { 553*0Sstevel@tonic-gate lzp = &log_global; 554*0Sstevel@tonic-gate } else if ((zptr = zone_find_by_id(zoneid)) == NULL) { 555*0Sstevel@tonic-gate /* specified zone doesn't exist, free message and return */ 556*0Sstevel@tonic-gate log_freemsg(mp); 557*0Sstevel@tonic-gate return; 558*0Sstevel@tonic-gate } else { 559*0Sstevel@tonic-gate lzp = zone_getspecific(log_zone_key, zptr); 560*0Sstevel@tonic-gate } 561*0Sstevel@tonic-gate ASSERT(lzp != NULL); 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate if ((lc->flags & lzp->lz_active) == 0) { 564*0Sstevel@tonic-gate if (zptr) 565*0Sstevel@tonic-gate zone_rele(zptr); 566*0Sstevel@tonic-gate log_freemsg(mp); 567*0Sstevel@tonic-gate return; 568*0Sstevel@tonic-gate } 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gate if (panicstr) { 571*0Sstevel@tonic-gate /* 572*0Sstevel@tonic-gate * Raise the console queue's q_hiwat to ensure that we 573*0Sstevel@tonic-gate * capture all panic messages. 574*0Sstevel@tonic-gate */ 575*0Sstevel@tonic-gate log_consq->q_hiwat = 2 * LOG_HIWAT; 576*0Sstevel@tonic-gate log_consq->q_flag &= ~QFULL; 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gate /* Message was created while panicking. */ 579*0Sstevel@tonic-gate lc->flags |= SL_PANICMSG; 580*0Sstevel@tonic-gate } 581*0Sstevel@tonic-gate 582*0Sstevel@tonic-gate src = (char *)mp2->b_rptr; 583*0Sstevel@tonic-gate dst = strstr(src, "FACILITY_AND_PRIORITY] "); 584*0Sstevel@tonic-gate if (dst != NULL) { 585*0Sstevel@tonic-gate facility = dst - src; 586*0Sstevel@tonic-gate body = facility + 23; /* strlen("FACILITY_AND_PRIORITY] ") */ 587*0Sstevel@tonic-gate } 588*0Sstevel@tonic-gate 589*0Sstevel@tonic-gate log_enter(); 590*0Sstevel@tonic-gate 591*0Sstevel@tonic-gate lc->ltime = lbolt; 592*0Sstevel@tonic-gate lc->ttime = gethrestime_sec(); 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gate flags = lc->flags & lzp->lz_active; 595*0Sstevel@tonic-gate log_seq_no[flags & SL_ERROR]++; 596*0Sstevel@tonic-gate log_seq_no[flags & SL_TRACE]++; 597*0Sstevel@tonic-gate log_seq_no[flags & SL_CONSOLE]++; 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gate /* 600*0Sstevel@tonic-gate * If this is in the global zone, start with the backlog, then 601*0Sstevel@tonic-gate * walk through the clone logs. If not, just do the clone logs. 602*0Sstevel@tonic-gate */ 603*0Sstevel@tonic-gate backlog = (zoneid == GLOBAL_ZONEID); 604*0Sstevel@tonic-gate i = LOG_LOGMINIDX; 605*0Sstevel@tonic-gate while (i <= LOG_LOGMAXIDX) { 606*0Sstevel@tonic-gate if (backlog) { 607*0Sstevel@tonic-gate /* 608*0Sstevel@tonic-gate * Do the backlog this time, then start on the 609*0Sstevel@tonic-gate * others. 610*0Sstevel@tonic-gate */ 611*0Sstevel@tonic-gate backlog = 0; 612*0Sstevel@tonic-gate lp = &log_backlog; 613*0Sstevel@tonic-gate } else { 614*0Sstevel@tonic-gate lp = &lzp->lz_clones[i++]; 615*0Sstevel@tonic-gate } 616*0Sstevel@tonic-gate 617*0Sstevel@tonic-gate if ((lp->log_flags & flags) && lp->log_wanted(lp, lc)) { 618*0Sstevel@tonic-gate if (canput(lp->log_q)) { 619*0Sstevel@tonic-gate lp->log_overflow = 0; 620*0Sstevel@tonic-gate lc->seq_no = log_seq_no[lp->log_flags]; 621*0Sstevel@tonic-gate if ((mp2 = copymsg(mp)) == NULL) 622*0Sstevel@tonic-gate break; 623*0Sstevel@tonic-gate if (facility != 0) { 624*0Sstevel@tonic-gate src = (char *)mp2->b_cont->b_rptr; 625*0Sstevel@tonic-gate dst = src + facility; 626*0Sstevel@tonic-gate fac = (lc->pri & LOG_FACMASK) >> 3; 627*0Sstevel@tonic-gate dst += snprintf(dst, 628*0Sstevel@tonic-gate LOG_FACSIZE + LOG_PRISIZE, "%s.%s", 629*0Sstevel@tonic-gate log_fac[MIN(fac, LOG_NFACILITIES)], 630*0Sstevel@tonic-gate log_pri[lc->pri & LOG_PRIMASK]); 631*0Sstevel@tonic-gate src += body - 2; /* copy "] " too */ 632*0Sstevel@tonic-gate while (*src != '\0') 633*0Sstevel@tonic-gate *dst++ = *src++; 634*0Sstevel@tonic-gate *dst++ = '\0'; 635*0Sstevel@tonic-gate mp2->b_cont->b_wptr = (uchar_t *)dst; 636*0Sstevel@tonic-gate } 637*0Sstevel@tonic-gate (void) putq(lp->log_q, mp2); 638*0Sstevel@tonic-gate } else if (++lp->log_overflow == 1) { 639*0Sstevel@tonic-gate if (lp->log_q == log_consq) { 640*0Sstevel@tonic-gate console_printf(log_overflow_msg, 641*0Sstevel@tonic-gate lp->log_minor, 642*0Sstevel@tonic-gate " -- is syslogd(1M) running?"); 643*0Sstevel@tonic-gate } else { 644*0Sstevel@tonic-gate printf(log_overflow_msg, 645*0Sstevel@tonic-gate lp->log_minor, ""); 646*0Sstevel@tonic-gate } 647*0Sstevel@tonic-gate } 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate } 650*0Sstevel@tonic-gate 651*0Sstevel@tonic-gate if (zptr) 652*0Sstevel@tonic-gate zone_rele(zptr); 653*0Sstevel@tonic-gate 654*0Sstevel@tonic-gate if ((flags & SL_CONSOLE) && (lc->pri & LOG_FACMASK) == LOG_KERN) { 655*0Sstevel@tonic-gate if ((mp2 == NULL || log_consq == log_backlogq || panicstr) && 656*0Sstevel@tonic-gate (lc->flags & SL_LOGONLY) == 0) 657*0Sstevel@tonic-gate console_printf("%s", (char *)mp->b_cont->b_rptr + body); 658*0Sstevel@tonic-gate if ((lc->flags & SL_CONSONLY) == 0 && 659*0Sstevel@tonic-gate (mp2 = copymsg(mp)) != NULL) { 660*0Sstevel@tonic-gate mp2->b_cont->b_rptr += body; 661*0Sstevel@tonic-gate if (log_recentq->q_flag & QFULL) 662*0Sstevel@tonic-gate freemsg(getq_noenab(log_recentq)); 663*0Sstevel@tonic-gate (void) putq(log_recentq, mp2); 664*0Sstevel@tonic-gate } 665*0Sstevel@tonic-gate } 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gate log_freemsg(mp); 668*0Sstevel@tonic-gate 669*0Sstevel@tonic-gate log_exit(); 670*0Sstevel@tonic-gate } 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate /* 673*0Sstevel@tonic-gate * Print queued messages to console. 674*0Sstevel@tonic-gate */ 675*0Sstevel@tonic-gate void 676*0Sstevel@tonic-gate log_printq(queue_t *qfirst) 677*0Sstevel@tonic-gate { 678*0Sstevel@tonic-gate mblk_t *mp; 679*0Sstevel@tonic-gate queue_t *q, *qlast; 680*0Sstevel@tonic-gate char *cp, *msgp; 681*0Sstevel@tonic-gate log_ctl_t *lc; 682*0Sstevel@tonic-gate 683*0Sstevel@tonic-gate /* 684*0Sstevel@tonic-gate * Look ahead to first queued message in the stream. 685*0Sstevel@tonic-gate */ 686*0Sstevel@tonic-gate qlast = NULL; 687*0Sstevel@tonic-gate do { 688*0Sstevel@tonic-gate for (q = qfirst; q->q_next != qlast; q = q->q_next) 689*0Sstevel@tonic-gate continue; 690*0Sstevel@tonic-gate for (mp = q->q_first; mp != NULL; mp = mp->b_next) { 691*0Sstevel@tonic-gate lc = (log_ctl_t *)mp->b_rptr; 692*0Sstevel@tonic-gate /* 693*0Sstevel@tonic-gate * Check if message is already displayed at 694*0Sstevel@tonic-gate * /dev/console. 695*0Sstevel@tonic-gate */ 696*0Sstevel@tonic-gate if (lc->flags & SL_PANICMSG) 697*0Sstevel@tonic-gate continue; 698*0Sstevel@tonic-gate 699*0Sstevel@tonic-gate cp = (char *)mp->b_cont->b_rptr; 700*0Sstevel@tonic-gate 701*0Sstevel@tonic-gate /* Strip off the message ID. */ 702*0Sstevel@tonic-gate if ((msgp = strstr(cp, "[ID ")) != NULL && 703*0Sstevel@tonic-gate (msgp = strstr(msgp, "] ")) != NULL) { 704*0Sstevel@tonic-gate cp = msgp + 2; 705*0Sstevel@tonic-gate } 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gate /* 708*0Sstevel@tonic-gate * Using console_printf instead of printf to avoid 709*0Sstevel@tonic-gate * queueing messages to log_consq. 710*0Sstevel@tonic-gate */ 711*0Sstevel@tonic-gate console_printf("%s", cp); 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate } while ((qlast = q) != qfirst); 714*0Sstevel@tonic-gate } 715