1 /* $OpenBSD: subr_log.c,v 1.48 2016/06/23 15:41:42 bluhm 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 69 struct 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 77 int log_open; /* also used in log() */ 78 int msgbufmapped; /* is the message buffer mapped */ 79 struct msgbuf *msgbufp; /* the mapped buffer, itself. */ 80 struct msgbuf *consbufp; /* console message buffer. */ 81 struct file *syslogf; 82 83 void filt_logrdetach(struct knote *kn); 84 int filt_logread(struct knote *kn, long hint); 85 86 struct filterops logread_filtops = 87 { 1, NULL, filt_logrdetach, filt_logread}; 88 89 int dosendsyslog(struct proc *, const char *, size_t, int, enum uio_seg); 90 91 void 92 initmsgbuf(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 126 void 127 initconsbuf(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 140 void 141 msgbuf_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 163 int 164 logopen(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 172 int 173 logclose(dev_t dev, int flag, int mode, struct proc *p) 174 { 175 176 if (syslogf) 177 FRELE(syslogf, p); 178 syslogf = NULL; 179 log_open = 0; 180 logsoftc.sc_state = 0; 181 return (0); 182 } 183 184 int 185 logread(dev_t dev, struct uio *uio, int flag) 186 { 187 struct msgbuf *mbp = msgbufp; 188 size_t l; 189 int s, error = 0; 190 191 s = splhigh(); 192 while (mbp->msg_bufr == mbp->msg_bufx) { 193 if (flag & IO_NDELAY) { 194 error = EWOULDBLOCK; 195 goto out; 196 } 197 logsoftc.sc_state |= LOG_RDWAIT; 198 error = tsleep(mbp, LOG_RDPRI | PCATCH, 199 "klog", 0); 200 if (error) 201 goto out; 202 } 203 logsoftc.sc_state &= ~LOG_RDWAIT; 204 205 if (mbp->msg_bufd > 0) { 206 char buf[64]; 207 208 l = snprintf(buf, sizeof(buf), 209 "<%d>klog: dropped %ld byte%s, message buffer full\n", 210 LOG_KERN|LOG_WARNING, mbp->msg_bufd, 211 mbp->msg_bufd == 1 ? "" : "s"); 212 error = uiomove(buf, ulmin(l, sizeof(buf) - 1), uio); 213 if (error) 214 goto out; 215 mbp->msg_bufd = 0; 216 } 217 218 while (uio->uio_resid > 0) { 219 if (mbp->msg_bufx >= mbp->msg_bufr) 220 l = mbp->msg_bufx - mbp->msg_bufr; 221 else 222 l = mbp->msg_bufs - mbp->msg_bufr; 223 l = ulmin(l, uio->uio_resid); 224 if (l == 0) 225 break; 226 error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], l, uio); 227 if (error) 228 break; 229 mbp->msg_bufr += l; 230 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 231 mbp->msg_bufr = 0; 232 } 233 out: 234 splx(s); 235 return (error); 236 } 237 238 int 239 logpoll(dev_t dev, int events, struct proc *p) 240 { 241 int s, revents = 0; 242 243 s = splhigh(); 244 if (events & (POLLIN | POLLRDNORM)) { 245 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 246 revents |= events & (POLLIN | POLLRDNORM); 247 else 248 selrecord(p, &logsoftc.sc_selp); 249 } 250 splx(s); 251 return (revents); 252 } 253 254 int 255 logkqfilter(dev_t dev, struct knote *kn) 256 { 257 struct klist *klist; 258 int s; 259 260 switch (kn->kn_filter) { 261 case EVFILT_READ: 262 klist = &logsoftc.sc_selp.si_note; 263 kn->kn_fop = &logread_filtops; 264 break; 265 default: 266 return (EINVAL); 267 } 268 269 kn->kn_hook = (void *)msgbufp; 270 271 s = splhigh(); 272 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 273 splx(s); 274 275 return (0); 276 } 277 278 void 279 filt_logrdetach(struct knote *kn) 280 { 281 int s; 282 283 s = splhigh(); 284 SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext); 285 splx(s); 286 } 287 288 int 289 filt_logread(struct knote *kn, long hint) 290 { 291 struct msgbuf *p = (struct msgbuf *)kn->kn_hook; 292 int s, event = 0; 293 294 s = splhigh(); 295 kn->kn_data = (int)(p->msg_bufx - p->msg_bufr); 296 event = (p->msg_bufx != p->msg_bufr); 297 splx(s); 298 return (event); 299 } 300 301 void 302 logwakeup(void) 303 { 304 if (!log_open) 305 return; 306 selwakeup(&logsoftc.sc_selp); 307 if (logsoftc.sc_state & LOG_ASYNC) 308 csignal(logsoftc.sc_pgid, SIGIO, 309 logsoftc.sc_siguid, logsoftc.sc_sigeuid); 310 if (logsoftc.sc_state & LOG_RDWAIT) { 311 wakeup(msgbufp); 312 logsoftc.sc_state &= ~LOG_RDWAIT; 313 } 314 } 315 316 int 317 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 318 { 319 struct file *fp; 320 long l; 321 int error, s; 322 323 switch (com) { 324 325 /* return number of characters immediately available */ 326 case FIONREAD: 327 s = splhigh(); 328 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 329 splx(s); 330 if (l < 0) 331 l += msgbufp->msg_bufs; 332 *(int *)data = l; 333 break; 334 335 case FIONBIO: 336 break; 337 338 case FIOASYNC: 339 if (*(int *)data) 340 logsoftc.sc_state |= LOG_ASYNC; 341 else 342 logsoftc.sc_state &= ~LOG_ASYNC; 343 break; 344 345 case TIOCSPGRP: 346 logsoftc.sc_pgid = *(int *)data; 347 logsoftc.sc_siguid = p->p_ucred->cr_ruid; 348 logsoftc.sc_sigeuid = p->p_ucred->cr_uid; 349 break; 350 351 case TIOCGPGRP: 352 *(int *)data = logsoftc.sc_pgid; 353 break; 354 355 case LIOCSFD: 356 if ((error = suser(p, 0)) != 0) 357 return (error); 358 if ((error = getsock(p, *(int *)data, &fp)) != 0) 359 return (error); 360 if (syslogf) 361 FRELE(syslogf, p); 362 syslogf = fp; 363 break; 364 365 default: 366 return (ENOTTY); 367 } 368 return (0); 369 } 370 371 int 372 sys_sendsyslog(struct proc *p, void *v, register_t *retval) 373 { 374 struct sys_sendsyslog_args /* { 375 syscallarg(const void *) buf; 376 syscallarg(size_t) nbyte; 377 syscallarg(int) flags; 378 } */ *uap = v; 379 int error; 380 static int dropped_count, orig_error; 381 382 if (dropped_count) { 383 size_t l; 384 char buf[64]; 385 386 l = snprintf(buf, sizeof(buf), 387 "<%d>sendsyslog: dropped %d message%s, error %d", 388 LOG_KERN|LOG_WARNING, dropped_count, 389 dropped_count == 1 ? "" : "s", orig_error); 390 error = dosendsyslog(p, buf, ulmin(l, sizeof(buf) - 1), 391 0, UIO_SYSSPACE); 392 if (error == 0) 393 dropped_count = 0; 394 } 395 error = dosendsyslog(p, SCARG(uap, buf), SCARG(uap, nbyte), 396 SCARG(uap, flags), UIO_USERSPACE); 397 if (error) { 398 dropped_count++; 399 orig_error = error; 400 } 401 return (error); 402 } 403 404 int 405 dosendsyslog(struct proc *p, const char *buf, size_t nbyte, int flags, 406 enum uio_seg sflg) 407 { 408 #ifdef KTRACE 409 struct iovec *ktriov = NULL; 410 int iovlen; 411 #endif 412 char pri[6], *kbuf; 413 struct iovec aiov; 414 struct uio auio; 415 size_t i, len; 416 int error; 417 418 if (syslogf) 419 FREF(syslogf); 420 else if (!ISSET(flags, LOG_CONS)) 421 return (ENOTCONN); 422 else { 423 /* 424 * Strip off syslog priority when logging to console. 425 * LOG_PRIMASK | LOG_FACMASK is 0x03ff, so at most 4 426 * decimal digits may appear in priority as <1023>. 427 */ 428 len = MIN(nbyte, sizeof(pri)); 429 if (sflg == UIO_USERSPACE) { 430 if ((error = copyin(buf, pri, len))) 431 return (error); 432 } else 433 memcpy(pri, buf, len); 434 if (0 < len && pri[0] == '<') { 435 for (i = 1; i < len; i++) { 436 if (pri[i] < '0' || pri[i] > '9') 437 break; 438 } 439 if (i < len && pri[i] == '>') { 440 i++; 441 /* There must be at least one digit <0>. */ 442 if (i >= 3) { 443 buf += i; 444 nbyte -= i; 445 } 446 } 447 } 448 } 449 450 aiov.iov_base = (char *)buf; 451 aiov.iov_len = nbyte; 452 auio.uio_iov = &aiov; 453 auio.uio_iovcnt = 1; 454 auio.uio_segflg = sflg; 455 auio.uio_rw = UIO_WRITE; 456 auio.uio_procp = p; 457 auio.uio_offset = 0; 458 auio.uio_resid = aiov.iov_len; 459 #ifdef KTRACE 460 if (KTRPOINT(p, KTR_GENIO)) { 461 ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec), 462 M_TEMP, M_WAITOK); 463 iovlen = auio.uio_iovcnt * sizeof (struct iovec); 464 465 memcpy(ktriov, auio.uio_iov, iovlen); 466 } 467 #endif 468 469 len = auio.uio_resid; 470 if (syslogf) { 471 error = sosend(syslogf->f_data, NULL, &auio, NULL, NULL, 0); 472 if (error == 0) 473 len -= auio.uio_resid; 474 } else if (constty || cn_devvp) { 475 error = cnwrite(0, &auio, 0); 476 if (error == 0) 477 len -= auio.uio_resid; 478 aiov.iov_base = "\r\n"; 479 aiov.iov_len = 2; 480 auio.uio_iov = &aiov; 481 auio.uio_iovcnt = 1; 482 auio.uio_segflg = UIO_SYSSPACE; 483 auio.uio_rw = UIO_WRITE; 484 auio.uio_procp = p; 485 auio.uio_offset = 0; 486 auio.uio_resid = aiov.iov_len; 487 cnwrite(0, &auio, 0); 488 } else { 489 /* XXX console redirection breaks down... */ 490 if (sflg == UIO_USERSPACE) { 491 kbuf = malloc(len, M_TEMP, M_WAITOK); 492 error = copyin(aiov.iov_base, kbuf, len); 493 } else { 494 kbuf = aiov.iov_base; 495 error = 0; 496 } 497 if (error == 0) 498 for (i = 0; i < len; i++) { 499 if (kbuf[i] == '\0') 500 break; 501 cnputc(kbuf[i]); 502 auio.uio_resid--; 503 } 504 if (sflg == UIO_USERSPACE) 505 free(kbuf, M_TEMP, len); 506 if (error == 0) 507 len -= auio.uio_resid; 508 cnputc('\n'); 509 } 510 511 #ifdef KTRACE 512 if (ktriov != NULL) { 513 if (error == 0) 514 ktrgenio(p, -1, UIO_WRITE, ktriov, len); 515 free(ktriov, M_TEMP, iovlen); 516 } 517 #endif 518 if (syslogf) 519 FRELE(syslogf, p); 520 else 521 error = ENOTCONN; 522 return (error); 523 } 524