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/capsicum.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, int leaving)
129 int error, revokecnt;
131 if (ttyhook_hashook(tp, getc_inject))
132 /* buffer is inaccessible */
135 while (ttyoutq_bytesused(&tp->t_outq) > 0) {
136 ttydevsw_outwakeup(tp);
137 /* Could be handled synchronously. */
138 bytesused = ttyoutq_bytesused(&tp->t_outq);
142 /* Wait for data to be drained. */
144 revokecnt = tp->t_revokecnt;
145 error = tty_timedwait(tp, &tp->t_outwait, hz);
148 if (revokecnt != tp->t_revokecnt)
152 if (ttyoutq_bytesused(&tp->t_outq) < bytesused)
157 error = tty_wait(tp, &tp->t_outwait);
167 * Though ttydev_enter() and ttydev_leave() seem to be related, they
168 * don't have to be used together. ttydev_enter() is used by the cdev
169 * operations to prevent an actual operation from being processed when
170 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
171 * and ttydev_close() to determine whether per-TTY data should be
176 ttydev_enter(struct tty *tp)
180 if (tty_gone(tp) || !tty_opened(tp)) {
181 /* Device is already gone. */
190 ttydev_leave(struct tty *tp)
192 tty_lock_assert(tp, MA_OWNED);
194 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
195 /* Device is still opened somewhere. */
200 tp->t_flags |= TF_OPENCLOSE;
202 /* Stop asynchronous I/O. */
203 funsetown(&tp->t_sigio);
205 /* Remove console TTY. */
209 /* Drain any output. */
210 MPASS((tp->t_flags & TF_STOPPED) == 0);
216 /* Destroy associated buffers already. */
217 ttyinq_free(&tp->t_inq);
219 ttyoutq_free(&tp->t_outq);
222 knlist_clear(&tp->t_inpoll.si_note, 1);
223 knlist_clear(&tp->t_outpoll.si_note, 1);
228 tp->t_flags &= ~TF_OPENCLOSE;
229 cv_broadcast(&tp->t_dcdwait);
234 * Operations that are exposed through the character device in /dev.
237 ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
242 while ((tp = dev->si_drv1) == NULL) {
243 error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1);
244 if (error != EWOULDBLOCK)
250 /* Device is already gone. */
256 * Block when other processes are currently opening or closing
259 while (tp->t_flags & TF_OPENCLOSE) {
260 error = tty_wait(tp, &tp->t_dcdwait);
266 tp->t_flags |= TF_OPENCLOSE;
269 * Make sure the "tty" and "cua" device cannot be opened at the
272 if (TTY_CALLOUT(tp, dev)) {
273 if (tp->t_flags & TF_OPENED_IN) {
278 if (tp->t_flags & TF_OPENED_OUT) {
284 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
289 if (!tty_opened(tp)) {
290 /* Set proper termios flags. */
291 if (TTY_CALLOUT(tp, dev))
292 tp->t_termios = tp->t_termios_init_out;
294 tp->t_termios = tp->t_termios_init_in;
295 ttydevsw_param(tp, &tp->t_termios);
296 /* Prevent modem control on callout devices and /dev/console. */
297 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
298 tp->t_termios.c_cflag |= CLOCAL;
300 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
302 error = ttydevsw_open(tp);
307 tty_watermarks(tp); /* XXXGL: drops lock */
310 /* Wait for Carrier Detect. */
311 if ((oflags & O_NONBLOCK) == 0 &&
312 (tp->t_termios.c_cflag & CLOCAL) == 0) {
313 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
314 error = tty_wait(tp, &tp->t_dcdwait);
320 if (dev == dev_console)
321 tp->t_flags |= TF_OPENED_CONS;
322 else if (TTY_CALLOUT(tp, dev))
323 tp->t_flags |= TF_OPENED_OUT;
325 tp->t_flags |= TF_OPENED_IN;
327 done: tp->t_flags &= ~TF_OPENCLOSE;
328 cv_broadcast(&tp->t_dcdwait);
335 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
337 struct tty *tp = dev->si_drv1;
342 * Don't actually close the device if it is being used as the
345 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED);
346 if (dev == dev_console)
347 tp->t_flags &= ~TF_OPENED_CONS;
349 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
351 if (tp->t_flags & TF_OPENED) {
357 * This can only be called once. The callin and the callout
358 * devices cannot be opened at the same time.
360 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
362 /* Properly wake up threads that are stuck - revoke(). */
364 tty_wakeup(tp, FREAD|FWRITE);
365 cv_broadcast(&tp->t_bgwait);
366 cv_broadcast(&tp->t_dcdwait);
374 tty_is_ctty(struct tty *tp, struct proc *p)
376 tty_lock_assert(tp, MA_OWNED);
378 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
382 tty_wait_background(struct tty *tp, struct thread *td, int sig)
384 struct proc *p = td->td_proc;
389 MPASS(sig == SIGTTIN || sig == SIGTTOU);
390 tty_lock_assert(tp, MA_OWNED);
395 * The process should only sleep, when:
396 * - This terminal is the controling terminal
397 * - Its process group is not the foreground process
399 * - The parent process isn't waiting for the child to
401 * - the signal to send to the process isn't masked
403 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
404 /* Allow the action to happen. */
409 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
410 SIGISMEMBER(td->td_sigmask, sig)) {
411 /* Only allow them in write()/ioctl(). */
413 return (sig == SIGTTOU ? 0 : EIO);
417 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
418 /* Don't allow the action to happen. */
425 * Send the signal and sleep until we're the new
426 * foreground process group.
430 ksi.ksi_code = SI_KERNEL;
435 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
438 error = tty_wait(tp, &tp->t_bgwait);
445 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
447 struct tty *tp = dev->si_drv1;
450 error = ttydev_enter(tp);
453 error = ttydisc_read(tp, uio, ioflag);
457 * The read() call should not throw an error when the device is
458 * being destroyed. Silently convert it to an EOF.
460 done: if (error == ENXIO)
466 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
468 struct tty *tp = dev->si_drv1;
471 error = ttydev_enter(tp);
475 if (tp->t_termios.c_lflag & TOSTOP) {
476 error = tty_wait_background(tp, curthread, SIGTTOU);
481 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
482 /* Allow non-blocking writes to bypass serialization. */
483 error = ttydisc_write(tp, uio, ioflag);
485 /* Serialize write() calls. */
486 while (tp->t_flags & TF_BUSY_OUT) {
487 error = tty_wait(tp, &tp->t_outserwait);
492 tp->t_flags |= TF_BUSY_OUT;
493 error = ttydisc_write(tp, uio, ioflag);
494 tp->t_flags &= ~TF_BUSY_OUT;
495 cv_signal(&tp->t_outserwait);
498 done: tty_unlock(tp);
503 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
506 struct tty *tp = dev->si_drv1;
509 error = ttydev_enter(tp);
544 #endif /* COMPAT_43TTY */
546 * If the ioctl() causes the TTY to be modified, let it
547 * wait in the background.
549 error = tty_wait_background(tp, curthread, SIGTTOU);
554 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
555 struct termios *old = &tp->t_termios;
556 struct termios *new = (struct termios *)data;
557 struct termios *lock = TTY_CALLOUT(tp, dev) ?
558 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
562 * Lock state devices. Just overwrite the values of the
563 * commands that are currently in use.
565 new->c_iflag = (old->c_iflag & lock->c_iflag) |
566 (new->c_iflag & ~lock->c_iflag);
567 new->c_oflag = (old->c_oflag & lock->c_oflag) |
568 (new->c_oflag & ~lock->c_oflag);
569 new->c_cflag = (old->c_cflag & lock->c_cflag) |
570 (new->c_cflag & ~lock->c_cflag);
571 new->c_lflag = (old->c_lflag & lock->c_lflag) |
572 (new->c_lflag & ~lock->c_lflag);
573 for (cc = 0; cc < NCCS; ++cc)
575 new->c_cc[cc] = old->c_cc[cc];
577 new->c_ispeed = old->c_ispeed;
579 new->c_ospeed = old->c_ospeed;
582 error = tty_ioctl(tp, cmd, data, fflag, td);
583 done: tty_unlock(tp);
589 ttydev_poll(struct cdev *dev, int events, struct thread *td)
591 struct tty *tp = dev->si_drv1;
592 int error, revents = 0;
594 error = ttydev_enter(tp);
596 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
598 if (events & (POLLIN|POLLRDNORM)) {
599 /* See if we can read something. */
600 if (ttydisc_read_poll(tp) > 0)
601 revents |= events & (POLLIN|POLLRDNORM);
604 if (tp->t_flags & TF_ZOMBIE) {
605 /* Hangup flag on zombie state. */
607 } else if (events & (POLLOUT|POLLWRNORM)) {
608 /* See if we can write something. */
609 if (ttydisc_write_poll(tp) > 0)
610 revents |= events & (POLLOUT|POLLWRNORM);
614 if (events & (POLLIN|POLLRDNORM))
615 selrecord(td, &tp->t_inpoll);
616 if (events & (POLLOUT|POLLWRNORM))
617 selrecord(td, &tp->t_outpoll);
626 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
627 int nprot, vm_memattr_t *memattr)
629 struct tty *tp = dev->si_drv1;
632 /* Handle mmap() through the driver. */
634 error = ttydev_enter(tp);
637 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
648 tty_kqops_read_detach(struct knote *kn)
650 struct tty *tp = kn->kn_hook;
652 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
656 tty_kqops_read_event(struct knote *kn, long hint)
658 struct tty *tp = kn->kn_hook;
660 tty_lock_assert(tp, MA_OWNED);
662 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
663 kn->kn_flags |= EV_EOF;
666 kn->kn_data = ttydisc_read_poll(tp);
667 return (kn->kn_data > 0);
672 tty_kqops_write_detach(struct knote *kn)
674 struct tty *tp = kn->kn_hook;
676 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
680 tty_kqops_write_event(struct knote *kn, long hint)
682 struct tty *tp = kn->kn_hook;
684 tty_lock_assert(tp, MA_OWNED);
687 kn->kn_flags |= EV_EOF;
690 kn->kn_data = ttydisc_write_poll(tp);
691 return (kn->kn_data > 0);
695 static struct filterops tty_kqops_read = {
697 .f_detach = tty_kqops_read_detach,
698 .f_event = tty_kqops_read_event,
700 static struct filterops tty_kqops_write = {
702 .f_detach = tty_kqops_write_detach,
703 .f_event = tty_kqops_write_event,
707 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
709 struct tty *tp = dev->si_drv1;
712 error = ttydev_enter(tp);
716 switch (kn->kn_filter) {
719 kn->kn_fop = &tty_kqops_read;
720 knlist_add(&tp->t_inpoll.si_note, kn, 1);
724 kn->kn_fop = &tty_kqops_write;
725 knlist_add(&tp->t_outpoll.si_note, kn, 1);
736 static struct cdevsw ttydev_cdevsw = {
737 .d_version = D_VERSION,
738 .d_open = ttydev_open,
739 .d_close = ttydev_close,
740 .d_read = ttydev_read,
741 .d_write = ttydev_write,
742 .d_ioctl = ttydev_ioctl,
743 .d_kqfilter = ttydev_kqfilter,
744 .d_poll = ttydev_poll,
745 .d_mmap = ttydev_mmap,
751 * Init/lock-state devices
755 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
760 while ((tp = dev->si_drv1) == NULL) {
761 error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1);
762 if (error != EWOULDBLOCK)
774 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
780 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
786 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
789 struct tty *tp = dev->si_drv1;
798 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
799 if (error != ENOIOCTL)
805 /* Obtain terminal flags through tcgetattr(). */
806 *(struct termios*)data = *(struct termios*)dev->si_drv2;
809 /* Set terminal flags through tcsetattr(). */
810 error = priv_check(td, PRIV_TTY_SETA);
813 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
816 *(int *)data = TTYDISC;
819 bzero(data, sizeof(struct winsize));
825 done: tty_unlock(tp);
829 static struct cdevsw ttyil_cdevsw = {
830 .d_version = D_VERSION,
831 .d_open = ttyil_open,
832 .d_close = ttyil_close,
833 .d_read = ttyil_rdwr,
834 .d_write = ttyil_rdwr,
835 .d_ioctl = ttyil_ioctl,
841 tty_init_termios(struct tty *tp)
843 struct termios *t = &tp->t_termios_init_in;
845 t->c_cflag = TTYDEF_CFLAG;
846 t->c_iflag = TTYDEF_IFLAG;
847 t->c_lflag = TTYDEF_LFLAG;
848 t->c_oflag = TTYDEF_OFLAG;
849 t->c_ispeed = TTYDEF_SPEED;
850 t->c_ospeed = TTYDEF_SPEED;
851 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
853 tp->t_termios_init_out = *t;
857 tty_init_console(struct tty *tp, speed_t s)
859 struct termios *ti = &tp->t_termios_init_in;
860 struct termios *to = &tp->t_termios_init_out;
863 ti->c_ispeed = ti->c_ospeed = s;
864 to->c_ispeed = to->c_ospeed = s;
867 ti->c_cflag |= CLOCAL;
868 to->c_cflag |= CLOCAL;
872 * Standard device routine implementations, mostly meant for
873 * pseudo-terminal device drivers. When a driver creates a new terminal
874 * device class, missing routines are patched.
878 ttydevsw_defopen(struct tty *tp)
885 ttydevsw_defclose(struct tty *tp)
890 ttydevsw_defoutwakeup(struct tty *tp)
893 panic("Terminal device has output, while not implemented");
897 ttydevsw_definwakeup(struct tty *tp)
902 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
909 ttydevsw_defcioctl(struct tty *tp, int unit, u_long cmd, caddr_t data, struct thread *td)
916 ttydevsw_defparam(struct tty *tp, struct termios *t)
920 * Allow the baud rate to be adjusted for pseudo-devices, but at
921 * least restrict it to 115200 to prevent excessive buffer
922 * usage. Also disallow 0, to prevent foot shooting.
924 if (t->c_ispeed < B50)
926 else if (t->c_ispeed > B115200)
927 t->c_ispeed = B115200;
928 if (t->c_ospeed < B50)
930 else if (t->c_ospeed > B115200)
931 t->c_ospeed = B115200;
938 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
941 /* Simulate a carrier to make the TTY layer happy. */
946 ttydevsw_defmmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
947 int nprot, vm_memattr_t *memattr)
954 ttydevsw_defpktnotify(struct tty *tp, char event)
959 ttydevsw_deffree(void *softc)
962 panic("Terminal device freed without a free-handler");
966 * TTY allocation and deallocation. TTY devices can be deallocated when
967 * the driver doesn't use it anymore, when the TTY isn't a session's
968 * controlling TTY and when the device node isn't opened through devfs.
972 tty_alloc(struct ttydevsw *tsw, void *sc)
975 return (tty_alloc_mutex(tsw, sc, NULL));
979 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
983 /* Make sure the driver defines all routines. */
984 #define PATCH_FUNC(x) do { \
985 if (tsw->tsw_ ## x == NULL) \
986 tsw->tsw_ ## x = ttydevsw_def ## x; \
990 PATCH_FUNC(outwakeup);
991 PATCH_FUNC(inwakeup);
997 PATCH_FUNC(pktnotify);
1001 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
1003 tp->t_devswsoftc = sc;
1004 tp->t_flags = tsw->tsw_flags;
1006 tty_init_termios(tp);
1008 cv_init(&tp->t_inwait, "ttyin");
1009 cv_init(&tp->t_outwait, "ttyout");
1010 cv_init(&tp->t_outserwait, "ttyosr");
1011 cv_init(&tp->t_bgwait, "ttybg");
1012 cv_init(&tp->t_dcdwait, "ttydcd");
1014 /* Allow drivers to use a custom mutex to lock the TTY. */
1015 if (mutex != NULL) {
1018 tp->t_mtx = &tp->t_mtxobj;
1019 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1022 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1023 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1029 tty_dealloc(void *arg)
1031 struct tty *tp = arg;
1033 /* Make sure we haven't leaked buffers. */
1034 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
1035 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
1037 seldrain(&tp->t_inpoll);
1038 seldrain(&tp->t_outpoll);
1039 knlist_destroy(&tp->t_inpoll.si_note);
1040 knlist_destroy(&tp->t_outpoll.si_note);
1042 cv_destroy(&tp->t_inwait);
1043 cv_destroy(&tp->t_outwait);
1044 cv_destroy(&tp->t_bgwait);
1045 cv_destroy(&tp->t_dcdwait);
1046 cv_destroy(&tp->t_outserwait);
1048 if (tp->t_mtx == &tp->t_mtxobj)
1049 mtx_destroy(&tp->t_mtxobj);
1055 tty_rel_free(struct tty *tp)
1059 tty_lock_assert(tp, MA_OWNED);
1061 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1062 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1063 /* TTY is still in use. */
1068 /* TTY can be deallocated. */
1074 sx_xlock(&tty_list_sx);
1075 TAILQ_REMOVE(&tty_list, tp, t_list);
1077 sx_xunlock(&tty_list_sx);
1078 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1083 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1085 MPASS(tp->t_sessioncnt > 0);
1086 tty_lock_assert(tp, MA_OWNED);
1088 if (tp->t_pgrp == pg)
1095 tty_rel_sess(struct tty *tp, struct session *sess)
1097 MPASS(tp->t_sessioncnt > 0);
1099 /* Current session has left. */
1100 if (tp->t_session == sess) {
1101 tp->t_session = NULL;
1102 MPASS(tp->t_pgrp == NULL);
1109 tty_rel_gone(struct tty *tp)
1111 MPASS(!tty_gone(tp));
1113 /* Simulate carrier removal. */
1114 ttydisc_modem(tp, 0);
1116 /* Wake up all blocked threads. */
1117 tty_wakeup(tp, FREAD|FWRITE);
1118 cv_broadcast(&tp->t_bgwait);
1119 cv_broadcast(&tp->t_dcdwait);
1121 tp->t_flags |= TF_GONE;
1126 * Exposing information about current TTY's through sysctl
1130 tty_to_xtty(struct tty *tp, struct xtty *xt)
1132 tty_lock_assert(tp, MA_OWNED);
1134 xt->xt_size = sizeof(struct xtty);
1135 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1136 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1137 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1138 xt->xt_inlow = tp->t_inlow;
1139 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1140 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1141 xt->xt_outlow = tp->t_outlow;
1142 xt->xt_column = tp->t_column;
1143 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1144 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1145 xt->xt_flags = tp->t_flags;
1146 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1150 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1152 unsigned long lsize;
1153 struct xtty *xtlist, *xt;
1157 sx_slock(&tty_list_sx);
1158 lsize = tty_list_count * sizeof(struct xtty);
1160 sx_sunlock(&tty_list_sx);
1164 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1166 TAILQ_FOREACH(tp, &tty_list, t_list) {
1168 tty_to_xtty(tp, xt);
1172 sx_sunlock(&tty_list_sx);
1174 error = SYSCTL_OUT(req, xtlist, lsize);
1175 free(xtlist, M_TTY);
1179 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1180 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1183 * Device node creation. Device has been set up, now we can expose it to
1188 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1189 const char *fmt, ...)
1192 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1193 const char *prefix = "tty";
1194 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1200 /* Remove "tty" prefix from devices like PTY's. */
1201 if (tp->t_flags & TF_NOPREFIX)
1205 vsnrprintf(name, sizeof name, 32, fmt, ap);
1209 /* System device. */
1212 mode = S_IRUSR|S_IWUSR;
1215 uid = cred->cr_ruid;
1217 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1220 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1221 flags |= MAKEDEV_CHECKNAME;
1223 /* Master call-in device. */
1224 error = make_dev_p(flags, &dev, &ttydev_cdevsw, cred, uid, gid, mode,
1225 "%s%s", prefix, name);
1229 wakeup(&dev->si_drv1);
1232 init = lock = cua = cinit = clock = NULL;
1234 /* Slave call-in devices. */
1235 if (tp->t_flags & TF_INITLOCK) {
1236 error = make_dev_p(flags, &init, &ttyil_cdevsw, cred, uid,
1237 gid, mode, "%s%s.init", prefix, name);
1240 dev_depends(dev, init);
1241 dev2unit(init) = TTYUNIT_INIT;
1243 wakeup(&init->si_drv1);
1244 init->si_drv2 = &tp->t_termios_init_in;
1246 error = make_dev_p(flags, &lock, &ttyil_cdevsw, cred, uid,
1247 gid, mode, "%s%s.lock", prefix, name);
1250 dev_depends(dev, lock);
1251 dev2unit(lock) = TTYUNIT_LOCK;
1253 wakeup(&lock->si_drv1);
1254 lock->si_drv2 = &tp->t_termios_lock_in;
1257 /* Call-out devices. */
1258 if (tp->t_flags & TF_CALLOUT) {
1259 error = make_dev_p(flags, &cua, &ttydev_cdevsw, cred,
1260 UID_UUCP, GID_DIALER, 0660, "cua%s", name);
1263 dev_depends(dev, cua);
1264 dev2unit(cua) = TTYUNIT_CALLOUT;
1266 wakeup(&cua->si_drv1);
1268 /* Slave call-out devices. */
1269 if (tp->t_flags & TF_INITLOCK) {
1270 error = make_dev_p(flags, &cinit, &ttyil_cdevsw, cred,
1271 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name);
1274 dev_depends(dev, cinit);
1275 dev2unit(cinit) = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1276 cinit->si_drv1 = tp;
1277 wakeup(&cinit->si_drv1);
1278 cinit->si_drv2 = &tp->t_termios_init_out;
1280 error = make_dev_p(flags, &clock, &ttyil_cdevsw, cred,
1281 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name);
1284 dev_depends(dev, clock);
1285 dev2unit(clock) = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1286 clock->si_drv1 = tp;
1287 wakeup(&clock->si_drv1);
1288 clock->si_drv2 = &tp->t_termios_lock_out;
1292 sx_xlock(&tty_list_sx);
1293 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1295 sx_xunlock(&tty_list_sx);
1314 * Signalling processes.
1318 tty_signal_sessleader(struct tty *tp, int sig)
1322 tty_lock_assert(tp, MA_OWNED);
1323 MPASS(sig >= 1 && sig < NSIG);
1325 /* Make signals start output again. */
1326 tp->t_flags &= ~TF_STOPPED;
1328 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1329 p = tp->t_session->s_leader;
1331 kern_psignal(p, sig);
1337 tty_signal_pgrp(struct tty *tp, int sig)
1341 tty_lock_assert(tp, MA_OWNED);
1342 MPASS(sig >= 1 && sig < NSIG);
1344 /* Make signals start output again. */
1345 tp->t_flags &= ~TF_STOPPED;
1347 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1349 if (tp->t_pgrp != NULL) {
1350 ksiginfo_init(&ksi);
1351 ksi.ksi_signo = sig;
1352 ksi.ksi_code = SI_KERNEL;
1353 PGRP_LOCK(tp->t_pgrp);
1354 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1355 PGRP_UNLOCK(tp->t_pgrp);
1360 tty_wakeup(struct tty *tp, int flags)
1362 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1363 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1365 if (flags & FWRITE) {
1366 cv_broadcast(&tp->t_outwait);
1367 selwakeup(&tp->t_outpoll);
1368 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1370 if (flags & FREAD) {
1371 cv_broadcast(&tp->t_inwait);
1372 selwakeup(&tp->t_inpoll);
1373 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1378 tty_wait(struct tty *tp, struct cv *cv)
1381 int revokecnt = tp->t_revokecnt;
1383 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1384 MPASS(!tty_gone(tp));
1386 error = cv_wait_sig(cv, tp->t_mtx);
1388 /* Bail out when the device slipped away. */
1392 /* Restart the system call when we may have been revoked. */
1393 if (tp->t_revokecnt != revokecnt)
1400 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1403 int revokecnt = tp->t_revokecnt;
1405 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1406 MPASS(!tty_gone(tp));
1408 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1410 /* Bail out when the device slipped away. */
1414 /* Restart the system call when we may have been revoked. */
1415 if (tp->t_revokecnt != revokecnt)
1422 tty_flush(struct tty *tp, int flags)
1424 if (flags & FWRITE) {
1425 tp->t_flags &= ~TF_HIWAT_OUT;
1426 ttyoutq_flush(&tp->t_outq);
1427 tty_wakeup(tp, FWRITE);
1428 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1430 if (flags & FREAD) {
1431 tty_hiwat_in_unblock(tp);
1432 ttyinq_flush(&tp->t_inq);
1433 ttydevsw_inwakeup(tp);
1434 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1439 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1442 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1444 tp->t_winsize = *wsz;
1445 tty_signal_pgrp(tp, SIGWINCH);
1449 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1457 * The SER_* and TIOCM_* flags are the same, but one bit
1458 * shifted. I don't know why.
1461 ttydevsw_modem(tp, SER_DTR, 0);
1464 ttydevsw_modem(tp, 0, SER_DTR);
1467 int bits = *(int *)data;
1469 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1470 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1474 int bits = *(int *)data;
1475 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1479 int bits = *(int *)data;
1480 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1484 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1489 tp->t_flags |= TF_ASYNC;
1491 tp->t_flags &= ~TF_ASYNC;
1494 /* This device supports non-blocking operation. */
1497 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1501 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1504 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1505 /* Not allowed to set ownership. */
1508 /* Temporarily unlock the TTY to set ownership. */
1510 error = fsetown(*(int *)data, &tp->t_sigio);
1514 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1515 /* Not allowed to set ownership. */
1518 /* Get ownership. */
1519 *(int *)data = fgetown(&tp->t_sigio);
1522 /* Obtain terminal flags through tcgetattr(). */
1523 *(struct termios*)data = tp->t_termios;
1528 struct termios *t = data;
1531 * Who makes up these funny rules? According to POSIX,
1532 * input baud rate is set equal to the output baud rate
1535 if (t->c_ispeed == 0)
1536 t->c_ispeed = t->c_ospeed;
1538 /* Discard any unsupported bits. */
1539 t->c_iflag &= TTYSUP_IFLAG;
1540 t->c_oflag &= TTYSUP_OFLAG;
1541 t->c_lflag &= TTYSUP_LFLAG;
1542 t->c_cflag &= TTYSUP_CFLAG;
1544 /* Set terminal flags through tcsetattr(). */
1545 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1546 error = tty_drain(tp, 0);
1549 if (cmd == TIOCSETAF)
1550 tty_flush(tp, FREAD);
1554 * Only call param() when the flags really change.
1556 if ((t->c_cflag & CIGNORE) == 0 &&
1557 (tp->t_termios.c_cflag != t->c_cflag ||
1558 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1559 (IXON|IXOFF|IXANY)) ||
1560 tp->t_termios.c_ispeed != t->c_ispeed ||
1561 tp->t_termios.c_ospeed != t->c_ospeed)) {
1562 error = ttydevsw_param(tp, t);
1568 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1569 tp->t_termios.c_ispeed = t->c_ispeed;
1570 tp->t_termios.c_ospeed = t->c_ospeed;
1572 /* Baud rate has changed - update watermarks. */
1576 /* Copy new non-device driver parameters. */
1577 tp->t_termios.c_iflag = t->c_iflag;
1578 tp->t_termios.c_oflag = t->c_oflag;
1579 tp->t_termios.c_lflag = t->c_lflag;
1580 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1582 ttydisc_optimize(tp);
1584 if ((t->c_lflag & ICANON) == 0) {
1586 * When in non-canonical mode, wake up all
1587 * readers. Canonicalize any partial input. VMIN
1588 * and VTIME could also be adjusted.
1590 ttyinq_canonicalize(&tp->t_inq);
1591 tty_wakeup(tp, FREAD);
1595 * For packet mode: notify the PTY consumer that VSTOP
1596 * and VSTART may have been changed.
1598 if (tp->t_termios.c_iflag & IXON &&
1599 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1600 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1601 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1603 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1607 /* For compatibility - we only support TTYDISC. */
1608 *(int *)data = TTYDISC;
1611 if (!tty_is_ctty(tp, td->td_proc))
1614 if (tp->t_pgrp != NULL)
1615 *(int *)data = tp->t_pgrp->pg_id;
1617 *(int *)data = NO_PID;
1620 if (!tty_is_ctty(tp, td->td_proc))
1623 MPASS(tp->t_session);
1624 *(int *)data = tp->t_session->s_sid;
1627 struct proc *p = td->td_proc;
1629 /* XXX: This looks awful. */
1631 sx_xlock(&proctree_lock);
1634 if (!SESS_LEADER(p)) {
1635 /* Only the session leader may do this. */
1636 sx_xunlock(&proctree_lock);
1640 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1641 /* This is already our controlling TTY. */
1642 sx_xunlock(&proctree_lock);
1646 if (p->p_session->s_ttyp != NULL ||
1647 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1648 tp->t_session->s_ttyvp->v_type != VBAD)) {
1650 * There is already a relation between a TTY and
1651 * a session, or the caller is not the session
1654 * Allow the TTY to be stolen when the vnode is
1655 * invalid, but the reference to the TTY is
1656 * still active. This allows immediate reuse of
1657 * TTYs of which the session leader has been
1658 * killed or the TTY revoked.
1660 sx_xunlock(&proctree_lock);
1664 /* Connect the session to the TTY. */
1665 tp->t_session = p->p_session;
1666 tp->t_session->s_ttyp = tp;
1668 sx_xunlock(&proctree_lock);
1670 /* Assign foreground process group. */
1671 tp->t_pgrp = p->p_pgrp;
1673 p->p_flag |= P_CONTROLT;
1682 * XXX: Temporarily unlock the TTY to locate the process
1683 * group. This code would be lot nicer if we would ever
1684 * decompose proctree_lock.
1687 sx_slock(&proctree_lock);
1688 pg = pgfind(*(int *)data);
1691 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1692 sx_sunlock(&proctree_lock);
1699 * Determine if this TTY is the controlling TTY after
1700 * relocking the TTY.
1702 if (!tty_is_ctty(tp, td->td_proc)) {
1703 sx_sunlock(&proctree_lock);
1707 sx_sunlock(&proctree_lock);
1709 /* Wake up the background process groups. */
1710 cv_broadcast(&tp->t_bgwait);
1714 int flags = *(int *)data;
1717 flags = (FREAD|FWRITE);
1719 flags &= (FREAD|FWRITE);
1720 tty_flush(tp, flags);
1724 /* Drain TTY output. */
1725 return tty_drain(tp, 0);
1727 /* Set terminal as console TTY. */
1729 error = priv_check(td, PRIV_TTY_CONSOLE);
1734 * XXX: constty should really need to be locked!
1735 * XXX: allow disconnected constty's to be stolen!
1740 if (constty != NULL)
1746 } else if (constty == tp) {
1751 /* Obtain window size. */
1752 *(struct winsize*)data = tp->t_winsize;
1755 /* Set window size. */
1756 tty_set_winsize(tp, data);
1759 tp->t_flags |= TF_EXCLUDE;
1762 tp->t_flags &= ~TF_EXCLUDE;
1765 tp->t_flags |= TF_STOPPED;
1766 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1769 tp->t_flags &= ~TF_STOPPED;
1770 ttydevsw_outwakeup(tp);
1771 ttydevsw_pktnotify(tp, TIOCPKT_START);
1777 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1779 if (!tty_is_ctty(tp, td->td_proc) &&
1780 priv_check(td, PRIV_TTY_STI))
1782 ttydisc_rint(tp, *(char *)data, 0);
1783 ttydisc_rint_done(tp);
1788 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1789 #else /* !COMPAT_43TTY */
1791 #endif /* COMPAT_43TTY */
1795 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1799 tty_lock_assert(tp, MA_OWNED);
1804 error = ttydevsw_ioctl(tp, cmd, data, td);
1805 if (error == ENOIOCTL)
1806 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1812 tty_udev(struct tty *tp)
1815 return dev2udev(tp->t_dev);
1821 tty_checkoutq(struct tty *tp)
1824 /* 256 bytes should be enough to print a log message. */
1825 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1829 tty_hiwat_in_block(struct tty *tp)
1832 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1833 tp->t_termios.c_iflag & IXOFF &&
1834 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1836 * Input flow control. Only enter the high watermark when we
1837 * can successfully store the VSTOP character.
1839 if (ttyoutq_write_nofrag(&tp->t_outq,
1840 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1841 tp->t_flags |= TF_HIWAT_IN;
1843 /* No input flow control. */
1844 tp->t_flags |= TF_HIWAT_IN;
1849 tty_hiwat_in_unblock(struct tty *tp)
1852 if (tp->t_flags & TF_HIWAT_IN &&
1853 tp->t_termios.c_iflag & IXOFF &&
1854 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1856 * Input flow control. Only leave the high watermark when we
1857 * can successfully store the VSTART character.
1859 if (ttyoutq_write_nofrag(&tp->t_outq,
1860 &tp->t_termios.c_cc[VSTART], 1) == 0)
1861 tp->t_flags &= ~TF_HIWAT_IN;
1863 /* No input flow control. */
1864 tp->t_flags &= ~TF_HIWAT_IN;
1868 ttydevsw_inwakeup(tp);
1872 * TTY hooks interface.
1876 ttyhook_defrint(struct tty *tp, char c, int flags)
1879 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1886 ttyhook_register(struct tty **rtp, struct proc *p, int fd,
1887 struct ttyhook *th, void *softc)
1893 struct filedesc *fdp;
1894 cap_rights_t rights;
1897 /* Validate the file descriptor. */
1899 error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1903 if (fp->f_ops == &badfileops) {
1909 * Make sure the vnode is bound to a character device.
1910 * Unlocked check for the vnode type is ok there, because we
1911 * only shall prevent calling devvn_refthread on the file that
1912 * never has been opened over a character device.
1914 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1919 /* Make sure it is a TTY. */
1920 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
1925 if (dev != fp->f_data) {
1929 if (cdp != &ttydev_cdevsw) {
1935 /* Try to attach the hook to the TTY. */
1938 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1939 if (tp->t_flags & TF_HOOK)
1942 tp->t_flags |= TF_HOOK;
1944 tp->t_hooksoftc = softc;
1948 /* Maybe we can switch into bypass mode now. */
1949 ttydisc_optimize(tp);
1951 /* Silently convert rint() calls to rint_bypass() when possible. */
1952 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1953 th->th_rint = ttyhook_defrint;
1955 done3: tty_unlock(tp);
1956 done2: dev_relthread(dev, ref);
1957 done1: fdrop(fp, curthread);
1962 ttyhook_unregister(struct tty *tp)
1965 tty_lock_assert(tp, MA_OWNED);
1966 MPASS(tp->t_flags & TF_HOOK);
1968 /* Disconnect the hook. */
1969 tp->t_flags &= ~TF_HOOK;
1972 /* Maybe we need to leave bypass mode. */
1973 ttydisc_optimize(tp);
1975 /* Maybe deallocate the TTY as well. */
1980 * /dev/console handling.
1984 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1988 /* System has no console device. */
1989 if (dev_console_filename == NULL)
1992 /* Look up corresponding TTY by device name. */
1993 sx_slock(&tty_list_sx);
1994 TAILQ_FOREACH(tp, &tty_list, t_list) {
1995 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
1996 dev_console->si_drv1 = tp;
2000 sx_sunlock(&tty_list_sx);
2002 /* System console has no TTY associated. */
2003 if (dev_console->si_drv1 == NULL)
2006 return (ttydev_open(dev, oflags, devtype, td));
2010 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2015 return (ttydev_write(dev, uio, ioflag));
2019 * /dev/console is a little different than normal TTY's. When opened,
2020 * it determines which TTY to use. When data gets written to it, it
2021 * will be logged in the kernel message buffer.
2023 static struct cdevsw ttyconsdev_cdevsw = {
2024 .d_version = D_VERSION,
2025 .d_open = ttyconsdev_open,
2026 .d_close = ttydev_close,
2027 .d_read = ttydev_read,
2028 .d_write = ttyconsdev_write,
2029 .d_ioctl = ttydev_ioctl,
2030 .d_kqfilter = ttydev_kqfilter,
2031 .d_poll = ttydev_poll,
2032 .d_mmap = ttydev_mmap,
2033 .d_name = "ttyconsdev",
2038 ttyconsdev_init(void *unused)
2041 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2042 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2045 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2048 ttyconsdev_select(const char *name)
2051 dev_console_filename = name;
2055 * Debugging routines.
2058 #include "opt_ddb.h"
2060 #include <ddb/ddb.h>
2061 #include <ddb/db_sym.h>
2068 { TF_NOPREFIX, 'N' },
2070 { TF_INITLOCK, 'I' },
2071 { TF_CALLOUT, 'C' },
2073 /* Keep these together -> 'Oi' and 'Oo'. */
2075 { TF_OPENED_IN, 'i' },
2076 { TF_OPENED_OUT, 'o' },
2077 { TF_OPENED_CONS, 'c' },
2080 { TF_OPENCLOSE, 'B' },
2082 { TF_LITERAL, 'L' },
2084 /* Keep these together -> 'Hi' and 'Ho'. */
2086 { TF_HIWAT_IN, 'i' },
2087 { TF_HIWAT_OUT, 'o' },
2089 { TF_STOPPED, 'S' },
2090 { TF_EXCLUDE, 'X' },
2095 /* Keep these together -> 'bi' and 'bo'. */
2097 { TF_BUSY_IN, 'i' },
2098 { TF_BUSY_OUT, 'o' },
2103 #define TTY_FLAG_BITS \
2104 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
2105 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
2106 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
2108 #define DB_PRINTSYM(name, addr) \
2109 db_printf("%s " #name ": ", sep); \
2110 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2114 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2116 db_printf("%sdevsw: ", sep);
2117 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2118 db_printf(" (%p)\n", tsw);
2119 DB_PRINTSYM(open, tsw->tsw_open);
2120 DB_PRINTSYM(close, tsw->tsw_close);
2121 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2122 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2123 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2124 DB_PRINTSYM(param, tsw->tsw_param);
2125 DB_PRINTSYM(modem, tsw->tsw_modem);
2126 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2127 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2128 DB_PRINTSYM(free, tsw->tsw_free);
2131 _db_show_hooks(const char *sep, const struct ttyhook *th)
2133 db_printf("%shook: ", sep);
2134 db_printsym((db_addr_t)th, DB_STGY_ANY);
2135 db_printf(" (%p)\n", th);
2138 DB_PRINTSYM(rint, th->th_rint);
2139 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2140 DB_PRINTSYM(rint_done, th->th_rint_done);
2141 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2142 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2143 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2144 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2145 DB_PRINTSYM(close, th->th_close);
2149 _db_show_termios(const char *name, const struct termios *t)
2152 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2153 "lflag 0x%x ispeed %u ospeed %u\n", name,
2154 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2155 t->c_ispeed, t->c_ospeed);
2158 /* DDB command to show TTY statistics. */
2159 DB_SHOW_COMMAND(tty, db_show_tty)
2164 db_printf("usage: show tty <addr>\n");
2167 tp = (struct tty *)addr;
2169 db_printf("0x%p: %s\n", tp, tty_devname(tp));
2170 db_printf("\tmtx: %p\n", tp->t_mtx);
2171 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2172 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2174 /* Buffering mechanisms. */
2175 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2176 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2177 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2178 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2179 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2180 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2181 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2182 db_printf("\tinlow: %zu\n", tp->t_inlow);
2183 db_printf("\toutlow: %zu\n", tp->t_outlow);
2184 _db_show_termios("\ttermios", &tp->t_termios);
2185 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2186 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2187 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2188 db_printf("\tcolumn: %u\n", tp->t_column);
2189 db_printf("\twritepos: %u\n", tp->t_writepos);
2190 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2192 /* Init/lock-state devices. */
2193 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2194 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2195 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2196 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2199 _db_show_devsw("\t", tp->t_devsw);
2200 _db_show_hooks("\t", tp->t_hook);
2203 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2204 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2205 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2206 db_printf("\tsession: %p", tp->t_session);
2207 if (tp->t_session != NULL)
2208 db_printf(" count %u leader %p tty %p sid %d login %s",
2209 tp->t_session->s_count, tp->t_session->s_leader,
2210 tp->t_session->s_ttyp, tp->t_session->s_sid,
2211 tp->t_session->s_login);
2213 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2214 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2215 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2216 db_printf("\tdev: %p\n", tp->t_dev);
2219 /* DDB command to list TTYs. */
2220 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2226 /* Make the output look like `pstat -t'. */
2228 #if defined(__LP64__)
2231 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2232 "COL SESS PGID STATE\n");
2234 TAILQ_FOREACH(tp, &tty_list, t_list) {
2235 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2236 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2238 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2242 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2243 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2246 tp->t_outq.to_end - tp->t_outq.to_begin,
2247 osiz - tp->t_outlow,
2248 MIN(tp->t_column, 99999),
2249 tp->t_session ? tp->t_session->s_sid : 0,
2250 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2253 for (i = j = 0; ttystates[i].flag; i++)
2254 if (tp->t_flags & ttystates[i].flag) {
2255 db_printf("%c", ttystates[i].val);