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