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
57656SSherry.Moore@Sun.COM * Common Development and Distribution License (the "License").
67656SSherry.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*8778SErik.Nordmark@Sun.COM * Copyright 2009 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>
35*8778SErik.Nordmark@Sun.COM #include <sys/strsubr.h>
360Sstevel@tonic-gate #include <sys/stream.h>
370Sstevel@tonic-gate #include <sys/strsun.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/cred.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/ddi.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/syslog.h>
440Sstevel@tonic-gate #include <sys/log.h>
450Sstevel@tonic-gate #include <sys/systm.h>
460Sstevel@tonic-gate #include <sys/modctl.h>
470Sstevel@tonic-gate #include <sys/policy.h>
480Sstevel@tonic-gate #include <sys/zone.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include <sys/conf.h>
510Sstevel@tonic-gate #include <sys/sunddi.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate static dev_info_t *log_devi; /* private copy of devinfo pointer */
540Sstevel@tonic-gate int log_msgid; /* log.conf tunable: enable msgid generation */
550Sstevel@tonic-gate
560Sstevel@tonic-gate /* ARGSUSED */
570Sstevel@tonic-gate static int
log_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)580Sstevel@tonic-gate log_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate switch (infocmd) {
610Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
620Sstevel@tonic-gate *result = log_devi;
630Sstevel@tonic-gate return (DDI_SUCCESS);
640Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
650Sstevel@tonic-gate *result = 0;
660Sstevel@tonic-gate return (DDI_SUCCESS);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate return (DDI_FAILURE);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate /* ARGSUSED */
720Sstevel@tonic-gate static int
log_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)730Sstevel@tonic-gate log_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate if (ddi_create_minor_node(devi, "conslog", S_IFCHR,
760Sstevel@tonic-gate LOG_CONSMIN, DDI_PSEUDO, NULL) == DDI_FAILURE ||
770Sstevel@tonic-gate ddi_create_minor_node(devi, "log", S_IFCHR,
780Sstevel@tonic-gate LOG_LOGMIN, DDI_PSEUDO, NULL) == DDI_FAILURE) {
790Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
800Sstevel@tonic-gate return (DDI_FAILURE);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate log_devi = devi;
830Sstevel@tonic-gate log_msgid = ddi_getprop(DDI_DEV_T_ANY, log_devi,
840Sstevel@tonic-gate DDI_PROP_CANSLEEP, "msgid", 1);
850Sstevel@tonic-gate return (DDI_SUCCESS);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
891259Sns92644 * log_open can be called for either /dev/log or dev/conslog.
901259Sns92644 *
911259Sns92644 * In the /dev/conslog case log_alloc() allocates a new minor device from
921259Sns92644 * its cache.
931259Sns92644 *
941259Sns92644 * In the case of /dev/log, LOG_NUMCLONES devices are pre-allocated at zone
951259Sns92644 * creation. log_alloc() finds the zone's next available minor device.
961259Sns92644 *
971259Sns92644 * On entry devp's minor number indicates which device (log or conslog), on
981259Sns92644 * successful return it is the device instance.
990Sstevel@tonic-gate */
1001259Sns92644
1010Sstevel@tonic-gate /* ARGSUSED */
1020Sstevel@tonic-gate static int
log_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * cr)1030Sstevel@tonic-gate log_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *cr)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate log_t *lp;
1060Sstevel@tonic-gate minor_t minor;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate if (sflag & (MODOPEN | CLONEOPEN))
1090Sstevel@tonic-gate return (ENXIO);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate switch (minor = getminor(*devp)) {
1121259Sns92644 case LOG_CONSMIN: /* clone open of /dev/conslog */
1130Sstevel@tonic-gate if (flag & FREAD)
1140Sstevel@tonic-gate return (EINVAL); /* write-only device */
1150Sstevel@tonic-gate if (q->q_ptr)
1160Sstevel@tonic-gate return (0);
1170Sstevel@tonic-gate break;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate case LOG_LOGMIN: /* clone open of /dev/log */
1200Sstevel@tonic-gate break;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate default:
1230Sstevel@tonic-gate return (ENXIO);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate lp = log_alloc(minor);
1270Sstevel@tonic-gate if (lp == NULL)
1280Sstevel@tonic-gate return (ENXIO);
1290Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), lp->log_minor);
1300Sstevel@tonic-gate q->q_ptr = lp;
1310Sstevel@tonic-gate WR(q)->q_ptr = lp;
1320Sstevel@tonic-gate lp->log_inuse = 1;
1330Sstevel@tonic-gate qprocson(q);
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate return (0);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /* ARGSUSED */
1390Sstevel@tonic-gate static int
log_close(queue_t * q,int flag,cred_t * cr)1400Sstevel@tonic-gate log_close(queue_t *q, int flag, cred_t *cr)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate log_t *lp = (log_t *)q->q_ptr;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate qprocsoff(q);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate lp->log_inuse = 0;
1470Sstevel@tonic-gate log_update(lp, NULL, 0, NULL);
1480Sstevel@tonic-gate freemsg(lp->log_data);
1490Sstevel@tonic-gate lp->log_data = NULL;
1501259Sns92644 if (lp->log_major == LOG_CONSMIN)
1511259Sns92644 log_free(lp);
1520Sstevel@tonic-gate q->q_ptr = NULL;
1530Sstevel@tonic-gate WR(q)->q_ptr = NULL;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate return (0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate static int
log_wput(queue_t * q,mblk_t * mp)1590Sstevel@tonic-gate log_wput(queue_t *q, mblk_t *mp)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate log_t *lp = (log_t *)q->q_ptr;
1620Sstevel@tonic-gate struct iocblk *iocp;
1630Sstevel@tonic-gate mblk_t *mp2;
164*8778SErik.Nordmark@Sun.COM cred_t *cr = msg_getcred(mp, NULL);
1650Sstevel@tonic-gate zoneid_t zoneid;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * Default to global zone if dblk doesn't have a valid cred.
1690Sstevel@tonic-gate * Calls to syslog() go through putmsg(), which does set up
1700Sstevel@tonic-gate * the cred.
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate zoneid = (cr != NULL) ? crgetzoneid(cr) : GLOBAL_ZONEID;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate switch (DB_TYPE(mp)) {
1750Sstevel@tonic-gate case M_FLUSH:
1760Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) {
1770Sstevel@tonic-gate flushq(q, FLUSHALL);
1780Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) {
1810Sstevel@tonic-gate flushq(RD(q), FLUSHALL);
1820Sstevel@tonic-gate qreply(q, mp);
1830Sstevel@tonic-gate return (0);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate break;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate case M_IOCTL:
1880Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
1890Sstevel@tonic-gate
1901259Sns92644 if (lp->log_major != LOG_LOGMIN) {
1911259Sns92644 /* write-only device */
1920Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL);
1930Sstevel@tonic-gate return (0);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate if (iocp->ioc_count == TRANSPARENT) {
1970Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL);
1980Sstevel@tonic-gate return (0);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (lp->log_flags) {
2020Sstevel@tonic-gate miocnak(q, mp, 0, EBUSY);
2030Sstevel@tonic-gate return (0);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate freemsg(lp->log_data);
2070Sstevel@tonic-gate lp->log_data = mp->b_cont;
2080Sstevel@tonic-gate mp->b_cont = NULL;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate switch (iocp->ioc_cmd) {
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate case I_CONSLOG:
2130Sstevel@tonic-gate log_update(lp, RD(q), SL_CONSOLE, log_console);
2140Sstevel@tonic-gate break;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate case I_TRCLOG:
2170Sstevel@tonic-gate if (lp->log_data == NULL) {
2180Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL);
2190Sstevel@tonic-gate return (0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate log_update(lp, RD(q), SL_TRACE, log_trace);
2220Sstevel@tonic-gate break;
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate case I_ERRLOG:
2250Sstevel@tonic-gate log_update(lp, RD(q), SL_ERROR, log_error);
2260Sstevel@tonic-gate break;
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate default:
2290Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL);
2300Sstevel@tonic-gate return (0);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate miocack(q, mp, 0, 0);
2330Sstevel@tonic-gate return (0);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate case M_PROTO:
2360Sstevel@tonic-gate if (MBLKL(mp) == sizeof (log_ctl_t) && mp->b_cont != NULL) {
2370Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr;
2380Sstevel@tonic-gate /* This code is used by savecore to log dump msgs */
2390Sstevel@tonic-gate if (mp->b_band != 0 &&
2400Sstevel@tonic-gate secpolicy_sys_config(CRED(), B_FALSE) == 0) {
2410Sstevel@tonic-gate (void) putq(log_consq, mp);
2420Sstevel@tonic-gate return (0);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate if ((lc->pri & LOG_FACMASK) == LOG_KERN)
2450Sstevel@tonic-gate lc->pri |= LOG_USER;
2460Sstevel@tonic-gate mp2 = log_makemsg(LOG_MID, LOG_CONSMIN, lc->level,
2470Sstevel@tonic-gate lc->flags, lc->pri, mp->b_cont->b_rptr,
2480Sstevel@tonic-gate MBLKL(mp->b_cont) + 1, 0);
2490Sstevel@tonic-gate if (mp2 != NULL)
2500Sstevel@tonic-gate log_sendmsg(mp2, zoneid);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate break;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate case M_DATA:
2550Sstevel@tonic-gate mp2 = log_makemsg(LOG_MID, LOG_CONSMIN, 0, SL_CONSOLE,
2560Sstevel@tonic-gate LOG_USER | LOG_INFO, mp->b_rptr, MBLKL(mp) + 1, 0);
2570Sstevel@tonic-gate if (mp2 != NULL)
2580Sstevel@tonic-gate log_sendmsg(mp2, zoneid);
2590Sstevel@tonic-gate break;
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate freemsg(mp);
2630Sstevel@tonic-gate return (0);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate static int
log_rsrv(queue_t * q)2670Sstevel@tonic-gate log_rsrv(queue_t *q)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate mblk_t *mp;
2700Sstevel@tonic-gate char *msg, *msgid_start, *msgid_end;
2710Sstevel@tonic-gate size_t idlen;
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate while (canputnext(q) && (mp = getq(q)) != NULL) {
2740Sstevel@tonic-gate if (log_msgid == 0) {
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate * Strip out the message ID. If it's a kernel
2770Sstevel@tonic-gate * SL_CONSOLE message, replace msgid with "unix: ".
2780Sstevel@tonic-gate */
2790Sstevel@tonic-gate msg = (char *)mp->b_cont->b_rptr;
2800Sstevel@tonic-gate if ((msgid_start = strstr(msg, "[ID ")) != NULL &&
2810Sstevel@tonic-gate (msgid_end = strstr(msgid_start, "] ")) != NULL) {
2820Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr;
2830Sstevel@tonic-gate if ((lc->flags & SL_CONSOLE) &&
2840Sstevel@tonic-gate (lc->pri & LOG_FACMASK) == LOG_KERN)
2850Sstevel@tonic-gate msgid_start = msg + snprintf(msg,
2860Sstevel@tonic-gate 7, "unix: ");
2870Sstevel@tonic-gate idlen = msgid_end + 2 - msgid_start;
2880Sstevel@tonic-gate ovbcopy(msg, msg + idlen, msgid_start - msg);
2890Sstevel@tonic-gate mp->b_cont->b_rptr += idlen;
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate mp->b_band = 0;
2930Sstevel@tonic-gate putnext(q, mp);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate return (0);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate static struct module_info logm_info =
2990Sstevel@tonic-gate { LOG_MID, "LOG", LOG_MINPS, LOG_MAXPS, LOG_HIWAT, LOG_LOWAT };
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate static struct qinit logrinit =
3020Sstevel@tonic-gate { NULL, log_rsrv, log_open, log_close, NULL, &logm_info, NULL };
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate static struct qinit logwinit =
3050Sstevel@tonic-gate { log_wput, NULL, NULL, NULL, NULL, &logm_info, NULL };
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate static struct streamtab loginfo = { &logrinit, &logwinit, NULL, NULL };
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(log_ops, nulldev, nulldev, log_attach, nodev,
3107656SSherry.Moore@Sun.COM nodev, log_info, D_NEW | D_MP | D_MTPERMOD, &loginfo,
3117656SSherry.Moore@Sun.COM ddi_quiesce_not_needed);
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
_init()3190Sstevel@tonic-gate _init()
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate return (mod_install(&modlinkage));
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate int
_fini()3250Sstevel@tonic-gate _fini()
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate return (mod_remove(&modlinkage));
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3310Sstevel@tonic-gate _info(struct modinfo *modinfop)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
3340Sstevel@tonic-gate }
335