2 * Copyright (c) 1995 Ugen J.S.Antsilevich
4 * Redistribution and use in source forms, with and without modification,
5 * are permitted provided that this entire comment appears intact.
7 * Redistribution in binary form may occur without any restrictions.
8 * Obviously, it would be nice if you gave credit where credit is due
9 * but requiring it would be too onerous.
11 * This software is provided ``AS IS'' without any warranties of any kind.
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/fcntl.h>
23 #include <sys/filio.h>
24 #include <sys/malloc.h>
28 #include <sys/kernel.h>
29 #include <sys/module.h>
30 #include <sys/queue.h>
31 #include <sys/snoop.h>
34 #include <sys/vnode.h>
36 static l_close_t snplclose;
37 static l_write_t snplwrite;
38 static d_open_t snpopen;
39 static d_close_t snpclose;
40 static d_read_t snpread;
41 static d_write_t snpwrite;
42 static d_ioctl_t snpioctl;
43 static d_poll_t snppoll;
45 static struct cdevsw snp_cdevsw = {
46 .d_version = D_VERSION,
47 .d_flags = D_PSEUDO | D_NEEDGIANT,
57 static struct linesw snpdisc = {
62 .l_ioctl = l_nullioctl,
69 * This is the main snoop per-device structure.
72 LIST_ENTRY(snoop) snp_list; /* List glue. */
73 int snp_unit; /* Device number. */
74 struct cdev *snp_target; /* Target tty device. */
75 struct tty *snp_tty; /* Target tty pointer. */
76 u_long snp_len; /* Possible length. */
77 u_long snp_base; /* Data base. */
78 u_long snp_blen; /* Used length. */
79 caddr_t snp_buf; /* Allocation pointer. */
80 int snp_flags; /* Flags. */
81 struct selinfo snp_sel; /* Select info. */
82 int snp_olddisc; /* Old line discipline. */
88 #define SNOOP_ASYNC 0x0002
89 #define SNOOP_OPEN 0x0004
90 #define SNOOP_RWAIT 0x0008
91 #define SNOOP_OFLOW 0x0010
92 #define SNOOP_DOWN 0x0020
97 #define SNOOP_MINLEN (4*1024) /* This should be power of 2.
98 * 4K tested to be the minimum
99 * for which on normal tty
100 * usage there is no need to
103 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough
104 * If we grow more,something
105 * really bad in this world..
108 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data");
110 * The number of the "snoop" line discipline. This gets determined at
113 static int snooplinedisc;
115 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist);
116 static struct clonedevs *snpclones;
118 static struct tty *snpdevtotty(struct cdev *dev);
119 static void snp_clone(void *arg, struct ucred *cred, char *name,
120 int namelen, struct cdev **dev);
121 static void snp_detach(void *arg);
122 static int snp_down(struct snoop *snp);
123 static int snp_in(struct snoop *snp, char *buf, int n);
124 static int snp_modevent(module_t mod, int what, void *arg);
125 static struct snoop *ttytosnp(struct tty *);
127 static struct snoop *
128 ttytosnp(struct tty *tp)
132 LIST_FOREACH(snp, &snp_sclist, snp_list) {
133 if (snp->snp_tty == tp)
140 snplclose(struct tty *tp, int flag)
146 error = snp_down(snp);
149 error = ttylclose(tp, flag);
154 snplwrite(struct tty *tp, struct uio *uio, int flag)
165 while (uio->uio_resid > 0) {
166 ilen = imin(512, uio->uio_resid);
167 ibuf = malloc(ilen, M_SNP, M_WAITOK);
168 error = uiomove(ibuf, ilen, uio);
171 snp_in(snp, ibuf, ilen);
172 /* Hackish, but probably the least of all evils. */
178 uio2.uio_resid = ilen;
179 uio2.uio_segflg = UIO_SYSSPACE;
180 uio2.uio_rw = UIO_WRITE;
181 uio2.uio_td = uio->uio_td;
182 error = ttwrite(tp, &uio2, flag);
194 snpdevtotty(struct cdev *dev)
199 cdp = dev_refthread(dev);
202 if (!(cdp->d_flags & D_TTY))
210 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal
211 * interactive mode write is 3 bytes
212 * length for function keys...
216 snpwrite(struct cdev *dev, struct uio *uio, int flag)
221 unsigned char c[SNP_INPUT_BUF];
227 if ((tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc)
230 printf("snp%d: attempt to write to bad tty\n", snp->snp_unit);
234 if (!(tp->t_state & TS_ISOPEN))
237 while (uio->uio_resid > 0) {
238 len = imin(uio->uio_resid, SNP_INPUT_BUF);
239 if ((error = uiomove(c, len, uio)) != 0)
241 for (i=0; i < len; i++) {
242 if (ttyinput(c[i], tp))
251 snpread(struct cdev *dev, struct uio *uio, int flag)
254 int error, len, n, nblen, s;
259 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen,
260 ("snoop buffer error"));
262 if (snp->snp_tty == NULL)
265 snp->snp_flags &= ~SNOOP_RWAIT;
268 if (snp->snp_len == 0) {
269 if (flag & O_NONBLOCK)
270 return (EWOULDBLOCK);
271 snp->snp_flags |= SNOOP_RWAIT;
272 error = tsleep(snp, (PZERO + 1) | PCATCH,
277 } while (snp->snp_len == 0);
282 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
283 len = min((unsigned)uio->uio_resid, snp->snp_len);
284 from = (caddr_t)(snp->snp_buf + snp->snp_base);
288 error = uiomove(from, len, uio);
289 snp->snp_base += len;
292 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) {
293 snp->snp_flags &= ~SNOOP_OFLOW;
296 nblen = snp->snp_blen;
297 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) {
298 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN)
300 if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) {
301 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
302 free(snp->snp_buf, M_SNP);
304 snp->snp_blen = nblen;
314 snp_in(struct snoop *snp, char *buf, int n)
321 KASSERT(n >= 0, ("negative snoop char count"));
326 if (snp->snp_flags & SNOOP_DOWN) {
327 printf("snp%d: more data to down interface\n", snp->snp_unit);
331 if (snp->snp_flags & SNOOP_OFLOW) {
332 printf("snp%d: buffer overflow\n", snp->snp_unit);
334 * On overflow we just repeat the standart close
335 * procedure...yes , this is waste of space but.. Then next
336 * read from device will fail if one would recall he is
337 * snooping and retry...
340 return (snp_down(snp));
342 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base);
343 s_free = snp->snp_blen - snp->snp_len;
348 nblen = snp->snp_blen;
349 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) {
350 nblen = snp->snp_blen * 2;
351 s_free = nblen - (snp->snp_len + snp->snp_base);
353 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) {
354 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len);
355 free(snp->snp_buf, M_SNP);
357 snp->snp_blen = nblen;
360 snp->snp_flags |= SNOOP_OFLOW;
361 if (snp->snp_flags & SNOOP_RWAIT) {
362 snp->snp_flags &= ~SNOOP_RWAIT;
371 from = (caddr_t)(snp->snp_buf + snp->snp_base);
372 to = (caddr_t)(snp->snp_buf);
374 bcopy(from, to, len);
377 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len);
381 if (snp->snp_flags & SNOOP_RWAIT) {
382 snp->snp_flags &= ~SNOOP_RWAIT;
385 selwakeuppri(&snp->snp_sel, PZERO + 1);
391 snpopen(struct cdev *dev, int flag, int mode, struct thread *td)
395 if (dev->si_drv1 == NULL) {
396 dev->si_flags &= ~SI_CHEAPCLONE;
397 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP,
399 snp->snp_unit = dev2unit(dev);
404 * We intentionally do not OR flags with SNOOP_OPEN, but set them so
405 * all previous settings (especially SNOOP_OFLOW) will be cleared.
407 snp->snp_flags = SNOOP_OPEN;
409 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
410 snp->snp_blen = SNOOP_MINLEN;
415 * snp_tty == NULL is for inactive snoop devices.
418 snp->snp_target = NULL;
420 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list);
426 snp_detach(void *arg)
431 snp = (struct snoop *)arg;
436 * If line disc. changed we do not touch this pointer, SLIP/PPP will
443 if ((tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc) {
444 tp->t_state &= ~TS_SNOOP;
445 tp->t_line = snp->snp_olddisc;
447 printf("snp%d: bad attached tty data\n", snp->snp_unit);
450 snp->snp_target = NULL;
453 selwakeuppri(&snp->snp_sel, PZERO + 1);
454 if ((snp->snp_flags & SNOOP_OPEN) == 0)
459 snpclose(struct cdev *dev, int flags, int fmt, struct thread *td)
465 LIST_REMOVE(snp, snp_list);
466 free(snp->snp_buf, M_SNP);
467 snp->snp_flags &= ~SNOOP_OPEN;
470 destroy_dev_sched(dev);
476 snp_down(struct snoop *snp)
479 if (snp->snp_blen != SNOOP_MINLEN) {
480 free(snp->snp_buf, M_SNP);
481 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK);
482 snp->snp_blen = SNOOP_MINLEN;
484 snp->snp_flags |= SNOOP_DOWN;
491 snpioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
505 return (snp_down(snp));
507 if (fget(td, s, &fp) != 0)
509 if (fp->f_type != DTYPE_VNODE ||
510 fp->f_vnode->v_type != VCHR ||
511 fp->f_vnode->v_rdev == NULL) {
515 tdev = fp->f_vnode->v_rdev;
518 if (snp->snp_tty != NULL)
521 tp = snpdevtotty(tdev);
524 if (tp->t_state & TS_SNOOP)
528 tp->t_state |= TS_SNOOP;
529 snp->snp_olddisc = tp->t_line;
530 tp->t_line = snooplinedisc;
532 snp->snp_target = tdev;
535 * Clean overflow and down flags -
536 * we'll have a chance to get them in the future :)))
538 snp->snp_flags &= ~SNOOP_OFLOW;
539 snp->snp_flags &= ~SNOOP_DOWN;
545 * We keep snp_target field specially to make
546 * SNPGTTY happy, else we can't know what is device
547 * major/minor for tty.
549 *((dev_t *)data) = dev2udev(snp->snp_target);
557 snp->snp_flags |= SNOOP_ASYNC;
559 snp->snp_flags &= ~SNOOP_ASYNC;
564 if (snp->snp_tty != NULL)
565 *(int *)data = snp->snp_len;
567 if (snp->snp_flags & SNOOP_DOWN) {
568 if (snp->snp_flags & SNOOP_OFLOW)
569 *(int *)data = SNP_OFLOW;
571 *(int *)data = SNP_TTYCLOSE;
573 *(int *)data = SNP_DETACH;
585 snppoll(struct cdev *dev, int events, struct thread *td)
593 * If snoop is down, we don't want to poll() forever so we return 1.
594 * Caller should see if we down via FIONREAD ioctl(). The last should
595 * return -1 to indicate down state.
597 if (events & (POLLIN | POLLRDNORM)) {
598 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0)
599 revents |= events & (POLLIN | POLLRDNORM);
601 selrecord(td, &snp->snp_sel);
607 snp_clone(void *arg, struct ucred *cred, char *name, int namelen,
614 if (dev_stdclone(name, NULL, "snp", &u) != 1)
616 i = clone_create(&snpclones, &snp_cdevsw, &u, dev, 0);
618 *dev = make_dev_credf(MAKEDEV_REF, &snp_cdevsw, unit2minor(u),
619 NULL, UID_ROOT, GID_WHEEL, 0600, "snp%d", u);
621 (*dev)->si_flags |= SI_CHEAPCLONE;
626 snp_modevent(module_t mod, int type, void *data)
628 static eventhandler_tag eh_tag;
632 /* XXX error checking. */
633 clone_setup(&snpclones);
634 eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000);
635 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc);
638 if (!LIST_EMPTY(&snp_sclist))
640 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
641 drain_dev_clone_events();
642 clone_cleanup(&snpclones);
643 destroy_dev_drain(&snp_cdevsw);
644 ldisc_deregister(snooplinedisc);
653 static moduledata_t snp_mod = {
658 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);