1 /* $NetBSD: tty_pty.c,v 1.94 2006/09/23 15:36:12 xtraeme Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95 32 */ 33 34 /* 35 * Pseudo-teletype Driver 36 * (Actually two drivers, requiring two entries in 'cdevsw') 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.94 2006/09/23 15:36:12 xtraeme Exp $"); 41 42 #include "opt_compat_sunos.h" 43 #include "opt_ptm.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/ioctl.h> 48 #include <sys/proc.h> 49 #include <sys/tty.h> 50 #include <sys/stat.h> 51 #include <sys/file.h> 52 #include <sys/kernel.h> 53 #include <sys/vnode.h> 54 #include <sys/namei.h> 55 #include <sys/signalvar.h> 56 #include <sys/uio.h> 57 #include <sys/filedesc.h> 58 #include <sys/conf.h> 59 #include <sys/poll.h> 60 #include <sys/malloc.h> 61 #include <sys/pty.h> 62 #include <sys/kauth.h> 63 64 #define DEFAULT_NPTYS 16 /* default number of initial ptys */ 65 #define DEFAULT_MAXPTYS 992 /* default maximum number of ptys */ 66 67 #define BUFSIZ 100 /* Chunk size iomoved to/from user */ 68 69 struct pt_softc { 70 struct tty *pt_tty; 71 int pt_flags; 72 struct selinfo pt_selr, pt_selw; 73 u_char pt_send; 74 u_char pt_ucntl; 75 }; 76 77 static struct pt_softc **pt_softc = NULL; /* pty array */ 78 static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */ 79 struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER; 80 int npty = 0; /* for pstat -t */ 81 82 #define PF_PKT 0x08 /* packet mode */ 83 #define PF_STOPPED 0x10 /* user told stopped */ 84 #define PF_REMOTE 0x20 /* remote and flow controlled input */ 85 #define PF_NOSTOP 0x40 86 #define PF_UCNTL 0x80 /* user control mode */ 87 88 void ptyattach(int); 89 void ptcwakeup(struct tty *, int); 90 void ptsstart(struct tty *); 91 int pty_maxptys(int, int); 92 93 static struct pt_softc **ptyarralloc(int); 94 95 dev_type_open(ptcopen); 96 dev_type_close(ptcclose); 97 dev_type_read(ptcread); 98 dev_type_write(ptcwrite); 99 dev_type_poll(ptcpoll); 100 dev_type_kqfilter(ptckqfilter); 101 102 dev_type_open(ptsopen); 103 dev_type_close(ptsclose); 104 dev_type_read(ptsread); 105 dev_type_write(ptswrite); 106 dev_type_stop(ptsstop); 107 dev_type_poll(ptspoll); 108 109 dev_type_ioctl(ptyioctl); 110 dev_type_tty(ptytty); 111 112 const struct cdevsw ptc_cdevsw = { 113 ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl, 114 nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY 115 }; 116 117 const struct cdevsw pts_cdevsw = { 118 ptsopen, ptsclose, ptsread, ptswrite, ptyioctl, 119 ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY 120 }; 121 122 #if defined(pmax) 123 const struct cdevsw ptc_ultrix_cdevsw = { 124 ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl, 125 nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY 126 }; 127 128 const struct cdevsw pts_ultrix_cdevsw = { 129 ptsopen, ptsclose, ptsread, ptswrite, ptyioctl, 130 ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY 131 }; 132 #endif /* defined(pmax) */ 133 134 /* 135 * Check if a pty is free to use. 136 */ 137 int 138 pty_isfree(int minor, int lock) 139 { 140 struct pt_softc *pt = pt_softc[minor]; 141 if (lock) 142 simple_lock(&pt_softc_mutex); 143 minor = pt == NULL || pt->pt_tty == NULL || 144 pt->pt_tty->t_oproc == NULL; 145 if (lock) 146 simple_unlock(&pt_softc_mutex); 147 return minor; 148 } 149 150 /* 151 * Allocate and zero array of nelem elements. 152 */ 153 static struct pt_softc ** 154 ptyarralloc(nelem) 155 int nelem; 156 { 157 struct pt_softc **pt; 158 nelem += 10; 159 pt = malloc(nelem * sizeof *pt, M_DEVBUF, M_WAITOK | M_ZERO); 160 return pt; 161 } 162 163 /* 164 * Check if the minor is correct and ensure necessary structures 165 * are properly allocated. 166 */ 167 int 168 pty_check(int ptn) 169 { 170 struct pt_softc *pti; 171 172 if (ptn >= npty) { 173 struct pt_softc **newpt, **oldpt; 174 int newnpty; 175 176 /* check if the requested pty can be granted */ 177 if (ptn >= maxptys) { 178 limit_reached: 179 tablefull("pty", "increase kern.maxptys"); 180 return (ENXIO); 181 } 182 183 /* Allocate a larger pty array */ 184 for (newnpty = npty; newnpty <= ptn;) 185 newnpty *= 2; 186 if (newnpty > maxptys) 187 newnpty = maxptys; 188 newpt = ptyarralloc(newnpty); 189 190 /* 191 * Now grab the pty array mutex - we need to ensure 192 * that the pty array is consistent while copying it's 193 * content to newly allocated, larger space; we also 194 * need to be safe against pty_maxptys(). 195 */ 196 simple_lock(&pt_softc_mutex); 197 198 if (newnpty >= maxptys) { 199 /* limit cut away beneath us... */ 200 newnpty = maxptys; 201 if (ptn >= newnpty) { 202 simple_unlock(&pt_softc_mutex); 203 free(newpt, M_DEVBUF); 204 goto limit_reached; 205 } 206 } 207 208 /* 209 * If the pty array was not enlarged while we were waiting 210 * for mutex, copy current contents of pt_softc[] to newly 211 * allocated array and start using the new bigger array. 212 */ 213 if (newnpty > npty) { 214 memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *)); 215 oldpt = pt_softc; 216 pt_softc = newpt; 217 npty = newnpty; 218 } else { 219 /* was enlarged when waited for lock, free new space */ 220 oldpt = newpt; 221 } 222 223 simple_unlock(&pt_softc_mutex); 224 free(oldpt, M_DEVBUF); 225 } 226 227 /* 228 * If the entry is not yet allocated, allocate one. The mutex is 229 * needed so that the state of pt_softc[] array is consistant 230 * in case it has been lengthened above. 231 */ 232 if (!pt_softc[ptn]) { 233 MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc), 234 M_DEVBUF, M_WAITOK); 235 memset(pti, 0, sizeof(struct pt_softc)); 236 237 pti->pt_tty = ttymalloc(); 238 239 simple_lock(&pt_softc_mutex); 240 241 /* 242 * Check the entry again - it might have been 243 * added while we were waiting for mutex. 244 */ 245 if (!pt_softc[ptn]) { 246 tty_attach(pti->pt_tty); 247 pt_softc[ptn] = pti; 248 } else { 249 ttyfree(pti->pt_tty); 250 free(pti, M_DEVBUF); 251 } 252 253 simple_unlock(&pt_softc_mutex); 254 } 255 256 return (0); 257 } 258 259 /* 260 * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise 261 * new value of maxptys. 262 */ 263 int 264 pty_maxptys(newmax, set) 265 int newmax, set; 266 { 267 if (!set) 268 return (maxptys); 269 270 /* 271 * We have to grab the pt_softc lock, so that we would pick correct 272 * value of npty (might be modified in pty_check()). 273 */ 274 simple_lock(&pt_softc_mutex); 275 276 /* 277 * The value cannot be set to value lower than the highest pty 278 * number ever allocated. 279 */ 280 if (newmax >= npty) 281 maxptys = newmax; 282 else 283 newmax = 0; 284 285 simple_unlock(&pt_softc_mutex); 286 287 return newmax; 288 } 289 290 /* 291 * Establish n (or default if n is 1) ptys in the system. 292 */ 293 void 294 ptyattach(n) 295 int n; 296 { 297 /* maybe should allow 0 => none? */ 298 if (n <= 1) 299 n = DEFAULT_NPTYS; 300 pt_softc = ptyarralloc(n); 301 npty = n; 302 #ifndef NO_DEV_PTM 303 ptmattach(1); 304 #endif 305 } 306 307 /*ARGSUSED*/ 308 int 309 ptsopen(dev, flag, devtype, l) 310 dev_t dev; 311 int flag, devtype; 312 struct lwp *l; 313 { 314 struct pt_softc *pti; 315 struct tty *tp; 316 int error; 317 int ptn = minor(dev); 318 int s; 319 320 if ((error = pty_check(ptn)) != 0) 321 return (error); 322 323 pti = pt_softc[ptn]; 324 tp = pti->pt_tty; 325 326 if (!ISSET(tp->t_state, TS_ISOPEN)) { 327 ttychars(tp); /* Set up default chars */ 328 tp->t_iflag = TTYDEF_IFLAG; 329 tp->t_oflag = TTYDEF_OFLAG; 330 tp->t_lflag = TTYDEF_LFLAG; 331 tp->t_cflag = TTYDEF_CFLAG; 332 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 333 ttsetwater(tp); /* would be done in xxparam() */ 334 } else if (ISSET(tp->t_state, TS_XCLUDE) && 335 kauth_cred_geteuid(l->l_cred) != 0) 336 return (EBUSY); 337 if (tp->t_oproc) /* Ctrlr still around. */ 338 SET(tp->t_state, TS_CARR_ON); 339 340 if (!ISSET(flag, O_NONBLOCK)) { 341 s = spltty(); 342 TTY_LOCK(tp); 343 while (!ISSET(tp->t_state, TS_CARR_ON)) { 344 tp->t_wopen++; 345 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, 346 ttopen, 0); 347 tp->t_wopen--; 348 if (error) { 349 TTY_UNLOCK(tp); 350 splx(s); 351 return (error); 352 } 353 } 354 TTY_UNLOCK(tp); 355 splx(s); 356 } 357 error = (*tp->t_linesw->l_open)(dev, tp); 358 ptcwakeup(tp, FREAD|FWRITE); 359 return (error); 360 } 361 362 int 363 ptsclose(dev, flag, mode, l) 364 dev_t dev; 365 int flag, mode; 366 struct lwp *l; 367 { 368 struct pt_softc *pti = pt_softc[minor(dev)]; 369 struct tty *tp = pti->pt_tty; 370 int error; 371 372 error = (*tp->t_linesw->l_close)(tp, flag); 373 error |= ttyclose(tp); 374 ptcwakeup(tp, FREAD|FWRITE); 375 return (error); 376 } 377 378 int 379 ptsread(dev, uio, flag) 380 dev_t dev; 381 struct uio *uio; 382 int flag; 383 { 384 struct proc *p = curproc; 385 struct pt_softc *pti = pt_softc[minor(dev)]; 386 struct tty *tp = pti->pt_tty; 387 int error = 0; 388 int cc; 389 int s; 390 391 again: 392 if (pti->pt_flags & PF_REMOTE) { 393 while (isbackground(p, tp)) { 394 if (sigismasked(p, SIGTTIN) || 395 p->p_pgrp->pg_jobc == 0 || 396 p->p_flag & P_PPWAIT) 397 return (EIO); 398 pgsignal(p->p_pgrp, SIGTTIN, 1); 399 s = spltty(); 400 TTY_LOCK(tp); 401 error = ttysleep(tp, (caddr_t)&lbolt, 402 TTIPRI | PCATCH | PNORELOCK, ttybg, 0); 403 splx(s); 404 if (error) 405 return (error); 406 } 407 s = spltty(); 408 TTY_LOCK(tp); 409 if (tp->t_canq.c_cc == 0) { 410 if (flag & IO_NDELAY) { 411 TTY_UNLOCK(tp); 412 splx(s); 413 return (EWOULDBLOCK); 414 } 415 error = ttysleep(tp, (caddr_t)&tp->t_canq, 416 TTIPRI | PCATCH | PNORELOCK, ttyin, 0); 417 splx(s); 418 if (error) 419 return (error); 420 goto again; 421 } 422 while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) { 423 TTY_UNLOCK(tp); 424 splx(s); 425 error = ureadc(getc(&tp->t_canq), uio); 426 s = spltty(); 427 TTY_LOCK(tp); 428 /* Re-check terminal state here? */ 429 } 430 if (tp->t_canq.c_cc == 1) 431 (void) getc(&tp->t_canq); 432 cc = tp->t_canq.c_cc; 433 TTY_UNLOCK(tp); 434 splx(s); 435 if (cc) 436 return (error); 437 } else 438 if (tp->t_oproc) 439 error = (*tp->t_linesw->l_read)(tp, uio, flag); 440 ptcwakeup(tp, FWRITE); 441 return (error); 442 } 443 444 /* 445 * Write to pseudo-tty. 446 * Wakeups of controlling tty will happen 447 * indirectly, when tty driver calls ptsstart. 448 */ 449 int 450 ptswrite(dev, uio, flag) 451 dev_t dev; 452 struct uio *uio; 453 int flag; 454 { 455 struct pt_softc *pti = pt_softc[minor(dev)]; 456 struct tty *tp = pti->pt_tty; 457 458 if (tp->t_oproc == 0) 459 return (EIO); 460 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 461 } 462 463 /* 464 * Poll pseudo-tty. 465 */ 466 int 467 ptspoll(dev, events, l) 468 dev_t dev; 469 int events; 470 struct lwp *l; 471 { 472 struct pt_softc *pti = pt_softc[minor(dev)]; 473 struct tty *tp = pti->pt_tty; 474 475 if (tp->t_oproc == 0) 476 return (POLLHUP); 477 478 return ((*tp->t_linesw->l_poll)(tp, events, l)); 479 } 480 481 /* 482 * Start output on pseudo-tty. 483 * Wake up process polling or sleeping for input from controlling tty. 484 * Called with tty slock held. 485 */ 486 void 487 ptsstart(tp) 488 struct tty *tp; 489 { 490 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 491 492 if (ISSET(tp->t_state, TS_TTSTOP)) 493 return; 494 if (pti->pt_flags & PF_STOPPED) { 495 pti->pt_flags &= ~PF_STOPPED; 496 pti->pt_send = TIOCPKT_START; 497 } 498 499 selnotify(&pti->pt_selr, NOTE_SUBMIT); 500 wakeup((caddr_t)&tp->t_outq.c_cf); 501 } 502 503 /* 504 * Stop output. 505 * Called with tty slock held. 506 */ 507 void 508 ptsstop(tp, flush) 509 struct tty *tp; 510 int flush; 511 { 512 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 513 514 /* note: FLUSHREAD and FLUSHWRITE already ok */ 515 if (flush == 0) { 516 flush = TIOCPKT_STOP; 517 pti->pt_flags |= PF_STOPPED; 518 } else 519 pti->pt_flags &= ~PF_STOPPED; 520 pti->pt_send |= flush; 521 522 /* change of perspective */ 523 if (flush & FREAD) { 524 selnotify(&pti->pt_selw, NOTE_SUBMIT); 525 wakeup((caddr_t)&tp->t_rawq.c_cf); 526 } 527 if (flush & FWRITE) { 528 selnotify(&pti->pt_selr, NOTE_SUBMIT); 529 wakeup((caddr_t)&tp->t_outq.c_cf); 530 } 531 } 532 533 void 534 ptcwakeup(tp, flag) 535 struct tty *tp; 536 int flag; 537 { 538 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 539 int s; 540 541 s = spltty(); 542 TTY_LOCK(tp); 543 if (flag & FREAD) { 544 selnotify(&pti->pt_selr, NOTE_SUBMIT); 545 wakeup((caddr_t)&tp->t_outq.c_cf); 546 } 547 if (flag & FWRITE) { 548 selnotify(&pti->pt_selw, NOTE_SUBMIT); 549 wakeup((caddr_t)&tp->t_rawq.c_cf); 550 } 551 TTY_UNLOCK(tp); 552 splx(s); 553 } 554 555 /*ARGSUSED*/ 556 int 557 ptcopen(dev, flag, devtype, l) 558 dev_t dev; 559 int flag, devtype; 560 struct lwp *l; 561 { 562 struct pt_softc *pti; 563 struct tty *tp; 564 int error; 565 int ptn = minor(dev); 566 int s; 567 568 if ((error = pty_check(ptn)) != 0) 569 return (error); 570 571 pti = pt_softc[ptn]; 572 tp = pti->pt_tty; 573 574 s = spltty(); 575 TTY_LOCK(tp); 576 if (tp->t_oproc) { 577 TTY_UNLOCK(tp); 578 splx(s); 579 return (EIO); 580 } 581 tp->t_oproc = ptsstart; 582 TTY_UNLOCK(tp); 583 splx(s); 584 (void)(*tp->t_linesw->l_modem)(tp, 1); 585 CLR(tp->t_lflag, EXTPROC); 586 pti->pt_flags = 0; 587 pti->pt_send = 0; 588 pti->pt_ucntl = 0; 589 return (0); 590 } 591 592 /*ARGSUSED*/ 593 int 594 ptcclose(dev, flag, devtype, l) 595 dev_t dev; 596 int flag, devtype; 597 struct lwp *l; 598 { 599 struct pt_softc *pti = pt_softc[minor(dev)]; 600 struct tty *tp = pti->pt_tty; 601 int s; 602 603 (void)(*tp->t_linesw->l_modem)(tp, 0); 604 CLR(tp->t_state, TS_CARR_ON); 605 s = spltty(); 606 TTY_LOCK(tp); 607 tp->t_oproc = 0; /* mark closed */ 608 TTY_UNLOCK(tp); 609 splx(s); 610 return (0); 611 } 612 613 int 614 ptcread(dev, uio, flag) 615 dev_t dev; 616 struct uio *uio; 617 int flag; 618 { 619 struct pt_softc *pti = pt_softc[minor(dev)]; 620 struct tty *tp = pti->pt_tty; 621 u_char bf[BUFSIZ]; 622 int error = 0, cc; 623 int s; 624 625 /* 626 * We want to block until the slave 627 * is open, and there's something to read; 628 * but if we lost the slave or we're NBIO, 629 * then return the appropriate error instead. 630 */ 631 s = spltty(); 632 TTY_LOCK(tp); 633 for (;;) { 634 if (ISSET(tp->t_state, TS_ISOPEN)) { 635 if (pti->pt_flags & PF_PKT && pti->pt_send) { 636 TTY_UNLOCK(tp); 637 splx(s); 638 error = ureadc((int)pti->pt_send, uio); 639 if (error) 640 return (error); 641 /* 642 * Since we don't have the tty locked, there's 643 * a risk of messing up `t_termios'. This is 644 * relevant only if the tty got closed and then 645 * opened again while we were out uiomoving. 646 */ 647 if (pti->pt_send & TIOCPKT_IOCTL) { 648 cc = min(uio->uio_resid, 649 sizeof(tp->t_termios)); 650 uiomove((caddr_t) &tp->t_termios, 651 cc, uio); 652 } 653 pti->pt_send = 0; 654 return (0); 655 } 656 if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) { 657 TTY_UNLOCK(tp); 658 splx(s); 659 error = ureadc((int)pti->pt_ucntl, uio); 660 if (error) 661 return (error); 662 pti->pt_ucntl = 0; 663 return (0); 664 } 665 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP)) 666 break; 667 } 668 if (!ISSET(tp->t_state, TS_CARR_ON)) { 669 error = 0; /* EOF */ 670 goto out; 671 } 672 if (flag & IO_NDELAY) { 673 error = EWOULDBLOCK; 674 goto out; 675 } 676 error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH, 677 ttyin, 0, &tp->t_slock); 678 if (error) 679 goto out; 680 } 681 682 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) { 683 TTY_UNLOCK(tp); 684 splx(s); 685 error = ureadc(0, uio); 686 s = spltty(); 687 TTY_LOCK(tp); 688 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN)) 689 error = EIO; 690 } 691 while (uio->uio_resid > 0 && error == 0) { 692 cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ)); 693 if (cc <= 0) 694 break; 695 TTY_UNLOCK(tp); 696 splx(s); 697 error = uiomove(bf, cc, uio); 698 s = spltty(); 699 TTY_LOCK(tp); 700 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN)) 701 error = EIO; 702 } 703 704 if (tp->t_outq.c_cc <= tp->t_lowat) { 705 if (ISSET(tp->t_state, TS_ASLEEP)) { 706 CLR(tp->t_state, TS_ASLEEP); 707 wakeup((caddr_t)&tp->t_outq); 708 } 709 selnotify(&tp->t_wsel, NOTE_SUBMIT); 710 } 711 out: 712 TTY_UNLOCK(tp); 713 splx(s); 714 return (error); 715 } 716 717 718 int 719 ptcwrite(dev, uio, flag) 720 dev_t dev; 721 struct uio *uio; 722 int flag; 723 { 724 struct pt_softc *pti = pt_softc[minor(dev)]; 725 struct tty *tp = pti->pt_tty; 726 u_char *cp = NULL; 727 int cc = 0; 728 u_char locbuf[BUFSIZ]; 729 int cnt = 0; 730 int error = 0; 731 int s; 732 733 again: 734 s = spltty(); 735 TTY_LOCK(tp); 736 if (!ISSET(tp->t_state, TS_ISOPEN)) 737 goto block; 738 if (pti->pt_flags & PF_REMOTE) { 739 if (tp->t_canq.c_cc) 740 goto block; 741 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) { 742 if (cc == 0) { 743 cc = min(uio->uio_resid, BUFSIZ); 744 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 745 cp = locbuf; 746 TTY_UNLOCK(tp); 747 splx(s); 748 error = uiomove((caddr_t)cp, cc, uio); 749 if (error) 750 return (error); 751 s = spltty(); 752 TTY_LOCK(tp); 753 /* check again for safety */ 754 if (!ISSET(tp->t_state, TS_ISOPEN)) { 755 /* 756 * adjust for data copied in but not 757 * written 758 */ 759 uio->uio_resid += cc; 760 error = EIO; 761 goto out; 762 } 763 } 764 if (cc) 765 (void) b_to_q(cp, cc, &tp->t_canq); 766 cc = 0; 767 } 768 (void) putc(0, &tp->t_canq); 769 ttwakeup(tp); 770 wakeup((caddr_t)&tp->t_canq); 771 error = 0; 772 goto out; 773 } 774 while (uio->uio_resid > 0) { 775 if (cc == 0) { 776 cc = min(uio->uio_resid, BUFSIZ); 777 cp = locbuf; 778 TTY_UNLOCK(tp); 779 splx(s); 780 error = uiomove((caddr_t)cp, cc, uio); 781 if (error) 782 return (error); 783 s = spltty(); 784 TTY_LOCK(tp); 785 /* check again for safety */ 786 if (!ISSET(tp->t_state, TS_ISOPEN)) { 787 /* adjust for data copied in but not written */ 788 uio->uio_resid += cc; 789 error = EIO; 790 goto out; 791 } 792 } 793 while (cc > 0) { 794 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 795 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) { 796 wakeup((caddr_t)&tp->t_rawq); 797 goto block; 798 } 799 /* XXX - should change l_rint to be called with lock 800 * see also tty.c:ttyinput_wlock() 801 */ 802 TTY_UNLOCK(tp); 803 splx(s); 804 (*tp->t_linesw->l_rint)(*cp++, tp); 805 s = spltty(); 806 TTY_LOCK(tp); 807 cnt++; 808 cc--; 809 } 810 cc = 0; 811 } 812 error = 0; 813 goto out; 814 815 block: 816 /* 817 * Come here to wait for slave to open, for space 818 * in outq, or space in rawq. 819 */ 820 if (!ISSET(tp->t_state, TS_CARR_ON)) { 821 /* adjust for data copied in but not written */ 822 uio->uio_resid += cc; 823 error = EIO; 824 goto out; 825 } 826 if (flag & IO_NDELAY) { 827 /* adjust for data copied in but not written */ 828 uio->uio_resid += cc; 829 error = cnt == 0 ? EWOULDBLOCK : 0; 830 goto out; 831 } 832 error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK, 833 ttyout, 0, &tp->t_slock); 834 splx(s); 835 if (error) { 836 /* adjust for data copied in but not written */ 837 uio->uio_resid += cc; 838 return (error); 839 } 840 goto again; 841 842 out: 843 TTY_UNLOCK(tp); 844 splx(s); 845 return (error); 846 } 847 848 int 849 ptcpoll(dev, events, l) 850 dev_t dev; 851 int events; 852 struct lwp *l; 853 { 854 struct pt_softc *pti = pt_softc[minor(dev)]; 855 struct tty *tp = pti->pt_tty; 856 int revents = 0; 857 int s = splsoftclock(); 858 859 if (events & (POLLIN | POLLRDNORM)) 860 if (ISSET(tp->t_state, TS_ISOPEN) && 861 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) || 862 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 863 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 864 revents |= events & (POLLIN | POLLRDNORM); 865 866 if (events & (POLLOUT | POLLWRNORM)) 867 if (ISSET(tp->t_state, TS_ISOPEN) && 868 ((pti->pt_flags & PF_REMOTE) ? 869 (tp->t_canq.c_cc == 0) : 870 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) || 871 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON))))) 872 revents |= events & (POLLOUT | POLLWRNORM); 873 874 if (events & POLLHUP) 875 if (!ISSET(tp->t_state, TS_CARR_ON)) 876 revents |= POLLHUP; 877 878 if (revents == 0) { 879 if (events & (POLLIN | POLLHUP | POLLRDNORM)) 880 selrecord(l, &pti->pt_selr); 881 882 if (events & (POLLOUT | POLLWRNORM)) 883 selrecord(l, &pti->pt_selw); 884 } 885 886 splx(s); 887 return (revents); 888 } 889 890 static void 891 filt_ptcrdetach(struct knote *kn) 892 { 893 struct pt_softc *pti; 894 int s; 895 896 pti = kn->kn_hook; 897 s = spltty(); 898 SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext); 899 splx(s); 900 } 901 902 static int 903 filt_ptcread(struct knote *kn, long hint) 904 { 905 struct pt_softc *pti; 906 struct tty *tp; 907 int canread; 908 909 pti = kn->kn_hook; 910 tp = pti->pt_tty; 911 912 canread = (ISSET(tp->t_state, TS_ISOPEN) && 913 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) || 914 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 915 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))); 916 917 if (canread) { 918 /* 919 * c_cc is number of characters after output post-processing; 920 * the amount of data actually read(2) depends on 921 * setting of input flags for the terminal. 922 */ 923 kn->kn_data = tp->t_outq.c_cc; 924 if (((pti->pt_flags & PF_PKT) && pti->pt_send) || 925 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 926 kn->kn_data++; 927 } 928 929 return (canread); 930 } 931 932 static void 933 filt_ptcwdetach(struct knote *kn) 934 { 935 struct pt_softc *pti; 936 int s; 937 938 pti = kn->kn_hook; 939 s = spltty(); 940 SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext); 941 splx(s); 942 } 943 944 static int 945 filt_ptcwrite(struct knote *kn, long hint) 946 { 947 struct pt_softc *pti; 948 struct tty *tp; 949 int canwrite; 950 int nwrite; 951 952 pti = kn->kn_hook; 953 tp = pti->pt_tty; 954 955 canwrite = (ISSET(tp->t_state, TS_ISOPEN) && 956 ((pti->pt_flags & PF_REMOTE) ? 957 (tp->t_canq.c_cc == 0) : 958 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) || 959 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON))))); 960 961 if (canwrite) { 962 if (pti->pt_flags & PF_REMOTE) 963 nwrite = tp->t_canq.c_cn; 964 else { 965 /* this is guaranteed to be > 0 due to above check */ 966 nwrite = tp->t_canq.c_cn 967 - (tp->t_rawq.c_cc + tp->t_canq.c_cc); 968 } 969 kn->kn_data = nwrite; 970 } 971 972 return (canwrite); 973 } 974 975 static const struct filterops ptcread_filtops = 976 { 1, NULL, filt_ptcrdetach, filt_ptcread }; 977 static const struct filterops ptcwrite_filtops = 978 { 1, NULL, filt_ptcwdetach, filt_ptcwrite }; 979 980 int 981 ptckqfilter(dev_t dev, struct knote *kn) 982 { 983 struct pt_softc *pti = pt_softc[minor(dev)]; 984 struct klist *klist; 985 int s; 986 987 switch (kn->kn_filter) { 988 case EVFILT_READ: 989 klist = &pti->pt_selr.sel_klist; 990 kn->kn_fop = &ptcread_filtops; 991 break; 992 case EVFILT_WRITE: 993 klist = &pti->pt_selw.sel_klist; 994 kn->kn_fop = &ptcwrite_filtops; 995 break; 996 default: 997 return (1); 998 } 999 1000 kn->kn_hook = pti; 1001 1002 s = spltty(); 1003 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1004 splx(s); 1005 1006 return (0); 1007 } 1008 1009 struct tty * 1010 ptytty(dev) 1011 dev_t dev; 1012 { 1013 struct pt_softc *pti = pt_softc[minor(dev)]; 1014 struct tty *tp = pti->pt_tty; 1015 1016 return (tp); 1017 } 1018 1019 /*ARGSUSED*/ 1020 int 1021 ptyioctl(dev, cmd, data, flag, l) 1022 dev_t dev; 1023 u_long cmd; 1024 caddr_t data; 1025 int flag; 1026 struct lwp *l; 1027 { 1028 struct pt_softc *pti = pt_softc[minor(dev)]; 1029 struct tty *tp = pti->pt_tty; 1030 const struct cdevsw *cdev; 1031 u_char *cc = tp->t_cc; 1032 int stop, error, sig; 1033 int s; 1034 1035 /* 1036 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 1037 * ttywflush(tp) will hang if there are characters in the outq. 1038 */ 1039 if (cmd == TIOCEXT) { 1040 /* 1041 * When the EXTPROC bit is being toggled, we need 1042 * to send an TIOCPKT_IOCTL if the packet driver 1043 * is turned on. 1044 */ 1045 if (*(int *)data) { 1046 if (pti->pt_flags & PF_PKT) { 1047 pti->pt_send |= TIOCPKT_IOCTL; 1048 ptcwakeup(tp, FREAD); 1049 } 1050 SET(tp->t_lflag, EXTPROC); 1051 } else { 1052 if (ISSET(tp->t_lflag, EXTPROC) && 1053 (pti->pt_flags & PF_PKT)) { 1054 pti->pt_send |= TIOCPKT_IOCTL; 1055 ptcwakeup(tp, FREAD); 1056 } 1057 CLR(tp->t_lflag, EXTPROC); 1058 } 1059 return(0); 1060 } 1061 1062 #ifndef NO_DEV_PTM 1063 /* Allow getting the name from either the master or the slave */ 1064 if (cmd == TIOCPTSNAME) 1065 return pty_fill_ptmget(l, dev, -1, -1, data); 1066 #endif 1067 1068 cdev = cdevsw_lookup(dev); 1069 if (cdev != NULL && cdev->d_open == ptcopen) 1070 switch (cmd) { 1071 #ifndef NO_DEV_PTM 1072 case TIOCGRANTPT: 1073 return pty_grant_slave(l, dev); 1074 #endif 1075 1076 case TIOCGPGRP: 1077 /* 1078 * We avoid calling ttioctl on the controller since, 1079 * in that case, tp must be the controlling terminal. 1080 */ 1081 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 1082 return (0); 1083 1084 case TIOCPKT: 1085 if (*(int *)data) { 1086 if (pti->pt_flags & PF_UCNTL) 1087 return (EINVAL); 1088 pti->pt_flags |= PF_PKT; 1089 } else 1090 pti->pt_flags &= ~PF_PKT; 1091 return (0); 1092 1093 case TIOCUCNTL: 1094 if (*(int *)data) { 1095 if (pti->pt_flags & PF_PKT) 1096 return (EINVAL); 1097 pti->pt_flags |= PF_UCNTL; 1098 } else 1099 pti->pt_flags &= ~PF_UCNTL; 1100 return (0); 1101 1102 case TIOCREMOTE: 1103 if (*(int *)data) 1104 pti->pt_flags |= PF_REMOTE; 1105 else 1106 pti->pt_flags &= ~PF_REMOTE; 1107 s = spltty(); 1108 TTY_LOCK(tp); 1109 ttyflush(tp, FREAD|FWRITE); 1110 TTY_UNLOCK(tp); 1111 splx(s); 1112 return (0); 1113 1114 #ifdef COMPAT_OLDTTY 1115 case TIOCSETP: 1116 case TIOCSETN: 1117 #endif 1118 case TIOCSETD: 1119 case TIOCSETA: 1120 case TIOCSETAW: 1121 case TIOCSETAF: 1122 TTY_LOCK(tp); 1123 ndflush(&tp->t_outq, tp->t_outq.c_cc); 1124 TTY_UNLOCK(tp); 1125 break; 1126 1127 case TIOCSIG: 1128 sig = (int)(long)*(caddr_t *)data; 1129 if (sig <= 0 || sig >= NSIG) 1130 return (EINVAL); 1131 TTY_LOCK(tp); 1132 if (!ISSET(tp->t_lflag, NOFLSH)) 1133 ttyflush(tp, FREAD|FWRITE); 1134 if ((sig == SIGINFO) && 1135 (!ISSET(tp->t_lflag, NOKERNINFO))) 1136 ttyinfo(tp, 1); 1137 TTY_UNLOCK(tp); 1138 pgsignal(tp->t_pgrp, sig, 1); 1139 return(0); 1140 } 1141 1142 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 1143 if (error == EPASSTHROUGH) 1144 error = ttioctl(tp, cmd, data, flag, l); 1145 if (error == EPASSTHROUGH) { 1146 if (pti->pt_flags & PF_UCNTL && 1147 (cmd & ~0xff) == UIOCCMD(0)) { 1148 if (cmd & 0xff) { 1149 pti->pt_ucntl = (u_char)cmd; 1150 ptcwakeup(tp, FREAD); 1151 } 1152 return (0); 1153 } 1154 } 1155 /* 1156 * If external processing and packet mode send ioctl packet. 1157 */ 1158 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) { 1159 switch(cmd) { 1160 case TIOCSETA: 1161 case TIOCSETAW: 1162 case TIOCSETAF: 1163 #ifdef COMPAT_OLDTTY 1164 case TIOCSETP: 1165 case TIOCSETN: 1166 case TIOCSETC: 1167 case TIOCSLTC: 1168 case TIOCLBIS: 1169 case TIOCLBIC: 1170 case TIOCLSET: 1171 #endif 1172 pti->pt_send |= TIOCPKT_IOCTL; 1173 ptcwakeup(tp, FREAD); 1174 default: 1175 break; 1176 } 1177 } 1178 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s')) 1179 && CCEQ(cc[VSTART], CTRL('q')); 1180 if (pti->pt_flags & PF_NOSTOP) { 1181 if (stop) { 1182 pti->pt_send &= ~TIOCPKT_NOSTOP; 1183 pti->pt_send |= TIOCPKT_DOSTOP; 1184 pti->pt_flags &= ~PF_NOSTOP; 1185 ptcwakeup(tp, FREAD); 1186 } 1187 } else { 1188 if (!stop) { 1189 pti->pt_send &= ~TIOCPKT_DOSTOP; 1190 pti->pt_send |= TIOCPKT_NOSTOP; 1191 pti->pt_flags |= PF_NOSTOP; 1192 ptcwakeup(tp, FREAD); 1193 } 1194 } 1195 return (error); 1196 } 1197