2 * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)tty.c 8.8 (Berkeley) 1/21/94
44 * o Fix races for sending the start char in ttyflush().
45 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
46 * With luck, there will be MIN chars before select() returns().
47 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
48 * o Don't allow input in TS_ZOMBIE case. It would be visible through
50 * o Do the new sio locking stuff here and use it to avoid special
53 * o Move EXTPROC and/or PENDIN to t_state?
54 * o Wrap most of ttioctl in spltty/splx.
55 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
56 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
57 * o Don't allow certain termios flags to affect disciplines other
58 * than TTYDISC. Cancel their effects before switch disciplines
59 * and ignore them if they are set while we are in another
61 * o Now that historical speed conversions are handled here, don't
63 * o Check for TS_CARR_ON being set while everything is closed and not
64 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
65 * so it would live until the next open even if carrier drops.
66 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
67 * only when _all_ openers leave open().
71 #include "opt_compat.h"
72 #include "opt_uconsole.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/filio.h>
77 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
78 #include <sys/ioctl_compat.h>
84 #include <sys/fcntl.h>
86 #include <sys/dkstat.h>
88 #include <sys/kernel.h>
89 #include <sys/vnode.h>
90 #include <sys/signalvar.h>
91 #include <sys/resourcevar.h>
92 #include <sys/malloc.h>
93 #include <sys/filedesc.h>
95 #include <sys/snoop.h>
97 #include <sys/sysctl.h>
100 #include <sys/lock.h>
102 #include <vm/vm_map.h>
104 MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures");
106 static int proc_compare __P((struct proc *p1, struct proc *p2));
107 static int ttnread __P((struct tty *tp));
108 static void ttyecho __P((int c, struct tty *tp));
109 static int ttyoutput __P((int c, register struct tty *tp));
110 static void ttypend __P((struct tty *tp));
111 static void ttyretype __P((struct tty *tp));
112 static void ttyrub __P((int c, struct tty *tp));
113 static void ttyrubo __P((struct tty *tp, int cnt));
114 static void ttyunblock __P((struct tty *tp));
115 static int ttywflush __P((struct tty *tp));
118 * Table with character classes and parity. The 8th bit indicates parity,
119 * the 7th bit indicates the character is an alphameric or underscore (for
120 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
121 * are 0 then the character needs no special processing on output; classes
122 * other than 0 might be translated or (not currently) require delays.
124 #define E 0x00 /* Even parity. */
125 #define O 0x80 /* Odd parity. */
126 #define PARITY(c) (char_type[c] & O)
128 #define ALPHA 0x40 /* Alpha or underscore. */
129 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
131 #define CCLASSMASK 0x3f
132 #define CCLASS(c) (char_type[c] & CCLASSMASK)
137 #define NA ORDINARY | ALPHA
143 static u_char const char_type[] = {
144 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
145 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
146 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
147 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
148 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
149 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
150 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
151 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
152 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
153 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
154 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
155 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
156 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
157 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
158 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
159 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
161 * Meta chars; should be settable per character set;
162 * for now, treat them all as normal characters.
164 NA, NA, NA, NA, NA, NA, NA, NA,
165 NA, NA, NA, NA, NA, NA, NA, NA,
166 NA, NA, NA, NA, NA, NA, NA, NA,
167 NA, NA, NA, NA, NA, NA, NA, NA,
168 NA, NA, NA, NA, NA, NA, NA, NA,
169 NA, NA, NA, NA, NA, NA, NA, NA,
170 NA, NA, NA, NA, NA, NA, NA, NA,
171 NA, NA, NA, NA, NA, NA, NA, NA,
172 NA, NA, NA, NA, NA, NA, NA, NA,
173 NA, NA, NA, NA, NA, NA, NA, NA,
174 NA, NA, NA, NA, NA, NA, NA, NA,
175 NA, NA, NA, NA, NA, NA, NA, NA,
176 NA, NA, NA, NA, NA, NA, NA, NA,
177 NA, NA, NA, NA, NA, NA, NA, NA,
178 NA, NA, NA, NA, NA, NA, NA, NA,
179 NA, NA, NA, NA, NA, NA, NA, NA,
190 /* Macros to clear/set/test flags. */
191 #define SET(t, f) (t) |= (f)
192 #define CLR(t, f) (t) &= ~(f)
193 #define ISSET(t, f) ((t) & (f))
195 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
196 #define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */
199 * list of struct tty where pstat(8) can pick it up with sysctl
201 static SLIST_HEAD(, tty) tty_list;
204 * Initial open of tty, or (re)entry to standard tty line discipline.
209 register struct tty *tp;
215 if (!ISSET(tp->t_state, TS_ISOPEN)) {
216 SET(tp->t_state, TS_ISOPEN);
217 if (ISSET(tp->t_cflag, CLOCAL))
218 SET(tp->t_state, TS_CONNECTED);
219 bzero(&tp->t_winsize, sizeof(tp->t_winsize));
227 * Handle close() on a tty line: flush and set to initial state,
228 * bumping generation number so that pending read/write calls
229 * can detect recycling of the tty.
230 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
231 * and l_close() should have flushed, but we repeat the spltty() and
232 * the flush in case there are buggy callers.
236 register struct tty *tp;
240 funsetown(tp->t_sigio);
245 ttyflush(tp, FREAD | FWRITE);
246 clist_free_cblocks(&tp->t_canq);
247 clist_free_cblocks(&tp->t_outq);
248 clist_free_cblocks(&tp->t_rawq);
251 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL)
252 snpdown((struct snoop *)tp->t_sc);
256 tp->t_line = TTYDISC;
258 tp->t_session = NULL;
264 #define FLUSHQ(q) { \
266 ndflush(q, (q)->c_cc); \
269 /* Is 'c' a line delimiter ("break" character)? */
270 #define TTBREAKC(c, lflag) \
271 ((c) == '\n' || (((c) == cc[VEOF] || \
272 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
273 (c) != _POSIX_VDISABLE))
276 * Process input of a single character received on a tty.
281 register struct tty *tp;
283 register tcflag_t iflag, lflag;
288 * If input is pending take it first.
291 if (ISSET(lflag, PENDIN))
296 if (ISSET(lflag, ICANON)) {
306 * Block further input iff:
307 * current input > threshold AND input is available to user program
308 * AND input flow control is enabled and not yet invoked.
309 * The 3 is slop for PARMRK.
312 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 &&
313 (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) &&
314 (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) &&
315 !ISSET(tp->t_state, TS_TBLOCK))
318 /* Handle exceptional conditions (break, parity, framing). */
320 err = (ISSET(c, TTY_ERRORMASK));
322 CLR(c, TTY_ERRORMASK);
323 if (ISSET(err, TTY_BI)) {
324 if (ISSET(iflag, IGNBRK))
326 if (ISSET(iflag, BRKINT)) {
327 ttyflush(tp, FREAD | FWRITE);
328 pgsignal(tp->t_pgrp, SIGINT, 1);
331 if (ISSET(iflag, PARMRK))
333 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK))
334 || ISSET(err, TTY_FE)) {
335 if (ISSET(iflag, IGNPAR))
337 else if (ISSET(iflag, PARMRK)) {
339 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >
342 (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);
351 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
353 if (!ISSET(lflag, EXTPROC)) {
355 * Check for literal nexting very first
357 if (ISSET(tp->t_state, TS_LNCH)) {
359 CLR(tp->t_state, TS_LNCH);
362 * Scan for special characters. This code
363 * is really just a big case statement with
364 * non-constant cases. The bottom of the
365 * case statement is labeled ``endcase'', so goto
366 * it after a case match, or similar.
370 * Control chars which aren't controlled
371 * by ICANON, ISIG, or IXON.
373 if (ISSET(lflag, IEXTEN)) {
374 if (CCEQ(cc[VLNEXT], c)) {
375 if (ISSET(lflag, ECHO)) {
376 if (ISSET(lflag, ECHOE)) {
377 (void)ttyoutput('^', tp);
378 (void)ttyoutput('\b', tp);
382 SET(tp->t_state, TS_LNCH);
385 if (CCEQ(cc[VDISCARD], c)) {
386 if (ISSET(lflag, FLUSHO))
387 CLR(tp->t_lflag, FLUSHO);
389 ttyflush(tp, FWRITE);
391 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
393 SET(tp->t_lflag, FLUSHO);
401 if (ISSET(lflag, ISIG)) {
402 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
403 if (!ISSET(lflag, NOFLSH))
404 ttyflush(tp, FREAD | FWRITE);
407 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
410 if (CCEQ(cc[VSUSP], c)) {
411 if (!ISSET(lflag, NOFLSH))
414 pgsignal(tp->t_pgrp, SIGTSTP, 1);
419 * Handle start/stop characters.
421 if (ISSET(iflag, IXON)) {
422 if (CCEQ(cc[VSTOP], c)) {
423 if (!ISSET(tp->t_state, TS_TTSTOP)) {
424 SET(tp->t_state, TS_TTSTOP);
425 #ifdef sun4c /* XXX */
426 (*tp->t_stop)(tp, 0);
428 (*devsw(tp->t_dev)->d_stop)(tp,
433 if (!CCEQ(cc[VSTART], c))
436 * if VSTART == VSTOP then toggle
440 if (CCEQ(cc[VSTART], c))
444 * IGNCR, ICRNL, & INLCR
447 if (ISSET(iflag, IGNCR))
449 else if (ISSET(iflag, ICRNL))
451 } else if (c == '\n' && ISSET(iflag, INLCR))
454 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
456 * From here on down canonical mode character
457 * processing takes place.
462 if (CCEQ(cc[VERASE], c)) {
464 ttyrub(unputc(&tp->t_rawq), tp);
470 if (CCEQ(cc[VKILL], c)) {
471 if (ISSET(lflag, ECHOKE) &&
472 tp->t_rawq.c_cc == tp->t_rocount &&
473 !ISSET(lflag, ECHOPRT))
474 while (tp->t_rawq.c_cc)
475 ttyrub(unputc(&tp->t_rawq), tp);
478 if (ISSET(lflag, ECHOK) ||
479 ISSET(lflag, ECHOKE))
484 CLR(tp->t_state, TS_LOCAL);
490 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) {
496 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
501 * erase last char of word and remember the
502 * next chars type (for ALTWERASE)
505 c = unputc(&tp->t_rawq);
508 if (c == ' ' || c == '\t') {
509 (void)putc(c, &tp->t_rawq);
518 c = unputc(&tp->t_rawq);
521 } while (c != ' ' && c != '\t' &&
522 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype));
523 (void)putc(c, &tp->t_rawq);
529 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) {
534 * ^T - kernel info and generate SIGINFO
536 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) {
537 if (ISSET(lflag, ISIG))
538 pgsignal(tp->t_pgrp, SIGINFO, 1);
539 if (!ISSET(lflag, NOKERNINFO))
545 * Check for input buffer overflow
547 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) {
549 if (ISSET(iflag, IMAXBEL)) {
550 if (tp->t_outq.c_cc < tp->t_ohiwat)
551 (void)ttyoutput(CTRL('g'), tp);
556 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP)
557 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR))
558 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
561 * Put data char in q for user and
562 * wakeup on seeing a line delimiter.
564 if (putc(c, &tp->t_rawq) >= 0) {
565 if (!ISSET(lflag, ICANON)) {
570 if (TTBREAKC(c, lflag)) {
572 catq(&tp->t_rawq, &tp->t_canq);
574 } else if (tp->t_rocount++ == 0)
575 tp->t_rocol = tp->t_column;
576 if (ISSET(tp->t_state, TS_ERASE)) {
578 * end of prterase \.../
580 CLR(tp->t_state, TS_ERASE);
581 (void)ttyoutput('/', tp);
585 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
587 * Place the cursor over the '^' of the ^D.
589 i = imin(2, tp->t_column - i);
591 (void)ttyoutput('\b', tp);
598 * IXANY means allow any character to restart output.
600 if (ISSET(tp->t_state, TS_TTSTOP) &&
601 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
604 CLR(tp->t_lflag, FLUSHO);
605 CLR(tp->t_state, TS_TTSTOP);
607 return (ttstart(tp));
611 * Output a single character on a tty, doing output processing
612 * as needed (expanding tabs, newline processing, etc.).
613 * Returns < 0 if succeeds, otherwise returns char to resend.
619 register struct tty *tp;
621 register tcflag_t oflag;
625 if (!ISSET(oflag, OPOST)) {
626 if (ISSET(tp->t_lflag, FLUSHO))
628 if (putc(c, &tp->t_outq))
635 * Do tab expansion if OXTABS is set. Special case if we external
636 * processing, we don't do the tab expansion because we'll probably
637 * get it wrong. If tab expansion needs to be done, let it happen
640 CLR(c, ~TTY_CHARMASK);
642 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
643 c = 8 - (tp->t_column & 7);
644 if (!ISSET(tp->t_lflag, FLUSHO)) {
645 s = spltty(); /* Don't interrupt tabs. */
646 c -= b_to_q(" ", c, &tp->t_outq);
652 return (c ? -1 : '\t');
654 if (c == CEOT && ISSET(oflag, ONOEOT))
658 * Newline translation: if ONLCR is set,
659 * translate newline into "\r\n".
661 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
664 if (putc('\r', &tp->t_outq))
669 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
688 col = (col + 8) & ~7;
696 * Ioctls for all tty devices. Called after line-discipline specific ioctl
697 * has been called to do discipline-specific functions and/or reject any
698 * of these ioctl commands.
702 ttioctl(tp, cmd, data, flag)
703 register struct tty *tp;
708 register struct proc *p;
711 p = curproc; /* XXX */
713 /* If the ioctl involves modification, hang if in the background. */
737 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
747 while (isbackground(p, tp) &&
748 (p->p_flag & P_PPWAIT) == 0 &&
749 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
750 (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
751 if (p->p_pgrp->pg_jobc == 0)
753 pgsignal(p->p_pgrp, SIGTTOU, 1);
754 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1",
762 switch (cmd) { /* Process the ioctl. */
763 case FIOASYNC: /* set/clear async i/o */
766 SET(tp->t_state, TS_ASYNC);
768 CLR(tp->t_state, TS_ASYNC);
771 case FIONBIO: /* set/clear non-blocking i/o */
772 break; /* XXX: delete. */
773 case FIONREAD: /* get # bytes to read */
775 *(int *)data = ttnread(tp);
781 * Policy -- Don't allow FIOSETOWN on someone else's
784 if (tp->t_session != NULL && !isctty(p, tp))
787 error = fsetown(*(int *)data, &tp->t_sigio);
792 if (tp->t_session != NULL && !isctty(p, tp))
794 *(int *)data = fgetown(tp->t_sigio);
797 case TIOCEXCL: /* set exclusive use of tty */
799 SET(tp->t_state, TS_XCLUDE);
802 case TIOCFLUSH: { /* flush buffers */
803 register int flags = *(int *)data;
806 flags = FREAD | FWRITE;
808 flags &= FREAD | FWRITE;
812 case TIOCCONS: /* become virtual console */
814 if (constty && constty != tp &&
815 ISSET(constty->t_state, TS_CONNECTED))
818 if ((error = suser(p)) != 0)
822 } else if (tp == constty)
825 case TIOCDRAIN: /* wait till output drained */
830 case TIOCGETA: { /* get termios struct */
831 struct termios *t = (struct termios *)data;
833 bcopy(&tp->t_termios, t, sizeof(struct termios));
836 case TIOCGETD: /* get line discipline */
837 *(int *)data = tp->t_line;
839 case TIOCGWINSZ: /* get window size */
840 *(struct winsize *)data = tp->t_winsize;
842 case TIOCGPGRP: /* get pgrp of tty */
845 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
848 case TIOCHPCL: /* hang up on last close */
850 SET(tp->t_cflag, HUPCL);
854 case TIOCNXCL: /* reset exclusive use of tty */
856 CLR(tp->t_state, TS_XCLUDE);
859 case TIOCOUTQ: /* output queue size */
860 *(int *)data = tp->t_outq.c_cc;
862 case TIOCSETA: /* set termios struct */
863 case TIOCSETAW: /* drain output, set */
864 case TIOCSETAF: { /* drn out, fls in, set */
865 register struct termios *t = (struct termios *)data;
867 if (t->c_ispeed == 0)
868 t->c_ispeed = t->c_ospeed;
869 if (t->c_ispeed == 0)
870 t->c_ispeed = tp->t_ospeed;
871 if (t->c_ispeed == 0)
874 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
880 if (cmd == TIOCSETAF)
883 if (!ISSET(t->c_cflag, CIGNORE)) {
885 * Set device hardware.
887 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
891 if (ISSET(t->c_cflag, CLOCAL) &&
892 !ISSET(tp->t_cflag, CLOCAL)) {
894 * XXX disconnections would be too hard to
895 * get rid of without this kludge. The only
896 * way to get rid of controlling terminals
897 * is to exit from the session leader.
899 CLR(tp->t_state, TS_ZOMBIE);
901 wakeup(TSA_CARR_ON(tp));
905 if ((ISSET(tp->t_state, TS_CARR_ON) ||
906 ISSET(t->c_cflag, CLOCAL)) &&
907 !ISSET(tp->t_state, TS_ZOMBIE))
908 SET(tp->t_state, TS_CONNECTED);
910 CLR(tp->t_state, TS_CONNECTED);
911 tp->t_cflag = t->c_cflag;
912 tp->t_ispeed = t->c_ispeed;
913 if (t->c_ospeed != 0)
914 tp->t_ospeed = t->c_ospeed;
917 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) &&
919 if (ISSET(t->c_lflag, ICANON))
920 SET(tp->t_lflag, PENDIN);
923 * XXX we really shouldn't allow toggling
924 * ICANON while we're in a non-termios line
925 * discipline. Now we have to worry about
926 * panicing for a null queue.
928 if (tp->t_canq.c_cbreserved > 0 &&
929 tp->t_rawq.c_cbreserved > 0) {
930 catq(&tp->t_rawq, &tp->t_canq);
932 * XXX the queue limits may be
933 * different, so the old queue
934 * swapping method no longer works.
936 catq(&tp->t_canq, &tp->t_rawq);
938 CLR(tp->t_lflag, PENDIN);
942 tp->t_iflag = t->c_iflag;
943 tp->t_oflag = t->c_oflag;
945 * Make the EXTPROC bit read only.
947 if (ISSET(tp->t_lflag, EXTPROC))
948 SET(t->c_lflag, EXTPROC);
950 CLR(t->c_lflag, EXTPROC);
951 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
952 if (t->c_cc[VMIN] != tp->t_cc[VMIN] ||
953 t->c_cc[VTIME] != tp->t_cc[VTIME])
955 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
959 case TIOCSETD: { /* set line discipline */
960 register int t = *(int *)data;
961 dev_t device = tp->t_dev;
963 if ((u_int)t >= nlinesw)
965 if (t != tp->t_line) {
967 (*linesw[tp->t_line].l_close)(tp, flag);
968 error = (*linesw[t].l_open)(device, tp);
970 (void)(*linesw[tp->t_line].l_open)(device, tp);
979 case TIOCSTART: /* start output, like ^Q */
981 if (ISSET(tp->t_state, TS_TTSTOP) ||
982 ISSET(tp->t_lflag, FLUSHO)) {
983 CLR(tp->t_lflag, FLUSHO);
984 CLR(tp->t_state, TS_TTSTOP);
989 case TIOCSTI: /* simulate terminal input */
990 if ((flag & FREAD) == 0 && suser(p))
992 if (!isctty(p, tp) && suser(p))
995 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
998 case TIOCSTOP: /* stop output, like ^S */
1000 if (!ISSET(tp->t_state, TS_TTSTOP)) {
1001 SET(tp->t_state, TS_TTSTOP);
1002 #ifdef sun4c /* XXX */
1003 (*tp->t_stop)(tp, 0);
1005 (*devsw(tp->t_dev)->d_stop)(tp, 0);
1010 case TIOCSCTTY: /* become controlling tty */
1011 /* Session ctty vnode pointer set in vnode layer. */
1012 if (!SESS_LEADER(p) ||
1013 ((p->p_session->s_ttyvp || tp->t_session) &&
1014 (tp->t_session != p->p_session)))
1016 tp->t_session = p->p_session;
1017 tp->t_pgrp = p->p_pgrp;
1018 p->p_session->s_ttyp = tp;
1019 p->p_flag |= P_CONTROLT;
1021 case TIOCSPGRP: { /* set pgrp of tty */
1022 register struct pgrp *pgrp = pgfind(*(int *)data);
1026 else if (pgrp == NULL || pgrp->pg_session != p->p_session)
1031 case TIOCSTAT: /* simulate control-T */
1036 case TIOCSWINSZ: /* set window size */
1037 if (bcmp((caddr_t)&tp->t_winsize, data,
1038 sizeof (struct winsize))) {
1039 tp->t_winsize = *(struct winsize *)data;
1040 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1043 case TIOCSDRAINWAIT:
1047 tp->t_timeout = *(int *)data * hz;
1048 wakeup(TSA_OCOMPLETE(tp));
1049 wakeup(TSA_OLOWAT(tp));
1051 case TIOCGDRAINWAIT:
1052 *(int *)data = tp->t_timeout / hz;
1055 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1056 return (ttcompat(tp, cmd, data, flag));
1065 ttypoll(tp, events, p)
1073 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */
1074 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM))
1078 if (events & (POLLIN | POLLRDNORM)) {
1079 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE))
1080 revents |= events & (POLLIN | POLLRDNORM);
1082 selrecord(p, &tp->t_rsel);
1084 if (events & (POLLOUT | POLLWRNORM)) {
1085 if ((tp->t_outq.c_cc <= tp->t_olowat &&
1086 ISSET(tp->t_state, TS_CONNECTED))
1087 || ISSET(tp->t_state, TS_ZOMBIE))
1088 revents |= events & (POLLOUT | POLLWRNORM);
1090 selrecord(p, &tp->t_wsel);
1097 * This is a wrapper for compatibility with the select vector used by
1098 * cdevsw. It relies on a proper xxxdevtotty routine.
1101 ttpoll(dev, events, p)
1106 return ttypoll((*devsw(dev)->d_devtotty)(dev), events, p);
1110 * Must be called at spltty().
1118 if (ISSET(tp->t_lflag, PENDIN))
1120 nread = tp->t_canq.c_cc;
1121 if (!ISSET(tp->t_lflag, ICANON)) {
1122 nread += tp->t_rawq.c_cc;
1123 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0)
1130 * Wait for output to drain.
1134 register struct tty *tp;
1140 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1141 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) {
1143 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1144 ISSET(tp->t_state, TS_CONNECTED)) {
1145 SET(tp->t_state, TS_SO_OCOMPLETE);
1146 error = ttysleep(tp, TSA_OCOMPLETE(tp),
1147 TTOPRI | PCATCH, "ttywai",
1150 if (error == EWOULDBLOCK)
1157 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)))
1164 * Flush if successfully wait.
1172 if ((error = ttywait(tp)) == 0)
1173 ttyflush(tp, FREAD);
1178 * Flush tty read and/or write queues, notifying anyone waiting.
1182 register struct tty *tp;
1192 FLUSHQ(&tp->t_outq);
1193 CLR(tp->t_state, TS_TTSTOP);
1195 #ifdef sun4c /* XXX */
1196 (*tp->t_stop)(tp, rw);
1198 (*devsw(tp->t_dev)->d_stop)(tp, rw);
1201 FLUSHQ(&tp->t_canq);
1202 FLUSHQ(&tp->t_rawq);
1203 CLR(tp->t_lflag, PENDIN);
1206 CLR(tp->t_state, TS_LOCAL);
1208 if (ISSET(tp->t_state, TS_TBLOCK)) {
1210 FLUSHQ(&tp->t_outq);
1214 * Don't let leave any state that might clobber the
1215 * next line discipline (although we should do more
1216 * to send the START char). Not clearing the state
1217 * may have caused the "putc to a clist with no
1218 * reserved cblocks" panic/printf.
1220 CLR(tp->t_state, TS_TBLOCK);
1222 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1223 if (ISSET(tp->t_iflag, IXOFF)) {
1225 * XXX wait a bit in the hope that the stop
1226 * character (if any) will go out. Waiting
1227 * isn't good since it allows races. This
1228 * will be fixed when the stop character is
1229 * put in a special queue. Don't bother with
1230 * the checks in ttywait() since the timeout
1233 SET(tp->t_state, TS_SO_OCOMPLETE);
1234 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI,
1237 * Don't try sending the stop character again.
1239 CLR(tp->t_state, TS_TBLOCK);
1246 FLUSHQ(&tp->t_outq);
1253 * Copy in the default termios characters.
1260 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc);
1271 termioschars(&tp->t_termios);
1275 * Handle input high water. Send stop character for the IXOFF case. Turn
1276 * on our input flow control bit and propagate the changes to the driver.
1277 * XXX the stop character should be put in a special high priority queue.
1284 SET(tp->t_state, TS_TBLOCK);
1285 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1286 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0)
1287 CLR(tp->t_state, TS_TBLOCK); /* try again later */
1292 * Handle input low water. Send start character for the IXOFF case. Turn
1293 * off our input flow control bit and propagate the changes to the driver.
1294 * XXX the start character should be put in a special high priority queue.
1301 CLR(tp->t_state, TS_TBLOCK);
1302 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE &&
1303 putc(tp->t_cc[VSTART], &tp->t_outq) != 0)
1304 SET(tp->t_state, TS_TBLOCK); /* try again later */
1309 /* Not used by any current (i386) drivers. */
1311 * Restart after an inter-char delay.
1320 KASSERT(tp_arg != NULL, ("ttrstrt"));
1325 CLR(tp->t_state, TS_TIMEOUT);
1337 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1343 * "close" a line discipline
1351 if (flag & FNONBLOCK || ttywflush(tp))
1352 ttyflush(tp, FREAD | FWRITE);
1357 * Handle modem control transition on a tty.
1358 * Flag indicates new state of carrier.
1359 * Returns 0 if the line should be turned off, otherwise 1.
1363 register struct tty *tp;
1367 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) {
1369 * MDMBUF: do flow control according to carrier flag
1370 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1371 * works if IXON and IXANY are clear.
1374 CLR(tp->t_state, TS_CAR_OFLOW);
1375 CLR(tp->t_state, TS_TTSTOP);
1377 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) {
1378 SET(tp->t_state, TS_CAR_OFLOW);
1379 SET(tp->t_state, TS_TTSTOP);
1380 #ifdef sun4c /* XXX */
1381 (*tp->t_stop)(tp, 0);
1383 (*devsw(tp->t_dev)->d_stop)(tp, 0);
1386 } else if (flag == 0) {
1390 CLR(tp->t_state, TS_CARR_ON);
1391 if (ISSET(tp->t_state, TS_ISOPEN) &&
1392 !ISSET(tp->t_cflag, CLOCAL)) {
1393 SET(tp->t_state, TS_ZOMBIE);
1394 CLR(tp->t_state, TS_CONNECTED);
1395 if (tp->t_session && tp->t_session->s_leader)
1396 psignal(tp->t_session->s_leader, SIGHUP);
1397 ttyflush(tp, FREAD | FWRITE);
1404 SET(tp->t_state, TS_CARR_ON);
1405 if (!ISSET(tp->t_state, TS_ZOMBIE))
1406 SET(tp->t_state, TS_CONNECTED);
1407 wakeup(TSA_CARR_ON(tp));
1415 * Reinput pending characters after state switch
1420 register struct tty *tp;
1425 CLR(tp->t_lflag, PENDIN);
1426 SET(tp->t_state, TS_TYPEN);
1428 * XXX this assumes too much about clist internals. It may even
1429 * fail if the cblock slush pool is empty. We can't allocate more
1430 * cblocks here because we are called from an interrupt handler
1431 * and clist_alloc_cblocks() can wait.
1434 bzero(&tp->t_rawq, sizeof tp->t_rawq);
1435 tp->t_rawq.c_cbmax = tq.c_cbmax;
1436 tp->t_rawq.c_cbreserved = tq.c_cbreserved;
1437 while ((c = getc(&tq)) >= 0)
1439 CLR(tp->t_state, TS_TYPEN);
1443 * Process a read call on a tty device.
1446 ttread(tp, uio, flag)
1447 register struct tty *tp;
1451 register struct clist *qp;
1453 register tcflag_t lflag;
1454 register cc_t *cc = tp->t_cc;
1455 register struct proc *p = curproc;
1456 int s, first, error = 0;
1457 int has_stime = 0, last_cc = 0;
1458 long slp = 0; /* XXX this should be renamed `timo'. */
1459 struct timeval stime;
1463 lflag = tp->t_lflag;
1465 * take pending input first
1467 if (ISSET(lflag, PENDIN)) {
1469 splx(s); /* reduce latency */
1471 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */
1475 * Hang process if it's in the background.
1477 if (isbackground(p, tp)) {
1479 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
1480 (p->p_sigmask & sigmask(SIGTTIN)) ||
1481 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0)
1483 pgsignal(p->p_pgrp, SIGTTIN, 1);
1484 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0);
1490 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1492 return (0); /* EOF */
1496 * If canonical, use the canonical queue,
1497 * else use the raw queue.
1499 * (should get rid of clists...)
1501 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq;
1503 if (flag & IO_NDELAY) {
1506 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) {
1511 return (EWOULDBLOCK);
1513 if (!ISSET(lflag, ICANON)) {
1516 struct timeval timecopy;
1519 * Check each of the four combinations.
1520 * (m > 0 && t == 0) is the normal read case.
1521 * It should be fairly efficient, so we check that and its
1522 * companion case (m == 0 && t == 0) first.
1523 * For the other two cases, we compute the target sleep time
1532 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1536 t *= 100000; /* time in us */
1537 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1538 ((t1).tv_usec - (t2).tv_usec))
1544 getmicrotime(&timecopy);
1546 /* first character, start timer */
1550 } else if (qp->c_cc > last_cc) {
1551 /* got a character, restart timer */
1555 /* nothing, check expiration */
1556 slp = t - diff(timecopy, stime);
1561 } else { /* m == 0 */
1564 getmicrotime(&timecopy);
1570 slp = t - diff(timecopy, stime);
1572 /* Timed out, but 0 is enough input. */
1580 * Rounding down may make us wake up just short
1581 * of the target, so we round up.
1582 * The formula is ceiling(slp * hz/1000000).
1583 * 32-bit arithmetic is enough for hz < 169.
1584 * XXX see tvtohz() for how to avoid overflow if hz
1585 * is large (divide by `tick' and/or arrange to
1586 * use tvtohz() if hz is large).
1588 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1591 if (qp->c_cc <= 0) {
1594 * There is no input, or not enough input and we can block.
1596 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH,
1597 ISSET(tp->t_state, TS_CONNECTED) ?
1598 "ttyin" : "ttyhup", (int)slp);
1600 if (error == EWOULDBLOCK)
1605 * XXX what happens if another process eats some input
1606 * while we are asleep (not just here)? It would be
1607 * safest to detect changes and reset our state variables
1608 * (has_stime and last_cc).
1616 * Input present, check for input mapping and processing.
1619 if (ISSET(lflag, ICANON | ISIG))
1625 icc = imin(uio->uio_resid, IBUFSIZ);
1626 icc = q_to_b(qp, ibuf, icc);
1632 error = uiomove(ibuf, icc, uio);
1634 * XXX if there was an error then we should ungetc() the
1635 * unmoved chars and reduce icc here.
1638 if (ISSET(tp->t_lflag, ECHO) &&
1639 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL)
1640 snpin((struct snoop *)tp->t_sc, ibuf, icc);
1644 if (uio->uio_resid == 0)
1658 * delayed suspend (^Y)
1660 if (CCEQ(cc[VDSUSP], c) &&
1661 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) {
1662 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1664 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH,
1673 * Interpret EOF only in canonical mode.
1675 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1678 * Give user character.
1680 error = ureadc(c, uio);
1682 /* XXX should ungetc(c, qp). */
1686 * Only snoop directly on input in echo mode. Non-echoed
1687 * input will be snooped later iff the application echoes it.
1689 if (ISSET(tp->t_lflag, ECHO) &&
1690 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL)
1691 snpinc((struct snoop *)tp->t_sc, (char)c);
1693 if (uio->uio_resid == 0)
1696 * In canonical mode check for a "break character"
1697 * marking the end of a "line of input".
1699 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1706 * Look to unblock input now that (presumably)
1707 * the input queue has gone down.
1710 if (ISSET(tp->t_state, TS_TBLOCK) &&
1711 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat)
1719 * Check the output queue on tp for space for a kernel message (from uprintf
1720 * or tprintf). Allow some space over the normal hiwater mark so we don't
1721 * lose messages due to normal flow control, but don't let the tty run amok.
1722 * Sleeps here are not interruptible, but we return prematurely if new signals
1726 ttycheckoutq(tp, wait)
1727 register struct tty *tp;
1730 int hiwat, s, oldsig;
1732 hiwat = tp->t_ohiwat;
1734 oldsig = wait ? curproc->p_siglist : 0;
1735 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100)
1736 while (tp->t_outq.c_cc > hiwat) {
1738 if (tp->t_outq.c_cc <= hiwat)
1740 if (wait == 0 || curproc->p_siglist != oldsig) {
1744 SET(tp->t_state, TS_SO_OLOWAT);
1745 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz);
1752 * Process a write call on a tty device.
1755 ttwrite(tp, uio, flag)
1756 register struct tty *tp;
1757 register struct uio *uio;
1760 register char *cp = NULL;
1761 register int cc, ce;
1762 register struct proc *p;
1763 int i, hiwat, cnt, error, s;
1766 hiwat = tp->t_ohiwat;
1767 cnt = uio->uio_resid;
1772 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1774 if (uio->uio_resid == cnt)
1778 if (!ISSET(tp->t_state, TS_CONNECTED)) {
1779 if (flag & IO_NDELAY) {
1781 error = EWOULDBLOCK;
1784 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1793 * Hang the process if it's in the background.
1796 if (isbackground(p, tp) &&
1797 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1798 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
1799 (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
1800 if (p->p_pgrp->pg_jobc == 0) {
1804 pgsignal(p->p_pgrp, SIGTTOU, 1);
1805 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0);
1811 * Process the user's data in at most OBUFSIZ chunks. Perform any
1812 * output translation. Keep track of high water mark, sleep on
1813 * overflow awaiting device aid in acquiring new space.
1815 while (uio->uio_resid > 0 || cc > 0) {
1816 if (ISSET(tp->t_lflag, FLUSHO)) {
1820 if (tp->t_outq.c_cc > hiwat)
1823 * Grab a hunk of data from the user, unless we have some
1824 * leftover from last time.
1827 cc = imin(uio->uio_resid, OBUFSIZ);
1829 error = uiomove(cp, cc, uio);
1835 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL)
1836 snpin((struct snoop *)tp->t_sc, cp, cc);
1840 * If nothing fancy need be done, grab those characters we
1841 * can handle without any of ttyoutput's processing and
1842 * just transfer them to the output q. For those chars
1843 * which require special processing (as indicated by the
1844 * bits in char_type), call ttyoutput. After processing
1845 * a hunk of data, look for FLUSHO so ^O's will take effect
1849 if (!ISSET(tp->t_oflag, OPOST))
1852 ce = cc - scanc((u_int)cc, (u_char *)cp,
1853 char_type, CCLASSMASK);
1855 * If ce is zero, then we're processing
1856 * a special character through ttyoutput.
1860 if (ttyoutput(*cp, tp) >= 0) {
1861 /* No Clists, wait a bit. */
1863 if (flag & IO_NDELAY) {
1864 error = EWOULDBLOCK;
1867 error = ttysleep(tp, &lbolt,
1876 if (ISSET(tp->t_lflag, FLUSHO) ||
1877 tp->t_outq.c_cc > hiwat)
1883 * A bunch of normal characters have been found.
1884 * Transfer them en masse to the output queue and
1885 * continue processing at the top of the loop.
1886 * If there are any further characters in this
1887 * <= OBUFSIZ chunk, the first should be a character
1888 * requiring special handling by ttyoutput.
1891 i = b_to_q(cp, ce, &tp->t_outq);
1894 cp += ce, cc -= ce, tk_nout += ce;
1897 /* No Clists, wait a bit. */
1899 if (flag & IO_NDELAY) {
1900 error = EWOULDBLOCK;
1903 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH,
1909 if (ISSET(tp->t_lflag, FLUSHO) ||
1910 tp->t_outq.c_cc > hiwat)
1917 * If cc is nonzero, we leave the uio structure inconsistent, as the
1918 * offset and iov pointers have moved forward, but it doesn't matter
1919 * (the call will either return short or restart with a new uio).
1921 uio->uio_resid += cc;
1928 * This can only occur if FLUSHO is set in t_lflag,
1929 * or if ttstart/oproc is synchronous (or very fast).
1931 if (tp->t_outq.c_cc <= hiwat) {
1935 if (flag & IO_NDELAY) {
1937 uio->uio_resid += cc;
1938 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1940 SET(tp->t_state, TS_SO_OLOWAT);
1941 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri",
1944 if (error == EWOULDBLOCK)
1952 * Rubout one character from the rawq of tp
1953 * as cleanly as possible.
1958 register struct tty *tp;
1961 register int savecol;
1964 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1966 CLR(tp->t_lflag, FLUSHO);
1967 if (ISSET(tp->t_lflag, ECHOE)) {
1968 if (tp->t_rocount == 0) {
1970 * Screwed by ttwrite; retype
1975 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1978 CLR(c, ~TTY_CHARMASK);
1979 switch (CCLASS(c)) {
1988 if (ISSET(tp->t_lflag, ECHOCTL))
1992 if (tp->t_rocount < tp->t_rawq.c_cc) {
1997 savecol = tp->t_column;
1998 SET(tp->t_state, TS_CNTTB);
1999 SET(tp->t_lflag, FLUSHO);
2000 tp->t_column = tp->t_rocol;
2001 cp = tp->t_rawq.c_cf;
2003 tabc = *cp; /* XXX FIX NEXTC */
2004 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc))
2006 CLR(tp->t_lflag, FLUSHO);
2007 CLR(tp->t_state, TS_CNTTB);
2010 /* savecol will now be length of the tab. */
2011 savecol -= tp->t_column;
2012 tp->t_column += savecol;
2014 savecol = 8; /* overflow screw */
2015 while (--savecol >= 0)
2016 (void)ttyoutput('\b', tp);
2019 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2020 (void)printf(PANICSTR, c, CCLASS(c));
2022 panic(PANICSTR, c, CCLASS(c));
2026 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
2027 if (!ISSET(tp->t_state, TS_ERASE)) {
2028 SET(tp->t_state, TS_ERASE);
2029 (void)ttyoutput('\\', tp);
2033 ttyecho(tp->t_cc[VERASE], tp);
2038 * Back over cnt characters, erasing them.
2042 register struct tty *tp;
2047 (void)ttyoutput('\b', tp);
2048 (void)ttyoutput(' ', tp);
2049 (void)ttyoutput('\b', tp);
2055 * Reprint the rawq line. Note, it is assumed that c_cc has already
2060 register struct tty *tp;
2065 /* Echo the reprint character. */
2066 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
2067 ttyecho(tp->t_cc[VREPRINT], tp);
2069 (void)ttyoutput('\n', tp);
2073 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2074 * BIT OF FIRST CHAR.
2077 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0);
2078 cp != NULL; cp = nextc(&tp->t_canq, cp, &c))
2080 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0);
2081 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c))
2083 CLR(tp->t_state, TS_ERASE);
2086 tp->t_rocount = tp->t_rawq.c_cc;
2091 * Echo a typed character to the terminal.
2096 register struct tty *tp;
2099 if (!ISSET(tp->t_state, TS_CNTTB))
2100 CLR(tp->t_lflag, FLUSHO);
2101 if ((!ISSET(tp->t_lflag, ECHO) &&
2102 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) ||
2103 ISSET(tp->t_lflag, EXTPROC))
2105 if (ISSET(tp->t_lflag, ECHOCTL) &&
2106 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') ||
2107 ISSET(c, TTY_CHARMASK) == 0177)) {
2108 (void)ttyoutput('^', tp);
2109 CLR(c, ~TTY_CHARMASK);
2115 (void)ttyoutput(c, tp);
2119 * Wake up any readers on a tty.
2123 register struct tty *tp;
2126 if (tp->t_rsel.si_pid != 0)
2127 selwakeup(&tp->t_rsel);
2128 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2129 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL));
2130 wakeup(TSA_HUP_OR_INPUT(tp));
2134 * Wake up any writers on a tty.
2138 register struct tty *tp;
2141 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat)
2142 selwakeup(&tp->t_wsel);
2143 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
2144 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
2145 CLR(tp->t_state, TS_SO_OCOMPLETE);
2146 wakeup(TSA_OCOMPLETE(tp));
2148 if (ISSET(tp->t_state, TS_SO_OLOWAT) &&
2149 tp->t_outq.c_cc <= tp->t_olowat) {
2150 CLR(tp->t_state, TS_SO_OLOWAT);
2151 wakeup(TSA_OLOWAT(tp));
2156 * Look up a code for a specified speed in a conversion table;
2157 * used by drivers to map software speed values to hardware parameters.
2160 ttspeedtab(speed, table)
2162 register struct speedtab *table;
2165 for ( ; table->sp_speed != -1; table++)
2166 if (table->sp_speed == speed)
2167 return (table->sp_code);
2172 * Set input and output watermarks and buffer sizes. For input, the
2173 * high watermark is about one second's worth of input above empty, the
2174 * low watermark is slightly below high water, and the buffer size is a
2175 * driver-dependent amount above high water. For output, the watermarks
2176 * are near the ends of the buffer, with about 1 second's worth of input
2177 * between them. All this only applies to the standard line discipline.
2183 register int cps, ttmaxhiwat, x;
2186 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512);
2187 switch (tp->t_ispeedwat) {
2189 cps = tp->t_ispeed / 10;
2193 * This case is for old drivers that don't know about
2194 * t_ispeedwat. Arrange for them to get the old buffer
2195 * sizes and watermarks.
2197 cps = TTYHOG - 2 * 256;
2198 tp->t_ififosize = 2 * 256;
2201 cps = tp->t_ispeedwat / 10;
2205 tp->t_ilowat = 7 * cps / 8;
2206 x = cps + tp->t_ififosize;
2207 clist_alloc_cblocks(&tp->t_rawq, x, x);
2210 switch (tp->t_ospeedwat) {
2212 cps = tp->t_ospeed / 10;
2213 ttmaxhiwat = 2 * TTMAXHIWAT;
2216 cps = tp->t_ospeed / 10;
2217 ttmaxhiwat = TTMAXHIWAT;
2220 cps = tp->t_ospeedwat / 10;
2221 ttmaxhiwat = 8 * TTMAXHIWAT;
2224 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2225 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
2227 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */
2228 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */
2229 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */
2231 clist_alloc_cblocks(&tp->t_outq, x, x);
2236 * Report on state of foreground process group.
2240 register struct tty *tp;
2242 register struct proc *p, *pick;
2243 struct timeval utime, stime;
2246 if (ttycheckoutq(tp,0) == 0)
2249 /* Print load average. */
2250 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
2251 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
2253 if (tp->t_session == NULL)
2254 ttyprintf(tp, "not a controlling terminal\n");
2255 else if (tp->t_pgrp == NULL)
2256 ttyprintf(tp, "no foreground process group\n");
2257 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
2258 ttyprintf(tp, "empty foreground process group\n");
2260 /* Pick interesting process. */
2261 for (pick = NULL; p != 0; p = p->p_pglist.le_next)
2262 if (proc_compare(pick, p))
2265 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
2266 pick->p_stat == SRUN ? "running" :
2267 pick->p_wmesg ? pick->p_wmesg : "iowait");
2269 if (pick->p_flag & P_INMEM) {
2270 calcru(pick, &utime, &stime, NULL);
2272 /* Print user time. */
2273 ttyprintf(tp, "%ld.%02ldu ",
2274 utime.tv_sec, utime.tv_usec / 10000);
2276 /* Print system time. */
2277 ttyprintf(tp, "%ld.%02lds ",
2278 stime.tv_sec, stime.tv_usec / 10000);
2280 ttyprintf(tp, "?.??u ?.??s ");
2282 /* Print percentage cpu, resident set size. */
2283 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
2284 ttyprintf(tp, "%d%% %ldk\n",
2286 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 :
2287 (long)pgtok(vmspace_resident_count(pick->p_vmspace)));
2289 tp->t_rocount = 0; /* so pending input will be retyped if BS */
2293 * Returns 1 if p2 is "better" than p1
2295 * The algorithm for picking the "interesting" process is thus:
2297 * 1) Only foreground processes are eligible - implied.
2298 * 2) Runnable processes are favored over anything else. The runner
2299 * with the highest cpu utilization is picked (p_estcpu). Ties are
2300 * broken by picking the highest pid.
2301 * 3) The sleeper with the shortest sleep time is next. With ties,
2302 * we pick out just "short-term" sleepers (P_SINTR == 0).
2303 * 4) Further ties are broken by picking the highest pid.
2305 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2306 #define TESTAB(a, b) ((a)<<1 | (b))
2312 proc_compare(p1, p2)
2313 register struct proc *p1, *p2;
2319 * see if at least one of them is runnable
2321 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2328 * tie - favor one with highest recent cpu utilization
2330 if (p2->p_estcpu > p1->p_estcpu)
2332 if (p1->p_estcpu > p2->p_estcpu)
2334 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2339 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
2345 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2348 * pick the one with the smallest sleep time
2350 if (p2->p_slptime > p1->p_slptime)
2352 if (p1->p_slptime > p2->p_slptime)
2355 * favor one sleeping in a non-interruptible sleep
2357 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
2359 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
2361 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2365 * Output char to tty; console putchar style.
2375 if (!ISSET(tp->t_state, TS_CONNECTED)) {
2380 (void)ttyoutput('\r', tp);
2381 (void)ttyoutput(c, tp);
2388 * Sleep on chan, returning ERESTART if tty changed while we napped and
2389 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2390 * the tty is revoked, restarting a pending call will redo validation done
2391 * at the start of the call.
2394 ttysleep(tp, chan, pri, wmesg, timo)
2404 error = tsleep(chan, pri, wmesg, timo);
2407 return (tp->t_gen == gen ? 0 : ERESTART);
2411 * Allocate a tty struct. Clists in the struct will be allocated by
2421 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK);
2422 bzero(tp, sizeof *tp);
2427 #if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */
2429 * Free a tty struct. Clists in the struct should have been freed by
2444 SLIST_INSERT_HEAD(&tty_list, tp, t_list);
2448 sysctl_kern_ttys SYSCTL_HANDLER_ARGS
2452 SLIST_FOREACH(tp, &tty_list, t_list) {
2455 t.t_dev = (dev_t)dev2udev(t.t_dev);
2456 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t));
2463 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD,
2464 0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys");