subr_log.c revision 1.11 1 /* $NetBSD: subr_log.c,v 1.11 1996/03/30 22:24:44 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 #include <sys/conf.h>
52
53 #define LOG_RDPRI (PZERO + 1)
54
55 #define LOG_ASYNC 0x04
56 #define LOG_RDWAIT 0x08
57
58 struct logsoftc {
59 int sc_state; /* see above for possibilities */
60 struct selinfo sc_selp; /* process waiting on select call */
61 int sc_pgid; /* process/group for async I/O */
62 } logsoftc;
63
64 int log_open; /* also used in log() */
65
66 /*ARGSUSED*/
67 int
68 logopen(dev, flags, mode, p)
69 dev_t dev;
70 int flags, mode;
71 struct proc *p;
72 {
73 register struct msgbuf *mbp = msgbufp;
74
75 if (log_open)
76 return (EBUSY);
77 log_open = 1;
78 logsoftc.sc_pgid = p->p_pid; /* signal process only */
79 /*
80 * Potential race here with putchar() but since putchar should be
81 * called by autoconf, msg_magic should be initialized by the time
82 * we get here.
83 */
84 if (mbp->msg_magic != MSG_MAGIC) {
85 register int i;
86
87 mbp->msg_magic = MSG_MAGIC;
88 mbp->msg_bufx = mbp->msg_bufr = 0;
89 for (i=0; i < MSG_BSIZE; i++)
90 mbp->msg_bufc[i] = 0;
91 }
92 return (0);
93 }
94
95 /*ARGSUSED*/
96 int
97 logclose(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*/
109 int
110 logread(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 error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
128 "klog", 0);
129 if (error) {
130 splx(s);
131 return (error);
132 }
133 }
134 splx(s);
135 logsoftc.sc_state &= ~LOG_RDWAIT;
136
137 while (uio->uio_resid > 0) {
138 l = mbp->msg_bufx - mbp->msg_bufr;
139 if (l < 0)
140 l = MSG_BSIZE - mbp->msg_bufr;
141 l = min(l, uio->uio_resid);
142 if (l == 0)
143 break;
144 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
145 (int)l, uio);
146 if (error)
147 break;
148 mbp->msg_bufr += l;
149 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
150 mbp->msg_bufr = 0;
151 }
152 return (error);
153 }
154
155 /*ARGSUSED*/
156 int
157 logselect(dev, rw, p)
158 dev_t dev;
159 int rw;
160 struct proc *p;
161 {
162 int s = splhigh();
163
164 switch (rw) {
165
166 case FREAD:
167 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
168 splx(s);
169 return (1);
170 }
171 selrecord(p, &logsoftc.sc_selp);
172 break;
173 }
174 splx(s);
175 return (0);
176 }
177
178 void
179 logwakeup()
180 {
181 struct proc *p;
182
183 if (!log_open)
184 return;
185 selwakeup(&logsoftc.sc_selp);
186 if (logsoftc.sc_state & LOG_ASYNC) {
187 if (logsoftc.sc_pgid < 0)
188 gsignal(-logsoftc.sc_pgid, SIGIO);
189 else if ((p = pfind(logsoftc.sc_pgid)) != NULL)
190 psignal(p, SIGIO);
191 }
192 if (logsoftc.sc_state & LOG_RDWAIT) {
193 wakeup((caddr_t)msgbufp);
194 logsoftc.sc_state &= ~LOG_RDWAIT;
195 }
196 }
197
198 /*ARGSUSED*/
199 int
200 logioctl(dev, com, data, flag, p)
201 dev_t dev;
202 u_long com;
203 caddr_t data;
204 int flag;
205 struct proc *p;
206 {
207 long l;
208 int s;
209
210 switch (com) {
211
212 /* return number of characters immediately available */
213 case FIONREAD:
214 s = splhigh();
215 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
216 splx(s);
217 if (l < 0)
218 l += MSG_BSIZE;
219 *(int *)data = l;
220 break;
221
222 case FIONBIO:
223 break;
224
225 case FIOASYNC:
226 if (*(int *)data)
227 logsoftc.sc_state |= LOG_ASYNC;
228 else
229 logsoftc.sc_state &= ~LOG_ASYNC;
230 break;
231
232 case TIOCSPGRP:
233 logsoftc.sc_pgid = *(int *)data;
234 break;
235
236 case TIOCGPGRP:
237 *(int *)data = logsoftc.sc_pgid;
238 break;
239
240 default:
241 return (-1);
242 }
243 return (0);
244 }
245