1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32df8bae1dSRodney W. Grimes /* 33df8bae1dSRodney W. Grimes * Error log buffer for kernel printf's. 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36677b542eSDavid E. O'Brien #include <sys/cdefs.h> 37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38677b542eSDavid E. O'Brien 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40df8bae1dSRodney W. Grimes #include <sys/systm.h> 4198d93822SBruce Evans #include <sys/conf.h> 42df8bae1dSRodney W. Grimes #include <sys/proc.h> 43df8bae1dSRodney W. Grimes #include <sys/vnode.h> 4420982410SBruce Evans #include <sys/filio.h> 4520982410SBruce Evans #include <sys/ttycom.h> 46df8bae1dSRodney W. Grimes #include <sys/msgbuf.h> 47797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 4853ac6efbSJulian Elischer #include <sys/kernel.h> 491514b90fSPeter Wemm #include <sys/poll.h> 50831d27a9SDon Lewis #include <sys/filedesc.h> 51a9b13707SJohn Baldwin #include <sys/sysctl.h> 5253ac6efbSJulian Elischer 53df8bae1dSRodney W. Grimes #define LOG_RDPRI (PZERO + 1) 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #define LOG_ASYNC 0x04 56df8bae1dSRodney W. Grimes #define LOG_RDWAIT 0x08 57df8bae1dSRodney W. Grimes 5887f6c662SJulian Elischer static d_open_t logopen; 5987f6c662SJulian Elischer static d_close_t logclose; 6087f6c662SJulian Elischer static d_read_t logread; 6187f6c662SJulian Elischer static d_ioctl_t logioctl; 621514b90fSPeter Wemm static d_poll_t logpoll; 6387f6c662SJulian Elischer 64a9b13707SJohn Baldwin static void logtimeout(void *arg); 65a9b13707SJohn Baldwin 6687f6c662SJulian Elischer #define CDEV_MAJOR 7 674e2f199eSPoul-Henning Kamp static struct cdevsw log_cdevsw = { 68dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 69dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 707ac40f5fSPoul-Henning Kamp .d_open = logopen, 717ac40f5fSPoul-Henning Kamp .d_close = logclose, 727ac40f5fSPoul-Henning Kamp .d_read = logread, 737ac40f5fSPoul-Henning Kamp .d_ioctl = logioctl, 747ac40f5fSPoul-Henning Kamp .d_poll = logpoll, 757ac40f5fSPoul-Henning Kamp .d_name = "log", 767ac40f5fSPoul-Henning Kamp .d_maj = CDEV_MAJOR, 774e2f199eSPoul-Henning Kamp }; 7887f6c662SJulian Elischer 7987b6de2bSPoul-Henning Kamp static struct logsoftc { 80df8bae1dSRodney W. Grimes int sc_state; /* see above for possibilities */ 81df8bae1dSRodney W. Grimes struct selinfo sc_selp; /* process waiting on select call */ 8262d6ce3aSDon Lewis struct sigio *sc_sigio; /* information for async I/O */ 83a9b13707SJohn Baldwin struct callout sc_callout; /* callout to wakeup syslog */ 84df8bae1dSRodney W. Grimes } logsoftc; 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes int log_open; /* also used in log() */ 87df8bae1dSRodney W. Grimes 88a9b13707SJohn Baldwin /* Times per second to check for a pending syslog wakeup. */ 89a9b13707SJohn Baldwin static int log_wakeups_per_second = 5; 90a9b13707SJohn Baldwin SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 91a9b13707SJohn Baldwin &log_wakeups_per_second, 0, ""); 92a9b13707SJohn Baldwin 93df8bae1dSRodney W. Grimes /*ARGSUSED*/ 9487f6c662SJulian Elischer static int 9589c9c53dSPoul-Henning Kamp logopen(struct cdev *dev, int flags, int mode, struct thread *td) 96df8bae1dSRodney W. Grimes { 97df8bae1dSRodney W. Grimes if (log_open) 98df8bae1dSRodney W. Grimes return (EBUSY); 99df8bae1dSRodney W. Grimes log_open = 1; 100a9b13707SJohn Baldwin callout_init(&logsoftc.sc_callout, 0); 101b40ce416SJulian Elischer fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */ 102b2b417bbSBosko Milekic if (log_wakeups_per_second < 1) { 103b2b417bbSBosko Milekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 104b2b417bbSBosko Milekic log_wakeups_per_second = 1; 105b2b417bbSBosko Milekic } 106a9b13707SJohn Baldwin callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 107a9b13707SJohn Baldwin logtimeout, NULL); 108df8bae1dSRodney W. Grimes return (0); 109df8bae1dSRodney W. Grimes } 110df8bae1dSRodney W. Grimes 111df8bae1dSRodney W. Grimes /*ARGSUSED*/ 11287f6c662SJulian Elischer static int 11389c9c53dSPoul-Henning Kamp logclose(struct cdev *dev, int flag, int mode, struct thread *td) 114df8bae1dSRodney W. Grimes { 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes log_open = 0; 1174787f91dSPoul-Henning Kamp callout_stop(&logsoftc.sc_callout); 118df8bae1dSRodney W. Grimes logsoftc.sc_state = 0; 119e649887bSAlfred Perlstein funsetown(&logsoftc.sc_sigio); 120df8bae1dSRodney W. Grimes return (0); 121df8bae1dSRodney W. Grimes } 122df8bae1dSRodney W. Grimes 123df8bae1dSRodney W. Grimes /*ARGSUSED*/ 12487f6c662SJulian Elischer static int 12589c9c53dSPoul-Henning Kamp logread(struct cdev *dev, struct uio *uio, int flag) 126df8bae1dSRodney W. Grimes { 1274784a469SIan Dowse char buf[128]; 12806b6617eSPoul-Henning Kamp struct msgbuf *mbp = msgbufp; 12901ee4395SThomas Moestl int error = 0, l, s; 130df8bae1dSRodney W. Grimes 131df8bae1dSRodney W. Grimes s = splhigh(); 1324784a469SIan Dowse while (msgbuf_getcount(mbp) == 0) { 133df8bae1dSRodney W. Grimes if (flag & IO_NDELAY) { 134df8bae1dSRodney W. Grimes splx(s); 135df8bae1dSRodney W. Grimes return (EWOULDBLOCK); 136df8bae1dSRodney W. Grimes } 137df8bae1dSRodney W. Grimes logsoftc.sc_state |= LOG_RDWAIT; 13801609114SAlfred Perlstein if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) { 139df8bae1dSRodney W. Grimes splx(s); 140df8bae1dSRodney W. Grimes return (error); 141df8bae1dSRodney W. Grimes } 142df8bae1dSRodney W. Grimes } 143df8bae1dSRodney W. Grimes splx(s); 144df8bae1dSRodney W. Grimes logsoftc.sc_state &= ~LOG_RDWAIT; 145df8bae1dSRodney W. Grimes 146df8bae1dSRodney W. Grimes while (uio->uio_resid > 0) { 1474784a469SIan Dowse l = imin(sizeof(buf), uio->uio_resid); 1484784a469SIan Dowse l = msgbuf_getbytes(mbp, buf, l); 149df8bae1dSRodney W. Grimes if (l == 0) 150df8bae1dSRodney W. Grimes break; 1514784a469SIan Dowse error = uiomove(buf, l, uio); 152df8bae1dSRodney W. Grimes if (error) 153df8bae1dSRodney W. Grimes break; 154df8bae1dSRodney W. Grimes } 155df8bae1dSRodney W. Grimes return (error); 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes /*ARGSUSED*/ 15987f6c662SJulian Elischer static int 16089c9c53dSPoul-Henning Kamp logpoll(struct cdev *dev, int events, struct thread *td) 161df8bae1dSRodney W. Grimes { 1621514b90fSPeter Wemm int s; 1631514b90fSPeter Wemm int revents = 0; 164df8bae1dSRodney W. Grimes 1651514b90fSPeter Wemm s = splhigh(); 166df8bae1dSRodney W. Grimes 167dfd5dee1SPeter Wemm if (events & (POLLIN | POLLRDNORM)) { 1684784a469SIan Dowse if (msgbuf_getcount(msgbufp) > 0) 1691514b90fSPeter Wemm revents |= events & (POLLIN | POLLRDNORM); 1701514b90fSPeter Wemm else 171ed01445dSJohn Baldwin selrecord(td, &logsoftc.sc_selp); 172dfd5dee1SPeter Wemm } 173df8bae1dSRodney W. Grimes splx(s); 1741514b90fSPeter Wemm return (revents); 175df8bae1dSRodney W. Grimes } 176df8bae1dSRodney W. Grimes 177a9b13707SJohn Baldwin static void 178a9b13707SJohn Baldwin logtimeout(void *arg) 179a9b13707SJohn Baldwin { 180a9b13707SJohn Baldwin 181df8bae1dSRodney W. Grimes if (!log_open) 182df8bae1dSRodney W. Grimes return; 183b2b417bbSBosko Milekic if (log_wakeups_per_second < 1) { 184b2b417bbSBosko Milekic printf("syslog wakeup is less than one. Adjusting to 1.\n"); 185b2b417bbSBosko Milekic log_wakeups_per_second = 1; 186b2b417bbSBosko Milekic } 1874787f91dSPoul-Henning Kamp if (msgbuftrigger == 0) { 1884787f91dSPoul-Henning Kamp callout_reset(&logsoftc.sc_callout, 1894787f91dSPoul-Henning Kamp hz / log_wakeups_per_second, logtimeout, NULL); 1904787f91dSPoul-Henning Kamp return; 1914787f91dSPoul-Henning Kamp } 1924787f91dSPoul-Henning Kamp msgbuftrigger = 0; 193512824f8SSeigo Tanimura selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI); 194831d27a9SDon Lewis if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 195f1320723SAlfred Perlstein pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 196df8bae1dSRodney W. Grimes if (logsoftc.sc_state & LOG_RDWAIT) { 197521f364bSDag-Erling Smørgrav wakeup(msgbufp); 198df8bae1dSRodney W. Grimes logsoftc.sc_state &= ~LOG_RDWAIT; 199df8bae1dSRodney W. Grimes } 200a9b13707SJohn Baldwin callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 201a9b13707SJohn Baldwin logtimeout, NULL); 202df8bae1dSRodney W. Grimes } 203df8bae1dSRodney W. Grimes 204df8bae1dSRodney W. Grimes /*ARGSUSED*/ 20587f6c662SJulian Elischer static int 20689c9c53dSPoul-Henning Kamp logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td) 207df8bae1dSRodney W. Grimes { 208df8bae1dSRodney W. Grimes 209df8bae1dSRodney W. Grimes switch (com) { 210df8bae1dSRodney W. Grimes 211df8bae1dSRodney W. Grimes /* return number of characters immediately available */ 212df8bae1dSRodney W. Grimes case FIONREAD: 2134784a469SIan Dowse *(int *)data = msgbuf_getcount(msgbufp); 214df8bae1dSRodney W. Grimes break; 215df8bae1dSRodney W. Grimes 216df8bae1dSRodney W. Grimes case FIONBIO: 217df8bae1dSRodney W. Grimes break; 218df8bae1dSRodney W. Grimes 219df8bae1dSRodney W. Grimes case FIOASYNC: 220df8bae1dSRodney W. Grimes if (*(int *)data) 221df8bae1dSRodney W. Grimes logsoftc.sc_state |= LOG_ASYNC; 222df8bae1dSRodney W. Grimes else 223df8bae1dSRodney W. Grimes logsoftc.sc_state &= ~LOG_ASYNC; 224df8bae1dSRodney W. Grimes break; 225df8bae1dSRodney W. Grimes 226831d27a9SDon Lewis case FIOSETOWN: 227831d27a9SDon Lewis return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 228831d27a9SDon Lewis 229831d27a9SDon Lewis case FIOGETOWN: 23091e97a82SDon Lewis *(int *)data = fgetown(&logsoftc.sc_sigio); 231df8bae1dSRodney W. Grimes break; 232df8bae1dSRodney W. Grimes 233831d27a9SDon Lewis /* This is deprecated, FIOSETOWN should be used instead. */ 234831d27a9SDon Lewis case TIOCSPGRP: 235831d27a9SDon Lewis return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 236831d27a9SDon Lewis 237831d27a9SDon Lewis /* This is deprecated, FIOGETOWN should be used instead */ 238df8bae1dSRodney W. Grimes case TIOCGPGRP: 23991e97a82SDon Lewis *(int *)data = -fgetown(&logsoftc.sc_sigio); 240df8bae1dSRodney W. Grimes break; 241df8bae1dSRodney W. Grimes 242df8bae1dSRodney W. Grimes default: 243bcac5617SJordan K. Hubbard return (ENOTTY); 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes return (0); 246df8bae1dSRodney W. Grimes } 24753ac6efbSJulian Elischer 24887f6c662SJulian Elischer static void 24906b6617eSPoul-Henning Kamp log_drvinit(void *unused) 25053ac6efbSJulian Elischer { 25106b6617eSPoul-Henning Kamp 2529dcbe240SPoul-Henning Kamp make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 2537198bf47SJulian Elischer } 25453ac6efbSJulian Elischer 25553ac6efbSJulian Elischer SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 256