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