subr_log.c revision 1.10 1 /* $NetBSD: subr_log.c,v 1.10 1996/02/09 18:59:58 christos Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93
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/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/ioctl.h>
47 #include <sys/msgbuf.h>
48 #include <sys/file.h>
49 #include <sys/signalvar.h>
50 #include <sys/syslog.h>
51
52 #include <kern/kern_conf.h>
53
54 #define LOG_RDPRI (PZERO + 1)
55
56 #define LOG_ASYNC 0x04
57 #define LOG_RDWAIT 0x08
58
59 struct logsoftc {
60 int sc_state; /* see above for possibilities */
61 struct selinfo sc_selp; /* process waiting on select call */
62 int sc_pgid; /* process/group for async I/O */
63 } logsoftc;
64
65 int log_open; /* also used in log() */
66
67 /*ARGSUSED*/
68 int
69 logopen(dev, flags, mode, p)
70 dev_t dev;
71 int flags, mode;
72 struct proc *p;
73 {
74 register struct msgbuf *mbp = msgbufp;
75
76 if (log_open)
77 return (EBUSY);
78 log_open = 1;
79 logsoftc.sc_pgid = p->p_pid; /* signal process only */
80 /*
81 * Potential race here with putchar() but since putchar should be
82 * called by autoconf, msg_magic should be initialized by the time
83 * we get here.
84 */
85 if (mbp->msg_magic != MSG_MAGIC) {
86 register int i;
87
88 mbp->msg_magic = MSG_MAGIC;
89 mbp->msg_bufx = mbp->msg_bufr = 0;
90 for (i=0; i < MSG_BSIZE; i++)
91 mbp->msg_bufc[i] = 0;
92 }
93 return (0);
94 }
95
96 /*ARGSUSED*/
97 int
98 logclose(dev, flag, mode, p)
99 dev_t dev;
100 int flag, mode;
101 struct proc *p;
102 {
103
104 log_open = 0;
105 logsoftc.sc_state = 0;
106 return (0);
107 }
108
109 /*ARGSUSED*/
110 int
111 logread(dev, uio, flag)
112 dev_t dev;
113 struct uio *uio;
114 int flag;
115 {
116 register struct msgbuf *mbp = msgbufp;
117 register long l;
118 register int s;
119 int error = 0;
120
121 s = splhigh();
122 while (mbp->msg_bufr == mbp->msg_bufx) {
123 if (flag & IO_NDELAY) {
124 splx(s);
125 return (EWOULDBLOCK);
126 }
127 logsoftc.sc_state |= LOG_RDWAIT;
128 error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
129 "klog", 0);
130 if (error) {
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 = MSG_BSIZE - mbp->msg_bufr;
142 l = min(l, uio->uio_resid);
143 if (l == 0)
144 break;
145 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
146 (int)l, uio);
147 if (error)
148 break;
149 mbp->msg_bufr += l;
150 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
151 mbp->msg_bufr = 0;
152 }
153 return (error);
154 }
155
156 /*ARGSUSED*/
157 int
158 logselect(dev, rw, p)
159 dev_t dev;
160 int rw;
161 struct proc *p;
162 {
163 int s = splhigh();
164
165 switch (rw) {
166
167 case FREAD:
168 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
169 splx(s);
170 return (1);
171 }
172 selrecord(p, &logsoftc.sc_selp);
173 break;
174 }
175 splx(s);
176 return (0);
177 }
178
179 void
180 logwakeup()
181 {
182 struct proc *p;
183
184 if (!log_open)
185 return;
186 selwakeup(&logsoftc.sc_selp);
187 if (logsoftc.sc_state & LOG_ASYNC) {
188 if (logsoftc.sc_pgid < 0)
189 gsignal(-logsoftc.sc_pgid, SIGIO);
190 else if ((p = pfind(logsoftc.sc_pgid)) != NULL)
191 psignal(p, SIGIO);
192 }
193 if (logsoftc.sc_state & LOG_RDWAIT) {
194 wakeup((caddr_t)msgbufp);
195 logsoftc.sc_state &= ~LOG_RDWAIT;
196 }
197 }
198
199 /*ARGSUSED*/
200 int
201 logioctl(dev, com, data, flag, p)
202 dev_t dev;
203 u_long com;
204 caddr_t data;
205 int flag;
206 struct proc *p;
207 {
208 long l;
209 int s;
210
211 switch (com) {
212
213 /* return number of characters immediately available */
214 case FIONREAD:
215 s = splhigh();
216 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
217 splx(s);
218 if (l < 0)
219 l += MSG_BSIZE;
220 *(int *)data = l;
221 break;
222
223 case FIONBIO:
224 break;
225
226 case FIOASYNC:
227 if (*(int *)data)
228 logsoftc.sc_state |= LOG_ASYNC;
229 else
230 logsoftc.sc_state &= ~LOG_ASYNC;
231 break;
232
233 case TIOCSPGRP:
234 logsoftc.sc_pgid = *(int *)data;
235 break;
236
237 case TIOCGPGRP:
238 *(int *)data = logsoftc.sc_pgid;
239 break;
240
241 default:
242 return (-1);
243 }
244 return (0);
245 }
246