1 /* 2 * Copyright (c) 1982, 1986, 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)tty_pty.c 7.28 (Berkeley) 10/11/92 8 */ 9 10 /* 11 * Pseudo-teletype Driver 12 * (Actually two drivers, requiring two entries in 'cdevsw') 13 */ 14 #include "pty.h" 15 16 #if NPTY > 0 17 #include <sys/param.h> 18 #include <sys/systm.h> 19 #include <sys/ioctl.h> 20 #include <sys/proc.h> 21 #include <sys/tty.h> 22 #include <sys/conf.h> 23 #include <sys/file.h> 24 #include <sys/uio.h> 25 #include <sys/kernel.h> 26 #include <sys/vnode.h> 27 28 #if NPTY == 1 29 #undef NPTY 30 #define NPTY 32 /* crude XXX */ 31 #endif 32 33 #define BUFSIZ 100 /* Chunk size iomoved to/from user */ 34 35 /* 36 * pts == /dev/tty[pqrs]? 37 * ptc == /dev/pty[pqrs]? 38 */ 39 struct tty pt_tty[NPTY]; 40 struct pt_ioctl { 41 int pt_flags; 42 struct selinfo pt_selr, pt_selw; 43 u_char pt_send; 44 u_char pt_ucntl; 45 } pt_ioctl[NPTY]; 46 int npty = NPTY; /* for pstat -t */ 47 48 #define PF_PKT 0x08 /* packet mode */ 49 #define PF_STOPPED 0x10 /* user told stopped */ 50 #define PF_REMOTE 0x20 /* remote and flow controlled input */ 51 #define PF_NOSTOP 0x40 52 #define PF_UCNTL 0x80 /* user control mode */ 53 54 void ptsstop __P((struct tty *, int)); 55 56 /*ARGSUSED*/ 57 ptsopen(dev, flag, devtype, p) 58 dev_t dev; 59 int flag, devtype; 60 struct proc *p; 61 { 62 register struct tty *tp; 63 int error; 64 65 #ifdef lint 66 npty = npty; 67 #endif 68 if (minor(dev) >= NPTY) 69 return (ENXIO); 70 tp = &pt_tty[minor(dev)]; 71 if ((tp->t_state & TS_ISOPEN) == 0) { 72 tp->t_state |= TS_WOPEN; 73 ttychars(tp); /* Set up default chars */ 74 tp->t_iflag = TTYDEF_IFLAG; 75 tp->t_oflag = TTYDEF_OFLAG; 76 tp->t_lflag = TTYDEF_LFLAG; 77 tp->t_cflag = TTYDEF_CFLAG; 78 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 79 ttsetwater(tp); /* would be done in xxparam() */ 80 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) 81 return (EBUSY); 82 if (tp->t_oproc) /* Ctrlr still around. */ 83 tp->t_state |= TS_CARR_ON; 84 while ((tp->t_state & TS_CARR_ON) == 0) { 85 tp->t_state |= TS_WOPEN; 86 if (flag&FNONBLOCK) 87 break; 88 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 89 ttopen, 0)) 90 return (error); 91 } 92 error = (*linesw[tp->t_line].l_open)(dev, tp); 93 ptcwakeup(tp, FREAD|FWRITE); 94 return (error); 95 } 96 97 ptsclose(dev, flag, mode, p) 98 dev_t dev; 99 int flag, mode; 100 struct proc *p; 101 { 102 register struct tty *tp; 103 int err; 104 105 tp = &pt_tty[minor(dev)]; 106 err = (*linesw[tp->t_line].l_close)(tp, flag); 107 err |= ttyclose(tp); 108 ptcwakeup(tp, FREAD|FWRITE); 109 return (err); 110 } 111 112 ptsread(dev, uio, flag) 113 dev_t dev; 114 struct uio *uio; 115 int flag; 116 { 117 struct proc *p = curproc; 118 register struct tty *tp = &pt_tty[minor(dev)]; 119 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 120 int error = 0; 121 122 again: 123 if (pti->pt_flags & PF_REMOTE) { 124 while (isbackground(p, tp)) { 125 if ((p->p_sigignore & sigmask(SIGTTIN)) || 126 (p->p_sigmask & sigmask(SIGTTIN)) || 127 p->p_pgrp->pg_jobc == 0 || 128 p->p_flag&SPPWAIT) 129 return (EIO); 130 pgsignal(p->p_pgrp, SIGTTIN, 1); 131 if (error = ttysleep(tp, (caddr_t)&lbolt, 132 TTIPRI | PCATCH, ttybg, 0)) 133 return (error); 134 } 135 if (tp->t_canq.c_cc == 0) { 136 if (flag & IO_NDELAY) 137 return (EWOULDBLOCK); 138 if (error = ttysleep(tp, (caddr_t)&tp->t_canq, 139 TTIPRI | PCATCH, ttyin, 0)) 140 return (error); 141 goto again; 142 } 143 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 144 if (ureadc(getc(&tp->t_canq), uio) < 0) { 145 error = EFAULT; 146 break; 147 } 148 if (tp->t_canq.c_cc == 1) 149 (void) getc(&tp->t_canq); 150 if (tp->t_canq.c_cc) 151 return (error); 152 } else 153 if (tp->t_oproc) 154 error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 155 ptcwakeup(tp, FWRITE); 156 return (error); 157 } 158 159 /* 160 * Write to pseudo-tty. 161 * Wakeups of controlling tty will happen 162 * indirectly, when tty driver calls ptsstart. 163 */ 164 ptswrite(dev, uio, flag) 165 dev_t dev; 166 struct uio *uio; 167 int flag; 168 { 169 register struct tty *tp; 170 171 tp = &pt_tty[minor(dev)]; 172 if (tp->t_oproc == 0) 173 return (EIO); 174 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 175 } 176 177 /* 178 * Start output on pseudo-tty. 179 * Wake up process selecting or sleeping for input from controlling tty. 180 */ 181 void 182 ptsstart(tp) 183 struct tty *tp; 184 { 185 register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 186 187 if (tp->t_state & TS_TTSTOP) 188 return; 189 if (pti->pt_flags & PF_STOPPED) { 190 pti->pt_flags &= ~PF_STOPPED; 191 pti->pt_send = TIOCPKT_START; 192 } 193 ptcwakeup(tp, FREAD); 194 } 195 196 ptcwakeup(tp, flag) 197 struct tty *tp; 198 int flag; 199 { 200 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 201 202 if (flag & FREAD) { 203 selwakeup(&pti->pt_selr); 204 wakeup((caddr_t)&tp->t_outq.c_cf); 205 } 206 if (flag & FWRITE) { 207 selwakeup(&pti->pt_selw); 208 wakeup((caddr_t)&tp->t_rawq.c_cf); 209 } 210 } 211 212 /*ARGSUSED*/ 213 #ifdef __STDC__ 214 ptcopen(dev_t dev, int flag, int devtype, struct proc *p) 215 #else 216 ptcopen(dev, flag, devtype, p) 217 dev_t dev; 218 int flag, devtype; 219 struct proc *p; 220 #endif 221 { 222 register struct tty *tp; 223 struct pt_ioctl *pti; 224 225 if (minor(dev) >= NPTY) 226 return (ENXIO); 227 tp = &pt_tty[minor(dev)]; 228 if (tp->t_oproc) 229 return (EIO); 230 tp->t_oproc = ptsstart; 231 #ifdef sun4c 232 tp->t_stop = ptsstop; 233 #endif 234 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 235 tp->t_lflag &= ~EXTPROC; 236 pti = &pt_ioctl[minor(dev)]; 237 pti->pt_flags = 0; 238 pti->pt_send = 0; 239 pti->pt_ucntl = 0; 240 return (0); 241 } 242 243 ptcclose(dev) 244 dev_t dev; 245 { 246 register struct tty *tp; 247 248 tp = &pt_tty[minor(dev)]; 249 (void)(*linesw[tp->t_line].l_modem)(tp, 0); 250 tp->t_state &= ~TS_CARR_ON; 251 tp->t_oproc = 0; /* mark closed */ 252 tp->t_session = 0; 253 return (0); 254 } 255 256 ptcread(dev, uio, flag) 257 dev_t dev; 258 struct uio *uio; 259 int flag; 260 { 261 register struct tty *tp = &pt_tty[minor(dev)]; 262 struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 263 char buf[BUFSIZ]; 264 int error = 0, cc; 265 266 /* 267 * We want to block until the slave 268 * is open, and there's something to read; 269 * but if we lost the slave or we're NBIO, 270 * then return the appropriate error instead. 271 */ 272 for (;;) { 273 if (tp->t_state&TS_ISOPEN) { 274 if (pti->pt_flags&PF_PKT && pti->pt_send) { 275 error = ureadc((int)pti->pt_send, uio); 276 if (error) 277 return (error); 278 if (pti->pt_send & TIOCPKT_IOCTL) { 279 cc = min(uio->uio_resid, 280 sizeof(tp->t_termios)); 281 uiomove(&tp->t_termios, cc, uio); 282 } 283 pti->pt_send = 0; 284 return (0); 285 } 286 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 287 error = ureadc((int)pti->pt_ucntl, uio); 288 if (error) 289 return (error); 290 pti->pt_ucntl = 0; 291 return (0); 292 } 293 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 294 break; 295 } 296 if ((tp->t_state&TS_CARR_ON) == 0) 297 return (0); /* EOF */ 298 if (flag & IO_NDELAY) 299 return (EWOULDBLOCK); 300 if (error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH, 301 ttyin, 0)) 302 return (error); 303 } 304 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 305 error = ureadc(0, uio); 306 while (uio->uio_resid > 0 && error == 0) { 307 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 308 if (cc <= 0) 309 break; 310 error = uiomove(buf, cc, uio); 311 } 312 if (tp->t_outq.c_cc <= tp->t_lowat) { 313 if (tp->t_state&TS_ASLEEP) { 314 tp->t_state &= ~TS_ASLEEP; 315 wakeup((caddr_t)&tp->t_outq); 316 } 317 selwakeup(&tp->t_wsel); 318 } 319 return (error); 320 } 321 322 void 323 ptsstop(tp, flush) 324 register struct tty *tp; 325 int flush; 326 { 327 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; 328 int flag; 329 330 /* note: FLUSHREAD and FLUSHWRITE already ok */ 331 if (flush == 0) { 332 flush = TIOCPKT_STOP; 333 pti->pt_flags |= PF_STOPPED; 334 } else 335 pti->pt_flags &= ~PF_STOPPED; 336 pti->pt_send |= flush; 337 /* change of perspective */ 338 flag = 0; 339 if (flush & FREAD) 340 flag |= FWRITE; 341 if (flush & FWRITE) 342 flag |= FREAD; 343 ptcwakeup(tp, flag); 344 } 345 346 ptcselect(dev, rw, p) 347 dev_t dev; 348 int rw; 349 struct proc *p; 350 { 351 register struct tty *tp = &pt_tty[minor(dev)]; 352 struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 353 int s; 354 355 if ((tp->t_state&TS_CARR_ON) == 0) 356 return (1); 357 switch (rw) { 358 359 case FREAD: 360 /* 361 * Need to block timeouts (ttrstart). 362 */ 363 s = spltty(); 364 if ((tp->t_state&TS_ISOPEN) && 365 tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { 366 splx(s); 367 return (1); 368 } 369 splx(s); 370 /* FALLTHROUGH */ 371 372 case 0: /* exceptional */ 373 if ((tp->t_state&TS_ISOPEN) && 374 (pti->pt_flags&PF_PKT && pti->pt_send || 375 pti->pt_flags&PF_UCNTL && pti->pt_ucntl)) 376 return (1); 377 selrecord(p, &pti->pt_selr); 378 break; 379 380 381 case FWRITE: 382 if (tp->t_state&TS_ISOPEN) { 383 if (pti->pt_flags & PF_REMOTE) { 384 if (tp->t_canq.c_cc == 0) 385 return (1); 386 } else { 387 if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) 388 return (1); 389 if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) 390 return (1); 391 } 392 } 393 selrecord(p, &pti->pt_selw); 394 break; 395 396 } 397 return (0); 398 } 399 400 ptcwrite(dev, uio, flag) 401 dev_t dev; 402 register struct uio *uio; 403 int flag; 404 { 405 register struct tty *tp = &pt_tty[minor(dev)]; 406 register u_char *cp; 407 register int cc = 0; 408 u_char locbuf[BUFSIZ]; 409 int cnt = 0; 410 struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 411 int error = 0; 412 413 again: 414 if ((tp->t_state&TS_ISOPEN) == 0) 415 goto block; 416 if (pti->pt_flags & PF_REMOTE) { 417 if (tp->t_canq.c_cc) 418 goto block; 419 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) { 420 if (cc == 0) { 421 cc = min(uio->uio_resid, BUFSIZ); 422 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 423 cp = locbuf; 424 error = uiomove((caddr_t)cp, cc, uio); 425 if (error) 426 return (error); 427 /* check again for safety */ 428 if ((tp->t_state&TS_ISOPEN) == 0) 429 return (EIO); 430 } 431 if (cc) 432 (void) b_to_q((char *)cp, cc, &tp->t_canq); 433 cc = 0; 434 } 435 (void) putc(0, &tp->t_canq); 436 ttwakeup(tp); 437 wakeup((caddr_t)&tp->t_canq); 438 return (0); 439 } 440 while (uio->uio_resid > 0) { 441 if (cc == 0) { 442 cc = min(uio->uio_resid, BUFSIZ); 443 cp = locbuf; 444 error = uiomove((caddr_t)cp, cc, uio); 445 if (error) 446 return (error); 447 /* check again for safety */ 448 if ((tp->t_state&TS_ISOPEN) == 0) 449 return (EIO); 450 } 451 while (cc > 0) { 452 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 453 (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 454 wakeup((caddr_t)&tp->t_rawq); 455 goto block; 456 } 457 (*linesw[tp->t_line].l_rint)(*cp++, tp); 458 cnt++; 459 cc--; 460 } 461 cc = 0; 462 } 463 return (0); 464 block: 465 /* 466 * Come here to wait for slave to open, for space 467 * in outq, or space in rawq. 468 */ 469 if ((tp->t_state&TS_CARR_ON) == 0) 470 return (EIO); 471 if (flag & IO_NDELAY) { 472 /* adjust for data copied in but not written */ 473 uio->uio_resid += cc; 474 if (cnt == 0) 475 return (EWOULDBLOCK); 476 return (0); 477 } 478 if (error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH, 479 ttyout, 0)) { 480 /* adjust for data copied in but not written */ 481 uio->uio_resid += cc; 482 return (error); 483 } 484 goto again; 485 } 486 487 /*ARGSUSED*/ 488 ptyioctl(dev, cmd, data, flag, p) 489 dev_t dev; 490 int cmd; 491 caddr_t data; 492 int flag; 493 struct proc *p; 494 { 495 register struct tty *tp = &pt_tty[minor(dev)]; 496 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; 497 register u_char *cc = tp->t_cc; 498 int stop, error; 499 500 /* 501 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 502 * ttywflush(tp) will hang if there are characters in the outq. 503 */ 504 if (cmd == TIOCEXT) { 505 /* 506 * When the EXTPROC bit is being toggled, we need 507 * to send an TIOCPKT_IOCTL if the packet driver 508 * is turned on. 509 */ 510 if (*(int *)data) { 511 if (pti->pt_flags & PF_PKT) { 512 pti->pt_send |= TIOCPKT_IOCTL; 513 ptcwakeup(tp); 514 } 515 tp->t_lflag |= EXTPROC; 516 } else { 517 if ((tp->t_state & EXTPROC) && 518 (pti->pt_flags & PF_PKT)) { 519 pti->pt_send |= TIOCPKT_IOCTL; 520 ptcwakeup(tp); 521 } 522 tp->t_lflag &= ~EXTPROC; 523 } 524 return(0); 525 } else 526 if (cdevsw[major(dev)].d_open == ptcopen) 527 switch (cmd) { 528 529 case TIOCGPGRP: 530 /* 531 * We aviod calling ttioctl on the controller since, 532 * in that case, tp must be the controlling terminal. 533 */ 534 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 535 return (0); 536 537 case TIOCPKT: 538 if (*(int *)data) { 539 if (pti->pt_flags & PF_UCNTL) 540 return (EINVAL); 541 pti->pt_flags |= PF_PKT; 542 } else 543 pti->pt_flags &= ~PF_PKT; 544 return (0); 545 546 case TIOCUCNTL: 547 if (*(int *)data) { 548 if (pti->pt_flags & PF_PKT) 549 return (EINVAL); 550 pti->pt_flags |= PF_UCNTL; 551 } else 552 pti->pt_flags &= ~PF_UCNTL; 553 return (0); 554 555 case TIOCREMOTE: 556 if (*(int *)data) 557 pti->pt_flags |= PF_REMOTE; 558 else 559 pti->pt_flags &= ~PF_REMOTE; 560 ttyflush(tp, FREAD|FWRITE); 561 return (0); 562 563 #ifdef COMPAT_43 564 case TIOCSETP: 565 case TIOCSETN: 566 #endif 567 case TIOCSETD: 568 case TIOCSETA: 569 case TIOCSETAW: 570 case TIOCSETAF: 571 ndflush(&tp->t_outq, tp->t_outq.c_cc); 572 break; 573 574 case TIOCSIG: 575 if (*(unsigned int *)data >= NSIG) 576 return(EINVAL); 577 if ((tp->t_lflag&NOFLSH) == 0) 578 ttyflush(tp, FREAD|FWRITE); 579 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 580 if ((*(unsigned int *)data == SIGINFO) && 581 ((tp->t_lflag&NOKERNINFO) == 0)) 582 ttyinfo(tp); 583 return(0); 584 } 585 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 586 if (error < 0) 587 error = ttioctl(tp, cmd, data, flag); 588 if (error < 0) { 589 if (pti->pt_flags & PF_UCNTL && 590 (cmd & ~0xff) == UIOCCMD(0)) { 591 if (cmd & 0xff) { 592 pti->pt_ucntl = (u_char)cmd; 593 ptcwakeup(tp, FREAD); 594 } 595 return (0); 596 } 597 error = ENOTTY; 598 } 599 /* 600 * If external processing and packet mode send ioctl packet. 601 */ 602 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 603 switch(cmd) { 604 case TIOCSETA: 605 case TIOCSETAW: 606 case TIOCSETAF: 607 #ifdef COMPAT_43 608 case TIOCSETP: 609 case TIOCSETN: 610 #endif 611 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 612 case TIOCSETC: 613 case TIOCSLTC: 614 case TIOCLBIS: 615 case TIOCLBIC: 616 case TIOCLSET: 617 #endif 618 pti->pt_send |= TIOCPKT_IOCTL; 619 default: 620 break; 621 } 622 } 623 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 624 && CCEQ(cc[VSTART], CTRL('q')); 625 if (pti->pt_flags & PF_NOSTOP) { 626 if (stop) { 627 pti->pt_send &= ~TIOCPKT_NOSTOP; 628 pti->pt_send |= TIOCPKT_DOSTOP; 629 pti->pt_flags &= ~PF_NOSTOP; 630 ptcwakeup(tp, FREAD); 631 } 632 } else { 633 if (!stop) { 634 pti->pt_send &= ~TIOCPKT_DOSTOP; 635 pti->pt_send |= TIOCPKT_NOSTOP; 636 pti->pt_flags |= PF_NOSTOP; 637 ptcwakeup(tp, FREAD); 638 } 639 } 640 return (error); 641 } 642 #endif 643