xref: /freebsd/sys/kern/subr_log.c (revision cb166ce422ac2bc81f42c2a2e2cd68625c11478d)
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  * $FreeBSD$
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/signalvar.h>
50 #include <sys/kernel.h>
51 #include <sys/poll.h>
52 #include <sys/filedesc.h>
53 
54 #define LOG_RDPRI	(PZERO + 1)
55 
56 #define LOG_ASYNC	0x04
57 #define LOG_RDWAIT	0x08
58 
59 static	d_open_t	logopen;
60 static	d_close_t	logclose;
61 static	d_read_t	logread;
62 static	d_ioctl_t	logioctl;
63 static	d_poll_t	logpoll;
64 
65 #define CDEV_MAJOR 7
66 static struct cdevsw log_cdevsw = {
67 	/* open */	logopen,
68 	/* close */	logclose,
69 	/* read */	logread,
70 	/* write */	nowrite,
71 	/* ioctl */	logioctl,
72 	/* poll */	logpoll,
73 	/* mmap */	nommap,
74 	/* strategy */	nostrategy,
75 	/* name */	"log",
76 	/* maj */	CDEV_MAJOR,
77 	/* dump */	nodump,
78 	/* psize */	nopsize,
79 	/* flags */	0,
80 	/* bmaj */	-1
81 };
82 
83 static struct logsoftc {
84 	int	sc_state;		/* see above for possibilities */
85 	struct	selinfo sc_selp;	/* process waiting on select call */
86 	struct  sigio *sc_sigio;	/* information for async I/O */
87 } logsoftc;
88 
89 int	log_open;			/* also used in log() */
90 
91 /*ARGSUSED*/
92 static	int
93 logopen(dev, flags, mode, p)
94 	dev_t dev;
95 	int flags, mode;
96 	struct proc *p;
97 {
98 	if (log_open)
99 		return (EBUSY);
100 	log_open = 1;
101 	fsetown(p->p_pid, &logsoftc.sc_sigio);	/* signal process only */
102 	return (0);
103 }
104 
105 /*ARGSUSED*/
106 static	int
107 logclose(dev, flag, mode, p)
108 	dev_t dev;
109 	int flag, mode;
110 	struct proc *p;
111 {
112 
113 	log_open = 0;
114 	logsoftc.sc_state = 0;
115 	funsetown(logsoftc.sc_sigio);
116 	return (0);
117 }
118 
119 /*ARGSUSED*/
120 static	int
121 logread(dev, uio, flag)
122 	dev_t dev;
123 	struct uio *uio;
124 	int flag;
125 {
126 	register struct msgbuf *mbp = msgbufp;
127 	register long l;
128 	register int s;
129 	int error = 0;
130 
131 	s = splhigh();
132 	while (mbp->msg_bufr == mbp->msg_bufx) {
133 		if (flag & IO_NDELAY) {
134 			splx(s);
135 			return (EWOULDBLOCK);
136 		}
137 		logsoftc.sc_state |= LOG_RDWAIT;
138 		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
139 		    "klog", 0))) {
140 			splx(s);
141 			return (error);
142 		}
143 	}
144 	splx(s);
145 	logsoftc.sc_state &= ~LOG_RDWAIT;
146 
147 	while (uio->uio_resid > 0) {
148 		l = mbp->msg_bufx - mbp->msg_bufr;
149 		if (l < 0)
150 			l = mbp->msg_size - mbp->msg_bufr;
151 		l = min(l, uio->uio_resid);
152 		if (l == 0)
153 			break;
154 		error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
155 		    (int)l, uio);
156 		if (error)
157 			break;
158 		mbp->msg_bufr += l;
159 		if (mbp->msg_bufr >= mbp->msg_size)
160 			mbp->msg_bufr = 0;
161 	}
162 	return (error);
163 }
164 
165 /*ARGSUSED*/
166 static	int
167 logpoll(dev, events, p)
168 	dev_t dev;
169 	int events;
170 	struct proc *p;
171 {
172 	int s;
173 	int revents = 0;
174 
175 	s = splhigh();
176 
177 	if (events & (POLLIN | POLLRDNORM)) {
178 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
179 			revents |= events & (POLLIN | POLLRDNORM);
180 		else
181 			selrecord(p, &logsoftc.sc_selp);
182 	}
183 	splx(s);
184 	return (revents);
185 }
186 
187 void
188 logwakeup()
189 {
190 	if (!log_open)
191 		return;
192 	selwakeup(&logsoftc.sc_selp);
193 	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
194 		pgsigio(logsoftc.sc_sigio, SIGIO, 0);
195 	if (logsoftc.sc_state & LOG_RDWAIT) {
196 		wakeup((caddr_t)msgbufp);
197 		logsoftc.sc_state &= ~LOG_RDWAIT;
198 	}
199 }
200 
201 /*ARGSUSED*/
202 static	int
203 logioctl(dev, com, data, flag, p)
204 	dev_t dev;
205 	u_long com;
206 	caddr_t data;
207 	int flag;
208 	struct proc *p;
209 {
210 	long l;
211 	int s;
212 
213 	switch (com) {
214 
215 	/* return number of characters immediately available */
216 	case FIONREAD:
217 		s = splhigh();
218 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
219 		splx(s);
220 		if (l < 0)
221 			l += msgbufp->msg_size;
222 		*(int *)data = l;
223 		break;
224 
225 	case FIONBIO:
226 		break;
227 
228 	case FIOASYNC:
229 		if (*(int *)data)
230 			logsoftc.sc_state |= LOG_ASYNC;
231 		else
232 			logsoftc.sc_state &= ~LOG_ASYNC;
233 		break;
234 
235 	case FIOSETOWN:
236 		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
237 
238 	case FIOGETOWN:
239 		*(int *)data = fgetown(logsoftc.sc_sigio);
240 		break;
241 
242 	/* This is deprecated, FIOSETOWN should be used instead. */
243 	case TIOCSPGRP:
244 		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
245 
246 	/* This is deprecated, FIOGETOWN should be used instead */
247 	case TIOCGPGRP:
248 		*(int *)data = -fgetown(logsoftc.sc_sigio);
249 		break;
250 
251 	default:
252 		return (ENOTTY);
253 	}
254 	return (0);
255 }
256 
257 
258 static void log_drvinit __P((void *unused));
259 
260 static void
261 log_drvinit(unused)
262 	void *unused;
263 {
264 	make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
265 }
266 
267 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
268