xref: /freebsd/sys/kern/tty.c (revision b88cc53d4ddda4486683ee2121f131b10ed21c30)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Portions of this software were developed under sponsorship from Snow
8  * B.V., the Netherlands.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_capsicum.h"
36 #include "opt_printf.h"
37 
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/conf.h>
41 #include <sys/cons.h>
42 #include <sys/fcntl.h>
43 #include <sys/file.h>
44 #include <sys/filedesc.h>
45 #include <sys/filio.h>
46 #ifdef COMPAT_43TTY
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>
53 #include <sys/poll.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/serial.h>
57 #include <sys/signal.h>
58 #include <sys/stat.h>
59 #include <sys/sx.h>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
62 #include <sys/tty.h>
63 #include <sys/ttycom.h>
64 #define TTYDEFCHARS
65 #include <sys/ttydefaults.h>
66 #undef TTYDEFCHARS
67 #include <sys/ucred.h>
68 #include <sys/vnode.h>
69 
70 #include <machine/stdarg.h>
71 
72 static MALLOC_DEFINE(M_TTY, "tty", "tty device");
73 
74 static void tty_rel_free(struct tty *tp);
75 
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;
80 
81 /* Character device of /dev/console. */
82 static struct cdev	*dev_console;
83 static const char	*dev_console_filename;
84 
85 /*
86  * Flags that are supported and stored by this implementation.
87  */
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|CNO_RTSDTR)
97 
98 #define	TTY_CALLOUT(tp,d) (dev2unit(d) & TTYUNIT_CALLOUT)
99 
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");
103 
104 /*
105  * Set TTY buffer sizes.
106  */
107 
108 #define	TTYBUF_MAX	65536
109 
110 #ifdef PRINTF_BUFR_SIZE
111 #define	TTY_PRBUF_SIZE	PRINTF_BUFR_SIZE
112 #else
113 #define	TTY_PRBUF_SIZE	256
114 #endif
115 
116 /*
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
120  * while unlocked.
121  */
122 static int
123 tty_watermarks(struct tty *tp)
124 {
125 	size_t bs = 0;
126 	int error;
127 
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);
132 	if (error != 0)
133 		return (error);
134 
135 	/* Set low watermark at 10% (when 90% is available). */
136 	tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
137 
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);
141 	if (error != 0)
142 		return (error);
143 
144 	/* Set low watermark at 10% (when 90% is available). */
145 	tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
146 
147 	return (0);
148 }
149 
150 static int
151 tty_drain(struct tty *tp, int leaving)
152 {
153 	sbintime_t timeout_at;
154 	size_t bytes;
155 	int error;
156 
157 	if (ttyhook_hashook(tp, getc_inject))
158 		/* buffer is inaccessible */
159 		return (0);
160 
161 	/*
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.
165 	 */
166 	if (leaving)
167 		timeout_at = getsbinuptime() + SBT_1S;
168 	else if (tp->t_drainwait != 0)
169 		timeout_at = getsbinuptime() + SBT_1S * tp->t_drainwait;
170 	else
171 		timeout_at = 0;
172 
173 	/*
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.
180 	 */
181 	error = 0;
182 	bytes = ttyoutq_bytesused(&tp->t_outq);
183 	for (;;) {
184 		if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp))
185 			return (0);
186 		if (error != 0)
187 			return (error);
188 		ttydevsw_outwakeup(tp);
189 		error = tty_timedwait(tp, &tp->t_outwait, hz / 10);
190 		if (error != 0 && error != EWOULDBLOCK)
191 			return (error);
192 		else if (timeout_at == 0 || getsbinuptime() < timeout_at)
193 			error = 0;
194 		else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes) {
195 			/* In close, making progress, grant an extra second. */
196 			error = 0;
197 			timeout_at += SBT_1S;
198 			bytes = ttyoutq_bytesused(&tp->t_outq);
199 		}
200 	}
201 }
202 
203 /*
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
209  * deallocated.
210  */
211 
212 static __inline int
213 ttydev_enter(struct tty *tp)
214 {
215 
216 	tty_lock(tp);
217 
218 	if (tty_gone(tp) || !tty_opened(tp)) {
219 		/* Device is already gone. */
220 		tty_unlock(tp);
221 		return (ENXIO);
222 	}
223 
224 	return (0);
225 }
226 
227 static void
228 ttydev_leave(struct tty *tp)
229 {
230 
231 	tty_lock_assert(tp, MA_OWNED);
232 
233 	if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
234 		/* Device is still opened somewhere. */
235 		tty_unlock(tp);
236 		return;
237 	}
238 
239 	tp->t_flags |= TF_OPENCLOSE;
240 
241 	/* Stop asynchronous I/O. */
242 	funsetown(&tp->t_sigio);
243 
244 	/* Remove console TTY. */
245 	if (constty == tp)
246 		constty_clear();
247 
248 	/* Drain any output. */
249 	if (!tty_gone(tp))
250 		tty_drain(tp, 1);
251 
252 	ttydisc_close(tp);
253 
254 	/* Free i/o queues now since they might be large. */
255 	ttyinq_free(&tp->t_inq);
256 	tp->t_inlow = 0;
257 	ttyoutq_free(&tp->t_outq);
258 	tp->t_outlow = 0;
259 
260 	knlist_clear(&tp->t_inpoll.si_note, 1);
261 	knlist_clear(&tp->t_outpoll.si_note, 1);
262 
263 	if (!tty_gone(tp))
264 		ttydevsw_close(tp);
265 
266 	tp->t_flags &= ~TF_OPENCLOSE;
267 	cv_broadcast(&tp->t_dcdwait);
268 	tty_rel_free(tp);
269 }
270 
271 /*
272  * Operations that are exposed through the character device in /dev.
273  */
274 static int
275 ttydev_open(struct cdev *dev, int oflags, int devtype __unused,
276     struct thread *td)
277 {
278 	struct tty *tp;
279 	int error;
280 
281 	tp = dev->si_drv1;
282 	error = 0;
283 	tty_lock(tp);
284 	if (tty_gone(tp)) {
285 		/* Device is already gone. */
286 		tty_unlock(tp);
287 		return (ENXIO);
288 	}
289 
290 	/*
291 	 * Block when other processes are currently opening or closing
292 	 * the TTY.
293 	 */
294 	while (tp->t_flags & TF_OPENCLOSE) {
295 		error = tty_wait(tp, &tp->t_dcdwait);
296 		if (error != 0) {
297 			tty_unlock(tp);
298 			return (error);
299 		}
300 	}
301 	tp->t_flags |= TF_OPENCLOSE;
302 
303 	/*
304 	 * Make sure the "tty" and "cua" device cannot be opened at the
305 	 * same time.  The console is a "tty" device.
306 	 */
307 	if (TTY_CALLOUT(tp, dev)) {
308 		if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) {
309 			error = EBUSY;
310 			goto done;
311 		}
312 	} else {
313 		if (tp->t_flags & TF_OPENED_OUT) {
314 			error = EBUSY;
315 			goto done;
316 		}
317 	}
318 
319 	if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
320 		error = EBUSY;
321 		goto done;
322 	}
323 
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;
328 		else
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;
334 
335 		if ((tp->t_termios.c_cflag & CNO_RTSDTR) == 0)
336 			ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
337 
338 		error = ttydevsw_open(tp);
339 		if (error != 0)
340 			goto done;
341 
342 		ttydisc_open(tp);
343 		error = tty_watermarks(tp);
344 		if (error != 0)
345 			goto done;
346 	}
347 
348 	/* Wait for Carrier Detect. */
349 	if ((oflags & O_NONBLOCK) == 0 &&
350 	    (tp->t_termios.c_cflag & CLOCAL) == 0) {
351 		while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
352 			error = tty_wait(tp, &tp->t_dcdwait);
353 			if (error != 0)
354 				goto done;
355 		}
356 	}
357 
358 	if (dev == dev_console)
359 		tp->t_flags |= TF_OPENED_CONS;
360 	else if (TTY_CALLOUT(tp, dev))
361 		tp->t_flags |= TF_OPENED_OUT;
362 	else
363 		tp->t_flags |= TF_OPENED_IN;
364 	MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
365 	    (tp->t_flags & TF_OPENED_OUT) == 0);
366 
367 done:	tp->t_flags &= ~TF_OPENCLOSE;
368 	cv_broadcast(&tp->t_dcdwait);
369 	ttydev_leave(tp);
370 
371 	return (error);
372 }
373 
374 static int
375 ttydev_close(struct cdev *dev, int fflag, int devtype __unused,
376     struct thread *td __unused)
377 {
378 	struct tty *tp = dev->si_drv1;
379 
380 	tty_lock(tp);
381 
382 	/*
383 	 * Don't actually close the device if it is being used as the
384 	 * console.
385 	 */
386 	MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
387 	    (tp->t_flags & TF_OPENED_OUT) == 0);
388 	if (dev == dev_console)
389 		tp->t_flags &= ~TF_OPENED_CONS;
390 	else
391 		tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
392 
393 	if (tp->t_flags & TF_OPENED) {
394 		tty_unlock(tp);
395 		return (0);
396 	}
397 
398 	/* If revoking, flush output now to avoid draining it later. */
399 	if (fflag & FREVOKE)
400 		tty_flush(tp, FWRITE);
401 
402 	tp->t_flags &= ~TF_EXCLUDE;
403 
404 	/* Properly wake up threads that are stuck - revoke(). */
405 	tp->t_revokecnt++;
406 	tty_wakeup(tp, FREAD|FWRITE);
407 	cv_broadcast(&tp->t_bgwait);
408 	cv_broadcast(&tp->t_dcdwait);
409 
410 	ttydev_leave(tp);
411 
412 	return (0);
413 }
414 
415 static __inline int
416 tty_is_ctty(struct tty *tp, struct proc *p)
417 {
418 
419 	tty_lock_assert(tp, MA_OWNED);
420 
421 	return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
422 }
423 
424 int
425 tty_wait_background(struct tty *tp, struct thread *td, int sig)
426 {
427 	struct proc *p = td->td_proc;
428 	struct pgrp *pg;
429 	ksiginfo_t ksi;
430 	int error;
431 
432 	MPASS(sig == SIGTTIN || sig == SIGTTOU);
433 	tty_lock_assert(tp, MA_OWNED);
434 
435 	for (;;) {
436 		PROC_LOCK(p);
437 		/*
438 		 * The process should only sleep, when:
439 		 * - This terminal is the controlling terminal
440 		 * - Its process group is not the foreground process
441 		 *   group
442 		 * - The parent process isn't waiting for the child to
443 		 *   exit
444 		 * - the signal to send to the process isn't masked
445 		 */
446 		if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
447 			/* Allow the action to happen. */
448 			PROC_UNLOCK(p);
449 			return (0);
450 		}
451 
452 		if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
453 		    SIGISMEMBER(td->td_sigmask, sig)) {
454 			/* Only allow them in write()/ioctl(). */
455 			PROC_UNLOCK(p);
456 			return (sig == SIGTTOU ? 0 : EIO);
457 		}
458 
459 		pg = p->p_pgrp;
460 		if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
461 			/* Don't allow the action to happen. */
462 			PROC_UNLOCK(p);
463 			return (EIO);
464 		}
465 		PROC_UNLOCK(p);
466 
467 		/*
468 		 * Send the signal and sleep until we're the new
469 		 * foreground process group.
470 		 */
471 		if (sig != 0) {
472 			ksiginfo_init(&ksi);
473 			ksi.ksi_code = SI_KERNEL;
474 			ksi.ksi_signo = sig;
475 			sig = 0;
476 		}
477 		PGRP_LOCK(pg);
478 		pgsignal(pg, ksi.ksi_signo, 1, &ksi);
479 		PGRP_UNLOCK(pg);
480 
481 		error = tty_wait(tp, &tp->t_bgwait);
482 		if (error)
483 			return (error);
484 	}
485 }
486 
487 static int
488 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
489 {
490 	struct tty *tp = dev->si_drv1;
491 	int error;
492 
493 	error = ttydev_enter(tp);
494 	if (error)
495 		goto done;
496 	error = ttydisc_read(tp, uio, ioflag);
497 	tty_unlock(tp);
498 
499 	/*
500 	 * The read() call should not throw an error when the device is
501 	 * being destroyed. Silently convert it to an EOF.
502 	 */
503 done:	if (error == ENXIO)
504 		error = 0;
505 	return (error);
506 }
507 
508 static int
509 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
510 {
511 	struct tty *tp = dev->si_drv1;
512 	int error;
513 
514 	error = ttydev_enter(tp);
515 	if (error)
516 		return (error);
517 
518 	if (tp->t_termios.c_lflag & TOSTOP) {
519 		error = tty_wait_background(tp, curthread, SIGTTOU);
520 		if (error)
521 			goto done;
522 	}
523 
524 	if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
525 		/* Allow non-blocking writes to bypass serialization. */
526 		error = ttydisc_write(tp, uio, ioflag);
527 	} else {
528 		/* Serialize write() calls. */
529 		while (tp->t_flags & TF_BUSY_OUT) {
530 			error = tty_wait(tp, &tp->t_outserwait);
531 			if (error)
532 				goto done;
533 		}
534 
535 		tp->t_flags |= TF_BUSY_OUT;
536 		error = ttydisc_write(tp, uio, ioflag);
537 		tp->t_flags &= ~TF_BUSY_OUT;
538 		cv_signal(&tp->t_outserwait);
539 	}
540 
541 done:	tty_unlock(tp);
542 	return (error);
543 }
544 
545 static int
546 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
547     struct thread *td)
548 {
549 	struct tty *tp = dev->si_drv1;
550 	int error;
551 
552 	error = ttydev_enter(tp);
553 	if (error)
554 		return (error);
555 
556 	switch (cmd) {
557 	case TIOCCBRK:
558 	case TIOCCONS:
559 	case TIOCDRAIN:
560 	case TIOCEXCL:
561 	case TIOCFLUSH:
562 	case TIOCNXCL:
563 	case TIOCSBRK:
564 	case TIOCSCTTY:
565 	case TIOCSETA:
566 	case TIOCSETAF:
567 	case TIOCSETAW:
568 	case TIOCSPGRP:
569 	case TIOCSTART:
570 	case TIOCSTAT:
571 	case TIOCSTI:
572 	case TIOCSTOP:
573 	case TIOCSWINSZ:
574 #if 0
575 	case TIOCSDRAINWAIT:
576 	case TIOCSETD:
577 #endif
578 #ifdef COMPAT_43TTY
579 	case  TIOCLBIC:
580 	case  TIOCLBIS:
581 	case  TIOCLSET:
582 	case  TIOCSETC:
583 	case OTIOCSETD:
584 	case  TIOCSETN:
585 	case  TIOCSETP:
586 	case  TIOCSLTC:
587 #endif /* COMPAT_43TTY */
588 		/*
589 		 * If the ioctl() causes the TTY to be modified, let it
590 		 * wait in the background.
591 		 */
592 		error = tty_wait_background(tp, curthread, SIGTTOU);
593 		if (error)
594 			goto done;
595 	}
596 
597 	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
598 		struct termios *old = &tp->t_termios;
599 		struct termios *new = (struct termios *)data;
600 		struct termios *lock = TTY_CALLOUT(tp, dev) ?
601 		    &tp->t_termios_lock_out : &tp->t_termios_lock_in;
602 		int cc;
603 
604 		/*
605 		 * Lock state devices.  Just overwrite the values of the
606 		 * commands that are currently in use.
607 		 */
608 		new->c_iflag = (old->c_iflag & lock->c_iflag) |
609 		    (new->c_iflag & ~lock->c_iflag);
610 		new->c_oflag = (old->c_oflag & lock->c_oflag) |
611 		    (new->c_oflag & ~lock->c_oflag);
612 		new->c_cflag = (old->c_cflag & lock->c_cflag) |
613 		    (new->c_cflag & ~lock->c_cflag);
614 		new->c_lflag = (old->c_lflag & lock->c_lflag) |
615 		    (new->c_lflag & ~lock->c_lflag);
616 		for (cc = 0; cc < NCCS; ++cc)
617 			if (lock->c_cc[cc])
618 				new->c_cc[cc] = old->c_cc[cc];
619 		if (lock->c_ispeed)
620 			new->c_ispeed = old->c_ispeed;
621 		if (lock->c_ospeed)
622 			new->c_ospeed = old->c_ospeed;
623 	}
624 
625 	error = tty_ioctl(tp, cmd, data, fflag, td);
626 done:	tty_unlock(tp);
627 
628 	return (error);
629 }
630 
631 static int
632 ttydev_poll(struct cdev *dev, int events, struct thread *td)
633 {
634 	struct tty *tp = dev->si_drv1;
635 	int error, revents = 0;
636 
637 	error = ttydev_enter(tp);
638 	if (error)
639 		return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
640 
641 	if (events & (POLLIN|POLLRDNORM)) {
642 		/* See if we can read something. */
643 		if (ttydisc_read_poll(tp) > 0)
644 			revents |= events & (POLLIN|POLLRDNORM);
645 	}
646 
647 	if (tp->t_flags & TF_ZOMBIE) {
648 		/* Hangup flag on zombie state. */
649 		revents |= POLLHUP;
650 	} else if (events & (POLLOUT|POLLWRNORM)) {
651 		/* See if we can write something. */
652 		if (ttydisc_write_poll(tp) > 0)
653 			revents |= events & (POLLOUT|POLLWRNORM);
654 	}
655 
656 	if (revents == 0) {
657 		if (events & (POLLIN|POLLRDNORM))
658 			selrecord(td, &tp->t_inpoll);
659 		if (events & (POLLOUT|POLLWRNORM))
660 			selrecord(td, &tp->t_outpoll);
661 	}
662 
663 	tty_unlock(tp);
664 
665 	return (revents);
666 }
667 
668 static int
669 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
670     int nprot, vm_memattr_t *memattr)
671 {
672 	struct tty *tp = dev->si_drv1;
673 	int error;
674 
675 	/* Handle mmap() through the driver. */
676 
677 	error = ttydev_enter(tp);
678 	if (error)
679 		return (-1);
680 	error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
681 	tty_unlock(tp);
682 
683 	return (error);
684 }
685 
686 /*
687  * kqueue support.
688  */
689 
690 static void
691 tty_kqops_read_detach(struct knote *kn)
692 {
693 	struct tty *tp = kn->kn_hook;
694 
695 	knlist_remove(&tp->t_inpoll.si_note, kn, 0);
696 }
697 
698 static int
699 tty_kqops_read_event(struct knote *kn, long hint __unused)
700 {
701 	struct tty *tp = kn->kn_hook;
702 
703 	tty_lock_assert(tp, MA_OWNED);
704 
705 	if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
706 		kn->kn_flags |= EV_EOF;
707 		return (1);
708 	} else {
709 		kn->kn_data = ttydisc_read_poll(tp);
710 		return (kn->kn_data > 0);
711 	}
712 }
713 
714 static void
715 tty_kqops_write_detach(struct knote *kn)
716 {
717 	struct tty *tp = kn->kn_hook;
718 
719 	knlist_remove(&tp->t_outpoll.si_note, kn, 0);
720 }
721 
722 static int
723 tty_kqops_write_event(struct knote *kn, long hint __unused)
724 {
725 	struct tty *tp = kn->kn_hook;
726 
727 	tty_lock_assert(tp, MA_OWNED);
728 
729 	if (tty_gone(tp)) {
730 		kn->kn_flags |= EV_EOF;
731 		return (1);
732 	} else {
733 		kn->kn_data = ttydisc_write_poll(tp);
734 		return (kn->kn_data > 0);
735 	}
736 }
737 
738 static struct filterops tty_kqops_read = {
739 	.f_isfd = 1,
740 	.f_detach = tty_kqops_read_detach,
741 	.f_event = tty_kqops_read_event,
742 };
743 
744 static struct filterops tty_kqops_write = {
745 	.f_isfd = 1,
746 	.f_detach = tty_kqops_write_detach,
747 	.f_event = tty_kqops_write_event,
748 };
749 
750 static int
751 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
752 {
753 	struct tty *tp = dev->si_drv1;
754 	int error;
755 
756 	error = ttydev_enter(tp);
757 	if (error)
758 		return (error);
759 
760 	switch (kn->kn_filter) {
761 	case EVFILT_READ:
762 		kn->kn_hook = tp;
763 		kn->kn_fop = &tty_kqops_read;
764 		knlist_add(&tp->t_inpoll.si_note, kn, 1);
765 		break;
766 	case EVFILT_WRITE:
767 		kn->kn_hook = tp;
768 		kn->kn_fop = &tty_kqops_write;
769 		knlist_add(&tp->t_outpoll.si_note, kn, 1);
770 		break;
771 	default:
772 		error = EINVAL;
773 		break;
774 	}
775 
776 	tty_unlock(tp);
777 	return (error);
778 }
779 
780 static struct cdevsw ttydev_cdevsw = {
781 	.d_version	= D_VERSION,
782 	.d_open		= ttydev_open,
783 	.d_close	= ttydev_close,
784 	.d_read		= ttydev_read,
785 	.d_write	= ttydev_write,
786 	.d_ioctl	= ttydev_ioctl,
787 	.d_kqfilter	= ttydev_kqfilter,
788 	.d_poll		= ttydev_poll,
789 	.d_mmap		= ttydev_mmap,
790 	.d_name		= "ttydev",
791 	.d_flags	= D_TTY,
792 };
793 
794 /*
795  * Init/lock-state devices
796  */
797 
798 static int
799 ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
800     struct thread *td)
801 {
802 	struct tty *tp;
803 	int error;
804 
805 	tp = dev->si_drv1;
806 	error = 0;
807 	tty_lock(tp);
808 	if (tty_gone(tp))
809 		error = ENODEV;
810 	tty_unlock(tp);
811 
812 	return (error);
813 }
814 
815 static int
816 ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
817     struct thread *td __unused)
818 {
819 
820 	return (0);
821 }
822 
823 static int
824 ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
825     int ioflag __unused)
826 {
827 
828 	return (ENODEV);
829 }
830 
831 static int
832 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
833     struct thread *td)
834 {
835 	struct tty *tp = dev->si_drv1;
836 	int error;
837 
838 	tty_lock(tp);
839 	if (tty_gone(tp)) {
840 		error = ENODEV;
841 		goto done;
842 	}
843 
844 	error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
845 	if (error != ENOIOCTL)
846 		goto done;
847 	error = 0;
848 
849 	switch (cmd) {
850 	case TIOCGETA:
851 		/* Obtain terminal flags through tcgetattr(). */
852 		*(struct termios*)data = *(struct termios*)dev->si_drv2;
853 		break;
854 	case TIOCSETA:
855 		/* Set terminal flags through tcsetattr(). */
856 		error = priv_check(td, PRIV_TTY_SETA);
857 		if (error)
858 			break;
859 		*(struct termios*)dev->si_drv2 = *(struct termios*)data;
860 		break;
861 	case TIOCGETD:
862 		*(int *)data = TTYDISC;
863 		break;
864 	case TIOCGWINSZ:
865 		bzero(data, sizeof(struct winsize));
866 		break;
867 	default:
868 		error = ENOTTY;
869 	}
870 
871 done:	tty_unlock(tp);
872 	return (error);
873 }
874 
875 static struct cdevsw ttyil_cdevsw = {
876 	.d_version	= D_VERSION,
877 	.d_open		= ttyil_open,
878 	.d_close	= ttyil_close,
879 	.d_read		= ttyil_rdwr,
880 	.d_write	= ttyil_rdwr,
881 	.d_ioctl	= ttyil_ioctl,
882 	.d_name		= "ttyil",
883 	.d_flags	= D_TTY,
884 };
885 
886 static void
887 tty_init_termios(struct tty *tp)
888 {
889 	struct termios *t = &tp->t_termios_init_in;
890 
891 	t->c_cflag = TTYDEF_CFLAG;
892 	t->c_iflag = TTYDEF_IFLAG;
893 	t->c_lflag = TTYDEF_LFLAG;
894 	t->c_oflag = TTYDEF_OFLAG;
895 	t->c_ispeed = TTYDEF_SPEED;
896 	t->c_ospeed = TTYDEF_SPEED;
897 	memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
898 
899 	tp->t_termios_init_out = *t;
900 }
901 
902 void
903 tty_init_console(struct tty *tp, speed_t s)
904 {
905 	struct termios *ti = &tp->t_termios_init_in;
906 	struct termios *to = &tp->t_termios_init_out;
907 
908 	if (s != 0) {
909 		ti->c_ispeed = ti->c_ospeed = s;
910 		to->c_ispeed = to->c_ospeed = s;
911 	}
912 
913 	ti->c_cflag |= CLOCAL;
914 	to->c_cflag |= CLOCAL;
915 }
916 
917 /*
918  * Standard device routine implementations, mostly meant for
919  * pseudo-terminal device drivers. When a driver creates a new terminal
920  * device class, missing routines are patched.
921  */
922 
923 static int
924 ttydevsw_defopen(struct tty *tp __unused)
925 {
926 
927 	return (0);
928 }
929 
930 static void
931 ttydevsw_defclose(struct tty *tp __unused)
932 {
933 
934 }
935 
936 static void
937 ttydevsw_defoutwakeup(struct tty *tp __unused)
938 {
939 
940 	panic("Terminal device has output, while not implemented");
941 }
942 
943 static void
944 ttydevsw_definwakeup(struct tty *tp __unused)
945 {
946 
947 }
948 
949 static int
950 ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
951     caddr_t data __unused, struct thread *td __unused)
952 {
953 
954 	return (ENOIOCTL);
955 }
956 
957 static int
958 ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
959     u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
960 {
961 
962 	return (ENOIOCTL);
963 }
964 
965 static int
966 ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
967 {
968 
969 	/*
970 	 * Allow the baud rate to be adjusted for pseudo-devices, but at
971 	 * least restrict it to 115200 to prevent excessive buffer
972 	 * usage.  Also disallow 0, to prevent foot shooting.
973 	 */
974 	if (t->c_ispeed < B50)
975 		t->c_ispeed = B50;
976 	else if (t->c_ispeed > B115200)
977 		t->c_ispeed = B115200;
978 	if (t->c_ospeed < B50)
979 		t->c_ospeed = B50;
980 	else if (t->c_ospeed > B115200)
981 		t->c_ospeed = B115200;
982 	t->c_cflag |= CREAD;
983 
984 	return (0);
985 }
986 
987 static int
988 ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
989     int sigoff __unused)
990 {
991 
992 	/* Simulate a carrier to make the TTY layer happy. */
993 	return (SER_DCD);
994 }
995 
996 static int
997 ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
998     vm_paddr_t *paddr __unused, int nprot __unused,
999     vm_memattr_t *memattr __unused)
1000 {
1001 
1002 	return (-1);
1003 }
1004 
1005 static void
1006 ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
1007 {
1008 
1009 }
1010 
1011 static void
1012 ttydevsw_deffree(void *softc __unused)
1013 {
1014 
1015 	panic("Terminal device freed without a free-handler");
1016 }
1017 
1018 static bool
1019 ttydevsw_defbusy(struct tty *tp __unused)
1020 {
1021 
1022 	return (FALSE);
1023 }
1024 
1025 /*
1026  * TTY allocation and deallocation. TTY devices can be deallocated when
1027  * the driver doesn't use it anymore, when the TTY isn't a session's
1028  * controlling TTY and when the device node isn't opened through devfs.
1029  */
1030 
1031 struct tty *
1032 tty_alloc(struct ttydevsw *tsw, void *sc)
1033 {
1034 
1035 	return (tty_alloc_mutex(tsw, sc, NULL));
1036 }
1037 
1038 struct tty *
1039 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1040 {
1041 	struct tty *tp;
1042 
1043 	/* Make sure the driver defines all routines. */
1044 #define PATCH_FUNC(x) do {				\
1045 	if (tsw->tsw_ ## x == NULL)			\
1046 		tsw->tsw_ ## x = ttydevsw_def ## x;	\
1047 } while (0)
1048 	PATCH_FUNC(open);
1049 	PATCH_FUNC(close);
1050 	PATCH_FUNC(outwakeup);
1051 	PATCH_FUNC(inwakeup);
1052 	PATCH_FUNC(ioctl);
1053 	PATCH_FUNC(cioctl);
1054 	PATCH_FUNC(param);
1055 	PATCH_FUNC(modem);
1056 	PATCH_FUNC(mmap);
1057 	PATCH_FUNC(pktnotify);
1058 	PATCH_FUNC(free);
1059 	PATCH_FUNC(busy);
1060 #undef PATCH_FUNC
1061 
1062 	tp = malloc(sizeof(struct tty) + TTY_PRBUF_SIZE, M_TTY,
1063 	    M_WAITOK | M_ZERO);
1064 	tp->t_prbufsz = TTY_PRBUF_SIZE;
1065 	tp->t_devsw = tsw;
1066 	tp->t_devswsoftc = sc;
1067 	tp->t_flags = tsw->tsw_flags;
1068 	tp->t_drainwait = tty_drainwait;
1069 
1070 	tty_init_termios(tp);
1071 
1072 	cv_init(&tp->t_inwait, "ttyin");
1073 	cv_init(&tp->t_outwait, "ttyout");
1074 	cv_init(&tp->t_outserwait, "ttyosr");
1075 	cv_init(&tp->t_bgwait, "ttybg");
1076 	cv_init(&tp->t_dcdwait, "ttydcd");
1077 
1078 	/* Allow drivers to use a custom mutex to lock the TTY. */
1079 	if (mutex != NULL) {
1080 		tp->t_mtx = mutex;
1081 	} else {
1082 		tp->t_mtx = &tp->t_mtxobj;
1083 		mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1084 	}
1085 
1086 	knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1087 	knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1088 
1089 	return (tp);
1090 }
1091 
1092 static void
1093 tty_dealloc(void *arg)
1094 {
1095 	struct tty *tp = arg;
1096 
1097 	/*
1098 	 * ttyydev_leave() usually frees the i/o queues earlier, but it is
1099 	 * not always called between queue allocation and here.  The queues
1100 	 * may be allocated by ioctls on a pty control device without the
1101 	 * corresponding pty slave device ever being open, or after it is
1102 	 * closed.
1103 	 */
1104 	ttyinq_free(&tp->t_inq);
1105 	ttyoutq_free(&tp->t_outq);
1106 	seldrain(&tp->t_inpoll);
1107 	seldrain(&tp->t_outpoll);
1108 	knlist_destroy(&tp->t_inpoll.si_note);
1109 	knlist_destroy(&tp->t_outpoll.si_note);
1110 
1111 	cv_destroy(&tp->t_inwait);
1112 	cv_destroy(&tp->t_outwait);
1113 	cv_destroy(&tp->t_bgwait);
1114 	cv_destroy(&tp->t_dcdwait);
1115 	cv_destroy(&tp->t_outserwait);
1116 
1117 	if (tp->t_mtx == &tp->t_mtxobj)
1118 		mtx_destroy(&tp->t_mtxobj);
1119 	ttydevsw_free(tp);
1120 	free(tp, M_TTY);
1121 }
1122 
1123 static void
1124 tty_rel_free(struct tty *tp)
1125 {
1126 	struct cdev *dev;
1127 
1128 	tty_lock_assert(tp, MA_OWNED);
1129 
1130 #define	TF_ACTIVITY	(TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1131 	if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1132 		/* TTY is still in use. */
1133 		tty_unlock(tp);
1134 		return;
1135 	}
1136 
1137 	/* TTY can be deallocated. */
1138 	dev = tp->t_dev;
1139 	tp->t_dev = NULL;
1140 	tty_unlock(tp);
1141 
1142 	if (dev != NULL) {
1143 		sx_xlock(&tty_list_sx);
1144 		TAILQ_REMOVE(&tty_list, tp, t_list);
1145 		tty_list_count--;
1146 		sx_xunlock(&tty_list_sx);
1147 		destroy_dev_sched_cb(dev, tty_dealloc, tp);
1148 	}
1149 }
1150 
1151 void
1152 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1153 {
1154 
1155 	MPASS(tp->t_sessioncnt > 0);
1156 	tty_lock_assert(tp, MA_OWNED);
1157 
1158 	if (tp->t_pgrp == pg)
1159 		tp->t_pgrp = NULL;
1160 
1161 	tty_unlock(tp);
1162 }
1163 
1164 void
1165 tty_rel_sess(struct tty *tp, struct session *sess)
1166 {
1167 
1168 	MPASS(tp->t_sessioncnt > 0);
1169 
1170 	/* Current session has left. */
1171 	if (tp->t_session == sess) {
1172 		tp->t_session = NULL;
1173 		MPASS(tp->t_pgrp == NULL);
1174 	}
1175 	tp->t_sessioncnt--;
1176 	tty_rel_free(tp);
1177 }
1178 
1179 void
1180 tty_rel_gone(struct tty *tp)
1181 {
1182 
1183 	MPASS(!tty_gone(tp));
1184 
1185 	/* Simulate carrier removal. */
1186 	ttydisc_modem(tp, 0);
1187 
1188 	/* Wake up all blocked threads. */
1189 	tty_wakeup(tp, FREAD|FWRITE);
1190 	cv_broadcast(&tp->t_bgwait);
1191 	cv_broadcast(&tp->t_dcdwait);
1192 
1193 	tp->t_flags |= TF_GONE;
1194 	tty_rel_free(tp);
1195 }
1196 
1197 /*
1198  * Exposing information about current TTY's through sysctl
1199  */
1200 
1201 static void
1202 tty_to_xtty(struct tty *tp, struct xtty *xt)
1203 {
1204 
1205 	tty_lock_assert(tp, MA_OWNED);
1206 
1207 	xt->xt_size = sizeof(struct xtty);
1208 	xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1209 	xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1210 	xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1211 	xt->xt_inlow = tp->t_inlow;
1212 	xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1213 	xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1214 	xt->xt_outlow = tp->t_outlow;
1215 	xt->xt_column = tp->t_column;
1216 	xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1217 	xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1218 	xt->xt_flags = tp->t_flags;
1219 	xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : (uint32_t)NODEV;
1220 }
1221 
1222 static int
1223 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1224 {
1225 	unsigned long lsize;
1226 	struct xtty *xtlist, *xt;
1227 	struct tty *tp;
1228 	int error;
1229 
1230 	sx_slock(&tty_list_sx);
1231 	lsize = tty_list_count * sizeof(struct xtty);
1232 	if (lsize == 0) {
1233 		sx_sunlock(&tty_list_sx);
1234 		return (0);
1235 	}
1236 
1237 	xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1238 
1239 	TAILQ_FOREACH(tp, &tty_list, t_list) {
1240 		tty_lock(tp);
1241 		tty_to_xtty(tp, xt);
1242 		tty_unlock(tp);
1243 		xt++;
1244 	}
1245 	sx_sunlock(&tty_list_sx);
1246 
1247 	error = SYSCTL_OUT(req, xtlist, lsize);
1248 	free(xtlist, M_TTY);
1249 	return (error);
1250 }
1251 
1252 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1253 	0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1254 
1255 /*
1256  * Device node creation. Device has been set up, now we can expose it to
1257  * the user.
1258  */
1259 
1260 int
1261 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1262     const char *fmt, ...)
1263 {
1264 	va_list ap;
1265 	struct make_dev_args args;
1266 	struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1267 	const char *prefix = "tty";
1268 	char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1269 	uid_t uid;
1270 	gid_t gid;
1271 	mode_t mode;
1272 	int error;
1273 
1274 	/* Remove "tty" prefix from devices like PTY's. */
1275 	if (tp->t_flags & TF_NOPREFIX)
1276 		prefix = "";
1277 
1278 	va_start(ap, fmt);
1279 	vsnrprintf(name, sizeof name, 32, fmt, ap);
1280 	va_end(ap);
1281 
1282 	if (cred == NULL) {
1283 		/* System device. */
1284 		uid = UID_ROOT;
1285 		gid = GID_WHEEL;
1286 		mode = S_IRUSR|S_IWUSR;
1287 	} else {
1288 		/* User device. */
1289 		uid = cred->cr_ruid;
1290 		gid = GID_TTY;
1291 		mode = S_IRUSR|S_IWUSR|S_IWGRP;
1292 	}
1293 
1294 	flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1295 	flags |= MAKEDEV_CHECKNAME;
1296 
1297 	/* Master call-in device. */
1298 	make_dev_args_init(&args);
1299 	args.mda_flags = flags;
1300 	args.mda_devsw = &ttydev_cdevsw;
1301 	args.mda_cr = cred;
1302 	args.mda_uid = uid;
1303 	args.mda_gid = gid;
1304 	args.mda_mode = mode;
1305 	args.mda_si_drv1 = tp;
1306 	error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1307 	if (error != 0)
1308 		return (error);
1309 	tp->t_dev = dev;
1310 
1311 	init = lock = cua = cinit = clock = NULL;
1312 
1313 	/* Slave call-in devices. */
1314 	if (tp->t_flags & TF_INITLOCK) {
1315 		args.mda_devsw = &ttyil_cdevsw;
1316 		args.mda_unit = TTYUNIT_INIT;
1317 		args.mda_si_drv1 = tp;
1318 		args.mda_si_drv2 = &tp->t_termios_init_in;
1319 		error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1320 		if (error != 0)
1321 			goto fail;
1322 		dev_depends(dev, init);
1323 
1324 		args.mda_unit = TTYUNIT_LOCK;
1325 		args.mda_si_drv2 = &tp->t_termios_lock_in;
1326 		error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1327 		if (error != 0)
1328 			goto fail;
1329 		dev_depends(dev, lock);
1330 	}
1331 
1332 	/* Call-out devices. */
1333 	if (tp->t_flags & TF_CALLOUT) {
1334 		make_dev_args_init(&args);
1335 		args.mda_flags = flags;
1336 		args.mda_devsw = &ttydev_cdevsw;
1337 		args.mda_cr = cred;
1338 		args.mda_uid = UID_UUCP;
1339 		args.mda_gid = GID_DIALER;
1340 		args.mda_mode = 0660;
1341 		args.mda_unit = TTYUNIT_CALLOUT;
1342 		args.mda_si_drv1 = tp;
1343 		error = make_dev_s(&args, &cua, "cua%s", name);
1344 		if (error != 0)
1345 			goto fail;
1346 		dev_depends(dev, cua);
1347 
1348 		/* Slave call-out devices. */
1349 		if (tp->t_flags & TF_INITLOCK) {
1350 			args.mda_devsw = &ttyil_cdevsw;
1351 			args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1352 			args.mda_si_drv2 = &tp->t_termios_init_out;
1353 			error = make_dev_s(&args, &cinit, "cua%s.init", name);
1354 			if (error != 0)
1355 				goto fail;
1356 			dev_depends(dev, cinit);
1357 
1358 			args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1359 			args.mda_si_drv2 = &tp->t_termios_lock_out;
1360 			error = make_dev_s(&args, &clock, "cua%s.lock", name);
1361 			if (error != 0)
1362 				goto fail;
1363 			dev_depends(dev, clock);
1364 		}
1365 	}
1366 
1367 	sx_xlock(&tty_list_sx);
1368 	TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1369 	tty_list_count++;
1370 	sx_xunlock(&tty_list_sx);
1371 
1372 	return (0);
1373 
1374 fail:
1375 	destroy_dev(dev);
1376 	if (init)
1377 		destroy_dev(init);
1378 	if (lock)
1379 		destroy_dev(lock);
1380 	if (cinit)
1381 		destroy_dev(cinit);
1382 	if (clock)
1383 		destroy_dev(clock);
1384 
1385 	return (error);
1386 }
1387 
1388 /*
1389  * Signalling processes.
1390  */
1391 
1392 void
1393 tty_signal_sessleader(struct tty *tp, int sig)
1394 {
1395 	struct proc *p;
1396 
1397 	tty_lock_assert(tp, MA_OWNED);
1398 	MPASS(sig >= 1 && sig < NSIG);
1399 
1400 	/* Make signals start output again. */
1401 	tp->t_flags &= ~TF_STOPPED;
1402 
1403 	if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1404 		p = tp->t_session->s_leader;
1405 		PROC_LOCK(p);
1406 		kern_psignal(p, sig);
1407 		PROC_UNLOCK(p);
1408 	}
1409 }
1410 
1411 void
1412 tty_signal_pgrp(struct tty *tp, int sig)
1413 {
1414 	ksiginfo_t ksi;
1415 
1416 	tty_lock_assert(tp, MA_OWNED);
1417 	MPASS(sig >= 1 && sig < NSIG);
1418 
1419 	/* Make signals start output again. */
1420 	tp->t_flags &= ~TF_STOPPED;
1421 
1422 	if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1423 		tty_info(tp);
1424 	if (tp->t_pgrp != NULL) {
1425 		ksiginfo_init(&ksi);
1426 		ksi.ksi_signo = sig;
1427 		ksi.ksi_code = SI_KERNEL;
1428 		PGRP_LOCK(tp->t_pgrp);
1429 		pgsignal(tp->t_pgrp, sig, 1, &ksi);
1430 		PGRP_UNLOCK(tp->t_pgrp);
1431 	}
1432 }
1433 
1434 void
1435 tty_wakeup(struct tty *tp, int flags)
1436 {
1437 
1438 	if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1439 		pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1440 
1441 	if (flags & FWRITE) {
1442 		cv_broadcast(&tp->t_outwait);
1443 		selwakeup(&tp->t_outpoll);
1444 		KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1445 	}
1446 	if (flags & FREAD) {
1447 		cv_broadcast(&tp->t_inwait);
1448 		selwakeup(&tp->t_inpoll);
1449 		KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1450 	}
1451 }
1452 
1453 int
1454 tty_wait(struct tty *tp, struct cv *cv)
1455 {
1456 	int error;
1457 	int revokecnt = tp->t_revokecnt;
1458 
1459 	tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1460 	MPASS(!tty_gone(tp));
1461 
1462 	error = cv_wait_sig(cv, tp->t_mtx);
1463 
1464 	/* Bail out when the device slipped away. */
1465 	if (tty_gone(tp))
1466 		return (ENXIO);
1467 
1468 	/* Restart the system call when we may have been revoked. */
1469 	if (tp->t_revokecnt != revokecnt)
1470 		return (ERESTART);
1471 
1472 	return (error);
1473 }
1474 
1475 int
1476 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1477 {
1478 	int error;
1479 	int revokecnt = tp->t_revokecnt;
1480 
1481 	tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1482 	MPASS(!tty_gone(tp));
1483 
1484 	error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1485 
1486 	/* Bail out when the device slipped away. */
1487 	if (tty_gone(tp))
1488 		return (ENXIO);
1489 
1490 	/* Restart the system call when we may have been revoked. */
1491 	if (tp->t_revokecnt != revokecnt)
1492 		return (ERESTART);
1493 
1494 	return (error);
1495 }
1496 
1497 void
1498 tty_flush(struct tty *tp, int flags)
1499 {
1500 
1501 	if (flags & FWRITE) {
1502 		tp->t_flags &= ~TF_HIWAT_OUT;
1503 		ttyoutq_flush(&tp->t_outq);
1504 		tty_wakeup(tp, FWRITE);
1505 		if (!tty_gone(tp)) {
1506 			ttydevsw_outwakeup(tp);
1507 			ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1508 		}
1509 	}
1510 	if (flags & FREAD) {
1511 		tty_hiwat_in_unblock(tp);
1512 		ttyinq_flush(&tp->t_inq);
1513 		tty_wakeup(tp, FREAD);
1514 		if (!tty_gone(tp)) {
1515 			ttydevsw_inwakeup(tp);
1516 			ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1517 		}
1518 	}
1519 }
1520 
1521 void
1522 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1523 {
1524 
1525 	if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1526 		return;
1527 	tp->t_winsize = *wsz;
1528 	tty_signal_pgrp(tp, SIGWINCH);
1529 }
1530 
1531 static int
1532 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1533     struct thread *td)
1534 {
1535 	int error;
1536 
1537 	switch (cmd) {
1538 	/*
1539 	 * Modem commands.
1540 	 * The SER_* and TIOCM_* flags are the same, but one bit
1541 	 * shifted. I don't know why.
1542 	 */
1543 	case TIOCSDTR:
1544 		ttydevsw_modem(tp, SER_DTR, 0);
1545 		return (0);
1546 	case TIOCCDTR:
1547 		ttydevsw_modem(tp, 0, SER_DTR);
1548 		return (0);
1549 	case TIOCMSET: {
1550 		int bits = *(int *)data;
1551 		ttydevsw_modem(tp,
1552 		    (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1553 		    ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1554 		return (0);
1555 	}
1556 	case TIOCMBIS: {
1557 		int bits = *(int *)data;
1558 		ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1559 		return (0);
1560 	}
1561 	case TIOCMBIC: {
1562 		int bits = *(int *)data;
1563 		ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1564 		return (0);
1565 	}
1566 	case TIOCMGET:
1567 		*(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1568 		return (0);
1569 
1570 	case FIOASYNC:
1571 		if (*(int *)data)
1572 			tp->t_flags |= TF_ASYNC;
1573 		else
1574 			tp->t_flags &= ~TF_ASYNC;
1575 		return (0);
1576 	case FIONBIO:
1577 		/* This device supports non-blocking operation. */
1578 		return (0);
1579 	case FIONREAD:
1580 		*(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1581 		return (0);
1582 	case FIONWRITE:
1583 	case TIOCOUTQ:
1584 		*(int *)data = ttyoutq_bytesused(&tp->t_outq);
1585 		return (0);
1586 	case FIOSETOWN:
1587 		if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1588 			/* Not allowed to set ownership. */
1589 			return (ENOTTY);
1590 
1591 		/* Temporarily unlock the TTY to set ownership. */
1592 		tty_unlock(tp);
1593 		error = fsetown(*(int *)data, &tp->t_sigio);
1594 		tty_lock(tp);
1595 		return (error);
1596 	case FIOGETOWN:
1597 		if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1598 			/* Not allowed to set ownership. */
1599 			return (ENOTTY);
1600 
1601 		/* Get ownership. */
1602 		*(int *)data = fgetown(&tp->t_sigio);
1603 		return (0);
1604 	case TIOCGETA:
1605 		/* Obtain terminal flags through tcgetattr(). */
1606 		*(struct termios*)data = tp->t_termios;
1607 		return (0);
1608 	case TIOCSETA:
1609 	case TIOCSETAW:
1610 	case TIOCSETAF: {
1611 		struct termios *t = data;
1612 
1613 		/*
1614 		 * Who makes up these funny rules? According to POSIX,
1615 		 * input baud rate is set equal to the output baud rate
1616 		 * when zero.
1617 		 */
1618 		if (t->c_ispeed == 0)
1619 			t->c_ispeed = t->c_ospeed;
1620 
1621 		/* Discard any unsupported bits. */
1622 		t->c_iflag &= TTYSUP_IFLAG;
1623 		t->c_oflag &= TTYSUP_OFLAG;
1624 		t->c_lflag &= TTYSUP_LFLAG;
1625 		t->c_cflag &= TTYSUP_CFLAG;
1626 
1627 		/* Set terminal flags through tcsetattr(). */
1628 		if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1629 			error = tty_drain(tp, 0);
1630 			if (error)
1631 				return (error);
1632 			if (cmd == TIOCSETAF)
1633 				tty_flush(tp, FREAD);
1634 		}
1635 
1636 		/*
1637 		 * Only call param() when the flags really change.
1638 		 */
1639 		if ((t->c_cflag & CIGNORE) == 0 &&
1640 		    (tp->t_termios.c_cflag != t->c_cflag ||
1641 		    ((tp->t_termios.c_iflag ^ t->c_iflag) &
1642 		    (IXON|IXOFF|IXANY)) ||
1643 		    tp->t_termios.c_ispeed != t->c_ispeed ||
1644 		    tp->t_termios.c_ospeed != t->c_ospeed)) {
1645 			error = ttydevsw_param(tp, t);
1646 			if (error)
1647 				return (error);
1648 
1649 			/* XXX: CLOCAL? */
1650 
1651 			tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1652 			tp->t_termios.c_ispeed = t->c_ispeed;
1653 			tp->t_termios.c_ospeed = t->c_ospeed;
1654 
1655 			/* Baud rate has changed - update watermarks. */
1656 			error = tty_watermarks(tp);
1657 			if (error)
1658 				return (error);
1659 		}
1660 
1661 		/* Copy new non-device driver parameters. */
1662 		tp->t_termios.c_iflag = t->c_iflag;
1663 		tp->t_termios.c_oflag = t->c_oflag;
1664 		tp->t_termios.c_lflag = t->c_lflag;
1665 		memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1666 
1667 		ttydisc_optimize(tp);
1668 
1669 		if ((t->c_lflag & ICANON) == 0) {
1670 			/*
1671 			 * When in non-canonical mode, wake up all
1672 			 * readers. Canonicalize any partial input. VMIN
1673 			 * and VTIME could also be adjusted.
1674 			 */
1675 			ttyinq_canonicalize(&tp->t_inq);
1676 			tty_wakeup(tp, FREAD);
1677 		}
1678 
1679 		/*
1680 		 * For packet mode: notify the PTY consumer that VSTOP
1681 		 * and VSTART may have been changed.
1682 		 */
1683 		if (tp->t_termios.c_iflag & IXON &&
1684 		    tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1685 		    tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1686 			ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1687 		else
1688 			ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1689 		return (0);
1690 	}
1691 	case TIOCGETD:
1692 		/* For compatibility - we only support TTYDISC. */
1693 		*(int *)data = TTYDISC;
1694 		return (0);
1695 	case TIOCGPGRP:
1696 		if (!tty_is_ctty(tp, td->td_proc))
1697 			return (ENOTTY);
1698 
1699 		if (tp->t_pgrp != NULL)
1700 			*(int *)data = tp->t_pgrp->pg_id;
1701 		else
1702 			*(int *)data = NO_PID;
1703 		return (0);
1704 	case TIOCGSID:
1705 		if (!tty_is_ctty(tp, td->td_proc))
1706 			return (ENOTTY);
1707 
1708 		MPASS(tp->t_session);
1709 		*(int *)data = tp->t_session->s_sid;
1710 		return (0);
1711 	case TIOCSCTTY: {
1712 		struct proc *p = td->td_proc;
1713 
1714 		/* XXX: This looks awful. */
1715 		tty_unlock(tp);
1716 		sx_xlock(&proctree_lock);
1717 		tty_lock(tp);
1718 
1719 		if (!SESS_LEADER(p)) {
1720 			/* Only the session leader may do this. */
1721 			sx_xunlock(&proctree_lock);
1722 			return (EPERM);
1723 		}
1724 
1725 		if (tp->t_session != NULL && tp->t_session == p->p_session) {
1726 			/* This is already our controlling TTY. */
1727 			sx_xunlock(&proctree_lock);
1728 			return (0);
1729 		}
1730 
1731 		if (p->p_session->s_ttyp != NULL ||
1732 		    (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1733 		    tp->t_session->s_ttyvp->v_type != VBAD)) {
1734 			/*
1735 			 * There is already a relation between a TTY and
1736 			 * a session, or the caller is not the session
1737 			 * leader.
1738 			 *
1739 			 * Allow the TTY to be stolen when the vnode is
1740 			 * invalid, but the reference to the TTY is
1741 			 * still active.  This allows immediate reuse of
1742 			 * TTYs of which the session leader has been
1743 			 * killed or the TTY revoked.
1744 			 */
1745 			sx_xunlock(&proctree_lock);
1746 			return (EPERM);
1747 		}
1748 
1749 		/* Connect the session to the TTY. */
1750 		tp->t_session = p->p_session;
1751 		tp->t_session->s_ttyp = tp;
1752 		tp->t_sessioncnt++;
1753 		sx_xunlock(&proctree_lock);
1754 
1755 		/* Assign foreground process group. */
1756 		tp->t_pgrp = p->p_pgrp;
1757 		PROC_LOCK(p);
1758 		p->p_flag |= P_CONTROLT;
1759 		PROC_UNLOCK(p);
1760 
1761 		return (0);
1762 	}
1763 	case TIOCSPGRP: {
1764 		struct pgrp *pg;
1765 
1766 		/*
1767 		 * XXX: Temporarily unlock the TTY to locate the process
1768 		 * group. This code would be lot nicer if we would ever
1769 		 * decompose proctree_lock.
1770 		 */
1771 		tty_unlock(tp);
1772 		sx_slock(&proctree_lock);
1773 		pg = pgfind(*(int *)data);
1774 		if (pg != NULL)
1775 			PGRP_UNLOCK(pg);
1776 		if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1777 			sx_sunlock(&proctree_lock);
1778 			tty_lock(tp);
1779 			return (EPERM);
1780 		}
1781 		tty_lock(tp);
1782 
1783 		/*
1784 		 * Determine if this TTY is the controlling TTY after
1785 		 * relocking the TTY.
1786 		 */
1787 		if (!tty_is_ctty(tp, td->td_proc)) {
1788 			sx_sunlock(&proctree_lock);
1789 			return (ENOTTY);
1790 		}
1791 		tp->t_pgrp = pg;
1792 		sx_sunlock(&proctree_lock);
1793 
1794 		/* Wake up the background process groups. */
1795 		cv_broadcast(&tp->t_bgwait);
1796 		return (0);
1797 	}
1798 	case TIOCFLUSH: {
1799 		int flags = *(int *)data;
1800 
1801 		if (flags == 0)
1802 			flags = (FREAD|FWRITE);
1803 		else
1804 			flags &= (FREAD|FWRITE);
1805 		tty_flush(tp, flags);
1806 		return (0);
1807 	}
1808 	case TIOCDRAIN:
1809 		/* Drain TTY output. */
1810 		return tty_drain(tp, 0);
1811 	case TIOCGDRAINWAIT:
1812 		*(int *)data = tp->t_drainwait;
1813 		return (0);
1814 	case TIOCSDRAINWAIT:
1815 		error = priv_check(td, PRIV_TTY_DRAINWAIT);
1816 		if (error == 0)
1817 			tp->t_drainwait = *(int *)data;
1818 		return (error);
1819 	case TIOCCONS:
1820 		/* Set terminal as console TTY. */
1821 		if (*(int *)data) {
1822 			error = priv_check(td, PRIV_TTY_CONSOLE);
1823 			if (error)
1824 				return (error);
1825 
1826 			/*
1827 			 * XXX: constty should really need to be locked!
1828 			 * XXX: allow disconnected constty's to be stolen!
1829 			 */
1830 
1831 			if (constty == tp)
1832 				return (0);
1833 			if (constty != NULL)
1834 				return (EBUSY);
1835 
1836 			tty_unlock(tp);
1837 			constty_set(tp);
1838 			tty_lock(tp);
1839 		} else if (constty == tp) {
1840 			constty_clear();
1841 		}
1842 		return (0);
1843 	case TIOCGWINSZ:
1844 		/* Obtain window size. */
1845 		*(struct winsize*)data = tp->t_winsize;
1846 		return (0);
1847 	case TIOCSWINSZ:
1848 		/* Set window size. */
1849 		tty_set_winsize(tp, data);
1850 		return (0);
1851 	case TIOCEXCL:
1852 		tp->t_flags |= TF_EXCLUDE;
1853 		return (0);
1854 	case TIOCNXCL:
1855 		tp->t_flags &= ~TF_EXCLUDE;
1856 		return (0);
1857 	case TIOCSTOP:
1858 		tp->t_flags |= TF_STOPPED;
1859 		ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1860 		return (0);
1861 	case TIOCSTART:
1862 		tp->t_flags &= ~TF_STOPPED;
1863 		ttydevsw_outwakeup(tp);
1864 		ttydevsw_pktnotify(tp, TIOCPKT_START);
1865 		return (0);
1866 	case TIOCSTAT:
1867 		tty_info(tp);
1868 		return (0);
1869 	case TIOCSTI:
1870 		if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1871 			return (EPERM);
1872 		if (!tty_is_ctty(tp, td->td_proc) &&
1873 		    priv_check(td, PRIV_TTY_STI))
1874 			return (EACCES);
1875 		ttydisc_rint(tp, *(char *)data, 0);
1876 		ttydisc_rint_done(tp);
1877 		return (0);
1878 	}
1879 
1880 #ifdef COMPAT_43TTY
1881 	return tty_ioctl_compat(tp, cmd, data, fflag, td);
1882 #else /* !COMPAT_43TTY */
1883 	return (ENOIOCTL);
1884 #endif /* COMPAT_43TTY */
1885 }
1886 
1887 int
1888 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1889 {
1890 	int error;
1891 
1892 	tty_lock_assert(tp, MA_OWNED);
1893 
1894 	if (tty_gone(tp))
1895 		return (ENXIO);
1896 
1897 	error = ttydevsw_ioctl(tp, cmd, data, td);
1898 	if (error == ENOIOCTL)
1899 		error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1900 
1901 	return (error);
1902 }
1903 
1904 dev_t
1905 tty_udev(struct tty *tp)
1906 {
1907 
1908 	if (tp->t_dev)
1909 		return (dev2udev(tp->t_dev));
1910 	else
1911 		return (NODEV);
1912 }
1913 
1914 int
1915 tty_checkoutq(struct tty *tp)
1916 {
1917 
1918 	/* 256 bytes should be enough to print a log message. */
1919 	return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1920 }
1921 
1922 void
1923 tty_hiwat_in_block(struct tty *tp)
1924 {
1925 
1926 	if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1927 	    tp->t_termios.c_iflag & IXOFF &&
1928 	    tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1929 		/*
1930 		 * Input flow control. Only enter the high watermark when we
1931 		 * can successfully store the VSTOP character.
1932 		 */
1933 		if (ttyoutq_write_nofrag(&tp->t_outq,
1934 		    &tp->t_termios.c_cc[VSTOP], 1) == 0)
1935 			tp->t_flags |= TF_HIWAT_IN;
1936 	} else {
1937 		/* No input flow control. */
1938 		tp->t_flags |= TF_HIWAT_IN;
1939 	}
1940 }
1941 
1942 void
1943 tty_hiwat_in_unblock(struct tty *tp)
1944 {
1945 
1946 	if (tp->t_flags & TF_HIWAT_IN &&
1947 	    tp->t_termios.c_iflag & IXOFF &&
1948 	    tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1949 		/*
1950 		 * Input flow control. Only leave the high watermark when we
1951 		 * can successfully store the VSTART character.
1952 		 */
1953 		if (ttyoutq_write_nofrag(&tp->t_outq,
1954 		    &tp->t_termios.c_cc[VSTART], 1) == 0)
1955 			tp->t_flags &= ~TF_HIWAT_IN;
1956 	} else {
1957 		/* No input flow control. */
1958 		tp->t_flags &= ~TF_HIWAT_IN;
1959 	}
1960 
1961 	if (!tty_gone(tp))
1962 		ttydevsw_inwakeup(tp);
1963 }
1964 
1965 /*
1966  * TTY hooks interface.
1967  */
1968 
1969 static int
1970 ttyhook_defrint(struct tty *tp, char c, int flags)
1971 {
1972 
1973 	if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1974 		return (-1);
1975 
1976 	return (0);
1977 }
1978 
1979 int
1980 ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
1981     void *softc)
1982 {
1983 	struct tty *tp;
1984 	struct file *fp;
1985 	struct cdev *dev;
1986 	struct cdevsw *cdp;
1987 	struct filedesc *fdp;
1988 	cap_rights_t rights;
1989 	int error, ref;
1990 
1991 	/* Validate the file descriptor. */
1992 	fdp = p->p_fd;
1993 	error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1994 	    &fp, NULL);
1995 	if (error != 0)
1996 		return (error);
1997 	if (fp->f_ops == &badfileops) {
1998 		error = EBADF;
1999 		goto done1;
2000 	}
2001 
2002 	/*
2003 	 * Make sure the vnode is bound to a character device.
2004 	 * Unlocked check for the vnode type is ok there, because we
2005 	 * only shall prevent calling devvn_refthread on the file that
2006 	 * never has been opened over a character device.
2007 	 */
2008 	if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
2009 		error = EINVAL;
2010 		goto done1;
2011 	}
2012 
2013 	/* Make sure it is a TTY. */
2014 	cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
2015 	if (cdp == NULL) {
2016 		error = ENXIO;
2017 		goto done1;
2018 	}
2019 	if (dev != fp->f_data) {
2020 		error = ENXIO;
2021 		goto done2;
2022 	}
2023 	if (cdp != &ttydev_cdevsw) {
2024 		error = ENOTTY;
2025 		goto done2;
2026 	}
2027 	tp = dev->si_drv1;
2028 
2029 	/* Try to attach the hook to the TTY. */
2030 	error = EBUSY;
2031 	tty_lock(tp);
2032 	MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
2033 	if (tp->t_flags & TF_HOOK)
2034 		goto done3;
2035 
2036 	tp->t_flags |= TF_HOOK;
2037 	tp->t_hook = th;
2038 	tp->t_hooksoftc = softc;
2039 	*rtp = tp;
2040 	error = 0;
2041 
2042 	/* Maybe we can switch into bypass mode now. */
2043 	ttydisc_optimize(tp);
2044 
2045 	/* Silently convert rint() calls to rint_bypass() when possible. */
2046 	if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2047 		th->th_rint = ttyhook_defrint;
2048 
2049 done3:	tty_unlock(tp);
2050 done2:	dev_relthread(dev, ref);
2051 done1:	fdrop(fp, curthread);
2052 	return (error);
2053 }
2054 
2055 void
2056 ttyhook_unregister(struct tty *tp)
2057 {
2058 
2059 	tty_lock_assert(tp, MA_OWNED);
2060 	MPASS(tp->t_flags & TF_HOOK);
2061 
2062 	/* Disconnect the hook. */
2063 	tp->t_flags &= ~TF_HOOK;
2064 	tp->t_hook = NULL;
2065 
2066 	/* Maybe we need to leave bypass mode. */
2067 	ttydisc_optimize(tp);
2068 
2069 	/* Maybe deallocate the TTY as well. */
2070 	tty_rel_free(tp);
2071 }
2072 
2073 /*
2074  * /dev/console handling.
2075  */
2076 
2077 static int
2078 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2079 {
2080 	struct tty *tp;
2081 
2082 	/* System has no console device. */
2083 	if (dev_console_filename == NULL)
2084 		return (ENXIO);
2085 
2086 	/* Look up corresponding TTY by device name. */
2087 	sx_slock(&tty_list_sx);
2088 	TAILQ_FOREACH(tp, &tty_list, t_list) {
2089 		if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2090 			dev_console->si_drv1 = tp;
2091 			break;
2092 		}
2093 	}
2094 	sx_sunlock(&tty_list_sx);
2095 
2096 	/* System console has no TTY associated. */
2097 	if (dev_console->si_drv1 == NULL)
2098 		return (ENXIO);
2099 
2100 	return (ttydev_open(dev, oflags, devtype, td));
2101 }
2102 
2103 static int
2104 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2105 {
2106 
2107 	log_console(uio);
2108 
2109 	return (ttydev_write(dev, uio, ioflag));
2110 }
2111 
2112 /*
2113  * /dev/console is a little different than normal TTY's.  When opened,
2114  * it determines which TTY to use.  When data gets written to it, it
2115  * will be logged in the kernel message buffer.
2116  */
2117 static struct cdevsw ttyconsdev_cdevsw = {
2118 	.d_version	= D_VERSION,
2119 	.d_open		= ttyconsdev_open,
2120 	.d_close	= ttydev_close,
2121 	.d_read		= ttydev_read,
2122 	.d_write	= ttyconsdev_write,
2123 	.d_ioctl	= ttydev_ioctl,
2124 	.d_kqfilter	= ttydev_kqfilter,
2125 	.d_poll		= ttydev_poll,
2126 	.d_mmap		= ttydev_mmap,
2127 	.d_name		= "ttyconsdev",
2128 	.d_flags	= D_TTY,
2129 };
2130 
2131 static void
2132 ttyconsdev_init(void *unused __unused)
2133 {
2134 
2135 	dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2136 	    NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2137 }
2138 
2139 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2140 
2141 void
2142 ttyconsdev_select(const char *name)
2143 {
2144 
2145 	dev_console_filename = name;
2146 }
2147 
2148 /*
2149  * Debugging routines.
2150  */
2151 
2152 #include "opt_ddb.h"
2153 #ifdef DDB
2154 #include <ddb/ddb.h>
2155 #include <ddb/db_sym.h>
2156 
2157 static const struct {
2158 	int flag;
2159 	char val;
2160 } ttystates[] = {
2161 #if 0
2162 	{ TF_NOPREFIX,		'N' },
2163 #endif
2164 	{ TF_INITLOCK,		'I' },
2165 	{ TF_CALLOUT,		'C' },
2166 
2167 	/* Keep these together -> 'Oi' and 'Oo'. */
2168 	{ TF_OPENED,		'O' },
2169 	{ TF_OPENED_IN,		'i' },
2170 	{ TF_OPENED_OUT,	'o' },
2171 	{ TF_OPENED_CONS,	'c' },
2172 
2173 	{ TF_GONE,		'G' },
2174 	{ TF_OPENCLOSE,		'B' },
2175 	{ TF_ASYNC,		'Y' },
2176 	{ TF_LITERAL,		'L' },
2177 
2178 	/* Keep these together -> 'Hi' and 'Ho'. */
2179 	{ TF_HIWAT,		'H' },
2180 	{ TF_HIWAT_IN,		'i' },
2181 	{ TF_HIWAT_OUT,		'o' },
2182 
2183 	{ TF_STOPPED,		'S' },
2184 	{ TF_EXCLUDE,		'X' },
2185 	{ TF_BYPASS,		'l' },
2186 	{ TF_ZOMBIE,		'Z' },
2187 	{ TF_HOOK,		's' },
2188 
2189 	/* Keep these together -> 'bi' and 'bo'. */
2190 	{ TF_BUSY,		'b' },
2191 	{ TF_BUSY_IN,		'i' },
2192 	{ TF_BUSY_OUT,		'o' },
2193 
2194 	{ 0,			'\0'},
2195 };
2196 
2197 #define	TTY_FLAG_BITS \
2198 	"\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2199 	"\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2200 	"\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2201 	"\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2202 	"\21HOOK\22BUSY_IN\23BUSY_OUT"
2203 
2204 #define DB_PRINTSYM(name, addr) \
2205 	db_printf("%s  " #name ": ", sep); \
2206 	db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2207 	db_printf("\n");
2208 
2209 static void
2210 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2211 {
2212 
2213 	db_printf("%sdevsw: ", sep);
2214 	db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2215 	db_printf(" (%p)\n", tsw);
2216 	DB_PRINTSYM(open, tsw->tsw_open);
2217 	DB_PRINTSYM(close, tsw->tsw_close);
2218 	DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2219 	DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2220 	DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2221 	DB_PRINTSYM(param, tsw->tsw_param);
2222 	DB_PRINTSYM(modem, tsw->tsw_modem);
2223 	DB_PRINTSYM(mmap, tsw->tsw_mmap);
2224 	DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2225 	DB_PRINTSYM(free, tsw->tsw_free);
2226 }
2227 
2228 static void
2229 _db_show_hooks(const char *sep, const struct ttyhook *th)
2230 {
2231 
2232 	db_printf("%shook: ", sep);
2233 	db_printsym((db_addr_t)th, DB_STGY_ANY);
2234 	db_printf(" (%p)\n", th);
2235 	if (th == NULL)
2236 		return;
2237 	DB_PRINTSYM(rint, th->th_rint);
2238 	DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2239 	DB_PRINTSYM(rint_done, th->th_rint_done);
2240 	DB_PRINTSYM(rint_poll, th->th_rint_poll);
2241 	DB_PRINTSYM(getc_inject, th->th_getc_inject);
2242 	DB_PRINTSYM(getc_capture, th->th_getc_capture);
2243 	DB_PRINTSYM(getc_poll, th->th_getc_poll);
2244 	DB_PRINTSYM(close, th->th_close);
2245 }
2246 
2247 static void
2248 _db_show_termios(const char *name, const struct termios *t)
2249 {
2250 
2251 	db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2252 	    "lflag 0x%x ispeed %u ospeed %u\n", name,
2253 	    t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2254 	    t->c_ispeed, t->c_ospeed);
2255 }
2256 
2257 /* DDB command to show TTY statistics. */
2258 DB_SHOW_COMMAND(tty, db_show_tty)
2259 {
2260 	struct tty *tp;
2261 
2262 	if (!have_addr) {
2263 		db_printf("usage: show tty <addr>\n");
2264 		return;
2265 	}
2266 	tp = (struct tty *)addr;
2267 
2268 	db_printf("%p: %s\n", tp, tty_devname(tp));
2269 	db_printf("\tmtx: %p\n", tp->t_mtx);
2270 	db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2271 	db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2272 
2273 	/* Buffering mechanisms. */
2274 	db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2275 	    "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2276 	    tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2277 	    tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2278 	db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2279 	    &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2280 	    tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2281 	db_printf("\tinlow: %zu\n", tp->t_inlow);
2282 	db_printf("\toutlow: %zu\n", tp->t_outlow);
2283 	_db_show_termios("\ttermios", &tp->t_termios);
2284 	db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2285 	    tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2286 	    tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2287 	db_printf("\tcolumn: %u\n", tp->t_column);
2288 	db_printf("\twritepos: %u\n", tp->t_writepos);
2289 	db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2290 
2291 	/* Init/lock-state devices. */
2292 	_db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2293 	_db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2294 	_db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2295 	_db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2296 
2297 	/* Hooks */
2298 	_db_show_devsw("\t", tp->t_devsw);
2299 	_db_show_hooks("\t", tp->t_hook);
2300 
2301 	/* Process info. */
2302 	db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2303 	    tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2304 	    tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2305 	db_printf("\tsession: %p", tp->t_session);
2306 	if (tp->t_session != NULL)
2307 	    db_printf(" count %u leader %p tty %p sid %d login %s",
2308 		tp->t_session->s_count, tp->t_session->s_leader,
2309 		tp->t_session->s_ttyp, tp->t_session->s_sid,
2310 		tp->t_session->s_login);
2311 	db_printf("\n");
2312 	db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2313 	db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2314 	db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2315 	db_printf("\tdev: %p\n", tp->t_dev);
2316 }
2317 
2318 /* DDB command to list TTYs. */
2319 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2320 {
2321 	struct tty *tp;
2322 	size_t isiz, osiz;
2323 	int i, j;
2324 
2325 	/* Make the output look like `pstat -t'. */
2326 	db_printf("PTR        ");
2327 #if defined(__LP64__)
2328 	db_printf("        ");
2329 #endif
2330 	db_printf("      LINE   INQ  CAN  LIN  LOW  OUTQ  USE  LOW   "
2331 	    "COL  SESS  PGID STATE\n");
2332 
2333 	TAILQ_FOREACH(tp, &tty_list, t_list) {
2334 		isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2335 		osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2336 
2337 		db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2338 		    "%5d ", tp, tty_devname(tp), isiz,
2339 		    tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2340 		    tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2341 		    isiz - tp->t_inlow, osiz,
2342 		    tp->t_outq.to_end - tp->t_outq.to_begin,
2343 		    osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2344 		    tp->t_session ? tp->t_session->s_sid : 0,
2345 		    tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2346 
2347 		/* Flag bits. */
2348 		for (i = j = 0; ttystates[i].flag; i++)
2349 			if (tp->t_flags & ttystates[i].flag) {
2350 				db_printf("%c", ttystates[i].val);
2351 				j++;
2352 			}
2353 		if (j == 0)
2354 			db_printf("-");
2355 		db_printf("\n");
2356 	}
2357 }
2358 #endif /* DDB */
2359