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