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 || ttydevsw_busy(tp)) {
136 ttydevsw_outwakeup(tp);
137 /* Could be handled synchronously. */
138 bytesused = ttyoutq_bytesused(&tp->t_outq);
139 if (bytesused == 0 && !ttydevsw_busy(tp))
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)
181 if (tty_gone(tp) || !tty_opened(tp)) {
182 /* Device is already gone. */
191 ttydev_leave(struct tty *tp)
194 tty_lock_assert(tp, MA_OWNED);
196 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
197 /* Device is still opened somewhere. */
202 tp->t_flags |= TF_OPENCLOSE;
204 /* Stop asynchronous I/O. */
205 funsetown(&tp->t_sigio);
207 /* Remove console TTY. */
211 /* Drain any output. */
212 MPASS((tp->t_flags & TF_STOPPED) == 0);
218 /* Destroy associated buffers already. */
219 ttyinq_free(&tp->t_inq);
221 ttyoutq_free(&tp->t_outq);
224 knlist_clear(&tp->t_inpoll.si_note, 1);
225 knlist_clear(&tp->t_outpoll.si_note, 1);
230 tp->t_flags &= ~TF_OPENCLOSE;
231 cv_broadcast(&tp->t_dcdwait);
236 * Operations that are exposed through the character device in /dev.
239 ttydev_open(struct cdev *dev, int oflags, int devtype __unused,
249 /* Device is already gone. */
255 * Block when other processes are currently opening or closing
258 while (tp->t_flags & TF_OPENCLOSE) {
259 error = tty_wait(tp, &tp->t_dcdwait);
265 tp->t_flags |= TF_OPENCLOSE;
268 * Make sure the "tty" and "cua" device cannot be opened at the
269 * same time. The console is a "tty" device.
271 if (TTY_CALLOUT(tp, dev)) {
272 if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) {
277 if (tp->t_flags & TF_OPENED_OUT) {
283 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
288 if (!tty_opened(tp)) {
289 /* Set proper termios flags. */
290 if (TTY_CALLOUT(tp, dev))
291 tp->t_termios = tp->t_termios_init_out;
293 tp->t_termios = tp->t_termios_init_in;
294 ttydevsw_param(tp, &tp->t_termios);
295 /* Prevent modem control on callout devices and /dev/console. */
296 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
297 tp->t_termios.c_cflag |= CLOCAL;
299 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
301 error = ttydevsw_open(tp);
306 tty_watermarks(tp); /* XXXGL: drops lock */
309 /* Wait for Carrier Detect. */
310 if ((oflags & O_NONBLOCK) == 0 &&
311 (tp->t_termios.c_cflag & CLOCAL) == 0) {
312 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
313 error = tty_wait(tp, &tp->t_dcdwait);
319 if (dev == dev_console)
320 tp->t_flags |= TF_OPENED_CONS;
321 else if (TTY_CALLOUT(tp, dev))
322 tp->t_flags |= TF_OPENED_OUT;
324 tp->t_flags |= TF_OPENED_IN;
325 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
326 (tp->t_flags & TF_OPENED_OUT) == 0);
328 done: tp->t_flags &= ~TF_OPENCLOSE;
329 cv_broadcast(&tp->t_dcdwait);
336 ttydev_close(struct cdev *dev, int fflag, int devtype __unused,
337 struct thread *td __unused)
339 struct tty *tp = dev->si_drv1;
344 * Don't actually close the device if it is being used as the
347 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
348 (tp->t_flags & TF_OPENED_OUT) == 0);
349 if (dev == dev_console)
350 tp->t_flags &= ~TF_OPENED_CONS;
352 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
354 if (tp->t_flags & TF_OPENED) {
360 * This can only be called once. The callin and the callout
361 * devices cannot be opened at the same time.
363 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
365 /* Properly wake up threads that are stuck - revoke(). */
367 tty_wakeup(tp, FREAD|FWRITE);
368 cv_broadcast(&tp->t_bgwait);
369 cv_broadcast(&tp->t_dcdwait);
377 tty_is_ctty(struct tty *tp, struct proc *p)
380 tty_lock_assert(tp, MA_OWNED);
382 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
386 tty_wait_background(struct tty *tp, struct thread *td, int sig)
388 struct proc *p = td->td_proc;
393 MPASS(sig == SIGTTIN || sig == SIGTTOU);
394 tty_lock_assert(tp, MA_OWNED);
399 * The process should only sleep, when:
400 * - This terminal is the controling terminal
401 * - Its process group is not the foreground process
403 * - The parent process isn't waiting for the child to
405 * - the signal to send to the process isn't masked
407 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
408 /* Allow the action to happen. */
413 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
414 SIGISMEMBER(td->td_sigmask, sig)) {
415 /* Only allow them in write()/ioctl(). */
417 return (sig == SIGTTOU ? 0 : EIO);
421 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
422 /* Don't allow the action to happen. */
429 * Send the signal and sleep until we're the new
430 * foreground process group.
434 ksi.ksi_code = SI_KERNEL;
439 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
442 error = tty_wait(tp, &tp->t_bgwait);
449 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
451 struct tty *tp = dev->si_drv1;
454 error = ttydev_enter(tp);
457 error = ttydisc_read(tp, uio, ioflag);
461 * The read() call should not throw an error when the device is
462 * being destroyed. Silently convert it to an EOF.
464 done: if (error == ENXIO)
470 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
472 struct tty *tp = dev->si_drv1;
475 error = ttydev_enter(tp);
479 if (tp->t_termios.c_lflag & TOSTOP) {
480 error = tty_wait_background(tp, curthread, SIGTTOU);
485 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
486 /* Allow non-blocking writes to bypass serialization. */
487 error = ttydisc_write(tp, uio, ioflag);
489 /* Serialize write() calls. */
490 while (tp->t_flags & TF_BUSY_OUT) {
491 error = tty_wait(tp, &tp->t_outserwait);
496 tp->t_flags |= TF_BUSY_OUT;
497 error = ttydisc_write(tp, uio, ioflag);
498 tp->t_flags &= ~TF_BUSY_OUT;
499 cv_signal(&tp->t_outserwait);
502 done: tty_unlock(tp);
507 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
510 struct tty *tp = dev->si_drv1;
513 error = ttydev_enter(tp);
548 #endif /* COMPAT_43TTY */
550 * If the ioctl() causes the TTY to be modified, let it
551 * wait in the background.
553 error = tty_wait_background(tp, curthread, SIGTTOU);
558 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
559 struct termios *old = &tp->t_termios;
560 struct termios *new = (struct termios *)data;
561 struct termios *lock = TTY_CALLOUT(tp, dev) ?
562 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
566 * Lock state devices. Just overwrite the values of the
567 * commands that are currently in use.
569 new->c_iflag = (old->c_iflag & lock->c_iflag) |
570 (new->c_iflag & ~lock->c_iflag);
571 new->c_oflag = (old->c_oflag & lock->c_oflag) |
572 (new->c_oflag & ~lock->c_oflag);
573 new->c_cflag = (old->c_cflag & lock->c_cflag) |
574 (new->c_cflag & ~lock->c_cflag);
575 new->c_lflag = (old->c_lflag & lock->c_lflag) |
576 (new->c_lflag & ~lock->c_lflag);
577 for (cc = 0; cc < NCCS; ++cc)
579 new->c_cc[cc] = old->c_cc[cc];
581 new->c_ispeed = old->c_ispeed;
583 new->c_ospeed = old->c_ospeed;
586 error = tty_ioctl(tp, cmd, data, fflag, td);
587 done: tty_unlock(tp);
593 ttydev_poll(struct cdev *dev, int events, struct thread *td)
595 struct tty *tp = dev->si_drv1;
596 int error, revents = 0;
598 error = ttydev_enter(tp);
600 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
602 if (events & (POLLIN|POLLRDNORM)) {
603 /* See if we can read something. */
604 if (ttydisc_read_poll(tp) > 0)
605 revents |= events & (POLLIN|POLLRDNORM);
608 if (tp->t_flags & TF_ZOMBIE) {
609 /* Hangup flag on zombie state. */
611 } else if (events & (POLLOUT|POLLWRNORM)) {
612 /* See if we can write something. */
613 if (ttydisc_write_poll(tp) > 0)
614 revents |= events & (POLLOUT|POLLWRNORM);
618 if (events & (POLLIN|POLLRDNORM))
619 selrecord(td, &tp->t_inpoll);
620 if (events & (POLLOUT|POLLWRNORM))
621 selrecord(td, &tp->t_outpoll);
630 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
631 int nprot, vm_memattr_t *memattr)
633 struct tty *tp = dev->si_drv1;
636 /* Handle mmap() through the driver. */
638 error = ttydev_enter(tp);
641 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
652 tty_kqops_read_detach(struct knote *kn)
654 struct tty *tp = kn->kn_hook;
656 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
660 tty_kqops_read_event(struct knote *kn, long hint __unused)
662 struct tty *tp = kn->kn_hook;
664 tty_lock_assert(tp, MA_OWNED);
666 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
667 kn->kn_flags |= EV_EOF;
670 kn->kn_data = ttydisc_read_poll(tp);
671 return (kn->kn_data > 0);
676 tty_kqops_write_detach(struct knote *kn)
678 struct tty *tp = kn->kn_hook;
680 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
684 tty_kqops_write_event(struct knote *kn, long hint __unused)
686 struct tty *tp = kn->kn_hook;
688 tty_lock_assert(tp, MA_OWNED);
691 kn->kn_flags |= EV_EOF;
694 kn->kn_data = ttydisc_write_poll(tp);
695 return (kn->kn_data > 0);
699 static struct filterops tty_kqops_read = {
701 .f_detach = tty_kqops_read_detach,
702 .f_event = tty_kqops_read_event,
705 static struct filterops tty_kqops_write = {
707 .f_detach = tty_kqops_write_detach,
708 .f_event = tty_kqops_write_event,
712 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
714 struct tty *tp = dev->si_drv1;
717 error = ttydev_enter(tp);
721 switch (kn->kn_filter) {
724 kn->kn_fop = &tty_kqops_read;
725 knlist_add(&tp->t_inpoll.si_note, kn, 1);
729 kn->kn_fop = &tty_kqops_write;
730 knlist_add(&tp->t_outpoll.si_note, kn, 1);
741 static struct cdevsw ttydev_cdevsw = {
742 .d_version = D_VERSION,
743 .d_open = ttydev_open,
744 .d_close = ttydev_close,
745 .d_read = ttydev_read,
746 .d_write = ttydev_write,
747 .d_ioctl = ttydev_ioctl,
748 .d_kqfilter = ttydev_kqfilter,
749 .d_poll = ttydev_poll,
750 .d_mmap = ttydev_mmap,
756 * Init/lock-state devices
760 ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
777 ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
778 struct thread *td __unused)
785 ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
793 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
796 struct tty *tp = dev->si_drv1;
805 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
806 if (error != ENOIOCTL)
812 /* Obtain terminal flags through tcgetattr(). */
813 *(struct termios*)data = *(struct termios*)dev->si_drv2;
816 /* Set terminal flags through tcsetattr(). */
817 error = priv_check(td, PRIV_TTY_SETA);
820 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
823 *(int *)data = TTYDISC;
826 bzero(data, sizeof(struct winsize));
832 done: tty_unlock(tp);
836 static struct cdevsw ttyil_cdevsw = {
837 .d_version = D_VERSION,
838 .d_open = ttyil_open,
839 .d_close = ttyil_close,
840 .d_read = ttyil_rdwr,
841 .d_write = ttyil_rdwr,
842 .d_ioctl = ttyil_ioctl,
848 tty_init_termios(struct tty *tp)
850 struct termios *t = &tp->t_termios_init_in;
852 t->c_cflag = TTYDEF_CFLAG;
853 t->c_iflag = TTYDEF_IFLAG;
854 t->c_lflag = TTYDEF_LFLAG;
855 t->c_oflag = TTYDEF_OFLAG;
856 t->c_ispeed = TTYDEF_SPEED;
857 t->c_ospeed = TTYDEF_SPEED;
858 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
860 tp->t_termios_init_out = *t;
864 tty_init_console(struct tty *tp, speed_t s)
866 struct termios *ti = &tp->t_termios_init_in;
867 struct termios *to = &tp->t_termios_init_out;
870 ti->c_ispeed = ti->c_ospeed = s;
871 to->c_ispeed = to->c_ospeed = s;
874 ti->c_cflag |= CLOCAL;
875 to->c_cflag |= CLOCAL;
879 * Standard device routine implementations, mostly meant for
880 * pseudo-terminal device drivers. When a driver creates a new terminal
881 * device class, missing routines are patched.
885 ttydevsw_defopen(struct tty *tp __unused)
892 ttydevsw_defclose(struct tty *tp __unused)
898 ttydevsw_defoutwakeup(struct tty *tp __unused)
901 panic("Terminal device has output, while not implemented");
905 ttydevsw_definwakeup(struct tty *tp __unused)
911 ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
912 caddr_t data __unused, struct thread *td __unused)
919 ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
920 u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
927 ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
931 * Allow the baud rate to be adjusted for pseudo-devices, but at
932 * least restrict it to 115200 to prevent excessive buffer
933 * usage. Also disallow 0, to prevent foot shooting.
935 if (t->c_ispeed < B50)
937 else if (t->c_ispeed > B115200)
938 t->c_ispeed = B115200;
939 if (t->c_ospeed < B50)
941 else if (t->c_ospeed > B115200)
942 t->c_ospeed = B115200;
949 ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
953 /* Simulate a carrier to make the TTY layer happy. */
958 ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
959 vm_paddr_t *paddr __unused, int nprot __unused,
960 vm_memattr_t *memattr __unused)
967 ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
973 ttydevsw_deffree(void *softc __unused)
976 panic("Terminal device freed without a free-handler");
980 ttydevsw_defbusy(struct tty *tp __unused)
987 * TTY allocation and deallocation. TTY devices can be deallocated when
988 * the driver doesn't use it anymore, when the TTY isn't a session's
989 * controlling TTY and when the device node isn't opened through devfs.
993 tty_alloc(struct ttydevsw *tsw, void *sc)
996 return (tty_alloc_mutex(tsw, sc, NULL));
1000 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1004 /* Make sure the driver defines all routines. */
1005 #define PATCH_FUNC(x) do { \
1006 if (tsw->tsw_ ## x == NULL) \
1007 tsw->tsw_ ## x = ttydevsw_def ## x; \
1011 PATCH_FUNC(outwakeup);
1012 PATCH_FUNC(inwakeup);
1018 PATCH_FUNC(pktnotify);
1023 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
1025 tp->t_devswsoftc = sc;
1026 tp->t_flags = tsw->tsw_flags;
1028 tty_init_termios(tp);
1030 cv_init(&tp->t_inwait, "ttyin");
1031 cv_init(&tp->t_outwait, "ttyout");
1032 cv_init(&tp->t_outserwait, "ttyosr");
1033 cv_init(&tp->t_bgwait, "ttybg");
1034 cv_init(&tp->t_dcdwait, "ttydcd");
1036 /* Allow drivers to use a custom mutex to lock the TTY. */
1037 if (mutex != NULL) {
1040 tp->t_mtx = &tp->t_mtxobj;
1041 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1044 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1045 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1051 tty_dealloc(void *arg)
1053 struct tty *tp = arg;
1055 /* Make sure we haven't leaked buffers. */
1056 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
1057 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
1059 seldrain(&tp->t_inpoll);
1060 seldrain(&tp->t_outpoll);
1061 knlist_destroy(&tp->t_inpoll.si_note);
1062 knlist_destroy(&tp->t_outpoll.si_note);
1064 cv_destroy(&tp->t_inwait);
1065 cv_destroy(&tp->t_outwait);
1066 cv_destroy(&tp->t_bgwait);
1067 cv_destroy(&tp->t_dcdwait);
1068 cv_destroy(&tp->t_outserwait);
1070 if (tp->t_mtx == &tp->t_mtxobj)
1071 mtx_destroy(&tp->t_mtxobj);
1077 tty_rel_free(struct tty *tp)
1081 tty_lock_assert(tp, MA_OWNED);
1083 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1084 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1085 /* TTY is still in use. */
1090 /* TTY can be deallocated. */
1096 sx_xlock(&tty_list_sx);
1097 TAILQ_REMOVE(&tty_list, tp, t_list);
1099 sx_xunlock(&tty_list_sx);
1100 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1105 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1108 MPASS(tp->t_sessioncnt > 0);
1109 tty_lock_assert(tp, MA_OWNED);
1111 if (tp->t_pgrp == pg)
1118 tty_rel_sess(struct tty *tp, struct session *sess)
1121 MPASS(tp->t_sessioncnt > 0);
1123 /* Current session has left. */
1124 if (tp->t_session == sess) {
1125 tp->t_session = NULL;
1126 MPASS(tp->t_pgrp == NULL);
1133 tty_rel_gone(struct tty *tp)
1136 MPASS(!tty_gone(tp));
1138 /* Simulate carrier removal. */
1139 ttydisc_modem(tp, 0);
1141 /* Wake up all blocked threads. */
1142 tty_wakeup(tp, FREAD|FWRITE);
1143 cv_broadcast(&tp->t_bgwait);
1144 cv_broadcast(&tp->t_dcdwait);
1146 tp->t_flags |= TF_GONE;
1151 * Exposing information about current TTY's through sysctl
1155 tty_to_xtty(struct tty *tp, struct xtty *xt)
1158 tty_lock_assert(tp, MA_OWNED);
1160 xt->xt_size = sizeof(struct xtty);
1161 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1162 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1163 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1164 xt->xt_inlow = tp->t_inlow;
1165 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1166 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1167 xt->xt_outlow = tp->t_outlow;
1168 xt->xt_column = tp->t_column;
1169 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1170 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1171 xt->xt_flags = tp->t_flags;
1172 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1176 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1178 unsigned long lsize;
1179 struct xtty *xtlist, *xt;
1183 sx_slock(&tty_list_sx);
1184 lsize = tty_list_count * sizeof(struct xtty);
1186 sx_sunlock(&tty_list_sx);
1190 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1192 TAILQ_FOREACH(tp, &tty_list, t_list) {
1194 tty_to_xtty(tp, xt);
1198 sx_sunlock(&tty_list_sx);
1200 error = SYSCTL_OUT(req, xtlist, lsize);
1201 free(xtlist, M_TTY);
1205 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1206 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1209 * Device node creation. Device has been set up, now we can expose it to
1214 tty_vmakedevf(struct tty *tp, struct ucred *cred, int flags,
1215 const char *fmt, va_list ap)
1217 struct make_dev_args args;
1218 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1219 const char *prefix = "tty";
1220 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1226 /* Remove "tty" prefix from devices like PTY's. */
1227 if (tp->t_flags & TF_NOPREFIX)
1230 vsnrprintf(name, sizeof name, 32, fmt, ap);
1233 /* System device. */
1236 mode = S_IRUSR|S_IWUSR;
1239 uid = cred->cr_ruid;
1241 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1244 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1245 flags |= MAKEDEV_CHECKNAME;
1247 /* Master call-in device. */
1248 make_dev_args_init(&args);
1249 args.mda_flags = flags;
1250 args.mda_devsw = &ttydev_cdevsw;
1254 args.mda_mode = mode;
1255 args.mda_si_drv1 = tp;
1256 error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1261 init = lock = cua = cinit = clock = NULL;
1263 /* Slave call-in devices. */
1264 if (tp->t_flags & TF_INITLOCK) {
1265 args.mda_devsw = &ttyil_cdevsw;
1266 args.mda_unit = TTYUNIT_INIT;
1267 args.mda_si_drv1 = tp;
1268 args.mda_si_drv2 = &tp->t_termios_init_in;
1269 error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1272 dev_depends(dev, init);
1274 args.mda_unit = TTYUNIT_LOCK;
1275 args.mda_si_drv2 = &tp->t_termios_lock_in;
1276 error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1279 dev_depends(dev, lock);
1282 /* Call-out devices. */
1283 if (tp->t_flags & TF_CALLOUT) {
1284 make_dev_args_init(&args);
1285 args.mda_flags = flags;
1286 args.mda_devsw = &ttydev_cdevsw;
1288 args.mda_uid = UID_UUCP;
1289 args.mda_gid = GID_DIALER;
1290 args.mda_mode = 0660;
1291 args.mda_unit = TTYUNIT_CALLOUT;
1292 args.mda_si_drv1 = tp;
1293 error = make_dev_s(&args, &cua, "cua%s", name);
1296 dev_depends(dev, cua);
1298 /* Slave call-out devices. */
1299 if (tp->t_flags & TF_INITLOCK) {
1300 args.mda_devsw = &ttyil_cdevsw;
1301 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1302 args.mda_si_drv2 = &tp->t_termios_init_out;
1303 error = make_dev_s(&args, &cinit, "cua%s.init", name);
1306 dev_depends(dev, cinit);
1308 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1309 args.mda_si_drv2 = &tp->t_termios_lock_out;
1310 error = make_dev_s(&args, &clock, "cua%s.lock", name);
1313 dev_depends(dev, clock);
1317 sx_xlock(&tty_list_sx);
1318 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1320 sx_xunlock(&tty_list_sx);
1339 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1340 const char *fmt, ...)
1346 error = tty_vmakedevf(tp, cred, flags, fmt, ap);
1353 tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
1358 (void) tty_vmakedevf(tp, cred, 0, fmt, ap);
1363 * Signalling processes.
1367 tty_signal_sessleader(struct tty *tp, int sig)
1371 tty_lock_assert(tp, MA_OWNED);
1372 MPASS(sig >= 1 && sig < NSIG);
1374 /* Make signals start output again. */
1375 tp->t_flags &= ~TF_STOPPED;
1377 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1378 p = tp->t_session->s_leader;
1380 kern_psignal(p, sig);
1386 tty_signal_pgrp(struct tty *tp, int sig)
1390 tty_lock_assert(tp, MA_OWNED);
1391 MPASS(sig >= 1 && sig < NSIG);
1393 /* Make signals start output again. */
1394 tp->t_flags &= ~TF_STOPPED;
1396 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1398 if (tp->t_pgrp != NULL) {
1399 ksiginfo_init(&ksi);
1400 ksi.ksi_signo = sig;
1401 ksi.ksi_code = SI_KERNEL;
1402 PGRP_LOCK(tp->t_pgrp);
1403 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1404 PGRP_UNLOCK(tp->t_pgrp);
1409 tty_wakeup(struct tty *tp, int flags)
1412 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1413 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1415 if (flags & FWRITE) {
1416 cv_broadcast(&tp->t_outwait);
1417 selwakeup(&tp->t_outpoll);
1418 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1420 if (flags & FREAD) {
1421 cv_broadcast(&tp->t_inwait);
1422 selwakeup(&tp->t_inpoll);
1423 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1428 tty_wait(struct tty *tp, struct cv *cv)
1431 int revokecnt = tp->t_revokecnt;
1433 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1434 MPASS(!tty_gone(tp));
1436 error = cv_wait_sig(cv, tp->t_mtx);
1438 /* Bail out when the device slipped away. */
1442 /* Restart the system call when we may have been revoked. */
1443 if (tp->t_revokecnt != revokecnt)
1450 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1453 int revokecnt = tp->t_revokecnt;
1455 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1456 MPASS(!tty_gone(tp));
1458 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1460 /* Bail out when the device slipped away. */
1464 /* Restart the system call when we may have been revoked. */
1465 if (tp->t_revokecnt != revokecnt)
1472 tty_flush(struct tty *tp, int flags)
1475 if (flags & FWRITE) {
1476 tp->t_flags &= ~TF_HIWAT_OUT;
1477 ttyoutq_flush(&tp->t_outq);
1478 tty_wakeup(tp, FWRITE);
1479 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1481 if (flags & FREAD) {
1482 tty_hiwat_in_unblock(tp);
1483 ttyinq_flush(&tp->t_inq);
1484 ttydevsw_inwakeup(tp);
1485 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1490 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1493 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1495 tp->t_winsize = *wsz;
1496 tty_signal_pgrp(tp, SIGWINCH);
1500 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1508 * The SER_* and TIOCM_* flags are the same, but one bit
1509 * shifted. I don't know why.
1512 ttydevsw_modem(tp, SER_DTR, 0);
1515 ttydevsw_modem(tp, 0, SER_DTR);
1518 int bits = *(int *)data;
1520 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1521 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1525 int bits = *(int *)data;
1526 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1530 int bits = *(int *)data;
1531 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1535 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1540 tp->t_flags |= TF_ASYNC;
1542 tp->t_flags &= ~TF_ASYNC;
1545 /* This device supports non-blocking operation. */
1548 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1552 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1555 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1556 /* Not allowed to set ownership. */
1559 /* Temporarily unlock the TTY to set ownership. */
1561 error = fsetown(*(int *)data, &tp->t_sigio);
1565 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1566 /* Not allowed to set ownership. */
1569 /* Get ownership. */
1570 *(int *)data = fgetown(&tp->t_sigio);
1573 /* Obtain terminal flags through tcgetattr(). */
1574 *(struct termios*)data = tp->t_termios;
1579 struct termios *t = data;
1582 * Who makes up these funny rules? According to POSIX,
1583 * input baud rate is set equal to the output baud rate
1586 if (t->c_ispeed == 0)
1587 t->c_ispeed = t->c_ospeed;
1589 /* Discard any unsupported bits. */
1590 t->c_iflag &= TTYSUP_IFLAG;
1591 t->c_oflag &= TTYSUP_OFLAG;
1592 t->c_lflag &= TTYSUP_LFLAG;
1593 t->c_cflag &= TTYSUP_CFLAG;
1595 /* Set terminal flags through tcsetattr(). */
1596 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1597 error = tty_drain(tp, 0);
1600 if (cmd == TIOCSETAF)
1601 tty_flush(tp, FREAD);
1605 * Only call param() when the flags really change.
1607 if ((t->c_cflag & CIGNORE) == 0 &&
1608 (tp->t_termios.c_cflag != t->c_cflag ||
1609 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1610 (IXON|IXOFF|IXANY)) ||
1611 tp->t_termios.c_ispeed != t->c_ispeed ||
1612 tp->t_termios.c_ospeed != t->c_ospeed)) {
1613 error = ttydevsw_param(tp, t);
1619 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1620 tp->t_termios.c_ispeed = t->c_ispeed;
1621 tp->t_termios.c_ospeed = t->c_ospeed;
1623 /* Baud rate has changed - update watermarks. */
1627 /* Copy new non-device driver parameters. */
1628 tp->t_termios.c_iflag = t->c_iflag;
1629 tp->t_termios.c_oflag = t->c_oflag;
1630 tp->t_termios.c_lflag = t->c_lflag;
1631 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1633 ttydisc_optimize(tp);
1635 if ((t->c_lflag & ICANON) == 0) {
1637 * When in non-canonical mode, wake up all
1638 * readers. Canonicalize any partial input. VMIN
1639 * and VTIME could also be adjusted.
1641 ttyinq_canonicalize(&tp->t_inq);
1642 tty_wakeup(tp, FREAD);
1646 * For packet mode: notify the PTY consumer that VSTOP
1647 * and VSTART may have been changed.
1649 if (tp->t_termios.c_iflag & IXON &&
1650 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1651 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1652 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1654 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1658 /* For compatibility - we only support TTYDISC. */
1659 *(int *)data = TTYDISC;
1662 if (!tty_is_ctty(tp, td->td_proc))
1665 if (tp->t_pgrp != NULL)
1666 *(int *)data = tp->t_pgrp->pg_id;
1668 *(int *)data = NO_PID;
1671 if (!tty_is_ctty(tp, td->td_proc))
1674 MPASS(tp->t_session);
1675 *(int *)data = tp->t_session->s_sid;
1678 struct proc *p = td->td_proc;
1680 /* XXX: This looks awful. */
1682 sx_xlock(&proctree_lock);
1685 if (!SESS_LEADER(p)) {
1686 /* Only the session leader may do this. */
1687 sx_xunlock(&proctree_lock);
1691 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1692 /* This is already our controlling TTY. */
1693 sx_xunlock(&proctree_lock);
1697 if (p->p_session->s_ttyp != NULL ||
1698 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1699 tp->t_session->s_ttyvp->v_type != VBAD)) {
1701 * There is already a relation between a TTY and
1702 * a session, or the caller is not the session
1705 * Allow the TTY to be stolen when the vnode is
1706 * invalid, but the reference to the TTY is
1707 * still active. This allows immediate reuse of
1708 * TTYs of which the session leader has been
1709 * killed or the TTY revoked.
1711 sx_xunlock(&proctree_lock);
1715 /* Connect the session to the TTY. */
1716 tp->t_session = p->p_session;
1717 tp->t_session->s_ttyp = tp;
1719 sx_xunlock(&proctree_lock);
1721 /* Assign foreground process group. */
1722 tp->t_pgrp = p->p_pgrp;
1724 p->p_flag |= P_CONTROLT;
1733 * XXX: Temporarily unlock the TTY to locate the process
1734 * group. This code would be lot nicer if we would ever
1735 * decompose proctree_lock.
1738 sx_slock(&proctree_lock);
1739 pg = pgfind(*(int *)data);
1742 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1743 sx_sunlock(&proctree_lock);
1750 * Determine if this TTY is the controlling TTY after
1751 * relocking the TTY.
1753 if (!tty_is_ctty(tp, td->td_proc)) {
1754 sx_sunlock(&proctree_lock);
1758 sx_sunlock(&proctree_lock);
1760 /* Wake up the background process groups. */
1761 cv_broadcast(&tp->t_bgwait);
1765 int flags = *(int *)data;
1768 flags = (FREAD|FWRITE);
1770 flags &= (FREAD|FWRITE);
1771 tty_flush(tp, flags);
1775 /* Drain TTY output. */
1776 return tty_drain(tp, 0);
1778 /* Set terminal as console TTY. */
1780 error = priv_check(td, PRIV_TTY_CONSOLE);
1785 * XXX: constty should really need to be locked!
1786 * XXX: allow disconnected constty's to be stolen!
1791 if (constty != NULL)
1797 } else if (constty == tp) {
1802 /* Obtain window size. */
1803 *(struct winsize*)data = tp->t_winsize;
1806 /* Set window size. */
1807 tty_set_winsize(tp, data);
1810 tp->t_flags |= TF_EXCLUDE;
1813 tp->t_flags &= ~TF_EXCLUDE;
1816 tp->t_flags |= TF_STOPPED;
1817 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1820 tp->t_flags &= ~TF_STOPPED;
1821 ttydevsw_outwakeup(tp);
1822 ttydevsw_pktnotify(tp, TIOCPKT_START);
1828 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1830 if (!tty_is_ctty(tp, td->td_proc) &&
1831 priv_check(td, PRIV_TTY_STI))
1833 ttydisc_rint(tp, *(char *)data, 0);
1834 ttydisc_rint_done(tp);
1839 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1840 #else /* !COMPAT_43TTY */
1842 #endif /* COMPAT_43TTY */
1846 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1850 tty_lock_assert(tp, MA_OWNED);
1855 error = ttydevsw_ioctl(tp, cmd, data, td);
1856 if (error == ENOIOCTL)
1857 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1863 tty_udev(struct tty *tp)
1867 return (dev2udev(tp->t_dev));
1873 tty_checkoutq(struct tty *tp)
1876 /* 256 bytes should be enough to print a log message. */
1877 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1881 tty_hiwat_in_block(struct tty *tp)
1884 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1885 tp->t_termios.c_iflag & IXOFF &&
1886 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1888 * Input flow control. Only enter the high watermark when we
1889 * can successfully store the VSTOP character.
1891 if (ttyoutq_write_nofrag(&tp->t_outq,
1892 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1893 tp->t_flags |= TF_HIWAT_IN;
1895 /* No input flow control. */
1896 tp->t_flags |= TF_HIWAT_IN;
1901 tty_hiwat_in_unblock(struct tty *tp)
1904 if (tp->t_flags & TF_HIWAT_IN &&
1905 tp->t_termios.c_iflag & IXOFF &&
1906 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1908 * Input flow control. Only leave the high watermark when we
1909 * can successfully store the VSTART character.
1911 if (ttyoutq_write_nofrag(&tp->t_outq,
1912 &tp->t_termios.c_cc[VSTART], 1) == 0)
1913 tp->t_flags &= ~TF_HIWAT_IN;
1915 /* No input flow control. */
1916 tp->t_flags &= ~TF_HIWAT_IN;
1920 ttydevsw_inwakeup(tp);
1924 * TTY hooks interface.
1928 ttyhook_defrint(struct tty *tp, char c, int flags)
1931 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1938 ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
1945 struct filedesc *fdp;
1946 cap_rights_t rights;
1949 /* Validate the file descriptor. */
1951 error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1955 if (fp->f_ops == &badfileops) {
1961 * Make sure the vnode is bound to a character device.
1962 * Unlocked check for the vnode type is ok there, because we
1963 * only shall prevent calling devvn_refthread on the file that
1964 * never has been opened over a character device.
1966 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1971 /* Make sure it is a TTY. */
1972 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
1977 if (dev != fp->f_data) {
1981 if (cdp != &ttydev_cdevsw) {
1987 /* Try to attach the hook to the TTY. */
1990 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1991 if (tp->t_flags & TF_HOOK)
1994 tp->t_flags |= TF_HOOK;
1996 tp->t_hooksoftc = softc;
2000 /* Maybe we can switch into bypass mode now. */
2001 ttydisc_optimize(tp);
2003 /* Silently convert rint() calls to rint_bypass() when possible. */
2004 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2005 th->th_rint = ttyhook_defrint;
2007 done3: tty_unlock(tp);
2008 done2: dev_relthread(dev, ref);
2009 done1: fdrop(fp, curthread);
2014 ttyhook_unregister(struct tty *tp)
2017 tty_lock_assert(tp, MA_OWNED);
2018 MPASS(tp->t_flags & TF_HOOK);
2020 /* Disconnect the hook. */
2021 tp->t_flags &= ~TF_HOOK;
2024 /* Maybe we need to leave bypass mode. */
2025 ttydisc_optimize(tp);
2027 /* Maybe deallocate the TTY as well. */
2032 * /dev/console handling.
2036 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2040 /* System has no console device. */
2041 if (dev_console_filename == NULL)
2044 /* Look up corresponding TTY by device name. */
2045 sx_slock(&tty_list_sx);
2046 TAILQ_FOREACH(tp, &tty_list, t_list) {
2047 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2048 dev_console->si_drv1 = tp;
2052 sx_sunlock(&tty_list_sx);
2054 /* System console has no TTY associated. */
2055 if (dev_console->si_drv1 == NULL)
2058 return (ttydev_open(dev, oflags, devtype, td));
2062 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2067 return (ttydev_write(dev, uio, ioflag));
2071 * /dev/console is a little different than normal TTY's. When opened,
2072 * it determines which TTY to use. When data gets written to it, it
2073 * will be logged in the kernel message buffer.
2075 static struct cdevsw ttyconsdev_cdevsw = {
2076 .d_version = D_VERSION,
2077 .d_open = ttyconsdev_open,
2078 .d_close = ttydev_close,
2079 .d_read = ttydev_read,
2080 .d_write = ttyconsdev_write,
2081 .d_ioctl = ttydev_ioctl,
2082 .d_kqfilter = ttydev_kqfilter,
2083 .d_poll = ttydev_poll,
2084 .d_mmap = ttydev_mmap,
2085 .d_name = "ttyconsdev",
2090 ttyconsdev_init(void *unused __unused)
2093 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2094 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2097 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2100 ttyconsdev_select(const char *name)
2103 dev_console_filename = name;
2107 * Debugging routines.
2110 #include "opt_ddb.h"
2112 #include <ddb/ddb.h>
2113 #include <ddb/db_sym.h>
2115 static const struct {
2120 { TF_NOPREFIX, 'N' },
2122 { TF_INITLOCK, 'I' },
2123 { TF_CALLOUT, 'C' },
2125 /* Keep these together -> 'Oi' and 'Oo'. */
2127 { TF_OPENED_IN, 'i' },
2128 { TF_OPENED_OUT, 'o' },
2129 { TF_OPENED_CONS, 'c' },
2132 { TF_OPENCLOSE, 'B' },
2134 { TF_LITERAL, 'L' },
2136 /* Keep these together -> 'Hi' and 'Ho'. */
2138 { TF_HIWAT_IN, 'i' },
2139 { TF_HIWAT_OUT, 'o' },
2141 { TF_STOPPED, 'S' },
2142 { TF_EXCLUDE, 'X' },
2147 /* Keep these together -> 'bi' and 'bo'. */
2149 { TF_BUSY_IN, 'i' },
2150 { TF_BUSY_OUT, 'o' },
2155 #define TTY_FLAG_BITS \
2156 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2157 "\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2158 "\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2159 "\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2160 "\21HOOK\22BUSY_IN\23BUSY_OUT"
2162 #define DB_PRINTSYM(name, addr) \
2163 db_printf("%s " #name ": ", sep); \
2164 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2168 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2171 db_printf("%sdevsw: ", sep);
2172 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2173 db_printf(" (%p)\n", tsw);
2174 DB_PRINTSYM(open, tsw->tsw_open);
2175 DB_PRINTSYM(close, tsw->tsw_close);
2176 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2177 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2178 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2179 DB_PRINTSYM(param, tsw->tsw_param);
2180 DB_PRINTSYM(modem, tsw->tsw_modem);
2181 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2182 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2183 DB_PRINTSYM(free, tsw->tsw_free);
2187 _db_show_hooks(const char *sep, const struct ttyhook *th)
2190 db_printf("%shook: ", sep);
2191 db_printsym((db_addr_t)th, DB_STGY_ANY);
2192 db_printf(" (%p)\n", th);
2195 DB_PRINTSYM(rint, th->th_rint);
2196 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2197 DB_PRINTSYM(rint_done, th->th_rint_done);
2198 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2199 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2200 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2201 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2202 DB_PRINTSYM(close, th->th_close);
2206 _db_show_termios(const char *name, const struct termios *t)
2209 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2210 "lflag 0x%x ispeed %u ospeed %u\n", name,
2211 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2212 t->c_ispeed, t->c_ospeed);
2215 /* DDB command to show TTY statistics. */
2216 DB_SHOW_COMMAND(tty, db_show_tty)
2221 db_printf("usage: show tty <addr>\n");
2224 tp = (struct tty *)addr;
2226 db_printf("%p: %s\n", tp, tty_devname(tp));
2227 db_printf("\tmtx: %p\n", tp->t_mtx);
2228 db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2229 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2231 /* Buffering mechanisms. */
2232 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2233 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2234 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2235 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2236 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2237 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2238 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2239 db_printf("\tinlow: %zu\n", tp->t_inlow);
2240 db_printf("\toutlow: %zu\n", tp->t_outlow);
2241 _db_show_termios("\ttermios", &tp->t_termios);
2242 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2243 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2244 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2245 db_printf("\tcolumn: %u\n", tp->t_column);
2246 db_printf("\twritepos: %u\n", tp->t_writepos);
2247 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2249 /* Init/lock-state devices. */
2250 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2251 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2252 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2253 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2256 _db_show_devsw("\t", tp->t_devsw);
2257 _db_show_hooks("\t", tp->t_hook);
2260 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2261 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2262 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2263 db_printf("\tsession: %p", tp->t_session);
2264 if (tp->t_session != NULL)
2265 db_printf(" count %u leader %p tty %p sid %d login %s",
2266 tp->t_session->s_count, tp->t_session->s_leader,
2267 tp->t_session->s_ttyp, tp->t_session->s_sid,
2268 tp->t_session->s_login);
2270 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2271 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2272 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2273 db_printf("\tdev: %p\n", tp->t_dev);
2276 /* DDB command to list TTYs. */
2277 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2283 /* Make the output look like `pstat -t'. */
2285 #if defined(__LP64__)
2288 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2289 "COL SESS PGID STATE\n");
2291 TAILQ_FOREACH(tp, &tty_list, t_list) {
2292 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2293 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2295 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2296 "%5d ", tp, tty_devname(tp), isiz,
2297 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2298 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2299 isiz - tp->t_inlow, osiz,
2300 tp->t_outq.to_end - tp->t_outq.to_begin,
2301 osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2302 tp->t_session ? tp->t_session->s_sid : 0,
2303 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2306 for (i = j = 0; ttystates[i].flag; i++)
2307 if (tp->t_flags & ttystates[i].flag) {
2308 db_printf("%c", ttystates[i].val);