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