subr_log.c revision 12577
1193326Sed/* 2193326Sed * Copyright (c) 1982, 1986, 1993 3193326Sed * The Regents of the University of California. All rights reserved. 4193326Sed * 5193326Sed * Redistribution and use in source and binary forms, with or without 6193326Sed * modification, are permitted provided that the following conditions 7193326Sed * are met: 8193326Sed * 1. Redistributions of source code must retain the above copyright 9243830Sdim * notice, this list of conditions and the following disclaimer. 10243830Sdim * 2. Redistributions in binary form must reproduce the above copyright 11243830Sdim * notice, this list of conditions and the following disclaimer in the 12243830Sdim * documentation and/or other materials provided with the distribution. 13193326Sed * 3. All advertising materials mentioning features or use of this software 14193326Sed * must display the following acknowledgement: 15193326Sed * This product includes software developed by the University of 16193326Sed * California, Berkeley and its contributors. 17193326Sed * 4. Neither the name of the University nor the names of its contributors 18249423Sdim * may be used to endorse or promote products derived from this software 19249423Sdim * without specific prior written permission. 20249423Sdim * 21249423Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22249423Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23249423Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24249423Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25249423Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26249423Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27221345Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29193326Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30198893Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31206084Srdivacky * SUCH DAMAGE. 32221345Sdim * 33193326Sed * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 34193326Sed * $Id: subr_log.c,v 1.12 1995/11/29 14:40:35 julian Exp $ 35221345Sdim */ 36193326Sed 37203955Srdivacky/* 38234353Sdim * Error log buffer for kernel printf's. 39193326Sed */ 40193326Sed 41193326Sed#include <sys/param.h> 42193326Sed#include <sys/systm.h> 43193326Sed#include <sys/conf.h> 44193326Sed#include <sys/proc.h> 45193326Sed#include <sys/vnode.h> 46193326Sed#include <sys/ioctl.h> 47193326Sed#include <sys/msgbuf.h> 48263508Sdim#include <sys/file.h> 49193326Sed#include <sys/signalvar.h> 50199482Srdivacky 51201361Srdivacky#ifdef JREMOD 52226633Sdim#include <sys/conf.h> 53193326Sed#include <sys/kernel.h> 54193326Sed#ifdef DEVFS 55218893Sdim#include <sys/devfsext.h> 56193326Sed#endif /*DEVFS*/ 57263508Sdim#define CDEV_MAJOR 7 58193326Sed#endif /*JREMOD*/ 59193326Sed 60212904Sdim#define LOG_RDPRI (PZERO + 1) 61263508Sdim 62193326Sed#define LOG_ASYNC 0x04 63218893Sdim#define LOG_RDWAIT 0x08 64198092Srdivacky 65193326Sedstruct logsoftc { 66249423Sdim int sc_state; /* see above for possibilities */ 67198092Srdivacky struct selinfo sc_selp; /* process waiting on select call */ 68200583Srdivacky int sc_pgid; /* process/group for async I/O */ 69198092Srdivacky} logsoftc; 70194179Sed 71198092Srdivackyint log_open; /* also used in log() */ 72239462Sdim 73239462Sdim/*ARGSUSED*/ 74239462Sdimint 75239462Sdimlogopen(dev, flags, mode, p) 76243830Sdim dev_t dev; 77243830Sdim int flags, mode; 78234353Sdim struct proc *p; 79210299Sed{ 80210299Sed if (log_open) 81249423Sdim return (EBUSY); 82218893Sdim log_open = 1; 83218893Sdim logsoftc.sc_pgid = p->p_pid; /* signal process only */ 84218893Sdim return (0); 85263508Sdim} 86218893Sdim 87218893Sdim/*ARGSUSED*/ 88218893Sdimint 89218893Sdimlogclose(dev, flag, mode, p) 90218893Sdim dev_t dev; 91218893Sdim int flag, mode; 92218893Sdim struct proc *p; 93218893Sdim{ 94218893Sdim 95218893Sdim log_open = 0; 96218893Sdim logsoftc.sc_state = 0; 97218893Sdim return (0); 98221345Sdim} 99221345Sdim 100218893Sdim/*ARGSUSED*/ 101218893Sdimint 102218893Sdimlogread(dev, uio, flag) 103218893Sdim dev_t dev; 104218893Sdim struct uio *uio; 105218893Sdim int flag; 106218893Sdim{ 107218893Sdim register struct msgbuf *mbp = msgbufp; 108210299Sed register long l; 109218893Sdim register int s; 110218893Sdim int error = 0; 111218893Sdim 112218893Sdim s = splhigh(); 113218893Sdim while (mbp->msg_bufr == mbp->msg_bufx) { 114210299Sed if (flag & IO_NDELAY) { 115218893Sdim splx(s); 116218893Sdim return (EWOULDBLOCK); 117218893Sdim } 118219077Sdim logsoftc.sc_state |= LOG_RDWAIT; 119226633Sdim if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 120218893Sdim "klog", 0))) { 121193326Sed splx(s); 122218893Sdim return (error); 123218893Sdim } 124224145Sdim } 125224145Sdim splx(s); 126224145Sdim logsoftc.sc_state &= ~LOG_RDWAIT; 127224145Sdim 128218893Sdim while (uio->uio_resid > 0) { 129218893Sdim l = mbp->msg_bufx - mbp->msg_bufr; 130193326Sed if (l < 0) 131193326Sed l = MSG_BSIZE - mbp->msg_bufr; 132193326Sed l = min(l, uio->uio_resid); 133218893Sdim if (l == 0) 134218893Sdim break; 135193326Sed error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], 136193326Sed (int)l, uio); 137243830Sdim if (error) 138243830Sdim break; 139243830Sdim mbp->msg_bufr += l; 140218893Sdim if (mbp->msg_bufr >= MSG_BSIZE) 141218893Sdim mbp->msg_bufr = 0; 142218893Sdim } 143218893Sdim return (error); 144193326Sed} 145243830Sdim 146234353Sdim/*ARGSUSED*/ 147234353Sdimint 148234353Sdimlogselect(dev, rw, p) 149234353Sdim dev_t dev; 150243830Sdim int rw; 151263508Sdim struct proc *p; 152200583Srdivacky{ 153198092Srdivacky int s = splhigh(); 154198092Srdivacky 155226633Sdim switch (rw) { 156226633Sdim 157226633Sdim case FREAD: 158226633Sdim if (msgbufp->msg_bufr != msgbufp->msg_bufx) { 159198092Srdivacky splx(s); 160218893Sdim return (1); 161218893Sdim } 162218893Sdim selrecord(p, &logsoftc.sc_selp); 163226633Sdim break; 164243830Sdim } 165226633Sdim splx(s); 166226633Sdim return (0); 167226633Sdim} 168263508Sdim 169263508Sdimvoid 170263508Sdimlogwakeup() 171263508Sdim{ 172263508Sdim struct proc *p; 173210299Sed 174210299Sed if (!log_open) 175210299Sed return; 176210299Sed selwakeup(&logsoftc.sc_selp); 177210299Sed if (logsoftc.sc_state & LOG_ASYNC) { 178210299Sed if (logsoftc.sc_pgid < 0) 179210299Sed gsignal(-logsoftc.sc_pgid, SIGIO); 180210299Sed else if ((p = pfind(logsoftc.sc_pgid))) 181210299Sed psignal(p, SIGIO); 182210299Sed } 183210299Sed if (logsoftc.sc_state & LOG_RDWAIT) { 184210299Sed wakeup((caddr_t)msgbufp); 185210299Sed logsoftc.sc_state &= ~LOG_RDWAIT; 186210299Sed } 187210299Sed} 188210299Sed 189218893Sdim/*ARGSUSED*/ 190218893Sdimint 191210299Sedlogioctl(dev, com, data, flag, p) 192218893Sdim dev_t dev; 193218893Sdim int com; 194210299Sed caddr_t data; 195226633Sdim int flag; 196226633Sdim struct proc *p; 197210299Sed{ 198226633Sdim long l; 199226633Sdim int s; 200263508Sdim 201263508Sdim switch (com) { 202263508Sdim 203226633Sdim /* return number of characters immediately available */ 204239462Sdim case FIONREAD: 205239462Sdim s = splhigh(); 206239462Sdim l = msgbufp->msg_bufx - msgbufp->msg_bufr; 207194613Sed splx(s); 208243830Sdim if (l < 0) 209226633Sdim l += MSG_BSIZE; 210226633Sdim *(int *)data = l; 211243830Sdim break; 212226633Sdim 213198092Srdivacky case FIONBIO: 214243830Sdim break; 215226633Sdim 216234353Sdim case FIOASYNC: 217243830Sdim if (*(int *)data) 218234353Sdim logsoftc.sc_state |= LOG_ASYNC; 219243830Sdim else 220243830Sdim logsoftc.sc_state &= ~LOG_ASYNC; 221243830Sdim break; 222234353Sdim 223226633Sdim case TIOCSPGRP: 224226633Sdim logsoftc.sc_pgid = *(int *)data; 225226633Sdim break; 226226633Sdim 227226633Sdim case TIOCGPGRP: 228198092Srdivacky *(int *)data = logsoftc.sc_pgid; 229193326Sed break; 230218893Sdim 231234353Sdim default: 232249423Sdim return (ENOTTY); 233249423Sdim } 234234353Sdim return (0); 235193326Sed} 236226633Sdim 237226633Sdim#ifdef JREMOD 238226633Sdimstruct cdevsw log_cdevsw = 239198092Srdivacky { logopen, logclose, logread, nowrite, /*7*/ 240198092Srdivacky logioctl, nostop, nullreset, nodevtotty,/* klog */ 241198092Srdivacky logselect, nommap, NULL }; 242198092Srdivacky 243198092Srdivackystatic log_devsw_installed = 0; 244198092Srdivacky 245198092Srdivackystatic void log_drvinit(void *unused) 246198092Srdivacky{ 247198092Srdivacky dev_t dev; 248227737Sdim 249227737Sdim if( ! log_devsw_installed ) { 250227737Sdim dev = makedev(CDEV_MAJOR,0); 251198398Srdivacky cdevsw_add(&dev,&log_cdevsw,NULL); 252226633Sdim log_devsw_installed = 1; 253226633Sdim#ifdef DEVFS 254226633Sdim { 255218893Sdim int x; 256198398Srdivacky/* default for a simple device with no probe routine (usually delete this) */ 257198398Srdivacky x=devfs_add_devsw( 258226633Sdim/* path name devsw minor type uid gid perm*/ 259226633Sdim "/", "log", major(dev), 0, DV_CHR, 0, 0, 0600); 260226633Sdim } 261218893Sdim#endif 262198398Srdivacky } 263218893Sdim} 264218893Sdim 265218893SdimSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 266210299Sed 267210299Sed#endif /* JREMOD */ 268198092Srdivacky 269194613Sed