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
74static MALLOC_DEFINE(M_TTY, "tty", "tty device");
75
76static void tty_rel_free(struct tty *tp);
77
78static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
79static struct sx tty_list_sx;
80SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
81static unsigned int tty_list_count = 0;
82
83/* Character device of /dev/console. */
84static struct cdev	*dev_console;
85static 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
102static int  tty_drainwait = 5 * 60;
103SYSCTL_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 */
124static int
125tty_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
152static int
153tty_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
214static __inline int
215ttydev_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
229static void
230ttydev_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 */
273static int
274ttydev_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
366done:	tp->t_flags &= ~TF_OPENCLOSE;
367	cv_broadcast(&tp->t_dcdwait);
368	ttydev_leave(tp);
369
370	return (error);
371}
372
373static int
374ttydev_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
414static __inline int
415tty_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
423int
424tty_wait_background(struct tty *tp, struct thread *td, int sig)
425{
426	struct proc *p;
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	p = td->td_proc;
435	for (;;) {
436		pg = p->p_pgrp;
437		PGRP_LOCK(pg);
438		PROC_LOCK(p);
439
440		/*
441		 * pg may no longer be our process group.
442		 * Re-check after locking.
443		 */
444		if (p->p_pgrp != pg) {
445			PROC_UNLOCK(p);
446			PGRP_UNLOCK(pg);
447			continue;
448		}
449
450		/*
451		 * The process should only sleep, when:
452		 * - This terminal is the controlling terminal
453		 * - Its process group is not the foreground process
454		 *   group
455		 * - The parent process isn't waiting for the child to
456		 *   exit
457		 * - the signal to send to the process isn't masked
458		 */
459		if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
460			/* Allow the action to happen. */
461			PROC_UNLOCK(p);
462			PGRP_UNLOCK(pg);
463			return (0);
464		}
465
466		if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
467		    SIGISMEMBER(td->td_sigmask, sig)) {
468			/* Only allow them in write()/ioctl(). */
469			PROC_UNLOCK(p);
470			PGRP_UNLOCK(pg);
471			return (sig == SIGTTOU ? 0 : EIO);
472		}
473
474		if ((p->p_flag & P_PPWAIT) != 0 ||
475		    (pg->pg_flags & PGRP_ORPHANED) != 0) {
476			/* Don't allow the action to happen. */
477			PROC_UNLOCK(p);
478			PGRP_UNLOCK(pg);
479			return (EIO);
480		}
481		PROC_UNLOCK(p);
482
483		/*
484		 * Send the signal and sleep until we're the new
485		 * foreground process group.
486		 */
487		if (sig != 0) {
488			ksiginfo_init(&ksi);
489			ksi.ksi_code = SI_KERNEL;
490			ksi.ksi_signo = sig;
491			sig = 0;
492		}
493
494		pgsignal(pg, ksi.ksi_signo, 1, &ksi);
495		PGRP_UNLOCK(pg);
496
497		error = tty_wait(tp, &tp->t_bgwait);
498		if (error)
499			return (error);
500	}
501}
502
503static int
504ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
505{
506	struct tty *tp = dev->si_drv1;
507	int error;
508
509	error = ttydev_enter(tp);
510	if (error)
511		goto done;
512	error = ttydisc_read(tp, uio, ioflag);
513	tty_unlock(tp);
514
515	/*
516	 * The read() call should not throw an error when the device is
517	 * being destroyed. Silently convert it to an EOF.
518	 */
519done:	if (error == ENXIO)
520		error = 0;
521	return (error);
522}
523
524static int
525ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
526{
527	struct tty *tp = dev->si_drv1;
528	int defer, error;
529
530	error = ttydev_enter(tp);
531	if (error)
532		return (error);
533
534	if (tp->t_termios.c_lflag & TOSTOP) {
535		error = tty_wait_background(tp, curthread, SIGTTOU);
536		if (error)
537			goto done;
538	}
539
540	if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
541		/* Allow non-blocking writes to bypass serialization. */
542		error = ttydisc_write(tp, uio, ioflag);
543	} else {
544		/* Serialize write() calls. */
545		while (tp->t_flags & TF_BUSY_OUT) {
546			error = tty_wait(tp, &tp->t_outserwait);
547			if (error)
548				goto done;
549		}
550
551		tp->t_flags |= TF_BUSY_OUT;
552		defer = sigdeferstop(SIGDEFERSTOP_ERESTART);
553		error = ttydisc_write(tp, uio, ioflag);
554		sigallowstop(defer);
555		tp->t_flags &= ~TF_BUSY_OUT;
556		cv_signal(&tp->t_outserwait);
557	}
558
559done:	tty_unlock(tp);
560	return (error);
561}
562
563static int
564ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
565    struct thread *td)
566{
567	struct tty *tp = dev->si_drv1;
568	int error;
569
570	error = ttydev_enter(tp);
571	if (error)
572		return (error);
573
574	switch (cmd) {
575	case TIOCCBRK:
576	case TIOCCONS:
577	case TIOCDRAIN:
578	case TIOCEXCL:
579	case TIOCFLUSH:
580	case TIOCNXCL:
581	case TIOCSBRK:
582	case TIOCSCTTY:
583	case TIOCSETA:
584	case TIOCSETAF:
585	case TIOCSETAW:
586	case TIOCSPGRP:
587	case TIOCSTART:
588	case TIOCSTAT:
589	case TIOCSTI:
590	case TIOCSTOP:
591	case TIOCSWINSZ:
592#if 0
593	case TIOCSDRAINWAIT:
594	case TIOCSETD:
595#endif
596#ifdef COMPAT_43TTY
597	case  TIOCLBIC:
598	case  TIOCLBIS:
599	case  TIOCLSET:
600	case  TIOCSETC:
601	case OTIOCSETD:
602	case  TIOCSETN:
603	case  TIOCSETP:
604	case  TIOCSLTC:
605#endif /* COMPAT_43TTY */
606		/*
607		 * If the ioctl() causes the TTY to be modified, let it
608		 * wait in the background.
609		 */
610		error = tty_wait_background(tp, curthread, SIGTTOU);
611		if (error)
612			goto done;
613	}
614
615	if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
616		struct termios *old = &tp->t_termios;
617		struct termios *new = (struct termios *)data;
618		struct termios *lock = TTY_CALLOUT(tp, dev) ?
619		    &tp->t_termios_lock_out : &tp->t_termios_lock_in;
620		int cc;
621
622		/*
623		 * Lock state devices.  Just overwrite the values of the
624		 * commands that are currently in use.
625		 */
626		new->c_iflag = (old->c_iflag & lock->c_iflag) |
627		    (new->c_iflag & ~lock->c_iflag);
628		new->c_oflag = (old->c_oflag & lock->c_oflag) |
629		    (new->c_oflag & ~lock->c_oflag);
630		new->c_cflag = (old->c_cflag & lock->c_cflag) |
631		    (new->c_cflag & ~lock->c_cflag);
632		new->c_lflag = (old->c_lflag & lock->c_lflag) |
633		    (new->c_lflag & ~lock->c_lflag);
634		for (cc = 0; cc < NCCS; ++cc)
635			if (lock->c_cc[cc])
636				new->c_cc[cc] = old->c_cc[cc];
637		if (lock->c_ispeed)
638			new->c_ispeed = old->c_ispeed;
639		if (lock->c_ospeed)
640			new->c_ospeed = old->c_ospeed;
641	}
642
643	error = tty_ioctl(tp, cmd, data, fflag, td);
644done:	tty_unlock(tp);
645
646	return (error);
647}
648
649static int
650ttydev_poll(struct cdev *dev, int events, struct thread *td)
651{
652	struct tty *tp = dev->si_drv1;
653	int error, revents = 0;
654
655	error = ttydev_enter(tp);
656	if (error)
657		return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
658
659	if (events & (POLLIN|POLLRDNORM)) {
660		/* See if we can read something. */
661		if (ttydisc_read_poll(tp) > 0)
662			revents |= events & (POLLIN|POLLRDNORM);
663	}
664
665	if (tp->t_flags & TF_ZOMBIE) {
666		/* Hangup flag on zombie state. */
667		revents |= POLLHUP;
668	} else if (events & (POLLOUT|POLLWRNORM)) {
669		/* See if we can write something. */
670		if (ttydisc_write_poll(tp) > 0)
671			revents |= events & (POLLOUT|POLLWRNORM);
672	}
673
674	if (revents == 0) {
675		if (events & (POLLIN|POLLRDNORM))
676			selrecord(td, &tp->t_inpoll);
677		if (events & (POLLOUT|POLLWRNORM))
678			selrecord(td, &tp->t_outpoll);
679	}
680
681	tty_unlock(tp);
682
683	return (revents);
684}
685
686static int
687ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
688    int nprot, vm_memattr_t *memattr)
689{
690	struct tty *tp = dev->si_drv1;
691	int error;
692
693	/* Handle mmap() through the driver. */
694
695	error = ttydev_enter(tp);
696	if (error)
697		return (-1);
698	error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
699	tty_unlock(tp);
700
701	return (error);
702}
703
704/*
705 * kqueue support.
706 */
707
708static void
709tty_kqops_read_detach(struct knote *kn)
710{
711	struct tty *tp = kn->kn_hook;
712
713	knlist_remove(&tp->t_inpoll.si_note, kn, 0);
714}
715
716static int
717tty_kqops_read_event(struct knote *kn, long hint __unused)
718{
719	struct tty *tp = kn->kn_hook;
720
721	tty_assert_locked(tp);
722
723	if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
724		kn->kn_flags |= EV_EOF;
725		return (1);
726	} else {
727		kn->kn_data = ttydisc_read_poll(tp);
728		return (kn->kn_data > 0);
729	}
730}
731
732static void
733tty_kqops_write_detach(struct knote *kn)
734{
735	struct tty *tp = kn->kn_hook;
736
737	knlist_remove(&tp->t_outpoll.si_note, kn, 0);
738}
739
740static int
741tty_kqops_write_event(struct knote *kn, long hint __unused)
742{
743	struct tty *tp = kn->kn_hook;
744
745	tty_assert_locked(tp);
746
747	if (tty_gone(tp)) {
748		kn->kn_flags |= EV_EOF;
749		return (1);
750	} else {
751		kn->kn_data = ttydisc_write_poll(tp);
752		return (kn->kn_data > 0);
753	}
754}
755
756static struct filterops tty_kqops_read = {
757	.f_isfd = 1,
758	.f_detach = tty_kqops_read_detach,
759	.f_event = tty_kqops_read_event,
760};
761
762static struct filterops tty_kqops_write = {
763	.f_isfd = 1,
764	.f_detach = tty_kqops_write_detach,
765	.f_event = tty_kqops_write_event,
766};
767
768static int
769ttydev_kqfilter(struct cdev *dev, struct knote *kn)
770{
771	struct tty *tp = dev->si_drv1;
772	int error;
773
774	error = ttydev_enter(tp);
775	if (error)
776		return (error);
777
778	switch (kn->kn_filter) {
779	case EVFILT_READ:
780		kn->kn_hook = tp;
781		kn->kn_fop = &tty_kqops_read;
782		knlist_add(&tp->t_inpoll.si_note, kn, 1);
783		break;
784	case EVFILT_WRITE:
785		kn->kn_hook = tp;
786		kn->kn_fop = &tty_kqops_write;
787		knlist_add(&tp->t_outpoll.si_note, kn, 1);
788		break;
789	default:
790		error = EINVAL;
791		break;
792	}
793
794	tty_unlock(tp);
795	return (error);
796}
797
798static struct cdevsw ttydev_cdevsw = {
799	.d_version	= D_VERSION,
800	.d_open		= ttydev_open,
801	.d_close	= ttydev_close,
802	.d_read		= ttydev_read,
803	.d_write	= ttydev_write,
804	.d_ioctl	= ttydev_ioctl,
805	.d_kqfilter	= ttydev_kqfilter,
806	.d_poll		= ttydev_poll,
807	.d_mmap		= ttydev_mmap,
808	.d_name		= "ttydev",
809	.d_flags	= D_TTY,
810};
811
812/*
813 * Init/lock-state devices
814 */
815
816static int
817ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
818    struct thread *td)
819{
820	struct tty *tp;
821	int error;
822
823	tp = dev->si_drv1;
824	error = 0;
825	tty_lock(tp);
826	if (tty_gone(tp))
827		error = ENODEV;
828	tty_unlock(tp);
829
830	return (error);
831}
832
833static int
834ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
835    struct thread *td __unused)
836{
837
838	return (0);
839}
840
841static int
842ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
843    int ioflag __unused)
844{
845
846	return (ENODEV);
847}
848
849static int
850ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
851    struct thread *td)
852{
853	struct tty *tp = dev->si_drv1;
854	int error;
855
856	tty_lock(tp);
857	if (tty_gone(tp)) {
858		error = ENODEV;
859		goto done;
860	}
861
862	error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
863	if (error != ENOIOCTL)
864		goto done;
865	error = 0;
866
867	switch (cmd) {
868	case TIOCGETA:
869		/* Obtain terminal flags through tcgetattr(). */
870		*(struct termios*)data = *(struct termios*)dev->si_drv2;
871		break;
872	case TIOCSETA:
873		/* Set terminal flags through tcsetattr(). */
874		error = priv_check(td, PRIV_TTY_SETA);
875		if (error)
876			break;
877		*(struct termios*)dev->si_drv2 = *(struct termios*)data;
878		break;
879	case TIOCGETD:
880		*(int *)data = TTYDISC;
881		break;
882	case TIOCGWINSZ:
883		bzero(data, sizeof(struct winsize));
884		break;
885	default:
886		error = ENOTTY;
887	}
888
889done:	tty_unlock(tp);
890	return (error);
891}
892
893static struct cdevsw ttyil_cdevsw = {
894	.d_version	= D_VERSION,
895	.d_open		= ttyil_open,
896	.d_close	= ttyil_close,
897	.d_read		= ttyil_rdwr,
898	.d_write	= ttyil_rdwr,
899	.d_ioctl	= ttyil_ioctl,
900	.d_name		= "ttyil",
901	.d_flags	= D_TTY,
902};
903
904static void
905tty_init_termios(struct tty *tp)
906{
907	struct termios *t = &tp->t_termios_init_in;
908
909	t->c_cflag = TTYDEF_CFLAG;
910	t->c_iflag = TTYDEF_IFLAG;
911	t->c_lflag = TTYDEF_LFLAG;
912	t->c_oflag = TTYDEF_OFLAG;
913	t->c_ispeed = TTYDEF_SPEED;
914	t->c_ospeed = TTYDEF_SPEED;
915	memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
916
917	tp->t_termios_init_out = *t;
918}
919
920void
921tty_init_console(struct tty *tp, speed_t s)
922{
923	struct termios *ti = &tp->t_termios_init_in;
924	struct termios *to = &tp->t_termios_init_out;
925
926	if (s != 0) {
927		ti->c_ispeed = ti->c_ospeed = s;
928		to->c_ispeed = to->c_ospeed = s;
929	}
930
931	ti->c_cflag |= CLOCAL;
932	to->c_cflag |= CLOCAL;
933}
934
935/*
936 * Standard device routine implementations, mostly meant for
937 * pseudo-terminal device drivers. When a driver creates a new terminal
938 * device class, missing routines are patched.
939 */
940
941static int
942ttydevsw_defopen(struct tty *tp __unused)
943{
944
945	return (0);
946}
947
948static void
949ttydevsw_defclose(struct tty *tp __unused)
950{
951
952}
953
954static void
955ttydevsw_defoutwakeup(struct tty *tp __unused)
956{
957
958	panic("Terminal device has output, while not implemented");
959}
960
961static void
962ttydevsw_definwakeup(struct tty *tp __unused)
963{
964
965}
966
967static int
968ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
969    caddr_t data __unused, struct thread *td __unused)
970{
971
972	return (ENOIOCTL);
973}
974
975static int
976ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
977    u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
978{
979
980	return (ENOIOCTL);
981}
982
983static int
984ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
985{
986
987	/*
988	 * Allow the baud rate to be adjusted for pseudo-devices, but at
989	 * least restrict it to 115200 to prevent excessive buffer
990	 * usage.  Also disallow 0, to prevent foot shooting.
991	 */
992	if (t->c_ispeed < B50)
993		t->c_ispeed = B50;
994	else if (t->c_ispeed > B115200)
995		t->c_ispeed = B115200;
996	if (t->c_ospeed < B50)
997		t->c_ospeed = B50;
998	else if (t->c_ospeed > B115200)
999		t->c_ospeed = B115200;
1000	t->c_cflag |= CREAD;
1001
1002	return (0);
1003}
1004
1005static int
1006ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
1007    int sigoff __unused)
1008{
1009
1010	/* Simulate a carrier to make the TTY layer happy. */
1011	return (SER_DCD);
1012}
1013
1014static int
1015ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
1016    vm_paddr_t *paddr __unused, int nprot __unused,
1017    vm_memattr_t *memattr __unused)
1018{
1019
1020	return (-1);
1021}
1022
1023static void
1024ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
1025{
1026
1027}
1028
1029static void
1030ttydevsw_deffree(void *softc __unused)
1031{
1032
1033	panic("Terminal device freed without a free-handler");
1034}
1035
1036static bool
1037ttydevsw_defbusy(struct tty *tp __unused)
1038{
1039
1040	return (FALSE);
1041}
1042
1043/*
1044 * TTY allocation and deallocation. TTY devices can be deallocated when
1045 * the driver doesn't use it anymore, when the TTY isn't a session's
1046 * controlling TTY and when the device node isn't opened through devfs.
1047 */
1048
1049struct tty *
1050tty_alloc(struct ttydevsw *tsw, void *sc)
1051{
1052
1053	return (tty_alloc_mutex(tsw, sc, NULL));
1054}
1055
1056struct tty *
1057tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1058{
1059	struct tty *tp;
1060
1061	/* Make sure the driver defines all routines. */
1062#define PATCH_FUNC(x) do {				\
1063	if (tsw->tsw_ ## x == NULL)			\
1064		tsw->tsw_ ## x = ttydevsw_def ## x;	\
1065} while (0)
1066	PATCH_FUNC(open);
1067	PATCH_FUNC(close);
1068	PATCH_FUNC(outwakeup);
1069	PATCH_FUNC(inwakeup);
1070	PATCH_FUNC(ioctl);
1071	PATCH_FUNC(cioctl);
1072	PATCH_FUNC(param);
1073	PATCH_FUNC(modem);
1074	PATCH_FUNC(mmap);
1075	PATCH_FUNC(pktnotify);
1076	PATCH_FUNC(free);
1077	PATCH_FUNC(busy);
1078#undef PATCH_FUNC
1079
1080	tp = malloc(sizeof(struct tty) + TTY_PRBUF_SIZE, M_TTY,
1081	    M_WAITOK | M_ZERO);
1082	tp->t_prbufsz = TTY_PRBUF_SIZE;
1083	tp->t_devsw = tsw;
1084	tp->t_devswsoftc = sc;
1085	tp->t_flags = tsw->tsw_flags;
1086	tp->t_drainwait = tty_drainwait;
1087
1088	tty_init_termios(tp);
1089
1090	cv_init(&tp->t_inwait, "ttyin");
1091	cv_init(&tp->t_outwait, "ttyout");
1092	cv_init(&tp->t_outserwait, "ttyosr");
1093	cv_init(&tp->t_bgwait, "ttybg");
1094	cv_init(&tp->t_dcdwait, "ttydcd");
1095
1096	/* Allow drivers to use a custom mutex to lock the TTY. */
1097	if (mutex != NULL) {
1098		tp->t_mtx = mutex;
1099	} else {
1100		tp->t_mtx = &tp->t_mtxobj;
1101		mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1102	}
1103
1104	knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1105	knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1106
1107	return (tp);
1108}
1109
1110static void
1111tty_dealloc(void *arg)
1112{
1113	struct tty *tp = arg;
1114
1115	/*
1116	 * ttyydev_leave() usually frees the i/o queues earlier, but it is
1117	 * not always called between queue allocation and here.  The queues
1118	 * may be allocated by ioctls on a pty control device without the
1119	 * corresponding pty slave device ever being open, or after it is
1120	 * closed.
1121	 */
1122	ttyinq_free(&tp->t_inq);
1123	ttyoutq_free(&tp->t_outq);
1124	seldrain(&tp->t_inpoll);
1125	seldrain(&tp->t_outpoll);
1126	knlist_destroy(&tp->t_inpoll.si_note);
1127	knlist_destroy(&tp->t_outpoll.si_note);
1128
1129	cv_destroy(&tp->t_inwait);
1130	cv_destroy(&tp->t_outwait);
1131	cv_destroy(&tp->t_bgwait);
1132	cv_destroy(&tp->t_dcdwait);
1133	cv_destroy(&tp->t_outserwait);
1134
1135	if (tp->t_mtx == &tp->t_mtxobj)
1136		mtx_destroy(&tp->t_mtxobj);
1137	ttydevsw_free(tp);
1138	free(tp, M_TTY);
1139}
1140
1141static void
1142tty_rel_free(struct tty *tp)
1143{
1144	struct cdev *dev;
1145
1146	tty_assert_locked(tp);
1147
1148#define	TF_ACTIVITY	(TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1149	if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1150		/* TTY is still in use. */
1151		tty_unlock(tp);
1152		return;
1153	}
1154
1155	/* Stop asynchronous I/O. */
1156	funsetown(&tp->t_sigio);
1157
1158	/* TTY can be deallocated. */
1159	dev = tp->t_dev;
1160	tp->t_dev = NULL;
1161	tty_unlock(tp);
1162
1163	if (dev != NULL) {
1164		sx_xlock(&tty_list_sx);
1165		TAILQ_REMOVE(&tty_list, tp, t_list);
1166		tty_list_count--;
1167		sx_xunlock(&tty_list_sx);
1168		destroy_dev_sched_cb(dev, tty_dealloc, tp);
1169	}
1170}
1171
1172void
1173tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1174{
1175
1176	MPASS(tp->t_sessioncnt > 0);
1177	tty_assert_locked(tp);
1178
1179	if (tp->t_pgrp == pg)
1180		tp->t_pgrp = NULL;
1181
1182	tty_unlock(tp);
1183}
1184
1185void
1186tty_rel_sess(struct tty *tp, struct session *sess)
1187{
1188
1189	MPASS(tp->t_sessioncnt > 0);
1190
1191	/* Current session has left. */
1192	if (tp->t_session == sess) {
1193		tp->t_session = NULL;
1194		MPASS(tp->t_pgrp == NULL);
1195	}
1196	tp->t_sessioncnt--;
1197	tty_rel_free(tp);
1198}
1199
1200void
1201tty_rel_gone(struct tty *tp)
1202{
1203
1204	tty_assert_locked(tp);
1205	MPASS(!tty_gone(tp));
1206
1207	/* Simulate carrier removal. */
1208	ttydisc_modem(tp, 0);
1209
1210	/* Wake up all blocked threads. */
1211	tty_wakeup(tp, FREAD|FWRITE);
1212	cv_broadcast(&tp->t_bgwait);
1213	cv_broadcast(&tp->t_dcdwait);
1214
1215	tp->t_flags |= TF_GONE;
1216	tty_rel_free(tp);
1217}
1218
1219static int
1220tty_drop_ctty(struct tty *tp, struct proc *p)
1221{
1222	struct session *session;
1223	struct vnode *vp;
1224
1225	/*
1226	 * This looks terrible, but it's generally safe as long as the tty
1227	 * hasn't gone away while we had the lock dropped.  All of our sanity
1228	 * checking that this operation is OK happens after we've picked it back
1229	 * up, so other state changes are generally not fatal and the potential
1230	 * for this particular operation to happen out-of-order in a
1231	 * multithreaded scenario is likely a non-issue.
1232	 */
1233	tty_unlock(tp);
1234	sx_xlock(&proctree_lock);
1235	tty_lock(tp);
1236	if (tty_gone(tp)) {
1237		sx_xunlock(&proctree_lock);
1238		return (ENODEV);
1239	}
1240
1241	/*
1242	 * If the session doesn't have a controlling TTY, or if we weren't
1243	 * invoked on the controlling TTY, we'll return ENOIOCTL as we've
1244	 * historically done.
1245	 */
1246	session = p->p_session;
1247	if (session->s_ttyp == NULL || session->s_ttyp != tp) {
1248		sx_xunlock(&proctree_lock);
1249		return (ENOTTY);
1250	}
1251
1252	if (!SESS_LEADER(p)) {
1253		sx_xunlock(&proctree_lock);
1254		return (EPERM);
1255	}
1256
1257	PROC_LOCK(p);
1258	SESS_LOCK(session);
1259	vp = session->s_ttyvp;
1260	session->s_ttyp = NULL;
1261	session->s_ttyvp = NULL;
1262	session->s_ttydp = NULL;
1263	SESS_UNLOCK(session);
1264
1265	tp->t_sessioncnt--;
1266	p->p_flag &= ~P_CONTROLT;
1267	PROC_UNLOCK(p);
1268	sx_xunlock(&proctree_lock);
1269
1270	/*
1271	 * If we did have a vnode, release our reference.  Ordinarily we manage
1272	 * these at the devfs layer, but we can't necessarily know that we were
1273	 * invoked on the vnode referenced in the session (i.e. the vnode we
1274	 * hold a reference to).  We explicitly don't check VBAD/VIRF_DOOMED here
1275	 * to avoid a vnode leak -- in circumstances elsewhere where we'd hit a
1276	 * VIRF_DOOMED vnode, release has been deferred until the controlling TTY
1277	 * is either changed or released.
1278	 */
1279	if (vp != NULL)
1280		devfs_ctty_unref(vp);
1281	return (0);
1282}
1283
1284/*
1285 * Exposing information about current TTY's through sysctl
1286 */
1287
1288static void
1289tty_to_xtty(struct tty *tp, struct xtty *xt)
1290{
1291
1292	tty_assert_locked(tp);
1293
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
1309static int
1310sysctl_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
1339SYSCTL_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
1347int
1348tty_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
1461fail:
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
1479void
1480tty_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
1506void
1507tty_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
1530void
1531tty_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
1549int
1550tty_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
1571int
1572tty_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
1593void
1594tty_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
1617void
1618tty_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
1627static int
1628tty_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
1924			/*
1925			 * XXX: constty should really need to be locked!
1926			 * XXX: allow disconnected constty's to be stolen!
1927			 */
1928
1929			if (constty == tp)
1930				return (0);
1931			if (constty != NULL)
1932				return (EBUSY);
1933
1934			tty_unlock(tp);
1935			constty_set(tp);
1936			tty_lock(tp);
1937		} else if (constty == tp) {
1938			constty_clear();
1939		}
1940		return (0);
1941	case TIOCGWINSZ:
1942		/* Obtain window size. */
1943		*(struct winsize*)data = tp->t_winsize;
1944		return (0);
1945	case TIOCSWINSZ:
1946		/* Set window size. */
1947		tty_set_winsize(tp, data);
1948		return (0);
1949	case TIOCEXCL:
1950		tp->t_flags |= TF_EXCLUDE;
1951		return (0);
1952	case TIOCNXCL:
1953		tp->t_flags &= ~TF_EXCLUDE;
1954		return (0);
1955	case TIOCSTOP:
1956		tp->t_flags |= TF_STOPPED;
1957		ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1958		return (0);
1959	case TIOCSTART:
1960		tp->t_flags &= ~TF_STOPPED;
1961		tp->t_termios.c_lflag &= ~FLUSHO;
1962		ttydevsw_outwakeup(tp);
1963		ttydevsw_pktnotify(tp, TIOCPKT_START);
1964		return (0);
1965	case TIOCSTAT:
1966		tty_info(tp);
1967		return (0);
1968	case TIOCSTI:
1969		if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1970			return (EPERM);
1971		if (!tty_is_ctty(tp, td->td_proc) &&
1972		    priv_check(td, PRIV_TTY_STI))
1973			return (EACCES);
1974		ttydisc_rint(tp, *(char *)data, 0);
1975		ttydisc_rint_done(tp);
1976		return (0);
1977	}
1978
1979#ifdef COMPAT_43TTY
1980	return tty_ioctl_compat(tp, cmd, data, fflag, td);
1981#else /* !COMPAT_43TTY */
1982	return (ENOIOCTL);
1983#endif /* COMPAT_43TTY */
1984}
1985
1986int
1987tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1988{
1989	int error;
1990
1991	tty_assert_locked(tp);
1992
1993	if (tty_gone(tp))
1994		return (ENXIO);
1995
1996	error = ttydevsw_ioctl(tp, cmd, data, td);
1997	if (error == ENOIOCTL)
1998		error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1999
2000	return (error);
2001}
2002
2003dev_t
2004tty_udev(struct tty *tp)
2005{
2006
2007	if (tp->t_dev)
2008		return (dev2udev(tp->t_dev));
2009	else
2010		return (NODEV);
2011}
2012
2013int
2014tty_checkoutq(struct tty *tp)
2015{
2016
2017	/* 256 bytes should be enough to print a log message. */
2018	return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
2019}
2020
2021void
2022tty_hiwat_in_block(struct tty *tp)
2023{
2024
2025	if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
2026	    tp->t_termios.c_iflag & IXOFF &&
2027	    tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
2028		/*
2029		 * Input flow control. Only enter the high watermark when we
2030		 * can successfully store the VSTOP character.
2031		 */
2032		if (ttyoutq_write_nofrag(&tp->t_outq,
2033		    &tp->t_termios.c_cc[VSTOP], 1) == 0)
2034			tp->t_flags |= TF_HIWAT_IN;
2035	} else {
2036		/* No input flow control. */
2037		tp->t_flags |= TF_HIWAT_IN;
2038	}
2039}
2040
2041void
2042tty_hiwat_in_unblock(struct tty *tp)
2043{
2044
2045	if (tp->t_flags & TF_HIWAT_IN &&
2046	    tp->t_termios.c_iflag & IXOFF &&
2047	    tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
2048		/*
2049		 * Input flow control. Only leave the high watermark when we
2050		 * can successfully store the VSTART character.
2051		 */
2052		if (ttyoutq_write_nofrag(&tp->t_outq,
2053		    &tp->t_termios.c_cc[VSTART], 1) == 0)
2054			tp->t_flags &= ~TF_HIWAT_IN;
2055	} else {
2056		/* No input flow control. */
2057		tp->t_flags &= ~TF_HIWAT_IN;
2058	}
2059
2060	if (!tty_gone(tp))
2061		ttydevsw_inwakeup(tp);
2062}
2063
2064/*
2065 * TTY hooks interface.
2066 */
2067
2068static int
2069ttyhook_defrint(struct tty *tp, char c, int flags)
2070{
2071
2072	if (ttyhook_rint_bypass(tp, &c, 1) != 1)
2073		return (-1);
2074
2075	return (0);
2076}
2077
2078int
2079ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
2080    void *softc)
2081{
2082	struct tty *tp;
2083	struct file *fp;
2084	struct cdev *dev;
2085	struct cdevsw *cdp;
2086	struct filedesc *fdp;
2087	cap_rights_t rights;
2088	int error, ref;
2089
2090	/* Validate the file descriptor. */
2091	fdp = p->p_fd;
2092	error = fget_unlocked(fdp, fd, cap_rights_init_one(&rights, CAP_TTYHOOK),
2093	    &fp);
2094	if (error != 0)
2095		return (error);
2096	if (fp->f_ops == &badfileops) {
2097		error = EBADF;
2098		goto done1;
2099	}
2100
2101	/*
2102	 * Make sure the vnode is bound to a character device.
2103	 * Unlocked check for the vnode type is ok there, because we
2104	 * only shall prevent calling devvn_refthread on the file that
2105	 * never has been opened over a character device.
2106	 */
2107	if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
2108		error = EINVAL;
2109		goto done1;
2110	}
2111
2112	/* Make sure it is a TTY. */
2113	cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
2114	if (cdp == NULL) {
2115		error = ENXIO;
2116		goto done1;
2117	}
2118	if (dev != fp->f_data) {
2119		error = ENXIO;
2120		goto done2;
2121	}
2122	if (cdp != &ttydev_cdevsw) {
2123		error = ENOTTY;
2124		goto done2;
2125	}
2126	tp = dev->si_drv1;
2127
2128	/* Try to attach the hook to the TTY. */
2129	error = EBUSY;
2130	tty_lock(tp);
2131	MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
2132	if (tp->t_flags & TF_HOOK)
2133		goto done3;
2134
2135	tp->t_flags |= TF_HOOK;
2136	tp->t_hook = th;
2137	tp->t_hooksoftc = softc;
2138	*rtp = tp;
2139	error = 0;
2140
2141	/* Maybe we can switch into bypass mode now. */
2142	ttydisc_optimize(tp);
2143
2144	/* Silently convert rint() calls to rint_bypass() when possible. */
2145	if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2146		th->th_rint = ttyhook_defrint;
2147
2148done3:	tty_unlock(tp);
2149done2:	dev_relthread(dev, ref);
2150done1:	fdrop(fp, curthread);
2151	return (error);
2152}
2153
2154void
2155ttyhook_unregister(struct tty *tp)
2156{
2157
2158	tty_assert_locked(tp);
2159	MPASS(tp->t_flags & TF_HOOK);
2160
2161	/* Disconnect the hook. */
2162	tp->t_flags &= ~TF_HOOK;
2163	tp->t_hook = NULL;
2164
2165	/* Maybe we need to leave bypass mode. */
2166	ttydisc_optimize(tp);
2167
2168	/* Maybe deallocate the TTY as well. */
2169	tty_rel_free(tp);
2170}
2171
2172/*
2173 * /dev/console handling.
2174 */
2175
2176static int
2177ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2178{
2179	struct tty *tp;
2180
2181	/* System has no console device. */
2182	if (dev_console_filename == NULL)
2183		return (ENXIO);
2184
2185	/* Look up corresponding TTY by device name. */
2186	sx_slock(&tty_list_sx);
2187	TAILQ_FOREACH(tp, &tty_list, t_list) {
2188		if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2189			dev_console->si_drv1 = tp;
2190			break;
2191		}
2192	}
2193	sx_sunlock(&tty_list_sx);
2194
2195	/* System console has no TTY associated. */
2196	if (dev_console->si_drv1 == NULL)
2197		return (ENXIO);
2198
2199	return (ttydev_open(dev, oflags, devtype, td));
2200}
2201
2202static int
2203ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2204{
2205
2206	log_console(uio);
2207
2208	return (ttydev_write(dev, uio, ioflag));
2209}
2210
2211/*
2212 * /dev/console is a little different than normal TTY's.  When opened,
2213 * it determines which TTY to use.  When data gets written to it, it
2214 * will be logged in the kernel message buffer.
2215 */
2216static struct cdevsw ttyconsdev_cdevsw = {
2217	.d_version	= D_VERSION,
2218	.d_open		= ttyconsdev_open,
2219	.d_close	= ttydev_close,
2220	.d_read		= ttydev_read,
2221	.d_write	= ttyconsdev_write,
2222	.d_ioctl	= ttydev_ioctl,
2223	.d_kqfilter	= ttydev_kqfilter,
2224	.d_poll		= ttydev_poll,
2225	.d_mmap		= ttydev_mmap,
2226	.d_name		= "ttyconsdev",
2227	.d_flags	= D_TTY,
2228};
2229
2230static void
2231ttyconsdev_init(void *unused __unused)
2232{
2233
2234	dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2235	    NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2236}
2237
2238SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2239
2240void
2241ttyconsdev_select(const char *name)
2242{
2243
2244	dev_console_filename = name;
2245}
2246
2247/*
2248 * Debugging routines.
2249 */
2250
2251#include "opt_ddb.h"
2252#ifdef DDB
2253#include <ddb/ddb.h>
2254#include <ddb/db_sym.h>
2255
2256static const struct {
2257	int flag;
2258	char val;
2259} ttystates[] = {
2260#if 0
2261	{ TF_NOPREFIX,		'N' },
2262#endif
2263	{ TF_INITLOCK,		'I' },
2264	{ TF_CALLOUT,		'C' },
2265
2266	/* Keep these together -> 'Oi' and 'Oo'. */
2267	{ TF_OPENED,		'O' },
2268	{ TF_OPENED_IN,		'i' },
2269	{ TF_OPENED_OUT,	'o' },
2270	{ TF_OPENED_CONS,	'c' },
2271
2272	{ TF_GONE,		'G' },
2273	{ TF_OPENCLOSE,		'B' },
2274	{ TF_ASYNC,		'Y' },
2275	{ TF_LITERAL,		'L' },
2276
2277	/* Keep these together -> 'Hi' and 'Ho'. */
2278	{ TF_HIWAT,		'H' },
2279	{ TF_HIWAT_IN,		'i' },
2280	{ TF_HIWAT_OUT,		'o' },
2281
2282	{ TF_STOPPED,		'S' },
2283	{ TF_EXCLUDE,		'X' },
2284	{ TF_BYPASS,		'l' },
2285	{ TF_ZOMBIE,		'Z' },
2286	{ TF_HOOK,		's' },
2287
2288	/* Keep these together -> 'bi' and 'bo'. */
2289	{ TF_BUSY,		'b' },
2290	{ TF_BUSY_IN,		'i' },
2291	{ TF_BUSY_OUT,		'o' },
2292
2293	{ 0,			'\0'},
2294};
2295
2296#define	TTY_FLAG_BITS \
2297	"\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2298	"\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2299	"\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2300	"\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2301	"\21HOOK\22BUSY_IN\23BUSY_OUT"
2302
2303#define DB_PRINTSYM(name, addr) \
2304	db_printf("%s  " #name ": ", sep); \
2305	db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2306	db_printf("\n");
2307
2308static void
2309_db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2310{
2311
2312	db_printf("%sdevsw: ", sep);
2313	db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2314	db_printf(" (%p)\n", tsw);
2315	DB_PRINTSYM(open, tsw->tsw_open);
2316	DB_PRINTSYM(close, tsw->tsw_close);
2317	DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2318	DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2319	DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2320	DB_PRINTSYM(param, tsw->tsw_param);
2321	DB_PRINTSYM(modem, tsw->tsw_modem);
2322	DB_PRINTSYM(mmap, tsw->tsw_mmap);
2323	DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2324	DB_PRINTSYM(free, tsw->tsw_free);
2325}
2326
2327static void
2328_db_show_hooks(const char *sep, const struct ttyhook *th)
2329{
2330
2331	db_printf("%shook: ", sep);
2332	db_printsym((db_addr_t)th, DB_STGY_ANY);
2333	db_printf(" (%p)\n", th);
2334	if (th == NULL)
2335		return;
2336	DB_PRINTSYM(rint, th->th_rint);
2337	DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2338	DB_PRINTSYM(rint_done, th->th_rint_done);
2339	DB_PRINTSYM(rint_poll, th->th_rint_poll);
2340	DB_PRINTSYM(getc_inject, th->th_getc_inject);
2341	DB_PRINTSYM(getc_capture, th->th_getc_capture);
2342	DB_PRINTSYM(getc_poll, th->th_getc_poll);
2343	DB_PRINTSYM(close, th->th_close);
2344}
2345
2346static void
2347_db_show_termios(const char *name, const struct termios *t)
2348{
2349
2350	db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2351	    "lflag 0x%x ispeed %u ospeed %u\n", name,
2352	    t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2353	    t->c_ispeed, t->c_ospeed);
2354}
2355
2356/* DDB command to show TTY statistics. */
2357DB_SHOW_COMMAND(tty, db_show_tty)
2358{
2359	struct tty *tp;
2360
2361	if (!have_addr) {
2362		db_printf("usage: show tty <addr>\n");
2363		return;
2364	}
2365	tp = (struct tty *)addr;
2366
2367	db_printf("%p: %s\n", tp, tty_devname(tp));
2368	db_printf("\tmtx: %p\n", tp->t_mtx);
2369	db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2370	db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2371
2372	/* Buffering mechanisms. */
2373	db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2374	    "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2375	    tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2376	    tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2377	db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2378	    &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2379	    tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2380	db_printf("\tinlow: %zu\n", tp->t_inlow);
2381	db_printf("\toutlow: %zu\n", tp->t_outlow);
2382	_db_show_termios("\ttermios", &tp->t_termios);
2383	db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2384	    tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2385	    tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2386	db_printf("\tcolumn: %u\n", tp->t_column);
2387	db_printf("\twritepos: %u\n", tp->t_writepos);
2388	db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2389
2390	/* Init/lock-state devices. */
2391	_db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2392	_db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2393	_db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2394	_db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2395
2396	/* Hooks */
2397	_db_show_devsw("\t", tp->t_devsw);
2398	_db_show_hooks("\t", tp->t_hook);
2399
2400	/* Process info. */
2401	db_printf("\tpgrp: %p gid %d\n", tp->t_pgrp,
2402	    tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2403	db_printf("\tsession: %p", tp->t_session);
2404	if (tp->t_session != NULL)
2405	    db_printf(" count %u leader %p tty %p sid %d login %s",
2406		tp->t_session->s_count, tp->t_session->s_leader,
2407		tp->t_session->s_ttyp, tp->t_session->s_sid,
2408		tp->t_session->s_login);
2409	db_printf("\n");
2410	db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2411	db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2412	db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2413	db_printf("\tdev: %p\n", tp->t_dev);
2414}
2415
2416/* DDB command to list TTYs. */
2417DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2418{
2419	struct tty *tp;
2420	size_t isiz, osiz;
2421	int i, j;
2422
2423	/* Make the output look like `pstat -t'. */
2424	db_printf("PTR        ");
2425#if defined(__LP64__)
2426	db_printf("        ");
2427#endif
2428	db_printf("      LINE   INQ  CAN  LIN  LOW  OUTQ  USE  LOW   "
2429	    "COL  SESS  PGID STATE\n");
2430
2431	TAILQ_FOREACH(tp, &tty_list, t_list) {
2432		isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2433		osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2434
2435		db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2436		    "%5d ", tp, tty_devname(tp), isiz,
2437		    tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2438		    tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2439		    isiz - tp->t_inlow, osiz,
2440		    tp->t_outq.to_end - tp->t_outq.to_begin,
2441		    osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2442		    tp->t_session ? tp->t_session->s_sid : 0,
2443		    tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2444
2445		/* Flag bits. */
2446		for (i = j = 0; ttystates[i].flag; i++)
2447			if (tp->t_flags & ttystates[i].flag) {
2448				db_printf("%c", ttystates[i].val);
2449				j++;
2450			}
2451		if (j == 0)
2452			db_printf("-");
2453		db_printf("\n");
2454	}
2455}
2456#endif /* DDB */
2457