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