123379Smckusick /* 229099Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 345732Smckusick * All rights reserved. 423379Smckusick * 545732Smckusick * %sccs.include.redist.c% 645732Smckusick * 7*55058Spendry * @(#)subr_log.c 7.16 (Berkeley) 07/12/92 823379Smckusick */ 916723Sralph 1016723Sralph /* 1116723Sralph * Error log buffer for kernel printf's. 1216723Sralph */ 1316723Sralph 1417093Sbloom #include "param.h" 1517093Sbloom #include "proc.h" 1643691Smckusick #include "vnode.h" 1717093Sbloom #include "ioctl.h" 1817093Sbloom #include "msgbuf.h" 1917093Sbloom #include "file.h" 2016723Sralph 2116723Sralph #define LOG_RDPRI (PZERO + 1) 2216723Sralph 2316723Sralph #define LOG_ASYNC 0x04 2416723Sralph #define LOG_RDWAIT 0x08 2516723Sralph 2616723Sralph struct logsoftc { 2716723Sralph int sc_state; /* see above for possibilities */ 2852521Smckusick struct selinfo sc_selp; /* process waiting on select call */ 2943661Skarels int sc_pgid; /* process/group for async I/O */ 3016723Sralph } logsoftc; 3116723Sralph 3218365Skarels int log_open; /* also used in log() */ 3318365Skarels 3424524Sbloom /*ARGSUSED*/ 3547540Skarels logopen(dev, flags, mode, p) 3616723Sralph dev_t dev; 3747540Skarels int flags, mode; 3847540Skarels struct proc *p; 3916723Sralph { 4045732Smckusick register struct msgbuf *mbp = msgbufp; 4116723Sralph 4218365Skarels if (log_open) 4316725Sralph return (EBUSY); 4418365Skarels log_open = 1; 4547540Skarels logsoftc.sc_pgid = p->p_pid; /* signal process only */ 4616723Sralph /* 4716723Sralph * Potential race here with putchar() but since putchar should be 4816723Sralph * called by autoconf, msg_magic should be initialized by the time 4916723Sralph * we get here. 5016723Sralph */ 5145732Smckusick if (mbp->msg_magic != MSG_MAGIC) { 5216723Sralph register int i; 5316723Sralph 5445732Smckusick mbp->msg_magic = MSG_MAGIC; 5545732Smckusick mbp->msg_bufx = mbp->msg_bufr = 0; 5616723Sralph for (i=0; i < MSG_BSIZE; i++) 5745732Smckusick mbp->msg_bufc[i] = 0; 5816723Sralph } 5916725Sralph return (0); 6016723Sralph } 6116723Sralph 6224524Sbloom /*ARGSUSED*/ 6352419Storek logclose(dev, flag, mode, p) 6416723Sralph dev_t dev; 6552419Storek int flag, mode; 6652419Storek struct proc *p; 6716723Sralph { 6852521Smckusick 6918365Skarels log_open = 0; 7016723Sralph logsoftc.sc_state = 0; 7153647Sbostic return (0); 7216723Sralph } 7316723Sralph 7424524Sbloom /*ARGSUSED*/ 7543661Skarels logread(dev, uio, flag) 7616723Sralph dev_t dev; 7716723Sralph struct uio *uio; 7843661Skarels int flag; 7916723Sralph { 8045732Smckusick register struct msgbuf *mbp = msgbufp; 8116723Sralph register long l; 8216723Sralph register int s; 8316723Sralph int error = 0; 8416723Sralph 8516723Sralph s = splhigh(); 8645732Smckusick while (mbp->msg_bufr == mbp->msg_bufx) { 8743661Skarels if (flag & IO_NDELAY) { 8816723Sralph splx(s); 8916725Sralph return (EWOULDBLOCK); 9016723Sralph } 9116723Sralph logsoftc.sc_state |= LOG_RDWAIT; 9245732Smckusick if (error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 9340811Smarc "klog", 0)) { 9440811Smarc splx(s); 9540811Smarc return (error); 9640811Smarc } 9716723Sralph } 9816723Sralph splx(s); 9916723Sralph logsoftc.sc_state &= ~LOG_RDWAIT; 10016723Sralph 10116723Sralph while (uio->uio_resid > 0) { 10245732Smckusick l = mbp->msg_bufx - mbp->msg_bufr; 10316723Sralph if (l < 0) 10445732Smckusick l = MSG_BSIZE - mbp->msg_bufr; 105*55058Spendry l = min(l, uio->uio_resid); 10626249Skarels if (l == 0) 10716725Sralph break; 10845732Smckusick error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], 10937728Smckusick (int)l, uio); 11016723Sralph if (error) 11116723Sralph break; 11245732Smckusick mbp->msg_bufr += l; 11345732Smckusick if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE) 11445732Smckusick mbp->msg_bufr = 0; 11516723Sralph } 11616725Sralph return (error); 11716723Sralph } 11816723Sralph 11924524Sbloom /*ARGSUSED*/ 12047540Skarels logselect(dev, rw, p) 12116723Sralph dev_t dev; 12216723Sralph int rw; 12347540Skarels struct proc *p; 12416723Sralph { 12516723Sralph int s = splhigh(); 12616723Sralph 12716723Sralph switch (rw) { 12816723Sralph 12916723Sralph case FREAD: 13045732Smckusick if (msgbufp->msg_bufr != msgbufp->msg_bufx) { 13126249Skarels splx(s); 13226249Skarels return (1); 13326249Skarels } 13452521Smckusick selrecord(p, &logsoftc.sc_selp); 13516723Sralph break; 13616723Sralph } 13716723Sralph splx(s); 13816725Sralph return (0); 13916723Sralph } 14016723Sralph 14116723Sralph logwakeup() 14216723Sralph { 14343661Skarels struct proc *p; 14416723Sralph 14518365Skarels if (!log_open) 14618365Skarels return; 14752521Smckusick selwakeup(&logsoftc.sc_selp); 14843661Skarels if (logsoftc.sc_state & LOG_ASYNC) { 14943661Skarels if (logsoftc.sc_pgid < 0) 15044986Smarc gsignal(-logsoftc.sc_pgid, SIGIO); 15143661Skarels else if (p = pfind(logsoftc.sc_pgid)) 15243661Skarels psignal(p, SIGIO); 15343661Skarels } 15416723Sralph if (logsoftc.sc_state & LOG_RDWAIT) { 15545732Smckusick wakeup((caddr_t)msgbufp); 15616723Sralph logsoftc.sc_state &= ~LOG_RDWAIT; 15716723Sralph } 15816723Sralph } 15916723Sralph 16016723Sralph /*ARGSUSED*/ 16152419Storek logioctl(dev, com, data, flag, p) 16252419Storek dev_t dev; 16352419Storek int com; 16416723Sralph caddr_t data; 16552419Storek int flag; 16652419Storek struct proc *p; 16716723Sralph { 16816723Sralph long l; 16916723Sralph int s; 17016723Sralph 17116723Sralph switch (com) { 17216723Sralph 17316723Sralph /* return number of characters immediately available */ 17416723Sralph case FIONREAD: 17516723Sralph s = splhigh(); 17645732Smckusick l = msgbufp->msg_bufx - msgbufp->msg_bufr; 17716723Sralph splx(s); 17816723Sralph if (l < 0) 17916723Sralph l += MSG_BSIZE; 18054773Storek *(int *)data = l; 18116723Sralph break; 18216723Sralph 18316723Sralph case FIONBIO: 18416723Sralph break; 18516723Sralph 18616723Sralph case FIOASYNC: 18716723Sralph if (*(int *)data) 18816723Sralph logsoftc.sc_state |= LOG_ASYNC; 18916723Sralph else 19016723Sralph logsoftc.sc_state &= ~LOG_ASYNC; 19116723Sralph break; 19216723Sralph 19343661Skarels case TIOCSPGRP: 19435810Smarc logsoftc.sc_pgid = *(int *)data; 19516723Sralph break; 19616723Sralph 19716723Sralph case TIOCGPGRP: 19843661Skarels *(int *)data = logsoftc.sc_pgid; 19916723Sralph break; 20016723Sralph 20116723Sralph default: 20216725Sralph return (-1); 20316723Sralph } 20416725Sralph return (0); 20516723Sralph } 206