xref: /netbsd/sys/kern/subr_log.c (revision bf9ec67e)
1 /*	$NetBSD: subr_log.c,v 1.22 2002/04/28 04:13:51 enami 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.3 (Berkeley) 2/14/95
36  */
37 
38 /*
39  * Error log buffer for kernel printf's.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.22 2002/04/28 04:13:51 enami Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/ioctl.h>
50 #include <sys/msgbuf.h>
51 #include <sys/file.h>
52 #include <sys/signalvar.h>
53 #include <sys/syslog.h>
54 #include <sys/conf.h>
55 #include <sys/select.h>
56 #include <sys/poll.h>
57 
58 #define LOG_RDPRI	(PZERO + 1)
59 
60 #define LOG_ASYNC	0x04
61 #define LOG_RDWAIT	0x08
62 
63 struct logsoftc {
64 	int	sc_state;		/* see above for possibilities */
65 	struct	selinfo sc_selp;	/* process waiting on select call */
66 	int	sc_pgid;		/* process/group for async I/O */
67 } logsoftc;
68 
69 int	log_open;			/* also used in log() */
70 int	msgbufmapped;			/* is the message buffer mapped */
71 int	msgbufenabled;			/* is logging to the buffer enabled */
72 struct	kern_msgbuf *msgbufp;		/* the mapped buffer, itself. */
73 
74 void
75 initmsgbuf(buf, bufsize)
76 	caddr_t buf;
77 	size_t bufsize;
78 {
79 	struct kern_msgbuf *mbp;
80 	long new_bufs;
81 
82 	/* Sanity-check the given size. */
83 	if (bufsize < sizeof(struct kern_msgbuf))
84 		return;
85 
86 	mbp = msgbufp = (struct kern_msgbuf *)buf;
87 
88 	new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
89 	if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
90 	    (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
91 	    (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
92 		/*
93 		 * If the buffer magic number is wrong, has changed
94 		 * size (which shouldn't happen often), or is
95 		 * internally inconsistent, initialize it.
96 		 */
97 
98 		memset(buf, 0, bufsize);
99 		mbp->msg_magic = MSG_MAGIC;
100 		mbp->msg_bufs = new_bufs;
101 	}
102 
103 	/* mark it as ready for use. */
104 	msgbufmapped = msgbufenabled = 1;
105 }
106 
107 /*ARGSUSED*/
108 int
109 logopen(dev, flags, mode, p)
110 	dev_t dev;
111 	int flags, mode;
112 	struct proc *p;
113 {
114 	struct kern_msgbuf *mbp = msgbufp;
115 
116 	if (log_open)
117 		return (EBUSY);
118 	log_open = 1;
119 	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
120 	/*
121 	 * The message buffer is initialized during system configuration.
122 	 * If it's been clobbered, note that and return an error.  (This
123 	 * allows a user to potentially read the buffer via /dev/kmem,
124 	 * and try to figure out what clobbered it.
125 	 */
126 	if (mbp->msg_magic != MSG_MAGIC) {
127 		msgbufenabled = 0;
128 		return (ENXIO);
129 	}
130 
131 	return (0);
132 }
133 
134 /*ARGSUSED*/
135 int
136 logclose(dev, flag, mode, p)
137 	dev_t dev;
138 	int flag, mode;
139 	struct proc *p;
140 {
141 
142 	log_open = 0;
143 	logsoftc.sc_state = 0;
144 	return (0);
145 }
146 
147 /*ARGSUSED*/
148 int
149 logread(dev, uio, flag)
150 	dev_t dev;
151 	struct uio *uio;
152 	int flag;
153 {
154 	struct kern_msgbuf *mbp = msgbufp;
155 	long l;
156 	int s;
157 	int error = 0;
158 
159 	s = splhigh();
160 	while (mbp->msg_bufr == mbp->msg_bufx) {
161 		if (flag & IO_NDELAY) {
162 			splx(s);
163 			return (EWOULDBLOCK);
164 		}
165 		logsoftc.sc_state |= LOG_RDWAIT;
166 		error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
167 			       "klog", 0);
168 		if (error) {
169 			splx(s);
170 			return (error);
171 		}
172 	}
173 	splx(s);
174 	logsoftc.sc_state &= ~LOG_RDWAIT;
175 
176 	while (uio->uio_resid > 0) {
177 		l = mbp->msg_bufx - mbp->msg_bufr;
178 		if (l < 0)
179 			l = mbp->msg_bufs - mbp->msg_bufr;
180 		l = min(l, uio->uio_resid);
181 		if (l == 0)
182 			break;
183 		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
184 			(int)l, uio);
185 		if (error)
186 			break;
187 		mbp->msg_bufr += l;
188 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
189 			mbp->msg_bufr = 0;
190 	}
191 	return (error);
192 }
193 
194 /*ARGSUSED*/
195 int
196 logpoll(dev, events, p)
197 	dev_t dev;
198 	int events;
199 	struct proc *p;
200 {
201 	int revents = 0;
202 	int s = splhigh();
203 
204 	if (events & (POLLIN | POLLRDNORM)) {
205 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
206 			revents |= events & (POLLIN | POLLRDNORM);
207 		else
208 			selrecord(p, &logsoftc.sc_selp);
209 	}
210 
211 	splx(s);
212 	return (revents);
213 }
214 
215 void
216 logwakeup()
217 {
218 	struct proc *p;
219 
220 	if (!log_open)
221 		return;
222 	selwakeup(&logsoftc.sc_selp);
223 	if (logsoftc.sc_state & LOG_ASYNC) {
224 		if (logsoftc.sc_pgid < 0)
225 			gsignal(-logsoftc.sc_pgid, SIGIO);
226 		else if (logsoftc.sc_pgid > 0 &&
227 		    (p = pfind(logsoftc.sc_pgid)) != NULL)
228 			psignal(p, SIGIO);
229 	}
230 	if (logsoftc.sc_state & LOG_RDWAIT) {
231 		wakeup((caddr_t)msgbufp);
232 		logsoftc.sc_state &= ~LOG_RDWAIT;
233 	}
234 }
235 
236 /*ARGSUSED*/
237 int
238 logioctl(dev, com, data, flag, p)
239 	dev_t dev;
240 	u_long com;
241 	caddr_t data;
242 	int flag;
243 	struct proc *p;
244 {
245 	long l;
246 	int s;
247 
248 	switch (com) {
249 
250 	/* return number of characters immediately available */
251 	case FIONREAD:
252 		s = splhigh();
253 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
254 		splx(s);
255 		if (l < 0)
256 			l += msgbufp->msg_bufs;
257 		*(int *)data = l;
258 		break;
259 
260 	case FIONBIO:
261 		break;
262 
263 	case FIOASYNC:
264 		if (*(int *)data)
265 			logsoftc.sc_state |= LOG_ASYNC;
266 		else
267 			logsoftc.sc_state &= ~LOG_ASYNC;
268 		break;
269 
270 	case TIOCSPGRP:
271 		logsoftc.sc_pgid = *(int *)data;
272 		break;
273 
274 	case TIOCGPGRP:
275 		*(int *)data = logsoftc.sc_pgid;
276 		break;
277 
278 	default:
279 		return (EPASSTHROUGH);
280 	}
281 	return (0);
282 }
283