10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1259Sns92644 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Streams log driver. See log(7D). 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <sys/param.h> 350Sstevel@tonic-gate #include <sys/errno.h> 360Sstevel@tonic-gate #include <sys/stropts.h> 370Sstevel@tonic-gate #include <sys/stream.h> 380Sstevel@tonic-gate #include <sys/strsun.h> 390Sstevel@tonic-gate #include <sys/debug.h> 400Sstevel@tonic-gate #include <sys/cred.h> 410Sstevel@tonic-gate #include <sys/file.h> 420Sstevel@tonic-gate #include <sys/ddi.h> 430Sstevel@tonic-gate #include <sys/stat.h> 440Sstevel@tonic-gate #include <sys/syslog.h> 450Sstevel@tonic-gate #include <sys/log.h> 460Sstevel@tonic-gate #include <sys/systm.h> 470Sstevel@tonic-gate #include <sys/modctl.h> 480Sstevel@tonic-gate #include <sys/policy.h> 490Sstevel@tonic-gate #include <sys/zone.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate #include <sys/conf.h> 520Sstevel@tonic-gate #include <sys/sunddi.h> 530Sstevel@tonic-gate 540Sstevel@tonic-gate static dev_info_t *log_devi; /* private copy of devinfo pointer */ 550Sstevel@tonic-gate int log_msgid; /* log.conf tunable: enable msgid generation */ 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* ARGSUSED */ 580Sstevel@tonic-gate static int 590Sstevel@tonic-gate log_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 600Sstevel@tonic-gate { 610Sstevel@tonic-gate switch (infocmd) { 620Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 630Sstevel@tonic-gate *result = log_devi; 640Sstevel@tonic-gate return (DDI_SUCCESS); 650Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 660Sstevel@tonic-gate *result = 0; 670Sstevel@tonic-gate return (DDI_SUCCESS); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate return (DDI_FAILURE); 700Sstevel@tonic-gate } 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* ARGSUSED */ 730Sstevel@tonic-gate static int 740Sstevel@tonic-gate log_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 750Sstevel@tonic-gate { 760Sstevel@tonic-gate if (ddi_create_minor_node(devi, "conslog", S_IFCHR, 770Sstevel@tonic-gate LOG_CONSMIN, DDI_PSEUDO, NULL) == DDI_FAILURE || 780Sstevel@tonic-gate ddi_create_minor_node(devi, "log", S_IFCHR, 790Sstevel@tonic-gate LOG_LOGMIN, DDI_PSEUDO, NULL) == DDI_FAILURE) { 800Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 810Sstevel@tonic-gate return (DDI_FAILURE); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate log_devi = devi; 840Sstevel@tonic-gate log_msgid = ddi_getprop(DDI_DEV_T_ANY, log_devi, 850Sstevel@tonic-gate DDI_PROP_CANSLEEP, "msgid", 1); 860Sstevel@tonic-gate return (DDI_SUCCESS); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 90*1259Sns92644 * log_open can be called for either /dev/log or dev/conslog. 91*1259Sns92644 * 92*1259Sns92644 * In the /dev/conslog case log_alloc() allocates a new minor device from 93*1259Sns92644 * its cache. 94*1259Sns92644 * 95*1259Sns92644 * In the case of /dev/log, LOG_NUMCLONES devices are pre-allocated at zone 96*1259Sns92644 * creation. log_alloc() finds the zone's next available minor device. 97*1259Sns92644 * 98*1259Sns92644 * On entry devp's minor number indicates which device (log or conslog), on 99*1259Sns92644 * successful return it is the device instance. 1000Sstevel@tonic-gate */ 101*1259Sns92644 1020Sstevel@tonic-gate /* ARGSUSED */ 1030Sstevel@tonic-gate static int 1040Sstevel@tonic-gate log_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *cr) 1050Sstevel@tonic-gate { 1060Sstevel@tonic-gate log_t *lp; 1070Sstevel@tonic-gate minor_t minor; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate if (sflag & (MODOPEN | CLONEOPEN)) 1100Sstevel@tonic-gate return (ENXIO); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate switch (minor = getminor(*devp)) { 113*1259Sns92644 case LOG_CONSMIN: /* clone open of /dev/conslog */ 1140Sstevel@tonic-gate if (flag & FREAD) 1150Sstevel@tonic-gate return (EINVAL); /* write-only device */ 1160Sstevel@tonic-gate if (q->q_ptr) 1170Sstevel@tonic-gate return (0); 1180Sstevel@tonic-gate break; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate case LOG_LOGMIN: /* clone open of /dev/log */ 1210Sstevel@tonic-gate break; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate default: 1240Sstevel@tonic-gate return (ENXIO); 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate lp = log_alloc(minor); 1280Sstevel@tonic-gate if (lp == NULL) 1290Sstevel@tonic-gate return (ENXIO); 1300Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), lp->log_minor); 1310Sstevel@tonic-gate q->q_ptr = lp; 1320Sstevel@tonic-gate WR(q)->q_ptr = lp; 1330Sstevel@tonic-gate lp->log_inuse = 1; 1340Sstevel@tonic-gate qprocson(q); 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate return (0); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* ARGSUSED */ 1400Sstevel@tonic-gate static int 1410Sstevel@tonic-gate log_close(queue_t *q, int flag, cred_t *cr) 1420Sstevel@tonic-gate { 1430Sstevel@tonic-gate log_t *lp = (log_t *)q->q_ptr; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate qprocsoff(q); 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate lp->log_inuse = 0; 1480Sstevel@tonic-gate log_update(lp, NULL, 0, NULL); 1490Sstevel@tonic-gate freemsg(lp->log_data); 1500Sstevel@tonic-gate lp->log_data = NULL; 151*1259Sns92644 if (lp->log_major == LOG_CONSMIN) 152*1259Sns92644 log_free(lp); 1530Sstevel@tonic-gate q->q_ptr = NULL; 1540Sstevel@tonic-gate WR(q)->q_ptr = NULL; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate return (0); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static int 1600Sstevel@tonic-gate log_wput(queue_t *q, mblk_t *mp) 1610Sstevel@tonic-gate { 1620Sstevel@tonic-gate log_t *lp = (log_t *)q->q_ptr; 1630Sstevel@tonic-gate struct iocblk *iocp; 1640Sstevel@tonic-gate mblk_t *mp2; 1650Sstevel@tonic-gate cred_t *cr = DB_CRED(mp); 1660Sstevel@tonic-gate zoneid_t zoneid; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * Default to global zone if dblk doesn't have a valid cred. 1700Sstevel@tonic-gate * Calls to syslog() go through putmsg(), which does set up 1710Sstevel@tonic-gate * the cred. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate zoneid = (cr != NULL) ? crgetzoneid(cr) : GLOBAL_ZONEID; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate switch (DB_TYPE(mp)) { 1760Sstevel@tonic-gate case M_FLUSH: 1770Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 1780Sstevel@tonic-gate flushq(q, FLUSHALL); 1790Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 1820Sstevel@tonic-gate flushq(RD(q), FLUSHALL); 1830Sstevel@tonic-gate qreply(q, mp); 1840Sstevel@tonic-gate return (0); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate break; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate case M_IOCTL: 1890Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 1900Sstevel@tonic-gate 191*1259Sns92644 if (lp->log_major != LOG_LOGMIN) { 192*1259Sns92644 /* write-only device */ 1930Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 1940Sstevel@tonic-gate return (0); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate if (iocp->ioc_count == TRANSPARENT) { 1980Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 1990Sstevel@tonic-gate return (0); 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate if (lp->log_flags) { 2030Sstevel@tonic-gate miocnak(q, mp, 0, EBUSY); 2040Sstevel@tonic-gate return (0); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate freemsg(lp->log_data); 2080Sstevel@tonic-gate lp->log_data = mp->b_cont; 2090Sstevel@tonic-gate mp->b_cont = NULL; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate switch (iocp->ioc_cmd) { 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate case I_CONSLOG: 2140Sstevel@tonic-gate log_update(lp, RD(q), SL_CONSOLE, log_console); 2150Sstevel@tonic-gate break; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate case I_TRCLOG: 2180Sstevel@tonic-gate if (lp->log_data == NULL) { 2190Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 2200Sstevel@tonic-gate return (0); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate log_update(lp, RD(q), SL_TRACE, log_trace); 2230Sstevel@tonic-gate break; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate case I_ERRLOG: 2260Sstevel@tonic-gate log_update(lp, RD(q), SL_ERROR, log_error); 2270Sstevel@tonic-gate break; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate default: 2300Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 2310Sstevel@tonic-gate return (0); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate miocack(q, mp, 0, 0); 2340Sstevel@tonic-gate return (0); 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate case M_PROTO: 2370Sstevel@tonic-gate if (MBLKL(mp) == sizeof (log_ctl_t) && mp->b_cont != NULL) { 2380Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr; 2390Sstevel@tonic-gate /* This code is used by savecore to log dump msgs */ 2400Sstevel@tonic-gate if (mp->b_band != 0 && 2410Sstevel@tonic-gate secpolicy_sys_config(CRED(), B_FALSE) == 0) { 2420Sstevel@tonic-gate (void) putq(log_consq, mp); 2430Sstevel@tonic-gate return (0); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate if ((lc->pri & LOG_FACMASK) == LOG_KERN) 2460Sstevel@tonic-gate lc->pri |= LOG_USER; 2470Sstevel@tonic-gate mp2 = log_makemsg(LOG_MID, LOG_CONSMIN, lc->level, 2480Sstevel@tonic-gate lc->flags, lc->pri, mp->b_cont->b_rptr, 2490Sstevel@tonic-gate MBLKL(mp->b_cont) + 1, 0); 2500Sstevel@tonic-gate if (mp2 != NULL) 2510Sstevel@tonic-gate log_sendmsg(mp2, zoneid); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate break; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate case M_DATA: 2560Sstevel@tonic-gate mp2 = log_makemsg(LOG_MID, LOG_CONSMIN, 0, SL_CONSOLE, 2570Sstevel@tonic-gate LOG_USER | LOG_INFO, mp->b_rptr, MBLKL(mp) + 1, 0); 2580Sstevel@tonic-gate if (mp2 != NULL) 2590Sstevel@tonic-gate log_sendmsg(mp2, zoneid); 2600Sstevel@tonic-gate break; 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate freemsg(mp); 2640Sstevel@tonic-gate return (0); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate static int 2680Sstevel@tonic-gate log_rsrv(queue_t *q) 2690Sstevel@tonic-gate { 2700Sstevel@tonic-gate mblk_t *mp; 2710Sstevel@tonic-gate char *msg, *msgid_start, *msgid_end; 2720Sstevel@tonic-gate size_t idlen; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate while (canputnext(q) && (mp = getq(q)) != NULL) { 2750Sstevel@tonic-gate if (log_msgid == 0) { 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * Strip out the message ID. If it's a kernel 2780Sstevel@tonic-gate * SL_CONSOLE message, replace msgid with "unix: ". 2790Sstevel@tonic-gate */ 2800Sstevel@tonic-gate msg = (char *)mp->b_cont->b_rptr; 2810Sstevel@tonic-gate if ((msgid_start = strstr(msg, "[ID ")) != NULL && 2820Sstevel@tonic-gate (msgid_end = strstr(msgid_start, "] ")) != NULL) { 2830Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr; 2840Sstevel@tonic-gate if ((lc->flags & SL_CONSOLE) && 2850Sstevel@tonic-gate (lc->pri & LOG_FACMASK) == LOG_KERN) 2860Sstevel@tonic-gate msgid_start = msg + snprintf(msg, 2870Sstevel@tonic-gate 7, "unix: "); 2880Sstevel@tonic-gate idlen = msgid_end + 2 - msgid_start; 2890Sstevel@tonic-gate ovbcopy(msg, msg + idlen, msgid_start - msg); 2900Sstevel@tonic-gate mp->b_cont->b_rptr += idlen; 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate mp->b_band = 0; 2940Sstevel@tonic-gate putnext(q, mp); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate return (0); 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate static struct module_info logm_info = 3000Sstevel@tonic-gate { LOG_MID, "LOG", LOG_MINPS, LOG_MAXPS, LOG_HIWAT, LOG_LOWAT }; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate static struct qinit logrinit = 3030Sstevel@tonic-gate { NULL, log_rsrv, log_open, log_close, NULL, &logm_info, NULL }; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate static struct qinit logwinit = 3060Sstevel@tonic-gate { log_wput, NULL, NULL, NULL, NULL, &logm_info, NULL }; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate static struct streamtab loginfo = { &logrinit, &logwinit, NULL, NULL }; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(log_ops, nulldev, nulldev, log_attach, nodev, 3110Sstevel@tonic-gate nodev, log_info, D_NEW | D_MP | D_MTPERMOD, &loginfo); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate static struct modldrv modldrv = 3140Sstevel@tonic-gate { &mod_driverops, "streams log driver", &log_ops }; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate static struct modlinkage modlinkage = { MODREV_1, (void *)&modldrv, NULL }; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate int 3190Sstevel@tonic-gate _init() 3200Sstevel@tonic-gate { 3210Sstevel@tonic-gate return (mod_install(&modlinkage)); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate int 3250Sstevel@tonic-gate _fini() 3260Sstevel@tonic-gate { 3270Sstevel@tonic-gate return (mod_remove(&modlinkage)); 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate int 3310Sstevel@tonic-gate _info(struct modinfo *modinfop) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3340Sstevel@tonic-gate } 335