Remove the priority part of the priority|flags argument to tsleep(). Only
[dragonfly.git] / sys / kern / subr_log.c
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: src/sys/kern/subr_log.c,v 1.39.2.2 2001/06/02 08:11:25 phk Exp $
35  * $DragonFly: src/sys/kern/subr_log.c,v 1.4 2003/07/19 21:14:38 dillon Exp $
36  */
37
38 /*
39  * Error log buffer for kernel printf's.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/filio.h>
48 #include <sys/ttycom.h>
49 #include <sys/msgbuf.h>
50 #include <sys/signalvar.h>
51 #include <sys/kernel.h>
52 #include <sys/poll.h>
53 #include <sys/filedesc.h>
54 #include <sys/sysctl.h>
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 static  void logtimeout(void *arg);
66
67 #define CDEV_MAJOR 7
68 static struct cdevsw log_cdevsw = {
69         /* open */      logopen,
70         /* close */     logclose,
71         /* read */      logread,
72         /* write */     nowrite,
73         /* ioctl */     logioctl,
74         /* poll */      logpoll,
75         /* mmap */      nommap,
76         /* strategy */  nostrategy,
77         /* name */      "log",
78         /* maj */       CDEV_MAJOR,
79         /* dump */      nodump,
80         /* psize */     nopsize,
81         /* flags */     0,
82         /* bmaj */      -1
83 };
84
85 static struct logsoftc {
86         int     sc_state;               /* see above for possibilities */
87         struct  selinfo sc_selp;        /* process waiting on select call */
88         struct  sigio *sc_sigio;        /* information for async I/O */
89         struct  callout sc_callout;     /* callout to wakeup syslog  */
90 } logsoftc;
91
92 int     log_open;                       /* also used in log() */
93
94 /* Times per second to check for a pending syslog wakeup. */
95 static int      log_wakeups_per_second = 5;
96 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
97     &log_wakeups_per_second, 0, "");
98
99 /*ARGSUSED*/
100 static  int
101 logopen(dev_t dev, int flags, int mode, struct thread *td)
102 {
103         struct proc *p = td->td_proc;
104
105         KKASSERT(p != NULL);
106         if (log_open)
107                 return (EBUSY);
108         log_open = 1;
109         callout_init(&logsoftc.sc_callout);
110         fsetown(p->p_pid, &logsoftc.sc_sigio);  /* signal process only */
111         callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
112             logtimeout, NULL);
113         return (0);
114 }
115
116 /*ARGSUSED*/
117 static  int
118 logclose(dev_t dev, int flag, int mode, struct thread *td)
119 {
120
121         log_open = 0;
122         callout_stop(&logsoftc.sc_callout);
123         logsoftc.sc_state = 0;
124         funsetown(logsoftc.sc_sigio);
125         return (0);
126 }
127
128 /*ARGSUSED*/
129 static  int
130 logread(dev_t dev, struct uio *uio, int flag)
131 {
132         struct msgbuf *mbp = msgbufp;
133         long l;
134         int s;
135         int error = 0;
136
137         s = splhigh();
138         while (mbp->msg_bufr == mbp->msg_bufx) {
139                 if (flag & IO_NDELAY) {
140                         splx(s);
141                         return (EWOULDBLOCK);
142                 }
143                 logsoftc.sc_state |= LOG_RDWAIT;
144                 if ((error = tsleep((caddr_t)mbp, PCATCH, "klog", 0))) {
145                         splx(s);
146                         return (error);
147                 }
148         }
149         splx(s);
150         logsoftc.sc_state &= ~LOG_RDWAIT;
151
152         while (uio->uio_resid > 0) {
153                 l = mbp->msg_bufx - mbp->msg_bufr;
154                 if (l < 0)
155                         l = mbp->msg_size - mbp->msg_bufr;
156                 l = min(l, uio->uio_resid);
157                 if (l == 0)
158                         break;
159                 error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
160                     (int)l, uio);
161                 if (error)
162                         break;
163                 mbp->msg_bufr += l;
164                 if (mbp->msg_bufr >= mbp->msg_size)
165                         mbp->msg_bufr = 0;
166         }
167         return (error);
168 }
169
170 /*ARGSUSED*/
171 static  int
172 logpoll(dev_t dev, int events, struct thread *td)
173 {
174         int s;
175         int revents = 0;
176
177         s = splhigh();
178
179         if (events & (POLLIN | POLLRDNORM)) {
180                 if (msgbufp->msg_bufr != msgbufp->msg_bufx)
181                         revents |= events & (POLLIN | POLLRDNORM);
182                 else
183                         selrecord(td, &logsoftc.sc_selp);
184         }
185         splx(s);
186         return (revents);
187 }
188
189 static void
190 logtimeout(void *arg)
191 {
192
193         if (!log_open)
194                 return;
195         if (msgbuftrigger == 0) {
196                 callout_reset(&logsoftc.sc_callout,
197                     hz / log_wakeups_per_second, logtimeout, NULL);
198                 return;
199         }
200         msgbuftrigger = 0;
201         selwakeup(&logsoftc.sc_selp);
202         if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
203                 pgsigio(logsoftc.sc_sigio, SIGIO, 0);
204         if (logsoftc.sc_state & LOG_RDWAIT) {
205                 wakeup((caddr_t)msgbufp);
206                 logsoftc.sc_state &= ~LOG_RDWAIT;
207         }
208         callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
209             logtimeout, NULL);
210 }
211
212 /*ARGSUSED*/
213 static  int
214 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct thread *td)
215 {
216         long l;
217         int s;
218
219         switch (com) {
220
221         /* return number of characters immediately available */
222         case FIONREAD:
223                 s = splhigh();
224                 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
225                 splx(s);
226                 if (l < 0)
227                         l += msgbufp->msg_size;
228                 *(int *)data = l;
229                 break;
230
231         case FIONBIO:
232                 break;
233
234         case FIOASYNC:
235                 if (*(int *)data)
236                         logsoftc.sc_state |= LOG_ASYNC;
237                 else
238                         logsoftc.sc_state &= ~LOG_ASYNC;
239                 break;
240
241         case FIOSETOWN:
242                 return (fsetown(*(int *)data, &logsoftc.sc_sigio));
243
244         case FIOGETOWN:
245                 *(int *)data = fgetown(logsoftc.sc_sigio);
246                 break;
247
248         /* This is deprecated, FIOSETOWN should be used instead. */
249         case TIOCSPGRP:
250                 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
251
252         /* This is deprecated, FIOGETOWN should be used instead */
253         case TIOCGPGRP:
254                 *(int *)data = -fgetown(logsoftc.sc_sigio);
255                 break;
256
257         default:
258                 return (ENOTTY);
259         }
260         return (0);
261 }
262
263 static void
264 log_drvinit(void *unused)
265 {
266
267         make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
268 }
269
270 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)