2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifndef _SYS_TTYHOOK_H_
30 #define _SYS_TTYHOOK_H_
33 #error "can only be included through <sys/tty.h>"
34 #endif /* !_SYS_TTY_H_ */
39 * Hooks interface, which allows to capture and inject traffic into the
40 * input and output paths of a TTY.
43 typedef int th_rint_t(struct tty *tp, char c, int flags);
44 typedef size_t th_rint_bypass_t(struct tty *tp, const void *buf, size_t len);
45 typedef void th_rint_done_t(struct tty *tp);
46 typedef size_t th_rint_poll_t(struct tty *tp);
48 typedef size_t th_getc_inject_t(struct tty *tp, void *buf, size_t len);
49 typedef void th_getc_capture_t(struct tty *tp, const void *buf, size_t len);
50 typedef size_t th_getc_poll_t(struct tty *tp);
52 typedef void th_close_t(struct tty *tp);
55 /* Character input. */
57 th_rint_bypass_t *th_rint_bypass;
58 th_rint_done_t *th_rint_done;
59 th_rint_poll_t *th_rint_poll;
61 /* Character output. */
62 th_getc_inject_t *th_getc_inject;
63 th_getc_capture_t *th_getc_capture;
64 th_getc_poll_t *th_getc_poll;
69 int ttyhook_register(struct tty **, struct proc *, int,
70 struct ttyhook *, void *);
71 void ttyhook_unregister(struct tty *);
72 #define ttyhook_softc(tp) ((tp)->t_hooksoftc)
73 #define ttyhook_hashook(tp,hook) ((tp)->t_hook != NULL && \
74 (tp)->t_hook->th_ ## hook != NULL)
77 ttyhook_rint(struct tty *tp, char c, int flags)
79 tty_lock_assert(tp, MA_OWNED);
82 return tp->t_hook->th_rint(tp, c, flags);
85 static __inline size_t
86 ttyhook_rint_bypass(struct tty *tp, const void *buf, size_t len)
88 tty_lock_assert(tp, MA_OWNED);
91 return tp->t_hook->th_rint_bypass(tp, buf, len);
95 ttyhook_rint_done(struct tty *tp)
97 tty_lock_assert(tp, MA_OWNED);
100 tp->t_hook->th_rint_done(tp);
103 static __inline size_t
104 ttyhook_rint_poll(struct tty *tp)
106 tty_lock_assert(tp, MA_OWNED);
107 MPASS(!tty_gone(tp));
109 return tp->t_hook->th_rint_poll(tp);
112 static __inline size_t
113 ttyhook_getc_inject(struct tty *tp, void *buf, size_t len)
115 tty_lock_assert(tp, MA_OWNED);
116 MPASS(!tty_gone(tp));
118 return tp->t_hook->th_getc_inject(tp, buf, len);
122 ttyhook_getc_capture(struct tty *tp, const void *buf, size_t len)
124 tty_lock_assert(tp, MA_OWNED);
125 MPASS(!tty_gone(tp));
127 tp->t_hook->th_getc_capture(tp, buf, len);
130 static __inline size_t
131 ttyhook_getc_poll(struct tty *tp)
133 tty_lock_assert(tp, MA_OWNED);
134 MPASS(!tty_gone(tp));
136 return tp->t_hook->th_getc_poll(tp);
140 ttyhook_close(struct tty *tp)
142 tty_lock_assert(tp, MA_OWNED);
144 tp->t_hook->th_close(tp);
147 #endif /* !_SYS_TTYHOOK_H_ */