2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/filio.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
41 #include <sys/snoop.h>
43 #include <sys/systm.h>
47 static struct cdev *snp_dev;
48 static MALLOC_DEFINE(M_SNP, "snp", "tty snoop device");
50 /* XXX: should be mtx, but TTY can be locked by Giant. */
52 static struct mtx snp_register_lock;
53 MTX_SYSINIT(snp_register_lock, &snp_register_lock,
54 "tty snoop registration", MTX_DEF);
55 #define SNP_LOCK() mtx_lock(&snp_register_lock)
56 #define SNP_UNLOCK() mtx_unlock(&snp_register_lock)
58 static struct sx snp_register_lock;
59 SX_SYSINIT(snp_register_lock, &snp_register_lock,
60 "tty snoop registration");
61 #define SNP_LOCK() sx_xlock(&snp_register_lock)
62 #define SNP_UNLOCK() sx_xunlock(&snp_register_lock)
65 #define SNPGTYY_32DEV _IOR('T', 89, uint32_t)
68 * There is no need to have a big input buffer. In most typical setups,
69 * we won't inject much data into the TTY, because users can't type
72 #define SNP_INPUT_BUFSIZE 16
74 * The output buffer has to be really big. Right now we don't support
75 * any form of flow control, which means we lost any data we can't
76 * accept. We set the output buffer size to about twice the size of a
77 * pseudo-terminal/virtual console's output buffer.
79 #define SNP_OUTPUT_BUFSIZE 16384
81 static d_open_t snp_open;
82 static d_read_t snp_read;
83 static d_write_t snp_write;
84 static d_ioctl_t snp_ioctl;
85 static d_poll_t snp_poll;
87 static struct cdevsw snp_cdevsw = {
88 .d_version = D_VERSION,
97 static th_getc_capture_t snp_getc_capture;
99 static struct ttyhook snp_hook = {
100 .th_getc_capture = snp_getc_capture,
104 * Per-instance structure.
107 * (r) locked by snp_register_lock on assignment
108 * (t) locked by tty_lock
111 struct tty *snp_tty; /* (r) TTY we're snooping. */
112 struct ttyoutq snp_outq; /* (t) Output queue. */
113 struct cv snp_outwait; /* (t) Output wait queue. */
114 struct selinfo snp_outpoll; /* (t) Output polling. */
120 struct snp_softc *ss = data;
126 ttyoutq_free(&ss->snp_outq);
127 ttyhook_unregister(tp);
130 cv_destroy(&ss->snp_outwait);
135 * Snoop device node routines.
139 snp_open(struct cdev *dev, int flag, int mode, struct thread *td)
141 struct snp_softc *ss;
143 /* Allocate per-snoop data. */
144 ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO);
145 cv_init(&ss->snp_outwait, "snp out");
147 devfs_set_cdevpriv(ss, snp_dtor);
153 snp_read(struct cdev *dev, struct uio *uio, int flag)
155 int error, oresid = uio->uio_resid;
156 struct snp_softc *ss;
159 if (uio->uio_resid == 0)
162 error = devfs_get_cdevpriv((void **)&ss);
167 if (tp == NULL || tty_gone(tp))
172 error = ttyoutq_read_uio(&ss->snp_outq, tp, uio);
173 if (error != 0 || uio->uio_resid != oresid)
176 /* Wait for more data. */
177 if (flag & O_NONBLOCK) {
181 error = cv_wait_sig(&ss->snp_outwait, tty_getlock(tp));
195 snp_write(struct cdev *dev, struct uio *uio, int flag)
197 struct snp_softc *ss;
200 char in[SNP_INPUT_BUFSIZE];
202 error = devfs_get_cdevpriv((void **)&ss);
207 if (tp == NULL || tty_gone(tp))
210 while (uio->uio_resid > 0) {
212 len = imin(uio->uio_resid, sizeof in);
213 error = uiomove(in, len, uio);
219 /* Driver could have abandoned the TTY in the mean time. */
226 * Deliver data to the TTY. Ignore errors for now,
227 * because we shouldn't bail out when we're running
228 * close to the watermarks.
230 ttydisc_rint_simple(tp, in, len);
231 ttydisc_rint_done(tp);
240 snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
243 struct snp_softc *ss;
247 error = devfs_get_cdevpriv((void **)&ss);
253 /* Bind TTY to snoop instance. */
255 if (ss->snp_tty != NULL) {
260 * XXXRW / XXXJA: no capability check here.
262 error = ttyhook_register(&ss->snp_tty, td->td_proc,
263 *(int *)data, &snp_hook, ss);
268 /* Now that went okay, allocate a buffer for the queue. */
271 ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE);
276 /* Obtain device number of associated TTY. */
277 if (ss->snp_tty == NULL)
278 *(dev_t *)data = NODEV;
280 *(dev_t *)data = tty_udev(ss->snp_tty);
283 if (ss->snp_tty == NULL)
284 *(uint32_t *)data = -1;
286 *(uint32_t *)data = tty_udev(ss->snp_tty); /* trunc */
292 *(int *)data = ttyoutq_bytesused(&ss->snp_outq);
304 snp_poll(struct cdev *dev, int events, struct thread *td)
306 struct snp_softc *ss;
310 if (devfs_get_cdevpriv((void **)&ss) != 0)
312 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
316 if (events & (POLLIN | POLLRDNORM)) {
320 if (ttyoutq_bytesused(&ss->snp_outq) > 0)
321 revents |= events & (POLLIN | POLLRDNORM);
327 selrecord(td, &ss->snp_outpoll);
337 snp_modevent(module_t mod, int type, void *data)
342 snp_dev = make_dev(&snp_cdevsw, 0,
343 UID_ROOT, GID_WHEEL, 0600, "snp");
346 /* XXX: Make existing users leave. */
347 destroy_dev(snp_dev);
355 snp_getc_capture(struct tty *tp, const void *buf, size_t len)
357 struct snp_softc *ss = ttyhook_softc(tp);
359 ttyoutq_write(&ss->snp_outq, buf, len);
361 cv_broadcast(&ss->snp_outwait);
362 selwakeup(&ss->snp_outpoll);
365 static moduledata_t snp_mod = {
371 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);