xref: /netbsd-src/sys/kern/subr_log.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: subr_log.c,v 1.13 1997/09/19 13:56:40 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
36  */
37 
38 /*
39  * Error log buffer for kernel printf's.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/ioctl.h>
47 #include <sys/msgbuf.h>
48 #include <sys/file.h>
49 #include <sys/signalvar.h>
50 #include <sys/syslog.h>
51 #include <sys/conf.h>
52 #include <sys/select.h>
53 #include <sys/poll.h>
54 
55 #define LOG_RDPRI	(PZERO + 1)
56 
57 #define LOG_ASYNC	0x04
58 #define LOG_RDWAIT	0x08
59 
60 struct 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 
66 int	log_open;			/* also used in log() */
67 int	msgbufmapped;			/* is the message buffer mapped */
68 int	msgbufenabled;			/* is logging to the buffer enabled */
69 struct	kern_msgbuf *msgbufp;		/* the mapped buffer, itself. */
70 
71 void
72 initmsgbuf(buf, bufsize)
73 	caddr_t buf;
74 	size_t bufsize;
75 {
76 	register struct kern_msgbuf *mbp;
77 	long new_bufs;
78 
79 	/* Sanity-check the given size. */
80 	if (bufsize < sizeof(struct kern_msgbuf))
81 		return;
82 
83 	mbp = msgbufp = (struct kern_msgbuf *)buf;
84 
85 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
86 	new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
87 #undef offsetof
88 	if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
89 	    (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
90 	    (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
91 		/*
92 		 * If the buffer magic number is wrong, has changed
93 		 * size (which shouldn't happen often), or is
94 		 * internally inconsistent, initialize it.
95 		 */
96 
97 		bzero(buf, bufsize);
98 		mbp->msg_magic = MSG_MAGIC;
99 		mbp->msg_bufs = new_bufs;
100 	}
101 
102 	/* mark it as ready for use. */
103 	msgbufmapped = msgbufenabled = 1;
104 }
105 
106 /*ARGSUSED*/
107 int
108 logopen(dev, flags, mode, p)
109 	dev_t dev;
110 	int flags, mode;
111 	struct proc *p;
112 {
113 	register struct kern_msgbuf *mbp = msgbufp;
114 
115 	if (log_open)
116 		return (EBUSY);
117 	log_open = 1;
118 	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
119 	/*
120 	 * The message buffer is initialized during system configuration.
121 	 * If it's been clobbered, note that and return an error.  (This
122 	 * allows a user to potentially read the buffer via /dev/kmem,
123 	 * and try to figure out what clobbered it.
124 	 */
125 	if (mbp->msg_magic != MSG_MAGIC) {
126 		msgbufenabled = 0;
127 		return (ENXIO);
128 	}
129 
130 	return (0);
131 }
132 
133 /*ARGSUSED*/
134 int
135 logclose(dev, flag, mode, p)
136 	dev_t dev;
137 	int flag, mode;
138 	struct proc *p;
139 {
140 
141 	log_open = 0;
142 	logsoftc.sc_state = 0;
143 	return (0);
144 }
145 
146 /*ARGSUSED*/
147 int
148 logread(dev, uio, flag)
149 	dev_t dev;
150 	struct uio *uio;
151 	int flag;
152 {
153 	register struct kern_msgbuf *mbp = msgbufp;
154 	register long l;
155 	register int s;
156 	int error = 0;
157 
158 	s = splhigh();
159 	while (mbp->msg_bufr == mbp->msg_bufx) {
160 		if (flag & IO_NDELAY) {
161 			splx(s);
162 			return (EWOULDBLOCK);
163 		}
164 		logsoftc.sc_state |= LOG_RDWAIT;
165 		error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
166 			       "klog", 0);
167 		if (error) {
168 			splx(s);
169 			return (error);
170 		}
171 	}
172 	splx(s);
173 	logsoftc.sc_state &= ~LOG_RDWAIT;
174 
175 	while (uio->uio_resid > 0) {
176 		l = mbp->msg_bufx - mbp->msg_bufr;
177 		if (l < 0)
178 			l = mbp->msg_bufs - mbp->msg_bufr;
179 		l = min(l, uio->uio_resid);
180 		if (l == 0)
181 			break;
182 		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
183 			(int)l, uio);
184 		if (error)
185 			break;
186 		mbp->msg_bufr += l;
187 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
188 			mbp->msg_bufr = 0;
189 	}
190 	return (error);
191 }
192 
193 /*ARGSUSED*/
194 int
195 logpoll(dev, events, p)
196 	dev_t dev;
197 	int events;
198 	struct proc *p;
199 {
200 	int revents = 0;
201 	int s = splhigh();
202 
203 	if (events & (POLLIN | POLLRDNORM))
204 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
205 			revents |= events & (POLLIN | POLLRDNORM);
206 		else
207 			selrecord(p, &logsoftc.sc_selp);
208 
209 	splx(s);
210 	return (revents);
211 }
212 
213 void
214 logwakeup()
215 {
216 	struct proc *p;
217 
218 	if (!log_open)
219 		return;
220 	selwakeup(&logsoftc.sc_selp);
221 	if (logsoftc.sc_state & LOG_ASYNC) {
222 		if (logsoftc.sc_pgid < 0)
223 			gsignal(-logsoftc.sc_pgid, SIGIO);
224 		else if ((p = pfind(logsoftc.sc_pgid)) != NULL)
225 			psignal(p, SIGIO);
226 	}
227 	if (logsoftc.sc_state & LOG_RDWAIT) {
228 		wakeup((caddr_t)msgbufp);
229 		logsoftc.sc_state &= ~LOG_RDWAIT;
230 	}
231 }
232 
233 /*ARGSUSED*/
234 int
235 logioctl(dev, com, data, flag, p)
236 	dev_t dev;
237 	u_long com;
238 	caddr_t data;
239 	int flag;
240 	struct proc *p;
241 {
242 	long l;
243 	int s;
244 
245 	switch (com) {
246 
247 	/* return number of characters immediately available */
248 	case FIONREAD:
249 		s = splhigh();
250 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
251 		splx(s);
252 		if (l < 0)
253 			l += msgbufp->msg_bufs;
254 		*(int *)data = l;
255 		break;
256 
257 	case FIONBIO:
258 		break;
259 
260 	case FIOASYNC:
261 		if (*(int *)data)
262 			logsoftc.sc_state |= LOG_ASYNC;
263 		else
264 			logsoftc.sc_state &= ~LOG_ASYNC;
265 		break;
266 
267 	case TIOCSPGRP:
268 		logsoftc.sc_pgid = *(int *)data;
269 		break;
270 
271 	case TIOCGPGRP:
272 		*(int *)data = logsoftc.sc_pgid;
273 		break;
274 
275 	default:
276 		return (-1);
277 	}
278 	return (0);
279 }
280