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