xref: /openbsd-src/sys/kern/subr_log.c (revision df930be708d50e9715f173caa26ffe1b7599b157)
1 /*	$NetBSD: subr_log.c,v 1.8 1994/10/30 21:47:47 cgd 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 
50 #define LOG_RDPRI	(PZERO + 1)
51 
52 #define LOG_ASYNC	0x04
53 #define LOG_RDWAIT	0x08
54 
55 struct 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 
61 int	log_open;			/* also used in log() */
62 
63 /*ARGSUSED*/
64 int
65 logopen(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 	/*
77 	 * Potential race here with putchar() but since putchar should be
78 	 * called by autoconf, msg_magic should be initialized by the time
79 	 * we get here.
80 	 */
81 	if (mbp->msg_magic != MSG_MAGIC) {
82 		register int i;
83 
84 		mbp->msg_magic = MSG_MAGIC;
85 		mbp->msg_bufx = mbp->msg_bufr = 0;
86 		for (i=0; i < MSG_BSIZE; i++)
87 			mbp->msg_bufc[i] = 0;
88 	}
89 	return (0);
90 }
91 
92 /*ARGSUSED*/
93 int
94 logclose(dev, flag, mode, p)
95 	dev_t dev;
96 	int flag;
97 {
98 
99 	log_open = 0;
100 	logsoftc.sc_state = 0;
101 	return (0);
102 }
103 
104 /*ARGSUSED*/
105 int
106 logread(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*/
151 int
152 logselect(dev, rw, p)
153 	dev_t dev;
154 	int rw;
155 	struct proc *p;
156 {
157 	int s = splhigh();
158 
159 	switch (rw) {
160 
161 	case FREAD:
162 		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
163 			splx(s);
164 			return (1);
165 		}
166 		selrecord(p, &logsoftc.sc_selp);
167 		break;
168 	}
169 	splx(s);
170 	return (0);
171 }
172 
173 void
174 logwakeup()
175 {
176 	struct proc *p;
177 
178 	if (!log_open)
179 		return;
180 	selwakeup(&logsoftc.sc_selp);
181 	if (logsoftc.sc_state & LOG_ASYNC) {
182 		if (logsoftc.sc_pgid < 0)
183 			gsignal(-logsoftc.sc_pgid, SIGIO);
184 		else if (p = pfind(logsoftc.sc_pgid))
185 			psignal(p, SIGIO);
186 	}
187 	if (logsoftc.sc_state & LOG_RDWAIT) {
188 		wakeup((caddr_t)msgbufp);
189 		logsoftc.sc_state &= ~LOG_RDWAIT;
190 	}
191 }
192 
193 /*ARGSUSED*/
194 int
195 logioctl(dev, com, data, flag)
196 	dev_t dev;
197 	u_long com;
198 	caddr_t data;
199 	int flag;
200 {
201 	long l;
202 	int s;
203 
204 	switch (com) {
205 
206 	/* return number of characters immediately available */
207 	case FIONREAD:
208 		s = splhigh();
209 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
210 		splx(s);
211 		if (l < 0)
212 			l += MSG_BSIZE;
213 		*(int *)data = l;
214 		break;
215 
216 	case FIONBIO:
217 		break;
218 
219 	case FIOASYNC:
220 		if (*(int *)data)
221 			logsoftc.sc_state |= LOG_ASYNC;
222 		else
223 			logsoftc.sc_state &= ~LOG_ASYNC;
224 		break;
225 
226 	case TIOCSPGRP:
227 		logsoftc.sc_pgid = *(int *)data;
228 		break;
229 
230 	case TIOCGPGRP:
231 		*(int *)data = logsoftc.sc_pgid;
232 		break;
233 
234 	default:
235 		return (-1);
236 	}
237 	return (0);
238 }
239