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 <fs/devfs/devfs.h>
72 #include <machine/stdarg.h>
74 static MALLOC_DEFINE(M_TTY, "tty", "tty device");
76 static void tty_rel_free(struct tty *tp);
78 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
79 static struct sx tty_list_sx;
80 SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
81 static unsigned int tty_list_count = 0;
83 /* Character device of /dev/console. */
84 static struct cdev *dev_console;
85 static const char *dev_console_filename;
88 * Flags that are supported and stored by this implementation.
90 #define TTYSUP_IFLAG (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\
91 INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL)
92 #define TTYSUP_OFLAG (OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET)
93 #define TTYSUP_LFLAG (ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\
94 ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\
95 FLUSHO|NOKERNINFO|NOFLSH)
96 #define TTYSUP_CFLAG (CIGNORE|CSIZE|CSTOPB|CREAD|PARENB|PARODD|\
97 HUPCL|CLOCAL|CCTS_OFLOW|CRTS_IFLOW|CDTR_IFLOW|\
98 CDSR_OFLOW|CCAR_OFLOW|CNO_RTSDTR)
100 #define TTY_CALLOUT(tp,d) (dev2unit(d) & TTYUNIT_CALLOUT)
102 static int tty_drainwait = 5 * 60;
103 SYSCTL_INT(_kern, OID_AUTO, tty_drainwait, CTLFLAG_RWTUN,
104 &tty_drainwait, 0, "Default output drain timeout in seconds");
107 * Set TTY buffer sizes.
110 #define TTYBUF_MAX 65536
112 #ifdef PRINTF_BUFR_SIZE
113 #define TTY_PRBUF_SIZE PRINTF_BUFR_SIZE
115 #define TTY_PRBUF_SIZE 256
119 * Allocate buffer space if necessary, and set low watermarks, based on speed.
120 * Note that the ttyxxxq_setsize() functions may drop and then reacquire the tty
121 * lock during memory allocation. They will return ENXIO if the tty disappears
125 tty_watermarks(struct tty *tp)
130 /* Provide an input buffer for 2 seconds of data. */
131 if (tp->t_termios.c_cflag & CREAD)
132 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX);
133 error = ttyinq_setsize(&tp->t_inq, tp, bs);
137 /* Set low watermark at 10% (when 90% is available). */
138 tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
140 /* Provide an output buffer for 2 seconds of data. */
141 bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX);
142 error = ttyoutq_setsize(&tp->t_outq, tp, bs);
146 /* Set low watermark at 10% (when 90% is available). */
147 tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
153 tty_drain(struct tty *tp, int leaving)
155 sbintime_t timeout_at;
159 if (ttyhook_hashook(tp, getc_inject))
160 /* buffer is inaccessible */
164 * For close(), use the recent historic timeout of "1 second without
165 * making progress". For tcdrain(), use t_drainwait as the timeout,
166 * with zero meaning "no timeout" which gives POSIX behavior.
169 timeout_at = getsbinuptime() + SBT_1S;
170 else if (tp->t_drainwait != 0)
171 timeout_at = getsbinuptime() + SBT_1S * tp->t_drainwait;
176 * Poll the output buffer and the hardware for completion, at 10 Hz.
177 * Polling is required for devices which are not able to signal an
178 * interrupt when the transmitter becomes idle (most USB serial devs).
179 * The unusual structure of this loop ensures we check for busy one more
180 * time after tty_timedwait() returns EWOULDBLOCK, so that success has
181 * higher priority than timeout if the IO completed in the last 100mS.
184 bytes = ttyoutq_bytesused(&tp->t_outq);
186 if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp))
190 ttydevsw_outwakeup(tp);
191 error = tty_timedwait(tp, &tp->t_outwait, hz / 10);
192 if (error != 0 && error != EWOULDBLOCK)
194 else if (timeout_at == 0 || getsbinuptime() < timeout_at)
196 else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes) {
197 /* In close, making progress, grant an extra second. */
199 timeout_at += SBT_1S;
200 bytes = ttyoutq_bytesused(&tp->t_outq);
206 * Though ttydev_enter() and ttydev_leave() seem to be related, they
207 * don't have to be used together. ttydev_enter() is used by the cdev
208 * operations to prevent an actual operation from being processed when
209 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
210 * and ttydev_close() to determine whether per-TTY data should be
215 ttydev_enter(struct tty *tp)
220 if (tty_gone(tp) || !tty_opened(tp)) {
221 /* Device is already gone. */
230 ttydev_leave(struct tty *tp)
233 tty_assert_locked(tp);
235 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
236 /* Device is still opened somewhere. */
241 tp->t_flags |= TF_OPENCLOSE;
243 /* Remove console TTY. */
246 /* Drain any output. */
252 /* Free i/o queues now since they might be large. */
253 ttyinq_free(&tp->t_inq);
255 ttyoutq_free(&tp->t_outq);
258 knlist_clear(&tp->t_inpoll.si_note, 1);
259 knlist_clear(&tp->t_outpoll.si_note, 1);
264 tp->t_flags &= ~TF_OPENCLOSE;
265 cv_broadcast(&tp->t_dcdwait);
270 * Operations that are exposed through the character device in /dev.
273 ttydev_open(struct cdev *dev, int oflags, int devtype __unused,
283 /* Device is already gone. */
289 * Block when other processes are currently opening or closing
292 while (tp->t_flags & TF_OPENCLOSE) {
293 error = tty_wait(tp, &tp->t_dcdwait);
299 tp->t_flags |= TF_OPENCLOSE;
302 * Make sure the "tty" and "cua" device cannot be opened at the
303 * same time. The console is a "tty" device.
305 if (TTY_CALLOUT(tp, dev)) {
306 if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) {
311 if (tp->t_flags & TF_OPENED_OUT) {
317 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
322 if (!tty_opened(tp)) {
323 /* Set proper termios flags. */
324 if (TTY_CALLOUT(tp, dev))
325 tp->t_termios = tp->t_termios_init_out;
327 tp->t_termios = tp->t_termios_init_in;
328 ttydevsw_param(tp, &tp->t_termios);
329 /* Prevent modem control on callout devices and /dev/console. */
330 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
331 tp->t_termios.c_cflag |= CLOCAL;
333 if ((tp->t_termios.c_cflag & CNO_RTSDTR) == 0)
334 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
336 error = ttydevsw_open(tp);
341 error = tty_watermarks(tp);
346 /* Wait for Carrier Detect. */
347 if ((oflags & O_NONBLOCK) == 0 &&
348 (tp->t_termios.c_cflag & CLOCAL) == 0) {
349 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
350 error = tty_wait(tp, &tp->t_dcdwait);
356 if (dev == dev_console)
357 tp->t_flags |= TF_OPENED_CONS;
358 else if (TTY_CALLOUT(tp, dev))
359 tp->t_flags |= TF_OPENED_OUT;
361 tp->t_flags |= TF_OPENED_IN;
362 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
363 (tp->t_flags & TF_OPENED_OUT) == 0);
365 done: tp->t_flags &= ~TF_OPENCLOSE;
366 cv_broadcast(&tp->t_dcdwait);
373 ttydev_close(struct cdev *dev, int fflag, int devtype __unused,
374 struct thread *td __unused)
376 struct tty *tp = dev->si_drv1;
381 * Don't actually close the device if it is being used as the
384 MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
385 (tp->t_flags & TF_OPENED_OUT) == 0);
386 if (dev == dev_console)
387 tp->t_flags &= ~TF_OPENED_CONS;
389 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
391 if (tp->t_flags & TF_OPENED) {
396 /* If revoking, flush output now to avoid draining it later. */
398 tty_flush(tp, FWRITE);
400 tp->t_flags &= ~TF_EXCLUDE;
402 /* Properly wake up threads that are stuck - revoke(). */
404 tty_wakeup(tp, FREAD|FWRITE);
405 cv_broadcast(&tp->t_bgwait);
406 cv_broadcast(&tp->t_dcdwait);
414 tty_is_ctty(struct tty *tp, struct proc *p)
417 tty_assert_locked(tp);
419 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
423 tty_wait_background(struct tty *tp, struct thread *td, int sig)
430 MPASS(sig == SIGTTIN || sig == SIGTTOU);
431 tty_assert_locked(tp);
440 * pg may no longer be our process group.
441 * Re-check after locking.
443 if (p->p_pgrp != pg) {
450 * The process should only sleep, when:
451 * - This terminal is the controlling terminal
452 * - Its process group is not the foreground process
454 * - The parent process isn't waiting for the child to
456 * - the signal to send to the process isn't masked
458 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
459 /* Allow the action to happen. */
465 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
466 SIGISMEMBER(td->td_sigmask, sig)) {
467 /* Only allow them in write()/ioctl(). */
470 return (sig == SIGTTOU ? 0 : EIO);
473 if ((p->p_flag & P_PPWAIT) != 0 ||
474 (pg->pg_flags & PGRP_ORPHANED) != 0) {
475 /* Don't allow the action to happen. */
483 * Send the signal and sleep until we're the new
484 * foreground process group.
488 ksi.ksi_code = SI_KERNEL;
493 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
496 error = tty_wait(tp, &tp->t_bgwait);
503 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
505 struct tty *tp = dev->si_drv1;
508 error = ttydev_enter(tp);
511 error = ttydisc_read(tp, uio, ioflag);
515 * The read() call should not throw an error when the device is
516 * being destroyed. Silently convert it to an EOF.
518 done: if (error == ENXIO)
524 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
526 struct tty *tp = dev->si_drv1;
529 error = ttydev_enter(tp);
533 if (tp->t_termios.c_lflag & TOSTOP) {
534 error = tty_wait_background(tp, curthread, SIGTTOU);
539 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
540 /* Allow non-blocking writes to bypass serialization. */
541 error = ttydisc_write(tp, uio, ioflag);
543 /* Serialize write() calls. */
544 while (tp->t_flags & TF_BUSY_OUT) {
545 error = tty_wait(tp, &tp->t_outserwait);
550 tp->t_flags |= TF_BUSY_OUT;
551 defer = sigdeferstop(SIGDEFERSTOP_ERESTART);
552 error = ttydisc_write(tp, uio, ioflag);
554 tp->t_flags &= ~TF_BUSY_OUT;
555 cv_signal(&tp->t_outserwait);
558 done: tty_unlock(tp);
563 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
566 struct tty *tp = dev->si_drv1;
569 error = ttydev_enter(tp);
604 #endif /* COMPAT_43TTY */
606 * If the ioctl() causes the TTY to be modified, let it
607 * wait in the background.
609 error = tty_wait_background(tp, curthread, SIGTTOU);
614 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
615 struct termios *old = &tp->t_termios;
616 struct termios *new = (struct termios *)data;
617 struct termios *lock = TTY_CALLOUT(tp, dev) ?
618 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
622 * Lock state devices. Just overwrite the values of the
623 * commands that are currently in use.
625 new->c_iflag = (old->c_iflag & lock->c_iflag) |
626 (new->c_iflag & ~lock->c_iflag);
627 new->c_oflag = (old->c_oflag & lock->c_oflag) |
628 (new->c_oflag & ~lock->c_oflag);
629 new->c_cflag = (old->c_cflag & lock->c_cflag) |
630 (new->c_cflag & ~lock->c_cflag);
631 new->c_lflag = (old->c_lflag & lock->c_lflag) |
632 (new->c_lflag & ~lock->c_lflag);
633 for (cc = 0; cc < NCCS; ++cc)
635 new->c_cc[cc] = old->c_cc[cc];
637 new->c_ispeed = old->c_ispeed;
639 new->c_ospeed = old->c_ospeed;
642 error = tty_ioctl(tp, cmd, data, fflag, td);
643 done: tty_unlock(tp);
649 ttydev_poll(struct cdev *dev, int events, struct thread *td)
651 struct tty *tp = dev->si_drv1;
652 int error, revents = 0;
654 error = ttydev_enter(tp);
656 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
658 if (events & (POLLIN|POLLRDNORM)) {
659 /* See if we can read something. */
660 if (ttydisc_read_poll(tp) > 0)
661 revents |= events & (POLLIN|POLLRDNORM);
664 if (tp->t_flags & TF_ZOMBIE) {
665 /* Hangup flag on zombie state. */
667 } else if (events & (POLLOUT|POLLWRNORM)) {
668 /* See if we can write something. */
669 if (ttydisc_write_poll(tp) > 0)
670 revents |= events & (POLLOUT|POLLWRNORM);
674 if (events & (POLLIN|POLLRDNORM))
675 selrecord(td, &tp->t_inpoll);
676 if (events & (POLLOUT|POLLWRNORM))
677 selrecord(td, &tp->t_outpoll);
686 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
687 int nprot, vm_memattr_t *memattr)
689 struct tty *tp = dev->si_drv1;
692 /* Handle mmap() through the driver. */
694 error = ttydev_enter(tp);
697 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
708 tty_kqops_read_detach(struct knote *kn)
710 struct tty *tp = kn->kn_hook;
712 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
716 tty_kqops_read_event(struct knote *kn, long hint __unused)
718 struct tty *tp = kn->kn_hook;
720 tty_assert_locked(tp);
722 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
723 kn->kn_flags |= EV_EOF;
726 kn->kn_data = ttydisc_read_poll(tp);
727 return (kn->kn_data > 0);
732 tty_kqops_write_detach(struct knote *kn)
734 struct tty *tp = kn->kn_hook;
736 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
740 tty_kqops_write_event(struct knote *kn, long hint __unused)
742 struct tty *tp = kn->kn_hook;
744 tty_assert_locked(tp);
747 kn->kn_flags |= EV_EOF;
750 kn->kn_data = ttydisc_write_poll(tp);
751 return (kn->kn_data > 0);
755 static struct filterops tty_kqops_read = {
757 .f_detach = tty_kqops_read_detach,
758 .f_event = tty_kqops_read_event,
761 static struct filterops tty_kqops_write = {
763 .f_detach = tty_kqops_write_detach,
764 .f_event = tty_kqops_write_event,
768 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
770 struct tty *tp = dev->si_drv1;
773 error = ttydev_enter(tp);
777 switch (kn->kn_filter) {
780 kn->kn_fop = &tty_kqops_read;
781 knlist_add(&tp->t_inpoll.si_note, kn, 1);
785 kn->kn_fop = &tty_kqops_write;
786 knlist_add(&tp->t_outpoll.si_note, kn, 1);
797 static struct cdevsw ttydev_cdevsw = {
798 .d_version = D_VERSION,
799 .d_open = ttydev_open,
800 .d_close = ttydev_close,
801 .d_read = ttydev_read,
802 .d_write = ttydev_write,
803 .d_ioctl = ttydev_ioctl,
804 .d_kqfilter = ttydev_kqfilter,
805 .d_poll = ttydev_poll,
806 .d_mmap = ttydev_mmap,
812 * Init/lock-state devices
816 ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
833 ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
834 struct thread *td __unused)
841 ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
849 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
852 struct tty *tp = dev->si_drv1;
861 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
862 if (error != ENOIOCTL)
868 /* Obtain terminal flags through tcgetattr(). */
869 *(struct termios*)data = *(struct termios*)dev->si_drv2;
872 /* Set terminal flags through tcsetattr(). */
873 error = priv_check(td, PRIV_TTY_SETA);
876 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
879 *(int *)data = TTYDISC;
882 bzero(data, sizeof(struct winsize));
888 done: tty_unlock(tp);
892 static struct cdevsw ttyil_cdevsw = {
893 .d_version = D_VERSION,
894 .d_open = ttyil_open,
895 .d_close = ttyil_close,
896 .d_read = ttyil_rdwr,
897 .d_write = ttyil_rdwr,
898 .d_ioctl = ttyil_ioctl,
904 tty_init_termios(struct tty *tp)
906 struct termios *t = &tp->t_termios_init_in;
908 t->c_cflag = TTYDEF_CFLAG;
909 t->c_iflag = TTYDEF_IFLAG;
910 t->c_lflag = TTYDEF_LFLAG;
911 t->c_oflag = TTYDEF_OFLAG;
912 t->c_ispeed = TTYDEF_SPEED;
913 t->c_ospeed = TTYDEF_SPEED;
914 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
916 tp->t_termios_init_out = *t;
920 tty_init_console(struct tty *tp, speed_t s)
922 struct termios *ti = &tp->t_termios_init_in;
923 struct termios *to = &tp->t_termios_init_out;
926 ti->c_ispeed = ti->c_ospeed = s;
927 to->c_ispeed = to->c_ospeed = s;
930 ti->c_cflag |= CLOCAL;
931 to->c_cflag |= CLOCAL;
935 * Standard device routine implementations, mostly meant for
936 * pseudo-terminal device drivers. When a driver creates a new terminal
937 * device class, missing routines are patched.
941 ttydevsw_defopen(struct tty *tp __unused)
948 ttydevsw_defclose(struct tty *tp __unused)
954 ttydevsw_defoutwakeup(struct tty *tp __unused)
957 panic("Terminal device has output, while not implemented");
961 ttydevsw_definwakeup(struct tty *tp __unused)
967 ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
968 caddr_t data __unused, struct thread *td __unused)
975 ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
976 u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
983 ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
987 * Allow the baud rate to be adjusted for pseudo-devices, but at
988 * least restrict it to 115200 to prevent excessive buffer
989 * usage. Also disallow 0, to prevent foot shooting.
991 if (t->c_ispeed < B50)
993 else if (t->c_ispeed > B115200)
994 t->c_ispeed = B115200;
995 if (t->c_ospeed < B50)
997 else if (t->c_ospeed > B115200)
998 t->c_ospeed = B115200;
1005 ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
1006 int sigoff __unused)
1009 /* Simulate a carrier to make the TTY layer happy. */
1014 ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
1015 vm_paddr_t *paddr __unused, int nprot __unused,
1016 vm_memattr_t *memattr __unused)
1023 ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
1029 ttydevsw_deffree(void *softc __unused)
1032 panic("Terminal device freed without a free-handler");
1036 ttydevsw_defbusy(struct tty *tp __unused)
1043 * TTY allocation and deallocation. TTY devices can be deallocated when
1044 * the driver doesn't use it anymore, when the TTY isn't a session's
1045 * controlling TTY and when the device node isn't opened through devfs.
1049 tty_alloc(struct ttydevsw *tsw, void *sc)
1052 return (tty_alloc_mutex(tsw, sc, NULL));
1056 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1060 /* Make sure the driver defines all routines. */
1061 #define PATCH_FUNC(x) do { \
1062 if (tsw->tsw_ ## x == NULL) \
1063 tsw->tsw_ ## x = ttydevsw_def ## x; \
1067 PATCH_FUNC(outwakeup);
1068 PATCH_FUNC(inwakeup);
1074 PATCH_FUNC(pktnotify);
1079 tp = malloc(sizeof(struct tty) + TTY_PRBUF_SIZE, M_TTY,
1081 tp->t_prbufsz = TTY_PRBUF_SIZE;
1083 tp->t_devswsoftc = sc;
1084 tp->t_flags = tsw->tsw_flags;
1085 tp->t_drainwait = tty_drainwait;
1087 tty_init_termios(tp);
1089 cv_init(&tp->t_inwait, "ttyin");
1090 cv_init(&tp->t_outwait, "ttyout");
1091 cv_init(&tp->t_outserwait, "ttyosr");
1092 cv_init(&tp->t_bgwait, "ttybg");
1093 cv_init(&tp->t_dcdwait, "ttydcd");
1095 /* Allow drivers to use a custom mutex to lock the TTY. */
1096 if (mutex != NULL) {
1099 tp->t_mtx = &tp->t_mtxobj;
1100 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1103 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1104 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1110 tty_dealloc(void *arg)
1112 struct tty *tp = arg;
1115 * ttyydev_leave() usually frees the i/o queues earlier, but it is
1116 * not always called between queue allocation and here. The queues
1117 * may be allocated by ioctls on a pty control device without the
1118 * corresponding pty slave device ever being open, or after it is
1121 ttyinq_free(&tp->t_inq);
1122 ttyoutq_free(&tp->t_outq);
1123 seldrain(&tp->t_inpoll);
1124 seldrain(&tp->t_outpoll);
1125 knlist_destroy(&tp->t_inpoll.si_note);
1126 knlist_destroy(&tp->t_outpoll.si_note);
1128 cv_destroy(&tp->t_inwait);
1129 cv_destroy(&tp->t_outwait);
1130 cv_destroy(&tp->t_bgwait);
1131 cv_destroy(&tp->t_dcdwait);
1132 cv_destroy(&tp->t_outserwait);
1134 if (tp->t_mtx == &tp->t_mtxobj)
1135 mtx_destroy(&tp->t_mtxobj);
1141 tty_rel_free(struct tty *tp)
1145 tty_assert_locked(tp);
1147 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1148 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1149 /* TTY is still in use. */
1154 /* Stop asynchronous I/O. */
1155 funsetown(&tp->t_sigio);
1157 /* TTY can be deallocated. */
1163 sx_xlock(&tty_list_sx);
1164 TAILQ_REMOVE(&tty_list, tp, t_list);
1166 sx_xunlock(&tty_list_sx);
1167 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1172 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1175 MPASS(tp->t_sessioncnt > 0);
1176 tty_assert_locked(tp);
1178 if (tp->t_pgrp == pg)
1185 tty_rel_sess(struct tty *tp, struct session *sess)
1188 MPASS(tp->t_sessioncnt > 0);
1190 /* Current session has left. */
1191 if (tp->t_session == sess) {
1192 tp->t_session = NULL;
1193 MPASS(tp->t_pgrp == NULL);
1200 tty_rel_gone(struct tty *tp)
1203 tty_assert_locked(tp);
1204 MPASS(!tty_gone(tp));
1206 /* Simulate carrier removal. */
1207 ttydisc_modem(tp, 0);
1209 /* Wake up all blocked threads. */
1210 tty_wakeup(tp, FREAD|FWRITE);
1211 cv_broadcast(&tp->t_bgwait);
1212 cv_broadcast(&tp->t_dcdwait);
1214 tp->t_flags |= TF_GONE;
1219 tty_drop_ctty(struct tty *tp, struct proc *p)
1221 struct session *session;
1225 * This looks terrible, but it's generally safe as long as the tty
1226 * hasn't gone away while we had the lock dropped. All of our sanity
1227 * checking that this operation is OK happens after we've picked it back
1228 * up, so other state changes are generally not fatal and the potential
1229 * for this particular operation to happen out-of-order in a
1230 * multithreaded scenario is likely a non-issue.
1233 sx_xlock(&proctree_lock);
1236 sx_xunlock(&proctree_lock);
1241 * If the session doesn't have a controlling TTY, or if we weren't
1242 * invoked on the controlling TTY, we'll return ENOIOCTL as we've
1243 * historically done.
1245 session = p->p_session;
1246 if (session->s_ttyp == NULL || session->s_ttyp != tp) {
1247 sx_xunlock(&proctree_lock);
1251 if (!SESS_LEADER(p)) {
1252 sx_xunlock(&proctree_lock);
1258 vp = session->s_ttyvp;
1259 session->s_ttyp = NULL;
1260 session->s_ttyvp = NULL;
1261 session->s_ttydp = NULL;
1262 SESS_UNLOCK(session);
1265 p->p_flag &= ~P_CONTROLT;
1267 sx_xunlock(&proctree_lock);
1270 * If we did have a vnode, release our reference. Ordinarily we manage
1271 * these at the devfs layer, but we can't necessarily know that we were
1272 * invoked on the vnode referenced in the session (i.e. the vnode we
1273 * hold a reference to). We explicitly don't check VBAD/VIRF_DOOMED here
1274 * to avoid a vnode leak -- in circumstances elsewhere where we'd hit a
1275 * VIRF_DOOMED vnode, release has been deferred until the controlling TTY
1276 * is either changed or released.
1279 devfs_ctty_unref(vp);
1284 * Exposing information about current TTY's through sysctl
1288 tty_to_xtty(struct tty *tp, struct xtty *xt)
1291 tty_assert_locked(tp);
1293 xt->xt_size = sizeof(struct xtty);
1294 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1295 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1296 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1297 xt->xt_inlow = tp->t_inlow;
1298 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1299 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1300 xt->xt_outlow = tp->t_outlow;
1301 xt->xt_column = tp->t_column;
1302 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1303 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1304 xt->xt_flags = tp->t_flags;
1305 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : (uint32_t)NODEV;
1309 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1311 unsigned long lsize;
1312 struct xtty *xtlist, *xt;
1316 sx_slock(&tty_list_sx);
1317 lsize = tty_list_count * sizeof(struct xtty);
1319 sx_sunlock(&tty_list_sx);
1323 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1325 TAILQ_FOREACH(tp, &tty_list, t_list) {
1327 tty_to_xtty(tp, xt);
1331 sx_sunlock(&tty_list_sx);
1333 error = SYSCTL_OUT(req, xtlist, lsize);
1334 free(xtlist, M_TTY);
1338 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1339 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1342 * Device node creation. Device has been set up, now we can expose it to
1347 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1348 const char *fmt, ...)
1351 struct make_dev_args args;
1352 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1353 const char *prefix = "tty";
1354 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1360 /* Remove "tty" prefix from devices like PTY's. */
1361 if (tp->t_flags & TF_NOPREFIX)
1365 vsnrprintf(name, sizeof name, 32, fmt, ap);
1369 /* System device. */
1372 mode = S_IRUSR|S_IWUSR;
1375 uid = cred->cr_ruid;
1377 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1380 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1381 flags |= MAKEDEV_CHECKNAME;
1383 /* Master call-in device. */
1384 make_dev_args_init(&args);
1385 args.mda_flags = flags;
1386 args.mda_devsw = &ttydev_cdevsw;
1390 args.mda_mode = mode;
1391 args.mda_si_drv1 = tp;
1392 error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1397 init = lock = cua = cinit = clock = NULL;
1399 /* Slave call-in devices. */
1400 if (tp->t_flags & TF_INITLOCK) {
1401 args.mda_devsw = &ttyil_cdevsw;
1402 args.mda_unit = TTYUNIT_INIT;
1403 args.mda_si_drv1 = tp;
1404 args.mda_si_drv2 = &tp->t_termios_init_in;
1405 error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1408 dev_depends(dev, init);
1410 args.mda_unit = TTYUNIT_LOCK;
1411 args.mda_si_drv2 = &tp->t_termios_lock_in;
1412 error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1415 dev_depends(dev, lock);
1418 /* Call-out devices. */
1419 if (tp->t_flags & TF_CALLOUT) {
1420 make_dev_args_init(&args);
1421 args.mda_flags = flags;
1422 args.mda_devsw = &ttydev_cdevsw;
1424 args.mda_uid = UID_UUCP;
1425 args.mda_gid = GID_DIALER;
1426 args.mda_mode = 0660;
1427 args.mda_unit = TTYUNIT_CALLOUT;
1428 args.mda_si_drv1 = tp;
1429 error = make_dev_s(&args, &cua, "cua%s", name);
1432 dev_depends(dev, cua);
1434 /* Slave call-out devices. */
1435 if (tp->t_flags & TF_INITLOCK) {
1436 args.mda_devsw = &ttyil_cdevsw;
1437 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1438 args.mda_si_drv2 = &tp->t_termios_init_out;
1439 error = make_dev_s(&args, &cinit, "cua%s.init", name);
1442 dev_depends(dev, cinit);
1444 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1445 args.mda_si_drv2 = &tp->t_termios_lock_out;
1446 error = make_dev_s(&args, &clock, "cua%s.lock", name);
1449 dev_depends(dev, clock);
1453 sx_xlock(&tty_list_sx);
1454 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1456 sx_xunlock(&tty_list_sx);
1475 * Signalling processes.
1479 tty_signal_sessleader(struct tty *tp, int sig)
1484 tty_assert_locked(tp);
1485 MPASS(sig >= 1 && sig < NSIG);
1487 /* Make signals start output again. */
1488 tp->t_flags &= ~TF_STOPPED;
1489 tp->t_termios.c_lflag &= ~FLUSHO;
1492 * Load s_leader exactly once to avoid race where s_leader is
1493 * set to NULL by a concurrent invocation of killjobc() by the
1494 * session leader. Note that we are not holding t_session's
1495 * lock for the read.
1497 if ((s = tp->t_session) != NULL &&
1498 (p = atomic_load_ptr(&s->s_leader)) != NULL) {
1500 kern_psignal(p, sig);
1506 tty_signal_pgrp(struct tty *tp, int sig)
1510 tty_assert_locked(tp);
1511 MPASS(sig >= 1 && sig < NSIG);
1513 /* Make signals start output again. */
1514 tp->t_flags &= ~TF_STOPPED;
1515 tp->t_termios.c_lflag &= ~FLUSHO;
1517 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1519 if (tp->t_pgrp != NULL) {
1520 ksiginfo_init(&ksi);
1521 ksi.ksi_signo = sig;
1522 ksi.ksi_code = SI_KERNEL;
1523 PGRP_LOCK(tp->t_pgrp);
1524 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1525 PGRP_UNLOCK(tp->t_pgrp);
1530 tty_wakeup(struct tty *tp, int flags)
1533 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1534 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1536 if (flags & FWRITE) {
1537 cv_broadcast(&tp->t_outwait);
1538 selwakeup(&tp->t_outpoll);
1539 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1541 if (flags & FREAD) {
1542 cv_broadcast(&tp->t_inwait);
1543 selwakeup(&tp->t_inpoll);
1544 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1549 tty_wait(struct tty *tp, struct cv *cv)
1552 int revokecnt = tp->t_revokecnt;
1554 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1555 MPASS(!tty_gone(tp));
1557 error = cv_wait_sig(cv, tp->t_mtx);
1559 /* Bail out when the device slipped away. */
1563 /* Restart the system call when we may have been revoked. */
1564 if (tp->t_revokecnt != revokecnt)
1571 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1574 int revokecnt = tp->t_revokecnt;
1576 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1577 MPASS(!tty_gone(tp));
1579 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1581 /* Bail out when the device slipped away. */
1585 /* Restart the system call when we may have been revoked. */
1586 if (tp->t_revokecnt != revokecnt)
1593 tty_flush(struct tty *tp, int flags)
1596 if (flags & FWRITE) {
1597 tp->t_flags &= ~TF_HIWAT_OUT;
1598 ttyoutq_flush(&tp->t_outq);
1599 tty_wakeup(tp, FWRITE);
1600 if (!tty_gone(tp)) {
1601 ttydevsw_outwakeup(tp);
1602 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1605 if (flags & FREAD) {
1606 tty_hiwat_in_unblock(tp);
1607 ttyinq_flush(&tp->t_inq);
1608 tty_wakeup(tp, FREAD);
1609 if (!tty_gone(tp)) {
1610 ttydevsw_inwakeup(tp);
1611 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1617 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1620 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1622 tp->t_winsize = *wsz;
1623 tty_signal_pgrp(tp, SIGWINCH);
1627 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1635 * The SER_* and TIOCM_* flags are the same, but one bit
1636 * shifted. I don't know why.
1639 ttydevsw_modem(tp, SER_DTR, 0);
1642 ttydevsw_modem(tp, 0, SER_DTR);
1645 int bits = *(int *)data;
1647 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1648 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1652 int bits = *(int *)data;
1653 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1657 int bits = *(int *)data;
1658 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1662 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1667 tp->t_flags |= TF_ASYNC;
1669 tp->t_flags &= ~TF_ASYNC;
1672 /* This device supports non-blocking operation. */
1675 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1679 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1682 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1683 /* Not allowed to set ownership. */
1686 /* Temporarily unlock the TTY to set ownership. */
1688 error = fsetown(*(int *)data, &tp->t_sigio);
1692 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1693 /* Not allowed to set ownership. */
1696 /* Get ownership. */
1697 *(int *)data = fgetown(&tp->t_sigio);
1700 /* Obtain terminal flags through tcgetattr(). */
1701 *(struct termios*)data = tp->t_termios;
1706 struct termios *t = data;
1709 * Who makes up these funny rules? According to POSIX,
1710 * input baud rate is set equal to the output baud rate
1713 if (t->c_ispeed == 0)
1714 t->c_ispeed = t->c_ospeed;
1716 /* Discard any unsupported bits. */
1717 t->c_iflag &= TTYSUP_IFLAG;
1718 t->c_oflag &= TTYSUP_OFLAG;
1719 t->c_lflag &= TTYSUP_LFLAG;
1720 t->c_cflag &= TTYSUP_CFLAG;
1722 /* Set terminal flags through tcsetattr(). */
1723 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1724 error = tty_drain(tp, 0);
1727 if (cmd == TIOCSETAF)
1728 tty_flush(tp, FREAD);
1732 * Only call param() when the flags really change.
1734 if ((t->c_cflag & CIGNORE) == 0 &&
1735 (tp->t_termios.c_cflag != t->c_cflag ||
1736 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1737 (IXON|IXOFF|IXANY)) ||
1738 tp->t_termios.c_ispeed != t->c_ispeed ||
1739 tp->t_termios.c_ospeed != t->c_ospeed)) {
1740 error = ttydevsw_param(tp, t);
1746 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1747 tp->t_termios.c_ispeed = t->c_ispeed;
1748 tp->t_termios.c_ospeed = t->c_ospeed;
1750 /* Baud rate has changed - update watermarks. */
1751 error = tty_watermarks(tp);
1756 /* Copy new non-device driver parameters. */
1757 tp->t_termios.c_iflag = t->c_iflag;
1758 tp->t_termios.c_oflag = t->c_oflag;
1759 tp->t_termios.c_lflag = t->c_lflag;
1760 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1762 ttydisc_optimize(tp);
1764 if ((t->c_lflag & ICANON) == 0) {
1766 * When in non-canonical mode, wake up all
1767 * readers. Canonicalize any partial input. VMIN
1768 * and VTIME could also be adjusted.
1770 ttyinq_canonicalize(&tp->t_inq);
1771 tty_wakeup(tp, FREAD);
1775 * For packet mode: notify the PTY consumer that VSTOP
1776 * and VSTART may have been changed.
1778 if (tp->t_termios.c_iflag & IXON &&
1779 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1780 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1781 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1783 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1787 /* For compatibility - we only support TTYDISC. */
1788 *(int *)data = TTYDISC;
1791 if (!tty_is_ctty(tp, td->td_proc))
1794 if (tp->t_pgrp != NULL)
1795 *(int *)data = tp->t_pgrp->pg_id;
1797 *(int *)data = NO_PID;
1800 if (!tty_is_ctty(tp, td->td_proc))
1803 MPASS(tp->t_session);
1804 *(int *)data = tp->t_session->s_sid;
1807 return (tty_drop_ctty(tp, td->td_proc));
1809 struct proc *p = td->td_proc;
1811 /* XXX: This looks awful. */
1813 sx_xlock(&proctree_lock);
1816 if (!SESS_LEADER(p)) {
1817 /* Only the session leader may do this. */
1818 sx_xunlock(&proctree_lock);
1822 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1823 /* This is already our controlling TTY. */
1824 sx_xunlock(&proctree_lock);
1828 if (p->p_session->s_ttyp != NULL ||
1829 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1830 tp->t_session->s_ttyvp->v_type != VBAD)) {
1832 * There is already a relation between a TTY and
1833 * a session, or the caller is not the session
1836 * Allow the TTY to be stolen when the vnode is
1837 * invalid, but the reference to the TTY is
1838 * still active. This allows immediate reuse of
1839 * TTYs of which the session leader has been
1840 * killed or the TTY revoked.
1842 sx_xunlock(&proctree_lock);
1846 /* Connect the session to the TTY. */
1847 tp->t_session = p->p_session;
1848 tp->t_session->s_ttyp = tp;
1851 /* Assign foreground process group. */
1852 tp->t_pgrp = p->p_pgrp;
1854 p->p_flag |= P_CONTROLT;
1857 sx_xunlock(&proctree_lock);
1864 * XXX: Temporarily unlock the TTY to locate the process
1865 * group. This code would be lot nicer if we would ever
1866 * decompose proctree_lock.
1869 sx_slock(&proctree_lock);
1870 pg = pgfind(*(int *)data);
1873 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1874 sx_sunlock(&proctree_lock);
1881 * Determine if this TTY is the controlling TTY after
1882 * relocking the TTY.
1884 if (!tty_is_ctty(tp, td->td_proc)) {
1885 sx_sunlock(&proctree_lock);
1889 sx_sunlock(&proctree_lock);
1891 /* Wake up the background process groups. */
1892 cv_broadcast(&tp->t_bgwait);
1896 int flags = *(int *)data;
1899 flags = (FREAD|FWRITE);
1901 flags &= (FREAD|FWRITE);
1902 tty_flush(tp, flags);
1906 /* Drain TTY output. */
1907 return tty_drain(tp, 0);
1908 case TIOCGDRAINWAIT:
1909 *(int *)data = tp->t_drainwait;
1911 case TIOCSDRAINWAIT:
1912 error = priv_check(td, PRIV_TTY_DRAINWAIT);
1914 tp->t_drainwait = *(int *)data;
1917 /* Set terminal as console TTY. */
1919 error = priv_check(td, PRIV_TTY_CONSOLE);
1922 error = constty_set(tp);
1924 error = constty_clear(tp);
1928 /* Obtain window size. */
1929 *(struct winsize*)data = tp->t_winsize;
1932 /* Set window size. */
1933 tty_set_winsize(tp, data);
1936 tp->t_flags |= TF_EXCLUDE;
1939 tp->t_flags &= ~TF_EXCLUDE;
1942 tp->t_flags |= TF_STOPPED;
1943 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1946 tp->t_flags &= ~TF_STOPPED;
1947 tp->t_termios.c_lflag &= ~FLUSHO;
1948 ttydevsw_outwakeup(tp);
1949 ttydevsw_pktnotify(tp, TIOCPKT_START);
1955 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1957 if (!tty_is_ctty(tp, td->td_proc) &&
1958 priv_check(td, PRIV_TTY_STI))
1960 ttydisc_rint(tp, *(char *)data, 0);
1961 ttydisc_rint_done(tp);
1966 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1967 #else /* !COMPAT_43TTY */
1969 #endif /* COMPAT_43TTY */
1973 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1977 tty_assert_locked(tp);
1982 error = ttydevsw_ioctl(tp, cmd, data, td);
1983 if (error == ENOIOCTL)
1984 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1990 tty_udev(struct tty *tp)
1994 return (dev2udev(tp->t_dev));
2000 tty_checkoutq(struct tty *tp)
2003 /* 256 bytes should be enough to print a log message. */
2004 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
2008 tty_hiwat_in_block(struct tty *tp)
2011 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
2012 tp->t_termios.c_iflag & IXOFF &&
2013 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
2015 * Input flow control. Only enter the high watermark when we
2016 * can successfully store the VSTOP character.
2018 if (ttyoutq_write_nofrag(&tp->t_outq,
2019 &tp->t_termios.c_cc[VSTOP], 1) == 0)
2020 tp->t_flags |= TF_HIWAT_IN;
2022 /* No input flow control. */
2023 tp->t_flags |= TF_HIWAT_IN;
2028 tty_hiwat_in_unblock(struct tty *tp)
2031 if (tp->t_flags & TF_HIWAT_IN &&
2032 tp->t_termios.c_iflag & IXOFF &&
2033 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
2035 * Input flow control. Only leave the high watermark when we
2036 * can successfully store the VSTART character.
2038 if (ttyoutq_write_nofrag(&tp->t_outq,
2039 &tp->t_termios.c_cc[VSTART], 1) == 0)
2040 tp->t_flags &= ~TF_HIWAT_IN;
2042 /* No input flow control. */
2043 tp->t_flags &= ~TF_HIWAT_IN;
2047 ttydevsw_inwakeup(tp);
2051 * TTY hooks interface.
2055 ttyhook_defrint(struct tty *tp, char c, int flags)
2058 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
2065 ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
2072 struct filedesc *fdp;
2073 cap_rights_t rights;
2076 /* Validate the file descriptor. */
2078 error = fget_unlocked(fdp, fd, cap_rights_init_one(&rights, CAP_TTYHOOK),
2082 if (fp->f_ops == &badfileops) {
2088 * Make sure the vnode is bound to a character device.
2089 * Unlocked check for the vnode type is ok there, because we
2090 * only shall prevent calling devvn_refthread on the file that
2091 * never has been opened over a character device.
2093 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
2098 /* Make sure it is a TTY. */
2099 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
2104 if (dev != fp->f_data) {
2108 if (cdp != &ttydev_cdevsw) {
2114 /* Try to attach the hook to the TTY. */
2117 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
2118 if (tp->t_flags & TF_HOOK)
2121 tp->t_flags |= TF_HOOK;
2123 tp->t_hooksoftc = softc;
2127 /* Maybe we can switch into bypass mode now. */
2128 ttydisc_optimize(tp);
2130 /* Silently convert rint() calls to rint_bypass() when possible. */
2131 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2132 th->th_rint = ttyhook_defrint;
2134 done3: tty_unlock(tp);
2135 done2: dev_relthread(dev, ref);
2136 done1: fdrop(fp, curthread);
2141 ttyhook_unregister(struct tty *tp)
2144 tty_assert_locked(tp);
2145 MPASS(tp->t_flags & TF_HOOK);
2147 /* Disconnect the hook. */
2148 tp->t_flags &= ~TF_HOOK;
2151 /* Maybe we need to leave bypass mode. */
2152 ttydisc_optimize(tp);
2154 /* Maybe deallocate the TTY as well. */
2159 * /dev/console handling.
2163 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2167 /* System has no console device. */
2168 if (dev_console_filename == NULL)
2171 /* Look up corresponding TTY by device name. */
2172 sx_slock(&tty_list_sx);
2173 TAILQ_FOREACH(tp, &tty_list, t_list) {
2174 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2175 dev_console->si_drv1 = tp;
2179 sx_sunlock(&tty_list_sx);
2181 /* System console has no TTY associated. */
2182 if (dev_console->si_drv1 == NULL)
2185 return (ttydev_open(dev, oflags, devtype, td));
2189 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2194 return (ttydev_write(dev, uio, ioflag));
2198 * /dev/console is a little different than normal TTY's. When opened,
2199 * it determines which TTY to use. When data gets written to it, it
2200 * will be logged in the kernel message buffer.
2202 static struct cdevsw ttyconsdev_cdevsw = {
2203 .d_version = D_VERSION,
2204 .d_open = ttyconsdev_open,
2205 .d_close = ttydev_close,
2206 .d_read = ttydev_read,
2207 .d_write = ttyconsdev_write,
2208 .d_ioctl = ttydev_ioctl,
2209 .d_kqfilter = ttydev_kqfilter,
2210 .d_poll = ttydev_poll,
2211 .d_mmap = ttydev_mmap,
2212 .d_name = "ttyconsdev",
2217 ttyconsdev_init(void *unused __unused)
2220 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2221 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2224 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2227 ttyconsdev_select(const char *name)
2230 dev_console_filename = name;
2234 * Debugging routines.
2237 #include "opt_ddb.h"
2239 #include <ddb/ddb.h>
2240 #include <ddb/db_sym.h>
2242 static const struct {
2247 { TF_NOPREFIX, 'N' },
2249 { TF_INITLOCK, 'I' },
2250 { TF_CALLOUT, 'C' },
2252 /* Keep these together -> 'Oi' and 'Oo'. */
2254 { TF_OPENED_IN, 'i' },
2255 { TF_OPENED_OUT, 'o' },
2256 { TF_OPENED_CONS, 'c' },
2259 { TF_OPENCLOSE, 'B' },
2261 { TF_LITERAL, 'L' },
2263 /* Keep these together -> 'Hi' and 'Ho'. */
2265 { TF_HIWAT_IN, 'i' },
2266 { TF_HIWAT_OUT, 'o' },
2268 { TF_STOPPED, 'S' },
2269 { TF_EXCLUDE, 'X' },
2274 /* Keep these together -> 'bi' and 'bo'. */
2276 { TF_BUSY_IN, 'i' },
2277 { TF_BUSY_OUT, 'o' },
2282 #define TTY_FLAG_BITS \
2283 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2284 "\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2285 "\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2286 "\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2287 "\21HOOK\22BUSY_IN\23BUSY_OUT"
2289 #define DB_PRINTSYM(name, addr) \
2290 db_printf("%s " #name ": ", sep); \
2291 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2295 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2298 db_printf("%sdevsw: ", sep);
2299 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2300 db_printf(" (%p)\n", tsw);
2301 DB_PRINTSYM(open, tsw->tsw_open);
2302 DB_PRINTSYM(close, tsw->tsw_close);
2303 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2304 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2305 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2306 DB_PRINTSYM(param, tsw->tsw_param);
2307 DB_PRINTSYM(modem, tsw->tsw_modem);
2308 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2309 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2310 DB_PRINTSYM(free, tsw->tsw_free);
2314 _db_show_hooks(const char *sep, const struct ttyhook *th)
2317 db_printf("%shook: ", sep);
2318 db_printsym((db_addr_t)th, DB_STGY_ANY);
2319 db_printf(" (%p)\n", th);
2322 DB_PRINTSYM(rint, th->th_rint);
2323 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2324 DB_PRINTSYM(rint_done, th->th_rint_done);
2325 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2326 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2327 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2328 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2329 DB_PRINTSYM(close, th->th_close);
2333 _db_show_termios(const char *name, const struct termios *t)
2336 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2337 "lflag 0x%x ispeed %u ospeed %u\n", name,
2338 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2339 t->c_ispeed, t->c_ospeed);
2342 /* DDB command to show TTY statistics. */
2343 DB_SHOW_COMMAND(tty, db_show_tty)
2348 db_printf("usage: show tty <addr>\n");
2351 tp = (struct tty *)addr;
2353 db_printf("%p: %s\n", tp, tty_devname(tp));
2354 db_printf("\tmtx: %p\n", tp->t_mtx);
2355 db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2356 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2358 /* Buffering mechanisms. */
2359 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2360 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2361 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2362 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2363 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2364 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2365 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2366 db_printf("\tinlow: %zu\n", tp->t_inlow);
2367 db_printf("\toutlow: %zu\n", tp->t_outlow);
2368 _db_show_termios("\ttermios", &tp->t_termios);
2369 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2370 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2371 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2372 db_printf("\tcolumn: %u\n", tp->t_column);
2373 db_printf("\twritepos: %u\n", tp->t_writepos);
2374 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2376 /* Init/lock-state devices. */
2377 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2378 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2379 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2380 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2383 _db_show_devsw("\t", tp->t_devsw);
2384 _db_show_hooks("\t", tp->t_hook);
2387 db_printf("\tpgrp: %p gid %d\n", tp->t_pgrp,
2388 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2389 db_printf("\tsession: %p", tp->t_session);
2390 if (tp->t_session != NULL)
2391 db_printf(" count %u leader %p tty %p sid %d login %s",
2392 tp->t_session->s_count, tp->t_session->s_leader,
2393 tp->t_session->s_ttyp, tp->t_session->s_sid,
2394 tp->t_session->s_login);
2396 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2397 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2398 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2399 db_printf("\tdev: %p\n", tp->t_dev);
2402 /* DDB command to list TTYs. */
2403 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2409 /* Make the output look like `pstat -t'. */
2411 #if defined(__LP64__)
2414 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2415 "COL SESS PGID STATE\n");
2417 TAILQ_FOREACH(tp, &tty_list, t_list) {
2418 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2419 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2421 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2422 "%5d ", tp, tty_devname(tp), isiz,
2423 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2424 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2425 isiz - tp->t_inlow, osiz,
2426 tp->t_outq.to_end - tp->t_outq.to_begin,
2427 osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2428 tp->t_session ? tp->t_session->s_sid : 0,
2429 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2432 for (i = j = 0; ttystates[i].flag; i++)
2433 if (tp->t_flags & ttystates[i].flag) {
2434 db_printf("%c", ttystates[i].val);