1 /* $NetBSD: subr_log.c,v 1.38 2006/10/12 01:32:18 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95 32 */ 33 34 /* 35 * Error log buffer for kernel printf's. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.38 2006/10/12 01:32:18 christos Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <sys/vnode.h> 45 #include <sys/ioctl.h> 46 #include <sys/msgbuf.h> 47 #include <sys/file.h> 48 #include <sys/signalvar.h> 49 #include <sys/syslog.h> 50 #include <sys/conf.h> 51 #include <sys/select.h> 52 #include <sys/poll.h> 53 54 #define LOG_RDPRI (PZERO + 1) 55 56 #define LOG_ASYNC 0x04 57 #define LOG_RDWAIT 0x08 58 59 struct logsoftc { 60 int sc_state; /* see above for possibilities */ 61 struct selinfo sc_selp; /* process waiting on select call */ 62 pid_t sc_pgid; /* process/group for async I/O */ 63 } logsoftc; 64 65 int log_open; /* also used in log() */ 66 int msgbufmapped; /* is the message buffer mapped */ 67 int msgbufenabled; /* is logging to the buffer enabled */ 68 struct kern_msgbuf *msgbufp; /* the mapped buffer, itself. */ 69 70 void 71 initmsgbuf(void *bf, size_t bufsize) 72 { 73 struct kern_msgbuf *mbp; 74 long new_bufs; 75 76 /* Sanity-check the given size. */ 77 if (bufsize < sizeof(struct kern_msgbuf)) 78 return; 79 80 mbp = msgbufp = (struct kern_msgbuf *)bf; 81 82 new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc); 83 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) || 84 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) || 85 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) { 86 /* 87 * If the buffer magic number is wrong, has changed 88 * size (which shouldn't happen often), or is 89 * internally inconsistent, initialize it. 90 */ 91 92 memset(bf, 0, bufsize); 93 mbp->msg_magic = MSG_MAGIC; 94 mbp->msg_bufs = new_bufs; 95 } 96 97 /* mark it as ready for use. */ 98 msgbufmapped = msgbufenabled = 1; 99 } 100 101 /*ARGSUSED*/ 102 static int 103 logopen(dev_t dev __unused, int flags __unused, int mode __unused, 104 struct lwp *l) 105 { 106 struct kern_msgbuf *mbp = msgbufp; 107 108 if (log_open) 109 return (EBUSY); 110 log_open = 1; 111 logsoftc.sc_pgid = l->l_proc->p_pid; /* signal process only */ 112 /* 113 * The message buffer is initialized during system configuration. 114 * If it's been clobbered, note that and return an error. (This 115 * allows a user to potentially read the buffer via /dev/kmem, 116 * and try to figure out what clobbered it. 117 */ 118 if (mbp->msg_magic != MSG_MAGIC) { 119 msgbufenabled = 0; 120 return (ENXIO); 121 } 122 123 return (0); 124 } 125 126 /*ARGSUSED*/ 127 static int 128 logclose(dev_t dev __unused, int flag __unused, int mode __unused, 129 struct lwp *l __unused) 130 { 131 132 log_open = 0; 133 logsoftc.sc_state = 0; 134 return (0); 135 } 136 137 /*ARGSUSED*/ 138 static int 139 logread(dev_t dev __unused, struct uio *uio, int flag) 140 { 141 struct kern_msgbuf *mbp = msgbufp; 142 long l; 143 int s; 144 int error = 0; 145 146 s = splhigh(); 147 while (mbp->msg_bufr == mbp->msg_bufx) { 148 if (flag & IO_NDELAY) { 149 splx(s); 150 return (EWOULDBLOCK); 151 } 152 logsoftc.sc_state |= LOG_RDWAIT; 153 error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 154 "klog", 0); 155 if (error) { 156 splx(s); 157 return (error); 158 } 159 } 160 splx(s); 161 logsoftc.sc_state &= ~LOG_RDWAIT; 162 163 while (uio->uio_resid > 0) { 164 l = mbp->msg_bufx - mbp->msg_bufr; 165 if (l < 0) 166 l = mbp->msg_bufs - mbp->msg_bufr; 167 l = min(l, uio->uio_resid); 168 if (l == 0) 169 break; 170 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], 171 (int)l, uio); 172 if (error) 173 break; 174 mbp->msg_bufr += l; 175 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 176 mbp->msg_bufr = 0; 177 } 178 return (error); 179 } 180 181 /*ARGSUSED*/ 182 static int 183 logpoll(dev_t dev __unused, int events, struct lwp *l) 184 { 185 int revents = 0; 186 int s = splhigh(); 187 188 if (events & (POLLIN | POLLRDNORM)) { 189 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 190 revents |= events & (POLLIN | POLLRDNORM); 191 else 192 selrecord(l, &logsoftc.sc_selp); 193 } 194 195 splx(s); 196 return (revents); 197 } 198 199 static void 200 filt_logrdetach(struct knote *kn) 201 { 202 int s; 203 204 s = splhigh(); 205 SLIST_REMOVE(&logsoftc.sc_selp.sel_klist, kn, knote, kn_selnext); 206 splx(s); 207 } 208 209 static int 210 filt_logread(struct knote *kn, long hint __unused) 211 { 212 213 if (msgbufp->msg_bufr == msgbufp->msg_bufx) 214 return (0); 215 216 if (msgbufp->msg_bufr < msgbufp->msg_bufx) 217 kn->kn_data = msgbufp->msg_bufx - msgbufp->msg_bufr; 218 else 219 kn->kn_data = (msgbufp->msg_bufs - msgbufp->msg_bufr) + 220 msgbufp->msg_bufx; 221 222 return (1); 223 } 224 225 static const struct filterops logread_filtops = 226 { 1, NULL, filt_logrdetach, filt_logread }; 227 228 static int 229 logkqfilter(dev_t dev __unused, struct knote *kn) 230 { 231 struct klist *klist; 232 int s; 233 234 switch (kn->kn_filter) { 235 case EVFILT_READ: 236 klist = &logsoftc.sc_selp.sel_klist; 237 kn->kn_fop = &logread_filtops; 238 break; 239 240 default: 241 return (1); 242 } 243 244 kn->kn_hook = NULL; 245 246 s = splhigh(); 247 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 248 splx(s); 249 250 return (0); 251 } 252 253 void 254 logwakeup(void) 255 { 256 if (!log_open) 257 return; 258 selnotify(&logsoftc.sc_selp, 0); 259 if (logsoftc.sc_state & LOG_ASYNC) 260 fownsignal(logsoftc.sc_pgid, SIGIO, 0, 0, NULL); 261 if (logsoftc.sc_state & LOG_RDWAIT) { 262 wakeup((caddr_t)msgbufp); 263 logsoftc.sc_state &= ~LOG_RDWAIT; 264 } 265 } 266 267 /*ARGSUSED*/ 268 static int 269 logioctl(dev_t dev __unused, u_long com, caddr_t data, int flag __unused, 270 struct lwp *lwp) 271 { 272 struct proc *p = lwp->l_proc; 273 long l; 274 int s; 275 276 switch (com) { 277 278 /* return number of characters immediately available */ 279 case FIONREAD: 280 s = splhigh(); 281 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 282 splx(s); 283 if (l < 0) 284 l += msgbufp->msg_bufs; 285 *(int *)data = l; 286 break; 287 288 case FIONBIO: 289 break; 290 291 case FIOASYNC: 292 if (*(int *)data) 293 logsoftc.sc_state |= LOG_ASYNC; 294 else 295 logsoftc.sc_state &= ~LOG_ASYNC; 296 break; 297 298 case TIOCSPGRP: 299 case FIOSETOWN: 300 return fsetown(p, &logsoftc.sc_pgid, com, data); 301 302 case TIOCGPGRP: 303 case FIOGETOWN: 304 return fgetown(p, logsoftc.sc_pgid, com, data); 305 306 default: 307 return (EPASSTHROUGH); 308 } 309 return (0); 310 } 311 312 const struct cdevsw log_cdevsw = { 313 logopen, logclose, logread, nowrite, logioctl, 314 nostop, notty, logpoll, nommap, logkqfilter, D_OTHER, 315 }; 316