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)
222 struct tty *tp = dev->si_drv1;
227 /* Device is already gone. */
233 * Block when other processes are currently opening or closing
236 while (tp->t_flags & TF_OPENCLOSE) {
237 error = tty_wait(tp, &tp->t_dcdwait);
243 tp->t_flags |= TF_OPENCLOSE;
246 * Make sure the "tty" and "cua" device cannot be opened at the
249 if (TTY_CALLOUT(tp, dev)) {
250 if (tp->t_flags & TF_OPENED_IN) {
255 if (tp->t_flags & TF_OPENED_OUT) {
261 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
266 if (!tty_opened(tp)) {
267 /* Set proper termios flags. */
268 if (TTY_CALLOUT(tp, dev))
269 tp->t_termios = tp->t_termios_init_out;
271 tp->t_termios = tp->t_termios_init_in;
272 ttydevsw_param(tp, &tp->t_termios);
273 /* Prevent modem control on callout devices and /dev/console. */
274 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
275 tp->t_termios.c_cflag |= CLOCAL;
277 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
279 error = ttydevsw_open(tp);
287 /* Wait for Carrier Detect. */
288 if ((oflags & O_NONBLOCK) == 0 &&
289 (tp->t_termios.c_cflag & CLOCAL) == 0) {
290 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
291 error = tty_wait(tp, &tp->t_dcdwait);
297 if (dev == dev_console)
298 tp->t_flags |= TF_OPENED_CONS;
299 else if (TTY_CALLOUT(tp, dev))
300 tp->t_flags |= TF_OPENED_OUT;
302 tp->t_flags |= TF_OPENED_IN;
304 done: tp->t_flags &= ~TF_OPENCLOSE;
305 cv_broadcast(&tp->t_dcdwait);
312 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
314 struct tty *tp = dev->si_drv1;
319 * Don't actually close the device if it is being used as the
322 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED);
323 if (dev == dev_console)
324 tp->t_flags &= ~TF_OPENED_CONS;
326 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
328 if (tp->t_flags & TF_OPENED) {
334 * This can only be called once. The callin and the callout
335 * devices cannot be opened at the same time.
337 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
339 /* Properly wake up threads that are stuck - revoke(). */
341 tty_wakeup(tp, FREAD|FWRITE);
342 cv_broadcast(&tp->t_bgwait);
343 cv_broadcast(&tp->t_dcdwait);
351 tty_is_ctty(struct tty *tp, struct proc *p)
353 tty_lock_assert(tp, MA_OWNED);
355 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
359 tty_wait_background(struct tty *tp, struct thread *td, int sig)
361 struct proc *p = td->td_proc;
366 MPASS(sig == SIGTTIN || sig == SIGTTOU);
367 tty_lock_assert(tp, MA_OWNED);
372 * The process should only sleep, when:
373 * - This terminal is the controling terminal
374 * - Its process group is not the foreground process
376 * - The parent process isn't waiting for the child to
378 * - the signal to send to the process isn't masked
380 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
381 /* Allow the action to happen. */
386 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
387 SIGISMEMBER(td->td_sigmask, sig)) {
388 /* Only allow them in write()/ioctl(). */
390 return (sig == SIGTTOU ? 0 : EIO);
394 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
395 /* Don't allow the action to happen. */
402 * Send the signal and sleep until we're the new
403 * foreground process group.
407 ksi.ksi_code = SI_KERNEL;
412 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
415 error = tty_wait(tp, &tp->t_bgwait);
422 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
424 struct tty *tp = dev->si_drv1;
427 error = ttydev_enter(tp);
431 error = tty_wait_background(tp, curthread, SIGTTIN);
437 error = ttydisc_read(tp, uio, ioflag);
441 * The read() call should not throw an error when the device is
442 * being destroyed. Silently convert it to an EOF.
444 done: if (error == ENXIO)
450 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
452 struct tty *tp = dev->si_drv1;
455 error = ttydev_enter(tp);
459 if (tp->t_termios.c_lflag & TOSTOP) {
460 error = tty_wait_background(tp, curthread, SIGTTOU);
465 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
466 /* Allow non-blocking writes to bypass serialization. */
467 error = ttydisc_write(tp, uio, ioflag);
469 /* Serialize write() calls. */
470 while (tp->t_flags & TF_BUSY_OUT) {
471 error = tty_wait(tp, &tp->t_outserwait);
476 tp->t_flags |= TF_BUSY_OUT;
477 error = ttydisc_write(tp, uio, ioflag);
478 tp->t_flags &= ~TF_BUSY_OUT;
479 cv_signal(&tp->t_outserwait);
482 done: tty_unlock(tp);
487 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
490 struct tty *tp = dev->si_drv1;
493 error = ttydev_enter(tp);
528 #endif /* COMPAT_43TTY */
530 * If the ioctl() causes the TTY to be modified, let it
531 * wait in the background.
533 error = tty_wait_background(tp, curthread, SIGTTOU);
538 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
539 struct termios *old = &tp->t_termios;
540 struct termios *new = (struct termios *)data;
541 struct termios *lock = TTY_CALLOUT(tp, dev) ?
542 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
546 * Lock state devices. Just overwrite the values of the
547 * commands that are currently in use.
549 new->c_iflag = (old->c_iflag & lock->c_iflag) |
550 (new->c_iflag & ~lock->c_iflag);
551 new->c_oflag = (old->c_oflag & lock->c_oflag) |
552 (new->c_oflag & ~lock->c_oflag);
553 new->c_cflag = (old->c_cflag & lock->c_cflag) |
554 (new->c_cflag & ~lock->c_cflag);
555 new->c_lflag = (old->c_lflag & lock->c_lflag) |
556 (new->c_lflag & ~lock->c_lflag);
557 for (cc = 0; cc < NCCS; ++cc)
559 new->c_cc[cc] = old->c_cc[cc];
561 new->c_ispeed = old->c_ispeed;
563 new->c_ospeed = old->c_ospeed;
566 error = tty_ioctl(tp, cmd, data, fflag, td);
567 done: tty_unlock(tp);
573 ttydev_poll(struct cdev *dev, int events, struct thread *td)
575 struct tty *tp = dev->si_drv1;
576 int error, revents = 0;
578 error = ttydev_enter(tp);
580 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
582 if (events & (POLLIN|POLLRDNORM)) {
583 /* See if we can read something. */
584 if (ttydisc_read_poll(tp) > 0)
585 revents |= events & (POLLIN|POLLRDNORM);
588 if (tp->t_flags & TF_ZOMBIE) {
589 /* Hangup flag on zombie state. */
591 } else if (events & (POLLOUT|POLLWRNORM)) {
592 /* See if we can write something. */
593 if (ttydisc_write_poll(tp) > 0)
594 revents |= events & (POLLOUT|POLLWRNORM);
598 if (events & (POLLIN|POLLRDNORM))
599 selrecord(td, &tp->t_inpoll);
600 if (events & (POLLOUT|POLLWRNORM))
601 selrecord(td, &tp->t_outpoll);
610 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
611 int nprot, vm_memattr_t *memattr)
613 struct tty *tp = dev->si_drv1;
616 /* Handle mmap() through the driver. */
618 error = ttydev_enter(tp);
621 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
632 tty_kqops_read_detach(struct knote *kn)
634 struct tty *tp = kn->kn_hook;
636 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
640 tty_kqops_read_event(struct knote *kn, long hint)
642 struct tty *tp = kn->kn_hook;
644 tty_lock_assert(tp, MA_OWNED);
646 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
647 kn->kn_flags |= EV_EOF;
650 kn->kn_data = ttydisc_read_poll(tp);
651 return (kn->kn_data > 0);
656 tty_kqops_write_detach(struct knote *kn)
658 struct tty *tp = kn->kn_hook;
660 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
664 tty_kqops_write_event(struct knote *kn, long hint)
666 struct tty *tp = kn->kn_hook;
668 tty_lock_assert(tp, MA_OWNED);
671 kn->kn_flags |= EV_EOF;
674 kn->kn_data = ttydisc_write_poll(tp);
675 return (kn->kn_data > 0);
679 static struct filterops tty_kqops_read = {
681 .f_detach = tty_kqops_read_detach,
682 .f_event = tty_kqops_read_event,
684 static struct filterops tty_kqops_write = {
686 .f_detach = tty_kqops_write_detach,
687 .f_event = tty_kqops_write_event,
691 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
693 struct tty *tp = dev->si_drv1;
696 error = ttydev_enter(tp);
700 switch (kn->kn_filter) {
703 kn->kn_fop = &tty_kqops_read;
704 knlist_add(&tp->t_inpoll.si_note, kn, 1);
708 kn->kn_fop = &tty_kqops_write;
709 knlist_add(&tp->t_outpoll.si_note, kn, 1);
720 static struct cdevsw ttydev_cdevsw = {
721 .d_version = D_VERSION,
722 .d_open = ttydev_open,
723 .d_close = ttydev_close,
724 .d_read = ttydev_read,
725 .d_write = ttydev_write,
726 .d_ioctl = ttydev_ioctl,
727 .d_kqfilter = ttydev_kqfilter,
728 .d_poll = ttydev_poll,
729 .d_mmap = ttydev_mmap,
735 * Init/lock-state devices
739 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
741 struct tty *tp = dev->si_drv1;
753 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
759 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
765 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
768 struct tty *tp = dev->si_drv1;
777 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
778 if (error != ENOIOCTL)
784 /* Obtain terminal flags through tcgetattr(). */
785 *(struct termios*)data = *(struct termios*)dev->si_drv2;
788 /* Set terminal flags through tcsetattr(). */
789 error = priv_check(td, PRIV_TTY_SETA);
792 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
795 *(int *)data = TTYDISC;
798 bzero(data, sizeof(struct winsize));
804 done: tty_unlock(tp);
808 static struct cdevsw ttyil_cdevsw = {
809 .d_version = D_VERSION,
810 .d_open = ttyil_open,
811 .d_close = ttyil_close,
812 .d_read = ttyil_rdwr,
813 .d_write = ttyil_rdwr,
814 .d_ioctl = ttyil_ioctl,
820 tty_init_termios(struct tty *tp)
822 struct termios *t = &tp->t_termios_init_in;
824 t->c_cflag = TTYDEF_CFLAG;
825 t->c_iflag = TTYDEF_IFLAG;
826 t->c_lflag = TTYDEF_LFLAG;
827 t->c_oflag = TTYDEF_OFLAG;
828 t->c_ispeed = TTYDEF_SPEED;
829 t->c_ospeed = TTYDEF_SPEED;
830 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
832 tp->t_termios_init_out = *t;
836 tty_init_console(struct tty *tp, speed_t s)
838 struct termios *ti = &tp->t_termios_init_in;
839 struct termios *to = &tp->t_termios_init_out;
842 ti->c_ispeed = ti->c_ospeed = s;
843 to->c_ispeed = to->c_ospeed = s;
846 ti->c_cflag |= CLOCAL;
847 to->c_cflag |= CLOCAL;
851 * Standard device routine implementations, mostly meant for
852 * pseudo-terminal device drivers. When a driver creates a new terminal
853 * device class, missing routines are patched.
857 ttydevsw_defopen(struct tty *tp)
864 ttydevsw_defclose(struct tty *tp)
869 ttydevsw_defoutwakeup(struct tty *tp)
872 panic("Terminal device has output, while not implemented");
876 ttydevsw_definwakeup(struct tty *tp)
881 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
888 ttydevsw_defcioctl(struct tty *tp, int unit, u_long cmd, caddr_t data, struct thread *td)
895 ttydevsw_defparam(struct tty *tp, struct termios *t)
899 * Allow the baud rate to be adjusted for pseudo-devices, but at
900 * least restrict it to 115200 to prevent excessive buffer
901 * usage. Also disallow 0, to prevent foot shooting.
903 if (t->c_ispeed < B50)
905 else if (t->c_ispeed > B115200)
906 t->c_ispeed = B115200;
907 if (t->c_ospeed < B50)
909 else if (t->c_ospeed > B115200)
910 t->c_ospeed = B115200;
917 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
920 /* Simulate a carrier to make the TTY layer happy. */
925 ttydevsw_defmmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
926 int nprot, vm_memattr_t *memattr)
933 ttydevsw_defpktnotify(struct tty *tp, char event)
938 ttydevsw_deffree(void *softc)
941 panic("Terminal device freed without a free-handler");
945 * TTY allocation and deallocation. TTY devices can be deallocated when
946 * the driver doesn't use it anymore, when the TTY isn't a session's
947 * controlling TTY and when the device node isn't opened through devfs.
951 tty_alloc(struct ttydevsw *tsw, void *sc)
954 return (tty_alloc_mutex(tsw, sc, NULL));
958 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
962 /* Make sure the driver defines all routines. */
963 #define PATCH_FUNC(x) do { \
964 if (tsw->tsw_ ## x == NULL) \
965 tsw->tsw_ ## x = ttydevsw_def ## x; \
969 PATCH_FUNC(outwakeup);
970 PATCH_FUNC(inwakeup);
976 PATCH_FUNC(pktnotify);
980 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
982 tp->t_devswsoftc = sc;
983 tp->t_flags = tsw->tsw_flags;
985 tty_init_termios(tp);
987 cv_init(&tp->t_inwait, "ttyin");
988 cv_init(&tp->t_outwait, "ttyout");
989 cv_init(&tp->t_outserwait, "ttyosr");
990 cv_init(&tp->t_bgwait, "ttybg");
991 cv_init(&tp->t_dcdwait, "ttydcd");
993 /* Allow drivers to use a custom mutex to lock the TTY. */
997 tp->t_mtx = &tp->t_mtxobj;
998 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1001 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1002 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1004 sx_xlock(&tty_list_sx);
1005 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1007 sx_xunlock(&tty_list_sx);
1013 tty_dealloc(void *arg)
1015 struct tty *tp = arg;
1017 sx_xlock(&tty_list_sx);
1018 TAILQ_REMOVE(&tty_list, tp, t_list);
1020 sx_xunlock(&tty_list_sx);
1022 /* Make sure we haven't leaked buffers. */
1023 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
1024 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
1026 seldrain(&tp->t_inpoll);
1027 seldrain(&tp->t_outpoll);
1028 knlist_destroy(&tp->t_inpoll.si_note);
1029 knlist_destroy(&tp->t_outpoll.si_note);
1031 cv_destroy(&tp->t_inwait);
1032 cv_destroy(&tp->t_outwait);
1033 cv_destroy(&tp->t_bgwait);
1034 cv_destroy(&tp->t_dcdwait);
1035 cv_destroy(&tp->t_outserwait);
1037 if (tp->t_mtx == &tp->t_mtxobj)
1038 mtx_destroy(&tp->t_mtxobj);
1044 tty_rel_free(struct tty *tp)
1048 tty_lock_assert(tp, MA_OWNED);
1050 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1051 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1052 /* TTY is still in use. */
1057 /* TTY can be deallocated. */
1063 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1067 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1069 MPASS(tp->t_sessioncnt > 0);
1070 tty_lock_assert(tp, MA_OWNED);
1072 if (tp->t_pgrp == pg)
1079 tty_rel_sess(struct tty *tp, struct session *sess)
1081 MPASS(tp->t_sessioncnt > 0);
1083 /* Current session has left. */
1084 if (tp->t_session == sess) {
1085 tp->t_session = NULL;
1086 MPASS(tp->t_pgrp == NULL);
1093 tty_rel_gone(struct tty *tp)
1095 MPASS(!tty_gone(tp));
1097 /* Simulate carrier removal. */
1098 ttydisc_modem(tp, 0);
1100 /* Wake up all blocked threads. */
1101 tty_wakeup(tp, FREAD|FWRITE);
1102 cv_broadcast(&tp->t_bgwait);
1103 cv_broadcast(&tp->t_dcdwait);
1105 tp->t_flags |= TF_GONE;
1110 * Exposing information about current TTY's through sysctl
1114 tty_to_xtty(struct tty *tp, struct xtty *xt)
1116 tty_lock_assert(tp, MA_OWNED);
1118 xt->xt_size = sizeof(struct xtty);
1119 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1120 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1121 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1122 xt->xt_inlow = tp->t_inlow;
1123 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1124 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1125 xt->xt_outlow = tp->t_outlow;
1126 xt->xt_column = tp->t_column;
1127 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1128 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1129 xt->xt_flags = tp->t_flags;
1130 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1134 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1136 unsigned long lsize;
1137 struct xtty *xtlist, *xt;
1141 sx_slock(&tty_list_sx);
1142 lsize = tty_list_count * sizeof(struct xtty);
1144 sx_sunlock(&tty_list_sx);
1148 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1150 TAILQ_FOREACH(tp, &tty_list, t_list) {
1152 tty_to_xtty(tp, xt);
1156 sx_sunlock(&tty_list_sx);
1158 error = SYSCTL_OUT(req, xtlist, lsize);
1159 free(xtlist, M_TTY);
1163 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1164 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1167 * Device node creation. Device has been set up, now we can expose it to
1172 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
1176 const char *prefix = "tty";
1177 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1182 /* Remove "tty" prefix from devices like PTY's. */
1183 if (tp->t_flags & TF_NOPREFIX)
1187 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 /* Master call-in device. */
1203 dev = make_dev_cred(&ttydev_cdevsw, 0, cred,
1204 uid, gid, mode, "%s%s", prefix, name);
1208 /* Slave call-in devices. */
1209 if (tp->t_flags & TF_INITLOCK) {
1210 dev = make_dev_cred(&ttyil_cdevsw, TTYUNIT_INIT, cred,
1211 uid, gid, mode, "%s%s.init", prefix, name);
1212 dev_depends(tp->t_dev, dev);
1214 dev->si_drv2 = &tp->t_termios_init_in;
1216 dev = make_dev_cred(&ttyil_cdevsw, TTYUNIT_LOCK, cred,
1217 uid, gid, mode, "%s%s.lock", prefix, name);
1218 dev_depends(tp->t_dev, dev);
1220 dev->si_drv2 = &tp->t_termios_lock_in;
1223 /* Call-out devices. */
1224 if (tp->t_flags & TF_CALLOUT) {
1225 dev = make_dev_cred(&ttydev_cdevsw, TTYUNIT_CALLOUT, cred,
1226 UID_UUCP, GID_DIALER, 0660, "cua%s", name);
1227 dev_depends(tp->t_dev, dev);
1230 /* Slave call-out devices. */
1231 if (tp->t_flags & TF_INITLOCK) {
1232 dev = make_dev_cred(&ttyil_cdevsw,
1233 TTYUNIT_CALLOUT | TTYUNIT_INIT, cred,
1234 UID_UUCP, GID_DIALER, 0660, "cua%s.init", name);
1235 dev_depends(tp->t_dev, dev);
1237 dev->si_drv2 = &tp->t_termios_init_out;
1239 dev = make_dev_cred(&ttyil_cdevsw,
1240 TTYUNIT_CALLOUT | TTYUNIT_LOCK, cred,
1241 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name);
1242 dev_depends(tp->t_dev, dev);
1244 dev->si_drv2 = &tp->t_termios_lock_out;
1250 * Signalling processes.
1254 tty_signal_sessleader(struct tty *tp, int sig)
1258 tty_lock_assert(tp, MA_OWNED);
1259 MPASS(sig >= 1 && sig < NSIG);
1261 /* Make signals start output again. */
1262 tp->t_flags &= ~TF_STOPPED;
1264 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1265 p = tp->t_session->s_leader;
1267 kern_psignal(p, sig);
1273 tty_signal_pgrp(struct tty *tp, int sig)
1277 tty_lock_assert(tp, MA_OWNED);
1278 MPASS(sig >= 1 && sig < NSIG);
1280 /* Make signals start output again. */
1281 tp->t_flags &= ~TF_STOPPED;
1283 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1285 if (tp->t_pgrp != NULL) {
1286 ksiginfo_init(&ksi);
1287 ksi.ksi_signo = sig;
1288 ksi.ksi_code = SI_KERNEL;
1289 PGRP_LOCK(tp->t_pgrp);
1290 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1291 PGRP_UNLOCK(tp->t_pgrp);
1296 tty_wakeup(struct tty *tp, int flags)
1298 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1299 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1301 if (flags & FWRITE) {
1302 cv_broadcast(&tp->t_outwait);
1303 selwakeup(&tp->t_outpoll);
1304 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1306 if (flags & FREAD) {
1307 cv_broadcast(&tp->t_inwait);
1308 selwakeup(&tp->t_inpoll);
1309 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1314 tty_wait(struct tty *tp, struct cv *cv)
1317 int revokecnt = tp->t_revokecnt;
1319 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1320 MPASS(!tty_gone(tp));
1322 error = cv_wait_sig(cv, tp->t_mtx);
1324 /* Restart the system call when we may have been revoked. */
1325 if (tp->t_revokecnt != revokecnt)
1328 /* Bail out when the device slipped away. */
1336 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1339 int revokecnt = tp->t_revokecnt;
1341 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1342 MPASS(!tty_gone(tp));
1344 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1346 /* Restart the system call when we may have been revoked. */
1347 if (tp->t_revokecnt != revokecnt)
1350 /* Bail out when the device slipped away. */
1358 tty_flush(struct tty *tp, int flags)
1360 if (flags & FWRITE) {
1361 tp->t_flags &= ~TF_HIWAT_OUT;
1362 ttyoutq_flush(&tp->t_outq);
1363 tty_wakeup(tp, FWRITE);
1364 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1366 if (flags & FREAD) {
1367 tty_hiwat_in_unblock(tp);
1368 ttyinq_flush(&tp->t_inq);
1369 ttydevsw_inwakeup(tp);
1370 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1375 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1383 * The SER_* and TIOCM_* flags are the same, but one bit
1384 * shifted. I don't know why.
1387 ttydevsw_modem(tp, SER_DTR, 0);
1390 ttydevsw_modem(tp, 0, SER_DTR);
1393 int bits = *(int *)data;
1395 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1396 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1400 int bits = *(int *)data;
1401 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1405 int bits = *(int *)data;
1406 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1410 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1415 tp->t_flags |= TF_ASYNC;
1417 tp->t_flags &= ~TF_ASYNC;
1420 /* This device supports non-blocking operation. */
1423 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1427 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1430 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1431 /* Not allowed to set ownership. */
1434 /* Temporarily unlock the TTY to set ownership. */
1436 error = fsetown(*(int *)data, &tp->t_sigio);
1440 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1441 /* Not allowed to set ownership. */
1444 /* Get ownership. */
1445 *(int *)data = fgetown(&tp->t_sigio);
1448 /* Obtain terminal flags through tcgetattr(). */
1449 *(struct termios*)data = tp->t_termios;
1454 struct termios *t = data;
1457 * Who makes up these funny rules? According to POSIX,
1458 * input baud rate is set equal to the output baud rate
1461 if (t->c_ispeed == 0)
1462 t->c_ispeed = t->c_ospeed;
1464 /* Discard any unsupported bits. */
1465 t->c_iflag &= TTYSUP_IFLAG;
1466 t->c_oflag &= TTYSUP_OFLAG;
1467 t->c_lflag &= TTYSUP_LFLAG;
1468 t->c_cflag &= TTYSUP_CFLAG;
1470 /* Set terminal flags through tcsetattr(). */
1471 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1472 error = tty_drain(tp);
1475 if (cmd == TIOCSETAF)
1476 tty_flush(tp, FREAD);
1480 * Only call param() when the flags really change.
1482 if ((t->c_cflag & CIGNORE) == 0 &&
1483 (tp->t_termios.c_cflag != t->c_cflag ||
1484 tp->t_termios.c_ispeed != t->c_ispeed ||
1485 tp->t_termios.c_ospeed != t->c_ospeed)) {
1486 error = ttydevsw_param(tp, t);
1492 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1493 tp->t_termios.c_ispeed = t->c_ispeed;
1494 tp->t_termios.c_ospeed = t->c_ospeed;
1496 /* Baud rate has changed - update watermarks. */
1500 /* Copy new non-device driver parameters. */
1501 tp->t_termios.c_iflag = t->c_iflag;
1502 tp->t_termios.c_oflag = t->c_oflag;
1503 tp->t_termios.c_lflag = t->c_lflag;
1504 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1506 ttydisc_optimize(tp);
1508 if ((t->c_lflag & ICANON) == 0) {
1510 * When in non-canonical mode, wake up all
1511 * readers. Canonicalize any partial input. VMIN
1512 * and VTIME could also be adjusted.
1514 ttyinq_canonicalize(&tp->t_inq);
1515 tty_wakeup(tp, FREAD);
1519 * For packet mode: notify the PTY consumer that VSTOP
1520 * and VSTART may have been changed.
1522 if (tp->t_termios.c_iflag & IXON &&
1523 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1524 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1525 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1527 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1531 /* For compatibility - we only support TTYDISC. */
1532 *(int *)data = TTYDISC;
1535 if (!tty_is_ctty(tp, td->td_proc))
1538 if (tp->t_pgrp != NULL)
1539 *(int *)data = tp->t_pgrp->pg_id;
1541 *(int *)data = NO_PID;
1544 if (!tty_is_ctty(tp, td->td_proc))
1547 MPASS(tp->t_session);
1548 *(int *)data = tp->t_session->s_sid;
1551 struct proc *p = td->td_proc;
1553 /* XXX: This looks awful. */
1555 sx_xlock(&proctree_lock);
1558 if (!SESS_LEADER(p)) {
1559 /* Only the session leader may do this. */
1560 sx_xunlock(&proctree_lock);
1564 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1565 /* This is already our controlling TTY. */
1566 sx_xunlock(&proctree_lock);
1570 if (p->p_session->s_ttyp != NULL ||
1571 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1572 tp->t_session->s_ttyvp->v_type != VBAD)) {
1574 * There is already a relation between a TTY and
1575 * a session, or the caller is not the session
1578 * Allow the TTY to be stolen when the vnode is
1579 * invalid, but the reference to the TTY is
1580 * still active. This allows immediate reuse of
1581 * TTYs of which the session leader has been
1582 * killed or the TTY revoked.
1584 sx_xunlock(&proctree_lock);
1588 /* Connect the session to the TTY. */
1589 tp->t_session = p->p_session;
1590 tp->t_session->s_ttyp = tp;
1592 sx_xunlock(&proctree_lock);
1594 /* Assign foreground process group. */
1595 tp->t_pgrp = p->p_pgrp;
1597 p->p_flag |= P_CONTROLT;
1606 * XXX: Temporarily unlock the TTY to locate the process
1607 * group. This code would be lot nicer if we would ever
1608 * decompose proctree_lock.
1611 sx_slock(&proctree_lock);
1612 pg = pgfind(*(int *)data);
1615 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1616 sx_sunlock(&proctree_lock);
1623 * Determine if this TTY is the controlling TTY after
1624 * relocking the TTY.
1626 if (!tty_is_ctty(tp, td->td_proc)) {
1627 sx_sunlock(&proctree_lock);
1631 sx_sunlock(&proctree_lock);
1633 /* Wake up the background process groups. */
1634 cv_broadcast(&tp->t_bgwait);
1638 int flags = *(int *)data;
1641 flags = (FREAD|FWRITE);
1643 flags &= (FREAD|FWRITE);
1644 tty_flush(tp, flags);
1648 /* Drain TTY output. */
1649 return tty_drain(tp);
1651 /* Set terminal as console TTY. */
1653 error = priv_check(td, PRIV_TTY_CONSOLE);
1658 * XXX: constty should really need to be locked!
1659 * XXX: allow disconnected constty's to be stolen!
1664 if (constty != NULL)
1670 } else if (constty == tp) {
1675 /* Obtain window size. */
1676 *(struct winsize*)data = tp->t_winsize;
1679 /* Set window size. */
1680 if (bcmp(&tp->t_winsize, data, sizeof(struct winsize)) == 0)
1682 tp->t_winsize = *(struct winsize*)data;
1683 tty_signal_pgrp(tp, SIGWINCH);
1686 tp->t_flags |= TF_EXCLUDE;
1689 tp->t_flags &= ~TF_EXCLUDE;
1692 tp->t_flags |= TF_STOPPED;
1693 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1696 tp->t_flags &= ~TF_STOPPED;
1697 ttydevsw_outwakeup(tp);
1698 ttydevsw_pktnotify(tp, TIOCPKT_START);
1704 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1706 if (!tty_is_ctty(tp, td->td_proc) &&
1707 priv_check(td, PRIV_TTY_STI))
1709 ttydisc_rint(tp, *(char *)data, 0);
1710 ttydisc_rint_done(tp);
1715 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1716 #else /* !COMPAT_43TTY */
1718 #endif /* COMPAT_43TTY */
1722 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1726 tty_lock_assert(tp, MA_OWNED);
1731 error = ttydevsw_ioctl(tp, cmd, data, td);
1732 if (error == ENOIOCTL)
1733 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1739 tty_udev(struct tty *tp)
1742 return dev2udev(tp->t_dev);
1748 tty_checkoutq(struct tty *tp)
1751 /* 256 bytes should be enough to print a log message. */
1752 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1756 tty_hiwat_in_block(struct tty *tp)
1759 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1760 tp->t_termios.c_iflag & IXOFF &&
1761 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1763 * Input flow control. Only enter the high watermark when we
1764 * can successfully store the VSTOP character.
1766 if (ttyoutq_write_nofrag(&tp->t_outq,
1767 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1768 tp->t_flags |= TF_HIWAT_IN;
1770 /* No input flow control. */
1771 tp->t_flags |= TF_HIWAT_IN;
1776 tty_hiwat_in_unblock(struct tty *tp)
1779 if (tp->t_flags & TF_HIWAT_IN &&
1780 tp->t_termios.c_iflag & IXOFF &&
1781 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1783 * Input flow control. Only leave the high watermark when we
1784 * can successfully store the VSTART character.
1786 if (ttyoutq_write_nofrag(&tp->t_outq,
1787 &tp->t_termios.c_cc[VSTART], 1) == 0)
1788 tp->t_flags &= ~TF_HIWAT_IN;
1790 /* No input flow control. */
1791 tp->t_flags &= ~TF_HIWAT_IN;
1795 ttydevsw_inwakeup(tp);
1799 * TTY hooks interface.
1803 ttyhook_defrint(struct tty *tp, char c, int flags)
1806 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1813 ttyhook_register(struct tty **rtp, struct proc *p, int fd,
1814 struct ttyhook *th, void *softc)
1819 struct file *fp_cap;
1823 struct filedesc *fdp;
1826 /* Validate the file descriptor. */
1827 if ((fdp = p->p_fd) == NULL)
1830 fp = fget_unlocked(fdp, fd);
1833 if (fp->f_ops == &badfileops) {
1840 error = cap_funwrap(fp_cap, CAP_TTYHOOK, &fp);
1846 * Make sure the vnode is bound to a character device.
1847 * Unlocked check for the vnode type is ok there, because we
1848 * only shall prevent calling devvn_refthread on the file that
1849 * never has been opened over a character device.
1851 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1856 /* Make sure it is a TTY. */
1857 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
1862 if (dev != fp->f_data) {
1866 if (cdp != &ttydev_cdevsw) {
1872 /* Try to attach the hook to the TTY. */
1875 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1876 if (tp->t_flags & TF_HOOK)
1879 tp->t_flags |= TF_HOOK;
1881 tp->t_hooksoftc = softc;
1885 /* Maybe we can switch into bypass mode now. */
1886 ttydisc_optimize(tp);
1888 /* Silently convert rint() calls to rint_bypass() when possible. */
1889 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1890 th->th_rint = ttyhook_defrint;
1892 done3: tty_unlock(tp);
1893 done2: dev_relthread(dev, ref);
1894 done1: fdrop(fp, curthread);
1899 ttyhook_unregister(struct tty *tp)
1902 tty_lock_assert(tp, MA_OWNED);
1903 MPASS(tp->t_flags & TF_HOOK);
1905 /* Disconnect the hook. */
1906 tp->t_flags &= ~TF_HOOK;
1909 /* Maybe we need to leave bypass mode. */
1910 ttydisc_optimize(tp);
1912 /* Maybe deallocate the TTY as well. */
1917 * /dev/console handling.
1921 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1925 /* System has no console device. */
1926 if (dev_console_filename == NULL)
1929 /* Look up corresponding TTY by device name. */
1930 sx_slock(&tty_list_sx);
1931 TAILQ_FOREACH(tp, &tty_list, t_list) {
1932 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
1933 dev_console->si_drv1 = tp;
1937 sx_sunlock(&tty_list_sx);
1939 /* System console has no TTY associated. */
1940 if (dev_console->si_drv1 == NULL)
1943 return (ttydev_open(dev, oflags, devtype, td));
1947 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
1952 return (ttydev_write(dev, uio, ioflag));
1956 * /dev/console is a little different than normal TTY's. When opened,
1957 * it determines which TTY to use. When data gets written to it, it
1958 * will be logged in the kernel message buffer.
1960 static struct cdevsw ttyconsdev_cdevsw = {
1961 .d_version = D_VERSION,
1962 .d_open = ttyconsdev_open,
1963 .d_close = ttydev_close,
1964 .d_read = ttydev_read,
1965 .d_write = ttyconsdev_write,
1966 .d_ioctl = ttydev_ioctl,
1967 .d_kqfilter = ttydev_kqfilter,
1968 .d_poll = ttydev_poll,
1969 .d_mmap = ttydev_mmap,
1970 .d_name = "ttyconsdev",
1975 ttyconsdev_init(void *unused)
1978 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
1979 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
1982 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
1985 ttyconsdev_select(const char *name)
1988 dev_console_filename = name;
1992 * Debugging routines.
1995 #include "opt_ddb.h"
1997 #include <ddb/ddb.h>
1998 #include <ddb/db_sym.h>
2005 { TF_NOPREFIX, 'N' },
2007 { TF_INITLOCK, 'I' },
2008 { TF_CALLOUT, 'C' },
2010 /* Keep these together -> 'Oi' and 'Oo'. */
2012 { TF_OPENED_IN, 'i' },
2013 { TF_OPENED_OUT, 'o' },
2014 { TF_OPENED_CONS, 'c' },
2017 { TF_OPENCLOSE, 'B' },
2019 { TF_LITERAL, 'L' },
2021 /* Keep these together -> 'Hi' and 'Ho'. */
2023 { TF_HIWAT_IN, 'i' },
2024 { TF_HIWAT_OUT, 'o' },
2026 { TF_STOPPED, 'S' },
2027 { TF_EXCLUDE, 'X' },
2032 /* Keep these together -> 'bi' and 'bo'. */
2034 { TF_BUSY_IN, 'i' },
2035 { TF_BUSY_OUT, 'o' },
2040 #define TTY_FLAG_BITS \
2041 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
2042 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
2043 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
2045 #define DB_PRINTSYM(name, addr) \
2046 db_printf("%s " #name ": ", sep); \
2047 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2051 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2053 db_printf("%sdevsw: ", sep);
2054 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2055 db_printf(" (%p)\n", tsw);
2056 DB_PRINTSYM(open, tsw->tsw_open);
2057 DB_PRINTSYM(close, tsw->tsw_close);
2058 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2059 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2060 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2061 DB_PRINTSYM(param, tsw->tsw_param);
2062 DB_PRINTSYM(modem, tsw->tsw_modem);
2063 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2064 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2065 DB_PRINTSYM(free, tsw->tsw_free);
2068 _db_show_hooks(const char *sep, const struct ttyhook *th)
2070 db_printf("%shook: ", sep);
2071 db_printsym((db_addr_t)th, DB_STGY_ANY);
2072 db_printf(" (%p)\n", th);
2075 DB_PRINTSYM(rint, th->th_rint);
2076 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2077 DB_PRINTSYM(rint_done, th->th_rint_done);
2078 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2079 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2080 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2081 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2082 DB_PRINTSYM(close, th->th_close);
2086 _db_show_termios(const char *name, const struct termios *t)
2089 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2090 "lflag 0x%x ispeed %u ospeed %u\n", name,
2091 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2092 t->c_ispeed, t->c_ospeed);
2095 /* DDB command to show TTY statistics. */
2096 DB_SHOW_COMMAND(tty, db_show_tty)
2101 db_printf("usage: show tty <addr>\n");
2104 tp = (struct tty *)addr;
2106 db_printf("0x%p: %s\n", tp, tty_devname(tp));
2107 db_printf("\tmtx: %p\n", tp->t_mtx);
2108 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2109 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2111 /* Buffering mechanisms. */
2112 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2113 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2114 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2115 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2116 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2117 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2118 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2119 db_printf("\tinlow: %zu\n", tp->t_inlow);
2120 db_printf("\toutlow: %zu\n", tp->t_outlow);
2121 _db_show_termios("\ttermios", &tp->t_termios);
2122 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2123 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2124 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2125 db_printf("\tcolumn: %u\n", tp->t_column);
2126 db_printf("\twritepos: %u\n", tp->t_writepos);
2127 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2129 /* Init/lock-state devices. */
2130 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2131 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2132 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2133 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2136 _db_show_devsw("\t", tp->t_devsw);
2137 _db_show_hooks("\t", tp->t_hook);
2140 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2141 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2142 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2143 db_printf("\tsession: %p", tp->t_session);
2144 if (tp->t_session != NULL)
2145 db_printf(" count %u leader %p tty %p sid %d login %s",
2146 tp->t_session->s_count, tp->t_session->s_leader,
2147 tp->t_session->s_ttyp, tp->t_session->s_sid,
2148 tp->t_session->s_login);
2150 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2151 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2152 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2153 db_printf("\tdev: %p\n", tp->t_dev);
2156 /* DDB command to list TTYs. */
2157 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2163 /* Make the output look like `pstat -t'. */
2165 #if defined(__LP64__)
2168 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2169 "COL SESS PGID STATE\n");
2171 TAILQ_FOREACH(tp, &tty_list, t_list) {
2172 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2173 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2175 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2179 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2180 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2183 tp->t_outq.to_end - tp->t_outq.to_begin,
2184 osiz - tp->t_outlow,
2185 MIN(tp->t_column, 99999),
2186 tp->t_session ? tp->t_session->s_sid : 0,
2187 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2190 for (i = j = 0; ttystates[i].flag; i++)
2191 if (tp->t_flags & ttystates[i].flag) {
2192 db_printf("%c", ttystates[i].val);