30 #include <sys/cdefs.h>
33 #include "opt_capsicum.h"
34 #include "opt_compat.h"
36 #include <sys/param.h>
37 #include <sys/capability.h>
40 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
45 #include <sys/ioctl_compat.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
54 #include <sys/serial.h>
55 #include <sys/signal.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
61 #include <sys/ttycom.h>
63 #include <sys/ttydefaults.h>
65 #include <sys/ucred.h>
66 #include <sys/vnode.h>
68 #include <machine/stdarg.h>
74 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
75 static struct sx tty_list_sx;
76 SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
77 static
unsigned int tty_list_count = 0;
80 static struct cdev *dev_console;
81 static const
char *dev_console_filename;
86 #define TTYSUP_IFLAG (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\
87 INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL)
88 #define TTYSUP_OFLAG (OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET)
89 #define TTYSUP_LFLAG (ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\
90 ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\
91 FLUSHO|NOKERNINFO|NOFLSH)
92 #define TTYSUP_CFLAG (CIGNORE|CSIZE|CSTOPB|CREAD|PARENB|PARODD|\
93 HUPCL|CLOCAL|CCTS_OFLOW|CRTS_IFLOW|CDTR_IFLOW|\
94 CDSR_OFLOW|CCAR_OFLOW)
96 #define TTY_CALLOUT(tp,d) (dev2unit(d) & TTYUNIT_CALLOUT)
102 #define TTYBUF_MAX 65536
105 tty_watermarks(
struct tty *tp)
110 if (tp->t_termios.c_cflag & CREAD)
111 bs = MIN(tp->t_termios.c_ispeed / 5,
TTYBUF_MAX);
115 tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
118 bs = MIN(tp->t_termios.c_ospeed / 5,
TTYBUF_MAX);
122 tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
130 if (ttyhook_hashook(tp, getc_inject))
134 while (ttyoutq_bytesused(&tp->t_outq) > 0) {
135 ttydevsw_outwakeup(tp);
137 if (ttyoutq_bytesused(&tp->t_outq) == 0)
141 error =
tty_wait(tp, &tp->t_outwait);
163 if (tty_gone(tp) || !tty_opened(tp)) {
175 tty_lock_assert(tp, MA_OWNED);
177 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
183 tp->t_flags |= TF_OPENCLOSE;
193 MPASS((tp->t_flags & TF_STOPPED) == 0);
205 knlist_clear(&tp->t_inpoll.si_note, 1);
206 knlist_clear(&tp->t_outpoll.si_note, 1);
211 tp->t_flags &= ~TF_OPENCLOSE;
212 cv_broadcast(&tp->t_dcdwait);
220 ttydev_open(
struct cdev *dev,
int oflags,
int devtype,
struct thread *td)
225 while ((tp = dev->si_drv1) == NULL) {
226 error = tsleep(&dev->si_drv1, PCATCH,
"ttdrv1", 1);
227 if (error != EWOULDBLOCK)
242 while (tp->t_flags & TF_OPENCLOSE) {
243 error =
tty_wait(tp, &tp->t_dcdwait);
249 tp->t_flags |= TF_OPENCLOSE;
256 if (tp->t_flags & TF_OPENED_IN) {
261 if (tp->t_flags & TF_OPENED_OUT) {
267 if (tp->t_flags & TF_EXCLUDE &&
priv_check(td, PRIV_TTY_EXCLUSIVE)) {
272 if (!tty_opened(tp)) {
275 tp->t_termios = tp->t_termios_init_out;
277 tp->t_termios = tp->t_termios_init_in;
278 ttydevsw_param(tp, &tp->t_termios);
281 tp->t_termios.c_cflag |= CLOCAL;
283 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
285 error = ttydevsw_open(tp);
294 if ((oflags & O_NONBLOCK) == 0 &&
295 (tp->t_termios.c_cflag & CLOCAL) == 0) {
296 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
297 error =
tty_wait(tp, &tp->t_dcdwait);
303 if (dev == dev_console)
304 tp->t_flags |= TF_OPENED_CONS;
306 tp->t_flags |= TF_OPENED_OUT;
308 tp->t_flags |= TF_OPENED_IN;
310 done: tp->t_flags &= ~TF_OPENCLOSE;
311 cv_broadcast(&tp->t_dcdwait);
318 ttydev_close(
struct cdev *dev,
int fflag,
int devtype,
struct thread *td)
320 struct tty *tp = dev->si_drv1;
328 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED);
329 if (dev == dev_console)
330 tp->t_flags &= ~TF_OPENED_CONS;
332 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
334 if (tp->t_flags & TF_OPENED) {
343 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
348 cv_broadcast(&tp->t_bgwait);
349 cv_broadcast(&tp->t_dcdwait);
359 tty_lock_assert(tp, MA_OWNED);
361 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
367 struct proc *p = td->td_proc;
372 MPASS(sig == SIGTTIN || sig == SIGTTOU);
373 tty_lock_assert(tp, MA_OWNED);
386 if (!
tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
392 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
393 SIGISMEMBER(td->td_sigmask, sig)) {
396 return (sig == SIGTTOU ? 0 : EIO);
400 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
413 ksi.ksi_code = SI_KERNEL;
418 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
421 error =
tty_wait(tp, &tp->t_bgwait);
430 struct tty *tp = dev->si_drv1;
443 done:
if (error == ENXIO)
451 struct tty *tp = dev->si_drv1;
458 if (tp->t_termios.c_lflag & TOSTOP) {
464 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
469 while (tp->t_flags & TF_BUSY_OUT) {
470 error =
tty_wait(tp, &tp->t_outserwait);
475 tp->t_flags |= TF_BUSY_OUT;
477 tp->t_flags &= ~TF_BUSY_OUT;
481 done: tty_unlock(tp);
489 struct tty *tp = dev->si_drv1;
537 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
538 struct termios *old = &tp->t_termios;
539 struct termios *
new = (
struct termios *)data;
541 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
548 new->c_iflag = (old->c_iflag & lock->c_iflag) |
549 (new->c_iflag & ~lock->c_iflag);
550 new->c_oflag = (old->c_oflag & lock->c_oflag) |
551 (new->c_oflag & ~lock->c_oflag);
552 new->c_cflag = (old->c_cflag & lock->c_cflag) |
553 (new->c_cflag & ~lock->c_cflag);
554 new->c_lflag = (old->c_lflag & lock->c_lflag) |
555 (new->c_lflag & ~lock->c_lflag);
556 for (cc = 0; cc < NCCS; ++cc)
558 new->c_cc[cc] = old->c_cc[cc];
560 new->c_ispeed = old->c_ispeed;
562 new->c_ospeed = old->c_ospeed;
565 error =
tty_ioctl(tp, cmd, data, fflag, td);
566 done: tty_unlock(tp);
574 struct tty *tp = dev->si_drv1;
575 int error, revents = 0;
579 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
581 if (events & (POLLIN|POLLRDNORM)) {
583 if (ttydisc_read_poll(tp) > 0)
584 revents |= events & (POLLIN|POLLRDNORM);
587 if (tp->t_flags & TF_ZOMBIE) {
590 }
else if (events & (POLLOUT|POLLWRNORM)) {
592 if (ttydisc_write_poll(tp) > 0)
593 revents |= events & (POLLOUT|POLLWRNORM);
597 if (events & (POLLIN|POLLRDNORM))
599 if (events & (POLLOUT|POLLWRNORM))
609 ttydev_mmap(
struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
610 int nprot, vm_memattr_t *memattr)
612 struct tty *tp = dev->si_drv1;
620 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
633 struct tty *tp = kn->kn_hook;
641 struct tty *tp = kn->kn_hook;
643 tty_lock_assert(tp, MA_OWNED);
645 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
646 kn->kn_flags |= EV_EOF;
649 kn->kn_data = ttydisc_read_poll(tp);
650 return (kn->kn_data > 0);
657 struct tty *tp = kn->kn_hook;
665 struct tty *tp = kn->kn_hook;
667 tty_lock_assert(tp, MA_OWNED);
670 kn->kn_flags |= EV_EOF;
673 kn->kn_data = ttydisc_write_poll(tp);
674 return (kn->kn_data > 0);
692 struct tty *tp = dev->si_drv1;
699 switch (kn->kn_filter) {
720 .d_version = D_VERSION,
738 ttyil_open(
struct cdev *dev,
int oflags,
int devtype,
struct thread *td)
743 while ((tp = dev->si_drv1) == NULL) {
744 error = tsleep(&dev->si_drv1, PCATCH,
"ttdrv1", 1);
745 if (error != EWOULDBLOCK)
769 ttyil_ioctl(
struct cdev *dev, u_long cmd, caddr_t data,
int fflag,
772 struct tty *tp = dev->si_drv1;
781 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
782 if (error != ENOIOCTL)
789 *(
struct termios*)data = *(
struct termios*)dev->si_drv2;
796 *(
struct termios*)dev->si_drv2 = *(
struct termios*)data;
799 *(
int *)data = TTYDISC;
802 bzero(data,
sizeof(
struct winsize));
808 done: tty_unlock(tp);
813 .d_version = D_VERSION,
826 struct termios *t = &tp->t_termios_init_in;
828 t->c_cflag = TTYDEF_CFLAG;
829 t->c_iflag = TTYDEF_IFLAG;
830 t->c_lflag = TTYDEF_LFLAG;
831 t->c_oflag = TTYDEF_OFLAG;
832 t->c_ispeed = TTYDEF_SPEED;
833 t->c_ospeed = TTYDEF_SPEED;
834 memcpy(&t->c_cc, ttydefchars,
sizeof ttydefchars);
836 tp->t_termios_init_out = *t;
842 struct termios *ti = &tp->t_termios_init_in;
843 struct termios *to = &tp->t_termios_init_out;
846 ti->c_ispeed = ti->c_ospeed = s;
847 to->c_ispeed = to->c_ospeed = s;
850 ti->c_cflag |= CLOCAL;
851 to->c_cflag |= CLOCAL;
876 panic(
"Terminal device has output, while not implemented");
907 if (t->c_ispeed < B50)
909 else if (t->c_ispeed > B115200)
910 t->c_ispeed = B115200;
911 if (t->c_ospeed < B50)
913 else if (t->c_ospeed > B115200)
914 t->c_ospeed = B115200;
930 int nprot, vm_memattr_t *memattr)
945 panic(
"Terminal device freed without a free-handler");
967 #define PATCH_FUNC(x) do { \
968 if (tsw->tsw_ ## x == NULL) \
969 tsw->tsw_ ## x = ttydevsw_def ## x; \
984 tp =
malloc(
sizeof(
struct tty), M_TTY, M_WAITOK|M_ZERO);
986 tp->t_devswsoftc = sc;
987 tp->t_flags = tsw->tsw_flags;
991 cv_init(&tp->t_inwait,
"ttyin");
992 cv_init(&tp->t_outwait,
"ttyout");
993 cv_init(&tp->t_outserwait,
"ttyosr");
994 cv_init(&tp->t_bgwait,
"ttybg");
995 cv_init(&tp->t_dcdwait,
"ttydcd");
1001 tp->t_mtx = &tp->t_mtxobj;
1002 mtx_init(&tp->t_mtxobj,
"ttymtx", NULL, MTX_DEF);
1008 sx_xlock(&tty_list_sx);
1009 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1011 sx_xunlock(&tty_list_sx);
1019 struct tty *tp = arg;
1021 sx_xlock(&tty_list_sx);
1022 TAILQ_REMOVE(&tty_list, tp, t_list);
1024 sx_xunlock(&tty_list_sx);
1027 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
1028 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
1041 if (tp->t_mtx == &tp->t_mtxobj)
1052 tty_lock_assert(tp, MA_OWNED);
1054 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1055 if (tp->t_sessioncnt != 0 || (tp->t_flags &
TF_ACTIVITY) != TF_GONE) {
1073 MPASS(tp->t_sessioncnt > 0);
1074 tty_lock_assert(tp, MA_OWNED);
1076 if (tp->t_pgrp == pg)
1085 MPASS(tp->t_sessioncnt > 0);
1088 if (tp->t_session == sess) {
1089 tp->t_session = NULL;
1090 MPASS(tp->t_pgrp == NULL);
1099 MPASS(!tty_gone(tp));
1106 cv_broadcast(&tp->t_bgwait);
1107 cv_broadcast(&tp->t_dcdwait);
1109 tp->t_flags |= TF_GONE;
1120 tty_lock_assert(tp, MA_OWNED);
1122 xt->xt_size =
sizeof(
struct xtty);
1123 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1124 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1125 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1126 xt->xt_inlow = tp->t_inlow;
1127 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1128 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1129 xt->xt_outlow = tp->t_outlow;
1130 xt->xt_column = tp->t_column;
1131 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1132 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1133 xt->xt_flags = tp->t_flags;
1134 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1140 unsigned long lsize;
1141 struct xtty *xtlist, *xt;
1145 sx_slock(&tty_list_sx);
1146 lsize = tty_list_count *
sizeof(
struct xtty);
1148 sx_sunlock(&tty_list_sx);
1152 xtlist = xt =
malloc(lsize, M_TTY, M_WAITOK);
1154 TAILQ_FOREACH(tp, &tty_list, t_list) {
1160 sx_sunlock(&tty_list_sx);
1162 error = SYSCTL_OUT(req, xtlist, lsize);
1163 free(xtlist, M_TTY);
1167 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1180 const char *prefix =
"tty";
1181 char name[SPECNAMELEN - 3];
1187 if (tp->t_flags & TF_NOPREFIX)
1198 mode = S_IRUSR|S_IWUSR;
1201 uid = cred->cr_ruid;
1203 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1208 uid, gid, mode,
"%s%s", prefix, name);
1214 if (tp->t_flags & TF_INITLOCK) {
1216 uid, gid, mode,
"%s%s.init", prefix, name);
1220 dev->si_drv2 = &tp->t_termios_init_in;
1223 uid, gid, mode,
"%s%s.lock", prefix, name);
1227 dev->si_drv2 = &tp->t_termios_lock_in;
1231 if (tp->t_flags & TF_CALLOUT) {
1233 UID_UUCP, GID_DIALER, 0660,
"cua%s", name);
1239 if (tp->t_flags & TF_INITLOCK) {
1241 TTYUNIT_CALLOUT | TTYUNIT_INIT, cred,
1242 UID_UUCP, GID_DIALER, 0660,
"cua%s.init", name);
1246 dev->si_drv2 = &tp->t_termios_init_out;
1249 TTYUNIT_CALLOUT | TTYUNIT_LOCK, cred,
1250 UID_UUCP, GID_DIALER, 0660,
"cua%s.lock", name);
1254 dev->si_drv2 = &tp->t_termios_lock_out;
1268 tty_lock_assert(tp, MA_OWNED);
1269 MPASS(sig >= 1 && sig < NSIG);
1272 tp->t_flags &= ~TF_STOPPED;
1274 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1275 p = tp->t_session->s_leader;
1287 tty_lock_assert(tp, MA_OWNED);
1288 MPASS(sig >= 1 && sig < NSIG);
1291 tp->t_flags &= ~TF_STOPPED;
1293 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1295 if (tp->t_pgrp != NULL) {
1296 ksiginfo_init(&ksi);
1297 ksi.ksi_signo = sig;
1298 ksi.ksi_code = SI_KERNEL;
1299 PGRP_LOCK(tp->t_pgrp);
1300 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1301 PGRP_UNLOCK(tp->t_pgrp);
1308 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1309 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1311 if (flags & FWRITE) {
1312 cv_broadcast(&tp->t_outwait);
1314 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1316 if (flags & FREAD) {
1317 cv_broadcast(&tp->t_inwait);
1319 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1327 int revokecnt = tp->t_revokecnt;
1329 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1330 MPASS(!tty_gone(tp));
1332 error = cv_wait_sig(cv, tp->t_mtx);
1335 if (tp->t_revokecnt != revokecnt)
1349 int revokecnt = tp->t_revokecnt;
1351 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1352 MPASS(!tty_gone(tp));
1354 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1357 if (tp->t_revokecnt != revokecnt)
1370 if (flags & FWRITE) {
1371 tp->t_flags &= ~TF_HIWAT_OUT;
1374 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1376 if (flags & FREAD) {
1379 ttydevsw_inwakeup(tp);
1380 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1388 if (memcmp(&tp->t_winsize, wsz,
sizeof(*wsz)) == 0)
1390 tp->t_winsize = *wsz;
1407 ttydevsw_modem(tp, SER_DTR, 0);
1410 ttydevsw_modem(tp, 0, SER_DTR);
1413 int bits = *(
int *)data;
1415 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1416 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1420 int bits = *(
int *)data;
1421 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1425 int bits = *(
int *)data;
1426 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1430 *(
int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1435 tp->t_flags |= TF_ASYNC;
1437 tp->t_flags &= ~TF_ASYNC;
1443 *(
int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1447 *(
int *)data = ttyoutq_bytesused(&tp->t_outq);
1450 if (tp->t_session != NULL && !
tty_is_ctty(tp, td->td_proc))
1456 error =
fsetown(*(
int *)data, &tp->t_sigio);
1460 if (tp->t_session != NULL && !
tty_is_ctty(tp, td->td_proc))
1465 *(
int *)data =
fgetown(&tp->t_sigio);
1469 *(
struct termios*)data = tp->t_termios;
1474 struct termios *t = data;
1481 if (t->c_ispeed == 0)
1482 t->c_ispeed = t->c_ospeed;
1491 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1495 if (cmd == TIOCSETAF)
1502 if ((t->c_cflag & CIGNORE) == 0 &&
1503 (tp->t_termios.c_cflag != t->c_cflag ||
1504 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1505 (IXON|IXOFF|IXANY)) ||
1506 tp->t_termios.c_ispeed != t->c_ispeed ||
1507 tp->t_termios.c_ospeed != t->c_ospeed)) {
1508 error = ttydevsw_param(tp, t);
1514 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1515 tp->t_termios.c_ispeed = t->c_ispeed;
1516 tp->t_termios.c_ospeed = t->c_ospeed;
1523 tp->t_termios.c_iflag = t->c_iflag;
1524 tp->t_termios.c_oflag = t->c_oflag;
1525 tp->t_termios.c_lflag = t->c_lflag;
1526 memcpy(&tp->t_termios.c_cc, t->c_cc,
sizeof t->c_cc);
1530 if ((t->c_lflag & ICANON) == 0) {
1544 if (tp->t_termios.c_iflag & IXON &&
1545 tp->t_termios.c_cc[VSTOP] == CTRL(
'S') &&
1546 tp->t_termios.c_cc[VSTART] == CTRL(
'Q'))
1547 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1549 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1554 *(
int *)data = TTYDISC;
1560 if (tp->t_pgrp != NULL)
1561 *(
int *)data = tp->t_pgrp->pg_id;
1563 *(
int *)data = NO_PID;
1569 MPASS(tp->t_session);
1570 *(
int *)data = tp->t_session->s_sid;
1573 struct proc *p = td->td_proc;
1580 if (!SESS_LEADER(p)) {
1586 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1592 if (p->p_session->s_ttyp != NULL ||
1593 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1594 tp->t_session->s_ttyvp->v_type != VBAD)) {
1611 tp->t_session = p->p_session;
1612 tp->t_session->s_ttyp = tp;
1617 tp->t_pgrp = p->p_pgrp;
1619 p->p_flag |= P_CONTROLT;
1634 pg =
pgfind(*(
int *)data);
1637 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1656 cv_broadcast(&tp->t_bgwait);
1660 int flags = *(
int *)data;
1663 flags = (FREAD|FWRITE);
1665 flags &= (FREAD|FWRITE);
1686 if (constty != NULL)
1692 }
else if (constty == tp) {
1698 *(
struct winsize*)data = tp->t_winsize;
1705 tp->t_flags |= TF_EXCLUDE;
1708 tp->t_flags &= ~TF_EXCLUDE;
1711 tp->t_flags |= TF_STOPPED;
1712 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1715 tp->t_flags &= ~TF_STOPPED;
1716 ttydevsw_outwakeup(tp);
1717 ttydevsw_pktnotify(tp, TIOCPKT_START);
1723 if ((fflag & FREAD) == 0 &&
priv_check(td, PRIV_TTY_STI))
1741 tty_ioctl(
struct tty *tp, u_long cmd,
void *data,
int fflag,
struct thread *td)
1745 tty_lock_assert(tp, MA_OWNED);
1750 error = ttydevsw_ioctl(tp, cmd, data, td);
1751 if (error == ENOIOCTL)
1761 return dev2udev(tp->t_dev);
1771 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1778 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1779 tp->t_termios.c_iflag & IXOFF &&
1780 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1786 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1787 tp->t_flags |= TF_HIWAT_IN;
1790 tp->t_flags |= TF_HIWAT_IN;
1798 if (tp->t_flags & TF_HIWAT_IN &&
1799 tp->t_termios.c_iflag & IXOFF &&
1800 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1806 &tp->t_termios.c_cc[VSTART], 1) == 0)
1807 tp->t_flags &= ~TF_HIWAT_IN;
1810 tp->t_flags &= ~TF_HIWAT_IN;
1814 ttydevsw_inwakeup(tp);
1825 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1833 struct ttyhook *th,
void *softc)
1838 struct file *fp_cap;
1842 struct filedesc *fdp;
1846 if ((fdp = p->p_fd) == NULL)
1870 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1881 if (dev != fp->f_data) {
1885 if (cdp != &ttydev_cdevsw) {
1894 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1895 if (tp->t_flags & TF_HOOK)
1898 tp->t_flags |= TF_HOOK;
1900 tp->t_hooksoftc = softc;
1908 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1911 done3: tty_unlock(tp);
1913 done1: fdrop(fp, curthread);
1921 tty_lock_assert(tp, MA_OWNED);
1922 MPASS(tp->t_flags & TF_HOOK);
1925 tp->t_flags &= ~TF_HOOK;
1945 if (dev_console_filename == NULL)
1949 sx_slock(&tty_list_sx);
1950 TAILQ_FOREACH(tp, &tty_list, t_list) {
1951 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
1952 dev_console->si_drv1 = tp;
1956 sx_sunlock(&tty_list_sx);
1959 if (dev_console->si_drv1 == NULL)
1980 .d_version = D_VERSION,
1989 .d_name =
"ttyconsdev",
1997 dev_console =
make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
1998 NULL, UID_ROOT, GID_WHEEL, 0600,
"console");
2007 dev_console_filename =
name;
2014 #include "opt_ddb.h"
2016 #include <ddb/ddb.h>
2017 #include <ddb/db_sym.h>
2024 { TF_NOPREFIX,
'N' },
2026 { TF_INITLOCK,
'I' },
2027 { TF_CALLOUT,
'C' },
2031 { TF_OPENED_IN,
'i' },
2032 { TF_OPENED_OUT,
'o' },
2033 { TF_OPENED_CONS,
'c' },
2036 { TF_OPENCLOSE,
'B' },
2038 { TF_LITERAL,
'L' },
2042 { TF_HIWAT_IN,
'i' },
2043 { TF_HIWAT_OUT,
'o' },
2045 { TF_STOPPED,
'S' },
2046 { TF_EXCLUDE,
'X' },
2053 { TF_BUSY_IN,
'i' },
2054 { TF_BUSY_OUT,
'o' },
2059 #define TTY_FLAG_BITS \
2060 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
2061 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
2062 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
2064 #define DB_PRINTSYM(name, addr) \
2065 db_printf("%s " #name ": ", sep); \
2066 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2070 _db_show_devsw(
const char *sep,
const struct ttydevsw *tsw)
2072 db_printf(
"%sdevsw: ", sep);
2073 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2074 db_printf(
" (%p)\n", tsw);
2075 DB_PRINTSYM(open, tsw->tsw_open);
2076 DB_PRINTSYM(close, tsw->tsw_close);
2077 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2078 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2079 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2080 DB_PRINTSYM(param, tsw->tsw_param);
2081 DB_PRINTSYM(modem, tsw->tsw_modem);
2082 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2083 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2084 DB_PRINTSYM(
free, tsw->tsw_free);
2087 _db_show_hooks(
const char *sep,
const struct ttyhook *th)
2089 db_printf(
"%shook: ", sep);
2090 db_printsym((db_addr_t)th, DB_STGY_ANY);
2091 db_printf(
" (%p)\n", th);
2094 DB_PRINTSYM(rint, th->th_rint);
2095 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2096 DB_PRINTSYM(rint_done, th->th_rint_done);
2097 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2098 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2099 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2100 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2101 DB_PRINTSYM(close, th->th_close);
2105 _db_show_termios(
const char *
name,
const struct termios *t)
2108 db_printf(
"%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2109 "lflag 0x%x ispeed %u ospeed %u\n", name,
2110 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2111 t->c_ispeed, t->c_ospeed);
2115 DB_SHOW_COMMAND(tty, db_show_tty)
2120 db_printf(
"usage: show tty <addr>\n");
2123 tp = (
struct tty *)addr;
2125 db_printf(
"0x%p: %s\n", tp, tty_devname(tp));
2126 db_printf(
"\tmtx: %p\n", tp->t_mtx);
2127 db_printf(
"\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2128 db_printf(
"\trevokecnt: %u\n", tp->t_revokecnt);
2131 db_printf(
"\tinq: %p begin %u linestart %u reprint %u end %u "
2132 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2133 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2134 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2135 db_printf(
"\toutq: %p begin %u end %u nblocks %u quota %u\n",
2136 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2137 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2138 db_printf(
"\tinlow: %zu\n", tp->t_inlow);
2139 db_printf(
"\toutlow: %zu\n", tp->t_outlow);
2140 _db_show_termios(
"\ttermios", &tp->t_termios);
2141 db_printf(
"\twinsize: row %u col %u xpixel %u ypixel %u\n",
2142 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2143 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2144 db_printf(
"\tcolumn: %u\n", tp->t_column);
2145 db_printf(
"\twritepos: %u\n", tp->t_writepos);
2146 db_printf(
"\tcompatflags: 0x%x\n", tp->t_compatflags);
2149 _db_show_termios(
"\ttermios_init_in", &tp->t_termios_init_in);
2150 _db_show_termios(
"\ttermios_init_out", &tp->t_termios_init_out);
2151 _db_show_termios(
"\ttermios_lock_in", &tp->t_termios_lock_in);
2152 _db_show_termios(
"\ttermios_lock_out", &tp->t_termios_lock_out);
2155 _db_show_devsw(
"\t", tp->t_devsw);
2156 _db_show_hooks(
"\t", tp->t_hook);
2159 db_printf(
"\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2160 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2161 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2162 db_printf(
"\tsession: %p", tp->t_session);
2163 if (tp->t_session != NULL)
2164 db_printf(
" count %u leader %p tty %p sid %d login %s",
2165 tp->t_session->s_count, tp->t_session->s_leader,
2166 tp->t_session->s_ttyp, tp->t_session->s_sid,
2167 tp->t_session->s_login);
2169 db_printf(
"\tsessioncnt: %u\n", tp->t_sessioncnt);
2170 db_printf(
"\tdevswsoftc: %p\n", tp->t_devswsoftc);
2171 db_printf(
"\thooksoftc: %p\n", tp->t_hooksoftc);
2172 db_printf(
"\tdev: %p\n", tp->t_dev);
2176 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2184 #if defined(__LP64__)
2187 db_printf(
" LINE INQ CAN LIN LOW OUTQ USE LOW "
2188 "COL SESS PGID STATE\n");
2190 TAILQ_FOREACH(tp, &tty_list, t_list) {
2191 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2192 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2194 db_printf(
"%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2198 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2199 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2202 tp->t_outq.to_end - tp->t_outq.to_begin,
2203 osiz - tp->t_outlow,
2204 MIN(tp->t_column, 99999),
2205 tp->t_session ? tp->t_session->s_sid : 0,
2206 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2209 for (i = j = 0; ttystates[i].flag; i++)
2210 if (tp->t_flags & ttystates[i].flag) {
2211 db_printf(
"%c", ttystates[i].val);
static int tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
int vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
pid_t fgetown(struct sigio **sigiop)
int fsetown(pid_t pgid, struct sigio **sigiop)
static struct filterops tty_kqops_write
int destroy_dev_sched_cb(struct cdev *dev, void(*cb)(void *), void *arg)
void tty_hiwat_in_unblock(struct tty *tp)
static void ttydevsw_definwakeup(struct tty *tp)
static void ttydev_leave(struct tty *tp)
void selwakeup(struct selinfo *sip)
struct file * fget_unlocked(struct filedesc *fdp, int fd)
void ttyinq_free(struct ttyinq *ti)
static void ttydevsw_defpktnotify(struct tty *tp, char event)
void selrecord(struct thread *selector, struct selinfo *sip)
void ttyinq_flush(struct ttyinq *ti)
void ttyhook_unregister(struct tty *tp)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
void ttyoutq_free(struct ttyoutq *to)
SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL)
static int sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
static struct filterops tty_kqops_read
int ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
void cv_destroy(struct cv *cvp)
SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE, 0, 0, sysctl_kern_ttys,"S,xtty","List of TTYs")
static int ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
int cap_funwrap(struct file *fp_cap, cap_rights_t rights, struct file **fpp)
void tty_info(struct tty *tp)
static __inline int tty_is_ctty(struct tty *tp, struct proc *p)
void dev_depends(struct cdev *pdev, struct cdev *cdev)
void panic(const char *fmt,...)
void ttyinq_canonicalize(struct ttyinq *ti)
void tty_rel_gone(struct tty *tp)
void knote(struct knlist *list, long hint, int lockflags)
void dev_relthread(struct cdev *dev, int ref)
struct cdev * make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
void pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
void tty_init_console(struct tty *tp, speed_t s)
static int tty_kqops_write_event(struct knote *kn, long hint)
void ttydisc_optimize(struct tty *tp)
static int ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
void ttydisc_close(struct tty *tp)
void ttyoutq_setsize(struct ttyoutq *to, struct tty *tp, size_t size)
void knlist_destroy(struct knlist *knl)
void cv_signal(struct cv *cvp)
static int ttydevsw_defcioctl(struct tty *tp, int unit, u_long cmd, caddr_t data, struct thread *td)
struct cdev * make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
void kern_psignal(struct proc *p, int sig)
static struct cdevsw ttyil_cdevsw
void ttydisc_rint_done(struct tty *tp)
void tty_set_winsize(struct tty *tp, const struct winsize *wsz)
void tty_rel_sess(struct tty *tp, struct session *sess)
void funsetown(struct sigio **sigiop)
static int ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
void ttydisc_modem(struct tty *tp, int open)
static int ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
int priv_check(struct thread *td, int priv)
struct tty * tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
static void ttyconsdev_init(void *unused)
struct tty * tty_alloc(struct ttydevsw *tsw, void *sc)
static void tty_kqops_write_detach(struct knote *kn)
static void tty_to_xtty(struct tty *tp, struct xtty *xt)
dev_t tty_udev(struct tty *tp)
void tty_wakeup(struct tty *tp, int flags)
int tty_wait(struct tty *tp, struct cv *cv)
static __inline int ttydev_enter(struct tty *tp)
void log_console(struct uio *uio)
static void tty_rel_free(struct tty *tp)
void ttyconsdev_select(const char *name)
static int ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
void knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
void tty_signal_sessleader(struct tty *tp, int sig)
void ttyinq_setsize(struct ttyinq *ti, struct tty *tp, size_t size)
static int ttydev_kqfilter(struct cdev *dev, struct knote *kn)
void seldrain(struct selinfo *sip)
void tty_flush(struct tty *tp, int flags)
static int ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
static int ttydevsw_defmmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
int tty_timedwait(struct tty *tp, struct cv *cv, int hz)
int ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th, void *softc)
static int ttydevsw_defopen(struct tty *tp)
static void tty_init_termios(struct tty *tp)
static int ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
static int ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
void knlist_add(struct knlist *knl, struct knote *kn, int islocked)
static int ttydevsw_defparam(struct tty *tp, struct termios *t)
int tty_checkoutq(struct tty *tp)
static int ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
int ttydisc_rint(struct tty *tp, char c, int flags)
struct pgrp * pgfind(pid_t pgid)
int tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
struct cdevsw * devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref)
static int tty_kqops_read_event(struct knote *kn, long hint)
void cv_init(struct cv *cvp, const char *desc)
void tty_hiwat_in_block(struct tty *tp)
static int ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
static int tty_drain(struct tty *tp)
SX_SYSINIT(acct,&acct_sx,"acct_sx")
void free(void *addr, struct malloc_type *mtp)
void ttyoutq_flush(struct ttyoutq *to)
int tty_wait_background(struct tty *tp, struct thread *td, int sig)
static MALLOC_DEFINE(M_TTY,"tty","tty device")
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
static void ttydevsw_deffree(void *softc)
void tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
void tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt,...)
static void ttydevsw_defclose(struct tty *tp)
void constty_set(struct tty *tp)
static struct cdevsw ttydev_cdevsw
static void ttydevsw_defoutwakeup(struct tty *tp)
static int ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
static struct cdevsw ttyconsdev_cdevsw
int ttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t nbytes)
static void tty_dealloc(void *arg)
static void tty_kqops_read_detach(struct knote *kn)
void mtx_destroy(struct mtx *m)
static int ttydev_poll(struct cdev *dev, int events, struct thread *td)
#define TTY_CALLOUT(tp, d)
static int ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
int tty_ioctl_compat(struct tty *tp, u_long com, caddr_t data, int fflag, struct thread *td)
int ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
void tty_signal_pgrp(struct tty *tp, int sig)
void knlist_init_mtx(struct knlist *knl, struct mtx *lock)
static int ttyhook_defrint(struct tty *tp, char c, int flags)
static int ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
void pgsigio(struct sigio **sigiop, int sig, int checkctty)
static int ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
struct fileops badfileops
void ttydisc_open(struct tty *tp)