]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1990, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tty.c 8.8 (Berkeley) 1/21/94
64 * o Fix races for sending the start char in ttyflush().
65 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
66 * With luck, there will be MIN chars before select() returns().
67 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
68 * o Don't allow input in TS_ZOMBIE case. It would be visible through
70 * o Do the new sio locking stuff here and use it to avoid special
73 * o Move EXTPROC and/or PENDIN to t_state?
74 * o Wrap most of ttioctl in spltty/splx.
75 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
76 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
77 * o Don't allow certain termios flags to affect disciplines other
78 * than TTYDISC. Cancel their effects before switch disciplines
79 * and ignore them if they are set while we are in another
81 * o Handle c_ispeed = 0 to c_ispeed = c_ospeed conversion here instead
82 * of in drivers and fix drivers that write to tp->t_termios.
83 * o Check for TS_CARR_ON being set while everything is closed and not
84 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
85 * so it would live until the next open even if carrier drops.
86 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
87 * only when _all_ openers leave open().
93 #include "opt_uconsole.h"
96 #include <sys/param.h>
98 #include <sys/systm.h>
100 #include <sys/ioctl.h>
101 #include <sys/proc.h>
102 #include <sys/file.h>
103 #include <sys/conf.h>
104 #include <sys/dkstat.h>
106 #include <sys/kernel.h>
107 #include <sys/vnode.h>
108 #include <sys/syslog.h>
109 #include <sys/user.h>
110 #include <sys/signalvar.h>
111 #include <sys/signalvar.h>
113 #include <sys/resourcevar.h>
115 #include <sys/malloc.h>
117 #include <sys/snoop.h>
122 #include <vm/vm_param.h>
123 #include <vm/vm_prot.h>
126 #include <vm/vm_map.h>
128 #include <dev/kmreg_com.h>
129 #include <machine/cons.h>
130 #include <machine/spl.h>
132 #include <machdep/machine/pmap.h>
137 static int proc_compare
__P((struct proc
*p1
, struct proc
*p2
));
139 static int ttnread
__P((struct tty
*tp
));
140 static void ttyecho
__P((int c
, struct tty
*tp
));
141 static int ttyoutput
__P((int c
, register struct tty
*tp
));
142 static void ttypend
__P((struct tty
*tp
));
143 static void ttyretype
__P((struct tty
*tp
));
144 static void ttyrub
__P((int c
, struct tty
*tp
));
145 static void ttyrubo
__P((struct tty
*tp
, int cnt
));
146 static void ttystop
__P((struct tty
*tp
, int rw
));
147 static void ttyunblock
__P((struct tty
*tp
));
148 static int ttywflush
__P((struct tty
*tp
));
151 * Table with character classes and parity. The 8th bit indicates parity,
152 * the 7th bit indicates the character is an alphameric or underscore (for
153 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
154 * are 0 then the character needs no special processing on output; classes
155 * other than 0 might be translated or (not currently) require delays.
157 #define E 0x00 /* Even parity. */
158 #define O 0x80 /* Odd parity. */
159 #define PARITY(c) (char_type[c] & O)
161 #define ALPHA 0x40 /* Alpha or underscore. */
162 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
164 #define CCLASSMASK 0x3f
165 #define CCLASS(c) (char_type[c] & CCLASSMASK)
170 #define NA ORDINARY | ALPHA
176 static u_char
const char_type
[] = {
177 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* nul - bel */
178 O
|BS
, E
|TB
, E
|NL
, O
|CC
, E
|VT
, O
|CR
, O
|CC
, E
|CC
, /* bs - si */
179 O
|CC
, E
|CC
, E
|CC
, O
|CC
, E
|CC
, O
|CC
, O
|CC
, E
|CC
, /* dle - etb */
180 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* can - us */
181 O
|NO
, E
|NO
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* sp - ' */
182 E
|NO
, O
|NO
, O
|NO
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|NO
, /* ( - / */
183 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* 0 - 7 */
184 O
|NA
, E
|NA
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* 8 - ? */
185 O
|NO
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* @ - G */
186 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* H - O */
187 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* P - W */
188 O
|NA
, E
|NA
, E
|NA
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, O
|NA
, /* X - _ */
189 E
|NO
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* ` - g */
190 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* h - o */
191 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* p - w */
192 E
|NA
, O
|NA
, O
|NA
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|CC
, /* x - del */
194 * Meta chars; should be settable per character set;
195 * for now, treat them all as normal characters.
197 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
198 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
199 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
200 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
201 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
202 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
203 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
204 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
205 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
206 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
207 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
208 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
209 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
210 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
211 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
212 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
223 /* Macros to clear/set/test flags. */
224 #define SET(t, f) (t) |= (f)
225 #define CLR(t, f) (t) &= ~(f)
226 #define ISSET(t, f) ((t) & (f))
229 * Input control starts when we would not be able to fit the maximum
230 * contents of the ping-pong buffers and finishes when we would be able
231 * to fit that much plus 1/8 more.
233 #define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */
234 #define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */
236 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
237 #define MAX_INPUT TTYHOG
240 * Initial open of tty, or (re)entry to standard tty line discipline.
245 register struct tty
*tp
;
248 boolean_t funnel_state
;
250 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
253 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
254 SET(tp
->t_state
, TS_ISOPEN
);
255 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
256 SET(tp
->t_state
, TS_CONNECTED
); }
257 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
262 * Initialize or restore a cblock allocation policy suitable for
263 * the standard line discipline.
265 clist_alloc_cblocks(&tp
->t_canq
, TTYHOG
, 512);
266 clist_alloc_cblocks(&tp
->t_outq
, TTMAXHIWAT
+ OBUFSIZ
+ 100,
267 TTMAXHIWAT
+ OBUFSIZ
+ 100);
268 clist_alloc_cblocks(&tp
->t_rawq
, TTYHOG
, TTYHOG
);
272 thread_funnel_set(kernel_flock
, funnel_state
);
277 * Handle close() on a tty line: flush and set to initial state,
278 * bumping generation number so that pending read/write calls
279 * can detect recycling of the tty.
280 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
281 * and l_close() should have flushed, but we repeat the spltty() and
282 * the flush in case there are buggy callers.
286 register struct tty
*tp
;
299 * Closing current console tty; disable printing of console
300 * messages at bottom-level driver.
302 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
303 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
307 ttyflush(tp
, FREAD
| FWRITE
);
309 clist_free_cblocks(&tp
->t_canq
);
310 clist_free_cblocks(&tp
->t_outq
);
311 clist_free_cblocks(&tp
->t_rawq
);
315 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
316 snpdown((struct snoop
*)tp
->t_sc
);
320 tp
->t_line
= TTYDISC
;
322 tp
->t_session
= NULL
;
325 selthreadclear(&tp
->t_wsel
);
326 selthreadclear(&tp
->t_rsel
);
332 #define FLUSHQ(q) { \
334 ndflush(q, (q)->c_cc); \
337 /* Is 'c' a line delimiter ("break" character)? */
338 #define TTBREAKC(c, lflag) \
339 ((c) == '\n' || (((c) == cc[VEOF] || \
340 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
341 (c) != _POSIX_VDISABLE))
344 * Process input of a single character received on a tty.
349 register struct tty
*tp
;
351 register tcflag_t iflag
, lflag
;
354 boolean_t funnel_state
;
356 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
359 * If input is pending take it first.
362 if (ISSET(lflag
, PENDIN
))
367 if (ISSET(lflag
, ICANON
)) {
377 * Block further input iff:
378 * current input > threshold AND input is available to user program
379 * AND input flow control is enabled and not yet invoked.
380 * The 3 is slop for PARMRK.
383 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
384 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
385 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
386 !ISSET(tp
->t_state
, TS_TBLOCK
))
389 /* Handle exceptional conditions (break, parity, framing). */
391 err
= (ISSET(c
, TTY_ERRORMASK
));
393 CLR(c
, TTY_ERRORMASK
);
394 if (ISSET(err
, TTY_BI
)) {
395 if (ISSET(iflag
, IGNBRK
)) {
396 thread_funnel_set(kernel_flock
, funnel_state
);
399 if (ISSET(iflag
, BRKINT
)) {
400 ttyflush(tp
, FREAD
| FWRITE
);
401 pgsignal(tp
->t_pgrp
, SIGINT
, 1);
404 if (ISSET(iflag
, PARMRK
))
406 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
407 || ISSET(err
, TTY_FE
)) {
408 if (ISSET(iflag
, IGNPAR
)) {
409 thread_funnel_set(kernel_flock
, funnel_state
);
412 else if (ISSET(iflag
, PARMRK
)) {
414 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
417 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
418 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
419 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
426 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
428 if (!ISSET(lflag
, EXTPROC
)) {
430 * Check for literal nexting very first
432 if (ISSET(tp
->t_state
, TS_LNCH
)) {
434 CLR(tp
->t_state
, TS_LNCH
);
437 * Scan for special characters. This code
438 * is really just a big case statement with
439 * non-constant cases. The bottom of the
440 * case statement is labeled ``endcase'', so goto
441 * it after a case match, or similar.
445 * Control chars which aren't controlled
446 * by ICANON, ISIG, or IXON.
448 if (ISSET(lflag
, IEXTEN
)) {
449 if (CCEQ(cc
[VLNEXT
], c
)) {
450 if (ISSET(lflag
, ECHO
)) {
451 if (ISSET(lflag
, ECHOE
)) {
452 (void)ttyoutput('^', tp
);
453 (void)ttyoutput('\b', tp
);
457 SET(tp
->t_state
, TS_LNCH
);
460 if (CCEQ(cc
[VDISCARD
], c
)) {
461 if (ISSET(lflag
, FLUSHO
))
462 CLR(tp
->t_lflag
, FLUSHO
);
464 ttyflush(tp
, FWRITE
);
466 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
468 SET(tp
->t_lflag
, FLUSHO
);
476 if (ISSET(lflag
, ISIG
)) {
477 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
478 if (!ISSET(lflag
, NOFLSH
))
479 ttyflush(tp
, FREAD
| FWRITE
);
482 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
485 if (CCEQ(cc
[VSUSP
], c
)) {
486 if (!ISSET(lflag
, NOFLSH
))
489 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
494 * Handle start/stop characters.
496 if (ISSET(iflag
, IXON
)) {
497 if (CCEQ(cc
[VSTOP
], c
)) {
498 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
499 SET(tp
->t_state
, TS_TTSTOP
);
501 thread_funnel_set(kernel_flock
, funnel_state
);
504 if (!CCEQ(cc
[VSTART
], c
)) {
505 thread_funnel_set(kernel_flock
, funnel_state
);
509 * if VSTART == VSTOP then toggle
513 if (CCEQ(cc
[VSTART
], c
))
517 * IGNCR, ICRNL, & INLCR
520 if (ISSET(iflag
, IGNCR
)) {
521 thread_funnel_set(kernel_flock
, funnel_state
);
524 else if (ISSET(iflag
, ICRNL
))
526 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
529 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
531 * From here on down canonical mode character
532 * processing takes place.
537 if (CCEQ(cc
[VERASE
], c
)) {
539 ttyrub(unputc(&tp
->t_rawq
), tp
);
545 if (CCEQ(cc
[VKILL
], c
)) {
546 if (ISSET(lflag
, ECHOKE
) &&
547 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
548 !ISSET(lflag
, ECHOPRT
))
549 while (tp
->t_rawq
.c_cc
)
550 ttyrub(unputc(&tp
->t_rawq
), tp
);
553 if (ISSET(lflag
, ECHOK
) ||
554 ISSET(lflag
, ECHOKE
))
559 CLR(tp
->t_state
, TS_LOCAL
);
565 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
571 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
576 * erase last char of word and remember the
577 * next chars type (for ALTWERASE)
580 c
= unputc(&tp
->t_rawq
);
583 if (c
== ' ' || c
== '\t') {
584 (void)putc(c
, &tp
->t_rawq
);
593 c
= unputc(&tp
->t_rawq
);
596 } while (c
!= ' ' && c
!= '\t' &&
597 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
598 (void)putc(c
, &tp
->t_rawq
);
604 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
609 * ^T - kernel info and generate SIGINFO
611 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
612 if (ISSET(lflag
, ISIG
))
613 pgsignal(tp
->t_pgrp
, SIGINFO
, 1);
614 if (!ISSET(lflag
, NOKERNINFO
))
620 * Check for input buffer overflow
622 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
624 if (ISSET(iflag
, IMAXBEL
)) {
625 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
626 (void)ttyoutput(CTRL('g'), tp
);
631 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
632 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
633 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
636 * Put data char in q for user and
637 * wakeup on seeing a line delimiter.
639 if (putc(c
, &tp
->t_rawq
) >= 0) {
640 if (!ISSET(lflag
, ICANON
)) {
645 if (TTBREAKC(c
, lflag
)) {
647 catq(&tp
->t_rawq
, &tp
->t_canq
);
649 } else if (tp
->t_rocount
++ == 0)
650 tp
->t_rocol
= tp
->t_column
;
651 if (ISSET(tp
->t_state
, TS_ERASE
)) {
653 * end of prterase \.../
655 CLR(tp
->t_state
, TS_ERASE
);
656 (void)ttyoutput('/', tp
);
660 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
662 * Place the cursor over the '^' of the ^D.
664 i
= min(2, tp
->t_column
- i
);
666 (void)ttyoutput('\b', tp
);
673 * IXANY means allow any character to restart output.
675 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
676 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
677 thread_funnel_set(kernel_flock
, funnel_state
);
681 CLR(tp
->t_lflag
, FLUSHO
);
682 CLR(tp
->t_state
, TS_TTSTOP
);
684 retval
= ttstart(tp
);
685 thread_funnel_set(kernel_flock
, funnel_state
);
690 * Output a single character on a tty, doing output processing
691 * as needed (expanding tabs, newline processing, etc.).
692 * Returns < 0 if succeeds, otherwise returns char to resend.
698 register struct tty
*tp
;
700 register tcflag_t oflag
;
704 if (!ISSET(oflag
, OPOST
)) {
705 if (ISSET(tp
->t_lflag
, FLUSHO
))
707 if (putc(c
, &tp
->t_outq
))
714 * Do tab expansion if OXTABS is set. Special case if we external
715 * processing, we don't do the tab expansion because we'll probably
716 * get it wrong. If tab expansion needs to be done, let it happen
719 CLR(c
, ~TTY_CHARMASK
);
721 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
722 c
= 8 - (tp
->t_column
& 7);
723 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
724 s
= spltty(); /* Don't interrupt tabs. */
725 c
-= b_to_q(" ", c
, &tp
->t_outq
);
731 return (c
? -1 : '\t');
733 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
737 * Newline translation: if ONLCR is set,
738 * translate newline into "\r\n".
740 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
743 if (putc('\r', &tp
->t_outq
))
748 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
767 col
= (col
+ 8) & ~7;
775 * Ioctls for all tty devices. Called after line-discipline specific ioctl
776 * has been called to do discipline-specific functions and/or reject any
777 * of these ioctl commands.
782 ttioctl(tp
, cmd
, data
, flag
)
783 register struct tty
*tp
;
787 ttioctl(tp
, cmd
, data
, flag
, p
)
788 register struct tty
*tp
;
796 register struct proc
*p
= curproc
; /* XXX */
801 ut
= (struct uthread
*)get_bsdthread_info(current_act());
802 /* If the ioctl involves modification, hang if in the background. */
815 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
825 while (isbackground(p
, tp
) &&
826 (p
->p_flag
& P_PPWAIT
) == 0 &&
827 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
828 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
829 if (p
->p_pgrp
->pg_jobc
== 0)
831 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
832 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
| PTTYBLOCK
, "ttybg1",
840 switch (cmd
) { /* Process the ioctl. */
841 case FIOASYNC
: /* set/clear async i/o */
844 SET(tp
->t_state
, TS_ASYNC
);
846 CLR(tp
->t_state
, TS_ASYNC
);
849 case FIONBIO
: /* set/clear non-blocking i/o */
850 break; /* XXX: delete. */
851 case FIONREAD
: /* get # bytes to read */
853 *(int *)data
= ttnread(tp
);
856 case TIOCEXCL
: /* set exclusive use of tty */
858 SET(tp
->t_state
, TS_XCLUDE
);
861 case TIOCFLUSH
: { /* flush buffers */
862 register int flags
= *(int *)data
;
865 flags
= FREAD
| FWRITE
;
867 flags
&= FREAD
| FWRITE
;
873 /* Set current console device to this line */
875 data
= (caddr_t
) &bogusData
;
877 /* No break - Fall through to BSD code */
880 case TIOCCONS
: { /* become virtual console */
882 if (constty
&& constty
!= tp
&&
883 ISSET(constty
->t_state
, TS_CONNECTED
)) {
886 #if defined(NeXT) || !defined(UCONSOLE)
887 if ( (error
= suser(p
->p_ucred
, &p
->p_acflag
)) )
891 } else if (tp
== constty
) {
896 (*cdevsw
[major(cons
.t_dev
)].d_ioctl
)
897 (cons
.t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
899 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
900 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
905 case TIOCDRAIN
: /* wait till output drained */
910 case TIOCGETA
: { /* get termios struct */
911 struct termios
*t
= (struct termios
*)data
;
913 bcopy(&tp
->t_termios
, t
, sizeof(struct termios
));
916 case TIOCGETD
: /* get line discipline */
917 *(int *)data
= tp
->t_line
;
919 case TIOCGWINSZ
: /* get window size */
920 *(struct winsize
*)data
= tp
->t_winsize
;
922 case TIOCGPGRP
: /* get pgrp of tty */
925 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
928 case TIOCHPCL
: /* hang up on last close */
930 SET(tp
->t_cflag
, HUPCL
);
934 case TIOCNXCL
: /* reset exclusive use of tty */
936 CLR(tp
->t_state
, TS_XCLUDE
);
939 case TIOCOUTQ
: /* output queue size */
940 *(int *)data
= tp
->t_outq
.c_cc
;
942 case TIOCSETA
: /* set termios struct */
943 case TIOCSETAW
: /* drain output, set */
944 case TIOCSETAF
: { /* drn out, fls in, set */
945 register struct termios
*t
= (struct termios
*)data
;
947 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0)
950 if (cmd
== TIOCSETAW
|| cmd
== TIOCSETAF
) {
956 if (cmd
== TIOCSETAF
)
959 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
961 * Set device hardware.
963 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
967 if (ISSET(t
->c_cflag
, CLOCAL
) &&
968 !ISSET(tp
->t_cflag
, CLOCAL
)) {
970 * XXX disconnections would be too hard to
971 * get rid of without this kludge. The only
972 * way to get rid of controlling terminals
973 * is to exit from the session leader.
975 CLR(tp
->t_state
, TS_ZOMBIE
);
977 wakeup(TSA_CARR_ON(tp
));
981 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
982 ISSET(t
->c_cflag
, CLOCAL
)) &&
983 !ISSET(tp
->t_state
, TS_ZOMBIE
))
984 SET(tp
->t_state
, TS_CONNECTED
);
986 CLR(tp
->t_state
, TS_CONNECTED
);
987 tp
->t_cflag
= t
->c_cflag
;
988 tp
->t_ispeed
= t
->c_ispeed
;
989 tp
->t_ospeed
= t
->c_ospeed
;
992 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
994 if (ISSET(t
->c_lflag
, ICANON
))
995 SET(tp
->t_lflag
, PENDIN
);
998 * XXX we really shouldn't allow toggling
999 * ICANON while we're in a non-termios line
1000 * discipline. Now we have to worry about
1001 * panicing for a null queue.
1004 if (tp
->t_canq
.c_cbreserved
> 0 &&
1005 tp
->t_rawq
.c_cbreserved
> 0) {
1006 catq(&tp
->t_rawq
, &tp
->t_canq
);
1008 * XXX the queue limits may be
1009 * different, so the old queue
1010 * swapping method no longer works.
1012 catq(&tp
->t_canq
, &tp
->t_rawq
);
1015 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1018 catq(&tp
->t_rawq
, &tp
->t_canq
);
1020 tp
->t_rawq
= tp
->t_canq
;
1024 CLR(tp
->t_lflag
, PENDIN
);
1028 tp
->t_iflag
= t
->c_iflag
;
1029 tp
->t_oflag
= t
->c_oflag
;
1031 * Make the EXTPROC bit read only.
1033 if (ISSET(tp
->t_lflag
, EXTPROC
))
1034 SET(t
->c_lflag
, EXTPROC
);
1036 CLR(t
->c_lflag
, EXTPROC
);
1037 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1038 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1039 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1041 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1045 case TIOCSETD
: { /* set line discipline */
1046 register int t
= *(int *)data
;
1047 dev_t device
= tp
->t_dev
;
1050 if ((u_int
)t
>= nlinesw
)
1052 if (t
!= tp
->t_line
) {
1054 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1055 error
= (*linesw
[t
].l_open
)(device
, tp
);
1057 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1066 case TIOCSTART
: /* start output, like ^Q */
1068 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1069 ISSET(tp
->t_lflag
, FLUSHO
)) {
1070 CLR(tp
->t_lflag
, FLUSHO
);
1071 CLR(tp
->t_state
, TS_TTSTOP
);
1076 case TIOCSTI
: /* simulate terminal input */
1077 if (p
->p_ucred
->cr_uid
&& (flag
& FREAD
) == 0)
1079 if (p
->p_ucred
->cr_uid
&& !isctty(p
, tp
))
1082 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1085 case TIOCSTOP
: /* stop output, like ^S */
1087 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1088 SET(tp
->t_state
, TS_TTSTOP
);
1093 case TIOCSCTTY
: /* become controlling tty */
1094 /* Session ctty vnode pointer set in vnode layer. */
1095 if (!SESS_LEADER(p
) ||
1096 ((p
->p_session
->s_ttyvp
|| tp
->t_session
) &&
1097 (tp
->t_session
!= p
->p_session
)))
1099 tp
->t_session
= p
->p_session
;
1100 tp
->t_pgrp
= p
->p_pgrp
;
1101 p
->p_session
->s_ttyp
= tp
;
1102 p
->p_flag
|= P_CONTROLT
;
1103 /* The backgrounded process blocking on tty now
1104 * could be foregound process. Wake such processes
1106 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1108 case TIOCSPGRP
: { /* set pgrp of tty */
1109 register struct pgrp
*pgrp
= pgfind(*(int *)data
);
1113 else if (pgrp
== NULL
|| pgrp
->pg_session
!= p
->p_session
)
1116 /* The backgrounded process blocking on tty now
1117 * could be foregound process. Wake such processes
1119 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1122 case TIOCSTAT
: /* simulate control-T */
1127 case TIOCSWINSZ
: /* set window size */
1128 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1129 sizeof (struct winsize
))) {
1130 tp
->t_winsize
= *(struct winsize
*)data
;
1131 pgsignal(tp
->t_pgrp
, SIGWINCH
, 1);
1134 case TIOCSDRAINWAIT
:
1135 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1138 tp
->t_timeout
= *(int *)data
* hz
;
1139 wakeup(TSA_OCOMPLETE(tp
));
1140 wakeup(TSA_OLOWAT(tp
));
1142 case TIOCGDRAINWAIT
:
1143 *(int *)data
= tp
->t_timeout
/ hz
;
1146 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1148 return (ttcompat(tp
, cmd
, data
, flag
, p
));
1150 return (ttcompat(tp
, cmd
, data
, flag
));
1161 ttyselect(tp
, rw
, wql
, p
)
1175 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
))
1177 selrecord(p
, &tp
->t_rsel
, wql
);
1180 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1181 ISSET(tp
->t_state
, TS_CONNECTED
))
1182 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1186 selrecord(p
, &tp
->t_wsel
, wql
);
1194 * This is a wrapper for compatibility with the select vector used by
1195 * cdevsw. It relies on a proper xxxdevtotty routine.
1198 ttselect(dev
, rw
, wql
, p
)
1205 return ttyselect((*cdevsw
[major(dev
)]->d_devtotty
)(dev
), rw
, wql
, p
);
1207 return ttyselect(cdevsw
[major(dev
)].d_ttys
[minor(dev
)], rw
, wql
, p
);
1212 * Must be called at spltty().
1220 if (ISSET(tp
->t_lflag
, PENDIN
))
1222 nread
= tp
->t_canq
.c_cc
;
1223 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1224 nread
+= tp
->t_rawq
.c_cc
;
1225 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1232 * Wait for output to drain.
1236 register struct tty
*tp
;
1242 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1243 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1245 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1246 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1247 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1248 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1249 TTOPRI
| PCATCH
, "ttywai",
1252 if (error
== EWOULDBLOCK
)
1259 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1270 #ifdef sun4c /* XXX */
1271 (*tp
->t_stop
)(tp
, rw
);
1273 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1275 (*cdevsw
[major(tp
->t_dev
)]->d_stop
)(tp
, rw
);
1280 * Flush if successfully wait.
1288 if ((error
= ttywait(tp
)) == 0)
1289 ttyflush(tp
, FREAD
);
1294 * Flush tty read and/or write queues, notifying anyone waiting.
1298 register struct tty
*tp
;
1308 FLUSHQ(&tp
->t_outq
);
1309 CLR(tp
->t_state
, TS_TTSTOP
);
1313 FLUSHQ(&tp
->t_canq
);
1314 FLUSHQ(&tp
->t_rawq
);
1315 CLR(tp
->t_lflag
, PENDIN
);
1318 CLR(tp
->t_state
, TS_LOCAL
);
1320 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1322 FLUSHQ(&tp
->t_outq
);
1326 * Don't let leave any state that might clobber the
1327 * next line discipline (although we should do more
1328 * to send the START char). Not clearing the state
1329 * may have caused the "putc to a clist with no
1330 * reserved cblocks" panic/printf.
1332 CLR(tp
->t_state
, TS_TBLOCK
);
1334 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1335 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1337 * XXX wait a bit in the hope that the stop
1338 * character (if any) will go out. Waiting
1339 * isn't good since it allows races. This
1340 * will be fixed when the stop character is
1341 * put in a special queue. Don't bother with
1342 * the checks in ttywait() since the timeout
1345 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1346 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1349 * Don't try sending the stop character again.
1351 CLR(tp
->t_state
, TS_TBLOCK
);
1358 FLUSHQ(&tp
->t_outq
);
1365 * Copy in the default termios characters.
1372 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1383 termioschars(&tp
->t_termios
);
1387 * Handle input high water. Send stop character for the IXOFF case. Turn
1388 * on our input flow control bit and propagate the changes to the driver.
1389 * XXX the stop character should be put in a special high priority queue.
1396 SET(tp
->t_state
, TS_TBLOCK
);
1397 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1398 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1399 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1404 * Handle input low water. Send start character for the IXOFF case. Turn
1405 * off our input flow control bit and propagate the changes to the driver.
1406 * XXX the start character should be put in a special high priority queue.
1413 CLR(tp
->t_state
, TS_TBLOCK
);
1414 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1415 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1416 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1420 #if defined(NeXT) || defined(notyet)
1421 /* FreeBSD: Not used by any current (i386) drivers. */
1423 * Restart after an inter-char delay.
1439 CLR(tp
->t_state
, TS_TIMEOUT
);
1444 #endif /* NeXT || notyet */
1450 boolean_t funnel_state
;
1452 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1454 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1456 thread_funnel_set(kernel_flock
, funnel_state
);
1461 * "close" a line discipline
1468 boolean_t funnel_state
;
1470 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1471 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1472 ttyflush(tp
, FREAD
| FWRITE
);
1473 thread_funnel_set(kernel_flock
, funnel_state
);
1478 * Handle modem control transition on a tty.
1479 * Flag indicates new state of carrier.
1480 * Returns 0 if the line should be turned off, otherwise 1.
1484 register struct tty
*tp
;
1487 boolean_t funnel_state
;
1489 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1491 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1493 * MDMBUF: do flow control according to carrier flag
1494 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1495 * works if IXON and IXANY are clear.
1498 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1499 CLR(tp
->t_state
, TS_TTSTOP
);
1501 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1502 SET(tp
->t_state
, TS_CAR_OFLOW
);
1503 SET(tp
->t_state
, TS_TTSTOP
);
1506 } else if (flag
== 0) {
1510 CLR(tp
->t_state
, TS_CARR_ON
);
1511 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1512 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1513 SET(tp
->t_state
, TS_ZOMBIE
);
1514 CLR(tp
->t_state
, TS_CONNECTED
);
1515 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1516 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1517 ttyflush(tp
, FREAD
| FWRITE
);
1518 thread_funnel_set(kernel_flock
, funnel_state
);
1525 SET(tp
->t_state
, TS_CARR_ON
);
1526 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1527 SET(tp
->t_state
, TS_CONNECTED
);
1528 wakeup(TSA_CARR_ON(tp
));
1532 thread_funnel_set(kernel_flock
, funnel_state
);
1537 * Reinput pending characters after state switch
1542 register struct tty
*tp
;
1547 CLR(tp
->t_lflag
, PENDIN
);
1548 SET(tp
->t_state
, TS_TYPEN
);
1551 * XXX this assumes too much about clist internals. It may even
1552 * fail if the cblock slush pool is empty. We can't allocate more
1553 * cblocks here because we are called from an interrupt handler
1554 * and clist_alloc_cblocks() can wait.
1557 bzero(&tp
->t_rawq
, sizeof tp
->t_rawq
);
1558 tp
->t_rawq
.c_cbmax
= tq
.c_cbmax
;
1559 tp
->t_rawq
.c_cbreserved
= tq
.c_cbreserved
;
1562 tp
->t_rawq
.c_cc
= 0;
1563 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
1565 while ((c
= getc(&tq
)) >= 0)
1567 CLR(tp
->t_state
, TS_TYPEN
);
1571 * Process a read call on a tty device.
1574 ttread(tp
, uio
, flag
)
1575 register struct tty
*tp
;
1579 register struct clist
*qp
;
1581 register tcflag_t lflag
;
1582 register cc_t
*cc
= tp
->t_cc
;
1583 register struct proc
*p
= current_proc();
1584 int s
, first
, error
= 0;
1585 int has_etime
= 0, last_cc
= 0;
1586 long slp
= 0; /* XXX this should be renamed `timo'. */
1587 boolean_t funnel_state
;
1590 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1592 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1596 lflag
= tp
->t_lflag
;
1598 * take pending input first
1600 if (ISSET(lflag
, PENDIN
)) {
1602 splx(s
); /* reduce latency */
1604 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1608 * Hang process if it's in the background.
1610 if (isbackground(p
, tp
)) {
1612 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1613 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1614 p
->p_flag
& P_PPWAIT
|| p
->p_pgrp
->pg_jobc
== 0) {
1615 thread_funnel_set(kernel_flock
, funnel_state
);
1618 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
1619 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg2", 0);
1621 thread_funnel_set(kernel_flock
, funnel_state
);
1627 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1629 thread_funnel_set(kernel_flock
, funnel_state
);
1630 return (0); /* EOF */
1634 * If canonical, use the canonical queue,
1635 * else use the raw queue.
1637 * (should get rid of clists...)
1639 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
1641 if (flag
& IO_NDELAY
) {
1644 if (!ISSET(lflag
, ICANON
) && cc
[VMIN
] == 0) {
1646 thread_funnel_set(kernel_flock
, funnel_state
);
1650 thread_funnel_set(kernel_flock
, funnel_state
);
1651 return (EWOULDBLOCK
);
1653 if (!ISSET(lflag
, ICANON
)) {
1656 struct timeval etime
, timecopy
;
1660 * Check each of the four combinations.
1661 * (m > 0 && t == 0) is the normal read case.
1662 * It should be fairly efficient, so we check that and its
1663 * companion case (m == 0 && t == 0) first.
1664 * For the other two cases, we compute the target sleep time
1673 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1675 thread_funnel_set(kernel_flock
, funnel_state
);
1678 t
*= 100000; /* time in us */
1679 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1680 ((t1).tv_usec - (t2).tv_usec))
1690 /* first character, start timer */
1693 etime
.tv_sec
= t
/ 1000000;
1694 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1695 timeradd(&etime
, &timecopy
, &etime
);
1698 } else if (qp
->c_cc
> last_cc
) {
1699 /* got a character, restart timer */
1701 etime
.tv_sec
= t
/ 1000000;
1702 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1703 timeradd(&etime
, &timecopy
, &etime
);
1707 /* nothing, check expiration */
1708 if (timercmp(&etime
, &timecopy
, <=))
1711 slp
= diff(etime
, timecopy
);
1714 } else { /* m == 0 */
1723 etime
.tv_sec
= t
/ 1000000;
1724 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1725 timeradd(&etime
, &timecopy
, &etime
);
1729 if (timercmp(&etime
, &timecopy
, <=)) {
1730 /* Timed out, but 0 is enough input. */
1732 thread_funnel_set(kernel_flock
, funnel_state
);
1735 slp
= diff(etime
, timecopy
);
1740 * Rounding down may make us wake up just short
1741 * of the target, so we round up.
1742 * The formula is ceiling(slp * hz/1000000).
1743 * 32-bit arithmetic is enough for hz < 169.
1744 * XXX see hzto() for how to avoid overflow if hz
1745 * is large (divide by `tick' and/or arrange to
1746 * use hzto() if hz is large).
1748 slp
= (long) (((u_long
)slp
* hz
) + 999999) / 1000000;
1751 if (qp
->c_cc
<= 0) {
1754 * There is no input, or not enough input and we can block.
1756 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
1757 ISSET(tp
->t_state
, TS_CONNECTED
) ?
1758 "ttyin" : "ttyhup", (int)slp
);
1760 if (error
== EWOULDBLOCK
)
1763 thread_funnel_set(kernel_flock
, funnel_state
);
1767 * XXX what happens if another process eats some input
1768 * while we are asleep (not just here)? It would be
1769 * safest to detect changes and reset our state variables
1770 * (has_stime and last_cc).
1778 * Input present, check for input mapping and processing.
1782 if (ISSET(lflag
, ICANON
)
1783 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
1785 if (ISSET(lflag
, ICANON
| ISIG
))
1792 icc
= min(uio
->uio_resid
, IBUFSIZ
);
1793 icc
= q_to_b(qp
, ibuf
, icc
);
1799 error
= uiomove(ibuf
, icc
, uio
);
1801 * XXX if there was an error then we should ungetc() the
1802 * unmoved chars and reduce icc here.
1805 if (ISSET(tp
->t_lflag
, ECHO
) &&
1806 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1807 snpin((struct snoop
*)tp
->t_sc
, ibuf
, icc
);
1811 if (uio
->uio_resid
== 0)
1825 * delayed suspend (^Y)
1827 if (CCEQ(cc
[VDSUSP
], c
) &&
1828 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
1829 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
1831 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
,
1840 * Interpret EOF only in canonical mode.
1842 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
1845 * Give user character.
1847 error
= ureadc(c
, uio
);
1849 /* XXX should ungetc(c, qp). */
1853 * Only snoop directly on input in echo mode. Non-echoed
1854 * input will be snooped later iff the application echoes it.
1856 if (ISSET(tp
->t_lflag
, ECHO
) &&
1857 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1858 snpinc((struct snoop
*)tp
->t_sc
, (char)c
);
1860 if (uio
->uio_resid
== 0)
1863 * In canonical mode check for a "break character"
1864 * marking the end of a "line of input".
1866 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
1873 * Look to unblock input now that (presumably)
1874 * the input queue has gone down.
1877 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
1878 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
1882 thread_funnel_set(kernel_flock
, funnel_state
);
1887 * Check the output queue on tp for space for a kernel message (from uprintf
1888 * or tprintf). Allow some space over the normal hiwater mark so we don't
1889 * lose messages due to normal flow control, but don't let the tty run amok.
1890 * Sleeps here are not interruptible, but we return prematurely if new signals
1894 ttycheckoutq(tp
, wait
)
1895 register struct tty
*tp
;
1898 int hiwat
, s
, oldsig
;
1901 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1903 hiwat
= tp
->t_hiwat
;
1905 oldsig
= wait
? ut
->uu_siglist
: 0;
1906 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
1907 while (tp
->t_outq
.c_cc
> hiwat
) {
1909 if (tp
->t_outq
.c_cc
<= hiwat
)
1911 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
1915 SET(tp
->t_state
, TS_SO_OLOWAT
);
1916 tsleep(TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
1923 * Process a write call on a tty device.
1926 ttwrite(tp
, uio
, flag
)
1927 register struct tty
*tp
;
1928 register struct uio
*uio
;
1931 register char *cp
= NULL
;
1932 register int cc
, ce
;
1933 register struct proc
*p
;
1934 int i
, hiwat
, cnt
, error
, s
;
1936 boolean_t funnel_state
;
1939 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1941 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1942 hiwat
= tp
->t_hiwat
;
1943 cnt
= uio
->uio_resid
;
1948 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1950 if (uio
->uio_resid
== cnt
)
1954 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
1955 if (flag
& IO_NDELAY
) {
1957 error
= EWOULDBLOCK
;
1960 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
1969 * Hang the process if it's in the background.
1972 if (isbackground(p
, tp
) &&
1973 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_flag
& P_PPWAIT
) == 0 &&
1974 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1975 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1976 if (p
->p_pgrp
->pg_jobc
== 0) {
1980 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
1981 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg4", 0);
1987 * Process the user's data in at most OBUFSIZ chunks. Perform any
1988 * output translation. Keep track of high water mark, sleep on
1989 * overflow awaiting device aid in acquiring new space.
1991 while (uio
->uio_resid
> 0 || cc
> 0) {
1992 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
1994 thread_funnel_set(kernel_flock
, funnel_state
);
1997 if (tp
->t_outq
.c_cc
> hiwat
)
2000 * Grab a hunk of data from the user, unless we have some
2001 * leftover from last time.
2004 cc
= min(uio
->uio_resid
, OBUFSIZ
);
2006 error
= uiomove(cp
, cc
, uio
);
2012 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
2013 snpin((struct snoop
*)tp
->t_sc
, cp
, cc
);
2017 * If nothing fancy need be done, grab those characters we
2018 * can handle without any of ttyoutput's processing and
2019 * just transfer them to the output q. For those chars
2020 * which require special processing (as indicated by the
2021 * bits in char_type), call ttyoutput. After processing
2022 * a hunk of data, look for FLUSHO so ^O's will take effect
2026 if (!ISSET(tp
->t_oflag
, OPOST
))
2029 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2030 (u_char
*)char_type
, CCLASSMASK
);
2032 * If ce is zero, then we're processing
2033 * a special character through ttyoutput.
2037 if (ttyoutput(*cp
, tp
) >= 0) {
2042 /* No Clists, wait a bit. */
2044 if (flag
& IO_NDELAY
) {
2045 error
= EWOULDBLOCK
;
2048 error
= ttysleep(tp
, &lbolt
,
2058 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2059 tp
->t_outq
.c_cc
> hiwat
)
2065 * A bunch of normal characters have been found.
2066 * Transfer them en masse to the output queue and
2067 * continue processing at the top of the loop.
2068 * If there are any further characters in this
2069 * <= OBUFSIZ chunk, the first should be a character
2070 * requiring special handling by ttyoutput.
2073 i
= b_to_q(cp
, ce
, &tp
->t_outq
);
2076 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2083 /* No Clists, wait a bit. */
2085 if (flag
& IO_NDELAY
) {
2086 error
= EWOULDBLOCK
;
2089 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
,
2096 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2097 tp
->t_outq
.c_cc
> hiwat
)
2104 * If cc is nonzero, we leave the uio structure inconsistent, as the
2105 * offset and iov pointers have moved forward, but it doesn't matter
2106 * (the call will either return short or restart with a new uio).
2108 uio
->uio_resid
+= cc
;
2109 thread_funnel_set(kernel_flock
, funnel_state
);
2116 * Since we are using ring buffers, if we can't insert any more into
2117 * the output queue, we can assume the ring is full and that someone
2118 * forgot to set the high water mark correctly. We set it and then
2119 * proceed as normal.
2121 hiwat
= tp
->t_outq
.c_cc
- 1;
2128 * This can only occur if FLUSHO is set in t_lflag,
2129 * or if ttstart/oproc is synchronous (or very fast).
2131 if (tp
->t_outq
.c_cc
<= hiwat
) {
2135 if (flag
& IO_NDELAY
) {
2137 uio
->uio_resid
+= cc
;
2138 thread_funnel_set(kernel_flock
, funnel_state
);
2139 return (uio
->uio_resid
== cnt
? EWOULDBLOCK
: 0);
2141 SET(tp
->t_state
, TS_SO_OLOWAT
);
2142 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2145 if (error
== EWOULDBLOCK
)
2153 * Rubout one character from the rawq of tp
2154 * as cleanly as possible.
2159 register struct tty
*tp
;
2161 register u_char
*cp
;
2162 register int savecol
;
2165 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2167 CLR(tp
->t_lflag
, FLUSHO
);
2168 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2169 if (tp
->t_rocount
== 0) {
2171 * Messed up by ttwrite; retype
2176 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2179 CLR(c
, ~TTY_CHARMASK
);
2180 switch (CCLASS(c
)) {
2189 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2193 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2198 savecol
= tp
->t_column
;
2199 SET(tp
->t_state
, TS_CNTTB
);
2200 SET(tp
->t_lflag
, FLUSHO
);
2201 tp
->t_column
= tp
->t_rocol
;
2203 cp
= tp
->t_rawq
.c_cf
;
2205 tabc
= *cp
; /* XXX FIX NEXTC */
2206 for (; cp
; cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2209 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2210 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2213 CLR(tp
->t_lflag
, FLUSHO
);
2214 CLR(tp
->t_state
, TS_CNTTB
);
2217 /* savecol will now be length of the tab. */
2218 savecol
-= tp
->t_column
;
2219 tp
->t_column
+= savecol
;
2221 savecol
= 8; /* overflow fixup */
2222 while (--savecol
>= 0)
2223 (void)ttyoutput('\b', tp
);
2226 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2227 (void)printf(PANICSTR
, c
, CCLASS(c
));
2229 panic(PANICSTR
, c
, CCLASS(c
));
2233 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2234 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2235 SET(tp
->t_state
, TS_ERASE
);
2236 (void)ttyoutput('\\', tp
);
2240 ttyecho(tp
->t_cc
[VERASE
], tp
);
2245 * Back over cnt characters, erasing them.
2249 register struct tty
*tp
;
2254 (void)ttyoutput('\b', tp
);
2255 (void)ttyoutput(' ', tp
);
2256 (void)ttyoutput('\b', tp
);
2262 * Reprint the rawq line. Note, it is assumed that c_cc has already
2267 register struct tty
*tp
;
2269 register u_char
*cp
;
2272 /* Echo the reprint character. */
2273 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2274 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2276 (void)ttyoutput('\n', tp
);
2280 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2281 * BIT OF FIRST CHAR.
2285 for (cp
= tp
->t_canq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2286 cp
!= NULL
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2288 for (cp
= tp
->t_rawq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2289 cp
!= NULL
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2292 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2294 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2297 CLR(tp
->t_state
, TS_ERASE
);
2300 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2305 * Echo a typed character to the terminal.
2310 register struct tty
*tp
;
2313 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2314 CLR(tp
->t_lflag
, FLUSHO
);
2315 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2316 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2317 ISSET(tp
->t_lflag
, EXTPROC
))
2319 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2320 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2321 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2322 (void)ttyoutput('^', tp
);
2323 CLR(c
, ~TTY_CHARMASK
);
2329 (void)ttyoutput(c
, tp
);
2333 * Wake up any readers on a tty.
2337 register struct tty
*tp
;
2341 if (tp
->t_rsel
.si_pid
!= 0)
2343 selwakeup(&tp
->t_rsel
);
2344 if (ISSET(tp
->t_state
, TS_ASYNC
))
2345 pgsignal(tp
->t_pgrp
, SIGIO
, 1);
2346 wakeup(TSA_HUP_OR_INPUT(tp
));
2350 * Wake up any writers on a tty.
2354 register struct tty
*tp
;
2357 if (tp
->t_wsel
.si_pid
!= 0 && tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2359 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2361 selwakeup(&tp
->t_wsel
);
2362 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2363 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2364 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2365 wakeup(TSA_OCOMPLETE(tp
));
2367 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2368 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2369 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2370 wakeup(TSA_OLOWAT(tp
));
2375 * Look up a code for a specified speed in a conversion table;
2376 * used by drivers to map software speed values to hardware parameters.
2379 ttspeedtab(speed
, table
)
2381 register struct speedtab
*table
;
2384 for ( ; table
->sp_speed
!= -1; table
++)
2385 if (table
->sp_speed
== speed
)
2386 return (table
->sp_code
);
2391 * Set tty hi and low water marks.
2393 * Try to arrange the dynamics so there's about one second
2394 * from hi to low water.
2401 register int cps
, x
;
2403 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2405 cps
= tp
->t_ospeed
/ 10;
2406 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2408 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2409 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2413 /* NeXT ttyinfo has been converted to the MACH kernel */
2414 #include <mach/thread_info.h>
2417 * Report on state of foreground process group.
2421 register struct tty
*tp
;
2423 /* NOT IMPLEMENTED FOR MACH */
2428 * Returns 1 if p2 is "better" than p1
2430 * The algorithm for picking the "interesting" process is thus:
2432 * 1) Only foreground processes are eligible - implied.
2433 * 2) Runnable processes are favored over anything else. The runner
2434 * with the highest cpu utilization is picked (p_estcpu). Ties are
2435 * broken by picking the highest pid.
2436 * 3) The sleeper with the shortest sleep time is next. With ties,
2437 * we pick out just "short-term" sleepers (P_SINTR == 0).
2438 * 4) Further ties are broken by picking the highest pid.
2440 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2441 #define TESTAB(a, b) ((a)<<1 | (b))
2447 proc_compare(p1
, p2
)
2448 register struct proc
*p1
, *p2
;
2454 * see if at least one of them is runnable
2456 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2463 * tie - favor one with highest recent cpu utilization
2465 if (p2
->p_estcpu
> p1
->p_estcpu
)
2467 if (p1
->p_estcpu
> p2
->p_estcpu
)
2469 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2474 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2480 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2483 * pick the one with the smallest sleep time
2485 if (p2
->p_slptime
> p1
->p_slptime
)
2487 if (p1
->p_slptime
> p2
->p_slptime
)
2490 * favor one sleeping in a non-interruptible sleep
2492 if (p1
->p_flag
& P_SINTR
&& (p2
->p_flag
& P_SINTR
) == 0)
2494 if (p2
->p_flag
& P_SINTR
&& (p1
->p_flag
& P_SINTR
) == 0)
2496 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2501 * Output char to tty; console putchar style.
2511 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2516 (void)ttyoutput('\r', tp
);
2517 (void)ttyoutput(c
, tp
);
2524 * Sleep on chan, returning ERESTART if tty changed while we napped and
2525 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2526 * the tty is revoked, restarting a pending call will redo validation done
2527 * at the start of the call.
2530 ttysleep(tp
, chan
, pri
, wmesg
, timo
)
2540 error
= tsleep(chan
, pri
, wmesg
, timo
);
2543 return (tp
->t_gen
== gen
? 0 : ERESTART
);
2548 * Allocate a tty structure and its associated buffers.
2555 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
);
2556 bzero(tp
, sizeof *tp
);
2557 /* XXX: default to TTYCLSIZE(1024) chars for now */
2558 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
2559 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
2560 /* output queue doesn't need quoting */
2561 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
2566 * Free a tty structure and its buffers.
2572 clfree(&tp
->t_rawq
);
2573 clfree(&tp
->t_canq
);
2574 clfree(&tp
->t_outq
);
2582 * XXX this is usable not useful or used. Most tty drivers have
2583 * ifdefs for using ttymalloc() but assume a different interface.
2586 * Allocate a tty struct. Clists in the struct will be allocated by
2594 tp
= _MALLOC(sizeof *tp
, M_TTYS
, M_WAITOK
);
2595 bzero(tp
, sizeof *tp
);
2600 #if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */
2602 * Free a tty struct. Clists in the struct should have been freed by