xref: /openbsd-src/sys/kern/subr_log.c (revision 10e180b8eec2e57a46b25e5ca7c5e641b9d9a38e)
1 /*	$OpenBSD: subr_log.c,v 1.43 2016/05/18 23:42:12 bluhm Exp $	*/
2 /*	$NetBSD: subr_log.c,v 1.11 1996/03/30 22:24:44 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
33  */
34 
35 /*
36  * Error log buffer for kernel printf's.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/ioctl.h>
44 #include <sys/msgbuf.h>
45 #include <sys/file.h>
46 #include <sys/tty.h>
47 #include <sys/signalvar.h>
48 #include <sys/syslog.h>
49 #include <sys/poll.h>
50 #include <sys/malloc.h>
51 #include <sys/filedesc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 
55 #ifdef KTRACE
56 #include <sys/ktrace.h>
57 #endif
58 
59 #include <sys/mount.h>
60 #include <sys/syscallargs.h>
61 
62 #include <dev/cons.h>
63 
64 #define LOG_RDPRI	(PZERO + 1)
65 
66 #define LOG_ASYNC	0x04
67 #define LOG_RDWAIT	0x08
68 
69 struct logsoftc {
70 	int	sc_state;		/* see above for possibilities */
71 	struct	selinfo sc_selp;	/* process waiting on select call */
72 	int	sc_pgid;		/* process/group for async I/O */
73 	uid_t	sc_siguid;		/* uid for process that set sc_pgid */
74 	uid_t	sc_sigeuid;		/* euid for process that set sc_pgid */
75 } logsoftc;
76 
77 int	log_open;			/* also used in log() */
78 int	msgbufmapped;			/* is the message buffer mapped */
79 struct	msgbuf *msgbufp;		/* the mapped buffer, itself. */
80 struct	msgbuf *consbufp;		/* console message buffer. */
81 struct	file *syslogf;
82 
83 void filt_logrdetach(struct knote *kn);
84 int filt_logread(struct knote *kn, long hint);
85 
86 struct filterops logread_filtops =
87 	{ 1, NULL, filt_logrdetach, filt_logread};
88 
89 int dosendsyslog(struct proc *, const char *, size_t, int, enum uio_seg);
90 
91 void
92 initmsgbuf(caddr_t buf, size_t bufsize)
93 {
94 	struct msgbuf *mbp;
95 	long new_bufs;
96 
97 	/* Sanity-check the given size. */
98 	if (bufsize < sizeof(struct msgbuf))
99 		return;
100 
101 	mbp = msgbufp = (struct msgbuf *)buf;
102 
103 	new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc);
104 	if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
105 	    (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
106 	    (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
107 		/*
108 		 * If the buffer magic number is wrong, has changed
109 		 * size (which shouldn't happen often), or is
110 		 * internally inconsistent, initialize it.
111 		 */
112 
113 		memset(buf, 0, bufsize);
114 		mbp->msg_magic = MSG_MAGIC;
115 		mbp->msg_bufs = new_bufs;
116 	}
117 
118 	/* Always start new buffer data on a new line. */
119 	if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n')
120 		msgbuf_putchar(msgbufp, '\n');
121 
122 	/* mark it as ready for use. */
123 	msgbufmapped = 1;
124 }
125 
126 void
127 initconsbuf(void)
128 {
129 	long new_bufs;
130 
131 	/* Set up a buffer to collect /dev/console output */
132 	consbufp = malloc(CONSBUFSIZE, M_TEMP, M_NOWAIT|M_ZERO);
133 	if (consbufp) {
134 		new_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc);
135 		consbufp->msg_magic = MSG_MAGIC;
136 		consbufp->msg_bufs = new_bufs;
137 	}
138 }
139 
140 void
141 msgbuf_putchar(struct msgbuf *mbp, const char c)
142 {
143 	if (mbp->msg_magic != MSG_MAGIC)
144 		/* Nothing we can do */
145 		return;
146 
147 	mbp->msg_bufc[mbp->msg_bufx++] = c;
148 	mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs);
149 	if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
150 		mbp->msg_bufx = 0;
151 	/* If the buffer is full, keep the most recent data. */
152 	if (mbp->msg_bufr == mbp->msg_bufx) {
153 		if (++mbp->msg_bufr >= mbp->msg_bufs)
154 			mbp->msg_bufr = 0;
155 	}
156 }
157 
158 int
159 logopen(dev_t dev, int flags, int mode, struct proc *p)
160 {
161 	if (log_open)
162 		return (EBUSY);
163 	log_open = 1;
164 	return (0);
165 }
166 
167 int
168 logclose(dev_t dev, int flag, int mode, struct proc *p)
169 {
170 
171 	if (syslogf)
172 		FRELE(syslogf, p);
173 	syslogf = NULL;
174 	log_open = 0;
175 	logsoftc.sc_state = 0;
176 	return (0);
177 }
178 
179 int
180 logread(dev_t dev, struct uio *uio, int flag)
181 {
182 	struct msgbuf *mbp = msgbufp;
183 	size_t l;
184 	int s;
185 	int error = 0;
186 
187 	s = splhigh();
188 	while (mbp->msg_bufr == mbp->msg_bufx) {
189 		if (flag & IO_NDELAY) {
190 			splx(s);
191 			return (EWOULDBLOCK);
192 		}
193 		logsoftc.sc_state |= LOG_RDWAIT;
194 		error = tsleep(mbp, LOG_RDPRI | PCATCH,
195 			       "klog", 0);
196 		if (error) {
197 			splx(s);
198 			return (error);
199 		}
200 	}
201 	splx(s);
202 	logsoftc.sc_state &= ~LOG_RDWAIT;
203 
204 	while (uio->uio_resid > 0) {
205 		if (mbp->msg_bufx >= mbp->msg_bufr)
206 			l = mbp->msg_bufx - mbp->msg_bufr;
207 		else
208 			l = mbp->msg_bufs - mbp->msg_bufr;
209 		l = ulmin(l, uio->uio_resid);
210 		if (l == 0)
211 			break;
212 		error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], l, uio);
213 		if (error)
214 			break;
215 		mbp->msg_bufr += l;
216 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
217 			mbp->msg_bufr = 0;
218 	}
219 	return (error);
220 }
221 
222 int
223 logpoll(dev_t dev, int events, struct proc *p)
224 {
225 	int revents = 0;
226 	int s = splhigh();
227 
228 	if (events & (POLLIN | POLLRDNORM)) {
229 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
230 			revents |= events & (POLLIN | POLLRDNORM);
231 		else
232 			selrecord(p, &logsoftc.sc_selp);
233 	}
234 	splx(s);
235 	return (revents);
236 }
237 
238 int
239 logkqfilter(dev_t dev, struct knote *kn)
240 {
241 	struct klist *klist;
242 	int s;
243 
244 	switch (kn->kn_filter) {
245 	case EVFILT_READ:
246 		klist = &logsoftc.sc_selp.si_note;
247 		kn->kn_fop = &logread_filtops;
248 		break;
249 	default:
250 		return (EINVAL);
251 	}
252 
253 	kn->kn_hook = (void *)msgbufp;
254 
255 	s = splhigh();
256 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
257 	splx(s);
258 
259 	return (0);
260 }
261 
262 void
263 filt_logrdetach(struct knote *kn)
264 {
265 	int s = splhigh();
266 
267 	SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext);
268 	splx(s);
269 }
270 
271 int
272 filt_logread(struct knote *kn, long hint)
273 {
274 	struct  msgbuf *p = (struct  msgbuf *)kn->kn_hook;
275 
276 	kn->kn_data = (int)(p->msg_bufx - p->msg_bufr);
277 
278 	return (p->msg_bufx != p->msg_bufr);
279 }
280 
281 void
282 logwakeup(void)
283 {
284 	if (!log_open)
285 		return;
286 	selwakeup(&logsoftc.sc_selp);
287 	if (logsoftc.sc_state & LOG_ASYNC)
288 		csignal(logsoftc.sc_pgid, SIGIO,
289 		    logsoftc.sc_siguid, logsoftc.sc_sigeuid);
290 	if (logsoftc.sc_state & LOG_RDWAIT) {
291 		wakeup(msgbufp);
292 		logsoftc.sc_state &= ~LOG_RDWAIT;
293 	}
294 }
295 
296 int
297 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p)
298 {
299 	struct file *fp;
300 	long l;
301 	int error, s;
302 
303 	switch (com) {
304 
305 	/* return number of characters immediately available */
306 	case FIONREAD:
307 		s = splhigh();
308 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
309 		splx(s);
310 		if (l < 0)
311 			l += msgbufp->msg_bufs;
312 		*(int *)data = l;
313 		break;
314 
315 	case FIONBIO:
316 		break;
317 
318 	case FIOASYNC:
319 		if (*(int *)data)
320 			logsoftc.sc_state |= LOG_ASYNC;
321 		else
322 			logsoftc.sc_state &= ~LOG_ASYNC;
323 		break;
324 
325 	case TIOCSPGRP:
326 		logsoftc.sc_pgid = *(int *)data;
327 		logsoftc.sc_siguid = p->p_ucred->cr_ruid;
328 		logsoftc.sc_sigeuid = p->p_ucred->cr_uid;
329 		break;
330 
331 	case TIOCGPGRP:
332 		*(int *)data = logsoftc.sc_pgid;
333 		break;
334 
335 	case LIOCSFD:
336 		if ((error = suser(p, 0)) != 0)
337 			return (error);
338 		if ((error = getsock(p, *(int *)data, &fp)) != 0)
339 			return (error);
340 		if (syslogf)
341 			FRELE(syslogf, p);
342 		syslogf = fp;
343 		break;
344 
345 	default:
346 		return (ENOTTY);
347 	}
348 	return (0);
349 }
350 
351 int
352 sys_osendsyslog(struct proc *p, void *v, register_t *retval)
353 {
354 	struct sys_osendsyslog_args /* {
355 		syscallarg(const void *) buf;
356 		syscallarg(size_t) nbyte;
357 	} */ *uap = v;
358 	struct sys_sendsyslog_args oap;
359 
360 	SCARG(&oap, buf) = SCARG(uap, buf);
361 	SCARG(&oap, nbyte) = SCARG(uap, nbyte);
362 	SCARG(&oap, flags) = 0;
363 	return sys_sendsyslog(p, &oap, retval);
364 }
365 
366 int
367 sys_sendsyslog(struct proc *p, void *v, register_t *retval)
368 {
369 	struct sys_sendsyslog_args /* {
370 		syscallarg(const void *) buf;
371 		syscallarg(size_t) nbyte;
372 		syscallarg(int) flags;
373 	} */ *uap = v;
374 	int error;
375 #ifndef SMALL_KERNEL
376 	static int dropped_count, orig_error;
377 	int len;
378 	char buf[64];
379 
380 	if (dropped_count) {
381 		len = snprintf(buf, sizeof(buf),
382 		    "<%d>sendsyslog: dropped %d message%s, error %d",
383 		    LOG_KERN|LOG_WARNING, dropped_count,
384 		    dropped_count == 1 ? "" : "s", orig_error);
385 		error = dosendsyslog(p, buf, MIN((size_t)len, sizeof(buf) - 1),
386 		    0, UIO_SYSSPACE);
387 		if (error == 0)
388 			dropped_count = 0;
389 	}
390 #endif
391 	error = dosendsyslog(p, SCARG(uap, buf), SCARG(uap, nbyte),
392 	    SCARG(uap, flags), UIO_USERSPACE);
393 #ifndef SMALL_KERNEL
394 	if (error) {
395 		dropped_count++;
396 		orig_error = error;
397 	}
398 #endif
399 	return (error);
400 }
401 
402 int
403 dosendsyslog(struct proc *p, const char *buf, size_t nbyte, int flags,
404     enum uio_seg sflg)
405 {
406 #ifdef KTRACE
407 	struct iovec *ktriov = NULL;
408 	int iovlen;
409 #endif
410 	char pri[6], *kbuf;
411 	struct iovec aiov;
412 	struct uio auio;
413 	size_t i, len;
414 	int error;
415 
416 	if (syslogf)
417 		FREF(syslogf);
418 	else if (!ISSET(flags, LOG_CONS))
419 		return (ENOTCONN);
420 	else {
421 		/*
422 		 * Strip off syslog priority when logging to console.
423 		 * LOG_PRIMASK | LOG_FACMASK is 0x03ff, so at most 4
424 		 * decimal digits may appear in priority as <1023>.
425 		 */
426 		len = MIN(nbyte, sizeof(pri));
427 		if (sflg == UIO_USERSPACE) {
428 			if ((error = copyin(buf, pri, len)))
429 				return (error);
430 		} else
431 			memcpy(pri, buf, len);
432 		if (0 < len && pri[0] == '<') {
433 			for (i = 1; i < len; i++) {
434 				if (pri[i] < '0' || pri[i] > '9')
435 					break;
436 			}
437 			if (i < len && pri[i] == '>') {
438 				i++;
439 				/* There must be at least one digit <0>. */
440 				if (i >= 3) {
441 					buf += i;
442 					nbyte -= i;
443 				}
444 			}
445 		}
446 	}
447 
448 	aiov.iov_base = (char *)buf;
449 	aiov.iov_len = nbyte;
450 	auio.uio_iov = &aiov;
451 	auio.uio_iovcnt = 1;
452 	auio.uio_segflg = sflg;
453 	auio.uio_rw = UIO_WRITE;
454 	auio.uio_procp = p;
455 	auio.uio_offset = 0;
456 	auio.uio_resid = aiov.iov_len;
457 #ifdef KTRACE
458 	if (KTRPOINT(p, KTR_GENIO)) {
459 		ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec),
460 		    M_TEMP, M_WAITOK);
461 		iovlen = auio.uio_iovcnt * sizeof (struct iovec);
462 
463 		memcpy(ktriov, auio.uio_iov, iovlen);
464 	}
465 #endif
466 
467 	len = auio.uio_resid;
468 	if (syslogf) {
469 		error = sosend(syslogf->f_data, NULL, &auio, NULL, NULL, 0);
470 		if (error == 0)
471 			len -= auio.uio_resid;
472 	} else if (constty || cn_devvp) {
473 		error = cnwrite(0, &auio, 0);
474 		if (error == 0)
475 			len -= auio.uio_resid;
476 		aiov.iov_base = "\r\n";
477 		aiov.iov_len = 2;
478 		auio.uio_iov = &aiov;
479 		auio.uio_iovcnt = 1;
480 		auio.uio_segflg = UIO_SYSSPACE;
481 		auio.uio_rw = UIO_WRITE;
482 		auio.uio_procp = p;
483 		auio.uio_offset = 0;
484 		auio.uio_resid = aiov.iov_len;
485 		cnwrite(0, &auio, 0);
486 	} else {
487 		/* XXX console redirection breaks down... */
488 		if (sflg == UIO_USERSPACE) {
489 			kbuf = malloc(len, M_TEMP, M_WAITOK);
490 			error = copyin(aiov.iov_base, kbuf, len);
491 		} else {
492 			kbuf = aiov.iov_base;
493 			error = 0;
494 		}
495 		if (error == 0)
496 			for (i = 0; i < len; i++) {
497 				if (kbuf[i] == '\0')
498 					break;
499 				cnputc(kbuf[i]);
500 				auio.uio_resid--;
501 			}
502 		if (sflg == UIO_USERSPACE)
503 			free(kbuf, M_TEMP, len);
504 		if (error == 0)
505 			len -= auio.uio_resid;
506 		cnputc('\n');
507 	}
508 
509 #ifdef KTRACE
510 	if (ktriov != NULL) {
511 		if (error == 0)
512 			ktrgenio(p, -1, UIO_WRITE, ktriov, len);
513 		free(ktriov, M_TEMP, iovlen);
514 	}
515 #endif
516 	if (syslogf)
517 		FRELE(syslogf, p);
518 	else
519 		error = ENOTCONN;
520 	return (error);
521 }
522