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