1 /* $NetBSD: subr_log.c,v 1.20 2000/05/28 18:31:13 jhawk 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/param.h> 43 #include <sys/systm.h> 44 #include <sys/proc.h> 45 #include <sys/vnode.h> 46 #include <sys/ioctl.h> 47 #include <sys/msgbuf.h> 48 #include <sys/file.h> 49 #include <sys/signalvar.h> 50 #include <sys/syslog.h> 51 #include <sys/conf.h> 52 #include <sys/select.h> 53 #include <sys/poll.h> 54 55 #define LOG_RDPRI (PZERO + 1) 56 57 #define LOG_ASYNC 0x04 58 #define LOG_RDWAIT 0x08 59 60 struct logsoftc { 61 int sc_state; /* see above for possibilities */ 62 struct selinfo sc_selp; /* process waiting on select call */ 63 int sc_pgid; /* process/group for async I/O */ 64 } logsoftc; 65 66 int log_open; /* also used in log() */ 67 int msgbufmapped; /* is the message buffer mapped */ 68 int msgbufenabled; /* is logging to the buffer enabled */ 69 struct kern_msgbuf *msgbufp; /* the mapped buffer, itself. */ 70 71 void 72 initmsgbuf(buf, bufsize) 73 caddr_t buf; 74 size_t bufsize; 75 { 76 struct kern_msgbuf *mbp; 77 long new_bufs; 78 79 /* Sanity-check the given size. */ 80 if (bufsize < sizeof(struct kern_msgbuf)) 81 return; 82 83 mbp = msgbufp = (struct kern_msgbuf *)buf; 84 85 new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc); 86 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) || 87 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) || 88 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) { 89 /* 90 * If the buffer magic number is wrong, has changed 91 * size (which shouldn't happen often), or is 92 * internally inconsistent, initialize it. 93 */ 94 95 memset(buf, 0, bufsize); 96 mbp->msg_magic = MSG_MAGIC; 97 mbp->msg_bufs = new_bufs; 98 } 99 100 /* mark it as ready for use. */ 101 msgbufmapped = msgbufenabled = 1; 102 } 103 104 /*ARGSUSED*/ 105 int 106 logopen(dev, flags, mode, p) 107 dev_t dev; 108 int flags, mode; 109 struct proc *p; 110 { 111 struct kern_msgbuf *mbp = msgbufp; 112 113 if (log_open) 114 return (EBUSY); 115 log_open = 1; 116 logsoftc.sc_pgid = p->p_pid; /* signal process only */ 117 /* 118 * The message buffer is initialized during system configuration. 119 * If it's been clobbered, note that and return an error. (This 120 * allows a user to potentially read the buffer via /dev/kmem, 121 * and try to figure out what clobbered it. 122 */ 123 if (mbp->msg_magic != MSG_MAGIC) { 124 msgbufenabled = 0; 125 return (ENXIO); 126 } 127 128 return (0); 129 } 130 131 /*ARGSUSED*/ 132 int 133 logclose(dev, flag, mode, p) 134 dev_t dev; 135 int flag, mode; 136 struct proc *p; 137 { 138 139 log_open = 0; 140 logsoftc.sc_state = 0; 141 return (0); 142 } 143 144 /*ARGSUSED*/ 145 int 146 logread(dev, uio, flag) 147 dev_t dev; 148 struct uio *uio; 149 int flag; 150 { 151 struct kern_msgbuf *mbp = msgbufp; 152 long l; 153 int s; 154 int error = 0; 155 156 s = splhigh(); 157 while (mbp->msg_bufr == mbp->msg_bufx) { 158 if (flag & IO_NDELAY) { 159 splx(s); 160 return (EWOULDBLOCK); 161 } 162 logsoftc.sc_state |= LOG_RDWAIT; 163 error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 164 "klog", 0); 165 if (error) { 166 splx(s); 167 return (error); 168 } 169 } 170 splx(s); 171 logsoftc.sc_state &= ~LOG_RDWAIT; 172 173 while (uio->uio_resid > 0) { 174 l = mbp->msg_bufx - mbp->msg_bufr; 175 if (l < 0) 176 l = mbp->msg_bufs - mbp->msg_bufr; 177 l = min(l, uio->uio_resid); 178 if (l == 0) 179 break; 180 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], 181 (int)l, uio); 182 if (error) 183 break; 184 mbp->msg_bufr += l; 185 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 186 mbp->msg_bufr = 0; 187 } 188 return (error); 189 } 190 191 /*ARGSUSED*/ 192 int 193 logpoll(dev, events, p) 194 dev_t dev; 195 int events; 196 struct proc *p; 197 { 198 int revents = 0; 199 int s = splhigh(); 200 201 if (events & (POLLIN | POLLRDNORM)) { 202 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 203 revents |= events & (POLLIN | POLLRDNORM); 204 else 205 selrecord(p, &logsoftc.sc_selp); 206 } 207 208 splx(s); 209 return (revents); 210 } 211 212 void 213 logwakeup() 214 { 215 struct proc *p; 216 217 if (!log_open) 218 return; 219 selwakeup(&logsoftc.sc_selp); 220 if (logsoftc.sc_state & LOG_ASYNC) { 221 if (logsoftc.sc_pgid < 0) 222 gsignal(-logsoftc.sc_pgid, SIGIO); 223 else if (logsoftc.sc_pgid > 0 && 224 (p = pfind(logsoftc.sc_pgid)) != NULL) 225 psignal(p, SIGIO); 226 } 227 if (logsoftc.sc_state & LOG_RDWAIT) { 228 wakeup((caddr_t)msgbufp); 229 logsoftc.sc_state &= ~LOG_RDWAIT; 230 } 231 } 232 233 /*ARGSUSED*/ 234 int 235 logioctl(dev, com, data, flag, p) 236 dev_t dev; 237 u_long com; 238 caddr_t data; 239 int flag; 240 struct proc *p; 241 { 242 long l; 243 int s; 244 245 switch (com) { 246 247 /* return number of characters immediately available */ 248 case FIONREAD: 249 s = splhigh(); 250 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 251 splx(s); 252 if (l < 0) 253 l += msgbufp->msg_bufs; 254 *(int *)data = l; 255 break; 256 257 case FIONBIO: 258 break; 259 260 case FIOASYNC: 261 if (*(int *)data) 262 logsoftc.sc_state |= LOG_ASYNC; 263 else 264 logsoftc.sc_state &= ~LOG_ASYNC; 265 break; 266 267 case TIOCSPGRP: 268 logsoftc.sc_pgid = *(int *)data; 269 break; 270 271 case TIOCGPGRP: 272 *(int *)data = logsoftc.sc_pgid; 273 break; 274 275 default: 276 return (-1); 277 } 278 return (0); 279 } 280