subr_log.c revision 1.50.44.2 1 /* $NetBSD: subr_log.c,v 1.50.44.2 2017/12/03 11:38:45 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95
61 */
62
63 /*
64 * Error log buffer for kernel printf's.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.50.44.2 2017/12/03 11:38:45 jdolecek Exp $");
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/proc.h>
74 #include <sys/vnode.h>
75 #include <sys/ioctl.h>
76 #include <sys/msgbuf.h>
77 #include <sys/file.h>
78 #include <sys/syslog.h>
79 #include <sys/conf.h>
80 #include <sys/select.h>
81 #include <sys/poll.h>
82 #include <sys/intr.h>
83 #include <sys/sysctl.h>
84 #include <sys/ktrace.h>
85
86 static int sysctl_msgbuf(SYSCTLFN_PROTO);
87
88 static void logsoftintr(void *);
89
90 static bool log_async;
91 static struct selinfo log_selp; /* process waiting on select call */
92 static pid_t log_pgid; /* process/group for async I/O */
93 static kcondvar_t log_cv;
94 static void *log_sih;
95
96 kmutex_t log_lock;
97 int log_open; /* also used in log() */
98 int msgbufmapped; /* is the message buffer mapped */
99 int msgbufenabled; /* is logging to the buffer enabled */
100 struct kern_msgbuf *msgbufp; /* the mapped buffer, itself. */
101
102 void
103 initmsgbuf(void *bf, size_t bufsize)
104 {
105 struct kern_msgbuf *mbp;
106 long new_bufs;
107
108 /* Sanity-check the given size. */
109 if (bufsize < sizeof(struct kern_msgbuf))
110 return;
111
112 mbp = msgbufp = (struct kern_msgbuf *)bf;
113
114 new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
115 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
116 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
117 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
118 /*
119 * If the buffer magic number is wrong, has changed
120 * size (which shouldn't happen often), or is
121 * internally inconsistent, initialize it.
122 */
123
124 memset(bf, 0, bufsize);
125 mbp->msg_magic = MSG_MAGIC;
126 mbp->msg_bufs = new_bufs;
127 }
128
129 /* mark it as ready for use. */
130 msgbufmapped = msgbufenabled = 1;
131 }
132
133 void
134 loginit(void)
135 {
136
137 mutex_init(&log_lock, MUTEX_DEFAULT, IPL_VM);
138 selinit(&log_selp);
139 cv_init(&log_cv, "klog");
140 log_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
141 logsoftintr, NULL);
142
143 sysctl_createv(NULL, 0, NULL, NULL,
144 CTLFLAG_PERMANENT,
145 CTLTYPE_INT, "msgbufsize",
146 SYSCTL_DESCR("Size of the kernel message buffer"),
147 sysctl_msgbuf, 0, NULL, 0,
148 CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
149 sysctl_createv(NULL, 0, NULL, NULL,
150 CTLFLAG_PERMANENT,
151 CTLTYPE_INT, "msgbuf",
152 SYSCTL_DESCR("Kernel message buffer"),
153 sysctl_msgbuf, 0, NULL, 0,
154 CTL_KERN, KERN_MSGBUF, CTL_EOL);
155 }
156
157 /*ARGSUSED*/
158 static int
159 logopen(dev_t dev, int flags, int mode, struct lwp *l)
160 {
161 struct kern_msgbuf *mbp = msgbufp;
162 int error = 0;
163
164 mutex_spin_enter(&log_lock);
165 if (log_open) {
166 error = EBUSY;
167 } else {
168 log_open = 1;
169 log_pgid = l->l_proc->p_pid; /* signal process only */
170 /*
171 * The message buffer is initialized during system
172 * configuration. If it's been clobbered, note that
173 * and return an error. (This allows a user to read
174 * the buffer via /dev/kmem, and try to figure out
175 * what clobbered it.
176 */
177 if (mbp->msg_magic != MSG_MAGIC) {
178 msgbufenabled = 0;
179 error = ENXIO;
180 }
181 }
182 mutex_spin_exit(&log_lock);
183
184 return error;
185 }
186
187 /*ARGSUSED*/
188 static int
189 logclose(dev_t dev, int flag, int mode, struct lwp *l)
190 {
191
192 mutex_spin_enter(&log_lock);
193 log_pgid = 0;
194 log_open = 0;
195 log_async = 0;
196 mutex_spin_exit(&log_lock);
197
198 return 0;
199 }
200
201 /*ARGSUSED*/
202 static int
203 logread(dev_t dev, struct uio *uio, int flag)
204 {
205 struct kern_msgbuf *mbp = msgbufp;
206 long l;
207 int error = 0;
208
209 mutex_spin_enter(&log_lock);
210 while (mbp->msg_bufr == mbp->msg_bufx) {
211 if (flag & IO_NDELAY) {
212 mutex_spin_exit(&log_lock);
213 return EWOULDBLOCK;
214 }
215 error = cv_wait_sig(&log_cv, &log_lock);
216 if (error) {
217 mutex_spin_exit(&log_lock);
218 return error;
219 }
220 }
221 while (uio->uio_resid > 0) {
222 l = mbp->msg_bufx - mbp->msg_bufr;
223 if (l < 0)
224 l = mbp->msg_bufs - mbp->msg_bufr;
225 l = min(l, uio->uio_resid);
226 if (l == 0)
227 break;
228 mutex_spin_exit(&log_lock);
229 error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio);
230 mutex_spin_enter(&log_lock);
231 if (error)
232 break;
233 mbp->msg_bufr += l;
234 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
235 mbp->msg_bufr = 0;
236 }
237 mutex_spin_exit(&log_lock);
238
239 return error;
240 }
241
242 /*ARGSUSED*/
243 static int
244 logpoll(dev_t dev, int events, struct lwp *l)
245 {
246 int revents = 0;
247
248 if (events & (POLLIN | POLLRDNORM)) {
249 mutex_spin_enter(&log_lock);
250 if (msgbufp->msg_bufr != msgbufp->msg_bufx)
251 revents |= events & (POLLIN | POLLRDNORM);
252 else
253 selrecord(l, &log_selp);
254 mutex_spin_exit(&log_lock);
255 }
256
257 return revents;
258 }
259
260 static void
261 filt_logrdetach(struct knote *kn)
262 {
263
264 mutex_spin_enter(&log_lock);
265 SLIST_REMOVE(&log_selp.sel_klist, kn, knote, kn_selnext);
266 mutex_spin_exit(&log_lock);
267 }
268
269 static int
270 filt_logread(struct knote *kn, long hint)
271 {
272 int rv;
273
274 if ((hint & NOTE_SUBMIT) == 0)
275 mutex_spin_enter(&log_lock);
276 if (msgbufp->msg_bufr == msgbufp->msg_bufx) {
277 rv = 0;
278 } else if (msgbufp->msg_bufr < msgbufp->msg_bufx) {
279 kn->kn_data = msgbufp->msg_bufx - msgbufp->msg_bufr;
280 rv = 1;
281 } else {
282 kn->kn_data = (msgbufp->msg_bufs - msgbufp->msg_bufr) +
283 msgbufp->msg_bufx;
284 rv = 1;
285 }
286 if ((hint & NOTE_SUBMIT) == 0)
287 mutex_spin_exit(&log_lock);
288
289 return rv;
290 }
291
292 static const struct filterops logread_filtops = {
293 .f_isfd = 1,
294 .f_attach = NULL,
295 .f_detach = filt_logrdetach,
296 .f_event = filt_logread,
297 };
298
299 static int
300 logkqfilter(dev_t dev, struct knote *kn)
301 {
302 struct klist *klist;
303
304 switch (kn->kn_filter) {
305 case EVFILT_READ:
306 klist = &log_selp.sel_klist;
307 kn->kn_fop = &logread_filtops;
308 break;
309
310 default:
311 return (EINVAL);
312 }
313
314 mutex_spin_enter(&log_lock);
315 kn->kn_hook = NULL;
316 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
317 mutex_spin_exit(&log_lock);
318
319 return (0);
320 }
321
322 void
323 logwakeup(void)
324 {
325
326 if (!cold && log_open) {
327 mutex_spin_enter(&log_lock);
328 selnotify(&log_selp, 0, NOTE_SUBMIT);
329 if (log_async)
330 softint_schedule(log_sih);
331 cv_broadcast(&log_cv);
332 mutex_spin_exit(&log_lock);
333 }
334 }
335
336 static void
337 logsoftintr(void *cookie)
338 {
339 pid_t pid;
340
341 if ((pid = log_pgid) != 0)
342 fownsignal(pid, SIGIO, 0, 0, NULL);
343 }
344
345 /*ARGSUSED*/
346 static int
347 logioctl(dev_t dev, u_long com, void *data, int flag, struct lwp *lwp)
348 {
349 long l;
350
351 switch (com) {
352
353 /* return number of characters immediately available */
354 case FIONREAD:
355 mutex_spin_enter(&log_lock);
356 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
357 if (l < 0)
358 l += msgbufp->msg_bufs;
359 mutex_spin_exit(&log_lock);
360 *(int *)data = l;
361 break;
362
363 case FIONBIO:
364 break;
365
366 case FIOASYNC:
367 /* No locking needed, 'thread private'. */
368 log_async = (*((int *)data) != 0);
369 break;
370
371 case TIOCSPGRP:
372 case FIOSETOWN:
373 return fsetown(&log_pgid, com, data);
374
375 case TIOCGPGRP:
376 case FIOGETOWN:
377 return fgetown(log_pgid, com, data);
378
379 default:
380 return (EPASSTHROUGH);
381 }
382 return (0);
383 }
384
385 void
386 logputchar(int c)
387 {
388 struct kern_msgbuf *mbp;
389
390 if (!cold)
391 mutex_spin_enter(&log_lock);
392 if (msgbufenabled) {
393 mbp = msgbufp;
394 if (mbp->msg_magic != MSG_MAGIC) {
395 /*
396 * Arguably should panic or somehow notify the
397 * user... but how? Panic may be too drastic,
398 * and would obliterate the message being kicked
399 * out (maybe a panic itself), and printf
400 * would invoke us recursively. Silently punt
401 * for now. If syslog is running, it should
402 * notice.
403 */
404 msgbufenabled = 0;
405 } else {
406 mbp->msg_bufc[mbp->msg_bufx++] = c;
407 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
408 mbp->msg_bufx = 0;
409 /* If the buffer is full, keep the most recent data. */
410 if (mbp->msg_bufr == mbp->msg_bufx) {
411 char c0;
412 int i;
413
414 /*
415 * Move forward read pointer to the next line
416 * in the buffer. Note that the buffer is
417 * a ring buffer so we should reset msg_bufr
418 * to 0 when msg_bufr exceeds msg_bufs.
419 *
420 * To prevent to loop forever, give up if we
421 * cannot find a newline in mbp->msg_bufs
422 * characters (the max size of the buffer).
423 */
424 for (i = 0; i < mbp->msg_bufs; i++) {
425 c0 = mbp->msg_bufc[mbp->msg_bufr];
426 if (++mbp->msg_bufr >= mbp->msg_bufs)
427 mbp->msg_bufr = 0;
428 if (c0 == '\n')
429 break;
430 }
431 }
432 }
433 }
434 if (!cold)
435 mutex_spin_exit(&log_lock);
436 }
437
438 /*
439 * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
440 * former it merely checks the message buffer is set up. For the latter,
441 * it also copies out the data if necessary.
442 */
443 static int
444 sysctl_msgbuf(SYSCTLFN_ARGS)
445 {
446 char *where = oldp;
447 size_t len, maxlen;
448 long beg, end;
449 extern kmutex_t log_lock;
450 int error;
451
452 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
453 msgbufenabled = 0;
454 return (ENXIO);
455 }
456
457 switch (rnode->sysctl_num) {
458 case KERN_MSGBUFSIZE: {
459 struct sysctlnode node = *rnode;
460 int msg_bufs = (int)msgbufp->msg_bufs;
461 node.sysctl_data = &msg_bufs;
462 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
463 }
464 case KERN_MSGBUF:
465 break;
466 default:
467 return (EOPNOTSUPP);
468 }
469
470 if (newp != NULL)
471 return (EPERM);
472
473 if (oldp == NULL) {
474 /* always return full buffer size */
475 *oldlenp = msgbufp->msg_bufs;
476 return (0);
477 }
478
479 sysctl_unlock();
480
481 /*
482 * First, copy from the write pointer to the end of
483 * message buffer.
484 */
485 error = 0;
486 mutex_spin_enter(&log_lock);
487 maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
488 beg = msgbufp->msg_bufx;
489 end = msgbufp->msg_bufs;
490 mutex_spin_exit(&log_lock);
491
492 while (maxlen > 0) {
493 len = MIN(end - beg, maxlen);
494 if (len == 0)
495 break;
496 /* XXX unlocked, but hardly matters. */
497 error = copyout(&msgbufp->msg_bufc[beg], where, len);
498 ktrmibio(-1, UIO_READ, where, len, error);
499 if (error)
500 break;
501 where += len;
502 maxlen -= len;
503
504 /*
505 * ... then, copy from the beginning of message buffer to
506 * the write pointer.
507 */
508 beg = 0;
509 end = msgbufp->msg_bufx;
510 }
511
512 sysctl_relock();
513 return (error);
514 }
515
516 const struct cdevsw log_cdevsw = {
517 .d_open = logopen,
518 .d_close = logclose,
519 .d_read = logread,
520 .d_write = nowrite,
521 .d_ioctl = logioctl,
522 .d_stop = nostop,
523 .d_tty = notty,
524 .d_poll = logpoll,
525 .d_mmap = nommap,
526 .d_kqfilter = logkqfilter,
527 .d_discard = nodiscard,
528 .d_flag = D_OTHER | D_MPSAFE
529 };
530