1 /* $OpenBSD: tty_pty.c,v 1.16 2003/10/03 16:44:51 miod Exp $ */ 2 /* $NetBSD: tty_pty.c,v 1.33.4.1 1996/06/02 09:08:11 mrg Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95 33 */ 34 35 /* 36 * Pseudo-teletype Driver 37 * (Actually two drivers, requiring two entries in 'cdevsw') 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/ioctl.h> 43 #include <sys/proc.h> 44 #include <sys/tty.h> 45 #include <sys/file.h> 46 #include <sys/uio.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/vnode.h> 50 #include <sys/signalvar.h> 51 #include <sys/uio.h> 52 #include <sys/conf.h> 53 #include <sys/poll.h> 54 55 #define BUFSIZ 100 /* Chunk size iomoved to/from user */ 56 57 /* 58 * pts == /dev/tty[pqrs]? 59 * ptc == /dev/pty[pqrs]? 60 */ 61 struct pt_softc { 62 struct tty *pt_tty; 63 int pt_flags; 64 struct selinfo pt_selr, pt_selw; 65 u_char pt_send; 66 u_char pt_ucntl; 67 } *pt_softc; 68 int npty; 69 70 #define PF_PKT 0x08 /* packet mode */ 71 #define PF_STOPPED 0x10 /* user told stopped */ 72 #define PF_REMOTE 0x20 /* remote and flow controlled input */ 73 #define PF_NOSTOP 0x40 74 #define PF_UCNTL 0x80 /* user control mode */ 75 76 void ptyattach(int); 77 void ptcwakeup(struct tty *, int); 78 struct tty *ptytty(dev_t); 79 void ptsstart(struct tty *); 80 81 void filt_ptcrdetach(struct knote *); 82 int filt_ptcread(struct knote *, long); 83 void filt_ptcwdetach(struct knote *); 84 int filt_ptcwrite(struct knote *, long); 85 86 /* 87 * Establish n (or default if n is 1) ptys in the system. 88 */ 89 void 90 ptyattach(n) 91 int n; 92 { 93 #define DEFAULT_NPTY 32 94 95 /* maybe should allow 0 => none? */ 96 if (n <= 1) 97 n = DEFAULT_NPTY; 98 pt_softc = malloc(n * sizeof(struct pt_softc), M_DEVBUF, M_WAITOK); 99 bzero(pt_softc, n * sizeof(struct pt_softc)); 100 npty = n; 101 } 102 103 /*ARGSUSED*/ 104 int 105 ptsopen(dev, flag, devtype, p) 106 dev_t dev; 107 int flag, devtype; 108 struct proc *p; 109 { 110 struct pt_softc *pti; 111 register struct tty *tp; 112 int error; 113 114 if (minor(dev) >= npty) 115 return (ENXIO); 116 pti = &pt_softc[minor(dev)]; 117 if (!pti->pt_tty) { 118 tp = pti->pt_tty = ttymalloc(); 119 } else 120 tp = pti->pt_tty; 121 if ((tp->t_state & TS_ISOPEN) == 0) { 122 tp->t_state |= TS_WOPEN; 123 ttychars(tp); /* Set up default chars */ 124 tp->t_iflag = TTYDEF_IFLAG; 125 tp->t_oflag = TTYDEF_OFLAG; 126 tp->t_lflag = TTYDEF_LFLAG; 127 tp->t_cflag = TTYDEF_CFLAG; 128 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 129 ttsetwater(tp); /* would be done in xxparam() */ 130 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) 131 return (EBUSY); 132 if (tp->t_oproc) /* Ctrlr still around. */ 133 tp->t_state |= TS_CARR_ON; 134 while ((tp->t_state & TS_CARR_ON) == 0) { 135 tp->t_state |= TS_WOPEN; 136 if (flag&FNONBLOCK) 137 break; 138 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, 139 ttopen, 0); 140 if (error) 141 return (error); 142 } 143 error = (*linesw[tp->t_line].l_open)(dev, tp); 144 ptcwakeup(tp, FREAD|FWRITE); 145 return (error); 146 } 147 148 int 149 ptsclose(dev, flag, mode, p) 150 dev_t dev; 151 int flag, mode; 152 struct proc *p; 153 { 154 register struct pt_softc *pti = &pt_softc[minor(dev)]; 155 register struct tty *tp = pti->pt_tty; 156 int error; 157 158 error = (*linesw[tp->t_line].l_close)(tp, flag); 159 error |= ttyclose(tp); 160 ptcwakeup(tp, FREAD|FWRITE); 161 return (error); 162 } 163 164 int 165 ptsread(dev, uio, flag) 166 dev_t dev; 167 struct uio *uio; 168 int flag; 169 { 170 struct proc *p = curproc; 171 register struct pt_softc *pti = &pt_softc[minor(dev)]; 172 register struct tty *tp = pti->pt_tty; 173 int error = 0; 174 175 again: 176 if (pti->pt_flags & PF_REMOTE) { 177 while (isbackground(p, tp)) { 178 if ((p->p_sigignore & sigmask(SIGTTIN)) || 179 (p->p_sigmask & sigmask(SIGTTIN)) || 180 p->p_pgrp->pg_jobc == 0 || 181 p->p_flag & P_PPWAIT) 182 return (EIO); 183 pgsignal(p->p_pgrp, SIGTTIN, 1); 184 error = ttysleep(tp, &lbolt, 185 TTIPRI | PCATCH, ttybg, 0); 186 if (error) 187 return (error); 188 } 189 if (tp->t_canq.c_cc == 0) { 190 if (flag & IO_NDELAY) 191 return (EWOULDBLOCK); 192 error = ttysleep(tp, &tp->t_canq, 193 TTIPRI | PCATCH, ttyin, 0); 194 if (error) 195 return (error); 196 goto again; 197 } 198 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 199 if (ureadc(getc(&tp->t_canq), uio) < 0) { 200 error = EFAULT; 201 break; 202 } 203 if (tp->t_canq.c_cc == 1) 204 (void) getc(&tp->t_canq); 205 if (tp->t_canq.c_cc) 206 return (error); 207 } else 208 if (tp->t_oproc) 209 error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 210 ptcwakeup(tp, FWRITE); 211 return (error); 212 } 213 214 /* 215 * Write to pseudo-tty. 216 * Wakeups of controlling tty will happen 217 * indirectly, when tty driver calls ptsstart. 218 */ 219 int 220 ptswrite(dev, uio, flag) 221 dev_t dev; 222 struct uio *uio; 223 int flag; 224 { 225 register struct pt_softc *pti = &pt_softc[minor(dev)]; 226 register struct tty *tp = pti->pt_tty; 227 228 if (tp->t_oproc == 0) 229 return (EIO); 230 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 231 } 232 233 /* 234 * Start output on pseudo-tty. 235 * Wake up process polling or sleeping for input from controlling tty. 236 */ 237 void 238 ptsstart(tp) 239 struct tty *tp; 240 { 241 register struct pt_softc *pti = &pt_softc[minor(tp->t_dev)]; 242 243 if (tp->t_state & TS_TTSTOP) 244 return; 245 if (pti->pt_flags & PF_STOPPED) { 246 pti->pt_flags &= ~PF_STOPPED; 247 pti->pt_send = TIOCPKT_START; 248 } 249 ptcwakeup(tp, FREAD); 250 } 251 252 int 253 ptsstop(tp, flush) 254 register struct tty *tp; 255 int flush; 256 { 257 struct pt_softc *pti = &pt_softc[minor(tp->t_dev)]; 258 int flag; 259 260 /* note: FLUSHREAD and FLUSHWRITE already ok */ 261 if (flush == 0) { 262 flush = TIOCPKT_STOP; 263 pti->pt_flags |= PF_STOPPED; 264 } else 265 pti->pt_flags &= ~PF_STOPPED; 266 pti->pt_send |= flush; 267 /* change of perspective */ 268 flag = 0; 269 if (flush & FREAD) 270 flag |= FWRITE; 271 if (flush & FWRITE) 272 flag |= FREAD; 273 ptcwakeup(tp, flag); 274 return 0; 275 } 276 277 void 278 ptcwakeup(tp, flag) 279 struct tty *tp; 280 int flag; 281 { 282 struct pt_softc *pti = &pt_softc[minor(tp->t_dev)]; 283 284 if (flag & FREAD) { 285 selwakeup(&pti->pt_selr); 286 wakeup(&tp->t_outq.c_cf); 287 KNOTE(&pti->pt_selr.si_note, 0); 288 } 289 if (flag & FWRITE) { 290 selwakeup(&pti->pt_selw); 291 wakeup(&tp->t_rawq.c_cf); 292 KNOTE(&pti->pt_selw.si_note, 0); 293 } 294 } 295 296 int ptcopen(dev_t, int, int, struct proc *); 297 298 /*ARGSUSED*/ 299 int 300 ptcopen(dev, flag, devtype, p) 301 dev_t dev; 302 int flag, devtype; 303 struct proc *p; 304 { 305 struct pt_softc *pti; 306 register struct tty *tp; 307 308 if (minor(dev) >= npty) 309 return (ENXIO); 310 pti = &pt_softc[minor(dev)]; 311 if (!pti->pt_tty) { 312 tp = pti->pt_tty = ttymalloc(); 313 } else 314 tp = pti->pt_tty; 315 if (tp->t_oproc) 316 return (EIO); 317 tp->t_oproc = ptsstart; 318 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 319 tp->t_lflag &= ~EXTPROC; 320 pti->pt_flags = 0; 321 pti->pt_send = 0; 322 pti->pt_ucntl = 0; 323 return (0); 324 } 325 326 /*ARGSUSED*/ 327 int 328 ptcclose(dev, flag, devtype, p) 329 dev_t dev; 330 int flag, devtype; 331 struct proc *p; 332 { 333 register struct pt_softc *pti = &pt_softc[minor(dev)]; 334 register struct tty *tp = pti->pt_tty; 335 336 (void)(*linesw[tp->t_line].l_modem)(tp, 0); 337 tp->t_state &= ~TS_CARR_ON; 338 tp->t_oproc = 0; /* mark closed */ 339 return (0); 340 } 341 342 int 343 ptcread(dev, uio, flag) 344 dev_t dev; 345 struct uio *uio; 346 int flag; 347 { 348 register struct pt_softc *pti = &pt_softc[minor(dev)]; 349 register struct tty *tp = pti->pt_tty; 350 char buf[BUFSIZ]; 351 int error = 0, cc; 352 353 /* 354 * We want to block until the slave 355 * is open, and there's something to read; 356 * but if we lost the slave or we're NBIO, 357 * then return the appropriate error instead. 358 */ 359 for (;;) { 360 if (tp->t_state&TS_ISOPEN) { 361 if (pti->pt_flags&PF_PKT && pti->pt_send) { 362 error = ureadc((int)pti->pt_send, uio); 363 if (error) 364 return (error); 365 if (pti->pt_send & TIOCPKT_IOCTL) { 366 cc = min(uio->uio_resid, 367 sizeof(tp->t_termios)); 368 uiomove(&tp->t_termios, cc, uio); 369 } 370 pti->pt_send = 0; 371 return (0); 372 } 373 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 374 error = ureadc((int)pti->pt_ucntl, uio); 375 if (error) 376 return (error); 377 pti->pt_ucntl = 0; 378 return (0); 379 } 380 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 381 break; 382 } 383 if ((tp->t_state&TS_CARR_ON) == 0) 384 return (0); /* EOF */ 385 if (flag & IO_NDELAY) 386 return (EWOULDBLOCK); 387 error = tsleep(&tp->t_outq.c_cf, TTIPRI | PCATCH, 388 ttyin, 0); 389 if (error) 390 return (error); 391 } 392 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 393 error = ureadc(0, uio); 394 while (uio->uio_resid > 0 && error == 0) { 395 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 396 if (cc <= 0) 397 break; 398 error = uiomove(buf, cc, uio); 399 } 400 if (tp->t_outq.c_cc <= tp->t_lowat) { 401 if (tp->t_state&TS_ASLEEP) { 402 tp->t_state &= ~TS_ASLEEP; 403 wakeup(&tp->t_outq); 404 } 405 selwakeup(&tp->t_wsel); 406 } 407 return (error); 408 } 409 410 411 int 412 ptcwrite(dev, uio, flag) 413 dev_t dev; 414 register struct uio *uio; 415 int flag; 416 { 417 register struct pt_softc *pti = &pt_softc[minor(dev)]; 418 register struct tty *tp = pti->pt_tty; 419 register u_char *cp = NULL; 420 register int cc = 0; 421 u_char locbuf[BUFSIZ]; 422 int cnt = 0; 423 int error = 0; 424 425 again: 426 if ((tp->t_state&TS_ISOPEN) == 0) 427 goto block; 428 if (pti->pt_flags & PF_REMOTE) { 429 if (tp->t_canq.c_cc) 430 goto block; 431 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) { 432 if (cc == 0) { 433 cc = min(uio->uio_resid, BUFSIZ); 434 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 435 cp = locbuf; 436 error = uiomove(cp, cc, uio); 437 if (error) 438 return (error); 439 /* check again for safety */ 440 if ((tp->t_state&TS_ISOPEN) == 0) 441 return (EIO); 442 } 443 if (cc) 444 (void) b_to_q((char *)cp, cc, &tp->t_canq); 445 cc = 0; 446 } 447 (void) putc(0, &tp->t_canq); 448 ttwakeup(tp); 449 wakeup(&tp->t_canq); 450 return (0); 451 } 452 while (uio->uio_resid > 0) { 453 if (cc == 0) { 454 cc = min(uio->uio_resid, BUFSIZ); 455 cp = locbuf; 456 error = uiomove(cp, cc, uio); 457 if (error) 458 return (error); 459 /* check again for safety */ 460 if ((tp->t_state&TS_ISOPEN) == 0) 461 return (EIO); 462 } 463 while (cc > 0) { 464 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 465 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) { 466 wakeup(&tp->t_rawq); 467 goto block; 468 } 469 (*linesw[tp->t_line].l_rint)(*cp++, tp); 470 cnt++; 471 cc--; 472 } 473 cc = 0; 474 } 475 return (0); 476 block: 477 /* 478 * Come here to wait for slave to open, for space 479 * in outq, or space in rawq. 480 */ 481 if ((tp->t_state&TS_CARR_ON) == 0) 482 return (EIO); 483 if (flag & IO_NDELAY) { 484 /* adjust for data copied in but not written */ 485 uio->uio_resid += cc; 486 if (cnt == 0) 487 return (EWOULDBLOCK); 488 return (0); 489 } 490 error = tsleep(&tp->t_rawq.c_cf, TTOPRI | PCATCH, 491 ttyout, 0); 492 if (error) { 493 /* adjust for data copied in but not written */ 494 uio->uio_resid += cc; 495 return (error); 496 } 497 goto again; 498 } 499 500 int 501 ptcpoll(dev_t dev, int events, struct proc *p) 502 { 503 struct pt_softc *pti = &pt_softc[minor(dev)]; 504 struct tty *tp = pti->pt_tty; 505 int revents = 0, s; 506 507 if (!ISSET(tp->t_state, TS_CARR_ON)) 508 return (POLLHUP); 509 510 if (!ISSET(tp->t_state, TS_ISOPEN)) 511 goto notopen; 512 513 if (events & (POLLIN | POLLRDNORM)) { 514 /* 515 * Need to protect access to t_outq 516 */ 517 s = spltty(); 518 if ((tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP)) || 519 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 520 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 521 revents |= events & (POLLIN | POLLRDNORM); 522 splx(s); 523 } 524 if (events & (POLLOUT | POLLWRNORM)) { 525 if ((pti->pt_flags & PF_REMOTE) ? 526 (tp->t_canq.c_cc == 0) : 527 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 528 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))) 529 revents |= events & (POLLOUT | POLLWRNORM); 530 } 531 if (events & (POLLPRI | POLLRDBAND)) { 532 /* If in packet or user control mode, check for data. */ 533 if (((pti->pt_flags & PF_PKT) && pti->pt_send) || 534 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 535 revents |= events & (POLLPRI | POLLRDBAND); 536 } 537 538 if (revents == 0) { 539 notopen: 540 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) 541 selrecord(p, &pti->pt_selr); 542 if (events & (POLLOUT | POLLWRNORM)) 543 selrecord(p, &pti->pt_selw); 544 } 545 546 return (revents); 547 } 548 549 void 550 filt_ptcrdetach(struct knote *kn) 551 { 552 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 553 int s; 554 555 s = spltty(); 556 SLIST_REMOVE(&pti->pt_selr.si_note, kn, knote, kn_selnext); 557 splx(s); 558 } 559 560 int 561 filt_ptcread(struct knote *kn, long hint) 562 { 563 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 564 struct tty *tp; 565 566 tp = pti->pt_tty; 567 kn->kn_data = 0; 568 569 if (ISSET(tp->t_state, TS_ISOPEN)) { 570 if (!ISSET(tp->t_state, TS_TTSTOP)) 571 kn->kn_data = tp->t_outq.c_cc; 572 if (((pti->pt_flags & PF_PKT) && pti->pt_send) || 573 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 574 kn->kn_data++; 575 } 576 return (kn->kn_data > 0); 577 } 578 579 void 580 filt_ptcwdetach(struct knote *kn) 581 { 582 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 583 int s; 584 585 s = spltty(); 586 SLIST_REMOVE(&pti->pt_selw.si_note, kn, knote, kn_selnext); 587 splx(s); 588 } 589 590 int 591 filt_ptcwrite(struct knote *kn, long hint) 592 { 593 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 594 struct tty *tp; 595 596 tp = pti->pt_tty; 597 kn->kn_data = 0; 598 599 if (ISSET(tp->t_state, TS_ISOPEN)) { 600 if (ISSET(pti->pt_flags, PF_REMOTE)) { 601 if (tp->t_canq.c_cc == 0) 602 kn->kn_data = tp->t_canq.c_cn; 603 } else if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) 604 kn->kn_data = tp->t_canq.c_cn - 605 (tp->t_rawq.c_cc + tp->t_canq.c_cc); 606 } 607 608 return (kn->kn_data > 0); 609 } 610 611 struct filterops ptcread_filtops = 612 { 1, NULL, filt_ptcrdetach, filt_ptcread }; 613 struct filterops ptcwrite_filtops = 614 { 1, NULL, filt_ptcwdetach, filt_ptcwrite }; 615 616 int 617 ptckqfilter(dev_t dev, struct knote *kn) 618 { 619 struct pt_softc *pti = &pt_softc[minor(dev)]; 620 struct klist *klist; 621 int s; 622 623 switch (kn->kn_filter) { 624 case EVFILT_READ: 625 klist = &pti->pt_selr.si_note; 626 kn->kn_fop = &ptcread_filtops; 627 break; 628 case EVFILT_WRITE: 629 klist = &pti->pt_selw.si_note; 630 kn->kn_fop = &ptcwrite_filtops; 631 break; 632 default: 633 return (1); 634 } 635 636 kn->kn_hook = (caddr_t)pti; 637 638 s = spltty(); 639 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 640 splx(s); 641 642 return (0); 643 } 644 645 struct tty * 646 ptytty(dev) 647 dev_t dev; 648 { 649 register struct pt_softc *pti = &pt_softc[minor(dev)]; 650 register struct tty *tp = pti->pt_tty; 651 652 return (tp); 653 } 654 655 /*ARGSUSED*/ 656 int 657 ptyioctl(dev, cmd, data, flag, p) 658 dev_t dev; 659 u_long cmd; 660 caddr_t data; 661 int flag; 662 struct proc *p; 663 { 664 register struct pt_softc *pti = &pt_softc[minor(dev)]; 665 register struct tty *tp = pti->pt_tty; 666 register u_char *cc = tp->t_cc; 667 int stop, error; 668 669 /* 670 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 671 * ttywflush(tp) will hang if there are characters in the outq. 672 */ 673 if (cmd == TIOCEXT) { 674 /* 675 * When the EXTPROC bit is being toggled, we need 676 * to send an TIOCPKT_IOCTL if the packet driver 677 * is turned on. 678 */ 679 if (*(int *)data) { 680 if (pti->pt_flags & PF_PKT) { 681 pti->pt_send |= TIOCPKT_IOCTL; 682 ptcwakeup(tp, FREAD); 683 } 684 tp->t_lflag |= EXTPROC; 685 } else { 686 if ((tp->t_lflag & EXTPROC) && 687 (pti->pt_flags & PF_PKT)) { 688 pti->pt_send |= TIOCPKT_IOCTL; 689 ptcwakeup(tp, FREAD); 690 } 691 tp->t_lflag &= ~EXTPROC; 692 } 693 return(0); 694 } else 695 if (cdevsw[major(dev)].d_open == ptcopen) 696 switch (cmd) { 697 698 case TIOCGPGRP: 699 #ifdef COMPAT_SUNOS 700 { 701 /* 702 * I'm not sure about SunOS TIOCGPGRP semantics 703 * on PTYs, but it's something like this: 704 */ 705 extern struct emul emul_sunos; 706 if (p->p_emul == &emul_sunos && tp->t_pgrp == 0) 707 return (EIO); 708 *(int *)data = tp->t_pgrp->pg_id; 709 return (0); 710 } 711 #endif 712 /* 713 * We aviod calling ttioctl on the controller since, 714 * in that case, tp must be the controlling terminal. 715 */ 716 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 717 return (0); 718 719 case TIOCPKT: 720 if (*(int *)data) { 721 if (pti->pt_flags & PF_UCNTL) 722 return (EINVAL); 723 pti->pt_flags |= PF_PKT; 724 } else 725 pti->pt_flags &= ~PF_PKT; 726 return (0); 727 728 case TIOCUCNTL: 729 if (*(int *)data) { 730 if (pti->pt_flags & PF_PKT) 731 return (EINVAL); 732 pti->pt_flags |= PF_UCNTL; 733 } else 734 pti->pt_flags &= ~PF_UCNTL; 735 return (0); 736 737 case TIOCREMOTE: 738 if (*(int *)data) 739 pti->pt_flags |= PF_REMOTE; 740 else 741 pti->pt_flags &= ~PF_REMOTE; 742 ttyflush(tp, FREAD|FWRITE); 743 return (0); 744 745 #ifdef COMPAT_OLDTTY 746 case TIOCSETP: 747 case TIOCSETN: 748 #endif 749 case TIOCSETD: 750 case TIOCSETA: 751 case TIOCSETAW: 752 case TIOCSETAF: 753 ndflush(&tp->t_outq, tp->t_outq.c_cc); 754 break; 755 756 case TIOCSIG: 757 if (*(unsigned int *)data >= NSIG) 758 return(EINVAL); 759 if ((tp->t_lflag&NOFLSH) == 0) 760 ttyflush(tp, FREAD|FWRITE); 761 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 762 if ((*(unsigned int *)data == SIGINFO) && 763 ((tp->t_lflag&NOKERNINFO) == 0)) 764 ttyinfo(tp); 765 return(0); 766 } 767 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 768 if (error < 0) 769 error = ttioctl(tp, cmd, data, flag, p); 770 if (error < 0) { 771 if (pti->pt_flags & PF_UCNTL && 772 (cmd & ~0xff) == UIOCCMD(0)) { 773 if (cmd & 0xff) { 774 pti->pt_ucntl = (u_char)cmd; 775 ptcwakeup(tp, FREAD); 776 } 777 return (0); 778 } 779 error = ENOTTY; 780 } 781 /* 782 * If external processing and packet mode send ioctl packet. 783 */ 784 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 785 switch(cmd) { 786 case TIOCSETA: 787 case TIOCSETAW: 788 case TIOCSETAF: 789 #ifdef COMPAT_OLDTTY 790 case TIOCSETP: 791 case TIOCSETN: 792 case TIOCSETC: 793 case TIOCSLTC: 794 case TIOCLBIS: 795 case TIOCLBIC: 796 case TIOCLSET: 797 #endif 798 pti->pt_send |= TIOCPKT_IOCTL; 799 ptcwakeup(tp, FREAD); 800 default: 801 break; 802 } 803 } 804 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 805 && CCEQ(cc[VSTART], CTRL('q')); 806 if (pti->pt_flags & PF_NOSTOP) { 807 if (stop) { 808 pti->pt_send &= ~TIOCPKT_NOSTOP; 809 pti->pt_send |= TIOCPKT_DOSTOP; 810 pti->pt_flags &= ~PF_NOSTOP; 811 ptcwakeup(tp, FREAD); 812 } 813 } else { 814 if (!stop) { 815 pti->pt_send &= ~TIOCPKT_DOSTOP; 816 pti->pt_send |= TIOCPKT_NOSTOP; 817 pti->pt_flags |= PF_NOSTOP; 818 ptcwakeup(tp, FREAD); 819 } 820 } 821 return (error); 822 } 823