xref: /freebsd/sys/kern/subr_log.c (revision 98d938220c45ad49ebbe6ed957fefd993d63bf2a)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
3498d93822SBruce Evans  * $Id: subr_log.c,v 1.12 1995/11/29 14:40:35 julian Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes /*
38df8bae1dSRodney W. Grimes  * Error log buffer for kernel printf's.
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
4398d93822SBruce Evans #include <sys/conf.h>
44df8bae1dSRodney W. Grimes #include <sys/proc.h>
45df8bae1dSRodney W. Grimes #include <sys/vnode.h>
46df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
47df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
48df8bae1dSRodney W. Grimes #include <sys/file.h>
49797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes 
5153ac6efbSJulian Elischer #ifdef JREMOD
5253ac6efbSJulian Elischer #include <sys/conf.h>
5353ac6efbSJulian Elischer #include <sys/kernel.h>
5453ac6efbSJulian Elischer #ifdef DEVFS
5553ac6efbSJulian Elischer #include <sys/devfsext.h>
5653ac6efbSJulian Elischer #endif /*DEVFS*/
5753ac6efbSJulian Elischer #define CDEV_MAJOR 7
5853ac6efbSJulian Elischer #endif /*JREMOD*/
5953ac6efbSJulian Elischer 
60df8bae1dSRodney W. Grimes #define LOG_RDPRI	(PZERO + 1)
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #define LOG_ASYNC	0x04
63df8bae1dSRodney W. Grimes #define LOG_RDWAIT	0x08
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes struct logsoftc {
66df8bae1dSRodney W. Grimes 	int	sc_state;		/* see above for possibilities */
67df8bae1dSRodney W. Grimes 	struct	selinfo sc_selp;	/* process waiting on select call */
68df8bae1dSRodney W. Grimes 	int	sc_pgid;		/* process/group for async I/O */
69df8bae1dSRodney W. Grimes } logsoftc;
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes int	log_open;			/* also used in log() */
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes /*ARGSUSED*/
7426f9a767SRodney W. Grimes int
75df8bae1dSRodney W. Grimes logopen(dev, flags, mode, p)
76df8bae1dSRodney W. Grimes 	dev_t dev;
77df8bae1dSRodney W. Grimes 	int flags, mode;
78df8bae1dSRodney W. Grimes 	struct proc *p;
79df8bae1dSRodney W. Grimes {
80df8bae1dSRodney W. Grimes 	if (log_open)
81df8bae1dSRodney W. Grimes 		return (EBUSY);
82df8bae1dSRodney W. Grimes 	log_open = 1;
83df8bae1dSRodney W. Grimes 	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
84df8bae1dSRodney W. Grimes 	return (0);
85df8bae1dSRodney W. Grimes }
86df8bae1dSRodney W. Grimes 
87df8bae1dSRodney W. Grimes /*ARGSUSED*/
8826f9a767SRodney W. Grimes int
89df8bae1dSRodney W. Grimes logclose(dev, flag, mode, p)
90df8bae1dSRodney W. Grimes 	dev_t dev;
91df8bae1dSRodney W. Grimes 	int flag, mode;
92df8bae1dSRodney W. Grimes 	struct proc *p;
93df8bae1dSRodney W. Grimes {
94df8bae1dSRodney W. Grimes 
95df8bae1dSRodney W. Grimes 	log_open = 0;
96df8bae1dSRodney W. Grimes 	logsoftc.sc_state = 0;
97df8bae1dSRodney W. Grimes 	return (0);
98df8bae1dSRodney W. Grimes }
99df8bae1dSRodney W. Grimes 
100df8bae1dSRodney W. Grimes /*ARGSUSED*/
10126f9a767SRodney W. Grimes int
102df8bae1dSRodney W. Grimes logread(dev, uio, flag)
103df8bae1dSRodney W. Grimes 	dev_t dev;
104df8bae1dSRodney W. Grimes 	struct uio *uio;
105df8bae1dSRodney W. Grimes 	int flag;
106df8bae1dSRodney W. Grimes {
107df8bae1dSRodney W. Grimes 	register struct msgbuf *mbp = msgbufp;
108df8bae1dSRodney W. Grimes 	register long l;
109df8bae1dSRodney W. Grimes 	register int s;
110df8bae1dSRodney W. Grimes 	int error = 0;
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes 	s = splhigh();
113df8bae1dSRodney W. Grimes 	while (mbp->msg_bufr == mbp->msg_bufx) {
114df8bae1dSRodney W. Grimes 		if (flag & IO_NDELAY) {
115df8bae1dSRodney W. Grimes 			splx(s);
116df8bae1dSRodney W. Grimes 			return (EWOULDBLOCK);
117df8bae1dSRodney W. Grimes 		}
118df8bae1dSRodney W. Grimes 		logsoftc.sc_state |= LOG_RDWAIT;
119bb56ec4aSPoul-Henning Kamp 		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
120bb56ec4aSPoul-Henning Kamp 		    "klog", 0))) {
121df8bae1dSRodney W. Grimes 			splx(s);
122df8bae1dSRodney W. Grimes 			return (error);
123df8bae1dSRodney W. Grimes 		}
124df8bae1dSRodney W. Grimes 	}
125df8bae1dSRodney W. Grimes 	splx(s);
126df8bae1dSRodney W. Grimes 	logsoftc.sc_state &= ~LOG_RDWAIT;
127df8bae1dSRodney W. Grimes 
128df8bae1dSRodney W. Grimes 	while (uio->uio_resid > 0) {
129df8bae1dSRodney W. Grimes 		l = mbp->msg_bufx - mbp->msg_bufr;
130df8bae1dSRodney W. Grimes 		if (l < 0)
131df8bae1dSRodney W. Grimes 			l = MSG_BSIZE - mbp->msg_bufr;
132df8bae1dSRodney W. Grimes 		l = min(l, uio->uio_resid);
133df8bae1dSRodney W. Grimes 		if (l == 0)
134df8bae1dSRodney W. Grimes 			break;
135df8bae1dSRodney W. Grimes 		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
136df8bae1dSRodney W. Grimes 			(int)l, uio);
137df8bae1dSRodney W. Grimes 		if (error)
138df8bae1dSRodney W. Grimes 			break;
139df8bae1dSRodney W. Grimes 		mbp->msg_bufr += l;
1406c8897cfSDavid Greenman 		if (mbp->msg_bufr >= MSG_BSIZE)
141df8bae1dSRodney W. Grimes 			mbp->msg_bufr = 0;
142df8bae1dSRodney W. Grimes 	}
143df8bae1dSRodney W. Grimes 	return (error);
144df8bae1dSRodney W. Grimes }
145df8bae1dSRodney W. Grimes 
146df8bae1dSRodney W. Grimes /*ARGSUSED*/
14726f9a767SRodney W. Grimes int
148df8bae1dSRodney W. Grimes logselect(dev, rw, p)
149df8bae1dSRodney W. Grimes 	dev_t dev;
150df8bae1dSRodney W. Grimes 	int rw;
151df8bae1dSRodney W. Grimes 	struct proc *p;
152df8bae1dSRodney W. Grimes {
153df8bae1dSRodney W. Grimes 	int s = splhigh();
154df8bae1dSRodney W. Grimes 
155df8bae1dSRodney W. Grimes 	switch (rw) {
156df8bae1dSRodney W. Grimes 
157df8bae1dSRodney W. Grimes 	case FREAD:
158df8bae1dSRodney W. Grimes 		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
159df8bae1dSRodney W. Grimes 			splx(s);
160df8bae1dSRodney W. Grimes 			return (1);
161df8bae1dSRodney W. Grimes 		}
162df8bae1dSRodney W. Grimes 		selrecord(p, &logsoftc.sc_selp);
163df8bae1dSRodney W. Grimes 		break;
164df8bae1dSRodney W. Grimes 	}
165df8bae1dSRodney W. Grimes 	splx(s);
166df8bae1dSRodney W. Grimes 	return (0);
167df8bae1dSRodney W. Grimes }
168df8bae1dSRodney W. Grimes 
16926f9a767SRodney W. Grimes void
170df8bae1dSRodney W. Grimes logwakeup()
171df8bae1dSRodney W. Grimes {
172df8bae1dSRodney W. Grimes 	struct proc *p;
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	if (!log_open)
175df8bae1dSRodney W. Grimes 		return;
176df8bae1dSRodney W. Grimes 	selwakeup(&logsoftc.sc_selp);
177df8bae1dSRodney W. Grimes 	if (logsoftc.sc_state & LOG_ASYNC) {
178df8bae1dSRodney W. Grimes 		if (logsoftc.sc_pgid < 0)
179df8bae1dSRodney W. Grimes 			gsignal(-logsoftc.sc_pgid, SIGIO);
180bb56ec4aSPoul-Henning Kamp 		else if ((p = pfind(logsoftc.sc_pgid)))
181df8bae1dSRodney W. Grimes 			psignal(p, SIGIO);
182df8bae1dSRodney W. Grimes 	}
183df8bae1dSRodney W. Grimes 	if (logsoftc.sc_state & LOG_RDWAIT) {
184df8bae1dSRodney W. Grimes 		wakeup((caddr_t)msgbufp);
185df8bae1dSRodney W. Grimes 		logsoftc.sc_state &= ~LOG_RDWAIT;
186df8bae1dSRodney W. Grimes 	}
187df8bae1dSRodney W. Grimes }
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes /*ARGSUSED*/
19026f9a767SRodney W. Grimes int
191df8bae1dSRodney W. Grimes logioctl(dev, com, data, flag, p)
192df8bae1dSRodney W. Grimes 	dev_t dev;
193df8bae1dSRodney W. Grimes 	int com;
194df8bae1dSRodney W. Grimes 	caddr_t data;
195df8bae1dSRodney W. Grimes 	int flag;
196df8bae1dSRodney W. Grimes 	struct proc *p;
197df8bae1dSRodney W. Grimes {
198df8bae1dSRodney W. Grimes 	long l;
199df8bae1dSRodney W. Grimes 	int s;
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes 	switch (com) {
202df8bae1dSRodney W. Grimes 
203df8bae1dSRodney W. Grimes 	/* return number of characters immediately available */
204df8bae1dSRodney W. Grimes 	case FIONREAD:
205df8bae1dSRodney W. Grimes 		s = splhigh();
206df8bae1dSRodney W. Grimes 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
207df8bae1dSRodney W. Grimes 		splx(s);
208df8bae1dSRodney W. Grimes 		if (l < 0)
209df8bae1dSRodney W. Grimes 			l += MSG_BSIZE;
210df8bae1dSRodney W. Grimes 		*(int *)data = l;
211df8bae1dSRodney W. Grimes 		break;
212df8bae1dSRodney W. Grimes 
213df8bae1dSRodney W. Grimes 	case FIONBIO:
214df8bae1dSRodney W. Grimes 		break;
215df8bae1dSRodney W. Grimes 
216df8bae1dSRodney W. Grimes 	case FIOASYNC:
217df8bae1dSRodney W. Grimes 		if (*(int *)data)
218df8bae1dSRodney W. Grimes 			logsoftc.sc_state |= LOG_ASYNC;
219df8bae1dSRodney W. Grimes 		else
220df8bae1dSRodney W. Grimes 			logsoftc.sc_state &= ~LOG_ASYNC;
221df8bae1dSRodney W. Grimes 		break;
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes 	case TIOCSPGRP:
224df8bae1dSRodney W. Grimes 		logsoftc.sc_pgid = *(int *)data;
225df8bae1dSRodney W. Grimes 		break;
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes 	case TIOCGPGRP:
228df8bae1dSRodney W. Grimes 		*(int *)data = logsoftc.sc_pgid;
229df8bae1dSRodney W. Grimes 		break;
230df8bae1dSRodney W. Grimes 
231df8bae1dSRodney W. Grimes 	default:
232bcac5617SJordan K. Hubbard 		return (ENOTTY);
233df8bae1dSRodney W. Grimes 	}
234df8bae1dSRodney W. Grimes 	return (0);
235df8bae1dSRodney W. Grimes }
23653ac6efbSJulian Elischer 
23753ac6efbSJulian Elischer #ifdef JREMOD
23853ac6efbSJulian Elischer struct cdevsw log_cdevsw =
23953ac6efbSJulian Elischer 	{ logopen,	logclose,	logread,	nowrite,	/*7*/
24053ac6efbSJulian Elischer 	  logioctl,	nostop,		nullreset,	nodevtotty,/* klog */
24153ac6efbSJulian Elischer 	  logselect,	nommap,		NULL };
24253ac6efbSJulian Elischer 
24353ac6efbSJulian Elischer static log_devsw_installed = 0;
24453ac6efbSJulian Elischer 
24553ac6efbSJulian Elischer static void 	log_drvinit(void *unused)
24653ac6efbSJulian Elischer {
24753ac6efbSJulian Elischer 	dev_t dev;
24853ac6efbSJulian Elischer 
24953ac6efbSJulian Elischer 	if( ! log_devsw_installed ) {
25053ac6efbSJulian Elischer 		dev = makedev(CDEV_MAJOR,0);
25153ac6efbSJulian Elischer 		cdevsw_add(&dev,&log_cdevsw,NULL);
25253ac6efbSJulian Elischer 		log_devsw_installed = 1;
25353ac6efbSJulian Elischer #ifdef DEVFS
25453ac6efbSJulian Elischer 		{
25553ac6efbSJulian Elischer 			int x;
25653ac6efbSJulian Elischer /* default for a simple device with no probe routine (usually delete this) */
25753ac6efbSJulian Elischer 			x=devfs_add_devsw(
25853ac6efbSJulian Elischer /*	path	name	devsw		minor	type   uid gid perm*/
25953ac6efbSJulian Elischer 	"/",	"log",	major(dev),	0,	DV_CHR,	0,  0, 0600);
26053ac6efbSJulian Elischer 		}
26153ac6efbSJulian Elischer #endif
26253ac6efbSJulian Elischer     	}
2637198bf47SJulian Elischer }
26453ac6efbSJulian Elischer 
26553ac6efbSJulian Elischer SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
26653ac6efbSJulian Elischer 
26753ac6efbSJulian Elischer #endif /* JREMOD */
26853ac6efbSJulian Elischer 
269