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