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