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