FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_log.c
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$
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>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/filio.h>
47 #include <sys/ttycom.h>
48 #include <sys/msgbuf.h>
49 #include <sys/signalvar.h>
50 #include <sys/kernel.h>
51 #include <sys/poll.h>
52 #include <sys/filedesc.h>
53 #include <sys/sysctl.h>
54
55 #define LOG_RDPRI (PZERO + 1)
56
57 #define LOG_ASYNC 0x04
58 #define LOG_RDWAIT 0x08
59
60 static d_open_t logopen;
61 static d_close_t logclose;
62 static d_read_t logread;
63 static d_ioctl_t logioctl;
64 static d_poll_t logpoll;
65
66 static void logtimeout(void *arg);
67
68 #define CDEV_MAJOR 7
69 static struct cdevsw log_cdevsw = {
70 /* open */ logopen,
71 /* close */ logclose,
72 /* read */ logread,
73 /* write */ nowrite,
74 /* ioctl */ logioctl,
75 /* poll */ logpoll,
76 /* mmap */ nommap,
77 /* strategy */ nostrategy,
78 /* name */ "log",
79 /* maj */ CDEV_MAJOR,
80 /* dump */ nodump,
81 /* psize */ nopsize,
82 /* flags */ 0,
83 /* bmaj */ -1
84 };
85
86 static struct logsoftc {
87 int sc_state; /* see above for possibilities */
88 struct selinfo sc_selp; /* process waiting on select call */
89 struct sigio *sc_sigio; /* information for async I/O */
90 struct callout sc_callout; /* callout to wakeup syslog */
91 } logsoftc;
92
93 int log_open; /* also used in log() */
94
95 /* Times per second to check for a pending syslog wakeup. */
96 static int log_wakeups_per_second = 5;
97 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
98 &log_wakeups_per_second, 0, "");
99
100 /*ARGSUSED*/
101 static int
102 logopen(dev_t dev, int flags, int mode, struct proc *p)
103 {
104 if (log_open)
105 return (EBUSY);
106 log_open = 1;
107 callout_init(&logsoftc.sc_callout);
108 fsetown(p->p_pid, &logsoftc.sc_sigio); /* signal process only */
109 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
110 logtimeout, NULL);
111 return (0);
112 }
113
114 /*ARGSUSED*/
115 static int
116 logclose(dev_t dev, int flag, int mode, struct proc *p)
117 {
118
119 log_open = 0;
120 callout_stop(&logsoftc.sc_callout);
121 logsoftc.sc_state = 0;
122 funsetown(logsoftc.sc_sigio);
123 return (0);
124 }
125
126 /*ARGSUSED*/
127 static int
128 logread(dev_t dev, struct uio *uio, int flag)
129 {
130 struct msgbuf *mbp = msgbufp;
131 long l;
132 int s;
133 int error = 0;
134
135 s = splhigh();
136 while (mbp->msg_bufr == mbp->msg_bufx) {
137 if (flag & IO_NDELAY) {
138 splx(s);
139 return (EWOULDBLOCK);
140 }
141 logsoftc.sc_state |= LOG_RDWAIT;
142 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
143 "klog", 0))) {
144 splx(s);
145 return (error);
146 }
147 }
148 splx(s);
149 logsoftc.sc_state &= ~LOG_RDWAIT;
150
151 while (uio->uio_resid > 0) {
152 l = mbp->msg_bufx - mbp->msg_bufr;
153 if (l < 0)
154 l = mbp->msg_size - mbp->msg_bufr;
155 l = min(l, uio->uio_resid);
156 if (l == 0)
157 break;
158 error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
159 (int)l, uio);
160 if (error)
161 break;
162 mbp->msg_bufr += l;
163 if (mbp->msg_bufr >= mbp->msg_size)
164 mbp->msg_bufr = 0;
165 }
166 return (error);
167 }
168
169 /*ARGSUSED*/
170 static int
171 logpoll(dev_t dev, int events, struct proc *p)
172 {
173 int s;
174 int revents = 0;
175
176 s = splhigh();
177
178 if (events & (POLLIN | POLLRDNORM)) {
179 if (msgbufp->msg_bufr != msgbufp->msg_bufx)
180 revents |= events & (POLLIN | POLLRDNORM);
181 else
182 selrecord(p, &logsoftc.sc_selp);
183 }
184 splx(s);
185 return (revents);
186 }
187
188 static void
189 logtimeout(void *arg)
190 {
191
192 if (!log_open)
193 return;
194 if (msgbuftrigger == 0) {
195 callout_reset(&logsoftc.sc_callout,
196 hz / log_wakeups_per_second, logtimeout, NULL);
197 return;
198 }
199 msgbuftrigger = 0;
200 selwakeup(&logsoftc.sc_selp);
201 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
202 pgsigio(logsoftc.sc_sigio, SIGIO, 0);
203 if (logsoftc.sc_state & LOG_RDWAIT) {
204 wakeup((caddr_t)msgbufp);
205 logsoftc.sc_state &= ~LOG_RDWAIT;
206 }
207 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
208 logtimeout, NULL);
209 }
210
211 /*ARGSUSED*/
212 static int
213 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p)
214 {
215 long l;
216 int s;
217
218 switch (com) {
219
220 /* return number of characters immediately available */
221 case FIONREAD:
222 s = splhigh();
223 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
224 splx(s);
225 if (l < 0)
226 l += msgbufp->msg_size;
227 *(int *)data = l;
228 break;
229
230 case FIONBIO:
231 break;
232
233 case FIOASYNC:
234 if (*(int *)data)
235 logsoftc.sc_state |= LOG_ASYNC;
236 else
237 logsoftc.sc_state &= ~LOG_ASYNC;
238 break;
239
240 case FIOSETOWN:
241 return (fsetown(*(int *)data, &logsoftc.sc_sigio));
242
243 case FIOGETOWN:
244 *(int *)data = fgetown(logsoftc.sc_sigio);
245 break;
246
247 /* This is deprecated, FIOSETOWN should be used instead. */
248 case TIOCSPGRP:
249 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
250
251 /* This is deprecated, FIOGETOWN should be used instead */
252 case TIOCGPGRP:
253 *(int *)data = -fgetown(logsoftc.sc_sigio);
254 break;
255
256 default:
257 return (ENOTTY);
258 }
259 return (0);
260 }
261
262 static void
263 log_drvinit(void *unused)
264 {
265
266 make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
267 }
268
269 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
Cache object: 2874bb962113b2ce074e226dd04a1107
|