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 * $Id: subr_log.c,v 1.21 1997/03/23 03:36:22 bde 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>
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/fcntl.h>
50#include <sys/signalvar.h>
51#include <sys/kernel.h>
52#ifdef DEVFS
53#include <sys/devfsext.h>
54#endif /*DEVFS*/
55
56#define LOG_RDPRI (PZERO + 1)
57
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_select_t logselect;
66
67#define CDEV_MAJOR 7
68static struct cdevsw log_cdevsw =
69 { logopen, logclose, logread, nowrite, /*7*/
70 logioctl, nostop, nullreset, nodevtotty,/* klog */
71 logselect, nommap, NULL, "log", NULL, -1 };
72
73static struct logsoftc {
74 int sc_state; /* see above for possibilities */
75 struct selinfo sc_selp; /* process waiting on select call */
76 int sc_pgid; /* process/group for async I/O */
77} logsoftc;
78
79int log_open; /* also used in log() */
80
81/*ARGSUSED*/
82static int
83logopen(dev, flags, mode, p)
84 dev_t dev;
85 int flags, mode;
86 struct proc *p;
87{
88 if (log_open)
89 return (EBUSY);
90 log_open = 1;
91 logsoftc.sc_pgid = p->p_pid; /* signal process only */
92 return (0);
93}
94
95/*ARGSUSED*/
96static int
97logclose(dev, flag, mode, p)
98 dev_t dev;
99 int flag, mode;
100 struct proc *p;
101{
102
103 log_open = 0;
104 logsoftc.sc_state = 0;
105 return (0);
106}
107
108/*ARGSUSED*/
109static int
110logread(dev, uio, flag)
111 dev_t dev;
112 struct uio *uio;
113 int flag;
114{
115 register struct msgbuf *mbp = msgbufp;
116 register long l;
117 register int s;
118 int error = 0;
119
120 s = splhigh();
121 while (mbp->msg_bufr == mbp->msg_bufx) {
122 if (flag & IO_NDELAY) {
123 splx(s);
124 return (EWOULDBLOCK);
125 }
126 logsoftc.sc_state |= LOG_RDWAIT;
127 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
128 "klog", 0))) {
129 splx(s);
130 return (error);
131 }
132 }
133 splx(s);
134 logsoftc.sc_state &= ~LOG_RDWAIT;
135
136 while (uio->uio_resid > 0) {
137 l = mbp->msg_bufx - mbp->msg_bufr;
138 if (l < 0)
139 l = MSG_BSIZE - mbp->msg_bufr;
140 l = min(l, uio->uio_resid);
141 if (l == 0)
142 break;
143 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
144 (int)l, uio);
145 if (error)
146 break;
147 mbp->msg_bufr += l;
148 if (mbp->msg_bufr >= MSG_BSIZE)
149 mbp->msg_bufr = 0;
150 }
151 return (error);
152}
153
154/*ARGSUSED*/
155static int
156logselect(dev, rw, p)
157 dev_t dev;
158 int rw;
159 struct proc *p;
160{
161 int s = splhigh();
162
163 switch (rw) {
164
165 case FREAD:
166 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
167 splx(s);
168 return (1);
169 }
170 selrecord(p, &logsoftc.sc_selp);
171 break;
172 }
173 splx(s);
174 return (0);
175}
176
177void
178logwakeup()
179{
180 struct proc *p;
181
182 if (!log_open)
183 return;
184 selwakeup(&logsoftc.sc_selp);
185 if (logsoftc.sc_state & LOG_ASYNC) {
186 if (logsoftc.sc_pgid < 0)
187 gsignal(-logsoftc.sc_pgid, SIGIO);
188 else if ((p = pfind(logsoftc.sc_pgid)))
189 psignal(p, SIGIO);
190 }
191 if (logsoftc.sc_state & LOG_RDWAIT) {
192 wakeup((caddr_t)msgbufp);
193 logsoftc.sc_state &= ~LOG_RDWAIT;
194 }
195}
196
197/*ARGSUSED*/
198static int
199logioctl(dev, com, data, flag, p)
200 dev_t dev;
201 int com;
202 caddr_t data;
203 int flag;
204 struct proc *p;
205{
206 long l;
207 int s;
208
209 switch (com) {
210
211 /* return number of characters immediately available */
212 case FIONREAD:
213 s = splhigh();
214 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
215 splx(s);
216 if (l < 0)
217 l += MSG_BSIZE;
218 *(int *)data = l;
219 break;
220
221 case FIONBIO:
222 break;
223
224 case FIOASYNC:
225 if (*(int *)data)
226 logsoftc.sc_state |= LOG_ASYNC;
227 else
228 logsoftc.sc_state &= ~LOG_ASYNC;
229 break;
230
231 case TIOCSPGRP:
232 logsoftc.sc_pgid = *(int *)data;
233 break;
234
235 case TIOCGPGRP:
236 *(int *)data = logsoftc.sc_pgid;
237 break;
238
239 default:
240 return (ENOTTY);
241 }
242 return (0);
243}
244
245static log_devsw_installed = 0;
246#ifdef DEVFS
247static void *log_devfs_token;
248#endif
249
250static void
251log_drvinit(void *unused)
252{
253 dev_t dev;
254
255 if( ! log_devsw_installed ) {
256 dev = makedev(CDEV_MAJOR,0);
257 cdevsw_add(&dev,&log_cdevsw,NULL);
258 log_devsw_installed = 1;
259#ifdef DEVFS
260 log_devfs_token = devfs_add_devswf(&log_cdevsw, 0, DV_CHR,
261 UID_ROOT, GID_WHEEL, 0600,
262 "klog");
263#endif
264 }
265}
266
267SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
268
269
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 * $Id: subr_log.c,v 1.21 1997/03/23 03:36:22 bde 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>
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/fcntl.h>
50#include <sys/signalvar.h>
51#include <sys/kernel.h>
52#ifdef DEVFS
53#include <sys/devfsext.h>
54#endif /*DEVFS*/
55
56#define LOG_RDPRI (PZERO + 1)
57
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_select_t logselect;
66
67#define CDEV_MAJOR 7
68static struct cdevsw log_cdevsw =
69 { logopen, logclose, logread, nowrite, /*7*/
70 logioctl, nostop, nullreset, nodevtotty,/* klog */
71 logselect, nommap, NULL, "log", NULL, -1 };
72
73static struct logsoftc {
74 int sc_state; /* see above for possibilities */
75 struct selinfo sc_selp; /* process waiting on select call */
76 int sc_pgid; /* process/group for async I/O */
77} logsoftc;
78
79int log_open; /* also used in log() */
80
81/*ARGSUSED*/
82static int
83logopen(dev, flags, mode, p)
84 dev_t dev;
85 int flags, mode;
86 struct proc *p;
87{
88 if (log_open)
89 return (EBUSY);
90 log_open = 1;
91 logsoftc.sc_pgid = p->p_pid; /* signal process only */
92 return (0);
93}
94
95/*ARGSUSED*/
96static int
97logclose(dev, flag, mode, p)
98 dev_t dev;
99 int flag, mode;
100 struct proc *p;
101{
102
103 log_open = 0;
104 logsoftc.sc_state = 0;
105 return (0);
106}
107
108/*ARGSUSED*/
109static int
110logread(dev, uio, flag)
111 dev_t dev;
112 struct uio *uio;
113 int flag;
114{
115 register struct msgbuf *mbp = msgbufp;
116 register long l;
117 register int s;
118 int error = 0;
119
120 s = splhigh();
121 while (mbp->msg_bufr == mbp->msg_bufx) {
122 if (flag & IO_NDELAY) {
123 splx(s);
124 return (EWOULDBLOCK);
125 }
126 logsoftc.sc_state |= LOG_RDWAIT;
127 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
128 "klog", 0))) {
129 splx(s);
130 return (error);
131 }
132 }
133 splx(s);
134 logsoftc.sc_state &= ~LOG_RDWAIT;
135
136 while (uio->uio_resid > 0) {
137 l = mbp->msg_bufx - mbp->msg_bufr;
138 if (l < 0)
139 l = MSG_BSIZE - mbp->msg_bufr;
140 l = min(l, uio->uio_resid);
141 if (l == 0)
142 break;
143 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
144 (int)l, uio);
145 if (error)
146 break;
147 mbp->msg_bufr += l;
148 if (mbp->msg_bufr >= MSG_BSIZE)
149 mbp->msg_bufr = 0;
150 }
151 return (error);
152}
153
154/*ARGSUSED*/
155static int
156logselect(dev, rw, p)
157 dev_t dev;
158 int rw;
159 struct proc *p;
160{
161 int s = splhigh();
162
163 switch (rw) {
164
165 case FREAD:
166 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
167 splx(s);
168 return (1);
169 }
170 selrecord(p, &logsoftc.sc_selp);
171 break;
172 }
173 splx(s);
174 return (0);
175}
176
177void
178logwakeup()
179{
180 struct proc *p;
181
182 if (!log_open)
183 return;
184 selwakeup(&logsoftc.sc_selp);
185 if (logsoftc.sc_state & LOG_ASYNC) {
186 if (logsoftc.sc_pgid < 0)
187 gsignal(-logsoftc.sc_pgid, SIGIO);
188 else if ((p = pfind(logsoftc.sc_pgid)))
189 psignal(p, SIGIO);
190 }
191 if (logsoftc.sc_state & LOG_RDWAIT) {
192 wakeup((caddr_t)msgbufp);
193 logsoftc.sc_state &= ~LOG_RDWAIT;
194 }
195}
196
197/*ARGSUSED*/
198static int
199logioctl(dev, com, data, flag, p)
200 dev_t dev;
201 int com;
202 caddr_t data;
203 int flag;
204 struct proc *p;
205{
206 long l;
207 int s;
208
209 switch (com) {
210
211 /* return number of characters immediately available */
212 case FIONREAD:
213 s = splhigh();
214 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
215 splx(s);
216 if (l < 0)
217 l += MSG_BSIZE;
218 *(int *)data = l;
219 break;
220
221 case FIONBIO:
222 break;
223
224 case FIOASYNC:
225 if (*(int *)data)
226 logsoftc.sc_state |= LOG_ASYNC;
227 else
228 logsoftc.sc_state &= ~LOG_ASYNC;
229 break;
230
231 case TIOCSPGRP:
232 logsoftc.sc_pgid = *(int *)data;
233 break;
234
235 case TIOCGPGRP:
236 *(int *)data = logsoftc.sc_pgid;
237 break;
238
239 default:
240 return (ENOTTY);
241 }
242 return (0);
243}
244
245static log_devsw_installed = 0;
246#ifdef DEVFS
247static void *log_devfs_token;
248#endif
249
250static void
251log_drvinit(void *unused)
252{
253 dev_t dev;
254
255 if( ! log_devsw_installed ) {
256 dev = makedev(CDEV_MAJOR,0);
257 cdevsw_add(&dev,&log_cdevsw,NULL);
258 log_devsw_installed = 1;
259#ifdef DEVFS
260 log_devfs_token = devfs_add_devswf(&log_cdevsw, 0, DV_CHR,
261 UID_ROOT, GID_WHEEL, 0600,
262 "klog");
263#endif
264 }
265}
266
267SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
268
269