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.32 1998/11/11 10:55:56 truckman Exp $ 35 */ 36 37 /* 38 * Error log buffer for kernel printf's. 39 */ 40 41 #include "opt_devfs.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/conf.h> 46 #include <sys/proc.h> 47 #include <sys/vnode.h> 48 #include <sys/filio.h> 49 #include <sys/ttycom.h> 50 #include <sys/msgbuf.h> 51 #include <sys/signalvar.h> 52 #include <sys/kernel.h> 53 #include <sys/poll.h> 54 #include <sys/filedesc.h> 55 #ifdef DEVFS 56 #include <sys/devfsext.h> 57 #endif /*DEVFS*/ 58 59 #define LOG_RDPRI (PZERO + 1) 60 61 #define LOG_ASYNC 0x04 62 #define LOG_RDWAIT 0x08 63 64 static d_open_t logopen; 65 static d_close_t logclose; 66 static d_read_t logread; 67 static d_ioctl_t logioctl; 68 static d_poll_t logpoll; 69 70 #define CDEV_MAJOR 7 71 static struct cdevsw log_cdevsw = 72 { logopen, logclose, logread, nowrite, /*7*/ 73 logioctl, nostop, nullreset, nodevtotty,/* klog */ 74 logpoll, nommap, NULL, "log", NULL, -1 }; 75 76 static struct logsoftc { 77 int sc_state; /* see above for possibilities */ 78 struct selinfo sc_selp; /* process waiting on select call */ 79 struct sigio *sc_sigio; /* information for async I/O */ 80 } logsoftc; 81 82 int log_open; /* also used in log() */ 83 84 /*ARGSUSED*/ 85 static int 86 logopen(dev, flags, mode, p) 87 dev_t dev; 88 int flags, mode; 89 struct proc *p; 90 { 91 if (log_open) 92 return (EBUSY); 93 log_open = 1; 94 fsetown(p->p_pid, &logsoftc.sc_sigio); /* signal process only */ 95 return (0); 96 } 97 98 /*ARGSUSED*/ 99 static int 100 logclose(dev, flag, mode, p) 101 dev_t dev; 102 int flag, mode; 103 struct proc *p; 104 { 105 106 log_open = 0; 107 logsoftc.sc_state = 0; 108 funsetown(logsoftc.sc_sigio); 109 return (0); 110 } 111 112 /*ARGSUSED*/ 113 static int 114 logread(dev, uio, flag) 115 dev_t dev; 116 struct uio *uio; 117 int flag; 118 { 119 register struct msgbuf *mbp = msgbufp; 120 register long l; 121 register int s; 122 int error = 0; 123 124 s = splhigh(); 125 while (mbp->msg_bufr == mbp->msg_bufx) { 126 if (flag & IO_NDELAY) { 127 splx(s); 128 return (EWOULDBLOCK); 129 } 130 logsoftc.sc_state |= LOG_RDWAIT; 131 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 132 "klog", 0))) { 133 splx(s); 134 return (error); 135 } 136 } 137 splx(s); 138 logsoftc.sc_state &= ~LOG_RDWAIT; 139 140 while (uio->uio_resid > 0) { 141 l = mbp->msg_bufx - mbp->msg_bufr; 142 if (l < 0) 143 l = mbp->msg_size - mbp->msg_bufr; 144 l = min(l, uio->uio_resid); 145 if (l == 0) 146 break; 147 error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr, 148 (int)l, uio); 149 if (error) 150 break; 151 mbp->msg_bufr += l; 152 if (mbp->msg_bufr >= mbp->msg_size) 153 mbp->msg_bufr = 0; 154 } 155 return (error); 156 } 157 158 /*ARGSUSED*/ 159 static int 160 logpoll(dev, events, p) 161 dev_t dev; 162 int events; 163 struct proc *p; 164 { 165 int s; 166 int revents = 0; 167 168 s = splhigh(); 169 170 if (events & (POLLIN | POLLRDNORM)) 171 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 172 revents |= events & (POLLIN | POLLRDNORM); 173 else 174 selrecord(p, &logsoftc.sc_selp); 175 176 splx(s); 177 return (revents); 178 } 179 180 void 181 logwakeup() 182 { 183 if (!log_open) 184 return; 185 selwakeup(&logsoftc.sc_selp); 186 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 187 pgsigio(logsoftc.sc_sigio, SIGIO, 0); 188 if (logsoftc.sc_state & LOG_RDWAIT) { 189 wakeup((caddr_t)msgbufp); 190 logsoftc.sc_state &= ~LOG_RDWAIT; 191 } 192 } 193 194 /*ARGSUSED*/ 195 static int 196 logioctl(dev, com, data, flag, p) 197 dev_t dev; 198 u_long 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 += msgbufp->msg_size; 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 FIOSETOWN: 229 return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 230 231 case FIOGETOWN: 232 *(int *)data = fgetown(logsoftc.sc_sigio); 233 break; 234 235 /* This is deprecated, FIOSETOWN should be used instead. */ 236 case TIOCSPGRP: 237 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 238 239 /* This is deprecated, FIOGETOWN should be used instead */ 240 case TIOCGPGRP: 241 *(int *)data = -fgetown(logsoftc.sc_sigio); 242 break; 243 244 default: 245 return (ENOTTY); 246 } 247 return (0); 248 } 249 250 static int log_devsw_installed; 251 #ifdef DEVFS 252 static void *log_devfs_token; 253 #endif 254 255 static void log_drvinit __P((void *unused)); 256 static void 257 log_drvinit(unused) 258 void *unused; 259 { 260 dev_t dev; 261 262 if( ! log_devsw_installed ) { 263 dev = makedev(CDEV_MAJOR,0); 264 cdevsw_add(&dev,&log_cdevsw,NULL); 265 log_devsw_installed = 1; 266 #ifdef DEVFS 267 log_devfs_token = devfs_add_devswf(&log_cdevsw, 0, DV_CHR, 268 UID_ROOT, GID_WHEEL, 0600, 269 "klog"); 270 #endif 271 } 272 } 273 274 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 275