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 ouput 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);
199 /* Destroy associated buffers already. */
200 ttyinq_free(&tp->t_inq);
202 ttyoutq_free(&tp->t_outq);
205 knlist_clear(&tp->t_inpoll.si_note, 1);
206 knlist_clear(&tp->t_outpoll.si_note, 1);
211 tp->t_flags &= ~TF_OPENCLOSE;
212 cv_broadcast(&tp->t_dcdwait);
217 * Operations that are exposed through the character device in /dev.
220 ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
225 while ((tp = dev->si_drv1) == NULL) {
226 error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1);
227 if (error != EWOULDBLOCK)
233 /* Device is already gone. */
239 * Block when other processes are currently opening or closing
242 while (tp->t_flags & TF_OPENCLOSE) {
243 error = tty_wait(tp, &tp->t_dcdwait);
249 tp->t_flags |= TF_OPENCLOSE;
252 * Make sure the "tty" and "cua" device cannot be opened at the
255 if (TTY_CALLOUT(tp, dev)) {
256 if (tp->t_flags & TF_OPENED_IN) {
261 if (tp->t_flags & TF_OPENED_OUT) {
267 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
272 if (!tty_opened(tp)) {
273 /* Set proper termios flags. */
274 if (TTY_CALLOUT(tp, dev))
275 tp->t_termios = tp->t_termios_init_out;
277 tp->t_termios = tp->t_termios_init_in;
278 ttydevsw_param(tp, &tp->t_termios);
279 /* Prevent modem control on callout devices and /dev/console. */
280 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
281 tp->t_termios.c_cflag |= CLOCAL;
283 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
285 error = ttydevsw_open(tp);
293 /* Wait for Carrier Detect. */
294 if ((oflags & O_NONBLOCK) == 0 &&
295 (tp->t_termios.c_cflag & CLOCAL) == 0) {
296 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
297 error = tty_wait(tp, &tp->t_dcdwait);
303 if (dev == dev_console)
304 tp->t_flags |= TF_OPENED_CONS;
305 else if (TTY_CALLOUT(tp, dev))
306 tp->t_flags |= TF_OPENED_OUT;
308 tp->t_flags |= TF_OPENED_IN;
310 done: tp->t_flags &= ~TF_OPENCLOSE;
311 cv_broadcast(&tp->t_dcdwait);
318 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
320 struct tty *tp = dev->si_drv1;
325 * Don't actually close the device if it is being used as the
328 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED);
329 if (dev == dev_console)
330 tp->t_flags &= ~TF_OPENED_CONS;
332 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
334 if (tp->t_flags & TF_OPENED) {
340 * This can only be called once. The callin and the callout
341 * devices cannot be opened at the same time.
343 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
345 /* Properly wake up threads that are stuck - revoke(). */
347 tty_wakeup(tp, FREAD|FWRITE);
348 cv_broadcast(&tp->t_bgwait);
349 cv_broadcast(&tp->t_dcdwait);
357 tty_is_ctty(struct tty *tp, struct proc *p)
359 tty_lock_assert(tp, MA_OWNED);
361 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
365 tty_wait_background(struct tty *tp, struct thread *td, int sig)
367 struct proc *p = td->td_proc;
372 MPASS(sig == SIGTTIN || sig == SIGTTOU);
373 tty_lock_assert(tp, MA_OWNED);
378 * The process should only sleep, when:
379 * - This terminal is the controling terminal
380 * - Its process group is not the foreground process
382 * - The parent process isn't waiting for the child to
384 * - the signal to send to the process isn't masked
386 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
387 /* Allow the action to happen. */
392 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
393 SIGISMEMBER(td->td_sigmask, sig)) {
394 /* Only allow them in write()/ioctl(). */
396 return (sig == SIGTTOU ? 0 : EIO);
400 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
401 /* Don't allow the action to happen. */
408 * Send the signal and sleep until we're the new
409 * foreground process group.
413 ksi.ksi_code = SI_KERNEL;
418 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
421 error = tty_wait(tp, &tp->t_bgwait);
428 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
430 struct tty *tp = dev->si_drv1;
433 error = ttydev_enter(tp);
437 error = tty_wait_background(tp, curthread, SIGTTIN);
443 error = ttydisc_read(tp, uio, ioflag);
447 * The read() call should not throw an error when the device is
448 * being destroyed. Silently convert it to an EOF.
450 done: if (error == ENXIO)
456 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
458 struct tty *tp = dev->si_drv1;
461 error = ttydev_enter(tp);
465 if (tp->t_termios.c_lflag & TOSTOP) {
466 error = tty_wait_background(tp, curthread, SIGTTOU);
471 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
472 /* Allow non-blocking writes to bypass serialization. */
473 error = ttydisc_write(tp, uio, ioflag);
475 /* Serialize write() calls. */
476 while (tp->t_flags & TF_BUSY_OUT) {
477 error = tty_wait(tp, &tp->t_outserwait);
482 tp->t_flags |= TF_BUSY_OUT;
483 error = ttydisc_write(tp, uio, ioflag);
484 tp->t_flags &= ~TF_BUSY_OUT;
485 cv_signal(&tp->t_outserwait);
488 done: tty_unlock(tp);
493 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
496 struct tty *tp = dev->si_drv1;
499 error = ttydev_enter(tp);
534 #endif /* COMPAT_43TTY */
536 * If the ioctl() causes the TTY to be modified, let it
537 * wait in the background.
539 error = tty_wait_background(tp, curthread, SIGTTOU);
544 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
545 struct termios *old = &tp->t_termios;
546 struct termios *new = (struct termios *)data;
547 struct termios *lock = TTY_CALLOUT(tp, dev) ?
548 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
552 * Lock state devices. Just overwrite the values of the
553 * commands that are currently in use.
555 new->c_iflag = (old->c_iflag & lock->c_iflag) |
556 (new->c_iflag & ~lock->c_iflag);
557 new->c_oflag = (old->c_oflag & lock->c_oflag) |
558 (new->c_oflag & ~lock->c_oflag);
559 new->c_cflag = (old->c_cflag & lock->c_cflag) |
560 (new->c_cflag & ~lock->c_cflag);
561 new->c_lflag = (old->c_lflag & lock->c_lflag) |
562 (new->c_lflag & ~lock->c_lflag);
563 for (cc = 0; cc < NCCS; ++cc)
565 new->c_cc[cc] = old->c_cc[cc];
567 new->c_ispeed = old->c_ispeed;
569 new->c_ospeed = old->c_ospeed;
572 error = tty_ioctl(tp, cmd, data, fflag, td);
573 done: tty_unlock(tp);
579 ttydev_poll(struct cdev *dev, int events, struct thread *td)
581 struct tty *tp = dev->si_drv1;
582 int error, revents = 0;
584 error = ttydev_enter(tp);
586 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
588 if (events & (POLLIN|POLLRDNORM)) {
589 /* See if we can read something. */
590 if (ttydisc_read_poll(tp) > 0)
591 revents |= events & (POLLIN|POLLRDNORM);
594 if (tp->t_flags & TF_ZOMBIE) {
595 /* Hangup flag on zombie state. */
597 } else if (events & (POLLOUT|POLLWRNORM)) {
598 /* See if we can write something. */
599 if (ttydisc_write_poll(tp) > 0)
600 revents |= events & (POLLOUT|POLLWRNORM);
604 if (events & (POLLIN|POLLRDNORM))
605 selrecord(td, &tp->t_inpoll);
606 if (events & (POLLOUT|POLLWRNORM))
607 selrecord(td, &tp->t_outpoll);
616 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
617 int nprot, vm_memattr_t *memattr)
619 struct tty *tp = dev->si_drv1;
622 /* Handle mmap() through the driver. */
624 error = ttydev_enter(tp);
627 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
638 tty_kqops_read_detach(struct knote *kn)
640 struct tty *tp = kn->kn_hook;
642 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
646 tty_kqops_read_event(struct knote *kn, long hint)
648 struct tty *tp = kn->kn_hook;
650 tty_lock_assert(tp, MA_OWNED);
652 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
653 kn->kn_flags |= EV_EOF;
656 kn->kn_data = ttydisc_read_poll(tp);
657 return (kn->kn_data > 0);
662 tty_kqops_write_detach(struct knote *kn)
664 struct tty *tp = kn->kn_hook;
666 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
670 tty_kqops_write_event(struct knote *kn, long hint)
672 struct tty *tp = kn->kn_hook;
674 tty_lock_assert(tp, MA_OWNED);
677 kn->kn_flags |= EV_EOF;
680 kn->kn_data = ttydisc_write_poll(tp);
681 return (kn->kn_data > 0);
685 static struct filterops tty_kqops_read = {
687 .f_detach = tty_kqops_read_detach,
688 .f_event = tty_kqops_read_event,
690 static struct filterops tty_kqops_write = {
692 .f_detach = tty_kqops_write_detach,
693 .f_event = tty_kqops_write_event,
697 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
699 struct tty *tp = dev->si_drv1;
702 error = ttydev_enter(tp);
706 switch (kn->kn_filter) {
709 kn->kn_fop = &tty_kqops_read;
710 knlist_add(&tp->t_inpoll.si_note, kn, 1);
714 kn->kn_fop = &tty_kqops_write;
715 knlist_add(&tp->t_outpoll.si_note, kn, 1);
726 static struct cdevsw ttydev_cdevsw = {
727 .d_version = D_VERSION,
728 .d_open = ttydev_open,
729 .d_close = ttydev_close,
730 .d_read = ttydev_read,
731 .d_write = ttydev_write,
732 .d_ioctl = ttydev_ioctl,
733 .d_kqfilter = ttydev_kqfilter,
734 .d_poll = ttydev_poll,
735 .d_mmap = ttydev_mmap,
741 * Init/lock-state devices
745 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
750 while ((tp = dev->si_drv1) == NULL) {
751 error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1);
752 if (error != EWOULDBLOCK)
764 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
770 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
776 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
779 struct tty *tp = dev->si_drv1;
788 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
789 if (error != ENOIOCTL)
795 /* Obtain terminal flags through tcgetattr(). */
796 *(struct termios*)data = *(struct termios*)dev->si_drv2;
799 /* Set terminal flags through tcsetattr(). */
800 error = priv_check(td, PRIV_TTY_SETA);
803 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
806 *(int *)data = TTYDISC;
809 bzero(data, sizeof(struct winsize));
815 done: tty_unlock(tp);
819 static struct cdevsw ttyil_cdevsw = {
820 .d_version = D_VERSION,
821 .d_open = ttyil_open,
822 .d_close = ttyil_close,
823 .d_read = ttyil_rdwr,
824 .d_write = ttyil_rdwr,
825 .d_ioctl = ttyil_ioctl,
831 tty_init_termios(struct tty *tp)
833 struct termios *t = &tp->t_termios_init_in;
835 t->c_cflag = TTYDEF_CFLAG;
836 t->c_iflag = TTYDEF_IFLAG;
837 t->c_lflag = TTYDEF_LFLAG;
838 t->c_oflag = TTYDEF_OFLAG;
839 t->c_ispeed = TTYDEF_SPEED;
840 t->c_ospeed = TTYDEF_SPEED;
841 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
843 tp->t_termios_init_out = *t;
847 tty_init_console(struct tty *tp, speed_t s)
849 struct termios *ti = &tp->t_termios_init_in;
850 struct termios *to = &tp->t_termios_init_out;
853 ti->c_ispeed = ti->c_ospeed = s;
854 to->c_ispeed = to->c_ospeed = s;
857 ti->c_cflag |= CLOCAL;
858 to->c_cflag |= CLOCAL;
862 * Standard device routine implementations, mostly meant for
863 * pseudo-terminal device drivers. When a driver creates a new terminal
864 * device class, missing routines are patched.
868 ttydevsw_defopen(struct tty *tp)
875 ttydevsw_defclose(struct tty *tp)
880 ttydevsw_defoutwakeup(struct tty *tp)
883 panic("Terminal device has output, while not implemented");
887 ttydevsw_definwakeup(struct tty *tp)
892 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
899 ttydevsw_defcioctl(struct tty *tp, int unit, u_long cmd, caddr_t data, struct thread *td)
906 ttydevsw_defparam(struct tty *tp, struct termios *t)
910 * Allow the baud rate to be adjusted for pseudo-devices, but at
911 * least restrict it to 115200 to prevent excessive buffer
912 * usage. Also disallow 0, to prevent foot shooting.
914 if (t->c_ispeed < B50)
916 else if (t->c_ispeed > B115200)
917 t->c_ispeed = B115200;
918 if (t->c_ospeed < B50)
920 else if (t->c_ospeed > B115200)
921 t->c_ospeed = B115200;
928 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
931 /* Simulate a carrier to make the TTY layer happy. */
936 ttydevsw_defmmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
937 int nprot, vm_memattr_t *memattr)
944 ttydevsw_defpktnotify(struct tty *tp, char event)
949 ttydevsw_deffree(void *softc)
952 panic("Terminal device freed without a free-handler");
956 * TTY allocation and deallocation. TTY devices can be deallocated when
957 * the driver doesn't use it anymore, when the TTY isn't a session's
958 * controlling TTY and when the device node isn't opened through devfs.
962 tty_alloc(struct ttydevsw *tsw, void *sc)
965 return (tty_alloc_mutex(tsw, sc, NULL));
969 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
973 /* Make sure the driver defines all routines. */
974 #define PATCH_FUNC(x) do { \
975 if (tsw->tsw_ ## x == NULL) \
976 tsw->tsw_ ## x = ttydevsw_def ## x; \
980 PATCH_FUNC(outwakeup);
981 PATCH_FUNC(inwakeup);
987 PATCH_FUNC(pktnotify);
991 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
993 tp->t_devswsoftc = sc;
994 tp->t_flags = tsw->tsw_flags;
996 tty_init_termios(tp);
998 cv_init(&tp->t_inwait, "ttyin");
999 cv_init(&tp->t_outwait, "ttyout");
1000 cv_init(&tp->t_outserwait, "ttyosr");
1001 cv_init(&tp->t_bgwait, "ttybg");
1002 cv_init(&tp->t_dcdwait, "ttydcd");
1004 /* Allow drivers to use a custom mutex to lock the TTY. */
1005 if (mutex != NULL) {
1008 tp->t_mtx = &tp->t_mtxobj;
1009 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1012 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1013 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1015 sx_xlock(&tty_list_sx);
1016 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1018 sx_xunlock(&tty_list_sx);
1024 tty_dealloc(void *arg)
1026 struct tty *tp = arg;
1028 sx_xlock(&tty_list_sx);
1029 TAILQ_REMOVE(&tty_list, tp, t_list);
1031 sx_xunlock(&tty_list_sx);
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 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1078 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1080 MPASS(tp->t_sessioncnt > 0);
1081 tty_lock_assert(tp, MA_OWNED);
1083 if (tp->t_pgrp == pg)
1090 tty_rel_sess(struct tty *tp, struct session *sess)
1092 MPASS(tp->t_sessioncnt > 0);
1094 /* Current session has left. */
1095 if (tp->t_session == sess) {
1096 tp->t_session = NULL;
1097 MPASS(tp->t_pgrp == NULL);
1104 tty_rel_gone(struct tty *tp)
1106 MPASS(!tty_gone(tp));
1108 /* Simulate carrier removal. */
1109 ttydisc_modem(tp, 0);
1111 /* Wake up all blocked threads. */
1112 tty_wakeup(tp, FREAD|FWRITE);
1113 cv_broadcast(&tp->t_bgwait);
1114 cv_broadcast(&tp->t_dcdwait);
1116 tp->t_flags |= TF_GONE;
1121 * Exposing information about current TTY's through sysctl
1125 tty_to_xtty(struct tty *tp, struct xtty *xt)
1127 tty_lock_assert(tp, MA_OWNED);
1129 xt->xt_size = sizeof(struct xtty);
1130 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1131 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1132 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1133 xt->xt_inlow = tp->t_inlow;
1134 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1135 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1136 xt->xt_outlow = tp->t_outlow;
1137 xt->xt_column = tp->t_column;
1138 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1139 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1140 xt->xt_flags = tp->t_flags;
1141 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1145 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1147 unsigned long lsize;
1148 struct xtty *xtlist, *xt;
1152 sx_slock(&tty_list_sx);
1153 lsize = tty_list_count * sizeof(struct xtty);
1155 sx_sunlock(&tty_list_sx);
1159 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1161 TAILQ_FOREACH(tp, &tty_list, t_list) {
1163 tty_to_xtty(tp, xt);
1167 sx_sunlock(&tty_list_sx);
1169 error = SYSCTL_OUT(req, xtlist, lsize);
1170 free(xtlist, M_TTY);
1174 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1175 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1178 * Device node creation. Device has been set up, now we can expose it to
1183 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
1187 const char *prefix = "tty";
1188 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1193 /* Remove "tty" prefix from devices like PTY's. */
1194 if (tp->t_flags & TF_NOPREFIX)
1198 vsnrprintf(name, sizeof name, 32, fmt, ap);
1202 /* System device. */
1205 mode = S_IRUSR|S_IWUSR;
1208 uid = cred->cr_ruid;
1210 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1213 /* Master call-in device. */
1214 dev = make_dev_cred(&ttydev_cdevsw, 0, cred,
1215 uid, gid, mode, "%s%s", prefix, name);
1217 wakeup(&dev->si_drv1);
1220 /* Slave call-in devices. */
1221 if (tp->t_flags & TF_INITLOCK) {
1222 dev = make_dev_cred(&ttyil_cdevsw, TTYUNIT_INIT, cred,
1223 uid, gid, mode, "%s%s.init", prefix, name);
1224 dev_depends(tp->t_dev, dev);
1226 wakeup(&dev->si_drv1);
1227 dev->si_drv2 = &tp->t_termios_init_in;
1229 dev = make_dev_cred(&ttyil_cdevsw, TTYUNIT_LOCK, cred,
1230 uid, gid, mode, "%s%s.lock", prefix, name);
1231 dev_depends(tp->t_dev, dev);
1233 wakeup(&dev->si_drv1);
1234 dev->si_drv2 = &tp->t_termios_lock_in;
1237 /* Call-out devices. */
1238 if (tp->t_flags & TF_CALLOUT) {
1239 dev = make_dev_cred(&ttydev_cdevsw, TTYUNIT_CALLOUT, cred,
1240 UID_UUCP, GID_DIALER, 0660, "cua%s", name);
1241 dev_depends(tp->t_dev, dev);
1243 wakeup(&dev->si_drv1);
1245 /* Slave call-out devices. */
1246 if (tp->t_flags & TF_INITLOCK) {
1247 dev = make_dev_cred(&ttyil_cdevsw,
1248 TTYUNIT_CALLOUT | TTYUNIT_INIT, cred,
1249 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name);
1250 dev_depends(tp->t_dev, dev);
1252 wakeup(&dev->si_drv1);
1253 dev->si_drv2 = &tp->t_termios_init_out;
1255 dev = make_dev_cred(&ttyil_cdevsw,
1256 TTYUNIT_CALLOUT | TTYUNIT_LOCK, cred,
1257 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name);
1258 dev_depends(tp->t_dev, dev);
1260 wakeup(&dev->si_drv1);
1261 dev->si_drv2 = &tp->t_termios_lock_out;
1267 * Signalling processes.
1271 tty_signal_sessleader(struct tty *tp, int sig)
1275 tty_lock_assert(tp, MA_OWNED);
1276 MPASS(sig >= 1 && sig < NSIG);
1278 /* Make signals start output again. */
1279 tp->t_flags &= ~TF_STOPPED;
1281 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1282 p = tp->t_session->s_leader;
1284 kern_psignal(p, sig);
1290 tty_signal_pgrp(struct tty *tp, int sig)
1294 tty_lock_assert(tp, MA_OWNED);
1295 MPASS(sig >= 1 && sig < NSIG);
1297 /* Make signals start output again. */
1298 tp->t_flags &= ~TF_STOPPED;
1300 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1302 if (tp->t_pgrp != NULL) {
1303 ksiginfo_init(&ksi);
1304 ksi.ksi_signo = sig;
1305 ksi.ksi_code = SI_KERNEL;
1306 PGRP_LOCK(tp->t_pgrp);
1307 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1308 PGRP_UNLOCK(tp->t_pgrp);
1313 tty_wakeup(struct tty *tp, int flags)
1315 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1316 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1318 if (flags & FWRITE) {
1319 cv_broadcast(&tp->t_outwait);
1320 selwakeup(&tp->t_outpoll);
1321 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1323 if (flags & FREAD) {
1324 cv_broadcast(&tp->t_inwait);
1325 selwakeup(&tp->t_inpoll);
1326 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1331 tty_wait(struct tty *tp, struct cv *cv)
1334 int revokecnt = tp->t_revokecnt;
1336 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1337 MPASS(!tty_gone(tp));
1339 error = cv_wait_sig(cv, tp->t_mtx);
1341 /* Restart the system call when we may have been revoked. */
1342 if (tp->t_revokecnt != revokecnt)
1345 /* Bail out when the device slipped away. */
1353 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1356 int revokecnt = tp->t_revokecnt;
1358 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1359 MPASS(!tty_gone(tp));
1361 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1363 /* Restart the system call when we may have been revoked. */
1364 if (tp->t_revokecnt != revokecnt)
1367 /* Bail out when the device slipped away. */
1375 tty_flush(struct tty *tp, int flags)
1377 if (flags & FWRITE) {
1378 tp->t_flags &= ~TF_HIWAT_OUT;
1379 ttyoutq_flush(&tp->t_outq);
1380 tty_wakeup(tp, FWRITE);
1381 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1383 if (flags & FREAD) {
1384 tty_hiwat_in_unblock(tp);
1385 ttyinq_flush(&tp->t_inq);
1386 ttydevsw_inwakeup(tp);
1387 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1392 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1400 * The SER_* and TIOCM_* flags are the same, but one bit
1401 * shifted. I don't know why.
1404 ttydevsw_modem(tp, SER_DTR, 0);
1407 ttydevsw_modem(tp, 0, SER_DTR);
1410 int bits = *(int *)data;
1412 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1413 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1417 int bits = *(int *)data;
1418 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1422 int bits = *(int *)data;
1423 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1427 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1432 tp->t_flags |= TF_ASYNC;
1434 tp->t_flags &= ~TF_ASYNC;
1437 /* This device supports non-blocking operation. */
1440 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1444 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1447 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1448 /* Not allowed to set ownership. */
1451 /* Temporarily unlock the TTY to set ownership. */
1453 error = fsetown(*(int *)data, &tp->t_sigio);
1457 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1458 /* Not allowed to set ownership. */
1461 /* Get ownership. */
1462 *(int *)data = fgetown(&tp->t_sigio);
1465 /* Obtain terminal flags through tcgetattr(). */
1466 *(struct termios*)data = tp->t_termios;
1471 struct termios *t = data;
1474 * Who makes up these funny rules? According to POSIX,
1475 * input baud rate is set equal to the output baud rate
1478 if (t->c_ispeed == 0)
1479 t->c_ispeed = t->c_ospeed;
1481 /* Discard any unsupported bits. */
1482 t->c_iflag &= TTYSUP_IFLAG;
1483 t->c_oflag &= TTYSUP_OFLAG;
1484 t->c_lflag &= TTYSUP_LFLAG;
1485 t->c_cflag &= TTYSUP_CFLAG;
1487 /* Set terminal flags through tcsetattr(). */
1488 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1489 error = tty_drain(tp);
1492 if (cmd == TIOCSETAF)
1493 tty_flush(tp, FREAD);
1497 * Only call param() when the flags really change.
1499 if ((t->c_cflag & CIGNORE) == 0 &&
1500 (tp->t_termios.c_cflag != t->c_cflag ||
1501 tp->t_termios.c_ispeed != t->c_ispeed ||
1502 tp->t_termios.c_ospeed != t->c_ospeed)) {
1503 error = ttydevsw_param(tp, t);
1509 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1510 tp->t_termios.c_ispeed = t->c_ispeed;
1511 tp->t_termios.c_ospeed = t->c_ospeed;
1513 /* Baud rate has changed - update watermarks. */
1517 /* Copy new non-device driver parameters. */
1518 tp->t_termios.c_iflag = t->c_iflag;
1519 tp->t_termios.c_oflag = t->c_oflag;
1520 tp->t_termios.c_lflag = t->c_lflag;
1521 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1523 ttydisc_optimize(tp);
1525 if ((t->c_lflag & ICANON) == 0) {
1527 * When in non-canonical mode, wake up all
1528 * readers. Canonicalize any partial input. VMIN
1529 * and VTIME could also be adjusted.
1531 ttyinq_canonicalize(&tp->t_inq);
1532 tty_wakeup(tp, FREAD);
1536 * For packet mode: notify the PTY consumer that VSTOP
1537 * and VSTART may have been changed.
1539 if (tp->t_termios.c_iflag & IXON &&
1540 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1541 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1542 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1544 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1548 /* For compatibility - we only support TTYDISC. */
1549 *(int *)data = TTYDISC;
1552 if (!tty_is_ctty(tp, td->td_proc))
1555 if (tp->t_pgrp != NULL)
1556 *(int *)data = tp->t_pgrp->pg_id;
1558 *(int *)data = NO_PID;
1561 if (!tty_is_ctty(tp, td->td_proc))
1564 MPASS(tp->t_session);
1565 *(int *)data = tp->t_session->s_sid;
1568 struct proc *p = td->td_proc;
1570 /* XXX: This looks awful. */
1572 sx_xlock(&proctree_lock);
1575 if (!SESS_LEADER(p)) {
1576 /* Only the session leader may do this. */
1577 sx_xunlock(&proctree_lock);
1581 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1582 /* This is already our controlling TTY. */
1583 sx_xunlock(&proctree_lock);
1587 if (p->p_session->s_ttyp != NULL ||
1588 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1589 tp->t_session->s_ttyvp->v_type != VBAD)) {
1591 * There is already a relation between a TTY and
1592 * a session, or the caller is not the session
1595 * Allow the TTY to be stolen when the vnode is
1596 * invalid, but the reference to the TTY is
1597 * still active. This allows immediate reuse of
1598 * TTYs of which the session leader has been
1599 * killed or the TTY revoked.
1601 sx_xunlock(&proctree_lock);
1605 /* Connect the session to the TTY. */
1606 tp->t_session = p->p_session;
1607 tp->t_session->s_ttyp = tp;
1609 sx_xunlock(&proctree_lock);
1611 /* Assign foreground process group. */
1612 tp->t_pgrp = p->p_pgrp;
1614 p->p_flag |= P_CONTROLT;
1623 * XXX: Temporarily unlock the TTY to locate the process
1624 * group. This code would be lot nicer if we would ever
1625 * decompose proctree_lock.
1628 sx_slock(&proctree_lock);
1629 pg = pgfind(*(int *)data);
1632 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1633 sx_sunlock(&proctree_lock);
1640 * Determine if this TTY is the controlling TTY after
1641 * relocking the TTY.
1643 if (!tty_is_ctty(tp, td->td_proc)) {
1644 sx_sunlock(&proctree_lock);
1648 sx_sunlock(&proctree_lock);
1650 /* Wake up the background process groups. */
1651 cv_broadcast(&tp->t_bgwait);
1655 int flags = *(int *)data;
1658 flags = (FREAD|FWRITE);
1660 flags &= (FREAD|FWRITE);
1661 tty_flush(tp, flags);
1665 /* Drain TTY output. */
1666 return tty_drain(tp);
1668 /* Set terminal as console TTY. */
1670 error = priv_check(td, PRIV_TTY_CONSOLE);
1675 * XXX: constty should really need to be locked!
1676 * XXX: allow disconnected constty's to be stolen!
1681 if (constty != NULL)
1687 } else if (constty == tp) {
1692 /* Obtain window size. */
1693 *(struct winsize*)data = tp->t_winsize;
1696 /* Set window size. */
1697 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0)
1699 tp->t_winsize = *(struct winsize*)data;
1700 tty_signal_pgrp(tp, SIGWINCH);
1703 tp->t_flags |= TF_EXCLUDE;
1706 tp->t_flags &= ~TF_EXCLUDE;
1709 tp->t_flags |= TF_STOPPED;
1710 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1713 tp->t_flags &= ~TF_STOPPED;
1714 ttydevsw_outwakeup(tp);
1715 ttydevsw_pktnotify(tp, TIOCPKT_START);
1721 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1723 if (!tty_is_ctty(tp, td->td_proc) &&
1724 priv_check(td, PRIV_TTY_STI))
1726 ttydisc_rint(tp, *(char *)data, 0);
1727 ttydisc_rint_done(tp);
1732 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1733 #else /* !COMPAT_43TTY */
1735 #endif /* COMPAT_43TTY */
1739 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1743 tty_lock_assert(tp, MA_OWNED);
1748 error = ttydevsw_ioctl(tp, cmd, data, td);
1749 if (error == ENOIOCTL)
1750 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1756 tty_udev(struct tty *tp)
1759 return dev2udev(tp->t_dev);
1765 tty_checkoutq(struct tty *tp)
1768 /* 256 bytes should be enough to print a log message. */
1769 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1773 tty_hiwat_in_block(struct tty *tp)
1776 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1777 tp->t_termios.c_iflag & IXOFF &&
1778 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1780 * Input flow control. Only enter the high watermark when we
1781 * can successfully store the VSTOP character.
1783 if (ttyoutq_write_nofrag(&tp->t_outq,
1784 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1785 tp->t_flags |= TF_HIWAT_IN;
1787 /* No input flow control. */
1788 tp->t_flags |= TF_HIWAT_IN;
1793 tty_hiwat_in_unblock(struct tty *tp)
1796 if (tp->t_flags & TF_HIWAT_IN &&
1797 tp->t_termios.c_iflag & IXOFF &&
1798 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1800 * Input flow control. Only leave the high watermark when we
1801 * can successfully store the VSTART character.
1803 if (ttyoutq_write_nofrag(&tp->t_outq,
1804 &tp->t_termios.c_cc[VSTART], 1) == 0)
1805 tp->t_flags &= ~TF_HIWAT_IN;
1807 /* No input flow control. */
1808 tp->t_flags &= ~TF_HIWAT_IN;
1812 ttydevsw_inwakeup(tp);
1816 * TTY hooks interface.
1820 ttyhook_defrint(struct tty *tp, char c, int flags)
1823 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1830 ttyhook_register(struct tty **rtp, struct proc *p, int fd,
1831 struct ttyhook *th, void *softc)
1836 struct file *fp_cap;
1840 struct filedesc *fdp;
1843 /* Validate the file descriptor. */
1844 if ((fdp = p->p_fd) == NULL)
1847 fp = fget_unlocked(fdp, fd);
1850 if (fp->f_ops == &badfileops) {
1857 error = cap_funwrap(fp_cap, CAP_TTYHOOK, &fp);
1863 * Make sure the vnode is bound to a character device.
1864 * Unlocked check for the vnode type is ok there, because we
1865 * only shall prevent calling devvn_refthread on the file that
1866 * never has been opened over a character device.
1868 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1873 /* Make sure it is a TTY. */
1874 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
1879 if (dev != fp->f_data) {
1883 if (cdp != &ttydev_cdevsw) {
1889 /* Try to attach the hook to the TTY. */
1892 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1893 if (tp->t_flags & TF_HOOK)
1896 tp->t_flags |= TF_HOOK;
1898 tp->t_hooksoftc = softc;
1902 /* Maybe we can switch into bypass mode now. */
1903 ttydisc_optimize(tp);
1905 /* Silently convert rint() calls to rint_bypass() when possible. */
1906 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1907 th->th_rint = ttyhook_defrint;
1909 done3: tty_unlock(tp);
1910 done2: dev_relthread(dev, ref);
1911 done1: fdrop(fp, curthread);
1916 ttyhook_unregister(struct tty *tp)
1919 tty_lock_assert(tp, MA_OWNED);
1920 MPASS(tp->t_flags & TF_HOOK);
1922 /* Disconnect the hook. */
1923 tp->t_flags &= ~TF_HOOK;
1926 /* Maybe we need to leave bypass mode. */
1927 ttydisc_optimize(tp);
1929 /* Maybe deallocate the TTY as well. */
1934 * /dev/console handling.
1938 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1942 /* System has no console device. */
1943 if (dev_console_filename == NULL)
1946 /* Look up corresponding TTY by device name. */
1947 sx_slock(&tty_list_sx);
1948 TAILQ_FOREACH(tp, &tty_list, t_list) {
1949 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
1950 dev_console->si_drv1 = tp;
1954 sx_sunlock(&tty_list_sx);
1956 /* System console has no TTY associated. */
1957 if (dev_console->si_drv1 == NULL)
1960 return (ttydev_open(dev, oflags, devtype, td));
1964 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
1969 return (ttydev_write(dev, uio, ioflag));
1973 * /dev/console is a little different than normal TTY's. When opened,
1974 * it determines which TTY to use. When data gets written to it, it
1975 * will be logged in the kernel message buffer.
1977 static struct cdevsw ttyconsdev_cdevsw = {
1978 .d_version = D_VERSION,
1979 .d_open = ttyconsdev_open,
1980 .d_close = ttydev_close,
1981 .d_read = ttydev_read,
1982 .d_write = ttyconsdev_write,
1983 .d_ioctl = ttydev_ioctl,
1984 .d_kqfilter = ttydev_kqfilter,
1985 .d_poll = ttydev_poll,
1986 .d_mmap = ttydev_mmap,
1987 .d_name = "ttyconsdev",
1992 ttyconsdev_init(void *unused)
1995 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
1996 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
1999 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2002 ttyconsdev_select(const char *name)
2005 dev_console_filename = name;
2009 * Debugging routines.
2012 #include "opt_ddb.h"
2014 #include <ddb/ddb.h>
2015 #include <ddb/db_sym.h>
2022 { TF_NOPREFIX, 'N' },
2024 { TF_INITLOCK, 'I' },
2025 { TF_CALLOUT, 'C' },
2027 /* Keep these together -> 'Oi' and 'Oo'. */
2029 { TF_OPENED_IN, 'i' },
2030 { TF_OPENED_OUT, 'o' },
2031 { TF_OPENED_CONS, 'c' },
2034 { TF_OPENCLOSE, 'B' },
2036 { TF_LITERAL, 'L' },
2038 /* Keep these together -> 'Hi' and 'Ho'. */
2040 { TF_HIWAT_IN, 'i' },
2041 { TF_HIWAT_OUT, 'o' },
2043 { TF_STOPPED, 'S' },
2044 { TF_EXCLUDE, 'X' },
2049 /* Keep these together -> 'bi' and 'bo'. */
2051 { TF_BUSY_IN, 'i' },
2052 { TF_BUSY_OUT, 'o' },
2057 #define TTY_FLAG_BITS \
2058 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
2059 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
2060 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
2062 #define DB_PRINTSYM(name, addr) \
2063 db_printf("%s " #name ": ", sep); \
2064 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2068 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2070 db_printf("%sdevsw: ", sep);
2071 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2072 db_printf(" (%p)\n", tsw);
2073 DB_PRINTSYM(open, tsw->tsw_open);
2074 DB_PRINTSYM(close, tsw->tsw_close);
2075 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2076 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2077 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2078 DB_PRINTSYM(param, tsw->tsw_param);
2079 DB_PRINTSYM(modem, tsw->tsw_modem);
2080 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2081 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2082 DB_PRINTSYM(free, tsw->tsw_free);
2085 _db_show_hooks(const char *sep, const struct ttyhook *th)
2087 db_printf("%shook: ", sep);
2088 db_printsym((db_addr_t)th, DB_STGY_ANY);
2089 db_printf(" (%p)\n", th);
2092 DB_PRINTSYM(rint, th->th_rint);
2093 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2094 DB_PRINTSYM(rint_done, th->th_rint_done);
2095 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2096 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2097 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2098 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2099 DB_PRINTSYM(close, th->th_close);
2103 _db_show_termios(const char *name, const struct termios *t)
2106 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2107 "lflag 0x%x ispeed %u ospeed %u\n", name,
2108 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2109 t->c_ispeed, t->c_ospeed);
2112 /* DDB command to show TTY statistics. */
2113 DB_SHOW_COMMAND(tty, db_show_tty)
2118 db_printf("usage: show tty <addr>\n");
2121 tp = (struct tty *)addr;
2123 db_printf("0x%p: %s\n", tp, tty_devname(tp));
2124 db_printf("\tmtx: %p\n", tp->t_mtx);
2125 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2126 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2128 /* Buffering mechanisms. */
2129 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2130 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2131 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2132 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2133 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2134 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2135 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2136 db_printf("\tinlow: %zu\n", tp->t_inlow);
2137 db_printf("\toutlow: %zu\n", tp->t_outlow);
2138 _db_show_termios("\ttermios", &tp->t_termios);
2139 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2140 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2141 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2142 db_printf("\tcolumn: %u\n", tp->t_column);
2143 db_printf("\twritepos: %u\n", tp->t_writepos);
2144 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2146 /* Init/lock-state devices. */
2147 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2148 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2149 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2150 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2153 _db_show_devsw("\t", tp->t_devsw);
2154 _db_show_hooks("\t", tp->t_hook);
2157 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2158 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2159 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2160 db_printf("\tsession: %p", tp->t_session);
2161 if (tp->t_session != NULL)
2162 db_printf(" count %u leader %p tty %p sid %d login %s",
2163 tp->t_session->s_count, tp->t_session->s_leader,
2164 tp->t_session->s_ttyp, tp->t_session->s_sid,
2165 tp->t_session->s_login);
2167 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2168 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2169 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2170 db_printf("\tdev: %p\n", tp->t_dev);
2173 /* DDB command to list TTYs. */
2174 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2180 /* Make the output look like `pstat -t'. */
2182 #if defined(__LP64__)
2185 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2186 "COL SESS PGID STATE\n");
2188 TAILQ_FOREACH(tp, &tty_list, t_list) {
2189 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2190 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2192 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2196 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2197 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2200 tp->t_outq.to_end - tp->t_outq.to_begin,
2201 osiz - tp->t_outlow,
2202 MIN(tp->t_column, 99999),
2203 tp->t_session ? tp->t_session->s_sid : 0,
2204 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2207 for (i = j = 0; ttystates[i].flag; i++)
2208 if (tp->t_flags & ttystates[i].flag) {
2209 db_printf("%c", ttystates[i].val);