subr_log.c revision 99012
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 99012 2002-06-29 02:00:02Z alfred $ 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> 5370069Sjhb#include <sys/sysctl.h> 5412517Sjulian 551541Srgrimes#define LOG_RDPRI (PZERO + 1) 561541Srgrimes 571541Srgrimes#define LOG_ASYNC 0x04 581541Srgrimes#define LOG_RDWAIT 0x08 591541Srgrimes 6012675Sjulianstatic d_open_t logopen; 6112675Sjulianstatic d_close_t logclose; 6212675Sjulianstatic d_read_t logread; 6312675Sjulianstatic d_ioctl_t logioctl; 6429357Speterstatic d_poll_t logpoll; 6512675Sjulian 6670069Sjhbstatic void logtimeout(void *arg); 6770069Sjhb 6812675Sjulian#define CDEV_MAJOR 7 6947625Sphkstatic struct cdevsw log_cdevsw = { 7047625Sphk /* open */ logopen, 7147625Sphk /* close */ logclose, 7247625Sphk /* read */ logread, 7347625Sphk /* write */ nowrite, 7447625Sphk /* ioctl */ logioctl, 7547625Sphk /* poll */ logpoll, 7647625Sphk /* mmap */ nommap, 7747625Sphk /* strategy */ nostrategy, 7847625Sphk /* name */ "log", 7947625Sphk /* maj */ CDEV_MAJOR, 8047625Sphk /* dump */ nodump, 8147625Sphk /* psize */ nopsize, 8247625Sphk /* flags */ 0, 8347625Sphk}; 8412675Sjulian 8512819Sphkstatic struct logsoftc { 861541Srgrimes int sc_state; /* see above for possibilities */ 871541Srgrimes struct selinfo sc_selp; /* process waiting on select call */ 8841087Struckman struct sigio *sc_sigio; /* information for async I/O */ 8970069Sjhb struct callout sc_callout; /* callout to wakeup syslog */ 901541Srgrimes} logsoftc; 911541Srgrimes 921541Srgrimesint log_open; /* also used in log() */ 931541Srgrimes 9470069Sjhb/* Times per second to check for a pending syslog wakeup. */ 9570069Sjhbstatic int log_wakeups_per_second = 5; 9670069SjhbSYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 9770069Sjhb &log_wakeups_per_second, 0, ""); 9870069Sjhb 991541Srgrimes/*ARGSUSED*/ 10012675Sjulianstatic int 10183366Sjulianlogopen(dev_t dev, int flags, int mode, struct thread *td) 1021541Srgrimes{ 1031541Srgrimes if (log_open) 1041541Srgrimes return (EBUSY); 1051541Srgrimes log_open = 1; 10670069Sjhb callout_init(&logsoftc.sc_callout, 0); 10783366Sjulian fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */ 10870069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 10970069Sjhb logtimeout, NULL); 1101541Srgrimes return (0); 1111541Srgrimes} 1121541Srgrimes 1131541Srgrimes/*ARGSUSED*/ 11412675Sjulianstatic int 11583366Sjulianlogclose(dev_t dev, int flag, int mode, struct thread *td) 1161541Srgrimes{ 1171541Srgrimes 11877057Sphk log_open = 0; 11970069Sjhb callout_stop(&logsoftc.sc_callout); 1201541Srgrimes logsoftc.sc_state = 0; 12196122Salfred funsetown(&logsoftc.sc_sigio); 1221541Srgrimes return (0); 1231541Srgrimes} 1241541Srgrimes 1251541Srgrimes/*ARGSUSED*/ 12612675Sjulianstatic int 12769741Sphklogread(dev_t dev, struct uio *uio, int flag) 1281541Srgrimes{ 12969741Sphk struct msgbuf *mbp = msgbufp; 13069741Sphk long l; 13169741Sphk int s; 1321541Srgrimes int error = 0; 1331541Srgrimes 1341541Srgrimes s = splhigh(); 1351541Srgrimes while (mbp->msg_bufr == mbp->msg_bufx) { 1361541Srgrimes if (flag & IO_NDELAY) { 1371541Srgrimes splx(s); 1381541Srgrimes return (EWOULDBLOCK); 1391541Srgrimes } 1401541Srgrimes logsoftc.sc_state |= LOG_RDWAIT; 14199012Salfred if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) { 1421541Srgrimes splx(s); 1431541Srgrimes return (error); 1441541Srgrimes } 1451541Srgrimes } 1461541Srgrimes splx(s); 1471541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1481541Srgrimes 1491541Srgrimes while (uio->uio_resid > 0) { 1501541Srgrimes l = mbp->msg_bufx - mbp->msg_bufr; 1511541Srgrimes if (l < 0) 15236179Sphk l = mbp->msg_size - mbp->msg_bufr; 1531541Srgrimes l = min(l, uio->uio_resid); 1541541Srgrimes if (l == 0) 1551541Srgrimes break; 15636179Sphk error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr, 15736179Sphk (int)l, uio); 1581541Srgrimes if (error) 1591541Srgrimes break; 1601541Srgrimes mbp->msg_bufr += l; 16136179Sphk if (mbp->msg_bufr >= mbp->msg_size) 1621541Srgrimes mbp->msg_bufr = 0; 1631541Srgrimes } 1641541Srgrimes return (error); 1651541Srgrimes} 1661541Srgrimes 1671541Srgrimes/*ARGSUSED*/ 16812675Sjulianstatic int 16983366Sjulianlogpoll(dev_t dev, int events, struct thread *td) 1701541Srgrimes{ 17129357Speter int s; 17229357Speter int revents = 0; 1731541Srgrimes 17429357Speter s = splhigh(); 1751541Srgrimes 17646568Speter if (events & (POLLIN | POLLRDNORM)) { 17729357Speter if (msgbufp->msg_bufr != msgbufp->msg_bufx) 17829357Speter revents |= events & (POLLIN | POLLRDNORM); 17929357Speter else 18083805Sjhb selrecord(td, &logsoftc.sc_selp); 18146568Speter } 1821541Srgrimes splx(s); 18329357Speter return (revents); 1841541Srgrimes} 1851541Srgrimes 18670069Sjhbstatic void 18770069Sjhblogtimeout(void *arg) 18870069Sjhb{ 18970069Sjhb 19077057Sphk if (!log_open) 19170069Sjhb return; 19277057Sphk if (msgbuftrigger == 0) { 19377057Sphk callout_reset(&logsoftc.sc_callout, 19477057Sphk hz / log_wakeups_per_second, logtimeout, NULL); 19577057Sphk return; 19677057Sphk } 19770239Sphk msgbuftrigger = 0; 1981541Srgrimes selwakeup(&logsoftc.sc_selp); 19941086Struckman if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 20095883Salfred pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 2011541Srgrimes if (logsoftc.sc_state & LOG_RDWAIT) { 2021541Srgrimes wakeup((caddr_t)msgbufp); 2031541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 2041541Srgrimes } 20570069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 20670069Sjhb logtimeout, NULL); 2071541Srgrimes} 2081541Srgrimes 2091541Srgrimes/*ARGSUSED*/ 21012675Sjulianstatic int 21183366Sjulianlogioctl(dev_t dev, u_long com, caddr_t data, int flag, struct thread *td) 2121541Srgrimes{ 2131541Srgrimes long l; 2141541Srgrimes int s; 2151541Srgrimes 2161541Srgrimes switch (com) { 2171541Srgrimes 2181541Srgrimes /* return number of characters immediately available */ 2191541Srgrimes case FIONREAD: 2201541Srgrimes s = splhigh(); 2211541Srgrimes l = msgbufp->msg_bufx - msgbufp->msg_bufr; 2221541Srgrimes splx(s); 2231541Srgrimes if (l < 0) 22436179Sphk l += msgbufp->msg_size; 2251541Srgrimes *(int *)data = l; 2261541Srgrimes break; 2271541Srgrimes 2281541Srgrimes case FIONBIO: 2291541Srgrimes break; 2301541Srgrimes 2311541Srgrimes case FIOASYNC: 2321541Srgrimes if (*(int *)data) 2331541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2341541Srgrimes else 2351541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 2361541Srgrimes break; 2371541Srgrimes 23841086Struckman case FIOSETOWN: 23941086Struckman return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 24041086Struckman 24141086Struckman case FIOGETOWN: 24241086Struckman *(int *)data = fgetown(logsoftc.sc_sigio); 2431541Srgrimes break; 2441541Srgrimes 24541086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 24641086Struckman case TIOCSPGRP: 24741086Struckman return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 24841086Struckman 24941086Struckman /* This is deprecated, FIOGETOWN should be used instead */ 2501541Srgrimes case TIOCGPGRP: 25141086Struckman *(int *)data = -fgetown(logsoftc.sc_sigio); 2521541Srgrimes break; 2531541Srgrimes 2541541Srgrimes default: 2558161Sjkh return (ENOTTY); 2561541Srgrimes } 2571541Srgrimes return (0); 2581541Srgrimes} 25912517Sjulian 26012675Sjulianstatic void 26169741Sphklog_drvinit(void *unused) 26212517Sjulian{ 26369741Sphk 26450254Sphk make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 26512517Sjulian} 26612517Sjulian 26712517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 268