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