2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
1995-02-14 21:21:26 +00:00
|
|
|
* Copyright (c) 1995 Ugen J.S.Antsilevich
|
|
|
|
*
|
|
|
|
* Redistribution and use in source forms, with and without modification,
|
|
|
|
* are permitted provided that this entire comment appears intact.
|
|
|
|
*
|
|
|
|
* Redistribution in binary form may occur without any restrictions.
|
|
|
|
* Obviously, it would be nice if you gave credit where credit is due
|
|
|
|
* but requiring it would be too onerous.
|
|
|
|
*
|
|
|
|
* This software is provided ``AS IS'' without any warranties of any kind.
|
|
|
|
*
|
|
|
|
* Snoop stuff.
|
1999-06-17 23:42:45 +00:00
|
|
|
*
|
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>
|
|
|
|
#include <sys/systm.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>
|
1997-10-12 20:26:33 +00:00
|
|
|
#include <sys/malloc.h>
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <sys/conf.h>
|
1997-09-14 02:42:03 +00:00
|
|
|
#include <sys/poll.h>
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/kernel.h>
|
2004-05-30 20:08:47 +00:00
|
|
|
#include <sys/module.h>
|
2001-07-18 13:39:43 +00:00
|
|
|
#include <sys/queue.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <sys/snoop.h>
|
2004-12-22 17:30:38 +00:00
|
|
|
#include <sys/uio.h>
|
2005-09-18 19:23:35 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/vnode.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
|
2001-06-05 05:07:53 +00:00
|
|
|
static l_close_t snplclose;
|
2001-06-05 05:00:17 +00:00
|
|
|
static l_write_t snplwrite;
|
1995-12-08 11:19:42 +00:00
|
|
|
static d_open_t snpopen;
|
|
|
|
static d_read_t snpread;
|
|
|
|
static d_write_t snpwrite;
|
|
|
|
static d_ioctl_t snpioctl;
|
1997-09-14 02:42:03 +00:00
|
|
|
static d_poll_t snppoll;
|
1995-12-08 11:19:42 +00:00
|
|
|
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw snp_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
2008-09-19 10:21:30 +00:00
|
|
|
.d_flags = D_PSEUDO | D_NEEDGIANT,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = snpopen,
|
|
|
|
.d_read = snpread,
|
|
|
|
.d_write = snpwrite,
|
|
|
|
.d_ioctl = snpioctl,
|
|
|
|
.d_poll = snppoll,
|
|
|
|
.d_name = "snp",
|
1999-05-30 16:53:49 +00:00
|
|
|
};
|
1995-12-08 11:19:42 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static struct linesw snpdisc = {
|
2004-07-15 20:47:41 +00:00
|
|
|
.l_open = tty_open,
|
2004-07-14 05:32:55 +00:00
|
|
|
.l_close = snplclose,
|
|
|
|
.l_read = ttread,
|
|
|
|
.l_write = snplwrite,
|
|
|
|
.l_ioctl = l_nullioctl,
|
|
|
|
.l_rint = ttyinput,
|
|
|
|
.l_start = ttstart,
|
|
|
|
.l_modem = ttymodem
|
2001-06-05 05:00:17 +00:00
|
|
|
};
|
1995-11-29 10:49:16 +00:00
|
|
|
|
2001-07-25 14:47:56 +00:00
|
|
|
/*
|
|
|
|
* This is the main snoop per-device structure.
|
|
|
|
*/
|
|
|
|
struct snoop {
|
|
|
|
LIST_ENTRY(snoop) snp_list; /* List glue. */
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *snp_target; /* Target tty device. */
|
2001-07-25 14:47:56 +00:00
|
|
|
struct tty *snp_tty; /* Target tty pointer. */
|
|
|
|
u_long snp_len; /* Possible length. */
|
|
|
|
u_long snp_base; /* Data base. */
|
|
|
|
u_long snp_blen; /* Used length. */
|
|
|
|
caddr_t snp_buf; /* Allocation pointer. */
|
|
|
|
int snp_flags; /* Flags. */
|
|
|
|
struct selinfo snp_sel; /* Select info. */
|
|
|
|
int snp_olddisc; /* Old line discipline. */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Possible flags.
|
|
|
|
*/
|
|
|
|
#define SNOOP_ASYNC 0x0002
|
|
|
|
#define SNOOP_OPEN 0x0004
|
|
|
|
#define SNOOP_RWAIT 0x0008
|
|
|
|
#define SNOOP_OFLOW 0x0010
|
|
|
|
#define SNOOP_DOWN 0x0020
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Other constants.
|
|
|
|
*/
|
|
|
|
#define SNOOP_MINLEN (4*1024) /* This should be power of 2.
|
|
|
|
* 4K tested to be the minimum
|
|
|
|
* for which on normal tty
|
|
|
|
* usage there is no need to
|
|
|
|
* allocate more.
|
|
|
|
*/
|
|
|
|
#define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
|
|
|
|
* If we grow more,something
|
|
|
|
* really bad in this world..
|
|
|
|
*/
|
|
|
|
|
2000-12-08 20:09:00 +00:00
|
|
|
static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
|
2001-05-22 22:13:14 +00:00
|
|
|
/*
|
|
|
|
* The number of the "snoop" line discipline. This gets determined at
|
|
|
|
* module load time.
|
|
|
|
*/
|
2001-06-05 05:00:17 +00:00
|
|
|
static int snooplinedisc;
|
2008-08-15 13:07:07 +00:00
|
|
|
static struct cdev *snoopdev;
|
2001-07-18 13:39:43 +00:00
|
|
|
|
|
|
|
static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
|
|
|
|
|
2004-06-16 09:47:26 +00:00
|
|
|
static struct tty *snpdevtotty(struct cdev *dev);
|
2007-07-05 13:07:12 +00:00
|
|
|
static void snp_detach(void *arg);
|
2002-03-20 02:08:01 +00:00
|
|
|
static int snp_down(struct snoop *snp);
|
|
|
|
static int snp_in(struct snoop *snp, char *buf, int n);
|
|
|
|
static int snp_modevent(module_t mod, int what, void *arg);
|
2004-11-05 18:32:14 +00:00
|
|
|
static struct snoop *ttytosnp(struct tty *);
|
|
|
|
|
|
|
|
static struct snoop *
|
|
|
|
ttytosnp(struct tty *tp)
|
|
|
|
{
|
|
|
|
struct snoop *snp;
|
|
|
|
|
|
|
|
LIST_FOREACH(snp, &snp_sclist, snp_list) {
|
|
|
|
if (snp->snp_tty == tp)
|
|
|
|
return (snp);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
2001-05-22 22:13:14 +00:00
|
|
|
|
2001-06-05 05:07:53 +00:00
|
|
|
static int
|
2007-03-23 22:48:44 +00:00
|
|
|
snplclose(struct tty *tp, int flag)
|
2001-06-05 05:07:53 +00:00
|
|
|
{
|
|
|
|
struct snoop *snp;
|
|
|
|
int error;
|
|
|
|
|
2004-11-05 18:32:14 +00:00
|
|
|
snp = ttytosnp(tp);
|
2001-06-05 05:07:53 +00:00
|
|
|
error = snp_down(snp);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
error = ttylclose(tp, flag);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
static int
|
2007-03-23 22:48:44 +00:00
|
|
|
snplwrite(struct tty *tp, struct uio *uio, int flag)
|
2001-05-22 22:13:14 +00:00
|
|
|
{
|
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio2;
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
int error, ilen;
|
2001-08-16 06:00:57 +00:00
|
|
|
char *ibuf;
|
2001-05-22 22:13:14 +00:00
|
|
|
|
2001-08-16 06:00:57 +00:00
|
|
|
error = 0;
|
|
|
|
ibuf = NULL;
|
2004-11-05 18:32:14 +00:00
|
|
|
snp = ttytosnp(tp);
|
2001-06-03 05:17:39 +00:00
|
|
|
while (uio->uio_resid > 0) {
|
2001-08-16 06:00:57 +00:00
|
|
|
ilen = imin(512, uio->uio_resid);
|
2003-02-19 05:47:46 +00:00
|
|
|
ibuf = malloc(ilen, M_SNP, M_WAITOK);
|
2001-05-22 22:13:14 +00:00
|
|
|
error = uiomove(ibuf, ilen, uio);
|
2001-06-05 05:00:17 +00:00
|
|
|
if (error != 0)
|
2001-08-16 06:00:57 +00:00
|
|
|
break;
|
2001-06-05 05:00:17 +00:00
|
|
|
snp_in(snp, ibuf, ilen);
|
|
|
|
/* Hackish, but probably the least of all evils. */
|
2001-05-22 22:13:14 +00:00
|
|
|
iov.iov_base = ibuf;
|
|
|
|
iov.iov_len = ilen;
|
|
|
|
uio2.uio_iov = &iov;
|
|
|
|
uio2.uio_iovcnt = 1;
|
|
|
|
uio2.uio_offset = 0;
|
|
|
|
uio2.uio_resid = ilen;
|
|
|
|
uio2.uio_segflg = UIO_SYSSPACE;
|
|
|
|
uio2.uio_rw = UIO_WRITE;
|
2001-09-12 08:38:13 +00:00
|
|
|
uio2.uio_td = uio->uio_td;
|
2001-05-22 22:13:14 +00:00
|
|
|
error = ttwrite(tp, &uio2, flag);
|
2001-06-05 05:00:17 +00:00
|
|
|
if (error != 0)
|
2001-08-16 06:00:57 +00:00
|
|
|
break;
|
|
|
|
free(ibuf, M_SNP);
|
|
|
|
ibuf = NULL;
|
2001-05-22 22:13:14 +00:00
|
|
|
}
|
2001-08-16 06:00:57 +00:00
|
|
|
if (ibuf != NULL)
|
|
|
|
free(ibuf, M_SNP);
|
|
|
|
return (error);
|
2001-05-22 22:13:14 +00:00
|
|
|
}
|
|
|
|
|
1995-02-25 20:09:44 +00:00
|
|
|
static struct tty *
|
2007-03-23 22:48:44 +00:00
|
|
|
snpdevtotty(struct cdev *dev)
|
1995-02-25 20:09:44 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct cdevsw *cdp;
|
2004-09-24 08:12:41 +00:00
|
|
|
struct tty *tp;
|
1997-03-16 19:11:40 +00:00
|
|
|
|
2004-09-24 08:12:41 +00:00
|
|
|
cdp = dev_refthread(dev);
|
|
|
|
if (cdp == NULL)
|
2001-06-03 05:17:39 +00:00
|
|
|
return (NULL);
|
2004-09-24 08:12:41 +00:00
|
|
|
if (!(cdp->d_flags & D_TTY))
|
|
|
|
tp = NULL;
|
|
|
|
else
|
|
|
|
tp = dev->si_tty;
|
|
|
|
dev_relthread(dev);
|
|
|
|
return (tp);
|
1995-02-25 20:09:44 +00:00
|
|
|
}
|
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
#define SNP_INPUT_BUF 5 /* This is even too much, the maximal
|
1995-02-27 19:47:53 +00:00
|
|
|
* interactive mode write is 3 bytes
|
|
|
|
* length for function keys...
|
|
|
|
*/
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2007-03-23 22:48:44 +00:00
|
|
|
snpwrite(struct cdev *dev, struct uio *uio, int flag)
|
1995-02-27 19:47:53 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
struct tty *tp;
|
|
|
|
int error, i, len;
|
2002-04-10 03:51:49 +00:00
|
|
|
unsigned char c[SNP_INPUT_BUF];
|
1995-02-27 19:47:53 +00:00
|
|
|
|
2008-08-15 13:07:07 +00:00
|
|
|
error = devfs_get_cdevpriv((void **)&snp);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
tp = snp->snp_tty;
|
2001-06-05 05:00:17 +00:00
|
|
|
if (tp == NULL)
|
|
|
|
return (EIO);
|
2004-11-05 18:32:14 +00:00
|
|
|
if ((tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc)
|
1995-02-27 19:47:53 +00:00
|
|
|
goto tty_input;
|
|
|
|
|
2008-08-15 13:07:07 +00:00
|
|
|
printf("snp: attempt to write to bad tty\n");
|
1995-02-27 19:47:53 +00:00
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
tty_input:
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!(tp->t_state & TS_ISOPEN))
|
1995-02-27 19:47:53 +00:00
|
|
|
return (EIO);
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
while (uio->uio_resid > 0) {
|
2001-06-03 05:17:39 +00:00
|
|
|
len = imin(uio->uio_resid, SNP_INPUT_BUF);
|
1995-02-27 19:47:53 +00:00
|
|
|
if ((error = uiomove(c, len, uio)) != 0)
|
|
|
|
return (error);
|
2000-04-02 00:35:37 +00:00
|
|
|
for (i=0; i < len; i++) {
|
|
|
|
if (ttyinput(c[i], tp))
|
1995-02-27 19:47:53 +00:00
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
}
|
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
|
2007-03-23 22:48:44 +00:00
|
|
|
snpread(struct cdev *dev, struct uio *uio, int flag)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
int error, len, n, nblen, s;
|
|
|
|
caddr_t from;
|
|
|
|
char *nbuf;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
2008-08-15 13:07:07 +00:00
|
|
|
error = devfs_get_cdevpriv((void **)&snp);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
1999-01-08 17:31:30 +00:00
|
|
|
KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
|
1999-01-10 01:58:29 +00:00
|
|
|
("snoop buffer error"));
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
if (snp->snp_tty == NULL)
|
1995-02-14 21:21:26 +00:00
|
|
|
return (EIO);
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_RWAIT;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
do {
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_len == 0) {
|
2004-12-22 17:30:38 +00:00
|
|
|
if (flag & O_NONBLOCK)
|
1997-11-18 16:12:51 +00:00
|
|
|
return (EWOULDBLOCK);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags |= SNOOP_RWAIT;
|
2003-03-02 16:54:40 +00:00
|
|
|
error = tsleep(snp, (PZERO + 1) | PCATCH,
|
2001-11-24 15:59:46 +00:00
|
|
|
"snprd", 0);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
} while (snp->snp_len == 0);
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
n = snp->snp_len;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
error = 0;
|
1995-02-15 16:35:38 +00:00
|
|
|
while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
|
2001-06-03 05:17:39 +00:00
|
|
|
len = min((unsigned)uio->uio_resid, snp->snp_len);
|
2000-04-02 00:35:37 +00:00
|
|
|
from = (caddr_t)(snp->snp_buf + snp->snp_base);
|
1995-02-14 21:21:26 +00:00
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
error = uiomove(from, len, uio);
|
|
|
|
snp->snp_base += len;
|
|
|
|
snp->snp_len -= len;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
1995-02-15 16:35:38 +00:00
|
|
|
if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
|
1995-02-14 21:21:26 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_OFLOW;
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
|
|
|
s = spltty();
|
|
|
|
nblen = snp->snp_blen;
|
|
|
|
if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
|
2001-06-05 05:00:17 +00:00
|
|
|
while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
|
1995-02-15 16:35:38 +00:00
|
|
|
nblen = nblen / 2;
|
2000-04-02 00:35:37 +00:00
|
|
|
if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
|
1995-02-15 16:35:38 +00:00
|
|
|
bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
|
2000-04-02 00:35:37 +00:00
|
|
|
free(snp->snp_buf, M_SNP);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_buf = nbuf;
|
|
|
|
snp->snp_blen = nblen;
|
|
|
|
snp->snp_base = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
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
|
2007-03-23 22:48:44 +00:00
|
|
|
snp_in(struct snoop *snp, char *buf, int n)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
int s_free, s_tail;
|
|
|
|
int s, len, nblen;
|
|
|
|
caddr_t from, to;
|
|
|
|
char *nbuf;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1999-01-10 01:58:29 +00:00
|
|
|
KASSERT(n >= 0, ("negative snoop char count"));
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (n == 0)
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_flags & SNOOP_DOWN) {
|
2008-08-15 13:07:07 +00:00
|
|
|
printf("snp: more data to down interface\n");
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
1995-02-15 18:41:57 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_flags & SNOOP_OFLOW) {
|
2008-08-15 13:07:07 +00:00
|
|
|
printf("snp: buffer overflow\n");
|
1995-02-15 16:35:38 +00:00
|
|
|
/*
|
|
|
|
* On overflow we just repeat the standart close
|
|
|
|
* procedure...yes , this is waste of space but.. Then next
|
|
|
|
* read from device will fail if one would recall he is
|
|
|
|
* snooping and retry...
|
|
|
|
*/
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
return (snp_down(snp));
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
|
|
|
s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
|
|
|
|
s_free = snp->snp_blen - snp->snp_len;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (n > s_free) {
|
1995-02-15 16:35:38 +00:00
|
|
|
s = spltty();
|
|
|
|
nblen = snp->snp_blen;
|
|
|
|
while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
|
|
|
|
nblen = snp->snp_blen * 2;
|
|
|
|
s_free = nblen - (snp->snp_len + snp->snp_base);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
2000-04-02 00:35:37 +00:00
|
|
|
if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
|
1995-02-15 16:35:38 +00:00
|
|
|
bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
|
2000-04-02 00:35:37 +00:00
|
|
|
free(snp->snp_buf, M_SNP);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_buf = nbuf;
|
|
|
|
snp->snp_blen = nblen;
|
|
|
|
snp->snp_base = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
} else {
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags |= SNOOP_OFLOW;
|
1995-02-14 21:21:26 +00:00
|
|
|
if (snp->snp_flags & SNOOP_RWAIT) {
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_RWAIT;
|
2003-03-02 16:54:40 +00:00
|
|
|
wakeup(snp);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
splx(s);
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
if (n > s_tail) {
|
2000-04-02 00:35:37 +00:00
|
|
|
from = (caddr_t)(snp->snp_buf + snp->snp_base);
|
|
|
|
to = (caddr_t)(snp->snp_buf);
|
1995-02-15 16:35:38 +00:00
|
|
|
len = snp->snp_len;
|
|
|
|
bcopy(from, to, len);
|
|
|
|
snp->snp_base = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
2000-04-02 00:35:37 +00:00
|
|
|
to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
|
1995-02-15 16:35:38 +00:00
|
|
|
bcopy(buf, to, n);
|
|
|
|
snp->snp_len += n;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
if (snp->snp_flags & SNOOP_RWAIT) {
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_RWAIT;
|
2003-03-02 16:54:40 +00:00
|
|
|
wakeup(snp);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&snp->snp_sel, PZERO + 1);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
return (n);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2008-08-15 13:07:07 +00:00
|
|
|
static void
|
|
|
|
snp_dtor(void *data)
|
|
|
|
{
|
|
|
|
struct snoop *snp = data;
|
|
|
|
|
|
|
|
snp->snp_blen = 0;
|
|
|
|
LIST_REMOVE(snp, snp_list);
|
|
|
|
free(snp->snp_buf, M_SNP);
|
|
|
|
snp->snp_flags &= ~SNOOP_OPEN;
|
|
|
|
snp_detach(snp);
|
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2007-03-23 22:48:44 +00:00
|
|
|
snpopen(struct cdev *dev, int flag, int mode, struct thread *td)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
2008-08-15 13:07:07 +00:00
|
|
|
int error;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2008-08-15 13:07:07 +00:00
|
|
|
snp = malloc(sizeof(*snp), M_SNP, M_WAITOK | M_ZERO);
|
|
|
|
error = devfs_set_cdevpriv(snp, snp_dtor);
|
|
|
|
if (error != 0) {
|
|
|
|
free(snp, M_SNP);
|
|
|
|
return (error);
|
|
|
|
}
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
/*
|
2000-04-02 00:35:37 +00:00
|
|
|
* We intentionally do not OR flags with SNOOP_OPEN, but set them so
|
1995-02-15 16:35:38 +00:00
|
|
|
* all previous settings (especially SNOOP_OFLOW) will be cleared.
|
|
|
|
*/
|
|
|
|
snp->snp_flags = SNOOP_OPEN;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_blen = SNOOP_MINLEN;
|
|
|
|
snp->snp_base = 0;
|
|
|
|
snp->snp_len = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
/*
|
1995-02-27 19:47:53 +00:00
|
|
|
* snp_tty == NULL is for inactive snoop devices.
|
1995-02-14 21:21:26 +00:00
|
|
|
*/
|
1995-02-27 19:47:53 +00:00
|
|
|
snp->snp_tty = NULL;
|
2004-06-17 17:16:53 +00:00
|
|
|
snp->snp_target = NULL;
|
2001-07-18 13:39:43 +00:00
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
|
1995-02-14 21:21:26 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
|
2007-07-05 13:07:12 +00:00
|
|
|
static void
|
|
|
|
snp_detach(void *arg)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2007-07-05 13:07:12 +00:00
|
|
|
struct snoop *snp;
|
2001-06-05 05:00:17 +00:00
|
|
|
struct tty *tp;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
2007-07-05 13:07:12 +00:00
|
|
|
snp = (struct snoop *)arg;
|
1995-02-14 21:21:26 +00:00
|
|
|
snp->snp_base = 0;
|
|
|
|
snp->snp_len = 0;
|
|
|
|
|
|
|
|
/*
|
2000-04-02 00:35:37 +00:00
|
|
|
* If line disc. changed we do not touch this pointer, SLIP/PPP will
|
1995-02-15 16:35:38 +00:00
|
|
|
* change it anyway.
|
1995-02-14 21:21:26 +00:00
|
|
|
*/
|
1995-02-27 19:47:53 +00:00
|
|
|
tp = snp->snp_tty;
|
2001-06-05 05:00:17 +00:00
|
|
|
if (tp == NULL)
|
|
|
|
goto detach_notty;
|
1995-02-27 19:47:53 +00:00
|
|
|
|
2004-11-05 18:32:14 +00:00
|
|
|
if ((tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc) {
|
1995-02-15 16:35:38 +00:00
|
|
|
tp->t_state &= ~TS_SNOOP;
|
2001-05-22 22:13:14 +00:00
|
|
|
tp->t_line = snp->snp_olddisc;
|
1995-02-15 16:35:38 +00:00
|
|
|
} else
|
2008-08-15 13:07:07 +00:00
|
|
|
printf("snp: bad attached tty data\n");
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
snp->snp_tty = NULL;
|
2004-06-17 17:16:53 +00:00
|
|
|
snp->snp_target = NULL;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
detach_notty:
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&snp->snp_sel, PZERO + 1);
|
2000-04-02 00:35:37 +00:00
|
|
|
if ((snp->snp_flags & SNOOP_OPEN) == 0)
|
|
|
|
free(snp, M_SNP);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2007-03-23 22:48:44 +00:00
|
|
|
snp_down(struct snoop *snp)
|
1995-02-15 18:41:57 +00:00
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
|
|
|
|
if (snp->snp_blen != SNOOP_MINLEN) {
|
|
|
|
free(snp->snp_buf, M_SNP);
|
2003-02-19 05:47:46 +00:00
|
|
|
snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
|
2000-04-02 00:35:37 +00:00
|
|
|
snp->snp_blen = SNOOP_MINLEN;
|
|
|
|
}
|
1995-02-15 18:41:57 +00:00
|
|
|
snp->snp_flags |= SNOOP_DOWN;
|
2007-07-05 13:07:12 +00:00
|
|
|
snp_detach(snp);
|
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
|
2007-03-23 22:48:44 +00:00
|
|
|
snpioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
|
|
|
|
struct thread *td)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
2007-12-24 13:47:16 +00:00
|
|
|
struct tty *tp;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *tdev;
|
2005-09-18 19:23:35 +00:00
|
|
|
struct file *fp;
|
2008-08-15 13:07:07 +00:00
|
|
|
int error, s;
|
|
|
|
|
|
|
|
error = devfs_get_cdevpriv((void **)&snp);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SNPSTTY:
|
2005-09-18 19:23:35 +00:00
|
|
|
s = *(int *)data;
|
2005-09-19 13:48:45 +00:00
|
|
|
if (s < 0)
|
|
|
|
return (snp_down(snp));
|
2007-12-03 14:02:27 +00:00
|
|
|
|
2005-09-19 13:48:45 +00:00
|
|
|
if (fget(td, s, &fp) != 0)
|
2005-09-18 19:23:35 +00:00
|
|
|
return (EINVAL);
|
|
|
|
if (fp->f_type != DTYPE_VNODE ||
|
2005-09-19 13:48:45 +00:00
|
|
|
fp->f_vnode->v_type != VCHR ||
|
|
|
|
fp->f_vnode->v_rdev == NULL) {
|
2005-09-18 19:23:35 +00:00
|
|
|
fdrop(fp, td);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
tdev = fp->f_vnode->v_rdev;
|
|
|
|
fdrop(fp, td);
|
1995-02-15 18:41:57 +00:00
|
|
|
|
2007-12-24 13:47:16 +00:00
|
|
|
if (snp->snp_tty != NULL)
|
|
|
|
return (EBUSY);
|
|
|
|
|
1997-03-16 19:11:40 +00:00
|
|
|
tp = snpdevtotty(tdev);
|
1995-02-25 20:09:44 +00:00
|
|
|
if (!tp)
|
1995-02-14 21:21:26 +00:00
|
|
|
return (EINVAL);
|
2001-11-24 15:34:18 +00:00
|
|
|
if (tp->t_state & TS_SNOOP)
|
|
|
|
return (EBUSY);
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
s = spltty();
|
1995-02-14 21:21:26 +00:00
|
|
|
tp->t_state |= TS_SNOOP;
|
2001-05-22 22:13:14 +00:00
|
|
|
snp->snp_olddisc = tp->t_line;
|
2001-06-05 05:00:17 +00:00
|
|
|
tp->t_line = snooplinedisc;
|
1995-02-27 19:47:53 +00:00
|
|
|
snp->snp_tty = tp;
|
1995-02-25 20:09:44 +00:00
|
|
|
snp->snp_target = tdev;
|
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
/*
|
|
|
|
* Clean overflow and down flags -
|
|
|
|
* we'll have a chance to get them in the future :)))
|
|
|
|
*/
|
|
|
|
snp->snp_flags &= ~SNOOP_OFLOW;
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_DOWN;
|
1995-02-14 21:21:26 +00:00
|
|
|
splx(s);
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case SNPGTTY:
|
1995-02-27 19:47:53 +00:00
|
|
|
/*
|
|
|
|
* We keep snp_target field specially to make
|
2000-04-02 00:35:37 +00:00
|
|
|
* SNPGTTY happy, else we can't know what is device
|
1995-02-27 19:47:53 +00:00
|
|
|
* major/minor for tty.
|
|
|
|
*/
|
2004-06-17 17:16:53 +00:00
|
|
|
*((dev_t *)data) = dev2udev(snp->snp_target);
|
1995-02-14 21:21:26 +00:00
|
|
|
break;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case FIONBIO:
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case FIOASYNC:
|
2000-04-02 00:35:37 +00:00
|
|
|
if (*(int *)data)
|
1995-02-14 21:21:26 +00:00
|
|
|
snp->snp_flags |= SNOOP_ASYNC;
|
|
|
|
else
|
|
|
|
snp->snp_flags &= ~SNOOP_ASYNC;
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
case FIONREAD:
|
|
|
|
s = spltty();
|
1995-02-27 19:47:53 +00:00
|
|
|
if (snp->snp_tty != NULL)
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = snp->snp_len;
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1995-02-25 20:09:44 +00:00
|
|
|
if (snp->snp_flags & SNOOP_DOWN) {
|
|
|
|
if (snp->snp_flags & SNOOP_OFLOW)
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = SNP_OFLOW;
|
1995-02-15 18:41:57 +00:00
|
|
|
else
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = SNP_TTYCLOSE;
|
1995-02-15 18:41:57 +00:00
|
|
|
} else {
|
2000-04-02 00:35:37 +00:00
|
|
|
*(int *)data = SNP_DETACH;
|
1995-02-15 18:41:57 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
splx(s);
|
|
|
|
break;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
default:
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
static int
|
2007-03-23 22:48:44 +00:00
|
|
|
snppoll(struct cdev *dev, int events, struct thread *td)
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2001-06-05 05:00:17 +00:00
|
|
|
struct snoop *snp;
|
|
|
|
int revents;
|
1995-02-25 20:09:44 +00:00
|
|
|
|
2008-08-15 13:07:07 +00:00
|
|
|
if (devfs_get_cdevpriv((void **)&snp) != 0)
|
|
|
|
return (events &
|
|
|
|
(POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
|
|
|
|
|
2001-06-05 05:00:17 +00:00
|
|
|
revents = 0;
|
1995-02-15 16:35:38 +00:00
|
|
|
/*
|
2000-04-02 00:35:37 +00:00
|
|
|
* If snoop is down, we don't want to poll() forever so we return 1.
|
|
|
|
* Caller should see if we down via FIONREAD ioctl(). The last should
|
1995-02-15 16:35:38 +00:00
|
|
|
* return -1 to indicate down state.
|
|
|
|
*/
|
1999-05-06 18:13:11 +00:00
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
1997-09-14 02:42:03 +00:00
|
|
|
if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
selrecord(td, &snp->snp_sel);
|
1999-05-06 18:13:11 +00:00
|
|
|
}
|
1997-09-14 02:42:03 +00:00
|
|
|
return (revents);
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
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:
|
2001-06-05 05:00:17 +00:00
|
|
|
snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
|
2008-08-15 13:07:07 +00:00
|
|
|
snoopdev = make_dev(&snp_cdevsw, 0, UID_ROOT, GID_WHEEL,
|
|
|
|
0600, "snp");
|
|
|
|
/* For compatibility */
|
|
|
|
make_dev_alias(snoopdev, "snp0");
|
2001-05-22 22:13:14 +00:00
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
2001-07-18 13:39:43 +00:00
|
|
|
if (!LIST_EMPTY(&snp_sclist))
|
|
|
|
return (EBUSY);
|
2008-08-15 13:07:07 +00:00
|
|
|
destroy_dev(snoopdev);
|
2001-06-05 05:00:17 +00:00
|
|
|
ldisc_deregister(snooplinedisc);
|
2001-05-22 22:13:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
2004-07-15 08:26:07 +00:00
|
|
|
return (EOPNOTSUPP);
|
2001-05-22 22:13:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-06-05 05:00:17 +00:00
|
|
|
return (0);
|
1995-11-29 10:49:16 +00:00
|
|
|
}
|
|
|
|
|
2001-05-22 22:13:14 +00:00
|
|
|
static moduledata_t snp_mod = {
|
|
|
|
"snp",
|
|
|
|
snp_modevent,
|
|
|
|
NULL
|
|
|
|
};
|
2004-02-21 20:29:52 +00:00
|
|
|
DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|