tty.c revision 197134
1284345Ssjg/*- 2284345Ssjg * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 3284345Ssjg * All rights reserved. 4284345Ssjg * 5284345Ssjg * Portions of this software were developed under sponsorship from Snow 6297154Sbdrewery * B.V., the Netherlands. 7297154Sbdrewery * 8297154Sbdrewery * Redistribution and use in source and binary forms, with or without 9297154Sbdrewery * modification, are permitted provided that the following conditions 10291563Sbdrewery * are met: 11291563Sbdrewery * 1. Redistributions of source code must retain the above copyright 12291563Sbdrewery * notice, this list of conditions and the following disclaimer. 13284345Ssjg * 2. Redistributions in binary form must reproduce the above copyright 14284345Ssjg * notice, this list of conditions and the following disclaimer in the 15284345Ssjg * documentation and/or other materials provided with the distribution. 16284345Ssjg * 17284345Ssjg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18284345Ssjg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19284345Ssjg * 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: head/sys/kern/tty.c 197134 2009-09-12 20:03:45Z rwatson $"); 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 68static MALLOC_DEFINE(M_TTY, "tty", "tty device"); 69 70static void tty_rel_free(struct tty *tp); 71 72static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list); 73static struct sx tty_list_sx; 74SX_SYSINIT(tty_list, &tty_list_sx, "tty list"); 75static unsigned int tty_list_count = 0; 76 77/* Character device of /dev/console. */ 78static struct cdev *dev_console; 79static 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 102static void 103tty_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 122static int 123tty_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 155static __inline int 156ttydev_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 169static void 170ttydev_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 */ 216static int 217ttydev_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 299done: tp->t_flags &= ~TF_OPENCLOSE; 300 cv_broadcast(&tp->t_dcdwait); 301 ttydev_leave(tp); 302 303 return (error); 304} 305 306static int 307ttydev_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 345static __inline int 346tty_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 353static int 354tty_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 409static int 410ttydev_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 */ 432done: if (error == ENXIO) 433 error = 0; 434 return (error); 435} 436 437static int 438ttydev_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 470done: tty_unlock(tp); 471 return (error); 472} 473 474static int 475ttydev_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); 527done: tty_unlock(tp); 528 529 return (error); 530} 531 532static int 533ttydev_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 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP); 541 542 if (events & (POLLIN|POLLRDNORM)) { 543 /* See if we can read something. */ 544 if (ttydisc_read_poll(tp) > 0) 545 revents |= events & (POLLIN|POLLRDNORM); 546 } 547 548 if (tp->t_flags & TF_ZOMBIE) { 549 /* Hangup flag on zombie state. */ 550 revents |= POLLHUP; 551 } else if (events & (POLLOUT|POLLWRNORM)) { 552 /* See if we can write something. */ 553 if (ttydisc_write_poll(tp) > 0) 554 revents |= events & (POLLOUT|POLLWRNORM); 555 } 556 557 if (revents == 0) { 558 if (events & (POLLIN|POLLRDNORM)) 559 selrecord(td, &tp->t_inpoll); 560 if (events & (POLLOUT|POLLWRNORM)) 561 selrecord(td, &tp->t_outpoll); 562 } 563 564 tty_unlock(tp); 565 566 return (revents); 567} 568 569static int 570ttydev_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 571{ 572 struct tty *tp = dev->si_drv1; 573 int error; 574 575 /* Handle mmap() through the driver. */ 576 577 error = ttydev_enter(tp); 578 if (error) 579 return (-1); 580 error = ttydevsw_mmap(tp, offset, paddr, nprot); 581 tty_unlock(tp); 582 583 return (error); 584} 585 586/* 587 * kqueue support. 588 */ 589 590static void 591tty_kqops_read_detach(struct knote *kn) 592{ 593 struct tty *tp = kn->kn_hook; 594 595 knlist_remove(&tp->t_inpoll.si_note, kn, 0); 596} 597 598static int 599tty_kqops_read_event(struct knote *kn, long hint) 600{ 601 struct tty *tp = kn->kn_hook; 602 603 tty_lock_assert(tp, MA_OWNED); 604 605 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) { 606 kn->kn_flags |= EV_EOF; 607 return (1); 608 } else { 609 kn->kn_data = ttydisc_read_poll(tp); 610 return (kn->kn_data > 0); 611 } 612} 613 614static void 615tty_kqops_write_detach(struct knote *kn) 616{ 617 struct tty *tp = kn->kn_hook; 618 619 knlist_remove(&tp->t_outpoll.si_note, kn, 0); 620} 621 622static int 623tty_kqops_write_event(struct knote *kn, long hint) 624{ 625 struct tty *tp = kn->kn_hook; 626 627 tty_lock_assert(tp, MA_OWNED); 628 629 if (tty_gone(tp)) { 630 kn->kn_flags |= EV_EOF; 631 return (1); 632 } else { 633 kn->kn_data = ttydisc_write_poll(tp); 634 return (kn->kn_data > 0); 635 } 636} 637 638static struct filterops tty_kqops_read = { 639 .f_isfd = 1, 640 .f_detach = tty_kqops_read_detach, 641 .f_event = tty_kqops_read_event, 642}; 643static struct filterops tty_kqops_write = { 644 .f_isfd = 1, 645 .f_detach = tty_kqops_write_detach, 646 .f_event = tty_kqops_write_event, 647}; 648 649static int 650ttydev_kqfilter(struct cdev *dev, struct knote *kn) 651{ 652 struct tty *tp = dev->si_drv1; 653 int error; 654 655 error = ttydev_enter(tp); 656 if (error) 657 return (error); 658 659 switch (kn->kn_filter) { 660 case EVFILT_READ: 661 kn->kn_hook = tp; 662 kn->kn_fop = &tty_kqops_read; 663 knlist_add(&tp->t_inpoll.si_note, kn, 1); 664 break; 665 case EVFILT_WRITE: 666 kn->kn_hook = tp; 667 kn->kn_fop = &tty_kqops_write; 668 knlist_add(&tp->t_outpoll.si_note, kn, 1); 669 break; 670 default: 671 error = EINVAL; 672 break; 673 } 674 675 tty_unlock(tp); 676 return (error); 677} 678 679static struct cdevsw ttydev_cdevsw = { 680 .d_version = D_VERSION, 681 .d_open = ttydev_open, 682 .d_close = ttydev_close, 683 .d_read = ttydev_read, 684 .d_write = ttydev_write, 685 .d_ioctl = ttydev_ioctl, 686 .d_kqfilter = ttydev_kqfilter, 687 .d_poll = ttydev_poll, 688 .d_mmap = ttydev_mmap, 689 .d_name = "ttydev", 690 .d_flags = D_TTY, 691}; 692 693/* 694 * Init/lock-state devices 695 */ 696 697static int 698ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 699{ 700 struct tty *tp = dev->si_drv1; 701 int error = 0; 702 703 tty_lock(tp); 704 if (tty_gone(tp)) 705 error = ENODEV; 706 tty_unlock(tp); 707 708 return (error); 709} 710 711static int 712ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td) 713{ 714 return (0); 715} 716 717static int 718ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag) 719{ 720 return (ENODEV); 721} 722 723static int 724ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 725 struct thread *td) 726{ 727 struct tty *tp = dev->si_drv1; 728 int error = 0; 729 730 tty_lock(tp); 731 if (tty_gone(tp)) { 732 error = ENODEV; 733 goto done; 734 } 735 736 switch (cmd) { 737 case TIOCGETA: 738 /* Obtain terminal flags through tcgetattr(). */ 739 *(struct termios*)data = *(struct termios*)dev->si_drv2; 740 break; 741 case TIOCSETA: 742 /* Set terminal flags through tcsetattr(). */ 743 error = priv_check(td, PRIV_TTY_SETA); 744 if (error) 745 break; 746 *(struct termios*)dev->si_drv2 = *(struct termios*)data; 747 break; 748 case TIOCGETD: 749 *(int *)data = TTYDISC; 750 break; 751 case TIOCGWINSZ: 752 bzero(data, sizeof(struct winsize)); 753 break; 754 default: 755 error = ENOTTY; 756 } 757 758done: tty_unlock(tp); 759 return (error); 760} 761 762static struct cdevsw ttyil_cdevsw = { 763 .d_version = D_VERSION, 764 .d_open = ttyil_open, 765 .d_close = ttyil_close, 766 .d_read = ttyil_rdwr, 767 .d_write = ttyil_rdwr, 768 .d_ioctl = ttyil_ioctl, 769 .d_name = "ttyil", 770 .d_flags = D_TTY, 771}; 772 773static void 774tty_init_termios(struct tty *tp) 775{ 776 struct termios *t = &tp->t_termios_init_in; 777 778 t->c_cflag = TTYDEF_CFLAG; 779 t->c_iflag = TTYDEF_IFLAG; 780 t->c_lflag = TTYDEF_LFLAG; 781 t->c_oflag = TTYDEF_OFLAG; 782 t->c_ispeed = TTYDEF_SPEED; 783 t->c_ospeed = TTYDEF_SPEED; 784 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars); 785 786 tp->t_termios_init_out = *t; 787} 788 789void 790tty_init_console(struct tty *tp, speed_t s) 791{ 792 struct termios *ti = &tp->t_termios_init_in; 793 struct termios *to = &tp->t_termios_init_out; 794 795 if (s != 0) { 796 ti->c_ispeed = ti->c_ospeed = s; 797 to->c_ispeed = to->c_ospeed = s; 798 } 799 800 ti->c_cflag |= CLOCAL; 801 to->c_cflag |= CLOCAL; 802} 803 804/* 805 * Standard device routine implementations, mostly meant for 806 * pseudo-terminal device drivers. When a driver creates a new terminal 807 * device class, missing routines are patched. 808 */ 809 810static int 811ttydevsw_defopen(struct tty *tp) 812{ 813 814 return (0); 815} 816 817static void 818ttydevsw_defclose(struct tty *tp) 819{ 820} 821 822static void 823ttydevsw_defoutwakeup(struct tty *tp) 824{ 825 826 panic("Terminal device has output, while not implemented"); 827} 828 829static void 830ttydevsw_definwakeup(struct tty *tp) 831{ 832} 833 834static int 835ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 836{ 837 838 return (ENOIOCTL); 839} 840 841static int 842ttydevsw_defparam(struct tty *tp, struct termios *t) 843{ 844 845 /* Use a fake baud rate, we're not a real device. */ 846 t->c_ispeed = t->c_ospeed = TTYDEF_SPEED; 847 848 return (0); 849} 850 851static int 852ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff) 853{ 854 855 /* Simulate a carrier to make the TTY layer happy. */ 856 return (SER_DCD); 857} 858 859static int 860ttydevsw_defmmap(struct tty *tp, vm_offset_t offset, vm_paddr_t *paddr, 861 int nprot) 862{ 863 864 return (-1); 865} 866 867static void 868ttydevsw_defpktnotify(struct tty *tp, char event) 869{ 870} 871 872static void 873ttydevsw_deffree(void *softc) 874{ 875 876 panic("Terminal device freed without a free-handler"); 877} 878 879/* 880 * TTY allocation and deallocation. TTY devices can be deallocated when 881 * the driver doesn't use it anymore, when the TTY isn't a session's 882 * controlling TTY and when the device node isn't opened through devfs. 883 */ 884 885struct tty * 886tty_alloc(struct ttydevsw *tsw, void *sc) 887{ 888 889 return (tty_alloc_mutex(tsw, sc, NULL)); 890} 891 892struct tty * 893tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex) 894{ 895 struct tty *tp; 896 897 /* Make sure the driver defines all routines. */ 898#define PATCH_FUNC(x) do { \ 899 if (tsw->tsw_ ## x == NULL) \ 900 tsw->tsw_ ## x = ttydevsw_def ## x; \ 901} while (0) 902 PATCH_FUNC(open); 903 PATCH_FUNC(close); 904 PATCH_FUNC(outwakeup); 905 PATCH_FUNC(inwakeup); 906 PATCH_FUNC(ioctl); 907 PATCH_FUNC(param); 908 PATCH_FUNC(modem); 909 PATCH_FUNC(mmap); 910 PATCH_FUNC(pktnotify); 911 PATCH_FUNC(free); 912#undef PATCH_FUNC 913 914 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO); 915 tp->t_devsw = tsw; 916 tp->t_devswsoftc = sc; 917 tp->t_flags = tsw->tsw_flags; 918 919 tty_init_termios(tp); 920 921 cv_init(&tp->t_inwait, "ttyin"); 922 cv_init(&tp->t_outwait, "ttyout"); 923 cv_init(&tp->t_outserwait, "ttyosr"); 924 cv_init(&tp->t_bgwait, "ttybg"); 925 cv_init(&tp->t_dcdwait, "ttydcd"); 926 927 /* Allow drivers to use a custom mutex to lock the TTY. */ 928 if (mutex != NULL) { 929 tp->t_mtx = mutex; 930 } else { 931 tp->t_mtx = &tp->t_mtxobj; 932 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF); 933 } 934 935 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx); 936 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx); 937 938 sx_xlock(&tty_list_sx); 939 TAILQ_INSERT_TAIL(&tty_list, tp, t_list); 940 tty_list_count++; 941 sx_xunlock(&tty_list_sx); 942 943 return (tp); 944} 945 946static void 947tty_dealloc(void *arg) 948{ 949 struct tty *tp = arg; 950 951 sx_xlock(&tty_list_sx); 952 TAILQ_REMOVE(&tty_list, tp, t_list); 953 tty_list_count--; 954 sx_xunlock(&tty_list_sx); 955 956 /* Make sure we haven't leaked buffers. */ 957 MPASS(ttyinq_getsize(&tp->t_inq) == 0); 958 MPASS(ttyoutq_getsize(&tp->t_outq) == 0); 959 960 knlist_destroy(&tp->t_inpoll.si_note); 961 knlist_destroy(&tp->t_outpoll.si_note); 962 963 cv_destroy(&tp->t_inwait); 964 cv_destroy(&tp->t_outwait); 965 cv_destroy(&tp->t_bgwait); 966 cv_destroy(&tp->t_dcdwait); 967 cv_destroy(&tp->t_outserwait); 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 975static void 976tty_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 997void 998tty_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 1009void 1010tty_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 1023void 1024tty_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 1044static void 1045tty_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 1064static int 1065sysctl_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 1094SYSCTL_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 1102void 1103tty_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 1182void 1183tty_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 1201void 1202tty_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 1219void 1220tty_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 1237int 1238tty_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 1259int 1260tty_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 1281void 1282tty_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 1298static int 1299tty_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 FIONWRITE: 1349 case TIOCOUTQ: 1350 *(int *)data = ttyoutq_bytesused(&tp->t_outq); 1351 return (0); 1352 case FIOSETOWN: 1353 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc)) 1354 /* Not allowed to set ownership. */ 1355 return (ENOTTY); 1356 1357 /* Temporarily unlock the TTY to set ownership. */ 1358 tty_unlock(tp); 1359 error = fsetown(*(int *)data, &tp->t_sigio); 1360 tty_lock(tp); 1361 return (error); 1362 case FIOGETOWN: 1363 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc)) 1364 /* Not allowed to set ownership. */ 1365 return (ENOTTY); 1366 1367 /* Get ownership. */ 1368 *(int *)data = fgetown(&tp->t_sigio); 1369 return (0); 1370 case TIOCGETA: 1371 /* Obtain terminal flags through tcgetattr(). */ 1372 *(struct termios*)data = tp->t_termios; 1373 return (0); 1374 case TIOCSETA: 1375 case TIOCSETAW: 1376 case TIOCSETAF: { 1377 struct termios *t = data; 1378 1379 /* 1380 * Who makes up these funny rules? According to POSIX, 1381 * input baud rate is set equal to the output baud rate 1382 * when zero. 1383 */ 1384 if (t->c_ispeed == 0) 1385 t->c_ispeed = t->c_ospeed; 1386 1387 /* Discard any unsupported bits. */ 1388 t->c_iflag &= TTYSUP_IFLAG; 1389 t->c_oflag &= TTYSUP_OFLAG; 1390 t->c_lflag &= TTYSUP_LFLAG; 1391 t->c_cflag &= TTYSUP_CFLAG; 1392 1393 /* Set terminal flags through tcsetattr(). */ 1394 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 1395 error = tty_drain(tp); 1396 if (error) 1397 return (error); 1398 if (cmd == TIOCSETAF) 1399 tty_flush(tp, FREAD); 1400 } 1401 1402 /* 1403 * Only call param() when the flags really change. 1404 */ 1405 if ((t->c_cflag & CIGNORE) == 0 && 1406 (tp->t_termios.c_cflag != t->c_cflag || 1407 tp->t_termios.c_ispeed != t->c_ispeed || 1408 tp->t_termios.c_ospeed != t->c_ospeed)) { 1409 error = ttydevsw_param(tp, t); 1410 if (error) 1411 return (error); 1412 1413 /* XXX: CLOCAL? */ 1414 1415 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE; 1416 tp->t_termios.c_ispeed = t->c_ispeed; 1417 tp->t_termios.c_ospeed = t->c_ospeed; 1418 1419 /* Baud rate has changed - update watermarks. */ 1420 tty_watermarks(tp); 1421 } 1422 1423 /* Copy new non-device driver parameters. */ 1424 tp->t_termios.c_iflag = t->c_iflag; 1425 tp->t_termios.c_oflag = t->c_oflag; 1426 tp->t_termios.c_lflag = t->c_lflag; 1427 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc); 1428 1429 ttydisc_optimize(tp); 1430 1431 if ((t->c_lflag & ICANON) == 0) { 1432 /* 1433 * When in non-canonical mode, wake up all 1434 * readers. Canonicalize any partial input. VMIN 1435 * and VTIME could also be adjusted. 1436 */ 1437 ttyinq_canonicalize(&tp->t_inq); 1438 tty_wakeup(tp, FREAD); 1439 } 1440 1441 /* 1442 * For packet mode: notify the PTY consumer that VSTOP 1443 * and VSTART may have been changed. 1444 */ 1445 if (tp->t_termios.c_iflag & IXON && 1446 tp->t_termios.c_cc[VSTOP] == CTRL('S') && 1447 tp->t_termios.c_cc[VSTART] == CTRL('Q')) 1448 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP); 1449 else 1450 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP); 1451 return (0); 1452 } 1453 case TIOCGETD: 1454 /* For compatibility - we only support TTYDISC. */ 1455 *(int *)data = TTYDISC; 1456 return (0); 1457 case TIOCGPGRP: 1458 if (!tty_is_ctty(tp, td->td_proc)) 1459 return (ENOTTY); 1460 1461 if (tp->t_pgrp != NULL) 1462 *(int *)data = tp->t_pgrp->pg_id; 1463 else 1464 *(int *)data = NO_PID; 1465 return (0); 1466 case TIOCGSID: 1467 if (!tty_is_ctty(tp, td->td_proc)) 1468 return (ENOTTY); 1469 1470 MPASS(tp->t_session); 1471 *(int *)data = tp->t_session->s_sid; 1472 return (0); 1473 case TIOCSCTTY: { 1474 struct proc *p = td->td_proc; 1475 1476 /* XXX: This looks awful. */ 1477 tty_unlock(tp); 1478 sx_xlock(&proctree_lock); 1479 tty_lock(tp); 1480 1481 if (!SESS_LEADER(p)) { 1482 /* Only the session leader may do this. */ 1483 sx_xunlock(&proctree_lock); 1484 return (EPERM); 1485 } 1486 1487 if (tp->t_session != NULL && tp->t_session == p->p_session) { 1488 /* This is already our controlling TTY. */ 1489 sx_xunlock(&proctree_lock); 1490 return (0); 1491 } 1492 1493 if (p->p_session->s_ttyp != NULL || 1494 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL && 1495 tp->t_session->s_ttyvp->v_type != VBAD)) { 1496 /* 1497 * There is already a relation between a TTY and 1498 * a session, or the caller is not the session 1499 * leader. 1500 * 1501 * Allow the TTY to be stolen when the vnode is 1502 * invalid, but the reference to the TTY is 1503 * still active. This allows immediate reuse of 1504 * TTYs of which the session leader has been 1505 * killed or the TTY revoked. 1506 */ 1507 sx_xunlock(&proctree_lock); 1508 return (EPERM); 1509 } 1510 1511 /* Connect the session to the TTY. */ 1512 tp->t_session = p->p_session; 1513 tp->t_session->s_ttyp = tp; 1514 tp->t_sessioncnt++; 1515 sx_xunlock(&proctree_lock); 1516 1517 /* Assign foreground process group. */ 1518 tp->t_pgrp = p->p_pgrp; 1519 PROC_LOCK(p); 1520 p->p_flag |= P_CONTROLT; 1521 PROC_UNLOCK(p); 1522 1523 return (0); 1524 } 1525 case TIOCSPGRP: { 1526 struct pgrp *pg; 1527 1528 /* 1529 * XXX: Temporarily unlock the TTY to locate the process 1530 * group. This code would be lot nicer if we would ever 1531 * decompose proctree_lock. 1532 */ 1533 tty_unlock(tp); 1534 sx_slock(&proctree_lock); 1535 pg = pgfind(*(int *)data); 1536 if (pg != NULL) 1537 PGRP_UNLOCK(pg); 1538 if (pg == NULL || pg->pg_session != td->td_proc->p_session) { 1539 sx_sunlock(&proctree_lock); 1540 tty_lock(tp); 1541 return (EPERM); 1542 } 1543 tty_lock(tp); 1544 1545 /* 1546 * Determine if this TTY is the controlling TTY after 1547 * relocking the TTY. 1548 */ 1549 if (!tty_is_ctty(tp, td->td_proc)) { 1550 sx_sunlock(&proctree_lock); 1551 return (ENOTTY); 1552 } 1553 tp->t_pgrp = pg; 1554 sx_sunlock(&proctree_lock); 1555 1556 /* Wake up the background process groups. */ 1557 cv_broadcast(&tp->t_bgwait); 1558 return (0); 1559 } 1560 case TIOCFLUSH: { 1561 int flags = *(int *)data; 1562 1563 if (flags == 0) 1564 flags = (FREAD|FWRITE); 1565 else 1566 flags &= (FREAD|FWRITE); 1567 tty_flush(tp, flags); 1568 return (0); 1569 } 1570 case TIOCDRAIN: 1571 /* Drain TTY output. */ 1572 return tty_drain(tp); 1573 case TIOCCONS: 1574 /* Set terminal as console TTY. */ 1575 if (*(int *)data) { 1576 error = priv_check(td, PRIV_TTY_CONSOLE); 1577 if (error) 1578 return (error); 1579 1580 /* 1581 * XXX: constty should really need to be locked! 1582 * XXX: allow disconnected constty's to be stolen! 1583 */ 1584 1585 if (constty == tp) 1586 return (0); 1587 if (constty != NULL) 1588 return (EBUSY); 1589 1590 tty_unlock(tp); 1591 constty_set(tp); 1592 tty_lock(tp); 1593 } else if (constty == tp) { 1594 constty_clear(); 1595 } 1596 return (0); 1597 case TIOCGWINSZ: 1598 /* Obtain window size. */ 1599 *(struct winsize*)data = tp->t_winsize; 1600 return (0); 1601 case TIOCSWINSZ: 1602 /* Set window size. */ 1603 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0) 1604 return (0); 1605 tp->t_winsize = *(struct winsize*)data; 1606 tty_signal_pgrp(tp, SIGWINCH); 1607 return (0); 1608 case TIOCEXCL: 1609 tp->t_flags |= TF_EXCLUDE; 1610 return (0); 1611 case TIOCNXCL: 1612 tp->t_flags &= ~TF_EXCLUDE; 1613 return (0); 1614 case TIOCSTOP: 1615 tp->t_flags |= TF_STOPPED; 1616 ttydevsw_pktnotify(tp, TIOCPKT_STOP); 1617 return (0); 1618 case TIOCSTART: 1619 tp->t_flags &= ~TF_STOPPED; 1620 ttydevsw_outwakeup(tp); 1621 ttydevsw_pktnotify(tp, TIOCPKT_START); 1622 return (0); 1623 case TIOCSTAT: 1624 tty_info(tp); 1625 return (0); 1626 } 1627 1628#ifdef COMPAT_43TTY 1629 return tty_ioctl_compat(tp, cmd, data, td); 1630#else /* !COMPAT_43TTY */ 1631 return (ENOIOCTL); 1632#endif /* COMPAT_43TTY */ 1633} 1634 1635int 1636tty_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td) 1637{ 1638 int error; 1639 1640 tty_lock_assert(tp, MA_OWNED); 1641 1642 if (tty_gone(tp)) 1643 return (ENXIO); 1644 1645 error = ttydevsw_ioctl(tp, cmd, data, td); 1646 if (error == ENOIOCTL) 1647 error = tty_generic_ioctl(tp, cmd, data, td); 1648 1649 return (error); 1650} 1651 1652dev_t 1653tty_udev(struct tty *tp) 1654{ 1655 if (tp->t_dev) 1656 return dev2udev(tp->t_dev); 1657 else 1658 return NODEV; 1659} 1660 1661int 1662tty_checkoutq(struct tty *tp) 1663{ 1664 1665 /* 256 bytes should be enough to print a log message. */ 1666 return (ttyoutq_bytesleft(&tp->t_outq) >= 256); 1667} 1668 1669void 1670tty_hiwat_in_block(struct tty *tp) 1671{ 1672 1673 if ((tp->t_flags & TF_HIWAT_IN) == 0 && 1674 tp->t_termios.c_iflag & IXOFF && 1675 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) { 1676 /* 1677 * Input flow control. Only enter the high watermark when we 1678 * can successfully store the VSTOP character. 1679 */ 1680 if (ttyoutq_write_nofrag(&tp->t_outq, 1681 &tp->t_termios.c_cc[VSTOP], 1) == 0) 1682 tp->t_flags |= TF_HIWAT_IN; 1683 } else { 1684 /* No input flow control. */ 1685 tp->t_flags |= TF_HIWAT_IN; 1686 } 1687} 1688 1689void 1690tty_hiwat_in_unblock(struct tty *tp) 1691{ 1692 1693 if (tp->t_flags & TF_HIWAT_IN && 1694 tp->t_termios.c_iflag & IXOFF && 1695 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) { 1696 /* 1697 * Input flow control. Only leave the high watermark when we 1698 * can successfully store the VSTART character. 1699 */ 1700 if (ttyoutq_write_nofrag(&tp->t_outq, 1701 &tp->t_termios.c_cc[VSTART], 1) == 0) 1702 tp->t_flags &= ~TF_HIWAT_IN; 1703 } else { 1704 /* No input flow control. */ 1705 tp->t_flags &= ~TF_HIWAT_IN; 1706 } 1707 1708 if (!tty_gone(tp)) 1709 ttydevsw_inwakeup(tp); 1710} 1711 1712/* 1713 * TTY hooks interface. 1714 */ 1715 1716static int 1717ttyhook_defrint(struct tty *tp, char c, int flags) 1718{ 1719 1720 if (ttyhook_rint_bypass(tp, &c, 1) != 1) 1721 return (-1); 1722 1723 return (0); 1724} 1725 1726int 1727ttyhook_register(struct tty **rtp, struct proc *p, int fd, 1728 struct ttyhook *th, void *softc) 1729{ 1730 struct tty *tp; 1731 struct file *fp; 1732 struct cdev *dev; 1733 struct cdevsw *cdp; 1734 struct filedesc *fdp; 1735 int error; 1736 1737 /* Validate the file descriptor. */ 1738 if ((fdp = p->p_fd) == NULL) 1739 return (EBADF); 1740 1741 fp = fget_unlocked(fdp, fd); 1742 if (fp == NULL) 1743 return (EBADF); 1744 if (fp->f_ops == &badfileops) { 1745 error = EBADF; 1746 goto done1; 1747 } 1748 1749 /* 1750 * Make sure the vnode is bound to a character device. 1751 * Unlocked check for the vnode type is ok there, because we 1752 * only shall prevent calling devvn_refthread on the file that 1753 * never has been opened over a character device. 1754 */ 1755 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) { 1756 error = EINVAL; 1757 goto done1; 1758 } 1759 1760 /* Make sure it is a TTY. */ 1761 cdp = devvn_refthread(fp->f_vnode, &dev); 1762 if (cdp == NULL) { 1763 error = ENXIO; 1764 goto done1; 1765 } 1766 if (dev != fp->f_data) { 1767 error = ENXIO; 1768 goto done2; 1769 } 1770 if (cdp != &ttydev_cdevsw) { 1771 error = ENOTTY; 1772 goto done2; 1773 } 1774 tp = dev->si_drv1; 1775 1776 /* Try to attach the hook to the TTY. */ 1777 error = EBUSY; 1778 tty_lock(tp); 1779 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0)); 1780 if (tp->t_flags & TF_HOOK) 1781 goto done3; 1782 1783 tp->t_flags |= TF_HOOK; 1784 tp->t_hook = th; 1785 tp->t_hooksoftc = softc; 1786 *rtp = tp; 1787 error = 0; 1788 1789 /* Maybe we can switch into bypass mode now. */ 1790 ttydisc_optimize(tp); 1791 1792 /* Silently convert rint() calls to rint_bypass() when possible. */ 1793 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass)) 1794 th->th_rint = ttyhook_defrint; 1795 1796done3: tty_unlock(tp); 1797done2: dev_relthread(dev); 1798done1: fdrop(fp, curthread); 1799 return (error); 1800} 1801 1802void 1803ttyhook_unregister(struct tty *tp) 1804{ 1805 1806 tty_lock_assert(tp, MA_OWNED); 1807 MPASS(tp->t_flags & TF_HOOK); 1808 1809 /* Disconnect the hook. */ 1810 tp->t_flags &= ~TF_HOOK; 1811 tp->t_hook = NULL; 1812 1813 /* Maybe we need to leave bypass mode. */ 1814 ttydisc_optimize(tp); 1815 1816 /* Maybe deallocate the TTY as well. */ 1817 tty_rel_free(tp); 1818} 1819 1820/* 1821 * /dev/console handling. 1822 */ 1823 1824static int 1825ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 1826{ 1827 struct tty *tp; 1828 1829 /* System has no console device. */ 1830 if (dev_console_filename == NULL) 1831 return (ENXIO); 1832 1833 /* Look up corresponding TTY by device name. */ 1834 sx_slock(&tty_list_sx); 1835 TAILQ_FOREACH(tp, &tty_list, t_list) { 1836 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) { 1837 dev_console->si_drv1 = tp; 1838 break; 1839 } 1840 } 1841 sx_sunlock(&tty_list_sx); 1842 1843 /* System console has no TTY associated. */ 1844 if (dev_console->si_drv1 == NULL) 1845 return (ENXIO); 1846 1847 return (ttydev_open(dev, oflags, devtype, td)); 1848} 1849 1850static int 1851ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag) 1852{ 1853 1854 log_console(uio); 1855 1856 return (ttydev_write(dev, uio, ioflag)); 1857} 1858 1859/* 1860 * /dev/console is a little different than normal TTY's. When opened, 1861 * it determines which TTY to use. When data gets written to it, it 1862 * will be logged in the kernel message buffer. 1863 */ 1864static struct cdevsw ttyconsdev_cdevsw = { 1865 .d_version = D_VERSION, 1866 .d_open = ttyconsdev_open, 1867 .d_close = ttydev_close, 1868 .d_read = ttydev_read, 1869 .d_write = ttyconsdev_write, 1870 .d_ioctl = ttydev_ioctl, 1871 .d_kqfilter = ttydev_kqfilter, 1872 .d_poll = ttydev_poll, 1873 .d_mmap = ttydev_mmap, 1874 .d_name = "ttyconsdev", 1875 .d_flags = D_TTY, 1876}; 1877 1878static void 1879ttyconsdev_init(void *unused) 1880{ 1881 1882 dev_console = make_dev(&ttyconsdev_cdevsw, 0, UID_ROOT, GID_WHEEL, 1883 0600, "console"); 1884} 1885 1886SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL); 1887 1888void 1889ttyconsdev_select(const char *name) 1890{ 1891 1892 dev_console_filename = name; 1893} 1894 1895/* 1896 * Debugging routines. 1897 */ 1898 1899#include "opt_ddb.h" 1900#ifdef DDB 1901#include <ddb/ddb.h> 1902#include <ddb/db_sym.h> 1903 1904static struct { 1905 int flag; 1906 char val; 1907} ttystates[] = { 1908#if 0 1909 { TF_NOPREFIX, 'N' }, 1910#endif 1911 { TF_INITLOCK, 'I' }, 1912 { TF_CALLOUT, 'C' }, 1913 1914 /* Keep these together -> 'Oi' and 'Oo'. */ 1915 { TF_OPENED, 'O' }, 1916 { TF_OPENED_IN, 'i' }, 1917 { TF_OPENED_OUT, 'o' }, 1918 { TF_OPENED_CONS, 'c' }, 1919 1920 { TF_GONE, 'G' }, 1921 { TF_OPENCLOSE, 'B' }, 1922 { TF_ASYNC, 'Y' }, 1923 { TF_LITERAL, 'L' }, 1924 1925 /* Keep these together -> 'Hi' and 'Ho'. */ 1926 { TF_HIWAT, 'H' }, 1927 { TF_HIWAT_IN, 'i' }, 1928 { TF_HIWAT_OUT, 'o' }, 1929 1930 { TF_STOPPED, 'S' }, 1931 { TF_EXCLUDE, 'X' }, 1932 { TF_BYPASS, 'l' }, 1933 { TF_ZOMBIE, 'Z' }, 1934 { TF_HOOK, 's' }, 1935 1936 /* Keep these together -> 'bi' and 'bo'. */ 1937 { TF_BUSY, 'b' }, 1938 { TF_BUSY_IN, 'i' }, 1939 { TF_BUSY_OUT, 'o' }, 1940 1941 { 0, '\0'}, 1942}; 1943 1944#define TTY_FLAG_BITS \ 1945 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \ 1946 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \ 1947 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK" 1948 1949#define DB_PRINTSYM(name, addr) \ 1950 db_printf("%s " #name ": ", sep); \ 1951 db_printsym((db_addr_t) addr, DB_STGY_ANY); \ 1952 db_printf("\n"); 1953 1954static void 1955_db_show_devsw(const char *sep, const struct ttydevsw *tsw) 1956{ 1957 db_printf("%sdevsw: ", sep); 1958 db_printsym((db_addr_t)tsw, DB_STGY_ANY); 1959 db_printf(" (%p)\n", tsw); 1960 DB_PRINTSYM(open, tsw->tsw_open); 1961 DB_PRINTSYM(close, tsw->tsw_close); 1962 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup); 1963 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup); 1964 DB_PRINTSYM(ioctl, tsw->tsw_ioctl); 1965 DB_PRINTSYM(param, tsw->tsw_param); 1966 DB_PRINTSYM(modem, tsw->tsw_modem); 1967 DB_PRINTSYM(mmap, tsw->tsw_mmap); 1968 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify); 1969 DB_PRINTSYM(free, tsw->tsw_free); 1970} 1971static void 1972_db_show_hooks(const char *sep, const struct ttyhook *th) 1973{ 1974 db_printf("%shook: ", sep); 1975 db_printsym((db_addr_t)th, DB_STGY_ANY); 1976 db_printf(" (%p)\n", th); 1977 if (th == NULL) 1978 return; 1979 DB_PRINTSYM(rint, th->th_rint); 1980 DB_PRINTSYM(rint_bypass, th->th_rint_bypass); 1981 DB_PRINTSYM(rint_done, th->th_rint_done); 1982 DB_PRINTSYM(rint_poll, th->th_rint_poll); 1983 DB_PRINTSYM(getc_inject, th->th_getc_inject); 1984 DB_PRINTSYM(getc_capture, th->th_getc_capture); 1985 DB_PRINTSYM(getc_poll, th->th_getc_poll); 1986 DB_PRINTSYM(close, th->th_close); 1987} 1988 1989static void 1990_db_show_termios(const char *name, const struct termios *t) 1991{ 1992 1993 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x " 1994 "lflag 0x%x ispeed %u ospeed %u\n", name, 1995 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag, 1996 t->c_ispeed, t->c_ospeed); 1997} 1998 1999/* DDB command to show TTY statistics. */ 2000DB_SHOW_COMMAND(tty, db_show_tty) 2001{ 2002 struct tty *tp; 2003 2004 if (!have_addr) { 2005 db_printf("usage: show tty <addr>\n"); 2006 return; 2007 } 2008 tp = (struct tty *)addr; 2009 2010 db_printf("0x%p: %s\n", tp, tty_devname(tp)); 2011 db_printf("\tmtx: %p\n", tp->t_mtx); 2012 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS); 2013 db_printf("\trevokecnt: %u\n", tp->t_revokecnt); 2014 2015 /* Buffering mechanisms. */ 2016 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u " 2017 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin, 2018 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end, 2019 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota); 2020 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n", 2021 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end, 2022 tp->t_outq.to_nblocks, tp->t_outq.to_quota); 2023 db_printf("\tinlow: %zu\n", tp->t_inlow); 2024 db_printf("\toutlow: %zu\n", tp->t_outlow); 2025 _db_show_termios("\ttermios", &tp->t_termios); 2026 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n", 2027 tp->t_winsize.ws_row, tp->t_winsize.ws_col, 2028 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel); 2029 db_printf("\tcolumn: %u\n", tp->t_column); 2030 db_printf("\twritepos: %u\n", tp->t_writepos); 2031 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags); 2032 2033 /* Init/lock-state devices. */ 2034 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in); 2035 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out); 2036 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in); 2037 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out); 2038 2039 /* Hooks */ 2040 _db_show_devsw("\t", tp->t_devsw); 2041 _db_show_hooks("\t", tp->t_hook); 2042 2043 /* Process info. */ 2044 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp, 2045 tp->t_pgrp ? tp->t_pgrp->pg_id : 0, 2046 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0); 2047 db_printf("\tsession: %p", tp->t_session); 2048 if (tp->t_session != NULL) 2049 db_printf(" count %u leader %p tty %p sid %d login %s", 2050 tp->t_session->s_count, tp->t_session->s_leader, 2051 tp->t_session->s_ttyp, tp->t_session->s_sid, 2052 tp->t_session->s_login); 2053 db_printf("\n"); 2054 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt); 2055 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc); 2056 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc); 2057 db_printf("\tdev: %p\n", tp->t_dev); 2058} 2059 2060/* DDB command to list TTYs. */ 2061DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys) 2062{ 2063 struct tty *tp; 2064 size_t isiz, osiz; 2065 int i, j; 2066 2067 /* Make the output look like `pstat -t'. */ 2068 db_printf("PTR "); 2069#if defined(__LP64__) 2070 db_printf(" "); 2071#endif 2072 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW " 2073 "COL SESS PGID STATE\n"); 2074 2075 TAILQ_FOREACH(tp, &tty_list, t_list) { 2076 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE; 2077 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE; 2078 2079 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ", 2080 tp, 2081 tty_devname(tp), 2082 isiz, 2083 tp->t_inq.ti_linestart - tp->t_inq.ti_begin, 2084 tp->t_inq.ti_end - tp->t_inq.ti_linestart, 2085 isiz - tp->t_inlow, 2086 osiz, 2087 tp->t_outq.to_end - tp->t_outq.to_begin, 2088 osiz - tp->t_outlow, 2089 MIN(tp->t_column, 99999), 2090 tp->t_session ? tp->t_session->s_sid : 0, 2091 tp->t_pgrp ? tp->t_pgrp->pg_id : 0); 2092 2093 /* Flag bits. */ 2094 for (i = j = 0; ttystates[i].flag; i++) 2095 if (tp->t_flags & ttystates[i].flag) { 2096 db_printf("%c", ttystates[i].val); 2097 j++; 2098 } 2099 if (j == 0) 2100 db_printf("-"); 2101 db_printf("\n"); 2102 } 2103} 2104#endif /* DDB */ 2105