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