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 * $Id: subr_log.c,v 1.6 1995/04/29 05:09:19 jkh Exp $ 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/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 50 #define LOG_RDPRI (PZERO + 1) 51 52 #define LOG_ASYNC 0x04 53 #define LOG_RDWAIT 0x08 54 55 struct logsoftc { 56 int sc_state; /* see above for possibilities */ 57 struct selinfo sc_selp; /* process waiting on select call */ 58 int sc_pgid; /* process/group for async I/O */ 59 } logsoftc; 60 61 int log_open; /* also used in log() */ 62 63 /*ARGSUSED*/ 64 int 65 logopen(dev, flags, mode, p) 66 dev_t dev; 67 int flags, mode; 68 struct proc *p; 69 { 70 register struct msgbuf *mbp = msgbufp; 71 72 if (log_open) 73 return (EBUSY); 74 log_open = 1; 75 logsoftc.sc_pgid = p->p_pid; /* signal process only */ 76 /* 77 * Potential race here with putchar() but since putchar should be 78 * called by autoconf, msg_magic should be initialized by the time 79 * we get here. 80 */ 81 if (mbp->msg_magic != MSG_MAGIC) { 82 register int i; 83 84 mbp->msg_magic = MSG_MAGIC; 85 mbp->msg_bufx = mbp->msg_bufr = 0; 86 for (i=0; i < MSG_BSIZE; i++) 87 mbp->msg_bufc[i] = 0; 88 } 89 return (0); 90 } 91 92 /*ARGSUSED*/ 93 int 94 logclose(dev, flag, mode, p) 95 dev_t dev; 96 int flag, mode; 97 struct proc *p; 98 { 99 100 log_open = 0; 101 logsoftc.sc_state = 0; 102 return (0); 103 } 104 105 /*ARGSUSED*/ 106 int 107 logread(dev, uio, flag) 108 dev_t dev; 109 struct uio *uio; 110 int flag; 111 { 112 register struct msgbuf *mbp = msgbufp; 113 register long l; 114 register int s; 115 int error = 0; 116 117 s = splhigh(); 118 while (mbp->msg_bufr == mbp->msg_bufx) { 119 if (flag & IO_NDELAY) { 120 splx(s); 121 return (EWOULDBLOCK); 122 } 123 logsoftc.sc_state |= LOG_RDWAIT; 124 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 125 "klog", 0))) { 126 splx(s); 127 return (error); 128 } 129 } 130 splx(s); 131 logsoftc.sc_state &= ~LOG_RDWAIT; 132 133 while (uio->uio_resid > 0) { 134 l = mbp->msg_bufx - mbp->msg_bufr; 135 if (l < 0) 136 l = MSG_BSIZE - mbp->msg_bufr; 137 l = min(l, uio->uio_resid); 138 if (l == 0) 139 break; 140 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], 141 (int)l, uio); 142 if (error) 143 break; 144 mbp->msg_bufr += l; 145 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE) 146 mbp->msg_bufr = 0; 147 } 148 return (error); 149 } 150 151 /*ARGSUSED*/ 152 int 153 logselect(dev, rw, p) 154 dev_t dev; 155 int rw; 156 struct proc *p; 157 { 158 int s = splhigh(); 159 160 switch (rw) { 161 162 case FREAD: 163 if (msgbufp->msg_bufr != msgbufp->msg_bufx) { 164 splx(s); 165 return (1); 166 } 167 selrecord(p, &logsoftc.sc_selp); 168 break; 169 } 170 splx(s); 171 return (0); 172 } 173 174 void 175 logwakeup() 176 { 177 struct proc *p; 178 179 if (!log_open) 180 return; 181 selwakeup(&logsoftc.sc_selp); 182 if (logsoftc.sc_state & LOG_ASYNC) { 183 if (logsoftc.sc_pgid < 0) 184 gsignal(-logsoftc.sc_pgid, SIGIO); 185 else if ((p = pfind(logsoftc.sc_pgid))) 186 psignal(p, SIGIO); 187 } 188 if (logsoftc.sc_state & LOG_RDWAIT) { 189 wakeup((caddr_t)msgbufp); 190 logsoftc.sc_state &= ~LOG_RDWAIT; 191 } 192 } 193 194 /*ARGSUSED*/ 195 int 196 logioctl(dev, com, data, flag, p) 197 dev_t dev; 198 int com; 199 caddr_t data; 200 int flag; 201 struct proc *p; 202 { 203 long l; 204 int s; 205 206 switch (com) { 207 208 /* return number of characters immediately available */ 209 case FIONREAD: 210 s = splhigh(); 211 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 212 splx(s); 213 if (l < 0) 214 l += MSG_BSIZE; 215 *(int *)data = l; 216 break; 217 218 case FIONBIO: 219 break; 220 221 case FIOASYNC: 222 if (*(int *)data) 223 logsoftc.sc_state |= LOG_ASYNC; 224 else 225 logsoftc.sc_state &= ~LOG_ASYNC; 226 break; 227 228 case TIOCSPGRP: 229 logsoftc.sc_pgid = *(int *)data; 230 break; 231 232 case TIOCGPGRP: 233 *(int *)data = logsoftc.sc_pgid; 234 break; 235 236 default: 237 return (ENOTTY); 238 } 239 return (0); 240 } 241