tty.c revision 1.78 1 /* $NetBSD: tty.c,v 1.78 1997/03/29 23:26:27 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)tty.c 8.8 (Berkeley) 1/21/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/ioctl.h>
46 #include <sys/proc.h>
47 #define TTYDEFCHARS
48 #include <sys/tty.h>
49 #undef TTYDEFCHARS
50 #include <sys/file.h>
51 #include <sys/conf.h>
52 #include <sys/dkstat.h>
53 #include <sys/uio.h>
54 #include <sys/kernel.h>
55 #include <sys/vnode.h>
56 #include <sys/syslog.h>
57 #include <sys/malloc.h>
58 #include <sys/signalvar.h>
59 #include <sys/resourcevar.h>
60 #include <sys/poll.h>
61
62 #include <vm/vm.h>
63
64 static int ttnread __P((struct tty *));
65 static void ttyblock __P((struct tty *));
66 static void ttyecho __P((int, struct tty *));
67 static void ttyrubo __P((struct tty *, int));
68 static int proc_compare __P((struct proc *, struct proc *));
69
70 /* Symbolic sleep message strings. */
71 char ttclos[] = "ttycls";
72 char ttopen[] = "ttyopn";
73 char ttybg[] = "ttybg";
74 #ifdef REAL_CLISTS
75 char ttybuf[] = "ttybuf";
76 #endif
77 char ttyin[] = "ttyin";
78 char ttyout[] = "ttyout";
79
80 /*
81 * Table with character classes and parity. The 8th bit indicates parity,
82 * the 7th bit indicates the character is an alphameric or underscore (for
83 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
84 * are 0 then the character needs no special processing on output; classes
85 * other than 0 might be translated or (not currently) require delays.
86 */
87 #define E 0x00 /* Even parity. */
88 #define O 0x80 /* Odd parity. */
89 #define PARITY(c) (char_type[c] & O)
90
91 #define ALPHA 0x40 /* Alpha or underscore. */
92 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
93
94 #define CCLASSMASK 0x3f
95 #define CCLASS(c) (char_type[c] & CCLASSMASK)
96
97 #define BS BACKSPACE
98 #define CC CONTROL
99 #define CR RETURN
100 #define NA ORDINARY | ALPHA
101 #define NL NEWLINE
102 #define NO ORDINARY
103 #define TB TAB
104 #define VT VTAB
105
106 char const char_type[] = {
107 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
108 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
109 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
110 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
111 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
112 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
113 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
114 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
115 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
116 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
117 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
118 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
119 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
120 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
121 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
122 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
123 /*
124 * Meta chars; should be settable per character set;
125 * for now, treat them all as normal characters.
126 */
127 NA, NA, NA, NA, NA, NA, NA, NA,
128 NA, NA, NA, NA, NA, NA, NA, NA,
129 NA, NA, NA, NA, NA, NA, NA, NA,
130 NA, NA, NA, NA, NA, NA, NA, NA,
131 NA, NA, NA, NA, NA, NA, NA, NA,
132 NA, NA, NA, NA, NA, NA, NA, NA,
133 NA, NA, NA, NA, NA, NA, NA, NA,
134 NA, NA, NA, NA, NA, NA, NA, NA,
135 NA, NA, NA, NA, NA, NA, NA, NA,
136 NA, NA, NA, NA, NA, NA, NA, NA,
137 NA, NA, NA, NA, NA, NA, NA, NA,
138 NA, NA, NA, NA, NA, NA, NA, NA,
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 };
144 #undef BS
145 #undef CC
146 #undef CR
147 #undef NA
148 #undef NL
149 #undef NO
150 #undef TB
151 #undef VT
152
153 /* Macros to clear/set/test flags. */
154 #define SET(t, f) (t) |= (f)
155 #define CLR(t, f) (t) &= ~((unsigned)(f))
156 #define ISSET(t, f) ((t) & (f))
157
158 struct ttylist_head ttylist; /* TAILQ_HEAD */
159 int tty_count;
160
161 /*
162 * Initial open of tty, or (re)entry to standard tty line discipline.
163 */
164 int
165 ttyopen(device, tp)
166 dev_t device;
167 register struct tty *tp;
168 {
169 int s;
170
171 s = spltty();
172 tp->t_dev = device;
173 if (!ISSET(tp->t_state, TS_ISOPEN)) {
174 SET(tp->t_state, TS_ISOPEN);
175 bzero(&tp->t_winsize, sizeof(tp->t_winsize));
176 #ifdef COMPAT_OLDTTY
177 tp->t_flags = 0;
178 #endif
179 }
180 CLR(tp->t_state, TS_WOPEN);
181 splx(s);
182 return (0);
183 }
184
185 /*
186 * Handle close() on a tty line: flush and set to initial state,
187 * bumping generation number so that pending read/write calls
188 * can detect recycling of the tty.
189 */
190 int
191 ttyclose(tp)
192 register struct tty *tp;
193 {
194 extern struct tty *constty; /* Temporary virtual console. */
195
196 if (constty == tp)
197 constty = NULL;
198
199 ttyflush(tp, FREAD | FWRITE);
200
201 tp->t_gen++;
202 tp->t_pgrp = NULL;
203 tp->t_session = NULL;
204 tp->t_state = 0;
205 return (0);
206 }
207
208 #define FLUSHQ(q) { \
209 if ((q)->c_cc) \
210 ndflush(q, (q)->c_cc); \
211 }
212
213 /* Is 'c' a line delimiter ("break" character)? */
214 #define TTBREAKC(c) \
215 ((c) == '\n' || ((c) == cc[VEOF] || \
216 (c) == cc[VEOL] || (((c) == cc[VEOL2]) && (c) != _POSIX_VDISABLE)))
217
218
219 /*
220 * Process input of a single character received on a tty.
221 */
222 int
223 ttyinput(c, tp)
224 register int c;
225 register struct tty *tp;
226 {
227 register int iflag, lflag;
228 register u_char *cc;
229 int i, error;
230
231 /*
232 * Unless the receiver is enabled, drop incoming data.
233 */
234 if (!ISSET(tp->t_cflag, CREAD))
235 return (0);
236
237 /*
238 * If input is pending take it first.
239 */
240 lflag = tp->t_lflag;
241 if (ISSET(lflag, PENDIN))
242 ttypend(tp);
243 /*
244 * Gather stats.
245 */
246 if (ISSET(lflag, ICANON)) {
247 ++tk_cancc;
248 ++tp->t_cancc;
249 } else {
250 ++tk_rawcc;
251 ++tp->t_rawcc;
252 }
253 ++tk_nin;
254
255 /* Handle exceptional conditions (break, parity, framing). */
256 cc = tp->t_cc;
257 iflag = tp->t_iflag;
258 if ((error = (ISSET(c, TTY_ERRORMASK))) != 0) {
259 CLR(c, TTY_ERRORMASK);
260 if (ISSET(error, TTY_FE) && !c) { /* Break. */
261 if (ISSET(iflag, IGNBRK))
262 goto endcase;
263 else if (ISSET(iflag, BRKINT) &&
264 ISSET(lflag, ISIG) &&
265 (cc[VINTR] != _POSIX_VDISABLE))
266 c = cc[VINTR];
267 else if (ISSET(iflag, PARMRK))
268 goto parmrk;
269 } else if ((ISSET(error, TTY_PE) &&
270 ISSET(iflag, INPCK)) || ISSET(error, TTY_FE)) {
271 if (ISSET(iflag, IGNPAR))
272 goto endcase;
273 else if (ISSET(iflag, PARMRK)) {
274 parmrk: (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
275 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
276 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
277 goto endcase;
278 } else
279 c = 0;
280 }
281 }
282 /*
283 * In tandem mode, check high water mark.
284 */
285 if (ISSET(iflag, IXOFF) || ISSET(tp->t_cflag, CHWFLOW))
286 ttyblock(tp);
287 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
288 CLR(c, 0x80);
289 if (!ISSET(lflag, EXTPROC)) {
290 /*
291 * Check for literal nexting very first
292 */
293 if (ISSET(tp->t_state, TS_LNCH)) {
294 SET(c, TTY_QUOTE);
295 CLR(tp->t_state, TS_LNCH);
296 }
297 /*
298 * Scan for special characters. This code
299 * is really just a big case statement with
300 * non-constant cases. The bottom of the
301 * case statement is labeled ``endcase'', so goto
302 * it after a case match, or similar.
303 */
304
305 /*
306 * Control chars which aren't controlled
307 * by ICANON, ISIG, or IXON.
308 */
309 if (ISSET(lflag, IEXTEN)) {
310 if (CCEQ(cc[VLNEXT], c)) {
311 if (ISSET(lflag, ECHO)) {
312 if (ISSET(lflag, ECHOE)) {
313 (void)ttyoutput('^', tp);
314 (void)ttyoutput('\b', tp);
315 } else
316 ttyecho(c, tp);
317 }
318 SET(tp->t_state, TS_LNCH);
319 goto endcase;
320 }
321 if (CCEQ(cc[VDISCARD], c)) {
322 if (ISSET(lflag, FLUSHO))
323 CLR(tp->t_lflag, FLUSHO);
324 else {
325 ttyflush(tp, FWRITE);
326 ttyecho(c, tp);
327 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
328 ttyretype(tp);
329 SET(tp->t_lflag, FLUSHO);
330 }
331 goto startoutput;
332 }
333 }
334 /*
335 * Signals.
336 */
337 if (ISSET(lflag, ISIG)) {
338 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
339 if (!ISSET(lflag, NOFLSH))
340 ttyflush(tp, FREAD | FWRITE);
341 ttyecho(c, tp);
342 pgsignal(tp->t_pgrp,
343 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
344 goto endcase;
345 }
346 if (CCEQ(cc[VSUSP], c)) {
347 if (!ISSET(lflag, NOFLSH))
348 ttyflush(tp, FREAD);
349 ttyecho(c, tp);
350 pgsignal(tp->t_pgrp, SIGTSTP, 1);
351 goto endcase;
352 }
353 }
354 /*
355 * Handle start/stop characters.
356 */
357 if (ISSET(iflag, IXON)) {
358 if (CCEQ(cc[VSTOP], c)) {
359 if (!ISSET(tp->t_state, TS_TTSTOP)) {
360 SET(tp->t_state, TS_TTSTOP);
361 (*cdevsw[major(tp->t_dev)].d_stop)(tp,
362 0);
363 return (0);
364 }
365 if (!CCEQ(cc[VSTART], c))
366 return (0);
367 /*
368 * if VSTART == VSTOP then toggle
369 */
370 goto endcase;
371 }
372 if (CCEQ(cc[VSTART], c))
373 goto restartoutput;
374 }
375 /*
376 * IGNCR, ICRNL, & INLCR
377 */
378 if (c == '\r') {
379 if (ISSET(iflag, IGNCR))
380 goto endcase;
381 else if (ISSET(iflag, ICRNL))
382 c = '\n';
383 } else if (c == '\n' && ISSET(iflag, INLCR))
384 c = '\r';
385 }
386 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
387 /*
388 * From here on down canonical mode character
389 * processing takes place.
390 */
391 /*
392 * erase (^H / ^?)
393 */
394 if (CCEQ(cc[VERASE], c)) {
395 if (tp->t_rawq.c_cc)
396 ttyrub(unputc(&tp->t_rawq), tp);
397 goto endcase;
398 }
399 /*
400 * kill (^U)
401 */
402 if (CCEQ(cc[VKILL], c)) {
403 if (ISSET(lflag, ECHOKE) &&
404 tp->t_rawq.c_cc == tp->t_rocount &&
405 !ISSET(lflag, ECHOPRT))
406 while (tp->t_rawq.c_cc)
407 ttyrub(unputc(&tp->t_rawq), tp);
408 else {
409 ttyecho(c, tp);
410 if (ISSET(lflag, ECHOK) ||
411 ISSET(lflag, ECHOKE))
412 ttyecho('\n', tp);
413 FLUSHQ(&tp->t_rawq);
414 tp->t_rocount = 0;
415 }
416 CLR(tp->t_state, TS_LOCAL);
417 goto endcase;
418 }
419 /*
420 * word erase (^W)
421 */
422 if (CCEQ(cc[VWERASE], c)) {
423 int alt = ISSET(lflag, ALTWERASE);
424 int ctype;
425
426 /*
427 * erase whitespace
428 */
429 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
430 ttyrub(c, tp);
431 if (c == -1)
432 goto endcase;
433 /*
434 * erase last char of word and remember the
435 * next chars type (for ALTWERASE)
436 */
437 ttyrub(c, tp);
438 c = unputc(&tp->t_rawq);
439 if (c == -1)
440 goto endcase;
441 if (c == ' ' || c == '\t') {
442 (void)putc(c, &tp->t_rawq);
443 goto endcase;
444 }
445 ctype = ISALPHA(c);
446 /*
447 * erase rest of word
448 */
449 do {
450 ttyrub(c, tp);
451 c = unputc(&tp->t_rawq);
452 if (c == -1)
453 goto endcase;
454 } while (c != ' ' && c != '\t' &&
455 (alt == 0 || ISALPHA(c) == ctype));
456 (void)putc(c, &tp->t_rawq);
457 goto endcase;
458 }
459 /*
460 * reprint line (^R)
461 */
462 if (CCEQ(cc[VREPRINT], c)) {
463 ttyretype(tp);
464 goto endcase;
465 }
466 /*
467 * ^T - kernel info and generate SIGINFO
468 */
469 if (CCEQ(cc[VSTATUS], c)) {
470 if (ISSET(lflag, ISIG))
471 pgsignal(tp->t_pgrp, SIGINFO, 1);
472 if (!ISSET(lflag, NOKERNINFO))
473 ttyinfo(tp);
474 goto endcase;
475 }
476 }
477 /*
478 * Check for input buffer overflow
479 */
480 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
481 if (ISSET(iflag, IMAXBEL)) {
482 if (tp->t_outq.c_cc < tp->t_hiwat)
483 (void)ttyoutput(CTRL('g'), tp);
484 } else
485 ttyflush(tp, FREAD | FWRITE);
486 goto endcase;
487 }
488 /*
489 * Put data char in q for user and
490 * wakeup on seeing a line delimiter.
491 */
492 if (putc(c, &tp->t_rawq) >= 0) {
493 if (!ISSET(lflag, ICANON)) {
494 ttwakeup(tp);
495 ttyecho(c, tp);
496 goto endcase;
497 }
498 if (TTBREAKC(c)) {
499 tp->t_rocount = 0;
500 catq(&tp->t_rawq, &tp->t_canq);
501 ttwakeup(tp);
502 } else if (tp->t_rocount++ == 0)
503 tp->t_rocol = tp->t_column;
504 if (ISSET(tp->t_state, TS_ERASE)) {
505 /*
506 * end of prterase \.../
507 */
508 CLR(tp->t_state, TS_ERASE);
509 (void)ttyoutput('/', tp);
510 }
511 i = tp->t_column;
512 ttyecho(c, tp);
513 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
514 /*
515 * Place the cursor over the '^' of the ^D.
516 */
517 i = min(2, tp->t_column - i);
518 while (i > 0) {
519 (void)ttyoutput('\b', tp);
520 i--;
521 }
522 }
523 }
524 endcase:
525 /*
526 * IXANY means allow any character to restart output.
527 */
528 if (ISSET(tp->t_state, TS_TTSTOP) &&
529 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
530 return (0);
531 restartoutput:
532 CLR(tp->t_lflag, FLUSHO);
533 CLR(tp->t_state, TS_TTSTOP);
534 startoutput:
535 return (ttstart(tp));
536 }
537
538 /*
539 * Output a single character on a tty, doing output processing
540 * as needed (expanding tabs, newline processing, etc.).
541 * Returns < 0 if succeeds, otherwise returns char to resend.
542 * Must be recursive.
543 */
544 int
545 ttyoutput(c, tp)
546 register int c;
547 register struct tty *tp;
548 {
549 register long oflag;
550 register int col, notout, s;
551
552 oflag = tp->t_oflag;
553 if (!ISSET(oflag, OPOST)) {
554 if (ISSET(tp->t_lflag, FLUSHO))
555 return (-1);
556 if (putc(c, &tp->t_outq))
557 return (c);
558 tk_nout++;
559 tp->t_outcc++;
560 return (-1);
561 }
562 /*
563 * Do tab expansion if OXTABS is set. Special case if we external
564 * processing, we don't do the tab expansion because we'll probably
565 * get it wrong. If tab expansion needs to be done, let it happen
566 * externally.
567 */
568 CLR(c, ~TTY_CHARMASK);
569 if (c == '\t' &&
570 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
571 c = 8 - (tp->t_column & 7);
572 if (ISSET(tp->t_lflag, FLUSHO)) {
573 notout = 0;
574 } else {
575 s = spltty(); /* Don't interrupt tabs. */
576 notout = b_to_q(" ", c, &tp->t_outq);
577 c -= notout;
578 tk_nout += c;
579 tp->t_outcc += c;
580 splx(s);
581 }
582 tp->t_column += c;
583 return (notout ? '\t' : -1);
584 }
585 if (c == CEOT && ISSET(oflag, ONOEOT))
586 return (-1);
587
588 /*
589 * Newline translation: if ONLCR is set,
590 * translate newline into "\r\n".
591 */
592 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
593 tk_nout++;
594 tp->t_outcc++;
595 if (putc('\r', &tp->t_outq))
596 return (c);
597 }
598 tk_nout++;
599 tp->t_outcc++;
600 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
601 return (c);
602
603 col = tp->t_column;
604 switch (CCLASS(c)) {
605 case BACKSPACE:
606 if (col > 0)
607 --col;
608 break;
609 case CONTROL:
610 break;
611 case NEWLINE:
612 case RETURN:
613 col = 0;
614 break;
615 case ORDINARY:
616 ++col;
617 break;
618 case TAB:
619 col = (col + 8) & ~7;
620 break;
621 }
622 tp->t_column = col;
623 return (-1);
624 }
625
626 /*
627 * Ioctls for all tty devices. Called after line-discipline specific ioctl
628 * has been called to do discipline-specific functions and/or reject any
629 * of these ioctl commands.
630 */
631 /* ARGSUSED */
632 int
633 ttioctl(tp, cmd, data, flag, p)
634 register struct tty *tp;
635 u_long cmd;
636 caddr_t data;
637 int flag;
638 struct proc *p;
639 {
640 extern struct tty *constty; /* Temporary virtual console. */
641 extern int nlinesw;
642 int s, error;
643
644 /* If the ioctl involves modification, hang if in the background. */
645 switch (cmd) {
646 case TIOCFLUSH:
647 case TIOCSETA:
648 case TIOCSETD:
649 case TIOCSETAF:
650 case TIOCSETAW:
651 #ifdef notdef
652 case TIOCSPGRP:
653 #endif
654 case TIOCSTAT:
655 case TIOCSTI:
656 case TIOCSWINSZ:
657 #ifdef COMPAT_OLDTTY
658 case TIOCLBIC:
659 case TIOCLBIS:
660 case TIOCLSET:
661 case TIOCSETC:
662 case OTIOCSETD:
663 case TIOCSETN:
664 case TIOCSETP:
665 case TIOCSLTC:
666 #endif
667 while (isbackground(curproc, tp) &&
668 p->p_pgrp->pg_jobc && (p->p_flag & P_PPWAIT) == 0 &&
669 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
670 (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
671 pgsignal(p->p_pgrp, SIGTTOU, 1);
672 error = ttysleep(tp,
673 &lbolt, TTOPRI | PCATCH, ttybg, 0);
674 if (error)
675 return (error);
676 }
677 break;
678 }
679
680 switch (cmd) { /* Process the ioctl. */
681 case FIOASYNC: /* set/clear async i/o */
682 s = spltty();
683 if (*(int *)data)
684 SET(tp->t_state, TS_ASYNC);
685 else
686 CLR(tp->t_state, TS_ASYNC);
687 splx(s);
688 break;
689 case FIONBIO: /* set/clear non-blocking i/o */
690 break; /* XXX: delete. */
691 case FIONREAD: /* get # bytes to read */
692 *(int *)data = ttnread(tp);
693 break;
694 case TIOCEXCL: /* set exclusive use of tty */
695 s = spltty();
696 SET(tp->t_state, TS_XCLUDE);
697 splx(s);
698 break;
699 case TIOCFLUSH: { /* flush buffers */
700 register int flags = *(int *)data;
701
702 if (flags == 0)
703 flags = FREAD | FWRITE;
704 else
705 flags &= FREAD | FWRITE;
706 ttyflush(tp, flags);
707 break;
708 }
709 case TIOCCONS: /* become virtual console */
710 if (*(int *)data) {
711 if (constty && constty != tp &&
712 ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) ==
713 (TS_CARR_ON | TS_ISOPEN))
714 return (EBUSY);
715 #ifndef UCONSOLE
716 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
717 return (error);
718 #endif
719 constty = tp;
720 } else if (tp == constty)
721 constty = NULL;
722 break;
723 case TIOCDRAIN: /* wait till output drained */
724 if ((error = ttywait(tp)) != 0)
725 return (error);
726 break;
727 case TIOCGETA: { /* get termios struct */
728 struct termios *t = (struct termios *)data;
729
730 bcopy(&tp->t_termios, t, sizeof(struct termios));
731 break;
732 }
733 case TIOCGETD: /* get line discipline */
734 *(int *)data = tp->t_line;
735 break;
736 case TIOCGWINSZ: /* get window size */
737 *(struct winsize *)data = tp->t_winsize;
738 break;
739 case TIOCGPGRP: /* get pgrp of tty */
740 if (!isctty(p, tp))
741 return (ENOTTY);
742 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
743 break;
744 #ifdef TIOCHPCL
745 case TIOCHPCL: /* hang up on last close */
746 s = spltty();
747 SET(tp->t_cflag, HUPCL);
748 splx(s);
749 break;
750 #endif
751 case TIOCNXCL: /* reset exclusive use of tty */
752 s = spltty();
753 CLR(tp->t_state, TS_XCLUDE);
754 splx(s);
755 break;
756 case TIOCOUTQ: /* output queue size */
757 *(int *)data = tp->t_outq.c_cc;
758 break;
759 case TIOCSETA: /* set termios struct */
760 case TIOCSETAW: /* drain output, set */
761 case TIOCSETAF: { /* drn out, fls in, set */
762 register struct termios *t = (struct termios *)data;
763
764 s = spltty();
765 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
766 if ((error = ttywait(tp)) != 0) {
767 splx(s);
768 return (error);
769 }
770 if (cmd == TIOCSETAF)
771 ttyflush(tp, FREAD);
772 }
773 if (!ISSET(t->c_cflag, CIGNORE)) {
774 /*
775 * Set device hardware.
776 */
777 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
778 splx(s);
779 return (error);
780 } else {
781 if (!ISSET(tp->t_state, TS_CARR_ON) &&
782 ISSET(tp->t_cflag, CLOCAL) &&
783 !ISSET(t->c_cflag, CLOCAL)) {
784 CLR(tp->t_state, TS_ISOPEN);
785 SET(tp->t_state, TS_WOPEN);
786 ttwakeup(tp);
787 }
788 tp->t_cflag = t->c_cflag;
789 tp->t_ispeed = t->c_ispeed;
790 tp->t_ospeed = t->c_ospeed;
791 if (t->c_ospeed == 0 && tp->t_session &&
792 tp->t_session->s_leader)
793 psignal(tp->t_session->s_leader,
794 SIGHUP);
795 }
796 ttsetwater(tp);
797 }
798 if (cmd != TIOCSETAF) {
799 if (ISSET(t->c_lflag, ICANON) !=
800 ISSET(tp->t_lflag, ICANON))
801 if (ISSET(t->c_lflag, ICANON)) {
802 SET(tp->t_lflag, PENDIN);
803 ttwakeup(tp);
804 } else {
805 struct clist tq;
806
807 catq(&tp->t_rawq, &tp->t_canq);
808 tq = tp->t_rawq;
809 tp->t_rawq = tp->t_canq;
810 tp->t_canq = tq;
811 CLR(tp->t_lflag, PENDIN);
812 }
813 }
814 tp->t_iflag = t->c_iflag;
815 tp->t_oflag = t->c_oflag;
816 /*
817 * Make the EXTPROC bit read only.
818 */
819 if (ISSET(tp->t_lflag, EXTPROC))
820 SET(t->c_lflag, EXTPROC);
821 else
822 CLR(t->c_lflag, EXTPROC);
823 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
824 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
825 splx(s);
826 break;
827 }
828 case TIOCSETD: { /* set line discipline */
829 register int t = *(int *)data;
830 dev_t device = tp->t_dev;
831
832 if ((u_int)t >= nlinesw)
833 return (ENXIO);
834 if (t != tp->t_line) {
835 s = spltty();
836 (*linesw[tp->t_line].l_close)(tp, flag);
837 error = (*linesw[t].l_open)(device, tp);
838 if (error) {
839 (void)(*linesw[tp->t_line].l_open)(device, tp);
840 splx(s);
841 return (error);
842 }
843 tp->t_line = t;
844 splx(s);
845 }
846 break;
847 }
848 case TIOCSTART: /* start output, like ^Q */
849 s = spltty();
850 if (ISSET(tp->t_state, TS_TTSTOP) ||
851 ISSET(tp->t_lflag, FLUSHO)) {
852 CLR(tp->t_lflag, FLUSHO);
853 CLR(tp->t_state, TS_TTSTOP);
854 ttstart(tp);
855 }
856 splx(s);
857 break;
858 case TIOCSTI: /* simulate terminal input */
859 if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
860 return (EPERM);
861 if (p->p_ucred->cr_uid && !isctty(p, tp))
862 return (EACCES);
863 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
864 break;
865 case TIOCSTOP: /* stop output, like ^S */
866 s = spltty();
867 if (!ISSET(tp->t_state, TS_TTSTOP)) {
868 SET(tp->t_state, TS_TTSTOP);
869 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
870 }
871 splx(s);
872 break;
873 case TIOCSCTTY: /* become controlling tty */
874 /* Session ctty vnode pointer set in vnode layer. */
875 if (!SESS_LEADER(p) ||
876 ((p->p_session->s_ttyvp || tp->t_session) &&
877 (tp->t_session != p->p_session)))
878 return (EPERM);
879 tp->t_session = p->p_session;
880 tp->t_pgrp = p->p_pgrp;
881 p->p_session->s_ttyp = tp;
882 p->p_flag |= P_CONTROLT;
883 break;
884 case TIOCSPGRP: { /* set pgrp of tty */
885 register struct pgrp *pgrp = pgfind(*(int *)data);
886
887 if (!isctty(p, tp))
888 return (ENOTTY);
889 else if (pgrp == NULL || pgrp->pg_session != p->p_session)
890 return (EPERM);
891 tp->t_pgrp = pgrp;
892 break;
893 }
894 case TIOCSTAT: /* get load avg stats */
895 ttyinfo(tp);
896 break;
897 case TIOCSWINSZ: /* set window size */
898 if (bcmp((caddr_t)&tp->t_winsize, data,
899 sizeof (struct winsize))) {
900 tp->t_winsize = *(struct winsize *)data;
901 pgsignal(tp->t_pgrp, SIGWINCH, 1);
902 }
903 break;
904 default:
905 #ifdef COMPAT_OLDTTY
906 return (ttcompat(tp, cmd, data, flag, p));
907 #else
908 return (-1);
909 #endif
910 }
911 return (0);
912 }
913
914 int
915 ttpoll(dev, events, p)
916 dev_t dev;
917 int events;
918 struct proc *p;
919 {
920 register struct tty *tp = (*cdevsw[major(dev)].d_tty)(dev);
921 int revents = 0;
922 int s = spltty();
923
924 if (events & (POLLIN | POLLRDNORM))
925 if (ttnread(tp) > 0)
926 revents |= events & (POLLIN | POLLRDNORM);
927
928 if (events & (POLLOUT | POLLWRNORM))
929 if (tp->t_outq.c_cc <= tp->t_lowat)
930 revents |= events & (POLLOUT | POLLWRNORM);
931
932 if (events & POLLHUP)
933 if (!ISSET(tp->t_cflag, CLOCAL) &&
934 !ISSET(tp->t_state, TS_CARR_ON))
935 revents |= POLLHUP;
936
937 if (revents == 0) {
938 if (events & (POLLIN | POLLHUP | POLLRDNORM))
939 selrecord(p, &tp->t_rsel);
940
941 if (events & (POLLOUT | POLLWRNORM))
942 selrecord(p, &tp->t_wsel);
943 }
944
945 splx(s);
946 return (revents);
947 }
948
949 static int
950 ttnread(tp)
951 struct tty *tp;
952 {
953 int nread;
954
955 if (ISSET(tp->t_lflag, PENDIN))
956 ttypend(tp);
957 nread = tp->t_canq.c_cc;
958 if (!ISSET(tp->t_lflag, ICANON)) {
959 nread += tp->t_rawq.c_cc;
960 if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
961 nread = 0;
962 }
963 return (nread);
964 }
965
966 /*
967 * Wait for output to drain.
968 */
969 int
970 ttywait(tp)
971 register struct tty *tp;
972 {
973 int error, s;
974
975 error = 0;
976 s = spltty();
977 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
978 (ISSET(tp->t_state, TS_CARR_ON) || ISSET(tp->t_cflag, CLOCAL))
979 && tp->t_oproc) {
980 (*tp->t_oproc)(tp);
981 SET(tp->t_state, TS_ASLEEP);
982 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
983 if (error)
984 break;
985 }
986 splx(s);
987 return (error);
988 }
989
990 /*
991 * Flush if successfully wait.
992 */
993 int
994 ttywflush(tp)
995 struct tty *tp;
996 {
997 int error;
998
999 if ((error = ttywait(tp)) == 0)
1000 ttyflush(tp, FREAD);
1001 return (error);
1002 }
1003
1004 /*
1005 * Flush tty read and/or write queues, notifying anyone waiting.
1006 */
1007 void
1008 ttyflush(tp, rw)
1009 register struct tty *tp;
1010 int rw;
1011 {
1012 register int s;
1013
1014 s = spltty();
1015 if (rw & FREAD) {
1016 FLUSHQ(&tp->t_canq);
1017 FLUSHQ(&tp->t_rawq);
1018 tp->t_rocount = 0;
1019 tp->t_rocol = 0;
1020 CLR(tp->t_state, TS_LOCAL);
1021 ttwakeup(tp);
1022 }
1023 if (rw & FWRITE) {
1024 CLR(tp->t_state, TS_TTSTOP);
1025 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
1026 FLUSHQ(&tp->t_outq);
1027 wakeup((caddr_t)&tp->t_outq);
1028 selwakeup(&tp->t_wsel);
1029 }
1030 splx(s);
1031 }
1032
1033 /*
1034 * Copy in the default termios characters.
1035 */
1036 void
1037 ttychars(tp)
1038 struct tty *tp;
1039 {
1040
1041 bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars));
1042 }
1043
1044 /*
1045 * Send stop character on input overflow.
1046 */
1047 static void
1048 ttyblock(tp)
1049 register struct tty *tp;
1050 {
1051 register int total;
1052
1053 total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
1054 if (tp->t_rawq.c_cc > TTYHOG) {
1055 ttyflush(tp, FREAD | FWRITE);
1056 CLR(tp->t_state, TS_TBLOCK);
1057 }
1058 /*
1059 * Block further input iff: current input > threshold
1060 * AND input is available to user program.
1061 */
1062 if ((total >= TTYHOG / 2 &&
1063 !ISSET(tp->t_state, TS_TBLOCK) &&
1064 !ISSET(tp->t_lflag, ICANON)) || tp->t_canq.c_cc > 0) {
1065 if (ISSET(tp->t_iflag, IXOFF) &&
1066 tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1067 putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
1068 SET(tp->t_state, TS_TBLOCK);
1069 ttstart(tp);
1070 }
1071 /* Try to block remote output via hardware flow control. */
1072 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1073 (*tp->t_hwiflow)(tp, 1) != 0)
1074 SET(tp->t_state, TS_TBLOCK);
1075 }
1076 }
1077
1078 void
1079 ttrstrt(tp_arg)
1080 void *tp_arg;
1081 {
1082 struct tty *tp;
1083 int s;
1084
1085 #ifdef DIAGNOSTIC
1086 if (tp_arg == NULL)
1087 panic("ttrstrt");
1088 #endif
1089 tp = tp_arg;
1090 s = spltty();
1091
1092 CLR(tp->t_state, TS_TIMEOUT);
1093 ttstart(tp);
1094
1095 splx(s);
1096 }
1097
1098 int
1099 ttstart(tp)
1100 struct tty *tp;
1101 {
1102
1103 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1104 (*tp->t_oproc)(tp);
1105 return (0);
1106 }
1107
1108 /*
1109 * "close" a line discipline
1110 */
1111 int
1112 ttylclose(tp, flag)
1113 struct tty *tp;
1114 int flag;
1115 {
1116
1117 if (flag & FNONBLOCK)
1118 ttyflush(tp, FREAD | FWRITE);
1119 else
1120 ttywflush(tp);
1121 return (0);
1122 }
1123
1124 /*
1125 * Handle modem control transition on a tty.
1126 * Flag indicates new state of carrier.
1127 * Returns 0 if the line should be turned off, otherwise 1.
1128 */
1129 int
1130 ttymodem(tp, flag)
1131 register struct tty *tp;
1132 int flag;
1133 {
1134
1135 if (!ISSET(tp->t_state, TS_WOPEN) && ISSET(tp->t_cflag, MDMBUF)) {
1136 /*
1137 * MDMBUF: do flow control according to carrier flag
1138 */
1139 if (flag) {
1140 CLR(tp->t_state, TS_TTSTOP);
1141 ttstart(tp);
1142 } else if (!ISSET(tp->t_state, TS_TTSTOP)) {
1143 SET(tp->t_state, TS_TTSTOP);
1144 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
1145 }
1146 } else if (flag == 0) {
1147 /*
1148 * Lost carrier.
1149 */
1150 CLR(tp->t_state, TS_CARR_ON);
1151 if (ISSET(tp->t_state, TS_ISOPEN) &&
1152 !ISSET(tp->t_cflag, CLOCAL)) {
1153 if (tp->t_session && tp->t_session->s_leader)
1154 psignal(tp->t_session->s_leader, SIGHUP);
1155 ttyflush(tp, FREAD | FWRITE);
1156 return (0);
1157 }
1158 } else {
1159 /*
1160 * Carrier now on.
1161 */
1162 SET(tp->t_state, TS_CARR_ON);
1163 ttwakeup(tp);
1164 }
1165 return (1);
1166 }
1167
1168 /*
1169 * Default modem control routine (for other line disciplines).
1170 * Return argument flag, to turn off device on carrier drop.
1171 */
1172 int
1173 nullmodem(tp, flag)
1174 register struct tty *tp;
1175 int flag;
1176 {
1177
1178 if (flag)
1179 SET(tp->t_state, TS_CARR_ON);
1180 else {
1181 CLR(tp->t_state, TS_CARR_ON);
1182 if (!ISSET(tp->t_cflag, CLOCAL)) {
1183 if (tp->t_session && tp->t_session->s_leader)
1184 psignal(tp->t_session->s_leader, SIGHUP);
1185 return (0);
1186 }
1187 }
1188 return (1);
1189 }
1190
1191 /*
1192 * Reinput pending characters after state switch
1193 * call at spltty().
1194 */
1195 void
1196 ttypend(tp)
1197 register struct tty *tp;
1198 {
1199 struct clist tq;
1200 register c;
1201
1202 CLR(tp->t_lflag, PENDIN);
1203 SET(tp->t_state, TS_TYPEN);
1204 tq = tp->t_rawq;
1205 tp->t_rawq.c_cc = 0;
1206 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
1207 while ((c = getc(&tq)) >= 0)
1208 ttyinput(c, tp);
1209 CLR(tp->t_state, TS_TYPEN);
1210 }
1211
1212 /*
1213 * Process a read call on a tty device.
1214 */
1215 int
1216 ttread(tp, uio, flag)
1217 register struct tty *tp;
1218 struct uio *uio;
1219 int flag;
1220 {
1221 register struct clist *qp;
1222 register int c;
1223 register long lflag;
1224 register u_char *cc = tp->t_cc;
1225 register struct proc *p = curproc;
1226 int s, first, error = 0;
1227 struct timeval stime;
1228 int has_stime = 0, last_cc = 0;
1229 long slp = 0;
1230
1231 loop: lflag = tp->t_lflag;
1232 s = spltty();
1233 /*
1234 * take pending input first
1235 */
1236 if (ISSET(lflag, PENDIN))
1237 ttypend(tp);
1238 splx(s);
1239
1240 /*
1241 * Hang process if it's in the background.
1242 */
1243 if (isbackground(p, tp)) {
1244 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
1245 (p->p_sigmask & sigmask(SIGTTIN)) ||
1246 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0)
1247 return (EIO);
1248 pgsignal(p->p_pgrp, SIGTTIN, 1);
1249 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1250 if (error)
1251 return (error);
1252 goto loop;
1253 }
1254
1255 s = spltty();
1256 if (!ISSET(lflag, ICANON)) {
1257 int m = cc[VMIN];
1258 long t = cc[VTIME];
1259
1260 qp = &tp->t_rawq;
1261 /*
1262 * Check each of the four combinations.
1263 * (m > 0 && t == 0) is the normal read case.
1264 * It should be fairly efficient, so we check that and its
1265 * companion case (m == 0 && t == 0) first.
1266 * For the other two cases, we compute the target sleep time
1267 * into slp.
1268 */
1269 if (t == 0) {
1270 if (qp->c_cc < m)
1271 goto sleep;
1272 goto read;
1273 }
1274 t *= 100000; /* time in us */
1275 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1276 ((t1).tv_usec - (t2).tv_usec))
1277 if (m > 0) {
1278 if (qp->c_cc <= 0)
1279 goto sleep;
1280 if (qp->c_cc >= m)
1281 goto read;
1282 if (!has_stime) {
1283 /* first character, start timer */
1284 has_stime = 1;
1285 stime = time;
1286 slp = t;
1287 } else if (qp->c_cc > last_cc) {
1288 /* got a character, restart timer */
1289 stime = time;
1290 slp = t;
1291 } else {
1292 /* nothing, check expiration */
1293 slp = t - diff(time, stime);
1294 }
1295 } else { /* m == 0 */
1296 if (qp->c_cc > 0)
1297 goto read;
1298 if (!has_stime) {
1299 has_stime = 1;
1300 stime = time;
1301 slp = t;
1302 } else
1303 slp = t - diff(time, stime);
1304 }
1305 last_cc = qp->c_cc;
1306 #undef diff
1307 if (slp > 0) {
1308 /*
1309 * Rounding down may make us wake up just short
1310 * of the target, so we round up.
1311 * The formula is ceiling(slp * hz/1000000).
1312 * 32-bit arithmetic is enough for hz < 169.
1313 *
1314 * Also, use plain wakeup() not ttwakeup().
1315 */
1316 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1317 goto sleep;
1318 }
1319 } else if ((qp = &tp->t_canq)->c_cc <= 0) {
1320 int carrier;
1321
1322 sleep:
1323 /*
1324 * If there is no input, sleep on rawq
1325 * awaiting hardware receipt and notification.
1326 * If we have data, we don't need to check for carrier.
1327 */
1328 carrier = ISSET(tp->t_state, TS_CARR_ON) ||
1329 ISSET(tp->t_cflag, CLOCAL);
1330 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1331 splx(s);
1332 return (0); /* EOF */
1333 }
1334 if (flag & IO_NDELAY) {
1335 splx(s);
1336 return (EWOULDBLOCK);
1337 }
1338 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
1339 carrier ? ttyin : ttopen, slp);
1340 splx(s);
1341 if (error && error != EWOULDBLOCK)
1342 return (error);
1343 goto loop;
1344 }
1345 read:
1346 splx(s);
1347
1348 /*
1349 * Input present, check for input mapping and processing.
1350 */
1351 first = 1;
1352 while ((c = getc(qp)) >= 0) {
1353 /*
1354 * delayed suspend (^Y)
1355 */
1356 if (CCEQ(cc[VDSUSP], c) && ISSET(lflag, ISIG)) {
1357 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1358 if (first) {
1359 error = ttysleep(tp, &lbolt,
1360 TTIPRI | PCATCH, ttybg, 0);
1361 if (error)
1362 break;
1363 goto loop;
1364 }
1365 break;
1366 }
1367 /*
1368 * Interpret EOF only in canonical mode.
1369 */
1370 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1371 break;
1372 /*
1373 * Give user character.
1374 */
1375 error = ureadc(c, uio);
1376 if (error)
1377 break;
1378 if (uio->uio_resid == 0)
1379 break;
1380 /*
1381 * In canonical mode check for a "break character"
1382 * marking the end of a "line of input".
1383 */
1384 if (ISSET(lflag, ICANON) && TTBREAKC(c))
1385 break;
1386 first = 0;
1387 }
1388 /*
1389 * Look to unblock output now that (presumably)
1390 * the input queue has gone down.
1391 */
1392 s = spltty();
1393 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) {
1394 if (ISSET(tp->t_iflag, IXOFF) &&
1395 cc[VSTART] != _POSIX_VDISABLE &&
1396 putc(cc[VSTART], &tp->t_outq) == 0) {
1397 CLR(tp->t_state, TS_TBLOCK);
1398 ttstart(tp);
1399 }
1400 /* Try to unblock remote output via hardware flow control. */
1401 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1402 (*tp->t_hwiflow)(tp, 0) != 0)
1403 CLR(tp->t_state, TS_TBLOCK);
1404 }
1405 splx(s);
1406 return (error);
1407 }
1408
1409 /*
1410 * Check the output queue on tp for space for a kernel message (from uprintf
1411 * or tprintf). Allow some space over the normal hiwater mark so we don't
1412 * lose messages due to normal flow control, but don't let the tty run amok.
1413 * Sleeps here are not interruptible, but we return prematurely if new signals
1414 * arrive.
1415 */
1416 int
1417 ttycheckoutq(tp, wait)
1418 register struct tty *tp;
1419 int wait;
1420 {
1421 int hiwat, s, oldsig;
1422
1423 hiwat = tp->t_hiwat;
1424 s = spltty();
1425 oldsig = wait ? curproc->p_siglist : 0;
1426 if (tp->t_outq.c_cc > hiwat + 200)
1427 while (tp->t_outq.c_cc > hiwat) {
1428 ttstart(tp);
1429 if (wait == 0 || curproc->p_siglist != oldsig) {
1430 splx(s);
1431 return (0);
1432 }
1433 timeout((void (*)__P((void *)))wakeup,
1434 (void *)&tp->t_outq, hz);
1435 SET(tp->t_state, TS_ASLEEP);
1436 tsleep(&tp->t_outq, PZERO - 1, "ttckoutq", 0);
1437 }
1438 splx(s);
1439 return (1);
1440 }
1441
1442 /*
1443 * Process a write call on a tty device.
1444 */
1445 int
1446 ttwrite(tp, uio, flag)
1447 register struct tty *tp;
1448 register struct uio *uio;
1449 int flag;
1450 {
1451 register u_char *cp = NULL;
1452 register int cc, ce;
1453 register struct proc *p;
1454 int i, hiwat, cnt, error, s;
1455 u_char obuf[OBUFSIZ];
1456
1457 hiwat = tp->t_hiwat;
1458 cnt = uio->uio_resid;
1459 error = 0;
1460 cc = 0;
1461 loop:
1462 s = spltty();
1463 if (!ISSET(tp->t_state, TS_CARR_ON) &&
1464 !ISSET(tp->t_cflag, CLOCAL)) {
1465 if (ISSET(tp->t_state, TS_ISOPEN)) {
1466 splx(s);
1467 return (EIO);
1468 } else if (flag & IO_NDELAY) {
1469 splx(s);
1470 error = EWOULDBLOCK;
1471 goto out;
1472 } else {
1473 /* Sleep awaiting carrier. */
1474 error = ttysleep(tp,
1475 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
1476 splx(s);
1477 if (error)
1478 goto out;
1479 goto loop;
1480 }
1481 }
1482 splx(s);
1483 /*
1484 * Hang the process if it's in the background.
1485 */
1486 p = curproc;
1487 if (isbackground(p, tp) &&
1488 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1489 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
1490 (p->p_sigmask & sigmask(SIGTTOU)) == 0 &&
1491 p->p_pgrp->pg_jobc) {
1492 pgsignal(p->p_pgrp, SIGTTOU, 1);
1493 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1494 if (error)
1495 goto out;
1496 goto loop;
1497 }
1498 /*
1499 * Process the user's data in at most OBUFSIZ chunks. Perform any
1500 * output translation. Keep track of high water mark, sleep on
1501 * overflow awaiting device aid in acquiring new space.
1502 */
1503 while (uio->uio_resid > 0 || cc > 0) {
1504 if (ISSET(tp->t_lflag, FLUSHO)) {
1505 uio->uio_resid = 0;
1506 return (0);
1507 }
1508 if (tp->t_outq.c_cc > hiwat)
1509 goto ovhiwat;
1510 /*
1511 * Grab a hunk of data from the user, unless we have some
1512 * leftover from last time.
1513 */
1514 if (cc == 0) {
1515 cc = min(uio->uio_resid, OBUFSIZ);
1516 cp = obuf;
1517 error = uiomove(cp, cc, uio);
1518 if (error) {
1519 cc = 0;
1520 break;
1521 }
1522 }
1523 /*
1524 * If nothing fancy need be done, grab those characters we
1525 * can handle without any of ttyoutput's processing and
1526 * just transfer them to the output q. For those chars
1527 * which require special processing (as indicated by the
1528 * bits in char_type), call ttyoutput. After processing
1529 * a hunk of data, look for FLUSHO so ^O's will take effect
1530 * immediately.
1531 */
1532 while (cc > 0) {
1533 if (!ISSET(tp->t_oflag, OPOST))
1534 ce = cc;
1535 else {
1536 ce = cc - scanc((u_int)cc, cp, char_type,
1537 CCLASSMASK);
1538 /*
1539 * If ce is zero, then we're processing
1540 * a special character through ttyoutput.
1541 */
1542 if (ce == 0) {
1543 tp->t_rocount = 0;
1544 if (ttyoutput(*cp, tp) >= 0) {
1545 #ifdef REAL_CLISTS
1546 /* No Clists, wait a bit. */
1547 ttstart(tp);
1548 if (error = ttysleep(tp, &lbolt,
1549 TTOPRI | PCATCH, ttybuf, 0))
1550 break;
1551 goto loop;
1552 #else
1553 /* out of space */
1554 goto overfull;
1555 #endif
1556 }
1557 cp++;
1558 cc--;
1559 if (ISSET(tp->t_lflag, FLUSHO) ||
1560 tp->t_outq.c_cc > hiwat)
1561 goto ovhiwat;
1562 continue;
1563 }
1564 }
1565 /*
1566 * A bunch of normal characters have been found.
1567 * Transfer them en masse to the output queue and
1568 * continue processing at the top of the loop.
1569 * If there are any further characters in this
1570 * <= OBUFSIZ chunk, the first should be a character
1571 * requiring special handling by ttyoutput.
1572 */
1573 tp->t_rocount = 0;
1574 i = b_to_q(cp, ce, &tp->t_outq);
1575 ce -= i;
1576 tp->t_column += ce;
1577 cp += ce, cc -= ce, tk_nout += ce;
1578 tp->t_outcc += ce;
1579 if (i > 0) {
1580 #ifdef REAL_CLISTS
1581 /* No Clists, wait a bit. */
1582 ttstart(tp);
1583 if (error = ttysleep(tp,
1584 &lbolt, TTOPRI | PCATCH, ttybuf, 0))
1585 break;
1586 goto loop;
1587 #else
1588 /* out of space */
1589 goto overfull;
1590 #endif
1591 }
1592 if (ISSET(tp->t_lflag, FLUSHO) ||
1593 tp->t_outq.c_cc > hiwat)
1594 break;
1595 }
1596 ttstart(tp);
1597 }
1598 out:
1599 /*
1600 * If cc is nonzero, we leave the uio structure inconsistent, as the
1601 * offset and iov pointers have moved forward, but it doesn't matter
1602 * (the call will either return short or restart with a new uio).
1603 */
1604 uio->uio_resid += cc;
1605 return (error);
1606
1607 #ifndef REAL_CLISTS
1608 overfull:
1609 /*
1610 * Since we are using ring buffers, if we can't insert any more into
1611 * the output queue, we can assume the ring is full and that someone
1612 * forgot to set the high water mark correctly. We set it and then
1613 * proceed as normal.
1614 */
1615 hiwat = tp->t_outq.c_cc - 1;
1616 #endif
1617
1618 ovhiwat:
1619 ttstart(tp);
1620 s = spltty();
1621 /*
1622 * This can only occur if FLUSHO is set in t_lflag,
1623 * or if ttstart/oproc is synchronous (or very fast).
1624 */
1625 if (tp->t_outq.c_cc <= hiwat) {
1626 splx(s);
1627 goto loop;
1628 }
1629 if (flag & IO_NDELAY) {
1630 splx(s);
1631 uio->uio_resid += cc;
1632 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1633 }
1634 SET(tp->t_state, TS_ASLEEP);
1635 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1636 splx(s);
1637 if (error)
1638 goto out;
1639 goto loop;
1640 }
1641
1642 /*
1643 * Rubout one character from the rawq of tp
1644 * as cleanly as possible.
1645 */
1646 void
1647 ttyrub(c, tp)
1648 int c;
1649 register struct tty *tp;
1650 {
1651 register u_char *cp;
1652 register int savecol;
1653 int tabc, s;
1654
1655 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1656 return;
1657 CLR(tp->t_lflag, FLUSHO);
1658 if (ISSET(tp->t_lflag, ECHOE)) {
1659 if (tp->t_rocount == 0) {
1660 /*
1661 * Screwed by ttwrite; retype
1662 */
1663 ttyretype(tp);
1664 return;
1665 }
1666 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1667 ttyrubo(tp, 2);
1668 else {
1669 CLR(c, ~TTY_CHARMASK);
1670 switch (CCLASS(c)) {
1671 case ORDINARY:
1672 ttyrubo(tp, 1);
1673 break;
1674 case BACKSPACE:
1675 case CONTROL:
1676 case NEWLINE:
1677 case RETURN:
1678 case VTAB:
1679 if (ISSET(tp->t_lflag, ECHOCTL))
1680 ttyrubo(tp, 2);
1681 break;
1682 case TAB:
1683 if (tp->t_rocount < tp->t_rawq.c_cc) {
1684 ttyretype(tp);
1685 return;
1686 }
1687 s = spltty();
1688 savecol = tp->t_column;
1689 SET(tp->t_state, TS_CNTTB);
1690 SET(tp->t_lflag, FLUSHO);
1691 tp->t_column = tp->t_rocol;
1692 for (cp = firstc(&tp->t_rawq, &tabc); cp;
1693 cp = nextc(&tp->t_rawq, cp, &tabc))
1694 ttyecho(tabc, tp);
1695 CLR(tp->t_lflag, FLUSHO);
1696 CLR(tp->t_state, TS_CNTTB);
1697 splx(s);
1698
1699 /* savecol will now be length of the tab. */
1700 savecol -= tp->t_column;
1701 tp->t_column += savecol;
1702 if (savecol > 8)
1703 savecol = 8; /* overflow screw */
1704 while (--savecol >= 0)
1705 (void)ttyoutput('\b', tp);
1706 break;
1707 default: /* XXX */
1708 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
1709 (void)printf(PANICSTR, c, CCLASS(c));
1710 #ifdef notdef
1711 panic(PANICSTR, c, CCLASS(c));
1712 #endif
1713 }
1714 }
1715 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
1716 if (!ISSET(tp->t_state, TS_ERASE)) {
1717 SET(tp->t_state, TS_ERASE);
1718 (void)ttyoutput('\\', tp);
1719 }
1720 ttyecho(c, tp);
1721 } else
1722 ttyecho(tp->t_cc[VERASE], tp);
1723 --tp->t_rocount;
1724 }
1725
1726 /*
1727 * Back over cnt characters, erasing them.
1728 */
1729 static void
1730 ttyrubo(tp, cnt)
1731 register struct tty *tp;
1732 int cnt;
1733 {
1734
1735 while (cnt-- > 0) {
1736 (void)ttyoutput('\b', tp);
1737 (void)ttyoutput(' ', tp);
1738 (void)ttyoutput('\b', tp);
1739 }
1740 }
1741
1742 /*
1743 * ttyretype --
1744 * Reprint the rawq line. Note, it is assumed that c_cc has already
1745 * been checked.
1746 */
1747 void
1748 ttyretype(tp)
1749 register struct tty *tp;
1750 {
1751 register u_char *cp;
1752 int s, c;
1753
1754 /* Echo the reprint character. */
1755 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1756 ttyecho(tp->t_cc[VREPRINT], tp);
1757
1758 (void)ttyoutput('\n', tp);
1759
1760 s = spltty();
1761 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
1762 ttyecho(c, tp);
1763 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
1764 ttyecho(c, tp);
1765 CLR(tp->t_state, TS_ERASE);
1766 splx(s);
1767
1768 tp->t_rocount = tp->t_rawq.c_cc;
1769 tp->t_rocol = 0;
1770 }
1771
1772 /*
1773 * Echo a typed character to the terminal.
1774 */
1775 static void
1776 ttyecho(c, tp)
1777 register int c;
1778 register struct tty *tp;
1779 {
1780
1781 if (!ISSET(tp->t_state, TS_CNTTB))
1782 CLR(tp->t_lflag, FLUSHO);
1783 if ((!ISSET(tp->t_lflag, ECHO) &&
1784 (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) ||
1785 ISSET(tp->t_lflag, EXTPROC))
1786 return;
1787 if (((ISSET(tp->t_lflag, ECHOCTL) &&
1788 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) ||
1789 ISSET(c, TTY_CHARMASK) == 0177)) {
1790 (void)ttyoutput('^', tp);
1791 CLR(c, ~TTY_CHARMASK);
1792 if (c == 0177)
1793 c = '?';
1794 else
1795 c += 'A' - 1;
1796 }
1797 (void)ttyoutput(c, tp);
1798 }
1799
1800 /*
1801 * Wake up any readers on a tty.
1802 */
1803 void
1804 ttwakeup(tp)
1805 register struct tty *tp;
1806 {
1807
1808 selwakeup(&tp->t_rsel);
1809 if (ISSET(tp->t_state, TS_ASYNC))
1810 pgsignal(tp->t_pgrp, SIGIO, 1);
1811 wakeup((caddr_t)&tp->t_rawq);
1812 }
1813
1814 /*
1815 * Look up a code for a specified speed in a conversion table;
1816 * used by drivers to map software speed values to hardware parameters.
1817 */
1818 int
1819 ttspeedtab(speed, table)
1820 int speed;
1821 register struct speedtab *table;
1822 {
1823
1824 for ( ; table->sp_speed != -1; table++)
1825 if (table->sp_speed == speed)
1826 return (table->sp_code);
1827 return (-1);
1828 }
1829
1830 /*
1831 * Set tty hi and low water marks.
1832 *
1833 * Try to arrange the dynamics so there's about one second
1834 * from hi to low water.
1835 */
1836 void
1837 ttsetwater(tp)
1838 struct tty *tp;
1839 {
1840 register int cps, x;
1841
1842 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
1843
1844 cps = tp->t_ospeed / 10;
1845 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
1846 x += cps;
1847 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
1848 tp->t_hiwat = roundup(x, CBSIZE);
1849 #undef CLAMP
1850 }
1851
1852 /*
1853 * Report on state of foreground process group.
1854 */
1855 void
1856 ttyinfo(tp)
1857 register struct tty *tp;
1858 {
1859 register struct proc *p, *pick;
1860 struct timeval utime, stime;
1861 int tmp;
1862
1863 if (ttycheckoutq(tp,0) == 0)
1864 return;
1865
1866 /* Print load average. */
1867 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
1868 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1869
1870 if (tp->t_session == NULL)
1871 ttyprintf(tp, "not a controlling terminal\n");
1872 else if (tp->t_pgrp == NULL)
1873 ttyprintf(tp, "no foreground process group\n");
1874 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
1875 ttyprintf(tp, "empty foreground process group\n");
1876 else {
1877 /* Pick interesting process. */
1878 for (pick = NULL; p != 0; p = p->p_pglist.le_next)
1879 if (proc_compare(pick, p))
1880 pick = p;
1881
1882 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
1883 pick->p_stat == SRUN ? "running" :
1884 pick->p_wmesg ? pick->p_wmesg : "iowait");
1885
1886 calcru(pick, &utime, &stime, NULL);
1887
1888 /* Round up and print user time. */
1889 utime.tv_usec += 5000;
1890 if (utime.tv_usec >= 1000000) {
1891 utime.tv_sec += 1;
1892 utime.tv_usec -= 1000000;
1893 }
1894 ttyprintf(tp, "%ld.%02ldu ", utime.tv_sec,
1895 utime.tv_usec / 10000);
1896
1897 /* Round up and print system time. */
1898 stime.tv_usec += 5000;
1899 if (stime.tv_usec >= 1000000) {
1900 stime.tv_sec += 1;
1901 stime.tv_usec -= 1000000;
1902 }
1903 ttyprintf(tp, "%ld.%02lds ", stime.tv_sec,
1904 stime.tv_usec / 10000);
1905
1906 #define pgtok(a) (((u_long) ((a) * NBPG) / 1024))
1907 /* Print percentage cpu, resident set size. */
1908 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
1909 ttyprintf(tp, "%d%% %ldk\n",
1910 tmp / 100,
1911 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 :
1912 #ifdef pmap_resident_count
1913 pgtok(pmap_resident_count(&pick->p_vmspace->vm_pmap))
1914 #else
1915 pgtok(pick->p_vmspace->vm_rssize)
1916 #endif
1917 );
1918 }
1919 tp->t_rocount = 0; /* so pending input will be retyped if BS */
1920 }
1921
1922 /*
1923 * Returns 1 if p2 is "better" than p1
1924 *
1925 * The algorithm for picking the "interesting" process is thus:
1926 *
1927 * 1) Only foreground processes are eligible - implied.
1928 * 2) Runnable processes are favored over anything else. The runner
1929 * with the highest cpu utilization is picked (p_estcpu). Ties are
1930 * broken by picking the highest pid.
1931 * 3) The sleeper with the shortest sleep time is next. With ties,
1932 * we pick out just "short-term" sleepers (P_SINTR == 0).
1933 * 4) Further ties are broken by picking the highest pid.
1934 */
1935 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
1936 #define TESTAB(a, b) ((a)<<1 | (b))
1937 #define ONLYA 2
1938 #define ONLYB 1
1939 #define BOTH 3
1940
1941 static int
1942 proc_compare(p1, p2)
1943 register struct proc *p1, *p2;
1944 {
1945
1946 if (p1 == NULL)
1947 return (1);
1948 /*
1949 * see if at least one of them is runnable
1950 */
1951 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
1952 case ONLYA:
1953 return (0);
1954 case ONLYB:
1955 return (1);
1956 case BOTH:
1957 /*
1958 * tie - favor one with highest recent cpu utilization
1959 */
1960 if (p2->p_estcpu > p1->p_estcpu)
1961 return (1);
1962 if (p1->p_estcpu > p2->p_estcpu)
1963 return (0);
1964 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1965 }
1966 /*
1967 * weed out zombies
1968 */
1969 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
1970 case ONLYA:
1971 return (1);
1972 case ONLYB:
1973 return (0);
1974 case BOTH:
1975 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1976 }
1977 /*
1978 * pick the one with the smallest sleep time
1979 */
1980 if (p2->p_slptime > p1->p_slptime)
1981 return (0);
1982 if (p1->p_slptime > p2->p_slptime)
1983 return (1);
1984 /*
1985 * favor one sleeping in a non-interruptible sleep
1986 */
1987 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
1988 return (1);
1989 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
1990 return (0);
1991 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1992 }
1993
1994 /*
1995 * Output char to tty; console putchar style.
1996 */
1997 int
1998 tputchar(c, tp)
1999 int c;
2000 struct tty *tp;
2001 {
2002 register int s;
2003
2004 s = spltty();
2005 if (ISSET(tp->t_state,
2006 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) {
2007 splx(s);
2008 return (-1);
2009 }
2010 if (c == '\n')
2011 (void)ttyoutput('\r', tp);
2012 (void)ttyoutput(c, tp);
2013 ttstart(tp);
2014 splx(s);
2015 return (0);
2016 }
2017
2018 /*
2019 * Sleep on chan, returning ERESTART if tty changed while we napped and
2020 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
2021 * the tty is revoked, restarting a pending call will redo validation done
2022 * at the start of the call.
2023 */
2024 int
2025 ttysleep(tp, chan, pri, wmesg, timo)
2026 struct tty *tp;
2027 void *chan;
2028 int pri, timo;
2029 char *wmesg;
2030 {
2031 int error;
2032 short gen;
2033
2034 gen = tp->t_gen;
2035 if ((error = tsleep(chan, pri, wmesg, timo)) != 0)
2036 return (error);
2037 return (tp->t_gen == gen ? 0 : ERESTART);
2038 }
2039
2040 /*
2041 * Initialise the global tty list.
2042 */
2043 void
2044 tty_init()
2045 {
2046
2047 TAILQ_INIT(&ttylist);
2048 tty_count = 0;
2049 }
2050
2051 /*
2052 * Attach a tty to the tty list.
2053 *
2054 * This should be called ONLY once per real tty (including pty's).
2055 * eg, on the sparc, the keyboard and mouse have struct tty's that are
2056 * distinctly NOT usable as tty's, and thus should not be attached to
2057 * the ttylist. This is why this call is not done from ttymalloc().
2058 *
2059 * Device drivers should attach tty's at a similar time that they are
2060 * ttymalloc()'ed, or, for the case of statically allocated struct tty's
2061 * either in the attach or (first) open routine.
2062 */
2063 void
2064 tty_attach(tp)
2065 struct tty *tp;
2066 {
2067
2068 TAILQ_INSERT_TAIL(&ttylist, tp, tty_link);
2069 ++tty_count;
2070 }
2071
2072 /*
2073 * Remove a tty from the tty list.
2074 */
2075 void
2076 tty_detach(tp)
2077 struct tty *tp;
2078 {
2079
2080 --tty_count;
2081 #ifdef DIAGNOSTIC
2082 if (tty_count < 0)
2083 panic("tty_detach: tty_count < 0");
2084 #endif
2085 TAILQ_REMOVE(&ttylist, tp, tty_link);
2086 }
2087
2088 /*
2089 * Allocate a tty structure and its associated buffers.
2090 */
2091 struct tty *
2092 ttymalloc()
2093 {
2094 struct tty *tp;
2095
2096 MALLOC(tp, struct tty *, sizeof(struct tty), M_TTYS, M_WAITOK);
2097 bzero(tp, sizeof *tp);
2098 /* XXX: default to 1024 chars for now */
2099 clalloc(&tp->t_rawq, 1024, 1);
2100 clalloc(&tp->t_canq, 1024, 1);
2101 /* output queue doesn't need quoting */
2102 clalloc(&tp->t_outq, 1024, 0);
2103 return(tp);
2104 }
2105
2106 /*
2107 * Free a tty structure and its buffers.
2108 *
2109 * Be sure to call tty_detach() for any tty that has been
2110 * tty_attach()ed.
2111 */
2112 void
2113 ttyfree(tp)
2114 struct tty *tp;
2115 {
2116
2117 clfree(&tp->t_rawq);
2118 clfree(&tp->t_canq);
2119 clfree(&tp->t_outq);
2120 FREE(tp, M_TTYS);
2121 }
2122