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_capsicum.h"
34 #include "opt_compat.h"
36 #include <sys/param.h>
37 #include <sys/capability.h>
40 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
45 #include <sys/ioctl_compat.h>
46 #endif /* COMPAT_43TTY */
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
54 #include <sys/serial.h>
55 #include <sys/signal.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
61 #include <sys/ttycom.h>
63 #include <sys/ttydefaults.h>
65 #include <sys/ucred.h>
66 #include <sys/vnode.h>
68 #include <machine/stdarg.h>
70 static MALLOC_DEFINE(M_TTY, "tty", "tty device");
72 static void tty_rel_free(struct tty *tp);
74 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
75 static struct sx tty_list_sx;
76 SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
77 static unsigned int tty_list_count = 0;
79 /* Character device of /dev/console. */
80 static struct cdev *dev_console;
81 static const char *dev_console_filename;
84 * Flags that are supported and stored by this implementation.
86 #define TTYSUP_IFLAG (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\
87 INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL)
88 #define TTYSUP_OFLAG (OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET)
89 #define TTYSUP_LFLAG (ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\
90 ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\
91 FLUSHO|NOKERNINFO|NOFLSH)
92 #define TTYSUP_CFLAG (CIGNORE|CSIZE|CSTOPB|CREAD|PARENB|PARODD|\
93 HUPCL|CLOCAL|CCTS_OFLOW|CRTS_IFLOW|CDTR_IFLOW|\
94 CDSR_OFLOW|CCAR_OFLOW)
96 #define TTY_CALLOUT(tp,d) (dev2unit(d) & TTYUNIT_CALLOUT)
99 * Set TTY buffer sizes.
102 #define TTYBUF_MAX 65536
105 tty_watermarks(struct tty *tp)
109 /* Provide an input buffer for 0.2 seconds of data. */
110 if (tp->t_termios.c_cflag & CREAD)
111 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX);
112 ttyinq_setsize(&tp->t_inq, tp, bs);
114 /* Set low watermark at 10% (when 90% is available). */
115 tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
117 /* Provide an output buffer for 0.2 seconds of data. */
118 bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX);
119 ttyoutq_setsize(&tp->t_outq, tp, bs);
121 /* Set low watermark at 10% (when 90% is available). */
122 tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
126 tty_drain(struct tty *tp)
130 if (ttyhook_hashook(tp, getc_inject))
131 /* buffer is inaccessible */
134 while (ttyoutq_bytesused(&tp->t_outq) > 0) {
135 ttydevsw_outwakeup(tp);
136 /* Could be handled synchronously. */
137 if (ttyoutq_bytesused(&tp->t_outq) == 0)
140 /* Wait for data to be drained. */
141 error = tty_wait(tp, &tp->t_outwait);
150 * Though ttydev_enter() and ttydev_leave() seem to be related, they
151 * don't have to be used together. ttydev_enter() is used by the cdev
152 * operations to prevent an actual operation from being processed when
153 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
154 * and ttydev_close() to determine whether per-TTY data should be
159 ttydev_enter(struct tty *tp)
163 if (tty_gone(tp) || !tty_opened(tp)) {
164 /* Device is already gone. */
173 ttydev_leave(struct tty *tp)
175 tty_lock_assert(tp, MA_OWNED);
177 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
178 /* Device is still opened somewhere. */
183 tp->t_flags |= TF_OPENCLOSE;
185 /* Stop asynchronous I/O. */
186 funsetown(&tp->t_sigio);
188 /* Remove console TTY. */
192 /* Drain any output. */
193 MPASS((tp->t_flags & TF_STOPPED) == 0);
195 while (tty_drain(tp) == ERESTART)
201 /* Destroy associated buffers already. */
202 ttyinq_free(&tp->t_inq);
204 ttyoutq_free(&tp->t_outq);
207 knlist_clear(&tp->t_inpoll.si_note, 1);
208 knlist_clear(&tp->t_outpoll.si_note, 1);
213 tp->t_flags &= ~TF_OPENCLOSE;
214 cv_broadcast(&tp->t_dcdwait);
219 * Operations that are exposed through the character device in /dev.
222 ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
227 while ((tp = dev->si_drv1) == NULL) {
228 error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1);
229 if (error != EWOULDBLOCK)
235 /* Device is already gone. */
241 * Block when other processes are currently opening or closing
244 while (tp->t_flags & TF_OPENCLOSE) {
245 error = tty_wait(tp, &tp->t_dcdwait);
251 tp->t_flags |= TF_OPENCLOSE;
254 * Make sure the "tty" and "cua" device cannot be opened at the
257 if (TTY_CALLOUT(tp, dev)) {
258 if (tp->t_flags & TF_OPENED_IN) {
263 if (tp->t_flags & TF_OPENED_OUT) {
269 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
274 if (!tty_opened(tp)) {
275 /* Set proper termios flags. */
276 if (TTY_CALLOUT(tp, dev))
277 tp->t_termios = tp->t_termios_init_out;
279 tp->t_termios = tp->t_termios_init_in;
280 ttydevsw_param(tp, &tp->t_termios);
281 /* Prevent modem control on callout devices and /dev/console. */
282 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
283 tp->t_termios.c_cflag |= CLOCAL;
285 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
287 error = ttydevsw_open(tp);
292 tty_watermarks(tp); /* XXXGL: drops lock */
295 /* Wait for Carrier Detect. */
296 if ((oflags & O_NONBLOCK) == 0 &&
297 (tp->t_termios.c_cflag & CLOCAL) == 0) {
298 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
299 error = tty_wait(tp, &tp->t_dcdwait);
305 if (dev == dev_console)
306 tp->t_flags |= TF_OPENED_CONS;
307 else if (TTY_CALLOUT(tp, dev))
308 tp->t_flags |= TF_OPENED_OUT;
310 tp->t_flags |= TF_OPENED_IN;
312 done: tp->t_flags &= ~TF_OPENCLOSE;
313 cv_broadcast(&tp->t_dcdwait);
320 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
322 struct tty *tp = dev->si_drv1;
327 * Don't actually close the device if it is being used as the
330 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED);
331 if (dev == dev_console)
332 tp->t_flags &= ~TF_OPENED_CONS;
334 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
336 if (tp->t_flags & TF_OPENED) {
342 * This can only be called once. The callin and the callout
343 * devices cannot be opened at the same time.
345 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
347 /* Properly wake up threads that are stuck - revoke(). */
349 tty_wakeup(tp, FREAD|FWRITE);
350 cv_broadcast(&tp->t_bgwait);
351 cv_broadcast(&tp->t_dcdwait);
359 tty_is_ctty(struct tty *tp, struct proc *p)
361 tty_lock_assert(tp, MA_OWNED);
363 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
367 tty_wait_background(struct tty *tp, struct thread *td, int sig)
369 struct proc *p = td->td_proc;
374 MPASS(sig == SIGTTIN || sig == SIGTTOU);
375 tty_lock_assert(tp, MA_OWNED);
380 * The process should only sleep, when:
381 * - This terminal is the controling terminal
382 * - Its process group is not the foreground process
384 * - The parent process isn't waiting for the child to
386 * - the signal to send to the process isn't masked
388 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
389 /* Allow the action to happen. */
394 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
395 SIGISMEMBER(td->td_sigmask, sig)) {
396 /* Only allow them in write()/ioctl(). */
398 return (sig == SIGTTOU ? 0 : EIO);
402 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
403 /* Don't allow the action to happen. */
410 * Send the signal and sleep until we're the new
411 * foreground process group.
415 ksi.ksi_code = SI_KERNEL;
420 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
423 error = tty_wait(tp, &tp->t_bgwait);
430 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
432 struct tty *tp = dev->si_drv1;
435 error = ttydev_enter(tp);
438 error = ttydisc_read(tp, uio, ioflag);
442 * The read() call should not throw an error when the device is
443 * being destroyed. Silently convert it to an EOF.
445 done: if (error == ENXIO)
451 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
453 struct tty *tp = dev->si_drv1;
456 error = ttydev_enter(tp);
460 if (tp->t_termios.c_lflag & TOSTOP) {
461 error = tty_wait_background(tp, curthread, SIGTTOU);
466 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
467 /* Allow non-blocking writes to bypass serialization. */
468 error = ttydisc_write(tp, uio, ioflag);
470 /* Serialize write() calls. */
471 while (tp->t_flags & TF_BUSY_OUT) {
472 error = tty_wait(tp, &tp->t_outserwait);
477 tp->t_flags |= TF_BUSY_OUT;
478 error = ttydisc_write(tp, uio, ioflag);
479 tp->t_flags &= ~TF_BUSY_OUT;
480 cv_signal(&tp->t_outserwait);
483 done: tty_unlock(tp);
488 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
491 struct tty *tp = dev->si_drv1;
494 error = ttydev_enter(tp);
529 #endif /* COMPAT_43TTY */
531 * If the ioctl() causes the TTY to be modified, let it
532 * wait in the background.
534 error = tty_wait_background(tp, curthread, SIGTTOU);
539 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
540 struct termios *old = &tp->t_termios;
541 struct termios *new = (struct termios *)data;
542 struct termios *lock = TTY_CALLOUT(tp, dev) ?
543 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
547 * Lock state devices. Just overwrite the values of the
548 * commands that are currently in use.
550 new->c_iflag = (old->c_iflag & lock->c_iflag) |
551 (new->c_iflag & ~lock->c_iflag);
552 new->c_oflag = (old->c_oflag & lock->c_oflag) |
553 (new->c_oflag & ~lock->c_oflag);
554 new->c_cflag = (old->c_cflag & lock->c_cflag) |
555 (new->c_cflag & ~lock->c_cflag);
556 new->c_lflag = (old->c_lflag & lock->c_lflag) |
557 (new->c_lflag & ~lock->c_lflag);
558 for (cc = 0; cc < NCCS; ++cc)
560 new->c_cc[cc] = old->c_cc[cc];
562 new->c_ispeed = old->c_ispeed;
564 new->c_ospeed = old->c_ospeed;
567 error = tty_ioctl(tp, cmd, data, fflag, td);
568 done: tty_unlock(tp);
574 ttydev_poll(struct cdev *dev, int events, struct thread *td)
576 struct tty *tp = dev->si_drv1;
577 int error, revents = 0;
579 error = ttydev_enter(tp);
581 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
583 if (events & (POLLIN|POLLRDNORM)) {
584 /* See if we can read something. */
585 if (ttydisc_read_poll(tp) > 0)
586 revents |= events & (POLLIN|POLLRDNORM);
589 if (tp->t_flags & TF_ZOMBIE) {
590 /* Hangup flag on zombie state. */
592 } else if (events & (POLLOUT|POLLWRNORM)) {
593 /* See if we can write something. */
594 if (ttydisc_write_poll(tp) > 0)
595 revents |= events & (POLLOUT|POLLWRNORM);
599 if (events & (POLLIN|POLLRDNORM))
600 selrecord(td, &tp->t_inpoll);
601 if (events & (POLLOUT|POLLWRNORM))
602 selrecord(td, &tp->t_outpoll);
611 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
612 int nprot, vm_memattr_t *memattr)
614 struct tty *tp = dev->si_drv1;
617 /* Handle mmap() through the driver. */
619 error = ttydev_enter(tp);
622 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
633 tty_kqops_read_detach(struct knote *kn)
635 struct tty *tp = kn->kn_hook;
637 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
641 tty_kqops_read_event(struct knote *kn, long hint)
643 struct tty *tp = kn->kn_hook;
645 tty_lock_assert(tp, MA_OWNED);
647 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
648 kn->kn_flags |= EV_EOF;
651 kn->kn_data = ttydisc_read_poll(tp);
652 return (kn->kn_data > 0);
657 tty_kqops_write_detach(struct knote *kn)
659 struct tty *tp = kn->kn_hook;
661 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
665 tty_kqops_write_event(struct knote *kn, long hint)
667 struct tty *tp = kn->kn_hook;
669 tty_lock_assert(tp, MA_OWNED);
672 kn->kn_flags |= EV_EOF;
675 kn->kn_data = ttydisc_write_poll(tp);
676 return (kn->kn_data > 0);
680 static struct filterops tty_kqops_read = {
682 .f_detach = tty_kqops_read_detach,
683 .f_event = tty_kqops_read_event,
685 static struct filterops tty_kqops_write = {
687 .f_detach = tty_kqops_write_detach,
688 .f_event = tty_kqops_write_event,
692 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
694 struct tty *tp = dev->si_drv1;
697 error = ttydev_enter(tp);
701 switch (kn->kn_filter) {
704 kn->kn_fop = &tty_kqops_read;
705 knlist_add(&tp->t_inpoll.si_note, kn, 1);
709 kn->kn_fop = &tty_kqops_write;
710 knlist_add(&tp->t_outpoll.si_note, kn, 1);
721 static struct cdevsw ttydev_cdevsw = {
722 .d_version = D_VERSION,
723 .d_open = ttydev_open,
724 .d_close = ttydev_close,
725 .d_read = ttydev_read,
726 .d_write = ttydev_write,
727 .d_ioctl = ttydev_ioctl,
728 .d_kqfilter = ttydev_kqfilter,
729 .d_poll = ttydev_poll,
730 .d_mmap = ttydev_mmap,
736 * Init/lock-state devices
740 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
745 while ((tp = dev->si_drv1) == NULL) {
746 error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1);
747 if (error != EWOULDBLOCK)
759 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
765 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
771 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
774 struct tty *tp = dev->si_drv1;
783 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
784 if (error != ENOIOCTL)
790 /* Obtain terminal flags through tcgetattr(). */
791 *(struct termios*)data = *(struct termios*)dev->si_drv2;
794 /* Set terminal flags through tcsetattr(). */
795 error = priv_check(td, PRIV_TTY_SETA);
798 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
801 *(int *)data = TTYDISC;
804 bzero(data, sizeof(struct winsize));
810 done: tty_unlock(tp);
814 static struct cdevsw ttyil_cdevsw = {
815 .d_version = D_VERSION,
816 .d_open = ttyil_open,
817 .d_close = ttyil_close,
818 .d_read = ttyil_rdwr,
819 .d_write = ttyil_rdwr,
820 .d_ioctl = ttyil_ioctl,
826 tty_init_termios(struct tty *tp)
828 struct termios *t = &tp->t_termios_init_in;
830 t->c_cflag = TTYDEF_CFLAG;
831 t->c_iflag = TTYDEF_IFLAG;
832 t->c_lflag = TTYDEF_LFLAG;
833 t->c_oflag = TTYDEF_OFLAG;
834 t->c_ispeed = TTYDEF_SPEED;
835 t->c_ospeed = TTYDEF_SPEED;
836 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
838 tp->t_termios_init_out = *t;
842 tty_init_console(struct tty *tp, speed_t s)
844 struct termios *ti = &tp->t_termios_init_in;
845 struct termios *to = &tp->t_termios_init_out;
848 ti->c_ispeed = ti->c_ospeed = s;
849 to->c_ispeed = to->c_ospeed = s;
852 ti->c_cflag |= CLOCAL;
853 to->c_cflag |= CLOCAL;
857 * Standard device routine implementations, mostly meant for
858 * pseudo-terminal device drivers. When a driver creates a new terminal
859 * device class, missing routines are patched.
863 ttydevsw_defopen(struct tty *tp)
870 ttydevsw_defclose(struct tty *tp)
875 ttydevsw_defoutwakeup(struct tty *tp)
878 panic("Terminal device has output, while not implemented");
882 ttydevsw_definwakeup(struct tty *tp)
887 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
894 ttydevsw_defcioctl(struct tty *tp, int unit, u_long cmd, caddr_t data, struct thread *td)
901 ttydevsw_defparam(struct tty *tp, struct termios *t)
905 * Allow the baud rate to be adjusted for pseudo-devices, but at
906 * least restrict it to 115200 to prevent excessive buffer
907 * usage. Also disallow 0, to prevent foot shooting.
909 if (t->c_ispeed < B50)
911 else if (t->c_ispeed > B115200)
912 t->c_ispeed = B115200;
913 if (t->c_ospeed < B50)
915 else if (t->c_ospeed > B115200)
916 t->c_ospeed = B115200;
923 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
926 /* Simulate a carrier to make the TTY layer happy. */
931 ttydevsw_defmmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
932 int nprot, vm_memattr_t *memattr)
939 ttydevsw_defpktnotify(struct tty *tp, char event)
944 ttydevsw_deffree(void *softc)
947 panic("Terminal device freed without a free-handler");
951 * TTY allocation and deallocation. TTY devices can be deallocated when
952 * the driver doesn't use it anymore, when the TTY isn't a session's
953 * controlling TTY and when the device node isn't opened through devfs.
957 tty_alloc(struct ttydevsw *tsw, void *sc)
960 return (tty_alloc_mutex(tsw, sc, NULL));
964 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
968 /* Make sure the driver defines all routines. */
969 #define PATCH_FUNC(x) do { \
970 if (tsw->tsw_ ## x == NULL) \
971 tsw->tsw_ ## x = ttydevsw_def ## x; \
975 PATCH_FUNC(outwakeup);
976 PATCH_FUNC(inwakeup);
982 PATCH_FUNC(pktnotify);
986 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
988 tp->t_devswsoftc = sc;
989 tp->t_flags = tsw->tsw_flags;
991 tty_init_termios(tp);
993 cv_init(&tp->t_inwait, "ttyin");
994 cv_init(&tp->t_outwait, "ttyout");
995 cv_init(&tp->t_outserwait, "ttyosr");
996 cv_init(&tp->t_bgwait, "ttybg");
997 cv_init(&tp->t_dcdwait, "ttydcd");
999 /* Allow drivers to use a custom mutex to lock the TTY. */
1000 if (mutex != NULL) {
1003 tp->t_mtx = &tp->t_mtxobj;
1004 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1007 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1008 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1014 tty_dealloc(void *arg)
1016 struct tty *tp = arg;
1018 /* Make sure we haven't leaked buffers. */
1019 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
1020 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
1022 seldrain(&tp->t_inpoll);
1023 seldrain(&tp->t_outpoll);
1024 knlist_destroy(&tp->t_inpoll.si_note);
1025 knlist_destroy(&tp->t_outpoll.si_note);
1027 cv_destroy(&tp->t_inwait);
1028 cv_destroy(&tp->t_outwait);
1029 cv_destroy(&tp->t_bgwait);
1030 cv_destroy(&tp->t_dcdwait);
1031 cv_destroy(&tp->t_outserwait);
1033 if (tp->t_mtx == &tp->t_mtxobj)
1034 mtx_destroy(&tp->t_mtxobj);
1040 tty_rel_free(struct tty *tp)
1044 tty_lock_assert(tp, MA_OWNED);
1046 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1047 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1048 /* TTY is still in use. */
1053 /* TTY can be deallocated. */
1059 sx_xlock(&tty_list_sx);
1060 TAILQ_REMOVE(&tty_list, tp, t_list);
1062 sx_xunlock(&tty_list_sx);
1063 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1068 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1070 MPASS(tp->t_sessioncnt > 0);
1071 tty_lock_assert(tp, MA_OWNED);
1073 if (tp->t_pgrp == pg)
1080 tty_rel_sess(struct tty *tp, struct session *sess)
1082 MPASS(tp->t_sessioncnt > 0);
1084 /* Current session has left. */
1085 if (tp->t_session == sess) {
1086 tp->t_session = NULL;
1087 MPASS(tp->t_pgrp == NULL);
1094 tty_rel_gone(struct tty *tp)
1096 MPASS(!tty_gone(tp));
1098 /* Simulate carrier removal. */
1099 ttydisc_modem(tp, 0);
1101 /* Wake up all blocked threads. */
1102 tty_wakeup(tp, FREAD|FWRITE);
1103 cv_broadcast(&tp->t_bgwait);
1104 cv_broadcast(&tp->t_dcdwait);
1106 tp->t_flags |= TF_GONE;
1111 * Exposing information about current TTY's through sysctl
1115 tty_to_xtty(struct tty *tp, struct xtty *xt)
1117 tty_lock_assert(tp, MA_OWNED);
1119 xt->xt_size = sizeof(struct xtty);
1120 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1121 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1122 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1123 xt->xt_inlow = tp->t_inlow;
1124 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1125 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1126 xt->xt_outlow = tp->t_outlow;
1127 xt->xt_column = tp->t_column;
1128 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1129 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1130 xt->xt_flags = tp->t_flags;
1131 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1135 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1137 unsigned long lsize;
1138 struct xtty *xtlist, *xt;
1142 sx_slock(&tty_list_sx);
1143 lsize = tty_list_count * sizeof(struct xtty);
1145 sx_sunlock(&tty_list_sx);
1149 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1151 TAILQ_FOREACH(tp, &tty_list, t_list) {
1153 tty_to_xtty(tp, xt);
1157 sx_sunlock(&tty_list_sx);
1159 error = SYSCTL_OUT(req, xtlist, lsize);
1160 free(xtlist, M_TTY);
1164 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1165 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1168 * Device node creation. Device has been set up, now we can expose it to
1173 tty_vmakedevf(struct tty *tp, struct ucred *cred, int flags,
1174 const char *fmt, va_list ap)
1176 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1177 const char *prefix = "tty";
1178 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1184 /* Remove "tty" prefix from devices like PTY's. */
1185 if (tp->t_flags & TF_NOPREFIX)
1188 vsnrprintf(name, sizeof name, 32, fmt, ap);
1191 /* System device. */
1194 mode = S_IRUSR|S_IWUSR;
1197 uid = cred->cr_ruid;
1199 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1202 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1203 flags |= MAKEDEV_CHECKNAME;
1205 /* Master call-in device. */
1206 error = make_dev_p(flags, &dev, &ttydev_cdevsw, cred, uid, gid, mode,
1207 "%s%s", prefix, name);
1211 wakeup(&dev->si_drv1);
1214 init = lock = cua = cinit = clock = NULL;
1216 /* Slave call-in devices. */
1217 if (tp->t_flags & TF_INITLOCK) {
1218 error = make_dev_p(flags, &init, &ttyil_cdevsw, cred, uid,
1219 gid, mode, "%s%s.init", prefix, name);
1222 dev_depends(dev, init);
1223 dev2unit(init) = TTYUNIT_INIT;
1225 wakeup(&init->si_drv1);
1226 init->si_drv2 = &tp->t_termios_init_in;
1228 error = make_dev_p(flags, &lock, &ttyil_cdevsw, cred, uid,
1229 gid, mode, "%s%s.lock", prefix, name);
1232 dev_depends(dev, lock);
1233 dev2unit(lock) = TTYUNIT_LOCK;
1235 wakeup(&lock->si_drv1);
1236 lock->si_drv2 = &tp->t_termios_lock_in;
1239 /* Call-out devices. */
1240 if (tp->t_flags & TF_CALLOUT) {
1241 error = make_dev_p(flags, &cua, &ttydev_cdevsw, cred,
1242 UID_UUCP, GID_DIALER, 0660, "cua%s", name);
1245 dev_depends(dev, cua);
1246 dev2unit(cua) = TTYUNIT_CALLOUT;
1248 wakeup(&cua->si_drv1);
1250 /* Slave call-out devices. */
1251 if (tp->t_flags & TF_INITLOCK) {
1252 error = make_dev_p(flags, &cinit, &ttyil_cdevsw, cred,
1253 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name);
1256 dev_depends(dev, cinit);
1257 dev2unit(cinit) = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1258 cinit->si_drv1 = tp;
1259 wakeup(&cinit->si_drv1);
1260 cinit->si_drv2 = &tp->t_termios_init_out;
1262 error = make_dev_p(flags, &clock, &ttyil_cdevsw, cred,
1263 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name);
1266 dev_depends(dev, clock);
1267 dev2unit(clock) = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1268 clock->si_drv1 = tp;
1269 wakeup(&clock->si_drv1);
1270 clock->si_drv2 = &tp->t_termios_lock_out;
1274 sx_xlock(&tty_list_sx);
1275 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1277 sx_xunlock(&tty_list_sx);
1296 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1297 const char *fmt, ...)
1303 error = tty_vmakedevf(tp, cred, flags, fmt, ap);
1310 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
1315 (void) tty_vmakedevf(tp, cred, 0, fmt, ap);
1320 * Signalling processes.
1324 tty_signal_sessleader(struct tty *tp, int sig)
1328 tty_lock_assert(tp, MA_OWNED);
1329 MPASS(sig >= 1 && sig < NSIG);
1331 /* Make signals start output again. */
1332 tp->t_flags &= ~TF_STOPPED;
1334 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1335 p = tp->t_session->s_leader;
1337 kern_psignal(p, sig);
1343 tty_signal_pgrp(struct tty *tp, int sig)
1347 tty_lock_assert(tp, MA_OWNED);
1348 MPASS(sig >= 1 && sig < NSIG);
1350 /* Make signals start output again. */
1351 tp->t_flags &= ~TF_STOPPED;
1353 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1355 if (tp->t_pgrp != NULL) {
1356 ksiginfo_init(&ksi);
1357 ksi.ksi_signo = sig;
1358 ksi.ksi_code = SI_KERNEL;
1359 PGRP_LOCK(tp->t_pgrp);
1360 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1361 PGRP_UNLOCK(tp->t_pgrp);
1366 tty_wakeup(struct tty *tp, int flags)
1368 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1369 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1371 if (flags & FWRITE) {
1372 cv_broadcast(&tp->t_outwait);
1373 selwakeup(&tp->t_outpoll);
1374 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1376 if (flags & FREAD) {
1377 cv_broadcast(&tp->t_inwait);
1378 selwakeup(&tp->t_inpoll);
1379 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1384 tty_wait(struct tty *tp, struct cv *cv)
1387 int revokecnt = tp->t_revokecnt;
1389 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1390 MPASS(!tty_gone(tp));
1392 error = cv_wait_sig(cv, tp->t_mtx);
1394 /* Restart the system call when we may have been revoked. */
1395 if (tp->t_revokecnt != revokecnt)
1398 /* Bail out when the device slipped away. */
1406 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1409 int revokecnt = tp->t_revokecnt;
1411 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1412 MPASS(!tty_gone(tp));
1414 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1416 /* Restart the system call when we may have been revoked. */
1417 if (tp->t_revokecnt != revokecnt)
1420 /* Bail out when the device slipped away. */
1428 tty_flush(struct tty *tp, int flags)
1430 if (flags & FWRITE) {
1431 tp->t_flags &= ~TF_HIWAT_OUT;
1432 ttyoutq_flush(&tp->t_outq);
1433 tty_wakeup(tp, FWRITE);
1434 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1436 if (flags & FREAD) {
1437 tty_hiwat_in_unblock(tp);
1438 ttyinq_flush(&tp->t_inq);
1439 ttydevsw_inwakeup(tp);
1440 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1445 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1448 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1450 tp->t_winsize = *wsz;
1451 tty_signal_pgrp(tp, SIGWINCH);
1455 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1463 * The SER_* and TIOCM_* flags are the same, but one bit
1464 * shifted. I don't know why.
1467 ttydevsw_modem(tp, SER_DTR, 0);
1470 ttydevsw_modem(tp, 0, SER_DTR);
1473 int bits = *(int *)data;
1475 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1476 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1480 int bits = *(int *)data;
1481 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1485 int bits = *(int *)data;
1486 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1490 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1495 tp->t_flags |= TF_ASYNC;
1497 tp->t_flags &= ~TF_ASYNC;
1500 /* This device supports non-blocking operation. */
1503 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1507 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1510 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1511 /* Not allowed to set ownership. */
1514 /* Temporarily unlock the TTY to set ownership. */
1516 error = fsetown(*(int *)data, &tp->t_sigio);
1520 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1521 /* Not allowed to set ownership. */
1524 /* Get ownership. */
1525 *(int *)data = fgetown(&tp->t_sigio);
1528 /* Obtain terminal flags through tcgetattr(). */
1529 *(struct termios*)data = tp->t_termios;
1534 struct termios *t = data;
1537 * Who makes up these funny rules? According to POSIX,
1538 * input baud rate is set equal to the output baud rate
1541 if (t->c_ispeed == 0)
1542 t->c_ispeed = t->c_ospeed;
1544 /* Discard any unsupported bits. */
1545 t->c_iflag &= TTYSUP_IFLAG;
1546 t->c_oflag &= TTYSUP_OFLAG;
1547 t->c_lflag &= TTYSUP_LFLAG;
1548 t->c_cflag &= TTYSUP_CFLAG;
1550 /* Set terminal flags through tcsetattr(). */
1551 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1552 error = tty_drain(tp);
1555 if (cmd == TIOCSETAF)
1556 tty_flush(tp, FREAD);
1560 * Only call param() when the flags really change.
1562 if ((t->c_cflag & CIGNORE) == 0 &&
1563 (tp->t_termios.c_cflag != t->c_cflag ||
1564 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1565 (IXON|IXOFF|IXANY)) ||
1566 tp->t_termios.c_ispeed != t->c_ispeed ||
1567 tp->t_termios.c_ospeed != t->c_ospeed)) {
1568 error = ttydevsw_param(tp, t);
1574 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1575 tp->t_termios.c_ispeed = t->c_ispeed;
1576 tp->t_termios.c_ospeed = t->c_ospeed;
1578 /* Baud rate has changed - update watermarks. */
1582 /* Copy new non-device driver parameters. */
1583 tp->t_termios.c_iflag = t->c_iflag;
1584 tp->t_termios.c_oflag = t->c_oflag;
1585 tp->t_termios.c_lflag = t->c_lflag;
1586 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1588 ttydisc_optimize(tp);
1590 if ((t->c_lflag & ICANON) == 0) {
1592 * When in non-canonical mode, wake up all
1593 * readers. Canonicalize any partial input. VMIN
1594 * and VTIME could also be adjusted.
1596 ttyinq_canonicalize(&tp->t_inq);
1597 tty_wakeup(tp, FREAD);
1601 * For packet mode: notify the PTY consumer that VSTOP
1602 * and VSTART may have been changed.
1604 if (tp->t_termios.c_iflag & IXON &&
1605 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1606 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1607 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1609 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1613 /* For compatibility - we only support TTYDISC. */
1614 *(int *)data = TTYDISC;
1617 if (!tty_is_ctty(tp, td->td_proc))
1620 if (tp->t_pgrp != NULL)
1621 *(int *)data = tp->t_pgrp->pg_id;
1623 *(int *)data = NO_PID;
1626 if (!tty_is_ctty(tp, td->td_proc))
1629 MPASS(tp->t_session);
1630 *(int *)data = tp->t_session->s_sid;
1633 struct proc *p = td->td_proc;
1635 /* XXX: This looks awful. */
1637 sx_xlock(&proctree_lock);
1640 if (!SESS_LEADER(p)) {
1641 /* Only the session leader may do this. */
1642 sx_xunlock(&proctree_lock);
1646 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1647 /* This is already our controlling TTY. */
1648 sx_xunlock(&proctree_lock);
1652 if (p->p_session->s_ttyp != NULL ||
1653 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1654 tp->t_session->s_ttyvp->v_type != VBAD)) {
1656 * There is already a relation between a TTY and
1657 * a session, or the caller is not the session
1660 * Allow the TTY to be stolen when the vnode is
1661 * invalid, but the reference to the TTY is
1662 * still active. This allows immediate reuse of
1663 * TTYs of which the session leader has been
1664 * killed or the TTY revoked.
1666 sx_xunlock(&proctree_lock);
1670 /* Connect the session to the TTY. */
1671 tp->t_session = p->p_session;
1672 tp->t_session->s_ttyp = tp;
1674 sx_xunlock(&proctree_lock);
1676 /* Assign foreground process group. */
1677 tp->t_pgrp = p->p_pgrp;
1679 p->p_flag |= P_CONTROLT;
1688 * XXX: Temporarily unlock the TTY to locate the process
1689 * group. This code would be lot nicer if we would ever
1690 * decompose proctree_lock.
1693 sx_slock(&proctree_lock);
1694 pg = pgfind(*(int *)data);
1697 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1698 sx_sunlock(&proctree_lock);
1705 * Determine if this TTY is the controlling TTY after
1706 * relocking the TTY.
1708 if (!tty_is_ctty(tp, td->td_proc)) {
1709 sx_sunlock(&proctree_lock);
1713 sx_sunlock(&proctree_lock);
1715 /* Wake up the background process groups. */
1716 cv_broadcast(&tp->t_bgwait);
1720 int flags = *(int *)data;
1723 flags = (FREAD|FWRITE);
1725 flags &= (FREAD|FWRITE);
1726 tty_flush(tp, flags);
1730 /* Drain TTY output. */
1731 return tty_drain(tp);
1733 /* Set terminal as console TTY. */
1735 error = priv_check(td, PRIV_TTY_CONSOLE);
1740 * XXX: constty should really need to be locked!
1741 * XXX: allow disconnected constty's to be stolen!
1746 if (constty != NULL)
1752 } else if (constty == tp) {
1757 /* Obtain window size. */
1758 *(struct winsize*)data = tp->t_winsize;
1761 /* Set window size. */
1762 tty_set_winsize(tp, data);
1765 tp->t_flags |= TF_EXCLUDE;
1768 tp->t_flags &= ~TF_EXCLUDE;
1771 tp->t_flags |= TF_STOPPED;
1772 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1775 tp->t_flags &= ~TF_STOPPED;
1776 ttydevsw_outwakeup(tp);
1777 ttydevsw_pktnotify(tp, TIOCPKT_START);
1783 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1785 if (!tty_is_ctty(tp, td->td_proc) &&
1786 priv_check(td, PRIV_TTY_STI))
1788 ttydisc_rint(tp, *(char *)data, 0);
1789 ttydisc_rint_done(tp);
1794 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1795 #else /* !COMPAT_43TTY */
1797 #endif /* COMPAT_43TTY */
1801 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1805 tty_lock_assert(tp, MA_OWNED);
1810 error = ttydevsw_ioctl(tp, cmd, data, td);
1811 if (error == ENOIOCTL)
1812 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1818 tty_udev(struct tty *tp)
1821 return dev2udev(tp->t_dev);
1827 tty_checkoutq(struct tty *tp)
1830 /* 256 bytes should be enough to print a log message. */
1831 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1835 tty_hiwat_in_block(struct tty *tp)
1838 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1839 tp->t_termios.c_iflag & IXOFF &&
1840 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1842 * Input flow control. Only enter the high watermark when we
1843 * can successfully store the VSTOP character.
1845 if (ttyoutq_write_nofrag(&tp->t_outq,
1846 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1847 tp->t_flags |= TF_HIWAT_IN;
1849 /* No input flow control. */
1850 tp->t_flags |= TF_HIWAT_IN;
1855 tty_hiwat_in_unblock(struct tty *tp)
1858 if (tp->t_flags & TF_HIWAT_IN &&
1859 tp->t_termios.c_iflag & IXOFF &&
1860 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1862 * Input flow control. Only leave the high watermark when we
1863 * can successfully store the VSTART character.
1865 if (ttyoutq_write_nofrag(&tp->t_outq,
1866 &tp->t_termios.c_cc[VSTART], 1) == 0)
1867 tp->t_flags &= ~TF_HIWAT_IN;
1869 /* No input flow control. */
1870 tp->t_flags &= ~TF_HIWAT_IN;
1874 ttydevsw_inwakeup(tp);
1878 * TTY hooks interface.
1882 ttyhook_defrint(struct tty *tp, char c, int flags)
1885 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1892 ttyhook_register(struct tty **rtp, struct proc *p, int fd,
1893 struct ttyhook *th, void *softc)
1899 struct filedesc *fdp;
1900 cap_rights_t rights;
1903 /* Validate the file descriptor. */
1905 error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1909 if (fp->f_ops == &badfileops) {
1915 * Make sure the vnode is bound to a character device.
1916 * Unlocked check for the vnode type is ok there, because we
1917 * only shall prevent calling devvn_refthread on the file that
1918 * never has been opened over a character device.
1920 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1925 /* Make sure it is a TTY. */
1926 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
1931 if (dev != fp->f_data) {
1935 if (cdp != &ttydev_cdevsw) {
1941 /* Try to attach the hook to the TTY. */
1944 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1945 if (tp->t_flags & TF_HOOK)
1948 tp->t_flags |= TF_HOOK;
1950 tp->t_hooksoftc = softc;
1954 /* Maybe we can switch into bypass mode now. */
1955 ttydisc_optimize(tp);
1957 /* Silently convert rint() calls to rint_bypass() when possible. */
1958 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1959 th->th_rint = ttyhook_defrint;
1961 done3: tty_unlock(tp);
1962 done2: dev_relthread(dev, ref);
1963 done1: fdrop(fp, curthread);
1968 ttyhook_unregister(struct tty *tp)
1971 tty_lock_assert(tp, MA_OWNED);
1972 MPASS(tp->t_flags & TF_HOOK);
1974 /* Disconnect the hook. */
1975 tp->t_flags &= ~TF_HOOK;
1978 /* Maybe we need to leave bypass mode. */
1979 ttydisc_optimize(tp);
1981 /* Maybe deallocate the TTY as well. */
1986 * /dev/console handling.
1990 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1994 /* System has no console device. */
1995 if (dev_console_filename == NULL)
1998 /* Look up corresponding TTY by device name. */
1999 sx_slock(&tty_list_sx);
2000 TAILQ_FOREACH(tp, &tty_list, t_list) {
2001 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2002 dev_console->si_drv1 = tp;
2006 sx_sunlock(&tty_list_sx);
2008 /* System console has no TTY associated. */
2009 if (dev_console->si_drv1 == NULL)
2012 return (ttydev_open(dev, oflags, devtype, td));
2016 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2021 return (ttydev_write(dev, uio, ioflag));
2025 * /dev/console is a little different than normal TTY's. When opened,
2026 * it determines which TTY to use. When data gets written to it, it
2027 * will be logged in the kernel message buffer.
2029 static struct cdevsw ttyconsdev_cdevsw = {
2030 .d_version = D_VERSION,
2031 .d_open = ttyconsdev_open,
2032 .d_close = ttydev_close,
2033 .d_read = ttydev_read,
2034 .d_write = ttyconsdev_write,
2035 .d_ioctl = ttydev_ioctl,
2036 .d_kqfilter = ttydev_kqfilter,
2037 .d_poll = ttydev_poll,
2038 .d_mmap = ttydev_mmap,
2039 .d_name = "ttyconsdev",
2044 ttyconsdev_init(void *unused)
2047 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2048 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2051 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2054 ttyconsdev_select(const char *name)
2057 dev_console_filename = name;
2061 * Debugging routines.
2064 #include "opt_ddb.h"
2066 #include <ddb/ddb.h>
2067 #include <ddb/db_sym.h>
2074 { TF_NOPREFIX, 'N' },
2076 { TF_INITLOCK, 'I' },
2077 { TF_CALLOUT, 'C' },
2079 /* Keep these together -> 'Oi' and 'Oo'. */
2081 { TF_OPENED_IN, 'i' },
2082 { TF_OPENED_OUT, 'o' },
2083 { TF_OPENED_CONS, 'c' },
2086 { TF_OPENCLOSE, 'B' },
2088 { TF_LITERAL, 'L' },
2090 /* Keep these together -> 'Hi' and 'Ho'. */
2092 { TF_HIWAT_IN, 'i' },
2093 { TF_HIWAT_OUT, 'o' },
2095 { TF_STOPPED, 'S' },
2096 { TF_EXCLUDE, 'X' },
2101 /* Keep these together -> 'bi' and 'bo'. */
2103 { TF_BUSY_IN, 'i' },
2104 { TF_BUSY_OUT, 'o' },
2109 #define TTY_FLAG_BITS \
2110 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
2111 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
2112 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
2114 #define DB_PRINTSYM(name, addr) \
2115 db_printf("%s " #name ": ", sep); \
2116 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2120 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2122 db_printf("%sdevsw: ", sep);
2123 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2124 db_printf(" (%p)\n", tsw);
2125 DB_PRINTSYM(open, tsw->tsw_open);
2126 DB_PRINTSYM(close, tsw->tsw_close);
2127 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2128 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2129 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2130 DB_PRINTSYM(param, tsw->tsw_param);
2131 DB_PRINTSYM(modem, tsw->tsw_modem);
2132 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2133 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2134 DB_PRINTSYM(free, tsw->tsw_free);
2137 _db_show_hooks(const char *sep, const struct ttyhook *th)
2139 db_printf("%shook: ", sep);
2140 db_printsym((db_addr_t)th, DB_STGY_ANY);
2141 db_printf(" (%p)\n", th);
2144 DB_PRINTSYM(rint, th->th_rint);
2145 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2146 DB_PRINTSYM(rint_done, th->th_rint_done);
2147 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2148 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2149 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2150 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2151 DB_PRINTSYM(close, th->th_close);
2155 _db_show_termios(const char *name, const struct termios *t)
2158 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2159 "lflag 0x%x ispeed %u ospeed %u\n", name,
2160 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2161 t->c_ispeed, t->c_ospeed);
2164 /* DDB command to show TTY statistics. */
2165 DB_SHOW_COMMAND(tty, db_show_tty)
2170 db_printf("usage: show tty <addr>\n");
2173 tp = (struct tty *)addr;
2175 db_printf("0x%p: %s\n", tp, tty_devname(tp));
2176 db_printf("\tmtx: %p\n", tp->t_mtx);
2177 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2178 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2180 /* Buffering mechanisms. */
2181 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2182 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2183 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2184 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2185 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2186 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2187 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2188 db_printf("\tinlow: %zu\n", tp->t_inlow);
2189 db_printf("\toutlow: %zu\n", tp->t_outlow);
2190 _db_show_termios("\ttermios", &tp->t_termios);
2191 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2192 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2193 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2194 db_printf("\tcolumn: %u\n", tp->t_column);
2195 db_printf("\twritepos: %u\n", tp->t_writepos);
2196 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2198 /* Init/lock-state devices. */
2199 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2200 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2201 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2202 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2205 _db_show_devsw("\t", tp->t_devsw);
2206 _db_show_hooks("\t", tp->t_hook);
2209 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2210 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2211 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2212 db_printf("\tsession: %p", tp->t_session);
2213 if (tp->t_session != NULL)
2214 db_printf(" count %u leader %p tty %p sid %d login %s",
2215 tp->t_session->s_count, tp->t_session->s_leader,
2216 tp->t_session->s_ttyp, tp->t_session->s_sid,
2217 tp->t_session->s_login);
2219 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2220 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2221 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2222 db_printf("\tdev: %p\n", tp->t_dev);
2225 /* DDB command to list TTYs. */
2226 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2232 /* Make the output look like `pstat -t'. */
2234 #if defined(__LP64__)
2237 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2238 "COL SESS PGID STATE\n");
2240 TAILQ_FOREACH(tp, &tty_list, t_list) {
2241 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2242 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2244 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2248 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2249 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2252 tp->t_outq.to_end - tp->t_outq.to_begin,
2253 osiz - tp->t_outlow,
2254 MIN(tp->t_column, 99999),
2255 tp->t_session ? tp->t_session->s_sid : 0,
2256 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2259 for (i = j = 0; ttystates[i].flag; i++)
2260 if (tp->t_flags & ttystates[i].flag) {
2261 db_printf("%c", ttystates[i].val);