subr_log.c revision 1.16 1 /* $NetBSD: subr_log.c,v 1.16 1998/08/04 04:03:15 perry 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/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 new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
86 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
87 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
88 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
89 /*
90 * If the buffer magic number is wrong, has changed
91 * size (which shouldn't happen often), or is
92 * internally inconsistent, initialize it.
93 */
94
95 memset(buf, 0, bufsize);
96 mbp->msg_magic = MSG_MAGIC;
97 mbp->msg_bufs = new_bufs;
98 }
99
100 /* mark it as ready for use. */
101 msgbufmapped = msgbufenabled = 1;
102 }
103
104 /*ARGSUSED*/
105 int
106 logopen(dev, flags, mode, p)
107 dev_t dev;
108 int flags, mode;
109 struct proc *p;
110 {
111 register struct kern_msgbuf *mbp = msgbufp;
112
113 if (log_open)
114 return (EBUSY);
115 log_open = 1;
116 logsoftc.sc_pgid = p->p_pid; /* signal process only */
117 /*
118 * The message buffer is initialized during system configuration.
119 * If it's been clobbered, note that and return an error. (This
120 * allows a user to potentially read the buffer via /dev/kmem,
121 * and try to figure out what clobbered it.
122 */
123 if (mbp->msg_magic != MSG_MAGIC) {
124 msgbufenabled = 0;
125 return (ENXIO);
126 }
127
128 return (0);
129 }
130
131 /*ARGSUSED*/
132 int
133 logclose(dev, flag, mode, p)
134 dev_t dev;
135 int flag, mode;
136 struct proc *p;
137 {
138
139 log_open = 0;
140 logsoftc.sc_state = 0;
141 return (0);
142 }
143
144 /*ARGSUSED*/
145 int
146 logread(dev, uio, flag)
147 dev_t dev;
148 struct uio *uio;
149 int flag;
150 {
151 register struct kern_msgbuf *mbp = msgbufp;
152 register long l;
153 register int s;
154 int error = 0;
155
156 s = splhigh();
157 while (mbp->msg_bufr == mbp->msg_bufx) {
158 if (flag & IO_NDELAY) {
159 splx(s);
160 return (EWOULDBLOCK);
161 }
162 logsoftc.sc_state |= LOG_RDWAIT;
163 error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
164 "klog", 0);
165 if (error) {
166 splx(s);
167 return (error);
168 }
169 }
170 splx(s);
171 logsoftc.sc_state &= ~LOG_RDWAIT;
172
173 while (uio->uio_resid > 0) {
174 l = mbp->msg_bufx - mbp->msg_bufr;
175 if (l < 0)
176 l = mbp->msg_bufs - mbp->msg_bufr;
177 l = min(l, uio->uio_resid);
178 if (l == 0)
179 break;
180 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
181 (int)l, uio);
182 if (error)
183 break;
184 mbp->msg_bufr += l;
185 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
186 mbp->msg_bufr = 0;
187 }
188 return (error);
189 }
190
191 /*ARGSUSED*/
192 int
193 logpoll(dev, events, p)
194 dev_t dev;
195 int events;
196 struct proc *p;
197 {
198 int revents = 0;
199 int s = splhigh();
200
201 if (events & (POLLIN | POLLRDNORM))
202 if (msgbufp->msg_bufr != msgbufp->msg_bufx)
203 revents |= events & (POLLIN | POLLRDNORM);
204 else
205 selrecord(p, &logsoftc.sc_selp);
206
207 splx(s);
208 return (revents);
209 }
210
211 void
212 logwakeup()
213 {
214 struct proc *p;
215
216 if (!log_open)
217 return;
218 selwakeup(&logsoftc.sc_selp);
219 if (logsoftc.sc_state & LOG_ASYNC) {
220 if (logsoftc.sc_pgid < 0)
221 gsignal(-logsoftc.sc_pgid, SIGIO);
222 else if ((p = pfind(logsoftc.sc_pgid)) != NULL)
223 psignal(p, SIGIO);
224 }
225 if (logsoftc.sc_state & LOG_RDWAIT) {
226 wakeup((caddr_t)msgbufp);
227 logsoftc.sc_state &= ~LOG_RDWAIT;
228 }
229 }
230
231 /*ARGSUSED*/
232 int
233 logioctl(dev, com, data, flag, p)
234 dev_t dev;
235 u_long com;
236 caddr_t data;
237 int flag;
238 struct proc *p;
239 {
240 long l;
241 int s;
242
243 switch (com) {
244
245 /* return number of characters immediately available */
246 case FIONREAD:
247 s = splhigh();
248 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
249 splx(s);
250 if (l < 0)
251 l += msgbufp->msg_bufs;
252 *(int *)data = l;
253 break;
254
255 case FIONBIO:
256 break;
257
258 case FIOASYNC:
259 if (*(int *)data)
260 logsoftc.sc_state |= LOG_ASYNC;
261 else
262 logsoftc.sc_state &= ~LOG_ASYNC;
263 break;
264
265 case TIOCSPGRP:
266 logsoftc.sc_pgid = *(int *)data;
267 break;
268
269 case TIOCGPGRP:
270 *(int *)data = logsoftc.sc_pgid;
271 break;
272
273 default:
274 return (-1);
275 }
276 return (0);
277 }
278