subr_log.c revision 1.21
1/* $OpenBSD: subr_log.c,v 1.21 2014/07/12 18:43:32 tedu 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#include <sys/malloc.h> 51#include <sys/filedesc.h> 52#include <sys/socket.h> 53#include <sys/socketvar.h> 54 55#ifdef KTRACE 56#include <sys/ktrace.h> 57#endif 58 59#include <sys/mount.h> 60#include <sys/syscallargs.h> 61 62#define LOG_RDPRI (PZERO + 1) 63 64#define LOG_ASYNC 0x04 65#define LOG_RDWAIT 0x08 66 67struct logsoftc { 68 int sc_state; /* see above for possibilities */ 69 struct selinfo sc_selp; /* process waiting on select call */ 70 int sc_pgid; /* process/group for async I/O */ 71 uid_t sc_siguid; /* uid for process that set sc_pgid */ 72 uid_t sc_sigeuid; /* euid for process that set sc_pgid */ 73} logsoftc; 74 75int log_open; /* also used in log() */ 76int msgbufmapped; /* is the message buffer mapped */ 77int msgbufenabled; /* is logging to the buffer enabled */ 78struct msgbuf *msgbufp; /* the mapped buffer, itself. */ 79struct file *syslogf; 80 81void filt_logrdetach(struct knote *kn); 82int filt_logread(struct knote *kn, long hint); 83 84struct filterops logread_filtops = 85 { 1, NULL, filt_logrdetach, filt_logread}; 86 87void 88initmsgbuf(caddr_t buf, size_t bufsize) 89{ 90 struct msgbuf *mbp; 91 long new_bufs; 92 93 /* Sanity-check the given size. */ 94 if (bufsize < sizeof(struct msgbuf)) 95 return; 96 97 mbp = msgbufp = (struct msgbuf *)buf; 98 99 new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc); 100 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) || 101 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) || 102 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) { 103 /* 104 * If the buffer magic number is wrong, has changed 105 * size (which shouldn't happen often), or is 106 * internally inconsistent, initialize it. 107 */ 108 109 memset(buf, 0, bufsize); 110 mbp->msg_magic = MSG_MAGIC; 111 mbp->msg_bufs = new_bufs; 112 } 113 114 /* Always start new buffer data on a new line. */ 115 if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n') 116 msgbuf_putchar('\n'); 117 118 /* mark it as ready for use. */ 119 msgbufmapped = msgbufenabled = 1; 120} 121 122void 123msgbuf_putchar(const char c) 124{ 125 struct msgbuf *mbp = msgbufp; 126 127 if (mbp->msg_magic != MSG_MAGIC) 128 /* Nothing we can do */ 129 return; 130 131 mbp->msg_bufc[mbp->msg_bufx++] = c; 132 mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs); 133 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) 134 mbp->msg_bufx = 0; 135 /* If the buffer is full, keep the most recent data. */ 136 if (mbp->msg_bufr == mbp->msg_bufx) { 137 if (++mbp->msg_bufr >= mbp->msg_bufs) 138 mbp->msg_bufr = 0; 139 } 140} 141 142/*ARGSUSED*/ 143int 144logopen(dev_t dev, int flags, int mode, struct proc *p) 145{ 146 if (log_open) 147 return (EBUSY); 148 log_open = 1; 149 return (0); 150} 151 152/*ARGSUSED*/ 153int 154logclose(dev_t dev, int flag, int mode, struct proc *p) 155{ 156 157 if (syslogf) 158 FRELE(syslogf, p); 159 syslogf = NULL; 160 log_open = 0; 161 logsoftc.sc_state = 0; 162 return (0); 163} 164 165/*ARGSUSED*/ 166int 167logread(dev_t dev, struct uio *uio, int flag) 168{ 169 struct msgbuf *mbp = msgbufp; 170 long l; 171 int s; 172 int error = 0; 173 174 s = splhigh(); 175 while (mbp->msg_bufr == mbp->msg_bufx) { 176 if (flag & IO_NDELAY) { 177 splx(s); 178 return (EWOULDBLOCK); 179 } 180 logsoftc.sc_state |= LOG_RDWAIT; 181 error = tsleep(mbp, LOG_RDPRI | PCATCH, 182 "klog", 0); 183 if (error) { 184 splx(s); 185 return (error); 186 } 187 } 188 splx(s); 189 logsoftc.sc_state &= ~LOG_RDWAIT; 190 191 while (uio->uio_resid > 0) { 192 l = mbp->msg_bufx - mbp->msg_bufr; 193 if (l < 0) 194 l = mbp->msg_bufs - mbp->msg_bufr; 195 l = min(l, uio->uio_resid); 196 if (l == 0) 197 break; 198 error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio); 199 if (error) 200 break; 201 mbp->msg_bufr += l; 202 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 203 mbp->msg_bufr = 0; 204 } 205 return (error); 206} 207 208/*ARGSUSED*/ 209int 210logpoll(dev_t dev, int events, struct proc *p) 211{ 212 int revents = 0; 213 int s = splhigh(); 214 215 if (events & (POLLIN | POLLRDNORM)) { 216 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 217 revents |= events & (POLLIN | POLLRDNORM); 218 else 219 selrecord(p, &logsoftc.sc_selp); 220 } 221 splx(s); 222 return (revents); 223} 224 225int 226logkqfilter(dev_t dev, struct knote *kn) 227{ 228 struct klist *klist; 229 int s; 230 231 switch (kn->kn_filter) { 232 case EVFILT_READ: 233 klist = &logsoftc.sc_selp.si_note; 234 kn->kn_fop = &logread_filtops; 235 break; 236 default: 237 return (EINVAL); 238 } 239 240 kn->kn_hook = (void *)msgbufp; 241 242 s = splhigh(); 243 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 244 splx(s); 245 246 return (0); 247} 248 249void 250filt_logrdetach(struct knote *kn) 251{ 252 int s = splhigh(); 253 254 SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext); 255 splx(s); 256} 257 258int 259filt_logread(struct knote *kn, long hint) 260{ 261 struct msgbuf *p = (struct msgbuf *)kn->kn_hook; 262 263 kn->kn_data = (int)(p->msg_bufx - p->msg_bufr); 264 265 return (p->msg_bufx != p->msg_bufr); 266} 267 268void 269logwakeup(void) 270{ 271 if (!log_open) 272 return; 273 selwakeup(&logsoftc.sc_selp); 274 if (logsoftc.sc_state & LOG_ASYNC) 275 csignal(logsoftc.sc_pgid, SIGIO, 276 logsoftc.sc_siguid, logsoftc.sc_sigeuid); 277 if (logsoftc.sc_state & LOG_RDWAIT) { 278 wakeup(msgbufp); 279 logsoftc.sc_state &= ~LOG_RDWAIT; 280 } 281} 282 283/*ARGSUSED*/ 284int 285logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 286{ 287 struct file *fp; 288 long l; 289 int error, s; 290 291 switch (com) { 292 293 /* return number of characters immediately available */ 294 case FIONREAD: 295 s = splhigh(); 296 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 297 splx(s); 298 if (l < 0) 299 l += msgbufp->msg_bufs; 300 *(int *)data = l; 301 break; 302 303 case FIONBIO: 304 break; 305 306 case FIOASYNC: 307 if (*(int *)data) 308 logsoftc.sc_state |= LOG_ASYNC; 309 else 310 logsoftc.sc_state &= ~LOG_ASYNC; 311 break; 312 313 case TIOCSPGRP: 314 logsoftc.sc_pgid = *(int *)data; 315 logsoftc.sc_siguid = p->p_ucred->cr_ruid; 316 logsoftc.sc_sigeuid = p->p_ucred->cr_uid; 317 break; 318 319 case TIOCGPGRP: 320 *(int *)data = logsoftc.sc_pgid; 321 break; 322 323 case LIOCSFD: 324 if ((error = suser(p, 0)) != 0) 325 return (error); 326 if ((error = getsock(p->p_fd, *(int *)data, &fp)) != 0) 327 return (error); 328 if (syslogf) 329 FRELE(syslogf, p); 330 syslogf = fp; 331 break; 332 333 default: 334 return (ENOTTY); 335 } 336 return (0); 337} 338 339int 340sys_sendsyslog(struct proc *p, void *v, register_t *retval) 341{ 342 struct sys_sendsyslog_args /* { 343 syscallarg(const void *) buf; 344 syscallarg(size_t) nbyte; 345 } */ *uap = v; 346#ifdef KTRACE 347 struct iovec *ktriov = NULL; 348#endif 349 struct iovec aiov; 350 struct uio auio; 351 struct file *f; 352 int error; 353 354 if (syslogf == NULL) 355 return (ENOTCONN); 356 f = syslogf; 357 FREF(f); 358 359 aiov.iov_base = (char *)SCARG(uap, buf); 360 aiov.iov_len = SCARG(uap, nbyte); 361 auio.uio_iov = &aiov; 362 auio.uio_iovcnt = 1; 363 auio.uio_segflg = UIO_USERSPACE; 364 auio.uio_rw = UIO_WRITE; 365 auio.uio_procp = p; 366 auio.uio_offset = 0; 367 auio.uio_resid = aiov.iov_len; 368#ifdef KTRACE 369 if (KTRPOINT(p, KTR_GENIO)) { 370 int iovlen = auio.uio_iovcnt * sizeof (struct iovec); 371 372 ktriov = malloc(iovlen, M_TEMP, M_WAITOK); 373 bcopy(auio.uio_iov, ktriov, iovlen); 374 } 375#endif 376 377 error = sosend(f->f_data, NULL, &auio, NULL, NULL, 0); 378 379#ifdef KTRACE 380 if (ktriov != NULL) { 381 if (error == 0) 382 ktrgenio(p, 0, UIO_WRITE, ktriov, aiov.iov_len); 383 free(ktriov, M_TEMP, 0); 384 } 385#endif 386 FRELE(f, p); 387 return error; 388} 389