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