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_printf.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
110 #ifdef PRINTF_BUFR_SIZE
111 #define TTY_PRBUF_SIZE PRINTF_BUFR_SIZE
113 #define TTY_PRBUF_SIZE 256
117 * Allocate buffer space if necessary, and set low watermarks, based on speed.
118 * Note that the ttyxxxq_setsize() functions may drop and then reacquire the tty
119 * lock during memory allocation. They will return ENXIO if the tty disappears
123 tty_watermarks(struct tty *tp)
128 /* Provide an input buffer for 2 seconds of data. */
129 if (tp->t_termios.c_cflag & CREAD)
130 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX);
131 error = ttyinq_setsize(&tp->t_inq, tp, bs);
135 /* Set low watermark at 10% (when 90% is available). */
136 tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
138 /* Provide an output buffer for 2 seconds of data. */
139 bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX);
140 error = ttyoutq_setsize(&tp->t_outq, tp, bs);
144 /* Set low watermark at 10% (when 90% is available). */
145 tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
151 tty_drain(struct tty *tp, int leaving)
153 sbintime_t timeout_at;
157 if (ttyhook_hashook(tp, getc_inject))
158 /* buffer is inaccessible */
162 * For close(), use the recent historic timeout of "1 second without
163 * making progress". For tcdrain(), use t_drainwait as the timeout,
164 * with zero meaning "no timeout" which gives POSIX behavior.
167 timeout_at = getsbinuptime() + SBT_1S;
168 else if (tp->t_drainwait != 0)
169 timeout_at = getsbinuptime() + SBT_1S * tp->t_drainwait;
174 * Poll the output buffer and the hardware for completion, at 10 Hz.
175 * Polling is required for devices which are not able to signal an
176 * interrupt when the transmitter becomes idle (most USB serial devs).
177 * The unusual structure of this loop ensures we check for busy one more
178 * time after tty_timedwait() returns EWOULDBLOCK, so that success has
179 * higher priority than timeout if the IO completed in the last 100mS.
182 bytes = ttyoutq_bytesused(&tp->t_outq);
184 if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp))
188 ttydevsw_outwakeup(tp);
189 error = tty_timedwait(tp, &tp->t_outwait, hz / 10);
190 if (error != 0 && error != EWOULDBLOCK)
192 else if (timeout_at == 0 || getsbinuptime() < timeout_at)
194 else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes) {
195 /* In close, making progress, grant an extra second. */
197 timeout_at += SBT_1S;
198 bytes = ttyoutq_bytesused(&tp->t_outq);
204 * Though ttydev_enter() and ttydev_leave() seem to be related, they
205 * don't have to be used together. ttydev_enter() is used by the cdev
206 * operations to prevent an actual operation from being processed when
207 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
208 * and ttydev_close() to determine whether per-TTY data should be
213 ttydev_enter(struct tty *tp)
218 if (tty_gone(tp) || !tty_opened(tp)) {
219 /* Device is already gone. */
228 ttydev_leave(struct tty *tp)
231 tty_lock_assert(tp, MA_OWNED);
233 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
234 /* Device is still opened somewhere. */
239 tp->t_flags |= TF_OPENCLOSE;
241 /* Stop asynchronous I/O. */
242 funsetown(&tp->t_sigio);
244 /* Remove console TTY. */
248 /* Drain any output. */
254 /* Free i/o queues now since they might be large. */
255 ttyinq_free(&tp->t_inq);
257 ttyoutq_free(&tp->t_outq);
260 knlist_clear(&tp->t_inpoll.si_note, 1);
261 knlist_clear(&tp->t_outpoll.si_note, 1);
266 tp->t_flags &= ~TF_OPENCLOSE;
267 cv_broadcast(&tp->t_dcdwait);
272 * Operations that are exposed through the character device in /dev.
275 ttydev_open(struct cdev *dev, int oflags, int devtype __unused,
285 /* Device is already gone. */
291 * Block when other processes are currently opening or closing
294 while (tp->t_flags & TF_OPENCLOSE) {
295 error = tty_wait(tp, &tp->t_dcdwait);
301 tp->t_flags |= TF_OPENCLOSE;
304 * Make sure the "tty" and "cua" device cannot be opened at the
305 * same time. The console is a "tty" device.
307 if (TTY_CALLOUT(tp, dev)) {
308 if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) {
313 if (tp->t_flags & TF_OPENED_OUT) {
319 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
324 if (!tty_opened(tp)) {
325 /* Set proper termios flags. */
326 if (TTY_CALLOUT(tp, dev))
327 tp->t_termios = tp->t_termios_init_out;
329 tp->t_termios = tp->t_termios_init_in;
330 ttydevsw_param(tp, &tp->t_termios);
331 /* Prevent modem control on callout devices and /dev/console. */
332 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
333 tp->t_termios.c_cflag |= CLOCAL;
335 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
337 error = ttydevsw_open(tp);
342 error = tty_watermarks(tp);
347 /* Wait for Carrier Detect. */
348 if ((oflags & O_NONBLOCK) == 0 &&
349 (tp->t_termios.c_cflag & CLOCAL) == 0) {
350 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
351 error = tty_wait(tp, &tp->t_dcdwait);
357 if (dev == dev_console)
358 tp->t_flags |= TF_OPENED_CONS;
359 else if (TTY_CALLOUT(tp, dev))
360 tp->t_flags |= TF_OPENED_OUT;
362 tp->t_flags |= TF_OPENED_IN;
363 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
364 (tp->t_flags & TF_OPENED_OUT) == 0);
366 done: tp->t_flags &= ~TF_OPENCLOSE;
367 cv_broadcast(&tp->t_dcdwait);
374 ttydev_close(struct cdev *dev, int fflag, int devtype __unused,
375 struct thread *td __unused)
377 struct tty *tp = dev->si_drv1;
382 * Don't actually close the device if it is being used as the
385 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
386 (tp->t_flags & TF_OPENED_OUT) == 0);
387 if (dev == dev_console)
388 tp->t_flags &= ~TF_OPENED_CONS;
390 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
392 if (tp->t_flags & TF_OPENED) {
397 /* If revoking, flush output now to avoid draining it later. */
399 tty_flush(tp, FWRITE);
401 tp->t_flags &= ~TF_EXCLUDE;
403 /* Properly wake up threads that are stuck - revoke(). */
405 tty_wakeup(tp, FREAD|FWRITE);
406 cv_broadcast(&tp->t_bgwait);
407 cv_broadcast(&tp->t_dcdwait);
415 tty_is_ctty(struct tty *tp, struct proc *p)
418 tty_lock_assert(tp, MA_OWNED);
420 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
424 tty_wait_background(struct tty *tp, struct thread *td, int sig)
426 struct proc *p = td->td_proc;
431 MPASS(sig == SIGTTIN || sig == SIGTTOU);
432 tty_lock_assert(tp, MA_OWNED);
437 * The process should only sleep, when:
438 * - This terminal is the controlling terminal
439 * - Its process group is not the foreground process
441 * - The parent process isn't waiting for the child to
443 * - the signal to send to the process isn't masked
445 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
446 /* Allow the action to happen. */
451 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
452 SIGISMEMBER(td->td_sigmask, sig)) {
453 /* Only allow them in write()/ioctl(). */
455 return (sig == SIGTTOU ? 0 : EIO);
459 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
460 /* Don't allow the action to happen. */
467 * Send the signal and sleep until we're the new
468 * foreground process group.
472 ksi.ksi_code = SI_KERNEL;
477 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
480 error = tty_wait(tp, &tp->t_bgwait);
487 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
489 struct tty *tp = dev->si_drv1;
492 error = ttydev_enter(tp);
495 error = ttydisc_read(tp, uio, ioflag);
499 * The read() call should not throw an error when the device is
500 * being destroyed. Silently convert it to an EOF.
502 done: if (error == ENXIO)
508 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
510 struct tty *tp = dev->si_drv1;
513 error = ttydev_enter(tp);
517 if (tp->t_termios.c_lflag & TOSTOP) {
518 error = tty_wait_background(tp, curthread, SIGTTOU);
523 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
524 /* Allow non-blocking writes to bypass serialization. */
525 error = ttydisc_write(tp, uio, ioflag);
527 /* Serialize write() calls. */
528 while (tp->t_flags & TF_BUSY_OUT) {
529 error = tty_wait(tp, &tp->t_outserwait);
534 tp->t_flags |= TF_BUSY_OUT;
535 error = ttydisc_write(tp, uio, ioflag);
536 tp->t_flags &= ~TF_BUSY_OUT;
537 cv_signal(&tp->t_outserwait);
540 done: tty_unlock(tp);
545 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
548 struct tty *tp = dev->si_drv1;
551 error = ttydev_enter(tp);
586 #endif /* COMPAT_43TTY */
588 * If the ioctl() causes the TTY to be modified, let it
589 * wait in the background.
591 error = tty_wait_background(tp, curthread, SIGTTOU);
596 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
597 struct termios *old = &tp->t_termios;
598 struct termios *new = (struct termios *)data;
599 struct termios *lock = TTY_CALLOUT(tp, dev) ?
600 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
604 * Lock state devices. Just overwrite the values of the
605 * commands that are currently in use.
607 new->c_iflag = (old->c_iflag & lock->c_iflag) |
608 (new->c_iflag & ~lock->c_iflag);
609 new->c_oflag = (old->c_oflag & lock->c_oflag) |
610 (new->c_oflag & ~lock->c_oflag);
611 new->c_cflag = (old->c_cflag & lock->c_cflag) |
612 (new->c_cflag & ~lock->c_cflag);
613 new->c_lflag = (old->c_lflag & lock->c_lflag) |
614 (new->c_lflag & ~lock->c_lflag);
615 for (cc = 0; cc < NCCS; ++cc)
617 new->c_cc[cc] = old->c_cc[cc];
619 new->c_ispeed = old->c_ispeed;
621 new->c_ospeed = old->c_ospeed;
624 error = tty_ioctl(tp, cmd, data, fflag, td);
625 done: tty_unlock(tp);
631 ttydev_poll(struct cdev *dev, int events, struct thread *td)
633 struct tty *tp = dev->si_drv1;
634 int error, revents = 0;
636 error = ttydev_enter(tp);
638 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
640 if (events & (POLLIN|POLLRDNORM)) {
641 /* See if we can read something. */
642 if (ttydisc_read_poll(tp) > 0)
643 revents |= events & (POLLIN|POLLRDNORM);
646 if (tp->t_flags & TF_ZOMBIE) {
647 /* Hangup flag on zombie state. */
649 } else if (events & (POLLOUT|POLLWRNORM)) {
650 /* See if we can write something. */
651 if (ttydisc_write_poll(tp) > 0)
652 revents |= events & (POLLOUT|POLLWRNORM);
656 if (events & (POLLIN|POLLRDNORM))
657 selrecord(td, &tp->t_inpoll);
658 if (events & (POLLOUT|POLLWRNORM))
659 selrecord(td, &tp->t_outpoll);
668 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
669 int nprot, vm_memattr_t *memattr)
671 struct tty *tp = dev->si_drv1;
674 /* Handle mmap() through the driver. */
676 error = ttydev_enter(tp);
679 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
690 tty_kqops_read_detach(struct knote *kn)
692 struct tty *tp = kn->kn_hook;
694 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
698 tty_kqops_read_event(struct knote *kn, long hint __unused)
700 struct tty *tp = kn->kn_hook;
702 tty_lock_assert(tp, MA_OWNED);
704 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
705 kn->kn_flags |= EV_EOF;
708 kn->kn_data = ttydisc_read_poll(tp);
709 return (kn->kn_data > 0);
714 tty_kqops_write_detach(struct knote *kn)
716 struct tty *tp = kn->kn_hook;
718 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
722 tty_kqops_write_event(struct knote *kn, long hint __unused)
724 struct tty *tp = kn->kn_hook;
726 tty_lock_assert(tp, MA_OWNED);
729 kn->kn_flags |= EV_EOF;
732 kn->kn_data = ttydisc_write_poll(tp);
733 return (kn->kn_data > 0);
737 static struct filterops tty_kqops_read = {
739 .f_detach = tty_kqops_read_detach,
740 .f_event = tty_kqops_read_event,
743 static struct filterops tty_kqops_write = {
745 .f_detach = tty_kqops_write_detach,
746 .f_event = tty_kqops_write_event,
750 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
752 struct tty *tp = dev->si_drv1;
755 error = ttydev_enter(tp);
759 switch (kn->kn_filter) {
762 kn->kn_fop = &tty_kqops_read;
763 knlist_add(&tp->t_inpoll.si_note, kn, 1);
767 kn->kn_fop = &tty_kqops_write;
768 knlist_add(&tp->t_outpoll.si_note, kn, 1);
779 static struct cdevsw ttydev_cdevsw = {
780 .d_version = D_VERSION,
781 .d_open = ttydev_open,
782 .d_close = ttydev_close,
783 .d_read = ttydev_read,
784 .d_write = ttydev_write,
785 .d_ioctl = ttydev_ioctl,
786 .d_kqfilter = ttydev_kqfilter,
787 .d_poll = ttydev_poll,
788 .d_mmap = ttydev_mmap,
794 * Init/lock-state devices
798 ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
815 ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
816 struct thread *td __unused)
823 ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
831 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
834 struct tty *tp = dev->si_drv1;
843 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
844 if (error != ENOIOCTL)
850 /* Obtain terminal flags through tcgetattr(). */
851 *(struct termios*)data = *(struct termios*)dev->si_drv2;
854 /* Set terminal flags through tcsetattr(). */
855 error = priv_check(td, PRIV_TTY_SETA);
858 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
861 *(int *)data = TTYDISC;
864 bzero(data, sizeof(struct winsize));
870 done: tty_unlock(tp);
874 static struct cdevsw ttyil_cdevsw = {
875 .d_version = D_VERSION,
876 .d_open = ttyil_open,
877 .d_close = ttyil_close,
878 .d_read = ttyil_rdwr,
879 .d_write = ttyil_rdwr,
880 .d_ioctl = ttyil_ioctl,
886 tty_init_termios(struct tty *tp)
888 struct termios *t = &tp->t_termios_init_in;
890 t->c_cflag = TTYDEF_CFLAG;
891 t->c_iflag = TTYDEF_IFLAG;
892 t->c_lflag = TTYDEF_LFLAG;
893 t->c_oflag = TTYDEF_OFLAG;
894 t->c_ispeed = TTYDEF_SPEED;
895 t->c_ospeed = TTYDEF_SPEED;
896 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
898 tp->t_termios_init_out = *t;
902 tty_init_console(struct tty *tp, speed_t s)
904 struct termios *ti = &tp->t_termios_init_in;
905 struct termios *to = &tp->t_termios_init_out;
908 ti->c_ispeed = ti->c_ospeed = s;
909 to->c_ispeed = to->c_ospeed = s;
912 ti->c_cflag |= CLOCAL;
913 to->c_cflag |= CLOCAL;
917 * Standard device routine implementations, mostly meant for
918 * pseudo-terminal device drivers. When a driver creates a new terminal
919 * device class, missing routines are patched.
923 ttydevsw_defopen(struct tty *tp __unused)
930 ttydevsw_defclose(struct tty *tp __unused)
936 ttydevsw_defoutwakeup(struct tty *tp __unused)
939 panic("Terminal device has output, while not implemented");
943 ttydevsw_definwakeup(struct tty *tp __unused)
949 ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
950 caddr_t data __unused, struct thread *td __unused)
957 ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
958 u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
965 ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
969 * Allow the baud rate to be adjusted for pseudo-devices, but at
970 * least restrict it to 115200 to prevent excessive buffer
971 * usage. Also disallow 0, to prevent foot shooting.
973 if (t->c_ispeed < B50)
975 else if (t->c_ispeed > B115200)
976 t->c_ispeed = B115200;
977 if (t->c_ospeed < B50)
979 else if (t->c_ospeed > B115200)
980 t->c_ospeed = B115200;
987 ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
991 /* Simulate a carrier to make the TTY layer happy. */
996 ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
997 vm_paddr_t *paddr __unused, int nprot __unused,
998 vm_memattr_t *memattr __unused)
1005 ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
1011 ttydevsw_deffree(void *softc __unused)
1014 panic("Terminal device freed without a free-handler");
1018 ttydevsw_defbusy(struct tty *tp __unused)
1025 * TTY allocation and deallocation. TTY devices can be deallocated when
1026 * the driver doesn't use it anymore, when the TTY isn't a session's
1027 * controlling TTY and when the device node isn't opened through devfs.
1031 tty_alloc(struct ttydevsw *tsw, void *sc)
1034 return (tty_alloc_mutex(tsw, sc, NULL));
1038 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1042 /* Make sure the driver defines all routines. */
1043 #define PATCH_FUNC(x) do { \
1044 if (tsw->tsw_ ## x == NULL) \
1045 tsw->tsw_ ## x = ttydevsw_def ## x; \
1049 PATCH_FUNC(outwakeup);
1050 PATCH_FUNC(inwakeup);
1056 PATCH_FUNC(pktnotify);
1061 tp = malloc(sizeof(struct tty) + TTY_PRBUF_SIZE, M_TTY,
1063 tp->t_prbufsz = TTY_PRBUF_SIZE;
1065 tp->t_devswsoftc = sc;
1066 tp->t_flags = tsw->tsw_flags;
1067 tp->t_drainwait = tty_drainwait;
1069 tty_init_termios(tp);
1071 cv_init(&tp->t_inwait, "ttyin");
1072 cv_init(&tp->t_outwait, "ttyout");
1073 cv_init(&tp->t_outserwait, "ttyosr");
1074 cv_init(&tp->t_bgwait, "ttybg");
1075 cv_init(&tp->t_dcdwait, "ttydcd");
1077 /* Allow drivers to use a custom mutex to lock the TTY. */
1078 if (mutex != NULL) {
1081 tp->t_mtx = &tp->t_mtxobj;
1082 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1085 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1086 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1092 tty_dealloc(void *arg)
1094 struct tty *tp = arg;
1097 * ttyydev_leave() usually frees the i/o queues earlier, but it is
1098 * not always called between queue allocation and here. The queues
1099 * may be allocated by ioctls on a pty control device without the
1100 * corresponding pty slave device ever being open, or after it is
1103 ttyinq_free(&tp->t_inq);
1104 ttyoutq_free(&tp->t_outq);
1105 seldrain(&tp->t_inpoll);
1106 seldrain(&tp->t_outpoll);
1107 knlist_destroy(&tp->t_inpoll.si_note);
1108 knlist_destroy(&tp->t_outpoll.si_note);
1110 cv_destroy(&tp->t_inwait);
1111 cv_destroy(&tp->t_outwait);
1112 cv_destroy(&tp->t_bgwait);
1113 cv_destroy(&tp->t_dcdwait);
1114 cv_destroy(&tp->t_outserwait);
1116 if (tp->t_mtx == &tp->t_mtxobj)
1117 mtx_destroy(&tp->t_mtxobj);
1123 tty_rel_free(struct tty *tp)
1127 tty_lock_assert(tp, MA_OWNED);
1129 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1130 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1131 /* TTY is still in use. */
1136 /* TTY can be deallocated. */
1142 sx_xlock(&tty_list_sx);
1143 TAILQ_REMOVE(&tty_list, tp, t_list);
1145 sx_xunlock(&tty_list_sx);
1146 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1151 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1154 MPASS(tp->t_sessioncnt > 0);
1155 tty_lock_assert(tp, MA_OWNED);
1157 if (tp->t_pgrp == pg)
1164 tty_rel_sess(struct tty *tp, struct session *sess)
1167 MPASS(tp->t_sessioncnt > 0);
1169 /* Current session has left. */
1170 if (tp->t_session == sess) {
1171 tp->t_session = NULL;
1172 MPASS(tp->t_pgrp == NULL);
1179 tty_rel_gone(struct tty *tp)
1182 MPASS(!tty_gone(tp));
1184 /* Simulate carrier removal. */
1185 ttydisc_modem(tp, 0);
1187 /* Wake up all blocked threads. */
1188 tty_wakeup(tp, FREAD|FWRITE);
1189 cv_broadcast(&tp->t_bgwait);
1190 cv_broadcast(&tp->t_dcdwait);
1192 tp->t_flags |= TF_GONE;
1197 * Exposing information about current TTY's through sysctl
1201 tty_to_xtty(struct tty *tp, struct xtty *xt)
1204 tty_lock_assert(tp, MA_OWNED);
1206 xt->xt_size = sizeof(struct xtty);
1207 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1208 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1209 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1210 xt->xt_inlow = tp->t_inlow;
1211 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1212 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1213 xt->xt_outlow = tp->t_outlow;
1214 xt->xt_column = tp->t_column;
1215 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1216 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1217 xt->xt_flags = tp->t_flags;
1218 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : (uint32_t)NODEV;
1222 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1224 unsigned long lsize;
1225 struct xtty *xtlist, *xt;
1229 sx_slock(&tty_list_sx);
1230 lsize = tty_list_count * sizeof(struct xtty);
1232 sx_sunlock(&tty_list_sx);
1236 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1238 TAILQ_FOREACH(tp, &tty_list, t_list) {
1240 tty_to_xtty(tp, xt);
1244 sx_sunlock(&tty_list_sx);
1246 error = SYSCTL_OUT(req, xtlist, lsize);
1247 free(xtlist, M_TTY);
1251 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1252 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1255 * Device node creation. Device has been set up, now we can expose it to
1260 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1261 const char *fmt, ...)
1264 struct make_dev_args args;
1265 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1266 const char *prefix = "tty";
1267 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1273 /* Remove "tty" prefix from devices like PTY's. */
1274 if (tp->t_flags & TF_NOPREFIX)
1278 vsnrprintf(name, sizeof name, 32, fmt, ap);
1282 /* System device. */
1285 mode = S_IRUSR|S_IWUSR;
1288 uid = cred->cr_ruid;
1290 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1293 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1294 flags |= MAKEDEV_CHECKNAME;
1296 /* Master call-in device. */
1297 make_dev_args_init(&args);
1298 args.mda_flags = flags;
1299 args.mda_devsw = &ttydev_cdevsw;
1303 args.mda_mode = mode;
1304 args.mda_si_drv1 = tp;
1305 error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1310 init = lock = cua = cinit = clock = NULL;
1312 /* Slave call-in devices. */
1313 if (tp->t_flags & TF_INITLOCK) {
1314 args.mda_devsw = &ttyil_cdevsw;
1315 args.mda_unit = TTYUNIT_INIT;
1316 args.mda_si_drv1 = tp;
1317 args.mda_si_drv2 = &tp->t_termios_init_in;
1318 error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1321 dev_depends(dev, init);
1323 args.mda_unit = TTYUNIT_LOCK;
1324 args.mda_si_drv2 = &tp->t_termios_lock_in;
1325 error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1328 dev_depends(dev, lock);
1331 /* Call-out devices. */
1332 if (tp->t_flags & TF_CALLOUT) {
1333 make_dev_args_init(&args);
1334 args.mda_flags = flags;
1335 args.mda_devsw = &ttydev_cdevsw;
1337 args.mda_uid = UID_UUCP;
1338 args.mda_gid = GID_DIALER;
1339 args.mda_mode = 0660;
1340 args.mda_unit = TTYUNIT_CALLOUT;
1341 args.mda_si_drv1 = tp;
1342 error = make_dev_s(&args, &cua, "cua%s", name);
1345 dev_depends(dev, cua);
1347 /* Slave call-out devices. */
1348 if (tp->t_flags & TF_INITLOCK) {
1349 args.mda_devsw = &ttyil_cdevsw;
1350 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1351 args.mda_si_drv2 = &tp->t_termios_init_out;
1352 error = make_dev_s(&args, &cinit, "cua%s.init", name);
1355 dev_depends(dev, cinit);
1357 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1358 args.mda_si_drv2 = &tp->t_termios_lock_out;
1359 error = make_dev_s(&args, &clock, "cua%s.lock", name);
1362 dev_depends(dev, clock);
1366 sx_xlock(&tty_list_sx);
1367 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1369 sx_xunlock(&tty_list_sx);
1388 * Signalling processes.
1392 tty_signal_sessleader(struct tty *tp, int sig)
1396 tty_lock_assert(tp, MA_OWNED);
1397 MPASS(sig >= 1 && sig < NSIG);
1399 /* Make signals start output again. */
1400 tp->t_flags &= ~TF_STOPPED;
1402 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1403 p = tp->t_session->s_leader;
1405 kern_psignal(p, sig);
1411 tty_signal_pgrp(struct tty *tp, int sig)
1415 tty_lock_assert(tp, MA_OWNED);
1416 MPASS(sig >= 1 && sig < NSIG);
1418 /* Make signals start output again. */
1419 tp->t_flags &= ~TF_STOPPED;
1421 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1423 if (tp->t_pgrp != NULL) {
1424 ksiginfo_init(&ksi);
1425 ksi.ksi_signo = sig;
1426 ksi.ksi_code = SI_KERNEL;
1427 PGRP_LOCK(tp->t_pgrp);
1428 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1429 PGRP_UNLOCK(tp->t_pgrp);
1434 tty_wakeup(struct tty *tp, int flags)
1437 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1438 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1440 if (flags & FWRITE) {
1441 cv_broadcast(&tp->t_outwait);
1442 selwakeup(&tp->t_outpoll);
1443 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1445 if (flags & FREAD) {
1446 cv_broadcast(&tp->t_inwait);
1447 selwakeup(&tp->t_inpoll);
1448 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1453 tty_wait(struct tty *tp, struct cv *cv)
1456 int revokecnt = tp->t_revokecnt;
1458 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1459 MPASS(!tty_gone(tp));
1461 error = cv_wait_sig(cv, tp->t_mtx);
1463 /* Bail out when the device slipped away. */
1467 /* Restart the system call when we may have been revoked. */
1468 if (tp->t_revokecnt != revokecnt)
1475 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1478 int revokecnt = tp->t_revokecnt;
1480 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1481 MPASS(!tty_gone(tp));
1483 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1485 /* Bail out when the device slipped away. */
1489 /* Restart the system call when we may have been revoked. */
1490 if (tp->t_revokecnt != revokecnt)
1497 tty_flush(struct tty *tp, int flags)
1500 if (flags & FWRITE) {
1501 tp->t_flags &= ~TF_HIWAT_OUT;
1502 ttyoutq_flush(&tp->t_outq);
1503 tty_wakeup(tp, FWRITE);
1504 if (!tty_gone(tp)) {
1505 ttydevsw_outwakeup(tp);
1506 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1509 if (flags & FREAD) {
1510 tty_hiwat_in_unblock(tp);
1511 ttyinq_flush(&tp->t_inq);
1512 tty_wakeup(tp, FREAD);
1513 if (!tty_gone(tp)) {
1514 ttydevsw_inwakeup(tp);
1515 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1521 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1524 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1526 tp->t_winsize = *wsz;
1527 tty_signal_pgrp(tp, SIGWINCH);
1531 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1539 * The SER_* and TIOCM_* flags are the same, but one bit
1540 * shifted. I don't know why.
1543 ttydevsw_modem(tp, SER_DTR, 0);
1546 ttydevsw_modem(tp, 0, SER_DTR);
1549 int bits = *(int *)data;
1551 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1552 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1556 int bits = *(int *)data;
1557 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1561 int bits = *(int *)data;
1562 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1566 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1571 tp->t_flags |= TF_ASYNC;
1573 tp->t_flags &= ~TF_ASYNC;
1576 /* This device supports non-blocking operation. */
1579 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1583 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1586 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1587 /* Not allowed to set ownership. */
1590 /* Temporarily unlock the TTY to set ownership. */
1592 error = fsetown(*(int *)data, &tp->t_sigio);
1596 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1597 /* Not allowed to set ownership. */
1600 /* Get ownership. */
1601 *(int *)data = fgetown(&tp->t_sigio);
1604 /* Obtain terminal flags through tcgetattr(). */
1605 *(struct termios*)data = tp->t_termios;
1610 struct termios *t = data;
1613 * Who makes up these funny rules? According to POSIX,
1614 * input baud rate is set equal to the output baud rate
1617 if (t->c_ispeed == 0)
1618 t->c_ispeed = t->c_ospeed;
1620 /* Discard any unsupported bits. */
1621 t->c_iflag &= TTYSUP_IFLAG;
1622 t->c_oflag &= TTYSUP_OFLAG;
1623 t->c_lflag &= TTYSUP_LFLAG;
1624 t->c_cflag &= TTYSUP_CFLAG;
1626 /* Set terminal flags through tcsetattr(). */
1627 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1628 error = tty_drain(tp, 0);
1631 if (cmd == TIOCSETAF)
1632 tty_flush(tp, FREAD);
1636 * Only call param() when the flags really change.
1638 if ((t->c_cflag & CIGNORE) == 0 &&
1639 (tp->t_termios.c_cflag != t->c_cflag ||
1640 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1641 (IXON|IXOFF|IXANY)) ||
1642 tp->t_termios.c_ispeed != t->c_ispeed ||
1643 tp->t_termios.c_ospeed != t->c_ospeed)) {
1644 error = ttydevsw_param(tp, t);
1650 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1651 tp->t_termios.c_ispeed = t->c_ispeed;
1652 tp->t_termios.c_ospeed = t->c_ospeed;
1654 /* Baud rate has changed - update watermarks. */
1655 error = tty_watermarks(tp);
1660 /* Copy new non-device driver parameters. */
1661 tp->t_termios.c_iflag = t->c_iflag;
1662 tp->t_termios.c_oflag = t->c_oflag;
1663 tp->t_termios.c_lflag = t->c_lflag;
1664 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1666 ttydisc_optimize(tp);
1668 if ((t->c_lflag & ICANON) == 0) {
1670 * When in non-canonical mode, wake up all
1671 * readers. Canonicalize any partial input. VMIN
1672 * and VTIME could also be adjusted.
1674 ttyinq_canonicalize(&tp->t_inq);
1675 tty_wakeup(tp, FREAD);
1679 * For packet mode: notify the PTY consumer that VSTOP
1680 * and VSTART may have been changed.
1682 if (tp->t_termios.c_iflag & IXON &&
1683 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1684 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1685 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1687 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1691 /* For compatibility - we only support TTYDISC. */
1692 *(int *)data = TTYDISC;
1695 if (!tty_is_ctty(tp, td->td_proc))
1698 if (tp->t_pgrp != NULL)
1699 *(int *)data = tp->t_pgrp->pg_id;
1701 *(int *)data = NO_PID;
1704 if (!tty_is_ctty(tp, td->td_proc))
1707 MPASS(tp->t_session);
1708 *(int *)data = tp->t_session->s_sid;
1711 struct proc *p = td->td_proc;
1713 /* XXX: This looks awful. */
1715 sx_xlock(&proctree_lock);
1718 if (!SESS_LEADER(p)) {
1719 /* Only the session leader may do this. */
1720 sx_xunlock(&proctree_lock);
1724 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1725 /* This is already our controlling TTY. */
1726 sx_xunlock(&proctree_lock);
1730 if (p->p_session->s_ttyp != NULL ||
1731 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1732 tp->t_session->s_ttyvp->v_type != VBAD)) {
1734 * There is already a relation between a TTY and
1735 * a session, or the caller is not the session
1738 * Allow the TTY to be stolen when the vnode is
1739 * invalid, but the reference to the TTY is
1740 * still active. This allows immediate reuse of
1741 * TTYs of which the session leader has been
1742 * killed or the TTY revoked.
1744 sx_xunlock(&proctree_lock);
1748 /* Connect the session to the TTY. */
1749 tp->t_session = p->p_session;
1750 tp->t_session->s_ttyp = tp;
1752 sx_xunlock(&proctree_lock);
1754 /* Assign foreground process group. */
1755 tp->t_pgrp = p->p_pgrp;
1757 p->p_flag |= P_CONTROLT;
1766 * XXX: Temporarily unlock the TTY to locate the process
1767 * group. This code would be lot nicer if we would ever
1768 * decompose proctree_lock.
1771 sx_slock(&proctree_lock);
1772 pg = pgfind(*(int *)data);
1775 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1776 sx_sunlock(&proctree_lock);
1783 * Determine if this TTY is the controlling TTY after
1784 * relocking the TTY.
1786 if (!tty_is_ctty(tp, td->td_proc)) {
1787 sx_sunlock(&proctree_lock);
1791 sx_sunlock(&proctree_lock);
1793 /* Wake up the background process groups. */
1794 cv_broadcast(&tp->t_bgwait);
1798 int flags = *(int *)data;
1801 flags = (FREAD|FWRITE);
1803 flags &= (FREAD|FWRITE);
1804 tty_flush(tp, flags);
1808 /* Drain TTY output. */
1809 return tty_drain(tp, 0);
1810 case TIOCGDRAINWAIT:
1811 *(int *)data = tp->t_drainwait;
1813 case TIOCSDRAINWAIT:
1814 error = priv_check(td, PRIV_TTY_DRAINWAIT);
1816 tp->t_drainwait = *(int *)data;
1819 /* Set terminal as console TTY. */
1821 error = priv_check(td, PRIV_TTY_CONSOLE);
1826 * XXX: constty should really need to be locked!
1827 * XXX: allow disconnected constty's to be stolen!
1832 if (constty != NULL)
1838 } else if (constty == tp) {
1843 /* Obtain window size. */
1844 *(struct winsize*)data = tp->t_winsize;
1847 /* Set window size. */
1848 tty_set_winsize(tp, data);
1851 tp->t_flags |= TF_EXCLUDE;
1854 tp->t_flags &= ~TF_EXCLUDE;
1857 tp->t_flags |= TF_STOPPED;
1858 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1861 tp->t_flags &= ~TF_STOPPED;
1862 ttydevsw_outwakeup(tp);
1863 ttydevsw_pktnotify(tp, TIOCPKT_START);
1869 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1871 if (!tty_is_ctty(tp, td->td_proc) &&
1872 priv_check(td, PRIV_TTY_STI))
1874 ttydisc_rint(tp, *(char *)data, 0);
1875 ttydisc_rint_done(tp);
1880 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1881 #else /* !COMPAT_43TTY */
1883 #endif /* COMPAT_43TTY */
1887 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1891 tty_lock_assert(tp, MA_OWNED);
1896 error = ttydevsw_ioctl(tp, cmd, data, td);
1897 if (error == ENOIOCTL)
1898 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1904 tty_udev(struct tty *tp)
1908 return (dev2udev(tp->t_dev));
1914 tty_checkoutq(struct tty *tp)
1917 /* 256 bytes should be enough to print a log message. */
1918 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1922 tty_hiwat_in_block(struct tty *tp)
1925 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1926 tp->t_termios.c_iflag & IXOFF &&
1927 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1929 * Input flow control. Only enter the high watermark when we
1930 * can successfully store the VSTOP character.
1932 if (ttyoutq_write_nofrag(&tp->t_outq,
1933 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1934 tp->t_flags |= TF_HIWAT_IN;
1936 /* No input flow control. */
1937 tp->t_flags |= TF_HIWAT_IN;
1942 tty_hiwat_in_unblock(struct tty *tp)
1945 if (tp->t_flags & TF_HIWAT_IN &&
1946 tp->t_termios.c_iflag & IXOFF &&
1947 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1949 * Input flow control. Only leave the high watermark when we
1950 * can successfully store the VSTART character.
1952 if (ttyoutq_write_nofrag(&tp->t_outq,
1953 &tp->t_termios.c_cc[VSTART], 1) == 0)
1954 tp->t_flags &= ~TF_HIWAT_IN;
1956 /* No input flow control. */
1957 tp->t_flags &= ~TF_HIWAT_IN;
1961 ttydevsw_inwakeup(tp);
1965 * TTY hooks interface.
1969 ttyhook_defrint(struct tty *tp, char c, int flags)
1972 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1979 ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
1986 struct filedesc *fdp;
1987 cap_rights_t rights;
1990 /* Validate the file descriptor. */
1992 error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1996 if (fp->f_ops == &badfileops) {
2002 * Make sure the vnode is bound to a character device.
2003 * Unlocked check for the vnode type is ok there, because we
2004 * only shall prevent calling devvn_refthread on the file that
2005 * never has been opened over a character device.
2007 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
2012 /* Make sure it is a TTY. */
2013 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
2018 if (dev != fp->f_data) {
2022 if (cdp != &ttydev_cdevsw) {
2028 /* Try to attach the hook to the TTY. */
2031 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
2032 if (tp->t_flags & TF_HOOK)
2035 tp->t_flags |= TF_HOOK;
2037 tp->t_hooksoftc = softc;
2041 /* Maybe we can switch into bypass mode now. */
2042 ttydisc_optimize(tp);
2044 /* Silently convert rint() calls to rint_bypass() when possible. */
2045 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2046 th->th_rint = ttyhook_defrint;
2048 done3: tty_unlock(tp);
2049 done2: dev_relthread(dev, ref);
2050 done1: fdrop(fp, curthread);
2055 ttyhook_unregister(struct tty *tp)
2058 tty_lock_assert(tp, MA_OWNED);
2059 MPASS(tp->t_flags & TF_HOOK);
2061 /* Disconnect the hook. */
2062 tp->t_flags &= ~TF_HOOK;
2065 /* Maybe we need to leave bypass mode. */
2066 ttydisc_optimize(tp);
2068 /* Maybe deallocate the TTY as well. */
2073 * /dev/console handling.
2077 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2081 /* System has no console device. */
2082 if (dev_console_filename == NULL)
2085 /* Look up corresponding TTY by device name. */
2086 sx_slock(&tty_list_sx);
2087 TAILQ_FOREACH(tp, &tty_list, t_list) {
2088 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2089 dev_console->si_drv1 = tp;
2093 sx_sunlock(&tty_list_sx);
2095 /* System console has no TTY associated. */
2096 if (dev_console->si_drv1 == NULL)
2099 return (ttydev_open(dev, oflags, devtype, td));
2103 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2108 return (ttydev_write(dev, uio, ioflag));
2112 * /dev/console is a little different than normal TTY's. When opened,
2113 * it determines which TTY to use. When data gets written to it, it
2114 * will be logged in the kernel message buffer.
2116 static struct cdevsw ttyconsdev_cdevsw = {
2117 .d_version = D_VERSION,
2118 .d_open = ttyconsdev_open,
2119 .d_close = ttydev_close,
2120 .d_read = ttydev_read,
2121 .d_write = ttyconsdev_write,
2122 .d_ioctl = ttydev_ioctl,
2123 .d_kqfilter = ttydev_kqfilter,
2124 .d_poll = ttydev_poll,
2125 .d_mmap = ttydev_mmap,
2126 .d_name = "ttyconsdev",
2131 ttyconsdev_init(void *unused __unused)
2134 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2135 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2138 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2141 ttyconsdev_select(const char *name)
2144 dev_console_filename = name;
2148 * Debugging routines.
2151 #include "opt_ddb.h"
2153 #include <ddb/ddb.h>
2154 #include <ddb/db_sym.h>
2156 static const struct {
2161 { TF_NOPREFIX, 'N' },
2163 { TF_INITLOCK, 'I' },
2164 { TF_CALLOUT, 'C' },
2166 /* Keep these together -> 'Oi' and 'Oo'. */
2168 { TF_OPENED_IN, 'i' },
2169 { TF_OPENED_OUT, 'o' },
2170 { TF_OPENED_CONS, 'c' },
2173 { TF_OPENCLOSE, 'B' },
2175 { TF_LITERAL, 'L' },
2177 /* Keep these together -> 'Hi' and 'Ho'. */
2179 { TF_HIWAT_IN, 'i' },
2180 { TF_HIWAT_OUT, 'o' },
2182 { TF_STOPPED, 'S' },
2183 { TF_EXCLUDE, 'X' },
2188 /* Keep these together -> 'bi' and 'bo'. */
2190 { TF_BUSY_IN, 'i' },
2191 { TF_BUSY_OUT, 'o' },
2196 #define TTY_FLAG_BITS \
2197 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2198 "\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2199 "\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2200 "\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2201 "\21HOOK\22BUSY_IN\23BUSY_OUT"
2203 #define DB_PRINTSYM(name, addr) \
2204 db_printf("%s " #name ": ", sep); \
2205 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2209 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2212 db_printf("%sdevsw: ", sep);
2213 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2214 db_printf(" (%p)\n", tsw);
2215 DB_PRINTSYM(open, tsw->tsw_open);
2216 DB_PRINTSYM(close, tsw->tsw_close);
2217 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2218 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2219 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2220 DB_PRINTSYM(param, tsw->tsw_param);
2221 DB_PRINTSYM(modem, tsw->tsw_modem);
2222 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2223 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2224 DB_PRINTSYM(free, tsw->tsw_free);
2228 _db_show_hooks(const char *sep, const struct ttyhook *th)
2231 db_printf("%shook: ", sep);
2232 db_printsym((db_addr_t)th, DB_STGY_ANY);
2233 db_printf(" (%p)\n", th);
2236 DB_PRINTSYM(rint, th->th_rint);
2237 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2238 DB_PRINTSYM(rint_done, th->th_rint_done);
2239 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2240 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2241 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2242 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2243 DB_PRINTSYM(close, th->th_close);
2247 _db_show_termios(const char *name, const struct termios *t)
2250 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2251 "lflag 0x%x ispeed %u ospeed %u\n", name,
2252 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2253 t->c_ispeed, t->c_ospeed);
2256 /* DDB command to show TTY statistics. */
2257 DB_SHOW_COMMAND(tty, db_show_tty)
2262 db_printf("usage: show tty <addr>\n");
2265 tp = (struct tty *)addr;
2267 db_printf("%p: %s\n", tp, tty_devname(tp));
2268 db_printf("\tmtx: %p\n", tp->t_mtx);
2269 db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2270 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2272 /* Buffering mechanisms. */
2273 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2274 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2275 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2276 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2277 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2278 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2279 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2280 db_printf("\tinlow: %zu\n", tp->t_inlow);
2281 db_printf("\toutlow: %zu\n", tp->t_outlow);
2282 _db_show_termios("\ttermios", &tp->t_termios);
2283 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2284 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2285 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2286 db_printf("\tcolumn: %u\n", tp->t_column);
2287 db_printf("\twritepos: %u\n", tp->t_writepos);
2288 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2290 /* Init/lock-state devices. */
2291 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2292 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2293 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2294 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2297 _db_show_devsw("\t", tp->t_devsw);
2298 _db_show_hooks("\t", tp->t_hook);
2301 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2302 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2303 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2304 db_printf("\tsession: %p", tp->t_session);
2305 if (tp->t_session != NULL)
2306 db_printf(" count %u leader %p tty %p sid %d login %s",
2307 tp->t_session->s_count, tp->t_session->s_leader,
2308 tp->t_session->s_ttyp, tp->t_session->s_sid,
2309 tp->t_session->s_login);
2311 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2312 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2313 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2314 db_printf("\tdev: %p\n", tp->t_dev);
2317 /* DDB command to list TTYs. */
2318 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2324 /* Make the output look like `pstat -t'. */
2326 #if defined(__LP64__)
2329 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2330 "COL SESS PGID STATE\n");
2332 TAILQ_FOREACH(tp, &tty_list, t_list) {
2333 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2334 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2336 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2337 "%5d ", tp, tty_devname(tp), isiz,
2338 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2339 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2340 isiz - tp->t_inlow, osiz,
2341 tp->t_outq.to_end - tp->t_outq.to_begin,
2342 osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2343 tp->t_session ? tp->t_session->s_sid : 0,
2344 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2347 for (i = j = 0; ttystates[i].flag; i++)
2348 if (tp->t_flags & ttystates[i].flag) {
2349 db_printf("%c", ttystates[i].val);