xref: /freebsd/sys/kern/subr_log.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 6/10/93
34  * $Id: subr_log.c,v 1.29 1998/05/28 09:30:20 phk Exp $
35  */
36 
37 /*
38  * Error log buffer for kernel printf's.
39  */
40 
41 #include "opt_devfs.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/filio.h>
49 #include <sys/ttycom.h>
50 #include <sys/msgbuf.h>
51 #include <sys/signalvar.h>
52 #include <sys/kernel.h>
53 #include <sys/poll.h>
54 #ifdef DEVFS
55 #include <sys/devfsext.h>
56 #endif /*DEVFS*/
57 
58 #define LOG_RDPRI	(PZERO + 1)
59 
60 #define LOG_ASYNC	0x04
61 #define LOG_RDWAIT	0x08
62 
63 static	d_open_t	logopen;
64 static	d_close_t	logclose;
65 static	d_read_t	logread;
66 static	d_ioctl_t	logioctl;
67 static	d_poll_t	logpoll;
68 
69 #define CDEV_MAJOR 7
70 static struct cdevsw log_cdevsw =
71 	{ logopen,	logclose,	logread,	nowrite,	/*7*/
72 	  logioctl,	nostop,		nullreset,	nodevtotty,/* klog */
73 	  logpoll,	nommap,		NULL,	"log",	NULL,	-1 };
74 
75 static struct logsoftc {
76 	int	sc_state;		/* see above for possibilities */
77 	struct	selinfo sc_selp;	/* process waiting on select call */
78 	int	sc_pgid;		/* process/group for async I/O */
79 } logsoftc;
80 
81 int	log_open;			/* also used in log() */
82 
83 /*ARGSUSED*/
84 static	int
85 logopen(dev, flags, mode, p)
86 	dev_t dev;
87 	int flags, mode;
88 	struct proc *p;
89 {
90 	if (log_open)
91 		return (EBUSY);
92 	log_open = 1;
93 	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
94 	return (0);
95 }
96 
97 /*ARGSUSED*/
98 static	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 static	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 		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
130 		    "klog", 0))) {
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 = mbp->msg_size - mbp->msg_bufr;
142 		l = min(l, uio->uio_resid);
143 		if (l == 0)
144 			break;
145 		error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
146 		    (int)l, uio);
147 		if (error)
148 			break;
149 		mbp->msg_bufr += l;
150 		if (mbp->msg_bufr >= mbp->msg_size)
151 			mbp->msg_bufr = 0;
152 	}
153 	return (error);
154 }
155 
156 /*ARGSUSED*/
157 static	int
158 logpoll(dev, events, p)
159 	dev_t dev;
160 	int events;
161 	struct proc *p;
162 {
163 	int s;
164 	int revents = 0;
165 
166 	s = splhigh();
167 
168 	if (events & (POLLIN | POLLRDNORM))
169 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
170 			revents |= events & (POLLIN | POLLRDNORM);
171 		else
172 			selrecord(p, &logsoftc.sc_selp);
173 
174 	splx(s);
175 	return (revents);
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)))
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 static	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 += msgbufp->msg_size;
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 (ENOTTY);
242 	}
243 	return (0);
244 }
245 
246 static	int	log_devsw_installed;
247 #ifdef DEVFS
248 static	void	*log_devfs_token;
249 #endif
250 
251 static void log_drvinit __P((void *unused));
252 static void
253 log_drvinit(unused)
254 	void *unused;
255 {
256 	dev_t dev;
257 
258 	if( ! log_devsw_installed ) {
259 		dev = makedev(CDEV_MAJOR,0);
260 		cdevsw_add(&dev,&log_cdevsw,NULL);
261 		log_devsw_installed = 1;
262 #ifdef DEVFS
263 		log_devfs_token = devfs_add_devswf(&log_cdevsw, 0, DV_CHR,
264 						   UID_ROOT, GID_WHEEL, 0600,
265 						   "klog");
266 #endif
267     	}
268 }
269 
270 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
271