1 /*- 2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Portions of this software were developed under sponsorship from Snow 6 * B.V., the Netherlands. 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_compat.h" 34 35 #include <sys/param.h> 36 #include <sys/conf.h> 37 #include <sys/cons.h> 38 #include <sys/fcntl.h> 39 #include <sys/file.h> 40 #include <sys/filio.h> 41 #ifdef COMPAT_43TTY 42 #include <sys/ioctl_compat.h> 43 #endif /* COMPAT_43TTY */ 44 #include <sys/kernel.h> 45 #include <sys/limits.h> 46 #include <sys/malloc.h> 47 #include <sys/mount.h> 48 #include <sys/poll.h> 49 #include <sys/priv.h> 50 #include <sys/proc.h> 51 #include <sys/serial.h> 52 #include <sys/signal.h> 53 #include <sys/stat.h> 54 #include <sys/sx.h> 55 #include <sys/sysctl.h> 56 #include <sys/systm.h> 57 #include <sys/tty.h> 58 #include <sys/ttycom.h> 59 #define TTYDEFCHARS 60 #include <sys/ttydefaults.h> 61 #undef TTYDEFCHARS 62 #include <sys/ucred.h> 63 #include <sys/vnode.h> 64 65 #include <machine/stdarg.h> 66 67 static MALLOC_DEFINE(M_TTY, "tty", "tty device"); 68 69 static void tty_rel_free(struct tty *tp); 70 71 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list); 72 static struct sx tty_list_sx; 73 SX_SYSINIT(tty_list, &tty_list_sx, "tty list"); 74 static unsigned int tty_list_count = 0; 75 76 /* Character device of /dev/console. */ 77 static struct cdev *dev_console; 78 static const char *dev_console_filename; 79 80 /* 81 * Flags that are supported and stored by this implementation. 82 */ 83 #define TTYSUP_IFLAG (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\ 84 INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL) 85 #define TTYSUP_OFLAG (OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET) 86 #define TTYSUP_LFLAG (ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\ 87 ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\ 88 FLUSHO|NOKERNINFO|NOFLSH) 89 #define TTYSUP_CFLAG (CIGNORE|CSIZE|CSTOPB|CREAD|PARENB|PARODD|\ 90 HUPCL|CLOCAL|CCTS_OFLOW|CRTS_IFLOW|CDTR_IFLOW|\ 91 CDSR_OFLOW|CCAR_OFLOW) 92 93 #define TTY_CALLOUT(tp,d) ((d) != (tp)->t_dev && (d) != dev_console) 94 95 /* 96 * Set TTY buffer sizes. 97 */ 98 99 #define TTYBUF_MAX 65536 100 101 static void 102 tty_watermarks(struct tty *tp) 103 { 104 size_t bs; 105 106 /* Provide an input buffer for 0.2 seconds of data. */ 107 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX); 108 ttyinq_setsize(&tp->t_inq, tp, bs); 109 110 /* Set low watermark at 10% (when 90% is available). */ 111 tp->t_inlow = (ttyinq_getsize(&tp->t_inq) * 9) / 10; 112 113 /* Provide an ouput buffer for 0.2 seconds of data. */ 114 bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX); 115 ttyoutq_setsize(&tp->t_outq, tp, bs); 116 117 /* Set low watermark at 10% (when 90% is available). */ 118 tp->t_outlow = (ttyoutq_getsize(&tp->t_outq) * 9) / 10; 119 } 120 121 static int 122 tty_drain(struct tty *tp) 123 { 124 int error; 125 126 if (ttyhook_hashook(tp, getc_inject)) 127 /* buffer is inaccessible */ 128 return (0); 129 130 while (ttyoutq_bytesused(&tp->t_outq) > 0) { 131 ttydevsw_outwakeup(tp); 132 /* Could be handled synchronously. */ 133 if (ttyoutq_bytesused(&tp->t_outq) == 0) 134 return (0); 135 136 /* Wait for data to be drained. */ 137 error = tty_wait(tp, &tp->t_outwait); 138 if (error) 139 return (error); 140 } 141 142 return (0); 143 } 144 145 /* 146 * Though ttydev_enter() and ttydev_leave() seem to be related, they 147 * don't have to be used together. ttydev_enter() is used by the cdev 148 * operations to prevent an actual operation from being processed when 149 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open() 150 * and ttydev_close() to determine whether per-TTY data should be 151 * deallocated. 152 */ 153 154 static __inline int 155 ttydev_enter(struct tty *tp) 156 { 157 tty_lock(tp); 158 159 if (tty_gone(tp) || !tty_opened(tp)) { 160 /* Device is already gone. */ 161 tty_unlock(tp); 162 return (ENXIO); 163 } 164 165 return (0); 166 } 167 168 static void 169 ttydev_leave(struct tty *tp) 170 { 171 tty_lock_assert(tp, MA_OWNED); 172 173 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) { 174 /* Device is still opened somewhere. */ 175 tty_unlock(tp); 176 return; 177 } 178 179 tp->t_flags |= TF_OPENCLOSE; 180 181 /* Stop asynchronous I/O. */ 182 funsetown(&tp->t_sigio); 183 184 /* Remove console TTY. */ 185 if (constty == tp) 186 constty_clear(); 187 188 /* Drain any output. */ 189 MPASS((tp->t_flags & TF_STOPPED) == 0); 190 if (!tty_gone(tp)) 191 tty_drain(tp); 192 193 ttydisc_close(tp); 194 195 /* Destroy associated buffers already. */ 196 ttyinq_free(&tp->t_inq); 197 tp->t_inlow = 0; 198 ttyoutq_free(&tp->t_outq); 199 tp->t_outlow = 0; 200 201 knlist_clear(&tp->t_inpoll.si_note, 1); 202 knlist_clear(&tp->t_outpoll.si_note, 1); 203 204 if (!tty_gone(tp)) 205 ttydevsw_close(tp); 206 207 tp->t_flags &= ~TF_OPENCLOSE; 208 tty_rel_free(tp); 209 } 210 211 /* 212 * Operations that are exposed through the character device in /dev. 213 */ 214 static int 215 ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 216 { 217 struct tty *tp = dev->si_drv1; 218 int error = 0; 219 220 /* Disallow access when the TTY belongs to a different prison. */ 221 if (dev->si_cred != NULL && 222 dev->si_cred->cr_prison != td->td_ucred->cr_prison && 223 priv_check(td, PRIV_TTY_PRISON)) { 224 return (EPERM); 225 } 226 227 tty_lock(tp); 228 if (tty_gone(tp)) { 229 /* Device is already gone. */ 230 tty_unlock(tp); 231 return (ENXIO); 232 } 233 /* 234 * Prevent the TTY from being opened when being torn down or 235 * built up by unrelated processes. 236 */ 237 if (tp->t_flags & TF_OPENCLOSE) { 238 tty_unlock(tp); 239 return (EBUSY); 240 } 241 tp->t_flags |= TF_OPENCLOSE; 242 243 /* 244 * Make sure the "tty" and "cua" device cannot be opened at the 245 * same time. 246 */ 247 if (TTY_CALLOUT(tp, dev)) { 248 if (tp->t_flags & TF_OPENED_IN) { 249 error = EBUSY; 250 goto done; 251 } 252 } else { 253 if (tp->t_flags & TF_OPENED_OUT) { 254 error = EBUSY; 255 goto done; 256 } 257 } 258 259 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) { 260 error = EBUSY; 261 goto done; 262 } 263 264 if (!tty_opened(tp)) { 265 /* Set proper termios flags. */ 266 if (TTY_CALLOUT(tp, dev)) { 267 tp->t_termios = tp->t_termios_init_out; 268 } else { 269 tp->t_termios = tp->t_termios_init_in; 270 } 271 ttydevsw_param(tp, &tp->t_termios); 272 273 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0); 274 275 error = ttydevsw_open(tp); 276 if (error != 0) 277 goto done; 278 279 ttydisc_open(tp); 280 tty_watermarks(tp); 281 } 282 283 /* Wait for Carrier Detect. */ 284 if (!TTY_CALLOUT(tp, dev) && (oflags & O_NONBLOCK) == 0 && 285 (tp->t_termios.c_cflag & CLOCAL) == 0) { 286 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) { 287 error = tty_wait(tp, &tp->t_dcdwait); 288 if (error != 0) 289 goto done; 290 } 291 } 292 293 if (TTY_CALLOUT(tp, dev)) { 294 tp->t_flags |= TF_OPENED_OUT; 295 } else { 296 tp->t_flags |= TF_OPENED_IN; 297 } 298 299 done: tp->t_flags &= ~TF_OPENCLOSE; 300 ttydev_leave(tp); 301 302 return (error); 303 } 304 305 static int 306 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 307 { 308 struct tty *tp = dev->si_drv1; 309 310 tty_lock(tp); 311 312 /* 313 * This can only be called once. The callin and the callout 314 * devices cannot be opened at the same time. 315 */ 316 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED); 317 tp->t_flags &= ~(TF_OPENED|TF_EXCLUDE|TF_STOPPED); 318 319 /* Properly wake up threads that are stuck - revoke(). */ 320 tp->t_revokecnt++; 321 tty_wakeup(tp, FREAD|FWRITE); 322 cv_broadcast(&tp->t_bgwait); 323 324 ttydev_leave(tp); 325 326 return (0); 327 } 328 329 static __inline int 330 tty_is_ctty(struct tty *tp, struct proc *p) 331 { 332 tty_lock_assert(tp, MA_OWNED); 333 334 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT); 335 } 336 337 static int 338 tty_wait_background(struct tty *tp, struct thread *td, int sig) 339 { 340 struct proc *p = td->td_proc; 341 struct pgrp *pg; 342 int error; 343 344 MPASS(sig == SIGTTIN || sig == SIGTTOU); 345 tty_lock_assert(tp, MA_OWNED); 346 347 for (;;) { 348 PROC_LOCK(p); 349 /* 350 * The process should only sleep, when: 351 * - This terminal is the controling terminal 352 * - Its process group is not the foreground process 353 * group 354 * - The parent process isn't waiting for the child to 355 * exit 356 * - the signal to send to the process isn't masked 357 */ 358 if (!tty_is_ctty(tp, p) || 359 p->p_pgrp == tp->t_pgrp || p->p_flag & P_PPWAIT || 360 SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) || 361 SIGISMEMBER(td->td_sigmask, sig)) { 362 /* Allow the action to happen. */ 363 PROC_UNLOCK(p); 364 return (0); 365 } 366 367 /* 368 * Send the signal and sleep until we're the new 369 * foreground process group. 370 */ 371 pg = p->p_pgrp; 372 PROC_UNLOCK(p); 373 if (pg->pg_jobc == 0) 374 return (EIO); 375 PGRP_LOCK(pg); 376 pgsignal(pg, sig, 1); 377 PGRP_UNLOCK(pg); 378 379 error = tty_wait(tp, &tp->t_bgwait); 380 if (error) 381 return (error); 382 } 383 } 384 385 static int 386 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag) 387 { 388 struct tty *tp = dev->si_drv1; 389 int error; 390 391 error = ttydev_enter(tp); 392 if (error) 393 goto done; 394 395 error = tty_wait_background(tp, curthread, SIGTTIN); 396 if (error) { 397 tty_unlock(tp); 398 goto done; 399 } 400 401 error = ttydisc_read(tp, uio, ioflag); 402 tty_unlock(tp); 403 404 /* 405 * The read() call should not throw an error when the device is 406 * being destroyed. Silently convert it to an EOF. 407 */ 408 done: if (error == ENXIO) 409 error = 0; 410 return (error); 411 } 412 413 static int 414 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag) 415 { 416 struct tty *tp = dev->si_drv1; 417 int error; 418 419 error = ttydev_enter(tp); 420 if (error) 421 return (error); 422 423 if (tp->t_termios.c_lflag & TOSTOP) { 424 error = tty_wait_background(tp, curthread, SIGTTOU); 425 if (error) { 426 tty_unlock(tp); 427 return (error); 428 } 429 } 430 431 error = ttydisc_write(tp, uio, ioflag); 432 tty_unlock(tp); 433 434 return (error); 435 } 436 437 static int 438 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 439 struct thread *td) 440 { 441 struct tty *tp = dev->si_drv1; 442 int error; 443 444 error = ttydev_enter(tp); 445 if (error) 446 return (error); 447 448 switch (cmd) { 449 case TIOCCBRK: 450 case TIOCCONS: 451 case TIOCDRAIN: 452 case TIOCEXCL: 453 case TIOCFLUSH: 454 case TIOCNXCL: 455 case TIOCSBRK: 456 case TIOCSCTTY: 457 case TIOCSETA: 458 case TIOCSETAF: 459 case TIOCSETAW: 460 case TIOCSPGRP: 461 case TIOCSTART: 462 case TIOCSTAT: 463 case TIOCSTOP: 464 case TIOCSWINSZ: 465 #if 0 466 case TIOCSDRAINWAIT: 467 case TIOCSETD: 468 case TIOCSTI: 469 #endif 470 #ifdef COMPAT_43TTY 471 case TIOCLBIC: 472 case TIOCLBIS: 473 case TIOCLSET: 474 case TIOCSETC: 475 case OTIOCSETD: 476 case TIOCSETN: 477 case TIOCSETP: 478 case TIOCSLTC: 479 #endif /* COMPAT_43TTY */ 480 /* 481 * If the ioctl() causes the TTY to be modified, let it 482 * wait in the background. 483 */ 484 error = tty_wait_background(tp, curthread, SIGTTOU); 485 if (error) 486 goto done; 487 } 488 489 error = tty_ioctl(tp, cmd, data, td); 490 done: tty_unlock(tp); 491 492 return (error); 493 } 494 495 static int 496 ttydev_poll(struct cdev *dev, int events, struct thread *td) 497 { 498 struct tty *tp = dev->si_drv1; 499 int error, revents = 0; 500 501 error = ttydev_enter(tp); 502 if (error) { 503 /* Don't return the error here, but the event mask. */ 504 return (events & 505 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 506 } 507 508 if (events & (POLLIN|POLLRDNORM)) { 509 /* See if we can read something. */ 510 if (ttydisc_read_poll(tp) > 0) 511 revents |= events & (POLLIN|POLLRDNORM); 512 } 513 if (events & (POLLOUT|POLLWRNORM)) { 514 /* See if we can write something. */ 515 if (ttydisc_write_poll(tp) > 0) 516 revents |= events & (POLLOUT|POLLWRNORM); 517 } 518 if (tp->t_flags & TF_ZOMBIE) 519 /* Hangup flag on zombie state. */ 520 revents |= events & POLLHUP; 521 522 if (revents == 0) { 523 if (events & (POLLIN|POLLRDNORM)) 524 selrecord(td, &tp->t_inpoll); 525 if (events & (POLLOUT|POLLWRNORM)) 526 selrecord(td, &tp->t_outpoll); 527 } 528 529 tty_unlock(tp); 530 531 return (revents); 532 } 533 534 static int 535 ttydev_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 536 { 537 struct tty *tp = dev->si_drv1; 538 int error; 539 540 /* Handle mmap() through the driver. */ 541 542 error = ttydev_enter(tp); 543 if (error) 544 return (-1); 545 error = ttydevsw_mmap(tp, offset, paddr, nprot); 546 tty_unlock(tp); 547 548 return (error); 549 } 550 551 /* 552 * kqueue support. 553 */ 554 555 static void 556 tty_kqops_read_detach(struct knote *kn) 557 { 558 struct tty *tp = kn->kn_hook; 559 560 knlist_remove(&tp->t_inpoll.si_note, kn, 0); 561 } 562 563 static int 564 tty_kqops_read_event(struct knote *kn, long hint) 565 { 566 struct tty *tp = kn->kn_hook; 567 568 tty_lock_assert(tp, MA_OWNED); 569 570 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) { 571 kn->kn_flags |= EV_EOF; 572 return (1); 573 } else { 574 kn->kn_data = ttydisc_read_poll(tp); 575 return (kn->kn_data > 0); 576 } 577 } 578 579 static void 580 tty_kqops_write_detach(struct knote *kn) 581 { 582 struct tty *tp = kn->kn_hook; 583 584 knlist_remove(&tp->t_outpoll.si_note, kn, 0); 585 } 586 587 static int 588 tty_kqops_write_event(struct knote *kn, long hint) 589 { 590 struct tty *tp = kn->kn_hook; 591 592 tty_lock_assert(tp, MA_OWNED); 593 594 if (tty_gone(tp)) { 595 kn->kn_flags |= EV_EOF; 596 return (1); 597 } else { 598 kn->kn_data = ttydisc_write_poll(tp); 599 return (kn->kn_data > 0); 600 } 601 } 602 603 static struct filterops tty_kqops_read = 604 { 1, NULL, tty_kqops_read_detach, tty_kqops_read_event }; 605 static struct filterops tty_kqops_write = 606 { 1, NULL, tty_kqops_write_detach, tty_kqops_write_event }; 607 608 static int 609 ttydev_kqfilter(struct cdev *dev, struct knote *kn) 610 { 611 struct tty *tp = dev->si_drv1; 612 int error; 613 614 error = ttydev_enter(tp); 615 if (error) 616 return (error); 617 618 switch (kn->kn_filter) { 619 case EVFILT_READ: 620 kn->kn_hook = tp; 621 kn->kn_fop = &tty_kqops_read; 622 knlist_add(&tp->t_inpoll.si_note, kn, 1); 623 break; 624 case EVFILT_WRITE: 625 kn->kn_hook = tp; 626 kn->kn_fop = &tty_kqops_write; 627 knlist_add(&tp->t_outpoll.si_note, kn, 1); 628 break; 629 default: 630 error = EINVAL; 631 break; 632 } 633 634 tty_unlock(tp); 635 return (error); 636 } 637 638 static struct cdevsw ttydev_cdevsw = { 639 .d_version = D_VERSION, 640 .d_open = ttydev_open, 641 .d_close = ttydev_close, 642 .d_read = ttydev_read, 643 .d_write = ttydev_write, 644 .d_ioctl = ttydev_ioctl, 645 .d_kqfilter = ttydev_kqfilter, 646 .d_poll = ttydev_poll, 647 .d_mmap = ttydev_mmap, 648 .d_name = "ttydev", 649 .d_flags = D_TTY, 650 }; 651 652 /* 653 * Init/lock-state devices 654 */ 655 656 static int 657 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 658 { 659 struct tty *tp = dev->si_drv1; 660 int error = 0; 661 662 tty_lock(tp); 663 if (tty_gone(tp)) 664 error = ENODEV; 665 tty_unlock(tp); 666 667 return (error); 668 } 669 670 static int 671 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td) 672 { 673 return (0); 674 } 675 676 static int 677 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag) 678 { 679 return (ENODEV); 680 } 681 682 static int 683 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 684 struct thread *td) 685 { 686 struct tty *tp = dev->si_drv1; 687 int error = 0; 688 689 tty_lock(tp); 690 if (tty_gone(tp)) { 691 error = ENODEV; 692 goto done; 693 } 694 695 switch (cmd) { 696 case TIOCGETA: 697 /* Obtain terminal flags through tcgetattr(). */ 698 bcopy(dev->si_drv2, data, sizeof(struct termios)); 699 break; 700 case TIOCSETA: 701 /* Set terminal flags through tcsetattr(). */ 702 error = priv_check(td, PRIV_TTY_SETA); 703 if (error) 704 break; 705 bcopy(data, dev->si_drv2, sizeof(struct termios)); 706 break; 707 case TIOCGETD: 708 *(int *)data = TTYDISC; 709 break; 710 case TIOCGWINSZ: 711 bzero(data, sizeof(struct winsize)); 712 break; 713 default: 714 error = ENOTTY; 715 } 716 717 done: tty_unlock(tp); 718 return (error); 719 } 720 721 static struct cdevsw ttyil_cdevsw = { 722 .d_version = D_VERSION, 723 .d_open = ttyil_open, 724 .d_close = ttyil_close, 725 .d_read = ttyil_rdwr, 726 .d_write = ttyil_rdwr, 727 .d_ioctl = ttyil_ioctl, 728 .d_name = "ttyil", 729 .d_flags = D_TTY, 730 }; 731 732 static void 733 tty_init_termios(struct tty *tp) 734 { 735 struct termios *t = &tp->t_termios_init_in; 736 737 t->c_cflag = TTYDEF_CFLAG; 738 t->c_iflag = TTYDEF_IFLAG; 739 t->c_lflag = TTYDEF_LFLAG; 740 t->c_oflag = TTYDEF_OFLAG; 741 t->c_ispeed = TTYDEF_SPEED; 742 t->c_ospeed = TTYDEF_SPEED; 743 bcopy(ttydefchars, &t->c_cc, sizeof ttydefchars); 744 745 tp->t_termios_init_out = *t; 746 } 747 748 void 749 tty_init_console(struct tty *tp, speed_t s) 750 { 751 struct termios *ti = &tp->t_termios_init_in; 752 struct termios *to = &tp->t_termios_init_out; 753 754 if (s != 0) { 755 ti->c_ispeed = ti->c_ospeed = s; 756 to->c_ispeed = to->c_ospeed = s; 757 } 758 759 ti->c_cflag |= CLOCAL; 760 to->c_cflag |= CLOCAL; 761 } 762 763 /* 764 * Standard device routine implementations, mostly meant for 765 * pseudo-terminal device drivers. When a driver creates a new terminal 766 * device class, missing routines are patched. 767 */ 768 769 static int 770 ttydevsw_defopen(struct tty *tp) 771 { 772 773 return (0); 774 } 775 776 static void 777 ttydevsw_defclose(struct tty *tp) 778 { 779 } 780 781 static void 782 ttydevsw_defoutwakeup(struct tty *tp) 783 { 784 785 panic("Terminal device has output, while not implemented"); 786 } 787 788 static void 789 ttydevsw_definwakeup(struct tty *tp) 790 { 791 } 792 793 static int 794 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 795 { 796 797 return (ENOIOCTL); 798 } 799 800 static int 801 ttydevsw_defparam(struct tty *tp, struct termios *t) 802 { 803 804 /* Use a fake baud rate, we're not a real device. */ 805 t->c_ispeed = t->c_ospeed = TTYDEF_SPEED; 806 807 return (0); 808 } 809 810 static int 811 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff) 812 { 813 814 /* Simulate a carrier to make the TTY layer happy. */ 815 return (SER_DCD); 816 } 817 818 static int 819 ttydevsw_defmmap(struct tty *tp, vm_offset_t offset, vm_paddr_t *paddr, 820 int nprot) 821 { 822 823 return (-1); 824 } 825 826 static void 827 ttydevsw_defpktnotify(struct tty *tp, char event) 828 { 829 } 830 831 static void 832 ttydevsw_deffree(void *softc) 833 { 834 835 panic("Terminal device freed without a free-handler"); 836 } 837 838 /* 839 * TTY allocation and deallocation. TTY devices can be deallocated when 840 * the driver doesn't use it anymore, when the TTY isn't a session's 841 * controlling TTY and when the device node isn't opened through devfs. 842 */ 843 844 struct tty * 845 tty_alloc(struct ttydevsw *tsw, void *sc, struct mtx *mutex) 846 { 847 struct tty *tp; 848 849 /* Make sure the driver defines all routines. */ 850 #define PATCH_FUNC(x) do { \ 851 if (tsw->tsw_ ## x == NULL) \ 852 tsw->tsw_ ## x = ttydevsw_def ## x; \ 853 } while (0) 854 PATCH_FUNC(open); 855 PATCH_FUNC(close); 856 PATCH_FUNC(outwakeup); 857 PATCH_FUNC(inwakeup); 858 PATCH_FUNC(ioctl); 859 PATCH_FUNC(param); 860 PATCH_FUNC(modem); 861 PATCH_FUNC(mmap); 862 PATCH_FUNC(pktnotify); 863 PATCH_FUNC(free); 864 #undef PATCH_FUNC 865 866 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO); 867 tp->t_devsw = tsw; 868 tp->t_devswsoftc = sc; 869 tp->t_flags = tsw->tsw_flags; 870 871 tty_init_termios(tp); 872 873 cv_init(&tp->t_inwait, "tty input"); 874 cv_init(&tp->t_outwait, "tty output"); 875 cv_init(&tp->t_bgwait, "tty background"); 876 cv_init(&tp->t_dcdwait, "tty dcd"); 877 878 ttyinq_init(&tp->t_inq); 879 ttyoutq_init(&tp->t_outq); 880 881 /* Allow drivers to use a custom mutex to lock the TTY. */ 882 if (mutex != NULL) { 883 tp->t_mtx = mutex; 884 } else { 885 tp->t_mtx = &tp->t_mtxobj; 886 mtx_init(&tp->t_mtxobj, "tty lock", NULL, MTX_DEF); 887 } 888 889 knlist_init(&tp->t_inpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 890 knlist_init(&tp->t_outpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 891 892 sx_xlock(&tty_list_sx); 893 TAILQ_INSERT_TAIL(&tty_list, tp, t_list); 894 tty_list_count++; 895 sx_xunlock(&tty_list_sx); 896 897 return (tp); 898 } 899 900 static void 901 tty_dealloc(void *arg) 902 { 903 struct tty *tp = arg; 904 905 sx_xlock(&tty_list_sx); 906 TAILQ_REMOVE(&tty_list, tp, t_list); 907 tty_list_count--; 908 sx_xunlock(&tty_list_sx); 909 910 /* Make sure we haven't leaked buffers. */ 911 MPASS(ttyinq_getsize(&tp->t_inq) == 0); 912 MPASS(ttyoutq_getsize(&tp->t_outq) == 0); 913 914 knlist_destroy(&tp->t_inpoll.si_note); 915 knlist_destroy(&tp->t_outpoll.si_note); 916 917 cv_destroy(&tp->t_inwait); 918 cv_destroy(&tp->t_outwait); 919 cv_destroy(&tp->t_bgwait); 920 cv_destroy(&tp->t_dcdwait); 921 922 if (tp->t_mtx == &tp->t_mtxobj) 923 mtx_destroy(&tp->t_mtxobj); 924 ttydevsw_free(tp); 925 free(tp, M_TTY); 926 } 927 928 static void 929 tty_rel_free(struct tty *tp) 930 { 931 struct cdev *dev; 932 933 tty_lock_assert(tp, MA_OWNED); 934 935 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE) 936 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) { 937 /* TTY is still in use. */ 938 tty_unlock(tp); 939 return; 940 } 941 942 /* TTY can be deallocated. */ 943 dev = tp->t_dev; 944 tp->t_dev = NULL; 945 tty_unlock(tp); 946 947 destroy_dev_sched_cb(dev, tty_dealloc, tp); 948 } 949 950 void 951 tty_rel_pgrp(struct tty *tp, struct pgrp *pg) 952 { 953 MPASS(tp->t_sessioncnt > 0); 954 tty_lock_assert(tp, MA_OWNED); 955 956 if (tp->t_pgrp == pg) 957 tp->t_pgrp = NULL; 958 959 tty_unlock(tp); 960 } 961 962 void 963 tty_rel_sess(struct tty *tp, struct session *sess) 964 { 965 MPASS(tp->t_sessioncnt > 0); 966 967 /* Current session has left. */ 968 if (tp->t_session == sess) { 969 tp->t_session = NULL; 970 MPASS(tp->t_pgrp == NULL); 971 } 972 tp->t_sessioncnt--; 973 tty_rel_free(tp); 974 } 975 976 void 977 tty_rel_gone(struct tty *tp) 978 { 979 MPASS(!tty_gone(tp)); 980 981 /* Simulate carrier removal. */ 982 ttydisc_modem(tp, 0); 983 984 /* Wake up all blocked threads. */ 985 tty_wakeup(tp, FREAD|FWRITE); 986 cv_broadcast(&tp->t_bgwait); 987 cv_broadcast(&tp->t_dcdwait); 988 989 tp->t_flags |= TF_GONE; 990 tty_rel_free(tp); 991 } 992 993 /* 994 * Exposing information about current TTY's through sysctl 995 */ 996 997 static void 998 tty_to_xtty(struct tty *tp, struct xtty *xt) 999 { 1000 tty_lock_assert(tp, MA_OWNED); 1001 1002 xt->xt_size = sizeof(struct xtty); 1003 xt->xt_insize = ttyinq_getsize(&tp->t_inq); 1004 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq); 1005 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq); 1006 xt->xt_inlow = tp->t_inlow; 1007 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq); 1008 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq); 1009 xt->xt_outlow = tp->t_outlow; 1010 xt->xt_column = tp->t_column; 1011 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 1012 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0; 1013 xt->xt_flags = tp->t_flags; 1014 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV; 1015 } 1016 1017 static int 1018 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS) 1019 { 1020 unsigned long lsize; 1021 struct xtty *xtlist, *xt; 1022 struct tty *tp; 1023 int error; 1024 1025 sx_slock(&tty_list_sx); 1026 lsize = tty_list_count * sizeof(struct xtty); 1027 if (lsize == 0) { 1028 sx_sunlock(&tty_list_sx); 1029 return (0); 1030 } 1031 1032 xtlist = xt = malloc(lsize, M_TEMP, M_WAITOK); 1033 1034 TAILQ_FOREACH(tp, &tty_list, t_list) { 1035 tty_lock(tp); 1036 tty_to_xtty(tp, xt); 1037 tty_unlock(tp); 1038 xt++; 1039 } 1040 sx_sunlock(&tty_list_sx); 1041 1042 error = SYSCTL_OUT(req, xtlist, lsize); 1043 free(xtlist, M_TEMP); 1044 return (error); 1045 } 1046 1047 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD, 1048 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs"); 1049 1050 /* 1051 * Device node creation. Device has been set up, now we can expose it to 1052 * the user. 1053 */ 1054 1055 void 1056 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...) 1057 { 1058 va_list ap; 1059 struct cdev *dev; 1060 const char *prefix = "tty"; 1061 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */ 1062 uid_t uid; 1063 gid_t gid; 1064 mode_t mode; 1065 1066 /* Remove "tty" prefix from devices like PTY's. */ 1067 if (tp->t_flags & TF_NOPREFIX) 1068 prefix = ""; 1069 1070 va_start(ap, fmt); 1071 vsnrprintf(name, sizeof name, 32, fmt, ap); 1072 va_end(ap); 1073 1074 if (cred == NULL) { 1075 /* System device. */ 1076 uid = UID_ROOT; 1077 gid = GID_WHEEL; 1078 mode = S_IRUSR|S_IWUSR; 1079 } else { 1080 /* User device. */ 1081 uid = cred->cr_ruid; 1082 gid = GID_TTY; 1083 mode = S_IRUSR|S_IWUSR|S_IWGRP; 1084 } 1085 1086 /* Master call-in device. */ 1087 dev = make_dev_cred(&ttydev_cdevsw, 0, cred, 1088 uid, gid, mode, "%s%s", prefix, name); 1089 dev->si_drv1 = tp; 1090 tp->t_dev = dev; 1091 1092 /* Slave call-in devices. */ 1093 if (tp->t_flags & TF_INITLOCK) { 1094 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1095 uid, gid, mode, "%s%s.init", prefix, name); 1096 dev_depends(tp->t_dev, dev); 1097 dev->si_drv1 = tp; 1098 dev->si_drv2 = &tp->t_termios_init_in; 1099 1100 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1101 uid, gid, mode, "%s%s.lock", prefix, name); 1102 dev_depends(tp->t_dev, dev); 1103 dev->si_drv1 = tp; 1104 dev->si_drv2 = &tp->t_termios_lock_in; 1105 } 1106 1107 /* Call-out devices. */ 1108 if (tp->t_flags & TF_CALLOUT) { 1109 dev = make_dev_cred(&ttydev_cdevsw, 0, cred, 1110 UID_UUCP, GID_DIALER, 0660, "cua%s", name); 1111 dev_depends(tp->t_dev, dev); 1112 dev->si_drv1 = tp; 1113 1114 /* Slave call-out devices. */ 1115 if (tp->t_flags & TF_INITLOCK) { 1116 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1117 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name); 1118 dev_depends(tp->t_dev, dev); 1119 dev->si_drv1 = tp; 1120 dev->si_drv2 = &tp->t_termios_init_out; 1121 1122 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1123 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name); 1124 dev_depends(tp->t_dev, dev); 1125 dev->si_drv1 = tp; 1126 dev->si_drv2 = &tp->t_termios_lock_out; 1127 } 1128 } 1129 } 1130 1131 /* 1132 * Signalling processes. 1133 */ 1134 1135 void 1136 tty_signal_sessleader(struct tty *tp, int sig) 1137 { 1138 struct proc *p; 1139 1140 tty_lock_assert(tp, MA_OWNED); 1141 MPASS(sig >= 1 && sig < NSIG); 1142 1143 /* Make signals start output again. */ 1144 tp->t_flags &= ~TF_STOPPED; 1145 1146 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) { 1147 p = tp->t_session->s_leader; 1148 PROC_LOCK(p); 1149 psignal(p, sig); 1150 PROC_UNLOCK(p); 1151 } 1152 } 1153 1154 void 1155 tty_signal_pgrp(struct tty *tp, int sig) 1156 { 1157 tty_lock_assert(tp, MA_OWNED); 1158 MPASS(sig >= 1 && sig < NSIG); 1159 1160 /* Make signals start output again. */ 1161 tp->t_flags &= ~TF_STOPPED; 1162 1163 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO)) 1164 tty_info(tp); 1165 if (tp->t_pgrp != NULL) { 1166 PGRP_LOCK(tp->t_pgrp); 1167 pgsignal(tp->t_pgrp, sig, 1); 1168 PGRP_UNLOCK(tp->t_pgrp); 1169 } 1170 } 1171 1172 void 1173 tty_wakeup(struct tty *tp, int flags) 1174 { 1175 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL) 1176 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL)); 1177 1178 if (flags & FWRITE) { 1179 cv_broadcast(&tp->t_outwait); 1180 selwakeup(&tp->t_outpoll); 1181 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0); 1182 } 1183 if (flags & FREAD) { 1184 cv_broadcast(&tp->t_inwait); 1185 selwakeup(&tp->t_inpoll); 1186 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0); 1187 } 1188 } 1189 1190 int 1191 tty_wait(struct tty *tp, struct cv *cv) 1192 { 1193 int error; 1194 int revokecnt = tp->t_revokecnt; 1195 1196 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED); 1197 MPASS(!tty_gone(tp)); 1198 1199 error = cv_wait_sig(cv, tp->t_mtx); 1200 1201 /* Restart the system call when we may have been revoked. */ 1202 if (tp->t_revokecnt != revokecnt) 1203 return (ERESTART); 1204 1205 /* Bail out when the device slipped away. */ 1206 if (tty_gone(tp)) 1207 return (ENXIO); 1208 1209 return (error); 1210 } 1211 1212 int 1213 tty_timedwait(struct tty *tp, struct cv *cv, int hz) 1214 { 1215 int error; 1216 int revokecnt = tp->t_revokecnt; 1217 1218 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED); 1219 MPASS(!tty_gone(tp)); 1220 1221 error = cv_timedwait_sig(cv, tp->t_mtx, hz); 1222 1223 /* Restart the system call when we may have been revoked. */ 1224 if (tp->t_revokecnt != revokecnt) 1225 return (ERESTART); 1226 1227 /* Bail out when the device slipped away. */ 1228 if (tty_gone(tp)) 1229 return (ENXIO); 1230 1231 return (error); 1232 } 1233 1234 void 1235 tty_flush(struct tty *tp, int flags) 1236 { 1237 if (flags & FWRITE) { 1238 tp->t_flags &= ~TF_HIWAT_OUT; 1239 ttyoutq_flush(&tp->t_outq); 1240 tty_wakeup(tp, FWRITE); 1241 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE); 1242 } 1243 if (flags & FREAD) { 1244 tty_hiwat_in_unblock(tp); 1245 ttyinq_flush(&tp->t_inq); 1246 ttydevsw_inwakeup(tp); 1247 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD); 1248 } 1249 } 1250 1251 static int 1252 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td) 1253 { 1254 int error; 1255 1256 switch (cmd) { 1257 /* 1258 * Modem commands. 1259 * The SER_* and TIOCM_* flags are the same, but one bit 1260 * shifted. I don't know why. 1261 */ 1262 case TIOCSDTR: 1263 ttydevsw_modem(tp, SER_DTR, 0); 1264 return (0); 1265 case TIOCCDTR: 1266 ttydevsw_modem(tp, 0, SER_DTR); 1267 return (0); 1268 case TIOCMSET: { 1269 int bits = *(int *)data; 1270 ttydevsw_modem(tp, 1271 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 1272 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1); 1273 return (0); 1274 } 1275 case TIOCMBIS: { 1276 int bits = *(int *)data; 1277 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0); 1278 return (0); 1279 } 1280 case TIOCMBIC: { 1281 int bits = *(int *)data; 1282 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1); 1283 return (0); 1284 } 1285 case TIOCMGET: 1286 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1); 1287 return (0); 1288 1289 case FIOASYNC: 1290 if (*(int *)data) 1291 tp->t_flags |= TF_ASYNC; 1292 else 1293 tp->t_flags &= ~TF_ASYNC; 1294 return (0); 1295 case FIONBIO: 1296 /* This device supports non-blocking operation. */ 1297 return (0); 1298 case FIONREAD: 1299 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq); 1300 return (0); 1301 case FIOSETOWN: 1302 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc)) 1303 /* Not allowed to set ownership. */ 1304 return (ENOTTY); 1305 1306 /* Temporarily unlock the TTY to set ownership. */ 1307 tty_unlock(tp); 1308 error = fsetown(*(int *)data, &tp->t_sigio); 1309 tty_lock(tp); 1310 return (error); 1311 case FIOGETOWN: 1312 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc)) 1313 /* Not allowed to set ownership. */ 1314 return (ENOTTY); 1315 1316 /* Get ownership. */ 1317 *(int *)data = fgetown(&tp->t_sigio); 1318 return (0); 1319 case TIOCGETA: 1320 /* Obtain terminal flags through tcgetattr(). */ 1321 bcopy(&tp->t_termios, data, sizeof(struct termios)); 1322 return (0); 1323 case TIOCSETA: 1324 case TIOCSETAW: 1325 case TIOCSETAF: { 1326 struct termios *t = data; 1327 1328 /* 1329 * Who makes up these funny rules? According to POSIX, 1330 * input baud rate is set equal to the output baud rate 1331 * when zero. 1332 */ 1333 if (t->c_ispeed == 0) 1334 t->c_ispeed = t->c_ospeed; 1335 1336 /* Discard any unsupported bits. */ 1337 t->c_iflag &= TTYSUP_IFLAG; 1338 t->c_oflag &= TTYSUP_OFLAG; 1339 t->c_lflag &= TTYSUP_LFLAG; 1340 t->c_cflag &= TTYSUP_CFLAG; 1341 1342 /* Set terminal flags through tcsetattr(). */ 1343 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 1344 error = tty_drain(tp); 1345 if (error) 1346 return (error); 1347 if (cmd == TIOCSETAF) 1348 tty_flush(tp, FREAD); 1349 } 1350 1351 /* 1352 * Only call param() when the flags really change. 1353 */ 1354 if ((t->c_cflag & CIGNORE) == 0 && 1355 (tp->t_termios.c_cflag != t->c_cflag || 1356 tp->t_termios.c_ispeed != t->c_ispeed || 1357 tp->t_termios.c_ospeed != t->c_ospeed)) { 1358 error = ttydevsw_param(tp, t); 1359 if (error) 1360 return (error); 1361 1362 /* XXX: CLOCAL? */ 1363 1364 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE; 1365 tp->t_termios.c_ispeed = t->c_ispeed; 1366 tp->t_termios.c_ospeed = t->c_ospeed; 1367 1368 /* Baud rate has changed - update watermarks. */ 1369 tty_watermarks(tp); 1370 } 1371 1372 /* Copy new non-device driver parameters. */ 1373 tp->t_termios.c_iflag = t->c_iflag; 1374 tp->t_termios.c_oflag = t->c_oflag; 1375 tp->t_termios.c_lflag = t->c_lflag; 1376 bcopy(t->c_cc, &tp->t_termios.c_cc, sizeof(t->c_cc)); 1377 1378 ttydisc_optimize(tp); 1379 1380 if ((t->c_lflag & ICANON) == 0) { 1381 /* 1382 * When in non-canonical mode, wake up all 1383 * readers. Canonicalize any partial input. VMIN 1384 * and VTIME could also be adjusted. 1385 */ 1386 ttyinq_canonicalize(&tp->t_inq); 1387 tty_wakeup(tp, FREAD); 1388 } 1389 1390 /* 1391 * For packet mode: notify the PTY consumer that VSTOP 1392 * and VSTART may have been changed. 1393 */ 1394 if (tp->t_termios.c_iflag & IXON && 1395 tp->t_termios.c_cc[VSTOP] == CTRL('S') && 1396 tp->t_termios.c_cc[VSTART] == CTRL('Q')) 1397 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP); 1398 else 1399 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP); 1400 return (0); 1401 } 1402 case TIOCGETD: 1403 /* For compatibility - we only support TTYDISC. */ 1404 *(int *)data = TTYDISC; 1405 return (0); 1406 case TIOCGPGRP: 1407 if (!tty_is_ctty(tp, td->td_proc)) 1408 return (ENOTTY); 1409 1410 if (tp->t_pgrp != NULL) 1411 *(int *)data = tp->t_pgrp->pg_id; 1412 else 1413 *(int *)data = NO_PID; 1414 return (0); 1415 case TIOCGSID: 1416 if (!tty_is_ctty(tp, td->td_proc)) 1417 return (ENOTTY); 1418 1419 MPASS(tp->t_session); 1420 *(int *)data = tp->t_session->s_sid; 1421 return (0); 1422 case TIOCSCTTY: { 1423 struct proc *p = td->td_proc; 1424 1425 /* XXX: This looks awful. */ 1426 tty_unlock(tp); 1427 sx_xlock(&proctree_lock); 1428 tty_lock(tp); 1429 1430 if (!SESS_LEADER(p)) { 1431 /* Only the session leader may do this. */ 1432 sx_xunlock(&proctree_lock); 1433 return (EPERM); 1434 } 1435 1436 if (tp->t_session != NULL && tp->t_session == p->p_session) { 1437 /* This is already our controlling TTY. */ 1438 sx_xunlock(&proctree_lock); 1439 return (0); 1440 } 1441 1442 if (!SESS_LEADER(p) || p->p_session->s_ttyvp != NULL || 1443 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL)) { 1444 /* 1445 * There is already a relation between a TTY and 1446 * a session, or the caller is not the session 1447 * leader. 1448 * 1449 * Allow the TTY to be stolen when the vnode is 1450 * NULL, but the reference to the TTY is still 1451 * active. 1452 */ 1453 sx_xunlock(&proctree_lock); 1454 return (EPERM); 1455 } 1456 1457 /* Connect the session to the TTY. */ 1458 tp->t_session = p->p_session; 1459 tp->t_session->s_ttyp = tp; 1460 tp->t_sessioncnt++; 1461 sx_xunlock(&proctree_lock); 1462 1463 /* Assign foreground process group. */ 1464 tp->t_pgrp = p->p_pgrp; 1465 PROC_LOCK(p); 1466 p->p_flag |= P_CONTROLT; 1467 PROC_UNLOCK(p); 1468 1469 return (0); 1470 } 1471 case TIOCSPGRP: { 1472 struct pgrp *pg; 1473 1474 /* 1475 * XXX: Temporarily unlock the TTY to locate the process 1476 * group. This code would be lot nicer if we would ever 1477 * decompose proctree_lock. 1478 */ 1479 tty_unlock(tp); 1480 sx_slock(&proctree_lock); 1481 pg = pgfind(*(int *)data); 1482 if (pg != NULL) 1483 PGRP_UNLOCK(pg); 1484 if (pg == NULL || pg->pg_session != td->td_proc->p_session) { 1485 sx_sunlock(&proctree_lock); 1486 tty_lock(tp); 1487 return (EPERM); 1488 } 1489 tty_lock(tp); 1490 1491 /* 1492 * Determine if this TTY is the controlling TTY after 1493 * relocking the TTY. 1494 */ 1495 if (!tty_is_ctty(tp, td->td_proc)) { 1496 sx_sunlock(&proctree_lock); 1497 return (ENOTTY); 1498 } 1499 tp->t_pgrp = pg; 1500 sx_sunlock(&proctree_lock); 1501 1502 /* Wake up the background process groups. */ 1503 cv_broadcast(&tp->t_bgwait); 1504 return (0); 1505 } 1506 case TIOCFLUSH: { 1507 int flags = *(int *)data; 1508 1509 if (flags == 0) 1510 flags = (FREAD|FWRITE); 1511 else 1512 flags &= (FREAD|FWRITE); 1513 tty_flush(tp, flags); 1514 return (0); 1515 } 1516 case TIOCDRAIN: 1517 /* Drain TTY output. */ 1518 return tty_drain(tp); 1519 case TIOCCONS: 1520 /* Set terminal as console TTY. */ 1521 if (*(int *)data) { 1522 error = priv_check(td, PRIV_TTY_CONSOLE); 1523 if (error) 1524 return (error); 1525 1526 /* 1527 * XXX: constty should really need to be locked! 1528 * XXX: allow disconnected constty's to be stolen! 1529 */ 1530 1531 if (constty == tp) 1532 return (0); 1533 if (constty != NULL) 1534 return (EBUSY); 1535 1536 tty_unlock(tp); 1537 constty_set(tp); 1538 tty_lock(tp); 1539 } else if (constty == tp) { 1540 constty_clear(); 1541 } 1542 return (0); 1543 case TIOCGWINSZ: 1544 /* Obtain window size. */ 1545 bcopy(&tp->t_winsize, data, sizeof(struct winsize)); 1546 return (0); 1547 case TIOCSWINSZ: 1548 /* Set window size. */ 1549 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0) 1550 return (0); 1551 bcopy(data, &tp->t_winsize, sizeof(struct winsize)); 1552 tty_signal_pgrp(tp, SIGWINCH); 1553 return (0); 1554 case TIOCEXCL: 1555 tp->t_flags |= TF_EXCLUDE; 1556 return (0); 1557 case TIOCNXCL: 1558 tp->t_flags &= ~TF_EXCLUDE; 1559 return (0); 1560 case TIOCOUTQ: 1561 *(unsigned int *)data = ttyoutq_bytesused(&tp->t_outq); 1562 return (0); 1563 case TIOCSTOP: 1564 tp->t_flags |= TF_STOPPED; 1565 ttydevsw_pktnotify(tp, TIOCPKT_STOP); 1566 return (0); 1567 case TIOCSTART: 1568 tp->t_flags &= ~TF_STOPPED; 1569 ttydevsw_outwakeup(tp); 1570 ttydevsw_pktnotify(tp, TIOCPKT_START); 1571 return (0); 1572 case TIOCSTAT: 1573 tty_info(tp); 1574 return (0); 1575 } 1576 1577 #ifdef COMPAT_43TTY 1578 return tty_ioctl_compat(tp, cmd, data, td); 1579 #else /* !COMPAT_43TTY */ 1580 return (ENOIOCTL); 1581 #endif /* COMPAT_43TTY */ 1582 } 1583 1584 int 1585 tty_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td) 1586 { 1587 int error; 1588 1589 tty_lock_assert(tp, MA_OWNED); 1590 1591 if (tty_gone(tp)) 1592 return (ENXIO); 1593 1594 error = ttydevsw_ioctl(tp, cmd, data, td); 1595 if (error == ENOIOCTL) 1596 error = tty_generic_ioctl(tp, cmd, data, td); 1597 1598 return (error); 1599 } 1600 1601 dev_t 1602 tty_udev(struct tty *tp) 1603 { 1604 if (tp->t_dev) 1605 return dev2udev(tp->t_dev); 1606 else 1607 return NODEV; 1608 } 1609 1610 int 1611 tty_checkoutq(struct tty *tp) 1612 { 1613 1614 /* 256 bytes should be enough to print a log message. */ 1615 return (ttyoutq_bytesleft(&tp->t_outq) >= 256); 1616 } 1617 1618 void 1619 tty_hiwat_in_block(struct tty *tp) 1620 { 1621 1622 if ((tp->t_flags & TF_HIWAT_IN) == 0 && 1623 tp->t_termios.c_iflag & IXOFF && 1624 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) { 1625 /* 1626 * Input flow control. Only enter the high watermark when we 1627 * can successfully store the VSTOP character. 1628 */ 1629 if (ttyoutq_write_nofrag(&tp->t_outq, 1630 &tp->t_termios.c_cc[VSTOP], 1) == 0) 1631 tp->t_flags |= TF_HIWAT_IN; 1632 } else { 1633 /* No input flow control. */ 1634 tp->t_flags |= TF_HIWAT_IN; 1635 } 1636 } 1637 1638 void 1639 tty_hiwat_in_unblock(struct tty *tp) 1640 { 1641 1642 if (tp->t_flags & TF_HIWAT_IN && 1643 tp->t_termios.c_iflag & IXOFF && 1644 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) { 1645 /* 1646 * Input flow control. Only leave the high watermark when we 1647 * can successfully store the VSTART character. 1648 */ 1649 if (ttyoutq_write_nofrag(&tp->t_outq, 1650 &tp->t_termios.c_cc[VSTART], 1) == 0) 1651 tp->t_flags &= ~TF_HIWAT_IN; 1652 } else { 1653 /* No input flow control. */ 1654 tp->t_flags &= ~TF_HIWAT_IN; 1655 } 1656 1657 if (!tty_gone(tp)) 1658 ttydevsw_inwakeup(tp); 1659 } 1660 1661 /* 1662 * TTY hooks interface. 1663 */ 1664 1665 static int 1666 ttyhook_defrint(struct tty *tp, char c, int flags) 1667 { 1668 1669 if (ttyhook_rint_bypass(tp, &c, 1) != 1) 1670 return (-1); 1671 1672 return (0); 1673 } 1674 1675 int 1676 ttyhook_register(struct tty **rtp, struct thread *td, int fd, 1677 struct ttyhook *th, void *softc) 1678 { 1679 struct tty *tp; 1680 struct file *fp; 1681 struct cdev *dev; 1682 struct cdevsw *cdp; 1683 int error; 1684 1685 /* Validate the file descriptor. */ 1686 if (fget(td, fd, &fp) != 0) 1687 return (EINVAL); 1688 1689 /* Make sure the vnode is bound to a character device. */ 1690 error = EINVAL; 1691 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR || 1692 fp->f_vnode->v_rdev == NULL) 1693 goto done1; 1694 dev = fp->f_vnode->v_rdev; 1695 1696 /* Make sure it is a TTY. */ 1697 cdp = dev_refthread(dev); 1698 if (cdp == NULL) 1699 goto done1; 1700 if (cdp != &ttydev_cdevsw) 1701 goto done2; 1702 tp = dev->si_drv1; 1703 1704 /* Try to attach the hook to the TTY. */ 1705 error = EBUSY; 1706 tty_lock(tp); 1707 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0)); 1708 if (tp->t_flags & TF_HOOK) 1709 goto done3; 1710 1711 tp->t_flags |= TF_HOOK; 1712 tp->t_hook = th; 1713 tp->t_hooksoftc = softc; 1714 *rtp = tp; 1715 error = 0; 1716 1717 /* Maybe we can switch into bypass mode now. */ 1718 ttydisc_optimize(tp); 1719 1720 /* Silently convert rint() calls to rint_bypass() when possible. */ 1721 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass)) 1722 th->th_rint = ttyhook_defrint; 1723 1724 done3: tty_unlock(tp); 1725 done2: dev_relthread(dev); 1726 done1: fdrop(fp, td); 1727 return (error); 1728 } 1729 1730 void 1731 ttyhook_unregister(struct tty *tp) 1732 { 1733 1734 tty_lock_assert(tp, MA_OWNED); 1735 MPASS(tp->t_flags & TF_HOOK); 1736 1737 /* Disconnect the hook. */ 1738 tp->t_flags &= ~TF_HOOK; 1739 tp->t_hook = NULL; 1740 1741 /* Maybe we need to leave bypass mode. */ 1742 ttydisc_optimize(tp); 1743 1744 /* Maybe deallocate the TTY as well. */ 1745 tty_rel_free(tp); 1746 } 1747 1748 /* 1749 * /dev/console handling. 1750 */ 1751 1752 static int 1753 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 1754 { 1755 struct tty *tp; 1756 1757 /* System has no console device. */ 1758 if (dev_console_filename == NULL) 1759 return (ENXIO); 1760 1761 /* Look up corresponding TTY by device name. */ 1762 sx_slock(&tty_list_sx); 1763 TAILQ_FOREACH(tp, &tty_list, t_list) { 1764 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) { 1765 dev_console->si_drv1 = tp; 1766 break; 1767 } 1768 } 1769 sx_sunlock(&tty_list_sx); 1770 1771 /* System console has no TTY associated. */ 1772 if (dev_console->si_drv1 == NULL) 1773 return (ENXIO); 1774 1775 return (ttydev_open(dev, oflags, devtype, td)); 1776 } 1777 1778 static int 1779 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag) 1780 { 1781 1782 log_console(uio); 1783 1784 return (ttydev_write(dev, uio, ioflag)); 1785 } 1786 1787 /* 1788 * /dev/console is a little different than normal TTY's. Unlike regular 1789 * TTY device nodes, this device node will not revoke the entire TTY 1790 * upon closure and all data written to it will be logged. 1791 */ 1792 static struct cdevsw ttyconsdev_cdevsw = { 1793 .d_version = D_VERSION, 1794 .d_open = ttyconsdev_open, 1795 .d_read = ttydev_read, 1796 .d_write = ttyconsdev_write, 1797 .d_ioctl = ttydev_ioctl, 1798 .d_kqfilter = ttydev_kqfilter, 1799 .d_poll = ttydev_poll, 1800 .d_mmap = ttydev_mmap, 1801 .d_name = "ttyconsdev", 1802 .d_flags = D_TTY, 1803 }; 1804 1805 static void 1806 ttyconsdev_init(void *unused) 1807 { 1808 1809 dev_console = make_dev(&ttyconsdev_cdevsw, 0, UID_ROOT, GID_WHEEL, 1810 0600, "console"); 1811 } 1812 1813 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL); 1814 1815 void 1816 ttyconsdev_select(const char *name) 1817 { 1818 1819 dev_console_filename = name; 1820 } 1821 1822 /* 1823 * Debugging routines. 1824 */ 1825 1826 #include "opt_ddb.h" 1827 #ifdef DDB 1828 #include <ddb/ddb.h> 1829 #include <ddb/db_sym.h> 1830 1831 static struct { 1832 int flag; 1833 char val; 1834 } ttystates[] = { 1835 #if 0 1836 { TF_NOPREFIX, 'N' }, 1837 #endif 1838 { TF_INITLOCK, 'I' }, 1839 { TF_CALLOUT, 'C' }, 1840 1841 /* Keep these together -> 'Oi' and 'Oo'. */ 1842 { TF_OPENED, 'O' }, 1843 { TF_OPENED_IN, 'i' }, 1844 { TF_OPENED_OUT,'o' }, 1845 1846 { TF_GONE, 'G' }, 1847 { TF_OPENCLOSE, 'B' }, 1848 { TF_ASYNC, 'Y' }, 1849 { TF_LITERAL, 'L' }, 1850 1851 /* Keep these together -> 'Hi' and 'Ho'. */ 1852 { TF_HIWAT, 'H' }, 1853 { TF_HIWAT_IN, 'i' }, 1854 { TF_HIWAT_OUT, 'o' }, 1855 1856 { TF_STOPPED, 'S' }, 1857 { TF_EXCLUDE, 'X' }, 1858 { TF_BYPASS, 'l' }, 1859 { TF_ZOMBIE, 'Z' }, 1860 { TF_HOOK, 's' }, 1861 1862 { 0, '\0' }, 1863 }; 1864 1865 #define TTY_FLAG_BITS \ 1866 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \ 1867 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \ 1868 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK" 1869 1870 #define DB_PRINTSYM(name, addr) \ 1871 db_printf("%s " #name ": ", sep); \ 1872 db_printsym((db_addr_t) addr, DB_STGY_ANY); \ 1873 db_printf("\n"); 1874 1875 static void 1876 _db_show_devsw(const char *sep, const struct ttydevsw *tsw) 1877 { 1878 db_printf("%sdevsw: ", sep); 1879 db_printsym((db_addr_t)tsw, DB_STGY_ANY); 1880 db_printf(" (%p)\n", tsw); 1881 DB_PRINTSYM(open, tsw->tsw_open); 1882 DB_PRINTSYM(close, tsw->tsw_close); 1883 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup); 1884 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup); 1885 DB_PRINTSYM(ioctl, tsw->tsw_ioctl); 1886 DB_PRINTSYM(param, tsw->tsw_param); 1887 DB_PRINTSYM(modem, tsw->tsw_modem); 1888 DB_PRINTSYM(mmap, tsw->tsw_mmap); 1889 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify); 1890 DB_PRINTSYM(free, tsw->tsw_free); 1891 } 1892 static void 1893 _db_show_hooks(const char *sep, const struct ttyhook *th) 1894 { 1895 db_printf("%shook: ", sep); 1896 db_printsym((db_addr_t)th, DB_STGY_ANY); 1897 db_printf(" (%p)\n", th); 1898 if (th == NULL) 1899 return; 1900 DB_PRINTSYM(rint, th->th_rint); 1901 DB_PRINTSYM(rint_bypass, th->th_rint_bypass); 1902 DB_PRINTSYM(rint_done, th->th_rint_done); 1903 DB_PRINTSYM(rint_poll, th->th_rint_poll); 1904 DB_PRINTSYM(getc_inject, th->th_getc_inject); 1905 DB_PRINTSYM(getc_capture, th->th_getc_capture); 1906 DB_PRINTSYM(getc_poll, th->th_getc_poll); 1907 DB_PRINTSYM(close, th->th_close); 1908 } 1909 1910 static void 1911 _db_show_termios(const char *name, const struct termios *t) 1912 { 1913 1914 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x " 1915 "lflag 0x%x ispeed %u ospeed %u\n", name, 1916 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag, 1917 t->c_ispeed, t->c_ospeed); 1918 } 1919 1920 /* DDB command to show TTY statistics. */ 1921 DB_SHOW_COMMAND(tty, db_show_tty) 1922 { 1923 struct tty *tp; 1924 1925 if (!have_addr) { 1926 db_printf("usage: show tty <addr>\n"); 1927 return; 1928 } 1929 tp = (struct tty *)addr; 1930 1931 db_printf("0x%p: %s\n", tp, tty_devname(tp)); 1932 db_printf("\tmtx: %p\n", tp->t_mtx); 1933 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS); 1934 db_printf("\trevokecnt: %u\n", tp->t_revokecnt); 1935 1936 /* Buffering mechanisms. */ 1937 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u " 1938 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin, 1939 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end, 1940 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota); 1941 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n", 1942 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end, 1943 tp->t_outq.to_nblocks, tp->t_outq.to_quota); 1944 db_printf("\tinlow: %zu\n", tp->t_inlow); 1945 db_printf("\toutlow: %zu\n", tp->t_outlow); 1946 _db_show_termios("\ttermios", &tp->t_termios); 1947 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n", 1948 tp->t_winsize.ws_row, tp->t_winsize.ws_col, 1949 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel); 1950 db_printf("\tcolumn: %u\n", tp->t_column); 1951 db_printf("\twritepos: %u\n", tp->t_writepos); 1952 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags); 1953 1954 /* Init/lock-state devices. */ 1955 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in); 1956 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out); 1957 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in); 1958 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out); 1959 1960 /* Hooks */ 1961 _db_show_devsw("\t", tp->t_devsw); 1962 _db_show_hooks("\t", tp->t_hook); 1963 1964 /* Process info. */ 1965 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp, 1966 tp->t_pgrp ? tp->t_pgrp->pg_id : 0, 1967 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0); 1968 db_printf("\tsession: %p", tp->t_session); 1969 if (tp->t_session != NULL) 1970 db_printf(" count %u leader %p tty %p sid %d login %s", 1971 tp->t_session->s_count, tp->t_session->s_leader, 1972 tp->t_session->s_ttyp, tp->t_session->s_sid, 1973 tp->t_session->s_login); 1974 db_printf("\n"); 1975 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt); 1976 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc); 1977 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc); 1978 db_printf("\tdev: %p\n", tp->t_dev); 1979 } 1980 1981 /* DDB command to list TTYs. */ 1982 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys) 1983 { 1984 struct tty *tp; 1985 size_t isiz, osiz; 1986 int i, j; 1987 1988 /* Make the output look like `pstat -t'. */ 1989 db_printf("PTR "); 1990 #if defined(__LP64__) 1991 db_printf(" "); 1992 #endif 1993 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW " 1994 "COL SESS PGID STATE\n"); 1995 1996 TAILQ_FOREACH(tp, &tty_list, t_list) { 1997 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE; 1998 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE; 1999 2000 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ", 2001 tp, 2002 tty_devname(tp), 2003 isiz, 2004 tp->t_inq.ti_linestart - tp->t_inq.ti_begin, 2005 tp->t_inq.ti_end - tp->t_inq.ti_linestart, 2006 isiz - tp->t_inlow, 2007 osiz, 2008 tp->t_outq.to_end - tp->t_outq.to_begin, 2009 osiz - tp->t_outlow, 2010 MIN(tp->t_column, 99999), 2011 tp->t_session ? tp->t_session->s_sid : 0, 2012 tp->t_pgrp ? tp->t_pgrp->pg_id : 0); 2013 2014 /* Flag bits. */ 2015 for (i = j = 0; ttystates[i].flag; i++) 2016 if (tp->t_flags & ttystates[i].flag) { 2017 db_printf("%c", ttystates[i].val); 2018 j++; 2019 } 2020 if (j == 0) 2021 db_printf("-"); 2022 db_printf("\n"); 2023 } 2024 } 2025 #endif /* DDB */ 2026