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