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_getallocatedsize(&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_getallocatedsize(&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;
361 MPASS(sig == SIGTTIN || sig == SIGTTOU);
362 tty_lock_assert(tp, MA_OWNED);
367 * The process should only sleep, when:
368 * - This terminal is the controling terminal
369 * - Its process group is not the foreground process
371 * - The parent process isn't waiting for the child to
373 * - the signal to send to the process isn't masked
375 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
376 /* Allow the action to happen. */
381 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
382 SIGISMEMBER(td->td_sigmask, sig)) {
383 /* Only allow them in write()/ioctl(). */
385 return (sig == SIGTTOU ? 0 : EIO);
389 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
390 /* Don't allow the action to happen. */
397 * Send the signal and sleep until we're the new
398 * foreground process group.
402 ksi.ksi_code = SI_KERNEL;
407 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
410 error = tty_wait(tp, &tp->t_bgwait);
417 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
419 struct tty *tp = dev->si_drv1;
422 error = ttydev_enter(tp);
426 error = tty_wait_background(tp, curthread, SIGTTIN);
432 error = ttydisc_read(tp, uio, ioflag);
436 * The read() call should not throw an error when the device is
437 * being destroyed. Silently convert it to an EOF.
439 done: if (error == ENXIO)
445 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
447 struct tty *tp = dev->si_drv1;
450 error = ttydev_enter(tp);
454 if (tp->t_termios.c_lflag & TOSTOP) {
455 error = tty_wait_background(tp, curthread, SIGTTOU);
460 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
461 /* Allow non-blocking writes to bypass serialization. */
462 error = ttydisc_write(tp, uio, ioflag);
464 /* Serialize write() calls. */
465 while (tp->t_flags & TF_BUSY_OUT) {
466 error = tty_wait(tp, &tp->t_outserwait);
471 tp->t_flags |= TF_BUSY_OUT;
472 error = ttydisc_write(tp, uio, ioflag);
473 tp->t_flags &= ~TF_BUSY_OUT;
474 cv_signal(&tp->t_outserwait);
477 done: tty_unlock(tp);
482 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
485 struct tty *tp = dev->si_drv1;
488 error = ttydev_enter(tp);
523 #endif /* COMPAT_43TTY */
525 * If the ioctl() causes the TTY to be modified, let it
526 * wait in the background.
528 error = tty_wait_background(tp, curthread, SIGTTOU);
533 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
534 struct termios *old = &tp->t_termios;
535 struct termios *new = (struct termios *)data;
536 struct termios *lock = TTY_CALLOUT(tp, dev) ?
537 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
541 * Lock state devices. Just overwrite the values of the
542 * commands that are currently in use.
544 new->c_iflag = (old->c_iflag & lock->c_iflag) |
545 (new->c_iflag & ~lock->c_iflag);
546 new->c_oflag = (old->c_oflag & lock->c_oflag) |
547 (new->c_oflag & ~lock->c_oflag);
548 new->c_cflag = (old->c_cflag & lock->c_cflag) |
549 (new->c_cflag & ~lock->c_cflag);
550 new->c_lflag = (old->c_lflag & lock->c_lflag) |
551 (new->c_lflag & ~lock->c_lflag);
552 for (cc = 0; cc < NCCS; ++cc)
554 new->c_cc[cc] = old->c_cc[cc];
556 new->c_ispeed = old->c_ispeed;
558 new->c_ospeed = old->c_ospeed;
561 error = tty_ioctl(tp, cmd, data, fflag, td);
562 done: tty_unlock(tp);
568 ttydev_poll(struct cdev *dev, int events, struct thread *td)
570 struct tty *tp = dev->si_drv1;
571 int error, revents = 0;
573 error = ttydev_enter(tp);
575 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
577 if (events & (POLLIN|POLLRDNORM)) {
578 /* See if we can read something. */
579 if (ttydisc_read_poll(tp) > 0)
580 revents |= events & (POLLIN|POLLRDNORM);
583 if (tp->t_flags & TF_ZOMBIE) {
584 /* Hangup flag on zombie state. */
586 } else if (events & (POLLOUT|POLLWRNORM)) {
587 /* See if we can write something. */
588 if (ttydisc_write_poll(tp) > 0)
589 revents |= events & (POLLOUT|POLLWRNORM);
593 if (events & (POLLIN|POLLRDNORM))
594 selrecord(td, &tp->t_inpoll);
595 if (events & (POLLOUT|POLLWRNORM))
596 selrecord(td, &tp->t_outpoll);
605 ttydev_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
607 struct tty *tp = dev->si_drv1;
610 /* Handle mmap() through the driver. */
612 error = ttydev_enter(tp);
615 error = ttydevsw_mmap(tp, offset, paddr, nprot);
626 tty_kqops_read_detach(struct knote *kn)
628 struct tty *tp = kn->kn_hook;
630 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
634 tty_kqops_read_event(struct knote *kn, long hint)
636 struct tty *tp = kn->kn_hook;
638 tty_lock_assert(tp, MA_OWNED);
640 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
641 kn->kn_flags |= EV_EOF;
644 kn->kn_data = ttydisc_read_poll(tp);
645 return (kn->kn_data > 0);
650 tty_kqops_write_detach(struct knote *kn)
652 struct tty *tp = kn->kn_hook;
654 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
658 tty_kqops_write_event(struct knote *kn, long hint)
660 struct tty *tp = kn->kn_hook;
662 tty_lock_assert(tp, MA_OWNED);
665 kn->kn_flags |= EV_EOF;
668 kn->kn_data = ttydisc_write_poll(tp);
669 return (kn->kn_data > 0);
673 static struct filterops tty_kqops_read =
674 { 1, NULL, tty_kqops_read_detach, tty_kqops_read_event };
675 static struct filterops tty_kqops_write =
676 { 1, NULL, tty_kqops_write_detach, tty_kqops_write_event };
679 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
681 struct tty *tp = dev->si_drv1;
684 error = ttydev_enter(tp);
688 switch (kn->kn_filter) {
691 kn->kn_fop = &tty_kqops_read;
692 knlist_add(&tp->t_inpoll.si_note, kn, 1);
696 kn->kn_fop = &tty_kqops_write;
697 knlist_add(&tp->t_outpoll.si_note, kn, 1);
708 static struct cdevsw ttydev_cdevsw = {
709 .d_version = D_VERSION,
710 .d_open = ttydev_open,
711 .d_close = ttydev_close,
712 .d_read = ttydev_read,
713 .d_write = ttydev_write,
714 .d_ioctl = ttydev_ioctl,
715 .d_kqfilter = ttydev_kqfilter,
716 .d_poll = ttydev_poll,
717 .d_mmap = ttydev_mmap,
723 * Init/lock-state devices
727 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
729 struct tty *tp = dev->si_drv1;
741 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
747 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
753 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
756 struct tty *tp = dev->si_drv1;
767 /* Obtain terminal flags through tcgetattr(). */
768 *(struct termios*)data = *(struct termios*)dev->si_drv2;
771 /* Set terminal flags through tcsetattr(). */
772 error = priv_check(td, PRIV_TTY_SETA);
775 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
778 *(int *)data = TTYDISC;
781 bzero(data, sizeof(struct winsize));
787 done: tty_unlock(tp);
791 static struct cdevsw ttyil_cdevsw = {
792 .d_version = D_VERSION,
793 .d_open = ttyil_open,
794 .d_close = ttyil_close,
795 .d_read = ttyil_rdwr,
796 .d_write = ttyil_rdwr,
797 .d_ioctl = ttyil_ioctl,
803 tty_init_termios(struct tty *tp)
805 struct termios *t = &tp->t_termios_init_in;
807 t->c_cflag = TTYDEF_CFLAG;
808 t->c_iflag = TTYDEF_IFLAG;
809 t->c_lflag = TTYDEF_LFLAG;
810 t->c_oflag = TTYDEF_OFLAG;
811 t->c_ispeed = TTYDEF_SPEED;
812 t->c_ospeed = TTYDEF_SPEED;
813 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
815 tp->t_termios_init_out = *t;
819 tty_init_console(struct tty *tp, speed_t s)
821 struct termios *ti = &tp->t_termios_init_in;
822 struct termios *to = &tp->t_termios_init_out;
825 ti->c_ispeed = ti->c_ospeed = s;
826 to->c_ispeed = to->c_ospeed = s;
829 ti->c_cflag |= CLOCAL;
830 to->c_cflag |= CLOCAL;
834 * Standard device routine implementations, mostly meant for
835 * pseudo-terminal device drivers. When a driver creates a new terminal
836 * device class, missing routines are patched.
840 ttydevsw_defopen(struct tty *tp)
847 ttydevsw_defclose(struct tty *tp)
852 ttydevsw_defoutwakeup(struct tty *tp)
855 panic("Terminal device has output, while not implemented");
859 ttydevsw_definwakeup(struct tty *tp)
864 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
871 ttydevsw_defparam(struct tty *tp, struct termios *t)
874 /* Use a fake baud rate, we're not a real device. */
875 t->c_ispeed = t->c_ospeed = TTYDEF_SPEED;
881 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
884 /* Simulate a carrier to make the TTY layer happy. */
889 ttydevsw_defmmap(struct tty *tp, vm_offset_t offset, vm_paddr_t *paddr,
897 ttydevsw_defpktnotify(struct tty *tp, char event)
902 ttydevsw_deffree(void *softc)
905 panic("Terminal device freed without a free-handler");
909 * TTY allocation and deallocation. TTY devices can be deallocated when
910 * the driver doesn't use it anymore, when the TTY isn't a session's
911 * controlling TTY and when the device node isn't opened through devfs.
915 tty_alloc(struct ttydevsw *tsw, void *sc)
918 return (tty_alloc_mutex(tsw, sc, NULL));
922 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
926 /* Make sure the driver defines all routines. */
927 #define PATCH_FUNC(x) do { \
928 if (tsw->tsw_ ## x == NULL) \
929 tsw->tsw_ ## x = ttydevsw_def ## x; \
933 PATCH_FUNC(outwakeup);
934 PATCH_FUNC(inwakeup);
939 PATCH_FUNC(pktnotify);
943 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
945 tp->t_devswsoftc = sc;
946 tp->t_flags = tsw->tsw_flags;
948 tty_init_termios(tp);
950 cv_init(&tp->t_inwait, "ttyin");
951 cv_init(&tp->t_outwait, "ttyout");
952 cv_init(&tp->t_outserwait, "ttyosr");
953 cv_init(&tp->t_bgwait, "ttybg");
954 cv_init(&tp->t_dcdwait, "ttydcd");
956 /* Allow drivers to use a custom mutex to lock the TTY. */
960 tp->t_mtx = &tp->t_mtxobj;
961 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
964 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
965 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
967 sx_xlock(&tty_list_sx);
968 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
970 sx_xunlock(&tty_list_sx);
976 tty_dealloc(void *arg)
978 struct tty *tp = arg;
980 sx_xlock(&tty_list_sx);
981 TAILQ_REMOVE(&tty_list, tp, t_list);
983 sx_xunlock(&tty_list_sx);
985 /* Make sure we haven't leaked buffers. */
986 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
987 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
989 seldrain(&tp->t_inpoll);
990 seldrain(&tp->t_outpoll);
991 knlist_destroy(&tp->t_inpoll.si_note);
992 knlist_destroy(&tp->t_outpoll.si_note);
994 cv_destroy(&tp->t_inwait);
995 cv_destroy(&tp->t_outwait);
996 cv_destroy(&tp->t_bgwait);
997 cv_destroy(&tp->t_dcdwait);
998 cv_destroy(&tp->t_outserwait);
1000 if (tp->t_mtx == &tp->t_mtxobj)
1001 mtx_destroy(&tp->t_mtxobj);
1007 tty_rel_free(struct tty *tp)
1011 tty_lock_assert(tp, MA_OWNED);
1013 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1014 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1015 /* TTY is still in use. */
1020 /* TTY can be deallocated. */
1026 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1030 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1032 MPASS(tp->t_sessioncnt > 0);
1033 tty_lock_assert(tp, MA_OWNED);
1035 if (tp->t_pgrp == pg)
1042 tty_rel_sess(struct tty *tp, struct session *sess)
1044 MPASS(tp->t_sessioncnt > 0);
1046 /* Current session has left. */
1047 if (tp->t_session == sess) {
1048 tp->t_session = NULL;
1049 MPASS(tp->t_pgrp == NULL);
1056 tty_rel_gone(struct tty *tp)
1058 MPASS(!tty_gone(tp));
1060 /* Simulate carrier removal. */
1061 ttydisc_modem(tp, 0);
1063 /* Wake up all blocked threads. */
1064 tty_wakeup(tp, FREAD|FWRITE);
1065 cv_broadcast(&tp->t_bgwait);
1066 cv_broadcast(&tp->t_dcdwait);
1068 tp->t_flags |= TF_GONE;
1073 * Exposing information about current TTY's through sysctl
1077 tty_to_xtty(struct tty *tp, struct xtty *xt)
1079 tty_lock_assert(tp, MA_OWNED);
1081 xt->xt_size = sizeof(struct xtty);
1082 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1083 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1084 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1085 xt->xt_inlow = tp->t_inlow;
1086 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1087 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1088 xt->xt_outlow = tp->t_outlow;
1089 xt->xt_column = tp->t_column;
1090 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1091 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1092 xt->xt_flags = tp->t_flags;
1093 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1097 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1099 unsigned long lsize;
1100 struct xtty *xtlist, *xt;
1104 sx_slock(&tty_list_sx);
1105 lsize = tty_list_count * sizeof(struct xtty);
1107 sx_sunlock(&tty_list_sx);
1111 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1113 TAILQ_FOREACH(tp, &tty_list, t_list) {
1115 tty_to_xtty(tp, xt);
1119 sx_sunlock(&tty_list_sx);
1121 error = SYSCTL_OUT(req, xtlist, lsize);
1122 free(xtlist, M_TTY);
1126 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1127 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1130 * Device node creation. Device has been set up, now we can expose it to
1135 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
1139 const char *prefix = "tty";
1140 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1145 /* Remove "tty" prefix from devices like PTY's. */
1146 if (tp->t_flags & TF_NOPREFIX)
1150 vsnrprintf(name, sizeof name, 32, fmt, ap);
1154 /* System device. */
1157 mode = S_IRUSR|S_IWUSR;
1160 uid = cred->cr_ruid;
1162 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1165 /* Master call-in device. */
1166 dev = make_dev_cred(&ttydev_cdevsw, 0, cred,
1167 uid, gid, mode, "%s%s", prefix, name);
1171 /* Slave call-in devices. */
1172 if (tp->t_flags & TF_INITLOCK) {
1173 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1174 uid, gid, mode, "%s%s.init", prefix, name);
1175 dev_depends(tp->t_dev, dev);
1177 dev->si_drv2 = &tp->t_termios_init_in;
1179 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1180 uid, gid, mode, "%s%s.lock", prefix, name);
1181 dev_depends(tp->t_dev, dev);
1183 dev->si_drv2 = &tp->t_termios_lock_in;
1186 /* Call-out devices. */
1187 if (tp->t_flags & TF_CALLOUT) {
1188 dev = make_dev_cred(&ttydev_cdevsw, 0, cred,
1189 UID_UUCP, GID_DIALER, 0660, "cua%s", name);
1190 dev_depends(tp->t_dev, dev);
1193 /* Slave call-out devices. */
1194 if (tp->t_flags & TF_INITLOCK) {
1195 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1196 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name);
1197 dev_depends(tp->t_dev, dev);
1199 dev->si_drv2 = &tp->t_termios_init_out;
1201 dev = make_dev_cred(&ttyil_cdevsw, 0, cred,
1202 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name);
1203 dev_depends(tp->t_dev, dev);
1205 dev->si_drv2 = &tp->t_termios_lock_out;
1211 * Signalling processes.
1215 tty_signal_sessleader(struct tty *tp, int sig)
1219 tty_lock_assert(tp, MA_OWNED);
1220 MPASS(sig >= 1 && sig < NSIG);
1222 /* Make signals start output again. */
1223 tp->t_flags &= ~TF_STOPPED;
1225 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1226 p = tp->t_session->s_leader;
1234 tty_signal_pgrp(struct tty *tp, int sig)
1238 tty_lock_assert(tp, MA_OWNED);
1239 MPASS(sig >= 1 && sig < NSIG);
1241 /* Make signals start output again. */
1242 tp->t_flags &= ~TF_STOPPED;
1244 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1246 if (tp->t_pgrp != NULL) {
1247 ksiginfo_init(&ksi);
1248 ksi.ksi_signo = sig;
1249 ksi.ksi_code = SI_KERNEL;
1250 PGRP_LOCK(tp->t_pgrp);
1251 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1252 PGRP_UNLOCK(tp->t_pgrp);
1257 tty_wakeup(struct tty *tp, int flags)
1259 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1260 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1262 if (flags & FWRITE) {
1263 cv_broadcast(&tp->t_outwait);
1264 selwakeup(&tp->t_outpoll);
1265 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1267 if (flags & FREAD) {
1268 cv_broadcast(&tp->t_inwait);
1269 selwakeup(&tp->t_inpoll);
1270 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1275 tty_wait(struct tty *tp, struct cv *cv)
1278 int revokecnt = tp->t_revokecnt;
1280 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1281 MPASS(!tty_gone(tp));
1283 error = cv_wait_sig(cv, tp->t_mtx);
1285 /* Restart the system call when we may have been revoked. */
1286 if (tp->t_revokecnt != revokecnt)
1289 /* Bail out when the device slipped away. */
1297 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1300 int revokecnt = tp->t_revokecnt;
1302 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1303 MPASS(!tty_gone(tp));
1305 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1307 /* Restart the system call when we may have been revoked. */
1308 if (tp->t_revokecnt != revokecnt)
1311 /* Bail out when the device slipped away. */
1319 tty_flush(struct tty *tp, int flags)
1321 if (flags & FWRITE) {
1322 tp->t_flags &= ~TF_HIWAT_OUT;
1323 ttyoutq_flush(&tp->t_outq);
1324 tty_wakeup(tp, FWRITE);
1325 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1327 if (flags & FREAD) {
1328 tty_hiwat_in_unblock(tp);
1329 ttyinq_flush(&tp->t_inq);
1330 ttydevsw_inwakeup(tp);
1331 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1336 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1344 * The SER_* and TIOCM_* flags are the same, but one bit
1345 * shifted. I don't know why.
1348 ttydevsw_modem(tp, SER_DTR, 0);
1351 ttydevsw_modem(tp, 0, SER_DTR);
1354 int bits = *(int *)data;
1356 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1357 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1361 int bits = *(int *)data;
1362 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1366 int bits = *(int *)data;
1367 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1371 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1376 tp->t_flags |= TF_ASYNC;
1378 tp->t_flags &= ~TF_ASYNC;
1381 /* This device supports non-blocking operation. */
1384 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1388 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1391 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1392 /* Not allowed to set ownership. */
1395 /* Temporarily unlock the TTY to set ownership. */
1397 error = fsetown(*(int *)data, &tp->t_sigio);
1401 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1402 /* Not allowed to set ownership. */
1405 /* Get ownership. */
1406 *(int *)data = fgetown(&tp->t_sigio);
1409 /* Obtain terminal flags through tcgetattr(). */
1410 *(struct termios*)data = tp->t_termios;
1415 struct termios *t = data;
1418 * Who makes up these funny rules? According to POSIX,
1419 * input baud rate is set equal to the output baud rate
1422 if (t->c_ispeed == 0)
1423 t->c_ispeed = t->c_ospeed;
1425 /* Discard any unsupported bits. */
1426 t->c_iflag &= TTYSUP_IFLAG;
1427 t->c_oflag &= TTYSUP_OFLAG;
1428 t->c_lflag &= TTYSUP_LFLAG;
1429 t->c_cflag &= TTYSUP_CFLAG;
1431 /* Set terminal flags through tcsetattr(). */
1432 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1433 error = tty_drain(tp);
1436 if (cmd == TIOCSETAF)
1437 tty_flush(tp, FREAD);
1441 * Only call param() when the flags really change.
1443 if ((t->c_cflag & CIGNORE) == 0 &&
1444 (tp->t_termios.c_cflag != t->c_cflag ||
1445 tp->t_termios.c_ispeed != t->c_ispeed ||
1446 tp->t_termios.c_ospeed != t->c_ospeed)) {
1447 error = ttydevsw_param(tp, t);
1453 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1454 tp->t_termios.c_ispeed = t->c_ispeed;
1455 tp->t_termios.c_ospeed = t->c_ospeed;
1457 /* Baud rate has changed - update watermarks. */
1461 /* Copy new non-device driver parameters. */
1462 tp->t_termios.c_iflag = t->c_iflag;
1463 tp->t_termios.c_oflag = t->c_oflag;
1464 tp->t_termios.c_lflag = t->c_lflag;
1465 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1467 ttydisc_optimize(tp);
1469 if ((t->c_lflag & ICANON) == 0) {
1471 * When in non-canonical mode, wake up all
1472 * readers. Canonicalize any partial input. VMIN
1473 * and VTIME could also be adjusted.
1475 ttyinq_canonicalize(&tp->t_inq);
1476 tty_wakeup(tp, FREAD);
1480 * For packet mode: notify the PTY consumer that VSTOP
1481 * and VSTART may have been changed.
1483 if (tp->t_termios.c_iflag & IXON &&
1484 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1485 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1486 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1488 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1492 /* For compatibility - we only support TTYDISC. */
1493 *(int *)data = TTYDISC;
1496 if (!tty_is_ctty(tp, td->td_proc))
1499 if (tp->t_pgrp != NULL)
1500 *(int *)data = tp->t_pgrp->pg_id;
1502 *(int *)data = NO_PID;
1505 if (!tty_is_ctty(tp, td->td_proc))
1508 MPASS(tp->t_session);
1509 *(int *)data = tp->t_session->s_sid;
1512 struct proc *p = td->td_proc;
1514 /* XXX: This looks awful. */
1516 sx_xlock(&proctree_lock);
1519 if (!SESS_LEADER(p)) {
1520 /* Only the session leader may do this. */
1521 sx_xunlock(&proctree_lock);
1525 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1526 /* This is already our controlling TTY. */
1527 sx_xunlock(&proctree_lock);
1531 if (p->p_session->s_ttyp != NULL ||
1532 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1533 tp->t_session->s_ttyvp->v_type != VBAD)) {
1535 * There is already a relation between a TTY and
1536 * a session, or the caller is not the session
1539 * Allow the TTY to be stolen when the vnode is
1540 * invalid, but the reference to the TTY is
1541 * still active. This allows immediate reuse of
1542 * TTYs of which the session leader has been
1543 * killed or the TTY revoked.
1545 sx_xunlock(&proctree_lock);
1549 /* Connect the session to the TTY. */
1550 tp->t_session = p->p_session;
1551 tp->t_session->s_ttyp = tp;
1553 sx_xunlock(&proctree_lock);
1555 /* Assign foreground process group. */
1556 tp->t_pgrp = p->p_pgrp;
1558 p->p_flag |= P_CONTROLT;
1567 * XXX: Temporarily unlock the TTY to locate the process
1568 * group. This code would be lot nicer if we would ever
1569 * decompose proctree_lock.
1572 sx_slock(&proctree_lock);
1573 pg = pgfind(*(int *)data);
1576 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1577 sx_sunlock(&proctree_lock);
1584 * Determine if this TTY is the controlling TTY after
1585 * relocking the TTY.
1587 if (!tty_is_ctty(tp, td->td_proc)) {
1588 sx_sunlock(&proctree_lock);
1592 sx_sunlock(&proctree_lock);
1594 /* Wake up the background process groups. */
1595 cv_broadcast(&tp->t_bgwait);
1599 int flags = *(int *)data;
1602 flags = (FREAD|FWRITE);
1604 flags &= (FREAD|FWRITE);
1605 tty_flush(tp, flags);
1609 /* Drain TTY output. */
1610 return tty_drain(tp);
1612 /* Set terminal as console TTY. */
1614 error = priv_check(td, PRIV_TTY_CONSOLE);
1619 * XXX: constty should really need to be locked!
1620 * XXX: allow disconnected constty's to be stolen!
1625 if (constty != NULL)
1631 } else if (constty == tp) {
1636 /* Obtain window size. */
1637 *(struct winsize*)data = tp->t_winsize;
1640 /* Set window size. */
1641 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0)
1643 tp->t_winsize = *(struct winsize*)data;
1644 tty_signal_pgrp(tp, SIGWINCH);
1647 tp->t_flags |= TF_EXCLUDE;
1650 tp->t_flags &= ~TF_EXCLUDE;
1653 tp->t_flags |= TF_STOPPED;
1654 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1657 tp->t_flags &= ~TF_STOPPED;
1658 ttydevsw_outwakeup(tp);
1659 ttydevsw_pktnotify(tp, TIOCPKT_START);
1665 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1667 if (!tty_is_ctty(tp, td->td_proc) &&
1668 priv_check(td, PRIV_TTY_STI))
1670 ttydisc_rint(tp, *(char *)data, 0);
1671 ttydisc_rint_done(tp);
1676 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1677 #else /* !COMPAT_43TTY */
1679 #endif /* COMPAT_43TTY */
1683 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1687 tty_lock_assert(tp, MA_OWNED);
1692 error = ttydevsw_ioctl(tp, cmd, data, td);
1693 if (error == ENOIOCTL)
1694 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1700 tty_udev(struct tty *tp)
1703 return dev2udev(tp->t_dev);
1709 tty_checkoutq(struct tty *tp)
1712 /* 256 bytes should be enough to print a log message. */
1713 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1717 tty_hiwat_in_block(struct tty *tp)
1720 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1721 tp->t_termios.c_iflag & IXOFF &&
1722 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1724 * Input flow control. Only enter the high watermark when we
1725 * can successfully store the VSTOP character.
1727 if (ttyoutq_write_nofrag(&tp->t_outq,
1728 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1729 tp->t_flags |= TF_HIWAT_IN;
1731 /* No input flow control. */
1732 tp->t_flags |= TF_HIWAT_IN;
1737 tty_hiwat_in_unblock(struct tty *tp)
1740 if (tp->t_flags & TF_HIWAT_IN &&
1741 tp->t_termios.c_iflag & IXOFF &&
1742 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1744 * Input flow control. Only leave the high watermark when we
1745 * can successfully store the VSTART character.
1747 if (ttyoutq_write_nofrag(&tp->t_outq,
1748 &tp->t_termios.c_cc[VSTART], 1) == 0)
1749 tp->t_flags &= ~TF_HIWAT_IN;
1751 /* No input flow control. */
1752 tp->t_flags &= ~TF_HIWAT_IN;
1756 ttydevsw_inwakeup(tp);
1760 * TTY hooks interface.
1764 ttyhook_defrint(struct tty *tp, char c, int flags)
1767 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1774 ttyhook_register(struct tty **rtp, struct proc *p, int fd,
1775 struct ttyhook *th, void *softc)
1781 struct filedesc *fdp;
1784 /* Validate the file descriptor. */
1785 if ((fdp = p->p_fd) == NULL)
1788 fp = fget_unlocked(fdp, fd);
1791 if (fp->f_ops == &badfileops) {
1797 * Make sure the vnode is bound to a character device.
1798 * Unlocked check for the vnode type is ok there, because we
1799 * only shall prevent calling devvn_refthread on the file that
1800 * never has been opened over a character device.
1802 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1807 /* Make sure it is a TTY. */
1808 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
1813 if (dev != fp->f_data) {
1817 if (cdp != &ttydev_cdevsw) {
1823 /* Try to attach the hook to the TTY. */
1826 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1827 if (tp->t_flags & TF_HOOK)
1830 tp->t_flags |= TF_HOOK;
1832 tp->t_hooksoftc = softc;
1836 /* Maybe we can switch into bypass mode now. */
1837 ttydisc_optimize(tp);
1839 /* Silently convert rint() calls to rint_bypass() when possible. */
1840 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1841 th->th_rint = ttyhook_defrint;
1843 done3: tty_unlock(tp);
1844 done2: dev_relthread(dev, ref);
1845 done1: fdrop(fp, curthread);
1850 ttyhook_unregister(struct tty *tp)
1853 tty_lock_assert(tp, MA_OWNED);
1854 MPASS(tp->t_flags & TF_HOOK);
1856 /* Disconnect the hook. */
1857 tp->t_flags &= ~TF_HOOK;
1860 /* Maybe we need to leave bypass mode. */
1861 ttydisc_optimize(tp);
1863 /* Maybe deallocate the TTY as well. */
1868 * /dev/console handling.
1872 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1876 /* System has no console device. */
1877 if (dev_console_filename == NULL)
1880 /* Look up corresponding TTY by device name. */
1881 sx_slock(&tty_list_sx);
1882 TAILQ_FOREACH(tp, &tty_list, t_list) {
1883 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
1884 dev_console->si_drv1 = tp;
1888 sx_sunlock(&tty_list_sx);
1890 /* System console has no TTY associated. */
1891 if (dev_console->si_drv1 == NULL)
1894 return (ttydev_open(dev, oflags, devtype, td));
1898 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
1903 return (ttydev_write(dev, uio, ioflag));
1907 * /dev/console is a little different than normal TTY's. When opened,
1908 * it determines which TTY to use. When data gets written to it, it
1909 * will be logged in the kernel message buffer.
1911 static struct cdevsw ttyconsdev_cdevsw = {
1912 .d_version = D_VERSION,
1913 .d_open = ttyconsdev_open,
1914 .d_close = ttydev_close,
1915 .d_read = ttydev_read,
1916 .d_write = ttyconsdev_write,
1917 .d_ioctl = ttydev_ioctl,
1918 .d_kqfilter = ttydev_kqfilter,
1919 .d_poll = ttydev_poll,
1920 .d_mmap = ttydev_mmap,
1921 .d_name = "ttyconsdev",
1926 ttyconsdev_init(void *unused)
1929 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
1930 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
1933 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
1936 ttyconsdev_select(const char *name)
1939 dev_console_filename = name;
1943 * Debugging routines.
1946 #include "opt_ddb.h"
1948 #include <ddb/ddb.h>
1949 #include <ddb/db_sym.h>
1956 { TF_NOPREFIX, 'N' },
1958 { TF_INITLOCK, 'I' },
1959 { TF_CALLOUT, 'C' },
1961 /* Keep these together -> 'Oi' and 'Oo'. */
1963 { TF_OPENED_IN, 'i' },
1964 { TF_OPENED_OUT, 'o' },
1965 { TF_OPENED_CONS, 'c' },
1968 { TF_OPENCLOSE, 'B' },
1970 { TF_LITERAL, 'L' },
1972 /* Keep these together -> 'Hi' and 'Ho'. */
1974 { TF_HIWAT_IN, 'i' },
1975 { TF_HIWAT_OUT, 'o' },
1977 { TF_STOPPED, 'S' },
1978 { TF_EXCLUDE, 'X' },
1983 /* Keep these together -> 'bi' and 'bo'. */
1985 { TF_BUSY_IN, 'i' },
1986 { TF_BUSY_OUT, 'o' },
1991 #define TTY_FLAG_BITS \
1992 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
1993 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
1994 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
1996 #define DB_PRINTSYM(name, addr) \
1997 db_printf("%s " #name ": ", sep); \
1998 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2002 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2004 db_printf("%sdevsw: ", sep);
2005 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2006 db_printf(" (%p)\n", tsw);
2007 DB_PRINTSYM(open, tsw->tsw_open);
2008 DB_PRINTSYM(close, tsw->tsw_close);
2009 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2010 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2011 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2012 DB_PRINTSYM(param, tsw->tsw_param);
2013 DB_PRINTSYM(modem, tsw->tsw_modem);
2014 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2015 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2016 DB_PRINTSYM(free, tsw->tsw_free);
2019 _db_show_hooks(const char *sep, const struct ttyhook *th)
2021 db_printf("%shook: ", sep);
2022 db_printsym((db_addr_t)th, DB_STGY_ANY);
2023 db_printf(" (%p)\n", th);
2026 DB_PRINTSYM(rint, th->th_rint);
2027 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2028 DB_PRINTSYM(rint_done, th->th_rint_done);
2029 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2030 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2031 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2032 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2033 DB_PRINTSYM(close, th->th_close);
2037 _db_show_termios(const char *name, const struct termios *t)
2040 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2041 "lflag 0x%x ispeed %u ospeed %u\n", name,
2042 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2043 t->c_ispeed, t->c_ospeed);
2046 /* DDB command to show TTY statistics. */
2047 DB_SHOW_COMMAND(tty, db_show_tty)
2052 db_printf("usage: show tty <addr>\n");
2055 tp = (struct tty *)addr;
2057 db_printf("0x%p: %s\n", tp, tty_devname(tp));
2058 db_printf("\tmtx: %p\n", tp->t_mtx);
2059 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2060 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2062 /* Buffering mechanisms. */
2063 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2064 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2065 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2066 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2067 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2068 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2069 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2070 db_printf("\tinlow: %zu\n", tp->t_inlow);
2071 db_printf("\toutlow: %zu\n", tp->t_outlow);
2072 _db_show_termios("\ttermios", &tp->t_termios);
2073 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2074 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2075 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2076 db_printf("\tcolumn: %u\n", tp->t_column);
2077 db_printf("\twritepos: %u\n", tp->t_writepos);
2078 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2080 /* Init/lock-state devices. */
2081 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2082 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2083 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2084 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2087 _db_show_devsw("\t", tp->t_devsw);
2088 _db_show_hooks("\t", tp->t_hook);
2091 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2092 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2093 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2094 db_printf("\tsession: %p", tp->t_session);
2095 if (tp->t_session != NULL)
2096 db_printf(" count %u leader %p tty %p sid %d login %s",
2097 tp->t_session->s_count, tp->t_session->s_leader,
2098 tp->t_session->s_ttyp, tp->t_session->s_sid,
2099 tp->t_session->s_login);
2101 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2102 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2103 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2104 db_printf("\tdev: %p\n", tp->t_dev);
2107 /* DDB command to list TTYs. */
2108 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2114 /* Make the output look like `pstat -t'. */
2116 #if defined(__LP64__)
2119 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2120 "COL SESS PGID STATE\n");
2122 TAILQ_FOREACH(tp, &tty_list, t_list) {
2123 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2124 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2126 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2130 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2131 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2134 tp->t_outq.to_end - tp->t_outq.to_begin,
2135 osiz - tp->t_outlow,
2136 MIN(tp->t_column, 99999),
2137 tp->t_session ? tp->t_session->s_sid : 0,
2138 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2141 for (i = j = 0; ttystates[i].flag; i++)
2142 if (tp->t_flags & ttystates[i].flag) {
2143 db_printf("%c", ttystates[i].val);