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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 30 */ 31 32 /* 33 * Error log buffer for kernel printf's. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/conf.h> 42 #include <sys/proc.h> 43 #include <sys/vnode.h> 44 #include <sys/filio.h> 45 #include <sys/ttycom.h> 46 #include <sys/msgbuf.h> 47 #include <sys/signalvar.h> 48 #include <sys/kernel.h> 49 #include <sys/poll.h> 50 #include <sys/filedesc.h> 51 #include <sys/sysctl.h> 52 53 #define LOG_RDPRI (PZERO + 1) 54 55 #define LOG_ASYNC 0x04 56 #define LOG_RDWAIT 0x08 57 58 static d_open_t logopen; 59 static d_close_t logclose; 60 static d_read_t logread; 61 static d_ioctl_t logioctl; 62 static d_poll_t logpoll; 63 64 static void logtimeout(void *arg); 65 66 #define CDEV_MAJOR 7 67 static struct cdevsw log_cdevsw = { 68 .d_version = D_VERSION, 69 .d_flags = D_NEEDGIANT, 70 .d_open = logopen, 71 .d_close = logclose, 72 .d_read = logread, 73 .d_ioctl = logioctl, 74 .d_poll = logpoll, 75 .d_name = "log", 76 .d_maj = CDEV_MAJOR, 77 }; 78 79 static struct logsoftc { 80 int sc_state; /* see above for possibilities */ 81 struct selinfo sc_selp; /* process waiting on select call */ 82 struct sigio *sc_sigio; /* information for async I/O */ 83 struct callout sc_callout; /* callout to wakeup syslog */ 84 } logsoftc; 85 86 int log_open; /* also used in log() */ 87 88 /* Times per second to check for a pending syslog wakeup. */ 89 static int log_wakeups_per_second = 5; 90 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, 91 &log_wakeups_per_second, 0, ""); 92 93 /*ARGSUSED*/ 94 static int 95 logopen(struct cdev *dev, int flags, int mode, struct thread *td) 96 { 97 if (log_open) 98 return (EBUSY); 99 log_open = 1; 100 callout_init(&logsoftc.sc_callout, 0); 101 fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */ 102 if (log_wakeups_per_second < 1) { 103 printf("syslog wakeup is less than one. Adjusting to 1.\n"); 104 log_wakeups_per_second = 1; 105 } 106 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 107 logtimeout, NULL); 108 return (0); 109 } 110 111 /*ARGSUSED*/ 112 static int 113 logclose(struct cdev *dev, int flag, int mode, struct thread *td) 114 { 115 116 log_open = 0; 117 callout_stop(&logsoftc.sc_callout); 118 logsoftc.sc_state = 0; 119 funsetown(&logsoftc.sc_sigio); 120 return (0); 121 } 122 123 /*ARGSUSED*/ 124 static int 125 logread(struct cdev *dev, struct uio *uio, int flag) 126 { 127 char buf[128]; 128 struct msgbuf *mbp = msgbufp; 129 int error = 0, l, s; 130 131 s = splhigh(); 132 while (msgbuf_getcount(mbp) == 0) { 133 if (flag & IO_NDELAY) { 134 splx(s); 135 return (EWOULDBLOCK); 136 } 137 logsoftc.sc_state |= LOG_RDWAIT; 138 if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) { 139 splx(s); 140 return (error); 141 } 142 } 143 splx(s); 144 logsoftc.sc_state &= ~LOG_RDWAIT; 145 146 while (uio->uio_resid > 0) { 147 l = imin(sizeof(buf), uio->uio_resid); 148 l = msgbuf_getbytes(mbp, buf, l); 149 if (l == 0) 150 break; 151 error = uiomove(buf, l, uio); 152 if (error) 153 break; 154 } 155 return (error); 156 } 157 158 /*ARGSUSED*/ 159 static int 160 logpoll(struct cdev *dev, int events, struct thread *td) 161 { 162 int s; 163 int revents = 0; 164 165 s = splhigh(); 166 167 if (events & (POLLIN | POLLRDNORM)) { 168 if (msgbuf_getcount(msgbufp) > 0) 169 revents |= events & (POLLIN | POLLRDNORM); 170 else 171 selrecord(td, &logsoftc.sc_selp); 172 } 173 splx(s); 174 return (revents); 175 } 176 177 static void 178 logtimeout(void *arg) 179 { 180 181 if (!log_open) 182 return; 183 if (log_wakeups_per_second < 1) { 184 printf("syslog wakeup is less than one. Adjusting to 1.\n"); 185 log_wakeups_per_second = 1; 186 } 187 if (msgbuftrigger == 0) { 188 callout_reset(&logsoftc.sc_callout, 189 hz / log_wakeups_per_second, logtimeout, NULL); 190 return; 191 } 192 msgbuftrigger = 0; 193 selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI); 194 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL) 195 pgsigio(&logsoftc.sc_sigio, SIGIO, 0); 196 if (logsoftc.sc_state & LOG_RDWAIT) { 197 wakeup(msgbufp); 198 logsoftc.sc_state &= ~LOG_RDWAIT; 199 } 200 callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second, 201 logtimeout, NULL); 202 } 203 204 /*ARGSUSED*/ 205 static int 206 logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td) 207 { 208 209 switch (com) { 210 211 /* return number of characters immediately available */ 212 case FIONREAD: 213 *(int *)data = msgbuf_getcount(msgbufp); 214 break; 215 216 case FIONBIO: 217 break; 218 219 case FIOASYNC: 220 if (*(int *)data) 221 logsoftc.sc_state |= LOG_ASYNC; 222 else 223 logsoftc.sc_state &= ~LOG_ASYNC; 224 break; 225 226 case FIOSETOWN: 227 return (fsetown(*(int *)data, &logsoftc.sc_sigio)); 228 229 case FIOGETOWN: 230 *(int *)data = fgetown(&logsoftc.sc_sigio); 231 break; 232 233 /* This is deprecated, FIOSETOWN should be used instead. */ 234 case TIOCSPGRP: 235 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio)); 236 237 /* This is deprecated, FIOGETOWN should be used instead */ 238 case TIOCGPGRP: 239 *(int *)data = -fgetown(&logsoftc.sc_sigio); 240 break; 241 242 default: 243 return (ENOTTY); 244 } 245 return (0); 246 } 247 248 static void 249 log_drvinit(void *unused) 250 { 251 252 make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog"); 253 } 254 255 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL) 256