tty.c revision 1.128 1 /* $NetBSD: tty.c,v 1.128 2001/05/02 10:32:08 scw 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(struct tty *tp, int events, struct proc *p)
1023 {
1024 int revents, s;
1025
1026 revents = 0;
1027 s = spltty();
1028 if (events & (POLLIN | POLLRDNORM))
1029 if (ttnread(tp) > 0)
1030 revents |= events & (POLLIN | POLLRDNORM);
1031
1032 if (events & (POLLOUT | POLLWRNORM))
1033 if (tp->t_outq.c_cc <= tp->t_lowat)
1034 revents |= events & (POLLOUT | POLLWRNORM);
1035
1036 if (events & POLLHUP)
1037 if (!CONNECTED(tp))
1038 revents |= POLLHUP;
1039
1040 if (revents == 0) {
1041 if (events & (POLLIN | POLLHUP | POLLRDNORM))
1042 selrecord(p, &tp->t_rsel);
1043
1044 if (events & (POLLOUT | POLLWRNORM))
1045 selrecord(p, &tp->t_wsel);
1046 }
1047
1048 splx(s);
1049 return (revents);
1050 }
1051
1052 static int
1053 ttnread(struct tty *tp)
1054 {
1055 int nread;
1056
1057 if (ISSET(tp->t_lflag, PENDIN))
1058 ttypend(tp);
1059 nread = tp->t_canq.c_cc;
1060 if (!ISSET(tp->t_lflag, ICANON)) {
1061 nread += tp->t_rawq.c_cc;
1062 if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
1063 nread = 0;
1064 }
1065 return (nread);
1066 }
1067
1068 /*
1069 * Wait for output to drain.
1070 */
1071 int
1072 ttywait(tp)
1073 struct tty *tp;
1074 {
1075 int error, s;
1076
1077 error = 0;
1078 s = spltty();
1079 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1080 CONNECTED(tp) && tp->t_oproc) {
1081 (*tp->t_oproc)(tp);
1082 SET(tp->t_state, TS_ASLEEP);
1083 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1084 if (error)
1085 break;
1086 }
1087 splx(s);
1088 return (error);
1089 }
1090
1091 /*
1092 * Flush if successfully wait.
1093 */
1094 int
1095 ttywflush(struct tty *tp)
1096 {
1097 int error;
1098
1099 if ((error = ttywait(tp)) == 0)
1100 ttyflush(tp, FREAD);
1101 return (error);
1102 }
1103
1104 /*
1105 * Flush tty read and/or write queues, notifying anyone waiting.
1106 */
1107 void
1108 ttyflush(struct tty *tp, int rw)
1109 {
1110 int s;
1111
1112 s = spltty();
1113 if (rw & FREAD) {
1114 FLUSHQ(&tp->t_canq);
1115 FLUSHQ(&tp->t_rawq);
1116 tp->t_rocount = 0;
1117 tp->t_rocol = 0;
1118 CLR(tp->t_state, TS_LOCAL);
1119 ttwakeup(tp);
1120 }
1121 if (rw & FWRITE) {
1122 CLR(tp->t_state, TS_TTSTOP);
1123 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
1124 FLUSHQ(&tp->t_outq);
1125 wakeup((caddr_t)&tp->t_outq);
1126 selwakeup(&tp->t_wsel);
1127 }
1128 splx(s);
1129 }
1130
1131 /*
1132 * Copy in the default termios characters.
1133 */
1134 void
1135 ttychars(struct tty *tp)
1136 {
1137
1138 memcpy(tp->t_cc, ttydefchars, sizeof(ttydefchars));
1139 }
1140
1141 /*
1142 * Send stop character on input overflow.
1143 */
1144 static void
1145 ttyblock(struct tty *tp)
1146 {
1147 int total;
1148
1149 total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
1150 if (tp->t_rawq.c_cc > TTYHOG) {
1151 ttyflush(tp, FREAD | FWRITE);
1152 CLR(tp->t_state, TS_TBLOCK);
1153 }
1154 /*
1155 * Block further input iff: current input > threshold
1156 * AND input is available to user program.
1157 */
1158 if (total >= TTYHOG / 2 &&
1159 !ISSET(tp->t_state, TS_TBLOCK) &&
1160 (!ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0)) {
1161 if (ISSET(tp->t_iflag, IXOFF) &&
1162 tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1163 putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
1164 SET(tp->t_state, TS_TBLOCK);
1165 ttstart(tp);
1166 }
1167 /* Try to block remote output via hardware flow control. */
1168 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1169 (*tp->t_hwiflow)(tp, 1) != 0)
1170 SET(tp->t_state, TS_TBLOCK);
1171 }
1172 }
1173
1174 void
1175 ttrstrt(void *tp_arg)
1176 {
1177 struct tty *tp;
1178 int s;
1179
1180 #ifdef DIAGNOSTIC
1181 if (tp_arg == NULL)
1182 panic("ttrstrt");
1183 #endif
1184 tp = tp_arg;
1185 s = spltty();
1186
1187 CLR(tp->t_state, TS_TIMEOUT);
1188 ttstart(tp);
1189
1190 splx(s);
1191 }
1192
1193 int
1194 ttstart(struct tty *tp)
1195 {
1196
1197 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1198 (*tp->t_oproc)(tp);
1199 return (0);
1200 }
1201
1202 /*
1203 * "close" a line discipline
1204 */
1205 int
1206 ttylclose(struct tty *tp, int flag)
1207 {
1208
1209 if (flag & FNONBLOCK)
1210 ttyflush(tp, FREAD | FWRITE);
1211 else
1212 ttywflush(tp);
1213 return (0);
1214 }
1215
1216 /*
1217 * Handle modem control transition on a tty.
1218 * Flag indicates new state of carrier.
1219 * Returns 0 if the line should be turned off, otherwise 1.
1220 */
1221 int
1222 ttymodem(struct tty *tp, int flag)
1223 {
1224
1225 if (flag == 0) {
1226 if (ISSET(tp->t_state, TS_CARR_ON)) {
1227 /*
1228 * Lost carrier.
1229 */
1230 CLR(tp->t_state, TS_CARR_ON);
1231 if (ISSET(tp->t_state, TS_ISOPEN) && !CONNECTED(tp)) {
1232 if (tp->t_session && tp->t_session->s_leader)
1233 psignal(tp->t_session->s_leader, SIGHUP);
1234 ttyflush(tp, FREAD | FWRITE);
1235 return (0);
1236 }
1237 }
1238 } else {
1239 if (!ISSET(tp->t_state, TS_CARR_ON)) {
1240 /*
1241 * Carrier now on.
1242 */
1243 SET(tp->t_state, TS_CARR_ON);
1244 ttwakeup(tp);
1245 }
1246 }
1247 return (1);
1248 }
1249
1250 /*
1251 * Default modem control routine (for other line disciplines).
1252 * Return argument flag, to turn off device on carrier drop.
1253 */
1254 int
1255 nullmodem(struct tty *tp, int flag)
1256 {
1257
1258 if (flag)
1259 SET(tp->t_state, TS_CARR_ON);
1260 else {
1261 CLR(tp->t_state, TS_CARR_ON);
1262 if (!CONNECTED(tp)) {
1263 if (tp->t_session && tp->t_session->s_leader)
1264 psignal(tp->t_session->s_leader, SIGHUP);
1265 return (0);
1266 }
1267 }
1268 return (1);
1269 }
1270
1271 /*
1272 * Reinput pending characters after state switch
1273 * call at spltty().
1274 */
1275 void
1276 ttypend(struct tty *tp)
1277 {
1278 struct clist tq;
1279 int c;
1280
1281 CLR(tp->t_lflag, PENDIN);
1282 SET(tp->t_state, TS_TYPEN);
1283 tq = tp->t_rawq;
1284 tp->t_rawq.c_cc = 0;
1285 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
1286 while ((c = getc(&tq)) >= 0)
1287 ttyinput(c, tp);
1288 CLR(tp->t_state, TS_TYPEN);
1289 }
1290
1291 /*
1292 * Process a read call on a tty device.
1293 */
1294 int
1295 ttread(struct tty *tp, struct uio *uio, int flag)
1296 {
1297 struct clist *qp;
1298 u_char *cc;
1299 struct proc *p;
1300 int c, s, first, error, has_stime, last_cc;
1301 long lflag, slp;
1302 struct timeval stime;
1303
1304 cc = tp->t_cc;
1305 p = curproc;
1306 error = 0;
1307 has_stime = 0;
1308 last_cc = 0;
1309 slp = 0;
1310
1311 loop:
1312 lflag = tp->t_lflag;
1313 s = spltty();
1314 /*
1315 * take pending input first
1316 */
1317 if (ISSET(lflag, PENDIN))
1318 ttypend(tp);
1319 splx(s);
1320
1321 /*
1322 * Hang process if it's in the background.
1323 */
1324 if (isbackground(p, tp)) {
1325 if (sigismember(&p->p_sigctx.ps_sigignore, SIGTTIN) ||
1326 sigismember(&p->p_sigctx.ps_sigmask, SIGTTIN) ||
1327 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0)
1328 return (EIO);
1329 pgsignal(p->p_pgrp, SIGTTIN, 1);
1330 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1331 if (error)
1332 return (error);
1333 goto loop;
1334 }
1335
1336 s = spltty();
1337 if (!ISSET(lflag, ICANON)) {
1338 int m = cc[VMIN];
1339 long t = cc[VTIME];
1340
1341 qp = &tp->t_rawq;
1342 /*
1343 * Check each of the four combinations.
1344 * (m > 0 && t == 0) is the normal read case.
1345 * It should be fairly efficient, so we check that and its
1346 * companion case (m == 0 && t == 0) first.
1347 * For the other two cases, we compute the target sleep time
1348 * into slp.
1349 */
1350 if (t == 0) {
1351 if (qp->c_cc < m)
1352 goto sleep;
1353 goto read;
1354 }
1355 t *= 100000; /* time in us */
1356 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1357 ((t1).tv_usec - (t2).tv_usec))
1358 if (m > 0) {
1359 if (qp->c_cc <= 0)
1360 goto sleep;
1361 if (qp->c_cc >= m)
1362 goto read;
1363 if (!has_stime) {
1364 /* first character, start timer */
1365 has_stime = 1;
1366 stime = time;
1367 slp = t;
1368 } else if (qp->c_cc > last_cc) {
1369 /* got a character, restart timer */
1370 stime = time;
1371 slp = t;
1372 } else {
1373 /* nothing, check expiration */
1374 slp = t - diff(time, stime);
1375 }
1376 } else { /* m == 0 */
1377 if (qp->c_cc > 0)
1378 goto read;
1379 if (!has_stime) {
1380 has_stime = 1;
1381 stime = time;
1382 slp = t;
1383 } else
1384 slp = t - diff(time, stime);
1385 }
1386 last_cc = qp->c_cc;
1387 #undef diff
1388 if (slp > 0) {
1389 /*
1390 * Rounding down may make us wake up just short
1391 * of the target, so we round up.
1392 * The formula is ceiling(slp * hz/1000000).
1393 * 32-bit arithmetic is enough for hz < 169.
1394 *
1395 * Also, use plain wakeup() not ttwakeup().
1396 */
1397 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1398 goto sleep;
1399 }
1400 } else if ((qp = &tp->t_canq)->c_cc <= 0) {
1401 int carrier;
1402
1403 sleep:
1404 /*
1405 * If there is no input, sleep on rawq
1406 * awaiting hardware receipt and notification.
1407 * If we have data, we don't need to check for carrier.
1408 */
1409 carrier = CONNECTED(tp);
1410 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1411 splx(s);
1412 return (0); /* EOF */
1413 }
1414 if (flag & IO_NDELAY) {
1415 splx(s);
1416 return (EWOULDBLOCK);
1417 }
1418 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
1419 carrier ? ttyin : ttopen, slp);
1420 splx(s);
1421 /* VMIN == 0: any quantity read satisfies */
1422 if (cc[VMIN] == 0 && error == EWOULDBLOCK)
1423 return (0);
1424 if (error && error != EWOULDBLOCK)
1425 return (error);
1426 goto loop;
1427 }
1428 read:
1429 splx(s);
1430
1431 /*
1432 * Input present, check for input mapping and processing.
1433 */
1434 first = 1;
1435 while ((c = getc(qp)) >= 0) {
1436 /*
1437 * delayed suspend (^Y)
1438 */
1439 if (CCEQ(cc[VDSUSP], c) &&
1440 ISSET(lflag, IEXTEN|ISIG) == (IEXTEN|ISIG)) {
1441 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1442 if (first) {
1443 error = ttysleep(tp, &lbolt,
1444 TTIPRI | PCATCH, ttybg, 0);
1445 if (error)
1446 break;
1447 goto loop;
1448 }
1449 break;
1450 }
1451 /*
1452 * Interpret EOF only in canonical mode.
1453 */
1454 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1455 break;
1456 /*
1457 * Give user character.
1458 */
1459 error = ureadc(c, uio);
1460 if (error)
1461 break;
1462 if (uio->uio_resid == 0)
1463 break;
1464 /*
1465 * In canonical mode check for a "break character"
1466 * marking the end of a "line of input".
1467 */
1468 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1469 break;
1470 first = 0;
1471 }
1472 /*
1473 * Look to unblock output now that (presumably)
1474 * the input queue has gone down.
1475 */
1476 s = spltty();
1477 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) {
1478 if (ISSET(tp->t_iflag, IXOFF) &&
1479 cc[VSTART] != _POSIX_VDISABLE &&
1480 putc(cc[VSTART], &tp->t_outq) == 0) {
1481 CLR(tp->t_state, TS_TBLOCK);
1482 ttstart(tp);
1483 }
1484 /* Try to unblock remote output via hardware flow control. */
1485 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1486 (*tp->t_hwiflow)(tp, 0) != 0)
1487 CLR(tp->t_state, TS_TBLOCK);
1488 }
1489 splx(s);
1490 return (error);
1491 }
1492
1493 /*
1494 * Check the output queue on tp for space for a kernel message (from uprintf
1495 * or tprintf). Allow some space over the normal hiwater mark so we don't
1496 * lose messages due to normal flow control, but don't let the tty run amok.
1497 * Sleeps here are not interruptible, but we return prematurely if new signals
1498 * arrive.
1499 */
1500 int
1501 ttycheckoutq(struct tty *tp, int wait)
1502 {
1503 int hiwat, s, error;
1504
1505 hiwat = tp->t_hiwat;
1506 s = spltty();
1507 if (tp->t_outq.c_cc > hiwat + 200)
1508 while (tp->t_outq.c_cc > hiwat) {
1509 ttstart(tp);
1510 if (wait == 0) {
1511 splx(s);
1512 return (0);
1513 }
1514 callout_reset(&tp->t_outq_ch, hz,
1515 (void (*)__P((void *)))wakeup, &tp->t_outq);
1516 SET(tp->t_state, TS_ASLEEP);
1517 error = tsleep(&tp->t_outq, (PZERO - 1) | PCATCH,
1518 "ttckoutq", 0);
1519 if (error == EINTR)
1520 wait = 0;
1521 }
1522 splx(s);
1523 return (1);
1524 }
1525
1526 /*
1527 * Process a write call on a tty device.
1528 */
1529 int
1530 ttwrite(struct tty *tp, struct uio *uio, int flag)
1531 {
1532 u_char *cp;
1533 struct proc *p;
1534 int cc, ce, i, hiwat, cnt, error, s;
1535 u_char obuf[OBUFSIZ];
1536
1537 cp = NULL;
1538 hiwat = tp->t_hiwat;
1539 cnt = uio->uio_resid;
1540 error = 0;
1541 cc = 0;
1542 loop:
1543 s = spltty();
1544 if (!CONNECTED(tp)) {
1545 if (ISSET(tp->t_state, TS_ISOPEN)) {
1546 splx(s);
1547 return (EIO);
1548 } else if (flag & IO_NDELAY) {
1549 splx(s);
1550 error = EWOULDBLOCK;
1551 goto out;
1552 } else {
1553 /* Sleep awaiting carrier. */
1554 error = ttysleep(tp,
1555 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
1556 splx(s);
1557 if (error)
1558 goto out;
1559 goto loop;
1560 }
1561 }
1562 splx(s);
1563 /*
1564 * Hang the process if it's in the background.
1565 */
1566 p = curproc;
1567 if (isbackground(p, tp) &&
1568 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1569 !sigismember(&p->p_sigctx.ps_sigignore, SIGTTOU) &&
1570 !sigismember(&p->p_sigctx.ps_sigmask, SIGTTOU)) {
1571 if (p->p_pgrp->pg_jobc == 0) {
1572 error = EIO;
1573 goto out;
1574 }
1575 pgsignal(p->p_pgrp, SIGTTOU, 1);
1576 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1577 if (error)
1578 goto out;
1579 goto loop;
1580 }
1581 /*
1582 * Process the user's data in at most OBUFSIZ chunks. Perform any
1583 * output translation. Keep track of high water mark, sleep on
1584 * overflow awaiting device aid in acquiring new space.
1585 */
1586 while (uio->uio_resid > 0 || cc > 0) {
1587 if (ISSET(tp->t_lflag, FLUSHO)) {
1588 uio->uio_resid = 0;
1589 return (0);
1590 }
1591 if (tp->t_outq.c_cc > hiwat)
1592 goto ovhiwat;
1593 /*
1594 * Grab a hunk of data from the user, unless we have some
1595 * leftover from last time.
1596 */
1597 if (cc == 0) {
1598 cc = min(uio->uio_resid, OBUFSIZ);
1599 cp = obuf;
1600 error = uiomove(cp, cc, uio);
1601 if (error) {
1602 cc = 0;
1603 break;
1604 }
1605 }
1606 /*
1607 * If nothing fancy need be done, grab those characters we
1608 * can handle without any of ttyoutput's processing and
1609 * just transfer them to the output q. For those chars
1610 * which require special processing (as indicated by the
1611 * bits in char_type), call ttyoutput. After processing
1612 * a hunk of data, look for FLUSHO so ^O's will take effect
1613 * immediately.
1614 */
1615 while (cc > 0) {
1616 if (!ISSET(tp->t_oflag, OPOST))
1617 ce = cc;
1618 else {
1619 ce = cc - scanc((u_int)cc, cp, char_type,
1620 CCLASSMASK);
1621 /*
1622 * If ce is zero, then we're processing
1623 * a special character through ttyoutput.
1624 */
1625 if (ce == 0) {
1626 tp->t_rocount = 0;
1627 if (ttyoutput(*cp, tp) >= 0) {
1628 /* out of space */
1629 goto overfull;
1630 }
1631 cp++;
1632 cc--;
1633 if (ISSET(tp->t_lflag, FLUSHO) ||
1634 tp->t_outq.c_cc > hiwat)
1635 goto ovhiwat;
1636 continue;
1637 }
1638 }
1639 /*
1640 * A bunch of normal characters have been found.
1641 * Transfer them en masse to the output queue and
1642 * continue processing at the top of the loop.
1643 * If there are any further characters in this
1644 * <= OBUFSIZ chunk, the first should be a character
1645 * requiring special handling by ttyoutput.
1646 */
1647 tp->t_rocount = 0;
1648 i = b_to_q(cp, ce, &tp->t_outq);
1649 ce -= i;
1650 tp->t_column += ce;
1651 cp += ce, cc -= ce, tk_nout += ce;
1652 tp->t_outcc += ce;
1653 if (i > 0) {
1654 /* out of space */
1655 goto overfull;
1656 }
1657 if (ISSET(tp->t_lflag, FLUSHO) ||
1658 tp->t_outq.c_cc > hiwat)
1659 break;
1660 }
1661 ttstart(tp);
1662 }
1663 out:
1664 /*
1665 * If cc is nonzero, we leave the uio structure inconsistent, as the
1666 * offset and iov pointers have moved forward, but it doesn't matter
1667 * (the call will either return short or restart with a new uio).
1668 */
1669 uio->uio_resid += cc;
1670 return (error);
1671
1672 overfull:
1673 /*
1674 * Since we are using ring buffers, if we can't insert any more into
1675 * the output queue, we can assume the ring is full and that someone
1676 * forgot to set the high water mark correctly. We set it and then
1677 * proceed as normal.
1678 */
1679 hiwat = tp->t_outq.c_cc - 1;
1680
1681 ovhiwat:
1682 ttstart(tp);
1683 s = spltty();
1684 /*
1685 * This can only occur if FLUSHO is set in t_lflag,
1686 * or if ttstart/oproc is synchronous (or very fast).
1687 */
1688 if (tp->t_outq.c_cc <= hiwat) {
1689 splx(s);
1690 goto loop;
1691 }
1692 if (flag & IO_NDELAY) {
1693 splx(s);
1694 uio->uio_resid += cc;
1695 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1696 }
1697 SET(tp->t_state, TS_ASLEEP);
1698 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1699 splx(s);
1700 if (error)
1701 goto out;
1702 goto loop;
1703 }
1704
1705 /*
1706 * Rubout one character from the rawq of tp
1707 * as cleanly as possible.
1708 */
1709 void
1710 ttyrub(int c, struct tty *tp)
1711 {
1712 u_char *cp;
1713 int savecol, tabc, s;
1714
1715 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1716 return;
1717 CLR(tp->t_lflag, FLUSHO);
1718 if (ISSET(tp->t_lflag, ECHOE)) {
1719 if (tp->t_rocount == 0) {
1720 /*
1721 * Screwed by ttwrite; retype
1722 */
1723 ttyretype(tp);
1724 return;
1725 }
1726 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1727 ttyrubo(tp, 2);
1728 else {
1729 CLR(c, ~TTY_CHARMASK);
1730 switch (CCLASS(c)) {
1731 case ORDINARY:
1732 ttyrubo(tp, 1);
1733 break;
1734 case BACKSPACE:
1735 case CONTROL:
1736 case NEWLINE:
1737 case RETURN:
1738 case VTAB:
1739 if (ISSET(tp->t_lflag, ECHOCTL))
1740 ttyrubo(tp, 2);
1741 break;
1742 case TAB:
1743 if (tp->t_rocount < tp->t_rawq.c_cc) {
1744 ttyretype(tp);
1745 return;
1746 }
1747 s = spltty();
1748 savecol = tp->t_column;
1749 SET(tp->t_state, TS_CNTTB);
1750 SET(tp->t_lflag, FLUSHO);
1751 tp->t_column = tp->t_rocol;
1752 for (cp = firstc(&tp->t_rawq, &tabc); cp;
1753 cp = nextc(&tp->t_rawq, cp, &tabc))
1754 ttyecho(tabc, tp);
1755 CLR(tp->t_lflag, FLUSHO);
1756 CLR(tp->t_state, TS_CNTTB);
1757 splx(s);
1758
1759 /* savecol will now be length of the tab. */
1760 savecol -= tp->t_column;
1761 tp->t_column += savecol;
1762 if (savecol > 8)
1763 savecol = 8; /* overflow screw */
1764 while (--savecol >= 0)
1765 (void)ttyoutput('\b', tp);
1766 break;
1767 default: /* XXX */
1768 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
1769 (void)printf(PANICSTR, c, CCLASS(c));
1770 #ifdef notdef
1771 panic(PANICSTR, c, CCLASS(c));
1772 #endif
1773 }
1774 }
1775 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
1776 if (!ISSET(tp->t_state, TS_ERASE)) {
1777 SET(tp->t_state, TS_ERASE);
1778 (void)ttyoutput('\\', tp);
1779 }
1780 ttyecho(c, tp);
1781 } else
1782 ttyecho(tp->t_cc[VERASE], tp);
1783 --tp->t_rocount;
1784 }
1785
1786 /*
1787 * Back over cnt characters, erasing them.
1788 */
1789 static void
1790 ttyrubo(struct tty *tp, int cnt)
1791 {
1792
1793 while (cnt-- > 0) {
1794 (void)ttyoutput('\b', tp);
1795 (void)ttyoutput(' ', tp);
1796 (void)ttyoutput('\b', tp);
1797 }
1798 }
1799
1800 /*
1801 * ttyretype --
1802 * Reprint the rawq line. Note, it is assumed that c_cc has already
1803 * been checked.
1804 */
1805 void
1806 ttyretype(struct tty *tp)
1807 {
1808 u_char *cp;
1809 int s, c;
1810
1811 /* Echo the reprint character. */
1812 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1813 ttyecho(tp->t_cc[VREPRINT], tp);
1814
1815 (void)ttyoutput('\n', tp);
1816
1817 s = spltty();
1818 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
1819 ttyecho(c, tp);
1820 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
1821 ttyecho(c, tp);
1822 CLR(tp->t_state, TS_ERASE);
1823 splx(s);
1824
1825 tp->t_rocount = tp->t_rawq.c_cc;
1826 tp->t_rocol = 0;
1827 }
1828
1829 /*
1830 * Echo a typed character to the terminal.
1831 */
1832 static void
1833 ttyecho(int c, struct tty *tp)
1834 {
1835
1836 if (!ISSET(tp->t_state, TS_CNTTB))
1837 CLR(tp->t_lflag, FLUSHO);
1838 if ((!ISSET(tp->t_lflag, ECHO) &&
1839 (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) ||
1840 ISSET(tp->t_lflag, EXTPROC))
1841 return;
1842 if (((ISSET(tp->t_lflag, ECHOCTL) &&
1843 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) ||
1844 ISSET(c, TTY_CHARMASK) == 0177)) {
1845 (void)ttyoutput('^', tp);
1846 CLR(c, ~TTY_CHARMASK);
1847 if (c == 0177)
1848 c = '?';
1849 else
1850 c += 'A' - 1;
1851 }
1852 (void)ttyoutput(c, tp);
1853 }
1854
1855 /*
1856 * Wake up any readers on a tty.
1857 */
1858 void
1859 ttwakeup(struct tty *tp)
1860 {
1861
1862 selwakeup(&tp->t_rsel);
1863 if (ISSET(tp->t_state, TS_ASYNC))
1864 pgsignal(tp->t_pgrp, SIGIO, 1);
1865 wakeup((caddr_t)&tp->t_rawq);
1866 }
1867
1868 /*
1869 * Look up a code for a specified speed in a conversion table;
1870 * used by drivers to map software speed values to hardware parameters.
1871 */
1872 int
1873 ttspeedtab(int speed, struct speedtab *table)
1874 {
1875
1876 for ( ; table->sp_speed != -1; table++)
1877 if (table->sp_speed == speed)
1878 return (table->sp_code);
1879 return (-1);
1880 }
1881
1882 /*
1883 * Set tty hi and low water marks.
1884 *
1885 * Try to arrange the dynamics so there's about one second
1886 * from hi to low water.
1887 */
1888 void
1889 ttsetwater(struct tty *tp)
1890 {
1891 int cps, x;
1892
1893 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
1894
1895 cps = tp->t_ospeed / 10;
1896 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
1897 x += cps;
1898 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
1899 tp->t_hiwat = roundup(x, CBSIZE);
1900 #undef CLAMP
1901 }
1902
1903 /*
1904 * Report on state of foreground process group.
1905 */
1906 void
1907 ttyinfo(struct tty *tp)
1908 {
1909 struct proc *p, *pick;
1910 struct timeval utime, stime;
1911 int tmp;
1912
1913 if (ttycheckoutq(tp,0) == 0)
1914 return;
1915
1916 /* Print load average. */
1917 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
1918 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1919
1920 if (tp->t_session == NULL)
1921 ttyprintf(tp, "not a controlling terminal\n");
1922 else if (tp->t_pgrp == NULL)
1923 ttyprintf(tp, "no foreground process group\n");
1924 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
1925 ttyprintf(tp, "empty foreground process group\n");
1926 else {
1927 /* Pick interesting process. */
1928 for (pick = NULL; p != NULL; p = p->p_pglist.le_next)
1929 if (proc_compare(pick, p))
1930 pick = p;
1931
1932 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
1933 pick->p_stat == SONPROC ? "running" :
1934 pick->p_stat == SRUN ? "runnable" :
1935 pick->p_wmesg ? pick->p_wmesg : "iowait");
1936
1937 calcru(pick, &utime, &stime, NULL);
1938
1939 /* Round up and print user time. */
1940 utime.tv_usec += 5000;
1941 if (utime.tv_usec >= 1000000) {
1942 utime.tv_sec += 1;
1943 utime.tv_usec -= 1000000;
1944 }
1945 ttyprintf(tp, "%ld.%02ldu ", (long int)utime.tv_sec,
1946 (long int)utime.tv_usec / 10000);
1947
1948 /* Round up and print system time. */
1949 stime.tv_usec += 5000;
1950 if (stime.tv_usec >= 1000000) {
1951 stime.tv_sec += 1;
1952 stime.tv_usec -= 1000000;
1953 }
1954 ttyprintf(tp, "%ld.%02lds ", (long int)stime.tv_sec,
1955 (long int)stime.tv_usec / 10000);
1956
1957 #define pgtok(a) (((u_long) ((a) * PAGE_SIZE) / 1024))
1958 /* Print percentage cpu. */
1959 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
1960 ttyprintf(tp, "%d%% ", tmp / 100);
1961
1962 /* Print resident set size. */
1963 if (pick->p_stat == SIDL || P_ZOMBIE(pick))
1964 tmp = 0;
1965 else {
1966 struct vmspace *vm = pick->p_vmspace;
1967 tmp = pgtok(vm_resident_count(vm));
1968 }
1969 ttyprintf(tp, "%dk\n", tmp);
1970 }
1971 tp->t_rocount = 0; /* so pending input will be retyped if BS */
1972 }
1973
1974 /*
1975 * Returns 1 if p2 is "better" than p1
1976 *
1977 * The algorithm for picking the "interesting" process is thus:
1978 *
1979 * 1) Only foreground processes are eligible - implied.
1980 * 2) Runnable processes are favored over anything else. The runner
1981 * with the highest cpu utilization is picked (p_estcpu). Ties are
1982 * broken by picking the highest pid.
1983 * 3) The sleeper with the shortest sleep time is next. With ties,
1984 * we pick out just "short-term" sleepers (P_SINTR == 0).
1985 * 4) Further ties are broken by picking the highest pid.
1986 */
1987 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL) || \
1988 ((p)->p_stat == SONPROC))
1989 #define TESTAB(a, b) ((a)<<1 | (b))
1990 #define ONLYA 2
1991 #define ONLYB 1
1992 #define BOTH 3
1993
1994 static int
1995 proc_compare(struct proc *p1, struct proc *p2)
1996 {
1997
1998 if (p1 == NULL)
1999 return (1);
2000 /*
2001 * see if at least one of them is runnable
2002 */
2003 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2004 case ONLYA:
2005 return (0);
2006 case ONLYB:
2007 return (1);
2008 case BOTH:
2009 /*
2010 * tie - favor one with highest recent cpu utilization
2011 */
2012 if (p2->p_estcpu > p1->p_estcpu)
2013 return (1);
2014 if (p1->p_estcpu > p2->p_estcpu)
2015 return (0);
2016 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2017 }
2018 /*
2019 * weed out zombies
2020 */
2021 switch (TESTAB(P_ZOMBIE(p1), P_ZOMBIE(p2))) {
2022 case ONLYA:
2023 return (1);
2024 case ONLYB:
2025 return (0);
2026 case BOTH:
2027 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2028 }
2029 /*
2030 * pick the one with the smallest sleep time
2031 */
2032 if (p2->p_slptime > p1->p_slptime)
2033 return (0);
2034 if (p1->p_slptime > p2->p_slptime)
2035 return (1);
2036 /*
2037 * favor one sleeping in a non-interruptible sleep
2038 */
2039 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
2040 return (1);
2041 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
2042 return (0);
2043 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2044 }
2045
2046 /*
2047 * Output char to tty; console putchar style.
2048 */
2049 int
2050 tputchar(int c, struct tty *tp)
2051 {
2052 int s;
2053
2054 s = spltty();
2055 if (ISSET(tp->t_state,
2056 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) {
2057 splx(s);
2058 return (-1);
2059 }
2060 if (c == '\n')
2061 (void)ttyoutput('\r', tp);
2062 (void)ttyoutput(c, tp);
2063 ttstart(tp);
2064 splx(s);
2065 return (0);
2066 }
2067
2068 /*
2069 * Sleep on chan, returning ERESTART if tty changed while we napped and
2070 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
2071 * the tty is revoked, restarting a pending call will redo validation done
2072 * at the start of the call.
2073 */
2074 int
2075 ttysleep(struct tty *tp, void *chan, int pri, const char *wmesg, int timo)
2076 {
2077 int error;
2078 short gen;
2079
2080 gen = tp->t_gen;
2081 if ((error = tsleep(chan, pri, wmesg, timo)) != 0)
2082 return (error);
2083 return (tp->t_gen == gen ? 0 : ERESTART);
2084 }
2085
2086 /*
2087 * Initialise the global tty list.
2088 */
2089 void
2090 tty_init(void)
2091 {
2092 ttyldisc_init();
2093
2094 TAILQ_INIT(&ttylist);
2095 tty_count = 0;
2096
2097 pool_init(&tty_pool, sizeof(struct tty), 0, 0, 0, "ttypl",
2098 0, pool_page_alloc_nointr, pool_page_free_nointr, M_TTYS);
2099 }
2100
2101 /*
2102 * Attach a tty to the tty list.
2103 *
2104 * This should be called ONLY once per real tty (including pty's).
2105 * eg, on the sparc, the keyboard and mouse have struct tty's that are
2106 * distinctly NOT usable as tty's, and thus should not be attached to
2107 * the ttylist. This is why this call is not done from ttymalloc().
2108 *
2109 * Device drivers should attach tty's at a similar time that they are
2110 * ttymalloc()'ed, or, for the case of statically allocated struct tty's
2111 * either in the attach or (first) open routine.
2112 */
2113 void
2114 tty_attach(struct tty *tp)
2115 {
2116
2117 TAILQ_INSERT_TAIL(&ttylist, tp, tty_link);
2118 ++tty_count;
2119 }
2120
2121 /*
2122 * Remove a tty from the tty list.
2123 */
2124 void
2125 tty_detach(struct tty *tp)
2126 {
2127
2128 --tty_count;
2129 #ifdef DIAGNOSTIC
2130 if (tty_count < 0)
2131 panic("tty_detach: tty_count < 0");
2132 #endif
2133 TAILQ_REMOVE(&ttylist, tp, tty_link);
2134 }
2135
2136 /*
2137 * Allocate a tty structure and its associated buffers.
2138 */
2139 struct tty *
2140 ttymalloc(void)
2141 {
2142 struct tty *tp;
2143
2144 tp = pool_get(&tty_pool, PR_WAITOK);
2145 memset(tp, 0, sizeof(*tp));
2146 callout_init(&tp->t_outq_ch);
2147 callout_init(&tp->t_rstrt_ch);
2148 /* XXX: default to 1024 chars for now */
2149 clalloc(&tp->t_rawq, 1024, 1);
2150 clalloc(&tp->t_canq, 1024, 1);
2151 /* output queue doesn't need quoting */
2152 clalloc(&tp->t_outq, 1024, 0);
2153 /* Set default line discipline. */
2154 tp->t_linesw = linesw[0];
2155 return(tp);
2156 }
2157
2158 /*
2159 * Free a tty structure and its buffers.
2160 *
2161 * Be sure to call tty_detach() for any tty that has been
2162 * tty_attach()ed.
2163 */
2164 void
2165 ttyfree(struct tty *tp)
2166 {
2167
2168 callout_stop(&tp->t_outq_ch);
2169 callout_stop(&tp->t_rstrt_ch);
2170 clfree(&tp->t_rawq);
2171 clfree(&tp->t_canq);
2172 clfree(&tp->t_outq);
2173 pool_put(&tty_pool, tp);
2174 }
2175