xref: /csrg-svn/sys/kern/subr_log.c (revision 47540)
123379Smckusick /*
229099Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
345732Smckusick  * All rights reserved.
423379Smckusick  *
545732Smckusick  * %sccs.include.redist.c%
645732Smckusick  *
7*47540Skarels  *	@(#)subr_log.c	7.11 (Berkeley) 03/17/91
823379Smckusick  */
916723Sralph 
1016723Sralph /*
1116723Sralph  * Error log buffer for kernel printf's.
1216723Sralph  */
1316723Sralph 
1417093Sbloom #include "param.h"
1517093Sbloom #include "proc.h"
1643691Smckusick #include "vnode.h"
1717093Sbloom #include "ioctl.h"
1817093Sbloom #include "msgbuf.h"
1917093Sbloom #include "file.h"
2016723Sralph 
2116723Sralph #define LOG_RDPRI	(PZERO + 1)
2216723Sralph 
2316723Sralph #define LOG_ASYNC	0x04
2416723Sralph #define LOG_RDWAIT	0x08
2516723Sralph 
2616723Sralph struct logsoftc {
2716723Sralph 	int	sc_state;		/* see above for possibilities */
2816723Sralph 	struct	proc *sc_selp;		/* process waiting on select call */
2943661Skarels 	int	sc_pgid;		/* process/group for async I/O */
3016723Sralph } logsoftc;
3116723Sralph 
3218365Skarels int	log_open;			/* also used in log() */
3318365Skarels 
3424524Sbloom /*ARGSUSED*/
35*47540Skarels logopen(dev, flags, mode, p)
3616723Sralph 	dev_t dev;
37*47540Skarels 	int flags, mode;
38*47540Skarels 	struct proc *p;
3916723Sralph {
4045732Smckusick 	register struct msgbuf *mbp = msgbufp;
4116723Sralph 
4218365Skarels 	if (log_open)
4316725Sralph 		return (EBUSY);
4418365Skarels 	log_open = 1;
45*47540Skarels 	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
4616723Sralph 	/*
4716723Sralph 	 * Potential race here with putchar() but since putchar should be
4816723Sralph 	 * called by autoconf, msg_magic should be initialized by the time
4916723Sralph 	 * we get here.
5016723Sralph 	 */
5145732Smckusick 	if (mbp->msg_magic != MSG_MAGIC) {
5216723Sralph 		register int i;
5316723Sralph 
5445732Smckusick 		mbp->msg_magic = MSG_MAGIC;
5545732Smckusick 		mbp->msg_bufx = mbp->msg_bufr = 0;
5616723Sralph 		for (i=0; i < MSG_BSIZE; i++)
5745732Smckusick 			mbp->msg_bufc[i] = 0;
5816723Sralph 	}
5916725Sralph 	return (0);
6016723Sralph }
6116723Sralph 
6224524Sbloom /*ARGSUSED*/
6316723Sralph logclose(dev, flag)
6416723Sralph 	dev_t dev;
6516723Sralph {
6618365Skarels 	log_open = 0;
6716723Sralph 	logsoftc.sc_state = 0;
6816723Sralph 	logsoftc.sc_selp = 0;
6916723Sralph }
7016723Sralph 
7124524Sbloom /*ARGSUSED*/
7243661Skarels logread(dev, uio, flag)
7316723Sralph 	dev_t dev;
7416723Sralph 	struct uio *uio;
7543661Skarels 	int flag;
7616723Sralph {
7745732Smckusick 	register struct msgbuf *mbp = msgbufp;
7816723Sralph 	register long l;
7916723Sralph 	register int s;
8016723Sralph 	int error = 0;
8116723Sralph 
8216723Sralph 	s = splhigh();
8345732Smckusick 	while (mbp->msg_bufr == mbp->msg_bufx) {
8443661Skarels 		if (flag & IO_NDELAY) {
8516723Sralph 			splx(s);
8616725Sralph 			return (EWOULDBLOCK);
8716723Sralph 		}
8816723Sralph 		logsoftc.sc_state |= LOG_RDWAIT;
8945732Smckusick 		if (error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
9040811Smarc 		    "klog", 0)) {
9140811Smarc 			splx(s);
9240811Smarc 			return (error);
9340811Smarc 		}
9416723Sralph 	}
9516723Sralph 	splx(s);
9616723Sralph 	logsoftc.sc_state &= ~LOG_RDWAIT;
9716723Sralph 
9816723Sralph 	while (uio->uio_resid > 0) {
9945732Smckusick 		l = mbp->msg_bufx - mbp->msg_bufr;
10016723Sralph 		if (l < 0)
10145732Smckusick 			l = MSG_BSIZE - mbp->msg_bufr;
10226356Skarels 		l = MIN(l, uio->uio_resid);
10326249Skarels 		if (l == 0)
10416725Sralph 			break;
10545732Smckusick 		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
10637728Smckusick 			(int)l, uio);
10716723Sralph 		if (error)
10816723Sralph 			break;
10945732Smckusick 		mbp->msg_bufr += l;
11045732Smckusick 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
11145732Smckusick 			mbp->msg_bufr = 0;
11216723Sralph 	}
11316725Sralph 	return (error);
11416723Sralph }
11516723Sralph 
11624524Sbloom /*ARGSUSED*/
117*47540Skarels logselect(dev, rw, p)
11816723Sralph 	dev_t dev;
11916723Sralph 	int rw;
120*47540Skarels 	struct proc *p;
12116723Sralph {
12216723Sralph 	int s = splhigh();
12316723Sralph 
12416723Sralph 	switch (rw) {
12516723Sralph 
12616723Sralph 	case FREAD:
12745732Smckusick 		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
12826249Skarels 			splx(s);
12926249Skarels 			return (1);
13026249Skarels 		}
131*47540Skarels 		logsoftc.sc_selp = p;
13216723Sralph 		break;
13316723Sralph 	}
13416723Sralph 	splx(s);
13516725Sralph 	return (0);
13616723Sralph }
13716723Sralph 
13816723Sralph logwakeup()
13916723Sralph {
14043661Skarels 	struct proc *p;
14116723Sralph 
14218365Skarels 	if (!log_open)
14318365Skarels 		return;
14416723Sralph 	if (logsoftc.sc_selp) {
14516723Sralph 		selwakeup(logsoftc.sc_selp, 0);
14616723Sralph 		logsoftc.sc_selp = 0;
14716723Sralph 	}
14843661Skarels 	if (logsoftc.sc_state & LOG_ASYNC) {
14943661Skarels 		if (logsoftc.sc_pgid < 0)
15044986Smarc 			gsignal(-logsoftc.sc_pgid, SIGIO);
15143661Skarels 		else if (p = pfind(logsoftc.sc_pgid))
15243661Skarels 			psignal(p, SIGIO);
15343661Skarels 	}
15416723Sralph 	if (logsoftc.sc_state & LOG_RDWAIT) {
15545732Smckusick 		wakeup((caddr_t)msgbufp);
15616723Sralph 		logsoftc.sc_state &= ~LOG_RDWAIT;
15716723Sralph 	}
15816723Sralph }
15916723Sralph 
16016723Sralph /*ARGSUSED*/
16143661Skarels logioctl(dev, com, data, flag)
16216723Sralph 	caddr_t data;
16316723Sralph {
16416723Sralph 	long l;
16516723Sralph 	int s;
16616723Sralph 
16716723Sralph 	switch (com) {
16816723Sralph 
16916723Sralph 	/* return number of characters immediately available */
17016723Sralph 	case FIONREAD:
17116723Sralph 		s = splhigh();
17245732Smckusick 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
17316723Sralph 		splx(s);
17416723Sralph 		if (l < 0)
17516723Sralph 			l += MSG_BSIZE;
17616723Sralph 		*(off_t *)data = l;
17716723Sralph 		break;
17816723Sralph 
17916723Sralph 	case FIONBIO:
18016723Sralph 		break;
18116723Sralph 
18216723Sralph 	case FIOASYNC:
18316723Sralph 		if (*(int *)data)
18416723Sralph 			logsoftc.sc_state |= LOG_ASYNC;
18516723Sralph 		else
18616723Sralph 			logsoftc.sc_state &= ~LOG_ASYNC;
18716723Sralph 		break;
18816723Sralph 
18943661Skarels 	case TIOCSPGRP:
19035810Smarc 		logsoftc.sc_pgid = *(int *)data;
19116723Sralph 		break;
19216723Sralph 
19316723Sralph 	case TIOCGPGRP:
19443661Skarels 		*(int *)data = logsoftc.sc_pgid;
19516723Sralph 		break;
19616723Sralph 
19716723Sralph 	default:
19816725Sralph 		return (-1);
19916723Sralph 	}
20016725Sralph 	return (0);
20116723Sralph }
202