subr_log.c revision 50254
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
3450254Sphk * $Id: subr_log.c,v 1.36 1999/05/31 11:27:35 phk 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>
4624206Sbde#include <sys/filio.h>
4724206Sbde#include <sys/ttycom.h>
481541Srgrimes#include <sys/msgbuf.h>
493308Sphk#include <sys/signalvar.h>
5012517Sjulian#include <sys/kernel.h>
5129357Speter#include <sys/poll.h>
5241086Struckman#include <sys/filedesc.h>
5312517Sjulian
541541Srgrimes#define LOG_RDPRI	(PZERO + 1)
551541Srgrimes
561541Srgrimes#define LOG_ASYNC	0x04
571541Srgrimes#define LOG_RDWAIT	0x08
581541Srgrimes
5912675Sjulianstatic	d_open_t	logopen;
6012675Sjulianstatic	d_close_t	logclose;
6112675Sjulianstatic	d_read_t	logread;
6212675Sjulianstatic	d_ioctl_t	logioctl;
6329357Speterstatic	d_poll_t	logpoll;
6412675Sjulian
6512675Sjulian#define CDEV_MAJOR 7
6647625Sphkstatic struct cdevsw log_cdevsw = {
6747625Sphk	/* open */	logopen,
6847625Sphk	/* close */	logclose,
6947625Sphk	/* read */	logread,
7047625Sphk	/* write */	nowrite,
7147625Sphk	/* ioctl */	logioctl,
7247625Sphk	/* stop */	nostop,
7347625Sphk	/* reset */	noreset,
7447625Sphk	/* devtotty */	nodevtotty,
7547625Sphk	/* poll */	logpoll,
7647625Sphk	/* mmap */	nommap,
7747625Sphk	/* strategy */	nostrategy,
7847625Sphk	/* name */	"log",
7947625Sphk	/* parms */	noparms,
8047625Sphk	/* maj */	CDEV_MAJOR,
8147625Sphk	/* dump */	nodump,
8247625Sphk	/* psize */	nopsize,
8347625Sphk	/* flags */	0,
8447625Sphk	/* maxio */	0,
8547625Sphk	/* bmaj */	-1
8647625Sphk};
8712675Sjulian
8812819Sphkstatic struct logsoftc {
891541Srgrimes	int	sc_state;		/* see above for possibilities */
901541Srgrimes	struct	selinfo sc_selp;	/* process waiting on select call */
9141087Struckman	struct  sigio *sc_sigio;	/* information for async I/O */
921541Srgrimes} logsoftc;
931541Srgrimes
941541Srgrimesint	log_open;			/* also used in log() */
951541Srgrimes
961541Srgrimes/*ARGSUSED*/
9712675Sjulianstatic	int
981541Srgrimeslogopen(dev, flags, mode, p)
991541Srgrimes	dev_t dev;
1001541Srgrimes	int flags, mode;
1011541Srgrimes	struct proc *p;
1021541Srgrimes{
1031541Srgrimes	if (log_open)
1041541Srgrimes		return (EBUSY);
1051541Srgrimes	log_open = 1;
10641086Struckman	fsetown(p->p_pid, &logsoftc.sc_sigio);	/* signal process only */
1071541Srgrimes	return (0);
1081541Srgrimes}
1091541Srgrimes
1101541Srgrimes/*ARGSUSED*/
11112675Sjulianstatic	int
1121541Srgrimeslogclose(dev, flag, mode, p)
1131541Srgrimes	dev_t dev;
1141541Srgrimes	int flag, mode;
1151541Srgrimes	struct proc *p;
1161541Srgrimes{
1171541Srgrimes
1181541Srgrimes	log_open = 0;
1191541Srgrimes	logsoftc.sc_state = 0;
12041086Struckman	funsetown(logsoftc.sc_sigio);
1211541Srgrimes	return (0);
1221541Srgrimes}
1231541Srgrimes
1241541Srgrimes/*ARGSUSED*/
12512675Sjulianstatic	int
1261541Srgrimeslogread(dev, uio, flag)
1271541Srgrimes	dev_t dev;
1281541Srgrimes	struct uio *uio;
1291541Srgrimes	int flag;
1301541Srgrimes{
1311541Srgrimes	register struct msgbuf *mbp = msgbufp;
1321541Srgrimes	register long l;
1331541Srgrimes	register int s;
1341541Srgrimes	int error = 0;
1351541Srgrimes
1361541Srgrimes	s = splhigh();
1371541Srgrimes	while (mbp->msg_bufr == mbp->msg_bufx) {
1381541Srgrimes		if (flag & IO_NDELAY) {
1391541Srgrimes			splx(s);
1401541Srgrimes			return (EWOULDBLOCK);
1411541Srgrimes		}
1421541Srgrimes		logsoftc.sc_state |= LOG_RDWAIT;
1433098Sphk		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
1443098Sphk		    "klog", 0))) {
1451541Srgrimes			splx(s);
1461541Srgrimes			return (error);
1471541Srgrimes		}
1481541Srgrimes	}
1491541Srgrimes	splx(s);
1501541Srgrimes	logsoftc.sc_state &= ~LOG_RDWAIT;
1511541Srgrimes
1521541Srgrimes	while (uio->uio_resid > 0) {
1531541Srgrimes		l = mbp->msg_bufx - mbp->msg_bufr;
1541541Srgrimes		if (l < 0)
15536179Sphk			l = mbp->msg_size - mbp->msg_bufr;
1561541Srgrimes		l = min(l, uio->uio_resid);
1571541Srgrimes		if (l == 0)
1581541Srgrimes			break;
15936179Sphk		error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
16036179Sphk		    (int)l, uio);
1611541Srgrimes		if (error)
1621541Srgrimes			break;
1631541Srgrimes		mbp->msg_bufr += l;
16436179Sphk		if (mbp->msg_bufr >= mbp->msg_size)
1651541Srgrimes			mbp->msg_bufr = 0;
1661541Srgrimes	}
1671541Srgrimes	return (error);
1681541Srgrimes}
1691541Srgrimes
1701541Srgrimes/*ARGSUSED*/
17112675Sjulianstatic	int
17229357Speterlogpoll(dev, events, p)
1731541Srgrimes	dev_t dev;
17429357Speter	int events;
1751541Srgrimes	struct proc *p;
1761541Srgrimes{
17729357Speter	int s;
17829357Speter	int revents = 0;
1791541Srgrimes
18029357Speter	s = splhigh();
1811541Srgrimes
18246568Speter	if (events & (POLLIN | POLLRDNORM)) {
18329357Speter		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
18429357Speter			revents |= events & (POLLIN | POLLRDNORM);
18529357Speter		else
18629357Speter			selrecord(p, &logsoftc.sc_selp);
18746568Speter	}
1881541Srgrimes	splx(s);
18929357Speter	return (revents);
1901541Srgrimes}
1911541Srgrimes
1921549Srgrimesvoid
1931541Srgrimeslogwakeup()
1941541Srgrimes{
1951541Srgrimes	if (!log_open)
1961541Srgrimes		return;
1971541Srgrimes	selwakeup(&logsoftc.sc_selp);
19841086Struckman	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
19941086Struckman		pgsigio(logsoftc.sc_sigio, SIGIO, 0);
2001541Srgrimes	if (logsoftc.sc_state & LOG_RDWAIT) {
2011541Srgrimes		wakeup((caddr_t)msgbufp);
2021541Srgrimes		logsoftc.sc_state &= ~LOG_RDWAIT;
2031541Srgrimes	}
2041541Srgrimes}
2051541Srgrimes
2061541Srgrimes/*ARGSUSED*/
20712675Sjulianstatic	int
2081541Srgrimeslogioctl(dev, com, data, flag, p)
2091541Srgrimes	dev_t dev;
21036735Sdfr	u_long com;
2111541Srgrimes	caddr_t data;
2121541Srgrimes	int flag;
2131541Srgrimes	struct proc *p;
2141541Srgrimes{
2151541Srgrimes	long l;
2161541Srgrimes	int s;
2171541Srgrimes
2181541Srgrimes	switch (com) {
2191541Srgrimes
2201541Srgrimes	/* return number of characters immediately available */
2211541Srgrimes	case FIONREAD:
2221541Srgrimes		s = splhigh();
2231541Srgrimes		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
2241541Srgrimes		splx(s);
2251541Srgrimes		if (l < 0)
22636179Sphk			l += msgbufp->msg_size;
2271541Srgrimes		*(int *)data = l;
2281541Srgrimes		break;
2291541Srgrimes
2301541Srgrimes	case FIONBIO:
2311541Srgrimes		break;
2321541Srgrimes
2331541Srgrimes	case FIOASYNC:
2341541Srgrimes		if (*(int *)data)
2351541Srgrimes			logsoftc.sc_state |= LOG_ASYNC;
2361541Srgrimes		else
2371541Srgrimes			logsoftc.sc_state &= ~LOG_ASYNC;
2381541Srgrimes		break;
2391541Srgrimes
24041086Struckman	case FIOSETOWN:
24141086Struckman		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
24241086Struckman
24341086Struckman	case FIOGETOWN:
24441086Struckman		*(int *)data = fgetown(logsoftc.sc_sigio);
2451541Srgrimes		break;
2461541Srgrimes
24741086Struckman	/* This is deprecated, FIOSETOWN should be used instead. */
24841086Struckman	case TIOCSPGRP:
24941086Struckman		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
25041086Struckman
25141086Struckman	/* This is deprecated, FIOGETOWN should be used instead */
2521541Srgrimes	case TIOCGPGRP:
25341086Struckman		*(int *)data = -fgetown(logsoftc.sc_sigio);
2541541Srgrimes		break;
2551541Srgrimes
2561541Srgrimes	default:
2578161Sjkh		return (ENOTTY);
2581541Srgrimes	}
2591541Srgrimes	return (0);
2601541Srgrimes}
26112517Sjulian
26212517Sjulian
26329506Sbdestatic void log_drvinit __P((void *unused));
26450254Sphk
26512675Sjulianstatic void
26629506Sbdelog_drvinit(unused)
26729506Sbde	void *unused;
26812517Sjulian{
26950254Sphk	make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
27012517Sjulian}
27112517Sjulian
27212517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
273