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