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