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*40811Smarc * @(#)subr_log.c 7.5 (Berkeley) 04/05/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" 1617093Sbloom #include "ioctl.h" 1717093Sbloom #include "msgbuf.h" 1817093Sbloom #include "file.h" 1917093Sbloom #include "errno.h" 2016723Sralph 2116723Sralph #define LOG_RDPRI (PZERO + 1) 2216723Sralph 2316723Sralph #define LOG_NBIO 0x02 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 */ 3039563Smarc struct pgrp *sc_pgrp; /* 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; 4316723Sralph logsoftc.sc_selp = 0; 4439563Smarc logsoftc.sc_pgrp = u.u_procp->p_pgrp; 4516723Sralph /* 4616723Sralph * Potential race here with putchar() but since putchar should be 4716723Sralph * called by autoconf, msg_magic should be initialized by the time 4816723Sralph * we get here. 4916723Sralph */ 5016723Sralph if (msgbuf.msg_magic != MSG_MAGIC) { 5116723Sralph register int i; 5216723Sralph 5316723Sralph msgbuf.msg_magic = MSG_MAGIC; 5416723Sralph msgbuf.msg_bufx = msgbuf.msg_bufr = 0; 5516723Sralph for (i=0; i < MSG_BSIZE; i++) 5616723Sralph msgbuf.msg_bufc[i] = 0; 5716723Sralph } 5816725Sralph return (0); 5916723Sralph } 6016723Sralph 6124524Sbloom /*ARGSUSED*/ 6216723Sralph logclose(dev, flag) 6316723Sralph dev_t dev; 6416723Sralph { 6518365Skarels log_open = 0; 6616723Sralph logsoftc.sc_state = 0; 6716723Sralph logsoftc.sc_selp = 0; 6839563Smarc logsoftc.sc_pgrp = NULL; 6916723Sralph } 7016723Sralph 7124524Sbloom /*ARGSUSED*/ 7216723Sralph logread(dev, uio) 7316723Sralph dev_t dev; 7416723Sralph struct uio *uio; 7516723Sralph { 7616723Sralph register long l; 7716723Sralph register int s; 7816723Sralph int error = 0; 7916723Sralph 8016723Sralph s = splhigh(); 8116723Sralph while (msgbuf.msg_bufr == msgbuf.msg_bufx) { 8216723Sralph if (logsoftc.sc_state & LOG_NBIO) { 8316723Sralph splx(s); 8416725Sralph return (EWOULDBLOCK); 8516723Sralph } 8616723Sralph logsoftc.sc_state |= LOG_RDWAIT; 87*40811Smarc if (error = tsleep((caddr_t)&msgbuf, LOG_RDPRI | PCATCH, 88*40811Smarc "klog", 0)) { 89*40811Smarc splx(s); 90*40811Smarc return (error); 91*40811Smarc } 9216723Sralph } 9316723Sralph splx(s); 9416723Sralph logsoftc.sc_state &= ~LOG_RDWAIT; 9516723Sralph 9616723Sralph while (uio->uio_resid > 0) { 9716723Sralph l = msgbuf.msg_bufx - msgbuf.msg_bufr; 9816723Sralph if (l < 0) 9916723Sralph l = MSG_BSIZE - msgbuf.msg_bufr; 10026356Skarels l = MIN(l, uio->uio_resid); 10126249Skarels if (l == 0) 10216725Sralph break; 10316723Sralph error = uiomove((caddr_t)&msgbuf.msg_bufc[msgbuf.msg_bufr], 10437728Smckusick (int)l, uio); 10516723Sralph if (error) 10616723Sralph break; 10726249Skarels msgbuf.msg_bufr += l; 10816723Sralph if (msgbuf.msg_bufr < 0 || msgbuf.msg_bufr >= MSG_BSIZE) 10916723Sralph msgbuf.msg_bufr = 0; 11016723Sralph } 11116725Sralph return (error); 11216723Sralph } 11316723Sralph 11424524Sbloom /*ARGSUSED*/ 11516723Sralph logselect(dev, rw) 11616723Sralph dev_t dev; 11716723Sralph int rw; 11816723Sralph { 11916723Sralph int s = splhigh(); 12016723Sralph 12116723Sralph switch (rw) { 12216723Sralph 12316723Sralph case FREAD: 12426249Skarels if (msgbuf.msg_bufr != msgbuf.msg_bufx) { 12526249Skarels splx(s); 12626249Skarels return (1); 12726249Skarels } 12816723Sralph logsoftc.sc_selp = u.u_procp; 12916723Sralph break; 13016723Sralph } 13116723Sralph splx(s); 13216725Sralph return (0); 13316723Sralph } 13416723Sralph 13516723Sralph logwakeup() 13616723Sralph { 13716723Sralph 13818365Skarels if (!log_open) 13918365Skarels return; 14016723Sralph if (logsoftc.sc_selp) { 14116723Sralph selwakeup(logsoftc.sc_selp, 0); 14216723Sralph logsoftc.sc_selp = 0; 14316723Sralph } 14416723Sralph if (logsoftc.sc_state & LOG_ASYNC) 14539563Smarc pgsignal(logsoftc.sc_pgrp, SIGIO); 14616723Sralph if (logsoftc.sc_state & LOG_RDWAIT) { 14716723Sralph wakeup((caddr_t)&msgbuf); 14816723Sralph logsoftc.sc_state &= ~LOG_RDWAIT; 14916723Sralph } 15016723Sralph } 15116723Sralph 15216723Sralph /*ARGSUSED*/ 15316723Sralph logioctl(com, data, flag) 15416723Sralph caddr_t data; 15516723Sralph { 15616723Sralph long l; 15716723Sralph int s; 15816723Sralph 15916723Sralph switch (com) { 16016723Sralph 16116723Sralph /* return number of characters immediately available */ 16216723Sralph case FIONREAD: 16316723Sralph s = splhigh(); 16416723Sralph l = msgbuf.msg_bufx - msgbuf.msg_bufr; 16516723Sralph splx(s); 16616723Sralph if (l < 0) 16716723Sralph l += MSG_BSIZE; 16816723Sralph *(off_t *)data = l; 16916723Sralph break; 17016723Sralph 17116723Sralph case FIONBIO: 17216723Sralph if (*(int *)data) 17316723Sralph logsoftc.sc_state |= LOG_NBIO; 17416723Sralph else 17516723Sralph logsoftc.sc_state &= ~LOG_NBIO; 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 18539563Smarc #ifdef notdef /* XXX remove -- a single open device doesn't need this */ 18635810Smarc case TIOCSPGRP: { 18735810Smarc logsoftc.sc_pgid = *(int *)data; 18816723Sralph break; 18935810Smarc } 19039563Smarc #endif 19116723Sralph 19216723Sralph case TIOCGPGRP: 19339563Smarc *(int *)data = logsoftc.sc_pgrp->pg_id; 19416723Sralph break; 19516723Sralph 19616723Sralph default: 19716725Sralph return (-1); 19816723Sralph } 19916725Sralph return (0); 20016723Sralph } 201