subr_log.c revision 1.53
1/* $OpenBSD: subr_log.c,v 1.53 2017/09/25 23:00:33 espie 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/tty.h> 47#include <sys/signalvar.h> 48#include <sys/syslog.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#include <dev/cons.h> 63 64#define LOG_RDPRI (PZERO + 1) 65 66#define LOG_ASYNC 0x04 67#define LOG_RDWAIT 0x08 68 69struct logsoftc { 70 int sc_state; /* see above for possibilities */ 71 struct selinfo sc_selp; /* process waiting on select call */ 72 int sc_pgid; /* process/group for async I/O */ 73 uid_t sc_siguid; /* uid for process that set sc_pgid */ 74 uid_t sc_sigeuid; /* euid for process that set sc_pgid */ 75} logsoftc; 76 77int log_open; /* also used in log() */ 78int msgbufmapped; /* is the message buffer mapped */ 79struct msgbuf *msgbufp; /* the mapped buffer, itself. */ 80struct msgbuf *consbufp; /* console message buffer. */ 81struct file *syslogf; 82 83void filt_logrdetach(struct knote *kn); 84int filt_logread(struct knote *kn, long hint); 85 86struct filterops logread_filtops = 87 { 1, NULL, filt_logrdetach, filt_logread}; 88 89int dosendsyslog(struct proc *, const char *, size_t, int, enum uio_seg); 90 91void 92initmsgbuf(caddr_t buf, size_t bufsize) 93{ 94 struct msgbuf *mbp; 95 long new_bufs; 96 97 /* Sanity-check the given size. */ 98 if (bufsize < sizeof(struct msgbuf)) 99 return; 100 101 mbp = msgbufp = (struct msgbuf *)buf; 102 103 new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc); 104 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) || 105 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) || 106 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) { 107 /* 108 * If the buffer magic number is wrong, has changed 109 * size (which shouldn't happen often), or is 110 * internally inconsistent, initialize it. 111 */ 112 113 memset(buf, 0, bufsize); 114 mbp->msg_magic = MSG_MAGIC; 115 mbp->msg_bufs = new_bufs; 116 } 117 118 /* Always start new buffer data on a new line. */ 119 if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n') 120 msgbuf_putchar(msgbufp, '\n'); 121 122 /* mark it as ready for use. */ 123 msgbufmapped = 1; 124} 125 126void 127initconsbuf(void) 128{ 129 long new_bufs; 130 131 /* Set up a buffer to collect /dev/console output */ 132 consbufp = malloc(CONSBUFSIZE, M_TEMP, M_NOWAIT|M_ZERO); 133 if (consbufp) { 134 new_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc); 135 consbufp->msg_magic = MSG_MAGIC; 136 consbufp->msg_bufs = new_bufs; 137 } 138} 139 140void 141msgbuf_putchar(struct msgbuf *mbp, const char c) 142{ 143 int s; 144 145 if (mbp->msg_magic != MSG_MAGIC) 146 /* Nothing we can do */ 147 return; 148 149 s = splhigh(); 150 mbp->msg_bufc[mbp->msg_bufx++] = c; 151 mbp->msg_bufl = lmin(mbp->msg_bufl+1, mbp->msg_bufs); 152 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) 153 mbp->msg_bufx = 0; 154 /* If the buffer is full, keep the most recent data. */ 155 if (mbp->msg_bufr == mbp->msg_bufx) { 156 if (++mbp->msg_bufr >= mbp->msg_bufs) 157 mbp->msg_bufr = 0; 158 mbp->msg_bufd++; 159 } 160 splx(s); 161} 162 163int 164logopen(dev_t dev, int flags, int mode, struct proc *p) 165{ 166 if (log_open) 167 return (EBUSY); 168 log_open = 1; 169 return (0); 170} 171 172int 173logclose(dev_t dev, int flag, int mode, struct proc *p) 174{ 175 struct file *fp; 176 177 fp = syslogf; 178 syslogf = NULL; 179 if (fp) 180 FRELE(fp, p); 181 log_open = 0; 182 logsoftc.sc_state = 0; 183 return (0); 184} 185 186int 187logread(dev_t dev, struct uio *uio, int flag) 188{ 189 struct msgbuf *mbp = msgbufp; 190 size_t l; 191 int s, error = 0; 192 193 s = splhigh(); 194 while (mbp->msg_bufr == mbp->msg_bufx) { 195 if (flag & IO_NDELAY) { 196 error = EWOULDBLOCK; 197 goto out; 198 } 199 logsoftc.sc_state |= LOG_RDWAIT; 200 error = tsleep(mbp, LOG_RDPRI | PCATCH, 201 "klog", 0); 202 if (error) 203 goto out; 204 } 205 logsoftc.sc_state &= ~LOG_RDWAIT; 206 207 if (mbp->msg_bufd > 0) { 208 char buf[64]; 209 210 l = snprintf(buf, sizeof(buf), 211 "<%d>klog: dropped %ld byte%s, message buffer full\n", 212 LOG_KERN|LOG_WARNING, mbp->msg_bufd, 213 mbp->msg_bufd == 1 ? "" : "s"); 214 error = uiomove(buf, ulmin(l, sizeof(buf) - 1), uio); 215 if (error) 216 goto out; 217 mbp->msg_bufd = 0; 218 } 219 220 while (uio->uio_resid > 0) { 221 if (mbp->msg_bufx >= mbp->msg_bufr) 222 l = mbp->msg_bufx - mbp->msg_bufr; 223 else 224 l = mbp->msg_bufs - mbp->msg_bufr; 225 l = ulmin(l, uio->uio_resid); 226 if (l == 0) 227 break; 228 error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], l, uio); 229 if (error) 230 break; 231 mbp->msg_bufr += l; 232 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 233 mbp->msg_bufr = 0; 234 } 235 out: 236 splx(s); 237 return (error); 238} 239 240int 241logpoll(dev_t dev, int events, struct proc *p) 242{ 243 int s, revents = 0; 244 245 s = splhigh(); 246 if (events & (POLLIN | POLLRDNORM)) { 247 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 248 revents |= events & (POLLIN | POLLRDNORM); 249 else 250 selrecord(p, &logsoftc.sc_selp); 251 } 252 splx(s); 253 return (revents); 254} 255 256int 257logkqfilter(dev_t dev, struct knote *kn) 258{ 259 struct klist *klist; 260 int s; 261 262 switch (kn->kn_filter) { 263 case EVFILT_READ: 264 klist = &logsoftc.sc_selp.si_note; 265 kn->kn_fop = &logread_filtops; 266 break; 267 default: 268 return (EINVAL); 269 } 270 271 kn->kn_hook = (void *)msgbufp; 272 273 s = splhigh(); 274 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 275 splx(s); 276 277 return (0); 278} 279 280void 281filt_logrdetach(struct knote *kn) 282{ 283 int s; 284 285 s = splhigh(); 286 SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext); 287 splx(s); 288} 289 290int 291filt_logread(struct knote *kn, long hint) 292{ 293 struct msgbuf *p = (struct msgbuf *)kn->kn_hook; 294 int s, event = 0; 295 296 s = splhigh(); 297 kn->kn_data = (int)(p->msg_bufx - p->msg_bufr); 298 event = (p->msg_bufx != p->msg_bufr); 299 splx(s); 300 return (event); 301} 302 303void 304logwakeup(void) 305{ 306 if (!log_open) 307 return; 308 selwakeup(&logsoftc.sc_selp); 309 if (logsoftc.sc_state & LOG_ASYNC) 310 csignal(logsoftc.sc_pgid, SIGIO, 311 logsoftc.sc_siguid, logsoftc.sc_sigeuid); 312 if (logsoftc.sc_state & LOG_RDWAIT) { 313 wakeup(msgbufp); 314 logsoftc.sc_state &= ~LOG_RDWAIT; 315 } 316} 317 318int 319logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 320{ 321 struct file *fp; 322 long l; 323 int error, s; 324 325 switch (com) { 326 327 /* return number of characters immediately available */ 328 case FIONREAD: 329 s = splhigh(); 330 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 331 splx(s); 332 if (l < 0) 333 l += msgbufp->msg_bufs; 334 *(int *)data = l; 335 break; 336 337 case FIONBIO: 338 break; 339 340 case FIOASYNC: 341 if (*(int *)data) 342 logsoftc.sc_state |= LOG_ASYNC; 343 else 344 logsoftc.sc_state &= ~LOG_ASYNC; 345 break; 346 347 case TIOCSPGRP: 348 logsoftc.sc_pgid = *(int *)data; 349 logsoftc.sc_siguid = p->p_ucred->cr_ruid; 350 logsoftc.sc_sigeuid = p->p_ucred->cr_uid; 351 break; 352 353 case TIOCGPGRP: 354 *(int *)data = logsoftc.sc_pgid; 355 break; 356 357 case LIOCSFD: 358 if ((error = suser(p, 0)) != 0) 359 return (error); 360 fp = syslogf; 361 if ((error = getsock(p, *(int *)data, &syslogf)) != 0) 362 return (error); 363 if (fp) 364 FRELE(fp, p); 365 break; 366 367 default: 368 return (ENOTTY); 369 } 370 return (0); 371} 372 373int 374sys_sendsyslog(struct proc *p, void *v, register_t *retval) 375{ 376 struct sys_sendsyslog_args /* { 377 syscallarg(const char *) buf; 378 syscallarg(size_t) nbyte; 379 syscallarg(int) flags; 380 } */ *uap = v; 381 int error; 382 static int dropped_count, orig_error; 383 384 if (dropped_count) { 385 size_t l; 386 char buf[64]; 387 388 l = snprintf(buf, sizeof(buf), 389 "<%d>sendsyslog: dropped %d message%s, error %d", 390 LOG_KERN|LOG_WARNING, dropped_count, 391 dropped_count == 1 ? "" : "s", orig_error); 392 error = dosendsyslog(p, buf, ulmin(l, sizeof(buf) - 1), 393 0, UIO_SYSSPACE); 394 if (error == 0) 395 dropped_count = 0; 396 } 397 error = dosendsyslog(p, SCARG(uap, buf), SCARG(uap, nbyte), 398 SCARG(uap, flags), UIO_USERSPACE); 399 if (error) { 400 dropped_count++; 401 orig_error = error; 402 } 403 return (error); 404} 405 406int 407dosendsyslog(struct proc *p, const char *buf, size_t nbyte, int flags, 408 enum uio_seg sflg) 409{ 410#ifdef KTRACE 411 struct iovec *ktriov = NULL; 412 int iovlen; 413#endif 414 struct file *fp; 415 char pri[6], *kbuf; 416 struct iovec aiov; 417 struct uio auio; 418 size_t i, len; 419 int error; 420 421 if (nbyte > LOG_MAXLINE) 422 nbyte = LOG_MAXLINE; 423 424 /* Global variable syslogf may change during sleep, use local copy. */ 425 fp = syslogf; 426 if (fp) 427 FREF(fp); 428 else if (!ISSET(flags, LOG_CONS)) 429 return (ENOTCONN); 430 else { 431 /* 432 * Strip off syslog priority when logging to console. 433 * LOG_PRIMASK | LOG_FACMASK is 0x03ff, so at most 4 434 * decimal digits may appear in priority as <1023>. 435 */ 436 len = MIN(nbyte, sizeof(pri)); 437 if (sflg == UIO_USERSPACE) { 438 if ((error = copyin(buf, pri, len))) 439 return (error); 440 } else 441 memcpy(pri, buf, len); 442 if (0 < len && pri[0] == '<') { 443 for (i = 1; i < len; i++) { 444 if (pri[i] < '0' || pri[i] > '9') 445 break; 446 } 447 if (i < len && pri[i] == '>') { 448 i++; 449 /* There must be at least one digit <0>. */ 450 if (i >= 3) { 451 buf += i; 452 nbyte -= i; 453 } 454 } 455 } 456 } 457 458 aiov.iov_base = (char *)buf; 459 aiov.iov_len = nbyte; 460 auio.uio_iov = &aiov; 461 auio.uio_iovcnt = 1; 462 auio.uio_segflg = sflg; 463 auio.uio_rw = UIO_WRITE; 464 auio.uio_procp = p; 465 auio.uio_offset = 0; 466 auio.uio_resid = aiov.iov_len; 467#ifdef KTRACE 468 if (KTRPOINT(p, KTR_GENIO)) { 469 ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec), 470 M_TEMP, M_WAITOK); 471 iovlen = auio.uio_iovcnt * sizeof (struct iovec); 472 473 memcpy(ktriov, auio.uio_iov, iovlen); 474 } 475#endif 476 477 len = auio.uio_resid; 478 if (fp) { 479 error = sosend(fp->f_data, NULL, &auio, NULL, NULL, 0); 480 if (error == 0) 481 len -= auio.uio_resid; 482 } else if (constty || cn_devvp) { 483 error = cnwrite(0, &auio, 0); 484 if (error == 0) 485 len -= auio.uio_resid; 486 aiov.iov_base = "\r\n"; 487 aiov.iov_len = 2; 488 auio.uio_iov = &aiov; 489 auio.uio_iovcnt = 1; 490 auio.uio_segflg = UIO_SYSSPACE; 491 auio.uio_rw = UIO_WRITE; 492 auio.uio_procp = p; 493 auio.uio_offset = 0; 494 auio.uio_resid = aiov.iov_len; 495 cnwrite(0, &auio, 0); 496 } else { 497 /* XXX console redirection breaks down... */ 498 if (sflg == UIO_USERSPACE) { 499 kbuf = malloc(len, M_TEMP, M_WAITOK); 500 error = copyin(aiov.iov_base, kbuf, len); 501 } else { 502 kbuf = aiov.iov_base; 503 error = 0; 504 } 505 if (error == 0) 506 for (i = 0; i < len; i++) { 507 if (kbuf[i] == '\0') 508 break; 509 cnputc(kbuf[i]); 510 auio.uio_resid--; 511 } 512 if (sflg == UIO_USERSPACE) 513 free(kbuf, M_TEMP, len); 514 if (error == 0) 515 len -= auio.uio_resid; 516 cnputc('\n'); 517 } 518 519#ifdef KTRACE 520 if (ktriov != NULL) { 521 if (error == 0) 522 ktrgenio(p, -1, UIO_WRITE, ktriov, len); 523 free(ktriov, M_TEMP, iovlen); 524 } 525#endif 526 if (fp) 527 FRELE(fp, p); 528 else 529 error = ENOTCONN; 530 return (error); 531} 532