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