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