2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
7 * Portions of this software were developed under sponsorship from Snow
8 * B.V., the Netherlands.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_capsicum.h"
36 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
42 #include <sys/fcntl.h>
44 #include <sys/filedesc.h>
45 #include <sys/filio.h>
47 #include <sys/ioctl_compat.h>
48 #endif /* COMPAT_43TTY */
49 #include <sys/kernel.h>
50 #include <sys/limits.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
56 #include <sys/serial.h>
57 #include <sys/signal.h>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
63 #include <sys/ttycom.h>
65 #include <sys/ttydefaults.h>
67 #include <sys/ucred.h>
68 #include <sys/vnode.h>
70 #include <machine/stdarg.h>
72 static MALLOC_DEFINE(M_TTY, "tty", "tty device");
74 static void tty_rel_free(struct tty *tp);
76 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
77 static struct sx tty_list_sx;
78 SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
79 static unsigned int tty_list_count = 0;
81 /* Character device of /dev/console. */
82 static struct cdev *dev_console;
83 static const char *dev_console_filename;
86 * Flags that are supported and stored by this implementation.
88 #define TTYSUP_IFLAG (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\
89 INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL)
90 #define TTYSUP_OFLAG (OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET)
91 #define TTYSUP_LFLAG (ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\
92 ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\
93 FLUSHO|NOKERNINFO|NOFLSH)
94 #define TTYSUP_CFLAG (CIGNORE|CSIZE|CSTOPB|CREAD|PARENB|PARODD|\
95 HUPCL|CLOCAL|CCTS_OFLOW|CRTS_IFLOW|CDTR_IFLOW|\
96 CDSR_OFLOW|CCAR_OFLOW)
98 #define TTY_CALLOUT(tp,d) (dev2unit(d) & TTYUNIT_CALLOUT)
100 static int tty_drainwait = 5 * 60;
101 SYSCTL_INT(_kern, OID_AUTO, tty_drainwait, CTLFLAG_RWTUN,
102 &tty_drainwait, 0, "Default output drain timeout in seconds");
105 * Set TTY buffer sizes.
108 #define TTYBUF_MAX 65536
111 * Allocate buffer space if necessary, and set low watermarks, based on speed.
112 * Note that the ttyxxxq_setsize() functions may drop and then reacquire the tty
113 * lock during memory allocation. They will return ENXIO if the tty disappears
117 tty_watermarks(struct tty *tp)
122 /* Provide an input buffer for 2 seconds of data. */
123 if (tp->t_termios.c_cflag & CREAD)
124 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX);
125 error = ttyinq_setsize(&tp->t_inq, tp, bs);
129 /* Set low watermark at 10% (when 90% is available). */
130 tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
132 /* Provide an output buffer for 2 seconds of data. */
133 bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX);
134 error = ttyoutq_setsize(&tp->t_outq, tp, bs);
138 /* Set low watermark at 10% (when 90% is available). */
139 tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
145 tty_drain(struct tty *tp, int leaving)
147 sbintime_t timeout_at;
151 if (ttyhook_hashook(tp, getc_inject))
152 /* buffer is inaccessible */
156 * For close(), use the recent historic timeout of "1 second without
157 * making progress". For tcdrain(), use t_drainwait as the timeout,
158 * with zero meaning "no timeout" which gives POSIX behavior.
161 timeout_at = getsbinuptime() + SBT_1S;
162 else if (tp->t_drainwait != 0)
163 timeout_at = getsbinuptime() + SBT_1S * tp->t_drainwait;
168 * Poll the output buffer and the hardware for completion, at 10 Hz.
169 * Polling is required for devices which are not able to signal an
170 * interrupt when the transmitter becomes idle (most USB serial devs).
171 * The unusual structure of this loop ensures we check for busy one more
172 * time after tty_timedwait() returns EWOULDBLOCK, so that success has
173 * higher priority than timeout if the IO completed in the last 100mS.
176 bytes = ttyoutq_bytesused(&tp->t_outq);
178 if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp))
182 ttydevsw_outwakeup(tp);
183 error = tty_timedwait(tp, &tp->t_outwait, hz / 10);
184 if (error != 0 && error != EWOULDBLOCK)
186 else if (timeout_at == 0 || getsbinuptime() < timeout_at)
188 else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes) {
189 /* In close, making progress, grant an extra second. */
191 timeout_at += SBT_1S;
192 bytes = ttyoutq_bytesused(&tp->t_outq);
198 * Though ttydev_enter() and ttydev_leave() seem to be related, they
199 * don't have to be used together. ttydev_enter() is used by the cdev
200 * operations to prevent an actual operation from being processed when
201 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
202 * and ttydev_close() to determine whether per-TTY data should be
207 ttydev_enter(struct tty *tp)
212 if (tty_gone(tp) || !tty_opened(tp)) {
213 /* Device is already gone. */
222 ttydev_leave(struct tty *tp)
225 tty_lock_assert(tp, MA_OWNED);
227 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
228 /* Device is still opened somewhere. */
233 tp->t_flags |= TF_OPENCLOSE;
235 /* Stop asynchronous I/O. */
236 funsetown(&tp->t_sigio);
238 /* Remove console TTY. */
242 /* Drain any output. */
248 /* Free i/o queues now since they might be large. */
249 ttyinq_free(&tp->t_inq);
251 ttyoutq_free(&tp->t_outq);
254 knlist_clear(&tp->t_inpoll.si_note, 1);
255 knlist_clear(&tp->t_outpoll.si_note, 1);
260 tp->t_flags &= ~TF_OPENCLOSE;
261 cv_broadcast(&tp->t_dcdwait);
266 * Operations that are exposed through the character device in /dev.
269 ttydev_open(struct cdev *dev, int oflags, int devtype __unused,
279 /* Device is already gone. */
285 * Block when other processes are currently opening or closing
288 while (tp->t_flags & TF_OPENCLOSE) {
289 error = tty_wait(tp, &tp->t_dcdwait);
295 tp->t_flags |= TF_OPENCLOSE;
298 * Make sure the "tty" and "cua" device cannot be opened at the
299 * same time. The console is a "tty" device.
301 if (TTY_CALLOUT(tp, dev)) {
302 if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) {
307 if (tp->t_flags & TF_OPENED_OUT) {
313 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
318 if (!tty_opened(tp)) {
319 /* Set proper termios flags. */
320 if (TTY_CALLOUT(tp, dev))
321 tp->t_termios = tp->t_termios_init_out;
323 tp->t_termios = tp->t_termios_init_in;
324 ttydevsw_param(tp, &tp->t_termios);
325 /* Prevent modem control on callout devices and /dev/console. */
326 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
327 tp->t_termios.c_cflag |= CLOCAL;
329 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
331 error = ttydevsw_open(tp);
336 error = tty_watermarks(tp);
341 /* Wait for Carrier Detect. */
342 if ((oflags & O_NONBLOCK) == 0 &&
343 (tp->t_termios.c_cflag & CLOCAL) == 0) {
344 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
345 error = tty_wait(tp, &tp->t_dcdwait);
351 if (dev == dev_console)
352 tp->t_flags |= TF_OPENED_CONS;
353 else if (TTY_CALLOUT(tp, dev))
354 tp->t_flags |= TF_OPENED_OUT;
356 tp->t_flags |= TF_OPENED_IN;
357 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
358 (tp->t_flags & TF_OPENED_OUT) == 0);
360 done: tp->t_flags &= ~TF_OPENCLOSE;
361 cv_broadcast(&tp->t_dcdwait);
368 ttydev_close(struct cdev *dev, int fflag, int devtype __unused,
369 struct thread *td __unused)
371 struct tty *tp = dev->si_drv1;
376 * Don't actually close the device if it is being used as the
379 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
380 (tp->t_flags & TF_OPENED_OUT) == 0);
381 if (dev == dev_console)
382 tp->t_flags &= ~TF_OPENED_CONS;
384 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
386 if (tp->t_flags & TF_OPENED) {
391 /* If revoking, flush output now to avoid draining it later. */
393 tty_flush(tp, FWRITE);
395 tp->t_flags &= ~TF_EXCLUDE;
397 /* Properly wake up threads that are stuck - revoke(). */
399 tty_wakeup(tp, FREAD|FWRITE);
400 cv_broadcast(&tp->t_bgwait);
401 cv_broadcast(&tp->t_dcdwait);
409 tty_is_ctty(struct tty *tp, struct proc *p)
412 tty_lock_assert(tp, MA_OWNED);
414 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
418 tty_wait_background(struct tty *tp, struct thread *td, int sig)
420 struct proc *p = td->td_proc;
425 MPASS(sig == SIGTTIN || sig == SIGTTOU);
426 tty_lock_assert(tp, MA_OWNED);
431 * The process should only sleep, when:
432 * - This terminal is the controlling terminal
433 * - Its process group is not the foreground process
435 * - The parent process isn't waiting for the child to
437 * - the signal to send to the process isn't masked
439 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
440 /* Allow the action to happen. */
445 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
446 SIGISMEMBER(td->td_sigmask, sig)) {
447 /* Only allow them in write()/ioctl(). */
449 return (sig == SIGTTOU ? 0 : EIO);
453 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
454 /* Don't allow the action to happen. */
461 * Send the signal and sleep until we're the new
462 * foreground process group.
466 ksi.ksi_code = SI_KERNEL;
471 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
474 error = tty_wait(tp, &tp->t_bgwait);
481 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
483 struct tty *tp = dev->si_drv1;
486 error = ttydev_enter(tp);
489 error = ttydisc_read(tp, uio, ioflag);
493 * The read() call should not throw an error when the device is
494 * being destroyed. Silently convert it to an EOF.
496 done: if (error == ENXIO)
502 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
504 struct tty *tp = dev->si_drv1;
507 error = ttydev_enter(tp);
511 if (tp->t_termios.c_lflag & TOSTOP) {
512 error = tty_wait_background(tp, curthread, SIGTTOU);
517 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
518 /* Allow non-blocking writes to bypass serialization. */
519 error = ttydisc_write(tp, uio, ioflag);
521 /* Serialize write() calls. */
522 while (tp->t_flags & TF_BUSY_OUT) {
523 error = tty_wait(tp, &tp->t_outserwait);
528 tp->t_flags |= TF_BUSY_OUT;
529 error = ttydisc_write(tp, uio, ioflag);
530 tp->t_flags &= ~TF_BUSY_OUT;
531 cv_signal(&tp->t_outserwait);
534 done: tty_unlock(tp);
539 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
542 struct tty *tp = dev->si_drv1;
545 error = ttydev_enter(tp);
580 #endif /* COMPAT_43TTY */
582 * If the ioctl() causes the TTY to be modified, let it
583 * wait in the background.
585 error = tty_wait_background(tp, curthread, SIGTTOU);
590 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
591 struct termios *old = &tp->t_termios;
592 struct termios *new = (struct termios *)data;
593 struct termios *lock = TTY_CALLOUT(tp, dev) ?
594 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
598 * Lock state devices. Just overwrite the values of the
599 * commands that are currently in use.
601 new->c_iflag = (old->c_iflag & lock->c_iflag) |
602 (new->c_iflag & ~lock->c_iflag);
603 new->c_oflag = (old->c_oflag & lock->c_oflag) |
604 (new->c_oflag & ~lock->c_oflag);
605 new->c_cflag = (old->c_cflag & lock->c_cflag) |
606 (new->c_cflag & ~lock->c_cflag);
607 new->c_lflag = (old->c_lflag & lock->c_lflag) |
608 (new->c_lflag & ~lock->c_lflag);
609 for (cc = 0; cc < NCCS; ++cc)
611 new->c_cc[cc] = old->c_cc[cc];
613 new->c_ispeed = old->c_ispeed;
615 new->c_ospeed = old->c_ospeed;
618 error = tty_ioctl(tp, cmd, data, fflag, td);
619 done: tty_unlock(tp);
625 ttydev_poll(struct cdev *dev, int events, struct thread *td)
627 struct tty *tp = dev->si_drv1;
628 int error, revents = 0;
630 error = ttydev_enter(tp);
632 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
634 if (events & (POLLIN|POLLRDNORM)) {
635 /* See if we can read something. */
636 if (ttydisc_read_poll(tp) > 0)
637 revents |= events & (POLLIN|POLLRDNORM);
640 if (tp->t_flags & TF_ZOMBIE) {
641 /* Hangup flag on zombie state. */
643 } else if (events & (POLLOUT|POLLWRNORM)) {
644 /* See if we can write something. */
645 if (ttydisc_write_poll(tp) > 0)
646 revents |= events & (POLLOUT|POLLWRNORM);
650 if (events & (POLLIN|POLLRDNORM))
651 selrecord(td, &tp->t_inpoll);
652 if (events & (POLLOUT|POLLWRNORM))
653 selrecord(td, &tp->t_outpoll);
662 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
663 int nprot, vm_memattr_t *memattr)
665 struct tty *tp = dev->si_drv1;
668 /* Handle mmap() through the driver. */
670 error = ttydev_enter(tp);
673 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
684 tty_kqops_read_detach(struct knote *kn)
686 struct tty *tp = kn->kn_hook;
688 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
692 tty_kqops_read_event(struct knote *kn, long hint __unused)
694 struct tty *tp = kn->kn_hook;
696 tty_lock_assert(tp, MA_OWNED);
698 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
699 kn->kn_flags |= EV_EOF;
702 kn->kn_data = ttydisc_read_poll(tp);
703 return (kn->kn_data > 0);
708 tty_kqops_write_detach(struct knote *kn)
710 struct tty *tp = kn->kn_hook;
712 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
716 tty_kqops_write_event(struct knote *kn, long hint __unused)
718 struct tty *tp = kn->kn_hook;
720 tty_lock_assert(tp, MA_OWNED);
723 kn->kn_flags |= EV_EOF;
726 kn->kn_data = ttydisc_write_poll(tp);
727 return (kn->kn_data > 0);
731 static struct filterops tty_kqops_read = {
733 .f_detach = tty_kqops_read_detach,
734 .f_event = tty_kqops_read_event,
737 static struct filterops tty_kqops_write = {
739 .f_detach = tty_kqops_write_detach,
740 .f_event = tty_kqops_write_event,
744 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
746 struct tty *tp = dev->si_drv1;
749 error = ttydev_enter(tp);
753 switch (kn->kn_filter) {
756 kn->kn_fop = &tty_kqops_read;
757 knlist_add(&tp->t_inpoll.si_note, kn, 1);
761 kn->kn_fop = &tty_kqops_write;
762 knlist_add(&tp->t_outpoll.si_note, kn, 1);
773 static struct cdevsw ttydev_cdevsw = {
774 .d_version = D_VERSION,
775 .d_open = ttydev_open,
776 .d_close = ttydev_close,
777 .d_read = ttydev_read,
778 .d_write = ttydev_write,
779 .d_ioctl = ttydev_ioctl,
780 .d_kqfilter = ttydev_kqfilter,
781 .d_poll = ttydev_poll,
782 .d_mmap = ttydev_mmap,
788 * Init/lock-state devices
792 ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
809 ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
810 struct thread *td __unused)
817 ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
825 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
828 struct tty *tp = dev->si_drv1;
837 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
838 if (error != ENOIOCTL)
844 /* Obtain terminal flags through tcgetattr(). */
845 *(struct termios*)data = *(struct termios*)dev->si_drv2;
848 /* Set terminal flags through tcsetattr(). */
849 error = priv_check(td, PRIV_TTY_SETA);
852 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
855 *(int *)data = TTYDISC;
858 bzero(data, sizeof(struct winsize));
864 done: tty_unlock(tp);
868 static struct cdevsw ttyil_cdevsw = {
869 .d_version = D_VERSION,
870 .d_open = ttyil_open,
871 .d_close = ttyil_close,
872 .d_read = ttyil_rdwr,
873 .d_write = ttyil_rdwr,
874 .d_ioctl = ttyil_ioctl,
880 tty_init_termios(struct tty *tp)
882 struct termios *t = &tp->t_termios_init_in;
884 t->c_cflag = TTYDEF_CFLAG;
885 t->c_iflag = TTYDEF_IFLAG;
886 t->c_lflag = TTYDEF_LFLAG;
887 t->c_oflag = TTYDEF_OFLAG;
888 t->c_ispeed = TTYDEF_SPEED;
889 t->c_ospeed = TTYDEF_SPEED;
890 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
892 tp->t_termios_init_out = *t;
896 tty_init_console(struct tty *tp, speed_t s)
898 struct termios *ti = &tp->t_termios_init_in;
899 struct termios *to = &tp->t_termios_init_out;
902 ti->c_ispeed = ti->c_ospeed = s;
903 to->c_ispeed = to->c_ospeed = s;
906 ti->c_cflag |= CLOCAL;
907 to->c_cflag |= CLOCAL;
911 * Standard device routine implementations, mostly meant for
912 * pseudo-terminal device drivers. When a driver creates a new terminal
913 * device class, missing routines are patched.
917 ttydevsw_defopen(struct tty *tp __unused)
924 ttydevsw_defclose(struct tty *tp __unused)
930 ttydevsw_defoutwakeup(struct tty *tp __unused)
933 panic("Terminal device has output, while not implemented");
937 ttydevsw_definwakeup(struct tty *tp __unused)
943 ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
944 caddr_t data __unused, struct thread *td __unused)
951 ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
952 u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
959 ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
963 * Allow the baud rate to be adjusted for pseudo-devices, but at
964 * least restrict it to 115200 to prevent excessive buffer
965 * usage. Also disallow 0, to prevent foot shooting.
967 if (t->c_ispeed < B50)
969 else if (t->c_ispeed > B115200)
970 t->c_ispeed = B115200;
971 if (t->c_ospeed < B50)
973 else if (t->c_ospeed > B115200)
974 t->c_ospeed = B115200;
981 ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
985 /* Simulate a carrier to make the TTY layer happy. */
990 ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
991 vm_paddr_t *paddr __unused, int nprot __unused,
992 vm_memattr_t *memattr __unused)
999 ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
1005 ttydevsw_deffree(void *softc __unused)
1008 panic("Terminal device freed without a free-handler");
1012 ttydevsw_defbusy(struct tty *tp __unused)
1019 * TTY allocation and deallocation. TTY devices can be deallocated when
1020 * the driver doesn't use it anymore, when the TTY isn't a session's
1021 * controlling TTY and when the device node isn't opened through devfs.
1025 tty_alloc(struct ttydevsw *tsw, void *sc)
1028 return (tty_alloc_mutex(tsw, sc, NULL));
1032 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1036 /* Make sure the driver defines all routines. */
1037 #define PATCH_FUNC(x) do { \
1038 if (tsw->tsw_ ## x == NULL) \
1039 tsw->tsw_ ## x = ttydevsw_def ## x; \
1043 PATCH_FUNC(outwakeup);
1044 PATCH_FUNC(inwakeup);
1050 PATCH_FUNC(pktnotify);
1055 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
1057 tp->t_devswsoftc = sc;
1058 tp->t_flags = tsw->tsw_flags;
1059 tp->t_drainwait = tty_drainwait;
1061 tty_init_termios(tp);
1063 cv_init(&tp->t_inwait, "ttyin");
1064 cv_init(&tp->t_outwait, "ttyout");
1065 cv_init(&tp->t_outserwait, "ttyosr");
1066 cv_init(&tp->t_bgwait, "ttybg");
1067 cv_init(&tp->t_dcdwait, "ttydcd");
1069 /* Allow drivers to use a custom mutex to lock the TTY. */
1070 if (mutex != NULL) {
1073 tp->t_mtx = &tp->t_mtxobj;
1074 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1077 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1078 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1084 tty_dealloc(void *arg)
1086 struct tty *tp = arg;
1089 * ttyydev_leave() usually frees the i/o queues earlier, but it is
1090 * not always called between queue allocation and here. The queues
1091 * may be allocated by ioctls on a pty control device without the
1092 * corresponding pty slave device ever being open, or after it is
1095 ttyinq_free(&tp->t_inq);
1096 ttyoutq_free(&tp->t_outq);
1097 seldrain(&tp->t_inpoll);
1098 seldrain(&tp->t_outpoll);
1099 knlist_destroy(&tp->t_inpoll.si_note);
1100 knlist_destroy(&tp->t_outpoll.si_note);
1102 cv_destroy(&tp->t_inwait);
1103 cv_destroy(&tp->t_outwait);
1104 cv_destroy(&tp->t_bgwait);
1105 cv_destroy(&tp->t_dcdwait);
1106 cv_destroy(&tp->t_outserwait);
1108 if (tp->t_mtx == &tp->t_mtxobj)
1109 mtx_destroy(&tp->t_mtxobj);
1115 tty_rel_free(struct tty *tp)
1119 tty_lock_assert(tp, MA_OWNED);
1121 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1122 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1123 /* TTY is still in use. */
1128 /* TTY can be deallocated. */
1134 sx_xlock(&tty_list_sx);
1135 TAILQ_REMOVE(&tty_list, tp, t_list);
1137 sx_xunlock(&tty_list_sx);
1138 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1143 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1146 MPASS(tp->t_sessioncnt > 0);
1147 tty_lock_assert(tp, MA_OWNED);
1149 if (tp->t_pgrp == pg)
1156 tty_rel_sess(struct tty *tp, struct session *sess)
1159 MPASS(tp->t_sessioncnt > 0);
1161 /* Current session has left. */
1162 if (tp->t_session == sess) {
1163 tp->t_session = NULL;
1164 MPASS(tp->t_pgrp == NULL);
1171 tty_rel_gone(struct tty *tp)
1174 MPASS(!tty_gone(tp));
1176 /* Simulate carrier removal. */
1177 ttydisc_modem(tp, 0);
1179 /* Wake up all blocked threads. */
1180 tty_wakeup(tp, FREAD|FWRITE);
1181 cv_broadcast(&tp->t_bgwait);
1182 cv_broadcast(&tp->t_dcdwait);
1184 tp->t_flags |= TF_GONE;
1189 * Exposing information about current TTY's through sysctl
1193 tty_to_xtty(struct tty *tp, struct xtty *xt)
1196 tty_lock_assert(tp, MA_OWNED);
1198 xt->xt_size = sizeof(struct xtty);
1199 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1200 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1201 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1202 xt->xt_inlow = tp->t_inlow;
1203 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1204 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1205 xt->xt_outlow = tp->t_outlow;
1206 xt->xt_column = tp->t_column;
1207 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1208 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1209 xt->xt_flags = tp->t_flags;
1210 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : (uint32_t)NODEV;
1214 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1216 unsigned long lsize;
1217 struct xtty *xtlist, *xt;
1221 sx_slock(&tty_list_sx);
1222 lsize = tty_list_count * sizeof(struct xtty);
1224 sx_sunlock(&tty_list_sx);
1228 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1230 TAILQ_FOREACH(tp, &tty_list, t_list) {
1232 tty_to_xtty(tp, xt);
1236 sx_sunlock(&tty_list_sx);
1238 error = SYSCTL_OUT(req, xtlist, lsize);
1239 free(xtlist, M_TTY);
1243 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1244 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1247 * Device node creation. Device has been set up, now we can expose it to
1252 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1253 const char *fmt, ...)
1256 struct make_dev_args args;
1257 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1258 const char *prefix = "tty";
1259 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1265 /* Remove "tty" prefix from devices like PTY's. */
1266 if (tp->t_flags & TF_NOPREFIX)
1270 vsnrprintf(name, sizeof name, 32, fmt, ap);
1274 /* System device. */
1277 mode = S_IRUSR|S_IWUSR;
1280 uid = cred->cr_ruid;
1282 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1285 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1286 flags |= MAKEDEV_CHECKNAME;
1288 /* Master call-in device. */
1289 make_dev_args_init(&args);
1290 args.mda_flags = flags;
1291 args.mda_devsw = &ttydev_cdevsw;
1295 args.mda_mode = mode;
1296 args.mda_si_drv1 = tp;
1297 error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1302 init = lock = cua = cinit = clock = NULL;
1304 /* Slave call-in devices. */
1305 if (tp->t_flags & TF_INITLOCK) {
1306 args.mda_devsw = &ttyil_cdevsw;
1307 args.mda_unit = TTYUNIT_INIT;
1308 args.mda_si_drv1 = tp;
1309 args.mda_si_drv2 = &tp->t_termios_init_in;
1310 error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1313 dev_depends(dev, init);
1315 args.mda_unit = TTYUNIT_LOCK;
1316 args.mda_si_drv2 = &tp->t_termios_lock_in;
1317 error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1320 dev_depends(dev, lock);
1323 /* Call-out devices. */
1324 if (tp->t_flags & TF_CALLOUT) {
1325 make_dev_args_init(&args);
1326 args.mda_flags = flags;
1327 args.mda_devsw = &ttydev_cdevsw;
1329 args.mda_uid = UID_UUCP;
1330 args.mda_gid = GID_DIALER;
1331 args.mda_mode = 0660;
1332 args.mda_unit = TTYUNIT_CALLOUT;
1333 args.mda_si_drv1 = tp;
1334 error = make_dev_s(&args, &cua, "cua%s", name);
1337 dev_depends(dev, cua);
1339 /* Slave call-out devices. */
1340 if (tp->t_flags & TF_INITLOCK) {
1341 args.mda_devsw = &ttyil_cdevsw;
1342 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1343 args.mda_si_drv2 = &tp->t_termios_init_out;
1344 error = make_dev_s(&args, &cinit, "cua%s.init", name);
1347 dev_depends(dev, cinit);
1349 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1350 args.mda_si_drv2 = &tp->t_termios_lock_out;
1351 error = make_dev_s(&args, &clock, "cua%s.lock", name);
1354 dev_depends(dev, clock);
1358 sx_xlock(&tty_list_sx);
1359 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1361 sx_xunlock(&tty_list_sx);
1380 * Signalling processes.
1384 tty_signal_sessleader(struct tty *tp, int sig)
1388 tty_lock_assert(tp, MA_OWNED);
1389 MPASS(sig >= 1 && sig < NSIG);
1391 /* Make signals start output again. */
1392 tp->t_flags &= ~TF_STOPPED;
1394 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1395 p = tp->t_session->s_leader;
1397 kern_psignal(p, sig);
1403 tty_signal_pgrp(struct tty *tp, int sig)
1407 tty_lock_assert(tp, MA_OWNED);
1408 MPASS(sig >= 1 && sig < NSIG);
1410 /* Make signals start output again. */
1411 tp->t_flags &= ~TF_STOPPED;
1413 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1415 if (tp->t_pgrp != NULL) {
1416 ksiginfo_init(&ksi);
1417 ksi.ksi_signo = sig;
1418 ksi.ksi_code = SI_KERNEL;
1419 PGRP_LOCK(tp->t_pgrp);
1420 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1421 PGRP_UNLOCK(tp->t_pgrp);
1426 tty_wakeup(struct tty *tp, int flags)
1429 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1430 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1432 if (flags & FWRITE) {
1433 cv_broadcast(&tp->t_outwait);
1434 selwakeup(&tp->t_outpoll);
1435 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1437 if (flags & FREAD) {
1438 cv_broadcast(&tp->t_inwait);
1439 selwakeup(&tp->t_inpoll);
1440 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1445 tty_wait(struct tty *tp, struct cv *cv)
1448 int revokecnt = tp->t_revokecnt;
1450 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1451 MPASS(!tty_gone(tp));
1453 error = cv_wait_sig(cv, tp->t_mtx);
1455 /* Bail out when the device slipped away. */
1459 /* Restart the system call when we may have been revoked. */
1460 if (tp->t_revokecnt != revokecnt)
1467 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1470 int revokecnt = tp->t_revokecnt;
1472 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1473 MPASS(!tty_gone(tp));
1475 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1477 /* Bail out when the device slipped away. */
1481 /* Restart the system call when we may have been revoked. */
1482 if (tp->t_revokecnt != revokecnt)
1489 tty_flush(struct tty *tp, int flags)
1492 if (flags & FWRITE) {
1493 tp->t_flags &= ~TF_HIWAT_OUT;
1494 ttyoutq_flush(&tp->t_outq);
1495 tty_wakeup(tp, FWRITE);
1496 if (!tty_gone(tp)) {
1497 ttydevsw_outwakeup(tp);
1498 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1501 if (flags & FREAD) {
1502 tty_hiwat_in_unblock(tp);
1503 ttyinq_flush(&tp->t_inq);
1504 tty_wakeup(tp, FREAD);
1505 if (!tty_gone(tp)) {
1506 ttydevsw_inwakeup(tp);
1507 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1513 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1516 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1518 tp->t_winsize = *wsz;
1519 tty_signal_pgrp(tp, SIGWINCH);
1523 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1531 * The SER_* and TIOCM_* flags are the same, but one bit
1532 * shifted. I don't know why.
1535 ttydevsw_modem(tp, SER_DTR, 0);
1538 ttydevsw_modem(tp, 0, SER_DTR);
1541 int bits = *(int *)data;
1543 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1544 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1548 int bits = *(int *)data;
1549 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1553 int bits = *(int *)data;
1554 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1558 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1563 tp->t_flags |= TF_ASYNC;
1565 tp->t_flags &= ~TF_ASYNC;
1568 /* This device supports non-blocking operation. */
1571 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1575 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1578 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1579 /* Not allowed to set ownership. */
1582 /* Temporarily unlock the TTY to set ownership. */
1584 error = fsetown(*(int *)data, &tp->t_sigio);
1588 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1589 /* Not allowed to set ownership. */
1592 /* Get ownership. */
1593 *(int *)data = fgetown(&tp->t_sigio);
1596 /* Obtain terminal flags through tcgetattr(). */
1597 *(struct termios*)data = tp->t_termios;
1602 struct termios *t = data;
1605 * Who makes up these funny rules? According to POSIX,
1606 * input baud rate is set equal to the output baud rate
1609 if (t->c_ispeed == 0)
1610 t->c_ispeed = t->c_ospeed;
1612 /* Discard any unsupported bits. */
1613 t->c_iflag &= TTYSUP_IFLAG;
1614 t->c_oflag &= TTYSUP_OFLAG;
1615 t->c_lflag &= TTYSUP_LFLAG;
1616 t->c_cflag &= TTYSUP_CFLAG;
1618 /* Set terminal flags through tcsetattr(). */
1619 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1620 error = tty_drain(tp, 0);
1623 if (cmd == TIOCSETAF)
1624 tty_flush(tp, FREAD);
1628 * Only call param() when the flags really change.
1630 if ((t->c_cflag & CIGNORE) == 0 &&
1631 (tp->t_termios.c_cflag != t->c_cflag ||
1632 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1633 (IXON|IXOFF|IXANY)) ||
1634 tp->t_termios.c_ispeed != t->c_ispeed ||
1635 tp->t_termios.c_ospeed != t->c_ospeed)) {
1636 error = ttydevsw_param(tp, t);
1642 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1643 tp->t_termios.c_ispeed = t->c_ispeed;
1644 tp->t_termios.c_ospeed = t->c_ospeed;
1646 /* Baud rate has changed - update watermarks. */
1647 error = tty_watermarks(tp);
1652 /* Copy new non-device driver parameters. */
1653 tp->t_termios.c_iflag = t->c_iflag;
1654 tp->t_termios.c_oflag = t->c_oflag;
1655 tp->t_termios.c_lflag = t->c_lflag;
1656 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1658 ttydisc_optimize(tp);
1660 if ((t->c_lflag & ICANON) == 0) {
1662 * When in non-canonical mode, wake up all
1663 * readers. Canonicalize any partial input. VMIN
1664 * and VTIME could also be adjusted.
1666 ttyinq_canonicalize(&tp->t_inq);
1667 tty_wakeup(tp, FREAD);
1671 * For packet mode: notify the PTY consumer that VSTOP
1672 * and VSTART may have been changed.
1674 if (tp->t_termios.c_iflag & IXON &&
1675 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1676 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1677 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1679 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1683 /* For compatibility - we only support TTYDISC. */
1684 *(int *)data = TTYDISC;
1687 if (!tty_is_ctty(tp, td->td_proc))
1690 if (tp->t_pgrp != NULL)
1691 *(int *)data = tp->t_pgrp->pg_id;
1693 *(int *)data = NO_PID;
1696 if (!tty_is_ctty(tp, td->td_proc))
1699 MPASS(tp->t_session);
1700 *(int *)data = tp->t_session->s_sid;
1703 struct proc *p = td->td_proc;
1705 /* XXX: This looks awful. */
1707 sx_xlock(&proctree_lock);
1710 if (!SESS_LEADER(p)) {
1711 /* Only the session leader may do this. */
1712 sx_xunlock(&proctree_lock);
1716 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1717 /* This is already our controlling TTY. */
1718 sx_xunlock(&proctree_lock);
1722 if (p->p_session->s_ttyp != NULL ||
1723 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1724 tp->t_session->s_ttyvp->v_type != VBAD)) {
1726 * There is already a relation between a TTY and
1727 * a session, or the caller is not the session
1730 * Allow the TTY to be stolen when the vnode is
1731 * invalid, but the reference to the TTY is
1732 * still active. This allows immediate reuse of
1733 * TTYs of which the session leader has been
1734 * killed or the TTY revoked.
1736 sx_xunlock(&proctree_lock);
1740 /* Connect the session to the TTY. */
1741 tp->t_session = p->p_session;
1742 tp->t_session->s_ttyp = tp;
1744 sx_xunlock(&proctree_lock);
1746 /* Assign foreground process group. */
1747 tp->t_pgrp = p->p_pgrp;
1749 p->p_flag |= P_CONTROLT;
1758 * XXX: Temporarily unlock the TTY to locate the process
1759 * group. This code would be lot nicer if we would ever
1760 * decompose proctree_lock.
1763 sx_slock(&proctree_lock);
1764 pg = pgfind(*(int *)data);
1767 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1768 sx_sunlock(&proctree_lock);
1775 * Determine if this TTY is the controlling TTY after
1776 * relocking the TTY.
1778 if (!tty_is_ctty(tp, td->td_proc)) {
1779 sx_sunlock(&proctree_lock);
1783 sx_sunlock(&proctree_lock);
1785 /* Wake up the background process groups. */
1786 cv_broadcast(&tp->t_bgwait);
1790 int flags = *(int *)data;
1793 flags = (FREAD|FWRITE);
1795 flags &= (FREAD|FWRITE);
1796 tty_flush(tp, flags);
1800 /* Drain TTY output. */
1801 return tty_drain(tp, 0);
1802 case TIOCGDRAINWAIT:
1803 *(int *)data = tp->t_drainwait;
1805 case TIOCSDRAINWAIT:
1806 error = priv_check(td, PRIV_TTY_DRAINWAIT);
1808 tp->t_drainwait = *(int *)data;
1811 /* Set terminal as console TTY. */
1813 error = priv_check(td, PRIV_TTY_CONSOLE);
1818 * XXX: constty should really need to be locked!
1819 * XXX: allow disconnected constty's to be stolen!
1824 if (constty != NULL)
1830 } else if (constty == tp) {
1835 /* Obtain window size. */
1836 *(struct winsize*)data = tp->t_winsize;
1839 /* Set window size. */
1840 tty_set_winsize(tp, data);
1843 tp->t_flags |= TF_EXCLUDE;
1846 tp->t_flags &= ~TF_EXCLUDE;
1849 tp->t_flags |= TF_STOPPED;
1850 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1853 tp->t_flags &= ~TF_STOPPED;
1854 ttydevsw_outwakeup(tp);
1855 ttydevsw_pktnotify(tp, TIOCPKT_START);
1861 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1863 if (!tty_is_ctty(tp, td->td_proc) &&
1864 priv_check(td, PRIV_TTY_STI))
1866 ttydisc_rint(tp, *(char *)data, 0);
1867 ttydisc_rint_done(tp);
1872 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1873 #else /* !COMPAT_43TTY */
1875 #endif /* COMPAT_43TTY */
1879 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1883 tty_lock_assert(tp, MA_OWNED);
1888 error = ttydevsw_ioctl(tp, cmd, data, td);
1889 if (error == ENOIOCTL)
1890 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1896 tty_udev(struct tty *tp)
1900 return (dev2udev(tp->t_dev));
1906 tty_checkoutq(struct tty *tp)
1909 /* 256 bytes should be enough to print a log message. */
1910 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1914 tty_hiwat_in_block(struct tty *tp)
1917 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1918 tp->t_termios.c_iflag & IXOFF &&
1919 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1921 * Input flow control. Only enter the high watermark when we
1922 * can successfully store the VSTOP character.
1924 if (ttyoutq_write_nofrag(&tp->t_outq,
1925 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1926 tp->t_flags |= TF_HIWAT_IN;
1928 /* No input flow control. */
1929 tp->t_flags |= TF_HIWAT_IN;
1934 tty_hiwat_in_unblock(struct tty *tp)
1937 if (tp->t_flags & TF_HIWAT_IN &&
1938 tp->t_termios.c_iflag & IXOFF &&
1939 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1941 * Input flow control. Only leave the high watermark when we
1942 * can successfully store the VSTART character.
1944 if (ttyoutq_write_nofrag(&tp->t_outq,
1945 &tp->t_termios.c_cc[VSTART], 1) == 0)
1946 tp->t_flags &= ~TF_HIWAT_IN;
1948 /* No input flow control. */
1949 tp->t_flags &= ~TF_HIWAT_IN;
1953 ttydevsw_inwakeup(tp);
1957 * TTY hooks interface.
1961 ttyhook_defrint(struct tty *tp, char c, int flags)
1964 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1971 ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
1978 struct filedesc *fdp;
1979 cap_rights_t rights;
1982 /* Validate the file descriptor. */
1984 error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1988 if (fp->f_ops == &badfileops) {
1994 * Make sure the vnode is bound to a character device.
1995 * Unlocked check for the vnode type is ok there, because we
1996 * only shall prevent calling devvn_refthread on the file that
1997 * never has been opened over a character device.
1999 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
2004 /* Make sure it is a TTY. */
2005 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
2010 if (dev != fp->f_data) {
2014 if (cdp != &ttydev_cdevsw) {
2020 /* Try to attach the hook to the TTY. */
2023 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
2024 if (tp->t_flags & TF_HOOK)
2027 tp->t_flags |= TF_HOOK;
2029 tp->t_hooksoftc = softc;
2033 /* Maybe we can switch into bypass mode now. */
2034 ttydisc_optimize(tp);
2036 /* Silently convert rint() calls to rint_bypass() when possible. */
2037 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2038 th->th_rint = ttyhook_defrint;
2040 done3: tty_unlock(tp);
2041 done2: dev_relthread(dev, ref);
2042 done1: fdrop(fp, curthread);
2047 ttyhook_unregister(struct tty *tp)
2050 tty_lock_assert(tp, MA_OWNED);
2051 MPASS(tp->t_flags & TF_HOOK);
2053 /* Disconnect the hook. */
2054 tp->t_flags &= ~TF_HOOK;
2057 /* Maybe we need to leave bypass mode. */
2058 ttydisc_optimize(tp);
2060 /* Maybe deallocate the TTY as well. */
2065 * /dev/console handling.
2069 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2073 /* System has no console device. */
2074 if (dev_console_filename == NULL)
2077 /* Look up corresponding TTY by device name. */
2078 sx_slock(&tty_list_sx);
2079 TAILQ_FOREACH(tp, &tty_list, t_list) {
2080 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2081 dev_console->si_drv1 = tp;
2085 sx_sunlock(&tty_list_sx);
2087 /* System console has no TTY associated. */
2088 if (dev_console->si_drv1 == NULL)
2091 return (ttydev_open(dev, oflags, devtype, td));
2095 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2100 return (ttydev_write(dev, uio, ioflag));
2104 * /dev/console is a little different than normal TTY's. When opened,
2105 * it determines which TTY to use. When data gets written to it, it
2106 * will be logged in the kernel message buffer.
2108 static struct cdevsw ttyconsdev_cdevsw = {
2109 .d_version = D_VERSION,
2110 .d_open = ttyconsdev_open,
2111 .d_close = ttydev_close,
2112 .d_read = ttydev_read,
2113 .d_write = ttyconsdev_write,
2114 .d_ioctl = ttydev_ioctl,
2115 .d_kqfilter = ttydev_kqfilter,
2116 .d_poll = ttydev_poll,
2117 .d_mmap = ttydev_mmap,
2118 .d_name = "ttyconsdev",
2123 ttyconsdev_init(void *unused __unused)
2126 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2127 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2130 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2133 ttyconsdev_select(const char *name)
2136 dev_console_filename = name;
2140 * Debugging routines.
2143 #include "opt_ddb.h"
2145 #include <ddb/ddb.h>
2146 #include <ddb/db_sym.h>
2148 static const struct {
2153 { TF_NOPREFIX, 'N' },
2155 { TF_INITLOCK, 'I' },
2156 { TF_CALLOUT, 'C' },
2158 /* Keep these together -> 'Oi' and 'Oo'. */
2160 { TF_OPENED_IN, 'i' },
2161 { TF_OPENED_OUT, 'o' },
2162 { TF_OPENED_CONS, 'c' },
2165 { TF_OPENCLOSE, 'B' },
2167 { TF_LITERAL, 'L' },
2169 /* Keep these together -> 'Hi' and 'Ho'. */
2171 { TF_HIWAT_IN, 'i' },
2172 { TF_HIWAT_OUT, 'o' },
2174 { TF_STOPPED, 'S' },
2175 { TF_EXCLUDE, 'X' },
2180 /* Keep these together -> 'bi' and 'bo'. */
2182 { TF_BUSY_IN, 'i' },
2183 { TF_BUSY_OUT, 'o' },
2188 #define TTY_FLAG_BITS \
2189 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2190 "\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2191 "\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2192 "\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2193 "\21HOOK\22BUSY_IN\23BUSY_OUT"
2195 #define DB_PRINTSYM(name, addr) \
2196 db_printf("%s " #name ": ", sep); \
2197 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2201 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2204 db_printf("%sdevsw: ", sep);
2205 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2206 db_printf(" (%p)\n", tsw);
2207 DB_PRINTSYM(open, tsw->tsw_open);
2208 DB_PRINTSYM(close, tsw->tsw_close);
2209 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2210 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2211 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2212 DB_PRINTSYM(param, tsw->tsw_param);
2213 DB_PRINTSYM(modem, tsw->tsw_modem);
2214 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2215 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2216 DB_PRINTSYM(free, tsw->tsw_free);
2220 _db_show_hooks(const char *sep, const struct ttyhook *th)
2223 db_printf("%shook: ", sep);
2224 db_printsym((db_addr_t)th, DB_STGY_ANY);
2225 db_printf(" (%p)\n", th);
2228 DB_PRINTSYM(rint, th->th_rint);
2229 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2230 DB_PRINTSYM(rint_done, th->th_rint_done);
2231 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2232 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2233 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2234 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2235 DB_PRINTSYM(close, th->th_close);
2239 _db_show_termios(const char *name, const struct termios *t)
2242 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2243 "lflag 0x%x ispeed %u ospeed %u\n", name,
2244 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2245 t->c_ispeed, t->c_ospeed);
2248 /* DDB command to show TTY statistics. */
2249 DB_SHOW_COMMAND(tty, db_show_tty)
2254 db_printf("usage: show tty <addr>\n");
2257 tp = (struct tty *)addr;
2259 db_printf("%p: %s\n", tp, tty_devname(tp));
2260 db_printf("\tmtx: %p\n", tp->t_mtx);
2261 db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2262 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2264 /* Buffering mechanisms. */
2265 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2266 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2267 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2268 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2269 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2270 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2271 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2272 db_printf("\tinlow: %zu\n", tp->t_inlow);
2273 db_printf("\toutlow: %zu\n", tp->t_outlow);
2274 _db_show_termios("\ttermios", &tp->t_termios);
2275 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2276 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2277 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2278 db_printf("\tcolumn: %u\n", tp->t_column);
2279 db_printf("\twritepos: %u\n", tp->t_writepos);
2280 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2282 /* Init/lock-state devices. */
2283 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2284 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2285 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2286 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2289 _db_show_devsw("\t", tp->t_devsw);
2290 _db_show_hooks("\t", tp->t_hook);
2293 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2294 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2295 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2296 db_printf("\tsession: %p", tp->t_session);
2297 if (tp->t_session != NULL)
2298 db_printf(" count %u leader %p tty %p sid %d login %s",
2299 tp->t_session->s_count, tp->t_session->s_leader,
2300 tp->t_session->s_ttyp, tp->t_session->s_sid,
2301 tp->t_session->s_login);
2303 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2304 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2305 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2306 db_printf("\tdev: %p\n", tp->t_dev);
2309 /* DDB command to list TTYs. */
2310 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2316 /* Make the output look like `pstat -t'. */
2318 #if defined(__LP64__)
2321 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2322 "COL SESS PGID STATE\n");
2324 TAILQ_FOREACH(tp, &tty_list, t_list) {
2325 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2326 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2328 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2329 "%5d ", tp, tty_devname(tp), isiz,
2330 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2331 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2332 isiz - tp->t_inlow, osiz,
2333 tp->t_outq.to_end - tp->t_outq.to_begin,
2334 osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2335 tp->t_session ? tp->t_session->s_sid : 0,
2336 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2339 for (i = j = 0; ttystates[i].flag; i++)
2340 if (tp->t_flags & ttystates[i].flag) {
2341 db_printf("%c", ttystates[i].val);