subr_log.c revision 1549
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
341541Srgrimes */
351541Srgrimes
361541Srgrimes/*
371541Srgrimes * Error log buffer for kernel printf's.
381541Srgrimes */
391541Srgrimes
401541Srgrimes#include <sys/param.h>
411541Srgrimes#include <sys/systm.h>
421541Srgrimes#include <sys/proc.h>
431541Srgrimes#include <sys/vnode.h>
441541Srgrimes#include <sys/ioctl.h>
451541Srgrimes#include <sys/msgbuf.h>
461541Srgrimes#include <sys/file.h>
471541Srgrimes
481541Srgrimes#define LOG_RDPRI	(PZERO + 1)
491541Srgrimes
501541Srgrimes#define LOG_ASYNC	0x04
511541Srgrimes#define LOG_RDWAIT	0x08
521541Srgrimes
531541Srgrimesstruct logsoftc {
541541Srgrimes	int	sc_state;		/* see above for possibilities */
551541Srgrimes	struct	selinfo sc_selp;	/* process waiting on select call */
561541Srgrimes	int	sc_pgid;		/* process/group for async I/O */
571541Srgrimes} logsoftc;
581541Srgrimes
591541Srgrimesint	log_open;			/* also used in log() */
601541Srgrimes
611541Srgrimes/*ARGSUSED*/
621549Srgrimesint
631541Srgrimeslogopen(dev, flags, mode, p)
641541Srgrimes	dev_t dev;
651541Srgrimes	int flags, mode;
661541Srgrimes	struct proc *p;
671541Srgrimes{
681541Srgrimes	register struct msgbuf *mbp = msgbufp;
691541Srgrimes
701541Srgrimes	if (log_open)
711541Srgrimes		return (EBUSY);
721541Srgrimes	log_open = 1;
731541Srgrimes	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
741541Srgrimes	/*
751541Srgrimes	 * Potential race here with putchar() but since putchar should be
761541Srgrimes	 * called by autoconf, msg_magic should be initialized by the time
771541Srgrimes	 * we get here.
781541Srgrimes	 */
791541Srgrimes	if (mbp->msg_magic != MSG_MAGIC) {
801541Srgrimes		register int i;
811541Srgrimes
821541Srgrimes		mbp->msg_magic = MSG_MAGIC;
831541Srgrimes		mbp->msg_bufx = mbp->msg_bufr = 0;
841541Srgrimes		for (i=0; i < MSG_BSIZE; i++)
851541Srgrimes			mbp->msg_bufc[i] = 0;
861541Srgrimes	}
871541Srgrimes	return (0);
881541Srgrimes}
891541Srgrimes
901541Srgrimes/*ARGSUSED*/
911549Srgrimesint
921541Srgrimeslogclose(dev, flag, mode, p)
931541Srgrimes	dev_t dev;
941541Srgrimes	int flag, mode;
951541Srgrimes	struct proc *p;
961541Srgrimes{
971541Srgrimes
981541Srgrimes	log_open = 0;
991541Srgrimes	logsoftc.sc_state = 0;
1001541Srgrimes	return (0);
1011541Srgrimes}
1021541Srgrimes
1031541Srgrimes/*ARGSUSED*/
1041549Srgrimesint
1051541Srgrimeslogread(dev, uio, flag)
1061541Srgrimes	dev_t dev;
1071541Srgrimes	struct uio *uio;
1081541Srgrimes	int flag;
1091541Srgrimes{
1101541Srgrimes	register struct msgbuf *mbp = msgbufp;
1111541Srgrimes	register long l;
1121541Srgrimes	register int s;
1131541Srgrimes	int error = 0;
1141541Srgrimes
1151541Srgrimes	s = splhigh();
1161541Srgrimes	while (mbp->msg_bufr == mbp->msg_bufx) {
1171541Srgrimes		if (flag & IO_NDELAY) {
1181541Srgrimes			splx(s);
1191541Srgrimes			return (EWOULDBLOCK);
1201541Srgrimes		}
1211541Srgrimes		logsoftc.sc_state |= LOG_RDWAIT;
1221541Srgrimes		if (error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
1231541Srgrimes		    "klog", 0)) {
1241541Srgrimes			splx(s);
1251541Srgrimes			return (error);
1261541Srgrimes		}
1271541Srgrimes	}
1281541Srgrimes	splx(s);
1291541Srgrimes	logsoftc.sc_state &= ~LOG_RDWAIT;
1301541Srgrimes
1311541Srgrimes	while (uio->uio_resid > 0) {
1321541Srgrimes		l = mbp->msg_bufx - mbp->msg_bufr;
1331541Srgrimes		if (l < 0)
1341541Srgrimes			l = MSG_BSIZE - mbp->msg_bufr;
1351541Srgrimes		l = min(l, uio->uio_resid);
1361541Srgrimes		if (l == 0)
1371541Srgrimes			break;
1381541Srgrimes		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
1391541Srgrimes			(int)l, uio);
1401541Srgrimes		if (error)
1411541Srgrimes			break;
1421541Srgrimes		mbp->msg_bufr += l;
1431541Srgrimes		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
1441541Srgrimes			mbp->msg_bufr = 0;
1451541Srgrimes	}
1461541Srgrimes	return (error);
1471541Srgrimes}
1481541Srgrimes
1491541Srgrimes/*ARGSUSED*/
1501549Srgrimesint
1511541Srgrimeslogselect(dev, rw, p)
1521541Srgrimes	dev_t dev;
1531541Srgrimes	int rw;
1541541Srgrimes	struct proc *p;
1551541Srgrimes{
1561541Srgrimes	int s = splhigh();
1571541Srgrimes
1581541Srgrimes	switch (rw) {
1591541Srgrimes
1601541Srgrimes	case FREAD:
1611541Srgrimes		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
1621541Srgrimes			splx(s);
1631541Srgrimes			return (1);
1641541Srgrimes		}
1651541Srgrimes		selrecord(p, &logsoftc.sc_selp);
1661541Srgrimes		break;
1671541Srgrimes	}
1681541Srgrimes	splx(s);
1691541Srgrimes	return (0);
1701541Srgrimes}
1711541Srgrimes
1721549Srgrimesvoid
1731541Srgrimeslogwakeup()
1741541Srgrimes{
1751541Srgrimes	struct proc *p;
1761541Srgrimes
1771541Srgrimes	if (!log_open)
1781541Srgrimes		return;
1791541Srgrimes	selwakeup(&logsoftc.sc_selp);
1801541Srgrimes	if (logsoftc.sc_state & LOG_ASYNC) {
1811541Srgrimes		if (logsoftc.sc_pgid < 0)
1821541Srgrimes			gsignal(-logsoftc.sc_pgid, SIGIO);
1831541Srgrimes		else if (p = pfind(logsoftc.sc_pgid))
1841541Srgrimes			psignal(p, SIGIO);
1851541Srgrimes	}
1861541Srgrimes	if (logsoftc.sc_state & LOG_RDWAIT) {
1871541Srgrimes		wakeup((caddr_t)msgbufp);
1881541Srgrimes		logsoftc.sc_state &= ~LOG_RDWAIT;
1891541Srgrimes	}
1901541Srgrimes}
1911541Srgrimes
1921541Srgrimes/*ARGSUSED*/
1931549Srgrimesint
1941541Srgrimeslogioctl(dev, com, data, flag, p)
1951541Srgrimes	dev_t dev;
1961541Srgrimes	int com;
1971541Srgrimes	caddr_t data;
1981541Srgrimes	int flag;
1991541Srgrimes	struct proc *p;
2001541Srgrimes{
2011541Srgrimes	long l;
2021541Srgrimes	int s;
2031541Srgrimes
2041541Srgrimes	switch (com) {
2051541Srgrimes
2061541Srgrimes	/* return number of characters immediately available */
2071541Srgrimes	case FIONREAD:
2081541Srgrimes		s = splhigh();
2091541Srgrimes		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
2101541Srgrimes		splx(s);
2111541Srgrimes		if (l < 0)
2121541Srgrimes			l += MSG_BSIZE;
2131541Srgrimes		*(int *)data = l;
2141541Srgrimes		break;
2151541Srgrimes
2161541Srgrimes	case FIONBIO:
2171541Srgrimes		break;
2181541Srgrimes
2191541Srgrimes	case FIOASYNC:
2201541Srgrimes		if (*(int *)data)
2211541Srgrimes			logsoftc.sc_state |= LOG_ASYNC;
2221541Srgrimes		else
2231541Srgrimes			logsoftc.sc_state &= ~LOG_ASYNC;
2241541Srgrimes		break;
2251541Srgrimes
2261541Srgrimes	case TIOCSPGRP:
2271541Srgrimes		logsoftc.sc_pgid = *(int *)data;
2281541Srgrimes		break;
2291541Srgrimes
2301541Srgrimes	case TIOCGPGRP:
2311541Srgrimes		*(int *)data = logsoftc.sc_pgid;
2321541Srgrimes		break;
2331541Srgrimes
2341541Srgrimes	default:
2351541Srgrimes		return (-1);
2361541Srgrimes	}
2371541Srgrimes	return (0);
2381541Srgrimes}
239