xref: /openbsd-src/sys/kern/subr_log.c (revision 6d39e169cabe4138ef00f38ead67a5257450d964)
1 /*	$OpenBSD: subr_log.c,v 1.31 2015/09/02 13:21:37 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/signalvar.h>
47 #include <sys/syslog.h>
48 #include <sys/poll.h>
49 #include <sys/malloc.h>
50 #include <sys/filedesc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 
54 #ifdef KTRACE
55 #include <sys/ktrace.h>
56 #endif
57 
58 #include <sys/mount.h>
59 #include <sys/syscallargs.h>
60 
61 #define LOG_RDPRI	(PZERO + 1)
62 
63 #define LOG_ASYNC	0x04
64 #define LOG_RDWAIT	0x08
65 
66 struct logsoftc {
67 	int	sc_state;		/* see above for possibilities */
68 	struct	selinfo sc_selp;	/* process waiting on select call */
69 	int	sc_pgid;		/* process/group for async I/O */
70 	uid_t	sc_siguid;		/* uid for process that set sc_pgid */
71 	uid_t	sc_sigeuid;		/* euid for process that set sc_pgid */
72 } logsoftc;
73 
74 int	log_open;			/* also used in log() */
75 int	msgbufmapped;			/* is the message buffer mapped */
76 struct	msgbuf *msgbufp;		/* the mapped buffer, itself. */
77 struct	msgbuf *consbufp;		/* console message buffer. */
78 struct file *syslogf;
79 
80 void filt_logrdetach(struct knote *kn);
81 int filt_logread(struct knote *kn, long hint);
82 
83 struct filterops logread_filtops =
84 	{ 1, NULL, filt_logrdetach, filt_logread};
85 
86 int dosendsyslog(struct proc *, const char *, size_t, enum uio_seg);
87 
88 void
89 initmsgbuf(caddr_t buf, size_t bufsize)
90 {
91 	struct msgbuf *mbp;
92 	long new_bufs;
93 
94 	/* Sanity-check the given size. */
95 	if (bufsize < sizeof(struct msgbuf))
96 		return;
97 
98 	mbp = msgbufp = (struct msgbuf *)buf;
99 
100 	new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc);
101 	if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
102 	    (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
103 	    (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
104 		/*
105 		 * If the buffer magic number is wrong, has changed
106 		 * size (which shouldn't happen often), or is
107 		 * internally inconsistent, initialize it.
108 		 */
109 
110 		memset(buf, 0, bufsize);
111 		mbp->msg_magic = MSG_MAGIC;
112 		mbp->msg_bufs = new_bufs;
113 	}
114 
115 	/* Always start new buffer data on a new line. */
116 	if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n')
117 		msgbuf_putchar(msgbufp, '\n');
118 
119 	/* mark it as ready for use. */
120 	msgbufmapped = 1;
121 }
122 
123 void
124 initconsbuf(void)
125 {
126 	long new_bufs;
127 
128 	/* Set up a buffer to collect /dev/console output */
129 	consbufp = malloc(CONSBUFSIZE, M_TEMP, M_NOWAIT|M_ZERO);
130 	if (consbufp) {
131 		new_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc);
132 		consbufp->msg_magic = MSG_MAGIC;
133 		consbufp->msg_bufs = new_bufs;
134 	}
135 }
136 
137 void
138 msgbuf_putchar(struct msgbuf *mbp, const char c)
139 {
140 	if (mbp->msg_magic != MSG_MAGIC)
141 		/* Nothing we can do */
142 		return;
143 
144 	mbp->msg_bufc[mbp->msg_bufx++] = c;
145 	mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs);
146 	if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
147 		mbp->msg_bufx = 0;
148 	/* If the buffer is full, keep the most recent data. */
149 	if (mbp->msg_bufr == mbp->msg_bufx) {
150 		if (++mbp->msg_bufr >= mbp->msg_bufs)
151 			mbp->msg_bufr = 0;
152 	}
153 }
154 
155 /*ARGSUSED*/
156 int
157 logopen(dev_t dev, int flags, int mode, struct proc *p)
158 {
159 	if (log_open)
160 		return (EBUSY);
161 	log_open = 1;
162 	return (0);
163 }
164 
165 /*ARGSUSED*/
166 int
167 logclose(dev_t dev, int flag, int mode, struct proc *p)
168 {
169 
170 	if (syslogf)
171 		FRELE(syslogf, p);
172 	syslogf = NULL;
173 	log_open = 0;
174 	logsoftc.sc_state = 0;
175 	return (0);
176 }
177 
178 /*ARGSUSED*/
179 int
180 logread(dev_t dev, struct uio *uio, int flag)
181 {
182 	struct msgbuf *mbp = msgbufp;
183 	long 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 		l = mbp->msg_bufx - mbp->msg_bufr;
206 		if (l < 0)
207 			l = mbp->msg_bufs - mbp->msg_bufr;
208 		l = min(l, uio->uio_resid);
209 		if (l == 0)
210 			break;
211 		error = uiomovei(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio);
212 		if (error)
213 			break;
214 		mbp->msg_bufr += l;
215 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
216 			mbp->msg_bufr = 0;
217 	}
218 	return (error);
219 }
220 
221 /*ARGSUSED*/
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 /*ARGSUSED*/
297 int
298 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p)
299 {
300 	struct file *fp;
301 	long l;
302 	int error, s;
303 
304 	switch (com) {
305 
306 	/* return number of characters immediately available */
307 	case FIONREAD:
308 		s = splhigh();
309 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
310 		splx(s);
311 		if (l < 0)
312 			l += msgbufp->msg_bufs;
313 		*(int *)data = l;
314 		break;
315 
316 	case FIONBIO:
317 		break;
318 
319 	case FIOASYNC:
320 		if (*(int *)data)
321 			logsoftc.sc_state |= LOG_ASYNC;
322 		else
323 			logsoftc.sc_state &= ~LOG_ASYNC;
324 		break;
325 
326 	case TIOCSPGRP:
327 		logsoftc.sc_pgid = *(int *)data;
328 		logsoftc.sc_siguid = p->p_ucred->cr_ruid;
329 		logsoftc.sc_sigeuid = p->p_ucred->cr_uid;
330 		break;
331 
332 	case TIOCGPGRP:
333 		*(int *)data = logsoftc.sc_pgid;
334 		break;
335 
336 	case LIOCSFD:
337 		if ((error = suser(p, 0)) != 0)
338 			return (error);
339 		if ((error = getsock(p, *(int *)data, &fp)) != 0)
340 			return (error);
341 		if (syslogf)
342 			FRELE(syslogf, p);
343 		syslogf = fp;
344 		break;
345 
346 	default:
347 		return (ENOTTY);
348 	}
349 	return (0);
350 }
351 
352 int
353 sys_sendsyslog(struct proc *p, void *v, register_t *retval)
354 {
355 	struct sys_sendsyslog_args /* {
356 		syscallarg(const void *) buf;
357 		syscallarg(size_t) nbyte;
358 	} */ *uap = v;
359 	int error;
360 #ifndef SMALL_KERNEL
361 	static int dropped_count, orig_error;
362 	int len;
363 	char buf[64];
364 
365 	if (dropped_count) {
366 		len = snprintf(buf, sizeof(buf),
367 		    "<%d> sendsyslog: dropped %d message%s, error %d",
368 		    LOG_KERN|LOG_WARNING, dropped_count,
369 		    dropped_count == 1 ? "" : "s", orig_error);
370 		error = dosendsyslog(p, buf, MIN((size_t)len, sizeof(buf) - 1),
371 		    UIO_SYSSPACE);
372 		if (error) {
373 			dropped_count++;
374 			return (error);
375 		}
376 		dropped_count = 0;
377 	}
378 #endif
379 	error = dosendsyslog(p, SCARG(uap, buf), SCARG(uap, nbyte),
380 	    UIO_USERSPACE);
381 #ifndef SMALL_KERNEL
382 	if (error) {
383 		dropped_count++;
384 		orig_error = error;
385 	}
386 #endif
387 	return (error);
388 }
389 
390 int
391 dosendsyslog(struct proc *p, const char *buf, size_t nbyte, enum uio_seg sflg)
392 {
393 #ifdef KTRACE
394 	struct iovec *ktriov = NULL;
395 	int iovlen;
396 #endif
397 	struct iovec aiov;
398 	struct uio auio;
399 	struct file *f;
400 	size_t len;
401 	int error;
402 
403 	if (syslogf == NULL)
404 		return (ENOTCONN);
405 	f = syslogf;
406 	FREF(f);
407 
408 	aiov.iov_base = (char *)buf;
409 	aiov.iov_len = nbyte;
410 	auio.uio_iov = &aiov;
411 	auio.uio_iovcnt = 1;
412 	auio.uio_segflg = sflg;
413 	auio.uio_rw = UIO_WRITE;
414 	auio.uio_procp = p;
415 	auio.uio_offset = 0;
416 	auio.uio_resid = aiov.iov_len;
417 #ifdef KTRACE
418 	if (KTRPOINT(p, KTR_GENIO)) {
419 		ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec),
420 		    M_TEMP, M_WAITOK);
421 		iovlen = auio.uio_iovcnt * sizeof (struct iovec);
422 
423 		memcpy(ktriov, auio.uio_iov, iovlen);
424 	}
425 #endif
426 
427 	len = auio.uio_resid;
428 	error = sosend(f->f_data, NULL, &auio, NULL, NULL, 0);
429 	if (error == 0)
430 		len -= auio.uio_resid;
431 
432 #ifdef KTRACE
433 	if (ktriov != NULL) {
434 		if (error == 0)
435 			ktrgenio(p, -1, UIO_WRITE, ktriov, len);
436 		free(ktriov, M_TEMP, iovlen);
437 	}
438 #endif
439 	FRELE(f, p);
440 	return error;
441 }
442