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.10 2006/07/28 02:17:40 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/device.h> 46 #include <sys/proc.h> 47 #include <sys/vnode.h> 48 #include <sys/filio.h> 49 #include <sys/ttycom.h> 50 #include <sys/msgbuf.h> 51 #include <sys/signalvar.h> 52 #include <sys/kernel.h> 53 #include <sys/poll.h> 54 #include <sys/event.h> 55 #include <sys/filedesc.h> 56 #include <sys/sysctl.h> 57 #include <sys/thread2.h> 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 static d_kqfilter_t logkqfilter; 68 69 static void logtimeout(void *arg); 70 static void logfiltdetach(struct knote *kn); 71 static int logfiltread(struct knote *kn, long hint); 72 73 #define CDEV_MAJOR 7 74 static struct dev_ops log_ops = { 75 { "log", CDEV_MAJOR, D_KQFILTER }, 76 .d_open = logopen, 77 .d_close = logclose, 78 .d_read = logread, 79 .d_ioctl = logioctl, 80 .d_poll = logpoll, 81 .d_kqfilter = logkqfilter 82 }; 83 84 static struct logsoftc { 85 int sc_state; /* see above for possibilities */ 86 struct selinfo sc_selp; /* process waiting on select call */ 87 struct sigio *sc_sigio; /* information for async I/O */ 88 struct callout sc_callout; /* callout to wakeup syslog */ 89 } logsoftc; 90 91 int log_open; /* also used in log() */ 92 93 /* Times per second to check for a pending syslog wakeup. */ 94 static int log_wakeups_per_second = 5; 95 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 96 &log_wakeups_per_second, 0, ""); 97 98 /*ARGSUSED*/ 99 static int 100 logopen(struct dev_open_args *ap) 101 { 102 struct proc *p = curproc; 103 104 KKASSERT(p != NULL); 105 if (log_open) 106 return (EBUSY); 107 log_open = 1; 108 callout_init(&logsoftc.sc_callout); 109 fsetown(p->p_pid, &logsoftc.sc_sigio); /* signal process only */ 110 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 111 logtimeout, NULL); 112 return (0); 113 } 114 115 /*ARGSUSED*/ 116 static int 117 logclose(struct dev_close_args *ap) 118 { 119 log_open = 0; 120 callout_stop(&logsoftc.sc_callout); 121 logsoftc.sc_state = 0; 122 funsetown(logsoftc.sc_sigio); 123 return (0); 124 } 125 126 /*ARGSUSED*/ 127 static int 128 logread(struct dev_read_args *ap) 129 { 130 struct uio *uio = ap->a_uio; 131 struct msgbuf *mbp = msgbufp; 132 long l; 133 int error = 0; 134 135 crit_enter(); 136 while (mbp->msg_bufr == mbp->msg_bufx) { 137 if (ap->a_ioflag & IO_NDELAY) { 138 crit_exit(); 139 return (EWOULDBLOCK); 140 } 141 logsoftc.sc_state |= LOG_RDWAIT; 142 if ((error = tsleep((caddr_t)mbp, PCATCH, "klog", 0))) { 143 crit_exit(); 144 return (error); 145 } 146 } 147 crit_exit(); 148 logsoftc.sc_state &= ~LOG_RDWAIT; 149 150 while (uio->uio_resid > 0) { 151 l = (long)mbp->msg_bufx - (long)mbp->msg_bufr; 152 if (l < 0) 153 l = mbp->msg_size - mbp->msg_bufr; 154 l = (long)szmin(l, uio->uio_resid); 155 if (l == 0) 156 break; 157 error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr, 158 (size_t)l, uio); 159 if (error) 160 break; 161 mbp->msg_bufr += l; 162 if (mbp->msg_bufr >= mbp->msg_size) 163 mbp->msg_bufr = 0; 164 } 165 return (error); 166 } 167 168 /*ARGSUSED*/ 169 static int 170 logpoll(struct dev_poll_args *ap) 171 { 172 int revents = 0; 173 174 crit_enter(); 175 if (ap->a_events & (POLLIN | POLLRDNORM)) { 176 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 177 revents |= ap->a_events & (POLLIN | POLLRDNORM); 178 else 179 selrecord(curthread, &logsoftc.sc_selp); 180 } 181 crit_exit(); 182 ap->a_events = revents; 183 return (0); 184 } 185 186 static struct filterops logread_filtops = 187 { 1, NULL, logfiltdetach, logfiltread }; 188 189 static int 190 logkqfilter(struct dev_kqfilter_args *ap) 191 { 192 struct knote *kn = ap->a_kn; 193 struct klist *klist = &logsoftc.sc_selp.si_note; 194 195 ap->a_result = 0; 196 switch (kn->kn_filter) { 197 case EVFILT_READ: 198 kn->kn_fop = &logread_filtops; 199 break; 200 default: 201 ap->a_result = 1; 202 return (0); 203 } 204 205 crit_enter(); 206 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 207 crit_exit(); 208 209 return (0); 210 } 211 212 static void 213 logfiltdetach(struct knote *kn) 214 { 215 struct klist *klist = &logsoftc.sc_selp.si_note; 216 217 crit_enter(); 218 SLIST_REMOVE(klist, kn, knote, kn_selnext); 219 crit_exit(); 220 } 221 222 static int 223 logfiltread(struct knote *kn, long hint) 224 { 225 int ret = 0; 226 227 crit_enter(); 228 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 229 ret = 1; 230 crit_exit(); 231 232 return (ret); 233 } 234 235 static void 236 logtimeout(void *arg) 237 { 238 239 if (!log_open) 240 return; 241 if (msgbuftrigger == 0) { 242 callout_reset(&logsoftc.sc_callout, 243 hz / log_wakeups_per_second, logtimeout, NULL); 244 return; 245 } 246 msgbuftrigger = 0; 247 selwakeup(&logsoftc.sc_selp); 248 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 249 pgsigio(logsoftc.sc_sigio, SIGIO, 0); 250 if (logsoftc.sc_state & LOG_RDWAIT) { 251 wakeup((caddr_t)msgbufp); 252 logsoftc.sc_state &= ~LOG_RDWAIT; 253 } 254 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 255 logtimeout, NULL); 256 } 257 258 /*ARGSUSED*/ 259 static int 260 logioctl(struct dev_ioctl_args *ap) 261 { 262 long l; 263 264 switch (ap->a_cmd) { 265 case FIONREAD: 266 /* return number of characters immediately available */ 267 crit_enter(); 268 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 269 crit_exit(); 270 if (l < 0) 271 l += msgbufp->msg_size; 272 *(int *)ap->a_data = l; 273 break; 274 275 case FIOASYNC: 276 if (*(int *)ap->a_data) 277 logsoftc.sc_state |= LOG_ASYNC; 278 else 279 logsoftc.sc_state &= ~LOG_ASYNC; 280 break; 281 282 case FIOSETOWN: 283 return (fsetown(*(int *)ap->a_data, &logsoftc.sc_sigio)); 284 285 case FIOGETOWN: 286 *(int *)ap->a_data = fgetown(logsoftc.sc_sigio); 287 break; 288 289 /* This is deprecated, FIOSETOWN should be used instead. */ 290 case TIOCSPGRP: 291 return (fsetown(-(*(int *)ap->a_data), &logsoftc.sc_sigio)); 292 293 /* This is deprecated, FIOGETOWN should be used instead */ 294 case TIOCGPGRP: 295 *(int *)ap->a_data = -fgetown(logsoftc.sc_sigio); 296 break; 297 298 default: 299 return (ENOTTY); 300 } 301 return (0); 302 } 303 304 static void 305 log_drvinit(void *unused) 306 { 307 make_dev(&log_ops, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 308 } 309 310 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 311