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: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $ 35 * $DragonFly: src/sys/kern/tty_pty.c,v 1.13 2005/06/06 15:02:28 dillon Exp $ 36 */ 37 38 /* 39 * Pseudo-teletype Driver 40 * (Actually two drivers, requiring two entries in 'cdevsw') 41 */ 42 #include "use_pty.h" /* XXX */ 43 #include "opt_compat.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.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 #include <sys/device.h> 60 #include <sys/thread2.h> 61 62 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); 63 64 static void ptsstart (struct tty *tp); 65 static void ptsstop (struct tty *tp, int rw); 66 static void ptcwakeup (struct tty *tp, int flag); 67 static void ptyinit (int n); 68 69 static d_open_t ptsopen; 70 static d_close_t ptsclose; 71 static d_read_t ptsread; 72 static d_write_t ptswrite; 73 static d_ioctl_t ptyioctl; 74 static d_open_t ptcopen; 75 static d_close_t ptcclose; 76 static d_read_t ptcread; 77 static d_write_t ptcwrite; 78 static d_poll_t ptcpoll; 79 80 #define CDEV_MAJOR_S 5 81 static struct cdevsw pts_cdevsw = { 82 /* name */ "pts", 83 /* maj */ CDEV_MAJOR_S, 84 /* flags */ D_TTY | D_KQFILTER, 85 /* port */ NULL, 86 /* clone */ NULL, 87 88 /* open */ ptsopen, 89 /* close */ ptsclose, 90 /* read */ ptsread, 91 /* write */ ptswrite, 92 /* ioctl */ ptyioctl, 93 /* poll */ ttypoll, 94 /* mmap */ nommap, 95 /* strategy */ nostrategy, 96 /* dump */ nodump, 97 /* psize */ nopsize, 98 /* kqfilter */ ttykqfilter 99 }; 100 101 #define CDEV_MAJOR_C 6 102 static struct cdevsw ptc_cdevsw = { 103 /* name */ "ptc", 104 /* maj */ CDEV_MAJOR_C, 105 /* flags */ D_TTY | D_KQFILTER | D_MASTER, 106 /* port */ NULL, 107 /* clone */ NULL, 108 109 /* open */ ptcopen, 110 /* close */ ptcclose, 111 /* read */ ptcread, 112 /* write */ ptcwrite, 113 /* ioctl */ ptyioctl, 114 /* poll */ ptcpoll, 115 /* mmap */ nommap, 116 /* strategy */ nostrategy, 117 /* dump */ nodump, 118 /* psize */ nopsize, 119 /* kqfilter */ ttykqfilter 120 }; 121 122 #define BUFSIZ 100 /* Chunk size iomoved to/from user */ 123 124 struct pt_ioctl { 125 int pt_flags; 126 struct selinfo pt_selr, pt_selw; 127 u_char pt_send; 128 u_char pt_ucntl; 129 struct tty pt_tty; 130 dev_t devs, devc; 131 struct prison *pt_prison; 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 */ 149 static void 150 ptyinit(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 = devc->si_tty = &pt->pt_tty; 170 pt->pt_tty.t_dev = devs; 171 ttyregister(&pt->pt_tty); 172 } 173 174 /*ARGSUSED*/ 175 static int 176 ptsopen(dev_t dev, int flag, int devtype, struct thread *td) 177 { 178 struct tty *tp; 179 int error; 180 int minr; 181 #if 0 182 dev_t nextdev; 183 #endif 184 struct pt_ioctl *pti; 185 struct proc *p; 186 187 p = td->td_proc; 188 KKASSERT(p != NULL); 189 190 minr = lminor(dev); 191 #if 0 192 /* 193 * REMOVED gross hack for devfs. also note that makedev() 194 * no longer exists in this form and reference counting makes 195 * this a problem if we don't clean it out later. 196 */ 197 if (minr < 255) { 198 nextdev = make_sub_dev(dev, minr + 1); 199 if (!nextdev->si_drv1) { 200 ptyinit(minr + 1); 201 } 202 } 203 #endif 204 if (!dev->si_drv1) 205 ptyinit(minor(dev)); 206 if (!dev->si_drv1) 207 return(ENXIO); 208 pti = dev->si_drv1; 209 tp = dev->si_tty; 210 if ((tp->t_state & TS_ISOPEN) == 0) { 211 ttychars(tp); /* Set up default chars */ 212 tp->t_iflag = TTYDEF_IFLAG; 213 tp->t_oflag = TTYDEF_OFLAG; 214 tp->t_lflag = TTYDEF_LFLAG; 215 tp->t_cflag = TTYDEF_CFLAG; 216 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 217 } else if (tp->t_state & TS_XCLUDE && suser(td)) { 218 return (EBUSY); 219 } else if (pti->pt_prison != p->p_ucred->cr_prison) { 220 return (EBUSY); 221 } 222 if (tp->t_oproc) /* Ctrlr still around. */ 223 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 224 while ((tp->t_state & TS_CARR_ON) == 0) { 225 if (flag&FNONBLOCK) 226 break; 227 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0); 228 if (error) 229 return (error); 230 } 231 error = (*linesw[tp->t_line].l_open)(dev, tp); 232 if (error == 0) 233 ptcwakeup(tp, FREAD|FWRITE); 234 return (error); 235 } 236 237 static int 238 ptsclose(dev, flag, mode, td) 239 dev_t dev; 240 int flag, mode; 241 struct thread *td; 242 { 243 struct tty *tp; 244 int err; 245 246 tp = dev->si_tty; 247 err = (*linesw[tp->t_line].l_close)(tp, flag); 248 ptsstop(tp, FREAD|FWRITE); 249 (void) ttyclose(tp); 250 return (err); 251 } 252 253 static int 254 ptsread(dev, uio, flag) 255 dev_t dev; 256 struct uio *uio; 257 int flag; 258 { 259 struct proc *p = curproc; 260 struct tty *tp = dev->si_tty; 261 struct pt_ioctl *pti = dev->si_drv1; 262 int error = 0; 263 264 again: 265 if (pti->pt_flags & PF_REMOTE) { 266 while (isbackground(p, tp)) { 267 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 268 SIGISMEMBER(p->p_sigmask, SIGTTIN) || 269 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) 270 return (EIO); 271 pgsignal(p->p_pgrp, SIGTTIN, 1); 272 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0); 273 if (error) 274 return (error); 275 } 276 if (tp->t_canq.c_cc == 0) { 277 if (flag & IO_NDELAY) 278 return (EWOULDBLOCK); 279 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH, 280 "ptsin", 0); 281 if (error) 282 return (error); 283 goto again; 284 } 285 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 286 if (ureadc(getc(&tp->t_canq), uio) < 0) { 287 error = EFAULT; 288 break; 289 } 290 if (tp->t_canq.c_cc == 1) 291 (void) getc(&tp->t_canq); 292 if (tp->t_canq.c_cc) 293 return (error); 294 } else 295 if (tp->t_oproc) 296 error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 297 ptcwakeup(tp, FWRITE); 298 return (error); 299 } 300 301 /* 302 * Write to pseudo-tty. 303 * Wakeups of controlling tty will happen 304 * indirectly, when tty driver calls ptsstart. 305 */ 306 static int 307 ptswrite(dev, uio, flag) 308 dev_t dev; 309 struct uio *uio; 310 int flag; 311 { 312 struct tty *tp; 313 314 tp = dev->si_tty; 315 if (tp->t_oproc == 0) 316 return (EIO); 317 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 318 } 319 320 /* 321 * Start output on pseudo-tty. 322 * Wake up process selecting or sleeping for input from controlling tty. 323 */ 324 static void 325 ptsstart(tp) 326 struct tty *tp; 327 { 328 struct pt_ioctl *pti = tp->t_dev->si_drv1; 329 330 if (tp->t_state & TS_TTSTOP) 331 return; 332 if (pti->pt_flags & PF_STOPPED) { 333 pti->pt_flags &= ~PF_STOPPED; 334 pti->pt_send = TIOCPKT_START; 335 } 336 ptcwakeup(tp, FREAD); 337 } 338 339 static void 340 ptcwakeup(tp, flag) 341 struct tty *tp; 342 int flag; 343 { 344 struct pt_ioctl *pti = tp->t_dev->si_drv1; 345 346 if (flag & FREAD) { 347 selwakeup(&pti->pt_selr); 348 wakeup(TSA_PTC_READ(tp)); 349 } 350 if (flag & FWRITE) { 351 selwakeup(&pti->pt_selw); 352 wakeup(TSA_PTC_WRITE(tp)); 353 } 354 } 355 356 static int 357 ptcopen(dev, flag, devtype, td) 358 dev_t dev; 359 int flag, devtype; 360 struct thread *td; 361 { 362 struct tty *tp; 363 struct pt_ioctl *pti; 364 365 if (!dev->si_drv1) 366 ptyinit(minor(dev)); 367 if (!dev->si_drv1) 368 return(ENXIO); 369 tp = dev->si_tty; 370 if (tp->t_oproc) 371 return (EIO); 372 KKASSERT(td->td_proc != NULL); 373 tp->t_oproc = ptsstart; 374 tp->t_stop = ptsstop; 375 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 376 tp->t_lflag &= ~EXTPROC; 377 pti = dev->si_drv1; 378 pti->pt_prison = td->td_proc->p_ucred->cr_prison; 379 pti->pt_flags = 0; 380 pti->pt_send = 0; 381 pti->pt_ucntl = 0; 382 return (0); 383 } 384 385 static int 386 ptcclose(dev, flags, fmt, td) 387 dev_t dev; 388 int flags; 389 int fmt; 390 struct thread *td; 391 { 392 struct tty *tp; 393 394 tp = dev->si_tty; 395 (void)(*linesw[tp->t_line].l_modem)(tp, 0); 396 397 /* 398 * XXX MDMBUF makes no sense for ptys but would inhibit the above 399 * l_modem(). CLOCAL makes sense but isn't supported. Special 400 * l_modem()s that ignore carrier drop make no sense for ptys but 401 * may be in use because other parts of the line discipline make 402 * sense for ptys. Recover by doing everything that a normal 403 * ttymodem() would have done except for sending a SIGHUP. 404 */ 405 if (tp->t_state & TS_ISOPEN) { 406 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 407 tp->t_state |= TS_ZOMBIE; 408 ttyflush(tp, FREAD | FWRITE); 409 } 410 411 tp->t_oproc = 0; /* mark closed */ 412 return (0); 413 } 414 415 static int 416 ptcread(dev, uio, flag) 417 dev_t dev; 418 struct uio *uio; 419 int flag; 420 { 421 struct tty *tp = dev->si_tty; 422 struct pt_ioctl *pti = dev->si_drv1; 423 char buf[BUFSIZ]; 424 int error = 0, cc; 425 426 /* 427 * We want to block until the slave 428 * is open, and there's something to read; 429 * but if we lost the slave or we're NBIO, 430 * then return the appropriate error instead. 431 */ 432 for (;;) { 433 if (tp->t_state&TS_ISOPEN) { 434 if (pti->pt_flags&PF_PKT && pti->pt_send) { 435 error = ureadc((int)pti->pt_send, uio); 436 if (error) 437 return (error); 438 if (pti->pt_send & TIOCPKT_IOCTL) { 439 cc = min(uio->uio_resid, 440 sizeof(tp->t_termios)); 441 uiomove((caddr_t)&tp->t_termios, cc, 442 uio); 443 } 444 pti->pt_send = 0; 445 return (0); 446 } 447 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 448 error = ureadc((int)pti->pt_ucntl, uio); 449 if (error) 450 return (error); 451 pti->pt_ucntl = 0; 452 return (0); 453 } 454 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 455 break; 456 } 457 if ((tp->t_state & TS_CONNECTED) == 0) 458 return (0); /* EOF */ 459 if (flag & IO_NDELAY) 460 return (EWOULDBLOCK); 461 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0); 462 if (error) 463 return (error); 464 } 465 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 466 error = ureadc(0, uio); 467 while (uio->uio_resid > 0 && error == 0) { 468 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 469 if (cc <= 0) 470 break; 471 error = uiomove(buf, cc, uio); 472 } 473 ttwwakeup(tp); 474 return (error); 475 } 476 477 static void 478 ptsstop(tp, flush) 479 struct tty *tp; 480 int flush; 481 { 482 struct pt_ioctl *pti = tp->t_dev->si_drv1; 483 int flag; 484 485 /* note: FLUSHREAD and FLUSHWRITE already ok */ 486 if (flush == 0) { 487 flush = TIOCPKT_STOP; 488 pti->pt_flags |= PF_STOPPED; 489 } else 490 pti->pt_flags &= ~PF_STOPPED; 491 pti->pt_send |= flush; 492 /* change of perspective */ 493 flag = 0; 494 if (flush & FREAD) 495 flag |= FWRITE; 496 if (flush & FWRITE) 497 flag |= FREAD; 498 ptcwakeup(tp, flag); 499 } 500 501 static int 502 ptcpoll(dev, events, td) 503 dev_t dev; 504 int events; 505 struct thread *td; 506 { 507 struct tty *tp = dev->si_tty; 508 struct pt_ioctl *pti = dev->si_drv1; 509 int revents = 0; 510 511 if ((tp->t_state & TS_CONNECTED) == 0) 512 return (seltrue(dev, events, td) | POLLHUP); 513 514 /* 515 * Need to block timeouts (ttrstart). 516 */ 517 crit_enter(); 518 519 if (events & (POLLIN | POLLRDNORM)) 520 if ((tp->t_state & TS_ISOPEN) && 521 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 522 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 523 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 524 revents |= events & (POLLIN | POLLRDNORM); 525 526 if (events & (POLLOUT | POLLWRNORM)) 527 if (tp->t_state & TS_ISOPEN && 528 ((pti->pt_flags & PF_REMOTE) ? 529 (tp->t_canq.c_cc == 0) : 530 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 531 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) 532 revents |= events & (POLLOUT | POLLWRNORM); 533 534 if (events & POLLHUP) 535 if ((tp->t_state & TS_CARR_ON) == 0) 536 revents |= POLLHUP; 537 538 if (revents == 0) { 539 if (events & (POLLIN | POLLRDNORM)) 540 selrecord(td, &pti->pt_selr); 541 542 if (events & (POLLOUT | POLLWRNORM)) 543 selrecord(td, &pti->pt_selw); 544 } 545 crit_exit(); 546 547 return (revents); 548 } 549 550 static int 551 ptcwrite(dev, uio, flag) 552 dev_t dev; 553 struct uio *uio; 554 int flag; 555 { 556 struct tty *tp = dev->si_tty; 557 u_char *cp = 0; 558 int cc = 0; 559 u_char locbuf[BUFSIZ]; 560 int cnt = 0; 561 struct pt_ioctl *pti = dev->si_drv1; 562 int error = 0; 563 564 again: 565 if ((tp->t_state&TS_ISOPEN) == 0) 566 goto block; 567 if (pti->pt_flags & PF_REMOTE) { 568 if (tp->t_canq.c_cc) 569 goto block; 570 while ((uio->uio_resid > 0 || cc > 0) && 571 tp->t_canq.c_cc < TTYHOG - 1) { 572 if (cc == 0) { 573 cc = min(uio->uio_resid, BUFSIZ); 574 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 575 cp = locbuf; 576 error = uiomove((caddr_t)cp, cc, uio); 577 if (error) 578 return (error); 579 /* check again for safety */ 580 if ((tp->t_state & TS_ISOPEN) == 0) { 581 /* adjust as usual */ 582 uio->uio_resid += cc; 583 return (EIO); 584 } 585 } 586 if (cc > 0) { 587 cc = b_to_q((char *)cp, cc, &tp->t_canq); 588 /* 589 * XXX we don't guarantee that the canq size 590 * is >= TTYHOG, so the above b_to_q() may 591 * leave some bytes uncopied. However, space 592 * is guaranteed for the null terminator if 593 * we don't fail here since (TTYHOG - 1) is 594 * not a multiple of CBSIZE. 595 */ 596 if (cc > 0) 597 break; 598 } 599 } 600 /* adjust for data copied in but not written */ 601 uio->uio_resid += cc; 602 (void) putc(0, &tp->t_canq); 603 ttwakeup(tp); 604 wakeup(TSA_PTS_READ(tp)); 605 return (0); 606 } 607 while (uio->uio_resid > 0 || cc > 0) { 608 if (cc == 0) { 609 cc = min(uio->uio_resid, BUFSIZ); 610 cp = locbuf; 611 error = uiomove((caddr_t)cp, cc, uio); 612 if (error) 613 return (error); 614 /* check again for safety */ 615 if ((tp->t_state & TS_ISOPEN) == 0) { 616 /* adjust for data copied in but not written */ 617 uio->uio_resid += cc; 618 return (EIO); 619 } 620 } 621 while (cc > 0) { 622 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 623 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) { 624 wakeup(TSA_HUP_OR_INPUT(tp)); 625 goto block; 626 } 627 (*linesw[tp->t_line].l_rint)(*cp++, tp); 628 cnt++; 629 cc--; 630 } 631 cc = 0; 632 } 633 return (0); 634 block: 635 /* 636 * Come here to wait for slave to open, for space 637 * in outq, or space in rawq, or an empty canq. 638 */ 639 if ((tp->t_state & TS_CONNECTED) == 0) { 640 /* adjust for data copied in but not written */ 641 uio->uio_resid += cc; 642 return (EIO); 643 } 644 if (flag & IO_NDELAY) { 645 /* adjust for data copied in but not written */ 646 uio->uio_resid += cc; 647 if (cnt == 0) 648 return (EWOULDBLOCK); 649 return (0); 650 } 651 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0); 652 if (error) { 653 /* adjust for data copied in but not written */ 654 uio->uio_resid += cc; 655 return (error); 656 } 657 goto again; 658 } 659 660 /*ARGSUSED*/ 661 static int 662 ptyioctl(dev, cmd, data, flag, td) 663 dev_t dev; 664 u_long cmd; 665 caddr_t data; 666 int flag; 667 struct thread *td; 668 { 669 struct tty *tp = dev->si_tty; 670 struct pt_ioctl *pti = dev->si_drv1; 671 u_char *cc = tp->t_cc; 672 int stop, error; 673 674 if (dev_dflags(dev) & D_MASTER) { 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, td); 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 831 static void ptc_drvinit (void *unused); 832 833 static void 834 ptc_drvinit(unused) 835 void *unused; 836 { 837 cdevsw_add(&pts_cdevsw, 0, 0); 838 cdevsw_add(&ptc_cdevsw, 0, 0); 839 /* XXX: Gross hack for DEVFS */ 840 /* XXX: DEVFS is no more, should this be removed? */ 841 ptyinit(0); 842 } 843 844 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 845