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)
98 static int tty_drainwait = 5 * 60;
99 SYSCTL_INT(_kern, OID_AUTO, tty_drainwait, CTLFLAG_RWTUN,
100 &tty_drainwait, 0, "Default output drain timeout in seconds");
103 * Set TTY buffer sizes.
106 #define TTYBUF_MAX 65536
109 * Allocate buffer space if necessary, and set low watermarks, based on speed.
110 * Note that the ttyxxxq_setsize() functions may drop and then reacquire the tty
111 * lock during memory allocation. They will return ENXIO if the tty disappears
115 tty_watermarks(struct tty *tp)
120 /* Provide an input buffer for 2 seconds of data. */
121 if (tp->t_termios.c_cflag & CREAD)
122 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX);
123 error = ttyinq_setsize(&tp->t_inq, tp, bs);
127 /* Set low watermark at 10% (when 90% is available). */
128 tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
130 /* Provide an output buffer for 2 seconds of data. */
131 bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX);
132 error = ttyoutq_setsize(&tp->t_outq, tp, bs);
136 /* Set low watermark at 10% (when 90% is available). */
137 tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
143 tty_drain(struct tty *tp, int leaving)
145 sbintime_t timeout_at;
149 if (ttyhook_hashook(tp, getc_inject))
150 /* buffer is inaccessible */
154 * For close(), use the recent historic timeout of "1 second without
155 * making progress". For tcdrain(), use t_drainwait as the timeout,
156 * with zero meaning "no timeout" which gives POSIX behavior.
159 timeout_at = getsbinuptime() + SBT_1S;
160 else if (tp->t_drainwait != 0)
161 timeout_at = getsbinuptime() + SBT_1S * tp->t_drainwait;
166 * Poll the output buffer and the hardware for completion, at 10 Hz.
167 * Polling is required for devices which are not able to signal an
168 * interrupt when the transmitter becomes idle (most USB serial devs).
169 * The unusual structure of this loop ensures we check for busy one more
170 * time after tty_timedwait() returns EWOULDBLOCK, so that success has
171 * higher priority than timeout if the IO completed in the last 100mS.
174 bytes = ttyoutq_bytesused(&tp->t_outq);
176 if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp))
180 ttydevsw_outwakeup(tp);
181 error = tty_timedwait(tp, &tp->t_outwait, hz / 10);
182 if (error != 0 && error != EWOULDBLOCK)
184 else if (timeout_at == 0 || getsbinuptime() < timeout_at)
186 else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes) {
187 /* In close, making progress, grant an extra second. */
189 timeout_at += SBT_1S;
190 bytes = ttyoutq_bytesused(&tp->t_outq);
196 * Though ttydev_enter() and ttydev_leave() seem to be related, they
197 * don't have to be used together. ttydev_enter() is used by the cdev
198 * operations to prevent an actual operation from being processed when
199 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
200 * and ttydev_close() to determine whether per-TTY data should be
205 ttydev_enter(struct tty *tp)
210 if (tty_gone(tp) || !tty_opened(tp)) {
211 /* Device is already gone. */
220 ttydev_leave(struct tty *tp)
223 tty_lock_assert(tp, MA_OWNED);
225 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
226 /* Device is still opened somewhere. */
231 tp->t_flags |= TF_OPENCLOSE;
233 /* Remove console TTY. */
237 /* Drain any output. */
243 /* Free i/o queues now since they might be large. */
244 ttyinq_free(&tp->t_inq);
246 ttyoutq_free(&tp->t_outq);
249 knlist_clear(&tp->t_inpoll.si_note, 1);
250 knlist_clear(&tp->t_outpoll.si_note, 1);
255 tp->t_flags &= ~TF_OPENCLOSE;
256 cv_broadcast(&tp->t_dcdwait);
261 * Operations that are exposed through the character device in /dev.
264 ttydev_open(struct cdev *dev, int oflags, int devtype __unused,
274 /* Device is already gone. */
280 * Block when other processes are currently opening or closing
283 while (tp->t_flags & TF_OPENCLOSE) {
284 error = tty_wait(tp, &tp->t_dcdwait);
290 tp->t_flags |= TF_OPENCLOSE;
293 * Make sure the "tty" and "cua" device cannot be opened at the
294 * same time. The console is a "tty" device.
296 if (TTY_CALLOUT(tp, dev)) {
297 if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) {
302 if (tp->t_flags & TF_OPENED_OUT) {
308 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
313 if (!tty_opened(tp)) {
314 /* Set proper termios flags. */
315 if (TTY_CALLOUT(tp, dev))
316 tp->t_termios = tp->t_termios_init_out;
318 tp->t_termios = tp->t_termios_init_in;
319 ttydevsw_param(tp, &tp->t_termios);
320 /* Prevent modem control on callout devices and /dev/console. */
321 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
322 tp->t_termios.c_cflag |= CLOCAL;
324 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
326 error = ttydevsw_open(tp);
331 error = tty_watermarks(tp);
336 /* Wait for Carrier Detect. */
337 if ((oflags & O_NONBLOCK) == 0 &&
338 (tp->t_termios.c_cflag & CLOCAL) == 0) {
339 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
340 error = tty_wait(tp, &tp->t_dcdwait);
346 if (dev == dev_console)
347 tp->t_flags |= TF_OPENED_CONS;
348 else if (TTY_CALLOUT(tp, dev))
349 tp->t_flags |= TF_OPENED_OUT;
351 tp->t_flags |= TF_OPENED_IN;
352 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
353 (tp->t_flags & TF_OPENED_OUT) == 0);
355 done: tp->t_flags &= ~TF_OPENCLOSE;
356 cv_broadcast(&tp->t_dcdwait);
363 ttydev_close(struct cdev *dev, int fflag, int devtype __unused,
364 struct thread *td __unused)
366 struct tty *tp = dev->si_drv1;
371 * Don't actually close the device if it is being used as the
374 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
375 (tp->t_flags & TF_OPENED_OUT) == 0);
376 if (dev == dev_console)
377 tp->t_flags &= ~TF_OPENED_CONS;
379 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
381 if (tp->t_flags & TF_OPENED) {
386 /* If revoking, flush output now to avoid draining it later. */
388 tty_flush(tp, FWRITE);
390 tp->t_flags &= ~TF_EXCLUDE;
392 /* Properly wake up threads that are stuck - revoke(). */
394 tty_wakeup(tp, FREAD|FWRITE);
395 cv_broadcast(&tp->t_bgwait);
396 cv_broadcast(&tp->t_dcdwait);
404 tty_is_ctty(struct tty *tp, struct proc *p)
407 tty_lock_assert(tp, MA_OWNED);
409 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
413 tty_wait_background(struct tty *tp, struct thread *td, int sig)
415 struct proc *p = td->td_proc;
420 MPASS(sig == SIGTTIN || sig == SIGTTOU);
421 tty_lock_assert(tp, MA_OWNED);
426 * The process should only sleep, when:
427 * - This terminal is the controlling terminal
428 * - Its process group is not the foreground process
430 * - The parent process isn't waiting for the child to
432 * - the signal to send to the process isn't masked
434 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
435 /* Allow the action to happen. */
440 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
441 SIGISMEMBER(td->td_sigmask, sig)) {
442 /* Only allow them in write()/ioctl(). */
444 return (sig == SIGTTOU ? 0 : EIO);
448 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
449 /* Don't allow the action to happen. */
456 * Send the signal and sleep until we're the new
457 * foreground process group.
461 ksi.ksi_code = SI_KERNEL;
466 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
469 error = tty_wait(tp, &tp->t_bgwait);
476 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
478 struct tty *tp = dev->si_drv1;
481 error = ttydev_enter(tp);
484 error = ttydisc_read(tp, uio, ioflag);
488 * The read() call should not throw an error when the device is
489 * being destroyed. Silently convert it to an EOF.
491 done: if (error == ENXIO)
497 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
499 struct tty *tp = dev->si_drv1;
502 error = ttydev_enter(tp);
506 if (tp->t_termios.c_lflag & TOSTOP) {
507 error = tty_wait_background(tp, curthread, SIGTTOU);
512 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
513 /* Allow non-blocking writes to bypass serialization. */
514 error = ttydisc_write(tp, uio, ioflag);
516 /* Serialize write() calls. */
517 while (tp->t_flags & TF_BUSY_OUT) {
518 error = tty_wait(tp, &tp->t_outserwait);
523 tp->t_flags |= TF_BUSY_OUT;
524 error = ttydisc_write(tp, uio, ioflag);
525 tp->t_flags &= ~TF_BUSY_OUT;
526 cv_signal(&tp->t_outserwait);
529 done: tty_unlock(tp);
534 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
537 struct tty *tp = dev->si_drv1;
540 error = ttydev_enter(tp);
575 #endif /* COMPAT_43TTY */
577 * If the ioctl() causes the TTY to be modified, let it
578 * wait in the background.
580 error = tty_wait_background(tp, curthread, SIGTTOU);
585 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
586 struct termios *old = &tp->t_termios;
587 struct termios *new = (struct termios *)data;
588 struct termios *lock = TTY_CALLOUT(tp, dev) ?
589 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
593 * Lock state devices. Just overwrite the values of the
594 * commands that are currently in use.
596 new->c_iflag = (old->c_iflag & lock->c_iflag) |
597 (new->c_iflag & ~lock->c_iflag);
598 new->c_oflag = (old->c_oflag & lock->c_oflag) |
599 (new->c_oflag & ~lock->c_oflag);
600 new->c_cflag = (old->c_cflag & lock->c_cflag) |
601 (new->c_cflag & ~lock->c_cflag);
602 new->c_lflag = (old->c_lflag & lock->c_lflag) |
603 (new->c_lflag & ~lock->c_lflag);
604 for (cc = 0; cc < NCCS; ++cc)
606 new->c_cc[cc] = old->c_cc[cc];
608 new->c_ispeed = old->c_ispeed;
610 new->c_ospeed = old->c_ospeed;
613 error = tty_ioctl(tp, cmd, data, fflag, td);
614 done: tty_unlock(tp);
620 ttydev_poll(struct cdev *dev, int events, struct thread *td)
622 struct tty *tp = dev->si_drv1;
623 int error, revents = 0;
625 error = ttydev_enter(tp);
627 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
629 if (events & (POLLIN|POLLRDNORM)) {
630 /* See if we can read something. */
631 if (ttydisc_read_poll(tp) > 0)
632 revents |= events & (POLLIN|POLLRDNORM);
635 if (tp->t_flags & TF_ZOMBIE) {
636 /* Hangup flag on zombie state. */
638 } else if (events & (POLLOUT|POLLWRNORM)) {
639 /* See if we can write something. */
640 if (ttydisc_write_poll(tp) > 0)
641 revents |= events & (POLLOUT|POLLWRNORM);
645 if (events & (POLLIN|POLLRDNORM))
646 selrecord(td, &tp->t_inpoll);
647 if (events & (POLLOUT|POLLWRNORM))
648 selrecord(td, &tp->t_outpoll);
657 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
658 int nprot, vm_memattr_t *memattr)
660 struct tty *tp = dev->si_drv1;
663 /* Handle mmap() through the driver. */
665 error = ttydev_enter(tp);
668 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
679 tty_kqops_read_detach(struct knote *kn)
681 struct tty *tp = kn->kn_hook;
683 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
687 tty_kqops_read_event(struct knote *kn, long hint __unused)
689 struct tty *tp = kn->kn_hook;
691 tty_lock_assert(tp, MA_OWNED);
693 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
694 kn->kn_flags |= EV_EOF;
697 kn->kn_data = ttydisc_read_poll(tp);
698 return (kn->kn_data > 0);
703 tty_kqops_write_detach(struct knote *kn)
705 struct tty *tp = kn->kn_hook;
707 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
711 tty_kqops_write_event(struct knote *kn, long hint __unused)
713 struct tty *tp = kn->kn_hook;
715 tty_lock_assert(tp, MA_OWNED);
718 kn->kn_flags |= EV_EOF;
721 kn->kn_data = ttydisc_write_poll(tp);
722 return (kn->kn_data > 0);
726 static struct filterops tty_kqops_read = {
728 .f_detach = tty_kqops_read_detach,
729 .f_event = tty_kqops_read_event,
732 static struct filterops tty_kqops_write = {
734 .f_detach = tty_kqops_write_detach,
735 .f_event = tty_kqops_write_event,
739 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
741 struct tty *tp = dev->si_drv1;
744 error = ttydev_enter(tp);
748 switch (kn->kn_filter) {
751 kn->kn_fop = &tty_kqops_read;
752 knlist_add(&tp->t_inpoll.si_note, kn, 1);
756 kn->kn_fop = &tty_kqops_write;
757 knlist_add(&tp->t_outpoll.si_note, kn, 1);
768 static struct cdevsw ttydev_cdevsw = {
769 .d_version = D_VERSION,
770 .d_open = ttydev_open,
771 .d_close = ttydev_close,
772 .d_read = ttydev_read,
773 .d_write = ttydev_write,
774 .d_ioctl = ttydev_ioctl,
775 .d_kqfilter = ttydev_kqfilter,
776 .d_poll = ttydev_poll,
777 .d_mmap = ttydev_mmap,
783 * Init/lock-state devices
787 ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
804 ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
805 struct thread *td __unused)
812 ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
820 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
823 struct tty *tp = dev->si_drv1;
832 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
833 if (error != ENOIOCTL)
839 /* Obtain terminal flags through tcgetattr(). */
840 *(struct termios*)data = *(struct termios*)dev->si_drv2;
843 /* Set terminal flags through tcsetattr(). */
844 error = priv_check(td, PRIV_TTY_SETA);
847 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
850 *(int *)data = TTYDISC;
853 bzero(data, sizeof(struct winsize));
859 done: tty_unlock(tp);
863 static struct cdevsw ttyil_cdevsw = {
864 .d_version = D_VERSION,
865 .d_open = ttyil_open,
866 .d_close = ttyil_close,
867 .d_read = ttyil_rdwr,
868 .d_write = ttyil_rdwr,
869 .d_ioctl = ttyil_ioctl,
875 tty_init_termios(struct tty *tp)
877 struct termios *t = &tp->t_termios_init_in;
879 t->c_cflag = TTYDEF_CFLAG;
880 t->c_iflag = TTYDEF_IFLAG;
881 t->c_lflag = TTYDEF_LFLAG;
882 t->c_oflag = TTYDEF_OFLAG;
883 t->c_ispeed = TTYDEF_SPEED;
884 t->c_ospeed = TTYDEF_SPEED;
885 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
887 tp->t_termios_init_out = *t;
891 tty_init_console(struct tty *tp, speed_t s)
893 struct termios *ti = &tp->t_termios_init_in;
894 struct termios *to = &tp->t_termios_init_out;
897 ti->c_ispeed = ti->c_ospeed = s;
898 to->c_ispeed = to->c_ospeed = s;
901 ti->c_cflag |= CLOCAL;
902 to->c_cflag |= CLOCAL;
906 * Standard device routine implementations, mostly meant for
907 * pseudo-terminal device drivers. When a driver creates a new terminal
908 * device class, missing routines are patched.
912 ttydevsw_defopen(struct tty *tp __unused)
919 ttydevsw_defclose(struct tty *tp __unused)
925 ttydevsw_defoutwakeup(struct tty *tp __unused)
928 panic("Terminal device has output, while not implemented");
932 ttydevsw_definwakeup(struct tty *tp __unused)
938 ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
939 caddr_t data __unused, struct thread *td __unused)
946 ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
947 u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
954 ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
958 * Allow the baud rate to be adjusted for pseudo-devices, but at
959 * least restrict it to 115200 to prevent excessive buffer
960 * usage. Also disallow 0, to prevent foot shooting.
962 if (t->c_ispeed < B50)
964 else if (t->c_ispeed > B115200)
965 t->c_ispeed = B115200;
966 if (t->c_ospeed < B50)
968 else if (t->c_ospeed > B115200)
969 t->c_ospeed = B115200;
976 ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
980 /* Simulate a carrier to make the TTY layer happy. */
985 ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
986 vm_paddr_t *paddr __unused, int nprot __unused,
987 vm_memattr_t *memattr __unused)
994 ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
1000 ttydevsw_deffree(void *softc __unused)
1003 panic("Terminal device freed without a free-handler");
1007 ttydevsw_defbusy(struct tty *tp __unused)
1014 * TTY allocation and deallocation. TTY devices can be deallocated when
1015 * the driver doesn't use it anymore, when the TTY isn't a session's
1016 * controlling TTY and when the device node isn't opened through devfs.
1020 tty_alloc(struct ttydevsw *tsw, void *sc)
1023 return (tty_alloc_mutex(tsw, sc, NULL));
1027 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1031 /* Make sure the driver defines all routines. */
1032 #define PATCH_FUNC(x) do { \
1033 if (tsw->tsw_ ## x == NULL) \
1034 tsw->tsw_ ## x = ttydevsw_def ## x; \
1038 PATCH_FUNC(outwakeup);
1039 PATCH_FUNC(inwakeup);
1045 PATCH_FUNC(pktnotify);
1050 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
1052 tp->t_devswsoftc = sc;
1053 tp->t_flags = tsw->tsw_flags;
1054 tp->t_drainwait = tty_drainwait;
1056 tty_init_termios(tp);
1058 cv_init(&tp->t_inwait, "ttyin");
1059 cv_init(&tp->t_outwait, "ttyout");
1060 cv_init(&tp->t_outserwait, "ttyosr");
1061 cv_init(&tp->t_bgwait, "ttybg");
1062 cv_init(&tp->t_dcdwait, "ttydcd");
1064 /* Allow drivers to use a custom mutex to lock the TTY. */
1065 if (mutex != NULL) {
1068 tp->t_mtx = &tp->t_mtxobj;
1069 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1072 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1073 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1079 tty_dealloc(void *arg)
1081 struct tty *tp = arg;
1084 * ttyydev_leave() usually frees the i/o queues earlier, but it is
1085 * not always called between queue allocation and here. The queues
1086 * may be allocated by ioctls on a pty control device without the
1087 * corresponding pty slave device ever being open, or after it is
1090 ttyinq_free(&tp->t_inq);
1091 ttyoutq_free(&tp->t_outq);
1092 seldrain(&tp->t_inpoll);
1093 seldrain(&tp->t_outpoll);
1094 knlist_destroy(&tp->t_inpoll.si_note);
1095 knlist_destroy(&tp->t_outpoll.si_note);
1097 cv_destroy(&tp->t_inwait);
1098 cv_destroy(&tp->t_outwait);
1099 cv_destroy(&tp->t_bgwait);
1100 cv_destroy(&tp->t_dcdwait);
1101 cv_destroy(&tp->t_outserwait);
1103 if (tp->t_mtx == &tp->t_mtxobj)
1104 mtx_destroy(&tp->t_mtxobj);
1110 tty_rel_free(struct tty *tp)
1114 tty_lock_assert(tp, MA_OWNED);
1116 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1117 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1118 /* TTY is still in use. */
1123 /* Stop asynchronous I/O. */
1124 funsetown(&tp->t_sigio);
1126 /* Stop asynchronous I/O. */
1127 funsetown(&tp->t_sigio);
1129 /* TTY can be deallocated. */
1135 sx_xlock(&tty_list_sx);
1136 TAILQ_REMOVE(&tty_list, tp, t_list);
1138 sx_xunlock(&tty_list_sx);
1139 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1144 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1147 MPASS(tp->t_sessioncnt > 0);
1148 tty_lock_assert(tp, MA_OWNED);
1150 if (tp->t_pgrp == pg)
1157 tty_rel_sess(struct tty *tp, struct session *sess)
1160 MPASS(tp->t_sessioncnt > 0);
1162 /* Current session has left. */
1163 if (tp->t_session == sess) {
1164 tp->t_session = NULL;
1165 MPASS(tp->t_pgrp == NULL);
1172 tty_rel_gone(struct tty *tp)
1175 MPASS(!tty_gone(tp));
1177 /* Simulate carrier removal. */
1178 ttydisc_modem(tp, 0);
1180 /* Wake up all blocked threads. */
1181 tty_wakeup(tp, FREAD|FWRITE);
1182 cv_broadcast(&tp->t_bgwait);
1183 cv_broadcast(&tp->t_dcdwait);
1185 tp->t_flags |= TF_GONE;
1190 * Exposing information about current TTY's through sysctl
1194 tty_to_xtty(struct tty *tp, struct xtty *xt)
1197 tty_lock_assert(tp, MA_OWNED);
1199 xt->xt_size = sizeof(struct xtty);
1200 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1201 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1202 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1203 xt->xt_inlow = tp->t_inlow;
1204 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1205 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1206 xt->xt_outlow = tp->t_outlow;
1207 xt->xt_column = tp->t_column;
1208 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1209 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1210 xt->xt_flags = tp->t_flags;
1211 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1215 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1217 unsigned long lsize;
1218 struct xtty *xtlist, *xt;
1222 sx_slock(&tty_list_sx);
1223 lsize = tty_list_count * sizeof(struct xtty);
1225 sx_sunlock(&tty_list_sx);
1229 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1231 TAILQ_FOREACH(tp, &tty_list, t_list) {
1233 tty_to_xtty(tp, xt);
1237 sx_sunlock(&tty_list_sx);
1239 error = SYSCTL_OUT(req, xtlist, lsize);
1240 free(xtlist, M_TTY);
1244 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1245 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1248 * Device node creation. Device has been set up, now we can expose it to
1253 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1254 const char *fmt, ...)
1257 struct make_dev_args args;
1258 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1259 const char *prefix = "tty";
1260 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1266 /* Remove "tty" prefix from devices like PTY's. */
1267 if (tp->t_flags & TF_NOPREFIX)
1271 vsnrprintf(name, sizeof name, 32, fmt, ap);
1275 /* System device. */
1278 mode = S_IRUSR|S_IWUSR;
1281 uid = cred->cr_ruid;
1283 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1286 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1287 flags |= MAKEDEV_CHECKNAME;
1289 /* Master call-in device. */
1290 make_dev_args_init(&args);
1291 args.mda_flags = flags;
1292 args.mda_devsw = &ttydev_cdevsw;
1296 args.mda_mode = mode;
1297 args.mda_si_drv1 = tp;
1298 error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1303 init = lock = cua = cinit = clock = NULL;
1305 /* Slave call-in devices. */
1306 if (tp->t_flags & TF_INITLOCK) {
1307 args.mda_devsw = &ttyil_cdevsw;
1308 args.mda_unit = TTYUNIT_INIT;
1309 args.mda_si_drv1 = tp;
1310 args.mda_si_drv2 = &tp->t_termios_init_in;
1311 error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1314 dev_depends(dev, init);
1316 args.mda_unit = TTYUNIT_LOCK;
1317 args.mda_si_drv2 = &tp->t_termios_lock_in;
1318 error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1321 dev_depends(dev, lock);
1324 /* Call-out devices. */
1325 if (tp->t_flags & TF_CALLOUT) {
1326 make_dev_args_init(&args);
1327 args.mda_flags = flags;
1328 args.mda_devsw = &ttydev_cdevsw;
1330 args.mda_uid = UID_UUCP;
1331 args.mda_gid = GID_DIALER;
1332 args.mda_mode = 0660;
1333 args.mda_unit = TTYUNIT_CALLOUT;
1334 args.mda_si_drv1 = tp;
1335 error = make_dev_s(&args, &cua, "cua%s", name);
1338 dev_depends(dev, cua);
1340 /* Slave call-out devices. */
1341 if (tp->t_flags & TF_INITLOCK) {
1342 args.mda_devsw = &ttyil_cdevsw;
1343 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1344 args.mda_si_drv2 = &tp->t_termios_init_out;
1345 error = make_dev_s(&args, &cinit, "cua%s.init", name);
1348 dev_depends(dev, cinit);
1350 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1351 args.mda_si_drv2 = &tp->t_termios_lock_out;
1352 error = make_dev_s(&args, &clock, "cua%s.lock", name);
1355 dev_depends(dev, clock);
1359 sx_xlock(&tty_list_sx);
1360 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1362 sx_xunlock(&tty_list_sx);
1381 * Signalling processes.
1385 tty_signal_sessleader(struct tty *tp, int sig)
1389 tty_lock_assert(tp, MA_OWNED);
1390 MPASS(sig >= 1 && sig < NSIG);
1392 /* Make signals start output again. */
1393 tp->t_flags &= ~TF_STOPPED;
1395 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1396 p = tp->t_session->s_leader;
1398 kern_psignal(p, sig);
1404 tty_signal_pgrp(struct tty *tp, int sig)
1408 tty_lock_assert(tp, MA_OWNED);
1409 MPASS(sig >= 1 && sig < NSIG);
1411 /* Make signals start output again. */
1412 tp->t_flags &= ~TF_STOPPED;
1414 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1416 if (tp->t_pgrp != NULL) {
1417 ksiginfo_init(&ksi);
1418 ksi.ksi_signo = sig;
1419 ksi.ksi_code = SI_KERNEL;
1420 PGRP_LOCK(tp->t_pgrp);
1421 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1422 PGRP_UNLOCK(tp->t_pgrp);
1427 tty_wakeup(struct tty *tp, int flags)
1430 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1431 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1433 if (flags & FWRITE) {
1434 cv_broadcast(&tp->t_outwait);
1435 selwakeup(&tp->t_outpoll);
1436 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1438 if (flags & FREAD) {
1439 cv_broadcast(&tp->t_inwait);
1440 selwakeup(&tp->t_inpoll);
1441 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1446 tty_wait(struct tty *tp, struct cv *cv)
1449 int revokecnt = tp->t_revokecnt;
1451 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1452 MPASS(!tty_gone(tp));
1454 error = cv_wait_sig(cv, tp->t_mtx);
1456 /* Bail out when the device slipped away. */
1460 /* Restart the system call when we may have been revoked. */
1461 if (tp->t_revokecnt != revokecnt)
1468 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1471 int revokecnt = tp->t_revokecnt;
1473 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1474 MPASS(!tty_gone(tp));
1476 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1478 /* Bail out when the device slipped away. */
1482 /* Restart the system call when we may have been revoked. */
1483 if (tp->t_revokecnt != revokecnt)
1490 tty_flush(struct tty *tp, int flags)
1493 if (flags & FWRITE) {
1494 tp->t_flags &= ~TF_HIWAT_OUT;
1495 ttyoutq_flush(&tp->t_outq);
1496 tty_wakeup(tp, FWRITE);
1497 if (!tty_gone(tp)) {
1498 ttydevsw_outwakeup(tp);
1499 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1502 if (flags & FREAD) {
1503 tty_hiwat_in_unblock(tp);
1504 ttyinq_flush(&tp->t_inq);
1505 tty_wakeup(tp, FREAD);
1506 if (!tty_gone(tp)) {
1507 ttydevsw_inwakeup(tp);
1508 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1514 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1517 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1519 tp->t_winsize = *wsz;
1520 tty_signal_pgrp(tp, SIGWINCH);
1524 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1532 * The SER_* and TIOCM_* flags are the same, but one bit
1533 * shifted. I don't know why.
1536 ttydevsw_modem(tp, SER_DTR, 0);
1539 ttydevsw_modem(tp, 0, SER_DTR);
1542 int bits = *(int *)data;
1544 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1545 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1549 int bits = *(int *)data;
1550 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1554 int bits = *(int *)data;
1555 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1559 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1564 tp->t_flags |= TF_ASYNC;
1566 tp->t_flags &= ~TF_ASYNC;
1569 /* This device supports non-blocking operation. */
1572 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1576 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1579 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1580 /* Not allowed to set ownership. */
1583 /* Temporarily unlock the TTY to set ownership. */
1585 error = fsetown(*(int *)data, &tp->t_sigio);
1589 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1590 /* Not allowed to set ownership. */
1593 /* Get ownership. */
1594 *(int *)data = fgetown(&tp->t_sigio);
1597 /* Obtain terminal flags through tcgetattr(). */
1598 *(struct termios*)data = tp->t_termios;
1603 struct termios *t = data;
1606 * Who makes up these funny rules? According to POSIX,
1607 * input baud rate is set equal to the output baud rate
1610 if (t->c_ispeed == 0)
1611 t->c_ispeed = t->c_ospeed;
1613 /* Discard any unsupported bits. */
1614 t->c_iflag &= TTYSUP_IFLAG;
1615 t->c_oflag &= TTYSUP_OFLAG;
1616 t->c_lflag &= TTYSUP_LFLAG;
1617 t->c_cflag &= TTYSUP_CFLAG;
1619 /* Set terminal flags through tcsetattr(). */
1620 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1621 error = tty_drain(tp, 0);
1624 if (cmd == TIOCSETAF)
1625 tty_flush(tp, FREAD);
1629 * Only call param() when the flags really change.
1631 if ((t->c_cflag & CIGNORE) == 0 &&
1632 (tp->t_termios.c_cflag != t->c_cflag ||
1633 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1634 (IXON|IXOFF|IXANY)) ||
1635 tp->t_termios.c_ispeed != t->c_ispeed ||
1636 tp->t_termios.c_ospeed != t->c_ospeed)) {
1637 error = ttydevsw_param(tp, t);
1643 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1644 tp->t_termios.c_ispeed = t->c_ispeed;
1645 tp->t_termios.c_ospeed = t->c_ospeed;
1647 /* Baud rate has changed - update watermarks. */
1648 error = tty_watermarks(tp);
1653 /* Copy new non-device driver parameters. */
1654 tp->t_termios.c_iflag = t->c_iflag;
1655 tp->t_termios.c_oflag = t->c_oflag;
1656 tp->t_termios.c_lflag = t->c_lflag;
1657 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1659 ttydisc_optimize(tp);
1661 if ((t->c_lflag & ICANON) == 0) {
1663 * When in non-canonical mode, wake up all
1664 * readers. Canonicalize any partial input. VMIN
1665 * and VTIME could also be adjusted.
1667 ttyinq_canonicalize(&tp->t_inq);
1668 tty_wakeup(tp, FREAD);
1672 * For packet mode: notify the PTY consumer that VSTOP
1673 * and VSTART may have been changed.
1675 if (tp->t_termios.c_iflag & IXON &&
1676 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1677 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1678 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1680 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1684 /* For compatibility - we only support TTYDISC. */
1685 *(int *)data = TTYDISC;
1688 if (!tty_is_ctty(tp, td->td_proc))
1691 if (tp->t_pgrp != NULL)
1692 *(int *)data = tp->t_pgrp->pg_id;
1694 *(int *)data = NO_PID;
1697 if (!tty_is_ctty(tp, td->td_proc))
1700 MPASS(tp->t_session);
1701 *(int *)data = tp->t_session->s_sid;
1704 struct proc *p = td->td_proc;
1706 /* XXX: This looks awful. */
1708 sx_xlock(&proctree_lock);
1711 if (!SESS_LEADER(p)) {
1712 /* Only the session leader may do this. */
1713 sx_xunlock(&proctree_lock);
1717 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1718 /* This is already our controlling TTY. */
1719 sx_xunlock(&proctree_lock);
1723 if (p->p_session->s_ttyp != NULL ||
1724 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1725 tp->t_session->s_ttyvp->v_type != VBAD)) {
1727 * There is already a relation between a TTY and
1728 * a session, or the caller is not the session
1731 * Allow the TTY to be stolen when the vnode is
1732 * invalid, but the reference to the TTY is
1733 * still active. This allows immediate reuse of
1734 * TTYs of which the session leader has been
1735 * killed or the TTY revoked.
1737 sx_xunlock(&proctree_lock);
1741 /* Connect the session to the TTY. */
1742 tp->t_session = p->p_session;
1743 tp->t_session->s_ttyp = tp;
1745 sx_xunlock(&proctree_lock);
1747 /* Assign foreground process group. */
1748 tp->t_pgrp = p->p_pgrp;
1750 p->p_flag |= P_CONTROLT;
1759 * XXX: Temporarily unlock the TTY to locate the process
1760 * group. This code would be lot nicer if we would ever
1761 * decompose proctree_lock.
1764 sx_slock(&proctree_lock);
1765 pg = pgfind(*(int *)data);
1768 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1769 sx_sunlock(&proctree_lock);
1776 * Determine if this TTY is the controlling TTY after
1777 * relocking the TTY.
1779 if (!tty_is_ctty(tp, td->td_proc)) {
1780 sx_sunlock(&proctree_lock);
1784 sx_sunlock(&proctree_lock);
1786 /* Wake up the background process groups. */
1787 cv_broadcast(&tp->t_bgwait);
1791 int flags = *(int *)data;
1794 flags = (FREAD|FWRITE);
1796 flags &= (FREAD|FWRITE);
1797 tty_flush(tp, flags);
1801 /* Drain TTY output. */
1802 return tty_drain(tp, 0);
1803 case TIOCGDRAINWAIT:
1804 *(int *)data = tp->t_drainwait;
1806 case TIOCSDRAINWAIT:
1807 error = priv_check(td, PRIV_TTY_DRAINWAIT);
1809 tp->t_drainwait = *(int *)data;
1812 /* Set terminal as console TTY. */
1814 error = priv_check(td, PRIV_TTY_CONSOLE);
1819 * XXX: constty should really need to be locked!
1820 * XXX: allow disconnected constty's to be stolen!
1825 if (constty != NULL)
1831 } else if (constty == tp) {
1836 /* Obtain window size. */
1837 *(struct winsize*)data = tp->t_winsize;
1840 /* Set window size. */
1841 tty_set_winsize(tp, data);
1844 tp->t_flags |= TF_EXCLUDE;
1847 tp->t_flags &= ~TF_EXCLUDE;
1850 tp->t_flags |= TF_STOPPED;
1851 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1854 tp->t_flags &= ~TF_STOPPED;
1855 ttydevsw_outwakeup(tp);
1856 ttydevsw_pktnotify(tp, TIOCPKT_START);
1862 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1864 if (!tty_is_ctty(tp, td->td_proc) &&
1865 priv_check(td, PRIV_TTY_STI))
1867 ttydisc_rint(tp, *(char *)data, 0);
1868 ttydisc_rint_done(tp);
1873 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1874 #else /* !COMPAT_43TTY */
1876 #endif /* COMPAT_43TTY */
1880 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1884 tty_lock_assert(tp, MA_OWNED);
1889 error = ttydevsw_ioctl(tp, cmd, data, td);
1890 if (error == ENOIOCTL)
1891 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1897 tty_udev(struct tty *tp)
1901 return (dev2udev(tp->t_dev));
1907 tty_checkoutq(struct tty *tp)
1910 /* 256 bytes should be enough to print a log message. */
1911 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1915 tty_hiwat_in_block(struct tty *tp)
1918 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1919 tp->t_termios.c_iflag & IXOFF &&
1920 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1922 * Input flow control. Only enter the high watermark when we
1923 * can successfully store the VSTOP character.
1925 if (ttyoutq_write_nofrag(&tp->t_outq,
1926 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1927 tp->t_flags |= TF_HIWAT_IN;
1929 /* No input flow control. */
1930 tp->t_flags |= TF_HIWAT_IN;
1935 tty_hiwat_in_unblock(struct tty *tp)
1938 if (tp->t_flags & TF_HIWAT_IN &&
1939 tp->t_termios.c_iflag & IXOFF &&
1940 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1942 * Input flow control. Only leave the high watermark when we
1943 * can successfully store the VSTART character.
1945 if (ttyoutq_write_nofrag(&tp->t_outq,
1946 &tp->t_termios.c_cc[VSTART], 1) == 0)
1947 tp->t_flags &= ~TF_HIWAT_IN;
1949 /* No input flow control. */
1950 tp->t_flags &= ~TF_HIWAT_IN;
1954 ttydevsw_inwakeup(tp);
1958 * TTY hooks interface.
1962 ttyhook_defrint(struct tty *tp, char c, int flags)
1965 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1972 ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
1979 struct filedesc *fdp;
1980 cap_rights_t rights;
1983 /* Validate the file descriptor. */
1985 error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1989 if (fp->f_ops == &badfileops) {
1995 * Make sure the vnode is bound to a character device.
1996 * Unlocked check for the vnode type is ok there, because we
1997 * only shall prevent calling devvn_refthread on the file that
1998 * never has been opened over a character device.
2000 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
2005 /* Make sure it is a TTY. */
2006 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
2011 if (dev != fp->f_data) {
2015 if (cdp != &ttydev_cdevsw) {
2021 /* Try to attach the hook to the TTY. */
2024 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
2025 if (tp->t_flags & TF_HOOK)
2028 tp->t_flags |= TF_HOOK;
2030 tp->t_hooksoftc = softc;
2034 /* Maybe we can switch into bypass mode now. */
2035 ttydisc_optimize(tp);
2037 /* Silently convert rint() calls to rint_bypass() when possible. */
2038 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2039 th->th_rint = ttyhook_defrint;
2041 done3: tty_unlock(tp);
2042 done2: dev_relthread(dev, ref);
2043 done1: fdrop(fp, curthread);
2048 ttyhook_unregister(struct tty *tp)
2051 tty_lock_assert(tp, MA_OWNED);
2052 MPASS(tp->t_flags & TF_HOOK);
2054 /* Disconnect the hook. */
2055 tp->t_flags &= ~TF_HOOK;
2058 /* Maybe we need to leave bypass mode. */
2059 ttydisc_optimize(tp);
2061 /* Maybe deallocate the TTY as well. */
2066 * /dev/console handling.
2070 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2074 /* System has no console device. */
2075 if (dev_console_filename == NULL)
2078 /* Look up corresponding TTY by device name. */
2079 sx_slock(&tty_list_sx);
2080 TAILQ_FOREACH(tp, &tty_list, t_list) {
2081 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2082 dev_console->si_drv1 = tp;
2086 sx_sunlock(&tty_list_sx);
2088 /* System console has no TTY associated. */
2089 if (dev_console->si_drv1 == NULL)
2092 return (ttydev_open(dev, oflags, devtype, td));
2096 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2101 return (ttydev_write(dev, uio, ioflag));
2105 * /dev/console is a little different than normal TTY's. When opened,
2106 * it determines which TTY to use. When data gets written to it, it
2107 * will be logged in the kernel message buffer.
2109 static struct cdevsw ttyconsdev_cdevsw = {
2110 .d_version = D_VERSION,
2111 .d_open = ttyconsdev_open,
2112 .d_close = ttydev_close,
2113 .d_read = ttydev_read,
2114 .d_write = ttyconsdev_write,
2115 .d_ioctl = ttydev_ioctl,
2116 .d_kqfilter = ttydev_kqfilter,
2117 .d_poll = ttydev_poll,
2118 .d_mmap = ttydev_mmap,
2119 .d_name = "ttyconsdev",
2124 ttyconsdev_init(void *unused __unused)
2127 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2128 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2131 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2134 ttyconsdev_select(const char *name)
2137 dev_console_filename = name;
2141 * Debugging routines.
2144 #include "opt_ddb.h"
2146 #include <ddb/ddb.h>
2147 #include <ddb/db_sym.h>
2149 static const struct {
2154 { TF_NOPREFIX, 'N' },
2156 { TF_INITLOCK, 'I' },
2157 { TF_CALLOUT, 'C' },
2159 /* Keep these together -> 'Oi' and 'Oo'. */
2161 { TF_OPENED_IN, 'i' },
2162 { TF_OPENED_OUT, 'o' },
2163 { TF_OPENED_CONS, 'c' },
2166 { TF_OPENCLOSE, 'B' },
2168 { TF_LITERAL, 'L' },
2170 /* Keep these together -> 'Hi' and 'Ho'. */
2172 { TF_HIWAT_IN, 'i' },
2173 { TF_HIWAT_OUT, 'o' },
2175 { TF_STOPPED, 'S' },
2176 { TF_EXCLUDE, 'X' },
2181 /* Keep these together -> 'bi' and 'bo'. */
2183 { TF_BUSY_IN, 'i' },
2184 { TF_BUSY_OUT, 'o' },
2189 #define TTY_FLAG_BITS \
2190 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2191 "\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2192 "\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2193 "\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2194 "\21HOOK\22BUSY_IN\23BUSY_OUT"
2196 #define DB_PRINTSYM(name, addr) \
2197 db_printf("%s " #name ": ", sep); \
2198 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2202 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2205 db_printf("%sdevsw: ", sep);
2206 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2207 db_printf(" (%p)\n", tsw);
2208 DB_PRINTSYM(open, tsw->tsw_open);
2209 DB_PRINTSYM(close, tsw->tsw_close);
2210 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2211 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2212 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2213 DB_PRINTSYM(param, tsw->tsw_param);
2214 DB_PRINTSYM(modem, tsw->tsw_modem);
2215 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2216 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2217 DB_PRINTSYM(free, tsw->tsw_free);
2221 _db_show_hooks(const char *sep, const struct ttyhook *th)
2224 db_printf("%shook: ", sep);
2225 db_printsym((db_addr_t)th, DB_STGY_ANY);
2226 db_printf(" (%p)\n", th);
2229 DB_PRINTSYM(rint, th->th_rint);
2230 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2231 DB_PRINTSYM(rint_done, th->th_rint_done);
2232 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2233 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2234 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2235 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2236 DB_PRINTSYM(close, th->th_close);
2240 _db_show_termios(const char *name, const struct termios *t)
2243 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2244 "lflag 0x%x ispeed %u ospeed %u\n", name,
2245 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2246 t->c_ispeed, t->c_ospeed);
2249 /* DDB command to show TTY statistics. */
2250 DB_SHOW_COMMAND(tty, db_show_tty)
2255 db_printf("usage: show tty <addr>\n");
2258 tp = (struct tty *)addr;
2260 db_printf("%p: %s\n", tp, tty_devname(tp));
2261 db_printf("\tmtx: %p\n", tp->t_mtx);
2262 db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2263 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2265 /* Buffering mechanisms. */
2266 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2267 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2268 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2269 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2270 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2271 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2272 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2273 db_printf("\tinlow: %zu\n", tp->t_inlow);
2274 db_printf("\toutlow: %zu\n", tp->t_outlow);
2275 _db_show_termios("\ttermios", &tp->t_termios);
2276 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2277 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2278 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2279 db_printf("\tcolumn: %u\n", tp->t_column);
2280 db_printf("\twritepos: %u\n", tp->t_writepos);
2281 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2283 /* Init/lock-state devices. */
2284 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2285 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2286 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2287 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2290 _db_show_devsw("\t", tp->t_devsw);
2291 _db_show_hooks("\t", tp->t_hook);
2294 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2295 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2296 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2297 db_printf("\tsession: %p", tp->t_session);
2298 if (tp->t_session != NULL)
2299 db_printf(" count %u leader %p tty %p sid %d login %s",
2300 tp->t_session->s_count, tp->t_session->s_leader,
2301 tp->t_session->s_ttyp, tp->t_session->s_sid,
2302 tp->t_session->s_login);
2304 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2305 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2306 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2307 db_printf("\tdev: %p\n", tp->t_dev);
2310 /* DDB command to list TTYs. */
2311 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2317 /* Make the output look like `pstat -t'. */
2319 #if defined(__LP64__)
2322 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2323 "COL SESS PGID STATE\n");
2325 TAILQ_FOREACH(tp, &tty_list, t_list) {
2326 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2327 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2329 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2330 "%5d ", tp, tty_devname(tp), isiz,
2331 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2332 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2333 isiz - tp->t_inlow, osiz,
2334 tp->t_outq.to_end - tp->t_outq.to_begin,
2335 osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2336 tp->t_session ? tp->t_session->s_sid : 0,
2337 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2340 for (i = j = 0; ttystates[i].flag; i++)
2341 if (tp->t_flags & ttystates[i].flag) {
2342 db_printf("%c", ttystates[i].val);