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