subr_log.c revision 122352
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 122352 2003-11-09 09:17:26Z tanimura $"); 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 */ 104116634Sbmilekic if (log_wakeups_per_second < 1) { 105116634Sbmilekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 106116634Sbmilekic log_wakeups_per_second = 1; 107116634Sbmilekic } 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{ 129116660Siedowse char buf[128]; 13069741Sphk struct msgbuf *mbp = msgbufp; 131106917Stmm int error = 0, l, s; 1321541Srgrimes 1331541Srgrimes s = splhigh(); 134116660Siedowse while (msgbuf_getcount(mbp) == 0) { 1351541Srgrimes if (flag & IO_NDELAY) { 1361541Srgrimes splx(s); 1371541Srgrimes return (EWOULDBLOCK); 1381541Srgrimes } 1391541Srgrimes logsoftc.sc_state |= LOG_RDWAIT; 14099012Salfred if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) { 1411541Srgrimes splx(s); 1421541Srgrimes return (error); 1431541Srgrimes } 1441541Srgrimes } 1451541Srgrimes splx(s); 1461541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1471541Srgrimes 1481541Srgrimes while (uio->uio_resid > 0) { 149116660Siedowse l = imin(sizeof(buf), uio->uio_resid); 150116660Siedowse l = msgbuf_getbytes(mbp, buf, l); 1511541Srgrimes if (l == 0) 1521541Srgrimes break; 153116660Siedowse error = uiomove(buf, l, uio); 1541541Srgrimes if (error) 1551541Srgrimes break; 1561541Srgrimes } 1571541Srgrimes return (error); 1581541Srgrimes} 1591541Srgrimes 1601541Srgrimes/*ARGSUSED*/ 16112675Sjulianstatic int 16283366Sjulianlogpoll(dev_t dev, int events, struct thread *td) 1631541Srgrimes{ 16429357Speter int s; 16529357Speter int revents = 0; 1661541Srgrimes 16729357Speter s = splhigh(); 1681541Srgrimes 16946568Speter if (events & (POLLIN | POLLRDNORM)) { 170116660Siedowse if (msgbuf_getcount(msgbufp) > 0) 17129357Speter revents |= events & (POLLIN | POLLRDNORM); 17229357Speter else 17383805Sjhb selrecord(td, &logsoftc.sc_selp); 17446568Speter } 1751541Srgrimes splx(s); 17629357Speter return (revents); 1771541Srgrimes} 1781541Srgrimes 17970069Sjhbstatic void 18070069Sjhblogtimeout(void *arg) 18170069Sjhb{ 18270069Sjhb 18377057Sphk if (!log_open) 18470069Sjhb return; 185116634Sbmilekic if (log_wakeups_per_second < 1) { 186116634Sbmilekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 187116634Sbmilekic log_wakeups_per_second = 1; 188116634Sbmilekic } 18977057Sphk if (msgbuftrigger == 0) { 19077057Sphk callout_reset(&logsoftc.sc_callout, 19177057Sphk hz / log_wakeups_per_second, logtimeout, NULL); 19277057Sphk return; 19377057Sphk } 19470239Sphk msgbuftrigger = 0; 195122352Stanimura selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI); 19641086Struckman if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 19795883Salfred pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 1981541Srgrimes if (logsoftc.sc_state & LOG_RDWAIT) { 199111748Sdes wakeup(msgbufp); 2001541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 2011541Srgrimes } 20270069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 20370069Sjhb logtimeout, NULL); 2041541Srgrimes} 2051541Srgrimes 2061541Srgrimes/*ARGSUSED*/ 20712675Sjulianstatic int 20883366Sjulianlogioctl(dev_t dev, u_long com, caddr_t data, int flag, struct thread *td) 2091541Srgrimes{ 2101541Srgrimes 2111541Srgrimes switch (com) { 2121541Srgrimes 2131541Srgrimes /* return number of characters immediately available */ 2141541Srgrimes case FIONREAD: 215116660Siedowse *(int *)data = msgbuf_getcount(msgbufp); 2161541Srgrimes break; 2171541Srgrimes 2181541Srgrimes case FIONBIO: 2191541Srgrimes break; 2201541Srgrimes 2211541Srgrimes case FIOASYNC: 2221541Srgrimes if (*(int *)data) 2231541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2241541Srgrimes else 2251541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 2261541Srgrimes break; 2271541Srgrimes 22841086Struckman case FIOSETOWN: 22941086Struckman return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 23041086Struckman 23141086Struckman case FIOGETOWN: 232104393Struckman *(int *)data = fgetown(&logsoftc.sc_sigio); 2331541Srgrimes break; 2341541Srgrimes 23541086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 23641086Struckman case TIOCSPGRP: 23741086Struckman return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 23841086Struckman 23941086Struckman /* This is deprecated, FIOGETOWN should be used instead */ 2401541Srgrimes case TIOCGPGRP: 241104393Struckman *(int *)data = -fgetown(&logsoftc.sc_sigio); 2421541Srgrimes break; 2431541Srgrimes 2441541Srgrimes default: 2458161Sjkh return (ENOTTY); 2461541Srgrimes } 2471541Srgrimes return (0); 2481541Srgrimes} 24912517Sjulian 25012675Sjulianstatic void 25169741Sphklog_drvinit(void *unused) 25212517Sjulian{ 25369741Sphk 25450254Sphk make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 25512517Sjulian} 25612517Sjulian 25712517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 258