subr_log.c revision 1.55
1/*	$OpenBSD: subr_log.c,v 1.55 2018/02/19 08:59:52 mpi 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
69struct 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
77int	log_open;			/* also used in log() */
78int	msgbufmapped;			/* is the message buffer mapped */
79struct	msgbuf *msgbufp;		/* the mapped buffer, itself. */
80struct	msgbuf *consbufp;		/* console message buffer. */
81struct	file *syslogf;
82
83void filt_logrdetach(struct knote *kn);
84int filt_logread(struct knote *kn, long hint);
85
86struct filterops logread_filtops =
87	{ 1, NULL, filt_logrdetach, filt_logread};
88
89int dosendsyslog(struct proc *, const char *, size_t, int, enum uio_seg);
90
91void
92initmsgbuf(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
126void
127initconsbuf(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
140void
141msgbuf_putchar(struct msgbuf *mbp, const char c)
142{
143	int s;
144
145	if (mbp->msg_magic != MSG_MAGIC)
146		/* Nothing we can do */
147		return;
148
149	s = splhigh();
150	mbp->msg_bufc[mbp->msg_bufx++] = c;
151	mbp->msg_bufl = lmin(mbp->msg_bufl+1, mbp->msg_bufs);
152	if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
153		mbp->msg_bufx = 0;
154	/* If the buffer is full, keep the most recent data. */
155	if (mbp->msg_bufr == mbp->msg_bufx) {
156		if (++mbp->msg_bufr >= mbp->msg_bufs)
157			mbp->msg_bufr = 0;
158		mbp->msg_bufd++;
159	}
160	splx(s);
161}
162
163int
164logopen(dev_t dev, int flags, int mode, struct proc *p)
165{
166	if (log_open)
167		return (EBUSY);
168	log_open = 1;
169	return (0);
170}
171
172int
173logclose(dev_t dev, int flag, int mode, struct proc *p)
174{
175	struct file *fp;
176
177	fp = syslogf;
178	syslogf = NULL;
179	if (fp)
180		FRELE(fp, p);
181	log_open = 0;
182	logsoftc.sc_state = 0;
183	return (0);
184}
185
186int
187logread(dev_t dev, struct uio *uio, int flag)
188{
189	struct msgbuf *mbp = msgbufp;
190	size_t l;
191	int s, error = 0;
192
193	s = splhigh();
194	while (mbp->msg_bufr == mbp->msg_bufx) {
195		if (flag & IO_NDELAY) {
196			error = EWOULDBLOCK;
197			goto out;
198		}
199		logsoftc.sc_state |= LOG_RDWAIT;
200		error = tsleep(mbp, LOG_RDPRI | PCATCH,
201			       "klog", 0);
202		if (error)
203			goto out;
204	}
205	logsoftc.sc_state &= ~LOG_RDWAIT;
206
207	if (mbp->msg_bufd > 0) {
208		char buf[64];
209
210		l = snprintf(buf, sizeof(buf),
211		    "<%d>klog: dropped %ld byte%s, message buffer full\n",
212		    LOG_KERN|LOG_WARNING, mbp->msg_bufd,
213                    mbp->msg_bufd == 1 ? "" : "s");
214		error = uiomove(buf, ulmin(l, sizeof(buf) - 1), uio);
215		if (error)
216			goto out;
217		mbp->msg_bufd = 0;
218	}
219
220	while (uio->uio_resid > 0) {
221		if (mbp->msg_bufx >= mbp->msg_bufr)
222			l = mbp->msg_bufx - mbp->msg_bufr;
223		else
224			l = mbp->msg_bufs - mbp->msg_bufr;
225		l = ulmin(l, uio->uio_resid);
226		if (l == 0)
227			break;
228		error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], l, uio);
229		if (error)
230			break;
231		mbp->msg_bufr += l;
232		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
233			mbp->msg_bufr = 0;
234	}
235 out:
236	splx(s);
237	return (error);
238}
239
240int
241logpoll(dev_t dev, int events, struct proc *p)
242{
243	int s, revents = 0;
244
245	s = splhigh();
246	if (events & (POLLIN | POLLRDNORM)) {
247		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
248			revents |= events & (POLLIN | POLLRDNORM);
249		else
250			selrecord(p, &logsoftc.sc_selp);
251	}
252	splx(s);
253	return (revents);
254}
255
256int
257logkqfilter(dev_t dev, struct knote *kn)
258{
259	struct klist *klist;
260	int s;
261
262	switch (kn->kn_filter) {
263	case EVFILT_READ:
264		klist = &logsoftc.sc_selp.si_note;
265		kn->kn_fop = &logread_filtops;
266		break;
267	default:
268		return (EINVAL);
269	}
270
271	kn->kn_hook = (void *)msgbufp;
272
273	s = splhigh();
274	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
275	splx(s);
276
277	return (0);
278}
279
280void
281filt_logrdetach(struct knote *kn)
282{
283	int s;
284
285	s = splhigh();
286	SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext);
287	splx(s);
288}
289
290int
291filt_logread(struct knote *kn, long hint)
292{
293	struct  msgbuf *p = (struct  msgbuf *)kn->kn_hook;
294	int s, event = 0;
295
296	s = splhigh();
297	kn->kn_data = (int)(p->msg_bufx - p->msg_bufr);
298	event = (p->msg_bufx != p->msg_bufr);
299	splx(s);
300	return (event);
301}
302
303void
304logwakeup(void)
305{
306	if (!log_open)
307		return;
308	selwakeup(&logsoftc.sc_selp);
309	if (logsoftc.sc_state & LOG_ASYNC)
310		csignal(logsoftc.sc_pgid, SIGIO,
311		    logsoftc.sc_siguid, logsoftc.sc_sigeuid);
312	if (logsoftc.sc_state & LOG_RDWAIT) {
313		wakeup(msgbufp);
314		logsoftc.sc_state &= ~LOG_RDWAIT;
315	}
316}
317
318int
319logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p)
320{
321	struct file *fp;
322	long l;
323	int error, s;
324
325	switch (com) {
326
327	/* return number of characters immediately available */
328	case FIONREAD:
329		s = splhigh();
330		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
331		splx(s);
332		if (l < 0)
333			l += msgbufp->msg_bufs;
334		*(int *)data = l;
335		break;
336
337	case FIONBIO:
338		break;
339
340	case FIOASYNC:
341		if (*(int *)data)
342			logsoftc.sc_state |= LOG_ASYNC;
343		else
344			logsoftc.sc_state &= ~LOG_ASYNC;
345		break;
346
347	case TIOCSPGRP:
348		logsoftc.sc_pgid = *(int *)data;
349		logsoftc.sc_siguid = p->p_ucred->cr_ruid;
350		logsoftc.sc_sigeuid = p->p_ucred->cr_uid;
351		break;
352
353	case TIOCGPGRP:
354		*(int *)data = logsoftc.sc_pgid;
355		break;
356
357	case LIOCSFD:
358		if ((error = suser(p)) != 0)
359			return (error);
360		fp = syslogf;
361		if ((error = getsock(p, *(int *)data, &syslogf)) != 0)
362			return (error);
363		if (fp)
364			FRELE(fp, p);
365		break;
366
367	default:
368		return (ENOTTY);
369	}
370	return (0);
371}
372
373int
374sys_sendsyslog(struct proc *p, void *v, register_t *retval)
375{
376	struct sys_sendsyslog_args /* {
377		syscallarg(const char *) buf;
378		syscallarg(size_t) nbyte;
379		syscallarg(int) flags;
380	} */ *uap = v;
381	int error;
382	static int dropped_count, orig_error, orig_pid;
383
384	if (dropped_count) {
385		size_t l;
386		char buf[80];
387
388		l = snprintf(buf, sizeof(buf),
389		    "<%d>sendsyslog: dropped %d message%s, error %d, pid %d",
390		    LOG_KERN|LOG_WARNING, dropped_count,
391		    dropped_count == 1 ? "" : "s", orig_error, orig_pid);
392		error = dosendsyslog(p, buf, ulmin(l, sizeof(buf) - 1),
393		    0, UIO_SYSSPACE);
394		if (error == 0) {
395			dropped_count = 0;
396			orig_error = 0;
397			orig_pid = 0;
398		}
399	}
400	error = dosendsyslog(p, SCARG(uap, buf), SCARG(uap, nbyte),
401	    SCARG(uap, flags), UIO_USERSPACE);
402	if (error) {
403		dropped_count++;
404		orig_error = error;
405		orig_pid = p->p_p->ps_pid;
406	}
407	return (error);
408}
409
410int
411dosendsyslog(struct proc *p, const char *buf, size_t nbyte, int flags,
412    enum uio_seg sflg)
413{
414#ifdef KTRACE
415	struct iovec *ktriov = NULL;
416	int iovlen;
417#endif
418	struct file *fp;
419	char pri[6], *kbuf;
420	struct iovec aiov;
421	struct uio auio;
422	size_t i, len;
423	int error;
424
425	if (nbyte > LOG_MAXLINE)
426		nbyte = LOG_MAXLINE;
427
428	/* Global variable syslogf may change during sleep, use local copy. */
429	fp = syslogf;
430	if (fp)
431		FREF(fp);
432	else if (!ISSET(flags, LOG_CONS))
433		return (ENOTCONN);
434	else {
435		/*
436		 * Strip off syslog priority when logging to console.
437		 * LOG_PRIMASK | LOG_FACMASK is 0x03ff, so at most 4
438		 * decimal digits may appear in priority as <1023>.
439		 */
440		len = MIN(nbyte, sizeof(pri));
441		if (sflg == UIO_USERSPACE) {
442			if ((error = copyin(buf, pri, len)))
443				return (error);
444		} else
445			memcpy(pri, buf, len);
446		if (0 < len && pri[0] == '<') {
447			for (i = 1; i < len; i++) {
448				if (pri[i] < '0' || pri[i] > '9')
449					break;
450			}
451			if (i < len && pri[i] == '>') {
452				i++;
453				/* There must be at least one digit <0>. */
454				if (i >= 3) {
455					buf += i;
456					nbyte -= i;
457				}
458			}
459		}
460	}
461
462	aiov.iov_base = (char *)buf;
463	aiov.iov_len = nbyte;
464	auio.uio_iov = &aiov;
465	auio.uio_iovcnt = 1;
466	auio.uio_segflg = sflg;
467	auio.uio_rw = UIO_WRITE;
468	auio.uio_procp = p;
469	auio.uio_offset = 0;
470	auio.uio_resid = aiov.iov_len;
471#ifdef KTRACE
472	if (KTRPOINT(p, KTR_GENIO)) {
473		ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec),
474		    M_TEMP, M_WAITOK);
475		iovlen = auio.uio_iovcnt * sizeof (struct iovec);
476
477		memcpy(ktriov, auio.uio_iov, iovlen);
478	}
479#endif
480
481	len = auio.uio_resid;
482	if (fp) {
483		error = sosend(fp->f_data, NULL, &auio, NULL, NULL, 0);
484		if (error == 0)
485			len -= auio.uio_resid;
486	} else if (constty || cn_devvp) {
487		error = cnwrite(0, &auio, 0);
488		if (error == 0)
489			len -= auio.uio_resid;
490		aiov.iov_base = "\r\n";
491		aiov.iov_len = 2;
492		auio.uio_iov = &aiov;
493		auio.uio_iovcnt = 1;
494		auio.uio_segflg = UIO_SYSSPACE;
495		auio.uio_rw = UIO_WRITE;
496		auio.uio_procp = p;
497		auio.uio_offset = 0;
498		auio.uio_resid = aiov.iov_len;
499		cnwrite(0, &auio, 0);
500	} else {
501		/* XXX console redirection breaks down... */
502		if (sflg == UIO_USERSPACE) {
503			kbuf = malloc(len, M_TEMP, M_WAITOK);
504			error = copyin(aiov.iov_base, kbuf, len);
505		} else {
506			kbuf = aiov.iov_base;
507			error = 0;
508		}
509		if (error == 0)
510			for (i = 0; i < len; i++) {
511				if (kbuf[i] == '\0')
512					break;
513				cnputc(kbuf[i]);
514				auio.uio_resid--;
515			}
516		if (sflg == UIO_USERSPACE)
517			free(kbuf, M_TEMP, len);
518		if (error == 0)
519			len -= auio.uio_resid;
520		cnputc('\n');
521	}
522
523#ifdef KTRACE
524	if (ktriov != NULL) {
525		if (error == 0)
526			ktrgenio(p, -1, UIO_WRITE, ktriov, len);
527		free(ktriov, M_TEMP, iovlen);
528	}
529#endif
530	if (fp)
531		FRELE(fp, p);
532	else
533		error = ENOTCONN;
534	return (error);
535}
536