#include <sys/param.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/filio.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/snoop.h>
#include <sys/sx.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/uio.h>
static struct cdev *snp_dev;
static MALLOC_DEFINE(M_SNP, "snp", "tty snoop device");
#if 0
static struct mtx snp_register_lock;
MTX_SYSINIT(snp_register_lock, &snp_register_lock,
"tty snoop registration", MTX_DEF);
#define SNP_LOCK() mtx_lock(&snp_register_lock)
#define SNP_UNLOCK() mtx_unlock(&snp_register_lock)
#else
static struct sx snp_register_lock;
SX_SYSINIT(snp_register_lock, &snp_register_lock,
"tty snoop registration");
#define SNP_LOCK() sx_xlock(&snp_register_lock)
#define SNP_UNLOCK() sx_xunlock(&snp_register_lock)
#endif
#define SNPGTYY_32DEV _IOR('T', 89, uint32_t)
#define SNP_INPUT_BUFSIZE 16
#define SNP_OUTPUT_BUFSIZE 16384
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;
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",
};
static th_getc_capture_t snp_getc_capture;
static struct ttyhook snp_hook = {
.th_getc_capture = snp_getc_capture,
};
struct snp_softc {
struct tty *snp_tty;
struct ttyoutq snp_outq;
struct cv snp_outwait;
struct selinfo snp_outpoll;
};
static void
snp_dtor(void *data)
{
struct snp_softc *ss = data;
struct tty *tp;
tp = ss->snp_tty;
if (tp != NULL) {
tty_lock(tp);
ttyoutq_free(&ss->snp_outq);
ttyhook_unregister(tp);
ss->snp_tty = NULL;
}
cv_destroy(&ss->snp_outwait);
free(ss, M_SNP);
}
static int
snp_open(struct cdev *dev, int flag, int mode, struct thread *td)
{
struct snp_softc *ss;
ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO);
cv_init(&ss->snp_outwait, "snp out");
devfs_set_cdevpriv(ss, snp_dtor);
return (0);
}
static int
snp_read(struct cdev *dev, struct uio *uio, int flag)
{
int error, oresid = uio->uio_resid;
struct snp_softc *ss;
struct tty *tp;
if (uio->uio_resid == 0)
return (0);
error = devfs_get_cdevpriv((void **)&ss);
if (error != 0)
return (error);
tp = ss->snp_tty;
if (tp == NULL || tty_gone(tp))
return (EIO);
tty_lock(tp);
for (;;) {
error = ttyoutq_read_uio(&ss->snp_outq, tp, uio);
if (error != 0 || uio->uio_resid != oresid)
break;
if (flag & O_NONBLOCK) {
error = EWOULDBLOCK;
break;
}
error = cv_wait_sig(&ss->snp_outwait, tty_getlock(tp));
if (error != 0)
break;
if (tty_gone(tp)) {
error = EIO;
break;
}
}
tty_unlock(tp);
return (error);
}
static int
snp_write(struct cdev *dev, struct uio *uio, int flag)
{
struct snp_softc *ss;
struct tty *tp;
int error, len;
char in[SNP_INPUT_BUFSIZE];
error = devfs_get_cdevpriv((void **)&ss);
if (error != 0)
return (error);
tp = ss->snp_tty;
if (tp == NULL || tty_gone(tp))
return (EIO);
while (uio->uio_resid > 0) {
len = imin(uio->uio_resid, sizeof in);
error = uiomove(in, len, uio);
if (error != 0)
return (error);
tty_lock(tp);
if (tty_gone(tp)) {
tty_unlock(tp);
return (ENXIO);
}
ttydisc_rint_simple(tp, in, len);
ttydisc_rint_done(tp);
tty_unlock(tp);
}
return (0);
}
static int
snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
struct thread *td)
{
struct snp_softc *ss;
struct tty *tp;
int error;
error = devfs_get_cdevpriv((void **)&ss);
if (error != 0)
return (error);
switch (cmd) {
case SNPSTTY:
SNP_LOCK();
tp = ss->snp_tty;
if (tp != NULL) {
if (*(int *)data == -1) {
tty_lock(tp);
ss->snp_tty = NULL;
ttyoutq_free(&ss->snp_outq);
ttyhook_unregister(tp);
error = 0;
} else {
error = EBUSY;
}
SNP_UNLOCK();
return (error);
}
error = ttyhook_register(&ss->snp_tty, td->td_proc,
*(int *)data, &snp_hook, ss);
SNP_UNLOCK();
if (error != 0)
return (error);
tp = ss->snp_tty;
tty_lock(tp);
ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE);
tty_unlock(tp);
return (0);
case SNPGTTY:
if (ss->snp_tty == NULL)
*(dev_t *)data = NODEV;
else
*(dev_t *)data = tty_udev(ss->snp_tty);
return (0);
case SNPGTYY_32DEV:
if (ss->snp_tty == NULL)
*(uint32_t *)data = -1;
else
*(uint32_t *)data = tty_udev(ss->snp_tty);
return (0);
case FIONREAD:
tp = ss->snp_tty;
if (tp != NULL) {
tty_lock(tp);
if (tty_gone(tp))
*(int *)data = SNP_TTYCLOSE;
else
*(int *)data = ttyoutq_bytesused(&ss->snp_outq);
tty_unlock(tp);
} else {
*(int *)data = SNP_DETACH;
}
return (0);
default:
return (ENOTTY);
}
}
static int
snp_poll(struct cdev *dev, int events, struct thread *td)
{
struct snp_softc *ss;
struct tty *tp;
int revents;
if (devfs_get_cdevpriv((void **)&ss) != 0)
return (events &
(POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
revents = 0;
if (events & (POLLIN | POLLRDNORM)) {
tp = ss->snp_tty;
if (tp != NULL) {
tty_lock(tp);
if (ttyoutq_bytesused(&ss->snp_outq) > 0)
revents |= events & (POLLIN | POLLRDNORM);
tty_unlock(tp);
}
}
if (revents == 0)
selrecord(td, &ss->snp_outpoll);
return (revents);
}
static int
snp_modevent(module_t mod, int type, void *data)
{
switch (type) {
case MOD_LOAD:
snp_dev = make_dev(&snp_cdevsw, 0,
UID_ROOT, GID_WHEEL, 0600, "snp");
return (0);
case MOD_UNLOAD:
destroy_dev(snp_dev);
return (0);
default:
return (EOPNOTSUPP);
}
}
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);
}
static moduledata_t snp_mod = {
"snp",
snp_modevent,
NULL
};
DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);