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