subr_log.c revision 106917
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 106917 2002-11-14 16:11:12Z tmm $ 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; 130106917Stmm int error = 0, l, s; 1311541Srgrimes 1321541Srgrimes s = splhigh(); 1331541Srgrimes while (mbp->msg_bufr == mbp->msg_bufx) { 1341541Srgrimes if (flag & IO_NDELAY) { 1351541Srgrimes splx(s); 1361541Srgrimes return (EWOULDBLOCK); 1371541Srgrimes } 1381541Srgrimes logsoftc.sc_state |= LOG_RDWAIT; 13999012Salfred if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) { 1401541Srgrimes splx(s); 1411541Srgrimes return (error); 1421541Srgrimes } 1431541Srgrimes } 1441541Srgrimes splx(s); 1451541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1461541Srgrimes 1471541Srgrimes while (uio->uio_resid > 0) { 148106917Stmm l = mbp->msg_bufx - mbp->msg_bufr; 1491541Srgrimes if (l < 0) 15036179Sphk l = mbp->msg_size - mbp->msg_bufr; 151106917Stmm l = imin(l, uio->uio_resid); 1521541Srgrimes if (l == 0) 1531541Srgrimes break; 15436179Sphk error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr, 155106917Stmm l, uio); 1561541Srgrimes if (error) 1571541Srgrimes break; 1581541Srgrimes mbp->msg_bufr += l; 15936179Sphk if (mbp->msg_bufr >= mbp->msg_size) 1601541Srgrimes mbp->msg_bufr = 0; 1611541Srgrimes } 1621541Srgrimes return (error); 1631541Srgrimes} 1641541Srgrimes 1651541Srgrimes/*ARGSUSED*/ 16612675Sjulianstatic int 16783366Sjulianlogpoll(dev_t dev, int events, struct thread *td) 1681541Srgrimes{ 16929357Speter int s; 17029357Speter int revents = 0; 1711541Srgrimes 17229357Speter s = splhigh(); 1731541Srgrimes 17446568Speter if (events & (POLLIN | POLLRDNORM)) { 17529357Speter if (msgbufp->msg_bufr != msgbufp->msg_bufx) 17629357Speter revents |= events & (POLLIN | POLLRDNORM); 17729357Speter else 17883805Sjhb selrecord(td, &logsoftc.sc_selp); 17946568Speter } 1801541Srgrimes splx(s); 18129357Speter return (revents); 1821541Srgrimes} 1831541Srgrimes 18470069Sjhbstatic void 18570069Sjhblogtimeout(void *arg) 18670069Sjhb{ 18770069Sjhb 18877057Sphk if (!log_open) 18970069Sjhb return; 19077057Sphk if (msgbuftrigger == 0) { 19177057Sphk callout_reset(&logsoftc.sc_callout, 19277057Sphk hz / log_wakeups_per_second, logtimeout, NULL); 19377057Sphk return; 19477057Sphk } 19570239Sphk msgbuftrigger = 0; 1961541Srgrimes selwakeup(&logsoftc.sc_selp); 19741086Struckman if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 19895883Salfred pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 1991541Srgrimes if (logsoftc.sc_state & LOG_RDWAIT) { 2001541Srgrimes wakeup((caddr_t)msgbufp); 2011541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 2021541Srgrimes } 20370069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 20470069Sjhb logtimeout, NULL); 2051541Srgrimes} 2061541Srgrimes 2071541Srgrimes/*ARGSUSED*/ 20812675Sjulianstatic int 20983366Sjulianlogioctl(dev_t dev, u_long com, caddr_t data, int flag, struct thread *td) 2101541Srgrimes{ 211106917Stmm int l, s; 2121541Srgrimes 2131541Srgrimes switch (com) { 2141541Srgrimes 2151541Srgrimes /* return number of characters immediately available */ 2161541Srgrimes case FIONREAD: 2171541Srgrimes s = splhigh(); 218106917Stmm l = msgbufp->msg_bufx - msgbufp->msg_bufr; 2191541Srgrimes splx(s); 2201541Srgrimes if (l < 0) 22136179Sphk l += msgbufp->msg_size; 2221541Srgrimes *(int *)data = l; 2231541Srgrimes break; 2241541Srgrimes 2251541Srgrimes case FIONBIO: 2261541Srgrimes break; 2271541Srgrimes 2281541Srgrimes case FIOASYNC: 2291541Srgrimes if (*(int *)data) 2301541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2311541Srgrimes else 2321541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 2331541Srgrimes break; 2341541Srgrimes 23541086Struckman case FIOSETOWN: 23641086Struckman return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 23741086Struckman 23841086Struckman case FIOGETOWN: 239104393Struckman *(int *)data = fgetown(&logsoftc.sc_sigio); 2401541Srgrimes break; 2411541Srgrimes 24241086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 24341086Struckman case TIOCSPGRP: 24441086Struckman return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 24541086Struckman 24641086Struckman /* This is deprecated, FIOGETOWN should be used instead */ 2471541Srgrimes case TIOCGPGRP: 248104393Struckman *(int *)data = -fgetown(&logsoftc.sc_sigio); 2491541Srgrimes break; 2501541Srgrimes 2511541Srgrimes default: 2528161Sjkh return (ENOTTY); 2531541Srgrimes } 2541541Srgrimes return (0); 2551541Srgrimes} 25612517Sjulian 25712675Sjulianstatic void 25869741Sphklog_drvinit(void *unused) 25912517Sjulian{ 26069741Sphk 26150254Sphk make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 26212517Sjulian} 26312517Sjulian 26412517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 265