subr_log.c revision 9979
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 349979Sdg * $Id: subr_log.c,v 1.8 1995/05/30 08:05:52 rgrimes Exp $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Error log buffer for kernel printf's. 391541Srgrimes */ 401541Srgrimes 411541Srgrimes#include <sys/param.h> 421541Srgrimes#include <sys/systm.h> 431541Srgrimes#include <sys/proc.h> 441541Srgrimes#include <sys/vnode.h> 451541Srgrimes#include <sys/ioctl.h> 461541Srgrimes#include <sys/msgbuf.h> 471541Srgrimes#include <sys/file.h> 483308Sphk#include <sys/signalvar.h> 491541Srgrimes 501541Srgrimes#define LOG_RDPRI (PZERO + 1) 511541Srgrimes 521541Srgrimes#define LOG_ASYNC 0x04 531541Srgrimes#define LOG_RDWAIT 0x08 541541Srgrimes 551541Srgrimesstruct logsoftc { 561541Srgrimes int sc_state; /* see above for possibilities */ 571541Srgrimes struct selinfo sc_selp; /* process waiting on select call */ 581541Srgrimes int sc_pgid; /* process/group for async I/O */ 591541Srgrimes} logsoftc; 601541Srgrimes 611541Srgrimesint log_open; /* also used in log() */ 621541Srgrimes 631541Srgrimes/*ARGSUSED*/ 641549Srgrimesint 651541Srgrimeslogopen(dev, flags, mode, p) 661541Srgrimes dev_t dev; 671541Srgrimes int flags, mode; 681541Srgrimes struct proc *p; 691541Srgrimes{ 701541Srgrimes register struct msgbuf *mbp = msgbufp; 711541Srgrimes 721541Srgrimes if (log_open) 731541Srgrimes return (EBUSY); 741541Srgrimes log_open = 1; 751541Srgrimes logsoftc.sc_pgid = p->p_pid; /* signal process only */ 761541Srgrimes return (0); 771541Srgrimes} 781541Srgrimes 791541Srgrimes/*ARGSUSED*/ 801549Srgrimesint 811541Srgrimeslogclose(dev, flag, mode, p) 821541Srgrimes dev_t dev; 831541Srgrimes int flag, mode; 841541Srgrimes struct proc *p; 851541Srgrimes{ 861541Srgrimes 871541Srgrimes log_open = 0; 881541Srgrimes logsoftc.sc_state = 0; 891541Srgrimes return (0); 901541Srgrimes} 911541Srgrimes 921541Srgrimes/*ARGSUSED*/ 931549Srgrimesint 941541Srgrimeslogread(dev, uio, flag) 951541Srgrimes dev_t dev; 961541Srgrimes struct uio *uio; 971541Srgrimes int flag; 981541Srgrimes{ 991541Srgrimes register struct msgbuf *mbp = msgbufp; 1001541Srgrimes register long l; 1011541Srgrimes register int s; 1021541Srgrimes int error = 0; 1031541Srgrimes 1041541Srgrimes s = splhigh(); 1051541Srgrimes while (mbp->msg_bufr == mbp->msg_bufx) { 1061541Srgrimes if (flag & IO_NDELAY) { 1071541Srgrimes splx(s); 1081541Srgrimes return (EWOULDBLOCK); 1091541Srgrimes } 1101541Srgrimes logsoftc.sc_state |= LOG_RDWAIT; 1113098Sphk if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 1123098Sphk "klog", 0))) { 1131541Srgrimes splx(s); 1141541Srgrimes return (error); 1151541Srgrimes } 1161541Srgrimes } 1171541Srgrimes splx(s); 1181541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1191541Srgrimes 1201541Srgrimes while (uio->uio_resid > 0) { 1211541Srgrimes l = mbp->msg_bufx - mbp->msg_bufr; 1221541Srgrimes if (l < 0) 1231541Srgrimes l = MSG_BSIZE - mbp->msg_bufr; 1241541Srgrimes l = min(l, uio->uio_resid); 1251541Srgrimes if (l == 0) 1261541Srgrimes break; 1271541Srgrimes error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], 1281541Srgrimes (int)l, uio); 1291541Srgrimes if (error) 1301541Srgrimes break; 1311541Srgrimes mbp->msg_bufr += l; 1329979Sdg if (mbp->msg_bufr >= MSG_BSIZE) 1331541Srgrimes mbp->msg_bufr = 0; 1341541Srgrimes } 1351541Srgrimes return (error); 1361541Srgrimes} 1371541Srgrimes 1381541Srgrimes/*ARGSUSED*/ 1391549Srgrimesint 1401541Srgrimeslogselect(dev, rw, p) 1411541Srgrimes dev_t dev; 1421541Srgrimes int rw; 1431541Srgrimes struct proc *p; 1441541Srgrimes{ 1451541Srgrimes int s = splhigh(); 1461541Srgrimes 1471541Srgrimes switch (rw) { 1481541Srgrimes 1491541Srgrimes case FREAD: 1501541Srgrimes if (msgbufp->msg_bufr != msgbufp->msg_bufx) { 1511541Srgrimes splx(s); 1521541Srgrimes return (1); 1531541Srgrimes } 1541541Srgrimes selrecord(p, &logsoftc.sc_selp); 1551541Srgrimes break; 1561541Srgrimes } 1571541Srgrimes splx(s); 1581541Srgrimes return (0); 1591541Srgrimes} 1601541Srgrimes 1611549Srgrimesvoid 1621541Srgrimeslogwakeup() 1631541Srgrimes{ 1641541Srgrimes struct proc *p; 1651541Srgrimes 1661541Srgrimes if (!log_open) 1671541Srgrimes return; 1681541Srgrimes selwakeup(&logsoftc.sc_selp); 1691541Srgrimes if (logsoftc.sc_state & LOG_ASYNC) { 1701541Srgrimes if (logsoftc.sc_pgid < 0) 1718876Srgrimes gsignal(-logsoftc.sc_pgid, SIGIO); 1723098Sphk else if ((p = pfind(logsoftc.sc_pgid))) 1731541Srgrimes psignal(p, SIGIO); 1741541Srgrimes } 1751541Srgrimes if (logsoftc.sc_state & LOG_RDWAIT) { 1761541Srgrimes wakeup((caddr_t)msgbufp); 1771541Srgrimes logsoftc.sc_state &= ~LOG_RDWAIT; 1781541Srgrimes } 1791541Srgrimes} 1801541Srgrimes 1811541Srgrimes/*ARGSUSED*/ 1821549Srgrimesint 1831541Srgrimeslogioctl(dev, com, data, flag, p) 1841541Srgrimes dev_t dev; 1851541Srgrimes int com; 1861541Srgrimes caddr_t data; 1871541Srgrimes int flag; 1881541Srgrimes struct proc *p; 1891541Srgrimes{ 1901541Srgrimes long l; 1911541Srgrimes int s; 1921541Srgrimes 1931541Srgrimes switch (com) { 1941541Srgrimes 1951541Srgrimes /* return number of characters immediately available */ 1961541Srgrimes case FIONREAD: 1971541Srgrimes s = splhigh(); 1981541Srgrimes l = msgbufp->msg_bufx - msgbufp->msg_bufr; 1991541Srgrimes splx(s); 2001541Srgrimes if (l < 0) 2011541Srgrimes l += MSG_BSIZE; 2021541Srgrimes *(int *)data = l; 2031541Srgrimes break; 2041541Srgrimes 2051541Srgrimes case FIONBIO: 2061541Srgrimes break; 2071541Srgrimes 2081541Srgrimes case FIOASYNC: 2091541Srgrimes if (*(int *)data) 2101541Srgrimes logsoftc.sc_state |= LOG_ASYNC; 2111541Srgrimes else 2121541Srgrimes logsoftc.sc_state &= ~LOG_ASYNC; 2131541Srgrimes break; 2141541Srgrimes 2151541Srgrimes case TIOCSPGRP: 2161541Srgrimes logsoftc.sc_pgid = *(int *)data; 2171541Srgrimes break; 2181541Srgrimes 2191541Srgrimes case TIOCGPGRP: 2201541Srgrimes *(int *)data = logsoftc.sc_pgid; 2211541Srgrimes break; 2221541Srgrimes 2231541Srgrimes default: 2248161Sjkh return (ENOTTY); 2251541Srgrimes } 2261541Srgrimes return (0); 2271541Srgrimes} 228