kernel - Remove D_KQFILTER flag
[dragonfly.git] / sys / kern / subr_log.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/kern/subr_log.c,v 1.39.2.2 2001/06/02 08:11:25 phk Exp $
fef8985e 35 * $DragonFly: src/sys/kern/subr_log.c,v 1.10 2006/07/28 02:17:40 dillon Exp $
984263bc
MD
36 */
37
38/*
39 * Error log buffer for kernel printf's.
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
fef8985e 45#include <sys/device.h>
984263bc
MD
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>
fef8f01a 53#include <sys/event.h>
984263bc
MD
54#include <sys/filedesc.h>
55#include <sys/sysctl.h>
e43a034f 56#include <sys/thread2.h>
984263bc 57
984263bc
MD
58#define LOG_ASYNC 0x04
59#define LOG_RDWAIT 0x08
60
61static d_open_t logopen;
62static d_close_t logclose;
63static d_read_t logread;
64static d_ioctl_t logioctl;
fef8f01a 65static d_kqfilter_t logkqfilter;
984263bc
MD
66
67static void logtimeout(void *arg);
fef8f01a
SG
68static void logfiltdetach(struct knote *kn);
69static int logfiltread(struct knote *kn, long hint);
984263bc
MD
70
71#define CDEV_MAJOR 7
fef8985e 72static struct dev_ops log_ops = {
d4b8aec4 73 { "log", CDEV_MAJOR, 0 },
fef8985e
MD
74 .d_open = logopen,
75 .d_close = logclose,
76 .d_read = logread,
77 .d_ioctl = logioctl,
fef8f01a 78 .d_kqfilter = logkqfilter
984263bc
MD
79};
80
81static struct logsoftc {
82 int sc_state; /* see above for possibilities */
5b22f1a7 83 struct kqinfo sc_kqp; /* processes waiting on I/O */
984263bc
MD
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
fef8985e 97logopen(struct dev_open_args *ap)
984263bc 98{
fef8985e 99 struct proc *p = curproc;
41c20dac
MD
100
101 KKASSERT(p != NULL);
984263bc
MD
102 if (log_open)
103 return (EBUSY);
104 log_open = 1;
105 callout_init(&logsoftc.sc_callout);
106 fsetown(p->p_pid, &logsoftc.sc_sigio); /* signal process only */
107 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
108 logtimeout, NULL);
109 return (0);
110}
111
112/*ARGSUSED*/
113static int
fef8985e 114logclose(struct dev_close_args *ap)
984263bc 115{
984263bc
MD
116 log_open = 0;
117 callout_stop(&logsoftc.sc_callout);
118 logsoftc.sc_state = 0;
119 funsetown(logsoftc.sc_sigio);
120 return (0);
121}
122
123/*ARGSUSED*/
124static int
fef8985e 125logread(struct dev_read_args *ap)
984263bc 126{
fef8985e 127 struct uio *uio = ap->a_uio;
984263bc
MD
128 struct msgbuf *mbp = msgbufp;
129 long l;
984263bc
MD
130 int error = 0;
131
e43a034f 132 crit_enter();
984263bc 133 while (mbp->msg_bufr == mbp->msg_bufx) {
fef8985e 134 if (ap->a_ioflag & IO_NDELAY) {
e43a034f 135 crit_exit();
984263bc
MD
136 return (EWOULDBLOCK);
137 }
138 logsoftc.sc_state |= LOG_RDWAIT;
377d4740 139 if ((error = tsleep((caddr_t)mbp, PCATCH, "klog", 0))) {
e43a034f 140 crit_exit();
984263bc
MD
141 return (error);
142 }
143 }
e43a034f 144 crit_exit();
984263bc
MD
145 logsoftc.sc_state &= ~LOG_RDWAIT;
146
147 while (uio->uio_resid > 0) {
ef46a41a 148 l = (long)mbp->msg_bufx - (long)mbp->msg_bufr;
984263bc
MD
149 if (l < 0)
150 l = mbp->msg_size - mbp->msg_bufr;
e54488bb 151 l = (long)szmin(l, uio->uio_resid);
984263bc
MD
152 if (l == 0)
153 break;
154 error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
e54488bb 155 (size_t)l, uio);
984263bc
MD
156 if (error)
157 break;
158 mbp->msg_bufr += l;
159 if (mbp->msg_bufr >= mbp->msg_size)
160 mbp->msg_bufr = 0;
161 }
162 return (error);
163}
164
fef8f01a 165static struct filterops logread_filtops =
4c91dbc9 166 { FILTEROP_ISFD, NULL, logfiltdetach, logfiltread };
fef8f01a
SG
167
168static int
169logkqfilter(struct dev_kqfilter_args *ap)
170{
171 struct knote *kn = ap->a_kn;
5b22f1a7 172 struct klist *klist = &logsoftc.sc_kqp.ki_note;
fef8f01a
SG
173
174 ap->a_result = 0;
175 switch (kn->kn_filter) {
176 case EVFILT_READ:
177 kn->kn_fop = &logread_filtops;
178 break;
179 default:
b287d649 180 ap->a_result = EOPNOTSUPP;
fef8f01a
SG
181 return (0);
182 }
183
5b22f1a7 184 knote_insert(klist, kn);
fef8f01a
SG
185
186 return (0);
187}
188
189static void
190logfiltdetach(struct knote *kn)
191{
5b22f1a7 192 struct klist *klist = &logsoftc.sc_kqp.ki_note;
fef8f01a 193
5b22f1a7 194 knote_remove(klist, kn);
fef8f01a
SG
195}
196
197static int
198logfiltread(struct knote *kn, long hint)
199{
200 int ret = 0;
201
202 crit_enter();
203 if (msgbufp->msg_bufr != msgbufp->msg_bufx)
204 ret = 1;
205 crit_exit();
206
207 return (ret);
208}
209
984263bc
MD
210static void
211logtimeout(void *arg)
212{
213
214 if (!log_open)
215 return;
216 if (msgbuftrigger == 0) {
217 callout_reset(&logsoftc.sc_callout,
218 hz / log_wakeups_per_second, logtimeout, NULL);
219 return;
220 }
221 msgbuftrigger = 0;
5b22f1a7 222 KNOTE(&logsoftc.sc_kqp.ki_note, 0);
984263bc
MD
223 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
224 pgsigio(logsoftc.sc_sigio, SIGIO, 0);
225 if (logsoftc.sc_state & LOG_RDWAIT) {
226 wakeup((caddr_t)msgbufp);
227 logsoftc.sc_state &= ~LOG_RDWAIT;
228 }
229 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
230 logtimeout, NULL);
231}
232
233/*ARGSUSED*/
234static int
fef8985e 235logioctl(struct dev_ioctl_args *ap)
984263bc
MD
236{
237 long l;
984263bc 238
fef8985e 239 switch (ap->a_cmd) {
984263bc 240 case FIONREAD:
fef8985e 241 /* return number of characters immediately available */
e43a034f 242 crit_enter();
984263bc 243 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
e43a034f 244 crit_exit();
984263bc
MD
245 if (l < 0)
246 l += msgbufp->msg_size;
fef8985e 247 *(int *)ap->a_data = l;
984263bc
MD
248 break;
249
984263bc 250 case FIOASYNC:
fef8985e 251 if (*(int *)ap->a_data)
984263bc
MD
252 logsoftc.sc_state |= LOG_ASYNC;
253 else
254 logsoftc.sc_state &= ~LOG_ASYNC;
255 break;
256
257 case FIOSETOWN:
fef8985e 258 return (fsetown(*(int *)ap->a_data, &logsoftc.sc_sigio));
984263bc
MD
259
260 case FIOGETOWN:
fef8985e 261 *(int *)ap->a_data = fgetown(logsoftc.sc_sigio);
984263bc
MD
262 break;
263
264 /* This is deprecated, FIOSETOWN should be used instead. */
265 case TIOCSPGRP:
fef8985e 266 return (fsetown(-(*(int *)ap->a_data), &logsoftc.sc_sigio));
984263bc
MD
267
268 /* This is deprecated, FIOGETOWN should be used instead */
269 case TIOCGPGRP:
fef8985e 270 *(int *)ap->a_data = -fgetown(logsoftc.sc_sigio);
984263bc
MD
271 break;
272
273 default:
274 return (ENOTTY);
275 }
276 return (0);
277}
278
279static void
280log_drvinit(void *unused)
281{
fef8985e 282 make_dev(&log_ops, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
984263bc
MD
283}
284
285SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)