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