17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 519397407SSherry Moore * Common Development and Distribution License (the "License"). 619397407SSherry Moore * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*de8c4a14SErik Nordmark * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * Streams log driver. See log(7D). 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/param.h> 337c478bd9Sstevel@tonic-gate #include <sys/errno.h> 347c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 35*de8c4a14SErik Nordmark #include <sys/strsubr.h> 367c478bd9Sstevel@tonic-gate #include <sys/stream.h> 377c478bd9Sstevel@tonic-gate #include <sys/strsun.h> 387c478bd9Sstevel@tonic-gate #include <sys/debug.h> 397c478bd9Sstevel@tonic-gate #include <sys/cred.h> 407c478bd9Sstevel@tonic-gate #include <sys/file.h> 417c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 427c478bd9Sstevel@tonic-gate #include <sys/stat.h> 437c478bd9Sstevel@tonic-gate #include <sys/syslog.h> 447c478bd9Sstevel@tonic-gate #include <sys/log.h> 457c478bd9Sstevel@tonic-gate #include <sys/systm.h> 467c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 477c478bd9Sstevel@tonic-gate #include <sys/policy.h> 487c478bd9Sstevel@tonic-gate #include <sys/zone.h> 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #include <sys/conf.h> 517c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate static dev_info_t *log_devi; /* private copy of devinfo pointer */ 547c478bd9Sstevel@tonic-gate int log_msgid; /* log.conf tunable: enable msgid generation */ 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* ARGSUSED */ 577c478bd9Sstevel@tonic-gate static int 587c478bd9Sstevel@tonic-gate log_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate switch (infocmd) { 617c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 627c478bd9Sstevel@tonic-gate *result = log_devi; 637c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 647c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 657c478bd9Sstevel@tonic-gate *result = 0; 667c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 697c478bd9Sstevel@tonic-gate } 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 727c478bd9Sstevel@tonic-gate static int 737c478bd9Sstevel@tonic-gate log_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "conslog", S_IFCHR, 767c478bd9Sstevel@tonic-gate LOG_CONSMIN, DDI_PSEUDO, NULL) == DDI_FAILURE || 777c478bd9Sstevel@tonic-gate ddi_create_minor_node(devi, "log", S_IFCHR, 787c478bd9Sstevel@tonic-gate LOG_LOGMIN, DDI_PSEUDO, NULL) == DDI_FAILURE) { 797c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 807c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate log_devi = devi; 837c478bd9Sstevel@tonic-gate log_msgid = ddi_getprop(DDI_DEV_T_ANY, log_devi, 847c478bd9Sstevel@tonic-gate DDI_PROP_CANSLEEP, "msgid", 1); 857c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 89068ccd7aSns92644 * log_open can be called for either /dev/log or dev/conslog. 90068ccd7aSns92644 * 91068ccd7aSns92644 * In the /dev/conslog case log_alloc() allocates a new minor device from 92068ccd7aSns92644 * its cache. 93068ccd7aSns92644 * 94068ccd7aSns92644 * In the case of /dev/log, LOG_NUMCLONES devices are pre-allocated at zone 95068ccd7aSns92644 * creation. log_alloc() finds the zone's next available minor device. 96068ccd7aSns92644 * 97068ccd7aSns92644 * On entry devp's minor number indicates which device (log or conslog), on 98068ccd7aSns92644 * successful return it is the device instance. 997c478bd9Sstevel@tonic-gate */ 100068ccd7aSns92644 1017c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1027c478bd9Sstevel@tonic-gate static int 1037c478bd9Sstevel@tonic-gate log_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *cr) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate log_t *lp; 1067c478bd9Sstevel@tonic-gate minor_t minor; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate if (sflag & (MODOPEN | CLONEOPEN)) 1097c478bd9Sstevel@tonic-gate return (ENXIO); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate switch (minor = getminor(*devp)) { 112068ccd7aSns92644 case LOG_CONSMIN: /* clone open of /dev/conslog */ 1137c478bd9Sstevel@tonic-gate if (flag & FREAD) 1147c478bd9Sstevel@tonic-gate return (EINVAL); /* write-only device */ 1157c478bd9Sstevel@tonic-gate if (q->q_ptr) 1167c478bd9Sstevel@tonic-gate return (0); 1177c478bd9Sstevel@tonic-gate break; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate case LOG_LOGMIN: /* clone open of /dev/log */ 1207c478bd9Sstevel@tonic-gate break; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate default: 1237c478bd9Sstevel@tonic-gate return (ENXIO); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate lp = log_alloc(minor); 1277c478bd9Sstevel@tonic-gate if (lp == NULL) 1287c478bd9Sstevel@tonic-gate return (ENXIO); 1297c478bd9Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), lp->log_minor); 1307c478bd9Sstevel@tonic-gate q->q_ptr = lp; 1317c478bd9Sstevel@tonic-gate WR(q)->q_ptr = lp; 1327c478bd9Sstevel@tonic-gate lp->log_inuse = 1; 1337c478bd9Sstevel@tonic-gate qprocson(q); 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate return (0); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1397c478bd9Sstevel@tonic-gate static int 1407c478bd9Sstevel@tonic-gate log_close(queue_t *q, int flag, cred_t *cr) 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate log_t *lp = (log_t *)q->q_ptr; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate qprocsoff(q); 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate lp->log_inuse = 0; 1477c478bd9Sstevel@tonic-gate log_update(lp, NULL, 0, NULL); 1487c478bd9Sstevel@tonic-gate freemsg(lp->log_data); 1497c478bd9Sstevel@tonic-gate lp->log_data = NULL; 150068ccd7aSns92644 if (lp->log_major == LOG_CONSMIN) 151068ccd7aSns92644 log_free(lp); 1527c478bd9Sstevel@tonic-gate q->q_ptr = NULL; 1537c478bd9Sstevel@tonic-gate WR(q)->q_ptr = NULL; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate return (0); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate static int 1597c478bd9Sstevel@tonic-gate log_wput(queue_t *q, mblk_t *mp) 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate log_t *lp = (log_t *)q->q_ptr; 1627c478bd9Sstevel@tonic-gate struct iocblk *iocp; 1637c478bd9Sstevel@tonic-gate mblk_t *mp2; 164*de8c4a14SErik Nordmark cred_t *cr = msg_getcred(mp, NULL); 1657c478bd9Sstevel@tonic-gate zoneid_t zoneid; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * Default to global zone if dblk doesn't have a valid cred. 1697c478bd9Sstevel@tonic-gate * Calls to syslog() go through putmsg(), which does set up 1707c478bd9Sstevel@tonic-gate * the cred. 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate zoneid = (cr != NULL) ? crgetzoneid(cr) : GLOBAL_ZONEID; 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate switch (DB_TYPE(mp)) { 1757c478bd9Sstevel@tonic-gate case M_FLUSH: 1767c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 1777c478bd9Sstevel@tonic-gate flushq(q, FLUSHALL); 1787c478bd9Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 1817c478bd9Sstevel@tonic-gate flushq(RD(q), FLUSHALL); 1827c478bd9Sstevel@tonic-gate qreply(q, mp); 1837c478bd9Sstevel@tonic-gate return (0); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate case M_IOCTL: 1887c478bd9Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 1897c478bd9Sstevel@tonic-gate 190068ccd7aSns92644 if (lp->log_major != LOG_LOGMIN) { 191068ccd7aSns92644 /* write-only device */ 1927c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 1937c478bd9Sstevel@tonic-gate return (0); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate if (iocp->ioc_count == TRANSPARENT) { 1977c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 1987c478bd9Sstevel@tonic-gate return (0); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate if (lp->log_flags) { 2027c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EBUSY); 2037c478bd9Sstevel@tonic-gate return (0); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate freemsg(lp->log_data); 2077c478bd9Sstevel@tonic-gate lp->log_data = mp->b_cont; 2087c478bd9Sstevel@tonic-gate mp->b_cont = NULL; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) { 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate case I_CONSLOG: 2137c478bd9Sstevel@tonic-gate log_update(lp, RD(q), SL_CONSOLE, log_console); 2147c478bd9Sstevel@tonic-gate break; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate case I_TRCLOG: 2177c478bd9Sstevel@tonic-gate if (lp->log_data == NULL) { 2187c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 2197c478bd9Sstevel@tonic-gate return (0); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate log_update(lp, RD(q), SL_TRACE, log_trace); 2227c478bd9Sstevel@tonic-gate break; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate case I_ERRLOG: 2257c478bd9Sstevel@tonic-gate log_update(lp, RD(q), SL_ERROR, log_error); 2267c478bd9Sstevel@tonic-gate break; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate default: 2297c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL); 2307c478bd9Sstevel@tonic-gate return (0); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate miocack(q, mp, 0, 0); 2337c478bd9Sstevel@tonic-gate return (0); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate case M_PROTO: 2367c478bd9Sstevel@tonic-gate if (MBLKL(mp) == sizeof (log_ctl_t) && mp->b_cont != NULL) { 2377c478bd9Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr; 2387c478bd9Sstevel@tonic-gate /* This code is used by savecore to log dump msgs */ 2397c478bd9Sstevel@tonic-gate if (mp->b_band != 0 && 2407c478bd9Sstevel@tonic-gate secpolicy_sys_config(CRED(), B_FALSE) == 0) { 2417c478bd9Sstevel@tonic-gate (void) putq(log_consq, mp); 2427c478bd9Sstevel@tonic-gate return (0); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate if ((lc->pri & LOG_FACMASK) == LOG_KERN) 2457c478bd9Sstevel@tonic-gate lc->pri |= LOG_USER; 2467c478bd9Sstevel@tonic-gate mp2 = log_makemsg(LOG_MID, LOG_CONSMIN, lc->level, 2477c478bd9Sstevel@tonic-gate lc->flags, lc->pri, mp->b_cont->b_rptr, 2487c478bd9Sstevel@tonic-gate MBLKL(mp->b_cont) + 1, 0); 2497c478bd9Sstevel@tonic-gate if (mp2 != NULL) 2507c478bd9Sstevel@tonic-gate log_sendmsg(mp2, zoneid); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate break; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate case M_DATA: 2557c478bd9Sstevel@tonic-gate mp2 = log_makemsg(LOG_MID, LOG_CONSMIN, 0, SL_CONSOLE, 2567c478bd9Sstevel@tonic-gate LOG_USER | LOG_INFO, mp->b_rptr, MBLKL(mp) + 1, 0); 2577c478bd9Sstevel@tonic-gate if (mp2 != NULL) 2587c478bd9Sstevel@tonic-gate log_sendmsg(mp2, zoneid); 2597c478bd9Sstevel@tonic-gate break; 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate freemsg(mp); 2637c478bd9Sstevel@tonic-gate return (0); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate static int 2677c478bd9Sstevel@tonic-gate log_rsrv(queue_t *q) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate mblk_t *mp; 2707c478bd9Sstevel@tonic-gate char *msg, *msgid_start, *msgid_end; 2717c478bd9Sstevel@tonic-gate size_t idlen; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate while (canputnext(q) && (mp = getq(q)) != NULL) { 2747c478bd9Sstevel@tonic-gate if (log_msgid == 0) { 2757c478bd9Sstevel@tonic-gate /* 2767c478bd9Sstevel@tonic-gate * Strip out the message ID. If it's a kernel 2777c478bd9Sstevel@tonic-gate * SL_CONSOLE message, replace msgid with "unix: ". 2787c478bd9Sstevel@tonic-gate */ 2797c478bd9Sstevel@tonic-gate msg = (char *)mp->b_cont->b_rptr; 2807c478bd9Sstevel@tonic-gate if ((msgid_start = strstr(msg, "[ID ")) != NULL && 2817c478bd9Sstevel@tonic-gate (msgid_end = strstr(msgid_start, "] ")) != NULL) { 2827c478bd9Sstevel@tonic-gate log_ctl_t *lc = (log_ctl_t *)mp->b_rptr; 2837c478bd9Sstevel@tonic-gate if ((lc->flags & SL_CONSOLE) && 2847c478bd9Sstevel@tonic-gate (lc->pri & LOG_FACMASK) == LOG_KERN) 2857c478bd9Sstevel@tonic-gate msgid_start = msg + snprintf(msg, 2867c478bd9Sstevel@tonic-gate 7, "unix: "); 2877c478bd9Sstevel@tonic-gate idlen = msgid_end + 2 - msgid_start; 2887c478bd9Sstevel@tonic-gate ovbcopy(msg, msg + idlen, msgid_start - msg); 2897c478bd9Sstevel@tonic-gate mp->b_cont->b_rptr += idlen; 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate mp->b_band = 0; 2937c478bd9Sstevel@tonic-gate putnext(q, mp); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate return (0); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate static struct module_info logm_info = 2997c478bd9Sstevel@tonic-gate { LOG_MID, "LOG", LOG_MINPS, LOG_MAXPS, LOG_HIWAT, LOG_LOWAT }; 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate static struct qinit logrinit = 3027c478bd9Sstevel@tonic-gate { NULL, log_rsrv, log_open, log_close, NULL, &logm_info, NULL }; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate static struct qinit logwinit = 3057c478bd9Sstevel@tonic-gate { log_wput, NULL, NULL, NULL, NULL, &logm_info, NULL }; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate static struct streamtab loginfo = { &logrinit, &logwinit, NULL, NULL }; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(log_ops, nulldev, nulldev, log_attach, nodev, 31019397407SSherry Moore nodev, log_info, D_NEW | D_MP | D_MTPERMOD, &loginfo, 31119397407SSherry Moore ddi_quiesce_not_needed); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate static struct modldrv modldrv = 3147c478bd9Sstevel@tonic-gate { &mod_driverops, "streams log driver", &log_ops }; 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { MODREV_1, (void *)&modldrv, NULL }; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate int 3197c478bd9Sstevel@tonic-gate _init() 3207c478bd9Sstevel@tonic-gate { 3217c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate int 3257c478bd9Sstevel@tonic-gate _fini() 3267c478bd9Sstevel@tonic-gate { 3277c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate int 3317c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3347c478bd9Sstevel@tonic-gate } 335