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