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