subr_log.c revision 41086
1/*
2 * Copyright (c) 1982, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
34 * $Id: subr_log.c,v 1.30 1998/06/07 17:11:38 dfr Exp $
35 */
36
37/*
38 * Error log buffer for kernel printf's.
39 */
40
41#include "opt_devfs.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/conf.h>
46#include <sys/proc.h>
47#include <sys/vnode.h>
48#include <sys/filio.h>
49#include <sys/ttycom.h>
50#include <sys/msgbuf.h>
51#include <sys/signalvar.h>
52#include <sys/kernel.h>
53#include <sys/poll.h>
54#include <sys/filedesc.h>
55#ifdef DEVFS
56#include <sys/devfsext.h>
57#endif /*DEVFS*/
58
59#define LOG_RDPRI	(PZERO + 1)
60
61#define LOG_ASYNC	0x04
62#define LOG_RDWAIT	0x08
63
64static	d_open_t	logopen;
65static	d_close_t	logclose;
66static	d_read_t	logread;
67static	d_ioctl_t	logioctl;
68static	d_poll_t	logpoll;
69
70#define CDEV_MAJOR 7
71static struct cdevsw log_cdevsw =
72	{ logopen,	logclose,	logread,	nowrite,	/*7*/
73	  logioctl,	nostop,		nullreset,	nodevtotty,/* klog */
74	  logpoll,	nommap,		NULL,	"log",	NULL,	-1 };
75
76static struct logsoftc {
77	int	sc_state;		/* see above for possibilities */
78	struct	selinfo sc_selp;	/* process waiting on select call */
79	struct  sigio *sc_sigio;	/* information for SIGIO */
80} logsoftc;
81
82int	log_open;			/* also used in log() */
83
84/*ARGSUSED*/
85static	int
86logopen(dev, flags, mode, p)
87	dev_t dev;
88	int flags, mode;
89	struct proc *p;
90{
91	if (log_open)
92		return (EBUSY);
93	log_open = 1;
94	fsetown(p->p_pid, &logsoftc.sc_sigio);	/* signal process only */
95	return (0);
96}
97
98/*ARGSUSED*/
99static	int
100logclose(dev, flag, mode, p)
101	dev_t dev;
102	int flag, mode;
103	struct proc *p;
104{
105
106	log_open = 0;
107	logsoftc.sc_state = 0;
108	funsetown(logsoftc.sc_sigio);
109	return (0);
110}
111
112/*ARGSUSED*/
113static	int
114logread(dev, uio, flag)
115	dev_t dev;
116	struct uio *uio;
117	int flag;
118{
119	register struct msgbuf *mbp = msgbufp;
120	register long l;
121	register int s;
122	int error = 0;
123
124	s = splhigh();
125	while (mbp->msg_bufr == mbp->msg_bufx) {
126		if (flag & IO_NDELAY) {
127			splx(s);
128			return (EWOULDBLOCK);
129		}
130		logsoftc.sc_state |= LOG_RDWAIT;
131		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
132		    "klog", 0))) {
133			splx(s);
134			return (error);
135		}
136	}
137	splx(s);
138	logsoftc.sc_state &= ~LOG_RDWAIT;
139
140	while (uio->uio_resid > 0) {
141		l = mbp->msg_bufx - mbp->msg_bufr;
142		if (l < 0)
143			l = mbp->msg_size - mbp->msg_bufr;
144		l = min(l, uio->uio_resid);
145		if (l == 0)
146			break;
147		error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
148		    (int)l, uio);
149		if (error)
150			break;
151		mbp->msg_bufr += l;
152		if (mbp->msg_bufr >= mbp->msg_size)
153			mbp->msg_bufr = 0;
154	}
155	return (error);
156}
157
158/*ARGSUSED*/
159static	int
160logpoll(dev, events, p)
161	dev_t dev;
162	int events;
163	struct proc *p;
164{
165	int s;
166	int revents = 0;
167
168	s = splhigh();
169
170	if (events & (POLLIN | POLLRDNORM))
171		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
172			revents |= events & (POLLIN | POLLRDNORM);
173		else
174			selrecord(p, &logsoftc.sc_selp);
175
176	splx(s);
177	return (revents);
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) && logsoftc.sc_sigio != NULL)
189		pgsigio(logsoftc.sc_sigio, SIGIO, 0);
190	if (logsoftc.sc_state & LOG_RDWAIT) {
191		wakeup((caddr_t)msgbufp);
192		logsoftc.sc_state &= ~LOG_RDWAIT;
193	}
194}
195
196/*ARGSUSED*/
197static	int
198logioctl(dev, com, data, flag, p)
199	dev_t dev;
200	u_long com;
201	caddr_t data;
202	int flag;
203	struct proc *p;
204{
205	long l;
206	int s;
207
208	switch (com) {
209
210	/* return number of characters immediately available */
211	case FIONREAD:
212		s = splhigh();
213		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
214		splx(s);
215		if (l < 0)
216			l += msgbufp->msg_size;
217		*(int *)data = l;
218		break;
219
220	case FIONBIO:
221		break;
222
223	case FIOASYNC:
224		if (*(int *)data)
225			logsoftc.sc_state |= LOG_ASYNC;
226		else
227			logsoftc.sc_state &= ~LOG_ASYNC;
228		break;
229
230	case FIOSETOWN:
231		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
232
233	case FIOGETOWN:
234		*(int *)data = fgetown(logsoftc.sc_sigio);
235		break;
236
237	/* This is deprecated, FIOSETOWN should be used instead. */
238	case TIOCSPGRP:
239		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
240
241	/* This is deprecated, FIOGETOWN should be used instead */
242	case TIOCGPGRP:
243		*(int *)data = -fgetown(logsoftc.sc_sigio);
244		break;
245
246	default:
247		return (ENOTTY);
248	}
249	return (0);
250}
251
252static	int	log_devsw_installed;
253#ifdef DEVFS
254static	void	*log_devfs_token;
255#endif
256
257static void log_drvinit __P((void *unused));
258static void
259log_drvinit(unused)
260	void *unused;
261{
262	dev_t dev;
263
264	if( ! log_devsw_installed ) {
265		dev = makedev(CDEV_MAJOR,0);
266		cdevsw_add(&dev,&log_cdevsw,NULL);
267		log_devsw_installed = 1;
268#ifdef DEVFS
269		log_devfs_token = devfs_add_devswf(&log_cdevsw, 0, DV_CHR,
270						   UID_ROOT, GID_WHEEL, 0600,
271						   "klog");
272#endif
273    	}
274}
275
276SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
277