subr_log.c revision 139804
1139804Simp/*- 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 * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 291541Srgrimes * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 301541Srgrimes */ 311541Srgrimes 321541Srgrimes/* 331541Srgrimes * Error log buffer for kernel printf's. 341541Srgrimes */ 351541Srgrimes 36116182Sobrien#include <sys/cdefs.h> 37116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_log.c 139804 2005-01-06 23:35:40Z imp $"); 38116182Sobrien 391541Srgrimes#include <sys/param.h> 401541Srgrimes#include <sys/systm.h> 4112577Sbde#include <sys/conf.h> 421541Srgrimes#include <sys/proc.h> 431541Srgrimes#include <sys/vnode.h> 4424206Sbde#include <sys/filio.h> 4524206Sbde#include <sys/ttycom.h> 461541Srgrimes#include <sys/msgbuf.h> 473308Sphk#include <sys/signalvar.h> 4812517Sjulian#include <sys/kernel.h> 4929357Speter#include <sys/poll.h> 5041086Struckman#include <sys/filedesc.h> 5170069Sjhb#include <sys/sysctl.h> 5212517Sjulian 531541Srgrimes#define LOG_RDPRI (PZERO + 1) 541541Srgrimes 551541Srgrimes#define LOG_ASYNC 0x04 561541Srgrimes#define LOG_RDWAIT 0x08 571541Srgrimes 5812675Sjulianstatic d_open_t logopen; 5912675Sjulianstatic d_close_t logclose; 6012675Sjulianstatic d_read_t logread; 6112675Sjulianstatic d_ioctl_t logioctl; 6229357Speterstatic d_poll_t logpoll; 6312675Sjulian 6470069Sjhbstatic void logtimeout(void *arg); 6570069Sjhb 6612675Sjulian#define CDEV_MAJOR 7 6747625Sphkstatic struct cdevsw log_cdevsw = { 68126080Sphk .d_version = D_VERSION, 69126080Sphk .d_flags = D_NEEDGIANT, 70111815Sphk .d_open = logopen, 71111815Sphk .d_close = logclose, 72111815Sphk .d_read = logread, 73111815Sphk .d_ioctl = logioctl, 74111815Sphk .d_poll = logpoll, 75111815Sphk .d_name = "log", 76111815Sphk .d_maj = CDEV_MAJOR, 7747625Sphk}; 7812675Sjulian 7912819Sphkstatic struct logsoftc { 801541Srgrimes int sc_state; /* see above for possibilities */ 811541Srgrimes struct selinfo sc_selp; /* process waiting on select call */ 8241087Struckman struct sigio *sc_sigio; /* information for async I/O */ 8370069Sjhb struct callout sc_callout; /* callout to wakeup syslog */ 841541Srgrimes} logsoftc; 851541Srgrimes 861541Srgrimesint log_open; /* also used in log() */ 871541Srgrimes 8870069Sjhb/* Times per second to check for a pending syslog wakeup. */ 8970069Sjhbstatic int log_wakeups_per_second = 5; 9070069SjhbSYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 9170069Sjhb &log_wakeups_per_second, 0, ""); 9270069Sjhb 931541Srgrimes/*ARGSUSED*/ 9412675Sjulianstatic int 95130585Sphklogopen(struct cdev *dev, int flags, int mode, struct thread *td) 961541Srgrimes{ 971541Srgrimes if (log_open) 981541Srgrimes return (EBUSY); 991541Srgrimes log_open = 1; 10070069Sjhb callout_init(&logsoftc.sc_callout, 0); 10183366Sjulian fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */ 102116634Sbmilekic if (log_wakeups_per_second < 1) { 103116634Sbmilekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 104116634Sbmilekic log_wakeups_per_second = 1; 105116634Sbmilekic } 10670069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 10770069Sjhb logtimeout, NULL); 1081541Srgrimes return (0); 1091541Srgrimes} 1101541Srgrimes 1111541Srgrimes/*ARGSUSED*/ 11212675Sjulianstatic int 113130585Sphklogclose(struct cdev *dev, int flag, int mode, struct thread *td) 1141541Srgrimes{ 1151541Srgrimes 11677057Sphk log_open = 0; 11770069Sjhb callout_stop(&logsoftc.sc_callout); 1181541Srgrimes logsoftc.sc_state = 0; 11996122Salfred funsetown(&logsoftc.sc_sigio); 1201541Srgrimes return (0); 1211541Srgrimes} 1221541Srgrimes 1231541Srgrimes/*ARGSUSED*/ 12412675Sjulianstatic int 125130585Sphklogread(struct cdev *dev, struct uio *uio, int flag) 1261541Srgrimes{ 127116660Siedowse char buf[128]; 12869741Sphk struct msgbuf *mbp = msgbufp; 129106917Stmm int error = 0, l, s; 1301541Srgrimes 1311541Srgrimes s = splhigh(); 132116660Siedowse while (msgbuf_getcount(mbp) == 0) { 1331541Srgrimes if (flag & IO_NDELAY) { 1341541Srgrimes splx(s); 1351541Srgrimes return (EWOULDBLOCK); 1361541Srgrimes } 1371541Srgrimes logsoftc.sc_state |= LOG_RDWAIT; 13899012Salfred if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) { 1391541Srgrimes splx(s); 1401541Srgrimes return (error); 1411541Srgrimes } 1421541Srgrimes } 1431541Srgrimes splx(s); 1441541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1451541Srgrimes 1461541Srgrimes while (uio->uio_resid > 0) { 147116660Siedowse l = imin(sizeof(buf), uio->uio_resid); 148116660Siedowse l = msgbuf_getbytes(mbp, buf, l); 1491541Srgrimes if (l == 0) 1501541Srgrimes break; 151116660Siedowse error = uiomove(buf, l, uio); 1521541Srgrimes if (error) 1531541Srgrimes break; 1541541Srgrimes } 1551541Srgrimes return (error); 1561541Srgrimes} 1571541Srgrimes 1581541Srgrimes/*ARGSUSED*/ 15912675Sjulianstatic int 160130585Sphklogpoll(struct cdev *dev, int events, struct thread *td) 1611541Srgrimes{ 16229357Speter int s; 16329357Speter int revents = 0; 1641541Srgrimes 16529357Speter s = splhigh(); 1661541Srgrimes 16746568Speter if (events & (POLLIN | POLLRDNORM)) { 168116660Siedowse if (msgbuf_getcount(msgbufp) > 0) 16929357Speter revents |= events & (POLLIN | POLLRDNORM); 17029357Speter else 17183805Sjhb selrecord(td, &logsoftc.sc_selp); 17246568Speter } 1731541Srgrimes splx(s); 17429357Speter return (revents); 1751541Srgrimes} 1761541Srgrimes 17770069Sjhbstatic void 17870069Sjhblogtimeout(void *arg) 17970069Sjhb{ 18070069Sjhb 18177057Sphk if (!log_open) 18270069Sjhb return; 183116634Sbmilekic if (log_wakeups_per_second < 1) { 184116634Sbmilekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 185116634Sbmilekic log_wakeups_per_second = 1; 186116634Sbmilekic } 18777057Sphk if (msgbuftrigger == 0) { 18877057Sphk callout_reset(&logsoftc.sc_callout, 18977057Sphk hz / log_wakeups_per_second, logtimeout, NULL); 19077057Sphk return; 19177057Sphk } 19270239Sphk msgbuftrigger = 0; 193122352Stanimura selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI); 19441086Struckman if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 19595883Salfred pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 1961541Srgrimes if (logsoftc.sc_state & LOG_RDWAIT) { 197111748Sdes wakeup(msgbufp); 1981541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1991541Srgrimes } 20070069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 20170069Sjhb logtimeout, NULL); 2021541Srgrimes} 2031541Srgrimes 2041541Srgrimes/*ARGSUSED*/ 20512675Sjulianstatic int 206130585Sphklogioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td) 2071541Srgrimes{ 2081541Srgrimes 2091541Srgrimes switch (com) { 2101541Srgrimes 2111541Srgrimes /* return number of characters immediately available */ 2121541Srgrimes case FIONREAD: 213116660Siedowse *(int *)data = msgbuf_getcount(msgbufp); 2141541Srgrimes break; 2151541Srgrimes 2161541Srgrimes case FIONBIO: 2171541Srgrimes break; 2181541Srgrimes 2191541Srgrimes case FIOASYNC: 2201541Srgrimes if (*(int *)data) 2211541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2221541Srgrimes else 2231541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 2241541Srgrimes break; 2251541Srgrimes 22641086Struckman case FIOSETOWN: 22741086Struckman return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 22841086Struckman 22941086Struckman case FIOGETOWN: 230104393Struckman *(int *)data = fgetown(&logsoftc.sc_sigio); 2311541Srgrimes break; 2321541Srgrimes 23341086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 23441086Struckman case TIOCSPGRP: 23541086Struckman return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 23641086Struckman 23741086Struckman /* This is deprecated, FIOGETOWN should be used instead */ 2381541Srgrimes case TIOCGPGRP: 239104393Struckman *(int *)data = -fgetown(&logsoftc.sc_sigio); 2401541Srgrimes break; 2411541Srgrimes 2421541Srgrimes default: 2438161Sjkh return (ENOTTY); 2441541Srgrimes } 2451541Srgrimes return (0); 2461541Srgrimes} 24712517Sjulian 24812675Sjulianstatic void 24969741Sphklog_drvinit(void *unused) 25012517Sjulian{ 25169741Sphk 25250254Sphk make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 25312517Sjulian} 25412517Sjulian 25512517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 256