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