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