subr_log.c revision 116182
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 341541Srgrimes */ 351541Srgrimes 361541Srgrimes/* 371541Srgrimes * Error log buffer for kernel printf's. 381541Srgrimes */ 391541Srgrimes 40116182Sobrien#include <sys/cdefs.h> 41116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_log.c 116182 2003-06-11 00:56:59Z obrien $"); 42116182Sobrien 431541Srgrimes#include <sys/param.h> 441541Srgrimes#include <sys/systm.h> 4512577Sbde#include <sys/conf.h> 461541Srgrimes#include <sys/proc.h> 471541Srgrimes#include <sys/vnode.h> 4824206Sbde#include <sys/filio.h> 4924206Sbde#include <sys/ttycom.h> 501541Srgrimes#include <sys/msgbuf.h> 513308Sphk#include <sys/signalvar.h> 5212517Sjulian#include <sys/kernel.h> 5329357Speter#include <sys/poll.h> 5441086Struckman#include <sys/filedesc.h> 5570069Sjhb#include <sys/sysctl.h> 5612517Sjulian 571541Srgrimes#define LOG_RDPRI (PZERO + 1) 581541Srgrimes 591541Srgrimes#define LOG_ASYNC 0x04 601541Srgrimes#define LOG_RDWAIT 0x08 611541Srgrimes 6212675Sjulianstatic d_open_t logopen; 6312675Sjulianstatic d_close_t logclose; 6412675Sjulianstatic d_read_t logread; 6512675Sjulianstatic d_ioctl_t logioctl; 6629357Speterstatic d_poll_t logpoll; 6712675Sjulian 6870069Sjhbstatic void logtimeout(void *arg); 6970069Sjhb 7012675Sjulian#define CDEV_MAJOR 7 7147625Sphkstatic struct cdevsw log_cdevsw = { 72111815Sphk .d_open = logopen, 73111815Sphk .d_close = logclose, 74111815Sphk .d_read = logread, 75111815Sphk .d_ioctl = logioctl, 76111815Sphk .d_poll = logpoll, 77111815Sphk .d_name = "log", 78111815Sphk .d_maj = CDEV_MAJOR, 7947625Sphk}; 8012675Sjulian 8112819Sphkstatic struct logsoftc { 821541Srgrimes int sc_state; /* see above for possibilities */ 831541Srgrimes struct selinfo sc_selp; /* process waiting on select call */ 8441087Struckman struct sigio *sc_sigio; /* information for async I/O */ 8570069Sjhb struct callout sc_callout; /* callout to wakeup syslog */ 861541Srgrimes} logsoftc; 871541Srgrimes 881541Srgrimesint log_open; /* also used in log() */ 891541Srgrimes 9070069Sjhb/* Times per second to check for a pending syslog wakeup. */ 9170069Sjhbstatic int log_wakeups_per_second = 5; 9270069SjhbSYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 9370069Sjhb &log_wakeups_per_second, 0, ""); 9470069Sjhb 951541Srgrimes/*ARGSUSED*/ 9612675Sjulianstatic int 9783366Sjulianlogopen(dev_t dev, int flags, int mode, struct thread *td) 981541Srgrimes{ 991541Srgrimes if (log_open) 1001541Srgrimes return (EBUSY); 1011541Srgrimes log_open = 1; 10270069Sjhb callout_init(&logsoftc.sc_callout, 0); 10383366Sjulian fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */ 10470069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 10570069Sjhb logtimeout, NULL); 1061541Srgrimes return (0); 1071541Srgrimes} 1081541Srgrimes 1091541Srgrimes/*ARGSUSED*/ 11012675Sjulianstatic int 11183366Sjulianlogclose(dev_t dev, int flag, int mode, struct thread *td) 1121541Srgrimes{ 1131541Srgrimes 11477057Sphk log_open = 0; 11570069Sjhb callout_stop(&logsoftc.sc_callout); 1161541Srgrimes logsoftc.sc_state = 0; 11796122Salfred funsetown(&logsoftc.sc_sigio); 1181541Srgrimes return (0); 1191541Srgrimes} 1201541Srgrimes 1211541Srgrimes/*ARGSUSED*/ 12212675Sjulianstatic int 12369741Sphklogread(dev_t dev, struct uio *uio, int flag) 1241541Srgrimes{ 12569741Sphk struct msgbuf *mbp = msgbufp; 126106917Stmm int error = 0, l, s; 1271541Srgrimes 1281541Srgrimes s = splhigh(); 1291541Srgrimes while (mbp->msg_bufr == mbp->msg_bufx) { 1301541Srgrimes if (flag & IO_NDELAY) { 1311541Srgrimes splx(s); 1321541Srgrimes return (EWOULDBLOCK); 1331541Srgrimes } 1341541Srgrimes logsoftc.sc_state |= LOG_RDWAIT; 13599012Salfred if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) { 1361541Srgrimes splx(s); 1371541Srgrimes return (error); 1381541Srgrimes } 1391541Srgrimes } 1401541Srgrimes splx(s); 1411541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1421541Srgrimes 1431541Srgrimes while (uio->uio_resid > 0) { 144106917Stmm l = mbp->msg_bufx - mbp->msg_bufr; 1451541Srgrimes if (l < 0) 14636179Sphk l = mbp->msg_size - mbp->msg_bufr; 147106917Stmm l = imin(l, uio->uio_resid); 1481541Srgrimes if (l == 0) 1491541Srgrimes break; 150111741Sdes error = uiomove((char *)msgbufp->msg_ptr + mbp->msg_bufr, 151106917Stmm l, uio); 1521541Srgrimes if (error) 1531541Srgrimes break; 1541541Srgrimes mbp->msg_bufr += l; 15536179Sphk if (mbp->msg_bufr >= mbp->msg_size) 1561541Srgrimes mbp->msg_bufr = 0; 1571541Srgrimes } 1581541Srgrimes return (error); 1591541Srgrimes} 1601541Srgrimes 1611541Srgrimes/*ARGSUSED*/ 16212675Sjulianstatic int 16383366Sjulianlogpoll(dev_t dev, int events, struct thread *td) 1641541Srgrimes{ 16529357Speter int s; 16629357Speter int revents = 0; 1671541Srgrimes 16829357Speter s = splhigh(); 1691541Srgrimes 17046568Speter if (events & (POLLIN | POLLRDNORM)) { 17129357Speter if (msgbufp->msg_bufr != msgbufp->msg_bufx) 17229357Speter revents |= events & (POLLIN | POLLRDNORM); 17329357Speter else 17483805Sjhb selrecord(td, &logsoftc.sc_selp); 17546568Speter } 1761541Srgrimes splx(s); 17729357Speter return (revents); 1781541Srgrimes} 1791541Srgrimes 18070069Sjhbstatic void 18170069Sjhblogtimeout(void *arg) 18270069Sjhb{ 18370069Sjhb 18477057Sphk if (!log_open) 18570069Sjhb return; 18677057Sphk if (msgbuftrigger == 0) { 18777057Sphk callout_reset(&logsoftc.sc_callout, 18877057Sphk hz / log_wakeups_per_second, logtimeout, NULL); 18977057Sphk return; 19077057Sphk } 19170239Sphk msgbuftrigger = 0; 1921541Srgrimes selwakeup(&logsoftc.sc_selp); 19341086Struckman if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 19495883Salfred pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 1951541Srgrimes if (logsoftc.sc_state & LOG_RDWAIT) { 196111748Sdes wakeup(msgbufp); 1971541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1981541Srgrimes } 19970069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 20070069Sjhb logtimeout, NULL); 2011541Srgrimes} 2021541Srgrimes 2031541Srgrimes/*ARGSUSED*/ 20412675Sjulianstatic int 20583366Sjulianlogioctl(dev_t dev, u_long com, caddr_t data, int flag, struct thread *td) 2061541Srgrimes{ 207106917Stmm int l, s; 2081541Srgrimes 2091541Srgrimes switch (com) { 2101541Srgrimes 2111541Srgrimes /* return number of characters immediately available */ 2121541Srgrimes case FIONREAD: 2131541Srgrimes s = splhigh(); 214106917Stmm l = msgbufp->msg_bufx - msgbufp->msg_bufr; 2151541Srgrimes splx(s); 2161541Srgrimes if (l < 0) 21736179Sphk l += msgbufp->msg_size; 2181541Srgrimes *(int *)data = l; 2191541Srgrimes break; 2201541Srgrimes 2211541Srgrimes case FIONBIO: 2221541Srgrimes break; 2231541Srgrimes 2241541Srgrimes case FIOASYNC: 2251541Srgrimes if (*(int *)data) 2261541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2271541Srgrimes else 2281541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 2291541Srgrimes break; 2301541Srgrimes 23141086Struckman case FIOSETOWN: 23241086Struckman return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 23341086Struckman 23441086Struckman case FIOGETOWN: 235104393Struckman *(int *)data = fgetown(&logsoftc.sc_sigio); 2361541Srgrimes break; 2371541Srgrimes 23841086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 23941086Struckman case TIOCSPGRP: 24041086Struckman return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 24141086Struckman 24241086Struckman /* This is deprecated, FIOGETOWN should be used instead */ 2431541Srgrimes case TIOCGPGRP: 244104393Struckman *(int *)data = -fgetown(&logsoftc.sc_sigio); 2451541Srgrimes break; 2461541Srgrimes 2471541Srgrimes default: 2488161Sjkh return (ENOTTY); 2491541Srgrimes } 2501541Srgrimes return (0); 2511541Srgrimes} 25212517Sjulian 25312675Sjulianstatic void 25469741Sphklog_drvinit(void *unused) 25512517Sjulian{ 25669741Sphk 25750254Sphk make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 25812517Sjulian} 25912517Sjulian 26012517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 261