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