xref: /freebsd/sys/kern/subr_log.c (revision 7f3dea244c40159a41ab22da77a434d7c5b5e85a)
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.36 1999/05/31 11:27:35 phk Exp $
35  */
36 
37 /*
38  * Error log buffer for kernel printf's.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/filio.h>
47 #include <sys/ttycom.h>
48 #include <sys/msgbuf.h>
49 #include <sys/signalvar.h>
50 #include <sys/kernel.h>
51 #include <sys/poll.h>
52 #include <sys/filedesc.h>
53 
54 #define LOG_RDPRI	(PZERO + 1)
55 
56 #define LOG_ASYNC	0x04
57 #define LOG_RDWAIT	0x08
58 
59 static	d_open_t	logopen;
60 static	d_close_t	logclose;
61 static	d_read_t	logread;
62 static	d_ioctl_t	logioctl;
63 static	d_poll_t	logpoll;
64 
65 #define CDEV_MAJOR 7
66 static struct cdevsw log_cdevsw = {
67 	/* open */	logopen,
68 	/* close */	logclose,
69 	/* read */	logread,
70 	/* write */	nowrite,
71 	/* ioctl */	logioctl,
72 	/* stop */	nostop,
73 	/* reset */	noreset,
74 	/* devtotty */	nodevtotty,
75 	/* poll */	logpoll,
76 	/* mmap */	nommap,
77 	/* strategy */	nostrategy,
78 	/* name */	"log",
79 	/* parms */	noparms,
80 	/* maj */	CDEV_MAJOR,
81 	/* dump */	nodump,
82 	/* psize */	nopsize,
83 	/* flags */	0,
84 	/* maxio */	0,
85 	/* bmaj */	-1
86 };
87 
88 static struct logsoftc {
89 	int	sc_state;		/* see above for possibilities */
90 	struct	selinfo sc_selp;	/* process waiting on select call */
91 	struct  sigio *sc_sigio;	/* information for async I/O */
92 } logsoftc;
93 
94 int	log_open;			/* also used in log() */
95 
96 /*ARGSUSED*/
97 static	int
98 logopen(dev, flags, mode, p)
99 	dev_t dev;
100 	int flags, mode;
101 	struct proc *p;
102 {
103 	if (log_open)
104 		return (EBUSY);
105 	log_open = 1;
106 	fsetown(p->p_pid, &logsoftc.sc_sigio);	/* signal process only */
107 	return (0);
108 }
109 
110 /*ARGSUSED*/
111 static	int
112 logclose(dev, flag, mode, p)
113 	dev_t dev;
114 	int flag, mode;
115 	struct proc *p;
116 {
117 
118 	log_open = 0;
119 	logsoftc.sc_state = 0;
120 	funsetown(logsoftc.sc_sigio);
121 	return (0);
122 }
123 
124 /*ARGSUSED*/
125 static	int
126 logread(dev, uio, flag)
127 	dev_t dev;
128 	struct uio *uio;
129 	int flag;
130 {
131 	register struct msgbuf *mbp = msgbufp;
132 	register long l;
133 	register int s;
134 	int error = 0;
135 
136 	s = splhigh();
137 	while (mbp->msg_bufr == mbp->msg_bufx) {
138 		if (flag & IO_NDELAY) {
139 			splx(s);
140 			return (EWOULDBLOCK);
141 		}
142 		logsoftc.sc_state |= LOG_RDWAIT;
143 		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
144 		    "klog", 0))) {
145 			splx(s);
146 			return (error);
147 		}
148 	}
149 	splx(s);
150 	logsoftc.sc_state &= ~LOG_RDWAIT;
151 
152 	while (uio->uio_resid > 0) {
153 		l = mbp->msg_bufx - mbp->msg_bufr;
154 		if (l < 0)
155 			l = mbp->msg_size - mbp->msg_bufr;
156 		l = min(l, uio->uio_resid);
157 		if (l == 0)
158 			break;
159 		error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
160 		    (int)l, uio);
161 		if (error)
162 			break;
163 		mbp->msg_bufr += l;
164 		if (mbp->msg_bufr >= mbp->msg_size)
165 			mbp->msg_bufr = 0;
166 	}
167 	return (error);
168 }
169 
170 /*ARGSUSED*/
171 static	int
172 logpoll(dev, events, p)
173 	dev_t dev;
174 	int events;
175 	struct proc *p;
176 {
177 	int s;
178 	int revents = 0;
179 
180 	s = splhigh();
181 
182 	if (events & (POLLIN | POLLRDNORM)) {
183 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
184 			revents |= events & (POLLIN | POLLRDNORM);
185 		else
186 			selrecord(p, &logsoftc.sc_selp);
187 	}
188 	splx(s);
189 	return (revents);
190 }
191 
192 void
193 logwakeup()
194 {
195 	if (!log_open)
196 		return;
197 	selwakeup(&logsoftc.sc_selp);
198 	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
199 		pgsigio(logsoftc.sc_sigio, SIGIO, 0);
200 	if (logsoftc.sc_state & LOG_RDWAIT) {
201 		wakeup((caddr_t)msgbufp);
202 		logsoftc.sc_state &= ~LOG_RDWAIT;
203 	}
204 }
205 
206 /*ARGSUSED*/
207 static	int
208 logioctl(dev, com, data, flag, p)
209 	dev_t dev;
210 	u_long com;
211 	caddr_t data;
212 	int flag;
213 	struct proc *p;
214 {
215 	long l;
216 	int s;
217 
218 	switch (com) {
219 
220 	/* return number of characters immediately available */
221 	case FIONREAD:
222 		s = splhigh();
223 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
224 		splx(s);
225 		if (l < 0)
226 			l += msgbufp->msg_size;
227 		*(int *)data = l;
228 		break;
229 
230 	case FIONBIO:
231 		break;
232 
233 	case FIOASYNC:
234 		if (*(int *)data)
235 			logsoftc.sc_state |= LOG_ASYNC;
236 		else
237 			logsoftc.sc_state &= ~LOG_ASYNC;
238 		break;
239 
240 	case FIOSETOWN:
241 		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
242 
243 	case FIOGETOWN:
244 		*(int *)data = fgetown(logsoftc.sc_sigio);
245 		break;
246 
247 	/* This is deprecated, FIOSETOWN should be used instead. */
248 	case TIOCSPGRP:
249 		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
250 
251 	/* This is deprecated, FIOGETOWN should be used instead */
252 	case TIOCGPGRP:
253 		*(int *)data = -fgetown(logsoftc.sc_sigio);
254 		break;
255 
256 	default:
257 		return (ENOTTY);
258 	}
259 	return (0);
260 }
261 
262 
263 static void log_drvinit __P((void *unused));
264 
265 static void
266 log_drvinit(unused)
267 	void *unused;
268 {
269 	make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
270 }
271 
272 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
273