diff options
author | ed <ed@FreeBSD.org> | 2008-09-22 19:25:14 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2008-09-22 19:25:14 +0000 |
commit | 1475e942ed4a544d79230d9b2774b0e79f9189cd (patch) | |
tree | b0edacd4932fd8b74d222a3adf26ec514131733e /sys/kern | |
parent | d1f0654ba47a78005fb632d88f7f7e1f2715a850 (diff) | |
download | FreeBSD-src-1475e942ed4a544d79230d9b2774b0e79f9189cd.zip FreeBSD-src-1475e942ed4a544d79230d9b2774b0e79f9189cd.tar.gz |
Introduce a hooks layer for the MPSAFE TTY layer.
One of the features that prevented us from fixing some of the TTY
consumers to work once again, was an interface that allowed consumers to
do the following:
- `Sniff' incoming data, which is used by the snp(4) driver.
- Take direct control of the input and output paths of a TTY, which is
used by ng_tty(4), ppp(4), sl(4), etc.
There's no practical advantage in committing a hooks layer without
having any consumers. In P4 there is a preliminary port of snp(4) and
thompsa@ is busy porting ng_tty(4) to this interface. I already want to
have it in the tree, because this may stimulate others to work on the
remaining modules.
Discussed with: thompsa
Obtained from: //depot/projects/mpsafetty/...
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/tty.c | 90 | ||||
-rw-r--r-- | sys/kern/tty_ttydisc.c | 101 |
2 files changed, 181 insertions, 10 deletions
diff --git a/sys/kern/tty.c b/sys/kern/tty.c index cd6a246..863b6ce 100644 --- a/sys/kern/tty.c +++ b/sys/kern/tty.c @@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$"); #include <sys/conf.h> #include <sys/cons.h> #include <sys/fcntl.h> +#include <sys/file.h> #include <sys/filio.h> #ifdef COMPAT_43TTY #include <sys/ioctl_compat.h> @@ -59,6 +60,7 @@ __FBSDID("$FreeBSD$"); #include <sys/ttydefaults.h> #undef TTYDEFCHARS #include <sys/ucred.h> +#include <sys/vnode.h> #include <machine/stdarg.h> @@ -855,7 +857,7 @@ tty_alloc(struct ttydevsw *tsw, void *sc, struct mtx *mutex) tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO); tp->t_devsw = tsw; - tp->t_softc = sc; + tp->t_devswsoftc = sc; tp->t_flags = tsw->tsw_flags; tty_init_termios(tp); @@ -923,7 +925,7 @@ tty_rel_free(struct tty *tp) tty_lock_assert(tp, MA_OWNED); if (tp->t_sessioncnt != 0 || - (tp->t_flags & (TF_GONE|TF_OPENED)) != TF_GONE) { + (tp->t_flags & (TF_GONE|TF_OPENED|TF_HOOK)) != TF_GONE) { /* TTY is still in use. */ tty_unlock(tp); return; @@ -1653,6 +1655,89 @@ tty_hiwat_in_unblock(struct tty *tp) ttydevsw_inwakeup(tp); } +static int +ttyhook_defrint(struct tty *tp, char c, int flags) +{ + + if (ttyhook_rint_bypass(tp, &c, 1) != 1) + return (-1); + + return (0); +} + +int +ttyhook_register(struct tty **rtp, struct thread *td, int fd, + struct ttyhook *th, void *softc) +{ + struct tty *tp; + struct file *fp; + struct cdev *dev; + struct cdevsw *cdp; + int error; + + /* Validate the file descriptor. */ + if (fget(td, fd, &fp) != 0) + return (EINVAL); + + /* Make sure the vnode is bound to a character device. */ + error = EINVAL; + if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR || + fp->f_vnode->v_rdev == NULL) + goto done1; + dev = fp->f_vnode->v_rdev; + + /* Make sure it is a TTY. */ + cdp = dev_refthread(dev); + if (cdp == NULL) + goto done1; + if ((cdp->d_flags & D_TTY) == 0) + goto done2; + tp = dev->si_drv1; + + /* Try to attach the hook to the TTY. */ + error = EBUSY; + tty_lock(tp); + MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0)); + if (tp->t_flags & TF_HOOK) + goto done3; + + tp->t_flags |= TF_HOOK; + tp->t_hook = th; + tp->t_hooksoftc = softc; + *rtp = tp; + error = 0; + + /* Maybe we can switch into bypass mode now. */ + ttydisc_optimize(tp); + + /* Silently convert rint() calls to rint_bypass() when possible. */ + if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass)) + th->th_rint = ttyhook_defrint; + +done3: tty_unlock(tp); +done2: dev_relthread(dev); +done1: fdrop(fp, td); + return (error); +} + +void +ttyhook_unregister(struct tty *tp) +{ + + tty_lock_assert(tp, MA_OWNED); + MPASS(tp->t_flags & TF_HOOK); + + /* Disconnect the hook. */ + tp->t_flags &= ~TF_HOOK; + tp->t_hook = NULL; + + /* Maybe we need to leave bypass mode. */ + ttydisc_optimize(tp); + + /* Maybe deallocate the TTY as well. */ + tty_rel_free(tp); +} + #include "opt_ddb.h" #ifdef DDB #include <ddb/ddb.h> @@ -1686,6 +1771,7 @@ static struct { { TF_EXCLUDE, 'X' }, { TF_BYPASS, 'l' }, { TF_ZOMBIE, 'Z' }, + { TF_HOOK, 's' }, { 0, '\0' }, }; diff --git a/sys/kern/tty_ttydisc.c b/sys/kern/tty_ttydisc.c index 693b480..0d7dd7f 100644 --- a/sys/kern/tty_ttydisc.c +++ b/sys/kern/tty_ttydisc.c @@ -79,6 +79,8 @@ SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD, #define CTL_ALNUM(c) (((c) >= '0' && (c) <= '9') || \ ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) +#define TTY_STACKBUF 256 + void ttydisc_open(struct tty *tp) { @@ -100,6 +102,9 @@ ttydisc_close(struct tty *tp) ttydevsw_inwakeup(tp); ttydevsw_outwakeup(tp); } + + if (ttyhook_hashook(tp, close)) + ttyhook_close(tp); } static int @@ -433,7 +438,7 @@ ttydisc_write_oproc(struct tty *tp, char c) int ttydisc_write(struct tty *tp, struct uio *uio, int ioflag) { - char ob[256]; + char ob[TTY_STACKBUF]; char *obstart; int error = 0; unsigned int oblen = 0; @@ -557,11 +562,12 @@ ttydisc_optimize(struct tty *tp) { tty_lock_assert(tp, MA_OWNED); - if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) && + if ((!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) && (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) && (!CMP_FLAG(i, PARMRK) || CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) && - !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) { + !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) || + ttyhook_hashook(tp, rint_bypass)) { tp->t_flags |= TF_BYPASS; } else { tp->t_flags &= ~TF_BYPASS; @@ -818,6 +824,9 @@ ttydisc_rint(struct tty *tp, char c, int flags) tty_lock_assert(tp, MA_OWNED); atomic_add_long(&tty_nin, 1); + + if (ttyhook_hashook(tp, rint)) + return ttyhook_rint(tp, c, flags); if (tp->t_flags & TF_BYPASS) goto processed; @@ -1045,6 +1054,9 @@ ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len) atomic_add_long(&tty_nin, len); + if (ttyhook_hashook(tp, rint_bypass)) + return ttyhook_rint_bypass(tp, buf, len); + ret = ttyinq_write(&tp->t_inq, buf, len, 0); ttyinq_canonicalize(&tp->t_inq); @@ -1057,12 +1069,38 @@ ttydisc_rint_done(struct tty *tp) tty_lock_assert(tp, MA_OWNED); + if (ttyhook_hashook(tp, rint_done)) + ttyhook_rint_done(tp); + /* Wake up readers. */ tty_wakeup(tp, FREAD); /* Wake up driver for echo. */ ttydevsw_outwakeup(tp); } +size_t +ttydisc_rint_poll(struct tty *tp) +{ + size_t l; + + tty_lock_assert(tp, MA_OWNED); + + if (ttyhook_hashook(tp, rint_poll)) + return ttyhook_rint_poll(tp); + + /* + * XXX: Still allow character input when there's no space in the + * buffers, but we haven't entered the high watermark. This is + * to allow backspace characters to be inserted when in + * canonical mode. + */ + l = ttyinq_bytesleft(&tp->t_inq); + if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0) + return (1); + + return (l); +} + static void ttydisc_wakeup_watermark(struct tty *tp) { @@ -1093,9 +1131,15 @@ ttydisc_getc(struct tty *tp, void *buf, size_t len) if (tp->t_flags & TF_STOPPED) return (0); + if (ttyhook_hashook(tp, getc_inject)) + return ttyhook_getc_inject(tp, buf, len); + len = ttyoutq_read(&tp->t_outq, buf, len); - ttydisc_wakeup_watermark(tp); + if (ttyhook_hashook(tp, getc_capture)) + ttyhook_getc_capture(tp, buf, len); + + ttydisc_wakeup_watermark(tp); atomic_add_long(&tty_nout, len); return (len); @@ -1104,22 +1148,63 @@ ttydisc_getc(struct tty *tp, void *buf, size_t len) int ttydisc_getc_uio(struct tty *tp, struct uio *uio) { - int error; + int error = 0; int obytes = uio->uio_resid; + size_t len; + char buf[TTY_STACKBUF]; tty_lock_assert(tp, MA_OWNED); if (tp->t_flags & TF_STOPPED) return (0); - error = ttyoutq_read_uio(&tp->t_outq, tp, uio); - ttydisc_wakeup_watermark(tp); + /* + * When a TTY hook is attached, we cannot perform unbuffered + * copying to userspace. Just call ttydisc_getc() and + * temporarily store data in a shadow buffer. + */ + if (ttyhook_hashook(tp, getc_capture) || + ttyhook_hashook(tp, getc_inject)) { + while (uio->uio_resid > 0) { + /* Read to shadow buffer. */ + len = ttydisc_getc(tp, buf, + MIN(uio->uio_resid, sizeof buf)); + if (len == 0) + break; + + /* Copy to userspace. */ + tty_unlock(tp); + error = uiomove(buf, len, uio); + tty_lock(tp); + + if (error != 0) + break; + } + } else { + error = ttyoutq_read_uio(&tp->t_outq, tp, uio); - atomic_add_long(&tty_nout, obytes - uio->uio_resid); + ttydisc_wakeup_watermark(tp); + atomic_add_long(&tty_nout, obytes - uio->uio_resid); + } return (error); } +size_t +ttydisc_getc_poll(struct tty *tp) +{ + + tty_lock_assert(tp, MA_OWNED); + + if (tp->t_flags & TF_STOPPED) + return (0); + + if (ttyhook_hashook(tp, getc_poll)) + return ttyhook_getc_poll(tp); + + return ttyoutq_bytesused(&tp->t_outq); +} + /* * XXX: not really related to the TTYDISC, but we'd better put * tty_putchar() here, because we need to perform proper output |