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