subr_log.c revision 8161
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
348161Sjkh * $Id: subr_log.c,v 1.6 1995/04/29 05:09:19 jkh 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	/*
771541Srgrimes	 * Potential race here with putchar() but since putchar should be
781541Srgrimes	 * called by autoconf, msg_magic should be initialized by the time
791541Srgrimes	 * we get here.
801541Srgrimes	 */
811541Srgrimes	if (mbp->msg_magic != MSG_MAGIC) {
821541Srgrimes		register int i;
831541Srgrimes
841541Srgrimes		mbp->msg_magic = MSG_MAGIC;
851541Srgrimes		mbp->msg_bufx = mbp->msg_bufr = 0;
861541Srgrimes		for (i=0; i < MSG_BSIZE; i++)
871541Srgrimes			mbp->msg_bufc[i] = 0;
881541Srgrimes	}
891541Srgrimes	return (0);
901541Srgrimes}
911541Srgrimes
921541Srgrimes/*ARGSUSED*/
931549Srgrimesint
941541Srgrimeslogclose(dev, flag, mode, p)
951541Srgrimes	dev_t dev;
961541Srgrimes	int flag, mode;
971541Srgrimes	struct proc *p;
981541Srgrimes{
991541Srgrimes
1001541Srgrimes	log_open = 0;
1011541Srgrimes	logsoftc.sc_state = 0;
1021541Srgrimes	return (0);
1031541Srgrimes}
1041541Srgrimes
1051541Srgrimes/*ARGSUSED*/
1061549Srgrimesint
1071541Srgrimeslogread(dev, uio, flag)
1081541Srgrimes	dev_t dev;
1091541Srgrimes	struct uio *uio;
1101541Srgrimes	int flag;
1111541Srgrimes{
1121541Srgrimes	register struct msgbuf *mbp = msgbufp;
1131541Srgrimes	register long l;
1141541Srgrimes	register int s;
1151541Srgrimes	int error = 0;
1161541Srgrimes
1171541Srgrimes	s = splhigh();
1181541Srgrimes	while (mbp->msg_bufr == mbp->msg_bufx) {
1191541Srgrimes		if (flag & IO_NDELAY) {
1201541Srgrimes			splx(s);
1211541Srgrimes			return (EWOULDBLOCK);
1221541Srgrimes		}
1231541Srgrimes		logsoftc.sc_state |= LOG_RDWAIT;
1243098Sphk		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
1253098Sphk		    "klog", 0))) {
1261541Srgrimes			splx(s);
1271541Srgrimes			return (error);
1281541Srgrimes		}
1291541Srgrimes	}
1301541Srgrimes	splx(s);
1311541Srgrimes	logsoftc.sc_state &= ~LOG_RDWAIT;
1321541Srgrimes
1331541Srgrimes	while (uio->uio_resid > 0) {
1341541Srgrimes		l = mbp->msg_bufx - mbp->msg_bufr;
1351541Srgrimes		if (l < 0)
1361541Srgrimes			l = MSG_BSIZE - mbp->msg_bufr;
1371541Srgrimes		l = min(l, uio->uio_resid);
1381541Srgrimes		if (l == 0)
1391541Srgrimes			break;
1401541Srgrimes		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
1411541Srgrimes			(int)l, uio);
1421541Srgrimes		if (error)
1431541Srgrimes			break;
1441541Srgrimes		mbp->msg_bufr += l;
1451541Srgrimes		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
1461541Srgrimes			mbp->msg_bufr = 0;
1471541Srgrimes	}
1481541Srgrimes	return (error);
1491541Srgrimes}
1501541Srgrimes
1511541Srgrimes/*ARGSUSED*/
1521549Srgrimesint
1531541Srgrimeslogselect(dev, rw, p)
1541541Srgrimes	dev_t dev;
1551541Srgrimes	int rw;
1561541Srgrimes	struct proc *p;
1571541Srgrimes{
1581541Srgrimes	int s = splhigh();
1591541Srgrimes
1601541Srgrimes	switch (rw) {
1611541Srgrimes
1621541Srgrimes	case FREAD:
1631541Srgrimes		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
1641541Srgrimes			splx(s);
1651541Srgrimes			return (1);
1661541Srgrimes		}
1671541Srgrimes		selrecord(p, &logsoftc.sc_selp);
1681541Srgrimes		break;
1691541Srgrimes	}
1701541Srgrimes	splx(s);
1711541Srgrimes	return (0);
1721541Srgrimes}
1731541Srgrimes
1741549Srgrimesvoid
1751541Srgrimeslogwakeup()
1761541Srgrimes{
1771541Srgrimes	struct proc *p;
1781541Srgrimes
1791541Srgrimes	if (!log_open)
1801541Srgrimes		return;
1811541Srgrimes	selwakeup(&logsoftc.sc_selp);
1821541Srgrimes	if (logsoftc.sc_state & LOG_ASYNC) {
1831541Srgrimes		if (logsoftc.sc_pgid < 0)
1841541Srgrimes			gsignal(-logsoftc.sc_pgid, SIGIO);
1853098Sphk		else if ((p = pfind(logsoftc.sc_pgid)))
1861541Srgrimes			psignal(p, SIGIO);
1871541Srgrimes	}
1881541Srgrimes	if (logsoftc.sc_state & LOG_RDWAIT) {
1891541Srgrimes		wakeup((caddr_t)msgbufp);
1901541Srgrimes		logsoftc.sc_state &= ~LOG_RDWAIT;
1911541Srgrimes	}
1921541Srgrimes}
1931541Srgrimes
1941541Srgrimes/*ARGSUSED*/
1951549Srgrimesint
1961541Srgrimeslogioctl(dev, com, data, flag, p)
1971541Srgrimes	dev_t dev;
1981541Srgrimes	int com;
1991541Srgrimes	caddr_t data;
2001541Srgrimes	int flag;
2011541Srgrimes	struct proc *p;
2021541Srgrimes{
2031541Srgrimes	long l;
2041541Srgrimes	int s;
2051541Srgrimes
2061541Srgrimes	switch (com) {
2071541Srgrimes
2081541Srgrimes	/* return number of characters immediately available */
2091541Srgrimes	case FIONREAD:
2101541Srgrimes		s = splhigh();
2111541Srgrimes		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
2121541Srgrimes		splx(s);
2131541Srgrimes		if (l < 0)
2141541Srgrimes			l += MSG_BSIZE;
2151541Srgrimes		*(int *)data = l;
2161541Srgrimes		break;
2171541Srgrimes
2181541Srgrimes	case FIONBIO:
2191541Srgrimes		break;
2201541Srgrimes
2211541Srgrimes	case FIOASYNC:
2221541Srgrimes		if (*(int *)data)
2231541Srgrimes			logsoftc.sc_state |= LOG_ASYNC;
2241541Srgrimes		else
2251541Srgrimes			logsoftc.sc_state &= ~LOG_ASYNC;
2261541Srgrimes		break;
2271541Srgrimes
2281541Srgrimes	case TIOCSPGRP:
2291541Srgrimes		logsoftc.sc_pgid = *(int *)data;
2301541Srgrimes		break;
2311541Srgrimes
2321541Srgrimes	case TIOCGPGRP:
2331541Srgrimes		*(int *)data = logsoftc.sc_pgid;
2341541Srgrimes		break;
2351541Srgrimes
2361541Srgrimes	default:
2378161Sjkh		return (ENOTTY);
2381541Srgrimes	}
2391541Srgrimes	return (0);
2401541Srgrimes}
241