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 /* Disallow access when the TTY belongs to a different prison. */ 223 if (dev->si_cred != NULL && 224 dev->si_cred->cr_prison != td->td_ucred->cr_prison && 225 priv_check(td, PRIV_TTY_PRISON)) { 226 return (EPERM); 227 } 228 229 tty_lock(tp); 230 if (tty_gone(tp)) { 231 /* Device is already gone. */ 232 tty_unlock(tp); 233 return (ENXIO); 234 } 235 236 /* 237 * Block when other processes are currently opening or closing 238 * the TTY. 239 */ 240 while (tp->t_flags & TF_OPENCLOSE) { 241 error = tty_wait(tp, &tp->t_dcdwait); 242 if (error != 0) { 243 tty_unlock(tp); 244 return (error); 245 } 246 } 247 tp->t_flags |= TF_OPENCLOSE; 248 249 /* 250 * Make sure the "tty" and "cua" device cannot be opened at the 251 * same time. 252 */ 253 if (TTY_CALLOUT(tp, dev)) { 254 if (tp->t_flags & TF_OPENED_IN) { 255 error = EBUSY; 256 goto done; 257 } 258 } else { 259 if (tp->t_flags & TF_OPENED_OUT) { 260 error = EBUSY; 261 goto done; 262 } 263 } 264 265 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) { 266 error = EBUSY; 267 goto done; 268 } 269 270 if (!tty_opened(tp)) { 271 /* Set proper termios flags. */ 272 if (TTY_CALLOUT(tp, dev)) { 273 tp->t_termios = tp->t_termios_init_out; 274 } else { 275 tp->t_termios = tp->t_termios_init_in; 276 } 277 ttydevsw_param(tp, &tp->t_termios); 278 279 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0); 280 281 error = ttydevsw_open(tp); 282 if (error != 0) 283 goto done; 284 285 ttydisc_open(tp); 286 tty_watermarks(tp); 287 } 288 289 /* Wait for Carrier Detect. */ 290 if (!TTY_CALLOUT(tp, dev) && (oflags & O_NONBLOCK) == 0 && 291 (tp->t_termios.c_cflag & CLOCAL) == 0) { 292 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) { 293 error = tty_wait(tp, &tp->t_dcdwait); 294 if (error != 0) 295 goto done; 296 } 297 } 298 299 if (dev == dev_console) 300 tp->t_flags |= TF_OPENED_CONS; 301 else if (TTY_CALLOUT(tp, dev)) 302 tp->t_flags |= TF_OPENED_OUT; 303 else 304 tp->t_flags |= TF_OPENED_IN; 305 306 done: tp->t_flags &= ~TF_OPENCLOSE; 307 cv_broadcast(&tp->t_dcdwait); 308 ttydev_leave(tp); 309 310 return (error); 311 } 312 313 static int 314 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 315 { 316 struct tty *tp = dev->si_drv1; 317 318 tty_lock(tp); 319 320 /* 321 * Don't actually close the device if it is being used as the 322 * console. 323 */ 324 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED); 325 if (dev == dev_console) 326 tp->t_flags &= ~TF_OPENED_CONS; 327 else 328 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT); 329 330 if (tp->t_flags & TF_OPENED) { 331 tty_unlock(tp); 332 return (0); 333 } 334 335 /* 336 * This can only be called once. The callin and the callout 337 * devices cannot be opened at the same time. 338 */ 339 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED); 340 341 /* Properly wake up threads that are stuck - revoke(). */ 342 tp->t_revokecnt++; 343 tty_wakeup(tp, FREAD|FWRITE); 344 cv_broadcast(&tp->t_bgwait); 345 346 ttydev_leave(tp); 347 348 return (0); 349 } 350 351 static __inline int 352 tty_is_ctty(struct tty *tp, struct proc *p) 353 { 354 tty_lock_assert(tp, MA_OWNED); 355 356 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT); 357 } 358 359 static int 360 tty_wait_background(struct tty *tp, struct thread *td, int sig) 361 { 362 struct proc *p = td->td_proc; 363 struct pgrp *pg; 364 int error; 365 366 MPASS(sig == SIGTTIN || sig == SIGTTOU); 367 tty_lock_assert(tp, MA_OWNED); 368 369 for (;;) { 370 PROC_LOCK(p); 371 /* 372 * The process should only sleep, when: 373 * - This terminal is the controling terminal 374 * - Its process group is not the foreground process 375 * group 376 * - The parent process isn't waiting for the child to 377 * exit 378 * - the signal to send to the process isn't masked 379 */ 380 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) { 381 /* Allow the action to happen. */ 382 PROC_UNLOCK(p); 383 return (0); 384 } 385 386 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) || 387 SIGISMEMBER(td->td_sigmask, sig)) { 388 /* Only allow them in write()/ioctl(). */ 389 PROC_UNLOCK(p); 390 return (sig == SIGTTOU ? 0 : EIO); 391 } 392 393 pg = p->p_pgrp; 394 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) { 395 /* Don't allow the action to happen. */ 396 PROC_UNLOCK(p); 397 return (EIO); 398 } 399 PROC_UNLOCK(p); 400 401 /* 402 * Send the signal and sleep until we're the new 403 * foreground process group. 404 */ 405 PGRP_LOCK(pg); 406 pgsignal(pg, sig, 1); 407 PGRP_UNLOCK(pg); 408 409 error = tty_wait(tp, &tp->t_bgwait); 410 if (error) 411 return (error); 412 } 413 } 414 415 static int 416 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag) 417 { 418 struct tty *tp = dev->si_drv1; 419 int error; 420 421 error = ttydev_enter(tp); 422 if (error) 423 goto done; 424 425 error = tty_wait_background(tp, curthread, SIGTTIN); 426 if (error) { 427 tty_unlock(tp); 428 goto done; 429 } 430 431 error = ttydisc_read(tp, uio, ioflag); 432 tty_unlock(tp); 433 434 /* 435 * The read() call should not throw an error when the device is 436 * being destroyed. Silently convert it to an EOF. 437 */ 438 done: if (error == ENXIO) 439 error = 0; 440 return (error); 441 } 442 443 static int 444 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag) 445 { 446 struct tty *tp = dev->si_drv1; 447 int error; 448 449 error = ttydev_enter(tp); 450 if (error) 451 return (error); 452 453 if (tp->t_termios.c_lflag & TOSTOP) { 454 error = tty_wait_background(tp, curthread, SIGTTOU); 455 if (error) 456 goto done; 457 } 458 459 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) { 460 /* Allow non-blocking writes to bypass serialization. */ 461 error = ttydisc_write(tp, uio, ioflag); 462 } else { 463 /* Serialize write() calls. */ 464 while (tp->t_flags & TF_BUSY_OUT) { 465 error = tty_wait(tp, &tp->t_bgwait); 466 if (error) 467 goto done; 468 } 469 470 tp->t_flags |= TF_BUSY_OUT; 471 error = ttydisc_write(tp, uio, ioflag); 472 tp->t_flags &= ~TF_BUSY_OUT; 473 cv_broadcast(&tp->t_bgwait); 474 } 475 476 done: tty_unlock(tp); 477 return (error); 478 } 479 480 static int 481 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 482 struct thread *td) 483 { 484 struct tty *tp = dev->si_drv1; 485 int error; 486 487 error = ttydev_enter(tp); 488 if (error) 489 return (error); 490 491 switch (cmd) { 492 case TIOCCBRK: 493 case TIOCCONS: 494 case TIOCDRAIN: 495 case TIOCEXCL: 496 case TIOCFLUSH: 497 case TIOCNXCL: 498 case TIOCSBRK: 499 case TIOCSCTTY: 500 case TIOCSETA: 501 case TIOCSETAF: 502 case TIOCSETAW: 503 case TIOCSPGRP: 504 case TIOCSTART: 505 case TIOCSTAT: 506 case TIOCSTOP: 507 case TIOCSWINSZ: 508 #if 0 509 case TIOCSDRAINWAIT: 510 case TIOCSETD: 511 case TIOCSTI: 512 #endif 513 #ifdef COMPAT_43TTY 514 case TIOCLBIC: 515 case TIOCLBIS: 516 case TIOCLSET: 517 case TIOCSETC: 518 case OTIOCSETD: 519 case TIOCSETN: 520 case TIOCSETP: 521 case TIOCSLTC: 522 #endif /* COMPAT_43TTY */ 523 /* 524 * If the ioctl() causes the TTY to be modified, let it 525 * wait in the background. 526 */ 527 error = tty_wait_background(tp, curthread, SIGTTOU); 528 if (error) 529 goto done; 530 } 531 532 error = tty_ioctl(tp, cmd, data, td); 533 done: tty_unlock(tp); 534 535 return (error); 536 } 537 538 static int 539 ttydev_poll(struct cdev *dev, int events, struct thread *td) 540 { 541 struct tty *tp = dev->si_drv1; 542 int error, revents = 0; 543 544 error = ttydev_enter(tp); 545 if (error) { 546 /* Don't return the error here, but the event mask. */ 547 return (events & 548 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 549 } 550 551 if (events & (POLLIN|POLLRDNORM)) { 552 /* See if we can read something. */ 553 if (ttydisc_read_poll(tp) > 0) 554 revents |= events & (POLLIN|POLLRDNORM); 555 } 556 if (events & (POLLOUT|POLLWRNORM)) { 557 /* See if we can write something. */ 558 if (ttydisc_write_poll(tp) > 0) 559 revents |= events & (POLLOUT|POLLWRNORM); 560 } 561 if (tp->t_flags & TF_ZOMBIE) 562 /* Hangup flag on zombie state. */ 563 revents |= events & POLLHUP; 564 565 if (revents == 0) { 566 if (events & (POLLIN|POLLRDNORM)) 567 selrecord(td, &tp->t_inpoll); 568 if (events & (POLLOUT|POLLWRNORM)) 569 selrecord(td, &tp->t_outpoll); 570 } 571 572 tty_unlock(tp); 573 574 return (revents); 575 } 576 577 static int 578 ttydev_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 579 { 580 struct tty *tp = dev->si_drv1; 581 int error; 582 583 /* Handle mmap() through the driver. */ 584 585 error = ttydev_enter(tp); 586 if (error) 587 return (-1); 588 error = ttydevsw_mmap(tp, offset, paddr, nprot); 589 tty_unlock(tp); 590 591 return (error); 592 } 593 594 /* 595 * kqueue support. 596 */ 597 598 static void 599 tty_kqops_read_detach(struct knote *kn) 600 { 601 struct tty *tp = kn->kn_hook; 602 603 knlist_remove(&tp->t_inpoll.si_note, kn, 0); 604 } 605 606 static int 607 tty_kqops_read_event(struct knote *kn, long hint) 608 { 609 struct tty *tp = kn->kn_hook; 610 611 tty_lock_assert(tp, MA_OWNED); 612 613 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) { 614 kn->kn_flags |= EV_EOF; 615 return (1); 616 } else { 617 kn->kn_data = ttydisc_read_poll(tp); 618 return (kn->kn_data > 0); 619 } 620 } 621 622 static void 623 tty_kqops_write_detach(struct knote *kn) 624 { 625 struct tty *tp = kn->kn_hook; 626 627 knlist_remove(&tp->t_outpoll.si_note, kn, 0); 628 } 629 630 static int 631 tty_kqops_write_event(struct knote *kn, long hint) 632 { 633 struct tty *tp = kn->kn_hook; 634 635 tty_lock_assert(tp, MA_OWNED); 636 637 if (tty_gone(tp)) { 638 kn->kn_flags |= EV_EOF; 639 return (1); 640 } else { 641 kn->kn_data = ttydisc_write_poll(tp); 642 return (kn->kn_data > 0); 643 } 644 } 645 646 static struct filterops tty_kqops_read = 647 { 1, NULL, tty_kqops_read_detach, tty_kqops_read_event }; 648 static struct filterops tty_kqops_write = 649 { 1, NULL, tty_kqops_write_detach, tty_kqops_write_event }; 650 651 static int 652 ttydev_kqfilter(struct cdev *dev, struct knote *kn) 653 { 654 struct tty *tp = dev->si_drv1; 655 int error; 656 657 error = ttydev_enter(tp); 658 if (error) 659 return (error); 660 661 switch (kn->kn_filter) { 662 case EVFILT_READ: 663 kn->kn_hook = tp; 664 kn->kn_fop = &tty_kqops_read; 665 knlist_add(&tp->t_inpoll.si_note, kn, 1); 666 break; 667 case EVFILT_WRITE: 668 kn->kn_hook = tp; 669 kn->kn_fop = &tty_kqops_write; 670 knlist_add(&tp->t_outpoll.si_note, kn, 1); 671 break; 672 default: 673 error = EINVAL; 674 break; 675 } 676 677 tty_unlock(tp); 678 return (error); 679 } 680 681 static struct cdevsw ttydev_cdevsw = { 682 .d_version = D_VERSION, 683 .d_open = ttydev_open, 684 .d_close = ttydev_close, 685 .d_read = ttydev_read, 686 .d_write = ttydev_write, 687 .d_ioctl = ttydev_ioctl, 688 .d_kqfilter = ttydev_kqfilter, 689 .d_poll = ttydev_poll, 690 .d_mmap = ttydev_mmap, 691 .d_name = "ttydev", 692 .d_flags = D_TTY, 693 }; 694 695 /* 696 * Init/lock-state devices 697 */ 698 699 static int 700 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 701 { 702 struct tty *tp = dev->si_drv1; 703 int error = 0; 704 705 tty_lock(tp); 706 if (tty_gone(tp)) 707 error = ENODEV; 708 tty_unlock(tp); 709 710 return (error); 711 } 712 713 static int 714 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td) 715 { 716 return (0); 717 } 718 719 static int 720 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag) 721 { 722 return (ENODEV); 723 } 724 725 static int 726 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 727 struct thread *td) 728 { 729 struct tty *tp = dev->si_drv1; 730 int error = 0; 731 732 tty_lock(tp); 733 if (tty_gone(tp)) { 734 error = ENODEV; 735 goto done; 736 } 737 738 switch (cmd) { 739 case TIOCGETA: 740 /* Obtain terminal flags through tcgetattr(). */ 741 *(struct termios*)data = *(struct termios*)dev->si_drv2; 742 break; 743 case TIOCSETA: 744 /* Set terminal flags through tcsetattr(). */ 745 error = priv_check(td, PRIV_TTY_SETA); 746 if (error) 747 break; 748 *(struct termios*)dev->si_drv2 = *(struct termios*)data; 749 break; 750 case TIOCGETD: 751 *(int *)data = TTYDISC; 752 break; 753 case TIOCGWINSZ: 754 bzero(data, sizeof(struct winsize)); 755 break; 756 default: 757 error = ENOTTY; 758 } 759 760 done: tty_unlock(tp); 761 return (error); 762 } 763 764 static struct cdevsw ttyil_cdevsw = { 765 .d_version = D_VERSION, 766 .d_open = ttyil_open, 767 .d_close = ttyil_close, 768 .d_read = ttyil_rdwr, 769 .d_write = ttyil_rdwr, 770 .d_ioctl = ttyil_ioctl, 771 .d_name = "ttyil", 772 .d_flags = D_TTY, 773 }; 774 775 static void 776 tty_init_termios(struct tty *tp) 777 { 778 struct termios *t = &tp->t_termios_init_in; 779 780 t->c_cflag = TTYDEF_CFLAG; 781 t->c_iflag = TTYDEF_IFLAG; 782 t->c_lflag = TTYDEF_LFLAG; 783 t->c_oflag = TTYDEF_OFLAG; 784 t->c_ispeed = TTYDEF_SPEED; 785 t->c_ospeed = TTYDEF_SPEED; 786 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars); 787 788 tp->t_termios_init_out = *t; 789 } 790 791 void 792 tty_init_console(struct tty *tp, speed_t s) 793 { 794 struct termios *ti = &tp->t_termios_init_in; 795 struct termios *to = &tp->t_termios_init_out; 796 797 if (s != 0) { 798 ti->c_ispeed = ti->c_ospeed = s; 799 to->c_ispeed = to->c_ospeed = s; 800 } 801 802 ti->c_cflag |= CLOCAL; 803 to->c_cflag |= CLOCAL; 804 } 805 806 /* 807 * Standard device routine implementations, mostly meant for 808 * pseudo-terminal device drivers. When a driver creates a new terminal 809 * device class, missing routines are patched. 810 */ 811 812 static int 813 ttydevsw_defopen(struct tty *tp) 814 { 815 816 return (0); 817 } 818 819 static void 820 ttydevsw_defclose(struct tty *tp) 821 { 822 } 823 824 static void 825 ttydevsw_defoutwakeup(struct tty *tp) 826 { 827 828 panic("Terminal device has output, while not implemented"); 829 } 830 831 static void 832 ttydevsw_definwakeup(struct tty *tp) 833 { 834 } 835 836 static int 837 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 838 { 839 840 return (ENOIOCTL); 841 } 842 843 static int 844 ttydevsw_defparam(struct tty *tp, struct termios *t) 845 { 846 847 /* Use a fake baud rate, we're not a real device. */ 848 t->c_ispeed = t->c_ospeed = TTYDEF_SPEED; 849 850 return (0); 851 } 852 853 static int 854 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff) 855 { 856 857 /* Simulate a carrier to make the TTY layer happy. */ 858 return (SER_DCD); 859 } 860 861 static int 862 ttydevsw_defmmap(struct tty *tp, vm_offset_t offset, vm_paddr_t *paddr, 863 int nprot) 864 { 865 866 return (-1); 867 } 868 869 static void 870 ttydevsw_defpktnotify(struct tty *tp, char event) 871 { 872 } 873 874 static void 875 ttydevsw_deffree(void *softc) 876 { 877 878 panic("Terminal device freed without a free-handler"); 879 } 880 881 /* 882 * TTY allocation and deallocation. TTY devices can be deallocated when 883 * the driver doesn't use it anymore, when the TTY isn't a session's 884 * controlling TTY and when the device node isn't opened through devfs. 885 */ 886 887 struct tty * 888 tty_alloc(struct ttydevsw *tsw, void *sc) 889 { 890 891 return (tty_alloc_mutex(tsw, sc, NULL)); 892 } 893 894 struct tty * 895 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex) 896 { 897 struct tty *tp; 898 899 /* Make sure the driver defines all routines. */ 900 #define PATCH_FUNC(x) do { \ 901 if (tsw->tsw_ ## x == NULL) \ 902 tsw->tsw_ ## x = ttydevsw_def ## x; \ 903 } while (0) 904 PATCH_FUNC(open); 905 PATCH_FUNC(close); 906 PATCH_FUNC(outwakeup); 907 PATCH_FUNC(inwakeup); 908 PATCH_FUNC(ioctl); 909 PATCH_FUNC(param); 910 PATCH_FUNC(modem); 911 PATCH_FUNC(mmap); 912 PATCH_FUNC(pktnotify); 913 PATCH_FUNC(free); 914 #undef PATCH_FUNC 915 916 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO); 917 tp->t_devsw = tsw; 918 tp->t_devswsoftc = sc; 919 tp->t_flags = tsw->tsw_flags; 920 921 tty_init_termios(tp); 922 923 cv_init(&tp->t_inwait, "ttyin"); 924 cv_init(&tp->t_outwait, "ttyout"); 925 cv_init(&tp->t_bgwait, "ttybg"); 926 cv_init(&tp->t_dcdwait, "ttydcd"); 927 928 /* Allow drivers to use a custom mutex to lock the TTY. */ 929 if (mutex != NULL) { 930 tp->t_mtx = mutex; 931 } else { 932 tp->t_mtx = &tp->t_mtxobj; 933 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF); 934 } 935 936 knlist_init(&tp->t_inpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 937 knlist_init(&tp->t_outpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 938 939 sx_xlock(&tty_list_sx); 940 TAILQ_INSERT_TAIL(&tty_list, tp, t_list); 941 tty_list_count++; 942 sx_xunlock(&tty_list_sx); 943 944 return (tp); 945 } 946 947 static void 948 tty_dealloc(void *arg) 949 { 950 struct tty *tp = arg; 951 952 sx_xlock(&tty_list_sx); 953 TAILQ_REMOVE(&tty_list, tp, t_list); 954 tty_list_count--; 955 sx_xunlock(&tty_list_sx); 956 957 /* Make sure we haven't leaked buffers. */ 958 MPASS(ttyinq_getsize(&tp->t_inq) == 0); 959 MPASS(ttyoutq_getsize(&tp->t_outq) == 0); 960 961 knlist_destroy(&tp->t_inpoll.si_note); 962 knlist_destroy(&tp->t_outpoll.si_note); 963 964 cv_destroy(&tp->t_inwait); 965 cv_destroy(&tp->t_outwait); 966 cv_destroy(&tp->t_bgwait); 967 cv_destroy(&tp->t_dcdwait); 968 969 if (tp->t_mtx == &tp->t_mtxobj) 970 mtx_destroy(&tp->t_mtxobj); 971 ttydevsw_free(tp); 972 free(tp, M_TTY); 973 } 974 975 static void 976 tty_rel_free(struct tty *tp) 977 { 978 struct cdev *dev; 979 980 tty_lock_assert(tp, MA_OWNED); 981 982 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE) 983 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) { 984 /* TTY is still in use. */ 985 tty_unlock(tp); 986 return; 987 } 988 989 /* TTY can be deallocated. */ 990 dev = tp->t_dev; 991 tp->t_dev = NULL; 992 tty_unlock(tp); 993 994 destroy_dev_sched_cb(dev, tty_dealloc, tp); 995 } 996 997 void 998 tty_rel_pgrp(struct tty *tp, struct pgrp *pg) 999 { 1000 MPASS(tp->t_sessioncnt > 0); 1001 tty_lock_assert(tp, MA_OWNED); 1002 1003 if (tp->t_pgrp == pg) 1004 tp->t_pgrp = NULL; 1005 1006 tty_unlock(tp); 1007 } 1008 1009 void 1010 tty_rel_sess(struct tty *tp, struct session *sess) 1011 { 1012 MPASS(tp->t_sessioncnt > 0); 1013 1014 /* Current session has left. */ 1015 if (tp->t_session == sess) { 1016 tp->t_session = NULL; 1017 MPASS(tp->t_pgrp == NULL); 1018 } 1019 tp->t_sessioncnt--; 1020 tty_rel_free(tp); 1021 } 1022 1023 void 1024 tty_rel_gone(struct tty *tp) 1025 { 1026 MPASS(!tty_gone(tp)); 1027 1028 /* Simulate carrier removal. */ 1029 ttydisc_modem(tp, 0); 1030 1031 /* Wake up all blocked threads. */ 1032 tty_wakeup(tp, FREAD|FWRITE); 1033 cv_broadcast(&tp->t_bgwait); 1034 cv_broadcast(&tp->t_dcdwait); 1035 1036 tp->t_flags |= TF_GONE; 1037 tty_rel_free(tp); 1038 } 1039 1040 /* 1041 * Exposing information about current TTY's through sysctl 1042 */ 1043 1044 static void 1045 tty_to_xtty(struct tty *tp, struct xtty *xt) 1046 { 1047 tty_lock_assert(tp, MA_OWNED); 1048 1049 xt->xt_size = sizeof(struct xtty); 1050 xt->xt_insize = ttyinq_getsize(&tp->t_inq); 1051 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq); 1052 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq); 1053 xt->xt_inlow = tp->t_inlow; 1054 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq); 1055 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq); 1056 xt->xt_outlow = tp->t_outlow; 1057 xt->xt_column = tp->t_column; 1058 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 1059 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0; 1060 xt->xt_flags = tp->t_flags; 1061 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV; 1062 } 1063 1064 static int 1065 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS) 1066 { 1067 unsigned long lsize; 1068 struct xtty *xtlist, *xt; 1069 struct tty *tp; 1070 int error; 1071 1072 sx_slock(&tty_list_sx); 1073 lsize = tty_list_count * sizeof(struct xtty); 1074 if (lsize == 0) { 1075 sx_sunlock(&tty_list_sx); 1076 return (0); 1077 } 1078 1079 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK); 1080 1081 TAILQ_FOREACH(tp, &tty_list, t_list) { 1082 tty_lock(tp); 1083 tty_to_xtty(tp, xt); 1084 tty_unlock(tp); 1085 xt++; 1086 } 1087 sx_sunlock(&tty_list_sx); 1088 1089 error = SYSCTL_OUT(req, xtlist, lsize); 1090 free(xtlist, M_TTY); 1091 return (error); 1092 } 1093 1094 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE, 1095 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs"); 1096 1097 /* 1098 * Device node creation. Device has been set up, now we can expose it to 1099 * the user. 1100 */ 1101 1102 void 1103 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...) 1104 { 1105 va_list ap; 1106 struct cdev *dev; 1107 const char *prefix = "tty"; 1108 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */ 1109 uid_t uid; 1110 gid_t gid; 1111 mode_t mode; 1112 1113 /* Remove "tty" prefix from devices like PTY's. */ 1114 if (tp->t_flags & TF_NOPREFIX) 1115 prefix = ""; 1116 1117 va_start(ap, fmt); 1118 vsnrprintf(name, sizeof name, 32, fmt, ap); 1119 va_end(ap); 1120 1121 if (cred == NULL) { 1122 /* System device. */ 1123 uid = UID_ROOT; 1124 gid = GID_WHEEL; 1125 mode = S_IRUSR|S_IWUSR; 1126 } else { 1127 /* User device. */ 1128 uid = cred->cr_ruid; 1129 gid = GID_TTY; 1130 mode = S_IRUSR|S_IWUSR|S_IWGRP; 1131 } 1132 1133 /* Master call-in device. */ 1134 dev = make_dev_cred(&ttydev_cdevsw, 0, cred, 1135 uid, gid, mode, "%s%s", prefix, name); 1136 dev->si_drv1 = tp; 1137 tp->t_dev = dev; 1138 1139 /* Slave call-in devices. */ 1140 if (tp->t_flags & TF_INITLOCK) { 1141 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1142 uid, gid, mode, "%s%s.init", prefix, name); 1143 dev_depends(tp->t_dev, dev); 1144 dev->si_drv1 = tp; 1145 dev->si_drv2 = &tp->t_termios_init_in; 1146 1147 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1148 uid, gid, mode, "%s%s.lock", prefix, name); 1149 dev_depends(tp->t_dev, dev); 1150 dev->si_drv1 = tp; 1151 dev->si_drv2 = &tp->t_termios_lock_in; 1152 } 1153 1154 /* Call-out devices. */ 1155 if (tp->t_flags & TF_CALLOUT) { 1156 dev = make_dev_cred(&ttydev_cdevsw, 0, cred, 1157 UID_UUCP, GID_DIALER, 0660, "cua%s", name); 1158 dev_depends(tp->t_dev, dev); 1159 dev->si_drv1 = tp; 1160 1161 /* Slave call-out devices. */ 1162 if (tp->t_flags & TF_INITLOCK) { 1163 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1164 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name); 1165 dev_depends(tp->t_dev, dev); 1166 dev->si_drv1 = tp; 1167 dev->si_drv2 = &tp->t_termios_init_out; 1168 1169 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1170 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name); 1171 dev_depends(tp->t_dev, dev); 1172 dev->si_drv1 = tp; 1173 dev->si_drv2 = &tp->t_termios_lock_out; 1174 } 1175 } 1176 } 1177 1178 /* 1179 * Signalling processes. 1180 */ 1181 1182 void 1183 tty_signal_sessleader(struct tty *tp, int sig) 1184 { 1185 struct proc *p; 1186 1187 tty_lock_assert(tp, MA_OWNED); 1188 MPASS(sig >= 1 && sig < NSIG); 1189 1190 /* Make signals start output again. */ 1191 tp->t_flags &= ~TF_STOPPED; 1192 1193 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) { 1194 p = tp->t_session->s_leader; 1195 PROC_LOCK(p); 1196 psignal(p, sig); 1197 PROC_UNLOCK(p); 1198 } 1199 } 1200 1201 void 1202 tty_signal_pgrp(struct tty *tp, int sig) 1203 { 1204 tty_lock_assert(tp, MA_OWNED); 1205 MPASS(sig >= 1 && sig < NSIG); 1206 1207 /* Make signals start output again. */ 1208 tp->t_flags &= ~TF_STOPPED; 1209 1210 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO)) 1211 tty_info(tp); 1212 if (tp->t_pgrp != NULL) { 1213 PGRP_LOCK(tp->t_pgrp); 1214 pgsignal(tp->t_pgrp, sig, 1); 1215 PGRP_UNLOCK(tp->t_pgrp); 1216 } 1217 } 1218 1219 void 1220 tty_wakeup(struct tty *tp, int flags) 1221 { 1222 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL) 1223 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL)); 1224 1225 if (flags & FWRITE) { 1226 cv_broadcast(&tp->t_outwait); 1227 selwakeup(&tp->t_outpoll); 1228 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0); 1229 } 1230 if (flags & FREAD) { 1231 cv_broadcast(&tp->t_inwait); 1232 selwakeup(&tp->t_inpoll); 1233 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0); 1234 } 1235 } 1236 1237 int 1238 tty_wait(struct tty *tp, struct cv *cv) 1239 { 1240 int error; 1241 int revokecnt = tp->t_revokecnt; 1242 1243 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED); 1244 MPASS(!tty_gone(tp)); 1245 1246 error = cv_wait_sig(cv, tp->t_mtx); 1247 1248 /* Restart the system call when we may have been revoked. */ 1249 if (tp->t_revokecnt != revokecnt) 1250 return (ERESTART); 1251 1252 /* Bail out when the device slipped away. */ 1253 if (tty_gone(tp)) 1254 return (ENXIO); 1255 1256 return (error); 1257 } 1258 1259 int 1260 tty_timedwait(struct tty *tp, struct cv *cv, int hz) 1261 { 1262 int error; 1263 int revokecnt = tp->t_revokecnt; 1264 1265 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED); 1266 MPASS(!tty_gone(tp)); 1267 1268 error = cv_timedwait_sig(cv, tp->t_mtx, hz); 1269 1270 /* Restart the system call when we may have been revoked. */ 1271 if (tp->t_revokecnt != revokecnt) 1272 return (ERESTART); 1273 1274 /* Bail out when the device slipped away. */ 1275 if (tty_gone(tp)) 1276 return (ENXIO); 1277 1278 return (error); 1279 } 1280 1281 void 1282 tty_flush(struct tty *tp, int flags) 1283 { 1284 if (flags & FWRITE) { 1285 tp->t_flags &= ~TF_HIWAT_OUT; 1286 ttyoutq_flush(&tp->t_outq); 1287 tty_wakeup(tp, FWRITE); 1288 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE); 1289 } 1290 if (flags & FREAD) { 1291 tty_hiwat_in_unblock(tp); 1292 ttyinq_flush(&tp->t_inq); 1293 ttydevsw_inwakeup(tp); 1294 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD); 1295 } 1296 } 1297 1298 static int 1299 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td) 1300 { 1301 int error; 1302 1303 switch (cmd) { 1304 /* 1305 * Modem commands. 1306 * The SER_* and TIOCM_* flags are the same, but one bit 1307 * shifted. I don't know why. 1308 */ 1309 case TIOCSDTR: 1310 ttydevsw_modem(tp, SER_DTR, 0); 1311 return (0); 1312 case TIOCCDTR: 1313 ttydevsw_modem(tp, 0, SER_DTR); 1314 return (0); 1315 case TIOCMSET: { 1316 int bits = *(int *)data; 1317 ttydevsw_modem(tp, 1318 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 1319 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1); 1320 return (0); 1321 } 1322 case TIOCMBIS: { 1323 int bits = *(int *)data; 1324 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0); 1325 return (0); 1326 } 1327 case TIOCMBIC: { 1328 int bits = *(int *)data; 1329 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1); 1330 return (0); 1331 } 1332 case TIOCMGET: 1333 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1); 1334 return (0); 1335 1336 case FIOASYNC: 1337 if (*(int *)data) 1338 tp->t_flags |= TF_ASYNC; 1339 else 1340 tp->t_flags &= ~TF_ASYNC; 1341 return (0); 1342 case FIONBIO: 1343 /* This device supports non-blocking operation. */ 1344 return (0); 1345 case FIONREAD: 1346 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq); 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_ttyvp != NULL || 1490 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL)) { 1491 /* 1492 * There is already a relation between a TTY and 1493 * a session, or the caller is not the session 1494 * leader. 1495 * 1496 * Allow the TTY to be stolen when the vnode is 1497 * NULL, but the reference to the TTY is still 1498 * active. 1499 */ 1500 sx_xunlock(&proctree_lock); 1501 return (EPERM); 1502 } 1503 1504 /* Connect the session to the TTY. */ 1505 tp->t_session = p->p_session; 1506 tp->t_session->s_ttyp = tp; 1507 tp->t_sessioncnt++; 1508 sx_xunlock(&proctree_lock); 1509 1510 /* Assign foreground process group. */ 1511 tp->t_pgrp = p->p_pgrp; 1512 PROC_LOCK(p); 1513 p->p_flag |= P_CONTROLT; 1514 PROC_UNLOCK(p); 1515 1516 return (0); 1517 } 1518 case TIOCSPGRP: { 1519 struct pgrp *pg; 1520 1521 /* 1522 * XXX: Temporarily unlock the TTY to locate the process 1523 * group. This code would be lot nicer if we would ever 1524 * decompose proctree_lock. 1525 */ 1526 tty_unlock(tp); 1527 sx_slock(&proctree_lock); 1528 pg = pgfind(*(int *)data); 1529 if (pg != NULL) 1530 PGRP_UNLOCK(pg); 1531 if (pg == NULL || pg->pg_session != td->td_proc->p_session) { 1532 sx_sunlock(&proctree_lock); 1533 tty_lock(tp); 1534 return (EPERM); 1535 } 1536 tty_lock(tp); 1537 1538 /* 1539 * Determine if this TTY is the controlling TTY after 1540 * relocking the TTY. 1541 */ 1542 if (!tty_is_ctty(tp, td->td_proc)) { 1543 sx_sunlock(&proctree_lock); 1544 return (ENOTTY); 1545 } 1546 tp->t_pgrp = pg; 1547 sx_sunlock(&proctree_lock); 1548 1549 /* Wake up the background process groups. */ 1550 cv_broadcast(&tp->t_bgwait); 1551 return (0); 1552 } 1553 case TIOCFLUSH: { 1554 int flags = *(int *)data; 1555 1556 if (flags == 0) 1557 flags = (FREAD|FWRITE); 1558 else 1559 flags &= (FREAD|FWRITE); 1560 tty_flush(tp, flags); 1561 return (0); 1562 } 1563 case TIOCDRAIN: 1564 /* Drain TTY output. */ 1565 return tty_drain(tp); 1566 case TIOCCONS: 1567 /* Set terminal as console TTY. */ 1568 if (*(int *)data) { 1569 error = priv_check(td, PRIV_TTY_CONSOLE); 1570 if (error) 1571 return (error); 1572 1573 /* 1574 * XXX: constty should really need to be locked! 1575 * XXX: allow disconnected constty's to be stolen! 1576 */ 1577 1578 if (constty == tp) 1579 return (0); 1580 if (constty != NULL) 1581 return (EBUSY); 1582 1583 tty_unlock(tp); 1584 constty_set(tp); 1585 tty_lock(tp); 1586 } else if (constty == tp) { 1587 constty_clear(); 1588 } 1589 return (0); 1590 case TIOCGWINSZ: 1591 /* Obtain window size. */ 1592 *(struct winsize*)data = tp->t_winsize; 1593 return (0); 1594 case TIOCSWINSZ: 1595 /* Set window size. */ 1596 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0) 1597 return (0); 1598 tp->t_winsize = *(struct winsize*)data; 1599 tty_signal_pgrp(tp, SIGWINCH); 1600 return (0); 1601 case TIOCEXCL: 1602 tp->t_flags |= TF_EXCLUDE; 1603 return (0); 1604 case TIOCNXCL: 1605 tp->t_flags &= ~TF_EXCLUDE; 1606 return (0); 1607 case TIOCOUTQ: 1608 *(unsigned int *)data = ttyoutq_bytesused(&tp->t_outq); 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 /* Make sure the vnode is bound to a character device. */ 1746 error = EINVAL; 1747 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR || 1748 fp->f_vnode->v_rdev == NULL) 1749 goto done1; 1750 dev = fp->f_vnode->v_rdev; 1751 1752 /* Make sure it is a TTY. */ 1753 cdp = dev_refthread(dev); 1754 if (cdp == NULL) 1755 goto done1; 1756 if (cdp != &ttydev_cdevsw) 1757 goto done2; 1758 tp = dev->si_drv1; 1759 1760 /* Try to attach the hook to the TTY. */ 1761 error = EBUSY; 1762 tty_lock(tp); 1763 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0)); 1764 if (tp->t_flags & TF_HOOK) 1765 goto done3; 1766 1767 tp->t_flags |= TF_HOOK; 1768 tp->t_hook = th; 1769 tp->t_hooksoftc = softc; 1770 *rtp = tp; 1771 error = 0; 1772 1773 /* Maybe we can switch into bypass mode now. */ 1774 ttydisc_optimize(tp); 1775 1776 /* Silently convert rint() calls to rint_bypass() when possible. */ 1777 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass)) 1778 th->th_rint = ttyhook_defrint; 1779 1780 done3: tty_unlock(tp); 1781 done2: dev_relthread(dev); 1782 done1: fdrop(fp, curthread); 1783 return (error); 1784 } 1785 1786 void 1787 ttyhook_unregister(struct tty *tp) 1788 { 1789 1790 tty_lock_assert(tp, MA_OWNED); 1791 MPASS(tp->t_flags & TF_HOOK); 1792 1793 /* Disconnect the hook. */ 1794 tp->t_flags &= ~TF_HOOK; 1795 tp->t_hook = NULL; 1796 1797 /* Maybe we need to leave bypass mode. */ 1798 ttydisc_optimize(tp); 1799 1800 /* Maybe deallocate the TTY as well. */ 1801 tty_rel_free(tp); 1802 } 1803 1804 /* 1805 * /dev/console handling. 1806 */ 1807 1808 static int 1809 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 1810 { 1811 struct tty *tp; 1812 1813 /* System has no console device. */ 1814 if (dev_console_filename == NULL) 1815 return (ENXIO); 1816 1817 /* Look up corresponding TTY by device name. */ 1818 sx_slock(&tty_list_sx); 1819 TAILQ_FOREACH(tp, &tty_list, t_list) { 1820 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) { 1821 dev_console->si_drv1 = tp; 1822 break; 1823 } 1824 } 1825 sx_sunlock(&tty_list_sx); 1826 1827 /* System console has no TTY associated. */ 1828 if (dev_console->si_drv1 == NULL) 1829 return (ENXIO); 1830 1831 return (ttydev_open(dev, oflags, devtype, td)); 1832 } 1833 1834 static int 1835 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag) 1836 { 1837 1838 log_console(uio); 1839 1840 return (ttydev_write(dev, uio, ioflag)); 1841 } 1842 1843 /* 1844 * /dev/console is a little different than normal TTY's. When opened, 1845 * it determines which TTY to use. When data gets written to it, it 1846 * will be logged in the kernel message buffer. 1847 */ 1848 static struct cdevsw ttyconsdev_cdevsw = { 1849 .d_version = D_VERSION, 1850 .d_open = ttyconsdev_open, 1851 .d_close = ttydev_close, 1852 .d_read = ttydev_read, 1853 .d_write = ttyconsdev_write, 1854 .d_ioctl = ttydev_ioctl, 1855 .d_kqfilter = ttydev_kqfilter, 1856 .d_poll = ttydev_poll, 1857 .d_mmap = ttydev_mmap, 1858 .d_name = "ttyconsdev", 1859 .d_flags = D_TTY, 1860 }; 1861 1862 static void 1863 ttyconsdev_init(void *unused) 1864 { 1865 1866 dev_console = make_dev(&ttyconsdev_cdevsw, 0, UID_ROOT, GID_WHEEL, 1867 0600, "console"); 1868 } 1869 1870 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL); 1871 1872 void 1873 ttyconsdev_select(const char *name) 1874 { 1875 1876 dev_console_filename = name; 1877 } 1878 1879 /* 1880 * Debugging routines. 1881 */ 1882 1883 #include "opt_ddb.h" 1884 #ifdef DDB 1885 #include <ddb/ddb.h> 1886 #include <ddb/db_sym.h> 1887 1888 static struct { 1889 int flag; 1890 char val; 1891 } ttystates[] = { 1892 #if 0 1893 { TF_NOPREFIX, 'N' }, 1894 #endif 1895 { TF_INITLOCK, 'I' }, 1896 { TF_CALLOUT, 'C' }, 1897 1898 /* Keep these together -> 'Oi' and 'Oo'. */ 1899 { TF_OPENED, 'O' }, 1900 { TF_OPENED_IN, 'i' }, 1901 { TF_OPENED_OUT, 'o' }, 1902 { TF_OPENED_CONS, 'c' }, 1903 1904 { TF_GONE, 'G' }, 1905 { TF_OPENCLOSE, 'B' }, 1906 { TF_ASYNC, 'Y' }, 1907 { TF_LITERAL, 'L' }, 1908 1909 /* Keep these together -> 'Hi' and 'Ho'. */ 1910 { TF_HIWAT, 'H' }, 1911 { TF_HIWAT_IN, 'i' }, 1912 { TF_HIWAT_OUT, 'o' }, 1913 1914 { TF_STOPPED, 'S' }, 1915 { TF_EXCLUDE, 'X' }, 1916 { TF_BYPASS, 'l' }, 1917 { TF_ZOMBIE, 'Z' }, 1918 { TF_HOOK, 's' }, 1919 1920 /* Keep these together -> 'bi' and 'bo'. */ 1921 { TF_BUSY, 'b' }, 1922 { TF_BUSY_IN, 'i' }, 1923 { TF_BUSY_OUT, 'o' }, 1924 1925 { 0, '\0'}, 1926 }; 1927 1928 #define TTY_FLAG_BITS \ 1929 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \ 1930 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \ 1931 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK" 1932 1933 #define DB_PRINTSYM(name, addr) \ 1934 db_printf("%s " #name ": ", sep); \ 1935 db_printsym((db_addr_t) addr, DB_STGY_ANY); \ 1936 db_printf("\n"); 1937 1938 static void 1939 _db_show_devsw(const char *sep, const struct ttydevsw *tsw) 1940 { 1941 db_printf("%sdevsw: ", sep); 1942 db_printsym((db_addr_t)tsw, DB_STGY_ANY); 1943 db_printf(" (%p)\n", tsw); 1944 DB_PRINTSYM(open, tsw->tsw_open); 1945 DB_PRINTSYM(close, tsw->tsw_close); 1946 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup); 1947 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup); 1948 DB_PRINTSYM(ioctl, tsw->tsw_ioctl); 1949 DB_PRINTSYM(param, tsw->tsw_param); 1950 DB_PRINTSYM(modem, tsw->tsw_modem); 1951 DB_PRINTSYM(mmap, tsw->tsw_mmap); 1952 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify); 1953 DB_PRINTSYM(free, tsw->tsw_free); 1954 } 1955 static void 1956 _db_show_hooks(const char *sep, const struct ttyhook *th) 1957 { 1958 db_printf("%shook: ", sep); 1959 db_printsym((db_addr_t)th, DB_STGY_ANY); 1960 db_printf(" (%p)\n", th); 1961 if (th == NULL) 1962 return; 1963 DB_PRINTSYM(rint, th->th_rint); 1964 DB_PRINTSYM(rint_bypass, th->th_rint_bypass); 1965 DB_PRINTSYM(rint_done, th->th_rint_done); 1966 DB_PRINTSYM(rint_poll, th->th_rint_poll); 1967 DB_PRINTSYM(getc_inject, th->th_getc_inject); 1968 DB_PRINTSYM(getc_capture, th->th_getc_capture); 1969 DB_PRINTSYM(getc_poll, th->th_getc_poll); 1970 DB_PRINTSYM(close, th->th_close); 1971 } 1972 1973 static void 1974 _db_show_termios(const char *name, const struct termios *t) 1975 { 1976 1977 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x " 1978 "lflag 0x%x ispeed %u ospeed %u\n", name, 1979 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag, 1980 t->c_ispeed, t->c_ospeed); 1981 } 1982 1983 /* DDB command to show TTY statistics. */ 1984 DB_SHOW_COMMAND(tty, db_show_tty) 1985 { 1986 struct tty *tp; 1987 1988 if (!have_addr) { 1989 db_printf("usage: show tty <addr>\n"); 1990 return; 1991 } 1992 tp = (struct tty *)addr; 1993 1994 db_printf("0x%p: %s\n", tp, tty_devname(tp)); 1995 db_printf("\tmtx: %p\n", tp->t_mtx); 1996 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS); 1997 db_printf("\trevokecnt: %u\n", tp->t_revokecnt); 1998 1999 /* Buffering mechanisms. */ 2000 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u " 2001 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin, 2002 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end, 2003 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota); 2004 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n", 2005 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end, 2006 tp->t_outq.to_nblocks, tp->t_outq.to_quota); 2007 db_printf("\tinlow: %zu\n", tp->t_inlow); 2008 db_printf("\toutlow: %zu\n", tp->t_outlow); 2009 _db_show_termios("\ttermios", &tp->t_termios); 2010 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n", 2011 tp->t_winsize.ws_row, tp->t_winsize.ws_col, 2012 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel); 2013 db_printf("\tcolumn: %u\n", tp->t_column); 2014 db_printf("\twritepos: %u\n", tp->t_writepos); 2015 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags); 2016 2017 /* Init/lock-state devices. */ 2018 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in); 2019 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out); 2020 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in); 2021 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out); 2022 2023 /* Hooks */ 2024 _db_show_devsw("\t", tp->t_devsw); 2025 _db_show_hooks("\t", tp->t_hook); 2026 2027 /* Process info. */ 2028 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp, 2029 tp->t_pgrp ? tp->t_pgrp->pg_id : 0, 2030 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0); 2031 db_printf("\tsession: %p", tp->t_session); 2032 if (tp->t_session != NULL) 2033 db_printf(" count %u leader %p tty %p sid %d login %s", 2034 tp->t_session->s_count, tp->t_session->s_leader, 2035 tp->t_session->s_ttyp, tp->t_session->s_sid, 2036 tp->t_session->s_login); 2037 db_printf("\n"); 2038 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt); 2039 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc); 2040 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc); 2041 db_printf("\tdev: %p\n", tp->t_dev); 2042 } 2043 2044 /* DDB command to list TTYs. */ 2045 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys) 2046 { 2047 struct tty *tp; 2048 size_t isiz, osiz; 2049 int i, j; 2050 2051 /* Make the output look like `pstat -t'. */ 2052 db_printf("PTR "); 2053 #if defined(__LP64__) 2054 db_printf(" "); 2055 #endif 2056 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW " 2057 "COL SESS PGID STATE\n"); 2058 2059 TAILQ_FOREACH(tp, &tty_list, t_list) { 2060 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE; 2061 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE; 2062 2063 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ", 2064 tp, 2065 tty_devname(tp), 2066 isiz, 2067 tp->t_inq.ti_linestart - tp->t_inq.ti_begin, 2068 tp->t_inq.ti_end - tp->t_inq.ti_linestart, 2069 isiz - tp->t_inlow, 2070 osiz, 2071 tp->t_outq.to_end - tp->t_outq.to_begin, 2072 osiz - tp->t_outlow, 2073 MIN(tp->t_column, 99999), 2074 tp->t_session ? tp->t_session->s_sid : 0, 2075 tp->t_pgrp ? tp->t_pgrp->pg_id : 0); 2076 2077 /* Flag bits. */ 2078 for (i = j = 0; ttystates[i].flag; i++) 2079 if (tp->t_flags & ttystates[i].flag) { 2080 db_printf("%c", ttystates[i].val); 2081 j++; 2082 } 2083 if (j == 0) 2084 db_printf("-"); 2085 db_printf("\n"); 2086 } 2087 } 2088 #endif /* DDB */ 2089