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