subr_log.c revision 1.19
1/* $OpenBSD: subr_log.c,v 1.19 2014/03/30 21:54:48 guenther 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 33 */ 34 35/* 36 * Error log buffer for kernel printf's. 37 */ 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/proc.h> 42#include <sys/vnode.h> 43#include <sys/ioctl.h> 44#include <sys/msgbuf.h> 45#include <sys/file.h> 46#include <sys/signalvar.h> 47#include <sys/syslog.h> 48#include <sys/conf.h> 49#include <sys/poll.h> 50 51#define LOG_RDPRI (PZERO + 1) 52 53#define LOG_ASYNC 0x04 54#define LOG_RDWAIT 0x08 55 56struct logsoftc { 57 int sc_state; /* see above for possibilities */ 58 struct selinfo sc_selp; /* process waiting on select call */ 59 int sc_pgid; /* process/group for async I/O */ 60 uid_t sc_siguid; /* uid for process that set sc_pgid */ 61 uid_t sc_sigeuid; /* euid for process that set sc_pgid */ 62} logsoftc; 63 64int log_open; /* also used in log() */ 65int msgbufmapped; /* is the message buffer mapped */ 66int msgbufenabled; /* is logging to the buffer enabled */ 67struct msgbuf *msgbufp; /* the mapped buffer, itself. */ 68 69void filt_logrdetach(struct knote *kn); 70int filt_logread(struct knote *kn, long hint); 71 72struct filterops logread_filtops = 73 { 1, NULL, filt_logrdetach, filt_logread}; 74 75void 76initmsgbuf(caddr_t buf, size_t bufsize) 77{ 78 struct msgbuf *mbp; 79 long new_bufs; 80 81 /* Sanity-check the given size. */ 82 if (bufsize < sizeof(struct msgbuf)) 83 return; 84 85 mbp = msgbufp = (struct msgbuf *)buf; 86 87 new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc); 88 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) || 89 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) || 90 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) { 91 /* 92 * If the buffer magic number is wrong, has changed 93 * size (which shouldn't happen often), or is 94 * internally inconsistent, initialize it. 95 */ 96 97 memset(buf, 0, bufsize); 98 mbp->msg_magic = MSG_MAGIC; 99 mbp->msg_bufs = new_bufs; 100 } 101 102 /* Always start new buffer data on a new line. */ 103 if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n') 104 msgbuf_putchar('\n'); 105 106 /* mark it as ready for use. */ 107 msgbufmapped = msgbufenabled = 1; 108} 109 110void 111msgbuf_putchar(const char c) 112{ 113 struct msgbuf *mbp = msgbufp; 114 115 if (mbp->msg_magic != MSG_MAGIC) 116 /* Nothing we can do */ 117 return; 118 119 mbp->msg_bufc[mbp->msg_bufx++] = c; 120 mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs); 121 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) 122 mbp->msg_bufx = 0; 123 /* If the buffer is full, keep the most recent data. */ 124 if (mbp->msg_bufr == mbp->msg_bufx) { 125 if (++mbp->msg_bufr >= mbp->msg_bufs) 126 mbp->msg_bufr = 0; 127 } 128} 129 130/*ARGSUSED*/ 131int 132logopen(dev_t dev, int flags, int mode, struct proc *p) 133{ 134 if (log_open) 135 return (EBUSY); 136 log_open = 1; 137 return (0); 138} 139 140/*ARGSUSED*/ 141int 142logclose(dev_t dev, int flag, int mode, struct proc *p) 143{ 144 145 log_open = 0; 146 logsoftc.sc_state = 0; 147 return (0); 148} 149 150/*ARGSUSED*/ 151int 152logread(dev_t dev, struct uio *uio, int flag) 153{ 154 struct msgbuf *mbp = msgbufp; 155 long l; 156 int s; 157 int error = 0; 158 159 s = splhigh(); 160 while (mbp->msg_bufr == mbp->msg_bufx) { 161 if (flag & IO_NDELAY) { 162 splx(s); 163 return (EWOULDBLOCK); 164 } 165 logsoftc.sc_state |= LOG_RDWAIT; 166 error = tsleep(mbp, LOG_RDPRI | PCATCH, 167 "klog", 0); 168 if (error) { 169 splx(s); 170 return (error); 171 } 172 } 173 splx(s); 174 logsoftc.sc_state &= ~LOG_RDWAIT; 175 176 while (uio->uio_resid > 0) { 177 l = mbp->msg_bufx - mbp->msg_bufr; 178 if (l < 0) 179 l = mbp->msg_bufs - mbp->msg_bufr; 180 l = min(l, uio->uio_resid); 181 if (l == 0) 182 break; 183 error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio); 184 if (error) 185 break; 186 mbp->msg_bufr += l; 187 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 188 mbp->msg_bufr = 0; 189 } 190 return (error); 191} 192 193/*ARGSUSED*/ 194int 195logpoll(dev_t dev, int events, struct proc *p) 196{ 197 int revents = 0; 198 int s = splhigh(); 199 200 if (events & (POLLIN | POLLRDNORM)) { 201 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 202 revents |= events & (POLLIN | POLLRDNORM); 203 else 204 selrecord(p, &logsoftc.sc_selp); 205 } 206 splx(s); 207 return (revents); 208} 209 210int 211logkqfilter(dev_t dev, struct knote *kn) 212{ 213 struct klist *klist; 214 int s; 215 216 switch (kn->kn_filter) { 217 case EVFILT_READ: 218 klist = &logsoftc.sc_selp.si_note; 219 kn->kn_fop = &logread_filtops; 220 break; 221 default: 222 return (EINVAL); 223 } 224 225 kn->kn_hook = (void *)msgbufp; 226 227 s = splhigh(); 228 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 229 splx(s); 230 231 return (0); 232} 233 234void 235filt_logrdetach(struct knote *kn) 236{ 237 int s = splhigh(); 238 239 SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext); 240 splx(s); 241} 242 243int 244filt_logread(struct knote *kn, long hint) 245{ 246 struct msgbuf *p = (struct msgbuf *)kn->kn_hook; 247 248 kn->kn_data = (int)(p->msg_bufx - p->msg_bufr); 249 250 return (p->msg_bufx != p->msg_bufr); 251} 252 253void 254logwakeup(void) 255{ 256 if (!log_open) 257 return; 258 selwakeup(&logsoftc.sc_selp); 259 if (logsoftc.sc_state & LOG_ASYNC) 260 csignal(logsoftc.sc_pgid, SIGIO, 261 logsoftc.sc_siguid, logsoftc.sc_sigeuid); 262 if (logsoftc.sc_state & LOG_RDWAIT) { 263 wakeup(msgbufp); 264 logsoftc.sc_state &= ~LOG_RDWAIT; 265 } 266} 267 268/*ARGSUSED*/ 269int 270logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 271{ 272 long l; 273 int s; 274 275 switch (com) { 276 277 /* return number of characters immediately available */ 278 case FIONREAD: 279 s = splhigh(); 280 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 281 splx(s); 282 if (l < 0) 283 l += msgbufp->msg_bufs; 284 *(int *)data = l; 285 break; 286 287 case FIONBIO: 288 break; 289 290 case FIOASYNC: 291 if (*(int *)data) 292 logsoftc.sc_state |= LOG_ASYNC; 293 else 294 logsoftc.sc_state &= ~LOG_ASYNC; 295 break; 296 297 case TIOCSPGRP: 298 logsoftc.sc_pgid = *(int *)data; 299 logsoftc.sc_siguid = p->p_ucred->cr_ruid; 300 logsoftc.sc_sigeuid = p->p_ucred->cr_uid; 301 break; 302 303 case TIOCGPGRP: 304 *(int *)data = logsoftc.sc_pgid; 305 break; 306 307 default: 308 return (ENOTTY); 309 } 310 return (0); 311} 312