tty.c revision 198223
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: head/sys/kern/tty.c 198223 2009-10-19 07:17:37Z ed $"); 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_getallocatedsize(&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_getallocatedsize(&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 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 527 struct termios *old = &tp->t_termios; 528 struct termios *new = (struct termios *)data; 529 struct termios *lock = TTY_CALLOUT(tp, dev) ? 530 &tp->t_termios_lock_out : &tp->t_termios_lock_in; 531 int cc; 532 533 /* 534 * Lock state devices. Just overwrite the values of the 535 * commands that are currently in use. 536 */ 537 new->c_iflag = (old->c_iflag & lock->c_iflag) | 538 (new->c_iflag & ~lock->c_iflag); 539 new->c_oflag = (old->c_oflag & lock->c_oflag) | 540 (new->c_oflag & ~lock->c_oflag); 541 new->c_cflag = (old->c_cflag & lock->c_cflag) | 542 (new->c_cflag & ~lock->c_cflag); 543 new->c_lflag = (old->c_lflag & lock->c_lflag) | 544 (new->c_lflag & ~lock->c_lflag); 545 for (cc = 0; cc < NCCS; ++cc) 546 if (lock->c_cc[cc]) 547 new->c_cc[cc] = old->c_cc[cc]; 548 if (lock->c_ispeed) 549 new->c_ispeed = old->c_ispeed; 550 if (lock->c_ospeed) 551 new->c_ospeed = old->c_ospeed; 552 } 553 554 error = tty_ioctl(tp, cmd, data, td); 555done: tty_unlock(tp); 556 557 return (error); 558} 559 560static int 561ttydev_poll(struct cdev *dev, int events, struct thread *td) 562{ 563 struct tty *tp = dev->si_drv1; 564 int error, revents = 0; 565 566 error = ttydev_enter(tp); 567 if (error) 568 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP); 569 570 if (events & (POLLIN|POLLRDNORM)) { 571 /* See if we can read something. */ 572 if (ttydisc_read_poll(tp) > 0) 573 revents |= events & (POLLIN|POLLRDNORM); 574 } 575 576 if (tp->t_flags & TF_ZOMBIE) { 577 /* Hangup flag on zombie state. */ 578 revents |= POLLHUP; 579 } else if (events & (POLLOUT|POLLWRNORM)) { 580 /* See if we can write something. */ 581 if (ttydisc_write_poll(tp) > 0) 582 revents |= events & (POLLOUT|POLLWRNORM); 583 } 584 585 if (revents == 0) { 586 if (events & (POLLIN|POLLRDNORM)) 587 selrecord(td, &tp->t_inpoll); 588 if (events & (POLLOUT|POLLWRNORM)) 589 selrecord(td, &tp->t_outpoll); 590 } 591 592 tty_unlock(tp); 593 594 return (revents); 595} 596 597static int 598ttydev_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 599{ 600 struct tty *tp = dev->si_drv1; 601 int error; 602 603 /* Handle mmap() through the driver. */ 604 605 error = ttydev_enter(tp); 606 if (error) 607 return (-1); 608 error = ttydevsw_mmap(tp, offset, paddr, nprot); 609 tty_unlock(tp); 610 611 return (error); 612} 613 614/* 615 * kqueue support. 616 */ 617 618static void 619tty_kqops_read_detach(struct knote *kn) 620{ 621 struct tty *tp = kn->kn_hook; 622 623 knlist_remove(&tp->t_inpoll.si_note, kn, 0); 624} 625 626static int 627tty_kqops_read_event(struct knote *kn, long hint) 628{ 629 struct tty *tp = kn->kn_hook; 630 631 tty_lock_assert(tp, MA_OWNED); 632 633 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) { 634 kn->kn_flags |= EV_EOF; 635 return (1); 636 } else { 637 kn->kn_data = ttydisc_read_poll(tp); 638 return (kn->kn_data > 0); 639 } 640} 641 642static void 643tty_kqops_write_detach(struct knote *kn) 644{ 645 struct tty *tp = kn->kn_hook; 646 647 knlist_remove(&tp->t_outpoll.si_note, kn, 0); 648} 649 650static int 651tty_kqops_write_event(struct knote *kn, long hint) 652{ 653 struct tty *tp = kn->kn_hook; 654 655 tty_lock_assert(tp, MA_OWNED); 656 657 if (tty_gone(tp)) { 658 kn->kn_flags |= EV_EOF; 659 return (1); 660 } else { 661 kn->kn_data = ttydisc_write_poll(tp); 662 return (kn->kn_data > 0); 663 } 664} 665 666static struct filterops tty_kqops_read = { 667 .f_isfd = 1, 668 .f_detach = tty_kqops_read_detach, 669 .f_event = tty_kqops_read_event, 670}; 671static struct filterops tty_kqops_write = { 672 .f_isfd = 1, 673 .f_detach = tty_kqops_write_detach, 674 .f_event = tty_kqops_write_event, 675}; 676 677static int 678ttydev_kqfilter(struct cdev *dev, struct knote *kn) 679{ 680 struct tty *tp = dev->si_drv1; 681 int error; 682 683 error = ttydev_enter(tp); 684 if (error) 685 return (error); 686 687 switch (kn->kn_filter) { 688 case EVFILT_READ: 689 kn->kn_hook = tp; 690 kn->kn_fop = &tty_kqops_read; 691 knlist_add(&tp->t_inpoll.si_note, kn, 1); 692 break; 693 case EVFILT_WRITE: 694 kn->kn_hook = tp; 695 kn->kn_fop = &tty_kqops_write; 696 knlist_add(&tp->t_outpoll.si_note, kn, 1); 697 break; 698 default: 699 error = EINVAL; 700 break; 701 } 702 703 tty_unlock(tp); 704 return (error); 705} 706 707static struct cdevsw ttydev_cdevsw = { 708 .d_version = D_VERSION, 709 .d_open = ttydev_open, 710 .d_close = ttydev_close, 711 .d_read = ttydev_read, 712 .d_write = ttydev_write, 713 .d_ioctl = ttydev_ioctl, 714 .d_kqfilter = ttydev_kqfilter, 715 .d_poll = ttydev_poll, 716 .d_mmap = ttydev_mmap, 717 .d_name = "ttydev", 718 .d_flags = D_TTY, 719}; 720 721/* 722 * Init/lock-state devices 723 */ 724 725static int 726ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 727{ 728 struct tty *tp = dev->si_drv1; 729 int error = 0; 730 731 tty_lock(tp); 732 if (tty_gone(tp)) 733 error = ENODEV; 734 tty_unlock(tp); 735 736 return (error); 737} 738 739static int 740ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td) 741{ 742 return (0); 743} 744 745static int 746ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag) 747{ 748 return (ENODEV); 749} 750 751static int 752ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 753 struct thread *td) 754{ 755 struct tty *tp = dev->si_drv1; 756 int error = 0; 757 758 tty_lock(tp); 759 if (tty_gone(tp)) { 760 error = ENODEV; 761 goto done; 762 } 763 764 switch (cmd) { 765 case TIOCGETA: 766 /* Obtain terminal flags through tcgetattr(). */ 767 *(struct termios*)data = *(struct termios*)dev->si_drv2; 768 break; 769 case TIOCSETA: 770 /* Set terminal flags through tcsetattr(). */ 771 error = priv_check(td, PRIV_TTY_SETA); 772 if (error) 773 break; 774 *(struct termios*)dev->si_drv2 = *(struct termios*)data; 775 break; 776 case TIOCGETD: 777 *(int *)data = TTYDISC; 778 break; 779 case TIOCGWINSZ: 780 bzero(data, sizeof(struct winsize)); 781 break; 782 default: 783 error = ENOTTY; 784 } 785 786done: tty_unlock(tp); 787 return (error); 788} 789 790static struct cdevsw ttyil_cdevsw = { 791 .d_version = D_VERSION, 792 .d_open = ttyil_open, 793 .d_close = ttyil_close, 794 .d_read = ttyil_rdwr, 795 .d_write = ttyil_rdwr, 796 .d_ioctl = ttyil_ioctl, 797 .d_name = "ttyil", 798 .d_flags = D_TTY, 799}; 800 801static void 802tty_init_termios(struct tty *tp) 803{ 804 struct termios *t = &tp->t_termios_init_in; 805 806 t->c_cflag = TTYDEF_CFLAG; 807 t->c_iflag = TTYDEF_IFLAG; 808 t->c_lflag = TTYDEF_LFLAG; 809 t->c_oflag = TTYDEF_OFLAG; 810 t->c_ispeed = TTYDEF_SPEED; 811 t->c_ospeed = TTYDEF_SPEED; 812 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars); 813 814 tp->t_termios_init_out = *t; 815} 816 817void 818tty_init_console(struct tty *tp, speed_t s) 819{ 820 struct termios *ti = &tp->t_termios_init_in; 821 struct termios *to = &tp->t_termios_init_out; 822 823 if (s != 0) { 824 ti->c_ispeed = ti->c_ospeed = s; 825 to->c_ispeed = to->c_ospeed = s; 826 } 827 828 ti->c_cflag |= CLOCAL; 829 to->c_cflag |= CLOCAL; 830} 831 832/* 833 * Standard device routine implementations, mostly meant for 834 * pseudo-terminal device drivers. When a driver creates a new terminal 835 * device class, missing routines are patched. 836 */ 837 838static int 839ttydevsw_defopen(struct tty *tp) 840{ 841 842 return (0); 843} 844 845static void 846ttydevsw_defclose(struct tty *tp) 847{ 848} 849 850static void 851ttydevsw_defoutwakeup(struct tty *tp) 852{ 853 854 panic("Terminal device has output, while not implemented"); 855} 856 857static void 858ttydevsw_definwakeup(struct tty *tp) 859{ 860} 861 862static int 863ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 864{ 865 866 return (ENOIOCTL); 867} 868 869static int 870ttydevsw_defparam(struct tty *tp, struct termios *t) 871{ 872 873 /* 874 * Allow the baud rate to be adjusted for pseudo-devices, but at 875 * least restrict it to 115200 to prevent excessive buffer 876 * usage. Also disallow 0, to prevent foot shooting. 877 */ 878 if (t->c_ispeed < B50) 879 t->c_ispeed = B50; 880 else if (t->c_ispeed > B115200) 881 t->c_ispeed = B115200; 882 if (t->c_ospeed < B50) 883 t->c_ospeed = B50; 884 else if (t->c_ospeed > B115200) 885 t->c_ospeed = B115200; 886 887 return (0); 888} 889 890static int 891ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff) 892{ 893 894 /* Simulate a carrier to make the TTY layer happy. */ 895 return (SER_DCD); 896} 897 898static int 899ttydevsw_defmmap(struct tty *tp, vm_offset_t offset, vm_paddr_t *paddr, 900 int nprot) 901{ 902 903 return (-1); 904} 905 906static void 907ttydevsw_defpktnotify(struct tty *tp, char event) 908{ 909} 910 911static void 912ttydevsw_deffree(void *softc) 913{ 914 915 panic("Terminal device freed without a free-handler"); 916} 917 918/* 919 * TTY allocation and deallocation. TTY devices can be deallocated when 920 * the driver doesn't use it anymore, when the TTY isn't a session's 921 * controlling TTY and when the device node isn't opened through devfs. 922 */ 923 924struct tty * 925tty_alloc(struct ttydevsw *tsw, void *sc) 926{ 927 928 return (tty_alloc_mutex(tsw, sc, NULL)); 929} 930 931struct tty * 932tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex) 933{ 934 struct tty *tp; 935 936 /* Make sure the driver defines all routines. */ 937#define PATCH_FUNC(x) do { \ 938 if (tsw->tsw_ ## x == NULL) \ 939 tsw->tsw_ ## x = ttydevsw_def ## x; \ 940} while (0) 941 PATCH_FUNC(open); 942 PATCH_FUNC(close); 943 PATCH_FUNC(outwakeup); 944 PATCH_FUNC(inwakeup); 945 PATCH_FUNC(ioctl); 946 PATCH_FUNC(param); 947 PATCH_FUNC(modem); 948 PATCH_FUNC(mmap); 949 PATCH_FUNC(pktnotify); 950 PATCH_FUNC(free); 951#undef PATCH_FUNC 952 953 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO); 954 tp->t_devsw = tsw; 955 tp->t_devswsoftc = sc; 956 tp->t_flags = tsw->tsw_flags; 957 958 tty_init_termios(tp); 959 960 cv_init(&tp->t_inwait, "ttyin"); 961 cv_init(&tp->t_outwait, "ttyout"); 962 cv_init(&tp->t_outserwait, "ttyosr"); 963 cv_init(&tp->t_bgwait, "ttybg"); 964 cv_init(&tp->t_dcdwait, "ttydcd"); 965 966 /* Allow drivers to use a custom mutex to lock the TTY. */ 967 if (mutex != NULL) { 968 tp->t_mtx = mutex; 969 } else { 970 tp->t_mtx = &tp->t_mtxobj; 971 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF); 972 } 973 974 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx); 975 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx); 976 977 sx_xlock(&tty_list_sx); 978 TAILQ_INSERT_TAIL(&tty_list, tp, t_list); 979 tty_list_count++; 980 sx_xunlock(&tty_list_sx); 981 982 return (tp); 983} 984 985static void 986tty_dealloc(void *arg) 987{ 988 struct tty *tp = arg; 989 990 sx_xlock(&tty_list_sx); 991 TAILQ_REMOVE(&tty_list, tp, t_list); 992 tty_list_count--; 993 sx_xunlock(&tty_list_sx); 994 995 /* Make sure we haven't leaked buffers. */ 996 MPASS(ttyinq_getsize(&tp->t_inq) == 0); 997 MPASS(ttyoutq_getsize(&tp->t_outq) == 0); 998 999 knlist_destroy(&tp->t_inpoll.si_note); 1000 knlist_destroy(&tp->t_outpoll.si_note); 1001 1002 cv_destroy(&tp->t_inwait); 1003 cv_destroy(&tp->t_outwait); 1004 cv_destroy(&tp->t_bgwait); 1005 cv_destroy(&tp->t_dcdwait); 1006 cv_destroy(&tp->t_outserwait); 1007 1008 if (tp->t_mtx == &tp->t_mtxobj) 1009 mtx_destroy(&tp->t_mtxobj); 1010 ttydevsw_free(tp); 1011 free(tp, M_TTY); 1012} 1013 1014static void 1015tty_rel_free(struct tty *tp) 1016{ 1017 struct cdev *dev; 1018 1019 tty_lock_assert(tp, MA_OWNED); 1020 1021#define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE) 1022 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) { 1023 /* TTY is still in use. */ 1024 tty_unlock(tp); 1025 return; 1026 } 1027 1028 /* TTY can be deallocated. */ 1029 dev = tp->t_dev; 1030 tp->t_dev = NULL; 1031 tty_unlock(tp); 1032 1033 destroy_dev_sched_cb(dev, tty_dealloc, tp); 1034} 1035 1036void 1037tty_rel_pgrp(struct tty *tp, struct pgrp *pg) 1038{ 1039 MPASS(tp->t_sessioncnt > 0); 1040 tty_lock_assert(tp, MA_OWNED); 1041 1042 if (tp->t_pgrp == pg) 1043 tp->t_pgrp = NULL; 1044 1045 tty_unlock(tp); 1046} 1047 1048void 1049tty_rel_sess(struct tty *tp, struct session *sess) 1050{ 1051 MPASS(tp->t_sessioncnt > 0); 1052 1053 /* Current session has left. */ 1054 if (tp->t_session == sess) { 1055 tp->t_session = NULL; 1056 MPASS(tp->t_pgrp == NULL); 1057 } 1058 tp->t_sessioncnt--; 1059 tty_rel_free(tp); 1060} 1061 1062void 1063tty_rel_gone(struct tty *tp) 1064{ 1065 MPASS(!tty_gone(tp)); 1066 1067 /* Simulate carrier removal. */ 1068 ttydisc_modem(tp, 0); 1069 1070 /* Wake up all blocked threads. */ 1071 tty_wakeup(tp, FREAD|FWRITE); 1072 cv_broadcast(&tp->t_bgwait); 1073 cv_broadcast(&tp->t_dcdwait); 1074 1075 tp->t_flags |= TF_GONE; 1076 tty_rel_free(tp); 1077} 1078 1079/* 1080 * Exposing information about current TTY's through sysctl 1081 */ 1082 1083static void 1084tty_to_xtty(struct tty *tp, struct xtty *xt) 1085{ 1086 tty_lock_assert(tp, MA_OWNED); 1087 1088 xt->xt_size = sizeof(struct xtty); 1089 xt->xt_insize = ttyinq_getsize(&tp->t_inq); 1090 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq); 1091 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq); 1092 xt->xt_inlow = tp->t_inlow; 1093 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq); 1094 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq); 1095 xt->xt_outlow = tp->t_outlow; 1096 xt->xt_column = tp->t_column; 1097 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 1098 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0; 1099 xt->xt_flags = tp->t_flags; 1100 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV; 1101} 1102 1103static int 1104sysctl_kern_ttys(SYSCTL_HANDLER_ARGS) 1105{ 1106 unsigned long lsize; 1107 struct xtty *xtlist, *xt; 1108 struct tty *tp; 1109 int error; 1110 1111 sx_slock(&tty_list_sx); 1112 lsize = tty_list_count * sizeof(struct xtty); 1113 if (lsize == 0) { 1114 sx_sunlock(&tty_list_sx); 1115 return (0); 1116 } 1117 1118 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK); 1119 1120 TAILQ_FOREACH(tp, &tty_list, t_list) { 1121 tty_lock(tp); 1122 tty_to_xtty(tp, xt); 1123 tty_unlock(tp); 1124 xt++; 1125 } 1126 sx_sunlock(&tty_list_sx); 1127 1128 error = SYSCTL_OUT(req, xtlist, lsize); 1129 free(xtlist, M_TTY); 1130 return (error); 1131} 1132 1133SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE, 1134 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs"); 1135 1136/* 1137 * Device node creation. Device has been set up, now we can expose it to 1138 * the user. 1139 */ 1140 1141void 1142tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...) 1143{ 1144 va_list ap; 1145 struct cdev *dev; 1146 const char *prefix = "tty"; 1147 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */ 1148 uid_t uid; 1149 gid_t gid; 1150 mode_t mode; 1151 1152 /* Remove "tty" prefix from devices like PTY's. */ 1153 if (tp->t_flags & TF_NOPREFIX) 1154 prefix = ""; 1155 1156 va_start(ap, fmt); 1157 vsnrprintf(name, sizeof name, 32, fmt, ap); 1158 va_end(ap); 1159 1160 if (cred == NULL) { 1161 /* System device. */ 1162 uid = UID_ROOT; 1163 gid = GID_WHEEL; 1164 mode = S_IRUSR|S_IWUSR; 1165 } else { 1166 /* User device. */ 1167 uid = cred->cr_ruid; 1168 gid = GID_TTY; 1169 mode = S_IRUSR|S_IWUSR|S_IWGRP; 1170 } 1171 1172 /* Master call-in device. */ 1173 dev = make_dev_cred(&ttydev_cdevsw, 0, cred, 1174 uid, gid, mode, "%s%s", prefix, name); 1175 dev->si_drv1 = tp; 1176 tp->t_dev = dev; 1177 1178 /* Slave call-in devices. */ 1179 if (tp->t_flags & TF_INITLOCK) { 1180 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1181 uid, gid, mode, "%s%s.init", prefix, name); 1182 dev_depends(tp->t_dev, dev); 1183 dev->si_drv1 = tp; 1184 dev->si_drv2 = &tp->t_termios_init_in; 1185 1186 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1187 uid, gid, mode, "%s%s.lock", prefix, name); 1188 dev_depends(tp->t_dev, dev); 1189 dev->si_drv1 = tp; 1190 dev->si_drv2 = &tp->t_termios_lock_in; 1191 } 1192 1193 /* Call-out devices. */ 1194 if (tp->t_flags & TF_CALLOUT) { 1195 dev = make_dev_cred(&ttydev_cdevsw, 0, cred, 1196 UID_UUCP, GID_DIALER, 0660, "cua%s", name); 1197 dev_depends(tp->t_dev, dev); 1198 dev->si_drv1 = tp; 1199 1200 /* Slave call-out devices. */ 1201 if (tp->t_flags & TF_INITLOCK) { 1202 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1203 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name); 1204 dev_depends(tp->t_dev, dev); 1205 dev->si_drv1 = tp; 1206 dev->si_drv2 = &tp->t_termios_init_out; 1207 1208 dev = make_dev_cred(&ttyil_cdevsw, 0, cred, 1209 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name); 1210 dev_depends(tp->t_dev, dev); 1211 dev->si_drv1 = tp; 1212 dev->si_drv2 = &tp->t_termios_lock_out; 1213 } 1214 } 1215} 1216 1217/* 1218 * Signalling processes. 1219 */ 1220 1221void 1222tty_signal_sessleader(struct tty *tp, int sig) 1223{ 1224 struct proc *p; 1225 1226 tty_lock_assert(tp, MA_OWNED); 1227 MPASS(sig >= 1 && sig < NSIG); 1228 1229 /* Make signals start output again. */ 1230 tp->t_flags &= ~TF_STOPPED; 1231 1232 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) { 1233 p = tp->t_session->s_leader; 1234 PROC_LOCK(p); 1235 psignal(p, sig); 1236 PROC_UNLOCK(p); 1237 } 1238} 1239 1240void 1241tty_signal_pgrp(struct tty *tp, int sig) 1242{ 1243 tty_lock_assert(tp, MA_OWNED); 1244 MPASS(sig >= 1 && sig < NSIG); 1245 1246 /* Make signals start output again. */ 1247 tp->t_flags &= ~TF_STOPPED; 1248 1249 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO)) 1250 tty_info(tp); 1251 if (tp->t_pgrp != NULL) { 1252 PGRP_LOCK(tp->t_pgrp); 1253 pgsignal(tp->t_pgrp, sig, 1); 1254 PGRP_UNLOCK(tp->t_pgrp); 1255 } 1256} 1257 1258void 1259tty_wakeup(struct tty *tp, int flags) 1260{ 1261 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL) 1262 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL)); 1263 1264 if (flags & FWRITE) { 1265 cv_broadcast(&tp->t_outwait); 1266 selwakeup(&tp->t_outpoll); 1267 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0); 1268 } 1269 if (flags & FREAD) { 1270 cv_broadcast(&tp->t_inwait); 1271 selwakeup(&tp->t_inpoll); 1272 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0); 1273 } 1274} 1275 1276int 1277tty_wait(struct tty *tp, struct cv *cv) 1278{ 1279 int error; 1280 int revokecnt = tp->t_revokecnt; 1281 1282 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED); 1283 MPASS(!tty_gone(tp)); 1284 1285 error = cv_wait_sig(cv, tp->t_mtx); 1286 1287 /* Restart the system call when we may have been revoked. */ 1288 if (tp->t_revokecnt != revokecnt) 1289 return (ERESTART); 1290 1291 /* Bail out when the device slipped away. */ 1292 if (tty_gone(tp)) 1293 return (ENXIO); 1294 1295 return (error); 1296} 1297 1298int 1299tty_timedwait(struct tty *tp, struct cv *cv, int hz) 1300{ 1301 int error; 1302 int revokecnt = tp->t_revokecnt; 1303 1304 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED); 1305 MPASS(!tty_gone(tp)); 1306 1307 error = cv_timedwait_sig(cv, tp->t_mtx, hz); 1308 1309 /* Restart the system call when we may have been revoked. */ 1310 if (tp->t_revokecnt != revokecnt) 1311 return (ERESTART); 1312 1313 /* Bail out when the device slipped away. */ 1314 if (tty_gone(tp)) 1315 return (ENXIO); 1316 1317 return (error); 1318} 1319 1320void 1321tty_flush(struct tty *tp, int flags) 1322{ 1323 if (flags & FWRITE) { 1324 tp->t_flags &= ~TF_HIWAT_OUT; 1325 ttyoutq_flush(&tp->t_outq); 1326 tty_wakeup(tp, FWRITE); 1327 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE); 1328 } 1329 if (flags & FREAD) { 1330 tty_hiwat_in_unblock(tp); 1331 ttyinq_flush(&tp->t_inq); 1332 ttydevsw_inwakeup(tp); 1333 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD); 1334 } 1335} 1336 1337static int 1338tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td) 1339{ 1340 int error; 1341 1342 switch (cmd) { 1343 /* 1344 * Modem commands. 1345 * The SER_* and TIOCM_* flags are the same, but one bit 1346 * shifted. I don't know why. 1347 */ 1348 case TIOCSDTR: 1349 ttydevsw_modem(tp, SER_DTR, 0); 1350 return (0); 1351 case TIOCCDTR: 1352 ttydevsw_modem(tp, 0, SER_DTR); 1353 return (0); 1354 case TIOCMSET: { 1355 int bits = *(int *)data; 1356 ttydevsw_modem(tp, 1357 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 1358 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1); 1359 return (0); 1360 } 1361 case TIOCMBIS: { 1362 int bits = *(int *)data; 1363 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0); 1364 return (0); 1365 } 1366 case TIOCMBIC: { 1367 int bits = *(int *)data; 1368 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1); 1369 return (0); 1370 } 1371 case TIOCMGET: 1372 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1); 1373 return (0); 1374 1375 case FIOASYNC: 1376 if (*(int *)data) 1377 tp->t_flags |= TF_ASYNC; 1378 else 1379 tp->t_flags &= ~TF_ASYNC; 1380 return (0); 1381 case FIONBIO: 1382 /* This device supports non-blocking operation. */ 1383 return (0); 1384 case FIONREAD: 1385 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq); 1386 return (0); 1387 case FIONWRITE: 1388 case TIOCOUTQ: 1389 *(int *)data = ttyoutq_bytesused(&tp->t_outq); 1390 return (0); 1391 case FIOSETOWN: 1392 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc)) 1393 /* Not allowed to set ownership. */ 1394 return (ENOTTY); 1395 1396 /* Temporarily unlock the TTY to set ownership. */ 1397 tty_unlock(tp); 1398 error = fsetown(*(int *)data, &tp->t_sigio); 1399 tty_lock(tp); 1400 return (error); 1401 case FIOGETOWN: 1402 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc)) 1403 /* Not allowed to set ownership. */ 1404 return (ENOTTY); 1405 1406 /* Get ownership. */ 1407 *(int *)data = fgetown(&tp->t_sigio); 1408 return (0); 1409 case TIOCGETA: 1410 /* Obtain terminal flags through tcgetattr(). */ 1411 *(struct termios*)data = tp->t_termios; 1412 return (0); 1413 case TIOCSETA: 1414 case TIOCSETAW: 1415 case TIOCSETAF: { 1416 struct termios *t = data; 1417 1418 /* 1419 * Who makes up these funny rules? According to POSIX, 1420 * input baud rate is set equal to the output baud rate 1421 * when zero. 1422 */ 1423 if (t->c_ispeed == 0) 1424 t->c_ispeed = t->c_ospeed; 1425 1426 /* Discard any unsupported bits. */ 1427 t->c_iflag &= TTYSUP_IFLAG; 1428 t->c_oflag &= TTYSUP_OFLAG; 1429 t->c_lflag &= TTYSUP_LFLAG; 1430 t->c_cflag &= TTYSUP_CFLAG; 1431 1432 /* Set terminal flags through tcsetattr(). */ 1433 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 1434 error = tty_drain(tp); 1435 if (error) 1436 return (error); 1437 if (cmd == TIOCSETAF) 1438 tty_flush(tp, FREAD); 1439 } 1440 1441 /* 1442 * Only call param() when the flags really change. 1443 */ 1444 if ((t->c_cflag & CIGNORE) == 0 && 1445 (tp->t_termios.c_cflag != t->c_cflag || 1446 tp->t_termios.c_ispeed != t->c_ispeed || 1447 tp->t_termios.c_ospeed != t->c_ospeed)) { 1448 error = ttydevsw_param(tp, t); 1449 if (error) 1450 return (error); 1451 1452 /* XXX: CLOCAL? */ 1453 1454 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE; 1455 tp->t_termios.c_ispeed = t->c_ispeed; 1456 tp->t_termios.c_ospeed = t->c_ospeed; 1457 1458 /* Baud rate has changed - update watermarks. */ 1459 tty_watermarks(tp); 1460 } 1461 1462 /* Copy new non-device driver parameters. */ 1463 tp->t_termios.c_iflag = t->c_iflag; 1464 tp->t_termios.c_oflag = t->c_oflag; 1465 tp->t_termios.c_lflag = t->c_lflag; 1466 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc); 1467 1468 ttydisc_optimize(tp); 1469 1470 if ((t->c_lflag & ICANON) == 0) { 1471 /* 1472 * When in non-canonical mode, wake up all 1473 * readers. Canonicalize any partial input. VMIN 1474 * and VTIME could also be adjusted. 1475 */ 1476 ttyinq_canonicalize(&tp->t_inq); 1477 tty_wakeup(tp, FREAD); 1478 } 1479 1480 /* 1481 * For packet mode: notify the PTY consumer that VSTOP 1482 * and VSTART may have been changed. 1483 */ 1484 if (tp->t_termios.c_iflag & IXON && 1485 tp->t_termios.c_cc[VSTOP] == CTRL('S') && 1486 tp->t_termios.c_cc[VSTART] == CTRL('Q')) 1487 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP); 1488 else 1489 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP); 1490 return (0); 1491 } 1492 case TIOCGETD: 1493 /* For compatibility - we only support TTYDISC. */ 1494 *(int *)data = TTYDISC; 1495 return (0); 1496 case TIOCGPGRP: 1497 if (!tty_is_ctty(tp, td->td_proc)) 1498 return (ENOTTY); 1499 1500 if (tp->t_pgrp != NULL) 1501 *(int *)data = tp->t_pgrp->pg_id; 1502 else 1503 *(int *)data = NO_PID; 1504 return (0); 1505 case TIOCGSID: 1506 if (!tty_is_ctty(tp, td->td_proc)) 1507 return (ENOTTY); 1508 1509 MPASS(tp->t_session); 1510 *(int *)data = tp->t_session->s_sid; 1511 return (0); 1512 case TIOCSCTTY: { 1513 struct proc *p = td->td_proc; 1514 1515 /* XXX: This looks awful. */ 1516 tty_unlock(tp); 1517 sx_xlock(&proctree_lock); 1518 tty_lock(tp); 1519 1520 if (!SESS_LEADER(p)) { 1521 /* Only the session leader may do this. */ 1522 sx_xunlock(&proctree_lock); 1523 return (EPERM); 1524 } 1525 1526 if (tp->t_session != NULL && tp->t_session == p->p_session) { 1527 /* This is already our controlling TTY. */ 1528 sx_xunlock(&proctree_lock); 1529 return (0); 1530 } 1531 1532 if (p->p_session->s_ttyp != NULL || 1533 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL && 1534 tp->t_session->s_ttyvp->v_type != VBAD)) { 1535 /* 1536 * There is already a relation between a TTY and 1537 * a session, or the caller is not the session 1538 * leader. 1539 * 1540 * Allow the TTY to be stolen when the vnode is 1541 * invalid, but the reference to the TTY is 1542 * still active. This allows immediate reuse of 1543 * TTYs of which the session leader has been 1544 * killed or the TTY revoked. 1545 */ 1546 sx_xunlock(&proctree_lock); 1547 return (EPERM); 1548 } 1549 1550 /* Connect the session to the TTY. */ 1551 tp->t_session = p->p_session; 1552 tp->t_session->s_ttyp = tp; 1553 tp->t_sessioncnt++; 1554 sx_xunlock(&proctree_lock); 1555 1556 /* Assign foreground process group. */ 1557 tp->t_pgrp = p->p_pgrp; 1558 PROC_LOCK(p); 1559 p->p_flag |= P_CONTROLT; 1560 PROC_UNLOCK(p); 1561 1562 return (0); 1563 } 1564 case TIOCSPGRP: { 1565 struct pgrp *pg; 1566 1567 /* 1568 * XXX: Temporarily unlock the TTY to locate the process 1569 * group. This code would be lot nicer if we would ever 1570 * decompose proctree_lock. 1571 */ 1572 tty_unlock(tp); 1573 sx_slock(&proctree_lock); 1574 pg = pgfind(*(int *)data); 1575 if (pg != NULL) 1576 PGRP_UNLOCK(pg); 1577 if (pg == NULL || pg->pg_session != td->td_proc->p_session) { 1578 sx_sunlock(&proctree_lock); 1579 tty_lock(tp); 1580 return (EPERM); 1581 } 1582 tty_lock(tp); 1583 1584 /* 1585 * Determine if this TTY is the controlling TTY after 1586 * relocking the TTY. 1587 */ 1588 if (!tty_is_ctty(tp, td->td_proc)) { 1589 sx_sunlock(&proctree_lock); 1590 return (ENOTTY); 1591 } 1592 tp->t_pgrp = pg; 1593 sx_sunlock(&proctree_lock); 1594 1595 /* Wake up the background process groups. */ 1596 cv_broadcast(&tp->t_bgwait); 1597 return (0); 1598 } 1599 case TIOCFLUSH: { 1600 int flags = *(int *)data; 1601 1602 if (flags == 0) 1603 flags = (FREAD|FWRITE); 1604 else 1605 flags &= (FREAD|FWRITE); 1606 tty_flush(tp, flags); 1607 return (0); 1608 } 1609 case TIOCDRAIN: 1610 /* Drain TTY output. */ 1611 return tty_drain(tp); 1612 case TIOCCONS: 1613 /* Set terminal as console TTY. */ 1614 if (*(int *)data) { 1615 error = priv_check(td, PRIV_TTY_CONSOLE); 1616 if (error) 1617 return (error); 1618 1619 /* 1620 * XXX: constty should really need to be locked! 1621 * XXX: allow disconnected constty's to be stolen! 1622 */ 1623 1624 if (constty == tp) 1625 return (0); 1626 if (constty != NULL) 1627 return (EBUSY); 1628 1629 tty_unlock(tp); 1630 constty_set(tp); 1631 tty_lock(tp); 1632 } else if (constty == tp) { 1633 constty_clear(); 1634 } 1635 return (0); 1636 case TIOCGWINSZ: 1637 /* Obtain window size. */ 1638 *(struct winsize*)data = tp->t_winsize; 1639 return (0); 1640 case TIOCSWINSZ: 1641 /* Set window size. */ 1642 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0) 1643 return (0); 1644 tp->t_winsize = *(struct winsize*)data; 1645 tty_signal_pgrp(tp, SIGWINCH); 1646 return (0); 1647 case TIOCEXCL: 1648 tp->t_flags |= TF_EXCLUDE; 1649 return (0); 1650 case TIOCNXCL: 1651 tp->t_flags &= ~TF_EXCLUDE; 1652 return (0); 1653 case TIOCSTOP: 1654 tp->t_flags |= TF_STOPPED; 1655 ttydevsw_pktnotify(tp, TIOCPKT_STOP); 1656 return (0); 1657 case TIOCSTART: 1658 tp->t_flags &= ~TF_STOPPED; 1659 ttydevsw_outwakeup(tp); 1660 ttydevsw_pktnotify(tp, TIOCPKT_START); 1661 return (0); 1662 case TIOCSTAT: 1663 tty_info(tp); 1664 return (0); 1665 } 1666 1667#ifdef COMPAT_43TTY 1668 return tty_ioctl_compat(tp, cmd, data, td); 1669#else /* !COMPAT_43TTY */ 1670 return (ENOIOCTL); 1671#endif /* COMPAT_43TTY */ 1672} 1673 1674int 1675tty_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td) 1676{ 1677 int error; 1678 1679 tty_lock_assert(tp, MA_OWNED); 1680 1681 if (tty_gone(tp)) 1682 return (ENXIO); 1683 1684 error = ttydevsw_ioctl(tp, cmd, data, td); 1685 if (error == ENOIOCTL) 1686 error = tty_generic_ioctl(tp, cmd, data, td); 1687 1688 return (error); 1689} 1690 1691dev_t 1692tty_udev(struct tty *tp) 1693{ 1694 if (tp->t_dev) 1695 return dev2udev(tp->t_dev); 1696 else 1697 return NODEV; 1698} 1699 1700int 1701tty_checkoutq(struct tty *tp) 1702{ 1703 1704 /* 256 bytes should be enough to print a log message. */ 1705 return (ttyoutq_bytesleft(&tp->t_outq) >= 256); 1706} 1707 1708void 1709tty_hiwat_in_block(struct tty *tp) 1710{ 1711 1712 if ((tp->t_flags & TF_HIWAT_IN) == 0 && 1713 tp->t_termios.c_iflag & IXOFF && 1714 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) { 1715 /* 1716 * Input flow control. Only enter the high watermark when we 1717 * can successfully store the VSTOP character. 1718 */ 1719 if (ttyoutq_write_nofrag(&tp->t_outq, 1720 &tp->t_termios.c_cc[VSTOP], 1) == 0) 1721 tp->t_flags |= TF_HIWAT_IN; 1722 } else { 1723 /* No input flow control. */ 1724 tp->t_flags |= TF_HIWAT_IN; 1725 } 1726} 1727 1728void 1729tty_hiwat_in_unblock(struct tty *tp) 1730{ 1731 1732 if (tp->t_flags & TF_HIWAT_IN && 1733 tp->t_termios.c_iflag & IXOFF && 1734 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) { 1735 /* 1736 * Input flow control. Only leave the high watermark when we 1737 * can successfully store the VSTART character. 1738 */ 1739 if (ttyoutq_write_nofrag(&tp->t_outq, 1740 &tp->t_termios.c_cc[VSTART], 1) == 0) 1741 tp->t_flags &= ~TF_HIWAT_IN; 1742 } else { 1743 /* No input flow control. */ 1744 tp->t_flags &= ~TF_HIWAT_IN; 1745 } 1746 1747 if (!tty_gone(tp)) 1748 ttydevsw_inwakeup(tp); 1749} 1750 1751/* 1752 * TTY hooks interface. 1753 */ 1754 1755static int 1756ttyhook_defrint(struct tty *tp, char c, int flags) 1757{ 1758 1759 if (ttyhook_rint_bypass(tp, &c, 1) != 1) 1760 return (-1); 1761 1762 return (0); 1763} 1764 1765int 1766ttyhook_register(struct tty **rtp, struct proc *p, int fd, 1767 struct ttyhook *th, void *softc) 1768{ 1769 struct tty *tp; 1770 struct file *fp; 1771 struct cdev *dev; 1772 struct cdevsw *cdp; 1773 struct filedesc *fdp; 1774 int error; 1775 1776 /* Validate the file descriptor. */ 1777 if ((fdp = p->p_fd) == NULL) 1778 return (EBADF); 1779 1780 fp = fget_unlocked(fdp, fd); 1781 if (fp == NULL) 1782 return (EBADF); 1783 if (fp->f_ops == &badfileops) { 1784 error = EBADF; 1785 goto done1; 1786 } 1787 1788 /* 1789 * Make sure the vnode is bound to a character device. 1790 * Unlocked check for the vnode type is ok there, because we 1791 * only shall prevent calling devvn_refthread on the file that 1792 * never has been opened over a character device. 1793 */ 1794 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) { 1795 error = EINVAL; 1796 goto done1; 1797 } 1798 1799 /* Make sure it is a TTY. */ 1800 cdp = devvn_refthread(fp->f_vnode, &dev); 1801 if (cdp == NULL) { 1802 error = ENXIO; 1803 goto done1; 1804 } 1805 if (dev != fp->f_data) { 1806 error = ENXIO; 1807 goto done2; 1808 } 1809 if (cdp != &ttydev_cdevsw) { 1810 error = ENOTTY; 1811 goto done2; 1812 } 1813 tp = dev->si_drv1; 1814 1815 /* Try to attach the hook to the TTY. */ 1816 error = EBUSY; 1817 tty_lock(tp); 1818 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0)); 1819 if (tp->t_flags & TF_HOOK) 1820 goto done3; 1821 1822 tp->t_flags |= TF_HOOK; 1823 tp->t_hook = th; 1824 tp->t_hooksoftc = softc; 1825 *rtp = tp; 1826 error = 0; 1827 1828 /* Maybe we can switch into bypass mode now. */ 1829 ttydisc_optimize(tp); 1830 1831 /* Silently convert rint() calls to rint_bypass() when possible. */ 1832 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass)) 1833 th->th_rint = ttyhook_defrint; 1834 1835done3: tty_unlock(tp); 1836done2: dev_relthread(dev); 1837done1: fdrop(fp, curthread); 1838 return (error); 1839} 1840 1841void 1842ttyhook_unregister(struct tty *tp) 1843{ 1844 1845 tty_lock_assert(tp, MA_OWNED); 1846 MPASS(tp->t_flags & TF_HOOK); 1847 1848 /* Disconnect the hook. */ 1849 tp->t_flags &= ~TF_HOOK; 1850 tp->t_hook = NULL; 1851 1852 /* Maybe we need to leave bypass mode. */ 1853 ttydisc_optimize(tp); 1854 1855 /* Maybe deallocate the TTY as well. */ 1856 tty_rel_free(tp); 1857} 1858 1859/* 1860 * /dev/console handling. 1861 */ 1862 1863static int 1864ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 1865{ 1866 struct tty *tp; 1867 1868 /* System has no console device. */ 1869 if (dev_console_filename == NULL) 1870 return (ENXIO); 1871 1872 /* Look up corresponding TTY by device name. */ 1873 sx_slock(&tty_list_sx); 1874 TAILQ_FOREACH(tp, &tty_list, t_list) { 1875 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) { 1876 dev_console->si_drv1 = tp; 1877 break; 1878 } 1879 } 1880 sx_sunlock(&tty_list_sx); 1881 1882 /* System console has no TTY associated. */ 1883 if (dev_console->si_drv1 == NULL) 1884 return (ENXIO); 1885 1886 return (ttydev_open(dev, oflags, devtype, td)); 1887} 1888 1889static int 1890ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag) 1891{ 1892 1893 log_console(uio); 1894 1895 return (ttydev_write(dev, uio, ioflag)); 1896} 1897 1898/* 1899 * /dev/console is a little different than normal TTY's. When opened, 1900 * it determines which TTY to use. When data gets written to it, it 1901 * will be logged in the kernel message buffer. 1902 */ 1903static struct cdevsw ttyconsdev_cdevsw = { 1904 .d_version = D_VERSION, 1905 .d_open = ttyconsdev_open, 1906 .d_close = ttydev_close, 1907 .d_read = ttydev_read, 1908 .d_write = ttyconsdev_write, 1909 .d_ioctl = ttydev_ioctl, 1910 .d_kqfilter = ttydev_kqfilter, 1911 .d_poll = ttydev_poll, 1912 .d_mmap = ttydev_mmap, 1913 .d_name = "ttyconsdev", 1914 .d_flags = D_TTY, 1915}; 1916 1917static void 1918ttyconsdev_init(void *unused) 1919{ 1920 1921 dev_console = make_dev(&ttyconsdev_cdevsw, 0, UID_ROOT, GID_WHEEL, 1922 0600, "console"); 1923} 1924 1925SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL); 1926 1927void 1928ttyconsdev_select(const char *name) 1929{ 1930 1931 dev_console_filename = name; 1932} 1933 1934/* 1935 * Debugging routines. 1936 */ 1937 1938#include "opt_ddb.h" 1939#ifdef DDB 1940#include <ddb/ddb.h> 1941#include <ddb/db_sym.h> 1942 1943static struct { 1944 int flag; 1945 char val; 1946} ttystates[] = { 1947#if 0 1948 { TF_NOPREFIX, 'N' }, 1949#endif 1950 { TF_INITLOCK, 'I' }, 1951 { TF_CALLOUT, 'C' }, 1952 1953 /* Keep these together -> 'Oi' and 'Oo'. */ 1954 { TF_OPENED, 'O' }, 1955 { TF_OPENED_IN, 'i' }, 1956 { TF_OPENED_OUT, 'o' }, 1957 { TF_OPENED_CONS, 'c' }, 1958 1959 { TF_GONE, 'G' }, 1960 { TF_OPENCLOSE, 'B' }, 1961 { TF_ASYNC, 'Y' }, 1962 { TF_LITERAL, 'L' }, 1963 1964 /* Keep these together -> 'Hi' and 'Ho'. */ 1965 { TF_HIWAT, 'H' }, 1966 { TF_HIWAT_IN, 'i' }, 1967 { TF_HIWAT_OUT, 'o' }, 1968 1969 { TF_STOPPED, 'S' }, 1970 { TF_EXCLUDE, 'X' }, 1971 { TF_BYPASS, 'l' }, 1972 { TF_ZOMBIE, 'Z' }, 1973 { TF_HOOK, 's' }, 1974 1975 /* Keep these together -> 'bi' and 'bo'. */ 1976 { TF_BUSY, 'b' }, 1977 { TF_BUSY_IN, 'i' }, 1978 { TF_BUSY_OUT, 'o' }, 1979 1980 { 0, '\0'}, 1981}; 1982 1983#define TTY_FLAG_BITS \ 1984 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \ 1985 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \ 1986 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK" 1987 1988#define DB_PRINTSYM(name, addr) \ 1989 db_printf("%s " #name ": ", sep); \ 1990 db_printsym((db_addr_t) addr, DB_STGY_ANY); \ 1991 db_printf("\n"); 1992 1993static void 1994_db_show_devsw(const char *sep, const struct ttydevsw *tsw) 1995{ 1996 db_printf("%sdevsw: ", sep); 1997 db_printsym((db_addr_t)tsw, DB_STGY_ANY); 1998 db_printf(" (%p)\n", tsw); 1999 DB_PRINTSYM(open, tsw->tsw_open); 2000 DB_PRINTSYM(close, tsw->tsw_close); 2001 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup); 2002 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup); 2003 DB_PRINTSYM(ioctl, tsw->tsw_ioctl); 2004 DB_PRINTSYM(param, tsw->tsw_param); 2005 DB_PRINTSYM(modem, tsw->tsw_modem); 2006 DB_PRINTSYM(mmap, tsw->tsw_mmap); 2007 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify); 2008 DB_PRINTSYM(free, tsw->tsw_free); 2009} 2010static void 2011_db_show_hooks(const char *sep, const struct ttyhook *th) 2012{ 2013 db_printf("%shook: ", sep); 2014 db_printsym((db_addr_t)th, DB_STGY_ANY); 2015 db_printf(" (%p)\n", th); 2016 if (th == NULL) 2017 return; 2018 DB_PRINTSYM(rint, th->th_rint); 2019 DB_PRINTSYM(rint_bypass, th->th_rint_bypass); 2020 DB_PRINTSYM(rint_done, th->th_rint_done); 2021 DB_PRINTSYM(rint_poll, th->th_rint_poll); 2022 DB_PRINTSYM(getc_inject, th->th_getc_inject); 2023 DB_PRINTSYM(getc_capture, th->th_getc_capture); 2024 DB_PRINTSYM(getc_poll, th->th_getc_poll); 2025 DB_PRINTSYM(close, th->th_close); 2026} 2027 2028static void 2029_db_show_termios(const char *name, const struct termios *t) 2030{ 2031 2032 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x " 2033 "lflag 0x%x ispeed %u ospeed %u\n", name, 2034 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag, 2035 t->c_ispeed, t->c_ospeed); 2036} 2037 2038/* DDB command to show TTY statistics. */ 2039DB_SHOW_COMMAND(tty, db_show_tty) 2040{ 2041 struct tty *tp; 2042 2043 if (!have_addr) { 2044 db_printf("usage: show tty <addr>\n"); 2045 return; 2046 } 2047 tp = (struct tty *)addr; 2048 2049 db_printf("0x%p: %s\n", tp, tty_devname(tp)); 2050 db_printf("\tmtx: %p\n", tp->t_mtx); 2051 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS); 2052 db_printf("\trevokecnt: %u\n", tp->t_revokecnt); 2053 2054 /* Buffering mechanisms. */ 2055 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u " 2056 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin, 2057 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end, 2058 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota); 2059 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n", 2060 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end, 2061 tp->t_outq.to_nblocks, tp->t_outq.to_quota); 2062 db_printf("\tinlow: %zu\n", tp->t_inlow); 2063 db_printf("\toutlow: %zu\n", tp->t_outlow); 2064 _db_show_termios("\ttermios", &tp->t_termios); 2065 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n", 2066 tp->t_winsize.ws_row, tp->t_winsize.ws_col, 2067 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel); 2068 db_printf("\tcolumn: %u\n", tp->t_column); 2069 db_printf("\twritepos: %u\n", tp->t_writepos); 2070 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags); 2071 2072 /* Init/lock-state devices. */ 2073 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in); 2074 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out); 2075 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in); 2076 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out); 2077 2078 /* Hooks */ 2079 _db_show_devsw("\t", tp->t_devsw); 2080 _db_show_hooks("\t", tp->t_hook); 2081 2082 /* Process info. */ 2083 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp, 2084 tp->t_pgrp ? tp->t_pgrp->pg_id : 0, 2085 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0); 2086 db_printf("\tsession: %p", tp->t_session); 2087 if (tp->t_session != NULL) 2088 db_printf(" count %u leader %p tty %p sid %d login %s", 2089 tp->t_session->s_count, tp->t_session->s_leader, 2090 tp->t_session->s_ttyp, tp->t_session->s_sid, 2091 tp->t_session->s_login); 2092 db_printf("\n"); 2093 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt); 2094 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc); 2095 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc); 2096 db_printf("\tdev: %p\n", tp->t_dev); 2097} 2098 2099/* DDB command to list TTYs. */ 2100DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys) 2101{ 2102 struct tty *tp; 2103 size_t isiz, osiz; 2104 int i, j; 2105 2106 /* Make the output look like `pstat -t'. */ 2107 db_printf("PTR "); 2108#if defined(__LP64__) 2109 db_printf(" "); 2110#endif 2111 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW " 2112 "COL SESS PGID STATE\n"); 2113 2114 TAILQ_FOREACH(tp, &tty_list, t_list) { 2115 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE; 2116 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE; 2117 2118 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ", 2119 tp, 2120 tty_devname(tp), 2121 isiz, 2122 tp->t_inq.ti_linestart - tp->t_inq.ti_begin, 2123 tp->t_inq.ti_end - tp->t_inq.ti_linestart, 2124 isiz - tp->t_inlow, 2125 osiz, 2126 tp->t_outq.to_end - tp->t_outq.to_begin, 2127 osiz - tp->t_outlow, 2128 MIN(tp->t_column, 99999), 2129 tp->t_session ? tp->t_session->s_sid : 0, 2130 tp->t_pgrp ? tp->t_pgrp->pg_id : 0); 2131 2132 /* Flag bits. */ 2133 for (i = j = 0; ttystates[i].flag; i++) 2134 if (tp->t_flags & ttystates[i].flag) { 2135 db_printf("%c", ttystates[i].val); 2136 j++; 2137 } 2138 if (j == 0) 2139 db_printf("-"); 2140 db_printf("\n"); 2141 } 2142} 2143#endif /* DDB */ 2144