2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5 * Portions of this software were developed under sponsorship from Snow
6 * B.V., the Netherlands.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_compat.h"
35 #include <sys/param.h>
38 #include <sys/fcntl.h>
40 #include <sys/filedesc.h>
41 #include <sys/filio.h>
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>
52 #include <sys/serial.h>
53 #include <sys/signal.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
59 #include <sys/ttycom.h>
61 #include <sys/ttydefaults.h>
63 #include <sys/ucred.h>
64 #include <sys/vnode.h>
66 #include <machine/stdarg.h>
68 static MALLOC_DEFINE(M_TTY, "tty", "tty device");
70 static void tty_rel_free(struct tty *tp);
72 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
73 static struct sx tty_list_sx;
74 SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
75 static unsigned int tty_list_count = 0;
77 /* Character device of /dev/console. */
78 static struct cdev *dev_console;
79 static const char *dev_console_filename;
82 * Flags that are supported and stored by this implementation.
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)
94 #define TTY_CALLOUT(tp,d) ((d) != (tp)->t_dev && (d) != dev_console)
97 * Set TTY buffer sizes.
100 #define TTYBUF_MAX 65536
103 tty_watermarks(struct tty *tp)
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);
111 /* Set low watermark at 10% (when 90% is available). */
112 tp->t_inlow = (ttyinq_getsize(&tp->t_inq) * 9) / 10;
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);
118 /* Set low watermark at 10% (when 90% is available). */
119 tp->t_outlow = (ttyoutq_getsize(&tp->t_outq) * 9) / 10;
123 tty_drain(struct tty *tp)
127 if (ttyhook_hashook(tp, getc_inject))
128 /* buffer is inaccessible */
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)
137 /* Wait for data to be drained. */
138 error = tty_wait(tp, &tp->t_outwait);
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
156 ttydev_enter(struct tty *tp)
160 if (tty_gone(tp) || !tty_opened(tp)) {
161 /* Device is already gone. */
170 ttydev_leave(struct tty *tp)
172 tty_lock_assert(tp, MA_OWNED);
174 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
175 /* Device is still opened somewhere. */
180 tp->t_flags |= TF_OPENCLOSE;
182 /* Stop asynchronous I/O. */
183 funsetown(&tp->t_sigio);
185 /* Remove console TTY. */
189 /* Drain any output. */
190 MPASS((tp->t_flags & TF_STOPPED) == 0);
196 /* Destroy associated buffers already. */
197 ttyinq_free(&tp->t_inq);
199 ttyoutq_free(&tp->t_outq);
202 knlist_clear(&tp->t_inpoll.si_note, 1);
203 knlist_clear(&tp->t_outpoll.si_note, 1);
208 tp->t_flags &= ~TF_OPENCLOSE;
209 cv_broadcast(&tp->t_dcdwait);
214 * Operations that are exposed through the character device in /dev.
217 ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
219 struct tty *tp = dev->si_drv1;
224 /* Device is already gone. */
230 * Block when other processes are currently opening or closing
233 while (tp->t_flags & TF_OPENCLOSE) {
234 error = tty_wait(tp, &tp->t_dcdwait);
240 tp->t_flags |= TF_OPENCLOSE;
243 * Make sure the "tty" and "cua" device cannot be opened at the
246 if (TTY_CALLOUT(tp, dev)) {
247 if (tp->t_flags & TF_OPENED_IN) {
252 if (tp->t_flags & TF_OPENED_OUT) {
258 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
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;
268 tp->t_termios = tp->t_termios_init_in;
270 ttydevsw_param(tp, &tp->t_termios);
272 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
274 error = ttydevsw_open(tp);
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);
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;
297 tp->t_flags |= TF_OPENED_IN;
299 done: tp->t_flags &= ~TF_OPENCLOSE;
300 cv_broadcast(&tp->t_dcdwait);
307 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
309 struct tty *tp = dev->si_drv1;
314 * Don't actually close the device if it is being used as the
317 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED);
318 if (dev == dev_console)
319 tp->t_flags &= ~TF_OPENED_CONS;
321 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
323 if (tp->t_flags & TF_OPENED) {
329 * This can only be called once. The callin and the callout
330 * devices cannot be opened at the same time.
332 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
334 /* Properly wake up threads that are stuck - revoke(). */
336 tty_wakeup(tp, FREAD|FWRITE);
337 cv_broadcast(&tp->t_bgwait);
338 cv_broadcast(&tp->t_dcdwait);
346 tty_is_ctty(struct tty *tp, struct proc *p)
348 tty_lock_assert(tp, MA_OWNED);
350 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
354 tty_wait_background(struct tty *tp, struct thread *td, int sig)
356 struct proc *p = td->td_proc;
360 MPASS(sig == SIGTTIN || sig == SIGTTOU);
361 tty_lock_assert(tp, MA_OWNED);
366 * The process should only sleep, when:
367 * - This terminal is the controling terminal
368 * - Its process group is not the foreground process
370 * - The parent process isn't waiting for the child to
372 * - the signal to send to the process isn't masked
374 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
375 /* Allow the action to happen. */
380 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
381 SIGISMEMBER(td->td_sigmask, sig)) {
382 /* Only allow them in write()/ioctl(). */
384 return (sig == SIGTTOU ? 0 : EIO);
388 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
389 /* Don't allow the action to happen. */
396 * Send the signal and sleep until we're the new
397 * foreground process group.
400 pgsignal(pg, sig, 1);
403 error = tty_wait(tp, &tp->t_bgwait);
410 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
412 struct tty *tp = dev->si_drv1;
415 error = ttydev_enter(tp);
419 error = tty_wait_background(tp, curthread, SIGTTIN);
425 error = ttydisc_read(tp, uio, ioflag);
429 * The read() call should not throw an error when the device is
430 * being destroyed. Silently convert it to an EOF.
432 done: if (error == ENXIO)
438 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
440 struct tty *tp = dev->si_drv1;
443 error = ttydev_enter(tp);
447 if (tp->t_termios.c_lflag & TOSTOP) {
448 error = tty_wait_background(tp, curthread, SIGTTOU);
453 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
454 /* Allow non-blocking writes to bypass serialization. */
455 error = ttydisc_write(tp, uio, ioflag);
457 /* Serialize write() calls. */
458 while (tp->t_flags & TF_BUSY_OUT) {
459 error = tty_wait(tp, &tp->t_outserwait);
464 tp->t_flags |= TF_BUSY_OUT;
465 error = ttydisc_write(tp, uio, ioflag);
466 tp->t_flags &= ~TF_BUSY_OUT;
467 cv_signal(&tp->t_outserwait);
470 done: tty_unlock(tp);
475 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
478 struct tty *tp = dev->si_drv1;
481 error = ttydev_enter(tp);
516 #endif /* COMPAT_43TTY */
518 * If the ioctl() causes the TTY to be modified, let it
519 * wait in the background.
521 error = tty_wait_background(tp, curthread, SIGTTOU);
526 error = tty_ioctl(tp, cmd, data, td);
527 done: tty_unlock(tp);
533 ttydev_poll(struct cdev *dev, int events, struct thread *td)
535 struct tty *tp = dev->si_drv1;
536 int error, revents = 0;
538 error = ttydev_enter(tp);
540 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
542 if (events & (POLLIN|POLLRDNORM)) {
543 /* See if we can read something. */
544 if (ttydisc_read_poll(tp) > 0)
545 revents |= events & (POLLIN|POLLRDNORM);
548 if (tp->t_flags & TF_ZOMBIE) {
549 /* Hangup flag on zombie state. */
551 } else if (events & (POLLOUT|POLLWRNORM)) {
552 /* See if we can write something. */
553 if (ttydisc_write_poll(tp) > 0)
554 revents |= events & (POLLOUT|POLLWRNORM);
558 if (events & (POLLIN|POLLRDNORM))
559 selrecord(td, &tp->t_inpoll);
560 if (events & (POLLOUT|POLLWRNORM))
561 selrecord(td, &tp->t_outpoll);
570 ttydev_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
572 struct tty *tp = dev->si_drv1;
575 /* Handle mmap() through the driver. */
577 error = ttydev_enter(tp);
580 error = ttydevsw_mmap(tp, offset, paddr, nprot);
591 tty_kqops_read_detach(struct knote *kn)
593 struct tty *tp = kn->kn_hook;
595 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
599 tty_kqops_read_event(struct knote *kn, long hint)
601 struct tty *tp = kn->kn_hook;
603 tty_lock_assert(tp, MA_OWNED);
605 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
606 kn->kn_flags |= EV_EOF;
609 kn->kn_data = ttydisc_read_poll(tp);
610 return (kn->kn_data > 0);
615 tty_kqops_write_detach(struct knote *kn)
617 struct tty *tp = kn->kn_hook;
619 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
623 tty_kqops_write_event(struct knote *kn, long hint)
625 struct tty *tp = kn->kn_hook;
627 tty_lock_assert(tp, MA_OWNED);
630 kn->kn_flags |= EV_EOF;
633 kn->kn_data = ttydisc_write_poll(tp);
634 return (kn->kn_data > 0);
638 static struct filterops tty_kqops_read =
639 { 1, NULL, tty_kqops_read_detach, tty_kqops_read_event };
640 static struct filterops tty_kqops_write =
641 { 1, NULL, tty_kqops_write_detach, tty_kqops_write_event };
644 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
646 struct tty *tp = dev->si_drv1;
649 error = ttydev_enter(tp);
653 switch (kn->kn_filter) {
656 kn->kn_fop = &tty_kqops_read;
657 knlist_add(&tp->t_inpoll.si_note, kn, 1);
661 kn->kn_fop = &tty_kqops_write;
662 knlist_add(&tp->t_outpoll.si_note, kn, 1);
673 static struct cdevsw ttydev_cdevsw = {
674 .d_version = D_VERSION,
675 .d_open = ttydev_open,
676 .d_close = ttydev_close,
677 .d_read = ttydev_read,
678 .d_write = ttydev_write,
679 .d_ioctl = ttydev_ioctl,
680 .d_kqfilter = ttydev_kqfilter,
681 .d_poll = ttydev_poll,
682 .d_mmap = ttydev_mmap,
688 * Init/lock-state devices
692 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
694 struct tty *tp = dev->si_drv1;
706 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
712 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
718 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
721 struct tty *tp = dev->si_drv1;
732 /* Obtain terminal flags through tcgetattr(). */
733 *(struct termios*)data = *(struct termios*)dev->si_drv2;
736 /* Set terminal flags through tcsetattr(). */
737 error = priv_check(td, PRIV_TTY_SETA);
740 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
743 *(int *)data = TTYDISC;
746 bzero(data, sizeof(struct winsize));
752 done: tty_unlock(tp);
756 static struct cdevsw ttyil_cdevsw = {
757 .d_version = D_VERSION,
758 .d_open = ttyil_open,
759 .d_close = ttyil_close,
760 .d_read = ttyil_rdwr,
761 .d_write = ttyil_rdwr,
762 .d_ioctl = ttyil_ioctl,
768 tty_init_termios(struct tty *tp)
770 struct termios *t = &tp->t_termios_init_in;
772 t->c_cflag = TTYDEF_CFLAG;
773 t->c_iflag = TTYDEF_IFLAG;
774 t->c_lflag = TTYDEF_LFLAG;
775 t->c_oflag = TTYDEF_OFLAG;
776 t->c_ispeed = TTYDEF_SPEED;
777 t->c_ospeed = TTYDEF_SPEED;
778 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
780 tp->t_termios_init_out = *t;
784 tty_init_console(struct tty *tp, speed_t s)
786 struct termios *ti = &tp->t_termios_init_in;
787 struct termios *to = &tp->t_termios_init_out;
790 ti->c_ispeed = ti->c_ospeed = s;
791 to->c_ispeed = to->c_ospeed = s;
794 ti->c_cflag |= CLOCAL;
795 to->c_cflag |= CLOCAL;
799 * Standard device routine implementations, mostly meant for
800 * pseudo-terminal device drivers. When a driver creates a new terminal
801 * device class, missing routines are patched.
805 ttydevsw_defopen(struct tty *tp)
812 ttydevsw_defclose(struct tty *tp)
817 ttydevsw_defoutwakeup(struct tty *tp)
820 panic("Terminal device has output, while not implemented");
824 ttydevsw_definwakeup(struct tty *tp)
829 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
836 ttydevsw_defparam(struct tty *tp, struct termios *t)
839 /* Use a fake baud rate, we're not a real device. */
840 t->c_ispeed = t->c_ospeed = TTYDEF_SPEED;
846 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
849 /* Simulate a carrier to make the TTY layer happy. */
854 ttydevsw_defmmap(struct tty *tp, vm_offset_t offset, vm_paddr_t *paddr,
862 ttydevsw_defpktnotify(struct tty *tp, char event)
867 ttydevsw_deffree(void *softc)
870 panic("Terminal device freed without a free-handler");
874 * TTY allocation and deallocation. TTY devices can be deallocated when
875 * the driver doesn't use it anymore, when the TTY isn't a session's
876 * controlling TTY and when the device node isn't opened through devfs.
880 tty_alloc(struct ttydevsw *tsw, void *sc)
883 return (tty_alloc_mutex(tsw, sc, NULL));
887 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
891 /* Make sure the driver defines all routines. */
892 #define PATCH_FUNC(x) do { \
893 if (tsw->tsw_ ## x == NULL) \
894 tsw->tsw_ ## x = ttydevsw_def ## x; \
898 PATCH_FUNC(outwakeup);
899 PATCH_FUNC(inwakeup);
904 PATCH_FUNC(pktnotify);
908 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
910 tp->t_devswsoftc = sc;
911 tp->t_flags = tsw->tsw_flags;
913 tty_init_termios(tp);
915 cv_init(&tp->t_inwait, "ttyin");
916 cv_init(&tp->t_outwait, "ttyout");
917 cv_init(&tp->t_outserwait, "ttyosr");
918 cv_init(&tp->t_bgwait, "ttybg");
919 cv_init(&tp->t_dcdwait, "ttydcd");
921 /* Allow drivers to use a custom mutex to lock the TTY. */
925 tp->t_mtx = &tp->t_mtxobj;
926 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
929 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
930 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
932 sx_xlock(&tty_list_sx);
933 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
935 sx_xunlock(&tty_list_sx);
941 tty_dealloc(void *arg)
943 struct tty *tp = arg;
945 sx_xlock(&tty_list_sx);
946 TAILQ_REMOVE(&tty_list, tp, t_list);
948 sx_xunlock(&tty_list_sx);
950 /* Make sure we haven't leaked buffers. */
951 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
952 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
954 knlist_destroy(&tp->t_inpoll.si_note);
955 knlist_destroy(&tp->t_outpoll.si_note);
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 cv_destroy(&tp->t_outserwait);
963 if (tp->t_mtx == &tp->t_mtxobj)
964 mtx_destroy(&tp->t_mtxobj);
970 tty_rel_free(struct tty *tp)
974 tty_lock_assert(tp, MA_OWNED);
976 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
977 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
978 /* TTY is still in use. */
983 /* TTY can be deallocated. */
988 destroy_dev_sched_cb(dev, tty_dealloc, tp);
992 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
994 MPASS(tp->t_sessioncnt > 0);
995 tty_lock_assert(tp, MA_OWNED);
997 if (tp->t_pgrp == pg)
1004 tty_rel_sess(struct tty *tp, struct session *sess)
1006 MPASS(tp->t_sessioncnt > 0);
1008 /* Current session has left. */
1009 if (tp->t_session == sess) {
1010 tp->t_session = NULL;
1011 MPASS(tp->t_pgrp == NULL);
1018 tty_rel_gone(struct tty *tp)
1020 MPASS(!tty_gone(tp));
1022 /* Simulate carrier removal. */
1023 ttydisc_modem(tp, 0);
1025 /* Wake up all blocked threads. */
1026 tty_wakeup(tp, FREAD|FWRITE);
1027 cv_broadcast(&tp->t_bgwait);
1028 cv_broadcast(&tp->t_dcdwait);
1030 tp->t_flags |= TF_GONE;
1035 * Exposing information about current TTY's through sysctl
1039 tty_to_xtty(struct tty *tp, struct xtty *xt)
1041 tty_lock_assert(tp, MA_OWNED);
1043 xt->xt_size = sizeof(struct xtty);
1044 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1045 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1046 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1047 xt->xt_inlow = tp->t_inlow;
1048 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1049 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1050 xt->xt_outlow = tp->t_outlow;
1051 xt->xt_column = tp->t_column;
1052 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1053 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1054 xt->xt_flags = tp->t_flags;
1055 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1059 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1061 unsigned long lsize;
1062 struct xtty *xtlist, *xt;
1066 sx_slock(&tty_list_sx);
1067 lsize = tty_list_count * sizeof(struct xtty);
1069 sx_sunlock(&tty_list_sx);
1073 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1075 TAILQ_FOREACH(tp, &tty_list, t_list) {
1077 tty_to_xtty(tp, xt);
1081 sx_sunlock(&tty_list_sx);
1083 error = SYSCTL_OUT(req, xtlist, lsize);
1084 free(xtlist, M_TTY);
1088 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1089 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1092 * Device node creation. Device has been set up, now we can expose it to
1097 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
1101 const char *prefix = "tty";
1102 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1107 /* Remove "tty" prefix from devices like PTY's. */
1108 if (tp->t_flags & TF_NOPREFIX)
1112 vsnrprintf(name, sizeof name, 32, fmt, ap);
1116 /* System device. */
1119 mode = S_IRUSR|S_IWUSR;
1122 uid = cred->cr_ruid;
1124 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1127 /* Master call-in device. */
1128 dev = make_dev_cred(&ttydev_cdevsw, 0, cred,
1129 uid, gid, mode, "%s%s", prefix, name);
1133 /* Slave call-in devices. */
1134 if (tp->t_flags & TF_INITLOCK) {
1135 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1136 uid, gid, mode, "%s%s.init", prefix, name);
1137 dev_depends(tp->t_dev, dev);
1139 dev->si_drv2 = &tp->t_termios_init_in;
1141 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1142 uid, gid, mode, "%s%s.lock", prefix, name);
1143 dev_depends(tp->t_dev, dev);
1145 dev->si_drv2 = &tp->t_termios_lock_in;
1148 /* Call-out devices. */
1149 if (tp->t_flags & TF_CALLOUT) {
1150 dev = make_dev_cred(&ttydev_cdevsw, 0, cred,
1151 UID_UUCP, GID_DIALER, 0660, "cua%s", name);
1152 dev_depends(tp->t_dev, dev);
1155 /* Slave call-out devices. */
1156 if (tp->t_flags & TF_INITLOCK) {
1157 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1158 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name);
1159 dev_depends(tp->t_dev, dev);
1161 dev->si_drv2 = &tp->t_termios_init_out;
1163 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1164 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name);
1165 dev_depends(tp->t_dev, dev);
1167 dev->si_drv2 = &tp->t_termios_lock_out;
1173 * Signalling processes.
1177 tty_signal_sessleader(struct tty *tp, int sig)
1181 tty_lock_assert(tp, MA_OWNED);
1182 MPASS(sig >= 1 && sig < NSIG);
1184 /* Make signals start output again. */
1185 tp->t_flags &= ~TF_STOPPED;
1187 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1188 p = tp->t_session->s_leader;
1196 tty_signal_pgrp(struct tty *tp, int sig)
1198 tty_lock_assert(tp, MA_OWNED);
1199 MPASS(sig >= 1 && sig < NSIG);
1201 /* Make signals start output again. */
1202 tp->t_flags &= ~TF_STOPPED;
1204 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1206 if (tp->t_pgrp != NULL) {
1207 PGRP_LOCK(tp->t_pgrp);
1208 pgsignal(tp->t_pgrp, sig, 1);
1209 PGRP_UNLOCK(tp->t_pgrp);
1214 tty_wakeup(struct tty *tp, int flags)
1216 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1217 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1219 if (flags & FWRITE) {
1220 cv_broadcast(&tp->t_outwait);
1221 selwakeup(&tp->t_outpoll);
1222 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1224 if (flags & FREAD) {
1225 cv_broadcast(&tp->t_inwait);
1226 selwakeup(&tp->t_inpoll);
1227 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1232 tty_wait(struct tty *tp, struct cv *cv)
1235 int revokecnt = tp->t_revokecnt;
1237 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1238 MPASS(!tty_gone(tp));
1240 error = cv_wait_sig(cv, tp->t_mtx);
1242 /* Restart the system call when we may have been revoked. */
1243 if (tp->t_revokecnt != revokecnt)
1246 /* Bail out when the device slipped away. */
1254 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1257 int revokecnt = tp->t_revokecnt;
1259 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1260 MPASS(!tty_gone(tp));
1262 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1264 /* Restart the system call when we may have been revoked. */
1265 if (tp->t_revokecnt != revokecnt)
1268 /* Bail out when the device slipped away. */
1276 tty_flush(struct tty *tp, int flags)
1278 if (flags & FWRITE) {
1279 tp->t_flags &= ~TF_HIWAT_OUT;
1280 ttyoutq_flush(&tp->t_outq);
1281 tty_wakeup(tp, FWRITE);
1282 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1284 if (flags & FREAD) {
1285 tty_hiwat_in_unblock(tp);
1286 ttyinq_flush(&tp->t_inq);
1287 ttydevsw_inwakeup(tp);
1288 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1293 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td)
1300 * The SER_* and TIOCM_* flags are the same, but one bit
1301 * shifted. I don't know why.
1304 ttydevsw_modem(tp, SER_DTR, 0);
1307 ttydevsw_modem(tp, 0, SER_DTR);
1310 int bits = *(int *)data;
1312 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1313 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1317 int bits = *(int *)data;
1318 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1322 int bits = *(int *)data;
1323 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1327 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1332 tp->t_flags |= TF_ASYNC;
1334 tp->t_flags &= ~TF_ASYNC;
1337 /* This device supports non-blocking operation. */
1340 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1344 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1347 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1348 /* Not allowed to set ownership. */
1351 /* Temporarily unlock the TTY to set ownership. */
1353 error = fsetown(*(int *)data, &tp->t_sigio);
1357 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1358 /* Not allowed to set ownership. */
1361 /* Get ownership. */
1362 *(int *)data = fgetown(&tp->t_sigio);
1365 /* Obtain terminal flags through tcgetattr(). */
1366 *(struct termios*)data = tp->t_termios;
1371 struct termios *t = data;
1374 * Who makes up these funny rules? According to POSIX,
1375 * input baud rate is set equal to the output baud rate
1378 if (t->c_ispeed == 0)
1379 t->c_ispeed = t->c_ospeed;
1381 /* Discard any unsupported bits. */
1382 t->c_iflag &= TTYSUP_IFLAG;
1383 t->c_oflag &= TTYSUP_OFLAG;
1384 t->c_lflag &= TTYSUP_LFLAG;
1385 t->c_cflag &= TTYSUP_CFLAG;
1387 /* Set terminal flags through tcsetattr(). */
1388 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1389 error = tty_drain(tp);
1392 if (cmd == TIOCSETAF)
1393 tty_flush(tp, FREAD);
1397 * Only call param() when the flags really change.
1399 if ((t->c_cflag & CIGNORE) == 0 &&
1400 (tp->t_termios.c_cflag != t->c_cflag ||
1401 tp->t_termios.c_ispeed != t->c_ispeed ||
1402 tp->t_termios.c_ospeed != t->c_ospeed)) {
1403 error = ttydevsw_param(tp, t);
1409 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1410 tp->t_termios.c_ispeed = t->c_ispeed;
1411 tp->t_termios.c_ospeed = t->c_ospeed;
1413 /* Baud rate has changed - update watermarks. */
1417 /* Copy new non-device driver parameters. */
1418 tp->t_termios.c_iflag = t->c_iflag;
1419 tp->t_termios.c_oflag = t->c_oflag;
1420 tp->t_termios.c_lflag = t->c_lflag;
1421 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1423 ttydisc_optimize(tp);
1425 if ((t->c_lflag & ICANON) == 0) {
1427 * When in non-canonical mode, wake up all
1428 * readers. Canonicalize any partial input. VMIN
1429 * and VTIME could also be adjusted.
1431 ttyinq_canonicalize(&tp->t_inq);
1432 tty_wakeup(tp, FREAD);
1436 * For packet mode: notify the PTY consumer that VSTOP
1437 * and VSTART may have been changed.
1439 if (tp->t_termios.c_iflag & IXON &&
1440 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1441 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1442 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1444 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1448 /* For compatibility - we only support TTYDISC. */
1449 *(int *)data = TTYDISC;
1452 if (!tty_is_ctty(tp, td->td_proc))
1455 if (tp->t_pgrp != NULL)
1456 *(int *)data = tp->t_pgrp->pg_id;
1458 *(int *)data = NO_PID;
1461 if (!tty_is_ctty(tp, td->td_proc))
1464 MPASS(tp->t_session);
1465 *(int *)data = tp->t_session->s_sid;
1468 struct proc *p = td->td_proc;
1470 /* XXX: This looks awful. */
1472 sx_xlock(&proctree_lock);
1475 if (!SESS_LEADER(p)) {
1476 /* Only the session leader may do this. */
1477 sx_xunlock(&proctree_lock);
1481 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1482 /* This is already our controlling TTY. */
1483 sx_xunlock(&proctree_lock);
1487 if (p->p_session->s_ttyp != NULL ||
1488 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1489 tp->t_session->s_ttyvp->v_type != VBAD)) {
1491 * There is already a relation between a TTY and
1492 * a session, or the caller is not the session
1495 * Allow the TTY to be stolen when the vnode is
1496 * invalid, but the reference to the TTY is
1497 * still active. This allows immediate reuse of
1498 * TTYs of which the session leader has been
1499 * killed or the TTY revoked.
1501 sx_xunlock(&proctree_lock);
1505 /* Connect the session to the TTY. */
1506 tp->t_session = p->p_session;
1507 tp->t_session->s_ttyp = tp;
1509 sx_xunlock(&proctree_lock);
1511 /* Assign foreground process group. */
1512 tp->t_pgrp = p->p_pgrp;
1514 p->p_flag |= P_CONTROLT;
1523 * XXX: Temporarily unlock the TTY to locate the process
1524 * group. This code would be lot nicer if we would ever
1525 * decompose proctree_lock.
1528 sx_slock(&proctree_lock);
1529 pg = pgfind(*(int *)data);
1532 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1533 sx_sunlock(&proctree_lock);
1540 * Determine if this TTY is the controlling TTY after
1541 * relocking the TTY.
1543 if (!tty_is_ctty(tp, td->td_proc)) {
1544 sx_sunlock(&proctree_lock);
1548 sx_sunlock(&proctree_lock);
1550 /* Wake up the background process groups. */
1551 cv_broadcast(&tp->t_bgwait);
1555 int flags = *(int *)data;
1558 flags = (FREAD|FWRITE);
1560 flags &= (FREAD|FWRITE);
1561 tty_flush(tp, flags);
1565 /* Drain TTY output. */
1566 return tty_drain(tp);
1568 /* Set terminal as console TTY. */
1570 error = priv_check(td, PRIV_TTY_CONSOLE);
1575 * XXX: constty should really need to be locked!
1576 * XXX: allow disconnected constty's to be stolen!
1581 if (constty != NULL)
1587 } else if (constty == tp) {
1592 /* Obtain window size. */
1593 *(struct winsize*)data = tp->t_winsize;
1596 /* Set window size. */
1597 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0)
1599 tp->t_winsize = *(struct winsize*)data;
1600 tty_signal_pgrp(tp, SIGWINCH);
1603 tp->t_flags |= TF_EXCLUDE;
1606 tp->t_flags &= ~TF_EXCLUDE;
1609 tp->t_flags |= TF_STOPPED;
1610 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1613 tp->t_flags &= ~TF_STOPPED;
1614 ttydevsw_outwakeup(tp);
1615 ttydevsw_pktnotify(tp, TIOCPKT_START);
1623 return tty_ioctl_compat(tp, cmd, data, td);
1624 #else /* !COMPAT_43TTY */
1626 #endif /* COMPAT_43TTY */
1630 tty_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td)
1634 tty_lock_assert(tp, MA_OWNED);
1639 error = ttydevsw_ioctl(tp, cmd, data, td);
1640 if (error == ENOIOCTL)
1641 error = tty_generic_ioctl(tp, cmd, data, td);
1647 tty_udev(struct tty *tp)
1650 return dev2udev(tp->t_dev);
1656 tty_checkoutq(struct tty *tp)
1659 /* 256 bytes should be enough to print a log message. */
1660 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1664 tty_hiwat_in_block(struct tty *tp)
1667 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1668 tp->t_termios.c_iflag & IXOFF &&
1669 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1671 * Input flow control. Only enter the high watermark when we
1672 * can successfully store the VSTOP character.
1674 if (ttyoutq_write_nofrag(&tp->t_outq,
1675 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1676 tp->t_flags |= TF_HIWAT_IN;
1678 /* No input flow control. */
1679 tp->t_flags |= TF_HIWAT_IN;
1684 tty_hiwat_in_unblock(struct tty *tp)
1687 if (tp->t_flags & TF_HIWAT_IN &&
1688 tp->t_termios.c_iflag & IXOFF &&
1689 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1691 * Input flow control. Only leave the high watermark when we
1692 * can successfully store the VSTART character.
1694 if (ttyoutq_write_nofrag(&tp->t_outq,
1695 &tp->t_termios.c_cc[VSTART], 1) == 0)
1696 tp->t_flags &= ~TF_HIWAT_IN;
1698 /* No input flow control. */
1699 tp->t_flags &= ~TF_HIWAT_IN;
1703 ttydevsw_inwakeup(tp);
1707 * TTY hooks interface.
1711 ttyhook_defrint(struct tty *tp, char c, int flags)
1714 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1721 ttyhook_register(struct tty **rtp, struct proc *p, int fd,
1722 struct ttyhook *th, void *softc)
1728 struct filedesc *fdp;
1731 /* Validate the file descriptor. */
1732 if ((fdp = p->p_fd) == NULL)
1735 fp = fget_unlocked(fdp, fd);
1738 if (fp->f_ops == &badfileops) {
1744 * Make sure the vnode is bound to a character device.
1745 * Unlocked check for the vnode type is ok there, because we
1746 * only shall prevent calling devvn_refthread on the file that
1747 * never has been opened over a character device.
1749 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1754 /* Make sure it is a TTY. */
1755 cdp = devvn_refthread(fp->f_vnode, &dev);
1760 if (dev != fp->f_data) {
1764 if (cdp != &ttydev_cdevsw) {
1770 /* Try to attach the hook to the TTY. */
1773 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1774 if (tp->t_flags & TF_HOOK)
1777 tp->t_flags |= TF_HOOK;
1779 tp->t_hooksoftc = softc;
1783 /* Maybe we can switch into bypass mode now. */
1784 ttydisc_optimize(tp);
1786 /* Silently convert rint() calls to rint_bypass() when possible. */
1787 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1788 th->th_rint = ttyhook_defrint;
1790 done3: tty_unlock(tp);
1791 done2: dev_relthread(dev);
1792 done1: fdrop(fp, curthread);
1797 ttyhook_unregister(struct tty *tp)
1800 tty_lock_assert(tp, MA_OWNED);
1801 MPASS(tp->t_flags & TF_HOOK);
1803 /* Disconnect the hook. */
1804 tp->t_flags &= ~TF_HOOK;
1807 /* Maybe we need to leave bypass mode. */
1808 ttydisc_optimize(tp);
1810 /* Maybe deallocate the TTY as well. */
1815 * /dev/console handling.
1819 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1823 /* System has no console device. */
1824 if (dev_console_filename == NULL)
1827 /* Look up corresponding TTY by device name. */
1828 sx_slock(&tty_list_sx);
1829 TAILQ_FOREACH(tp, &tty_list, t_list) {
1830 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
1831 dev_console->si_drv1 = tp;
1835 sx_sunlock(&tty_list_sx);
1837 /* System console has no TTY associated. */
1838 if (dev_console->si_drv1 == NULL)
1841 return (ttydev_open(dev, oflags, devtype, td));
1845 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
1850 return (ttydev_write(dev, uio, ioflag));
1854 * /dev/console is a little different than normal TTY's. When opened,
1855 * it determines which TTY to use. When data gets written to it, it
1856 * will be logged in the kernel message buffer.
1858 static struct cdevsw ttyconsdev_cdevsw = {
1859 .d_version = D_VERSION,
1860 .d_open = ttyconsdev_open,
1861 .d_close = ttydev_close,
1862 .d_read = ttydev_read,
1863 .d_write = ttyconsdev_write,
1864 .d_ioctl = ttydev_ioctl,
1865 .d_kqfilter = ttydev_kqfilter,
1866 .d_poll = ttydev_poll,
1867 .d_mmap = ttydev_mmap,
1868 .d_name = "ttyconsdev",
1873 ttyconsdev_init(void *unused)
1876 dev_console = make_dev(&ttyconsdev_cdevsw, 0, UID_ROOT, GID_WHEEL,
1880 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
1883 ttyconsdev_select(const char *name)
1886 dev_console_filename = name;
1890 * Debugging routines.
1893 #include "opt_ddb.h"
1895 #include <ddb/ddb.h>
1896 #include <ddb/db_sym.h>
1903 { TF_NOPREFIX, 'N' },
1905 { TF_INITLOCK, 'I' },
1906 { TF_CALLOUT, 'C' },
1908 /* Keep these together -> 'Oi' and 'Oo'. */
1910 { TF_OPENED_IN, 'i' },
1911 { TF_OPENED_OUT, 'o' },
1912 { TF_OPENED_CONS, 'c' },
1915 { TF_OPENCLOSE, 'B' },
1917 { TF_LITERAL, 'L' },
1919 /* Keep these together -> 'Hi' and 'Ho'. */
1921 { TF_HIWAT_IN, 'i' },
1922 { TF_HIWAT_OUT, 'o' },
1924 { TF_STOPPED, 'S' },
1925 { TF_EXCLUDE, 'X' },
1930 /* Keep these together -> 'bi' and 'bo'. */
1932 { TF_BUSY_IN, 'i' },
1933 { TF_BUSY_OUT, 'o' },
1938 #define TTY_FLAG_BITS \
1939 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
1940 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
1941 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
1943 #define DB_PRINTSYM(name, addr) \
1944 db_printf("%s " #name ": ", sep); \
1945 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
1949 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
1951 db_printf("%sdevsw: ", sep);
1952 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
1953 db_printf(" (%p)\n", tsw);
1954 DB_PRINTSYM(open, tsw->tsw_open);
1955 DB_PRINTSYM(close, tsw->tsw_close);
1956 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
1957 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
1958 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
1959 DB_PRINTSYM(param, tsw->tsw_param);
1960 DB_PRINTSYM(modem, tsw->tsw_modem);
1961 DB_PRINTSYM(mmap, tsw->tsw_mmap);
1962 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
1963 DB_PRINTSYM(free, tsw->tsw_free);
1966 _db_show_hooks(const char *sep, const struct ttyhook *th)
1968 db_printf("%shook: ", sep);
1969 db_printsym((db_addr_t)th, DB_STGY_ANY);
1970 db_printf(" (%p)\n", th);
1973 DB_PRINTSYM(rint, th->th_rint);
1974 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
1975 DB_PRINTSYM(rint_done, th->th_rint_done);
1976 DB_PRINTSYM(rint_poll, th->th_rint_poll);
1977 DB_PRINTSYM(getc_inject, th->th_getc_inject);
1978 DB_PRINTSYM(getc_capture, th->th_getc_capture);
1979 DB_PRINTSYM(getc_poll, th->th_getc_poll);
1980 DB_PRINTSYM(close, th->th_close);
1984 _db_show_termios(const char *name, const struct termios *t)
1987 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
1988 "lflag 0x%x ispeed %u ospeed %u\n", name,
1989 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
1990 t->c_ispeed, t->c_ospeed);
1993 /* DDB command to show TTY statistics. */
1994 DB_SHOW_COMMAND(tty, db_show_tty)
1999 db_printf("usage: show tty <addr>\n");
2002 tp = (struct tty *)addr;
2004 db_printf("0x%p: %s\n", tp, tty_devname(tp));
2005 db_printf("\tmtx: %p\n", tp->t_mtx);
2006 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2007 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2009 /* Buffering mechanisms. */
2010 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2011 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2012 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2013 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2014 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2015 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2016 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2017 db_printf("\tinlow: %zu\n", tp->t_inlow);
2018 db_printf("\toutlow: %zu\n", tp->t_outlow);
2019 _db_show_termios("\ttermios", &tp->t_termios);
2020 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2021 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2022 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2023 db_printf("\tcolumn: %u\n", tp->t_column);
2024 db_printf("\twritepos: %u\n", tp->t_writepos);
2025 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2027 /* Init/lock-state devices. */
2028 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2029 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2030 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2031 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2034 _db_show_devsw("\t", tp->t_devsw);
2035 _db_show_hooks("\t", tp->t_hook);
2038 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2039 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2040 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2041 db_printf("\tsession: %p", tp->t_session);
2042 if (tp->t_session != NULL)
2043 db_printf(" count %u leader %p tty %p sid %d login %s",
2044 tp->t_session->s_count, tp->t_session->s_leader,
2045 tp->t_session->s_ttyp, tp->t_session->s_sid,
2046 tp->t_session->s_login);
2048 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2049 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2050 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2051 db_printf("\tdev: %p\n", tp->t_dev);
2054 /* DDB command to list TTYs. */
2055 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2061 /* Make the output look like `pstat -t'. */
2063 #if defined(__LP64__)
2066 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2067 "COL SESS PGID STATE\n");
2069 TAILQ_FOREACH(tp, &tty_list, t_list) {
2070 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2071 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2073 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2077 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2078 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2081 tp->t_outq.to_end - tp->t_outq.to_begin,
2082 osiz - tp->t_outlow,
2083 MIN(tp->t_column, 99999),
2084 tp->t_session ? tp->t_session->s_sid : 0,
2085 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2088 for (i = j = 0; ttystates[i].flag; i++)
2089 if (tp->t_flags & ttystates[i].flag) {
2090 db_printf("%c", ttystates[i].val);