123379Smckusick /* 229099Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323379Smckusick * All rights reserved. The Berkeley software License Agreement 423379Smckusick * specifies the terms and conditions for redistribution. 523379Smckusick * 6*44986Smarc * @(#)subr_log.c 7.9 (Berkeley) 07/26/90 723379Smckusick */ 816723Sralph 916723Sralph /* 1016723Sralph * Error log buffer for kernel printf's. 1116723Sralph */ 1216723Sralph 1317093Sbloom #include "param.h" 1417093Sbloom #include "user.h" 1517093Sbloom #include "proc.h" 1643691Smckusick #include "vnode.h" 1717093Sbloom #include "ioctl.h" 1817093Sbloom #include "msgbuf.h" 1917093Sbloom #include "file.h" 2017093Sbloom #include "errno.h" 2116723Sralph 2216723Sralph #define LOG_RDPRI (PZERO + 1) 2316723Sralph 2416723Sralph #define LOG_ASYNC 0x04 2516723Sralph #define LOG_RDWAIT 0x08 2616723Sralph 2716723Sralph struct logsoftc { 2816723Sralph int sc_state; /* see above for possibilities */ 2916723Sralph struct proc *sc_selp; /* process waiting on select call */ 3043661Skarels int sc_pgid; /* process/group for async I/O */ 3116723Sralph } logsoftc; 3216723Sralph 3318365Skarels int log_open; /* also used in log() */ 3418365Skarels 3524524Sbloom /*ARGSUSED*/ 3616723Sralph logopen(dev) 3716723Sralph dev_t dev; 3816723Sralph { 3916723Sralph 4018365Skarels if (log_open) 4116725Sralph return (EBUSY); 4218365Skarels log_open = 1; 4343661Skarels logsoftc.sc_pgid = u.u_procp->p_pid; /* signal process only */ 4416723Sralph /* 4516723Sralph * Potential race here with putchar() but since putchar should be 4616723Sralph * called by autoconf, msg_magic should be initialized by the time 4716723Sralph * we get here. 4816723Sralph */ 4916723Sralph if (msgbuf.msg_magic != MSG_MAGIC) { 5016723Sralph register int i; 5116723Sralph 5216723Sralph msgbuf.msg_magic = MSG_MAGIC; 5316723Sralph msgbuf.msg_bufx = msgbuf.msg_bufr = 0; 5416723Sralph for (i=0; i < MSG_BSIZE; i++) 5516723Sralph msgbuf.msg_bufc[i] = 0; 5616723Sralph } 5716725Sralph return (0); 5816723Sralph } 5916723Sralph 6024524Sbloom /*ARGSUSED*/ 6116723Sralph logclose(dev, flag) 6216723Sralph dev_t dev; 6316723Sralph { 6418365Skarels log_open = 0; 6516723Sralph logsoftc.sc_state = 0; 6616723Sralph logsoftc.sc_selp = 0; 6716723Sralph } 6816723Sralph 6924524Sbloom /*ARGSUSED*/ 7043661Skarels logread(dev, uio, flag) 7116723Sralph dev_t dev; 7216723Sralph struct uio *uio; 7343661Skarels int flag; 7416723Sralph { 7516723Sralph register long l; 7616723Sralph register int s; 7716723Sralph int error = 0; 7816723Sralph 7916723Sralph s = splhigh(); 8016723Sralph while (msgbuf.msg_bufr == msgbuf.msg_bufx) { 8143661Skarels if (flag & IO_NDELAY) { 8216723Sralph splx(s); 8316725Sralph return (EWOULDBLOCK); 8416723Sralph } 8516723Sralph logsoftc.sc_state |= LOG_RDWAIT; 8640811Smarc if (error = tsleep((caddr_t)&msgbuf, LOG_RDPRI | PCATCH, 8740811Smarc "klog", 0)) { 8840811Smarc splx(s); 8940811Smarc return (error); 9040811Smarc } 9116723Sralph } 9216723Sralph splx(s); 9316723Sralph logsoftc.sc_state &= ~LOG_RDWAIT; 9416723Sralph 9516723Sralph while (uio->uio_resid > 0) { 9616723Sralph l = msgbuf.msg_bufx - msgbuf.msg_bufr; 9716723Sralph if (l < 0) 9816723Sralph l = MSG_BSIZE - msgbuf.msg_bufr; 9926356Skarels l = MIN(l, uio->uio_resid); 10026249Skarels if (l == 0) 10116725Sralph break; 10216723Sralph error = uiomove((caddr_t)&msgbuf.msg_bufc[msgbuf.msg_bufr], 10337728Smckusick (int)l, uio); 10416723Sralph if (error) 10516723Sralph break; 10626249Skarels msgbuf.msg_bufr += l; 10716723Sralph if (msgbuf.msg_bufr < 0 || msgbuf.msg_bufr >= MSG_BSIZE) 10816723Sralph msgbuf.msg_bufr = 0; 10916723Sralph } 11016725Sralph return (error); 11116723Sralph } 11216723Sralph 11324524Sbloom /*ARGSUSED*/ 11416723Sralph logselect(dev, rw) 11516723Sralph dev_t dev; 11616723Sralph int rw; 11716723Sralph { 11816723Sralph int s = splhigh(); 11916723Sralph 12016723Sralph switch (rw) { 12116723Sralph 12216723Sralph case FREAD: 12326249Skarels if (msgbuf.msg_bufr != msgbuf.msg_bufx) { 12426249Skarels splx(s); 12526249Skarels return (1); 12626249Skarels } 12716723Sralph logsoftc.sc_selp = u.u_procp; 12816723Sralph break; 12916723Sralph } 13016723Sralph splx(s); 13116725Sralph return (0); 13216723Sralph } 13316723Sralph 13416723Sralph logwakeup() 13516723Sralph { 13643661Skarels struct proc *p; 13716723Sralph 13818365Skarels if (!log_open) 13918365Skarels return; 14016723Sralph if (logsoftc.sc_selp) { 14116723Sralph selwakeup(logsoftc.sc_selp, 0); 14216723Sralph logsoftc.sc_selp = 0; 14316723Sralph } 14443661Skarels if (logsoftc.sc_state & LOG_ASYNC) { 14543661Skarels if (logsoftc.sc_pgid < 0) 146*44986Smarc gsignal(-logsoftc.sc_pgid, SIGIO); 14743661Skarels else if (p = pfind(logsoftc.sc_pgid)) 14843661Skarels psignal(p, SIGIO); 14943661Skarels } 15016723Sralph if (logsoftc.sc_state & LOG_RDWAIT) { 15116723Sralph wakeup((caddr_t)&msgbuf); 15216723Sralph logsoftc.sc_state &= ~LOG_RDWAIT; 15316723Sralph } 15416723Sralph } 15516723Sralph 15616723Sralph /*ARGSUSED*/ 15743661Skarels logioctl(dev, com, data, flag) 15816723Sralph caddr_t data; 15916723Sralph { 16016723Sralph long l; 16116723Sralph int s; 16216723Sralph 16316723Sralph switch (com) { 16416723Sralph 16516723Sralph /* return number of characters immediately available */ 16616723Sralph case FIONREAD: 16716723Sralph s = splhigh(); 16816723Sralph l = msgbuf.msg_bufx - msgbuf.msg_bufr; 16916723Sralph splx(s); 17016723Sralph if (l < 0) 17116723Sralph l += MSG_BSIZE; 17216723Sralph *(off_t *)data = l; 17316723Sralph break; 17416723Sralph 17516723Sralph case FIONBIO: 17616723Sralph break; 17716723Sralph 17816723Sralph case FIOASYNC: 17916723Sralph if (*(int *)data) 18016723Sralph logsoftc.sc_state |= LOG_ASYNC; 18116723Sralph else 18216723Sralph logsoftc.sc_state &= ~LOG_ASYNC; 18316723Sralph break; 18416723Sralph 18543661Skarels case TIOCSPGRP: 18635810Smarc logsoftc.sc_pgid = *(int *)data; 18716723Sralph break; 18816723Sralph 18916723Sralph case TIOCGPGRP: 19043661Skarels *(int *)data = logsoftc.sc_pgid; 19116723Sralph break; 19216723Sralph 19316723Sralph default: 19416725Sralph return (-1); 19516723Sralph } 19616725Sralph return (0); 19716723Sralph } 198