Deleted Added
full compact
subr_log.c (9979) subr_log.c (11921)
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
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.8 1995/05/30 08:05:52 rgrimes Exp $
34 * $Id: subr_log.c,v 1.9 1995/08/07 07:58:17 davidg 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/proc.h>
44#include <sys/vnode.h>
45#include <sys/ioctl.h>
46#include <sys/msgbuf.h>
47#include <sys/file.h>
48#include <sys/signalvar.h>
49
50#define LOG_RDPRI (PZERO + 1)
51
52#define LOG_ASYNC 0x04
53#define LOG_RDWAIT 0x08
54
55struct logsoftc {
56 int sc_state; /* see above for possibilities */
57 struct selinfo sc_selp; /* process waiting on select call */
58 int sc_pgid; /* process/group for async I/O */
59} logsoftc;
60
61int log_open; /* also used in log() */
62
63/*ARGSUSED*/
64int
65logopen(dev, flags, mode, p)
66 dev_t dev;
67 int flags, mode;
68 struct proc *p;
69{
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/proc.h>
44#include <sys/vnode.h>
45#include <sys/ioctl.h>
46#include <sys/msgbuf.h>
47#include <sys/file.h>
48#include <sys/signalvar.h>
49
50#define LOG_RDPRI (PZERO + 1)
51
52#define LOG_ASYNC 0x04
53#define LOG_RDWAIT 0x08
54
55struct logsoftc {
56 int sc_state; /* see above for possibilities */
57 struct selinfo sc_selp; /* process waiting on select call */
58 int sc_pgid; /* process/group for async I/O */
59} logsoftc;
60
61int log_open; /* also used in log() */
62
63/*ARGSUSED*/
64int
65logopen(dev, flags, mode, p)
66 dev_t dev;
67 int flags, mode;
68 struct proc *p;
69{
70 register struct msgbuf *mbp = msgbufp;
71
72 if (log_open)
73 return (EBUSY);
74 log_open = 1;
75 logsoftc.sc_pgid = p->p_pid; /* signal process only */
76 return (0);
77}
78
79/*ARGSUSED*/
80int
81logclose(dev, flag, mode, p)
82 dev_t dev;
83 int flag, mode;
84 struct proc *p;
85{
86
87 log_open = 0;
88 logsoftc.sc_state = 0;
89 return (0);
90}
91
92/*ARGSUSED*/
93int
94logread(dev, uio, flag)
95 dev_t dev;
96 struct uio *uio;
97 int flag;
98{
99 register struct msgbuf *mbp = msgbufp;
100 register long l;
101 register int s;
102 int error = 0;
103
104 s = splhigh();
105 while (mbp->msg_bufr == mbp->msg_bufx) {
106 if (flag & IO_NDELAY) {
107 splx(s);
108 return (EWOULDBLOCK);
109 }
110 logsoftc.sc_state |= LOG_RDWAIT;
111 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
112 "klog", 0))) {
113 splx(s);
114 return (error);
115 }
116 }
117 splx(s);
118 logsoftc.sc_state &= ~LOG_RDWAIT;
119
120 while (uio->uio_resid > 0) {
121 l = mbp->msg_bufx - mbp->msg_bufr;
122 if (l < 0)
123 l = MSG_BSIZE - mbp->msg_bufr;
124 l = min(l, uio->uio_resid);
125 if (l == 0)
126 break;
127 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
128 (int)l, uio);
129 if (error)
130 break;
131 mbp->msg_bufr += l;
132 if (mbp->msg_bufr >= MSG_BSIZE)
133 mbp->msg_bufr = 0;
134 }
135 return (error);
136}
137
138/*ARGSUSED*/
139int
140logselect(dev, rw, p)
141 dev_t dev;
142 int rw;
143 struct proc *p;
144{
145 int s = splhigh();
146
147 switch (rw) {
148
149 case FREAD:
150 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
151 splx(s);
152 return (1);
153 }
154 selrecord(p, &logsoftc.sc_selp);
155 break;
156 }
157 splx(s);
158 return (0);
159}
160
161void
162logwakeup()
163{
164 struct proc *p;
165
166 if (!log_open)
167 return;
168 selwakeup(&logsoftc.sc_selp);
169 if (logsoftc.sc_state & LOG_ASYNC) {
170 if (logsoftc.sc_pgid < 0)
171 gsignal(-logsoftc.sc_pgid, SIGIO);
172 else if ((p = pfind(logsoftc.sc_pgid)))
173 psignal(p, SIGIO);
174 }
175 if (logsoftc.sc_state & LOG_RDWAIT) {
176 wakeup((caddr_t)msgbufp);
177 logsoftc.sc_state &= ~LOG_RDWAIT;
178 }
179}
180
181/*ARGSUSED*/
182int
183logioctl(dev, com, data, flag, p)
184 dev_t dev;
185 int com;
186 caddr_t data;
187 int flag;
188 struct proc *p;
189{
190 long l;
191 int s;
192
193 switch (com) {
194
195 /* return number of characters immediately available */
196 case FIONREAD:
197 s = splhigh();
198 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
199 splx(s);
200 if (l < 0)
201 l += MSG_BSIZE;
202 *(int *)data = l;
203 break;
204
205 case FIONBIO:
206 break;
207
208 case FIOASYNC:
209 if (*(int *)data)
210 logsoftc.sc_state |= LOG_ASYNC;
211 else
212 logsoftc.sc_state &= ~LOG_ASYNC;
213 break;
214
215 case TIOCSPGRP:
216 logsoftc.sc_pgid = *(int *)data;
217 break;
218
219 case TIOCGPGRP:
220 *(int *)data = logsoftc.sc_pgid;
221 break;
222
223 default:
224 return (ENOTTY);
225 }
226 return (0);
227}
70 if (log_open)
71 return (EBUSY);
72 log_open = 1;
73 logsoftc.sc_pgid = p->p_pid; /* signal process only */
74 return (0);
75}
76
77/*ARGSUSED*/
78int
79logclose(dev, flag, mode, p)
80 dev_t dev;
81 int flag, mode;
82 struct proc *p;
83{
84
85 log_open = 0;
86 logsoftc.sc_state = 0;
87 return (0);
88}
89
90/*ARGSUSED*/
91int
92logread(dev, uio, flag)
93 dev_t dev;
94 struct uio *uio;
95 int flag;
96{
97 register struct msgbuf *mbp = msgbufp;
98 register long l;
99 register int s;
100 int error = 0;
101
102 s = splhigh();
103 while (mbp->msg_bufr == mbp->msg_bufx) {
104 if (flag & IO_NDELAY) {
105 splx(s);
106 return (EWOULDBLOCK);
107 }
108 logsoftc.sc_state |= LOG_RDWAIT;
109 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
110 "klog", 0))) {
111 splx(s);
112 return (error);
113 }
114 }
115 splx(s);
116 logsoftc.sc_state &= ~LOG_RDWAIT;
117
118 while (uio->uio_resid > 0) {
119 l = mbp->msg_bufx - mbp->msg_bufr;
120 if (l < 0)
121 l = MSG_BSIZE - mbp->msg_bufr;
122 l = min(l, uio->uio_resid);
123 if (l == 0)
124 break;
125 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
126 (int)l, uio);
127 if (error)
128 break;
129 mbp->msg_bufr += l;
130 if (mbp->msg_bufr >= MSG_BSIZE)
131 mbp->msg_bufr = 0;
132 }
133 return (error);
134}
135
136/*ARGSUSED*/
137int
138logselect(dev, rw, p)
139 dev_t dev;
140 int rw;
141 struct proc *p;
142{
143 int s = splhigh();
144
145 switch (rw) {
146
147 case FREAD:
148 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
149 splx(s);
150 return (1);
151 }
152 selrecord(p, &logsoftc.sc_selp);
153 break;
154 }
155 splx(s);
156 return (0);
157}
158
159void
160logwakeup()
161{
162 struct proc *p;
163
164 if (!log_open)
165 return;
166 selwakeup(&logsoftc.sc_selp);
167 if (logsoftc.sc_state & LOG_ASYNC) {
168 if (logsoftc.sc_pgid < 0)
169 gsignal(-logsoftc.sc_pgid, SIGIO);
170 else if ((p = pfind(logsoftc.sc_pgid)))
171 psignal(p, SIGIO);
172 }
173 if (logsoftc.sc_state & LOG_RDWAIT) {
174 wakeup((caddr_t)msgbufp);
175 logsoftc.sc_state &= ~LOG_RDWAIT;
176 }
177}
178
179/*ARGSUSED*/
180int
181logioctl(dev, com, data, flag, p)
182 dev_t dev;
183 int com;
184 caddr_t data;
185 int flag;
186 struct proc *p;
187{
188 long l;
189 int s;
190
191 switch (com) {
192
193 /* return number of characters immediately available */
194 case FIONREAD:
195 s = splhigh();
196 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
197 splx(s);
198 if (l < 0)
199 l += MSG_BSIZE;
200 *(int *)data = l;
201 break;
202
203 case FIONBIO:
204 break;
205
206 case FIOASYNC:
207 if (*(int *)data)
208 logsoftc.sc_state |= LOG_ASYNC;
209 else
210 logsoftc.sc_state &= ~LOG_ASYNC;
211 break;
212
213 case TIOCSPGRP:
214 logsoftc.sc_pgid = *(int *)data;
215 break;
216
217 case TIOCGPGRP:
218 *(int *)data = logsoftc.sc_pgid;
219 break;
220
221 default:
222 return (ENOTTY);
223 }
224 return (0);
225}