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