subr_log.c revision 1.27
1/* $OpenBSD: subr_log.c,v 1.27 2015/01/13 18:51:27 kettenis 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 */ 77struct msgbuf *msgbufp; /* the mapped buffer, itself. */ 78struct msgbuf *consbufp; /* console message buffer. */ 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(msgbufp, '\n'); 117 118 /* mark it as ready for use. */ 119 msgbufmapped = 1; 120} 121 122void 123initconsbuf(void) 124{ 125 long new_bufs; 126 127 /* Set up a buffer to collect /dev/console output */ 128 consbufp = malloc(CONSBUFSIZE, M_TEMP, M_NOWAIT|M_ZERO); 129 if (consbufp) { 130 new_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc); 131 consbufp->msg_magic = MSG_MAGIC; 132 consbufp->msg_bufs = new_bufs; 133 } 134} 135 136void 137msgbuf_putchar(struct msgbuf *mbp, const char c) 138{ 139 if (mbp->msg_magic != MSG_MAGIC) 140 /* Nothing we can do */ 141 return; 142 143 mbp->msg_bufc[mbp->msg_bufx++] = c; 144 mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs); 145 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) 146 mbp->msg_bufx = 0; 147 /* If the buffer is full, keep the most recent data. */ 148 if (mbp->msg_bufr == mbp->msg_bufx) { 149 if (++mbp->msg_bufr >= mbp->msg_bufs) 150 mbp->msg_bufr = 0; 151 } 152} 153 154/*ARGSUSED*/ 155int 156logopen(dev_t dev, int flags, int mode, struct proc *p) 157{ 158 if (log_open) 159 return (EBUSY); 160 log_open = 1; 161 return (0); 162} 163 164/*ARGSUSED*/ 165int 166logclose(dev_t dev, int flag, int mode, struct proc *p) 167{ 168 169 if (syslogf) 170 FRELE(syslogf, p); 171 syslogf = NULL; 172 log_open = 0; 173 logsoftc.sc_state = 0; 174 return (0); 175} 176 177/*ARGSUSED*/ 178int 179logread(dev_t dev, struct uio *uio, int flag) 180{ 181 struct msgbuf *mbp = msgbufp; 182 long l; 183 int s; 184 int error = 0; 185 186 s = splhigh(); 187 while (mbp->msg_bufr == mbp->msg_bufx) { 188 if (flag & IO_NDELAY) { 189 splx(s); 190 return (EWOULDBLOCK); 191 } 192 logsoftc.sc_state |= LOG_RDWAIT; 193 error = tsleep(mbp, LOG_RDPRI | PCATCH, 194 "klog", 0); 195 if (error) { 196 splx(s); 197 return (error); 198 } 199 } 200 splx(s); 201 logsoftc.sc_state &= ~LOG_RDWAIT; 202 203 while (uio->uio_resid > 0) { 204 l = mbp->msg_bufx - mbp->msg_bufr; 205 if (l < 0) 206 l = mbp->msg_bufs - mbp->msg_bufr; 207 l = min(l, uio->uio_resid); 208 if (l == 0) 209 break; 210 error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio); 211 if (error) 212 break; 213 mbp->msg_bufr += l; 214 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 215 mbp->msg_bufr = 0; 216 } 217 return (error); 218} 219 220/*ARGSUSED*/ 221int 222logpoll(dev_t dev, int events, struct proc *p) 223{ 224 int revents = 0; 225 int s = splhigh(); 226 227 if (events & (POLLIN | POLLRDNORM)) { 228 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 229 revents |= events & (POLLIN | POLLRDNORM); 230 else 231 selrecord(p, &logsoftc.sc_selp); 232 } 233 splx(s); 234 return (revents); 235} 236 237int 238logkqfilter(dev_t dev, struct knote *kn) 239{ 240 struct klist *klist; 241 int s; 242 243 switch (kn->kn_filter) { 244 case EVFILT_READ: 245 klist = &logsoftc.sc_selp.si_note; 246 kn->kn_fop = &logread_filtops; 247 break; 248 default: 249 return (EINVAL); 250 } 251 252 kn->kn_hook = (void *)msgbufp; 253 254 s = splhigh(); 255 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 256 splx(s); 257 258 return (0); 259} 260 261void 262filt_logrdetach(struct knote *kn) 263{ 264 int s = splhigh(); 265 266 SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext); 267 splx(s); 268} 269 270int 271filt_logread(struct knote *kn, long hint) 272{ 273 struct msgbuf *p = (struct msgbuf *)kn->kn_hook; 274 275 kn->kn_data = (int)(p->msg_bufx - p->msg_bufr); 276 277 return (p->msg_bufx != p->msg_bufr); 278} 279 280void 281logwakeup(void) 282{ 283 if (!log_open) 284 return; 285 selwakeup(&logsoftc.sc_selp); 286 if (logsoftc.sc_state & LOG_ASYNC) 287 csignal(logsoftc.sc_pgid, SIGIO, 288 logsoftc.sc_siguid, logsoftc.sc_sigeuid); 289 if (logsoftc.sc_state & LOG_RDWAIT) { 290 wakeup(msgbufp); 291 logsoftc.sc_state &= ~LOG_RDWAIT; 292 } 293} 294 295/*ARGSUSED*/ 296int 297logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 298{ 299 struct file *fp; 300 long l; 301 int error, s; 302 303 switch (com) { 304 305 /* return number of characters immediately available */ 306 case FIONREAD: 307 s = splhigh(); 308 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 309 splx(s); 310 if (l < 0) 311 l += msgbufp->msg_bufs; 312 *(int *)data = l; 313 break; 314 315 case FIONBIO: 316 break; 317 318 case FIOASYNC: 319 if (*(int *)data) 320 logsoftc.sc_state |= LOG_ASYNC; 321 else 322 logsoftc.sc_state &= ~LOG_ASYNC; 323 break; 324 325 case TIOCSPGRP: 326 logsoftc.sc_pgid = *(int *)data; 327 logsoftc.sc_siguid = p->p_ucred->cr_ruid; 328 logsoftc.sc_sigeuid = p->p_ucred->cr_uid; 329 break; 330 331 case TIOCGPGRP: 332 *(int *)data = logsoftc.sc_pgid; 333 break; 334 335 case LIOCSFD: 336 if ((error = suser(p, 0)) != 0) 337 return (error); 338 if ((error = getsock(p->p_fd, *(int *)data, &fp)) != 0) 339 return (error); 340 if (syslogf) 341 FRELE(syslogf, p); 342 syslogf = fp; 343 break; 344 345 default: 346 return (ENOTTY); 347 } 348 return (0); 349} 350 351int 352sys_sendsyslog(struct proc *p, void *v, register_t *retval) 353{ 354 struct sys_sendsyslog_args /* { 355 syscallarg(const void *) buf; 356 syscallarg(size_t) nbyte; 357 } */ *uap = v; 358#ifdef KTRACE 359 struct iovec *ktriov = NULL; 360 int iovlen; 361#endif 362 struct iovec aiov; 363 struct uio auio; 364 struct file *f; 365 size_t len; 366 int error; 367 368 if (syslogf == NULL) 369 return (ENOTCONN); 370 f = syslogf; 371 FREF(f); 372 373 aiov.iov_base = (char *)SCARG(uap, buf); 374 aiov.iov_len = SCARG(uap, nbyte); 375 auio.uio_iov = &aiov; 376 auio.uio_iovcnt = 1; 377 auio.uio_segflg = UIO_USERSPACE; 378 auio.uio_rw = UIO_WRITE; 379 auio.uio_procp = p; 380 auio.uio_offset = 0; 381 auio.uio_resid = aiov.iov_len; 382#ifdef KTRACE 383 if (KTRPOINT(p, KTR_GENIO)) { 384 ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec), 385 M_TEMP, M_WAITOK); 386 iovlen = auio.uio_iovcnt * sizeof (struct iovec); 387 388 memcpy(ktriov, auio.uio_iov, iovlen); 389 } 390#endif 391 392 len = auio.uio_resid; 393 error = sosend(f->f_data, NULL, &auio, NULL, NULL, 0); 394 if (error == 0) 395 len -= auio.uio_resid; 396 397#ifdef KTRACE 398 if (ktriov != NULL) { 399 if (error == 0) 400 ktrgenio(p, -1, UIO_WRITE, ktriov, len); 401 free(ktriov, M_TEMP, iovlen); 402 } 403#endif 404 FRELE(f, p); 405 return error; 406} 407