xref: /openbsd-src/sys/kern/subr_log.c (revision 1e7911a6aea07e315efb5b46ef18a231aac9c7ef)
1 /*	$OpenBSD: subr_log.c,v 1.5 1997/09/18 13:23:26 deraadt Exp $	*/
2 /*	$NetBSD: subr_log.c,v 1.11 1996/03/30 22:24:44 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
37  */
38 
39 /*
40  * Error log buffer for kernel printf's.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/ioctl.h>
48 #include <sys/msgbuf.h>
49 #include <sys/file.h>
50 #include <sys/signalvar.h>
51 #include <sys/syslog.h>
52 #include <sys/conf.h>
53 
54 #define LOG_RDPRI	(PZERO + 1)
55 
56 #define LOG_ASYNC	0x04
57 #define LOG_RDWAIT	0x08
58 
59 struct logsoftc {
60 	int	sc_state;		/* see above for possibilities */
61 	struct	selinfo sc_selp;	/* process waiting on select call */
62 	int	sc_pgid;		/* process/group for async I/O */
63 	uid_t	sc_siguid;		/* uid for process that set sc_pgid */
64 	uid_t	sc_sigeuid;		/* euid for process that set sc_pgid */
65 } logsoftc;
66 
67 int	log_open;			/* also used in log() */
68 
69 /*ARGSUSED*/
70 int
71 logopen(dev, flags, mode, p)
72 	dev_t dev;
73 	int flags, mode;
74 	struct proc *p;
75 {
76 	register struct msgbuf *mbp = msgbufp;
77 
78 	if (log_open)
79 		return (EBUSY);
80 	log_open = 1;
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 logselect(dev, rw, p)
160 	dev_t dev;
161 	int rw;
162 	struct proc *p;
163 {
164 	int s = splhigh();
165 
166 	switch (rw) {
167 
168 	case FREAD:
169 		if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
170 			splx(s);
171 			return (1);
172 		}
173 		selrecord(p, &logsoftc.sc_selp);
174 		break;
175 	}
176 	splx(s);
177 	return (0);
178 }
179 
180 void
181 logwakeup()
182 {
183 	if (!log_open)
184 		return;
185 	selwakeup(&logsoftc.sc_selp);
186 	if (logsoftc.sc_state & LOG_ASYNC)
187 		csignal(logsoftc.sc_pgid, SIGIO,
188 		    logsoftc.sc_siguid, logsoftc.sc_sigeuid);
189 	if (logsoftc.sc_state & LOG_RDWAIT) {
190 		wakeup((caddr_t)msgbufp);
191 		logsoftc.sc_state &= ~LOG_RDWAIT;
192 	}
193 }
194 
195 /*ARGSUSED*/
196 int
197 logioctl(dev, com, data, flag, p)
198 	dev_t dev;
199 	u_long com;
200 	caddr_t data;
201 	int flag;
202 	struct proc *p;
203 {
204 	long l;
205 	int s;
206 
207 	switch (com) {
208 
209 	/* return number of characters immediately available */
210 	case FIONREAD:
211 		s = splhigh();
212 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
213 		splx(s);
214 		if (l < 0)
215 			l += MSG_BSIZE;
216 		*(int *)data = l;
217 		break;
218 
219 	case FIONBIO:
220 		break;
221 
222 	case FIOASYNC:
223 		if (*(int *)data)
224 			logsoftc.sc_state |= LOG_ASYNC;
225 		else
226 			logsoftc.sc_state &= ~LOG_ASYNC;
227 		break;
228 
229 	case TIOCSPGRP:
230 		logsoftc.sc_pgid = *(int *)data;
231 		logsoftc.sc_siguid = p->p_cred->p_ruid;
232 		logsoftc.sc_sigeuid = p->p_ucred->cr_uid;
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