1a1215e37SEd Schouten /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3c4e20cadSPedro F. Giffuni *
4a1215e37SEd Schouten * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5a1215e37SEd Schouten * All rights reserved.
6a1215e37SEd Schouten *
7a1215e37SEd Schouten * Redistribution and use in source and binary forms, with or without
8a1215e37SEd Schouten * modification, are permitted provided that the following conditions
9a1215e37SEd Schouten * are met:
10a1215e37SEd Schouten * 1. Redistributions of source code must retain the above copyright
11a1215e37SEd Schouten * notice, this list of conditions and the following disclaimer.
12a1215e37SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
13a1215e37SEd Schouten * notice, this list of conditions and the following disclaimer in the
14a1215e37SEd Schouten * documentation and/or other materials provided with the distribution.
15a1215e37SEd Schouten *
16a1215e37SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a1215e37SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a1215e37SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a1215e37SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a1215e37SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a1215e37SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a1215e37SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a1215e37SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a1215e37SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a1215e37SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a1215e37SEd Schouten * SUCH DAMAGE.
27a1215e37SEd Schouten */
28a1215e37SEd Schouten
29a1215e37SEd Schouten #ifndef _SYS_TTYHOOK_H_
30a1215e37SEd Schouten #define _SYS_TTYHOOK_H_
31a1215e37SEd Schouten
32a1215e37SEd Schouten #ifndef _SYS_TTY_H_
33a1215e37SEd Schouten #error "can only be included through <sys/tty.h>"
34a1215e37SEd Schouten #endif /* !_SYS_TTY_H_ */
35a1215e37SEd Schouten
36a1215e37SEd Schouten struct tty;
37a1215e37SEd Schouten
38a1215e37SEd Schouten /*
39a1215e37SEd Schouten * Hooks interface, which allows to capture and inject traffic into the
40a1215e37SEd Schouten * input and output paths of a TTY.
41a1215e37SEd Schouten */
42a1215e37SEd Schouten
43a1215e37SEd Schouten typedef int th_rint_t(struct tty *tp, char c, int flags);
44a1215e37SEd Schouten typedef size_t th_rint_bypass_t(struct tty *tp, const void *buf, size_t len);
45a1215e37SEd Schouten typedef void th_rint_done_t(struct tty *tp);
46a1215e37SEd Schouten typedef size_t th_rint_poll_t(struct tty *tp);
47a1215e37SEd Schouten
48a1215e37SEd Schouten typedef size_t th_getc_inject_t(struct tty *tp, void *buf, size_t len);
49a1215e37SEd Schouten typedef void th_getc_capture_t(struct tty *tp, const void *buf, size_t len);
50a1215e37SEd Schouten typedef size_t th_getc_poll_t(struct tty *tp);
51a1215e37SEd Schouten
52a1215e37SEd Schouten typedef void th_close_t(struct tty *tp);
53a1215e37SEd Schouten
54a1215e37SEd Schouten struct ttyhook {
55a1215e37SEd Schouten /* Character input. */
56a1215e37SEd Schouten th_rint_t *th_rint;
57a1215e37SEd Schouten th_rint_bypass_t *th_rint_bypass;
58a1215e37SEd Schouten th_rint_done_t *th_rint_done;
59a1215e37SEd Schouten th_rint_poll_t *th_rint_poll;
60a1215e37SEd Schouten
61a1215e37SEd Schouten /* Character output. */
62a1215e37SEd Schouten th_getc_inject_t *th_getc_inject;
63a1215e37SEd Schouten th_getc_capture_t *th_getc_capture;
64a1215e37SEd Schouten th_getc_poll_t *th_getc_poll;
65a1215e37SEd Schouten
66a1215e37SEd Schouten th_close_t *th_close;
67a1215e37SEd Schouten };
68a1215e37SEd Schouten
69a9385ad1SAlexander Motin int ttyhook_register(struct tty **, struct proc *, int,
70a1215e37SEd Schouten struct ttyhook *, void *);
71a1215e37SEd Schouten void ttyhook_unregister(struct tty *);
72a1215e37SEd Schouten #define ttyhook_softc(tp) ((tp)->t_hooksoftc)
73a1215e37SEd Schouten #define ttyhook_hashook(tp,hook) ((tp)->t_hook != NULL && \
74a1215e37SEd Schouten (tp)->t_hook->th_ ## hook != NULL)
75a1215e37SEd Schouten
76a1215e37SEd Schouten static __inline int
ttyhook_rint(struct tty * tp,char c,int flags)77a1215e37SEd Schouten ttyhook_rint(struct tty *tp, char c, int flags)
78a1215e37SEd Schouten {
7923d53268SKyle Evans tty_assert_locked(tp);
80a1215e37SEd Schouten MPASS(!tty_gone(tp));
81a1215e37SEd Schouten
82a1215e37SEd Schouten return tp->t_hook->th_rint(tp, c, flags);
83a1215e37SEd Schouten }
84a1215e37SEd Schouten
85a1215e37SEd Schouten static __inline size_t
ttyhook_rint_bypass(struct tty * tp,const void * buf,size_t len)86a1215e37SEd Schouten ttyhook_rint_bypass(struct tty *tp, const void *buf, size_t len)
87a1215e37SEd Schouten {
8823d53268SKyle Evans tty_assert_locked(tp);
89a1215e37SEd Schouten MPASS(!tty_gone(tp));
90a1215e37SEd Schouten
91a1215e37SEd Schouten return tp->t_hook->th_rint_bypass(tp, buf, len);
92a1215e37SEd Schouten }
93a1215e37SEd Schouten
94a1215e37SEd Schouten static __inline void
ttyhook_rint_done(struct tty * tp)95a1215e37SEd Schouten ttyhook_rint_done(struct tty *tp)
96a1215e37SEd Schouten {
9723d53268SKyle Evans tty_assert_locked(tp);
98a1215e37SEd Schouten MPASS(!tty_gone(tp));
99a1215e37SEd Schouten
100a1215e37SEd Schouten tp->t_hook->th_rint_done(tp);
101a1215e37SEd Schouten }
102a1215e37SEd Schouten
103a1215e37SEd Schouten static __inline size_t
ttyhook_rint_poll(struct tty * tp)104a1215e37SEd Schouten ttyhook_rint_poll(struct tty *tp)
105a1215e37SEd Schouten {
10623d53268SKyle Evans tty_assert_locked(tp);
107a1215e37SEd Schouten MPASS(!tty_gone(tp));
108a1215e37SEd Schouten
109a1215e37SEd Schouten return tp->t_hook->th_rint_poll(tp);
110a1215e37SEd Schouten }
111a1215e37SEd Schouten
112a1215e37SEd Schouten static __inline size_t
ttyhook_getc_inject(struct tty * tp,void * buf,size_t len)113a1215e37SEd Schouten ttyhook_getc_inject(struct tty *tp, void *buf, size_t len)
114a1215e37SEd Schouten {
11523d53268SKyle Evans tty_assert_locked(tp);
116a1215e37SEd Schouten MPASS(!tty_gone(tp));
117a1215e37SEd Schouten
118a1215e37SEd Schouten return tp->t_hook->th_getc_inject(tp, buf, len);
119a1215e37SEd Schouten }
120a1215e37SEd Schouten
121a1215e37SEd Schouten static __inline void
ttyhook_getc_capture(struct tty * tp,const void * buf,size_t len)122a1215e37SEd Schouten ttyhook_getc_capture(struct tty *tp, const void *buf, size_t len)
123a1215e37SEd Schouten {
12423d53268SKyle Evans tty_assert_locked(tp);
125a1215e37SEd Schouten MPASS(!tty_gone(tp));
126a1215e37SEd Schouten
127a1215e37SEd Schouten tp->t_hook->th_getc_capture(tp, buf, len);
128a1215e37SEd Schouten }
129a1215e37SEd Schouten
130a1215e37SEd Schouten static __inline size_t
ttyhook_getc_poll(struct tty * tp)131a1215e37SEd Schouten ttyhook_getc_poll(struct tty *tp)
132a1215e37SEd Schouten {
13323d53268SKyle Evans tty_assert_locked(tp);
134a1215e37SEd Schouten MPASS(!tty_gone(tp));
135a1215e37SEd Schouten
136a1215e37SEd Schouten return tp->t_hook->th_getc_poll(tp);
137a1215e37SEd Schouten }
138a1215e37SEd Schouten
139a1215e37SEd Schouten static __inline void
ttyhook_close(struct tty * tp)140a1215e37SEd Schouten ttyhook_close(struct tty *tp)
141a1215e37SEd Schouten {
14223d53268SKyle Evans tty_assert_locked(tp);
143a1215e37SEd Schouten
144a1215e37SEd Schouten tp->t_hook->th_close(tp);
145a1215e37SEd Schouten }
146a1215e37SEd Schouten
147a1215e37SEd Schouten #endif /* !_SYS_TTYHOOK_H_ */
148