xref: /csrg-svn/sys/kern/subr_log.c (revision 17093)
1*17093Sbloom /*	subr_log.c	6.3	84/08/29	*/
216723Sralph 
316723Sralph /*
416723Sralph  * Error log buffer for kernel printf's.
516723Sralph  */
616723Sralph 
7*17093Sbloom #include "param.h"
8*17093Sbloom #include "dir.h"
9*17093Sbloom #include "user.h"
10*17093Sbloom #include "proc.h"
11*17093Sbloom #include "ioctl.h"
12*17093Sbloom #include "msgbuf.h"
13*17093Sbloom #include "file.h"
14*17093Sbloom #include "errno.h"
1516723Sralph 
1616723Sralph #define LOG_RDPRI	(PZERO + 1)
1716723Sralph 
1816723Sralph #define LOG_OPEN	0x01
1916723Sralph #define LOG_NBIO	0x02
2016723Sralph #define LOG_ASYNC	0x04
2116723Sralph #define LOG_RDWAIT	0x08
2216723Sralph 
2316723Sralph struct logsoftc {
2416723Sralph 	int	sc_state;		/* see above for possibilities */
2516723Sralph 	struct	proc *sc_selp;		/* process waiting on select call */
2616723Sralph 	int	sc_pgrp;		/* process group for async I/O */
2716723Sralph } logsoftc;
2816723Sralph 
2916725Sralph #ifdef LOGDEBUG
3016725Sralph /*VARARGS1*/
3116725Sralph xprintf(fmt, x1)
3216725Sralph 	char *fmt;
3316725Sralph 	unsigned x1;
3416725Sralph {
3516725Sralph 
3616725Sralph 	prf(fmt, &x1, 1, (struct tty *)0);
3716725Sralph }
3816725Sralph #endif
3916725Sralph 
4016723Sralph logopen(dev)
4116723Sralph 	dev_t dev;
4216723Sralph {
4316723Sralph 
4416723Sralph #ifdef LOGDEBUG
4516725Sralph 	xprintf("logopen: dev=0x%x\n", dev);
4616723Sralph #endif
4716723Sralph 	if (logsoftc.sc_state & LOG_OPEN)
4816725Sralph 		return (EBUSY);
4916723Sralph 	logsoftc.sc_state |= LOG_OPEN;
5016723Sralph 	logsoftc.sc_selp = 0;
5116723Sralph 	logsoftc.sc_pgrp = u.u_procp->p_pgrp;
5216723Sralph 	/*
5316723Sralph 	 * Potential race here with putchar() but since putchar should be
5416723Sralph 	 * called by autoconf, msg_magic should be initialized by the time
5516723Sralph 	 * we get here.
5616723Sralph 	 */
5716723Sralph 	if (msgbuf.msg_magic != MSG_MAGIC) {
5816723Sralph 		register int i;
5916723Sralph 
6016723Sralph 		msgbuf.msg_magic = MSG_MAGIC;
6116723Sralph 		msgbuf.msg_bufx = msgbuf.msg_bufr = 0;
6216723Sralph 		for (i=0; i < MSG_BSIZE; i++)
6316723Sralph 			msgbuf.msg_bufc[i] = 0;
6416723Sralph 	}
6516725Sralph #ifdef LOGDEBUG
6616725Sralph 	xprintf("logopen: bufx=%d, bufr=%d\n", msgbuf.msg_bufx, msgbuf.msg_bufr);
6716725Sralph #endif
6816725Sralph 	return (0);
6916723Sralph }
7016723Sralph 
7116723Sralph logclose(dev, flag)
7216723Sralph 	dev_t dev;
7316723Sralph {
7416723Sralph 	logsoftc.sc_state = 0;
7516723Sralph 	logsoftc.sc_selp = 0;
7616723Sralph 	logsoftc.sc_pgrp = 0;
7716723Sralph #ifdef LOGDEBUG
7816725Sralph 	xprintf("logclose: dev=0x%x\n", dev);
7916723Sralph #endif
8016723Sralph }
8116723Sralph 
8216723Sralph logread(dev, uio)
8316723Sralph 	dev_t dev;
8416723Sralph 	struct uio *uio;
8516723Sralph {
8616723Sralph 	register long l;
8716723Sralph 	register u_int c;
8816723Sralph 	register struct iovec *iov;
8916723Sralph 	register int s;
9016723Sralph 	int error = 0;
9116723Sralph 
9216723Sralph #ifdef LOGDEBUG
9316725Sralph 	xprintf("logread: dev=0x%x\n", dev);
9416723Sralph #endif
9516723Sralph 
9616723Sralph 	s = splhigh();
9716723Sralph 	while (msgbuf.msg_bufr == msgbuf.msg_bufx) {
9816723Sralph 		if (logsoftc.sc_state & LOG_NBIO) {
9916723Sralph 			splx(s);
10016725Sralph 			return (EWOULDBLOCK);
10116723Sralph 		}
10216723Sralph 		logsoftc.sc_state |= LOG_RDWAIT;
10316723Sralph 		sleep((caddr_t)&msgbuf, LOG_RDPRI);
10416723Sralph 	}
10516723Sralph 	splx(s);
10616723Sralph 	logsoftc.sc_state &= ~LOG_RDWAIT;
10716723Sralph 
10816723Sralph 	while (uio->uio_resid > 0) {
10916723Sralph 		l = msgbuf.msg_bufx - msgbuf.msg_bufr;
11016723Sralph 		if (l < 0)
11116723Sralph 			l = MSG_BSIZE - msgbuf.msg_bufr;
11216723Sralph 		c = min((u_int) l, (u_int)uio->uio_resid);
11316725Sralph #ifdef LOGDEBUG
11416725Sralph 		xprintf("logread: bufx=%d, bufr=%d, l=%d, c=%d\n",
11516725Sralph 			msgbuf.msg_bufx, msgbuf.msg_bufr, l, c);
11616725Sralph #endif
11716725Sralph 		if (c <= 0)
11816725Sralph 			break;
11916723Sralph 		error = uiomove((caddr_t)&msgbuf.msg_bufc[msgbuf.msg_bufr],
12016723Sralph 			(int)c, UIO_READ, uio);
12116723Sralph 		if (error)
12216723Sralph 			break;
12316723Sralph 		msgbuf.msg_bufr += c;
12416723Sralph 		if (msgbuf.msg_bufr < 0 || msgbuf.msg_bufr >= MSG_BSIZE)
12516723Sralph 			msgbuf.msg_bufr = 0;
12616723Sralph 	}
12716725Sralph 	return (error);
12816723Sralph }
12916723Sralph 
13016723Sralph logselect(dev, rw)
13116723Sralph 	dev_t dev;
13216723Sralph 	int rw;
13316723Sralph {
13416723Sralph 	int s = splhigh();
13516723Sralph 
13616723Sralph 	switch (rw) {
13716723Sralph 
13816723Sralph 	case FREAD:
13916723Sralph 		if (msgbuf.msg_bufr != msgbuf.msg_bufx)
14016723Sralph 			goto win;
14116723Sralph #ifdef LOGDEBUG
14216723Sralph 		if (logsoftc.sc_selp)
14316725Sralph 			xprintf("logselect: collision\n");
14416723Sralph #endif
14516723Sralph 		logsoftc.sc_selp = u.u_procp;
14616723Sralph 		break;
14716723Sralph 
14816723Sralph 	case FWRITE:
14916723Sralph #ifdef LOGDEBUG
15016725Sralph 		xprintf("logselect: FWRITE\n");
15116723Sralph #endif
15216723Sralph 		break;
15316723Sralph 	}
15416723Sralph 	splx(s);
15516725Sralph 	return (0);
15616723Sralph win:
15716723Sralph 	splx(s);
15816725Sralph 	return (1);
15916723Sralph }
16016723Sralph 
16116723Sralph logwakeup()
16216723Sralph {
16316723Sralph 
16416723Sralph 	if (logsoftc.sc_selp) {
16516723Sralph 		selwakeup(logsoftc.sc_selp, 0);
16616723Sralph 		logsoftc.sc_selp = 0;
16716723Sralph 	}
16816723Sralph 	if (logsoftc.sc_state & LOG_ASYNC)
16916723Sralph 		gsignal(logsoftc.sc_pgrp, SIGIO);
17016723Sralph 	if (logsoftc.sc_state & LOG_RDWAIT) {
17116723Sralph 		wakeup((caddr_t)&msgbuf);
17216723Sralph 		logsoftc.sc_state &= ~LOG_RDWAIT;
17316723Sralph 	}
17416723Sralph }
17516723Sralph 
17616723Sralph /*ARGSUSED*/
17716723Sralph logioctl(com, data, flag)
17816723Sralph 	caddr_t data;
17916723Sralph {
18016723Sralph 	long l;
18116723Sralph 	int s;
18216723Sralph 
18316723Sralph 	switch (com) {
18416723Sralph 
18516723Sralph 	/* return number of characters immediately available */
18616723Sralph 	case FIONREAD:
18716723Sralph 		s = splhigh();
18816723Sralph 		l = msgbuf.msg_bufx - msgbuf.msg_bufr;
18916723Sralph 		splx(s);
19016723Sralph 		if (l < 0)
19116723Sralph 			l += MSG_BSIZE;
19216723Sralph 		*(off_t *)data = l;
19316723Sralph 		break;
19416723Sralph 
19516723Sralph 	case FIONBIO:
19616723Sralph 		if (*(int *)data)
19716723Sralph 			logsoftc.sc_state |= LOG_NBIO;
19816723Sralph 		else
19916723Sralph 			logsoftc.sc_state &= ~LOG_NBIO;
20016723Sralph 		break;
20116723Sralph 
20216723Sralph 	case FIOASYNC:
20316723Sralph 		if (*(int *)data)
20416723Sralph 			logsoftc.sc_state |= LOG_ASYNC;
20516723Sralph 		else
20616723Sralph 			logsoftc.sc_state &= ~LOG_ASYNC;
20716723Sralph 		break;
20816723Sralph 
20916723Sralph 	case TIOCSPGRP:
21016723Sralph 		logsoftc.sc_pgrp = *(int *)data;
21116723Sralph 		break;
21216723Sralph 
21316723Sralph 	case TIOCGPGRP:
21416723Sralph 		*(int *)data = logsoftc.sc_pgrp;
21516723Sralph 		break;
21616723Sralph 
21716723Sralph 	default:
21816725Sralph 		return (-1);
21916723Sralph 	}
22016725Sralph 	return (0);
22116723Sralph }
222