123379Smckusick /*
263176Sbostic * Copyright (c) 1982, 1986, 1993
363176Sbostic * The Regents of the University of California. All rights reserved.
423379Smckusick *
545732Smckusick * %sccs.include.redist.c%
645732Smckusick *
7*68314Scgd * @(#)subr_log.c 8.3 (Berkeley) 02/14/95
823379Smckusick */
916723Sralph
1016723Sralph /*
1116723Sralph * Error log buffer for kernel printf's.
1216723Sralph */
1316723Sralph
1456517Sbostic #include <sys/param.h>
1556517Sbostic #include <sys/systm.h>
1656517Sbostic #include <sys/proc.h>
1756517Sbostic #include <sys/vnode.h>
1856517Sbostic #include <sys/ioctl.h>
1956517Sbostic #include <sys/msgbuf.h>
2056517Sbostic #include <sys/file.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 */
2952521Smckusick struct selinfo 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*/
36*68314Scgd int
logopen(dev,flags,mode,p)3747540Skarels logopen(dev, flags, mode, p)
3816723Sralph dev_t dev;
3947540Skarels int flags, mode;
4047540Skarels struct proc *p;
4116723Sralph {
4245732Smckusick register struct msgbuf *mbp = msgbufp;
4316723Sralph
4418365Skarels if (log_open)
4516725Sralph return (EBUSY);
4618365Skarels log_open = 1;
4747540Skarels logsoftc.sc_pgid = p->p_pid; /* signal process only */
4816723Sralph /*
4916723Sralph * Potential race here with putchar() but since putchar should be
5016723Sralph * called by autoconf, msg_magic should be initialized by the time
5116723Sralph * we get here.
5216723Sralph */
5345732Smckusick if (mbp->msg_magic != MSG_MAGIC) {
5416723Sralph register int i;
5516723Sralph
5645732Smckusick mbp->msg_magic = MSG_MAGIC;
5745732Smckusick mbp->msg_bufx = mbp->msg_bufr = 0;
5816723Sralph for (i=0; i < MSG_BSIZE; i++)
5945732Smckusick mbp->msg_bufc[i] = 0;
6016723Sralph }
6116725Sralph return (0);
6216723Sralph }
6316723Sralph
6424524Sbloom /*ARGSUSED*/
65*68314Scgd int
logclose(dev,flag,mode,p)6652419Storek logclose(dev, flag, mode, p)
6716723Sralph dev_t dev;
6852419Storek int flag, mode;
6952419Storek struct proc *p;
7016723Sralph {
7152521Smckusick
7218365Skarels log_open = 0;
7316723Sralph logsoftc.sc_state = 0;
7453647Sbostic return (0);
7516723Sralph }
7616723Sralph
7724524Sbloom /*ARGSUSED*/
78*68314Scgd int
logread(dev,uio,flag)7943661Skarels logread(dev, uio, flag)
8016723Sralph dev_t dev;
8116723Sralph struct uio *uio;
8243661Skarels int flag;
8316723Sralph {
8445732Smckusick register struct msgbuf *mbp = msgbufp;
8516723Sralph register long l;
8616723Sralph register int s;
8716723Sralph int error = 0;
8816723Sralph
8916723Sralph s = splhigh();
9045732Smckusick while (mbp->msg_bufr == mbp->msg_bufx) {
9143661Skarels if (flag & IO_NDELAY) {
9216723Sralph splx(s);
9316725Sralph return (EWOULDBLOCK);
9416723Sralph }
9516723Sralph logsoftc.sc_state |= LOG_RDWAIT;
9645732Smckusick if (error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
9740811Smarc "klog", 0)) {
9840811Smarc splx(s);
9940811Smarc return (error);
10040811Smarc }
10116723Sralph }
10216723Sralph splx(s);
10316723Sralph logsoftc.sc_state &= ~LOG_RDWAIT;
10416723Sralph
10516723Sralph while (uio->uio_resid > 0) {
10645732Smckusick l = mbp->msg_bufx - mbp->msg_bufr;
10716723Sralph if (l < 0)
10845732Smckusick l = MSG_BSIZE - mbp->msg_bufr;
10955058Spendry l = min(l, uio->uio_resid);
11026249Skarels if (l == 0)
11116725Sralph break;
11245732Smckusick error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
11337728Smckusick (int)l, uio);
11416723Sralph if (error)
11516723Sralph break;
11645732Smckusick mbp->msg_bufr += l;
11745732Smckusick if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE)
11845732Smckusick mbp->msg_bufr = 0;
11916723Sralph }
12016725Sralph return (error);
12116723Sralph }
12216723Sralph
12324524Sbloom /*ARGSUSED*/
124*68314Scgd int
logselect(dev,rw,p)12547540Skarels logselect(dev, rw, p)
12616723Sralph dev_t dev;
12716723Sralph int rw;
12847540Skarels struct proc *p;
12916723Sralph {
13016723Sralph int s = splhigh();
13116723Sralph
13216723Sralph switch (rw) {
13316723Sralph
13416723Sralph case FREAD:
13545732Smckusick if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
13626249Skarels splx(s);
13726249Skarels return (1);
13826249Skarels }
13952521Smckusick selrecord(p, &logsoftc.sc_selp);
14016723Sralph break;
14116723Sralph }
14216723Sralph splx(s);
14316725Sralph return (0);
14416723Sralph }
14516723Sralph
146*68314Scgd void
logwakeup()14716723Sralph logwakeup()
14816723Sralph {
14943661Skarels struct proc *p;
15016723Sralph
15118365Skarels if (!log_open)
15218365Skarels return;
15352521Smckusick selwakeup(&logsoftc.sc_selp);
15443661Skarels if (logsoftc.sc_state & LOG_ASYNC) {
15543661Skarels if (logsoftc.sc_pgid < 0)
15644986Smarc gsignal(-logsoftc.sc_pgid, SIGIO);
15743661Skarels else if (p = pfind(logsoftc.sc_pgid))
15843661Skarels psignal(p, SIGIO);
15943661Skarels }
16016723Sralph if (logsoftc.sc_state & LOG_RDWAIT) {
16145732Smckusick wakeup((caddr_t)msgbufp);
16216723Sralph logsoftc.sc_state &= ~LOG_RDWAIT;
16316723Sralph }
16416723Sralph }
16516723Sralph
16616723Sralph /*ARGSUSED*/
167*68314Scgd int
logioctl(dev,com,data,flag,p)16852419Storek logioctl(dev, com, data, flag, p)
16952419Storek dev_t dev;
17068171Scgd u_long com;
17116723Sralph caddr_t data;
17252419Storek int flag;
17352419Storek struct proc *p;
17416723Sralph {
17516723Sralph long l;
17616723Sralph int s;
17716723Sralph
17816723Sralph switch (com) {
17916723Sralph
18016723Sralph /* return number of characters immediately available */
18116723Sralph case FIONREAD:
18216723Sralph s = splhigh();
18345732Smckusick l = msgbufp->msg_bufx - msgbufp->msg_bufr;
18416723Sralph splx(s);
18516723Sralph if (l < 0)
18616723Sralph l += MSG_BSIZE;
18754773Storek *(int *)data = l;
18816723Sralph break;
18916723Sralph
19016723Sralph case FIONBIO:
19116723Sralph break;
19216723Sralph
19316723Sralph case FIOASYNC:
19416723Sralph if (*(int *)data)
19516723Sralph logsoftc.sc_state |= LOG_ASYNC;
19616723Sralph else
19716723Sralph logsoftc.sc_state &= ~LOG_ASYNC;
19816723Sralph break;
19916723Sralph
20043661Skarels case TIOCSPGRP:
20135810Smarc logsoftc.sc_pgid = *(int *)data;
20216723Sralph break;
20316723Sralph
20416723Sralph case TIOCGPGRP:
20543661Skarels *(int *)data = logsoftc.sc_pgid;
20616723Sralph break;
20716723Sralph
20816723Sralph default:
20916725Sralph return (-1);
21016723Sralph }
21116725Sralph return (0);
21216723Sralph }
213