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