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