1 /* $OpenBSD: tty_pty.c,v 1.97 2020/02/20 16:56:52 visa Exp $ */ 2 /* $NetBSD: tty_pty.c,v 1.33.4.1 1996/06/02 09:08:11 mrg Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95 33 */ 34 35 /* 36 * Pseudo-teletype Driver 37 * (Actually two drivers, requiring two entries in 'cdevsw') 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/namei.h> 43 #include <sys/mount.h> 44 #include <sys/ioctl.h> 45 #include <sys/proc.h> 46 #include <sys/tty.h> 47 #include <sys/fcntl.h> 48 #include <sys/file.h> 49 #include <sys/filedesc.h> 50 #include <sys/uio.h> 51 #include <sys/kernel.h> 52 #include <sys/malloc.h> 53 #include <sys/vnode.h> 54 #include <sys/signalvar.h> 55 #include <sys/conf.h> 56 #include <sys/stat.h> 57 #include <sys/sysctl.h> 58 #include <sys/poll.h> 59 #include <sys/pledge.h> 60 #include <sys/rwlock.h> 61 62 #define BUFSIZ 100 /* Chunk size iomoved to/from user */ 63 64 /* 65 * pts == /dev/tty[p-zP-T][0-9a-zA-Z] 66 * ptc == /dev/pty[p-zP-T][0-9a-zA-Z] 67 */ 68 69 /* XXX this needs to come from somewhere sane, and work with MAKEDEV */ 70 #define TTY_LETTERS "pqrstuvwxyzPQRST" 71 #define TTY_SUFFIX "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 72 73 static int pts_major; 74 75 struct pt_softc { 76 struct tty *pt_tty; 77 int pt_flags; 78 struct selinfo pt_selr, pt_selw; 79 u_char pt_send; 80 u_char pt_ucntl; 81 char pty_pn[11]; 82 char pty_sn[11]; 83 }; 84 85 #define NPTY_MIN 8 /* number of initial ptys */ 86 #define NPTY_MAX 992 /* maximum number of ptys supported */ 87 88 static struct pt_softc **pt_softc = NULL; /* pty array */ 89 static int npty = 0; /* size of pty array */ 90 static int maxptys = NPTY_MAX; /* maximum number of ptys */ 91 /* for pty array */ 92 struct rwlock pt_softc_lock = RWLOCK_INITIALIZER("ptarrlk"); 93 94 #define PF_PKT 0x08 /* packet mode */ 95 #define PF_STOPPED 0x10 /* user told stopped */ 96 #define PF_REMOTE 0x20 /* remote and flow controlled input */ 97 #define PF_NOSTOP 0x40 98 #define PF_UCNTL 0x80 /* user control mode */ 99 100 void ptyattach(int); 101 void ptcwakeup(struct tty *, int); 102 struct tty *ptytty(dev_t); 103 void ptsstart(struct tty *); 104 int sysctl_pty(int *, u_int, void *, size_t *, void *, size_t); 105 106 void filt_ptcrdetach(struct knote *); 107 int filt_ptcread(struct knote *, long); 108 void filt_ptcwdetach(struct knote *); 109 int filt_ptcwrite(struct knote *, long); 110 111 static struct pt_softc **ptyarralloc(int); 112 static int check_pty(int); 113 114 static gid_t tty_gid = TTY_GID; 115 116 void ptydevname(int, struct pt_softc *); 117 dev_t pty_getfree(void); 118 119 void ptmattach(int); 120 int ptmopen(dev_t, int, int, struct proc *); 121 int ptmclose(dev_t, int, int, struct proc *); 122 int ptmioctl(dev_t, u_long, caddr_t, int, struct proc *p); 123 static int ptm_vn_open(struct nameidata *); 124 125 void 126 ptydevname(int minor, struct pt_softc *pti) 127 { 128 char buf[11] = "/dev/XtyXX"; 129 int i, j; 130 131 i = minor / (sizeof(TTY_SUFFIX) - 1); 132 j = minor % (sizeof(TTY_SUFFIX) - 1); 133 if (i >= sizeof(TTY_LETTERS) - 1) { 134 pti->pty_pn[0] = '\0'; 135 pti->pty_sn[0] = '\0'; 136 return; 137 } 138 buf[5] = 'p'; 139 buf[8] = TTY_LETTERS[i]; 140 buf[9] = TTY_SUFFIX[j]; 141 memcpy(pti->pty_pn, buf, sizeof(buf)); 142 buf[5] = 't'; 143 memcpy(pti->pty_sn, buf, sizeof(buf)); 144 } 145 146 /* 147 * Allocate and zero array of nelem elements. 148 */ 149 struct pt_softc ** 150 ptyarralloc(int nelem) 151 { 152 struct pt_softc **pt; 153 154 pt = mallocarray(nelem, sizeof(struct pt_softc *), M_DEVBUF, 155 M_WAITOK|M_ZERO); 156 return pt; 157 } 158 159 /* 160 * Check if the minor is correct and ensure necessary structures 161 * are properly allocated. 162 */ 163 int 164 check_pty(int dev) 165 { 166 struct pt_softc *pti; 167 int minor = minor(dev); 168 169 rw_enter_write(&pt_softc_lock); 170 if (minor >= npty) { 171 struct pt_softc **newpt; 172 int newnpty; 173 174 /* check if the requested pty can be granted */ 175 if (minor >= maxptys) 176 goto limit_reached; 177 178 /* grow pty array by powers of two, up to maxptys */ 179 for (newnpty = npty; newnpty <= minor; newnpty *= 2) 180 ; 181 182 if (newnpty > maxptys) 183 newnpty = maxptys; 184 newpt = ptyarralloc(newnpty); 185 186 memcpy(newpt, pt_softc, npty * sizeof(struct pt_softc *)); 187 free(pt_softc, M_DEVBUF, npty * sizeof(struct pt_softc *)); 188 pt_softc = newpt; 189 npty = newnpty; 190 } 191 192 /* 193 * If the entry is not yet allocated, allocate one. 194 */ 195 if (!pt_softc[minor]) { 196 pti = malloc(sizeof(struct pt_softc), M_DEVBUF, 197 M_WAITOK|M_ZERO); 198 pti->pt_tty = ttymalloc(1000000); 199 pti->pt_tty->t_dev = dev; 200 ptydevname(minor, pti); 201 pt_softc[minor] = pti; 202 } 203 rw_exit_write(&pt_softc_lock); 204 return (0); 205 limit_reached: 206 rw_exit_write(&pt_softc_lock); 207 tablefull("pty"); 208 return (ENXIO); 209 } 210 211 /* 212 * Establish n (or default if n is 1) ptys in the system. 213 */ 214 void 215 ptyattach(int n) 216 { 217 /* maybe should allow 0 => none? */ 218 if (n <= 1) 219 n = NPTY_MIN; 220 pt_softc = ptyarralloc(n); 221 npty = n; 222 223 /* 224 * If we have pty, we need ptm too. 225 */ 226 ptmattach(1); 227 } 228 229 int 230 ptsopen(dev_t dev, int flag, int devtype, struct proc *p) 231 { 232 struct pt_softc *pti; 233 struct tty *tp; 234 int error; 235 236 if ((error = check_pty(dev))) 237 return (error); 238 239 pti = pt_softc[minor(dev)]; 240 tp = pti->pt_tty; 241 if ((tp->t_state & TS_ISOPEN) == 0) { 242 tp->t_state |= TS_WOPEN; 243 ttychars(tp); /* Set up default chars */ 244 tp->t_iflag = TTYDEF_IFLAG; 245 tp->t_oflag = TTYDEF_OFLAG; 246 tp->t_lflag = TTYDEF_LFLAG; 247 tp->t_cflag = TTYDEF_CFLAG; 248 tp->t_ispeed = tp->t_ospeed = B115200; 249 ttsetwater(tp); /* would be done in xxparam() */ 250 } else if (tp->t_state & TS_XCLUDE && suser(p) != 0) 251 return (EBUSY); 252 if (tp->t_oproc) /* Ctrlr still around. */ 253 tp->t_state |= TS_CARR_ON; 254 while ((tp->t_state & TS_CARR_ON) == 0) { 255 tp->t_state |= TS_WOPEN; 256 if (flag & FNONBLOCK) 257 break; 258 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, ttopen); 259 if (error) 260 return (error); 261 } 262 error = (*linesw[tp->t_line].l_open)(dev, tp, p); 263 ptcwakeup(tp, FREAD|FWRITE); 264 return (error); 265 } 266 267 int 268 ptsclose(dev_t dev, int flag, int mode, struct proc *p) 269 { 270 struct pt_softc *pti = pt_softc[minor(dev)]; 271 struct tty *tp = pti->pt_tty; 272 int error; 273 274 error = (*linesw[tp->t_line].l_close)(tp, flag, p); 275 error |= ttyclose(tp); 276 ptcwakeup(tp, FREAD|FWRITE); 277 return (error); 278 } 279 280 int 281 ptsread(dev_t dev, struct uio *uio, int flag) 282 { 283 struct proc *p = curproc; 284 struct process *pr = p->p_p; 285 struct pt_softc *pti = pt_softc[minor(dev)]; 286 struct tty *tp = pti->pt_tty; 287 int error = 0; 288 289 again: 290 if (pti->pt_flags & PF_REMOTE) { 291 while (isbackground(pr, tp)) { 292 if ((pr->ps_sigacts->ps_sigignore & sigmask(SIGTTIN)) || 293 (p->p_sigmask & sigmask(SIGTTIN)) || 294 pr->ps_pgrp->pg_jobc == 0 || 295 pr->ps_flags & PS_PPWAIT) 296 return (EIO); 297 pgsignal(pr->ps_pgrp, SIGTTIN, 1); 298 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg); 299 if (error) 300 return (error); 301 } 302 if (tp->t_canq.c_cc == 0) { 303 if (flag & IO_NDELAY) 304 return (EWOULDBLOCK); 305 error = ttysleep(tp, &tp->t_canq, 306 TTIPRI | PCATCH, ttyin); 307 if (error) 308 return (error); 309 goto again; 310 } 311 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 312 if (ureadc(getc(&tp->t_canq), uio) < 0) { 313 error = EFAULT; 314 break; 315 } 316 if (tp->t_canq.c_cc == 1) 317 (void) getc(&tp->t_canq); 318 if (tp->t_canq.c_cc) 319 return (error); 320 } else 321 if (tp->t_oproc) 322 error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 323 ptcwakeup(tp, FWRITE); 324 return (error); 325 } 326 327 /* 328 * Write to pseudo-tty. 329 * Wakeups of controlling tty will happen 330 * indirectly, when tty driver calls ptsstart. 331 */ 332 int 333 ptswrite(dev_t dev, struct uio *uio, int flag) 334 { 335 struct pt_softc *pti = pt_softc[minor(dev)]; 336 struct tty *tp = pti->pt_tty; 337 338 if (tp->t_oproc == 0) 339 return (EIO); 340 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 341 } 342 343 /* 344 * Start output on pseudo-tty. 345 * Wake up process polling or sleeping for input from controlling tty. 346 */ 347 void 348 ptsstart(struct tty *tp) 349 { 350 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 351 352 if (tp->t_state & TS_TTSTOP) 353 return; 354 if (pti->pt_flags & PF_STOPPED) { 355 pti->pt_flags &= ~PF_STOPPED; 356 pti->pt_send = TIOCPKT_START; 357 } 358 ptcwakeup(tp, FREAD); 359 } 360 361 int 362 ptsstop(struct tty *tp, int flush) 363 { 364 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 365 int flag; 366 367 /* note: FLUSHREAD and FLUSHWRITE already ok */ 368 if (flush == 0) { 369 flush = TIOCPKT_STOP; 370 pti->pt_flags |= PF_STOPPED; 371 } else 372 pti->pt_flags &= ~PF_STOPPED; 373 pti->pt_send |= flush; 374 /* change of perspective */ 375 flag = 0; 376 if (flush & FREAD) 377 flag |= FWRITE; 378 if (flush & FWRITE) 379 flag |= FREAD; 380 ptcwakeup(tp, flag); 381 return 0; 382 } 383 384 void 385 ptcwakeup(struct tty *tp, int flag) 386 { 387 struct pt_softc *pti = pt_softc[minor(tp->t_dev)]; 388 389 if (flag & FREAD) { 390 selwakeup(&pti->pt_selr); 391 wakeup(&tp->t_outq.c_cf); 392 } 393 if (flag & FWRITE) { 394 selwakeup(&pti->pt_selw); 395 wakeup(&tp->t_rawq.c_cf); 396 } 397 } 398 399 int ptcopen(dev_t, int, int, struct proc *); 400 401 int 402 ptcopen(dev_t dev, int flag, int devtype, struct proc *p) 403 { 404 struct pt_softc *pti; 405 struct tty *tp; 406 int error; 407 408 if ((error = check_pty(dev))) 409 return (error); 410 411 pti = pt_softc[minor(dev)]; 412 tp = pti->pt_tty; 413 if (tp->t_oproc) 414 return (EIO); 415 tp->t_oproc = ptsstart; 416 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 417 tp->t_lflag &= ~EXTPROC; 418 pti->pt_flags = 0; 419 pti->pt_send = 0; 420 pti->pt_ucntl = 0; 421 return (0); 422 } 423 424 int 425 ptcclose(dev_t dev, int flag, int devtype, struct proc *p) 426 { 427 struct pt_softc *pti = pt_softc[minor(dev)]; 428 struct tty *tp = pti->pt_tty; 429 430 (void)(*linesw[tp->t_line].l_modem)(tp, 0); 431 tp->t_state &= ~TS_CARR_ON; 432 tp->t_oproc = 0; /* mark closed */ 433 return (0); 434 } 435 436 int 437 ptcread(dev_t dev, struct uio *uio, int flag) 438 { 439 struct pt_softc *pti = pt_softc[minor(dev)]; 440 struct tty *tp = pti->pt_tty; 441 char buf[BUFSIZ]; 442 int error = 0, cc, bufcc = 0; 443 444 /* 445 * We want to block until the slave 446 * is open, and there's something to read; 447 * but if we lost the slave or we're NBIO, 448 * then return the appropriate error instead. 449 */ 450 for (;;) { 451 if (tp->t_state & TS_ISOPEN) { 452 if (pti->pt_flags & PF_PKT && pti->pt_send) { 453 error = ureadc((int)pti->pt_send, uio); 454 if (error) 455 return (error); 456 if (pti->pt_send & TIOCPKT_IOCTL) { 457 cc = MIN(uio->uio_resid, 458 sizeof(tp->t_termios)); 459 error = uiomove(&tp->t_termios, cc, uio); 460 if (error) 461 return (error); 462 } 463 pti->pt_send = 0; 464 return (0); 465 } 466 if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) { 467 error = ureadc((int)pti->pt_ucntl, uio); 468 if (error) 469 return (error); 470 pti->pt_ucntl = 0; 471 return (0); 472 } 473 if (tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) 474 break; 475 } 476 if ((tp->t_state & TS_CARR_ON) == 0) 477 return (0); /* EOF */ 478 if (flag & IO_NDELAY) 479 return (EWOULDBLOCK); 480 error = tsleep_nsec(&tp->t_outq.c_cf, TTIPRI | PCATCH, ttyin, 481 INFSLP); 482 if (error) 483 return (error); 484 } 485 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 486 error = ureadc(0, uio); 487 while (uio->uio_resid > 0 && error == 0) { 488 cc = MIN(uio->uio_resid, BUFSIZ); 489 cc = q_to_b(&tp->t_outq, buf, cc); 490 if (cc > bufcc) 491 bufcc = cc; 492 if (cc <= 0) 493 break; 494 error = uiomove(buf, cc, uio); 495 } 496 ttwakeupwr(tp); 497 if (bufcc) 498 explicit_bzero(buf, bufcc); 499 return (error); 500 } 501 502 503 int 504 ptcwrite(dev_t dev, struct uio *uio, int flag) 505 { 506 struct pt_softc *pti = pt_softc[minor(dev)]; 507 struct tty *tp = pti->pt_tty; 508 u_char *cp = NULL; 509 int cc = 0, bufcc = 0; 510 u_char buf[BUFSIZ]; 511 size_t cnt = 0; 512 int error = 0; 513 514 again: 515 if ((tp->t_state & TS_ISOPEN) == 0) 516 goto block; 517 if (pti->pt_flags & PF_REMOTE) { 518 if (tp->t_canq.c_cc) 519 goto block; 520 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG(tp) - 1) { 521 if (cc == 0) { 522 cc = MIN(uio->uio_resid, BUFSIZ); 523 cc = min(cc, TTYHOG(tp) - 1 - tp->t_canq.c_cc); 524 if (cc > bufcc) 525 bufcc = cc; 526 cp = buf; 527 error = uiomove(cp, cc, uio); 528 if (error) 529 goto done; 530 /* check again for safety */ 531 if ((tp->t_state & TS_ISOPEN) == 0) { 532 error = EIO; 533 goto done; 534 } 535 } 536 if (cc) 537 (void) b_to_q((char *)cp, cc, &tp->t_canq); 538 cc = 0; 539 } 540 (void) putc(0, &tp->t_canq); 541 ttwakeup(tp); 542 wakeup(&tp->t_canq); 543 goto done; 544 } 545 do { 546 if (cc == 0) { 547 cc = MIN(uio->uio_resid, BUFSIZ); 548 if (cc > bufcc) 549 bufcc = cc; 550 cp = buf; 551 error = uiomove(cp, cc, uio); 552 if (error) 553 goto done; 554 /* check again for safety */ 555 if ((tp->t_state & TS_ISOPEN) == 0) { 556 error = EIO; 557 goto done; 558 } 559 } 560 bufcc = cc; 561 while (cc > 0) { 562 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG(tp) - 2 && 563 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) { 564 wakeup(&tp->t_rawq); 565 goto block; 566 } 567 (*linesw[tp->t_line].l_rint)(*cp++, tp); 568 cnt++; 569 cc--; 570 } 571 cc = 0; 572 } while (uio->uio_resid > 0); 573 goto done; 574 block: 575 /* 576 * Come here to wait for slave to open, for space 577 * in outq, or space in rawq. 578 */ 579 if ((tp->t_state & TS_CARR_ON) == 0) { 580 error = EIO; 581 goto done; 582 } 583 if (flag & IO_NDELAY) { 584 /* adjust for data copied in but not written */ 585 uio->uio_resid += cc; 586 if (cnt == 0) 587 error = EWOULDBLOCK; 588 goto done; 589 } 590 error = tsleep_nsec(&tp->t_rawq.c_cf, TTOPRI | PCATCH, ttyout, INFSLP); 591 if (error == 0) 592 goto again; 593 594 /* adjust for data copied in but not written */ 595 uio->uio_resid += cc; 596 done: 597 if (bufcc) 598 explicit_bzero(buf, bufcc); 599 return (error); 600 } 601 602 int 603 ptcpoll(dev_t dev, int events, struct proc *p) 604 { 605 struct pt_softc *pti = pt_softc[minor(dev)]; 606 struct tty *tp = pti->pt_tty; 607 int revents = 0, s; 608 609 if (!ISSET(tp->t_state, TS_ISOPEN) && ISSET(tp->t_state, TS_CARR_ON)) 610 goto notopen; 611 612 if (events & (POLLIN | POLLRDNORM)) { 613 /* 614 * Need to protect access to t_outq 615 */ 616 s = spltty(); 617 if ((tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP)) || 618 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 619 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 620 revents |= events & (POLLIN | POLLRDNORM); 621 splx(s); 622 } 623 /* NOTE: POLLHUP and POLLOUT/POLLWRNORM are mutually exclusive */ 624 if (!ISSET(tp->t_state, TS_CARR_ON)) { 625 revents |= POLLHUP; 626 } else if (events & (POLLOUT | POLLWRNORM)) { 627 if ((pti->pt_flags & PF_REMOTE) ? 628 (tp->t_canq.c_cc == 0) : 629 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG(tp) - 2) || 630 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))) 631 revents |= events & (POLLOUT | POLLWRNORM); 632 } 633 if (events & (POLLPRI | POLLRDBAND)) { 634 /* If in packet or user control mode, check for data. */ 635 if (((pti->pt_flags & PF_PKT) && pti->pt_send) || 636 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 637 revents |= events & (POLLPRI | POLLRDBAND); 638 } 639 640 if (revents == 0) { 641 notopen: 642 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) 643 selrecord(p, &pti->pt_selr); 644 if (events & (POLLOUT | POLLWRNORM)) 645 selrecord(p, &pti->pt_selw); 646 } 647 648 return (revents); 649 } 650 651 void 652 filt_ptcrdetach(struct knote *kn) 653 { 654 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 655 int s; 656 657 s = spltty(); 658 SLIST_REMOVE(&pti->pt_selr.si_note, kn, knote, kn_selnext); 659 splx(s); 660 } 661 662 int 663 filt_ptcread(struct knote *kn, long hint) 664 { 665 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 666 struct tty *tp; 667 668 tp = pti->pt_tty; 669 kn->kn_data = 0; 670 671 if (ISSET(tp->t_state, TS_ISOPEN)) { 672 if (!ISSET(tp->t_state, TS_TTSTOP)) 673 kn->kn_data = tp->t_outq.c_cc; 674 if (((pti->pt_flags & PF_PKT) && pti->pt_send) || 675 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)) 676 kn->kn_data++; 677 } 678 679 if (!ISSET(tp->t_state, TS_CARR_ON)) { 680 kn->kn_flags |= EV_EOF; 681 return (1); 682 } 683 684 return (kn->kn_data > 0); 685 } 686 687 void 688 filt_ptcwdetach(struct knote *kn) 689 { 690 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 691 int s; 692 693 s = spltty(); 694 SLIST_REMOVE(&pti->pt_selw.si_note, kn, knote, kn_selnext); 695 splx(s); 696 } 697 698 int 699 filt_ptcwrite(struct knote *kn, long hint) 700 { 701 struct pt_softc *pti = (struct pt_softc *)kn->kn_hook; 702 struct tty *tp; 703 704 tp = pti->pt_tty; 705 kn->kn_data = 0; 706 707 if (ISSET(tp->t_state, TS_ISOPEN)) { 708 if (ISSET(pti->pt_flags, PF_REMOTE)) { 709 if (tp->t_canq.c_cc == 0) 710 kn->kn_data = tp->t_canq.c_cn; 711 } else if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG(tp)-2) 712 kn->kn_data = tp->t_canq.c_cn - 713 (tp->t_rawq.c_cc + tp->t_canq.c_cc); 714 } 715 716 return (kn->kn_data > 0); 717 } 718 719 const struct filterops ptcread_filtops = { 720 .f_flags = FILTEROP_ISFD, 721 .f_attach = NULL, 722 .f_detach = filt_ptcrdetach, 723 .f_event = filt_ptcread, 724 }; 725 726 const struct filterops ptcwrite_filtops = { 727 .f_flags = FILTEROP_ISFD, 728 .f_attach = NULL, 729 .f_detach = filt_ptcwdetach, 730 .f_event = filt_ptcwrite, 731 }; 732 733 int 734 ptckqfilter(dev_t dev, struct knote *kn) 735 { 736 struct pt_softc *pti = pt_softc[minor(dev)]; 737 struct klist *klist; 738 int s; 739 740 switch (kn->kn_filter) { 741 case EVFILT_READ: 742 klist = &pti->pt_selr.si_note; 743 kn->kn_fop = &ptcread_filtops; 744 break; 745 case EVFILT_WRITE: 746 klist = &pti->pt_selw.si_note; 747 kn->kn_fop = &ptcwrite_filtops; 748 break; 749 default: 750 return (EINVAL); 751 } 752 753 kn->kn_hook = (caddr_t)pti; 754 755 s = spltty(); 756 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 757 splx(s); 758 759 return (0); 760 } 761 762 struct tty * 763 ptytty(dev_t dev) 764 { 765 struct pt_softc *pti = pt_softc[minor(dev)]; 766 struct tty *tp = pti->pt_tty; 767 768 return (tp); 769 } 770 771 int 772 ptyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 773 { 774 struct pt_softc *pti = pt_softc[minor(dev)]; 775 struct tty *tp = pti->pt_tty; 776 u_char *cc = tp->t_cc; 777 int stop, error; 778 779 /* 780 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 781 * ttywflush(tp) will hang if there are characters in the outq. 782 */ 783 if (cmd == TIOCEXT) { 784 /* 785 * When the EXTPROC bit is being toggled, we need 786 * to send an TIOCPKT_IOCTL if the packet driver 787 * is turned on. 788 */ 789 if (*(int *)data) { 790 if (pti->pt_flags & PF_PKT) { 791 pti->pt_send |= TIOCPKT_IOCTL; 792 ptcwakeup(tp, FREAD); 793 } 794 tp->t_lflag |= EXTPROC; 795 } else { 796 if ((tp->t_lflag & EXTPROC) && 797 (pti->pt_flags & PF_PKT)) { 798 pti->pt_send |= TIOCPKT_IOCTL; 799 ptcwakeup(tp, FREAD); 800 } 801 tp->t_lflag &= ~EXTPROC; 802 } 803 return(0); 804 } else if (cdevsw[major(dev)].d_open == ptcopen) 805 switch (cmd) { 806 807 case TIOCGPGRP: 808 /* 809 * We avoid calling ttioctl on the controller since, 810 * in that case, tp must be the controlling terminal. 811 */ 812 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 813 return (0); 814 815 case TIOCPKT: 816 if (*(int *)data) { 817 if (pti->pt_flags & PF_UCNTL) 818 return (EINVAL); 819 pti->pt_flags |= PF_PKT; 820 } else 821 pti->pt_flags &= ~PF_PKT; 822 return (0); 823 824 case TIOCUCNTL: 825 if (*(int *)data) { 826 if (pti->pt_flags & PF_PKT) 827 return (EINVAL); 828 pti->pt_flags |= PF_UCNTL; 829 } else 830 pti->pt_flags &= ~PF_UCNTL; 831 return (0); 832 833 case TIOCREMOTE: 834 if (*(int *)data) 835 pti->pt_flags |= PF_REMOTE; 836 else 837 pti->pt_flags &= ~PF_REMOTE; 838 ttyflush(tp, FREAD|FWRITE); 839 return (0); 840 841 case TIOCSETD: 842 case TIOCSETA: 843 case TIOCSETAW: 844 case TIOCSETAF: 845 ndflush(&tp->t_outq, tp->t_outq.c_cc); 846 break; 847 848 case TIOCSIG: 849 if (*(unsigned int *)data >= NSIG || 850 *(unsigned int *)data == 0) 851 return(EINVAL); 852 if ((tp->t_lflag & NOFLSH) == 0) 853 ttyflush(tp, FREAD|FWRITE); 854 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 855 if ((*(unsigned int *)data == SIGINFO) && 856 ((tp->t_lflag & NOKERNINFO) == 0)) 857 ttyinfo(tp); 858 return (0); 859 860 case FIONREAD: 861 /* 862 * FIONREAD on the master side must return the amount 863 * in the output queue rather than the input. 864 */ 865 *(int *)data = tp->t_outq.c_cc; 866 return (0); 867 } 868 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 869 if (error < 0) 870 error = ttioctl(tp, cmd, data, flag, p); 871 if (error < 0) { 872 /* 873 * Translate TIOCSBRK/TIOCCBRK to user mode ioctls to 874 * let the master interpret BREAK conditions. 875 */ 876 switch (cmd) { 877 case TIOCSBRK: 878 cmd = UIOCCMD(TIOCUCNTL_SBRK); 879 break; 880 case TIOCCBRK: 881 cmd = UIOCCMD(TIOCUCNTL_CBRK); 882 break; 883 default: 884 break; 885 } 886 if (pti->pt_flags & PF_UCNTL && 887 (cmd & ~0xff) == UIOCCMD(0)) { 888 if (cmd & 0xff) { 889 pti->pt_ucntl = (u_char)cmd; 890 ptcwakeup(tp, FREAD); 891 } 892 return (0); 893 } 894 error = ENOTTY; 895 } 896 /* 897 * If external processing and packet mode send ioctl packet. 898 */ 899 if ((tp->t_lflag & EXTPROC) && (pti->pt_flags & PF_PKT)) { 900 switch (cmd) { 901 case TIOCSETA: 902 case TIOCSETAW: 903 case TIOCSETAF: 904 pti->pt_send |= TIOCPKT_IOCTL; 905 ptcwakeup(tp, FREAD); 906 default: 907 break; 908 } 909 } 910 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) && 911 CCEQ(cc[VSTART], CTRL('q')); 912 if (pti->pt_flags & PF_NOSTOP) { 913 if (stop) { 914 pti->pt_send &= ~TIOCPKT_NOSTOP; 915 pti->pt_send |= TIOCPKT_DOSTOP; 916 pti->pt_flags &= ~PF_NOSTOP; 917 ptcwakeup(tp, FREAD); 918 } 919 } else { 920 if (!stop) { 921 pti->pt_send &= ~TIOCPKT_DOSTOP; 922 pti->pt_send |= TIOCPKT_NOSTOP; 923 pti->pt_flags |= PF_NOSTOP; 924 ptcwakeup(tp, FREAD); 925 } 926 } 927 return (error); 928 } 929 930 /* 931 * Return pty-related information. 932 */ 933 int 934 sysctl_pty(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 935 size_t newlen) 936 { 937 if (namelen != 1) 938 return (ENOTDIR); 939 940 switch (name[0]) { 941 default: 942 return (EOPNOTSUPP); 943 } 944 /* NOTREACHED */ 945 } 946 947 /* 948 * Check if a pty is free to use. 949 */ 950 static int 951 pty_isfree_locked(int minor) 952 { 953 struct pt_softc *pt = pt_softc[minor]; 954 955 return (pt == NULL || pt->pt_tty == NULL || 956 pt->pt_tty->t_oproc == NULL); 957 } 958 959 static int 960 pty_isfree(int minor) 961 { 962 int isfree; 963 964 rw_enter_read(&pt_softc_lock); 965 isfree = pty_isfree_locked(minor); 966 rw_exit_read(&pt_softc_lock); 967 return(isfree); 968 } 969 970 dev_t 971 pty_getfree(void) 972 { 973 int i; 974 975 rw_enter_read(&pt_softc_lock); 976 for (i = 0; i < npty; i++) { 977 if (pty_isfree_locked(i)) 978 break; 979 } 980 rw_exit_read(&pt_softc_lock); 981 return (makedev(pts_major, i)); 982 } 983 984 /* 985 * Hacked up version of vn_open. We _only_ handle ptys and only open 986 * them with FREAD|FWRITE and never deal with creat or stuff like that. 987 * 988 * We need it because we have to fake up root credentials to open the pty. 989 */ 990 static int 991 ptm_vn_open(struct nameidata *ndp) 992 { 993 struct proc *p = ndp->ni_cnd.cn_proc; 994 struct ucred *cred; 995 struct vattr vattr; 996 struct vnode *vp; 997 int error; 998 999 if ((error = namei(ndp)) != 0) 1000 return (error); 1001 vp = ndp->ni_vp; 1002 if (vp->v_type != VCHR) { 1003 error = EINVAL; 1004 goto bad; 1005 } 1006 1007 /* 1008 * Get us a fresh cred with root privileges. 1009 */ 1010 cred = crget(); 1011 error = VOP_OPEN(vp, FREAD|FWRITE, cred, p); 1012 if (!error) { 1013 /* update atime/mtime */ 1014 VATTR_NULL(&vattr); 1015 getnanotime(&vattr.va_atime); 1016 vattr.va_mtime = vattr.va_atime; 1017 vattr.va_vaflags |= VA_UTIMES_NULL; 1018 (void)VOP_SETATTR(vp, &vattr, p->p_ucred, p); 1019 } 1020 crfree(cred); 1021 1022 if (error) 1023 goto bad; 1024 1025 vp->v_writecount++; 1026 1027 return (0); 1028 bad: 1029 vput(vp); 1030 return (error); 1031 } 1032 1033 void 1034 ptmattach(int n) 1035 { 1036 /* find the major and minor of the pty devices */ 1037 int i; 1038 1039 for (i = 0; i < nchrdev; i++) 1040 if (cdevsw[i].d_open == ptsopen) 1041 break; 1042 1043 if (i == nchrdev) 1044 panic("ptmattach: Can't find pty slave in cdevsw"); 1045 1046 pts_major = i; 1047 } 1048 1049 int 1050 ptmopen(dev_t dev, int flag, int mode, struct proc *p) 1051 { 1052 return(0); 1053 } 1054 1055 1056 int 1057 ptmclose(dev_t dev, int flag, int mode, struct proc *p) 1058 { 1059 return (0); 1060 } 1061 1062 int 1063 ptmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 1064 { 1065 dev_t newdev, error; 1066 struct pt_softc * pti; 1067 struct nameidata cnd, snd; 1068 struct filedesc *fdp = p->p_fd; 1069 struct file *cfp = NULL, *sfp = NULL; 1070 int cindx, sindx; 1071 uid_t uid; 1072 gid_t gid; 1073 struct vattr vattr; 1074 struct ucred *cred; 1075 struct ptmget *ptm = (struct ptmget *)data; 1076 1077 switch (cmd) { 1078 case PTMGET: 1079 fdplock(fdp); 1080 /* Grab two filedescriptors. */ 1081 if ((error = falloc(p, &cfp, &cindx)) != 0) { 1082 fdpunlock(fdp); 1083 break; 1084 } 1085 if ((error = falloc(p, &sfp, &sindx)) != 0) { 1086 fdremove(fdp, cindx); 1087 closef(cfp, p); 1088 fdpunlock(fdp); 1089 break; 1090 } 1091 1092 retry: 1093 /* Find and open a free master pty. */ 1094 newdev = pty_getfree(); 1095 if ((error = check_pty(newdev))) 1096 goto bad; 1097 pti = pt_softc[minor(newdev)]; 1098 NDINIT(&cnd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, 1099 pti->pty_pn, p); 1100 cnd.ni_pledge = PLEDGE_RPATH | PLEDGE_WPATH; 1101 if ((error = ptm_vn_open(&cnd)) != 0) { 1102 /* 1103 * Check if the master open failed because we lost 1104 * the race to grab it. 1105 */ 1106 if (error == EIO && !pty_isfree(minor(newdev))) 1107 goto retry; 1108 goto bad; 1109 } 1110 cfp->f_flag = FREAD|FWRITE; 1111 cfp->f_type = DTYPE_VNODE; 1112 cfp->f_ops = &vnops; 1113 cfp->f_data = (caddr_t) cnd.ni_vp; 1114 VOP_UNLOCK(cnd.ni_vp); 1115 1116 /* 1117 * Open the slave. 1118 * namei -> setattr -> unlock -> revoke -> vrele -> 1119 * namei -> open -> unlock 1120 * Three stage rocket: 1121 * 1. Change the owner and permissions on the slave. 1122 * 2. Revoke all the users of the slave. 1123 * 3. open the slave. 1124 */ 1125 NDINIT(&snd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, 1126 pti->pty_sn, p); 1127 snd.ni_pledge = PLEDGE_RPATH | PLEDGE_WPATH; 1128 snd.ni_unveil = UNVEIL_READ | UNVEIL_WRITE; 1129 if ((error = namei(&snd)) != 0) 1130 goto bad; 1131 if ((snd.ni_vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 1132 gid = tty_gid; 1133 /* get real uid */ 1134 uid = p->p_ucred->cr_ruid; 1135 1136 VATTR_NULL(&vattr); 1137 vattr.va_uid = uid; 1138 vattr.va_gid = gid; 1139 vattr.va_mode = (S_IRUSR|S_IWUSR|S_IWGRP) & ALLPERMS; 1140 /* Get a fake cred to pretend we're root. */ 1141 cred = crget(); 1142 error = VOP_SETATTR(snd.ni_vp, &vattr, cred, p); 1143 crfree(cred); 1144 if (error) { 1145 vput(snd.ni_vp); 1146 goto bad; 1147 } 1148 } 1149 VOP_UNLOCK(snd.ni_vp); 1150 if (snd.ni_vp->v_usecount > 1 || 1151 (snd.ni_vp->v_flag & (VALIASED))) 1152 VOP_REVOKE(snd.ni_vp, REVOKEALL); 1153 1154 /* 1155 * The vnode is useless after the revoke, we need to 1156 * namei again. 1157 */ 1158 vrele(snd.ni_vp); 1159 1160 NDINIT(&snd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, 1161 pti->pty_sn, p); 1162 snd.ni_pledge = PLEDGE_RPATH | PLEDGE_WPATH; 1163 snd.ni_unveil= UNVEIL_READ | UNVEIL_WRITE; 1164 /* now open it */ 1165 if ((error = ptm_vn_open(&snd)) != 0) 1166 goto bad; 1167 sfp->f_flag = FREAD|FWRITE; 1168 sfp->f_type = DTYPE_VNODE; 1169 sfp->f_ops = &vnops; 1170 sfp->f_data = (caddr_t) snd.ni_vp; 1171 VOP_UNLOCK(snd.ni_vp); 1172 1173 /* now, put the indexen and names into struct ptmget */ 1174 ptm->cfd = cindx; 1175 ptm->sfd = sindx; 1176 memcpy(ptm->cn, pti->pty_pn, sizeof(pti->pty_pn)); 1177 memcpy(ptm->sn, pti->pty_sn, sizeof(pti->pty_sn)); 1178 1179 /* insert files now that we've passed all errors */ 1180 fdinsert(fdp, cindx, 0, cfp); 1181 fdinsert(fdp, sindx, 0, sfp); 1182 fdpunlock(fdp); 1183 FRELE(cfp, p); 1184 FRELE(sfp, p); 1185 break; 1186 default: 1187 error = EINVAL; 1188 break; 1189 } 1190 return (error); 1191 bad: 1192 fdremove(fdp, cindx); 1193 closef(cfp, p); 1194 fdremove(fdp, sindx); 1195 closef(sfp, p); 1196 fdpunlock(fdp); 1197 return (error); 1198 } 1199