]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1990, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)tty.c 8.8 (Berkeley) 1/21/94
67 * o Fix races for sending the start char in ttyflush().
68 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
69 * With luck, there will be MIN chars before select() returns().
70 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
71 * o Don't allow input in TS_ZOMBIE case. It would be visible through
73 * o Do the new sio locking stuff here and use it to avoid special
76 * o Move EXTPROC and/or PENDIN to t_state?
77 * o Wrap most of ttioctl in spltty/splx.
78 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
79 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
80 * o Don't allow certain termios flags to affect disciplines other
81 * than TTYDISC. Cancel their effects before switch disciplines
82 * and ignore them if they are set while we are in another
84 * o Handle c_ispeed = 0 to c_ispeed = c_ospeed conversion here instead
85 * of in drivers and fix drivers that write to tp->t_termios.
86 * o Check for TS_CARR_ON being set while everything is closed and not
87 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
88 * so it would live until the next open even if carrier drops.
89 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
90 * only when _all_ openers leave open().
96 #include "opt_uconsole.h"
99 #include <sys/param.h>
100 #define TTYDEFCHARS 1
101 #include <sys/systm.h>
103 #include <sys/ioctl.h>
104 #include <sys/proc.h>
105 #include <sys/file.h>
106 #include <sys/conf.h>
107 #include <sys/dkstat.h>
109 #include <sys/kernel.h>
110 #include <sys/vnode.h>
111 #include <sys/syslog.h>
112 #include <sys/user.h>
113 #include <sys/signalvar.h>
114 #include <sys/signalvar.h>
116 #include <sys/resourcevar.h>
118 #include <sys/malloc.h>
120 #include <sys/snoop.h>
125 #include <vm/vm_param.h>
126 #include <vm/vm_prot.h>
129 #include <vm/vm_map.h>
131 #include <dev/kmreg_com.h>
132 #include <machine/cons.h>
133 #include <machine/spl.h>
135 #include <machdep/machine/pmap.h>
140 static int proc_compare
__P((struct proc
*p1
, struct proc
*p2
));
142 static int ttnread
__P((struct tty
*tp
));
143 static void ttyecho
__P((int c
, struct tty
*tp
));
144 static int ttyoutput
__P((int c
, register struct tty
*tp
));
145 static void ttypend
__P((struct tty
*tp
));
146 static void ttyretype
__P((struct tty
*tp
));
147 static void ttyrub
__P((int c
, struct tty
*tp
));
148 static void ttyrubo
__P((struct tty
*tp
, int cnt
));
149 static void ttystop
__P((struct tty
*tp
, int rw
));
150 static void ttyunblock
__P((struct tty
*tp
));
151 static int ttywflush
__P((struct tty
*tp
));
154 * Table with character classes and parity. The 8th bit indicates parity,
155 * the 7th bit indicates the character is an alphameric or underscore (for
156 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
157 * are 0 then the character needs no special processing on output; classes
158 * other than 0 might be translated or (not currently) require delays.
160 #define E 0x00 /* Even parity. */
161 #define O 0x80 /* Odd parity. */
162 #define PARITY(c) (char_type[c] & O)
164 #define ALPHA 0x40 /* Alpha or underscore. */
165 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
167 #define CCLASSMASK 0x3f
168 #define CCLASS(c) (char_type[c] & CCLASSMASK)
173 #define NA ORDINARY | ALPHA
179 static u_char
const char_type
[] = {
180 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* nul - bel */
181 O
|BS
, E
|TB
, E
|NL
, O
|CC
, E
|VT
, O
|CR
, O
|CC
, E
|CC
, /* bs - si */
182 O
|CC
, E
|CC
, E
|CC
, O
|CC
, E
|CC
, O
|CC
, O
|CC
, E
|CC
, /* dle - etb */
183 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* can - us */
184 O
|NO
, E
|NO
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* sp - ' */
185 E
|NO
, O
|NO
, O
|NO
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|NO
, /* ( - / */
186 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* 0 - 7 */
187 O
|NA
, E
|NA
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* 8 - ? */
188 O
|NO
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* @ - G */
189 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* H - O */
190 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* P - W */
191 O
|NA
, E
|NA
, E
|NA
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, O
|NA
, /* X - _ */
192 E
|NO
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* ` - g */
193 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* h - o */
194 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* p - w */
195 E
|NA
, O
|NA
, O
|NA
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|CC
, /* x - del */
197 * Meta chars; should be settable per character set;
198 * for now, treat them all as normal characters.
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
,
213 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
214 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
215 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
226 /* Macros to clear/set/test flags. */
227 #define SET(t, f) (t) |= (f)
228 #define CLR(t, f) (t) &= ~(f)
229 #define ISSET(t, f) ((t) & (f))
232 * Input control starts when we would not be able to fit the maximum
233 * contents of the ping-pong buffers and finishes when we would be able
234 * to fit that much plus 1/8 more.
236 #define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */
237 #define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */
239 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
240 #define MAX_INPUT TTYHOG
243 * Initial open of tty, or (re)entry to standard tty line discipline.
248 register struct tty
*tp
;
251 boolean_t funnel_state
;
253 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
256 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
257 SET(tp
->t_state
, TS_ISOPEN
);
258 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
259 SET(tp
->t_state
, TS_CONNECTED
); }
260 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
265 * Initialize or restore a cblock allocation policy suitable for
266 * the standard line discipline.
268 clist_alloc_cblocks(&tp
->t_canq
, TTYHOG
, 512);
269 clist_alloc_cblocks(&tp
->t_outq
, TTMAXHIWAT
+ OBUFSIZ
+ 100,
270 TTMAXHIWAT
+ OBUFSIZ
+ 100);
271 clist_alloc_cblocks(&tp
->t_rawq
, TTYHOG
, TTYHOG
);
275 thread_funnel_set(kernel_flock
, funnel_state
);
280 * Handle close() on a tty line: flush and set to initial state,
281 * bumping generation number so that pending read/write calls
282 * can detect recycling of the tty.
283 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
284 * and l_close() should have flushed, but we repeat the spltty() and
285 * the flush in case there are buggy callers.
289 register struct tty
*tp
;
302 * Closing current console tty; disable printing of console
303 * messages at bottom-level driver.
305 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
306 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
310 ttyflush(tp
, FREAD
| FWRITE
);
312 clist_free_cblocks(&tp
->t_canq
);
313 clist_free_cblocks(&tp
->t_outq
);
314 clist_free_cblocks(&tp
->t_rawq
);
318 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
319 snpdown((struct snoop
*)tp
->t_sc
);
323 tp
->t_line
= TTYDISC
;
325 tp
->t_session
= NULL
;
328 selthreadclear(&tp
->t_wsel
);
329 selthreadclear(&tp
->t_rsel
);
335 #define FLUSHQ(q) { \
337 ndflush(q, (q)->c_cc); \
340 /* Is 'c' a line delimiter ("break" character)? */
341 #define TTBREAKC(c, lflag) \
342 ((c) == '\n' || (((c) == cc[VEOF] || \
343 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
344 (c) != _POSIX_VDISABLE))
347 * Process input of a single character received on a tty.
352 register struct tty
*tp
;
354 register tcflag_t iflag
, lflag
;
357 boolean_t funnel_state
;
359 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
362 * If input is pending take it first.
365 if (ISSET(lflag
, PENDIN
))
370 if (ISSET(lflag
, ICANON
)) {
380 * Block further input iff:
381 * current input > threshold AND input is available to user program
382 * AND input flow control is enabled and not yet invoked.
383 * The 3 is slop for PARMRK.
386 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
387 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
388 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
389 !ISSET(tp
->t_state
, TS_TBLOCK
))
392 /* Handle exceptional conditions (break, parity, framing). */
394 err
= (ISSET(c
, TTY_ERRORMASK
));
396 CLR(c
, TTY_ERRORMASK
);
397 if (ISSET(err
, TTY_BI
)) {
398 if (ISSET(iflag
, IGNBRK
)) {
399 thread_funnel_set(kernel_flock
, funnel_state
);
402 if (ISSET(iflag
, BRKINT
)) {
403 ttyflush(tp
, FREAD
| FWRITE
);
404 pgsignal(tp
->t_pgrp
, SIGINT
, 1);
407 if (ISSET(iflag
, PARMRK
))
409 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
410 || ISSET(err
, TTY_FE
)) {
411 if (ISSET(iflag
, IGNPAR
)) {
412 thread_funnel_set(kernel_flock
, funnel_state
);
415 else if (ISSET(iflag
, PARMRK
)) {
417 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
420 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
421 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
422 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
429 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
431 if (!ISSET(lflag
, EXTPROC
)) {
433 * Check for literal nexting very first
435 if (ISSET(tp
->t_state
, TS_LNCH
)) {
437 CLR(tp
->t_state
, TS_LNCH
);
440 * Scan for special characters. This code
441 * is really just a big case statement with
442 * non-constant cases. The bottom of the
443 * case statement is labeled ``endcase'', so goto
444 * it after a case match, or similar.
448 * Control chars which aren't controlled
449 * by ICANON, ISIG, or IXON.
451 if (ISSET(lflag
, IEXTEN
)) {
452 if (CCEQ(cc
[VLNEXT
], c
)) {
453 if (ISSET(lflag
, ECHO
)) {
454 if (ISSET(lflag
, ECHOE
)) {
455 (void)ttyoutput('^', tp
);
456 (void)ttyoutput('\b', tp
);
460 SET(tp
->t_state
, TS_LNCH
);
463 if (CCEQ(cc
[VDISCARD
], c
)) {
464 if (ISSET(lflag
, FLUSHO
))
465 CLR(tp
->t_lflag
, FLUSHO
);
467 ttyflush(tp
, FWRITE
);
469 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
471 SET(tp
->t_lflag
, FLUSHO
);
479 if (ISSET(lflag
, ISIG
)) {
480 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
481 if (!ISSET(lflag
, NOFLSH
))
482 ttyflush(tp
, FREAD
| FWRITE
);
485 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
488 if (CCEQ(cc
[VSUSP
], c
)) {
489 if (!ISSET(lflag
, NOFLSH
))
492 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
497 * Handle start/stop characters.
499 if (ISSET(iflag
, IXON
)) {
500 if (CCEQ(cc
[VSTOP
], c
)) {
501 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
502 SET(tp
->t_state
, TS_TTSTOP
);
504 thread_funnel_set(kernel_flock
, funnel_state
);
507 if (!CCEQ(cc
[VSTART
], c
)) {
508 thread_funnel_set(kernel_flock
, funnel_state
);
512 * if VSTART == VSTOP then toggle
516 if (CCEQ(cc
[VSTART
], c
))
520 * IGNCR, ICRNL, & INLCR
523 if (ISSET(iflag
, IGNCR
)) {
524 thread_funnel_set(kernel_flock
, funnel_state
);
527 else if (ISSET(iflag
, ICRNL
))
529 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
532 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
534 * From here on down canonical mode character
535 * processing takes place.
540 if (CCEQ(cc
[VERASE
], c
)) {
542 ttyrub(unputc(&tp
->t_rawq
), tp
);
548 if (CCEQ(cc
[VKILL
], c
)) {
549 if (ISSET(lflag
, ECHOKE
) &&
550 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
551 !ISSET(lflag
, ECHOPRT
))
552 while (tp
->t_rawq
.c_cc
)
553 ttyrub(unputc(&tp
->t_rawq
), tp
);
556 if (ISSET(lflag
, ECHOK
) ||
557 ISSET(lflag
, ECHOKE
))
562 CLR(tp
->t_state
, TS_LOCAL
);
568 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
574 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
579 * erase last char of word and remember the
580 * next chars type (for ALTWERASE)
583 c
= unputc(&tp
->t_rawq
);
586 if (c
== ' ' || c
== '\t') {
587 (void)putc(c
, &tp
->t_rawq
);
596 c
= unputc(&tp
->t_rawq
);
599 } while (c
!= ' ' && c
!= '\t' &&
600 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
601 (void)putc(c
, &tp
->t_rawq
);
607 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
612 * ^T - kernel info and generate SIGINFO
614 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
615 if (ISSET(lflag
, ISIG
))
616 pgsignal(tp
->t_pgrp
, SIGINFO
, 1);
617 if (!ISSET(lflag
, NOKERNINFO
))
623 * Check for input buffer overflow
625 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
627 if (ISSET(iflag
, IMAXBEL
)) {
628 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
629 (void)ttyoutput(CTRL('g'), tp
);
634 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
635 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
636 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
639 * Put data char in q for user and
640 * wakeup on seeing a line delimiter.
642 if (putc(c
, &tp
->t_rawq
) >= 0) {
643 if (!ISSET(lflag
, ICANON
)) {
648 if (TTBREAKC(c
, lflag
)) {
650 catq(&tp
->t_rawq
, &tp
->t_canq
);
652 } else if (tp
->t_rocount
++ == 0)
653 tp
->t_rocol
= tp
->t_column
;
654 if (ISSET(tp
->t_state
, TS_ERASE
)) {
656 * end of prterase \.../
658 CLR(tp
->t_state
, TS_ERASE
);
659 (void)ttyoutput('/', tp
);
663 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
665 * Place the cursor over the '^' of the ^D.
667 i
= min(2, tp
->t_column
- i
);
669 (void)ttyoutput('\b', tp
);
676 * IXANY means allow any character to restart output.
678 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
679 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
680 thread_funnel_set(kernel_flock
, funnel_state
);
684 CLR(tp
->t_lflag
, FLUSHO
);
685 CLR(tp
->t_state
, TS_TTSTOP
);
687 retval
= ttstart(tp
);
688 thread_funnel_set(kernel_flock
, funnel_state
);
693 * Output a single character on a tty, doing output processing
694 * as needed (expanding tabs, newline processing, etc.).
695 * Returns < 0 if succeeds, otherwise returns char to resend.
701 register struct tty
*tp
;
703 register tcflag_t oflag
;
707 if (!ISSET(oflag
, OPOST
)) {
708 if (ISSET(tp
->t_lflag
, FLUSHO
))
710 if (putc(c
, &tp
->t_outq
))
717 * Do tab expansion if OXTABS is set. Special case if we external
718 * processing, we don't do the tab expansion because we'll probably
719 * get it wrong. If tab expansion needs to be done, let it happen
722 CLR(c
, ~TTY_CHARMASK
);
724 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
725 c
= 8 - (tp
->t_column
& 7);
726 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
727 s
= spltty(); /* Don't interrupt tabs. */
728 c
-= b_to_q(" ", c
, &tp
->t_outq
);
734 return (c
? -1 : '\t');
736 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
740 * Newline translation: if ONLCR is set,
741 * translate newline into "\r\n".
743 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
746 if (putc('\r', &tp
->t_outq
))
751 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
770 col
= (col
+ 8) & ~7;
778 * Ioctls for all tty devices. Called after line-discipline specific ioctl
779 * has been called to do discipline-specific functions and/or reject any
780 * of these ioctl commands.
785 ttioctl(tp
, cmd
, data
, flag
)
786 register struct tty
*tp
;
790 ttioctl(tp
, cmd
, data
, flag
, p
)
791 register struct tty
*tp
;
799 register struct proc
*p
= curproc
; /* XXX */
804 ut
= (struct uthread
*)get_bsdthread_info(current_act());
805 /* If the ioctl involves modification, hang if in the background. */
818 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
828 while (isbackground(p
, tp
) &&
829 (p
->p_flag
& P_PPWAIT
) == 0 &&
830 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
831 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
832 if (p
->p_pgrp
->pg_jobc
== 0)
834 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
835 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
| PTTYBLOCK
, "ttybg1",
843 switch (cmd
) { /* Process the ioctl. */
844 case FIOASYNC
: /* set/clear async i/o */
847 SET(tp
->t_state
, TS_ASYNC
);
849 CLR(tp
->t_state
, TS_ASYNC
);
852 case FIONBIO
: /* set/clear non-blocking i/o */
853 break; /* XXX: delete. */
854 case FIONREAD
: /* get # bytes to read */
856 *(int *)data
= ttnread(tp
);
859 case TIOCEXCL
: /* set exclusive use of tty */
861 SET(tp
->t_state
, TS_XCLUDE
);
864 case TIOCFLUSH
: { /* flush buffers */
865 register int flags
= *(int *)data
;
868 flags
= FREAD
| FWRITE
;
870 flags
&= FREAD
| FWRITE
;
876 /* Set current console device to this line */
878 data
= (caddr_t
) &bogusData
;
880 /* No break - Fall through to BSD code */
883 case TIOCCONS
: { /* become virtual console */
885 if (constty
&& constty
!= tp
&&
886 ISSET(constty
->t_state
, TS_CONNECTED
)) {
889 #if defined(NeXT) || !defined(UCONSOLE)
890 if ( (error
= suser(p
->p_ucred
, &p
->p_acflag
)) )
894 } else if (tp
== constty
) {
899 (*cdevsw
[major(cons
.t_dev
)].d_ioctl
)
900 (cons
.t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
902 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
903 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
908 case TIOCDRAIN
: /* wait till output drained */
913 case TIOCGETA
: { /* get termios struct */
914 struct termios
*t
= (struct termios
*)data
;
916 bcopy(&tp
->t_termios
, t
, sizeof(struct termios
));
919 case TIOCGETD
: /* get line discipline */
920 *(int *)data
= tp
->t_line
;
922 case TIOCGWINSZ
: /* get window size */
923 *(struct winsize
*)data
= tp
->t_winsize
;
925 case TIOCGPGRP
: /* get pgrp of tty */
928 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
931 case TIOCHPCL
: /* hang up on last close */
933 SET(tp
->t_cflag
, HUPCL
);
937 case TIOCNXCL
: /* reset exclusive use of tty */
939 CLR(tp
->t_state
, TS_XCLUDE
);
942 case TIOCOUTQ
: /* output queue size */
943 *(int *)data
= tp
->t_outq
.c_cc
;
945 case TIOCSETA
: /* set termios struct */
946 case TIOCSETAW
: /* drain output, set */
947 case TIOCSETAF
: { /* drn out, fls in, set */
948 register struct termios
*t
= (struct termios
*)data
;
950 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0)
953 if (cmd
== TIOCSETAW
|| cmd
== TIOCSETAF
) {
959 if (cmd
== TIOCSETAF
)
962 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
964 * Set device hardware.
966 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
970 if (ISSET(t
->c_cflag
, CLOCAL
) &&
971 !ISSET(tp
->t_cflag
, CLOCAL
)) {
973 * XXX disconnections would be too hard to
974 * get rid of without this kludge. The only
975 * way to get rid of controlling terminals
976 * is to exit from the session leader.
978 CLR(tp
->t_state
, TS_ZOMBIE
);
980 wakeup(TSA_CARR_ON(tp
));
984 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
985 ISSET(t
->c_cflag
, CLOCAL
)) &&
986 !ISSET(tp
->t_state
, TS_ZOMBIE
))
987 SET(tp
->t_state
, TS_CONNECTED
);
989 CLR(tp
->t_state
, TS_CONNECTED
);
990 tp
->t_cflag
= t
->c_cflag
;
991 tp
->t_ispeed
= t
->c_ispeed
;
992 tp
->t_ospeed
= t
->c_ospeed
;
995 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
997 if (ISSET(t
->c_lflag
, ICANON
))
998 SET(tp
->t_lflag
, PENDIN
);
1001 * XXX we really shouldn't allow toggling
1002 * ICANON while we're in a non-termios line
1003 * discipline. Now we have to worry about
1004 * panicing for a null queue.
1007 if (tp
->t_canq
.c_cbreserved
> 0 &&
1008 tp
->t_rawq
.c_cbreserved
> 0) {
1009 catq(&tp
->t_rawq
, &tp
->t_canq
);
1011 * XXX the queue limits may be
1012 * different, so the old queue
1013 * swapping method no longer works.
1015 catq(&tp
->t_canq
, &tp
->t_rawq
);
1018 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1021 catq(&tp
->t_rawq
, &tp
->t_canq
);
1023 tp
->t_rawq
= tp
->t_canq
;
1027 CLR(tp
->t_lflag
, PENDIN
);
1031 tp
->t_iflag
= t
->c_iflag
;
1032 tp
->t_oflag
= t
->c_oflag
;
1034 * Make the EXTPROC bit read only.
1036 if (ISSET(tp
->t_lflag
, EXTPROC
))
1037 SET(t
->c_lflag
, EXTPROC
);
1039 CLR(t
->c_lflag
, EXTPROC
);
1040 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1041 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1042 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1044 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1048 case TIOCSETD
: { /* set line discipline */
1049 register int t
= *(int *)data
;
1050 dev_t device
= tp
->t_dev
;
1053 if ((u_int
)t
>= nlinesw
)
1055 if (t
!= tp
->t_line
) {
1057 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1058 error
= (*linesw
[t
].l_open
)(device
, tp
);
1060 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1069 case TIOCSTART
: /* start output, like ^Q */
1071 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1072 ISSET(tp
->t_lflag
, FLUSHO
)) {
1073 CLR(tp
->t_lflag
, FLUSHO
);
1074 CLR(tp
->t_state
, TS_TTSTOP
);
1079 case TIOCSTI
: /* simulate terminal input */
1080 if (p
->p_ucred
->cr_uid
&& (flag
& FREAD
) == 0)
1082 if (p
->p_ucred
->cr_uid
&& !isctty(p
, tp
))
1085 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1088 case TIOCSTOP
: /* stop output, like ^S */
1090 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1091 SET(tp
->t_state
, TS_TTSTOP
);
1096 case TIOCSCTTY
: /* become controlling tty */
1097 /* Session ctty vnode pointer set in vnode layer. */
1098 if (!SESS_LEADER(p
) ||
1099 ((p
->p_session
->s_ttyvp
|| tp
->t_session
) &&
1100 (tp
->t_session
!= p
->p_session
)))
1102 tp
->t_session
= p
->p_session
;
1103 tp
->t_pgrp
= p
->p_pgrp
;
1104 p
->p_session
->s_ttyp
= tp
;
1105 p
->p_flag
|= P_CONTROLT
;
1106 /* The backgrounded process blocking on tty now
1107 * could be foregound process. Wake such processes
1109 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1111 case TIOCSPGRP
: { /* set pgrp of tty */
1112 register struct pgrp
*pgrp
= pgfind(*(int *)data
);
1116 else if (pgrp
== NULL
|| pgrp
->pg_session
!= p
->p_session
)
1119 /* The backgrounded process blocking on tty now
1120 * could be foregound process. Wake such processes
1122 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1125 case TIOCSTAT
: /* simulate control-T */
1130 case TIOCSWINSZ
: /* set window size */
1131 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1132 sizeof (struct winsize
))) {
1133 tp
->t_winsize
= *(struct winsize
*)data
;
1134 pgsignal(tp
->t_pgrp
, SIGWINCH
, 1);
1137 case TIOCSDRAINWAIT
:
1138 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1141 tp
->t_timeout
= *(int *)data
* hz
;
1142 wakeup(TSA_OCOMPLETE(tp
));
1143 wakeup(TSA_OLOWAT(tp
));
1145 case TIOCGDRAINWAIT
:
1146 *(int *)data
= tp
->t_timeout
/ hz
;
1149 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1151 return (ttcompat(tp
, cmd
, data
, flag
, p
));
1153 return (ttcompat(tp
, cmd
, data
, flag
));
1164 ttyselect(tp
, rw
, wql
, p
)
1178 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
))
1180 selrecord(p
, &tp
->t_rsel
, wql
);
1183 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1184 ISSET(tp
->t_state
, TS_CONNECTED
))
1185 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1189 selrecord(p
, &tp
->t_wsel
, wql
);
1197 * This is a wrapper for compatibility with the select vector used by
1198 * cdevsw. It relies on a proper xxxdevtotty routine.
1201 ttselect(dev
, rw
, wql
, p
)
1208 return ttyselect((*cdevsw
[major(dev
)]->d_devtotty
)(dev
), rw
, wql
, p
);
1210 return ttyselect(cdevsw
[major(dev
)].d_ttys
[minor(dev
)], rw
, wql
, p
);
1215 * Must be called at spltty().
1223 if (ISSET(tp
->t_lflag
, PENDIN
))
1225 nread
= tp
->t_canq
.c_cc
;
1226 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1227 nread
+= tp
->t_rawq
.c_cc
;
1228 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1235 * Wait for output to drain.
1239 register struct tty
*tp
;
1245 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1246 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1248 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1249 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1250 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1251 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1252 TTOPRI
| PCATCH
, "ttywai",
1255 if (error
== EWOULDBLOCK
)
1262 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1273 #ifdef sun4c /* XXX */
1274 (*tp
->t_stop
)(tp
, rw
);
1276 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1278 (*cdevsw
[major(tp
->t_dev
)]->d_stop
)(tp
, rw
);
1283 * Flush if successfully wait.
1291 if ((error
= ttywait(tp
)) == 0)
1292 ttyflush(tp
, FREAD
);
1297 * Flush tty read and/or write queues, notifying anyone waiting.
1301 register struct tty
*tp
;
1311 FLUSHQ(&tp
->t_outq
);
1312 CLR(tp
->t_state
, TS_TTSTOP
);
1316 FLUSHQ(&tp
->t_canq
);
1317 FLUSHQ(&tp
->t_rawq
);
1318 CLR(tp
->t_lflag
, PENDIN
);
1321 CLR(tp
->t_state
, TS_LOCAL
);
1323 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1325 FLUSHQ(&tp
->t_outq
);
1329 * Don't let leave any state that might clobber the
1330 * next line discipline (although we should do more
1331 * to send the START char). Not clearing the state
1332 * may have caused the "putc to a clist with no
1333 * reserved cblocks" panic/printf.
1335 CLR(tp
->t_state
, TS_TBLOCK
);
1337 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1338 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1340 * XXX wait a bit in the hope that the stop
1341 * character (if any) will go out. Waiting
1342 * isn't good since it allows races. This
1343 * will be fixed when the stop character is
1344 * put in a special queue. Don't bother with
1345 * the checks in ttywait() since the timeout
1348 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1349 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1352 * Don't try sending the stop character again.
1354 CLR(tp
->t_state
, TS_TBLOCK
);
1361 FLUSHQ(&tp
->t_outq
);
1368 * Copy in the default termios characters.
1375 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1386 termioschars(&tp
->t_termios
);
1390 * Handle input high water. Send stop character for the IXOFF case. Turn
1391 * on our input flow control bit and propagate the changes to the driver.
1392 * XXX the stop character should be put in a special high priority queue.
1399 SET(tp
->t_state
, TS_TBLOCK
);
1400 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1401 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1402 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1407 * Handle input low water. Send start character for the IXOFF case. Turn
1408 * off our input flow control bit and propagate the changes to the driver.
1409 * XXX the start character should be put in a special high priority queue.
1416 CLR(tp
->t_state
, TS_TBLOCK
);
1417 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1418 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1419 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1423 #if defined(NeXT) || defined(notyet)
1424 /* FreeBSD: Not used by any current (i386) drivers. */
1426 * Restart after an inter-char delay.
1442 CLR(tp
->t_state
, TS_TIMEOUT
);
1447 #endif /* NeXT || notyet */
1453 boolean_t funnel_state
;
1455 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1457 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1459 thread_funnel_set(kernel_flock
, funnel_state
);
1464 * "close" a line discipline
1471 boolean_t funnel_state
;
1473 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1474 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1475 ttyflush(tp
, FREAD
| FWRITE
);
1476 thread_funnel_set(kernel_flock
, funnel_state
);
1481 * Handle modem control transition on a tty.
1482 * Flag indicates new state of carrier.
1483 * Returns 0 if the line should be turned off, otherwise 1.
1487 register struct tty
*tp
;
1490 boolean_t funnel_state
;
1492 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1494 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1496 * MDMBUF: do flow control according to carrier flag
1497 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1498 * works if IXON and IXANY are clear.
1501 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1502 CLR(tp
->t_state
, TS_TTSTOP
);
1504 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1505 SET(tp
->t_state
, TS_CAR_OFLOW
);
1506 SET(tp
->t_state
, TS_TTSTOP
);
1509 } else if (flag
== 0) {
1513 CLR(tp
->t_state
, TS_CARR_ON
);
1514 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1515 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1516 SET(tp
->t_state
, TS_ZOMBIE
);
1517 CLR(tp
->t_state
, TS_CONNECTED
);
1518 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1519 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1520 ttyflush(tp
, FREAD
| FWRITE
);
1521 thread_funnel_set(kernel_flock
, funnel_state
);
1528 SET(tp
->t_state
, TS_CARR_ON
);
1529 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1530 SET(tp
->t_state
, TS_CONNECTED
);
1531 wakeup(TSA_CARR_ON(tp
));
1535 thread_funnel_set(kernel_flock
, funnel_state
);
1540 * Reinput pending characters after state switch
1545 register struct tty
*tp
;
1550 CLR(tp
->t_lflag
, PENDIN
);
1551 SET(tp
->t_state
, TS_TYPEN
);
1554 * XXX this assumes too much about clist internals. It may even
1555 * fail if the cblock slush pool is empty. We can't allocate more
1556 * cblocks here because we are called from an interrupt handler
1557 * and clist_alloc_cblocks() can wait.
1560 bzero(&tp
->t_rawq
, sizeof tp
->t_rawq
);
1561 tp
->t_rawq
.c_cbmax
= tq
.c_cbmax
;
1562 tp
->t_rawq
.c_cbreserved
= tq
.c_cbreserved
;
1565 tp
->t_rawq
.c_cc
= 0;
1566 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
1568 while ((c
= getc(&tq
)) >= 0)
1570 CLR(tp
->t_state
, TS_TYPEN
);
1574 * Process a read call on a tty device.
1577 ttread(tp
, uio
, flag
)
1578 register struct tty
*tp
;
1582 register struct clist
*qp
;
1584 register tcflag_t lflag
;
1585 register cc_t
*cc
= tp
->t_cc
;
1586 register struct proc
*p
= current_proc();
1587 int s
, first
, error
= 0;
1588 int has_etime
= 0, last_cc
= 0;
1589 long slp
= 0; /* XXX this should be renamed `timo'. */
1590 boolean_t funnel_state
;
1593 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1595 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1599 lflag
= tp
->t_lflag
;
1601 * take pending input first
1603 if (ISSET(lflag
, PENDIN
)) {
1605 splx(s
); /* reduce latency */
1607 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1611 * Hang process if it's in the background.
1613 if (isbackground(p
, tp
)) {
1615 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1616 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1617 p
->p_flag
& P_PPWAIT
|| p
->p_pgrp
->pg_jobc
== 0) {
1618 thread_funnel_set(kernel_flock
, funnel_state
);
1621 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
1622 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg2", 0);
1624 thread_funnel_set(kernel_flock
, funnel_state
);
1630 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1632 thread_funnel_set(kernel_flock
, funnel_state
);
1633 return (0); /* EOF */
1637 * If canonical, use the canonical queue,
1638 * else use the raw queue.
1640 * (should get rid of clists...)
1642 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
1644 if (flag
& IO_NDELAY
) {
1647 if (!ISSET(lflag
, ICANON
) && cc
[VMIN
] == 0) {
1649 thread_funnel_set(kernel_flock
, funnel_state
);
1653 thread_funnel_set(kernel_flock
, funnel_state
);
1654 return (EWOULDBLOCK
);
1656 if (!ISSET(lflag
, ICANON
)) {
1659 struct timeval etime
, timecopy
;
1663 * Check each of the four combinations.
1664 * (m > 0 && t == 0) is the normal read case.
1665 * It should be fairly efficient, so we check that and its
1666 * companion case (m == 0 && t == 0) first.
1667 * For the other two cases, we compute the target sleep time
1676 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1678 thread_funnel_set(kernel_flock
, funnel_state
);
1681 t
*= 100000; /* time in us */
1682 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1683 ((t1).tv_usec - (t2).tv_usec))
1693 /* first character, start timer */
1696 etime
.tv_sec
= t
/ 1000000;
1697 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1698 timeradd(&etime
, &timecopy
, &etime
);
1701 } else if (qp
->c_cc
> last_cc
) {
1702 /* got a character, restart timer */
1704 etime
.tv_sec
= t
/ 1000000;
1705 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1706 timeradd(&etime
, &timecopy
, &etime
);
1710 /* nothing, check expiration */
1711 if (timercmp(&etime
, &timecopy
, <=))
1714 slp
= diff(etime
, timecopy
);
1717 } else { /* m == 0 */
1726 etime
.tv_sec
= t
/ 1000000;
1727 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1728 timeradd(&etime
, &timecopy
, &etime
);
1732 if (timercmp(&etime
, &timecopy
, <=)) {
1733 /* Timed out, but 0 is enough input. */
1735 thread_funnel_set(kernel_flock
, funnel_state
);
1738 slp
= diff(etime
, timecopy
);
1743 * Rounding down may make us wake up just short
1744 * of the target, so we round up.
1745 * The formula is ceiling(slp * hz/1000000).
1746 * 32-bit arithmetic is enough for hz < 169.
1747 * XXX see hzto() for how to avoid overflow if hz
1748 * is large (divide by `tick' and/or arrange to
1749 * use hzto() if hz is large).
1751 slp
= (long) (((u_long
)slp
* hz
) + 999999) / 1000000;
1754 if (qp
->c_cc
<= 0) {
1757 * There is no input, or not enough input and we can block.
1759 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
1760 ISSET(tp
->t_state
, TS_CONNECTED
) ?
1761 "ttyin" : "ttyhup", (int)slp
);
1763 if (error
== EWOULDBLOCK
)
1766 thread_funnel_set(kernel_flock
, funnel_state
);
1770 * XXX what happens if another process eats some input
1771 * while we are asleep (not just here)? It would be
1772 * safest to detect changes and reset our state variables
1773 * (has_stime and last_cc).
1781 * Input present, check for input mapping and processing.
1785 if (ISSET(lflag
, ICANON
)
1786 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
1788 if (ISSET(lflag
, ICANON
| ISIG
))
1795 icc
= min(uio
->uio_resid
, IBUFSIZ
);
1796 icc
= q_to_b(qp
, ibuf
, icc
);
1802 error
= uiomove(ibuf
, icc
, uio
);
1804 * XXX if there was an error then we should ungetc() the
1805 * unmoved chars and reduce icc here.
1808 if (ISSET(tp
->t_lflag
, ECHO
) &&
1809 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1810 snpin((struct snoop
*)tp
->t_sc
, ibuf
, icc
);
1814 if (uio
->uio_resid
== 0)
1828 * delayed suspend (^Y)
1830 if (CCEQ(cc
[VDSUSP
], c
) &&
1831 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
1832 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
1834 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
,
1843 * Interpret EOF only in canonical mode.
1845 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
1848 * Give user character.
1850 error
= ureadc(c
, uio
);
1852 /* XXX should ungetc(c, qp). */
1856 * Only snoop directly on input in echo mode. Non-echoed
1857 * input will be snooped later iff the application echoes it.
1859 if (ISSET(tp
->t_lflag
, ECHO
) &&
1860 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1861 snpinc((struct snoop
*)tp
->t_sc
, (char)c
);
1863 if (uio
->uio_resid
== 0)
1866 * In canonical mode check for a "break character"
1867 * marking the end of a "line of input".
1869 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
1876 * Look to unblock input now that (presumably)
1877 * the input queue has gone down.
1880 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
1881 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
1885 thread_funnel_set(kernel_flock
, funnel_state
);
1890 * Check the output queue on tp for space for a kernel message (from uprintf
1891 * or tprintf). Allow some space over the normal hiwater mark so we don't
1892 * lose messages due to normal flow control, but don't let the tty run amok.
1893 * Sleeps here are not interruptible, but we return prematurely if new signals
1897 ttycheckoutq(tp
, wait
)
1898 register struct tty
*tp
;
1901 int hiwat
, s
, oldsig
;
1904 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1906 hiwat
= tp
->t_hiwat
;
1908 oldsig
= wait
? ut
->uu_siglist
: 0;
1909 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
1910 while (tp
->t_outq
.c_cc
> hiwat
) {
1912 if (tp
->t_outq
.c_cc
<= hiwat
)
1914 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
1918 SET(tp
->t_state
, TS_SO_OLOWAT
);
1919 tsleep(TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
1926 * Process a write call on a tty device.
1929 ttwrite(tp
, uio
, flag
)
1930 register struct tty
*tp
;
1931 register struct uio
*uio
;
1934 register char *cp
= NULL
;
1935 register int cc
, ce
;
1936 register struct proc
*p
;
1937 int i
, hiwat
, cnt
, error
, s
;
1939 boolean_t funnel_state
;
1942 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1944 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1945 hiwat
= tp
->t_hiwat
;
1946 cnt
= uio
->uio_resid
;
1951 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1953 if (uio
->uio_resid
== cnt
)
1957 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
1958 if (flag
& IO_NDELAY
) {
1960 error
= EWOULDBLOCK
;
1963 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
1972 * Hang the process if it's in the background.
1975 if (isbackground(p
, tp
) &&
1976 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_flag
& P_PPWAIT
) == 0 &&
1977 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1978 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1979 if (p
->p_pgrp
->pg_jobc
== 0) {
1983 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
1984 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg4", 0);
1990 * Process the user's data in at most OBUFSIZ chunks. Perform any
1991 * output translation. Keep track of high water mark, sleep on
1992 * overflow awaiting device aid in acquiring new space.
1994 while (uio
->uio_resid
> 0 || cc
> 0) {
1995 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
1997 thread_funnel_set(kernel_flock
, funnel_state
);
2000 if (tp
->t_outq
.c_cc
> hiwat
)
2003 * Grab a hunk of data from the user, unless we have some
2004 * leftover from last time.
2007 cc
= min(uio
->uio_resid
, OBUFSIZ
);
2009 error
= uiomove(cp
, cc
, uio
);
2015 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
2016 snpin((struct snoop
*)tp
->t_sc
, cp
, cc
);
2020 * If nothing fancy need be done, grab those characters we
2021 * can handle without any of ttyoutput's processing and
2022 * just transfer them to the output q. For those chars
2023 * which require special processing (as indicated by the
2024 * bits in char_type), call ttyoutput. After processing
2025 * a hunk of data, look for FLUSHO so ^O's will take effect
2029 if (!ISSET(tp
->t_oflag
, OPOST
))
2032 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2033 (u_char
*)char_type
, CCLASSMASK
);
2035 * If ce is zero, then we're processing
2036 * a special character through ttyoutput.
2040 if (ttyoutput(*cp
, tp
) >= 0) {
2045 /* No Clists, wait a bit. */
2047 if (flag
& IO_NDELAY
) {
2048 error
= EWOULDBLOCK
;
2051 error
= ttysleep(tp
, &lbolt
,
2061 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2062 tp
->t_outq
.c_cc
> hiwat
)
2068 * A bunch of normal characters have been found.
2069 * Transfer them en masse to the output queue and
2070 * continue processing at the top of the loop.
2071 * If there are any further characters in this
2072 * <= OBUFSIZ chunk, the first should be a character
2073 * requiring special handling by ttyoutput.
2076 i
= b_to_q(cp
, ce
, &tp
->t_outq
);
2079 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2086 /* No Clists, wait a bit. */
2088 if (flag
& IO_NDELAY
) {
2089 error
= EWOULDBLOCK
;
2092 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
,
2099 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2100 tp
->t_outq
.c_cc
> hiwat
)
2107 * If cc is nonzero, we leave the uio structure inconsistent, as the
2108 * offset and iov pointers have moved forward, but it doesn't matter
2109 * (the call will either return short or restart with a new uio).
2111 uio
->uio_resid
+= cc
;
2112 thread_funnel_set(kernel_flock
, funnel_state
);
2119 * Since we are using ring buffers, if we can't insert any more into
2120 * the output queue, we can assume the ring is full and that someone
2121 * forgot to set the high water mark correctly. We set it and then
2122 * proceed as normal.
2124 hiwat
= tp
->t_outq
.c_cc
- 1;
2131 * This can only occur if FLUSHO is set in t_lflag,
2132 * or if ttstart/oproc is synchronous (or very fast).
2134 if (tp
->t_outq
.c_cc
<= hiwat
) {
2138 if (flag
& IO_NDELAY
) {
2140 uio
->uio_resid
+= cc
;
2141 thread_funnel_set(kernel_flock
, funnel_state
);
2142 return (uio
->uio_resid
== cnt
? EWOULDBLOCK
: 0);
2144 SET(tp
->t_state
, TS_SO_OLOWAT
);
2145 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2148 if (error
== EWOULDBLOCK
)
2156 * Rubout one character from the rawq of tp
2157 * as cleanly as possible.
2162 register struct tty
*tp
;
2164 register u_char
*cp
;
2165 register int savecol
;
2168 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2170 CLR(tp
->t_lflag
, FLUSHO
);
2171 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2172 if (tp
->t_rocount
== 0) {
2174 * Messed up by ttwrite; retype
2179 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2182 CLR(c
, ~TTY_CHARMASK
);
2183 switch (CCLASS(c
)) {
2192 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2196 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2201 savecol
= tp
->t_column
;
2202 SET(tp
->t_state
, TS_CNTTB
);
2203 SET(tp
->t_lflag
, FLUSHO
);
2204 tp
->t_column
= tp
->t_rocol
;
2206 cp
= tp
->t_rawq
.c_cf
;
2208 tabc
= *cp
; /* XXX FIX NEXTC */
2209 for (; cp
; cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2212 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2213 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2216 CLR(tp
->t_lflag
, FLUSHO
);
2217 CLR(tp
->t_state
, TS_CNTTB
);
2220 /* savecol will now be length of the tab. */
2221 savecol
-= tp
->t_column
;
2222 tp
->t_column
+= savecol
;
2224 savecol
= 8; /* overflow fixup */
2225 while (--savecol
>= 0)
2226 (void)ttyoutput('\b', tp
);
2229 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2230 (void)printf(PANICSTR
, c
, CCLASS(c
));
2232 panic(PANICSTR
, c
, CCLASS(c
));
2236 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2237 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2238 SET(tp
->t_state
, TS_ERASE
);
2239 (void)ttyoutput('\\', tp
);
2243 ttyecho(tp
->t_cc
[VERASE
], tp
);
2248 * Back over cnt characters, erasing them.
2252 register struct tty
*tp
;
2257 (void)ttyoutput('\b', tp
);
2258 (void)ttyoutput(' ', tp
);
2259 (void)ttyoutput('\b', tp
);
2265 * Reprint the rawq line. Note, it is assumed that c_cc has already
2270 register struct tty
*tp
;
2272 register u_char
*cp
;
2275 /* Echo the reprint character. */
2276 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2277 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2279 (void)ttyoutput('\n', tp
);
2283 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2284 * BIT OF FIRST CHAR.
2288 for (cp
= tp
->t_canq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2289 cp
!= NULL
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2291 for (cp
= tp
->t_rawq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2292 cp
!= NULL
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2295 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2297 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2300 CLR(tp
->t_state
, TS_ERASE
);
2303 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2308 * Echo a typed character to the terminal.
2313 register struct tty
*tp
;
2316 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2317 CLR(tp
->t_lflag
, FLUSHO
);
2318 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2319 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2320 ISSET(tp
->t_lflag
, EXTPROC
))
2322 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2323 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2324 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2325 (void)ttyoutput('^', tp
);
2326 CLR(c
, ~TTY_CHARMASK
);
2332 (void)ttyoutput(c
, tp
);
2336 * Wake up any readers on a tty.
2340 register struct tty
*tp
;
2344 if (tp
->t_rsel
.si_pid
!= 0)
2346 selwakeup(&tp
->t_rsel
);
2347 if (ISSET(tp
->t_state
, TS_ASYNC
))
2348 pgsignal(tp
->t_pgrp
, SIGIO
, 1);
2349 wakeup(TSA_HUP_OR_INPUT(tp
));
2353 * Wake up any writers on a tty.
2357 register struct tty
*tp
;
2360 if (tp
->t_wsel
.si_pid
!= 0 && tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2362 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2364 selwakeup(&tp
->t_wsel
);
2365 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2366 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2367 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2368 wakeup(TSA_OCOMPLETE(tp
));
2370 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2371 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2372 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2373 wakeup(TSA_OLOWAT(tp
));
2378 * Look up a code for a specified speed in a conversion table;
2379 * used by drivers to map software speed values to hardware parameters.
2382 ttspeedtab(speed
, table
)
2384 register struct speedtab
*table
;
2387 for ( ; table
->sp_speed
!= -1; table
++)
2388 if (table
->sp_speed
== speed
)
2389 return (table
->sp_code
);
2394 * Set tty hi and low water marks.
2396 * Try to arrange the dynamics so there's about one second
2397 * from hi to low water.
2404 register int cps
, x
;
2406 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2408 cps
= tp
->t_ospeed
/ 10;
2409 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2411 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2412 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2416 /* NeXT ttyinfo has been converted to the MACH kernel */
2417 #include <mach/thread_info.h>
2420 * Report on state of foreground process group.
2424 register struct tty
*tp
;
2426 /* NOT IMPLEMENTED FOR MACH */
2431 * Returns 1 if p2 is "better" than p1
2433 * The algorithm for picking the "interesting" process is thus:
2435 * 1) Only foreground processes are eligible - implied.
2436 * 2) Runnable processes are favored over anything else. The runner
2437 * with the highest cpu utilization is picked (p_estcpu). Ties are
2438 * broken by picking the highest pid.
2439 * 3) The sleeper with the shortest sleep time is next. With ties,
2440 * we pick out just "short-term" sleepers (P_SINTR == 0).
2441 * 4) Further ties are broken by picking the highest pid.
2443 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2444 #define TESTAB(a, b) ((a)<<1 | (b))
2450 proc_compare(p1
, p2
)
2451 register struct proc
*p1
, *p2
;
2457 * see if at least one of them is runnable
2459 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2466 * tie - favor one with highest recent cpu utilization
2468 if (p2
->p_estcpu
> p1
->p_estcpu
)
2470 if (p1
->p_estcpu
> p2
->p_estcpu
)
2472 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2477 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2483 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2486 * pick the one with the smallest sleep time
2488 if (p2
->p_slptime
> p1
->p_slptime
)
2490 if (p1
->p_slptime
> p2
->p_slptime
)
2493 * favor one sleeping in a non-interruptible sleep
2495 if (p1
->p_flag
& P_SINTR
&& (p2
->p_flag
& P_SINTR
) == 0)
2497 if (p2
->p_flag
& P_SINTR
&& (p1
->p_flag
& P_SINTR
) == 0)
2499 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2504 * Output char to tty; console putchar style.
2514 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2519 (void)ttyoutput('\r', tp
);
2520 (void)ttyoutput(c
, tp
);
2527 * Sleep on chan, returning ERESTART if tty changed while we napped and
2528 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2529 * the tty is revoked, restarting a pending call will redo validation done
2530 * at the start of the call.
2533 ttysleep(tp
, chan
, pri
, wmesg
, timo
)
2543 error
= tsleep(chan
, pri
, wmesg
, timo
);
2546 return (tp
->t_gen
== gen
? 0 : ERESTART
);
2551 * Allocate a tty structure and its associated buffers.
2558 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
);
2559 bzero(tp
, sizeof *tp
);
2560 /* XXX: default to TTYCLSIZE(1024) chars for now */
2561 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
2562 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
2563 /* output queue doesn't need quoting */
2564 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
2569 * Free a tty structure and its buffers.
2575 clfree(&tp
->t_rawq
);
2576 clfree(&tp
->t_canq
);
2577 clfree(&tp
->t_outq
);
2585 * XXX this is usable not useful or used. Most tty drivers have
2586 * ifdefs for using ttymalloc() but assume a different interface.
2589 * Allocate a tty struct. Clists in the struct will be allocated by
2597 tp
= _MALLOC(sizeof *tp
, M_TTYS
, M_WAITOK
);
2598 bzero(tp
, sizeof *tp
);
2603 #if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */
2605 * Free a tty struct. Clists in the struct should have been freed by