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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/fcntl.h>
33 #include <sys/filio.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
39 #include <sys/snoop.h>
41 #include <sys/systm.h>
45 static struct cdev *snp_dev;
46 static MALLOC_DEFINE(M_SNP, "snp", "tty snoop device");
48 /* XXX: should be mtx, but TTY can be locked by Giant. */
50 static struct mtx snp_register_lock;
51 MTX_SYSINIT(snp_register_lock, &snp_register_lock,
52 "tty snoop registration", MTX_DEF);
53 #define SNP_LOCK() mtx_lock(&snp_register_lock)
54 #define SNP_UNLOCK() mtx_unlock(&snp_register_lock)
56 static struct sx snp_register_lock;
57 SX_SYSINIT(snp_register_lock, &snp_register_lock,
58 "tty snoop registration");
59 #define SNP_LOCK() sx_xlock(&snp_register_lock)
60 #define SNP_UNLOCK() sx_xunlock(&snp_register_lock)
64 * There is no need to have a big input buffer. In most typical setups,
65 * we won't inject much data into the TTY, because users can't type
68 #define SNP_INPUT_BUFSIZE 16
70 * The output buffer has to be really big. Right now we don't support
71 * any form of flow control, which means we lost any data we can't
72 * accept. We set the output buffer size to about twice the size of a
73 * pseudo-terminal/virtual console's output buffer.
75 #define SNP_OUTPUT_BUFSIZE 16384
77 static d_open_t snp_open;
78 static d_read_t snp_read;
79 static d_write_t snp_write;
80 static d_ioctl_t snp_ioctl;
81 static d_poll_t snp_poll;
83 static struct cdevsw snp_cdevsw = {
84 .d_version = D_VERSION,
93 static th_getc_capture_t snp_getc_capture;
95 static struct ttyhook snp_hook = {
96 .th_getc_capture = snp_getc_capture,
100 * Per-instance structure.
103 * (r) locked by snp_register_lock on assignment
104 * (t) locked by tty_lock
107 struct tty *snp_tty; /* (r) TTY we're snooping. */
108 struct ttyoutq snp_outq; /* (t) Output queue. */
109 struct cv snp_outwait; /* (t) Output wait queue. */
110 struct selinfo snp_outpoll; /* (t) Output polling. */
116 struct snp_softc *ss = data;
122 ttyoutq_free(&ss->snp_outq);
123 ttyhook_unregister(tp);
126 cv_destroy(&ss->snp_outwait);
131 * Snoop device node routines.
135 snp_open(struct cdev *dev, int flag, int mode, struct thread *td)
137 struct snp_softc *ss;
139 /* Allocate per-snoop data. */
140 ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO);
141 cv_init(&ss->snp_outwait, "snp out");
143 devfs_set_cdevpriv(ss, snp_dtor);
149 snp_read(struct cdev *dev, struct uio *uio, int flag)
151 int error, oresid = uio->uio_resid;
152 struct snp_softc *ss;
155 if (uio->uio_resid == 0)
158 error = devfs_get_cdevpriv((void **)&ss);
163 if (tp == NULL || tty_gone(tp))
168 error = ttyoutq_read_uio(&ss->snp_outq, tp, uio);
169 if (error != 0 || uio->uio_resid != oresid)
172 /* Wait for more data. */
173 if (flag & O_NONBLOCK) {
177 error = cv_wait_sig(&ss->snp_outwait, tp->t_mtx);
191 snp_write(struct cdev *dev, struct uio *uio, int flag)
193 struct snp_softc *ss;
196 char in[SNP_INPUT_BUFSIZE];
198 error = devfs_get_cdevpriv((void **)&ss);
203 if (tp == NULL || tty_gone(tp))
206 while (uio->uio_resid > 0) {
208 len = imin(uio->uio_resid, sizeof in);
209 error = uiomove(in, len, uio);
215 /* Driver could have abandoned the TTY in the mean time. */
222 * Deliver data to the TTY. Ignore errors for now,
223 * because we shouldn't bail out when we're running
224 * close to the watermarks.
226 ttydisc_rint_simple(tp, in, len);
227 ttydisc_rint_done(tp);
236 snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
239 struct snp_softc *ss;
243 error = devfs_get_cdevpriv((void **)&ss);
249 /* Bind TTY to snoop instance. */
251 if (ss->snp_tty != NULL) {
256 * XXXRW / XXXJA: no capability check here.
258 error = ttyhook_register(&ss->snp_tty, td->td_proc,
259 *(int *)data, &snp_hook, ss);
264 /* Now that went okay, allocate a buffer for the queue. */
267 ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE);
272 /* Obtain device number of associated TTY. */
273 if (ss->snp_tty == NULL)
274 *(dev_t *)data = NODEV;
276 *(dev_t *)data = tty_udev(ss->snp_tty);
282 *(int *)data = ttyoutq_bytesused(&ss->snp_outq);
294 snp_poll(struct cdev *dev, int events, struct thread *td)
296 struct snp_softc *ss;
300 if (devfs_get_cdevpriv((void **)&ss) != 0)
302 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
306 if (events & (POLLIN | POLLRDNORM)) {
310 if (ttyoutq_bytesused(&ss->snp_outq) > 0)
311 revents |= events & (POLLIN | POLLRDNORM);
317 selrecord(td, &ss->snp_outpoll);
327 snp_modevent(module_t mod, int type, void *data)
332 snp_dev = make_dev(&snp_cdevsw, 0,
333 UID_ROOT, GID_WHEEL, 0600, "snp");
336 /* XXX: Make existing users leave. */
337 destroy_dev(snp_dev);
345 snp_getc_capture(struct tty *tp, const void *buf, size_t len)
347 struct snp_softc *ss = ttyhook_softc(tp);
349 ttyoutq_write(&ss->snp_outq, buf, len);
351 cv_broadcast(&ss->snp_outwait);
352 selwakeup(&ss->snp_outpoll);
355 static moduledata_t snp_mod = {
361 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);