1 /* $NetBSD: tty_pty.c,v 1.86 2005/12/11 12:24:30 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.86 2005/12/11 12:24:30 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, l) 315 dev_t dev; 316 int flag, devtype; 317 struct lwp *l; 318 { 319 struct proc *p = l->l_proc; 320 struct pt_softc *pti; 321 struct tty *tp; 322 int error; 323 int ptn = minor(dev); 324 int s; 325 326 if ((error = pty_check(ptn)) != 0) 327 return (error); 328 329 pti = pt_softc[ptn]; 330 tp = pti->pt_tty; 331 332 if (!ISSET(tp->t_state, TS_ISOPEN)) { 333 ttychars(tp); /* Set up default chars */ 334 tp->t_iflag = TTYDEF_IFLAG; 335 tp->t_oflag = TTYDEF_OFLAG; 336 tp->t_lflag = TTYDEF_LFLAG; 337 tp->t_cflag = TTYDEF_CFLAG; 338 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 339 ttsetwater(tp); /* would be done in xxparam() */ 340 } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0) 341 return (EBUSY); 342 if (tp->t_oproc) /* Ctrlr still around. */ 343 SET(tp->t_state, TS_CARR_ON); 344 345 if (!ISSET(flag, O_NONBLOCK)) { 346 s = spltty(); 347 TTY_LOCK(tp); 348 while (!ISSET(tp->t_state, TS_CARR_ON)) { 349 tp->t_wopen++; 350 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, 351 ttopen, 0); 352 tp->t_wopen--; 353 if (error) { 354 TTY_UNLOCK(tp); 355 splx(s); 356 return (error); 357 } 358 } 359 TTY_UNLOCK(tp); 360 splx(s); 361 } 362 error = (*tp->t_linesw->l_open)(dev, tp); 363 ptcwakeup(tp, FREAD|FWRITE); 364 return (error); 365 } 366 367 int 368 ptsclose(dev, flag, mode, l) 369 dev_t dev; 370 int flag, mode; 371 struct lwp *l; 372 { 373 struct pt_softc *pti = pt_softc[minor(dev)]; 374 struct tty *tp = pti->pt_tty; 375 int error; 376 377 error = (*tp->t_linesw->l_close)(tp, flag); 378 error |= ttyclose(tp); 379 ptcwakeup(tp, FREAD|FWRITE); 380 return (error); 381 } 382 383 int 384 ptsread(dev, uio, flag) 385 dev_t dev; 386 struct uio *uio; 387 int flag; 388 { 389 struct proc *p = curproc; 390 struct pt_softc *pti = pt_softc[minor(dev)]; 391 struct tty *tp = pti->pt_tty; 392 int error = 0; 393 int cc; 394 int s; 395 396 again: 397 if (pti->pt_flags & PF_REMOTE) { 398 while (isbackground(p, tp)) { 399 if (sigismasked(p, SIGTTIN) || 400 p->p_pgrp->pg_jobc == 0 || 401 p->p_flag & P_PPWAIT) 402 return (EIO); 403 pgsignal(p->p_pgrp, SIGTTIN, 1); 404 s = spltty(); 405 TTY_LOCK(tp); 406 error = ttysleep(tp, (caddr_t)&lbolt, 407 TTIPRI | PCATCH | PNORELOCK, ttybg, 0); 408 splx(s); 409 if (error) 410 return (error); 411 } 412 s = spltty(); 413 TTY_LOCK(tp); 414 if (tp->t_canq.c_cc == 0) { 415 if (flag & IO_NDELAY) { 416 TTY_UNLOCK(tp); 417 splx(s); 418 return (EWOULDBLOCK); 419 } 420 error = ttysleep(tp, (caddr_t)&tp->t_canq, 421 TTIPRI | PCATCH | PNORELOCK, ttyin, 0); 422 splx(s); 423 if (error) 424 return (error); 425 goto again; 426 } 427 while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) { 428 TTY_UNLOCK(tp); 429 splx(s); 430 error = ureadc(getc(&tp->t_canq), uio); 431 s = spltty(); 432 TTY_LOCK(tp); 433 /* Re-check terminal state here? */ 434 } 435 if (tp->t_canq.c_cc == 1) 436 (void) getc(&tp->t_canq); 437 cc = tp->t_canq.c_cc; 438 TTY_UNLOCK(tp); 439 splx(s); 440 if (cc) 441 return (error); 442 } else 443 if (tp->t_oproc) 444 error = (*tp->t_linesw->l_read)(tp, uio, flag); 445 ptcwakeup(tp, FWRITE); 446 return (error); 447 } 448 449 /* 450 * Write to pseudo-tty. 451 * Wakeups of controlling tty will happen 452 * indirectly, when tty driver calls ptsstart. 453 */ 454 int 455 ptswrite(dev, uio, flag) 456 dev_t dev; 457 struct uio *uio; 458 int flag; 459 { 460 struct pt_softc *pti = pt_softc[minor(dev)]; 461 struct tty *tp = pti->pt_tty; 462 463 if (tp->t_oproc == 0) 464 return (EIO); 465 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 466 } 467 468 /* 469 * Poll pseudo-tty. 470 */ 471 int 472 ptspoll(dev, events, l) 473 dev_t dev; 474 int events; 475 struct lwp *l; 476 { 477 struct pt_softc *pti = pt_softc[minor(dev)]; 478 struct tty *tp = pti->pt_tty; 479 480 if (tp->t_oproc == 0) 481 return (POLLHUP); 482 483 return ((*tp->t_linesw->l_poll)(tp, events, l)); 484 } 485 486 /* 487 * Start output on pseudo-tty. 488 * Wake up process polling or sleeping for input from controlling tty. 489 * Called with tty slock held. 490 */ 491 void 492 ptsstart(tp) 493 struct tty *tp; 494 { 495 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 496 497 if (ISSET(tp->t_state, TS_TTSTOP)) 498 return; 499 if (pti->pt_flags & PF_STOPPED) { 500 pti->pt_flags &= ~PF_STOPPED; 501 pti->pt_send = TIOCPKT_START; 502 } 503 504 selnotify(&pti->pt_selr, NOTE_SUBMIT); 505 wakeup((caddr_t)&tp->t_outq.c_cf); 506 } 507 508 /* 509 * Stop output. 510 * Called with tty slock held. 511 */ 512 void 513 ptsstop(tp, flush) 514 struct tty *tp; 515 int flush; 516 { 517 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 518 519 /* note: FLUSHREAD and FLUSHWRITE already ok */ 520 if (flush == 0) { 521 flush = TIOCPKT_STOP; 522 pti->pt_flags |= PF_STOPPED; 523 } else 524 pti->pt_flags &= ~PF_STOPPED; 525 pti->pt_send |= flush; 526 527 /* change of perspective */ 528 if (flush & FREAD) { 529 selnotify(&pti->pt_selw, NOTE_SUBMIT); 530 wakeup((caddr_t)&tp->t_rawq.c_cf); 531 } 532 if (flush & FWRITE) { 533 selnotify(&pti->pt_selr, NOTE_SUBMIT); 534 wakeup((caddr_t)&tp->t_outq.c_cf); 535 } 536 } 537 538 void 539 ptcwakeup(tp, flag) 540 struct tty *tp; 541 int flag; 542 { 543 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 544 int s; 545 546 s = spltty(); 547 TTY_LOCK(tp); 548 if (flag & FREAD) { 549 selnotify(&pti->pt_selr, NOTE_SUBMIT); 550 wakeup((caddr_t)&tp->t_outq.c_cf); 551 } 552 if (flag & FWRITE) { 553 selnotify(&pti->pt_selw, NOTE_SUBMIT); 554 wakeup((caddr_t)&tp->t_rawq.c_cf); 555 } 556 TTY_UNLOCK(tp); 557 splx(s); 558 } 559 560 /*ARGSUSED*/ 561 int 562 ptcopen(dev, flag, devtype, l) 563 dev_t dev; 564 int flag, devtype; 565 struct lwp *l; 566 { 567 struct pt_softc *pti; 568 struct tty *tp; 569 int error; 570 int ptn = minor(dev); 571 int s; 572 573 if ((error = pty_check(ptn)) != 0) 574 return (error); 575 576 pti = pt_softc[ptn]; 577 tp = pti->pt_tty; 578 579 s = spltty(); 580 TTY_LOCK(tp); 581 if (tp->t_oproc) { 582 TTY_UNLOCK(tp); 583 splx(s); 584 return (EIO); 585 } 586 tp->t_oproc = ptsstart; 587 TTY_UNLOCK(tp); 588 splx(s); 589 (void)(*tp->t_linesw->l_modem)(tp, 1); 590 CLR(tp->t_lflag, EXTPROC); 591 pti->pt_flags = 0; 592 pti->pt_send = 0; 593 pti->pt_ucntl = 0; 594 return (0); 595 } 596 597 /*ARGSUSED*/ 598 int 599 ptcclose(dev, flag, devtype, l) 600 dev_t dev; 601 int flag, devtype; 602 struct lwp *l; 603 { 604 struct pt_softc *pti = pt_softc[minor(dev)]; 605 struct tty *tp = pti->pt_tty; 606 int s; 607 608 (void)(*tp->t_linesw->l_modem)(tp, 0); 609 CLR(tp->t_state, TS_CARR_ON); 610 s = spltty(); 611 TTY_LOCK(tp); 612 tp->t_oproc = 0; /* mark closed */ 613 TTY_UNLOCK(tp); 614 splx(s); 615 return (0); 616 } 617 618 int 619 ptcread(dev, uio, flag) 620 dev_t dev; 621 struct uio *uio; 622 int flag; 623 { 624 struct pt_softc *pti = pt_softc[minor(dev)]; 625 struct tty *tp = pti->pt_tty; 626 u_char bf[BUFSIZ]; 627 int error = 0, cc; 628 int s; 629 630 /* 631 * We want to block until the slave 632 * is open, and there's something to read; 633 * but if we lost the slave or we're NBIO, 634 * then return the appropriate error instead. 635 */ 636 s = spltty(); 637 TTY_LOCK(tp); 638 for (;;) { 639 if (ISSET(tp->t_state, TS_ISOPEN)) { 640 if (pti->pt_flags & PF_PKT && pti->pt_send) { 641 TTY_UNLOCK(tp); 642 splx(s); 643 error = ureadc((int)pti->pt_send, uio); 644 if (error) 645 return (error); 646 /* 647 * Since we don't have the tty locked, there's 648 * a risk of messing up `t_termios'. This is 649 * relevant only if the tty got closed and then 650 * opened again while we were out uiomoving. 651 */ 652 if (pti->pt_send & TIOCPKT_IOCTL) { 653 cc = min(uio->uio_resid, 654 sizeof(tp->t_termios)); 655 uiomove((caddr_t) &tp->t_termios, 656 cc, uio); 657 } 658 pti->pt_send = 0; 659 return (0); 660 } 661 if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) { 662 TTY_UNLOCK(tp); 663 splx(s); 664 error = ureadc((int)pti->pt_ucntl, uio); 665 if (error) 666 return (error); 667 pti->pt_ucntl = 0; 668 return (0); 669 } 670 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP)) 671 break; 672 } 673 if (!ISSET(tp->t_state, TS_CARR_ON)) { 674 error = 0; /* EOF */ 675 goto out; 676 } 677 if (flag & IO_NDELAY) { 678 error = EWOULDBLOCK; 679 goto out; 680 } 681 error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH, 682 ttyin, 0, &tp->t_slock); 683 if (error) 684 goto out; 685 } 686 687 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) { 688 TTY_UNLOCK(tp); 689 splx(s); 690 error = ureadc(0, uio); 691 s = spltty(); 692 TTY_LOCK(tp); 693 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN)) 694 error = EIO; 695 } 696 while (uio->uio_resid > 0 && error == 0) { 697 cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ)); 698 if (cc <= 0) 699 break; 700 TTY_UNLOCK(tp); 701 splx(s); 702 error = uiomove(bf, cc, uio); 703 s = spltty(); 704 TTY_LOCK(tp); 705 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN)) 706 error = EIO; 707 } 708 709 if (tp->t_outq.c_cc <= tp->t_lowat) { 710 if (ISSET(tp->t_state, TS_ASLEEP)) { 711 CLR(tp->t_state, TS_ASLEEP); 712 wakeup((caddr_t)&tp->t_outq); 713 } 714 selnotify(&tp->t_wsel, NOTE_SUBMIT); 715 } 716 out: 717 TTY_UNLOCK(tp); 718 splx(s); 719 return (error); 720 } 721 722 723 int 724 ptcwrite(dev, uio, flag) 725 dev_t dev; 726 struct uio *uio; 727 int flag; 728 { 729 struct pt_softc *pti = pt_softc[minor(dev)]; 730 struct tty *tp = pti->pt_tty; 731 u_char *cp = NULL; 732 int cc = 0; 733 u_char locbuf[BUFSIZ]; 734 int cnt = 0; 735 int error = 0; 736 int s; 737 738 again: 739 s = spltty(); 740 TTY_LOCK(tp); 741 if (!ISSET(tp->t_state, TS_ISOPEN)) 742 goto block; 743 if (pti->pt_flags & PF_REMOTE) { 744 if (tp->t_canq.c_cc) 745 goto block; 746 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) { 747 if (cc == 0) { 748 cc = min(uio->uio_resid, BUFSIZ); 749 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 750 cp = locbuf; 751 TTY_UNLOCK(tp); 752 splx(s); 753 error = uiomove((caddr_t)cp, cc, uio); 754 if (error) 755 return (error); 756 s = spltty(); 757 TTY_LOCK(tp); 758 /* check again for safety */ 759 if (!ISSET(tp->t_state, TS_ISOPEN)) { 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 error = EIO; 788 goto out; 789 } 790 } 791 while (cc > 0) { 792 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 793 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) { 794 wakeup((caddr_t)&tp->t_rawq); 795 goto block; 796 } 797 /* XXX - should change l_rint to be called with lock 798 * see also tty.c:ttyinput_wlock() 799 */ 800 TTY_UNLOCK(tp); 801 splx(s); 802 (*tp->t_linesw->l_rint)(*cp++, tp); 803 s = spltty(); 804 TTY_LOCK(tp); 805 cnt++; 806 cc--; 807 } 808 cc = 0; 809 } 810 error = 0; 811 goto out; 812 813 block: 814 /* 815 * Come here to wait for slave to open, for space 816 * in outq, or space in rawq. 817 */ 818 if (!ISSET(tp->t_state, TS_CARR_ON)) { 819 error = EIO; 820 goto out; 821 } 822 if (flag & IO_NDELAY) { 823 /* adjust for data copied in but not written */ 824 uio->uio_resid += cc; 825 error = cnt == 0 ? EWOULDBLOCK : 0; 826 goto out; 827 } 828 error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK, 829 ttyout, 0, &tp->t_slock); 830 splx(s); 831 if (error) { 832 /* adjust for data copied in but not written */ 833 uio->uio_resid += cc; 834 return (error); 835 } 836 goto again; 837 838 out: 839 TTY_UNLOCK(tp); 840 splx(s); 841 return (error); 842 } 843 844 int 845 ptcpoll(dev, events, l) 846 dev_t dev; 847 int events; 848 struct lwp *l; 849 { 850 struct pt_softc *pti = pt_softc[minor(dev)]; 851 struct tty *tp = pti->pt_tty; 852 int revents = 0; 853 int s = splsoftclock(); 854 855 if (events & (POLLIN | POLLRDNORM)) 856 if (ISSET(tp->t_state, TS_ISOPEN) && 857 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) || 858 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 859 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 860 revents |= events & (POLLIN | POLLRDNORM); 861 862 if (events & (POLLOUT | POLLWRNORM)) 863 if (ISSET(tp->t_state, TS_ISOPEN) && 864 ((pti->pt_flags & PF_REMOTE) ? 865 (tp->t_canq.c_cc == 0) : 866 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) || 867 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON))))) 868 revents |= events & (POLLOUT | POLLWRNORM); 869 870 if (events & POLLHUP) 871 if (!ISSET(tp->t_state, TS_CARR_ON)) 872 revents |= POLLHUP; 873 874 if (revents == 0) { 875 if (events & (POLLIN | POLLHUP | POLLRDNORM)) 876 selrecord(l, &pti->pt_selr); 877 878 if (events & (POLLOUT | POLLWRNORM)) 879 selrecord(l, &pti->pt_selw); 880 } 881 882 splx(s); 883 return (revents); 884 } 885 886 static void 887 filt_ptcrdetach(struct knote *kn) 888 { 889 struct pt_softc *pti; 890 int s; 891 892 pti = kn->kn_hook; 893 s = spltty(); 894 SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext); 895 splx(s); 896 } 897 898 static int 899 filt_ptcread(struct knote *kn, long hint) 900 { 901 struct pt_softc *pti; 902 struct tty *tp; 903 int canread; 904 905 pti = kn->kn_hook; 906 tp = pti->pt_tty; 907 908 canread = (ISSET(tp->t_state, TS_ISOPEN) && 909 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) || 910 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 911 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))); 912 913 if (canread) { 914 /* 915 * c_cc is number of characters after output post-processing; 916 * the amount of data actually read(2) depends on 917 * setting of input flags for the terminal. 918 */ 919 kn->kn_data = tp->t_outq.c_cc; 920 if (((pti->pt_flags & PF_PKT) && pti->pt_send) || 921 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 922 kn->kn_data++; 923 } 924 925 return (canread); 926 } 927 928 static void 929 filt_ptcwdetach(struct knote *kn) 930 { 931 struct pt_softc *pti; 932 int s; 933 934 pti = kn->kn_hook; 935 s = spltty(); 936 SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext); 937 splx(s); 938 } 939 940 static int 941 filt_ptcwrite(struct knote *kn, long hint) 942 { 943 struct pt_softc *pti; 944 struct tty *tp; 945 int canwrite; 946 int nwrite; 947 948 pti = kn->kn_hook; 949 tp = pti->pt_tty; 950 951 canwrite = (ISSET(tp->t_state, TS_ISOPEN) && 952 ((pti->pt_flags & PF_REMOTE) ? 953 (tp->t_canq.c_cc == 0) : 954 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) || 955 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON))))); 956 957 if (canwrite) { 958 if (pti->pt_flags & PF_REMOTE) 959 nwrite = tp->t_canq.c_cn; 960 else { 961 /* this is guaranteed to be > 0 due to above check */ 962 nwrite = tp->t_canq.c_cn 963 - (tp->t_rawq.c_cc + tp->t_canq.c_cc); 964 } 965 kn->kn_data = nwrite; 966 } 967 968 return (canwrite); 969 } 970 971 static const struct filterops ptcread_filtops = 972 { 1, NULL, filt_ptcrdetach, filt_ptcread }; 973 static const struct filterops ptcwrite_filtops = 974 { 1, NULL, filt_ptcwdetach, filt_ptcwrite }; 975 976 int 977 ptckqfilter(dev_t dev, struct knote *kn) 978 { 979 struct pt_softc *pti = pt_softc[minor(dev)]; 980 struct klist *klist; 981 int s; 982 983 switch (kn->kn_filter) { 984 case EVFILT_READ: 985 klist = &pti->pt_selr.sel_klist; 986 kn->kn_fop = &ptcread_filtops; 987 break; 988 case EVFILT_WRITE: 989 klist = &pti->pt_selw.sel_klist; 990 kn->kn_fop = &ptcwrite_filtops; 991 break; 992 default: 993 return (1); 994 } 995 996 kn->kn_hook = pti; 997 998 s = spltty(); 999 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1000 splx(s); 1001 1002 return (0); 1003 } 1004 1005 struct tty * 1006 ptytty(dev) 1007 dev_t dev; 1008 { 1009 struct pt_softc *pti = pt_softc[minor(dev)]; 1010 struct tty *tp = pti->pt_tty; 1011 1012 return (tp); 1013 } 1014 1015 /*ARGSUSED*/ 1016 int 1017 ptyioctl(dev, cmd, data, flag, l) 1018 dev_t dev; 1019 u_long cmd; 1020 caddr_t data; 1021 int flag; 1022 struct lwp *l; 1023 { 1024 struct pt_softc *pti = pt_softc[minor(dev)]; 1025 struct tty *tp = pti->pt_tty; 1026 const struct cdevsw *cdev; 1027 u_char *cc = tp->t_cc; 1028 int stop, error, sig; 1029 int s; 1030 1031 /* 1032 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 1033 * ttywflush(tp) will hang if there are characters in the outq. 1034 */ 1035 if (cmd == TIOCEXT) { 1036 /* 1037 * When the EXTPROC bit is being toggled, we need 1038 * to send an TIOCPKT_IOCTL if the packet driver 1039 * is turned on. 1040 */ 1041 if (*(int *)data) { 1042 if (pti->pt_flags & PF_PKT) { 1043 pti->pt_send |= TIOCPKT_IOCTL; 1044 ptcwakeup(tp, FREAD); 1045 } 1046 SET(tp->t_lflag, EXTPROC); 1047 } else { 1048 if (ISSET(tp->t_lflag, EXTPROC) && 1049 (pti->pt_flags & PF_PKT)) { 1050 pti->pt_send |= TIOCPKT_IOCTL; 1051 ptcwakeup(tp, FREAD); 1052 } 1053 CLR(tp->t_lflag, EXTPROC); 1054 } 1055 return(0); 1056 } 1057 1058 #ifndef NO_DEV_PTM 1059 /* Allow getting the name from either the master or the slave */ 1060 if (cmd == TIOCPTSNAME) 1061 return pty_fill_ptmget(dev, -1, -1, data); 1062 #endif 1063 1064 cdev = cdevsw_lookup(dev); 1065 if (cdev != NULL && cdev->d_open == ptcopen) 1066 switch (cmd) { 1067 #ifndef NO_DEV_PTM 1068 case TIOCGRANTPT: 1069 return pty_grant_slave(l, dev); 1070 #endif 1071 1072 case TIOCGPGRP: 1073 /* 1074 * We avoid calling ttioctl on the controller since, 1075 * in that case, tp must be the controlling terminal. 1076 */ 1077 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 1078 return (0); 1079 1080 case TIOCPKT: 1081 if (*(int *)data) { 1082 if (pti->pt_flags & PF_UCNTL) 1083 return (EINVAL); 1084 pti->pt_flags |= PF_PKT; 1085 } else 1086 pti->pt_flags &= ~PF_PKT; 1087 return (0); 1088 1089 case TIOCUCNTL: 1090 if (*(int *)data) { 1091 if (pti->pt_flags & PF_PKT) 1092 return (EINVAL); 1093 pti->pt_flags |= PF_UCNTL; 1094 } else 1095 pti->pt_flags &= ~PF_UCNTL; 1096 return (0); 1097 1098 case TIOCREMOTE: 1099 if (*(int *)data) 1100 pti->pt_flags |= PF_REMOTE; 1101 else 1102 pti->pt_flags &= ~PF_REMOTE; 1103 s = spltty(); 1104 TTY_LOCK(tp); 1105 ttyflush(tp, FREAD|FWRITE); 1106 TTY_UNLOCK(tp); 1107 splx(s); 1108 return (0); 1109 1110 #ifdef COMPAT_OLDTTY 1111 case TIOCSETP: 1112 case TIOCSETN: 1113 #endif 1114 case TIOCSETD: 1115 case TIOCSETA: 1116 case TIOCSETAW: 1117 case TIOCSETAF: 1118 TTY_LOCK(tp); 1119 ndflush(&tp->t_outq, tp->t_outq.c_cc); 1120 TTY_UNLOCK(tp); 1121 break; 1122 1123 case TIOCSIG: 1124 sig = (int)(long)*(caddr_t *)data; 1125 if (sig <= 0 || sig >= NSIG) 1126 return (EINVAL); 1127 TTY_LOCK(tp); 1128 if (!ISSET(tp->t_lflag, NOFLSH)) 1129 ttyflush(tp, FREAD|FWRITE); 1130 if ((sig == SIGINFO) && 1131 (!ISSET(tp->t_lflag, NOKERNINFO))) 1132 ttyinfo(tp); 1133 TTY_UNLOCK(tp); 1134 pgsignal(tp->t_pgrp, sig, 1); 1135 return(0); 1136 } 1137 1138 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 1139 if (error == EPASSTHROUGH) 1140 error = ttioctl(tp, cmd, data, flag, l); 1141 if (error == EPASSTHROUGH) { 1142 if (pti->pt_flags & PF_UCNTL && 1143 (cmd & ~0xff) == UIOCCMD(0)) { 1144 if (cmd & 0xff) { 1145 pti->pt_ucntl = (u_char)cmd; 1146 ptcwakeup(tp, FREAD); 1147 } 1148 return (0); 1149 } 1150 } 1151 /* 1152 * If external processing and packet mode send ioctl packet. 1153 */ 1154 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) { 1155 switch(cmd) { 1156 case TIOCSETA: 1157 case TIOCSETAW: 1158 case TIOCSETAF: 1159 #ifdef COMPAT_OLDTTY 1160 case TIOCSETP: 1161 case TIOCSETN: 1162 case TIOCSETC: 1163 case TIOCSLTC: 1164 case TIOCLBIS: 1165 case TIOCLBIC: 1166 case TIOCLSET: 1167 #endif 1168 pti->pt_send |= TIOCPKT_IOCTL; 1169 ptcwakeup(tp, FREAD); 1170 default: 1171 break; 1172 } 1173 } 1174 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s')) 1175 && CCEQ(cc[VSTART], CTRL('q')); 1176 if (pti->pt_flags & PF_NOSTOP) { 1177 if (stop) { 1178 pti->pt_send &= ~TIOCPKT_NOSTOP; 1179 pti->pt_send |= TIOCPKT_DOSTOP; 1180 pti->pt_flags &= ~PF_NOSTOP; 1181 ptcwakeup(tp, FREAD); 1182 } 1183 } else { 1184 if (!stop) { 1185 pti->pt_send &= ~TIOCPKT_DOSTOP; 1186 pti->pt_send |= TIOCPKT_NOSTOP; 1187 pti->pt_flags |= PF_NOSTOP; 1188 ptcwakeup(tp, FREAD); 1189 } 1190 } 1191 return (error); 1192 } 1193