| 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.4 1993/10/16 15:24:39 rgrimes Exp $ |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * Error log buffer for kernel printf's. |
| 39 | */ |
| 40 | |
| 41 | #include "param.h" |
| 42 | #include "systm.h" |
| 43 | #include "proc.h" |
| 44 | #include "vnode.h" |
| 45 | #include "ioctl.h" |
| 46 | #include "msgbuf.h" |
| 47 | #include "file.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 | pid_t sc_sel; /* pid of process waiting on select call 16 Jun 93 */ |
| 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 | int |
| 64 | logopen(dev, flags, mode, p) |
| 65 | dev_t dev; |
| 66 | int flags, mode; |
| 67 | struct proc *p; |
| 68 | { |
| 69 | register struct msgbuf *mbp = msgbufp; |
| 70 | |
| 71 | if (log_open) |
| 72 | return (EBUSY); |
| 73 | log_open = 1; |
| 74 | logsoftc.sc_pgid = p->p_pid; /* signal process only */ |
| 75 | /* |
| 76 | * Potential race here with putchar() but since putchar should be |
| 77 | * called by autoconf, msg_magic should be initialized by the time |
| 78 | * we get here. |
| 79 | */ |
| 80 | if (mbp->msg_magic != MSG_MAGIC) { |
| 81 | register int i; |
| 82 | |
| 83 | mbp->msg_magic = MSG_MAGIC; |
| 84 | mbp->msg_bufx = mbp->msg_bufr = 0; |
| 85 | for (i=0; i < MSG_BSIZE; i++) |
| 86 | mbp->msg_bufc[i] = 0; |
| 87 | } |
| 88 | return (0); |
| 89 | } |
| 90 | |
| 91 | /*ARGSUSED*/ |
| 92 | int |
| 93 | logclose(dev, flag) |
| 94 | dev_t dev; |
| 95 | int flag; |
| 96 | { |
| 97 | log_open = 0; |
| 98 | logsoftc.sc_state = 0; |
| 99 | logsoftc.sc_sel = 0; /* 16 Jun 93 */ |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /*ARGSUSED*/ |
| 104 | int |
| 105 | logread(dev, uio, flag) |
| 106 | dev_t dev; |
| 107 | struct uio *uio; |
| 108 | int flag; |
| 109 | { |
| 110 | register struct msgbuf *mbp = msgbufp; |
| 111 | register long l; |
| 112 | register int s; |
| 113 | int error = 0; |
| 114 | |
| 115 | s = splhigh(); |
| 116 | while (mbp->msg_bufr == mbp->msg_bufx) { |
| 117 | if (flag & IO_NDELAY) { |
| 118 | splx(s); |
| 119 | return (EWOULDBLOCK); |
| 120 | } |
| 121 | logsoftc.sc_state |= LOG_RDWAIT; |
| 122 | if (error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, |
| 123 | "klog", 0)) { |
| 124 | splx(s); |
| 125 | return (error); |
| 126 | } |
| 127 | } |
| 128 | splx(s); |
| 129 | logsoftc.sc_state &= ~LOG_RDWAIT; |
| 130 | |
| 131 | while (uio->uio_resid > 0) { |
| 132 | l = mbp->msg_bufx - mbp->msg_bufr; |
| 133 | if (l < 0) |
| 134 | l = MSG_BSIZE - mbp->msg_bufr; |
| 135 | l = MIN(l, uio->uio_resid); |
| 136 | if (l == 0) |
| 137 | break; |
| 138 | error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], |
| 139 | (int)l, uio); |
| 140 | if (error) |
| 141 | break; |
| 142 | mbp->msg_bufr += l; |
| 143 | if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE) |
| 144 | mbp->msg_bufr = 0; |
| 145 | } |
| 146 | return (error); |
| 147 | } |
| 148 | |
| 149 | /*ARGSUSED*/ |
| 150 | int |
| 151 | logselect(dev, rw, p) |
| 152 | dev_t dev; |
| 153 | int rw; |
| 154 | struct proc *p; |
| 155 | { |
| 156 | int s = splhigh(); |
| 157 | |
| 158 | switch (rw) { |
| 159 | |
| 160 | case FREAD: |
| 161 | if (msgbufp->msg_bufr != msgbufp->msg_bufx) { |
| 162 | splx(s); |
| 163 | return (1); |
| 164 | } |
| 165 | logsoftc.sc_sel = p->p_pid; /* 16 Jun 93 */ |
| 166 | break; |
| 167 | } |
| 168 | splx(s); |
| 169 | return (0); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | logwakeup() |
| 174 | { |
| 175 | struct proc *p; |
| 176 | |
| 177 | if (!log_open) |
| 178 | return; |
| 179 | if (logsoftc.sc_sel) { /* 16 Jun 93 */ |
| 180 | selwakeup(logsoftc.sc_sel, 0); |
| 181 | logsoftc.sc_sel = 0; |
| 182 | } |
| 183 | if (logsoftc.sc_state & LOG_ASYNC) { |
| 184 | if (logsoftc.sc_pgid < 0) |
| 185 | gsignal(-logsoftc.sc_pgid, SIGIO); |
| 186 | else if (p = pfind(logsoftc.sc_pgid)) |
| 187 | psignal(p, SIGIO); |
| 188 | } |
| 189 | if (logsoftc.sc_state & LOG_RDWAIT) { |
| 190 | wakeup((caddr_t)msgbufp); |
| 191 | logsoftc.sc_state &= ~LOG_RDWAIT; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /*ARGSUSED*/ |
| 196 | int |
| 197 | logioctl(dev, com, data, flag) |
| 198 | dev_t dev; |
| 199 | int com; |
| 200 | caddr_t data; |
| 201 | int flag; |
| 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 | *(off_t *)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 (-1); |
| 238 | } |
| 239 | return (0); |
| 240 | } |