subr_log.c revision 12675
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
3412675Sjulian * $Id: subr_log.c,v 1.13 1995/12/02 18:58:52 bde Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Error log buffer for kernel printf's.
391541Srgrimes */
401541Srgrimes
411541Srgrimes#include <sys/param.h>
421541Srgrimes#include <sys/systm.h>
4312577Sbde#include <sys/conf.h>
441541Srgrimes#include <sys/proc.h>
451541Srgrimes#include <sys/vnode.h>
461541Srgrimes#include <sys/ioctl.h>
471541Srgrimes#include <sys/msgbuf.h>
481541Srgrimes#include <sys/file.h>
493308Sphk#include <sys/signalvar.h>
5012517Sjulian#include <sys/kernel.h>
5112517Sjulian#ifdef DEVFS
5212517Sjulian#include <sys/devfsext.h>
5312517Sjulian#endif /*DEVFS*/
5412517Sjulian
551541Srgrimes#define LOG_RDPRI	(PZERO + 1)
561541Srgrimes
571541Srgrimes#define LOG_ASYNC	0x04
581541Srgrimes#define LOG_RDWAIT	0x08
591541Srgrimes
6012675Sjulianstatic	d_open_t	logopen;
6112675Sjulianstatic	d_close_t	logclose;
6212675Sjulianstatic	d_read_t	logread;
6312675Sjulianstatic	d_ioctl_t	logioctl;
6412675Sjulianstatic	d_select_t	logselect;
6512675Sjulian
6612675Sjulian#define CDEV_MAJOR 7
6712675Sjulianstruct cdevsw log_cdevsw =
6812675Sjulian	{ logopen,	logclose,	logread,	nowrite,	/*7*/
6912675Sjulian	  logioctl,	nostop,		nullreset,	nodevtotty,/* klog */
7012675Sjulian	  logselect,	nommap,		NULL,	"log",	NULL,	-1 };
7112675Sjulian
721541Srgrimesstruct logsoftc {
731541Srgrimes	int	sc_state;		/* see above for possibilities */
741541Srgrimes	struct	selinfo sc_selp;	/* process waiting on select call */
751541Srgrimes	int	sc_pgid;		/* process/group for async I/O */
761541Srgrimes} logsoftc;
771541Srgrimes
781541Srgrimesint	log_open;			/* also used in log() */
791541Srgrimes
801541Srgrimes/*ARGSUSED*/
8112675Sjulianstatic	int
821541Srgrimeslogopen(dev, flags, mode, p)
831541Srgrimes	dev_t dev;
841541Srgrimes	int flags, mode;
851541Srgrimes	struct proc *p;
861541Srgrimes{
871541Srgrimes	if (log_open)
881541Srgrimes		return (EBUSY);
891541Srgrimes	log_open = 1;
901541Srgrimes	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
911541Srgrimes	return (0);
921541Srgrimes}
931541Srgrimes
941541Srgrimes/*ARGSUSED*/
9512675Sjulianstatic	int
961541Srgrimeslogclose(dev, flag, mode, p)
971541Srgrimes	dev_t dev;
981541Srgrimes	int flag, mode;
991541Srgrimes	struct proc *p;
1001541Srgrimes{
1011541Srgrimes
1021541Srgrimes	log_open = 0;
1031541Srgrimes	logsoftc.sc_state = 0;
1041541Srgrimes	return (0);
1051541Srgrimes}
1061541Srgrimes
1071541Srgrimes/*ARGSUSED*/
10812675Sjulianstatic	int
1091541Srgrimeslogread(dev, uio, flag)
1101541Srgrimes	dev_t dev;
1111541Srgrimes	struct uio *uio;
1121541Srgrimes	int flag;
1131541Srgrimes{
1141541Srgrimes	register struct msgbuf *mbp = msgbufp;
1151541Srgrimes	register long l;
1161541Srgrimes	register int s;
1171541Srgrimes	int error = 0;
1181541Srgrimes
1191541Srgrimes	s = splhigh();
1201541Srgrimes	while (mbp->msg_bufr == mbp->msg_bufx) {
1211541Srgrimes		if (flag & IO_NDELAY) {
1221541Srgrimes			splx(s);
1231541Srgrimes			return (EWOULDBLOCK);
1241541Srgrimes		}
1251541Srgrimes		logsoftc.sc_state |= LOG_RDWAIT;
1263098Sphk		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
1273098Sphk		    "klog", 0))) {
1281541Srgrimes			splx(s);
1291541Srgrimes			return (error);
1301541Srgrimes		}
1311541Srgrimes	}
1321541Srgrimes	splx(s);
1331541Srgrimes	logsoftc.sc_state &= ~LOG_RDWAIT;
1341541Srgrimes
1351541Srgrimes	while (uio->uio_resid > 0) {
1361541Srgrimes		l = mbp->msg_bufx - mbp->msg_bufr;
1371541Srgrimes		if (l < 0)
1381541Srgrimes			l = MSG_BSIZE - mbp->msg_bufr;
1391541Srgrimes		l = min(l, uio->uio_resid);
1401541Srgrimes		if (l == 0)
1411541Srgrimes			break;
1421541Srgrimes		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
1431541Srgrimes			(int)l, uio);
1441541Srgrimes		if (error)
1451541Srgrimes			break;
1461541Srgrimes		mbp->msg_bufr += l;
1479979Sdg		if (mbp->msg_bufr >= MSG_BSIZE)
1481541Srgrimes			mbp->msg_bufr = 0;
1491541Srgrimes	}
1501541Srgrimes	return (error);
1511541Srgrimes}
1521541Srgrimes
1531541Srgrimes/*ARGSUSED*/
15412675Sjulianstatic	int
1551541Srgrimeslogselect(dev, rw, p)
1561541Srgrimes	dev_t dev;
1571541Srgrimes	int rw;
1581541Srgrimes	struct proc *p;
1591541Srgrimes{
1601541Srgrimes	int s = splhigh();
1611541Srgrimes
1621541Srgrimes	switch (rw) {
1631541Srgrimes
1641541Srgrimes	case FREAD:
1651541Srgrimes		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
1661541Srgrimes			splx(s);
1671541Srgrimes			return (1);
1681541Srgrimes		}
1691541Srgrimes		selrecord(p, &logsoftc.sc_selp);
1701541Srgrimes		break;
1711541Srgrimes	}
1721541Srgrimes	splx(s);
1731541Srgrimes	return (0);
1741541Srgrimes}
1751541Srgrimes
1761549Srgrimesvoid
1771541Srgrimeslogwakeup()
1781541Srgrimes{
1791541Srgrimes	struct proc *p;
1801541Srgrimes
1811541Srgrimes	if (!log_open)
1821541Srgrimes		return;
1831541Srgrimes	selwakeup(&logsoftc.sc_selp);
1841541Srgrimes	if (logsoftc.sc_state & LOG_ASYNC) {
1851541Srgrimes		if (logsoftc.sc_pgid < 0)
1868876Srgrimes			gsignal(-logsoftc.sc_pgid, SIGIO);
1873098Sphk		else if ((p = pfind(logsoftc.sc_pgid)))
1881541Srgrimes			psignal(p, SIGIO);
1891541Srgrimes	}
1901541Srgrimes	if (logsoftc.sc_state & LOG_RDWAIT) {
1911541Srgrimes		wakeup((caddr_t)msgbufp);
1921541Srgrimes		logsoftc.sc_state &= ~LOG_RDWAIT;
1931541Srgrimes	}
1941541Srgrimes}
1951541Srgrimes
1961541Srgrimes/*ARGSUSED*/
19712675Sjulianstatic	int
1981541Srgrimeslogioctl(dev, com, data, flag, p)
1991541Srgrimes	dev_t dev;
2001541Srgrimes	int com;
2011541Srgrimes	caddr_t data;
2021541Srgrimes	int flag;
2031541Srgrimes	struct proc *p;
2041541Srgrimes{
2051541Srgrimes	long l;
2061541Srgrimes	int s;
2071541Srgrimes
2081541Srgrimes	switch (com) {
2091541Srgrimes
2101541Srgrimes	/* return number of characters immediately available */
2111541Srgrimes	case FIONREAD:
2121541Srgrimes		s = splhigh();
2131541Srgrimes		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
2141541Srgrimes		splx(s);
2151541Srgrimes		if (l < 0)
2161541Srgrimes			l += MSG_BSIZE;
2171541Srgrimes		*(int *)data = l;
2181541Srgrimes		break;
2191541Srgrimes
2201541Srgrimes	case FIONBIO:
2211541Srgrimes		break;
2221541Srgrimes
2231541Srgrimes	case FIOASYNC:
2241541Srgrimes		if (*(int *)data)
2251541Srgrimes			logsoftc.sc_state |= LOG_ASYNC;
2261541Srgrimes		else
2271541Srgrimes			logsoftc.sc_state &= ~LOG_ASYNC;
2281541Srgrimes		break;
2291541Srgrimes
2301541Srgrimes	case TIOCSPGRP:
2311541Srgrimes		logsoftc.sc_pgid = *(int *)data;
2321541Srgrimes		break;
2331541Srgrimes
2341541Srgrimes	case TIOCGPGRP:
2351541Srgrimes		*(int *)data = logsoftc.sc_pgid;
2361541Srgrimes		break;
2371541Srgrimes
2381541Srgrimes	default:
2398161Sjkh		return (ENOTTY);
2401541Srgrimes	}
2411541Srgrimes	return (0);
2421541Srgrimes}
24312517Sjulian
24412517Sjulianstatic log_devsw_installed = 0;
24512675Sjulianstatic	void	*log_devfs_token;
24612517Sjulian
24712675Sjulianstatic void
24812675Sjulianlog_drvinit(void *unused)
24912517Sjulian{
25012517Sjulian	dev_t dev;
25112517Sjulian
25212517Sjulian	if( ! log_devsw_installed ) {
25312517Sjulian		dev = makedev(CDEV_MAJOR,0);
25412517Sjulian		cdevsw_add(&dev,&log_cdevsw,NULL);
25512517Sjulian		log_devsw_installed = 1;
25612517Sjulian#ifdef DEVFS
25712675Sjulian		log_devfs_token = devfs_add_devsw(
25812675Sjulian			"/", "log", &log_cdevsw, 0, DV_CHR, 0, 0, 0600);
25912521Sjulian#endif
26012517Sjulian    	}
26112517Sjulian}
26212517Sjulian
26312517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
26412517Sjulian
26512517Sjulian
266