AMD64 - Refactor uio_resid and size_t assumptions.
[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>
53#include <sys/poll.h>
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;
65static d_poll_t logpoll;
66
67static void logtimeout(void *arg);
68
69#define CDEV_MAJOR 7
fef8985e
MD
70static struct dev_ops log_ops = {
71 { "log", CDEV_MAJOR, 0 },
72 .d_open = logopen,
73 .d_close = logclose,
74 .d_read = logread,
75 .d_ioctl = logioctl,
76 .d_poll = logpoll,
984263bc
MD
77};
78
79static struct logsoftc {
80 int sc_state; /* see above for possibilities */
81 struct selinfo sc_selp; /* process waiting on select call */
82 struct sigio *sc_sigio; /* information for async I/O */
83 struct callout sc_callout; /* callout to wakeup syslog */
84} logsoftc;
85
86int log_open; /* also used in log() */
87
88/* Times per second to check for a pending syslog wakeup. */
89static int log_wakeups_per_second = 5;
90SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
91 &log_wakeups_per_second, 0, "");
92
93/*ARGSUSED*/
94static int
fef8985e 95logopen(struct dev_open_args *ap)
984263bc 96{
fef8985e 97 struct proc *p = curproc;
41c20dac
MD
98
99 KKASSERT(p != NULL);
984263bc
MD
100 if (log_open)
101 return (EBUSY);
102 log_open = 1;
103 callout_init(&logsoftc.sc_callout);
104 fsetown(p->p_pid, &logsoftc.sc_sigio); /* signal process only */
105 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
106 logtimeout, NULL);
107 return (0);
108}
109
110/*ARGSUSED*/
111static int
fef8985e 112logclose(struct dev_close_args *ap)
984263bc 113{
984263bc
MD
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
fef8985e 123logread(struct dev_read_args *ap)
984263bc 124{
fef8985e 125 struct uio *uio = ap->a_uio;
984263bc
MD
126 struct msgbuf *mbp = msgbufp;
127 long l;
984263bc
MD
128 int error = 0;
129
e43a034f 130 crit_enter();
984263bc 131 while (mbp->msg_bufr == mbp->msg_bufx) {
fef8985e 132 if (ap->a_ioflag & IO_NDELAY) {
e43a034f 133 crit_exit();
984263bc
MD
134 return (EWOULDBLOCK);
135 }
136 logsoftc.sc_state |= LOG_RDWAIT;
377d4740 137 if ((error = tsleep((caddr_t)mbp, PCATCH, "klog", 0))) {
e43a034f 138 crit_exit();
984263bc
MD
139 return (error);
140 }
141 }
e43a034f 142 crit_exit();
984263bc
MD
143 logsoftc.sc_state &= ~LOG_RDWAIT;
144
145 while (uio->uio_resid > 0) {
146 l = mbp->msg_bufx - mbp->msg_bufr;
147 if (l < 0)
148 l = mbp->msg_size - mbp->msg_bufr;
e54488bb 149 l = (long)szmin(l, uio->uio_resid);
984263bc
MD
150 if (l == 0)
151 break;
152 error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
e54488bb 153 (size_t)l, uio);
984263bc
MD
154 if (error)
155 break;
156 mbp->msg_bufr += l;
157 if (mbp->msg_bufr >= mbp->msg_size)
158 mbp->msg_bufr = 0;
159 }
160 return (error);
161}
162
163/*ARGSUSED*/
164static int
fef8985e 165logpoll(struct dev_poll_args *ap)
984263bc 166{
984263bc
MD
167 int revents = 0;
168
e43a034f 169 crit_enter();
fef8985e 170 if (ap->a_events & (POLLIN | POLLRDNORM)) {
984263bc 171 if (msgbufp->msg_bufr != msgbufp->msg_bufx)
fef8985e 172 revents |= ap->a_events & (POLLIN | POLLRDNORM);
984263bc 173 else
fef8985e 174 selrecord(curthread, &logsoftc.sc_selp);
984263bc 175 }
e43a034f 176 crit_exit();
fef8985e
MD
177 ap->a_events = revents;
178 return (0);
984263bc
MD
179}
180
181static void
182logtimeout(void *arg)
183{
184
185 if (!log_open)
186 return;
187 if (msgbuftrigger == 0) {
188 callout_reset(&logsoftc.sc_callout,
189 hz / log_wakeups_per_second, logtimeout, NULL);
190 return;
191 }
192 msgbuftrigger = 0;
193 selwakeup(&logsoftc.sc_selp);
194 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
195 pgsigio(logsoftc.sc_sigio, SIGIO, 0);
196 if (logsoftc.sc_state & LOG_RDWAIT) {
197 wakeup((caddr_t)msgbufp);
198 logsoftc.sc_state &= ~LOG_RDWAIT;
199 }
200 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
201 logtimeout, NULL);
202}
203
204/*ARGSUSED*/
205static int
fef8985e 206logioctl(struct dev_ioctl_args *ap)
984263bc
MD
207{
208 long l;
984263bc 209
fef8985e 210 switch (ap->a_cmd) {
984263bc 211 case FIONREAD:
fef8985e 212 /* return number of characters immediately available */
e43a034f 213 crit_enter();
984263bc 214 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
e43a034f 215 crit_exit();
984263bc
MD
216 if (l < 0)
217 l += msgbufp->msg_size;
fef8985e 218 *(int *)ap->a_data = l;
984263bc
MD
219 break;
220
984263bc 221 case FIOASYNC:
fef8985e 222 if (*(int *)ap->a_data)
984263bc
MD
223 logsoftc.sc_state |= LOG_ASYNC;
224 else
225 logsoftc.sc_state &= ~LOG_ASYNC;
226 break;
227
228 case FIOSETOWN:
fef8985e 229 return (fsetown(*(int *)ap->a_data, &logsoftc.sc_sigio));
984263bc
MD
230
231 case FIOGETOWN:
fef8985e 232 *(int *)ap->a_data = fgetown(logsoftc.sc_sigio);
984263bc
MD
233 break;
234
235 /* This is deprecated, FIOSETOWN should be used instead. */
236 case TIOCSPGRP:
fef8985e 237 return (fsetown(-(*(int *)ap->a_data), &logsoftc.sc_sigio));
984263bc
MD
238
239 /* This is deprecated, FIOGETOWN should be used instead */
240 case TIOCGPGRP:
fef8985e 241 *(int *)ap->a_data = -fgetown(logsoftc.sc_sigio);
984263bc
MD
242 break;
243
244 default:
245 return (ENOTTY);
246 }
247 return (0);
248}
249
250static void
251log_drvinit(void *unused)
252{
fef8985e 253 make_dev(&log_ops, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
984263bc
MD
254}
255
256SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)