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