1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-25 11:37:56 +00:00
freebsd/sys/kern/tty_snoop.c

517 lines
10 KiB
C
Raw Normal View History

/*
* 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$
*/
#include "opt_compat.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/filio.h>
#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
#include <sys/ioctl_compat.h>
#endif
#include <sys/malloc.h>
#include <sys/tty.h>
#include <sys/conf.h>
1997-09-14 02:42:03 +00:00
#include <sys/poll.h>
#include <sys/kernel.h>
#include <sys/snoop.h>
#include <sys/vnode.h>
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;
#define CDEV_MAJOR 53
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
};
#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");
static struct tty *snpdevtotty __P((dev_t dev));
static int snp_detach __P((struct snoop *snp));
static struct tty *
snpdevtotty (dev)
dev_t dev;
{
struct cdevsw *cdp;
cdp = devsw(dev);
if (cdp && cdp->d_flags & D_TTY)
return (dev->si_tty);
return (NULL);
}
#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...
*/
static int
1995-02-27 19:47:53 +00:00
snpwrite(dev, uio, flag)
dev_t dev;
struct uio *uio;
int flag;
{
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) {
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);
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
}
static int
snpread(dev, uio, flag)
dev_t dev;
struct uio *uio;
int flag;
{
struct snoop *snp = dev->si_drv1;
int len, n, nblen, s, error = 0;
caddr_t from;
char *nbuf;
KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
1999-01-10 01:58:29 +00:00
("snoop buffer error"));
1995-02-27 19:47:53 +00:00
if (snp->snp_tty == NULL)
return (EIO);
snp->snp_flags &= ~SNOOP_RWAIT;
do {
if (snp->snp_len == 0) {
if (flag & IO_NDELAY)
return (EWOULDBLOCK);
snp->snp_flags |= SNOOP_RWAIT;
tsleep((caddr_t)snp, (PZERO + 1) | PCATCH, "snprd", 0);
}
} while (snp->snp_len == 0);
n = snp->snp_len;
while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
len = MIN(uio->uio_resid, snp->snp_len);
from = (caddr_t)(snp->snp_buf + snp->snp_base);
if (len == 0)
break;
error = uiomove(from, len, uio);
snp->snp_base += len;
snp->snp_len -= len;
}
if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
snp->snp_flags &= ~SNOOP_OFLOW;
}
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;
if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
free(snp->snp_buf, M_SNP);
snp->snp_buf = nbuf;
snp->snp_blen = nblen;
snp->snp_base = 0;
}
}
splx(s);
return error;
}
1995-02-27 19:47:53 +00:00
int
snpinc(struct snoop *snp, char c)
1995-02-27 19:47:53 +00:00
{
char buf;
1995-02-27 19:47:53 +00:00
buf = c;
return (snpin(snp, &buf, 1));
1995-02-27 19:47:53 +00:00
}
int
snpin(snp, buf, n)
struct snoop *snp;
char *buf;
int n;
{
int s_free, s_tail;
int s, len, nblen;
caddr_t from, to;
char *nbuf;
1999-01-10 01:58:29 +00:00
KASSERT(n >= 0, ("negative snoop char count"));
if (n == 0)
return 0;
if (snp->snp_flags & SNOOP_DOWN) {
printf("Snoop: more data to down interface.\n");
return 0;
}
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...
*/
return (snpdown(snp));
}
s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
s_free = snp->snp_blen - snp->snp_len;
if (n > s_free) {
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);
}
if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
free(snp->snp_buf, M_SNP);
snp->snp_buf = nbuf;
snp->snp_blen = nblen;
snp->snp_base = 0;
} else {
snp->snp_flags |= SNOOP_OFLOW;
if (snp->snp_flags & SNOOP_RWAIT) {
snp->snp_flags &= ~SNOOP_RWAIT;
wakeup((caddr_t)snp);
}
splx(s);
return 0;
}
splx(s);
}
if (n > s_tail) {
from = (caddr_t)(snp->snp_buf + snp->snp_base);
to = (caddr_t)(snp->snp_buf);
len = snp->snp_len;
bcopy(from, to, len);
snp->snp_base = 0;
}
to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
bcopy(buf, to, n);
snp->snp_len += n;
if (snp->snp_flags & SNOOP_RWAIT) {
snp->snp_flags &= ~SNOOP_RWAIT;
wakeup((caddr_t)snp);
}
selwakeup(&snp->snp_sel);
snp->snp_sel.si_pid = 0;
return n;
}
static int
snpopen(dev, flag, mode, p)
dev_t dev;
int flag, mode;
struct proc *p;
{
struct snoop *snp;
int error;
if ((error = suser(p)) != 0)
return (error);
if (dev->si_drv1 == NULL) {
int mynor = minor(dev);
dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP, M_WAITOK|M_ZERO);
make_dev(&snp_cdevsw, mynor, 0, 0, 0600, "snp%d", mynor);
} else
return (EBUSY);
/*
* We intentionally do not OR flags with SNOOP_OPEN, but set them so
* all previous settings (especially SNOOP_OFLOW) will be cleared.
*/
snp->snp_flags = SNOOP_OPEN;
snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
snp->snp_blen = SNOOP_MINLEN;
snp->snp_base = 0;
snp->snp_len = 0;
/*
1995-02-27 19:47:53 +00:00
* snp_tty == NULL is for inactive snoop devices.
*/
1995-02-27 19:47:53 +00:00
snp->snp_tty = NULL;
1999-05-10 18:10:08 +00:00
snp->snp_target = NODEV;
return (0);
}
static int
snp_detach(snp)
struct snoop *snp;
{
struct tty *tp;
snp->snp_base = 0;
snp->snp_len = 0;
/*
* If line disc. changed we do not touch this pointer, SLIP/PPP will
* change it anyway.
*/
1995-02-27 19:47:53 +00:00
if (snp->snp_tty == NULL)
goto detach_notty;
1995-02-27 19:47:53 +00:00
tp = snp->snp_tty;
if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) &&
(tp->t_line == OTTYDISC || tp->t_line == NTTYDISC)) {
tp->t_sc = NULL;
tp->t_state &= ~TS_SNOOP;
} else
printf("Snoop: bad attached tty data.\n");
1995-02-27 19:47:53 +00:00
snp->snp_tty = NULL;
1999-05-10 18:10:08 +00:00
snp->snp_target = NODEV;
detach_notty:
selwakeup(&snp->snp_sel);
snp->snp_sel.si_pid = 0;
if ((snp->snp_flags & SNOOP_OPEN) == 0)
free(snp, M_SNP);
return (0);
}
static int
snpclose(dev, flags, fmt, p)
dev_t dev;
int flags;
int fmt;
struct proc *p;
{
struct snoop *snp = dev->si_drv1;
snp->snp_blen = 0;
free(snp->snp_buf, M_SNP);
snp->snp_flags &= ~SNOOP_OPEN;
dev->si_drv1 = NULL;
return (snp_detach(snp));
}
int
snpdown(snp)
struct snoop *snp;
{
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;
}
snp->snp_flags |= SNOOP_DOWN;
return (snp_detach(snp));
}
static int
snpioctl(dev, cmd, data, flags, p)
dev_t dev;
u_long cmd;
caddr_t data;
int flags;
struct proc *p;
{
dev_t tdev;
struct snoop *snp = dev->si_drv1;
struct tty *tp, *tpo;
int s;
switch (cmd) {
case SNPSTTY:
tdev = udev2dev(*((udev_t *)data), 0);
1999-05-10 18:10:08 +00:00
if (tdev == NODEV)
return (snpdown(snp));
tp = snpdevtotty(tdev);
if (!tp)
return (EINVAL);
if ((tp->t_sc != (caddr_t)snp) && (tp->t_state & TS_SNOOP))
return (EBUSY);
if ((tp->t_line != OTTYDISC) && (tp->t_line != NTTYDISC))
return (EBUSY);
s = spltty();
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;
if (tpo)
tpo->t_state &= ~TS_SNOOP;
}
tp->t_sc = (caddr_t)snp;
tp->t_state |= TS_SNOOP;
1995-02-27 19:47:53 +00:00
snp->snp_tty = tp;
snp->snp_target = tdev;
/*
* Clean overflow and down flags -
* we'll have a chance to get them in the future :)))
*/
snp->snp_flags &= ~SNOOP_OFLOW;
snp->snp_flags &= ~SNOOP_DOWN;
splx(s);
break;
case SNPGTTY:
1995-02-27 19:47:53 +00:00
/*
* We keep snp_target field specially to make
* SNPGTTY happy, else we can't know what is device
1995-02-27 19:47:53 +00:00
* major/minor for tty.
*/
*((dev_t *)data) = snp->snp_target;
break;
case FIONBIO:
break;
case FIOASYNC:
if (*(int *)data)
snp->snp_flags |= SNOOP_ASYNC;
else
snp->snp_flags &= ~SNOOP_ASYNC;
break;
case FIONREAD:
s = spltty();
1995-02-27 19:47:53 +00:00
if (snp->snp_tty != NULL)
*(int *)data = snp->snp_len;
1995-05-30 08:16:23 +00:00
else
if (snp->snp_flags & SNOOP_DOWN) {
if (snp->snp_flags & SNOOP_OFLOW)
*(int *)data = SNP_OFLOW;
else
*(int *)data = SNP_TTYCLOSE;
} else {
*(int *)data = SNP_DETACH;
}
splx(s);
break;
default:
return (ENOTTY);
}
return (0);
}
static int
1997-09-14 02:42:03 +00:00
snppoll(dev, events, p)
dev_t dev;
1997-09-14 02:42:03 +00:00
int events;
struct proc *p;
{
struct snoop *snp = dev->si_drv1;
1997-09-14 02:42:03 +00:00
int revents = 0;
/*
* 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
* return -1 to indicate down state.
*/
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);
}
1997-09-14 02:42:03 +00:00
return (revents);
}
1997-09-16 11:44:05 +00:00
static void snp_drvinit __P((void *unused));
static void
1997-09-16 11:44:05 +00:00
snp_drvinit(unused)
void *unused;
{
cdevsw_add(&snp_cdevsw);
}
SYSINIT(snpdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,snp_drvinit,NULL)