1 /* $OpenBSD: subr_log.c,v 1.36 2016/01/07 12:27:07 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 if (mbp->msg_magic != MSG_MAGIC) 144 /* Nothing we can do */ 145 return; 146 147 mbp->msg_bufc[mbp->msg_bufx++] = c; 148 mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs); 149 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) 150 mbp->msg_bufx = 0; 151 /* If the buffer is full, keep the most recent data. */ 152 if (mbp->msg_bufr == mbp->msg_bufx) { 153 if (++mbp->msg_bufr >= mbp->msg_bufs) 154 mbp->msg_bufr = 0; 155 } 156 } 157 158 int 159 logopen(dev_t dev, int flags, int mode, struct proc *p) 160 { 161 if (log_open) 162 return (EBUSY); 163 log_open = 1; 164 return (0); 165 } 166 167 int 168 logclose(dev_t dev, int flag, int mode, struct proc *p) 169 { 170 171 if (syslogf) 172 FRELE(syslogf, p); 173 syslogf = NULL; 174 log_open = 0; 175 logsoftc.sc_state = 0; 176 return (0); 177 } 178 179 int 180 logread(dev_t dev, struct uio *uio, int flag) 181 { 182 struct msgbuf *mbp = msgbufp; 183 long l; 184 int s; 185 int error = 0; 186 187 s = splhigh(); 188 while (mbp->msg_bufr == mbp->msg_bufx) { 189 if (flag & IO_NDELAY) { 190 splx(s); 191 return (EWOULDBLOCK); 192 } 193 logsoftc.sc_state |= LOG_RDWAIT; 194 error = tsleep(mbp, LOG_RDPRI | PCATCH, 195 "klog", 0); 196 if (error) { 197 splx(s); 198 return (error); 199 } 200 } 201 splx(s); 202 logsoftc.sc_state &= ~LOG_RDWAIT; 203 204 while (uio->uio_resid > 0) { 205 l = mbp->msg_bufx - mbp->msg_bufr; 206 if (l < 0) 207 l = mbp->msg_bufs - mbp->msg_bufr; 208 l = min(l, uio->uio_resid); 209 if (l == 0) 210 break; 211 error = uiomovei(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio); 212 if (error) 213 break; 214 mbp->msg_bufr += l; 215 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 216 mbp->msg_bufr = 0; 217 } 218 return (error); 219 } 220 221 int 222 logpoll(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 237 int 238 logkqfilter(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 261 void 262 filt_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 270 int 271 filt_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 280 void 281 logwakeup(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 int 296 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) 297 { 298 struct file *fp; 299 long l; 300 int error, s; 301 302 switch (com) { 303 304 /* return number of characters immediately available */ 305 case FIONREAD: 306 s = splhigh(); 307 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 308 splx(s); 309 if (l < 0) 310 l += msgbufp->msg_bufs; 311 *(int *)data = l; 312 break; 313 314 case FIONBIO: 315 break; 316 317 case FIOASYNC: 318 if (*(int *)data) 319 logsoftc.sc_state |= LOG_ASYNC; 320 else 321 logsoftc.sc_state &= ~LOG_ASYNC; 322 break; 323 324 case TIOCSPGRP: 325 logsoftc.sc_pgid = *(int *)data; 326 logsoftc.sc_siguid = p->p_ucred->cr_ruid; 327 logsoftc.sc_sigeuid = p->p_ucred->cr_uid; 328 break; 329 330 case TIOCGPGRP: 331 *(int *)data = logsoftc.sc_pgid; 332 break; 333 334 case LIOCSFD: 335 if ((error = suser(p, 0)) != 0) 336 return (error); 337 if ((error = getsock(p, *(int *)data, &fp)) != 0) 338 return (error); 339 if (syslogf) 340 FRELE(syslogf, p); 341 syslogf = fp; 342 break; 343 344 default: 345 return (ENOTTY); 346 } 347 return (0); 348 } 349 350 int 351 sys_sendsyslog(struct proc *p, void *v, register_t *retval) 352 { 353 struct sys_sendsyslog_args /* { 354 syscallarg(const void *) buf; 355 syscallarg(size_t) nbyte; 356 } */ *uap = v; 357 struct sys_sendsyslog2_args oap; 358 359 SCARG(&oap, buf) = SCARG(uap, buf); 360 SCARG(&oap, nbyte) = SCARG(uap, nbyte); 361 SCARG(&oap, flags) = 0; 362 return sys_sendsyslog2(p, &oap, retval); 363 } 364 365 int 366 sys_sendsyslog2(struct proc *p, void *v, register_t *retval) 367 { 368 struct sys_sendsyslog2_args /* { 369 syscallarg(const void *) buf; 370 syscallarg(size_t) nbyte; 371 syscallarg(int) flags; 372 } */ *uap = v; 373 int error; 374 #ifndef SMALL_KERNEL 375 static int dropped_count, orig_error; 376 int len; 377 char buf[64]; 378 379 if (dropped_count) { 380 len = snprintf(buf, sizeof(buf), 381 "<%d>sendsyslog: dropped %d message%s, error %d", 382 LOG_KERN|LOG_WARNING, dropped_count, 383 dropped_count == 1 ? "" : "s", orig_error); 384 error = dosendsyslog(p, buf, MIN((size_t)len, sizeof(buf) - 1), 385 0, UIO_SYSSPACE); 386 if (error == 0) 387 dropped_count = 0; 388 } 389 #endif 390 error = dosendsyslog(p, SCARG(uap, buf), SCARG(uap, nbyte), 391 SCARG(uap, flags), UIO_USERSPACE); 392 #ifndef SMALL_KERNEL 393 if (error) { 394 dropped_count++; 395 orig_error = error; 396 } 397 #endif 398 return (error); 399 } 400 401 int 402 dosendsyslog(struct proc *p, const char *buf, size_t nbyte, int flags, 403 enum uio_seg sflg) 404 { 405 #ifdef KTRACE 406 struct iovec *ktriov = NULL; 407 int iovlen; 408 #endif 409 char pri[6]; 410 struct iovec aiov; 411 struct uio auio; 412 size_t i, len; 413 int error; 414 415 if (syslogf) 416 FREF(syslogf); 417 else if ((flags & LOG_CONS) == 0) 418 return (ENOTCONN); 419 else { 420 /* 421 * Strip off syslog priority when logging to console. 422 * LOG_PRIMASK | LOG_FACMASK is 0x03ff, so at most 4 423 * decimal digits may appear in priority as <1023>. 424 */ 425 len = MIN(nbyte, sizeof(pri)); 426 if (sflg == UIO_USERSPACE) { 427 if ((error = copyin(buf, pri, len))) 428 return (error); 429 } else 430 memcpy(pri, buf, len); 431 if (0 < len && pri[0] == '<') { 432 for (i = 1; i < len; i++) { 433 if (pri[i] < '0' || pri[i] > '9') 434 break; 435 } 436 if (i < len && pri[i] == '>') { 437 i++; 438 /* There must be at least one digit <0>. */ 439 if (i >= 3) { 440 buf += i; 441 nbyte -= i; 442 } 443 } 444 } 445 } 446 447 aiov.iov_base = (char *)buf; 448 aiov.iov_len = nbyte; 449 auio.uio_iov = &aiov; 450 auio.uio_iovcnt = 1; 451 auio.uio_segflg = sflg; 452 auio.uio_rw = UIO_WRITE; 453 auio.uio_procp = p; 454 auio.uio_offset = 0; 455 auio.uio_resid = aiov.iov_len; 456 #ifdef KTRACE 457 if (KTRPOINT(p, KTR_GENIO)) { 458 ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec), 459 M_TEMP, M_WAITOK); 460 iovlen = auio.uio_iovcnt * sizeof (struct iovec); 461 462 memcpy(ktriov, auio.uio_iov, iovlen); 463 } 464 #endif 465 466 len = auio.uio_resid; 467 if (syslogf) 468 error = sosend(syslogf->f_data, NULL, &auio, NULL, NULL, 0); 469 else 470 error = cnwrite(0, &auio, 0); 471 if (error == 0) 472 len -= auio.uio_resid; 473 if (syslogf == NULL) { 474 aiov.iov_base = "\r\n"; 475 aiov.iov_len = 2; 476 auio.uio_iov = &aiov; 477 auio.uio_iovcnt = 1; 478 auio.uio_segflg = UIO_SYSSPACE; 479 auio.uio_rw = UIO_WRITE; 480 auio.uio_procp = p; 481 auio.uio_offset = 0; 482 auio.uio_resid = aiov.iov_len; 483 cnwrite(0, &auio, 0); 484 } 485 486 #ifdef KTRACE 487 if (ktriov != NULL) { 488 if (error == 0) 489 ktrgenio(p, -1, UIO_WRITE, ktriov, len); 490 free(ktriov, M_TEMP, iovlen); 491 } 492 #endif 493 if (syslogf) 494 FRELE(syslogf, p); 495 else 496 error = ENOTCONN; 497 return (error); 498 } 499