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