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)
63 #define SNPGTYY_32DEV _IOR('T', 89, uint32_t)
66 * There is no need to have a big input buffer. In most typical setups,
67 * we won't inject much data into the TTY, because users can't type
70 #define SNP_INPUT_BUFSIZE 16
72 * The output buffer has to be really big. Right now we don't support
73 * any form of flow control, which means we lost any data we can't
74 * accept. We set the output buffer size to about twice the size of a
75 * pseudo-terminal/virtual console's output buffer.
77 #define SNP_OUTPUT_BUFSIZE 16384
79 static d_open_t snp_open;
80 static d_read_t snp_read;
81 static d_write_t snp_write;
82 static d_ioctl_t snp_ioctl;
83 static d_poll_t snp_poll;
85 static struct cdevsw snp_cdevsw = {
86 .d_version = D_VERSION,
95 static th_getc_capture_t snp_getc_capture;
97 static struct ttyhook snp_hook = {
98 .th_getc_capture = snp_getc_capture,
102 * Per-instance structure.
105 * (r) locked by snp_register_lock on assignment
106 * (t) locked by tty_lock
109 struct tty *snp_tty; /* (r) TTY we're snooping. */
110 struct ttyoutq snp_outq; /* (t) Output queue. */
111 struct cv snp_outwait; /* (t) Output wait queue. */
112 struct selinfo snp_outpoll; /* (t) Output polling. */
118 struct snp_softc *ss = data;
124 ttyoutq_free(&ss->snp_outq);
125 ttyhook_unregister(tp);
128 cv_destroy(&ss->snp_outwait);
133 * Snoop device node routines.
137 snp_open(struct cdev *dev, int flag, int mode, struct thread *td)
139 struct snp_softc *ss;
141 /* Allocate per-snoop data. */
142 ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO);
143 cv_init(&ss->snp_outwait, "snp out");
145 devfs_set_cdevpriv(ss, snp_dtor);
151 snp_read(struct cdev *dev, struct uio *uio, int flag)
153 int error, oresid = uio->uio_resid;
154 struct snp_softc *ss;
157 if (uio->uio_resid == 0)
160 error = devfs_get_cdevpriv((void **)&ss);
165 if (tp == NULL || tty_gone(tp))
170 error = ttyoutq_read_uio(&ss->snp_outq, tp, uio);
171 if (error != 0 || uio->uio_resid != oresid)
174 /* Wait for more data. */
175 if (flag & O_NONBLOCK) {
179 error = cv_wait_sig(&ss->snp_outwait, tp->t_mtx);
193 snp_write(struct cdev *dev, struct uio *uio, int flag)
195 struct snp_softc *ss;
198 char in[SNP_INPUT_BUFSIZE];
200 error = devfs_get_cdevpriv((void **)&ss);
205 if (tp == NULL || tty_gone(tp))
208 while (uio->uio_resid > 0) {
210 len = imin(uio->uio_resid, sizeof in);
211 error = uiomove(in, len, uio);
217 /* Driver could have abandoned the TTY in the mean time. */
224 * Deliver data to the TTY. Ignore errors for now,
225 * because we shouldn't bail out when we're running
226 * close to the watermarks.
228 ttydisc_rint_simple(tp, in, len);
229 ttydisc_rint_done(tp);
238 snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
241 struct snp_softc *ss;
245 error = devfs_get_cdevpriv((void **)&ss);
251 /* Bind TTY to snoop instance. */
253 if (ss->snp_tty != NULL) {
258 * XXXRW / XXXJA: no capability check here.
260 error = ttyhook_register(&ss->snp_tty, td->td_proc,
261 *(int *)data, &snp_hook, ss);
266 /* Now that went okay, allocate a buffer for the queue. */
269 ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE);
274 /* Obtain device number of associated TTY. */
275 if (ss->snp_tty == NULL)
276 *(dev_t *)data = NODEV;
278 *(dev_t *)data = tty_udev(ss->snp_tty);
281 if (ss->snp_tty == NULL)
282 *(uint32_t *)data = -1;
284 *(uint32_t *)data = tty_udev(ss->snp_tty); /* trunc */
290 *(int *)data = ttyoutq_bytesused(&ss->snp_outq);
302 snp_poll(struct cdev *dev, int events, struct thread *td)
304 struct snp_softc *ss;
308 if (devfs_get_cdevpriv((void **)&ss) != 0)
310 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
314 if (events & (POLLIN | POLLRDNORM)) {
318 if (ttyoutq_bytesused(&ss->snp_outq) > 0)
319 revents |= events & (POLLIN | POLLRDNORM);
325 selrecord(td, &ss->snp_outpoll);
335 snp_modevent(module_t mod, int type, void *data)
340 snp_dev = make_dev(&snp_cdevsw, 0,
341 UID_ROOT, GID_WHEEL, 0600, "snp");
344 /* XXX: Make existing users leave. */
345 destroy_dev(snp_dev);
353 snp_getc_capture(struct tty *tp, const void *buf, size_t len)
355 struct snp_softc *ss = ttyhook_softc(tp);
357 ttyoutq_write(&ss->snp_outq, buf, len);
359 cv_broadcast(&ss->snp_outwait);
360 selwakeup(&ss->snp_outpoll);
363 static moduledata_t snp_mod = {
369 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);