1 /* 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 34 * $FreeBSD$ 35 */ 36 37 /* 38 * Error log buffer for kernel printf's. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/proc.h> 45 #include <sys/vnode.h> 46 #include <sys/filio.h> 47 #include <sys/ttycom.h> 48 #include <sys/msgbuf.h> 49 #include <sys/signalvar.h> 50 #include <sys/kernel.h> 51 #include <sys/poll.h> 52 #include <sys/filedesc.h> 53 #include <sys/sysctl.h> 54 55 #define LOG_RDPRI (PZERO + 1) 56 57 #define LOG_ASYNC 0x04 58 #define LOG_RDWAIT 0x08 59 60 static d_open_t logopen; 61 static d_close_t logclose; 62 static d_read_t logread; 63 static d_ioctl_t logioctl; 64 static d_poll_t logpoll; 65 66 static void logtimeout(void *arg); 67 68 #define CDEV_MAJOR 7 69 static struct cdevsw log_cdevsw = { 70 /* open */ logopen, 71 /* close */ logclose, 72 /* read */ logread, 73 /* write */ nowrite, 74 /* ioctl */ logioctl, 75 /* poll */ logpoll, 76 /* mmap */ nommap, 77 /* strategy */ nostrategy, 78 /* name */ "log", 79 /* maj */ CDEV_MAJOR, 80 /* dump */ nodump, 81 /* psize */ nopsize, 82 /* flags */ 0, 83 }; 84 85 static struct logsoftc { 86 int sc_state; /* see above for possibilities */ 87 struct selinfo sc_selp; /* process waiting on select call */ 88 struct sigio *sc_sigio; /* information for async I/O */ 89 struct callout sc_callout; /* callout to wakeup syslog */ 90 } logsoftc; 91 92 int log_open; /* also used in log() */ 93 94 /* Times per second to check for a pending syslog wakeup. */ 95 static int log_wakeups_per_second = 5; 96 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 97 &log_wakeups_per_second, 0, ""); 98 99 /*ARGSUSED*/ 100 static int 101 logopen(dev_t dev, int flags, int mode, struct proc *p) 102 { 103 if (log_open) 104 return (EBUSY); 105 log_open = 1; 106 callout_init(&logsoftc.sc_callout, 0); 107 fsetown(p->p_pid, &logsoftc.sc_sigio); /* signal process only */ 108 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 109 logtimeout, NULL); 110 return (0); 111 } 112 113 /*ARGSUSED*/ 114 static int 115 logclose(dev_t dev, int flag, int mode, struct proc *p) 116 { 117 118 callout_stop(&logsoftc.sc_callout); 119 log_open = 0; 120 logsoftc.sc_state = 0; 121 funsetown(logsoftc.sc_sigio); 122 return (0); 123 } 124 125 /*ARGSUSED*/ 126 static int 127 logread(dev_t dev, struct uio *uio, int flag) 128 { 129 struct msgbuf *mbp = msgbufp; 130 long l; 131 int s; 132 int error = 0; 133 134 s = splhigh(); 135 while (mbp->msg_bufr == mbp->msg_bufx) { 136 if (flag & IO_NDELAY) { 137 splx(s); 138 return (EWOULDBLOCK); 139 } 140 logsoftc.sc_state |= LOG_RDWAIT; 141 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 142 "klog", 0))) { 143 splx(s); 144 return (error); 145 } 146 } 147 splx(s); 148 logsoftc.sc_state &= ~LOG_RDWAIT; 149 150 while (uio->uio_resid > 0) { 151 l = mbp->msg_bufx - mbp->msg_bufr; 152 if (l < 0) 153 l = mbp->msg_size - mbp->msg_bufr; 154 l = min(l, uio->uio_resid); 155 if (l == 0) 156 break; 157 error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr, 158 (int)l, uio); 159 if (error) 160 break; 161 mbp->msg_bufr += l; 162 if (mbp->msg_bufr >= mbp->msg_size) 163 mbp->msg_bufr = 0; 164 } 165 return (error); 166 } 167 168 /*ARGSUSED*/ 169 static int 170 logpoll(dev_t dev, int events, struct proc *p) 171 { 172 int s; 173 int revents = 0; 174 175 s = splhigh(); 176 177 if (events & (POLLIN | POLLRDNORM)) { 178 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 179 revents |= events & (POLLIN | POLLRDNORM); 180 else 181 selrecord(p, &logsoftc.sc_selp); 182 } 183 splx(s); 184 return (revents); 185 } 186 187 static void 188 logtimeout(void *arg) 189 { 190 191 if (msgbuftrigger == 0) 192 return; 193 msgbuftrigger = 0; 194 if (!log_open) 195 return; 196 selwakeup(&logsoftc.sc_selp); 197 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 198 pgsigio(logsoftc.sc_sigio, SIGIO, 0); 199 if (logsoftc.sc_state & LOG_RDWAIT) { 200 wakeup((caddr_t)msgbufp); 201 logsoftc.sc_state &= ~LOG_RDWAIT; 202 } 203 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 204 logtimeout, NULL); 205 } 206 207 /*ARGSUSED*/ 208 static int 209 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 210 { 211 long l; 212 int s; 213 214 switch (com) { 215 216 /* return number of characters immediately available */ 217 case FIONREAD: 218 s = splhigh(); 219 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 220 splx(s); 221 if (l < 0) 222 l += msgbufp->msg_size; 223 *(int *)data = l; 224 break; 225 226 case FIONBIO: 227 break; 228 229 case FIOASYNC: 230 if (*(int *)data) 231 logsoftc.sc_state |= LOG_ASYNC; 232 else 233 logsoftc.sc_state &= ~LOG_ASYNC; 234 break; 235 236 case FIOSETOWN: 237 return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 238 239 case FIOGETOWN: 240 *(int *)data = fgetown(logsoftc.sc_sigio); 241 break; 242 243 /* This is deprecated, FIOSETOWN should be used instead. */ 244 case TIOCSPGRP: 245 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 246 247 /* This is deprecated, FIOGETOWN should be used instead */ 248 case TIOCGPGRP: 249 *(int *)data = -fgetown(logsoftc.sc_sigio); 250 break; 251 252 default: 253 return (ENOTTY); 254 } 255 return (0); 256 } 257 258 static void 259 log_drvinit(void *unused) 260 { 261 262 make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 263 } 264 265 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 266