xref: /netbsd-src/sys/kern/tty.c (revision df0caa2637da0538ecdf6b878c4d08e684b43d8f)
1 /*	$NetBSD: tty.c,v 1.173 2005/06/11 22:42:24 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1986, 1990, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)tty.c	8.13 (Berkeley) 1/9/95
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: tty.c,v 1.173 2005/06/11 22:42:24 christos Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/ioctl.h>
45 #include <sys/proc.h>
46 #define	TTYDEFCHARS
47 #include <sys/tty.h>
48 #undef	TTYDEFCHARS
49 #include <sys/file.h>
50 #include <sys/conf.h>
51 #include <sys/dkstat.h>
52 #include <sys/uio.h>
53 #include <sys/kernel.h>
54 #include <sys/vnode.h>
55 #include <sys/syslog.h>
56 #include <sys/malloc.h>
57 #include <sys/pool.h>
58 #include <sys/signalvar.h>
59 #include <sys/resourcevar.h>
60 #include <sys/poll.h>
61 #include <sys/kprintf.h>
62 #include <sys/namei.h>
63 #include <sys/sysctl.h>
64 
65 #include <machine/stdarg.h>
66 
67 static int	ttnread(struct tty *);
68 static void	ttyblock(struct tty *);
69 static void	ttyecho(int, struct tty *);
70 static void	ttyrubo(struct tty *, int);
71 static void	ttyprintf_nolock(struct tty *, const char *fmt, ...)
72     __attribute__((__format__(__printf__,2,3)));
73 static int	proc_compare(struct proc *, struct proc *);
74 
75 /* Symbolic sleep message strings. */
76 const char	ttclos[] = "ttycls";
77 const char	ttopen[] = "ttyopn";
78 const char	ttybg[] = "ttybg";
79 const char	ttyin[] = "ttyin";
80 const char	ttyout[] = "ttyout";
81 
82 /*
83  * Used to determine whether we still have a connection.  This is true in
84  * one of 3 cases:
85  * 1) We have carrier.
86  * 2) It's a locally attached terminal, and we are therefore ignoring carrier.
87  * 3) We're using a flow control mechanism that overloads the carrier signal.
88  */
89 #define	CONNECTED(tp)	(ISSET(tp->t_state, TS_CARR_ON) ||	\
90 			 ISSET(tp->t_cflag, CLOCAL | MDMBUF))
91 
92 /*
93  * Table with character classes and parity. The 8th bit indicates parity,
94  * the 7th bit indicates the character is an alphameric or underscore (for
95  * ALTWERASE), and the low 6 bits indicate delay type.  If the low 6 bits
96  * are 0 then the character needs no special processing on output; classes
97  * other than 0 might be translated or (not currently) require delays.
98  */
99 #define	E	0x00	/* Even parity. */
100 #define	O	0x80	/* Odd parity. */
101 #define	PARITY(c)	(char_type[c] & O)
102 
103 #define	ALPHA	0x40	/* Alpha or underscore. */
104 #define	ISALPHA(c)	(char_type[(c) & TTY_CHARMASK] & ALPHA)
105 
106 #define	CCLASSMASK	0x3f
107 #define	CCLASS(c)	(char_type[c] & CCLASSMASK)
108 
109 #define	BS	BACKSPACE
110 #define	CC	CONTROL
111 #define	CR	RETURN
112 #define	NA	ORDINARY | ALPHA
113 #define	NL	NEWLINE
114 #define	NO	ORDINARY
115 #define	TB	TAB
116 #define	VT	VTAB
117 
118 unsigned char const char_type[] = {
119 	E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC,	/* nul - bel */
120 	O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC,	/* bs - si */
121 	O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC,	/* dle - etb */
122 	E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC,	/* can - us */
123 	O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO,	/* sp - ' */
124 	E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO,	/* ( - / */
125 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* 0 - 7 */
126 	O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO,	/* 8 - ? */
127 	O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA,	/* @ - G */
128 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* H - O */
129 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* P - W */
130 	O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA,	/* X - _ */
131 	E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* ` - g */
132 	O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA,	/* h - o */
133 	O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA,	/* p - w */
134 	E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC,	/* x - del */
135 	/*
136 	 * Meta chars; should be settable per character set;
137 	 * for now, treat them all as normal characters.
138 	 */
139 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
140 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
141 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
142 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
143 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
144 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
145 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
146 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
147 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
148 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
149 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
150 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
151 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
152 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
153 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
154 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
155 };
156 #undef	BS
157 #undef	CC
158 #undef	CR
159 #undef	NA
160 #undef	NL
161 #undef	NO
162 #undef	TB
163 #undef	VT
164 
165 /* Macros to clear/set/test flags. */
166 #define	SET(t, f)	(t) |= (f)
167 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
168 #define	ISSET(t, f)	((t) & (f))
169 
170 struct simplelock ttylist_slock = SIMPLELOCK_INITIALIZER;
171 struct ttylist_head ttylist = TAILQ_HEAD_INITIALIZER(ttylist);
172 int tty_count;
173 
174 POOL_INIT(tty_pool, sizeof(struct tty), 0, 0, 0, "ttypl",
175     &pool_allocator_nointr);
176 
177 u_int64_t tk_cancc;
178 u_int64_t tk_nin;
179 u_int64_t tk_nout;
180 u_int64_t tk_rawcc;
181 
182 SYSCTL_SETUP(sysctl_kern_tkstat_setup, "sysctl kern.tkstat subtree setup")
183 {
184 
185 	sysctl_createv(clog, 0, NULL, NULL,
186 		       CTLFLAG_PERMANENT,
187 		       CTLTYPE_NODE, "kern", NULL,
188 		       NULL, 0, NULL, 0,
189 		       CTL_KERN, CTL_EOL);
190 	sysctl_createv(clog, 0, NULL, NULL,
191 		       CTLFLAG_PERMANENT,
192 		       CTLTYPE_NODE, "tkstat",
193 		       SYSCTL_DESCR("Number of characters sent and and "
194 				    "received on ttys"),
195 		       NULL, 0, NULL, 0,
196 		       CTL_KERN, KERN_TKSTAT, CTL_EOL);
197 
198 	sysctl_createv(clog, 0, NULL, NULL,
199 		       CTLFLAG_PERMANENT,
200 		       CTLTYPE_QUAD, "nin",
201 		       SYSCTL_DESCR("Total number of tty input characters"),
202 		       NULL, 0, &tk_nin, 0,
203 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_NIN, CTL_EOL);
204 	sysctl_createv(clog, 0, NULL, NULL,
205 		       CTLFLAG_PERMANENT,
206 		       CTLTYPE_QUAD, "nout",
207 		       SYSCTL_DESCR("Total number of tty output characters"),
208 		       NULL, 0, &tk_nout, 0,
209 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_NOUT, CTL_EOL);
210 	sysctl_createv(clog, 0, NULL, NULL,
211 		       CTLFLAG_PERMANENT,
212 		       CTLTYPE_QUAD, "cancc",
213 		       SYSCTL_DESCR("Number of canonical tty input characters"),
214 		       NULL, 0, &tk_cancc, 0,
215 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_CANCC, CTL_EOL);
216 	sysctl_createv(clog, 0, NULL, NULL,
217 		       CTLFLAG_PERMANENT,
218 		       CTLTYPE_QUAD, "rawcc",
219 		       SYSCTL_DESCR("Number of raw tty input characters"),
220 		       NULL, 0, &tk_rawcc, 0,
221 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_RAWCC, CTL_EOL);
222 }
223 
224 int
225 ttyopen(struct tty *tp, int dialout, int nonblock)
226 {
227 	int	s, error;
228 
229 	error = 0;
230 
231 	s = spltty();
232 	TTY_LOCK(tp);
233 
234 	if (dialout) {
235 		/*
236 		 * If the device is already open for non-dialout, fail.
237 		 * Otherwise, set TS_DIALOUT to block any pending non-dialout
238 		 * opens.
239 		 */
240 		if (ISSET(tp->t_state, TS_ISOPEN) &&
241 		    !ISSET(tp->t_state, TS_DIALOUT)) {
242 			error = EBUSY;
243 			goto out;
244 		}
245 		SET(tp->t_state, TS_DIALOUT);
246 	} else {
247 		if (!nonblock) {
248 			/*
249 			 * Wait for carrier.  Also wait for any dialout
250 			 * processes to close the tty first.
251 			 */
252 			while (ISSET(tp->t_state, TS_DIALOUT) ||
253 			       !CONNECTED(tp)) {
254 				tp->t_wopen++;
255 				error = ttysleep(tp, &tp->t_rawq,
256 				    TTIPRI | PCATCH, ttopen, 0);
257 				tp->t_wopen--;
258 				if (error)
259 					goto out;
260 			}
261 		} else {
262 			/*
263 			 * Don't allow a non-blocking non-dialout open if the
264 			 * device is already open for dialout.
265 			 */
266 			if (ISSET(tp->t_state, TS_DIALOUT)) {
267 				error = EBUSY;
268 				goto out;
269 			}
270 		}
271 	}
272 
273 out:
274 	TTY_UNLOCK(tp);
275 	splx(s);
276 	return (error);
277 }
278 
279 /*
280  * Initial open of tty, or (re)entry to standard tty line discipline.
281  */
282 int
283 ttylopen(dev_t device, struct tty *tp)
284 {
285 	int	s;
286 
287 	s = spltty();
288 	TTY_LOCK(tp);
289 	tp->t_dev = device;
290 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
291 		SET(tp->t_state, TS_ISOPEN);
292 		memset(&tp->t_winsize, 0, sizeof(tp->t_winsize));
293 #ifdef COMPAT_OLDTTY
294 		tp->t_flags = 0;
295 #endif
296 	}
297 	TTY_UNLOCK(tp);
298 	splx(s);
299 	return (0);
300 }
301 
302 /*
303  * Handle close() on a tty line: flush and set to initial state,
304  * bumping generation number so that pending read/write calls
305  * can detect recycling of the tty.
306  */
307 int
308 ttyclose(struct tty *tp)
309 {
310 	extern struct tty *constty;	/* Temporary virtual console. */
311 	int s;
312 
313 	s = spltty();
314 	TTY_LOCK(tp);
315 
316 	if (constty == tp)
317 		constty = NULL;
318 
319 	ttyflush(tp, FREAD | FWRITE);
320 
321 	tp->t_gen++;
322 	tp->t_pgrp = NULL;
323 	if (tp->t_session != NULL) {
324 		SESSRELE(tp->t_session);
325 		tp->t_session = NULL;
326 	}
327 	tp->t_state = 0;
328 
329 	TTY_UNLOCK(tp);
330 	splx(s);
331 	return (0);
332 }
333 
334 #define	FLUSHQ(q) {							\
335 	if ((q)->c_cc)							\
336 		ndflush(q, (q)->c_cc);					\
337 }
338 
339 /*
340  * This macro is used in canonical mode input processing, where a read
341  * request shall not return unless a 'line delimiter' ('\n') or 'break'
342  * (EOF, EOL, EOL2) character (or a signal) has been received. As EOL2
343  * is an extension to the POSIX.1 defined set of special characters,
344  * recognize it only if IEXTEN is set in the set of local flags.
345  */
346 #define	TTBREAKC(c, lflg)						\
347 	((c) == '\n' || (((c) == cc[VEOF] || (c) == cc[VEOL] ||		\
348 	((c) == cc[VEOL2] && ISSET(lflg, IEXTEN))) && (c) != _POSIX_VDISABLE))
349 
350 
351 
352 /*
353  * ttyinput() helper.
354  * Call at spltty() and with the tty slock held.
355  */
356 static int
357 ttyinput_wlock(int c, struct tty *tp)
358 {
359 	const struct cdevsw *cdev;
360 	int	iflag, lflag, i, error;
361 	u_char	*cc;
362 
363 	/*
364 	 * If input is pending take it first.
365 	 */
366 	lflag = tp->t_lflag;
367 	if (ISSET(lflag, PENDIN))
368 		ttypend(tp);
369 	/*
370 	 * Gather stats.
371 	 */
372 	if (ISSET(lflag, ICANON)) {
373 		++tk_cancc;
374 		++tp->t_cancc;
375 	} else {
376 		++tk_rawcc;
377 		++tp->t_rawcc;
378 	}
379 	++tk_nin;
380 
381 	cc = tp->t_cc;
382 
383 	/*
384 	 * Handle exceptional conditions (break, parity, framing).
385 	 */
386 	iflag = tp->t_iflag;
387 	if ((error = (ISSET(c, TTY_ERRORMASK))) != 0) {
388 		CLR(c, TTY_ERRORMASK);
389 		if (ISSET(error, TTY_FE) && c == 0) {		/* Break. */
390 			if (ISSET(iflag, IGNBRK))
391 				return (0);
392 			else if (ISSET(iflag, BRKINT)) {
393 				ttyflush(tp, FREAD | FWRITE);
394 				pgsignal(tp->t_pgrp, SIGINT, 1);
395 				return (0);
396 			} else if (ISSET(iflag, PARMRK))
397 				goto parmrk;
398 		} else if ((ISSET(error, TTY_PE) && ISSET(iflag, INPCK)) ||
399 		    ISSET(error, TTY_FE)) {
400 			if (ISSET(iflag, IGNPAR))
401 				return (0);
402 			else if (ISSET(iflag, PARMRK)) {
403  parmrk:			(void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
404 				(void)putc(0    | TTY_QUOTE, &tp->t_rawq);
405 				(void)putc(c    | TTY_QUOTE, &tp->t_rawq);
406 				return (0);
407 			} else
408 				c = 0;
409 		}
410 	} else if (c == 0377 &&
411 	    ISSET(iflag, ISTRIP|IGNPAR|INPCK|PARMRK) == (INPCK|PARMRK)) {
412 		/* "Escape" a valid character of '\377'. */
413 		(void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
414 		(void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
415 		goto endcase;
416 	}
417 
418 	/*
419 	 * In tandem mode, check high water mark.
420 	 */
421 	if (ISSET(iflag, IXOFF) || ISSET(tp->t_cflag, CHWFLOW))
422 		ttyblock(tp);
423 	if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
424 		CLR(c, 0x80);
425 	if (!ISSET(lflag, EXTPROC)) {
426 		/*
427 		 * Check for literal nexting very first
428 		 */
429 		if (ISSET(tp->t_state, TS_LNCH)) {
430 			SET(c, TTY_QUOTE);
431 			CLR(tp->t_state, TS_LNCH);
432 		}
433 		/*
434 		 * Scan for special characters.  This code
435 		 * is really just a big case statement with
436 		 * non-constant cases.  The bottom of the
437 		 * case statement is labeled ``endcase'', so goto
438 		 * it after a case match, or similar.
439 		 */
440 
441 		/*
442 		 * Control chars which aren't controlled
443 		 * by ICANON, ISIG, or IXON.
444 		 */
445 		if (ISSET(lflag, IEXTEN)) {
446 			if (CCEQ(cc[VLNEXT], c)) {
447 				if (ISSET(lflag, ECHO)) {
448 					if (ISSET(lflag, ECHOE)) {
449 						(void)ttyoutput('^', tp);
450 						(void)ttyoutput('\b', tp);
451 					} else
452 						ttyecho(c, tp);
453 				}
454 				SET(tp->t_state, TS_LNCH);
455 				goto endcase;
456 			}
457 			if (CCEQ(cc[VDISCARD], c)) {
458 				if (ISSET(lflag, FLUSHO))
459 					CLR(tp->t_lflag, FLUSHO);
460 				else {
461 					ttyflush(tp, FWRITE);
462 					ttyecho(c, tp);
463 					if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
464 						ttyretype(tp);
465 					SET(tp->t_lflag, FLUSHO);
466 				}
467 				goto startoutput;
468 			}
469 		}
470 		/*
471 		 * Signals.
472 		 */
473 		if (ISSET(lflag, ISIG)) {
474 			if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
475 				if (!ISSET(lflag, NOFLSH))
476 					ttyflush(tp, FREAD | FWRITE);
477 				ttyecho(c, tp);
478 				pgsignal(tp->t_pgrp,
479 				    CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
480 				goto endcase;
481 			}
482 			if (CCEQ(cc[VSUSP], c)) {
483 				if (!ISSET(lflag, NOFLSH))
484 					ttyflush(tp, FREAD);
485 				ttyecho(c, tp);
486 				pgsignal(tp->t_pgrp, SIGTSTP, 1);
487 				goto endcase;
488 			}
489 		}
490 		/*
491 		 * Handle start/stop characters.
492 		 */
493 		if (ISSET(iflag, IXON)) {
494 			if (CCEQ(cc[VSTOP], c)) {
495 				if (!ISSET(tp->t_state, TS_TTSTOP)) {
496 					SET(tp->t_state, TS_TTSTOP);
497 					cdev = cdevsw_lookup(tp->t_dev);
498 					if (cdev != NULL)
499 						(*cdev->d_stop)(tp, 0);
500 					return (0);
501 				}
502 				if (!CCEQ(cc[VSTART], c))
503 					return (0);
504 				/*
505 				 * if VSTART == VSTOP then toggle
506 				 */
507 				goto endcase;
508 			}
509 			if (CCEQ(cc[VSTART], c))
510 				goto restartoutput;
511 		}
512 		/*
513 		 * IGNCR, ICRNL, & INLCR
514 		 */
515 		if (c == '\r') {
516 			if (ISSET(iflag, IGNCR))
517 				goto endcase;
518 			else if (ISSET(iflag, ICRNL))
519 				c = '\n';
520 		} else if (c == '\n' && ISSET(iflag, INLCR))
521 			c = '\r';
522 	}
523 	if (!ISSET(lflag, EXTPROC) && ISSET(lflag, ICANON)) {
524 		/*
525 		 * From here on down canonical mode character
526 		 * processing takes place.
527 		 */
528 		/*
529 		 * erase (^H / ^?)
530 		 */
531 		if (CCEQ(cc[VERASE], c)) {
532 			if (tp->t_rawq.c_cc)
533 				ttyrub(unputc(&tp->t_rawq), tp);
534 			goto endcase;
535 		}
536 		/*
537 		 * kill (^U)
538 		 */
539 		if (CCEQ(cc[VKILL], c)) {
540 			if (ISSET(lflag, ECHOKE) &&
541 			    tp->t_rawq.c_cc == tp->t_rocount &&
542 			    !ISSET(lflag, ECHOPRT))
543 				while (tp->t_rawq.c_cc)
544 					ttyrub(unputc(&tp->t_rawq), tp);
545 			else {
546 				ttyecho(c, tp);
547 				if (ISSET(lflag, ECHOK) ||
548 				    ISSET(lflag, ECHOKE))
549 					ttyecho('\n', tp);
550 				FLUSHQ(&tp->t_rawq);
551 				tp->t_rocount = 0;
552 			}
553 			CLR(tp->t_state, TS_LOCAL);
554 			goto endcase;
555 		}
556 		/*
557 		 * Extensions to the POSIX.1 GTI set of functions.
558 		 */
559 		if (ISSET(lflag, IEXTEN)) {
560 			/*
561 			 * word erase (^W)
562 			 */
563 			if (CCEQ(cc[VWERASE], c)) {
564 				int alt = ISSET(lflag, ALTWERASE);
565 				int ctype;
566 
567 				/*
568 				 * erase whitespace
569 				 */
570 				while ((c = unputc(&tp->t_rawq)) == ' ' ||
571 				    c == '\t')
572 					ttyrub(c, tp);
573 				if (c == -1)
574 					goto endcase;
575 				/*
576 				 * erase last char of word and remember the
577 				 * next chars type (for ALTWERASE)
578 				 */
579 				ttyrub(c, tp);
580 				c = unputc(&tp->t_rawq);
581 				if (c == -1)
582 					goto endcase;
583 				if (c == ' ' || c == '\t') {
584 					(void)putc(c, &tp->t_rawq);
585 					goto endcase;
586 				}
587 				ctype = ISALPHA(c);
588 				/*
589 				 * erase rest of word
590 				 */
591 				do {
592 					ttyrub(c, tp);
593 					c = unputc(&tp->t_rawq);
594 					if (c == -1)
595 						goto endcase;
596 				} while (c != ' ' && c != '\t' &&
597 				    (alt == 0 || ISALPHA(c) == ctype));
598 				(void)putc(c, &tp->t_rawq);
599 				goto endcase;
600 			}
601 			/*
602 			 * reprint line (^R)
603 			 */
604 			if (CCEQ(cc[VREPRINT], c)) {
605 				ttyretype(tp);
606 				goto endcase;
607 			}
608 			/*
609 			 * ^T - kernel info and generate SIGINFO
610 			 */
611 			if (CCEQ(cc[VSTATUS], c)) {
612 				if (!ISSET(lflag, NOKERNINFO))
613 					ttyinfo(tp);
614 				if (ISSET(lflag, ISIG))
615 					pgsignal(tp->t_pgrp, SIGINFO, 1);
616 				goto endcase;
617 			}
618 		}
619 	}
620 	/*
621 	 * Check for input buffer overflow
622 	 */
623 	if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
624 		if (ISSET(iflag, IMAXBEL)) {
625 			if (tp->t_outq.c_cc < tp->t_hiwat)
626 				(void)ttyoutput(CTRL('g'), tp);
627 		} else
628 			ttyflush(tp, FREAD | FWRITE);
629 		goto endcase;
630 	}
631 	/*
632 	 * Put data char in q for user and
633 	 * wakeup on seeing a line delimiter.
634 	 */
635 	if (putc(c, &tp->t_rawq) >= 0) {
636 		if (!ISSET(lflag, ICANON)) {
637 			ttwakeup(tp);
638 			ttyecho(c, tp);
639 			goto endcase;
640 		}
641 		if (TTBREAKC(c, lflag)) {
642 			tp->t_rocount = 0;
643 			catq(&tp->t_rawq, &tp->t_canq);
644 			ttwakeup(tp);
645 		} else if (tp->t_rocount++ == 0)
646 			tp->t_rocol = tp->t_column;
647 		if (ISSET(tp->t_state, TS_ERASE)) {
648 			/*
649 			 * end of prterase \.../
650 			 */
651 			CLR(tp->t_state, TS_ERASE);
652 			(void)ttyoutput('/', tp);
653 		}
654 		i = tp->t_column;
655 		ttyecho(c, tp);
656 		if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
657 			/*
658 			 * Place the cursor over the '^' of the ^D.
659 			 */
660 			i = min(2, tp->t_column - i);
661 			while (i > 0) {
662 				(void)ttyoutput('\b', tp);
663 				i--;
664 			}
665 		}
666 	}
667  endcase:
668 	/*
669 	 * IXANY means allow any character to restart output.
670 	 */
671 	if (ISSET(tp->t_state, TS_TTSTOP) &&
672 	    !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) {
673 		return (0);
674 	}
675  restartoutput:
676 	CLR(tp->t_lflag, FLUSHO);
677 	CLR(tp->t_state, TS_TTSTOP);
678  startoutput:
679 	return (ttstart(tp));
680 }
681 
682 /*
683  * Process input of a single character received on a tty.
684  * Must be called at spltty().
685  *
686  * XXX - this is a hack, all drivers must changed to acquire the
687  *	 lock before calling linesw->l_rint()
688  */
689 int
690 ttyinput(int c, struct tty *tp)
691 {
692 	int error;
693 	int s;
694 
695 	/*
696 	 * Unless the receiver is enabled, drop incoming data.
697 	 */
698 	if (!ISSET(tp->t_cflag, CREAD))
699 		return (0);
700 
701 	s = spltty();
702 	TTY_LOCK(tp);
703 	error = ttyinput_wlock(c, tp);
704 	TTY_UNLOCK(tp);
705 	splx(s);
706 	return (error);
707 }
708 
709 /*
710  * Output a single character on a tty, doing output processing
711  * as needed (expanding tabs, newline processing, etc.).
712  * Returns < 0 if succeeds, otherwise returns char to resend.
713  * Must be recursive.
714  * Call with tty slock held.
715  */
716 int
717 ttyoutput(int c, struct tty *tp)
718 {
719 	long	oflag;
720 	int	col, notout, s;
721 
722 	oflag = tp->t_oflag;
723 	if (!ISSET(oflag, OPOST)) {
724 		tk_nout++;
725 		tp->t_outcc++;
726 		if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
727 			return (c);
728 		return (-1);
729 	}
730 	/*
731 	 * Do tab expansion if OXTABS is set.  Special case if we do external
732 	 * processing, we don't do the tab expansion because we'll probably
733 	 * get it wrong.  If tab expansion needs to be done, let it happen
734 	 * externally.
735 	 */
736 	CLR(c, ~TTY_CHARMASK);
737 	if (c == '\t' &&
738 	    ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
739 		c = 8 - (tp->t_column & 7);
740 		if (ISSET(tp->t_lflag, FLUSHO)) {
741 			notout = 0;
742 		} else {
743 			s = spltty();		/* Don't interrupt tabs. */
744 			notout = b_to_q("        ", c, &tp->t_outq);
745 			c -= notout;
746 			tk_nout += c;
747 			tp->t_outcc += c;
748 			splx(s);
749 		}
750 		tp->t_column += c;
751 		return (notout ? '\t' : -1);
752 	}
753 	if (c == CEOT && ISSET(oflag, ONOEOT))
754 		return (-1);
755 
756 	/*
757 	 * Newline translation: if ONLCR is set,
758 	 * translate newline into "\r\n".
759 	 */
760 	if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
761 		tk_nout++;
762 		tp->t_outcc++;
763 		if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
764 			return (c);
765 	}
766 	/* If OCRNL is set, translate "\r" into "\n". */
767 	else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
768 		c = '\n';
769 	/* If ONOCR is set, don't transmit CRs when on column 0. */
770 	else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
771 		return (-1);
772 
773 	tk_nout++;
774 	tp->t_outcc++;
775 	if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
776 		return (c);
777 
778 	col = tp->t_column;
779 	switch (CCLASS(c)) {
780 	case BACKSPACE:
781 		if (col > 0)
782 			--col;
783 		break;
784 	case CONTROL:
785 		break;
786 	case NEWLINE:
787 		if (ISSET(tp->t_oflag, ONLCR | ONLRET))
788 			col = 0;
789 		break;
790 	case RETURN:
791 		col = 0;
792 		break;
793 	case ORDINARY:
794 		++col;
795 		break;
796 	case TAB:
797 		col = (col + 8) & ~7;
798 		break;
799 	}
800 	tp->t_column = col;
801 	return (-1);
802 }
803 
804 /*
805  * Ioctls for all tty devices.  Called after line-discipline specific ioctl
806  * has been called to do discipline-specific functions and/or reject any
807  * of these ioctl commands.
808  */
809 /* ARGSUSED */
810 int
811 ttioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p)
812 {
813 	extern struct tty *constty;	/* Temporary virtual console. */
814 	struct linesw	*lp;
815 	int		s, error;
816 	struct nameidata nd;
817 
818 	/* If the ioctl involves modification, hang if in the background. */
819 	switch (cmd) {
820 	case  TIOCFLUSH:
821 	case  TIOCDRAIN:
822 	case  TIOCSBRK:
823 	case  TIOCCBRK:
824 	case  TIOCSTART:
825 	case  TIOCSETA:
826 	case  TIOCSETD:
827 	case  TIOCSLINED:
828 	case  TIOCSETAF:
829 	case  TIOCSETAW:
830 #ifdef notdef
831 	case  TIOCSPGRP:
832 	case  FIOSETOWN:
833 #endif
834 	case  TIOCSTAT:
835 	case  TIOCSTI:
836 	case  TIOCSWINSZ:
837 #ifdef COMPAT_OLDTTY
838 	case  TIOCLBIC:
839 	case  TIOCLBIS:
840 	case  TIOCLSET:
841 	case  TIOCSETC:
842 	case OTIOCSETD:
843 	case  TIOCSETN:
844 	case  TIOCSETP:
845 	case  TIOCSLTC:
846 #endif
847 		while (isbackground(curproc, tp) &&
848 		    p->p_pgrp->pg_jobc && (p->p_flag & P_PPWAIT) == 0 &&
849 		    !sigismasked(p, SIGTTOU)) {
850 			pgsignal(p->p_pgrp, SIGTTOU, 1);
851 			s = spltty();
852 			TTY_LOCK(tp);
853 			error = ttysleep(tp, &lbolt,
854 					 TTOPRI | PCATCH | PNORELOCK, ttybg, 0);
855 			splx(s);
856 			if (error) {
857 				return (error);
858 			}
859 		}
860 		break;
861 	}
862 
863 	switch (cmd) {			/* Process the ioctl. */
864 	case FIOASYNC:			/* set/clear async i/o */
865 		s = spltty();
866 		TTY_LOCK(tp);
867 		if (*(int *)data)
868 			SET(tp->t_state, TS_ASYNC);
869 		else
870 			CLR(tp->t_state, TS_ASYNC);
871 		TTY_UNLOCK(tp);
872 		splx(s);
873 		break;
874 	case FIONBIO:			/* set/clear non-blocking i/o */
875 		break;			/* XXX: delete. */
876 	case FIONREAD:			/* get # bytes to read */
877 		s = spltty();
878 		TTY_LOCK(tp);
879 		*(int *)data = ttnread(tp);
880 		TTY_UNLOCK(tp);
881 		splx(s);
882 		break;
883 	case FIONWRITE:			/* get # bytes to written & unsent */
884 		s = spltty();
885 		TTY_LOCK(tp);
886 		*(int *)data = tp->t_outq.c_cc;
887 		TTY_UNLOCK(tp);
888 		splx(s);
889 		break;
890 	case FIONSPACE:			/* get # bytes to written & unsent */
891 		s = spltty();
892 		TTY_LOCK(tp);
893 		*(int *)data = tp->t_outq.c_cn - tp->t_outq.c_cc;
894 		TTY_UNLOCK(tp);
895 		splx(s);
896 		break;
897 	case TIOCEXCL:			/* set exclusive use of tty */
898 		s = spltty();
899 		TTY_LOCK(tp);
900 		SET(tp->t_state, TS_XCLUDE);
901 		splx(s);
902 		TTY_UNLOCK(tp);
903 		break;
904 	case TIOCFLUSH: {		/* flush buffers */
905 		int flags = *(int *)data;
906 
907 		if (flags == 0)
908 			flags = FREAD | FWRITE;
909 		else
910 			flags &= FREAD | FWRITE;
911 		s = spltty();
912 		TTY_LOCK(tp);
913 		ttyflush(tp, flags);
914 		TTY_UNLOCK(tp);
915 		splx(s);
916 		break;
917 	}
918 	case TIOCCONS:			/* become virtual console */
919 		if (*(int *)data) {
920 			if (constty && constty != tp &&
921 			    ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) ==
922 			    (TS_CARR_ON | TS_ISOPEN))
923 				return EBUSY;
924 
925 			NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
926 			    "/dev/console", p);
927 			if ((error = namei(&nd)) != 0)
928 				return error;
929 			error = VOP_ACCESS(nd.ni_vp, VREAD, p->p_ucred, p);
930 			vput(nd.ni_vp);
931 			if (error)
932 				return error;
933 
934 			constty = tp;
935 		} else if (tp == constty)
936 			constty = NULL;
937 		break;
938 	case TIOCDRAIN:			/* wait till output drained */
939 		if ((error = ttywait(tp)) != 0)
940 			return (error);
941 		break;
942 	case TIOCGETA: {		/* get termios struct */
943 		struct termios *t = (struct termios *)data;
944 
945 		memcpy(t, &tp->t_termios, sizeof(struct termios));
946 		break;
947 	}
948 	case TIOCGETD:			/* get line discipline */
949 		*(int *)data = tp->t_linesw->l_no;
950 		break;
951 	case TIOCGLINED:
952 		(void)strncpy((char *)data, tp->t_linesw->l_name,
953 		    TTLINEDNAMELEN - 1);
954 		break;
955 	case TIOCGWINSZ:		/* get window size */
956 		*(struct winsize *)data = tp->t_winsize;
957 		break;
958 	case FIOGETOWN:
959 		if (!isctty(p, tp))
960 			return (ENOTTY);
961 		*(int *)data = tp->t_pgrp ? -tp->t_pgrp->pg_id : 0;
962 		break;
963 	case TIOCGPGRP:			/* get pgrp of tty */
964 		if (!isctty(p, tp))
965 			return (ENOTTY);
966 		*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
967 		break;
968 	case TIOCGSID:			/* get sid of tty */
969 		if (!isctty(p, tp))
970 			return (ENOTTY);
971 		*(int *)data = tp->t_session->s_sid;
972 		break;
973 #ifdef TIOCHPCL
974 	case TIOCHPCL:			/* hang up on last close */
975 		s = spltty();
976 		TTY_LOCK(tp);
977 		SET(tp->t_cflag, HUPCL);
978 		TTY_UNLOCK(tp);
979 		splx(s);
980 		break;
981 #endif
982 	case TIOCNXCL:			/* reset exclusive use of tty */
983 		s = spltty();
984 		TTY_LOCK(tp);
985 		CLR(tp->t_state, TS_XCLUDE);
986 		TTY_UNLOCK(tp);
987 		splx(s);
988 		break;
989 	case TIOCOUTQ:			/* output queue size */
990 		*(int *)data = tp->t_outq.c_cc;
991 		break;
992 	case TIOCSETA:			/* set termios struct */
993 	case TIOCSETAW:			/* drain output, set */
994 	case TIOCSETAF: {		/* drn out, fls in, set */
995 		struct termios *t = (struct termios *)data;
996 
997 		if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
998 			if ((error = ttywait(tp)) != 0)
999 				return (error);
1000 
1001 			if (cmd == TIOCSETAF) {
1002 				s = spltty();
1003 				TTY_LOCK(tp);
1004 				ttyflush(tp, FREAD);
1005 				TTY_UNLOCK(tp);
1006 				splx(s);
1007 			}
1008 		}
1009 
1010 		s = spltty();
1011 		/*
1012 		 * XXXSMP - some drivers call back on us from t_param(), so
1013 		 *	    don't take the tty spin lock here.
1014 		 *	    require t_param() to unlock upon callback?
1015 		 */
1016 		/* wanted here: TTY_LOCK(tp); */
1017 		if (!ISSET(t->c_cflag, CIGNORE)) {
1018 			/*
1019 			 * Set device hardware.
1020 			 */
1021 			if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
1022 				/* wanted here: TTY_UNLOCK(tp); */
1023 				splx(s);
1024 				return (error);
1025 			} else {
1026 				tp->t_cflag = t->c_cflag;
1027 				tp->t_ispeed = t->c_ispeed;
1028 				tp->t_ospeed = t->c_ospeed;
1029 				if (t->c_ospeed == 0 && tp->t_session &&
1030 				    tp->t_session->s_leader)
1031 					psignal(tp->t_session->s_leader,
1032 					    SIGHUP);
1033 			}
1034 			ttsetwater(tp);
1035 		}
1036 
1037 		/* delayed lock acquiring */TTY_LOCK(tp);
1038 		if (cmd != TIOCSETAF) {
1039 			if (ISSET(t->c_lflag, ICANON) !=
1040 			    ISSET(tp->t_lflag, ICANON)) {
1041 				if (ISSET(t->c_lflag, ICANON)) {
1042 					SET(tp->t_lflag, PENDIN);
1043 					ttwakeup(tp);
1044 				} else {
1045 					struct clist tq;
1046 
1047 					catq(&tp->t_rawq, &tp->t_canq);
1048 					tq = tp->t_rawq;
1049 					tp->t_rawq = tp->t_canq;
1050 					tp->t_canq = tq;
1051 					CLR(tp->t_lflag, PENDIN);
1052 				}
1053 			}
1054 		}
1055 		tp->t_iflag = t->c_iflag;
1056 		tp->t_oflag = t->c_oflag;
1057 		/*
1058 		 * Make the EXTPROC bit read only.
1059 		 */
1060 		if (ISSET(tp->t_lflag, EXTPROC))
1061 			SET(t->c_lflag, EXTPROC);
1062 		else
1063 			CLR(t->c_lflag, EXTPROC);
1064 		tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
1065 		memcpy(tp->t_cc, t->c_cc, sizeof(t->c_cc));
1066 		TTY_UNLOCK(tp);
1067 		splx(s);
1068 		break;
1069 	}
1070 	case TIOCSETD: {		/* set line discipline */
1071 		int t = *(int *)data;
1072 
1073 		if (t < 0)
1074 			return (EINVAL);
1075 		if (t >= nlinesw)
1076 			return (ENXIO);
1077 		lp = linesw[t];
1078 		goto setldisc;
1079 	}
1080 	case TIOCSLINED: {		/* set line discipline */
1081 		char *name = (char *)data;
1082 		dev_t device;
1083 
1084 		/* Null terminate to prevent buffer overflow */
1085 		name[TTLINEDNAMELEN - 1] = '\0';
1086 		lp = ttyldisc_lookup(name);
1087 
1088  setldisc:
1089 		if (lp == NULL)
1090 			return (ENXIO);
1091 
1092 		if (lp != tp->t_linesw) {
1093 			device = tp->t_dev;
1094 			s = spltty();
1095 			(*tp->t_linesw->l_close)(tp, flag);
1096 			error = (*lp->l_open)(device, tp);
1097 			if (error) {
1098 				(void)(*tp->t_linesw->l_open)(device, tp);
1099 				splx(s);
1100 				return (error);
1101 			}
1102 			tp->t_linesw = lp;
1103 			splx(s);
1104 		}
1105 		break;
1106 	}
1107 	case TIOCSTART:			/* start output, like ^Q */
1108 		s = spltty();
1109 		TTY_LOCK(tp);
1110 		if (ISSET(tp->t_state, TS_TTSTOP) ||
1111 		    ISSET(tp->t_lflag, FLUSHO)) {
1112 			CLR(tp->t_lflag, FLUSHO);
1113 			CLR(tp->t_state, TS_TTSTOP);
1114 			ttstart(tp);
1115 		}
1116 		TTY_UNLOCK(tp);
1117 		splx(s);
1118 		break;
1119 	case TIOCSTI:			/* simulate terminal input */
1120 		if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
1121 			return (EPERM);
1122 		if (p->p_ucred->cr_uid && !isctty(p, tp))
1123 			return (EACCES);
1124 		(*tp->t_linesw->l_rint)(*(u_char *)data, tp);
1125 		break;
1126 	case TIOCSTOP:			/* stop output, like ^S */
1127 	{
1128 		const struct cdevsw *cdev;
1129 		s = spltty();
1130 		TTY_LOCK(tp);
1131 		if (!ISSET(tp->t_state, TS_TTSTOP)) {
1132 			SET(tp->t_state, TS_TTSTOP);
1133 			cdev = cdevsw_lookup(tp->t_dev);
1134 			if (cdev != NULL)
1135 				(*cdev->d_stop)(tp, 0);
1136 		}
1137 		TTY_UNLOCK(tp);
1138 		splx(s);
1139 		break;
1140 	}
1141 	case TIOCSCTTY:			/* become controlling tty */
1142 		/* Session ctty vnode pointer set in vnode layer. */
1143 		if (!SESS_LEADER(p) ||
1144 		    ((p->p_session->s_ttyvp || tp->t_session) &&
1145 		    (tp->t_session != p->p_session)))
1146 			return (EPERM);
1147 
1148 		/*
1149 		 * `p_session' acquires a reference.
1150 		 * But note that if `t_session' is set at this point,
1151 		 * it must equal `p_session', in which case the session
1152 		 * already has the correct reference count.
1153 		 */
1154 		if (tp->t_session == NULL)
1155 			SESSHOLD(p->p_session);
1156 
1157 		tp->t_session = p->p_session;
1158 		tp->t_pgrp = p->p_pgrp;
1159 		p->p_session->s_ttyp = tp;
1160 		p->p_flag |= P_CONTROLT;
1161 		break;
1162 	case FIOSETOWN: {		/* set pgrp of tty */
1163 		pid_t pgid = *(int *)data;
1164 		struct pgrp *pgrp;
1165 
1166 		if (!isctty(p, tp))
1167 			return (ENOTTY);
1168 
1169 		if (pgid < 0)
1170 			pgrp = pgfind(-pgid);
1171 		else {
1172 			struct proc *p1 = pfind(pgid);
1173 			if (!p1)
1174 				return (ESRCH);
1175 			pgrp = p1->p_pgrp;
1176 		}
1177 
1178 		if (pgrp == NULL)
1179 			return (EINVAL);
1180 		else if (pgrp->pg_session != p->p_session)
1181 			return (EPERM);
1182 		tp->t_pgrp = pgrp;
1183 		break;
1184 	}
1185 	case TIOCSPGRP: {		/* set pgrp of tty */
1186 		struct pgrp *pgrp = pgfind(*(int *)data);
1187 
1188 		if (!isctty(p, tp))
1189 			return (ENOTTY);
1190 		else if (pgrp == NULL)
1191 			return (EINVAL);
1192 		else if (pgrp->pg_session != p->p_session)
1193 			return (EPERM);
1194 		tp->t_pgrp = pgrp;
1195 		break;
1196 	}
1197 	case TIOCSTAT:			/* get load avg stats */
1198 		s = spltty();
1199 		TTY_LOCK(tp);
1200 		ttyinfo(tp);
1201 		TTY_UNLOCK(tp);
1202 		splx(s);
1203 		break;
1204 	case TIOCSWINSZ:		/* set window size */
1205 		if (memcmp((caddr_t)&tp->t_winsize, data,
1206 		    sizeof(struct winsize))) {
1207 			tp->t_winsize = *(struct winsize *)data;
1208 			pgsignal(tp->t_pgrp, SIGWINCH, 1);
1209 		}
1210 		break;
1211 	default:
1212 #ifdef COMPAT_OLDTTY
1213 		return (ttcompat(tp, cmd, data, flag, p));
1214 #else
1215 		return (EPASSTHROUGH);
1216 #endif
1217 	}
1218 	return (0);
1219 }
1220 
1221 int
1222 ttpoll(struct tty *tp, int events, struct proc *p)
1223 {
1224 	int	revents, s;
1225 
1226 	revents = 0;
1227 	s = spltty();
1228 	TTY_LOCK(tp);
1229 	if (events & (POLLIN | POLLRDNORM))
1230 		if (ttnread(tp) > 0)
1231 			revents |= events & (POLLIN | POLLRDNORM);
1232 
1233 	if (events & (POLLOUT | POLLWRNORM))
1234 		if (tp->t_outq.c_cc <= tp->t_lowat)
1235 			revents |= events & (POLLOUT | POLLWRNORM);
1236 
1237 	if (events & POLLHUP)
1238 		if (!CONNECTED(tp))
1239 			revents |= POLLHUP;
1240 
1241 	if (revents == 0) {
1242 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
1243 			selrecord(p, &tp->t_rsel);
1244 
1245 		if (events & (POLLOUT | POLLWRNORM))
1246 			selrecord(p, &tp->t_wsel);
1247 	}
1248 
1249 	TTY_UNLOCK(tp);
1250 	splx(s);
1251 	return (revents);
1252 }
1253 
1254 static void
1255 filt_ttyrdetach(struct knote *kn)
1256 {
1257 	struct tty	*tp;
1258 	int		s;
1259 
1260 	tp = kn->kn_hook;
1261 	s = spltty();
1262 	SLIST_REMOVE(&tp->t_rsel.sel_klist, kn, knote, kn_selnext);
1263 	splx(s);
1264 }
1265 
1266 static int
1267 filt_ttyread(struct knote *kn, long hint)
1268 {
1269 	struct tty	*tp;
1270 	int		s;
1271 
1272 	tp = kn->kn_hook;
1273 	s = spltty();
1274 	if ((hint & NOTE_SUBMIT) == 0)
1275 		TTY_LOCK(tp);
1276 	kn->kn_data = ttnread(tp);
1277 	if ((hint & NOTE_SUBMIT) == 0)
1278 		TTY_UNLOCK(tp);
1279 	splx(s);
1280 	return (kn->kn_data > 0);
1281 }
1282 
1283 static void
1284 filt_ttywdetach(struct knote *kn)
1285 {
1286 	struct tty	*tp;
1287 	int		s;
1288 
1289 	tp = kn->kn_hook;
1290 	s = spltty();
1291 	TTY_LOCK(tp);
1292 	SLIST_REMOVE(&tp->t_wsel.sel_klist, kn, knote, kn_selnext);
1293 	TTY_UNLOCK(tp);
1294 	splx(s);
1295 }
1296 
1297 static int
1298 filt_ttywrite(struct knote *kn, long hint)
1299 {
1300 	struct tty	*tp;
1301 	int		canwrite, s;
1302 
1303 	tp = kn->kn_hook;
1304 	s = spltty();
1305 	if ((hint & NOTE_SUBMIT) == 0)
1306 		TTY_LOCK(tp);
1307 	kn->kn_data = tp->t_outq.c_cn - tp->t_outq.c_cc;
1308 	canwrite = (tp->t_outq.c_cc <= tp->t_lowat) && CONNECTED(tp);
1309 	if ((hint & NOTE_SUBMIT) == 0)
1310 		TTY_UNLOCK(tp);
1311 	splx(s);
1312 	return (canwrite);
1313 }
1314 
1315 static const struct filterops ttyread_filtops =
1316 	{ 1, NULL, filt_ttyrdetach, filt_ttyread };
1317 static const struct filterops ttywrite_filtops =
1318 	{ 1, NULL, filt_ttywdetach, filt_ttywrite };
1319 
1320 int
1321 ttykqfilter(dev_t dev, struct knote *kn)
1322 {
1323 	struct tty	*tp;
1324 	struct klist	*klist;
1325 	int		s;
1326 	const struct cdevsw	*cdev;
1327 
1328         if (((cdev = cdevsw_lookup(dev)) == NULL) ||
1329 	    (cdev->d_tty == NULL) ||
1330 	    ((tp = (*cdev->d_tty)(dev)) == NULL))
1331 		return (ENXIO);
1332 
1333 	switch (kn->kn_filter) {
1334 	case EVFILT_READ:
1335 		klist = &tp->t_rsel.sel_klist;
1336 		kn->kn_fop = &ttyread_filtops;
1337 		break;
1338 	case EVFILT_WRITE:
1339 		klist = &tp->t_wsel.sel_klist;
1340 		kn->kn_fop = &ttywrite_filtops;
1341 		break;
1342 	default:
1343 		return (1);
1344 	}
1345 
1346 	kn->kn_hook = tp;
1347 
1348 	s = spltty();
1349 	TTY_LOCK(tp);
1350 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1351 	TTY_UNLOCK(tp);
1352 	splx(s);
1353 
1354 	return (0);
1355 }
1356 
1357 /*
1358  * Find the number of chars ready to be read from this tty.
1359  * Call at spltty() and with the tty slock held.
1360  */
1361 static int
1362 ttnread(struct tty *tp)
1363 {
1364 	int	nread;
1365 
1366 	if (ISSET(tp->t_lflag, PENDIN))
1367 		ttypend(tp);
1368 	nread = tp->t_canq.c_cc;
1369 	if (!ISSET(tp->t_lflag, ICANON)) {
1370 		nread += tp->t_rawq.c_cc;
1371 		if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
1372 			nread = 0;
1373 	}
1374 	return (nread);
1375 }
1376 
1377 /*
1378  * Wait for output to drain.
1379  */
1380 int
1381 ttywait(struct tty *tp)
1382 {
1383 	int	error, s;
1384 
1385 	error = 0;
1386 	s = spltty();
1387 	TTY_LOCK(tp);
1388 	while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1389 	    CONNECTED(tp) && tp->t_oproc) {
1390 		(*tp->t_oproc)(tp);
1391 		SET(tp->t_state, TS_ASLEEP);
1392 		error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1393 		if (error)
1394 			break;
1395 	}
1396 	TTY_UNLOCK(tp);
1397 	splx(s);
1398 	return (error);
1399 }
1400 
1401 /*
1402  * Flush if successfully wait.
1403  */
1404 int
1405 ttywflush(struct tty *tp)
1406 {
1407 	int	error;
1408 	int	s;
1409 
1410 	if ((error = ttywait(tp)) == 0) {
1411 		s = spltty();
1412 		TTY_LOCK(tp);
1413 		ttyflush(tp, FREAD);
1414 		TTY_UNLOCK(tp);
1415 		splx(s);
1416 	}
1417 	return (error);
1418 }
1419 
1420 /*
1421  * Flush tty read and/or write queues, notifying anyone waiting.
1422  * Call at spltty() and with the tty slock held.
1423  */
1424 void
1425 ttyflush(struct tty *tp, int rw)
1426 {
1427 	const struct cdevsw *cdev;
1428 
1429 	if (rw & FREAD) {
1430 		FLUSHQ(&tp->t_canq);
1431 		FLUSHQ(&tp->t_rawq);
1432 		tp->t_rocount = 0;
1433 		tp->t_rocol = 0;
1434 		CLR(tp->t_state, TS_LOCAL);
1435 		ttwakeup(tp);
1436 	}
1437 	if (rw & FWRITE) {
1438 		CLR(tp->t_state, TS_TTSTOP);
1439 		cdev = cdevsw_lookup(tp->t_dev);
1440 		if (cdev != NULL)
1441 			(*cdev->d_stop)(tp, rw);
1442 		FLUSHQ(&tp->t_outq);
1443 		wakeup((caddr_t)&tp->t_outq);
1444 		selnotify(&tp->t_wsel, NOTE_SUBMIT);
1445 	}
1446 }
1447 
1448 /*
1449  * Copy in the default termios characters.
1450  */
1451 void
1452 ttychars(struct tty *tp)
1453 {
1454 
1455 	memcpy(tp->t_cc, ttydefchars, sizeof(ttydefchars));
1456 }
1457 
1458 /*
1459  * Send stop character on input overflow.
1460  * Call at spltty() and with the tty slock held.
1461  */
1462 static void
1463 ttyblock(struct tty *tp)
1464 {
1465 	int	total;
1466 
1467 	total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
1468 	if (tp->t_rawq.c_cc > TTYHOG) {
1469 		ttyflush(tp, FREAD | FWRITE);
1470 		CLR(tp->t_state, TS_TBLOCK);
1471 	}
1472 	/*
1473 	 * Block further input iff: current input > threshold
1474 	 * AND input is available to user program.
1475 	 */
1476 	if (total >= TTYHOG / 2 &&
1477 	    !ISSET(tp->t_state, TS_TBLOCK) &&
1478 	    (!ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0)) {
1479 		if (ISSET(tp->t_iflag, IXOFF) &&
1480 		    tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1481 		    putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
1482 			SET(tp->t_state, TS_TBLOCK);
1483 			ttstart(tp);
1484 		}
1485 		/* Try to block remote output via hardware flow control. */
1486 		if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1487 		    (*tp->t_hwiflow)(tp, 1) != 0)
1488 			SET(tp->t_state, TS_TBLOCK);
1489 	}
1490 }
1491 
1492 /*
1493  * Delayed line discipline output
1494  */
1495 void
1496 ttrstrt(void *tp_arg)
1497 {
1498 	struct tty	*tp;
1499 	int		s;
1500 
1501 #ifdef DIAGNOSTIC
1502 	if (tp_arg == NULL)
1503 		panic("ttrstrt");
1504 #endif
1505 	tp = tp_arg;
1506 	s = spltty();
1507 	TTY_LOCK(tp);
1508 
1509 	CLR(tp->t_state, TS_TIMEOUT);
1510 	ttstart(tp); /* XXX - Shouldn't this be tp->l_start(tp)? */
1511 
1512 	TTY_UNLOCK(tp);
1513 	splx(s);
1514 }
1515 
1516 /*
1517  * start a line discipline
1518  * Always call at spltty() and with tty slock held?
1519  */
1520 int
1521 ttstart(struct tty *tp)
1522 {
1523 
1524 	if (tp->t_oproc != NULL)	/* XXX: Kludge for pty. */
1525 		(*tp->t_oproc)(tp);
1526 	return (0);
1527 }
1528 
1529 /*
1530  * "close" a line discipline
1531  */
1532 int
1533 ttylclose(struct tty *tp, int flag)
1534 {
1535 	int s;
1536 
1537 	if (flag & FNONBLOCK) {
1538 		s = spltty();
1539 		TTY_LOCK(tp);
1540 		ttyflush(tp, FREAD | FWRITE);
1541 		TTY_UNLOCK(tp);
1542 		splx(s);
1543 	} else
1544 		ttywflush(tp);
1545 	return (0);
1546 }
1547 
1548 /*
1549  * Handle modem control transition on a tty.
1550  * Flag indicates new state of carrier.
1551  * Returns 0 if the line should be turned off, otherwise 1.
1552  *
1553  * Must be called at spltty().
1554  * XXX except that it is often isn't, which should be fixed.
1555  */
1556 int
1557 ttymodem(struct tty *tp, int flag)
1558 {
1559 	int s;
1560 
1561 	s = spltty();
1562 	TTY_LOCK(tp);
1563 	if (flag == 0) {
1564 		if (ISSET(tp->t_state, TS_CARR_ON)) {
1565 			/*
1566 			 * Lost carrier.
1567 			 */
1568 			CLR(tp->t_state, TS_CARR_ON);
1569 			if (ISSET(tp->t_state, TS_ISOPEN) && !CONNECTED(tp)) {
1570 				if (tp->t_session && tp->t_session->s_leader)
1571 					psignal(tp->t_session->s_leader,
1572 					    SIGHUP);
1573 				ttyflush(tp, FREAD | FWRITE);
1574 				TTY_UNLOCK(tp);
1575 				splx(s);
1576 				return (0);
1577 			}
1578 		}
1579 	} else {
1580 		if (!ISSET(tp->t_state, TS_CARR_ON)) {
1581 			/*
1582 			 * Carrier now on.
1583 			 */
1584 			SET(tp->t_state, TS_CARR_ON);
1585 			ttwakeup(tp);
1586 		}
1587 	}
1588 	TTY_UNLOCK(tp);
1589 	splx(s);
1590 	return (1);
1591 }
1592 
1593 /*
1594  * Default modem control routine (for other line disciplines).
1595  * Return argument flag, to turn off device on carrier drop.
1596  *
1597  * Must be called at spltty().
1598  * XXX except that it is often isn't, which should be fixed.
1599  */
1600 int
1601 nullmodem(struct tty *tp, int flag)
1602 {
1603 	int s;
1604 
1605 	s = spltty();
1606 	TTY_LOCK(tp);
1607 	if (flag)
1608 		SET(tp->t_state, TS_CARR_ON);
1609 	else {
1610 		CLR(tp->t_state, TS_CARR_ON);
1611 		if (!CONNECTED(tp)) {
1612 			if (tp->t_session && tp->t_session->s_leader)
1613 				psignal(tp->t_session->s_leader, SIGHUP);
1614 			TTY_UNLOCK(tp);
1615 			splx(s);
1616 			return (0);
1617 		}
1618 	}
1619 	TTY_UNLOCK(tp);
1620 	splx(s);
1621 	return (1);
1622 }
1623 
1624 /*
1625  * Reinput pending characters after state switch.
1626  * Call at spltty() and with the tty slock held.
1627  */
1628 void
1629 ttypend(struct tty *tp)
1630 {
1631 	struct clist	tq;
1632 	int		c;
1633 
1634 	CLR(tp->t_lflag, PENDIN);
1635 	SET(tp->t_state, TS_TYPEN);
1636 	tq = tp->t_rawq;
1637 	tp->t_rawq.c_cc = 0;
1638 	tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
1639 	while ((c = getc(&tq)) >= 0)
1640 		ttyinput_wlock(c, tp);
1641 	CLR(tp->t_state, TS_TYPEN);
1642 }
1643 
1644 /*
1645  * Process a read call on a tty device.
1646  */
1647 int
1648 ttread(struct tty *tp, struct uio *uio, int flag)
1649 {
1650 	struct clist	*qp;
1651 	u_char		*cc;
1652 	struct proc	*p;
1653 	int		c, s, first, error, has_stime, last_cc;
1654 	long		lflag, slp;
1655 	struct timeval	stime;
1656 
1657 	cc = tp->t_cc;
1658 	p = curproc;
1659 	error = 0;
1660 	has_stime = 0;
1661 	last_cc = 0;
1662 	slp = 0;
1663 
1664  loop:
1665 	s = spltty();
1666 	TTY_LOCK(tp);
1667 	lflag = tp->t_lflag;
1668 	/*
1669 	 * take pending input first
1670 	 */
1671 	if (ISSET(lflag, PENDIN))
1672 		ttypend(tp);
1673 
1674 	/*
1675 	 * Hang process if it's in the background.
1676 	 */
1677 	if (isbackground(p, tp)) {
1678 		if (sigismember(&p->p_sigctx.ps_sigignore, SIGTTIN) ||
1679 		    sigismember(&p->p_sigctx.ps_sigmask, SIGTTIN) ||
1680 		    p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0) {
1681 			TTY_UNLOCK(tp);
1682 			splx(s);
1683 			return (EIO);
1684 		}
1685 		pgsignal(p->p_pgrp, SIGTTIN, 1);
1686 		error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
1687 		splx(s);
1688 		if (error)
1689 			return (error);
1690 		goto loop;
1691 	}
1692 
1693 	if (!ISSET(lflag, ICANON)) {
1694 		int m = cc[VMIN];
1695 		long t = cc[VTIME];
1696 
1697 		qp = &tp->t_rawq;
1698 		/*
1699 		 * Check each of the four combinations.
1700 		 * (m > 0 && t == 0) is the normal read case.
1701 		 * It should be fairly efficient, so we check that and its
1702 		 * companion case (m == 0 && t == 0) first.
1703 		 * For the other two cases, we compute the target sleep time
1704 		 * into slp.
1705 		 */
1706 		if (t == 0) {
1707 			if (qp->c_cc < m)
1708 				goto sleep;
1709 			goto read;
1710 		}
1711 		t *= hz;		/* time in deca-ticks */
1712 /*
1713  * Time difference in deca-ticks, split division to avoid numeric overflow.
1714  * Ok for hz < ~200kHz
1715  */
1716 #define	diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 10 * hz + \
1717 			 ((t1).tv_usec - (t2).tv_usec) / 100 * hz / 1000)
1718 		if (m > 0) {
1719 			if (qp->c_cc <= 0)
1720 				goto sleep;
1721 			if (qp->c_cc >= m)
1722 				goto read;
1723 			if (!has_stime) {
1724 				/* first character, start timer */
1725 				has_stime = 1;
1726 				stime = time;
1727 				slp = t;
1728 			} else if (qp->c_cc > last_cc) {
1729 				/* got a character, restart timer */
1730 				stime = time;
1731 				slp = t;
1732 			} else {
1733 				/* nothing, check expiration */
1734 				slp = t - diff(time, stime);
1735 			}
1736 		} else {	/* m == 0 */
1737 			if (qp->c_cc > 0)
1738 				goto read;
1739 			if (!has_stime) {
1740 				has_stime = 1;
1741 				stime = time;
1742 				slp = t;
1743 			} else
1744 				slp = t - diff(time, stime);
1745 		}
1746 		last_cc = qp->c_cc;
1747 #undef diff
1748 		if (slp > 0) {
1749 			/*
1750 			 * Convert deca-ticks back to ticks.
1751 			 * Rounding down may make us wake up just short
1752 			 * of the target, so we round up.
1753 			 * Maybe we should do 'slp/10 + 1' because the
1754 			 * first tick maybe almost immediate.
1755 			 * However it is more useful for a program that sets
1756 			 * VTIME=10 to wakeup every second not every 1.01
1757 			 * seconds (if hz=100).
1758 			 */
1759 			slp = (slp + 9)/ 10;
1760 			goto sleep;
1761 		}
1762 	} else if ((qp = &tp->t_canq)->c_cc <= 0) {
1763 		int	carrier;
1764 
1765  sleep:
1766 		/*
1767 		 * If there is no input, sleep on rawq
1768 		 * awaiting hardware receipt and notification.
1769 		 * If we have data, we don't need to check for carrier.
1770 		 */
1771 		carrier = CONNECTED(tp);
1772 		if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1773 			TTY_UNLOCK(tp);
1774 			splx(s);
1775 			return (0);	/* EOF */
1776 		}
1777 		if (flag & IO_NDELAY) {
1778 			TTY_UNLOCK(tp);
1779 			splx(s);
1780 			return (EWOULDBLOCK);
1781 		}
1782 		error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH | PNORELOCK,
1783 		    carrier ? ttyin : ttopen, slp);
1784 		splx(s);
1785 		/* VMIN == 0: any quantity read satisfies */
1786 		if (cc[VMIN] == 0 && error == EWOULDBLOCK)
1787 			return (0);
1788 		if (error && error != EWOULDBLOCK)
1789 			return (error);
1790 		goto loop;
1791 	}
1792  read:
1793 	TTY_UNLOCK(tp);
1794 	splx(s);
1795 
1796 	/*
1797 	 * Input present, check for input mapping and processing.
1798 	 */
1799 	first = 1;
1800 	while ((c = getc(qp)) >= 0) {
1801 		/*
1802 		 * delayed suspend (^Y)
1803 		 */
1804 		if (CCEQ(cc[VDSUSP], c) &&
1805 		    ISSET(lflag, IEXTEN|ISIG) == (IEXTEN|ISIG)) {
1806 			pgsignal(tp->t_pgrp, SIGTSTP, 1);
1807 			if (first) {
1808 				s = spltty();
1809 				TTY_LOCK(tp);
1810 				error = ttysleep(tp, &lbolt,
1811 				    TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
1812 				splx(s);
1813 				if (error)
1814 					break;
1815 				goto loop;
1816 			}
1817 			break;
1818 		}
1819 		/*
1820 		 * Interpret EOF only in canonical mode.
1821 		 */
1822 		if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1823 			break;
1824 		/*
1825 		 * Give user character.
1826 		 */
1827  		error = ureadc(c, uio);
1828 		if (error)
1829 			break;
1830  		if (uio->uio_resid == 0)
1831 			break;
1832 		/*
1833 		 * In canonical mode check for a "break character"
1834 		 * marking the end of a "line of input".
1835 		 */
1836 		if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1837 			break;
1838 		first = 0;
1839 	}
1840 	/*
1841 	 * Look to unblock output now that (presumably)
1842 	 * the input queue has gone down.
1843 	 */
1844 	s = spltty();
1845 	TTY_LOCK(tp);
1846 	if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG / 5) {
1847 		if (ISSET(tp->t_iflag, IXOFF) &&
1848 		    cc[VSTART] != _POSIX_VDISABLE &&
1849 		    putc(cc[VSTART], &tp->t_outq) == 0) {
1850 			CLR(tp->t_state, TS_TBLOCK);
1851 			ttstart(tp);
1852 		}
1853 		/* Try to unblock remote output via hardware flow control. */
1854 		if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1855 		    (*tp->t_hwiflow)(tp, 0) != 0)
1856 			CLR(tp->t_state, TS_TBLOCK);
1857 	}
1858 	TTY_UNLOCK(tp);
1859 	splx(s);
1860 	return (error);
1861 }
1862 
1863 /*
1864  * Check the output queue on tp for space for a kernel message (from uprintf
1865  * or tprintf).  Allow some space over the normal hiwater mark so we don't
1866  * lose messages due to normal flow control, but don't let the tty run amok.
1867  * Sleeps here are not interruptible, but we return prematurely if new signals
1868  * arrive.
1869  * Call with tty slock held.
1870  */
1871 static int
1872 ttycheckoutq_wlock(struct tty *tp, int wait)
1873 {
1874 	int	hiwat, s, error;
1875 
1876 	hiwat = tp->t_hiwat;
1877 	s = spltty();
1878 	if (tp->t_outq.c_cc > hiwat + 200)
1879 		while (tp->t_outq.c_cc > hiwat) {
1880 			ttstart(tp);
1881 			if (wait == 0) {
1882 				splx(s);
1883 				return (0);
1884 			}
1885 			SET(tp->t_state, TS_ASLEEP);
1886 			error = ltsleep(&tp->t_outq, (PZERO - 1) | PCATCH,
1887 			    "ttckoutq", hz, &tp->t_slock);
1888 			if (error == EINTR)
1889 				wait = 0;
1890 		}
1891 
1892 	splx(s);
1893 	return (1);
1894 }
1895 
1896 int
1897 ttycheckoutq(struct tty *tp, int wait)
1898 {
1899 	int	r, s;
1900 
1901 	s = spltty();
1902 	TTY_LOCK(tp);
1903 	r = ttycheckoutq_wlock(tp, wait);
1904 	TTY_UNLOCK(tp);
1905 	splx(s);
1906 	return (r);
1907 }
1908 
1909 /*
1910  * Process a write call on a tty device.
1911  */
1912 int
1913 ttwrite(struct tty *tp, struct uio *uio, int flag)
1914 {
1915 	u_char		*cp;
1916 	struct proc	*p;
1917 	int		cc, ce, i, hiwat, error, s;
1918 	size_t		cnt;
1919 	u_char		obuf[OBUFSIZ];
1920 
1921 	cp = NULL;
1922 	hiwat = tp->t_hiwat;
1923 	cnt = uio->uio_resid;
1924 	error = 0;
1925 	cc = 0;
1926  loop:
1927 	s = spltty();
1928 	TTY_LOCK(tp);
1929 	if (!CONNECTED(tp)) {
1930 		if (ISSET(tp->t_state, TS_ISOPEN)) {
1931 			TTY_UNLOCK(tp);
1932 			splx(s);
1933 			return (EIO);
1934 		} else if (flag & IO_NDELAY) {
1935 			TTY_UNLOCK(tp);
1936 			splx(s);
1937 			error = EWOULDBLOCK;
1938 			goto out;
1939 		} else {
1940 			/* Sleep awaiting carrier. */
1941 			error = ttysleep(tp,
1942 			    &tp->t_rawq, TTIPRI | PCATCH | PNORELOCK, ttopen, 0);
1943 			splx(s);
1944 			if (error)
1945 				goto out;
1946 			goto loop;
1947 		}
1948 	}
1949 	TTY_UNLOCK(tp);
1950 	splx(s);
1951 	/*
1952 	 * Hang the process if it's in the background.
1953 	 */
1954 	p = curproc;
1955 	if (isbackground(p, tp) &&
1956 	    ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1957 	    !sigismember(&p->p_sigctx.ps_sigignore, SIGTTOU) &&
1958 	    !sigismember(&p->p_sigctx.ps_sigmask, SIGTTOU)) {
1959 		if (p->p_pgrp->pg_jobc == 0) {
1960 			error = EIO;
1961 			goto out;
1962 		}
1963 		pgsignal(p->p_pgrp, SIGTTOU, 1);
1964 		s = spltty();
1965 		TTY_LOCK(tp);
1966 		error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
1967 		splx(s);
1968 		if (error)
1969 			goto out;
1970 		goto loop;
1971 	}
1972 	/*
1973 	 * Process the user's data in at most OBUFSIZ chunks.  Perform any
1974 	 * output translation.  Keep track of high water mark, sleep on
1975 	 * overflow awaiting device aid in acquiring new space.
1976 	 */
1977 	while (uio->uio_resid > 0 || cc > 0) {
1978 		if (ISSET(tp->t_lflag, FLUSHO)) {
1979 			uio->uio_resid = 0;
1980 			return (0);
1981 		}
1982 		if (tp->t_outq.c_cc > hiwat)
1983 			goto ovhiwat;
1984 		/*
1985 		 * Grab a hunk of data from the user, unless we have some
1986 		 * leftover from last time.
1987 		 */
1988 		if (cc == 0) {
1989 			cc = min(uio->uio_resid, OBUFSIZ);
1990 			cp = obuf;
1991 			error = uiomove(cp, cc, uio);
1992 			if (error) {
1993 				cc = 0;
1994 				goto out;
1995 			}
1996 		}
1997 		/*
1998 		 * If nothing fancy need be done, grab those characters we
1999 		 * can handle without any of ttyoutput's processing and
2000 		 * just transfer them to the output q.  For those chars
2001 		 * which require special processing (as indicated by the
2002 		 * bits in char_type), call ttyoutput.  After processing
2003 		 * a hunk of data, look for FLUSHO so ^O's will take effect
2004 		 * immediately.
2005 		 */
2006 		s = spltty();
2007 		TTY_LOCK(tp);
2008 		while (cc > 0) {
2009 			if (!ISSET(tp->t_oflag, OPOST))
2010 				ce = cc;
2011 			else {
2012 				ce = cc - scanc((u_int)cc, cp, char_type,
2013 				    CCLASSMASK);
2014 				/*
2015 				 * If ce is zero, then we're processing
2016 				 * a special character through ttyoutput.
2017 				 */
2018 				if (ce == 0) {
2019 					tp->t_rocount = 0;
2020 					if (ttyoutput(*cp, tp) >= 0) {
2021 						/* out of space */
2022 						TTY_UNLOCK(tp);
2023 						splx(s);
2024 						goto overfull;
2025 					}
2026 					cp++;
2027 					cc--;
2028 					if (ISSET(tp->t_lflag, FLUSHO) ||
2029 					    tp->t_outq.c_cc > hiwat) {
2030 						TTY_UNLOCK(tp);
2031 						splx(s);
2032 						goto ovhiwat;
2033 					}
2034 					continue;
2035 				}
2036 			}
2037 			/*
2038 			 * A bunch of normal characters have been found.
2039 			 * Transfer them en masse to the output queue and
2040 			 * continue processing at the top of the loop.
2041 			 * If there are any further characters in this
2042 			 * <= OBUFSIZ chunk, the first should be a character
2043 			 * requiring special handling by ttyoutput.
2044 			 */
2045 			tp->t_rocount = 0;
2046 			i = b_to_q(cp, ce, &tp->t_outq);
2047 			ce -= i;
2048 			tp->t_column += ce;
2049 			cp += ce, cc -= ce, tk_nout += ce;
2050 			tp->t_outcc += ce;
2051 			if (i > 0) {
2052 				/* out of space */
2053 				TTY_UNLOCK(tp);
2054 				splx(s);
2055 				goto overfull;
2056 			}
2057 			if (ISSET(tp->t_lflag, FLUSHO) ||
2058 			    tp->t_outq.c_cc > hiwat)
2059 				break;
2060 		}
2061 		TTY_UNLOCK(tp);
2062 		splx(s);
2063 		ttstart(tp);
2064 	}
2065 
2066  out:
2067 	/*
2068 	 * If cc is nonzero, we leave the uio structure inconsistent, as the
2069 	 * offset and iov pointers have moved forward, but it doesn't matter
2070 	 * (the call will either return short or restart with a new uio).
2071 	 */
2072 	uio->uio_resid += cc;
2073 	return (error);
2074 
2075  overfull:
2076 	/*
2077 	 * Since we are using ring buffers, if we can't insert any more into
2078 	 * the output queue, we can assume the ring is full and that someone
2079 	 * forgot to set the high water mark correctly.  We set it and then
2080 	 * proceed as normal.
2081 	 */
2082 	hiwat = tp->t_outq.c_cc - 1;
2083 
2084  ovhiwat:
2085 	ttstart(tp);
2086 	s = spltty();
2087 	TTY_LOCK(tp);
2088 	/*
2089 	 * This can only occur if FLUSHO is set in t_lflag,
2090 	 * or if ttstart/oproc is synchronous (or very fast).
2091 	 */
2092 	if (tp->t_outq.c_cc <= hiwat) {
2093 		TTY_UNLOCK(tp);
2094 		splx(s);
2095 		goto loop;
2096 	}
2097 	if (flag & IO_NDELAY) {
2098 		TTY_UNLOCK(tp);
2099 		splx(s);
2100 		error = (uio->uio_resid == cnt) ? EWOULDBLOCK : 0;
2101 		goto out;
2102 	}
2103 	SET(tp->t_state, TS_ASLEEP);
2104 	error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH | PNORELOCK, ttyout, 0);
2105 	splx(s);
2106 	if (error)
2107 		goto out;
2108 	goto loop;
2109 }
2110 
2111 /*
2112  * Rubout one character from the rawq of tp
2113  * as cleanly as possible.
2114  * Called with tty slock held.
2115  */
2116 void
2117 ttyrub(int c, struct tty *tp)
2118 {
2119 	u_char	*cp;
2120 	int	savecol, tabc, s;
2121 
2122 	if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
2123 		return;
2124 	CLR(tp->t_lflag, FLUSHO);
2125 	if (ISSET(tp->t_lflag, ECHOE)) {
2126 		if (tp->t_rocount == 0) {
2127 			/*
2128 			 * Screwed by ttwrite; retype
2129 			 */
2130 			ttyretype(tp);
2131 			return;
2132 		}
2133 		if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
2134 			ttyrubo(tp, 2);
2135 		else {
2136 			CLR(c, ~TTY_CHARMASK);
2137 			switch (CCLASS(c)) {
2138 			case ORDINARY:
2139 				ttyrubo(tp, 1);
2140 				break;
2141 			case BACKSPACE:
2142 			case CONTROL:
2143 			case NEWLINE:
2144 			case RETURN:
2145 			case VTAB:
2146 				if (ISSET(tp->t_lflag, ECHOCTL))
2147 					ttyrubo(tp, 2);
2148 				break;
2149 			case TAB:
2150 				if (tp->t_rocount < tp->t_rawq.c_cc) {
2151 					ttyretype(tp);
2152 					return;
2153 				}
2154 				s = spltty();
2155 				savecol = tp->t_column;
2156 				SET(tp->t_state, TS_CNTTB);
2157 				SET(tp->t_lflag, FLUSHO);
2158 				tp->t_column = tp->t_rocol;
2159 				for (cp = firstc(&tp->t_rawq, &tabc); cp;
2160 				    cp = nextc(&tp->t_rawq, cp, &tabc))
2161 					ttyecho(tabc, tp);
2162 				CLR(tp->t_lflag, FLUSHO);
2163 				CLR(tp->t_state, TS_CNTTB);
2164 				splx(s);
2165 
2166 				/* savecol will now be length of the tab. */
2167 				savecol -= tp->t_column;
2168 				tp->t_column += savecol;
2169 				if (savecol > 8)
2170 					savecol = 8;	/* overflow screw */
2171 				while (--savecol >= 0)
2172 					(void)ttyoutput('\b', tp);
2173 				break;
2174 			default:			/* XXX */
2175 				(void)printf("ttyrub: would panic c = %d, "
2176 				    "val = %d\n", c, CCLASS(c));
2177 			}
2178 		}
2179 	} else if (ISSET(tp->t_lflag, ECHOPRT)) {
2180 		if (!ISSET(tp->t_state, TS_ERASE)) {
2181 			SET(tp->t_state, TS_ERASE);
2182 			(void)ttyoutput('\\', tp);
2183 		}
2184 		ttyecho(c, tp);
2185 	} else
2186 		ttyecho(tp->t_cc[VERASE], tp);
2187 	--tp->t_rocount;
2188 }
2189 
2190 /*
2191  * Back over cnt characters, erasing them.
2192  * Called with tty slock held.
2193  */
2194 static void
2195 ttyrubo(struct tty *tp, int cnt)
2196 {
2197 
2198 	while (cnt-- > 0) {
2199 		(void)ttyoutput('\b', tp);
2200 		(void)ttyoutput(' ', tp);
2201 		(void)ttyoutput('\b', tp);
2202 	}
2203 }
2204 
2205 /*
2206  * ttyretype --
2207  *	Reprint the rawq line.  Note, it is assumed that c_cc has already
2208  *	been checked.
2209  *
2210  * Called with tty slock held.
2211  */
2212 void
2213 ttyretype(struct tty *tp)
2214 {
2215 	u_char	*cp;
2216 	int	s, c;
2217 
2218 	/* Echo the reprint character. */
2219 	if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
2220 		ttyecho(tp->t_cc[VREPRINT], tp);
2221 
2222 	(void)ttyoutput('\n', tp);
2223 
2224 	s = spltty();
2225 	for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
2226 		ttyecho(c, tp);
2227 	for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
2228 		ttyecho(c, tp);
2229 	CLR(tp->t_state, TS_ERASE);
2230 	splx(s);
2231 
2232 	tp->t_rocount = tp->t_rawq.c_cc;
2233 	tp->t_rocol = 0;
2234 }
2235 
2236 /*
2237  * Echo a typed character to the terminal.
2238  * Called with tty slock held.
2239  */
2240 static void
2241 ttyecho(int c, struct tty *tp)
2242 {
2243 
2244 	if (!ISSET(tp->t_state, TS_CNTTB))
2245 		CLR(tp->t_lflag, FLUSHO);
2246 	if ((!ISSET(tp->t_lflag, ECHO) &&
2247 	    (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) ||
2248 	    ISSET(tp->t_lflag, EXTPROC))
2249 		return;
2250 	if (((ISSET(tp->t_lflag, ECHOCTL) &&
2251 	    (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) ||
2252 	    ISSET(c, TTY_CHARMASK) == 0177)) {
2253 		(void)ttyoutput('^', tp);
2254 		CLR(c, ~TTY_CHARMASK);
2255 		if (c == 0177)
2256 			c = '?';
2257 		else
2258 			c += 'A' - 1;
2259 	}
2260 	(void)ttyoutput(c, tp);
2261 }
2262 
2263 /*
2264  * Wake up any readers on a tty.
2265  * Called with tty slock held.
2266  */
2267 void
2268 ttwakeup(struct tty *tp)
2269 {
2270 
2271 	selnotify(&tp->t_rsel, NOTE_SUBMIT);
2272 	if (ISSET(tp->t_state, TS_ASYNC))
2273 		pgsignal(tp->t_pgrp, SIGIO, 1);
2274 	wakeup((caddr_t)&tp->t_rawq);
2275 }
2276 
2277 /*
2278  * Look up a code for a specified speed in a conversion table;
2279  * used by drivers to map software speed values to hardware parameters.
2280  */
2281 int
2282 ttspeedtab(int speed, const struct speedtab *table)
2283 {
2284 
2285 	for (; table->sp_speed != -1; table++)
2286 		if (table->sp_speed == speed)
2287 			return (table->sp_code);
2288 	return (-1);
2289 }
2290 
2291 /*
2292  * Set tty hi and low water marks.
2293  *
2294  * Try to arrange the dynamics so there's about one second
2295  * from hi to low water.
2296  */
2297 void
2298 ttsetwater(struct tty *tp)
2299 {
2300 	int	cps, x;
2301 
2302 #define	CLAMP(x, h, l)	((x) > h ? h : ((x) < l) ? l : (x))
2303 
2304 	cps = tp->t_ospeed / 10;
2305 	tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
2306 	x += cps;
2307 	x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
2308 	tp->t_hiwat = roundup(x, CBSIZE);
2309 #undef	CLAMP
2310 }
2311 
2312 /*
2313  * Report on state of foreground process group.
2314  * Call with tty slock held.
2315  */
2316 void
2317 ttyinfo(struct tty *tp)
2318 {
2319 	struct lwp	*l;
2320 	struct proc	*p, *pick;
2321 	struct timeval	utime, stime;
2322 	int		tmp;
2323 
2324 	if (ttycheckoutq_wlock(tp, 0) == 0)
2325 		return;
2326 
2327 	/* Print load average. */
2328 	tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
2329 	ttyprintf_nolock(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
2330 
2331 	if (tp->t_session == NULL)
2332 		ttyprintf_nolock(tp, "not a controlling terminal\n");
2333 	else if (tp->t_pgrp == NULL)
2334 		ttyprintf_nolock(tp, "no foreground process group\n");
2335 	else if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0)
2336 		ttyprintf_nolock(tp, "empty foreground process group\n");
2337 	else {
2338 		/* Pick interesting process. */
2339 		for (pick = NULL; p != NULL; p = LIST_NEXT(p, p_pglist))
2340 			if (proc_compare(pick, p))
2341 				pick = p;
2342 
2343 		ttyprintf_nolock(tp, " cmd: %s %d [", pick->p_comm, pick->p_pid);
2344 		LIST_FOREACH(l, &pick->p_lwps, l_sibling)
2345 		    ttyprintf_nolock(tp, "%s%s",
2346 		    l->l_stat == LSONPROC ? "running" :
2347 		    l->l_stat == LSRUN ? "runnable" :
2348 		    l->l_wmesg ? l->l_wmesg : "iowait",
2349 			(LIST_NEXT(l, l_sibling) != NULL) ? " " : "] ");
2350 
2351 		calcru(pick, &utime, &stime, NULL);
2352 
2353 		/* Round up and print user time. */
2354 		utime.tv_usec += 5000;
2355 		if (utime.tv_usec >= 1000000) {
2356 			utime.tv_sec += 1;
2357 			utime.tv_usec -= 1000000;
2358 		}
2359 		ttyprintf_nolock(tp, "%ld.%02ldu ", (long int)utime.tv_sec,
2360 		    (long int)utime.tv_usec / 10000);
2361 
2362 		/* Round up and print system time. */
2363 		stime.tv_usec += 5000;
2364 		if (stime.tv_usec >= 1000000) {
2365 			stime.tv_sec += 1;
2366 			stime.tv_usec -= 1000000;
2367 		}
2368 		ttyprintf_nolock(tp, "%ld.%02lds ", (long int)stime.tv_sec,
2369 		    (long int)stime.tv_usec / 10000);
2370 
2371 #define	pgtok(a)	(((u_long) ((a) * PAGE_SIZE) / 1024))
2372 		/* Print percentage CPU. */
2373 		tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
2374 		ttyprintf_nolock(tp, "%d%% ", tmp / 100);
2375 
2376 		/* Print resident set size. */
2377 		if (pick->p_stat == SIDL || P_ZOMBIE(pick))
2378 			tmp = 0;
2379 		else {
2380 			struct vmspace *vm = pick->p_vmspace;
2381 			tmp = pgtok(vm_resident_count(vm));
2382 		}
2383 		ttyprintf_nolock(tp, "%dk\n", tmp);
2384 	}
2385 	tp->t_rocount = 0;	/* so pending input will be retyped if BS */
2386 }
2387 
2388 /*
2389  * Returns 1 if p2 is "better" than p1
2390  *
2391  * The algorithm for picking the "interesting" process is thus:
2392  *
2393  *	1) Only foreground processes are eligible - implied.
2394  *	2) Runnable processes are favored over anything else.  The runner
2395  *	   with the highest CPU utilization is picked (p_estcpu).  Ties are
2396  *	   broken by picking the highest pid.
2397  *	3) The sleeper with the shortest sleep time is next.  With ties,
2398  *	   we pick out just "short-term" sleepers (P_SINTR == 0).
2399  *	4) Further ties are broken by picking the highest pid.
2400  */
2401 #define	ISRUN(p)	((p)->p_nrlwps > 0)
2402 #define	TESTAB(a, b)	((a)<<1 | (b))
2403 #define	ONLYA	2
2404 #define	ONLYB	1
2405 #define	BOTH	3
2406 
2407 static int
2408 proc_compare(struct proc *p1, struct proc *p2)
2409 {
2410 
2411 	if (p1 == NULL)
2412 		return (1);
2413 	/*
2414 	 * see if at least one of them is runnable
2415 	 */
2416 	switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2417 	case ONLYA:
2418 		return (0);
2419 	case ONLYB:
2420 		return (1);
2421 	case BOTH:
2422 		/*
2423 		 * tie - favor one with highest recent CPU utilization
2424 		 */
2425 		if (p2->p_estcpu > p1->p_estcpu)
2426 			return (1);
2427 		if (p1->p_estcpu > p2->p_estcpu)
2428 			return (0);
2429 		return (p2->p_pid > p1->p_pid);	/* tie - return highest pid */
2430 	}
2431 	/*
2432  	 * weed out zombies
2433 	 */
2434 	switch (TESTAB(P_ZOMBIE(p1), P_ZOMBIE(p2))) {
2435 	case ONLYA:
2436 		return (1);
2437 	case ONLYB:
2438 		return (0);
2439 	case BOTH:
2440 		return (p2->p_pid > p1->p_pid);	/* tie - return highest pid */
2441 	}
2442 #if 0 /* XXX NJWLWP */
2443 	/*
2444 	 * pick the one with the smallest sleep time
2445 	 */
2446 	if (p2->p_slptime > p1->p_slptime)
2447 		return (0);
2448 	if (p1->p_slptime > p2->p_slptime)
2449 		return (1);
2450 	/*
2451 	 * favor one sleeping in a non-interruptible sleep
2452 	 */
2453 	if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
2454 		return (1);
2455 	if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
2456 		return (0);
2457 #endif
2458 	return (p2->p_pid > p1->p_pid);		/* tie - return highest pid */
2459 }
2460 
2461 /*
2462  * Output char to tty; console putchar style.
2463  * Can be called with tty lock held through kprintf() machinery..
2464  */
2465 int
2466 tputchar(int c, int flags, struct tty *tp)
2467 {
2468 	int s, r = 0;
2469 
2470 	s = spltty();
2471 	if ((flags & NOLOCK) == 0)
2472 		simple_lock(&tp->t_slock);
2473 	if (!CONNECTED(tp)) {
2474 		r = -1;
2475 		goto out;
2476 	}
2477 	if (c == '\n')
2478 		(void)ttyoutput('\r', tp);
2479 	(void)ttyoutput(c, tp);
2480 	ttstart(tp);
2481 out:
2482 	if ((flags & NOLOCK) == 0)
2483 		TTY_UNLOCK(tp);
2484 	splx(s);
2485 	return (r);
2486 }
2487 
2488 /*
2489  * Sleep on chan, returning ERESTART if tty changed while we napped and
2490  * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep.  If
2491  * the tty is revoked, restarting a pending call will redo validation done
2492  * at the start of the call.
2493  *
2494  * Must be called with the tty slock held.
2495  */
2496 int
2497 ttysleep(struct tty *tp, void *chan, int pri, const char *wmesg, int timo)
2498 {
2499 	int	error;
2500 	short	gen;
2501 
2502 	gen = tp->t_gen;
2503 	if ((error = ltsleep(chan, pri, wmesg, timo, &tp->t_slock)) != 0)
2504 		return (error);
2505 	return (tp->t_gen == gen ? 0 : ERESTART);
2506 }
2507 
2508 /*
2509  * Initialise the global tty list.
2510  */
2511 void
2512 tty_init(void)
2513 {
2514 
2515 	ttyldisc_init();
2516 }
2517 
2518 /*
2519  * Attach a tty to the tty list.
2520  *
2521  * This should be called ONLY once per real tty (including pty's).
2522  * eg, on the sparc, the keyboard and mouse have struct tty's that are
2523  * distinctly NOT usable as tty's, and thus should not be attached to
2524  * the ttylist.  This is why this call is not done from ttymalloc().
2525  *
2526  * Device drivers should attach tty's at a similar time that they are
2527  * ttymalloc()'ed, or, for the case of statically allocated struct tty's
2528  * either in the attach or (first) open routine.
2529  */
2530 void
2531 tty_attach(struct tty *tp)
2532 {
2533 
2534 	simple_lock(&ttylist_slock);
2535 	TAILQ_INSERT_TAIL(&ttylist, tp, tty_link);
2536 	++tty_count;
2537 	simple_unlock(&ttylist_slock);
2538 }
2539 
2540 /*
2541  * Remove a tty from the tty list.
2542  */
2543 void
2544 tty_detach(struct tty *tp)
2545 {
2546 
2547 	simple_lock(&ttylist_slock);
2548 	--tty_count;
2549 #ifdef DIAGNOSTIC
2550 	if (tty_count < 0)
2551 		panic("tty_detach: tty_count < 0");
2552 #endif
2553 	TAILQ_REMOVE(&ttylist, tp, tty_link);
2554 	simple_unlock(&ttylist_slock);
2555 }
2556 
2557 /*
2558  * Allocate a tty structure and its associated buffers.
2559  */
2560 struct tty *
2561 ttymalloc(void)
2562 {
2563 	struct tty	*tp;
2564 
2565 	tp = pool_get(&tty_pool, PR_WAITOK);
2566 	memset(tp, 0, sizeof(*tp));
2567 	simple_lock_init(&tp->t_slock);
2568 	callout_init(&tp->t_rstrt_ch);
2569 	/* XXX: default to 1024 chars for now */
2570 	clalloc(&tp->t_rawq, 1024, 1);
2571 	clalloc(&tp->t_canq, 1024, 1);
2572 	/* output queue doesn't need quoting */
2573 	clalloc(&tp->t_outq, 1024, 0);
2574 	/* Set default line discipline. */
2575 	tp->t_linesw = linesw[0];
2576 	return (tp);
2577 }
2578 
2579 /*
2580  * Free a tty structure and its buffers.
2581  *
2582  * Be sure to call tty_detach() for any tty that has been
2583  * tty_attach()ed.
2584  */
2585 void
2586 ttyfree(struct tty *tp)
2587 {
2588 
2589 	callout_stop(&tp->t_rstrt_ch);
2590 	clfree(&tp->t_rawq);
2591 	clfree(&tp->t_canq);
2592 	clfree(&tp->t_outq);
2593 	pool_put(&tty_pool, tp);
2594 }
2595 
2596 /*
2597  * ttyprintf_nolock: send a message to a specific tty, without locking.
2598  *
2599  * => should be used only by tty driver or anything that knows the
2600  *    underlying tty will not be revoked(2)'d away.  [otherwise,
2601  *    use tprintf]
2602  */
2603 static void
2604 ttyprintf_nolock(struct tty *tp, const char *fmt, ...)
2605 {
2606 	va_list ap;
2607 
2608 	/* No mutex needed; going to process TTY. */
2609 	va_start(ap, fmt);
2610 	kprintf(fmt, TOTTY|NOLOCK, tp, NULL, ap);
2611 	va_end(ap);
2612 }
2613