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