xref: /freebsd/sys/kern/subr_log.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*-
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
30  */
31 
32 /*
33  * Error log buffer for kernel printf's.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/filio.h>
45 #include <sys/ttycom.h>
46 #include <sys/msgbuf.h>
47 #include <sys/signalvar.h>
48 #include <sys/kernel.h>
49 #include <sys/poll.h>
50 #include <sys/filedesc.h>
51 #include <sys/sysctl.h>
52 
53 #define LOG_RDPRI	(PZERO + 1)
54 
55 #define LOG_ASYNC	0x04
56 #define LOG_RDWAIT	0x08
57 
58 static	d_open_t	logopen;
59 static	d_close_t	logclose;
60 static	d_read_t	logread;
61 static	d_ioctl_t	logioctl;
62 static	d_poll_t	logpoll;
63 
64 static	void logtimeout(void *arg);
65 
66 static struct cdevsw log_cdevsw = {
67 	.d_version =	D_VERSION,
68 	.d_flags =	D_NEEDGIANT,
69 	.d_open =	logopen,
70 	.d_close =	logclose,
71 	.d_read =	logread,
72 	.d_ioctl =	logioctl,
73 	.d_poll =	logpoll,
74 	.d_name =	"log",
75 };
76 
77 static struct logsoftc {
78 	int	sc_state;		/* see above for possibilities */
79 	struct	selinfo sc_selp;	/* process waiting on select call */
80 	struct  sigio *sc_sigio;	/* information for async I/O */
81 	struct	callout sc_callout;	/* callout to wakeup syslog  */
82 } logsoftc;
83 
84 int	log_open;			/* also used in log() */
85 
86 /* Times per second to check for a pending syslog wakeup. */
87 static int	log_wakeups_per_second = 5;
88 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
89     &log_wakeups_per_second, 0, "");
90 
91 /*ARGSUSED*/
92 static	int
93 logopen(struct cdev *dev, int flags, int mode, struct thread *td)
94 {
95 	if (log_open)
96 		return (EBUSY);
97 	log_open = 1;
98 	callout_init(&logsoftc.sc_callout, 0);
99 	fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio);	/* signal process only */
100 	if (log_wakeups_per_second < 1) {
101 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
102 		log_wakeups_per_second = 1;
103 	}
104 	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
105 	    logtimeout, NULL);
106 	return (0);
107 }
108 
109 /*ARGSUSED*/
110 static	int
111 logclose(struct cdev *dev, int flag, int mode, struct thread *td)
112 {
113 
114 	log_open = 0;
115 	callout_stop(&logsoftc.sc_callout);
116 	logsoftc.sc_state = 0;
117 	funsetown(&logsoftc.sc_sigio);
118 	return (0);
119 }
120 
121 /*ARGSUSED*/
122 static	int
123 logread(struct cdev *dev, struct uio *uio, int flag)
124 {
125 	char buf[128];
126 	struct msgbuf *mbp = msgbufp;
127 	int error = 0, l, s;
128 
129 	s = splhigh();
130 	while (msgbuf_getcount(mbp) == 0) {
131 		if (flag & IO_NDELAY) {
132 			splx(s);
133 			return (EWOULDBLOCK);
134 		}
135 		logsoftc.sc_state |= LOG_RDWAIT;
136 		if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) {
137 			splx(s);
138 			return (error);
139 		}
140 	}
141 	splx(s);
142 	logsoftc.sc_state &= ~LOG_RDWAIT;
143 
144 	while (uio->uio_resid > 0) {
145 		l = imin(sizeof(buf), uio->uio_resid);
146 		l = msgbuf_getbytes(mbp, buf, l);
147 		if (l == 0)
148 			break;
149 		error = uiomove(buf, l, uio);
150 		if (error)
151 			break;
152 	}
153 	return (error);
154 }
155 
156 /*ARGSUSED*/
157 static	int
158 logpoll(struct cdev *dev, int events, struct thread *td)
159 {
160 	int s;
161 	int revents = 0;
162 
163 	s = splhigh();
164 
165 	if (events & (POLLIN | POLLRDNORM)) {
166 		if (msgbuf_getcount(msgbufp) > 0)
167 			revents |= events & (POLLIN | POLLRDNORM);
168 		else
169 			selrecord(td, &logsoftc.sc_selp);
170 	}
171 	splx(s);
172 	return (revents);
173 }
174 
175 static void
176 logtimeout(void *arg)
177 {
178 
179 	if (!log_open)
180 		return;
181 	if (log_wakeups_per_second < 1) {
182 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
183 		log_wakeups_per_second = 1;
184 	}
185 	if (msgbuftrigger == 0) {
186 		callout_reset(&logsoftc.sc_callout,
187 		    hz / log_wakeups_per_second, logtimeout, NULL);
188 		return;
189 	}
190 	msgbuftrigger = 0;
191 	selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI);
192 	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
193 		pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
194 	if (logsoftc.sc_state & LOG_RDWAIT) {
195 		wakeup(msgbufp);
196 		logsoftc.sc_state &= ~LOG_RDWAIT;
197 	}
198 	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
199 	    logtimeout, NULL);
200 }
201 
202 /*ARGSUSED*/
203 static	int
204 logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
205 {
206 
207 	switch (com) {
208 
209 	/* return number of characters immediately available */
210 	case FIONREAD:
211 		*(int *)data = msgbuf_getcount(msgbufp);
212 		break;
213 
214 	case FIONBIO:
215 		break;
216 
217 	case FIOASYNC:
218 		if (*(int *)data)
219 			logsoftc.sc_state |= LOG_ASYNC;
220 		else
221 			logsoftc.sc_state &= ~LOG_ASYNC;
222 		break;
223 
224 	case FIOSETOWN:
225 		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
226 
227 	case FIOGETOWN:
228 		*(int *)data = fgetown(&logsoftc.sc_sigio);
229 		break;
230 
231 	/* This is deprecated, FIOSETOWN should be used instead. */
232 	case TIOCSPGRP:
233 		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
234 
235 	/* This is deprecated, FIOGETOWN should be used instead */
236 	case TIOCGPGRP:
237 		*(int *)data = -fgetown(&logsoftc.sc_sigio);
238 		break;
239 
240 	default:
241 		return (ENOTTY);
242 	}
243 	return (0);
244 }
245 
246 static void
247 log_drvinit(void *unused)
248 {
249 
250 	make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
251 }
252 
253 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL)
254