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
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1995-02-14 21:21:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "snp.h"
|
1997-12-16 17:40:42 +00:00
|
|
|
#include "opt_compat.h"
|
1995-02-14 21:21:26 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1997-03-24 11:37:53 +00:00
|
|
|
#include <sys/filio.h>
|
1997-11-18 16:12:51 +00:00
|
|
|
#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
|
|
|
|
#include <sys/ioctl_compat.h>
|
|
|
|
#endif
|
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>
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <sys/snoop.h>
|
1997-11-18 16:12:51 +00:00
|
|
|
#include <sys/vnode.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
|
|
|
|
static d_open_t snpopen;
|
|
|
|
static d_close_t snpclose;
|
|
|
|
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
|
|
|
|
1995-11-29 10:49:16 +00:00
|
|
|
#define CDEV_MAJOR 53
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw snp_cdevsw = {
|
|
|
|
/* open */ snpopen,
|
|
|
|
/* close */ snpclose,
|
|
|
|
/* read */ snpread,
|
|
|
|
/* write */ snpwrite,
|
|
|
|
/* ioctl */ snpioctl,
|
|
|
|
/* poll */ snppoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
|
|
|
/* name */ "snp",
|
|
|
|
/* maj */ CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
|
|
|
/* flags */ 0,
|
|
|
|
/* bmaj */ -1
|
|
|
|
};
|
1995-12-08 11:19:42 +00:00
|
|
|
|
1995-11-29 10:49:16 +00:00
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
2000-12-08 20:09:00 +00:00
|
|
|
static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1997-03-16 19:11:40 +00:00
|
|
|
static struct tty *snpdevtotty __P((dev_t dev));
|
1995-12-14 08:32:45 +00:00
|
|
|
static int snp_detach __P((struct snoop *snp));
|
1995-12-02 18:58:56 +00:00
|
|
|
|
1995-02-25 20:09:44 +00:00
|
|
|
static struct tty *
|
1997-03-16 19:11:40 +00:00
|
|
|
snpdevtotty (dev)
|
1995-02-25 20:09:44 +00:00
|
|
|
dev_t dev;
|
|
|
|
{
|
1997-03-16 19:11:40 +00:00
|
|
|
struct cdevsw *cdp;
|
|
|
|
|
1999-05-08 06:40:31 +00:00
|
|
|
cdp = devsw(dev);
|
1999-09-25 16:21:39 +00:00
|
|
|
if (cdp && cdp->d_flags & D_TTY)
|
|
|
|
return (dev->si_tty);
|
|
|
|
return (NULL);
|
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...
|
|
|
|
*/
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1995-02-27 19:47:53 +00:00
|
|
|
snpwrite(dev, uio, flag)
|
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
|
|
|
int flag;
|
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
int len, i, error;
|
|
|
|
struct snoop *snp = dev->si_drv1;
|
1995-02-27 19:47:53 +00:00
|
|
|
struct tty *tp;
|
1995-05-30 08:16:23 +00:00
|
|
|
char c[SNP_INPUT_BUF];
|
1995-02-27 19:47:53 +00:00
|
|
|
|
|
|
|
if (snp->snp_tty == NULL)
|
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
tp = snp->snp_tty;
|
|
|
|
|
|
|
|
if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
|
1995-05-30 08:16:23 +00:00
|
|
|
(tp->t_line == OTTYDISC || tp->t_line == NTTYDISC))
|
1995-02-27 19:47:53 +00:00
|
|
|
goto tty_input;
|
|
|
|
|
|
|
|
printf("Snoop: attempt to write to bad tty.\n");
|
|
|
|
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) {
|
2000-04-02 00:35:37 +00:00
|
|
|
len = MIN(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1995-02-14 21:21:26 +00:00
|
|
|
snpread(dev, uio, flag)
|
1995-02-15 16:35:38 +00:00
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
|
|
|
int flag;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
struct snoop *snp = dev->si_drv1;
|
|
|
|
int len, n, nblen, s, error = 0;
|
1995-02-15 16:35:38 +00:00
|
|
|
caddr_t from;
|
|
|
|
char *nbuf;
|
|
|
|
|
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) {
|
1997-11-18 16:12:51 +00:00
|
|
|
if (flag & IO_NDELAY)
|
|
|
|
return (EWOULDBLOCK);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags |= SNOOP_RWAIT;
|
2000-04-02 00:35:37 +00:00
|
|
|
tsleep((caddr_t)snp, (PZERO + 1) | PCATCH, "snprd", 0);
|
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
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
|
|
|
|
len = MIN(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) {
|
|
|
|
while (((nblen / 2) >= snp->snp_len) && ((nblen / 2) >= SNOOP_MINLEN))
|
|
|
|
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);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
int
|
1997-11-18 16:12:51 +00:00
|
|
|
snpinc(struct snoop *snp, char c)
|
1995-02-27 19:47:53 +00:00
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
char buf;
|
1995-02-27 19:47:53 +00:00
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
buf = c;
|
|
|
|
return (snpin(snp, &buf, 1));
|
1995-02-27 19:47:53 +00:00
|
|
|
}
|
|
|
|
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
int
|
1995-02-15 16:35:38 +00:00
|
|
|
snpin(snp, buf, n)
|
|
|
|
struct snoop *snp;
|
|
|
|
char *buf;
|
|
|
|
int n;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
1995-02-15 16:35:38 +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)
|
1995-02-14 21:21:26 +00:00
|
|
|
return 0;
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_flags & SNOOP_DOWN) {
|
|
|
|
printf("Snoop: more data to down interface.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
1995-02-15 18:41:57 +00:00
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
if (snp->snp_flags & SNOOP_OFLOW) {
|
|
|
|
printf("Snoop: buffer overflow.\n");
|
|
|
|
/*
|
|
|
|
* 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...
|
|
|
|
*/
|
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
return (snpdown(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;
|
2000-04-02 00:35:37 +00:00
|
|
|
wakeup((caddr_t)snp);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
splx(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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;
|
2000-04-02 00:35:37 +00:00
|
|
|
wakeup((caddr_t)snp);
|
1995-02-15 16:35:38 +00:00
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
selwakeup(&snp->snp_sel);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_sel.si_pid = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1995-02-14 21:21:26 +00:00
|
|
|
snpopen(dev, flag, mode, p)
|
1995-02-15 16:35:38 +00:00
|
|
|
dev_t dev;
|
|
|
|
int flag, mode;
|
|
|
|
struct proc *p;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
1995-02-15 16:35:38 +00:00
|
|
|
struct snoop *snp;
|
2000-04-02 00:35:37 +00:00
|
|
|
int error;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1999-04-27 11:18:52 +00:00
|
|
|
if ((error = suser(p)) != 0)
|
1995-02-14 21:21:26 +00:00
|
|
|
return (error);
|
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
if (dev->si_drv1 == NULL) {
|
|
|
|
int mynor = minor(dev);
|
1995-02-25 20:09:44 +00:00
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP, M_WAITOK);
|
|
|
|
bzero(snp, sizeof(*snp));
|
|
|
|
make_dev(&snp_cdevsw, mynor, 0, 0, 0600, "snp%d", mynor);
|
|
|
|
} else
|
|
|
|
return (EBUSY);
|
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
|
|
|
|
2000-04-02 00:35:37 +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;
|
1999-05-10 18:10:08 +00:00
|
|
|
snp->snp_target = NODEV;
|
1995-02-14 21:21:26 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-12-14 08:32:45 +00:00
|
|
|
static int
|
1995-02-15 16:35:38 +00:00
|
|
|
snp_detach(snp)
|
|
|
|
struct snoop *snp;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
1995-02-15 16:35:38 +00:00
|
|
|
struct tty *tp;
|
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
|
|
|
if (snp->snp_tty == NULL)
|
1995-02-15 18:41:57 +00:00
|
|
|
goto detach_notty;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-02-27 19:47:53 +00:00
|
|
|
tp = snp->snp_tty;
|
|
|
|
|
1995-02-25 20:09:44 +00:00
|
|
|
if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
|
1995-02-15 16:35:38 +00:00
|
|
|
(tp->t_line == OTTYDISC || tp->t_line == NTTYDISC)) {
|
1995-02-14 21:21:26 +00:00
|
|
|
tp->t_sc = NULL;
|
1995-02-15 16:35:38 +00:00
|
|
|
tp->t_state &= ~TS_SNOOP;
|
|
|
|
} else
|
|
|
|
printf("Snoop: 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;
|
1999-05-10 18:10:08 +00:00
|
|
|
snp->snp_target = NODEV;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
detach_notty:
|
1995-02-14 21:21:26 +00:00
|
|
|
selwakeup(&snp->snp_sel);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_sel.si_pid = 0;
|
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
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1995-09-08 11:09:15 +00:00
|
|
|
snpclose(dev, flags, fmt, p)
|
1995-02-15 16:35:38 +00:00
|
|
|
dev_t dev;
|
1995-09-08 11:09:15 +00:00
|
|
|
int flags;
|
|
|
|
int fmt;
|
|
|
|
struct proc *p;
|
1995-02-15 16:35:38 +00:00
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
struct snoop *snp = dev->si_drv1;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
|
|
|
snp->snp_blen = 0;
|
2000-04-02 00:35:37 +00:00
|
|
|
free(snp->snp_buf, M_SNP);
|
1995-02-15 16:35:38 +00:00
|
|
|
snp->snp_flags &= ~SNOOP_OPEN;
|
2000-04-02 00:35:37 +00:00
|
|
|
dev->si_drv1 = NULL;
|
1995-02-15 16:35:38 +00:00
|
|
|
|
|
|
|
return (snp_detach(snp));
|
|
|
|
}
|
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
int
|
|
|
|
snpdown(snp)
|
|
|
|
struct snoop *snp;
|
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
|
|
|
|
if (snp->snp_blen != SNOOP_MINLEN) {
|
|
|
|
free(snp->snp_buf, M_SNP);
|
|
|
|
snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
|
|
|
|
snp->snp_blen = SNOOP_MINLEN;
|
|
|
|
}
|
1995-02-15 18:41:57 +00:00
|
|
|
snp->snp_flags |= SNOOP_DOWN;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-15 18:41:57 +00:00
|
|
|
return (snp_detach(snp));
|
|
|
|
}
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1995-09-08 11:09:15 +00:00
|
|
|
snpioctl(dev, cmd, data, flags, p)
|
1995-02-15 16:35:38 +00:00
|
|
|
dev_t dev;
|
1998-06-07 17:13:14 +00:00
|
|
|
u_long cmd;
|
1995-02-15 16:35:38 +00:00
|
|
|
caddr_t data;
|
1995-09-08 11:09:15 +00:00
|
|
|
int flags;
|
|
|
|
struct proc *p;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
1995-02-25 20:09:44 +00:00
|
|
|
dev_t tdev;
|
2000-04-02 00:35:37 +00:00
|
|
|
struct snoop *snp = dev->si_drv1;
|
1995-02-15 16:35:38 +00:00
|
|
|
struct tty *tp, *tpo;
|
2000-04-02 00:35:37 +00:00
|
|
|
int s;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SNPSTTY:
|
2000-04-02 00:35:37 +00:00
|
|
|
tdev = udev2dev(*((udev_t *)data), 0);
|
1999-05-10 18:10:08 +00:00
|
|
|
if (tdev == NODEV)
|
1995-02-15 18:41:57 +00:00
|
|
|
return (snpdown(snp));
|
|
|
|
|
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);
|
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
if ((tp->t_sc != (caddr_t)snp) && (tp->t_state & TS_SNOOP))
|
1995-02-14 21:21:26 +00:00
|
|
|
return (EBUSY);
|
|
|
|
|
1995-02-25 20:09:44 +00:00
|
|
|
if ((tp->t_line != OTTYDISC) && (tp->t_line != NTTYDISC))
|
1995-02-14 21:21:26 +00:00
|
|
|
return (EBUSY);
|
|
|
|
|
1995-02-15 16:35:38 +00:00
|
|
|
s = spltty();
|
1995-02-25 20:09:44 +00:00
|
|
|
|
1999-05-10 18:10:08 +00:00
|
|
|
if (snp->snp_target == NODEV) {
|
1995-02-27 19:47:53 +00:00
|
|
|
tpo = snp->snp_tty;
|
1995-02-25 20:09:44 +00:00
|
|
|
if (tpo)
|
|
|
|
tpo->t_state &= ~TS_SNOOP;
|
1995-02-14 21:21:26 +00:00
|
|
|
}
|
1995-02-25 20:09:44 +00:00
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
tp->t_sc = (caddr_t)snp;
|
1995-02-14 21:21:26 +00:00
|
|
|
tp->t_state |= TS_SNOOP;
|
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.
|
|
|
|
*/
|
2000-04-02 00:35:37 +00:00
|
|
|
*((dev_t *)data) = 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1997-09-14 02:42:03 +00:00
|
|
|
snppoll(dev, events, p)
|
1995-02-15 16:35:38 +00:00
|
|
|
dev_t dev;
|
1997-09-14 02:42:03 +00:00
|
|
|
int events;
|
1995-02-15 16:35:38 +00:00
|
|
|
struct proc *p;
|
1995-02-14 21:21:26 +00:00
|
|
|
{
|
2000-04-02 00:35:37 +00:00
|
|
|
struct snoop *snp = dev->si_drv1;
|
1997-09-14 02:42:03 +00:00
|
|
|
int revents = 0;
|
1995-02-14 21:21:26 +00:00
|
|
|
|
1995-02-25 20:09:44 +00:00
|
|
|
|
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
|
|
|
|
selrecord(p, &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
|
|
|
}
|
|
|
|
|
1997-09-16 11:44:05 +00:00
|
|
|
static void snp_drvinit __P((void *unused));
|
1999-08-23 20:59:21 +00:00
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static void
|
1997-09-16 11:44:05 +00:00
|
|
|
snp_drvinit(unused)
|
|
|
|
void *unused;
|
1995-11-29 10:49:16 +00:00
|
|
|
{
|
|
|
|
|
2000-04-02 00:35:37 +00:00
|
|
|
cdevsw_add(&snp_cdevsw);
|
1995-11-29 10:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(snpdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,snp_drvinit,NULL)
|