subr_log.c revision 1.2
1/*	$OpenBSD: subr_log.c,v 1.2 1996/03/03 17:20:00 niklas Exp $	*/
2/*	$NetBSD: subr_log.c,v 1.10 1996/02/09 18:59:58 christos Exp $	*/
3
4/*
5 * Copyright (c) 1982, 1986, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
37 */
38
39/*
40 * Error log buffer for kernel printf's.
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/proc.h>
46#include <sys/vnode.h>
47#include <sys/ioctl.h>
48#include <sys/msgbuf.h>
49#include <sys/file.h>
50#include <sys/signalvar.h>
51#include <sys/syslog.h>
52
53#include <kern/kern_conf.h>
54
55#define LOG_RDPRI	(PZERO + 1)
56
57#define LOG_ASYNC	0x04
58#define LOG_RDWAIT	0x08
59
60struct logsoftc {
61	int	sc_state;		/* see above for possibilities */
62	struct	selinfo sc_selp;	/* process waiting on select call */
63	int	sc_pgid;		/* process/group for async I/O */
64} logsoftc;
65
66int	log_open;			/* also used in log() */
67
68/*ARGSUSED*/
69int
70logopen(dev, flags, mode, p)
71	dev_t dev;
72	int flags, mode;
73	struct proc *p;
74{
75	register struct msgbuf *mbp = msgbufp;
76
77	if (log_open)
78		return (EBUSY);
79	log_open = 1;
80	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
81	/*
82	 * Potential race here with putchar() but since putchar should be
83	 * called by autoconf, msg_magic should be initialized by the time
84	 * we get here.
85	 */
86	if (mbp->msg_magic != MSG_MAGIC) {
87		register int i;
88
89		mbp->msg_magic = MSG_MAGIC;
90		mbp->msg_bufx = mbp->msg_bufr = 0;
91		for (i=0; i < MSG_BSIZE; i++)
92			mbp->msg_bufc[i] = 0;
93	}
94	return (0);
95}
96
97/*ARGSUSED*/
98int
99logclose(dev, flag, mode, p)
100	dev_t dev;
101	int flag, mode;
102	struct proc *p;
103{
104
105	log_open = 0;
106	logsoftc.sc_state = 0;
107	return (0);
108}
109
110/*ARGSUSED*/
111int
112logread(dev, uio, flag)
113	dev_t dev;
114	struct uio *uio;
115	int flag;
116{
117	register struct msgbuf *mbp = msgbufp;
118	register long l;
119	register int s;
120	int error = 0;
121
122	s = splhigh();
123	while (mbp->msg_bufr == mbp->msg_bufx) {
124		if (flag & IO_NDELAY) {
125			splx(s);
126			return (EWOULDBLOCK);
127		}
128		logsoftc.sc_state |= LOG_RDWAIT;
129		error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
130			       "klog", 0);
131		if (error) {
132			splx(s);
133			return (error);
134		}
135	}
136	splx(s);
137	logsoftc.sc_state &= ~LOG_RDWAIT;
138
139	while (uio->uio_resid > 0) {
140		l = mbp->msg_bufx - mbp->msg_bufr;
141		if (l < 0)
142			l = MSG_BSIZE - mbp->msg_bufr;
143		l = min(l, uio->uio_resid);
144		if (l == 0)
145			break;
146		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
147			(int)l, uio);
148		if (error)
149			break;
150		mbp->msg_bufr += l;
151		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
152			mbp->msg_bufr = 0;
153	}
154	return (error);
155}
156
157/*ARGSUSED*/
158int
159logselect(dev, rw, p)
160	dev_t dev;
161	int rw;
162	struct proc *p;
163{
164	int s = splhigh();
165
166	switch (rw) {
167
168	case FREAD:
169		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
170			splx(s);
171			return (1);
172		}
173		selrecord(p, &logsoftc.sc_selp);
174		break;
175	}
176	splx(s);
177	return (0);
178}
179
180void
181logwakeup()
182{
183	struct proc *p;
184
185	if (!log_open)
186		return;
187	selwakeup(&logsoftc.sc_selp);
188	if (logsoftc.sc_state & LOG_ASYNC) {
189		if (logsoftc.sc_pgid < 0)
190			gsignal(-logsoftc.sc_pgid, SIGIO);
191		else if ((p = pfind(logsoftc.sc_pgid)) != NULL)
192			psignal(p, SIGIO);
193	}
194	if (logsoftc.sc_state & LOG_RDWAIT) {
195		wakeup((caddr_t)msgbufp);
196		logsoftc.sc_state &= ~LOG_RDWAIT;
197	}
198}
199
200/*ARGSUSED*/
201int
202logioctl(dev, com, data, flag, p)
203	dev_t dev;
204	u_long com;
205	caddr_t data;
206	int flag;
207	struct proc *p;
208{
209	long l;
210	int s;
211
212	switch (com) {
213
214	/* return number of characters immediately available */
215	case FIONREAD:
216		s = splhigh();
217		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
218		splx(s);
219		if (l < 0)
220			l += MSG_BSIZE;
221		*(int *)data = l;
222		break;
223
224	case FIONBIO:
225		break;
226
227	case FIOASYNC:
228		if (*(int *)data)
229			logsoftc.sc_state |= LOG_ASYNC;
230		else
231			logsoftc.sc_state &= ~LOG_ASYNC;
232		break;
233
234	case TIOCSPGRP:
235		logsoftc.sc_pgid = *(int *)data;
236		break;
237
238	case TIOCGPGRP:
239		*(int *)data = logsoftc.sc_pgid;
240		break;
241
242	default:
243		return (-1);
244	}
245	return (0);
246}
247