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$"); 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 5712675Sjulianstatic d_open_t logopen; 5812675Sjulianstatic d_close_t logclose; 5912675Sjulianstatic d_read_t logread; 6012675Sjulianstatic d_ioctl_t logioctl; 6129357Speterstatic d_poll_t logpoll; 62231203Skibstatic d_kqfilter_t logkqfilter; 6312675Sjulian 6470069Sjhbstatic void logtimeout(void *arg); 6570069Sjhb 6647625Sphkstatic struct cdevsw log_cdevsw = { 67126080Sphk .d_version = D_VERSION, 68111815Sphk .d_open = logopen, 69111815Sphk .d_close = logclose, 70111815Sphk .d_read = logread, 71111815Sphk .d_ioctl = logioctl, 72111815Sphk .d_poll = logpoll, 73231203Skib .d_kqfilter = logkqfilter, 74111815Sphk .d_name = "log", 7547625Sphk}; 7612675Sjulian 77231203Skibstatic int logkqread(struct knote *note, long hint); 78231203Skibstatic void logkqdetach(struct knote *note); 79231203Skib 80231203Skibstatic struct filterops log_read_filterops = { 81231203Skib .f_isfd = 1, 82231203Skib .f_attach = NULL, 83231203Skib .f_detach = logkqdetach, 84231203Skib .f_event = logkqread, 85231203Skib}; 86231203Skib 8712819Sphkstatic struct logsoftc { 881541Srgrimes int sc_state; /* see above for possibilities */ 891541Srgrimes struct selinfo sc_selp; /* process waiting on select call */ 9041087Struckman struct sigio *sc_sigio; /* information for async I/O */ 9170069Sjhb struct callout sc_callout; /* callout to wakeup syslog */ 921541Srgrimes} logsoftc; 931541Srgrimes 94198860Sedint log_open; /* also used in log() */ 95198860Sedstatic struct cv log_wakeup; 96198860Sedstruct mtx msgbuf_lock; 97198860SedMTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF); 981541Srgrimes 9970069Sjhb/* Times per second to check for a pending syslog wakeup. */ 10070069Sjhbstatic int log_wakeups_per_second = 5; 10170069SjhbSYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 10270069Sjhb &log_wakeups_per_second, 0, ""); 10370069Sjhb 1041541Srgrimes/*ARGSUSED*/ 10512675Sjulianstatic int 106130585Sphklogopen(struct cdev *dev, int flags, int mode, struct thread *td) 1071541Srgrimes{ 108198860Sed 109116634Sbmilekic if (log_wakeups_per_second < 1) { 110116634Sbmilekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 111116634Sbmilekic log_wakeups_per_second = 1; 112116634Sbmilekic } 113198860Sed 114198860Sed mtx_lock(&msgbuf_lock); 115198860Sed if (log_open) { 116198860Sed mtx_unlock(&msgbuf_lock); 117198860Sed return (EBUSY); 118198860Sed } 119198860Sed log_open = 1; 12070069Sjhb callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 12170069Sjhb logtimeout, NULL); 122198860Sed mtx_unlock(&msgbuf_lock); 123198860Sed 124198860Sed fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */ 1251541Srgrimes return (0); 1261541Srgrimes} 1271541Srgrimes 1281541Srgrimes/*ARGSUSED*/ 12912675Sjulianstatic int 130130585Sphklogclose(struct cdev *dev, int flag, int mode, struct thread *td) 1311541Srgrimes{ 1321541Srgrimes 133198860Sed funsetown(&logsoftc.sc_sigio); 134198860Sed 135198860Sed mtx_lock(&msgbuf_lock); 13670069Sjhb callout_stop(&logsoftc.sc_callout); 1371541Srgrimes logsoftc.sc_state = 0; 138198860Sed log_open = 0; 139198860Sed mtx_unlock(&msgbuf_lock); 140198860Sed 1411541Srgrimes return (0); 1421541Srgrimes} 1431541Srgrimes 1441541Srgrimes/*ARGSUSED*/ 14512675Sjulianstatic int 146130585Sphklogread(struct cdev *dev, struct uio *uio, int flag) 1471541Srgrimes{ 148116660Siedowse char buf[128]; 14969741Sphk struct msgbuf *mbp = msgbufp; 150198860Sed int error = 0, l; 1511541Srgrimes 152198860Sed mtx_lock(&msgbuf_lock); 153116660Siedowse while (msgbuf_getcount(mbp) == 0) { 1541541Srgrimes if (flag & IO_NDELAY) { 155198860Sed mtx_unlock(&msgbuf_lock); 1561541Srgrimes return (EWOULDBLOCK); 1571541Srgrimes } 158198860Sed if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) { 159198860Sed mtx_unlock(&msgbuf_lock); 1601541Srgrimes return (error); 1611541Srgrimes } 1621541Srgrimes } 1631541Srgrimes 1641541Srgrimes while (uio->uio_resid > 0) { 165116660Siedowse l = imin(sizeof(buf), uio->uio_resid); 166116660Siedowse l = msgbuf_getbytes(mbp, buf, l); 1671541Srgrimes if (l == 0) 1681541Srgrimes break; 169198860Sed mtx_unlock(&msgbuf_lock); 170116660Siedowse error = uiomove(buf, l, uio); 171198860Sed if (error || uio->uio_resid == 0) 172198860Sed return (error); 173198860Sed mtx_lock(&msgbuf_lock); 1741541Srgrimes } 175198860Sed mtx_unlock(&msgbuf_lock); 1761541Srgrimes return (error); 1771541Srgrimes} 1781541Srgrimes 1791541Srgrimes/*ARGSUSED*/ 18012675Sjulianstatic int 181130585Sphklogpoll(struct cdev *dev, int events, struct thread *td) 1821541Srgrimes{ 18329357Speter int revents = 0; 1841541Srgrimes 18546568Speter if (events & (POLLIN | POLLRDNORM)) { 186198860Sed mtx_lock(&msgbuf_lock); 187116660Siedowse if (msgbuf_getcount(msgbufp) > 0) 18829357Speter revents |= events & (POLLIN | POLLRDNORM); 18929357Speter else 19083805Sjhb selrecord(td, &logsoftc.sc_selp); 191198860Sed mtx_unlock(&msgbuf_lock); 19246568Speter } 19329357Speter return (revents); 1941541Srgrimes} 1951541Srgrimes 196231203Skibstatic int 197231203Skiblogkqfilter(struct cdev *dev, struct knote *kn) 198231203Skib{ 199231203Skib 200231203Skib if (kn->kn_filter != EVFILT_READ) 201231203Skib return (EINVAL); 202231203Skib 203231203Skib kn->kn_fop = &log_read_filterops; 204231203Skib kn->kn_hook = NULL; 205231203Skib 206231203Skib mtx_lock(&msgbuf_lock); 207231203Skib knlist_add(&logsoftc.sc_selp.si_note, kn, 1); 208231203Skib mtx_unlock(&msgbuf_lock); 209231203Skib return (0); 210231203Skib} 211231203Skib 212231203Skibstatic int 213231203Skiblogkqread(struct knote *kn, long hint) 214231203Skib{ 215231203Skib 216231203Skib mtx_assert(&msgbuf_lock, MA_OWNED); 217231203Skib kn->kn_data = msgbuf_getcount(msgbufp); 218231203Skib return (kn->kn_data != 0); 219231203Skib} 220231203Skib 22170069Sjhbstatic void 222231203Skiblogkqdetach(struct knote *kn) 223231203Skib{ 224231203Skib 225231203Skib mtx_lock(&msgbuf_lock); 226231203Skib knlist_remove(&logsoftc.sc_selp.si_note, kn, 1); 227231203Skib mtx_unlock(&msgbuf_lock); 228231203Skib} 229231203Skib 230231203Skibstatic void 23170069Sjhblogtimeout(void *arg) 23270069Sjhb{ 23370069Sjhb 23477057Sphk if (!log_open) 23570069Sjhb return; 236116634Sbmilekic if (log_wakeups_per_second < 1) { 237116634Sbmilekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 238116634Sbmilekic log_wakeups_per_second = 1; 239116634Sbmilekic } 24077057Sphk if (msgbuftrigger == 0) { 241198860Sed callout_schedule(&logsoftc.sc_callout, 242198860Sed hz / log_wakeups_per_second); 24377057Sphk return; 24477057Sphk } 24570239Sphk msgbuftrigger = 0; 246122352Stanimura selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI); 247231203Skib KNOTE_LOCKED(&logsoftc.sc_selp.si_note, 0); 24841086Struckman if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 24995883Salfred pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 250198860Sed cv_broadcastpri(&log_wakeup, LOG_RDPRI); 251198860Sed callout_schedule(&logsoftc.sc_callout, hz / log_wakeups_per_second); 2521541Srgrimes} 2531541Srgrimes 2541541Srgrimes/*ARGSUSED*/ 25512675Sjulianstatic int 256130585Sphklogioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td) 2571541Srgrimes{ 2581541Srgrimes 2591541Srgrimes switch (com) { 2601541Srgrimes 2611541Srgrimes /* return number of characters immediately available */ 2621541Srgrimes case FIONREAD: 263116660Siedowse *(int *)data = msgbuf_getcount(msgbufp); 2641541Srgrimes break; 2651541Srgrimes 2661541Srgrimes case FIONBIO: 2671541Srgrimes break; 2681541Srgrimes 2691541Srgrimes case FIOASYNC: 270198860Sed mtx_lock(&msgbuf_lock); 2711541Srgrimes if (*(int *)data) 2721541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2731541Srgrimes else 2741541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 275198860Sed mtx_unlock(&msgbuf_lock); 2761541Srgrimes break; 2771541Srgrimes 27841086Struckman case FIOSETOWN: 27941086Struckman return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 28041086Struckman 28141086Struckman case FIOGETOWN: 282104393Struckman *(int *)data = fgetown(&logsoftc.sc_sigio); 2831541Srgrimes break; 2841541Srgrimes 28541086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 28641086Struckman case TIOCSPGRP: 28741086Struckman return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 28841086Struckman 28941086Struckman /* This is deprecated, FIOGETOWN should be used instead */ 2901541Srgrimes case TIOCGPGRP: 291104393Struckman *(int *)data = -fgetown(&logsoftc.sc_sigio); 2921541Srgrimes break; 2931541Srgrimes 2941541Srgrimes default: 2958161Sjkh return (ENOTTY); 2961541Srgrimes } 2971541Srgrimes return (0); 2981541Srgrimes} 29912517Sjulian 30012675Sjulianstatic void 30169741Sphklog_drvinit(void *unused) 30212517Sjulian{ 30369741Sphk 304198860Sed cv_init(&log_wakeup, "klog"); 305198860Sed callout_init_mtx(&logsoftc.sc_callout, &msgbuf_lock, 0); 306231203Skib knlist_init_mtx(&logsoftc.sc_selp.si_note, &msgbuf_lock); 307216952Skib make_dev_credf(MAKEDEV_ETERNAL, &log_cdevsw, 0, NULL, UID_ROOT, 308216952Skib GID_WHEEL, 0600, "klog"); 30912517Sjulian} 31012517Sjulian 311177253SrwatsonSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL); 312