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