1 /* $OpenBSD: subr_log.c,v 1.30 2015/05/06 08:52:17 mpi 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/poll.h> 49 #include <sys/malloc.h> 50 #include <sys/filedesc.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 54 #ifdef KTRACE 55 #include <sys/ktrace.h> 56 #endif 57 58 #include <sys/mount.h> 59 #include <sys/syscallargs.h> 60 61 #define LOG_RDPRI (PZERO + 1) 62 63 #define LOG_ASYNC 0x04 64 #define LOG_RDWAIT 0x08 65 66 struct logsoftc { 67 int sc_state; /* see above for possibilities */ 68 struct selinfo sc_selp; /* process waiting on select call */ 69 int sc_pgid; /* process/group for async I/O */ 70 uid_t sc_siguid; /* uid for process that set sc_pgid */ 71 uid_t sc_sigeuid; /* euid for process that set sc_pgid */ 72 } logsoftc; 73 74 int log_open; /* also used in log() */ 75 int msgbufmapped; /* is the message buffer mapped */ 76 struct msgbuf *msgbufp; /* the mapped buffer, itself. */ 77 struct msgbuf *consbufp; /* console message buffer. */ 78 struct file *syslogf; 79 80 void filt_logrdetach(struct knote *kn); 81 int filt_logread(struct knote *kn, long hint); 82 83 struct filterops logread_filtops = 84 { 1, NULL, filt_logrdetach, filt_logread}; 85 86 void 87 initmsgbuf(caddr_t buf, size_t bufsize) 88 { 89 struct msgbuf *mbp; 90 long new_bufs; 91 92 /* Sanity-check the given size. */ 93 if (bufsize < sizeof(struct msgbuf)) 94 return; 95 96 mbp = msgbufp = (struct msgbuf *)buf; 97 98 new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc); 99 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) || 100 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) || 101 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) { 102 /* 103 * If the buffer magic number is wrong, has changed 104 * size (which shouldn't happen often), or is 105 * internally inconsistent, initialize it. 106 */ 107 108 memset(buf, 0, bufsize); 109 mbp->msg_magic = MSG_MAGIC; 110 mbp->msg_bufs = new_bufs; 111 } 112 113 /* Always start new buffer data on a new line. */ 114 if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n') 115 msgbuf_putchar(msgbufp, '\n'); 116 117 /* mark it as ready for use. */ 118 msgbufmapped = 1; 119 } 120 121 void 122 initconsbuf(void) 123 { 124 long new_bufs; 125 126 /* Set up a buffer to collect /dev/console output */ 127 consbufp = malloc(CONSBUFSIZE, M_TEMP, M_NOWAIT|M_ZERO); 128 if (consbufp) { 129 new_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc); 130 consbufp->msg_magic = MSG_MAGIC; 131 consbufp->msg_bufs = new_bufs; 132 } 133 } 134 135 void 136 msgbuf_putchar(struct msgbuf *mbp, const char c) 137 { 138 if (mbp->msg_magic != MSG_MAGIC) 139 /* Nothing we can do */ 140 return; 141 142 mbp->msg_bufc[mbp->msg_bufx++] = c; 143 mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs); 144 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) 145 mbp->msg_bufx = 0; 146 /* If the buffer is full, keep the most recent data. */ 147 if (mbp->msg_bufr == mbp->msg_bufx) { 148 if (++mbp->msg_bufr >= mbp->msg_bufs) 149 mbp->msg_bufr = 0; 150 } 151 } 152 153 /*ARGSUSED*/ 154 int 155 logopen(dev_t dev, int flags, int mode, struct proc *p) 156 { 157 if (log_open) 158 return (EBUSY); 159 log_open = 1; 160 return (0); 161 } 162 163 /*ARGSUSED*/ 164 int 165 logclose(dev_t dev, int flag, int mode, struct proc *p) 166 { 167 168 if (syslogf) 169 FRELE(syslogf, p); 170 syslogf = NULL; 171 log_open = 0; 172 logsoftc.sc_state = 0; 173 return (0); 174 } 175 176 /*ARGSUSED*/ 177 int 178 logread(dev_t dev, struct uio *uio, int flag) 179 { 180 struct msgbuf *mbp = msgbufp; 181 long l; 182 int s; 183 int error = 0; 184 185 s = splhigh(); 186 while (mbp->msg_bufr == mbp->msg_bufx) { 187 if (flag & IO_NDELAY) { 188 splx(s); 189 return (EWOULDBLOCK); 190 } 191 logsoftc.sc_state |= LOG_RDWAIT; 192 error = tsleep(mbp, LOG_RDPRI | PCATCH, 193 "klog", 0); 194 if (error) { 195 splx(s); 196 return (error); 197 } 198 } 199 splx(s); 200 logsoftc.sc_state &= ~LOG_RDWAIT; 201 202 while (uio->uio_resid > 0) { 203 l = mbp->msg_bufx - mbp->msg_bufr; 204 if (l < 0) 205 l = mbp->msg_bufs - mbp->msg_bufr; 206 l = min(l, uio->uio_resid); 207 if (l == 0) 208 break; 209 error = uiomovei(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio); 210 if (error) 211 break; 212 mbp->msg_bufr += l; 213 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs) 214 mbp->msg_bufr = 0; 215 } 216 return (error); 217 } 218 219 /*ARGSUSED*/ 220 int 221 logpoll(dev_t dev, int events, struct proc *p) 222 { 223 int revents = 0; 224 int s = splhigh(); 225 226 if (events & (POLLIN | POLLRDNORM)) { 227 if (msgbufp->msg_bufr != msgbufp->msg_bufx) 228 revents |= events & (POLLIN | POLLRDNORM); 229 else 230 selrecord(p, &logsoftc.sc_selp); 231 } 232 splx(s); 233 return (revents); 234 } 235 236 int 237 logkqfilter(dev_t dev, struct knote *kn) 238 { 239 struct klist *klist; 240 int s; 241 242 switch (kn->kn_filter) { 243 case EVFILT_READ: 244 klist = &logsoftc.sc_selp.si_note; 245 kn->kn_fop = &logread_filtops; 246 break; 247 default: 248 return (EINVAL); 249 } 250 251 kn->kn_hook = (void *)msgbufp; 252 253 s = splhigh(); 254 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 255 splx(s); 256 257 return (0); 258 } 259 260 void 261 filt_logrdetach(struct knote *kn) 262 { 263 int s = splhigh(); 264 265 SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext); 266 splx(s); 267 } 268 269 int 270 filt_logread(struct knote *kn, long hint) 271 { 272 struct msgbuf *p = (struct msgbuf *)kn->kn_hook; 273 274 kn->kn_data = (int)(p->msg_bufx - p->msg_bufr); 275 276 return (p->msg_bufx != p->msg_bufr); 277 } 278 279 void 280 logwakeup(void) 281 { 282 if (!log_open) 283 return; 284 selwakeup(&logsoftc.sc_selp); 285 if (logsoftc.sc_state & LOG_ASYNC) 286 csignal(logsoftc.sc_pgid, SIGIO, 287 logsoftc.sc_siguid, logsoftc.sc_sigeuid); 288 if (logsoftc.sc_state & LOG_RDWAIT) { 289 wakeup(msgbufp); 290 logsoftc.sc_state &= ~LOG_RDWAIT; 291 } 292 } 293 294 /*ARGSUSED*/ 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 #ifdef KTRACE 358 struct iovec *ktriov = NULL; 359 int iovlen; 360 #endif 361 struct iovec aiov; 362 struct uio auio; 363 struct file *f; 364 size_t len; 365 int error; 366 367 if (syslogf == NULL) 368 return (ENOTCONN); 369 f = syslogf; 370 FREF(f); 371 372 aiov.iov_base = (char *)SCARG(uap, buf); 373 aiov.iov_len = SCARG(uap, nbyte); 374 auio.uio_iov = &aiov; 375 auio.uio_iovcnt = 1; 376 auio.uio_segflg = UIO_USERSPACE; 377 auio.uio_rw = UIO_WRITE; 378 auio.uio_procp = p; 379 auio.uio_offset = 0; 380 auio.uio_resid = aiov.iov_len; 381 #ifdef KTRACE 382 if (KTRPOINT(p, KTR_GENIO)) { 383 ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec), 384 M_TEMP, M_WAITOK); 385 iovlen = auio.uio_iovcnt * sizeof (struct iovec); 386 387 memcpy(ktriov, auio.uio_iov, iovlen); 388 } 389 #endif 390 391 len = auio.uio_resid; 392 error = sosend(f->f_data, NULL, &auio, NULL, NULL, 0); 393 if (error == 0) 394 len -= auio.uio_resid; 395 396 #ifdef KTRACE 397 if (ktriov != NULL) { 398 if (error == 0) 399 ktrgenio(p, -1, UIO_WRITE, ktriov, len); 400 free(ktriov, M_TEMP, iovlen); 401 } 402 #endif 403 FRELE(f, p); 404 return error; 405 } 406