subr_log.c revision 69741
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 3450477Speter * $FreeBSD: head/sys/kern/subr_log.c 69741 2000-12-08 06:57:39Z phk $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Error log buffer for kernel printf's. 391541Srgrimes */ 401541Srgrimes 411541Srgrimes#include <sys/param.h> 421541Srgrimes#include <sys/systm.h> 4312577Sbde#include <sys/conf.h> 441541Srgrimes#include <sys/proc.h> 451541Srgrimes#include <sys/vnode.h> 4624206Sbde#include <sys/filio.h> 4724206Sbde#include <sys/ttycom.h> 481541Srgrimes#include <sys/msgbuf.h> 493308Sphk#include <sys/signalvar.h> 5012517Sjulian#include <sys/kernel.h> 5129357Speter#include <sys/poll.h> 5241086Struckman#include <sys/filedesc.h> 5312517Sjulian 541541Srgrimes#define LOG_RDPRI (PZERO + 1) 551541Srgrimes 561541Srgrimes#define LOG_ASYNC 0x04 571541Srgrimes#define LOG_RDWAIT 0x08 581541Srgrimes 5912675Sjulianstatic d_open_t logopen; 6012675Sjulianstatic d_close_t logclose; 6112675Sjulianstatic d_read_t logread; 6212675Sjulianstatic d_ioctl_t logioctl; 6329357Speterstatic d_poll_t logpoll; 6412675Sjulian 6512675Sjulian#define CDEV_MAJOR 7 6647625Sphkstatic struct cdevsw log_cdevsw = { 6747625Sphk /* open */ logopen, 6847625Sphk /* close */ logclose, 6947625Sphk /* read */ logread, 7047625Sphk /* write */ nowrite, 7147625Sphk /* ioctl */ logioctl, 7247625Sphk /* poll */ logpoll, 7347625Sphk /* mmap */ nommap, 7447625Sphk /* strategy */ nostrategy, 7547625Sphk /* name */ "log", 7647625Sphk /* maj */ CDEV_MAJOR, 7747625Sphk /* dump */ nodump, 7847625Sphk /* psize */ nopsize, 7947625Sphk /* flags */ 0, 8047625Sphk /* bmaj */ -1 8147625Sphk}; 8212675Sjulian 8312819Sphkstatic struct logsoftc { 841541Srgrimes int sc_state; /* see above for possibilities */ 851541Srgrimes struct selinfo sc_selp; /* process waiting on select call */ 8641087Struckman struct sigio *sc_sigio; /* information for async I/O */ 871541Srgrimes} logsoftc; 881541Srgrimes 891541Srgrimesint log_open; /* also used in log() */ 901541Srgrimes 911541Srgrimes/*ARGSUSED*/ 9212675Sjulianstatic int 9369741Sphklogopen(dev_t dev, int flags, int mode, struct proc *p) 941541Srgrimes{ 951541Srgrimes if (log_open) 961541Srgrimes return (EBUSY); 971541Srgrimes log_open = 1; 9841086Struckman fsetown(p->p_pid, &logsoftc.sc_sigio); /* signal process only */ 991541Srgrimes return (0); 1001541Srgrimes} 1011541Srgrimes 1021541Srgrimes/*ARGSUSED*/ 10312675Sjulianstatic int 10469741Sphklogclose(dev_t dev, int flag, int mode, struct proc *p) 1051541Srgrimes{ 1061541Srgrimes 1071541Srgrimes log_open = 0; 1081541Srgrimes logsoftc.sc_state = 0; 10941086Struckman funsetown(logsoftc.sc_sigio); 1101541Srgrimes return (0); 1111541Srgrimes} 1121541Srgrimes 1131541Srgrimes/*ARGSUSED*/ 11412675Sjulianstatic int 11569741Sphklogread(dev_t dev, struct uio *uio, int flag) 1161541Srgrimes{ 11769741Sphk struct msgbuf *mbp = msgbufp; 11869741Sphk long l; 11969741Sphk int s; 1201541Srgrimes int error = 0; 1211541Srgrimes 1221541Srgrimes s = splhigh(); 1231541Srgrimes while (mbp->msg_bufr == mbp->msg_bufx) { 1241541Srgrimes if (flag & IO_NDELAY) { 1251541Srgrimes splx(s); 1261541Srgrimes return (EWOULDBLOCK); 1271541Srgrimes } 1281541Srgrimes logsoftc.sc_state |= LOG_RDWAIT; 1293098Sphk if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 1303098Sphk "klog", 0))) { 1311541Srgrimes splx(s); 1321541Srgrimes return (error); 1331541Srgrimes } 1341541Srgrimes } 1351541Srgrimes splx(s); 1361541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1371541Srgrimes 1381541Srgrimes while (uio->uio_resid > 0) { 1391541Srgrimes l = mbp->msg_bufx - mbp->msg_bufr; 1401541Srgrimes if (l < 0) 14136179Sphk l = mbp->msg_size - mbp->msg_bufr; 1421541Srgrimes l = min(l, uio->uio_resid); 1431541Srgrimes if (l == 0) 1441541Srgrimes break; 14536179Sphk error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr, 14636179Sphk (int)l, uio); 1471541Srgrimes if (error) 1481541Srgrimes break; 1491541Srgrimes mbp->msg_bufr += l; 15036179Sphk if (mbp->msg_bufr >= mbp->msg_size) 1511541Srgrimes mbp->msg_bufr = 0; 1521541Srgrimes } 1531541Srgrimes return (error); 1541541Srgrimes} 1551541Srgrimes 1561541Srgrimes/*ARGSUSED*/ 15712675Sjulianstatic int 15869741Sphklogpoll(dev_t dev, int events, struct proc *p) 1591541Srgrimes{ 16029357Speter int s; 16129357Speter int revents = 0; 1621541Srgrimes 16329357Speter s = splhigh(); 1641541Srgrimes 16546568Speter if (events & (POLLIN | POLLRDNORM)) { 16629357Speter if (msgbufp->msg_bufr != msgbufp->msg_bufx) 16729357Speter revents |= events & (POLLIN | POLLRDNORM); 16829357Speter else 16929357Speter selrecord(p, &logsoftc.sc_selp); 17046568Speter } 1711541Srgrimes splx(s); 17229357Speter return (revents); 1731541Srgrimes} 1741541Srgrimes 1751549Srgrimesvoid 17669741Sphklogwakeup(void) 1771541Srgrimes{ 1781541Srgrimes if (!log_open) 1791541Srgrimes return; 1801541Srgrimes selwakeup(&logsoftc.sc_selp); 18141086Struckman if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 18241086Struckman pgsigio(logsoftc.sc_sigio, SIGIO, 0); 1831541Srgrimes if (logsoftc.sc_state & LOG_RDWAIT) { 1841541Srgrimes wakeup((caddr_t)msgbufp); 1851541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1861541Srgrimes } 1871541Srgrimes} 1881541Srgrimes 1891541Srgrimes/*ARGSUSED*/ 19012675Sjulianstatic int 19169741Sphklogioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 1921541Srgrimes{ 1931541Srgrimes long l; 1941541Srgrimes int s; 1951541Srgrimes 1961541Srgrimes switch (com) { 1971541Srgrimes 1981541Srgrimes /* return number of characters immediately available */ 1991541Srgrimes case FIONREAD: 2001541Srgrimes s = splhigh(); 2011541Srgrimes l = msgbufp->msg_bufx - msgbufp->msg_bufr; 2021541Srgrimes splx(s); 2031541Srgrimes if (l < 0) 20436179Sphk l += msgbufp->msg_size; 2051541Srgrimes *(int *)data = l; 2061541Srgrimes break; 2071541Srgrimes 2081541Srgrimes case FIONBIO: 2091541Srgrimes break; 2101541Srgrimes 2111541Srgrimes case FIOASYNC: 2121541Srgrimes if (*(int *)data) 2131541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2141541Srgrimes else 2151541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 2161541Srgrimes break; 2171541Srgrimes 21841086Struckman case FIOSETOWN: 21941086Struckman return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 22041086Struckman 22141086Struckman case FIOGETOWN: 22241086Struckman *(int *)data = fgetown(logsoftc.sc_sigio); 2231541Srgrimes break; 2241541Srgrimes 22541086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 22641086Struckman case TIOCSPGRP: 22741086Struckman return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 22841086Struckman 22941086Struckman /* This is deprecated, FIOGETOWN should be used instead */ 2301541Srgrimes case TIOCGPGRP: 23141086Struckman *(int *)data = -fgetown(logsoftc.sc_sigio); 2321541Srgrimes break; 2331541Srgrimes 2341541Srgrimes default: 2358161Sjkh return (ENOTTY); 2361541Srgrimes } 2371541Srgrimes return (0); 2381541Srgrimes} 23912517Sjulian 24012675Sjulianstatic void 24169741Sphklog_drvinit(void *unused) 24212517Sjulian{ 24369741Sphk 24450254Sphk make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 24512517Sjulian} 24612517Sjulian 24712517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 248