54}; 55 56static struct linesw snpdisc = { 57 ttyopen, snplclose, ttread, snplwrite, 58 l_nullioctl, ttyinput, ttstart, ttymodem 59}; 60 61/* 62 * This is the main snoop per-device structure. 63 */ 64struct snoop { 65 LIST_ENTRY(snoop) snp_list; /* List glue. */ 66 int snp_unit; /* Device number. */ 67 dev_t snp_target; /* Target tty device. */ 68 struct tty *snp_tty; /* Target tty pointer. */ 69 u_long snp_len; /* Possible length. */ 70 u_long snp_base; /* Data base. */ 71 u_long snp_blen; /* Used length. */ 72 caddr_t snp_buf; /* Allocation pointer. */ 73 int snp_flags; /* Flags. */ 74 struct selinfo snp_sel; /* Select info. */ 75 int snp_olddisc; /* Old line discipline. */ 76}; 77 78/* 79 * Possible flags. 80 */ 81#define SNOOP_ASYNC 0x0002 82#define SNOOP_OPEN 0x0004 83#define SNOOP_RWAIT 0x0008 84#define SNOOP_OFLOW 0x0010 85#define SNOOP_DOWN 0x0020 86 87/* 88 * Other constants. 89 */ 90#define SNOOP_MINLEN (4*1024) /* This should be power of 2. 91 * 4K tested to be the minimum 92 * for which on normal tty 93 * usage there is no need to 94 * allocate more. 95 */ 96#define SNOOP_MAXLEN (64*1024) /* This one also,64K enough 97 * If we grow more,something 98 * really bad in this world.. 99 */ 100 101static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data"); 102/* 103 * The number of the "snoop" line discipline. This gets determined at 104 * module load time. 105 */ 106static int snooplinedisc; 107static udev_t snpbasedev = NOUDEV; 108 109 110static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist); 111 112static struct tty *snpdevtotty(dev_t dev); 113static void snp_clone(void *arg, char *name, 114 int namelen, dev_t *dev); 115static int snp_detach(struct snoop *snp); 116static int snp_down(struct snoop *snp); 117static int snp_in(struct snoop *snp, char *buf, int n); 118static int snp_modevent(module_t mod, int what, void *arg); 119 120static int 121snplclose(tp, flag) 122 struct tty *tp; 123 int flag; 124{ 125 struct snoop *snp; 126 int error; 127 128 snp = tp->t_sc; 129 error = snp_down(snp); 130 if (error != 0) 131 return (error); 132 error = ttylclose(tp, flag); 133 return (error); 134} 135 136static int 137snplwrite(tp, uio, flag) 138 struct tty *tp; 139 struct uio *uio; 140 int flag; 141{ 142 struct iovec iov; 143 struct uio uio2; 144 struct snoop *snp; 145 int error, ilen; 146 char *ibuf; 147 148 error = 0; 149 ibuf = NULL; 150 snp = tp->t_sc; 151 while (uio->uio_resid > 0) { 152 ilen = imin(512, uio->uio_resid); 153 ibuf = malloc(ilen, M_SNP, M_WAITOK); 154 error = uiomove(ibuf, ilen, uio); 155 if (error != 0) 156 break; 157 snp_in(snp, ibuf, ilen); 158 /* Hackish, but probably the least of all evils. */ 159 iov.iov_base = ibuf; 160 iov.iov_len = ilen; 161 uio2.uio_iov = &iov; 162 uio2.uio_iovcnt = 1; 163 uio2.uio_offset = 0; 164 uio2.uio_resid = ilen; 165 uio2.uio_segflg = UIO_SYSSPACE; 166 uio2.uio_rw = UIO_WRITE; 167 uio2.uio_td = uio->uio_td; 168 error = ttwrite(tp, &uio2, flag); 169 if (error != 0) 170 break; 171 free(ibuf, M_SNP); 172 ibuf = NULL; 173 } 174 if (ibuf != NULL) 175 free(ibuf, M_SNP); 176 return (error); 177} 178 179static struct tty * 180snpdevtotty(dev) 181 dev_t dev; 182{ 183 struct cdevsw *cdp; 184 185 cdp = devsw(dev); 186 if (cdp == NULL || (cdp->d_flags & D_TTY) == 0) 187 return (NULL); 188 return (dev->si_tty); 189} 190 191#define SNP_INPUT_BUF 5 /* This is even too much, the maximal 192 * interactive mode write is 3 bytes 193 * length for function keys... 194 */ 195 196static int 197snpwrite(dev, uio, flag) 198 dev_t dev; 199 struct uio *uio; 200 int flag; 201{ 202 struct snoop *snp; 203 struct tty *tp; 204 int error, i, len; 205 unsigned char c[SNP_INPUT_BUF]; 206 207 snp = dev->si_drv1; 208 tp = snp->snp_tty; 209 if (tp == NULL) 210 return (EIO); 211 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && 212 tp->t_line == snooplinedisc) 213 goto tty_input; 214 215 printf("snp%d: attempt to write to bad tty\n", snp->snp_unit); 216 return (EIO); 217 218tty_input: 219 if (!(tp->t_state & TS_ISOPEN)) 220 return (EIO); 221 222 while (uio->uio_resid > 0) { 223 len = imin(uio->uio_resid, SNP_INPUT_BUF); 224 if ((error = uiomove(c, len, uio)) != 0) 225 return (error); 226 for (i=0; i < len; i++) { 227 if (ttyinput(c[i], tp)) 228 return (EIO); 229 } 230 } 231 return (0); 232} 233 234 235static int 236snpread(dev, uio, flag) 237 dev_t dev; 238 struct uio *uio; 239 int flag; 240{ 241 struct snoop *snp; 242 int error, len, n, nblen, s; 243 caddr_t from; 244 char *nbuf; 245 246 snp = dev->si_drv1; 247 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen, 248 ("snoop buffer error")); 249 250 if (snp->snp_tty == NULL) 251 return (EIO); 252 253 snp->snp_flags &= ~SNOOP_RWAIT; 254 255 do { 256 if (snp->snp_len == 0) { 257 if (flag & IO_NDELAY) 258 return (EWOULDBLOCK); 259 snp->snp_flags |= SNOOP_RWAIT; 260 error = tsleep(snp, (PZERO + 1) | PCATCH, 261 "snprd", 0); 262 if (error != 0) 263 return (error); 264 } 265 } while (snp->snp_len == 0); 266 267 n = snp->snp_len; 268 269 error = 0; 270 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) { 271 len = min((unsigned)uio->uio_resid, snp->snp_len); 272 from = (caddr_t)(snp->snp_buf + snp->snp_base); 273 if (len == 0) 274 break; 275 276 error = uiomove(from, len, uio); 277 snp->snp_base += len; 278 snp->snp_len -= len; 279 } 280 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) { 281 snp->snp_flags &= ~SNOOP_OFLOW; 282 } 283 s = spltty(); 284 nblen = snp->snp_blen; 285 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) { 286 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN) 287 nblen = nblen / 2; 288 if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) { 289 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); 290 free(snp->snp_buf, M_SNP); 291 snp->snp_buf = nbuf; 292 snp->snp_blen = nblen; 293 snp->snp_base = 0; 294 } 295 } 296 splx(s); 297 298 return (error); 299} 300 301static int 302snp_in(snp, buf, n) 303 struct snoop *snp; 304 char *buf; 305 int n; 306{ 307 int s_free, s_tail; 308 int s, len, nblen; 309 caddr_t from, to; 310 char *nbuf; 311 312 KASSERT(n >= 0, ("negative snoop char count")); 313 314 if (n == 0) 315 return (0); 316 317 if (snp->snp_flags & SNOOP_DOWN) { 318 printf("snp%d: more data to down interface\n", snp->snp_unit); 319 return (0); 320 } 321 322 if (snp->snp_flags & SNOOP_OFLOW) { 323 printf("snp%d: buffer overflow\n", snp->snp_unit); 324 /* 325 * On overflow we just repeat the standart close 326 * procedure...yes , this is waste of space but.. Then next 327 * read from device will fail if one would recall he is 328 * snooping and retry... 329 */ 330 331 return (snp_down(snp)); 332 } 333 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base); 334 s_free = snp->snp_blen - snp->snp_len; 335 336 337 if (n > s_free) { 338 s = spltty(); 339 nblen = snp->snp_blen; 340 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) { 341 nblen = snp->snp_blen * 2; 342 s_free = nblen - (snp->snp_len + snp->snp_base); 343 } 344 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) { 345 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); 346 free(snp->snp_buf, M_SNP); 347 snp->snp_buf = nbuf; 348 snp->snp_blen = nblen; 349 snp->snp_base = 0; 350 } else { 351 snp->snp_flags |= SNOOP_OFLOW; 352 if (snp->snp_flags & SNOOP_RWAIT) { 353 snp->snp_flags &= ~SNOOP_RWAIT; 354 wakeup(snp); 355 } 356 splx(s); 357 return (0); 358 } 359 splx(s); 360 } 361 if (n > s_tail) { 362 from = (caddr_t)(snp->snp_buf + snp->snp_base); 363 to = (caddr_t)(snp->snp_buf); 364 len = snp->snp_len; 365 bcopy(from, to, len); 366 snp->snp_base = 0; 367 } 368 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len); 369 bcopy(buf, to, n); 370 snp->snp_len += n; 371 372 if (snp->snp_flags & SNOOP_RWAIT) { 373 snp->snp_flags &= ~SNOOP_RWAIT; 374 wakeup(snp); 375 } 376 selwakeup(&snp->snp_sel); 377 378 return (n); 379} 380 381static int 382snpopen(dev, flag, mode, td) 383 dev_t dev; 384 int flag, mode; 385 struct thread *td; 386{ 387 struct snoop *snp; 388 389 if (dev->si_drv1 == NULL) { 390 if (!(dev->si_flags & SI_NAMED)) 391 make_dev(&snp_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 392 0600, "snp%d", dev2unit(dev)); 393 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP, 394 M_WAITOK | M_ZERO); 395 snp->snp_unit = dev2unit(dev); 396 } else 397 return (EBUSY); 398 399 /* 400 * We intentionally do not OR flags with SNOOP_OPEN, but set them so 401 * all previous settings (especially SNOOP_OFLOW) will be cleared. 402 */ 403 snp->snp_flags = SNOOP_OPEN; 404 405 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK); 406 snp->snp_blen = SNOOP_MINLEN; 407 snp->snp_base = 0; 408 snp->snp_len = 0; 409 410 /* 411 * snp_tty == NULL is for inactive snoop devices. 412 */ 413 snp->snp_tty = NULL; 414 snp->snp_target = NODEV; 415 416 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list); 417 return (0); 418} 419 420 421static int 422snp_detach(snp) 423 struct snoop *snp; 424{ 425 struct tty *tp; 426 427 snp->snp_base = 0; 428 snp->snp_len = 0; 429 430 /* 431 * If line disc. changed we do not touch this pointer, SLIP/PPP will 432 * change it anyway. 433 */ 434 tp = snp->snp_tty; 435 if (tp == NULL) 436 goto detach_notty; 437 438 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && 439 tp->t_line == snooplinedisc) { 440 tp->t_sc = NULL; 441 tp->t_state &= ~TS_SNOOP; 442 tp->t_line = snp->snp_olddisc; 443 } else 444 printf("snp%d: bad attached tty data\n", snp->snp_unit); 445 446 snp->snp_tty = NULL; 447 snp->snp_target = NODEV; 448 449detach_notty: 450 selwakeup(&snp->snp_sel); 451 if ((snp->snp_flags & SNOOP_OPEN) == 0) 452 free(snp, M_SNP); 453 454 return (0); 455} 456 457static int 458snpclose(dev, flags, fmt, td) 459 dev_t dev; 460 int flags; 461 int fmt; 462 struct thread *td; 463{ 464 struct snoop *snp; 465 466 snp = dev->si_drv1; 467 snp->snp_blen = 0; 468 LIST_REMOVE(snp, snp_list); 469 free(snp->snp_buf, M_SNP); 470 snp->snp_flags &= ~SNOOP_OPEN; 471 dev->si_drv1 = NULL; 472 473 return (snp_detach(snp)); 474} 475 476static int 477snp_down(snp) 478 struct snoop *snp; 479{ 480 481 if (snp->snp_blen != SNOOP_MINLEN) { 482 free(snp->snp_buf, M_SNP); 483 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK); 484 snp->snp_blen = SNOOP_MINLEN; 485 } 486 snp->snp_flags |= SNOOP_DOWN; 487 488 return (snp_detach(snp)); 489} 490 491static int 492snpioctl(dev, cmd, data, flags, td) 493 dev_t dev; 494 u_long cmd; 495 caddr_t data; 496 int flags; 497 struct thread *td; 498{ 499 struct snoop *snp; 500 struct tty *tp, *tpo; 501 dev_t tdev; 502 int s; 503 504 snp = dev->si_drv1; 505 switch (cmd) { 506 case SNPSTTY: 507 tdev = udev2dev(*((udev_t *)data), 0); 508 if (tdev == NODEV) 509 return (snp_down(snp)); 510 511 tp = snpdevtotty(tdev); 512 if (!tp) 513 return (EINVAL); 514 if (tp->t_state & TS_SNOOP) 515 return (EBUSY); 516 517 s = spltty(); 518 519 if (snp->snp_target == NODEV) { 520 tpo = snp->snp_tty; 521 if (tpo) 522 tpo->t_state &= ~TS_SNOOP; 523 } 524 525 tp->t_sc = (caddr_t)snp; 526 tp->t_state |= TS_SNOOP; 527 snp->snp_olddisc = tp->t_line; 528 tp->t_line = snooplinedisc; 529 snp->snp_tty = tp; 530 snp->snp_target = tdev; 531 532 /* 533 * Clean overflow and down flags - 534 * we'll have a chance to get them in the future :))) 535 */ 536 snp->snp_flags &= ~SNOOP_OFLOW; 537 snp->snp_flags &= ~SNOOP_DOWN; 538 splx(s); 539 break; 540 541 case SNPGTTY: 542 /* 543 * We keep snp_target field specially to make 544 * SNPGTTY happy, else we can't know what is device 545 * major/minor for tty. 546 */ 547 *((udev_t *)data) = dev2udev(snp->snp_target); 548 break; 549 550 case FIONBIO: 551 break; 552 553 case FIOASYNC: 554 if (*(int *)data) 555 snp->snp_flags |= SNOOP_ASYNC; 556 else 557 snp->snp_flags &= ~SNOOP_ASYNC; 558 break; 559 560 case FIONREAD: 561 s = spltty(); 562 if (snp->snp_tty != NULL) 563 *(int *)data = snp->snp_len; 564 else 565 if (snp->snp_flags & SNOOP_DOWN) { 566 if (snp->snp_flags & SNOOP_OFLOW) 567 *(int *)data = SNP_OFLOW; 568 else 569 *(int *)data = SNP_TTYCLOSE; 570 } else { 571 *(int *)data = SNP_DETACH; 572 } 573 splx(s); 574 break; 575 576 default: 577 return (ENOTTY); 578 } 579 return (0); 580} 581 582static int 583snppoll(dev, events, td) 584 dev_t dev; 585 int events; 586 struct thread *td; 587{ 588 struct snoop *snp; 589 int revents; 590 591 snp = dev->si_drv1; 592 revents = 0; 593 /* 594 * If snoop is down, we don't want to poll() forever so we return 1. 595 * Caller should see if we down via FIONREAD ioctl(). The last should 596 * return -1 to indicate down state. 597 */ 598 if (events & (POLLIN | POLLRDNORM)) { 599 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0) 600 revents |= events & (POLLIN | POLLRDNORM); 601 else 602 selrecord(td, &snp->snp_sel); 603 } 604 return (revents); 605} 606 607static void 608snp_clone(arg, name, namelen, dev) 609 void *arg; 610 char *name; 611 int namelen; 612 dev_t *dev; 613{ 614 int u; 615 616 if (*dev != NODEV) 617 return; 618 if (dev_stdclone(name, NULL, "snp", &u) != 1) 619 return; 620 *dev = make_dev(&snp_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600, 621 "snp%d", u); 622 if (snpbasedev == NOUDEV) 623 snpbasedev = (*dev)->si_udev; 624 else { 625 (*dev)->si_flags |= SI_CHEAPCLONE; 626 dev_depends(udev2dev(snpbasedev, 0), *dev); 627 } 628} 629 630static int 631snp_modevent(mod, type, data) 632 module_t mod; 633 int type; 634 void *data; 635{ 636 static eventhandler_tag eh_tag; 637 638 switch (type) { 639 case MOD_LOAD: 640 /* XXX error checking. */ 641 eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000); 642 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc); 643 break; 644 case MOD_UNLOAD: 645 if (!LIST_EMPTY(&snp_sclist)) 646 return (EBUSY); 647 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag); 648 if (snpbasedev != NOUDEV) 649 destroy_dev(udev2dev(snpbasedev, 0)); 650 ldisc_deregister(snooplinedisc); 651 break; 652 default: 653 break; 654 } 655 return (0); 656} 657 658static moduledata_t snp_mod = { 659 "snp", 660 snp_modevent, 661 NULL 662}; 663DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);
| 49}; 50 51static struct linesw snpdisc = { 52 ttyopen, snplclose, ttread, snplwrite, 53 l_nullioctl, ttyinput, ttstart, ttymodem 54}; 55 56/* 57 * This is the main snoop per-device structure. 58 */ 59struct snoop { 60 LIST_ENTRY(snoop) snp_list; /* List glue. */ 61 int snp_unit; /* Device number. */ 62 dev_t snp_target; /* Target tty device. */ 63 struct tty *snp_tty; /* Target tty pointer. */ 64 u_long snp_len; /* Possible length. */ 65 u_long snp_base; /* Data base. */ 66 u_long snp_blen; /* Used length. */ 67 caddr_t snp_buf; /* Allocation pointer. */ 68 int snp_flags; /* Flags. */ 69 struct selinfo snp_sel; /* Select info. */ 70 int snp_olddisc; /* Old line discipline. */ 71}; 72 73/* 74 * Possible flags. 75 */ 76#define SNOOP_ASYNC 0x0002 77#define SNOOP_OPEN 0x0004 78#define SNOOP_RWAIT 0x0008 79#define SNOOP_OFLOW 0x0010 80#define SNOOP_DOWN 0x0020 81 82/* 83 * Other constants. 84 */ 85#define SNOOP_MINLEN (4*1024) /* This should be power of 2. 86 * 4K tested to be the minimum 87 * for which on normal tty 88 * usage there is no need to 89 * allocate more. 90 */ 91#define SNOOP_MAXLEN (64*1024) /* This one also,64K enough 92 * If we grow more,something 93 * really bad in this world.. 94 */ 95 96static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data"); 97/* 98 * The number of the "snoop" line discipline. This gets determined at 99 * module load time. 100 */ 101static int snooplinedisc; 102static udev_t snpbasedev = NOUDEV; 103 104 105static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist); 106 107static struct tty *snpdevtotty(dev_t dev); 108static void snp_clone(void *arg, char *name, 109 int namelen, dev_t *dev); 110static int snp_detach(struct snoop *snp); 111static int snp_down(struct snoop *snp); 112static int snp_in(struct snoop *snp, char *buf, int n); 113static int snp_modevent(module_t mod, int what, void *arg); 114 115static int 116snplclose(tp, flag) 117 struct tty *tp; 118 int flag; 119{ 120 struct snoop *snp; 121 int error; 122 123 snp = tp->t_sc; 124 error = snp_down(snp); 125 if (error != 0) 126 return (error); 127 error = ttylclose(tp, flag); 128 return (error); 129} 130 131static int 132snplwrite(tp, uio, flag) 133 struct tty *tp; 134 struct uio *uio; 135 int flag; 136{ 137 struct iovec iov; 138 struct uio uio2; 139 struct snoop *snp; 140 int error, ilen; 141 char *ibuf; 142 143 error = 0; 144 ibuf = NULL; 145 snp = tp->t_sc; 146 while (uio->uio_resid > 0) { 147 ilen = imin(512, uio->uio_resid); 148 ibuf = malloc(ilen, M_SNP, M_WAITOK); 149 error = uiomove(ibuf, ilen, uio); 150 if (error != 0) 151 break; 152 snp_in(snp, ibuf, ilen); 153 /* Hackish, but probably the least of all evils. */ 154 iov.iov_base = ibuf; 155 iov.iov_len = ilen; 156 uio2.uio_iov = &iov; 157 uio2.uio_iovcnt = 1; 158 uio2.uio_offset = 0; 159 uio2.uio_resid = ilen; 160 uio2.uio_segflg = UIO_SYSSPACE; 161 uio2.uio_rw = UIO_WRITE; 162 uio2.uio_td = uio->uio_td; 163 error = ttwrite(tp, &uio2, flag); 164 if (error != 0) 165 break; 166 free(ibuf, M_SNP); 167 ibuf = NULL; 168 } 169 if (ibuf != NULL) 170 free(ibuf, M_SNP); 171 return (error); 172} 173 174static struct tty * 175snpdevtotty(dev) 176 dev_t dev; 177{ 178 struct cdevsw *cdp; 179 180 cdp = devsw(dev); 181 if (cdp == NULL || (cdp->d_flags & D_TTY) == 0) 182 return (NULL); 183 return (dev->si_tty); 184} 185 186#define SNP_INPUT_BUF 5 /* This is even too much, the maximal 187 * interactive mode write is 3 bytes 188 * length for function keys... 189 */ 190 191static int 192snpwrite(dev, uio, flag) 193 dev_t dev; 194 struct uio *uio; 195 int flag; 196{ 197 struct snoop *snp; 198 struct tty *tp; 199 int error, i, len; 200 unsigned char c[SNP_INPUT_BUF]; 201 202 snp = dev->si_drv1; 203 tp = snp->snp_tty; 204 if (tp == NULL) 205 return (EIO); 206 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && 207 tp->t_line == snooplinedisc) 208 goto tty_input; 209 210 printf("snp%d: attempt to write to bad tty\n", snp->snp_unit); 211 return (EIO); 212 213tty_input: 214 if (!(tp->t_state & TS_ISOPEN)) 215 return (EIO); 216 217 while (uio->uio_resid > 0) { 218 len = imin(uio->uio_resid, SNP_INPUT_BUF); 219 if ((error = uiomove(c, len, uio)) != 0) 220 return (error); 221 for (i=0; i < len; i++) { 222 if (ttyinput(c[i], tp)) 223 return (EIO); 224 } 225 } 226 return (0); 227} 228 229 230static int 231snpread(dev, uio, flag) 232 dev_t dev; 233 struct uio *uio; 234 int flag; 235{ 236 struct snoop *snp; 237 int error, len, n, nblen, s; 238 caddr_t from; 239 char *nbuf; 240 241 snp = dev->si_drv1; 242 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen, 243 ("snoop buffer error")); 244 245 if (snp->snp_tty == NULL) 246 return (EIO); 247 248 snp->snp_flags &= ~SNOOP_RWAIT; 249 250 do { 251 if (snp->snp_len == 0) { 252 if (flag & IO_NDELAY) 253 return (EWOULDBLOCK); 254 snp->snp_flags |= SNOOP_RWAIT; 255 error = tsleep(snp, (PZERO + 1) | PCATCH, 256 "snprd", 0); 257 if (error != 0) 258 return (error); 259 } 260 } while (snp->snp_len == 0); 261 262 n = snp->snp_len; 263 264 error = 0; 265 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) { 266 len = min((unsigned)uio->uio_resid, snp->snp_len); 267 from = (caddr_t)(snp->snp_buf + snp->snp_base); 268 if (len == 0) 269 break; 270 271 error = uiomove(from, len, uio); 272 snp->snp_base += len; 273 snp->snp_len -= len; 274 } 275 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) { 276 snp->snp_flags &= ~SNOOP_OFLOW; 277 } 278 s = spltty(); 279 nblen = snp->snp_blen; 280 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) { 281 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN) 282 nblen = nblen / 2; 283 if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) { 284 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); 285 free(snp->snp_buf, M_SNP); 286 snp->snp_buf = nbuf; 287 snp->snp_blen = nblen; 288 snp->snp_base = 0; 289 } 290 } 291 splx(s); 292 293 return (error); 294} 295 296static int 297snp_in(snp, buf, n) 298 struct snoop *snp; 299 char *buf; 300 int n; 301{ 302 int s_free, s_tail; 303 int s, len, nblen; 304 caddr_t from, to; 305 char *nbuf; 306 307 KASSERT(n >= 0, ("negative snoop char count")); 308 309 if (n == 0) 310 return (0); 311 312 if (snp->snp_flags & SNOOP_DOWN) { 313 printf("snp%d: more data to down interface\n", snp->snp_unit); 314 return (0); 315 } 316 317 if (snp->snp_flags & SNOOP_OFLOW) { 318 printf("snp%d: buffer overflow\n", snp->snp_unit); 319 /* 320 * On overflow we just repeat the standart close 321 * procedure...yes , this is waste of space but.. Then next 322 * read from device will fail if one would recall he is 323 * snooping and retry... 324 */ 325 326 return (snp_down(snp)); 327 } 328 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base); 329 s_free = snp->snp_blen - snp->snp_len; 330 331 332 if (n > s_free) { 333 s = spltty(); 334 nblen = snp->snp_blen; 335 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) { 336 nblen = snp->snp_blen * 2; 337 s_free = nblen - (snp->snp_len + snp->snp_base); 338 } 339 if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) { 340 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); 341 free(snp->snp_buf, M_SNP); 342 snp->snp_buf = nbuf; 343 snp->snp_blen = nblen; 344 snp->snp_base = 0; 345 } else { 346 snp->snp_flags |= SNOOP_OFLOW; 347 if (snp->snp_flags & SNOOP_RWAIT) { 348 snp->snp_flags &= ~SNOOP_RWAIT; 349 wakeup(snp); 350 } 351 splx(s); 352 return (0); 353 } 354 splx(s); 355 } 356 if (n > s_tail) { 357 from = (caddr_t)(snp->snp_buf + snp->snp_base); 358 to = (caddr_t)(snp->snp_buf); 359 len = snp->snp_len; 360 bcopy(from, to, len); 361 snp->snp_base = 0; 362 } 363 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len); 364 bcopy(buf, to, n); 365 snp->snp_len += n; 366 367 if (snp->snp_flags & SNOOP_RWAIT) { 368 snp->snp_flags &= ~SNOOP_RWAIT; 369 wakeup(snp); 370 } 371 selwakeup(&snp->snp_sel); 372 373 return (n); 374} 375 376static int 377snpopen(dev, flag, mode, td) 378 dev_t dev; 379 int flag, mode; 380 struct thread *td; 381{ 382 struct snoop *snp; 383 384 if (dev->si_drv1 == NULL) { 385 if (!(dev->si_flags & SI_NAMED)) 386 make_dev(&snp_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 387 0600, "snp%d", dev2unit(dev)); 388 dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP, 389 M_WAITOK | M_ZERO); 390 snp->snp_unit = dev2unit(dev); 391 } else 392 return (EBUSY); 393 394 /* 395 * We intentionally do not OR flags with SNOOP_OPEN, but set them so 396 * all previous settings (especially SNOOP_OFLOW) will be cleared. 397 */ 398 snp->snp_flags = SNOOP_OPEN; 399 400 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK); 401 snp->snp_blen = SNOOP_MINLEN; 402 snp->snp_base = 0; 403 snp->snp_len = 0; 404 405 /* 406 * snp_tty == NULL is for inactive snoop devices. 407 */ 408 snp->snp_tty = NULL; 409 snp->snp_target = NODEV; 410 411 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list); 412 return (0); 413} 414 415 416static int 417snp_detach(snp) 418 struct snoop *snp; 419{ 420 struct tty *tp; 421 422 snp->snp_base = 0; 423 snp->snp_len = 0; 424 425 /* 426 * If line disc. changed we do not touch this pointer, SLIP/PPP will 427 * change it anyway. 428 */ 429 tp = snp->snp_tty; 430 if (tp == NULL) 431 goto detach_notty; 432 433 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && 434 tp->t_line == snooplinedisc) { 435 tp->t_sc = NULL; 436 tp->t_state &= ~TS_SNOOP; 437 tp->t_line = snp->snp_olddisc; 438 } else 439 printf("snp%d: bad attached tty data\n", snp->snp_unit); 440 441 snp->snp_tty = NULL; 442 snp->snp_target = NODEV; 443 444detach_notty: 445 selwakeup(&snp->snp_sel); 446 if ((snp->snp_flags & SNOOP_OPEN) == 0) 447 free(snp, M_SNP); 448 449 return (0); 450} 451 452static int 453snpclose(dev, flags, fmt, td) 454 dev_t dev; 455 int flags; 456 int fmt; 457 struct thread *td; 458{ 459 struct snoop *snp; 460 461 snp = dev->si_drv1; 462 snp->snp_blen = 0; 463 LIST_REMOVE(snp, snp_list); 464 free(snp->snp_buf, M_SNP); 465 snp->snp_flags &= ~SNOOP_OPEN; 466 dev->si_drv1 = NULL; 467 468 return (snp_detach(snp)); 469} 470 471static int 472snp_down(snp) 473 struct snoop *snp; 474{ 475 476 if (snp->snp_blen != SNOOP_MINLEN) { 477 free(snp->snp_buf, M_SNP); 478 snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK); 479 snp->snp_blen = SNOOP_MINLEN; 480 } 481 snp->snp_flags |= SNOOP_DOWN; 482 483 return (snp_detach(snp)); 484} 485 486static int 487snpioctl(dev, cmd, data, flags, td) 488 dev_t dev; 489 u_long cmd; 490 caddr_t data; 491 int flags; 492 struct thread *td; 493{ 494 struct snoop *snp; 495 struct tty *tp, *tpo; 496 dev_t tdev; 497 int s; 498 499 snp = dev->si_drv1; 500 switch (cmd) { 501 case SNPSTTY: 502 tdev = udev2dev(*((udev_t *)data), 0); 503 if (tdev == NODEV) 504 return (snp_down(snp)); 505 506 tp = snpdevtotty(tdev); 507 if (!tp) 508 return (EINVAL); 509 if (tp->t_state & TS_SNOOP) 510 return (EBUSY); 511 512 s = spltty(); 513 514 if (snp->snp_target == NODEV) { 515 tpo = snp->snp_tty; 516 if (tpo) 517 tpo->t_state &= ~TS_SNOOP; 518 } 519 520 tp->t_sc = (caddr_t)snp; 521 tp->t_state |= TS_SNOOP; 522 snp->snp_olddisc = tp->t_line; 523 tp->t_line = snooplinedisc; 524 snp->snp_tty = tp; 525 snp->snp_target = tdev; 526 527 /* 528 * Clean overflow and down flags - 529 * we'll have a chance to get them in the future :))) 530 */ 531 snp->snp_flags &= ~SNOOP_OFLOW; 532 snp->snp_flags &= ~SNOOP_DOWN; 533 splx(s); 534 break; 535 536 case SNPGTTY: 537 /* 538 * We keep snp_target field specially to make 539 * SNPGTTY happy, else we can't know what is device 540 * major/minor for tty. 541 */ 542 *((udev_t *)data) = dev2udev(snp->snp_target); 543 break; 544 545 case FIONBIO: 546 break; 547 548 case FIOASYNC: 549 if (*(int *)data) 550 snp->snp_flags |= SNOOP_ASYNC; 551 else 552 snp->snp_flags &= ~SNOOP_ASYNC; 553 break; 554 555 case FIONREAD: 556 s = spltty(); 557 if (snp->snp_tty != NULL) 558 *(int *)data = snp->snp_len; 559 else 560 if (snp->snp_flags & SNOOP_DOWN) { 561 if (snp->snp_flags & SNOOP_OFLOW) 562 *(int *)data = SNP_OFLOW; 563 else 564 *(int *)data = SNP_TTYCLOSE; 565 } else { 566 *(int *)data = SNP_DETACH; 567 } 568 splx(s); 569 break; 570 571 default: 572 return (ENOTTY); 573 } 574 return (0); 575} 576 577static int 578snppoll(dev, events, td) 579 dev_t dev; 580 int events; 581 struct thread *td; 582{ 583 struct snoop *snp; 584 int revents; 585 586 snp = dev->si_drv1; 587 revents = 0; 588 /* 589 * If snoop is down, we don't want to poll() forever so we return 1. 590 * Caller should see if we down via FIONREAD ioctl(). The last should 591 * return -1 to indicate down state. 592 */ 593 if (events & (POLLIN | POLLRDNORM)) { 594 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0) 595 revents |= events & (POLLIN | POLLRDNORM); 596 else 597 selrecord(td, &snp->snp_sel); 598 } 599 return (revents); 600} 601 602static void 603snp_clone(arg, name, namelen, dev) 604 void *arg; 605 char *name; 606 int namelen; 607 dev_t *dev; 608{ 609 int u; 610 611 if (*dev != NODEV) 612 return; 613 if (dev_stdclone(name, NULL, "snp", &u) != 1) 614 return; 615 *dev = make_dev(&snp_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600, 616 "snp%d", u); 617 if (snpbasedev == NOUDEV) 618 snpbasedev = (*dev)->si_udev; 619 else { 620 (*dev)->si_flags |= SI_CHEAPCLONE; 621 dev_depends(udev2dev(snpbasedev, 0), *dev); 622 } 623} 624 625static int 626snp_modevent(mod, type, data) 627 module_t mod; 628 int type; 629 void *data; 630{ 631 static eventhandler_tag eh_tag; 632 633 switch (type) { 634 case MOD_LOAD: 635 /* XXX error checking. */ 636 eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000); 637 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc); 638 break; 639 case MOD_UNLOAD: 640 if (!LIST_EMPTY(&snp_sclist)) 641 return (EBUSY); 642 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag); 643 if (snpbasedev != NOUDEV) 644 destroy_dev(udev2dev(snpbasedev, 0)); 645 ldisc_deregister(snooplinedisc); 646 break; 647 default: 648 break; 649 } 650 return (0); 651} 652 653static moduledata_t snp_mod = { 654 "snp", 655 snp_modevent, 656 NULL 657}; 658DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);
|