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