Removed all patch kit headers, sccsid and rcsid strings, put $Id$ in, some
[unix-history] / sys / kern / subr_log.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986 Regents of the University of California.
3 * 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 7.11 (Berkeley) 3/17/91
d6075007
NW
34 *
35 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
36 * -------------------- ----- ----------------------
37 * CURRENT PATCH LEVEL: 1 XXXXX
38 * -------------------- ----- ----------------------
39 *
40 * 16 Jun 93 Juha Nurmela select uses pids, not pointers
15637ed4
RG
41 */
42
43/*
44 * Error log buffer for kernel printf's.
45 */
46
47#include "param.h"
dd18dc33 48#include "systm.h"
15637ed4
RG
49#include "proc.h"
50#include "vnode.h"
51#include "ioctl.h"
52#include "msgbuf.h"
53#include "file.h"
54
55#define LOG_RDPRI (PZERO + 1)
56
57#define LOG_ASYNC 0x04
58#define LOG_RDWAIT 0x08
59
60struct logsoftc {
61 int sc_state; /* see above for possibilities */
d6075007 62 pid_t sc_sel; /* pid of process waiting on select call 16 Jun 93 */
15637ed4
RG
63 int sc_pgid; /* process/group for async I/O */
64} logsoftc;
65
66int log_open; /* also used in log() */
67
68/*ARGSUSED*/
69logopen(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*/
97logclose(dev, flag)
98 dev_t dev;
99{
100 log_open = 0;
101 logsoftc.sc_state = 0;
d6075007 102 logsoftc.sc_sel = 0; /* 16 Jun 93 */
15637ed4
RG
103}
104
105/*ARGSUSED*/
106logread(dev, uio, flag)
107 dev_t dev;
108 struct uio *uio;
109 int flag;
110{
111 register struct msgbuf *mbp = msgbufp;
112 register long l;
113 register int s;
114 int error = 0;
115
116 s = splhigh();
117 while (mbp->msg_bufr == mbp->msg_bufx) {
118 if (flag & IO_NDELAY) {
119 splx(s);
120 return (EWOULDBLOCK);
121 }
122 logsoftc.sc_state |= LOG_RDWAIT;
123 if (error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
124 "klog", 0)) {
125 splx(s);
126 return (error);
127 }
128 }
129 splx(s);
130 logsoftc.sc_state &= ~LOG_RDWAIT;
131
132 while (uio->uio_resid > 0) {
133 l = mbp->msg_bufx - mbp->msg_bufr;
134 if (l < 0)
135 l = MSG_BSIZE - mbp->msg_bufr;
136 l = MIN(l, uio->uio_resid);
137 if (l == 0)
138 break;
139 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
140 (int)l, uio);
141 if (error)
142 break;
143 mbp->msg_bufr += l;
144 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
145 mbp->msg_bufr = 0;
146 }
147 return (error);
148}
149
150/*ARGSUSED*/
151logselect(dev, rw, p)
152 dev_t dev;
153 int rw;
154 struct proc *p;
155{
156 int s = splhigh();
157
158 switch (rw) {
159
160 case FREAD:
161 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
162 splx(s);
163 return (1);
164 }
d6075007 165 logsoftc.sc_sel = p->p_pid; /* 16 Jun 93 */
15637ed4
RG
166 break;
167 }
168 splx(s);
169 return (0);
170}
171
172logwakeup()
173{
174 struct proc *p;
175
176 if (!log_open)
177 return;
d6075007
NW
178 if (logsoftc.sc_sel) { /* 16 Jun 93 */
179 selwakeup(logsoftc.sc_sel, 0);
180 logsoftc.sc_sel = 0;
15637ed4
RG
181 }
182 if (logsoftc.sc_state & LOG_ASYNC) {
183 if (logsoftc.sc_pgid < 0)
184 gsignal(-logsoftc.sc_pgid, SIGIO);
185 else if (p = pfind(logsoftc.sc_pgid))
186 psignal(p, SIGIO);
187 }
188 if (logsoftc.sc_state & LOG_RDWAIT) {
189 wakeup((caddr_t)msgbufp);
190 logsoftc.sc_state &= ~LOG_RDWAIT;
191 }
192}
193
194/*ARGSUSED*/
195logioctl(dev, com, data, flag)
196 caddr_t data;
197{
198 long l;
199 int s;
200
201 switch (com) {
202
203 /* return number of characters immediately available */
204 case FIONREAD:
205 s = splhigh();
206 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
207 splx(s);
208 if (l < 0)
209 l += MSG_BSIZE;
210 *(off_t *)data = l;
211 break;
212
213 case FIONBIO:
214 break;
215
216 case FIOASYNC:
217 if (*(int *)data)
218 logsoftc.sc_state |= LOG_ASYNC;
219 else
220 logsoftc.sc_state &= ~LOG_ASYNC;
221 break;
222
223 case TIOCSPGRP:
224 logsoftc.sc_pgid = *(int *)data;
225 break;
226
227 case TIOCGPGRP:
228 *(int *)data = logsoftc.sc_pgid;
229 break;
230
231 default:
232 return (-1);
233 }
234 return (0);
235}