2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
2008-11-05 15:04:03 +00:00
|
|
|
* Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
1995-02-14 21:21:26 +00:00
|
|
|
*
|
2008-11-05 15:04:03 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1999-06-17 23:42:45 +00:00
|
|
|
*
|
2008-11-05 15:04:03 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-02-14 21:21:26 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-24 18:03:45 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/param.h>
|
2008-11-05 15:04:03 +00:00
|
|
|
#include <sys/conf.h>
|
2004-12-22 17:30:38 +00:00
|
|
|
#include <sys/fcntl.h>
|
1997-03-24 11:37:53 +00:00
|
|
|
#include <sys/filio.h>
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/kernel.h>
|
2008-11-05 15:04:03 +00:00
|
|
|
#include <sys/malloc.h>
|
2004-05-30 20:08:47 +00:00
|
|
|
#include <sys/module.h>
|
2008-11-05 15:04:03 +00:00
|
|
|
#include <sys/poll.h>
|
2008-12-13 21:17:46 +00:00
|
|
|
#include <sys/proc.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <sys/snoop.h>
|
2008-11-05 15:04:03 +00:00
|
|
|
#include <sys/sx.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/tty.h>
|
2004-12-22 17:30:38 +00:00
|
|
|
#include <sys/uio.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
static struct cdev *snp_dev;
|
|
|
|
/* XXX: should be mtx, but TTY can be locked by Giant. */
|
|
|
|
static struct sx snp_register_lock;
|
|
|
|
SX_SYSINIT(snp_register_lock, &snp_register_lock,
|
|
|
|
"tty snoop registration");
|
|
|
|
static MALLOC_DEFINE(M_SNP, "snp", "tty snoop device");
|
1995-11-29 10:49:16 +00:00
|
|
|
|
2001-07-25 14:47:56 +00:00
|
|
|
/*
|
2008-11-05 15:04:03 +00:00
|
|
|
* There is no need to have a big input buffer. In most typical setups,
|
|
|
|
* we won't inject much data into the TTY, because users can't type
|
|
|
|
* really fast.
|
2001-07-25 14:47:56 +00:00
|
|
|
*/
|
2008-11-05 15:04:03 +00:00
|
|
|
#define SNP_INPUT_BUFSIZE 16
|
2001-07-25 14:47:56 +00:00
|
|
|
/*
|
2008-11-05 15:04:03 +00:00
|
|
|
* The output buffer has to be really big. Right now we don't support
|
|
|
|
* any form of flow control, which means we lost any data we can't
|
|
|
|
* accept. We set the output buffer size to about twice the size of a
|
|
|
|
* pseudo-terminal/virtual console's output buffer.
|
2001-07-25 14:47:56 +00:00
|
|
|
*/
|
2008-11-05 15:04:03 +00:00
|
|
|
#define SNP_OUTPUT_BUFSIZE 16384
|
2001-07-25 14:47:56 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
static d_open_t snp_open;
|
|
|
|
static d_read_t snp_read;
|
|
|
|
static d_write_t snp_write;
|
|
|
|
static d_ioctl_t snp_ioctl;
|
|
|
|
static d_poll_t snp_poll;
|
2001-07-18 13:39:43 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
static struct cdevsw snp_cdevsw = {
|
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_open = snp_open,
|
|
|
|
.d_read = snp_read,
|
|
|
|
.d_write = snp_write,
|
|
|
|
.d_ioctl = snp_ioctl,
|
|
|
|
.d_poll = snp_poll,
|
|
|
|
.d_name = "snp",
|
|
|
|
};
|
2001-05-22 22:13:14 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
static th_getc_capture_t snp_getc_capture;
|
2001-06-05 05:07:53 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
static struct ttyhook snp_hook = {
|
|
|
|
.th_getc_capture = snp_getc_capture,
|
|
|
|
};
|
2001-06-05 05:07:53 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
/*
|
|
|
|
* Per-instance structure.
|
|
|
|
*
|
|
|
|
* List of locks
|
|
|
|
* (r) locked by snp_register_lock on assignment
|
|
|
|
* (t) locked by tty_lock
|
|
|
|
*/
|
|
|
|
struct snp_softc {
|
|
|
|
struct tty *snp_tty; /* (r) TTY we're snooping. */
|
|
|
|
struct ttyoutq snp_outq; /* (t) Output queue. */
|
|
|
|
struct cv snp_outwait; /* (t) Output wait queue. */
|
|
|
|
struct selinfo snp_outpoll; /* (t) Output polling. */
|
|
|
|
};
|
2001-05-22 22:13:14 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
static void
|
|
|
|
snp_dtor(void *data)
|
1995-02-25 20:09:44 +00:00
|
|
|
{
|
2008-11-05 15:04:03 +00:00
|
|
|
struct snp_softc *ss = data;
|
2004-09-24 08:12:41 +00:00
|
|
|
struct tty *tp;
|
1997-03-16 19:11:40 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
tp = ss->snp_tty;
|
|
|
|
if (tp != NULL) {
|
|
|
|
tty_lock(tp);
|
|
|
|
ttyoutq_free(&ss->snp_outq);
|
|
|
|
ttyhook_unregister(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
cv_destroy(&ss->snp_outwait);
|
|
|
|
free(ss, M_SNP);
|
1995-02-25 20:09:44 +00:00
|
|
|
}
|
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
/*
|
|
|
|
* Snoop device node routines.
|
|
|
|
*/
|
1995-02-27 19:47:53 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2008-11-05 15:04:03 +00:00
|
|
|
snp_open(struct cdev *dev, int flag, int mode, struct thread *td)
|
1995-02-27 19:47:53 +00:00
|
|
|
{
|
2008-11-05 15:04:03 +00:00
|
|
|
struct snp_softc *ss;
|
2008-08-15 13:07:07 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
/* Allocate per-snoop data. */
|
|
|
|
ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO);
|
|
|
|
cv_init(&ss->snp_outwait, "snp out");
|
1995-02-27 19:47:53 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
devfs_set_cdevpriv(ss, snp_dtor);
|
1995-02-27 19:47:53 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-27 19:47:53 +00:00
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2008-11-05 15:04:03 +00:00
|
|
|
snp_read(struct cdev *dev, struct uio *uio, int flag)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2008-11-05 15:04:03 +00:00
|
|
|
int error, oresid = uio->uio_resid;
|
|
|
|
struct snp_softc *ss;
|
|
|
|
struct tty *tp;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
if (uio->uio_resid == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
error = devfs_get_cdevpriv((void **)&ss);
|
2008-08-15 13:07:07 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2008-11-05 15:04:03 +00:00
|
|
|
|
|
|
|
tp = ss->snp_tty;
|
|
|
|
if (tp == NULL || tty_gone(tp))
|
1995-02-14 21:21:26 +00:00
|
|
|
return (EIO);
|
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
tty_lock(tp);
|
|
|
|
for (;;) {
|
|
|
|
error = ttyoutq_read_uio(&ss->snp_outq, tp, uio);
|
|
|
|
if (error != 0 || uio->uio_resid != oresid)
|
1995-02-14 21:21:26 +00:00
|
|
|
break;
|
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
/* Wait for more data. */
|
|
|
|
if (flag & O_NONBLOCK) {
|
|
|
|
error = EWOULDBLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
error = cv_wait_sig(&ss->snp_outwait, tp->t_mtx);
|
|
|
|
if (error != 0)
|
|
|
|
break;
|
|
|
|
if (tty_gone(tp)) {
|
|
|
|
error = EIO;
|
|
|
|
break;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-05 15:04:03 +00:00
|
|
|
tty_unlock(tp);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2008-11-05 15:04:03 +00:00
|
|
|
snp_write(struct cdev *dev, struct uio *uio, int flag)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2008-11-05 15:04:03 +00:00
|
|
|
struct snp_softc *ss;
|
|
|
|
struct tty *tp;
|
|
|
|
int error, len, i;
|
|
|
|
char in[SNP_INPUT_BUFSIZE];
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
error = devfs_get_cdevpriv((void **)&ss);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
tp = ss->snp_tty;
|
|
|
|
if (tp == NULL || tty_gone(tp))
|
|
|
|
return (EIO);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
while (uio->uio_resid > 0) {
|
|
|
|
/* Read new data. */
|
|
|
|
len = imin(uio->uio_resid, sizeof in);
|
|
|
|
error = uiomove(in, len, uio);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
tty_lock(tp);
|
|
|
|
|
|
|
|
/* Driver could have abandoned the TTY in the mean time. */
|
|
|
|
if (tty_gone(tp)) {
|
|
|
|
tty_unlock(tp);
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
1995-02-15 18:41:57 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
/*
|
2008-11-05 15:04:03 +00:00
|
|
|
* Deliver data to the TTY. Ignore errors for now,
|
|
|
|
* because we shouldn't bail out when we're running
|
|
|
|
* close to the watermarks.
|
1995-02-15 16:35:38 +00:00
|
|
|
*/
|
2008-11-05 15:04:03 +00:00
|
|
|
if (ttydisc_can_bypass(tp)) {
|
|
|
|
ttydisc_rint_bypass(tp, in, len);
|
1995-02-14 21:21:26 +00:00
|
|
|
} else {
|
2008-11-05 15:04:03 +00:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
ttydisc_rint(tp, in[i], 0);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
2000-04-02 00:35:37 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
ttydisc_rint_done(tp);
|
|
|
|
tty_unlock(tp);
|
2000-04-02 00:35:37 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2007-07-05 13:07:12 +00:00
|
|
|
return (0);
|
1995-02-15 18:41:57 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2008-11-05 15:04:03 +00:00
|
|
|
snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
|
2007-03-23 22:48:44 +00:00
|
|
|
struct thread *td)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2008-11-05 15:04:03 +00:00
|
|
|
struct snp_softc *ss;
|
2007-12-24 13:47:16 +00:00
|
|
|
struct tty *tp;
|
2008-11-05 15:04:03 +00:00
|
|
|
int error;
|
2008-08-15 13:07:07 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
error = devfs_get_cdevpriv((void **)&ss);
|
2008-08-15 13:07:07 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SNPSTTY:
|
2008-11-05 15:04:03 +00:00
|
|
|
/* Bind TTY to snoop instance. */
|
|
|
|
sx_xlock(&snp_register_lock);
|
|
|
|
if (ss->snp_tty != NULL) {
|
|
|
|
sx_xunlock(&snp_register_lock);
|
2001-11-24 15:34:18 +00:00
|
|
|
return (EBUSY);
|
2008-11-05 15:04:03 +00:00
|
|
|
}
|
2008-12-13 21:17:46 +00:00
|
|
|
error = ttyhook_register(&ss->snp_tty, td->td_proc, *(int *)data,
|
2008-11-05 15:04:03 +00:00
|
|
|
&snp_hook, ss);
|
|
|
|
sx_xunlock(&snp_register_lock);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
/* Now that went okay, allocate a buffer for the queue. */
|
|
|
|
tp = ss->snp_tty;
|
|
|
|
tty_lock(tp);
|
|
|
|
ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE);
|
|
|
|
tty_unlock(tp);
|
1995-02-25 20:09:44 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
return (0);
|
1995-02-14 21:21:26 +00:00
|
|
|
case SNPGTTY:
|
2008-11-05 15:04:03 +00:00
|
|
|
/* Obtain device number of associated TTY. */
|
|
|
|
if (ss->snp_tty == NULL)
|
|
|
|
*(dev_t *)data = NODEV;
|
1995-02-14 21:21:26 +00:00
|
|
|
else
|
2008-11-05 15:04:03 +00:00
|
|
|
*(dev_t *)data = tty_udev(ss->snp_tty);
|
|
|
|
return (0);
|
1995-02-14 21:21:26 +00:00
|
|
|
case FIONREAD:
|
2008-11-05 15:04:03 +00:00
|
|
|
tp = ss->snp_tty;
|
|
|
|
if (tp != NULL) {
|
|
|
|
tty_lock(tp);
|
|
|
|
*(int *)data = ttyoutq_bytesused(&ss->snp_outq);
|
|
|
|
tty_unlock(tp);
|
|
|
|
} else {
|
|
|
|
*(int *)data = 0;
|
|
|
|
}
|
|
|
|
return (0);
|
1995-02-14 21:21:26 +00:00
|
|
|
default:
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2008-11-05 15:04:03 +00:00
|
|
|
snp_poll(struct cdev *dev, int events, struct thread *td)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2008-11-05 15:04:03 +00:00
|
|
|
struct snp_softc *ss;
|
|
|
|
struct tty *tp;
|
2001-06-05 05:00:17 +00:00
|
|
|
int revents;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
if (devfs_get_cdevpriv((void **)&ss) != 0)
|
2008-08-15 13:07:07 +00:00
|
|
|
return (events &
|
|
|
|
(POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
revents = 0;
|
2008-11-05 15:04:03 +00:00
|
|
|
|
1999-05-06 18:13:11 +00:00
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
2008-11-05 15:04:03 +00:00
|
|
|
tp = ss->snp_tty;
|
|
|
|
if (tp != NULL) {
|
|
|
|
tty_lock(tp);
|
|
|
|
if (ttyoutq_bytesused(&ss->snp_outq) > 0)
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
tty_unlock(tp);
|
|
|
|
}
|
1999-05-06 18:13:11 +00:00
|
|
|
}
|
2008-11-05 15:04:03 +00:00
|
|
|
|
|
|
|
if (revents == 0)
|
|
|
|
selrecord(td, &ss->snp_outpoll);
|
|
|
|
|
1997-09-14 02:42:03 +00:00
|
|
|
return (revents);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2008-11-05 15:04:03 +00:00
|
|
|
/*
|
|
|
|
* TTY hook events.
|
|
|
|
*/
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
static int
|
2007-03-23 22:48:44 +00:00
|
|
|
snp_modevent(module_t mod, int type, void *data)
|
1995-11-29 10:49:16 +00:00
|
|
|
{
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
2008-11-05 15:04:03 +00:00
|
|
|
snp_dev = make_dev(&snp_cdevsw, 0,
|
|
|
|
UID_ROOT, GID_WHEEL, 0600, "snp");
|
|
|
|
return (0);
|
2001-05-22 22:13:14 +00:00
|
|
|
case MOD_UNLOAD:
|
2008-11-05 15:04:03 +00:00
|
|
|
/* XXX: Make existing users leave. */
|
|
|
|
destroy_dev(snp_dev);
|
|
|
|
return (0);
|
2001-05-22 22:13:14 +00:00
|
|
|
default:
|
2004-07-15 08:26:07 +00:00
|
|
|
return (EOPNOTSUPP);
|
2001-05-22 22:13:14 +00:00
|
|
|
}
|
2008-11-05 15:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
snp_getc_capture(struct tty *tp, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct snp_softc *ss = ttyhook_softc(tp);
|
|
|
|
|
|
|
|
ttyoutq_write(&ss->snp_outq, buf, len);
|
|
|
|
|
|
|
|
cv_broadcast(&ss->snp_outwait);
|
|
|
|
selwakeup(&ss->snp_outpoll);
|
1995-11-29 10:49:16 +00:00
|
|
|
}
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
static moduledata_t snp_mod = {
|
2008-11-05 15:04:03 +00:00
|
|
|
"snp",
|
|
|
|
snp_modevent,
|
|
|
|
NULL
|
2001-05-22 22:13:14 +00:00
|
|
|
};
|
2008-11-05 15:04:03 +00:00
|
|
|
|
2004-02-21 20:29:52 +00:00
|
|
|
DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|