xref: /freebsd/sys/kern/subr_log.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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.23 1997/09/14 02:46:44 peter Exp $
35  */
36 
37 /*
38  * Error log buffer for kernel printf's.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/filio.h>
47 #include <sys/ttycom.h>
48 #include <sys/msgbuf.h>
49 #include <sys/fcntl.h>
50 #include <sys/signalvar.h>
51 #include <sys/kernel.h>
52 #include <sys/poll.h>
53 #ifdef DEVFS
54 #include <sys/devfsext.h>
55 #endif /*DEVFS*/
56 
57 #define LOG_RDPRI	(PZERO + 1)
58 
59 #define LOG_ASYNC	0x04
60 #define LOG_RDWAIT	0x08
61 
62 static	d_open_t	logopen;
63 static	d_close_t	logclose;
64 static	d_read_t	logread;
65 static	d_ioctl_t	logioctl;
66 static	d_poll_t	logpoll;
67 
68 #define CDEV_MAJOR 7
69 static struct cdevsw log_cdevsw =
70 	{ logopen,	logclose,	logread,	nowrite,	/*7*/
71 	  logioctl,	nostop,		nullreset,	nodevtotty,/* klog */
72 	  logpoll,	nommap,		NULL,	"log",	NULL,	-1 };
73 
74 static struct logsoftc {
75 	int	sc_state;		/* see above for possibilities */
76 	struct	selinfo sc_selp;	/* process waiting on select call */
77 	int	sc_pgid;		/* process/group for async I/O */
78 } logsoftc;
79 
80 int	log_open;			/* also used in log() */
81 
82 /*ARGSUSED*/
83 static	int
84 logopen(dev, flags, mode, p)
85 	dev_t dev;
86 	int flags, mode;
87 	struct proc *p;
88 {
89 	if (log_open)
90 		return (EBUSY);
91 	log_open = 1;
92 	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
93 	return (0);
94 }
95 
96 /*ARGSUSED*/
97 static	int
98 logclose(dev, flag, mode, p)
99 	dev_t dev;
100 	int flag, mode;
101 	struct proc *p;
102 {
103 
104 	log_open = 0;
105 	logsoftc.sc_state = 0;
106 	return (0);
107 }
108 
109 /*ARGSUSED*/
110 static	int
111 logread(dev, uio, flag)
112 	dev_t dev;
113 	struct uio *uio;
114 	int flag;
115 {
116 	register struct msgbuf *mbp = msgbufp;
117 	register long l;
118 	register int s;
119 	int error = 0;
120 
121 	s = splhigh();
122 	while (mbp->msg_bufr == mbp->msg_bufx) {
123 		if (flag & IO_NDELAY) {
124 			splx(s);
125 			return (EWOULDBLOCK);
126 		}
127 		logsoftc.sc_state |= LOG_RDWAIT;
128 		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
129 		    "klog", 0))) {
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 >= MSG_BSIZE)
150 			mbp->msg_bufr = 0;
151 	}
152 	return (error);
153 }
154 
155 /*ARGSUSED*/
156 static	int
157 logpoll(dev, events, p)
158 	dev_t dev;
159 	int events;
160 	struct proc *p;
161 {
162 	int s;
163 	int revents = 0;
164 
165 	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)))
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 static	int
199 logioctl(dev, com, data, flag, p)
200 	dev_t dev;
201 	int 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 (ENOTTY);
241 	}
242 	return (0);
243 }
244 
245 static log_devsw_installed = 0;
246 #ifdef DEVFS
247 static	void	*log_devfs_token;
248 #endif
249 
250 static void log_drvinit __P((void *unused));
251 static void
252 log_drvinit(unused)
253 	void *unused;
254 {
255 	dev_t dev;
256 
257 	if( ! log_devsw_installed ) {
258 		dev = makedev(CDEV_MAJOR,0);
259 		cdevsw_add(&dev,&log_cdevsw,NULL);
260 		log_devsw_installed = 1;
261 #ifdef DEVFS
262 		log_devfs_token = devfs_add_devswf(&log_cdevsw, 0, DV_CHR,
263 						   UID_ROOT, GID_WHEEL, 0600,
264 						   "klog");
265 #endif
266     	}
267 }
268 
269 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
270 
271 
272