2 * Copyright (c) 1997-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1990, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)tty.c 8.8 (Berkeley) 1/21/94
69 * o Fix races for sending the start char in ttyflush().
70 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
71 * With luck, there will be MIN chars before select() returns().
72 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
73 * o Don't allow input in TS_ZOMBIE case. It would be visible through
75 * o Do the new sio locking stuff here and use it to avoid special
78 * o Move EXTPROC and/or PENDIN to t_state?
79 * o Wrap most of ttioctl in spltty/splx.
80 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
81 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
82 * o Don't allow certain termios flags to affect disciplines other
83 * than TTYDISC. Cancel their effects before switch disciplines
84 * and ignore them if they are set while we are in another
86 * o Handle c_ispeed = 0 to c_ispeed = c_ospeed conversion here instead
87 * of in drivers and fix drivers that write to tp->t_termios.
88 * o Check for TS_CARR_ON being set while everything is closed and not
89 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
90 * so it would live until the next open even if carrier drops.
91 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
92 * only when _all_ openers leave open().
94 #include <sys/param.h>
96 #include <sys/systm.h>
98 #include <sys/ioctl.h>
99 #include <sys/proc_internal.h>
100 #include <sys/kauth.h>
101 #include <sys/file_internal.h>
102 #include <sys/conf.h>
103 #include <sys/dkstat.h>
104 #include <sys/uio_internal.h>
105 #include <sys/kernel.h>
106 #include <sys/vnode.h>
107 #include <sys/syslog.h>
108 #include <sys/user.h>
109 #include <sys/signalvar.h>
110 #include <sys/signalvar.h>
111 #include <sys/malloc.h>
113 #include <dev/kmreg_com.h>
114 #include <machine/cons.h>
115 #include <sys/resource.h> /* averunnable */
118 * Debugging assertions for tty locks
122 #define TTY_LOCK_OWNED(tp) do {lck_mtx_assert(&tp->t_lock, LCK_MTX_ASSERT_OWNED); } while (0)
123 #define TTY_LOCK_NOTOWNED(tp) do {lck_mtx_assert(&tp->t_lock, LCK_MTX_ASSERT_NOTOWNED); } while (0)
125 #define TTY_LOCK_OWNED(tp)
126 #define TTY_LOCK_NOTOWNED(tp)
129 static lck_grp_t
*tty_lck_grp
;
130 static lck_grp_attr_t
*tty_lck_grp_attr
;
131 static lck_attr_t
*tty_lck_attr
;
133 __private_extern__
int ttnread(struct tty
*tp
);
134 static void ttyecho(int c
, struct tty
*tp
);
135 static int ttyoutput(int c
, struct tty
*tp
);
136 static void ttypend(struct tty
*tp
);
137 static void ttyretype(struct tty
*tp
);
138 static void ttyrub(int c
, struct tty
*tp
);
139 static void ttyrubo(struct tty
*tp
, int count
);
140 static void ttystop(struct tty
*tp
, int rw
);
141 static void ttyunblock(struct tty
*tp
);
142 static int ttywflush(struct tty
*tp
);
143 static int proc_compare(proc_t p1
, proc_t p2
);
145 static void ttyhold(struct tty
*tp
);
146 static void ttydeallocate(struct tty
*tp
);
148 static int isctty(proc_t p
, struct tty
*tp
);
149 static int isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
);
152 * Table with character classes and parity. The 8th bit indicates parity,
153 * the 7th bit indicates the character is an alphameric or underscore (for
154 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
155 * are 0 then the character needs no special processing on output; classes
156 * other than 0 might be translated or (not currently) require delays.
158 #define E 0x00 /* Even parity. */
159 #define O 0x80 /* Odd parity. */
160 #define PARITY(c) (char_type[c] & O)
162 #define ALPHA 0x40 /* Alpha or underscore. */
163 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
165 #define CCLASSMASK 0x3f
166 #define CCLASS(c) (char_type[c] & CCLASSMASK)
167 /* 0b10xxxxxx is the mask for UTF-8 continuations */
168 #define CCONT(c) ((c & 0xc0) == 0x80)
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 */
240 termios32to64(struct termios32
*in
, struct user_termios
*out
)
242 out
->c_iflag
= (user_tcflag_t
)in
->c_iflag
;
243 out
->c_oflag
= (user_tcflag_t
)in
->c_oflag
;
244 out
->c_cflag
= (user_tcflag_t
)in
->c_cflag
;
245 out
->c_lflag
= (user_tcflag_t
)in
->c_lflag
;
247 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
248 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
250 out
->c_ispeed
= (user_speed_t
)in
->c_ispeed
;
251 out
->c_ospeed
= (user_speed_t
)in
->c_ospeed
;
255 termios64to32(struct user_termios
*in
, struct termios32
*out
)
257 out
->c_iflag
= (tcflag_t
)in
->c_iflag
;
258 out
->c_oflag
= (tcflag_t
)in
->c_oflag
;
259 out
->c_cflag
= (tcflag_t
)in
->c_cflag
;
260 out
->c_lflag
= (tcflag_t
)in
->c_lflag
;
262 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
263 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
265 out
->c_ispeed
= (speed_t
)in
->c_ispeed
;
266 out
->c_ospeed
= (speed_t
)in
->c_ospeed
;
273 * Initialize the tty line discipline subsystem.
279 * Locks: No ttys can be allocated and no tty locks can be used
280 * until after this function is called
282 * Notes: The intent of this is to set up a log group attribute,
283 * lock group, and loc atribute for subsequent per-tty locks.
284 * This function is called early in bsd_init(), prior to the
285 * console device initialization.
290 tty_lck_grp_attr
= lck_grp_attr_alloc_init();
291 tty_lck_grp
= lck_grp_alloc_init("tty", tty_lck_grp_attr
);
292 tty_lck_attr
= lck_attr_alloc_init();
299 * Lock the requested tty structure.
301 * Parameters: tp The tty we want to lock
305 * Locks: On return, tp is locked
308 tty_lock(struct tty
*tp
)
310 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
311 lck_mtx_lock(&tp
->t_lock
);
318 * Unlock the requested tty structure.
320 * Parameters: tp The tty we want to unlock
324 * Locks: On return, tp is unlocked
327 tty_unlock(struct tty
*tp
)
329 TTY_LOCK_OWNED(tp
); /* debug assert */
330 lck_mtx_unlock(&tp
->t_lock
);
337 * Initial open of tty, or (re)entry to standard tty line discipline.
339 * Locks: Assumes tty_lock() is held prior to calling.
342 ttyopen(dev_t device
, struct tty
*tp
)
344 TTY_LOCK_OWNED(tp
); /* debug assert */
348 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
349 SET(tp
->t_state
, TS_ISOPEN
);
350 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
351 SET(tp
->t_state
, TS_CONNECTED
); }
352 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
361 * Handle close() on a tty line: flush and set to initial state,
362 * bumping generation number so that pending read/write calls
363 * can detect recycling of the tty.
364 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
365 * and l_close() should have flushed, but we repeat the spltty() and
366 * the flush in case there are buggy callers.
368 * Locks: Assumes tty_lock() is held prior to calling.
371 ttyclose(struct tty
*tp
)
374 struct session
* oldsessp
;
376 TTY_LOCK_OWNED(tp
); /* debug assert */
383 * Closing current console tty; disable printing of console
384 * messages at bottom-level driver.
386 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
387 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
390 ttyflush(tp
, FREAD
| FWRITE
);
393 tp
->t_line
= TTYDISC
;
396 oldsessp
= tp
->t_session
;
398 tp
->t_session
= NULL
;
399 if (oldsessp
!= SESSION_NULL
)
400 oldsessp
->s_ttypgrpid
= NO_PID
;
402 /* drop the reference on prev session and pgrp */
403 /* SAFE: All callers drop the lock on return */
405 if (oldsessp
!= SESSION_NULL
)
406 session_rele(oldsessp
);
407 if (oldpg
!= PGRP_NULL
)
411 selthreadclear(&tp
->t_wsel
);
412 selthreadclear(&tp
->t_rsel
);
416 #define FLUSHQ(q) { \
418 ndflush(q, (q)->c_cc); \
421 /* Is 'c' a line delimiter ("break" character)? */
422 #define TTBREAKC(c, lflag) \
423 ((c) == '\n' || (((c) == cc[VEOF] || \
424 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
425 (c) != _POSIX_VDISABLE))
430 * Process input of a single character received on a tty.
432 * Parameters: c The character received
433 * tp The tty on which it was received
437 * Locks: Assumes tty_lock() is held prior to calling.
440 ttyinput(int c
, struct tty
*tp
)
442 tcflag_t iflag
, lflag
;
445 int retval
= 0; /* default return value */
447 TTY_LOCK_OWNED(tp
); /* debug assert */
450 * If input is pending take it first.
453 if (ISSET(lflag
, PENDIN
))
458 if (ISSET(lflag
, ICANON
)) {
468 * Block further input iff:
469 * current input > threshold AND input is available to user program
470 * AND input flow control is enabled and not yet invoked.
471 * The 3 is slop for PARMRK.
474 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
475 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
476 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
477 !ISSET(tp
->t_state
, TS_TBLOCK
))
480 /* Handle exceptional conditions (break, parity, framing). */
482 err
= (ISSET(c
, TTY_ERRORMASK
));
484 CLR(c
, TTY_ERRORMASK
);
485 if (ISSET(err
, TTY_BI
)) {
486 if (ISSET(iflag
, IGNBRK
)) {
489 if (ISSET(iflag
, BRKINT
)) {
490 ttyflush(tp
, FREAD
| FWRITE
);
491 /* SAFE: All callers drop the lock on return */
493 tty_pgsignal(tp
, SIGINT
, 1);
497 if (ISSET(iflag
, PARMRK
))
499 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
500 || ISSET(err
, TTY_FE
)) {
501 if (ISSET(iflag
, IGNPAR
)) {
504 else if (ISSET(iflag
, PARMRK
)) {
506 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
509 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
510 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
511 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
518 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
520 if (!ISSET(lflag
, EXTPROC
)) {
522 * Check for literal nexting very first
524 if (ISSET(tp
->t_state
, TS_LNCH
)) {
526 CLR(tp
->t_state
, TS_LNCH
);
529 * Scan for special characters. This code
530 * is really just a big case statement with
531 * non-constant cases. The bottom of the
532 * case statement is labeled ``endcase'', so goto
533 * it after a case match, or similar.
537 * Control chars which aren't controlled
538 * by ICANON, ISIG, or IXON.
540 if (ISSET(lflag
, IEXTEN
)) {
541 if (CCEQ(cc
[VLNEXT
], c
)) {
542 if (ISSET(lflag
, ECHO
)) {
543 if (ISSET(lflag
, ECHOE
)) {
544 (void)ttyoutput('^', tp
);
545 (void)ttyoutput('\b', tp
);
549 SET(tp
->t_state
, TS_LNCH
);
552 if (CCEQ(cc
[VDISCARD
], c
)) {
553 if (ISSET(lflag
, FLUSHO
))
554 CLR(tp
->t_lflag
, FLUSHO
);
556 ttyflush(tp
, FWRITE
);
558 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
560 SET(tp
->t_lflag
, FLUSHO
);
568 if (ISSET(lflag
, ISIG
)) {
569 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
570 if (!ISSET(lflag
, NOFLSH
))
571 ttyflush(tp
, FREAD
| FWRITE
);
574 * SAFE: All callers drop the lock on return;
575 * SAFE: if we lose a threaded race on change
576 * SAFE: of the interrupt character, we could
577 * SAFE: have lost that race anyway due to the
578 * SAFE: scheduler executing threads in
579 * SAFE: priority order rather than "last
580 * SAFE: active thread" order (FEATURE).
584 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
588 if (CCEQ(cc
[VSUSP
], c
)) {
589 if (!ISSET(lflag
, NOFLSH
))
592 /* SAFE: All callers drop the lock on return */
594 tty_pgsignal(tp
, SIGTSTP
, 1);
600 * Handle start/stop characters.
602 if (ISSET(iflag
, IXON
)) {
603 if (CCEQ(cc
[VSTOP
], c
)) {
604 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
605 SET(tp
->t_state
, TS_TTSTOP
);
609 if (!CCEQ(cc
[VSTART
], c
)) {
613 * if VSTART == VSTOP then toggle
617 if (CCEQ(cc
[VSTART
], c
))
621 * IGNCR, ICRNL, & INLCR
624 if (ISSET(iflag
, IGNCR
)) {
627 else if (ISSET(iflag
, ICRNL
))
629 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
632 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
634 * From here on down canonical mode character
635 * processing takes place.
640 if (CCEQ(cc
[VERASE
], c
)) {
641 if (tp
->t_rawq
.c_cc
) {
642 if (ISSET(iflag
, IUTF8
)) {
644 ttyrub((c
= unputc(&tp
->t_rawq
)), tp
);
645 } while(tp
->t_rawq
.c_cc
&& CCONT(c
));
647 ttyrub(unputc(&tp
->t_rawq
), tp
);
655 if (CCEQ(cc
[VKILL
], c
)) {
656 if (ISSET(lflag
, ECHOKE
) &&
657 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
658 !ISSET(lflag
, ECHOPRT
))
659 while (tp
->t_rawq
.c_cc
)
660 ttyrub(unputc(&tp
->t_rawq
), tp
);
663 if (ISSET(lflag
, ECHOK
) ||
664 ISSET(lflag
, ECHOKE
))
669 CLR(tp
->t_state
, TS_LOCAL
);
675 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
681 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
686 * erase last char of word and remember the
687 * next chars type (for ALTWERASE)
690 c
= unputc(&tp
->t_rawq
);
693 if (c
== ' ' || c
== '\t') {
694 (void)putc(c
, &tp
->t_rawq
);
703 c
= unputc(&tp
->t_rawq
);
706 } while (c
!= ' ' && c
!= '\t' &&
707 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
708 (void)putc(c
, &tp
->t_rawq
);
714 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
719 * ^T - kernel info and generate SIGINFO
721 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
722 if (ISSET(lflag
, ISIG
)) {
723 /* SAFE: All callers drop the lock on return */
725 tty_pgsignal(tp
, SIGINFO
, 1);
728 if (!ISSET(lflag
, NOKERNINFO
))
734 * Check for input buffer overflow
736 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
738 if (ISSET(iflag
, IMAXBEL
)) {
739 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
740 (void)ttyoutput(CTRL('g'), tp
);
745 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
746 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
747 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
750 * Put data char in q for user and
751 * wakeup on seeing a line delimiter.
753 if (putc(c
, &tp
->t_rawq
) >= 0) {
754 if (!ISSET(lflag
, ICANON
)) {
759 if (TTBREAKC(c
, lflag
)) {
761 catq(&tp
->t_rawq
, &tp
->t_canq
);
763 } else if (tp
->t_rocount
++ == 0)
764 tp
->t_rocol
= tp
->t_column
;
765 if (ISSET(tp
->t_state
, TS_ERASE
)) {
767 * end of prterase \.../
769 CLR(tp
->t_state
, TS_ERASE
);
770 (void)ttyoutput('/', tp
);
774 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
776 * Place the cursor over the '^' of the ^D.
778 i
= min(2, tp
->t_column
- i
);
780 (void)ttyoutput('\b', tp
);
788 * IXANY means allow any character to restart output.
790 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
791 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
796 CLR(tp
->t_lflag
, FLUSHO
);
797 CLR(tp
->t_state
, TS_TTSTOP
);
800 /* Start the output */
801 retval
= ttstart(tp
);
811 * Output a single character on a tty, doing output processing
812 * as needed (expanding tabs, newline processing, etc.).
814 * Parameters: c The character to output
815 * tp The tty on which to output on the tty
817 * Returns: < 0 Success
818 * >= 0 Character to resend (failure)
820 * Locks: Assumes tp is locked on entry, remains locked on exit
822 * Notes: Must be recursive.
825 ttyoutput(int c
, struct tty
*tp
)
830 TTY_LOCK_OWNED(tp
); /* debug assert */
833 if (!ISSET(oflag
, OPOST
)) {
834 if (ISSET(tp
->t_lflag
, FLUSHO
))
836 if (putc(c
, &tp
->t_outq
))
843 * Do tab expansion if OXTABS is set. Special case if we external
844 * processing, we don't do the tab expansion because we'll probably
845 * get it wrong. If tab expansion needs to be done, let it happen
848 CLR(c
, ~TTY_CHARMASK
);
850 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
851 col
= c
= 8 - (tp
->t_column
& 7);
852 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
853 c
-= b_to_q((const u_char
*)" ", c
, &tp
->t_outq
);
858 return (c
== col
? -1 : '\t');
860 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
864 * Newline translation: if ONLCR is set,
865 * translate newline into "\r\n".
867 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
870 if (putc('\r', &tp
->t_outq
))
873 /* If OCRNL is set, translate "\r" into "\n". */
874 else if (c
== '\r' && ISSET(tp
->t_oflag
, OCRNL
))
876 /* If ONOCR is set, don't transmit CRs when on column 0. */
877 else if (c
== '\r' && ISSET(tp
->t_oflag
, ONOCR
) && tp
->t_column
== 0)
881 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
900 col
= (col
+ 8) & ~7;
908 * Sets the tty state to not allow any more changes of foreground process
909 * group. This is required to be done so that a subsequent revoke on a vnode
910 * is able to always successfully complete.
912 * Locks : Assumes tty_lock held on entry
915 ttysetpgrphup(struct tty
*tp
)
917 TTY_LOCK_OWNED(tp
); /* debug assert */
918 SET(tp
->t_state
, TS_PGRPHUP
);
922 * Locks : Assumes tty lock held on entry
925 ttyclrpgrphup(struct tty
*tp
)
927 TTY_LOCK_OWNED(tp
); /* debug assert */
928 CLR(tp
->t_state
, TS_PGRPHUP
);
934 * Identical to ttioctl_locked, only the lock is not held
936 * Parameters: <See ttioctl_locked()>
938 * Returns: <See ttioctl_locked()>
940 * Locks: This function assumes the tty_lock() is not held on entry;
941 * it takes the lock, and releases it before returning.
943 * Notes: This is supported to ensure the line discipline interfaces
944 * all have the same locking semantics.
946 * This function is called from
949 ttioctl(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
954 retval
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
964 * Ioctls for all tty devices.
966 * Parameters: tp Tty on which ioctl() is being called
967 * cmd ioctl() command parameter
968 * data ioctl() data argument (if any)
969 * flag fileglob open modes from fcntl.h;
970 * if called internally, this is usually
971 * set to 0, rather than something useful
972 * p Process context for the call; if the
973 * call is proxied to a worker thread,
974 * this will not be the current process!!!
977 * EIO I/O error (no process group, job
979 * EINTR Interrupted by signal
980 * EBUSY Attempt to become the console while
981 * the console is busy
982 * ENOTTY TIOCGPGRP on a non-controlling tty
983 * EINVAL Invalid baud rate
984 * ENXIO TIOCSETD of invalid line discipline
985 * EPERM TIOCSTI, not root, not open for read
986 * EACCES TIOCSTI, not root, not your controlling
988 * EPERM TIOCSCTTY failed
989 * ENOTTY/EINVAL/EPERM TIOCSPGRP failed
990 * EPERM TIOCSDRAINWAIT as non-root user
991 * suser:EPERM Console control denied
992 * ttywait:EIO t_timeout too small/expired
993 * ttywait:ERESTART Upper layer must redrive the call;
994 * this is usually done by the Libc
996 * ttywait:EINTR Interrupted (usually a signal)
1000 * ttcompat:ENOTTY TIOCGSID, if no session or session
1002 * ttcompat:ENOTTY All unrecognized ioctls
1003 * *tp->t_param:? TIOCSETA* underlying function
1004 * *linesw[t].l_open:? TIOCSETD line discipline open failure
1007 * Locks: This function assumes that the tty_lock() is held for the
1008 * tp at the time of the call. The lock remains held on return.
1010 * Notes: This function is called after line-discipline specific ioctl
1011 * has been called to do discipline-specific functions and/or
1012 * reject any of these ioctl() commands.
1014 * This function calls ttcompat(), which can re-call ttioctl()
1015 * to a depth of one (FORTRAN style mutual recursion); at some
1016 * point, we should just in-line ttcompat() here.
1019 ttioctl_locked(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1024 struct pgrp
*pg
, *oldpg
;
1025 struct session
*sessp
, *oldsessp
;
1028 TTY_LOCK_OWNED(tp
); /* debug assert */
1030 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1031 /* If the ioctl involves modification, signal if in the background. */
1058 while (isbackground(p
, tp
) &&
1059 (p
->p_lflag
& P_LPPWAIT
) == 0 &&
1060 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1061 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1063 if (pg
== PGRP_NULL
) {
1067 /* SAFE: All callers drop the lock on return */
1069 if (pg
->pg_jobc
== 0) {
1075 pgsignal(pg
, SIGTTOU
, 1);
1081 * We signalled ourself, so we need to act as if we
1082 * have been "interrupted" from a "sleep" to act on
1083 * the signal. If it's a signal that stops the
1084 * process, that's handled in the signal sending code.
1092 switch (cmd
) { /* Process the ioctl. */
1093 case FIOASYNC
: /* set/clear async i/o */
1095 SET(tp
->t_state
, TS_ASYNC
);
1097 CLR(tp
->t_state
, TS_ASYNC
);
1099 case FIONBIO
: /* set/clear non-blocking i/o */
1100 break; /* XXX: delete. */
1101 case FIONREAD
: /* get # bytes to read */
1102 *(int *)data
= ttnread(tp
);
1104 case TIOCEXCL
: /* set exclusive use of tty */
1105 SET(tp
->t_state
, TS_XCLUDE
);
1107 case TIOCFLUSH
: { /* flush buffers */
1108 int flags
= *(int *)data
;
1111 flags
= FREAD
| FWRITE
;
1113 flags
&= FREAD
| FWRITE
;
1114 ttyflush(tp
, flags
);
1118 /* Set current console device to this line */
1119 data
= (caddr_t
) &bogusData
;
1121 /* No break - Fall through to BSD code */
1123 case TIOCCONS
: { /* become virtual console */
1125 if (constty
&& constty
!= tp
&&
1126 ISSET(constty
->t_state
, TS_CONNECTED
)) {
1130 if ( (error
= suser(kauth_cred_get(), &p
->p_acflag
)) )
1133 } else if (tp
== constty
) {
1137 (*cdevsw
[major(constty
->t_dev
)].d_ioctl
)
1138 (constty
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1140 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
1141 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1145 case TIOCDRAIN
: /* wait till output drained */
1146 error
= ttywait(tp
);
1150 case TIOCGETA_32
: /* get termios struct */
1152 termios64to32((struct user_termios
*)&tp
->t_termios
, (struct termios32
*)data
);
1154 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1157 case TIOCGETA_64
: /* get termios struct */
1159 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1161 termios32to64((struct termios32
*)&tp
->t_termios
, (struct user_termios
*)data
);
1164 case TIOCGETD
: /* get line discipline */
1165 *(int *)data
= tp
->t_line
;
1167 case TIOCGWINSZ
: /* get window size */
1168 *(struct winsize
*)data
= tp
->t_winsize
;
1170 case TIOCGPGRP
: /* get pgrp of tty */
1171 if (!isctty(p
, tp
)) {
1175 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
1178 case TIOCHPCL
: /* hang up on last close */
1179 SET(tp
->t_cflag
, HUPCL
);
1182 case TIOCNXCL
: /* reset exclusive use of tty */
1183 CLR(tp
->t_state
, TS_XCLUDE
);
1185 case TIOCOUTQ
: /* output queue size */
1186 *(int *)data
= tp
->t_outq
.c_cc
;
1188 case TIOCSETA_32
: /* set termios struct */
1190 case TIOCSETAW_32
: /* drain output, set */
1192 case TIOCSETAF_32
: /* drn out, fls in, set */
1194 { /* drn out, fls in, set */
1195 struct termios
*t
= (struct termios
*)data
;
1196 struct termios lcl_termios
;
1199 if (cmd
==TIOCSETA_32
|| cmd
==TIOCSETAW_32
|| cmd
==TIOCSETAF_32
) {
1200 termios32to64((struct termios32
*)data
, (struct user_termios
*)&lcl_termios
);
1204 if (cmd
==TIOCSETA_64
|| cmd
==TIOCSETAW_64
|| cmd
==TIOCSETAF_64
) {
1205 termios64to32((struct user_termios
*)data
, (struct termios32
*)&lcl_termios
);
1210 /* XXX bogus test; always false */
1211 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0) {
1215 #endif /* 0 - leave in; may end up being a conformance issue */
1216 if (t
->c_ispeed
== 0)
1217 t
->c_ispeed
= t
->c_ospeed
;
1218 if (cmd
== TIOCSETAW_32
|| cmd
== TIOCSETAF_32
||
1219 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
1220 error
= ttywait(tp
);
1224 if (cmd
== TIOCSETAF_32
|| cmd
== TIOCSETAF_64
)
1225 ttyflush(tp
, FREAD
);
1227 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1229 * Set device hardware.
1231 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1234 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1235 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1237 * XXX disconnections would be too hard to
1238 * get rid of without this kludge. The only
1239 * way to get rid of controlling terminals
1240 * is to exit from the session leader.
1242 CLR(tp
->t_state
, TS_ZOMBIE
);
1244 wakeup(TSA_CARR_ON(tp
));
1248 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1249 ISSET(t
->c_cflag
, CLOCAL
)) &&
1250 !ISSET(tp
->t_state
, TS_ZOMBIE
))
1251 SET(tp
->t_state
, TS_CONNECTED
);
1253 CLR(tp
->t_state
, TS_CONNECTED
);
1254 tp
->t_cflag
= t
->c_cflag
;
1255 tp
->t_ispeed
= t
->c_ispeed
;
1256 tp
->t_ospeed
= t
->c_ospeed
;
1259 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1260 cmd
!= TIOCSETAF_32
&& cmd
!= TIOCSETAF_64
) {
1261 if (ISSET(t
->c_lflag
, ICANON
))
1262 SET(tp
->t_lflag
, PENDIN
);
1265 * XXX we really shouldn't allow toggling
1266 * ICANON while we're in a non-termios line
1267 * discipline. Now we have to worry about
1268 * panicing for a null queue.
1270 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1273 catq(&tp
->t_rawq
, &tp
->t_canq
);
1275 tp
->t_rawq
= tp
->t_canq
;
1278 CLR(tp
->t_lflag
, PENDIN
);
1282 tp
->t_iflag
= t
->c_iflag
;
1283 tp
->t_oflag
= t
->c_oflag
;
1285 * Make the EXTPROC bit read only.
1287 if (ISSET(tp
->t_lflag
, EXTPROC
))
1288 SET(t
->c_lflag
, EXTPROC
);
1290 CLR(t
->c_lflag
, EXTPROC
);
1291 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1292 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1293 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1295 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1298 case TIOCSETD
: { /* set line discipline */
1299 int t
= *(int *)data
;
1300 dev_t device
= tp
->t_dev
;
1302 if (t
>= nlinesw
|| t
< 0) {
1307 * If the new line discipline is not equal to the old one,
1308 * close the old one and open the new one.
1310 if (t
!= tp
->t_line
) {
1311 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1312 error
= (*linesw
[t
].l_open
)(device
, tp
);
1314 /* This is racy; it's possible to lose both */
1315 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1322 case TIOCSTART
: /* start output, like ^Q */
1323 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1324 ISSET(tp
->t_lflag
, FLUSHO
)) {
1325 CLR(tp
->t_lflag
, FLUSHO
);
1326 CLR(tp
->t_state
, TS_TTSTOP
);
1330 case TIOCSTI
: /* simulate terminal input */
1331 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0) {
1335 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
)) {
1339 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1341 case TIOCSTOP
: /* stop output, like ^S */
1342 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1343 SET(tp
->t_state
, TS_TTSTOP
);
1353 case TIOCSCTTY
: /* become controlling tty */
1354 /* Session ctty vnode pointer set in vnode layer. */
1355 sessp
= proc_session(p
);
1356 if (sessp
== SESSION_NULL
) {
1362 * This can only be done by a session leader.
1364 if (!SESS_LEADER(p
, sessp
)) {
1365 /* SAFE: All callers drop the lock on return */
1367 session_rele(sessp
);
1373 * If this terminal is already the controlling terminal for the
1374 * session, nothing to do here.
1376 if (tp
->t_session
== sessp
) {
1377 /* SAFE: All callers drop the lock on return */
1379 session_rele(sessp
);
1386 * Deny if the terminal is already attached to another session or
1387 * the session already has a terminal vnode.
1389 session_lock(sessp
);
1390 if (sessp
->s_ttyvp
|| tp
->t_session
) {
1391 session_unlock(sessp
);
1392 /* SAFE: All callers drop the lock on return */
1394 if (pg
!= PGRP_NULL
) {
1397 session_rele(sessp
);
1402 sessp
->s_ttypgrpid
= pg
->pg_id
;
1403 oldtp
= sessp
->s_ttyp
;
1406 session_unlock(sessp
);
1408 oldsessp
= tp
->t_session
;
1410 if (oldsessp
!= SESSION_NULL
)
1411 oldsessp
->s_ttypgrpid
= NO_PID
;
1412 /* do not drop refs on sessp and pg as tp holds them */
1413 tp
->t_session
= sessp
;
1416 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
1417 /* SAFE: All callers drop the lock on return */
1419 /* drop the reference on prev session and pgrp */
1420 if (oldsessp
!= SESSION_NULL
)
1421 session_rele(oldsessp
);
1422 if (oldpg
!= PGRP_NULL
)
1429 case TIOCSPGRP
: { /* set pgrp of tty */
1430 struct pgrp
*pgrp
= PGRP_NULL
;
1432 sessp
= proc_session(p
);
1433 if (!isctty_sp(p
, tp
, sessp
)) {
1434 if (sessp
!= SESSION_NULL
)
1435 session_rele(sessp
);
1439 else if ((pgrp
= pgfind(*(int *)data
)) == PGRP_NULL
) {
1440 if (sessp
!= SESSION_NULL
)
1441 session_rele(sessp
);
1444 } else if (pgrp
->pg_session
!= sessp
) {
1445 /* SAFE: All callers drop the lock on return */
1447 if (sessp
!= SESSION_NULL
)
1448 session_rele(sessp
);
1455 * The session leader is going away and is possibly going to revoke
1456 * the terminal, we can't change the process group when that is the
1459 if (ISSET(tp
->t_state
, TS_PGRPHUP
)) {
1466 sessp
->s_ttypgrpid
= pgrp
->pg_id
;
1468 /* SAFE: All callers drop the lock on return */
1470 if (oldpg
!= PGRP_NULL
)
1472 if (sessp
!= SESSION_NULL
)
1473 session_rele(sessp
);
1477 case TIOCSTAT
: /* simulate control-T */
1480 case TIOCSWINSZ
: /* set window size */
1481 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1482 sizeof (struct winsize
))) {
1483 tp
->t_winsize
= *(struct winsize
*)data
;
1484 /* SAFE: All callers drop the lock on return */
1486 tty_pgsignal(tp
, SIGWINCH
, 1);
1490 case TIOCSDRAINWAIT
:
1491 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1495 tp
->t_timeout
= *(int *)data
* hz
;
1496 wakeup(TSA_OCOMPLETE(tp
));
1497 wakeup(TSA_OLOWAT(tp
));
1499 case TIOCGDRAINWAIT
:
1500 *(int *)data
= tp
->t_timeout
/ hz
;
1503 error
= ttcompat(tp
, cmd
, data
, flag
, p
);
1514 * Locks: Assumes tp is locked on entry, remains locked on exit
1517 ttyselect(struct tty
*tp
, int rw
, void *wql
, proc_t p
)
1524 TTY_LOCK_OWNED(tp
); /* debug assert */
1528 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1532 retval
= ttnread(tp
);
1537 selrecord(p
, &tp
->t_rsel
, wql
);
1540 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1544 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1545 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1546 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
1550 selrecord(p
, &tp
->t_wsel
, wql
);
1558 * This is a wrapper for compatibility with the select vector used by
1559 * cdevsw. It relies on a proper xxxdevtotty routine.
1561 * Locks: Assumes tty_lock() is not held prior to calling.
1564 ttselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
1567 struct tty
*tp
= cdevsw
[major(dev
)].d_ttys
[minor(dev
)];
1570 rv
= ttyselect(tp
, rw
, wql
, p
);
1578 * Locks: Assumes tp is locked on entry, remains locked on exit
1580 __private_extern__
int
1581 ttnread(struct tty
*tp
)
1585 TTY_LOCK_OWNED(tp
); /* debug assert */
1587 if (ISSET(tp
->t_lflag
, PENDIN
))
1589 nread
= tp
->t_canq
.c_cc
;
1590 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1591 nread
+= tp
->t_rawq
.c_cc
;
1592 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1602 * Wait for output to drain.
1604 * Parameters: tp Tty on which to wait for output to drain
1606 * Returns: 0 Success
1607 * EIO t_timeout too small/expired
1608 * ttysleep:ERESTART Upper layer must redrive the call;
1609 * this is usually done by the Libc
1610 * stub in user space
1611 * ttysleep:EINTR Interrupted (usually a signal)
1613 * Notes: Called from proc_exit() and vproc_exit().
1615 * Locks: Assumes tp is locked on entry, remains locked on exit
1618 ttywait(struct tty
*tp
)
1622 TTY_LOCK_OWNED(tp
); /* debug assert */
1625 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1626 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1628 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1629 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1630 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1631 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1632 TTOPRI
| PCATCH
, "ttywai",
1635 if (error
== EWOULDBLOCK
)
1642 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1648 * Stop the underlying device driver.
1650 * Locks: Assumes tty_lock() is held prior to calling.
1653 ttystop(struct tty
*tp
, int rw
)
1655 TTY_LOCK_OWNED(tp
); /* debug assert */
1657 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1661 * Flush if successfully wait.
1663 * Locks: Assumes tty_lock() is held prior to calling.
1666 ttywflush(struct tty
*tp
)
1670 TTY_LOCK_OWNED(tp
); /* debug assert */
1672 if ((error
= ttywait(tp
)) == 0)
1673 ttyflush(tp
, FREAD
);
1678 * Flush tty read and/or write queues, notifying anyone waiting.
1680 * Locks: Assumes tty_lock() is held prior to calling.
1683 ttyflush(struct tty
*tp
, int rw
)
1685 TTY_LOCK_OWNED(tp
); /* debug assert */
1691 FLUSHQ(&tp
->t_outq
);
1692 CLR(tp
->t_state
, TS_TTSTOP
);
1696 FLUSHQ(&tp
->t_canq
);
1697 FLUSHQ(&tp
->t_rawq
);
1698 CLR(tp
->t_lflag
, PENDIN
);
1701 CLR(tp
->t_state
, TS_LOCAL
);
1703 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1705 FLUSHQ(&tp
->t_outq
);
1709 * Don't let leave any state that might clobber the
1710 * next line discipline (although we should do more
1711 * to send the START char). Not clearing the state
1712 * may have caused the "putc to a clist with no
1713 * reserved cblocks" panic/printf.
1715 CLR(tp
->t_state
, TS_TBLOCK
);
1717 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1718 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1720 * XXX wait a bit in the hope that the stop
1721 * character (if any) will go out. Waiting
1722 * isn't good since it allows races. This
1723 * will be fixed when the stop character is
1724 * put in a special queue. Don't bother with
1725 * the checks in ttywait() since the timeout
1728 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1729 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1732 * Don't try sending the stop character again.
1734 CLR(tp
->t_state
, TS_TBLOCK
);
1741 FLUSHQ(&tp
->t_outq
);
1747 * Copy in the default termios characters.
1749 * Locks: Assumes tty_lock() is held prior to calling.
1751 * Notes: No assertion; tp is not in scope.
1754 termioschars(struct termios
*t
)
1756 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1761 * Handle input high water. Send stop character for the IXOFF case. Turn
1762 * on our input flow control bit and propagate the changes to the driver.
1763 * XXX the stop character should be put in a special high priority queue.
1765 * Locks: Assumes tty_lock() is held for the call.
1768 ttyblock(struct tty
*tp
)
1770 TTY_LOCK_OWNED(tp
); /* debug assert */
1772 SET(tp
->t_state
, TS_TBLOCK
);
1773 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1774 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1775 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1781 * Handle input low water. Send start character for the IXOFF case. Turn
1782 * off our input flow control bit and propagate the changes to the driver.
1783 * XXX the start character should be put in a special high priority queue.
1785 * Locks: Assumes tty_lock() is held for the call.
1788 ttyunblock(struct tty
*tp
)
1790 TTY_LOCK_OWNED(tp
); /* debug assert */
1792 CLR(tp
->t_state
, TS_TBLOCK
);
1793 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1794 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1795 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1805 * Parameters: tp tty on which to start output
1807 * Returns: 0 Success
1809 * Locks: Assumes tty_lock() is held for the call.
1811 * Notes: This function might as well be void; it always returns success
1813 * Called from ttioctl_locked(), LDISC routines, and
1814 * ttycheckoutq(), ttyblock(), ttyunblock(), and tputchar()
1817 ttstart(struct tty
*tp
)
1819 TTY_LOCK_OWNED(tp
); /* debug assert */
1821 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1831 * "close" a line discipline
1833 * Locks: Assumes tty_lock() is held prior to calling.
1836 ttylclose(struct tty
*tp
, int flag
)
1838 TTY_LOCK_OWNED(tp
); /* debug assert */
1840 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1841 ttyflush(tp
, FREAD
| FWRITE
);
1850 * Handle modem control transition on a tty.
1851 * Flag indicates new state of carrier.
1852 * Returns 0 if the line should be turned off, otherwise 1.
1854 * Locks: Assumes tty_lock() is held prior to calling.
1857 ttymodem(struct tty
*tp
, int flag
)
1859 int rval
= 1; /* default return value */
1861 TTY_LOCK_OWNED(tp
); /* debug assert */
1863 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1865 * MDMBUF: do flow control according to carrier flag
1866 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1867 * works if IXON and IXANY are clear.
1870 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1871 CLR(tp
->t_state
, TS_TTSTOP
);
1873 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1874 SET(tp
->t_state
, TS_CAR_OFLOW
);
1875 SET(tp
->t_state
, TS_TTSTOP
);
1878 } else if (flag
== 0) {
1882 CLR(tp
->t_state
, TS_CARR_ON
);
1883 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1884 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1885 SET(tp
->t_state
, TS_ZOMBIE
);
1886 CLR(tp
->t_state
, TS_CONNECTED
);
1887 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1888 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1889 ttyflush(tp
, FREAD
| FWRITE
);
1897 SET(tp
->t_state
, TS_CARR_ON
);
1898 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1899 SET(tp
->t_state
, TS_CONNECTED
);
1900 wakeup(TSA_CARR_ON(tp
));
1911 * Reinput pending characters after state switch
1914 * Locks: Assumes tty_lock() is held for the call.
1917 ttypend(struct tty
*tp
)
1922 TTY_LOCK_OWNED(tp
); /* debug assert */
1924 CLR(tp
->t_lflag
, PENDIN
);
1925 SET(tp
->t_state
, TS_TYPEN
);
1927 tp
->t_rawq
.c_cc
= 0;
1928 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= NULL
;
1929 while ((c
= getc(&tq
)) >= 0)
1931 CLR(tp
->t_state
, TS_TYPEN
);
1938 * Process a read call on a tty device.
1940 * Locks: Assumes tty_lock() is held prior to calling.
1943 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1948 cc_t
*cc
= tp
->t_cc
;
1949 proc_t p
= current_proc();
1950 int first
, error
= 0;
1951 int has_etime
= 0, last_cc
= 0;
1952 long slp
= 0; /* XXX this should be renamed `timo'. */
1956 TTY_LOCK_OWNED(tp
); /* debug assert */
1958 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1961 lflag
= tp
->t_lflag
;
1963 * take pending input first
1965 if (ISSET(lflag
, PENDIN
)) {
1967 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1971 * Signal the process if it's in the background.
1973 if (isbackground(p
, tp
)) {
1974 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1975 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1976 p
->p_lflag
& P_LPPWAIT
) {
1981 if (pg
== PGRP_NULL
) {
1985 if (pg
->pg_jobc
== 0) {
1986 /* SAFE: All callers drop the lock on return */
1993 /* SAFE: All callers drop the lock on return */
1995 pgsignal(pg
, SIGTTIN
, 1);
2000 * We signalled ourself, so we need to act as if we
2001 * have been "interrupted" from a "sleep" to act on
2002 * the signal. If it's a signal that stops the
2003 * process, that's handled in the signal sending code.
2009 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2010 /* EOF - returning 0 */
2015 * If canonical, use the canonical queue,
2016 * else use the raw queue.
2018 * (should get rid of clists...)
2020 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
2022 if (flag
& IO_NDELAY
) {
2025 if (ISSET(lflag
, ICANON
) || cc
[VMIN
] != 0) {
2026 error
= EWOULDBLOCK
;
2028 /* else polling - returning 0 */
2031 if (!ISSET(lflag
, ICANON
)) {
2034 struct timeval timecopy
;
2035 struct timeval etime
= {0, 0}; /* protected by !has_etime */
2038 * Check each of the four combinations.
2039 * (m > 0 && t == 0) is the normal read case.
2040 * It should be fairly efficient, so we check that and its
2041 * companion case (m == 0 && t == 0) first.
2042 * For the other two cases, we compute the target sleep time
2051 /* m, t and qp->c_cc are all 0. 0 is enough input. */
2054 t
*= 100000; /* time in us */
2055 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
2056 ((t1).tv_usec - (t2).tv_usec))
2062 microuptime(&timecopy
);
2064 /* first character, start timer */
2067 etime
.tv_sec
= t
/ 1000000;
2068 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2069 timeradd(&etime
, &timecopy
, &etime
);
2072 } else if (qp
->c_cc
> last_cc
) {
2073 /* got a character, restart timer */
2075 etime
.tv_sec
= t
/ 1000000;
2076 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2077 timeradd(&etime
, &timecopy
, &etime
);
2081 /* nothing, check expiration */
2082 if (timercmp(&etime
, &timecopy
, <=))
2085 slp
= diff(etime
, timecopy
);
2088 } else { /* m == 0 */
2091 microuptime(&timecopy
);
2095 etime
.tv_sec
= t
/ 1000000;
2096 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2097 timeradd(&etime
, &timecopy
, &etime
);
2101 if (timercmp(&etime
, &timecopy
, <=)) {
2102 /* Timed out, but 0 is enough input. */
2105 slp
= diff(etime
, timecopy
);
2110 * Rounding down may make us wake up just short
2111 * of the target, so we round up.
2112 * The formula is ceiling(slp * hz/1000000).
2113 * 32-bit arithmetic is enough for hz < 169.
2114 * XXX see hzto() for how to avoid overflow if hz
2115 * is large (divide by `tick' and/or arrange to
2116 * use hzto() if hz is large).
2118 slp
= (long) (((u_int32_t
)slp
* hz
) + 999999) / 1000000;
2121 if (qp
->c_cc
<= 0) {
2124 * There is no input, or not enough input and we can block.
2126 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
2127 ISSET(tp
->t_state
, TS_CONNECTED
) ?
2128 "ttyin" : "ttyhup", (int)slp
);
2129 if (error
== EWOULDBLOCK
)
2135 * XXX what happens if another process eats some input
2136 * while we are asleep (not just here)? It would be
2137 * safest to detect changes and reset our state variables
2138 * (has_stime and last_cc).
2145 * Input present, check for input mapping and processing.
2148 if (ISSET(lflag
, ICANON
)
2149 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
2155 icc
= MIN(uio_resid(uio
), IBUFSIZ
);
2156 icc
= q_to_b(qp
, (u_char
*)ibuf
, icc
);
2162 error
= uiomove(ibuf
, icc
, uio
);
2164 * XXX if there was an error then we should ungetc() the
2165 * unmoved chars and reduce icc here.
2169 if (uio_resid(uio
) == 0)
2183 * delayed suspend (^Y)
2185 if (CCEQ(cc
[VDSUSP
], c
) &&
2186 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
2188 * SAFE: All callers drop the lock on return and
2189 * SAFE: current thread will not change out from
2190 * SAFE: under us in the "goto loop" case.
2193 tty_pgsignal(tp
, SIGTSTP
, 1);
2196 error
= ttysleep(tp
, &ttread
, TTIPRI
| PCATCH
,
2205 * Interpret EOF only in canonical mode.
2207 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
2210 * Give user character.
2212 error
= ureadc(c
, uio
);
2214 /* XXX should ungetc(c, qp). */
2216 if (uio_resid(uio
) == 0)
2219 * In canonical mode check for a "break character"
2220 * marking the end of a "line of input".
2222 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
2229 * Look to unblock input now that (presumably)
2230 * the input queue has gone down.
2232 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
2233 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
2242 * Check the output queue on tp for space for a kernel message (from uprintf
2243 * or tprintf). Allow some space over the normal hiwater mark so we don't
2244 * lose messages due to normal flow control, but don't let the tty run amok.
2245 * Sleeps here are not interruptible, but we return prematurely if new signals
2248 * Locks: Assumes tty_lock() is held before calling
2250 * Notes: This function is called from tprintf() in subr_prf.c
2253 ttycheckoutq(struct tty
*tp
, int wait
)
2259 TTY_LOCK_OWNED(tp
); /* debug assert */
2261 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2263 hiwat
= tp
->t_hiwat
;
2264 oldsig
= wait
? ut
->uu_siglist
: 0;
2265 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
2266 while (tp
->t_outq
.c_cc
> hiwat
) {
2268 if (tp
->t_outq
.c_cc
<= hiwat
)
2270 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
2273 SET(tp
->t_state
, TS_SO_OLOWAT
);
2274 ttysleep(tp
, TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
2283 * Process a write call on a tty device.
2285 * Locks: Assumes tty_lock() is held prior to calling.
2288 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
2293 int i
, hiwat
, error
;
2299 TTY_LOCK_OWNED(tp
); /* debug assert */
2301 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2302 hiwat
= tp
->t_hiwat
;
2303 count
= uio_resid(uio
);
2307 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2308 if (uio_resid(uio
) == count
)
2312 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2313 if (flag
& IO_NDELAY
) {
2314 error
= EWOULDBLOCK
;
2317 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2324 * Signal the process if it's in the background.
2327 if (isbackground(p
, tp
) &&
2328 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& P_LPPWAIT
) == 0 &&
2329 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2330 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2333 if (pg
== PGRP_NULL
) {
2337 if (pg
->pg_jobc
== 0) {
2338 /* SAFE: All callers drop the lock on return */
2345 /* SAFE: All callers drop the lock on return */
2347 pgsignal(pg
, SIGTTOU
, 1);
2351 * We signalled ourself, so we need to act as if we
2352 * have been "interrupted" from a "sleep" to act on
2353 * the signal. If it's a signal that stops the
2354 * process, that's handled in the signal sending code.
2360 * Process the user's data in at most OBUFSIZ chunks. Perform any
2361 * output translation. Keep track of high water mark, sleep on
2362 * overflow awaiting device aid in acquiring new space.
2364 while (uio_resid(uio
) > 0 || cc
> 0) {
2365 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2366 uio_setresid(uio
, 0);
2369 if (tp
->t_outq
.c_cc
> hiwat
)
2372 * Grab a hunk of data from the user, unless we have some
2373 * leftover from last time.
2376 cc
= MIN(uio_resid(uio
), OBUFSIZ
);
2378 error
= uiomove(cp
, cc
, uio
);
2385 * If nothing fancy need be done, grab those characters we
2386 * can handle without any of ttyoutput's processing and
2387 * just transfer them to the output q. For those chars
2388 * which require special processing (as indicated by the
2389 * bits in char_type), call ttyoutput. After processing
2390 * a hunk of data, look for FLUSHO so ^O's will take effect
2394 if (!ISSET(tp
->t_oflag
, OPOST
))
2397 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2398 char_type
, CCLASSMASK
);
2400 * If ce is zero, then we're processing
2401 * a special character through ttyoutput.
2405 if (ttyoutput(*cp
, tp
) >= 0) {
2411 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2412 tp
->t_outq
.c_cc
> hiwat
)
2418 * A bunch of normal characters have been found.
2419 * Transfer them en masse to the output queue and
2420 * continue processing at the top of the loop.
2421 * If there are any further characters in this
2422 * <= OBUFSIZ chunk, the first should be a character
2423 * requiring special handling by ttyoutput.
2426 i
= b_to_q((u_char
*)cp
, ce
, &tp
->t_outq
);
2437 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2438 tp
->t_outq
.c_cc
> hiwat
)
2445 * If cc is nonzero, we leave the uio structure inconsistent, as the
2446 * offset and iov pointers have moved forward, but it doesn't matter
2447 * (the call will either return short or restart with a new uio).
2449 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2455 * Since we are using ring buffers, if we can't insert any more into
2456 * the output queue, we can assume the ring is full and that someone
2457 * forgot to set the high water mark correctly. We set it and then
2458 * proceed as normal.
2460 hiwat
= tp
->t_outq
.c_cc
- 1;
2465 * This can only occur if FLUSHO is set in t_lflag,
2466 * or if ttstart/oproc is synchronous (or very fast).
2468 if (tp
->t_outq
.c_cc
<= hiwat
) {
2471 if (flag
& IO_NDELAY
) {
2472 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2473 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2475 SET(tp
->t_state
, TS_SO_OLOWAT
);
2476 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2478 if (error
== EWOULDBLOCK
)
2487 * Rubout one character from the rawq of tp
2488 * as cleanly as possible.
2490 * Locks: Assumes tty_lock() is held prior to calling.
2493 ttyrub(int c
, struct tty
*tp
)
2499 TTY_LOCK_OWNED(tp
); /* debug assert */
2501 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2503 CLR(tp
->t_lflag
, FLUSHO
);
2504 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2505 if (tp
->t_rocount
== 0) {
2507 * Messed up by ttwrite; retype
2512 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2515 CLR(c
, ~TTY_CHARMASK
);
2516 switch (CCLASS(c
)) {
2518 if(!(ISSET(tp
->t_iflag
, IUTF8
) && CCONT(c
))) {
2527 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2531 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2535 savecol
= tp
->t_column
;
2536 SET(tp
->t_state
, TS_CNTTB
);
2537 SET(tp
->t_lflag
, FLUSHO
);
2538 tp
->t_column
= tp
->t_rocol
;
2539 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2540 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2542 CLR(tp
->t_lflag
, FLUSHO
);
2543 CLR(tp
->t_state
, TS_CNTTB
);
2545 /* savecol will now be length of the tab. */
2546 savecol
-= tp
->t_column
;
2547 tp
->t_column
+= savecol
;
2549 savecol
= 8; /* overflow fixup */
2550 while (--savecol
>= 0)
2551 (void)ttyoutput('\b', tp
);
2554 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2555 printf(PANICSTR
, c
, CCLASS(c
));
2557 panic(PANICSTR
, c
, CCLASS(c
));
2561 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2562 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2563 SET(tp
->t_state
, TS_ERASE
);
2564 (void)ttyoutput('\\', tp
);
2568 ttyecho(tp
->t_cc
[VERASE
], tp
);
2574 * Back over count characters, erasing them.
2576 * Locks: Assumes tty_lock() is held prior to calling.
2579 ttyrubo(struct tty
*tp
, int count
)
2581 TTY_LOCK_OWNED(tp
); /* debug assert */
2583 while (count
-- > 0) {
2584 (void)ttyoutput('\b', tp
);
2585 (void)ttyoutput(' ', tp
);
2586 (void)ttyoutput('\b', tp
);
2593 * Reprint the rawq line. Note, it is assumed that c_cc has already
2596 * Locks: Assumes tty_lock() is held prior to calling.
2599 ttyretype(struct tty
*tp
)
2604 TTY_LOCK_OWNED(tp
); /* debug assert */
2606 /* Echo the reprint character. */
2607 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2608 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2610 (void)ttyoutput('\n', tp
);
2614 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2615 * BIT OF FIRST CHAR.
2617 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2619 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2621 CLR(tp
->t_state
, TS_ERASE
);
2623 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2629 * Echo a typed character to the terminal.
2631 * Locks: Assumes tty_lock() is held prior to calling.
2634 ttyecho(int c
, struct tty
*tp
)
2636 TTY_LOCK_OWNED(tp
); /* debug assert */
2638 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2639 CLR(tp
->t_lflag
, FLUSHO
);
2640 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2641 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2642 ISSET(tp
->t_lflag
, EXTPROC
))
2644 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2645 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2646 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2647 (void)ttyoutput('^', tp
);
2648 CLR(c
, ~TTY_CHARMASK
);
2654 (void)ttyoutput(c
, tp
);
2659 * Wake up any readers on a tty.
2661 * Locks: Assumes tty_lock() is held for the call.
2664 ttwakeup(struct tty
*tp
)
2666 TTY_LOCK_OWNED(tp
); /* debug assert */
2668 selwakeup(&tp
->t_rsel
);
2669 KNOTE(&tp
->t_rsel
.si_note
, 1);
2670 if (ISSET(tp
->t_state
, TS_ASYNC
)) {
2672 * XXX: Callers may not revalidate it the tty is closed
2673 * XXX: out from under them by another thread, but we do
2674 * XXX: not support queued signals. This should be safe,
2675 * XXX: since the process we intend to wakeup is in the
2676 * XXX: process group, and will wake up because of the
2677 * XXX: signal anyway.
2680 tty_pgsignal(tp
, SIGIO
, 1);
2683 wakeup(TSA_HUP_OR_INPUT(tp
));
2690 * Wake up any writers on a tty.
2692 * Locks: Assumes tty_lock() is held prior to calling.
2695 ttwwakeup(struct tty
*tp
)
2697 TTY_LOCK_OWNED(tp
); /* debug assert */
2699 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2700 selwakeup(&tp
->t_wsel
);
2701 KNOTE(&tp
->t_wsel
.si_note
, 1);
2703 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2704 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2705 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2706 wakeup(TSA_OCOMPLETE(tp
));
2708 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2709 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2710 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2711 wakeup(TSA_OLOWAT(tp
));
2717 * Look up a code for a specified speed in a conversion table;
2718 * used by drivers to map software speed values to hardware parameters.
2720 * Notes: No locks are assumed for this function; it does not
2721 * directly access struct tty.
2724 ttspeedtab(int speed
, struct speedtab
*table
)
2726 for ( ; table
->sp_speed
!= -1; table
++)
2727 if (table
->sp_speed
== speed
)
2728 return (table
->sp_code
);
2734 * Set tty hi and low water marks.
2736 * Try to arrange the dynamics so there's about one second
2737 * from hi to low water.
2739 * Locks: Assumes tty_lock() is held prior to calling.
2742 ttsetwater(struct tty
*tp
)
2747 TTY_LOCK_OWNED(tp
); /* debug assert */
2749 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2751 cps
= tp
->t_ospeed
/ 10;
2752 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2754 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2755 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2759 /* ttyinfo has been converted to the MACH kernel */
2760 #include <mach/thread_info.h>
2762 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2763 extern kern_return_t
thread_info_internal(thread_t thread
,
2764 thread_flavor_t flavor
,
2765 thread_info_t thread_info_out
,
2766 mach_msg_type_number_t
*thread_info_count
);
2770 * Report on state of foreground process group.
2772 * Locks: Assumes tty_lock() is held prior to calling.
2775 ttyinfo_locked(struct tty
*tp
)
2784 struct timeval utime
;
2785 struct timeval stime
;
2786 thread_basic_info_data_t basic_info
;
2787 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2790 TTY_LOCK_OWNED(tp
); /* debug assert */
2792 if (ttycheckoutq(tp
,0) == 0)
2795 /* Print load average. */
2796 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2797 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2800 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2801 * that pending input will be retyped on BS.
2803 if (tp
->t_session
== NULL
) {
2804 ttyprintf(tp
, "not a controlling terminal\n");
2808 if (tp
->t_pgrp
== NULL
) {
2809 ttyprintf(tp
, "no foreground process group\n");
2813 /* first process in process group */
2814 /* XXX is there a need for pgrp lock ? */
2815 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2816 ttyprintf(tp
, "empty foreground process group\n");
2822 * Pick the most interesting process and copy some of its
2823 * state for printing later.
2827 /* the proc_compare is non blocking fn, no need to use iterator */
2828 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2829 if (proc_compare(pick
, p
)) {
2833 pickpid
= pick
->p_pid
;
2837 /* SAFE: All callers drop the lock on return */
2842 pick
= proc_find(pickpid
);
2843 if (pick
== PROC_NULL
)
2846 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2847 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2848 (thread
= vfs_context_thread(&uthread
->uu_context
)) == NULL
||
2849 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2850 ttyprintf(tp
, "foreground process without thread\n");
2856 switch(basic_info
.run_state
) {
2857 case TH_STATE_RUNNING
:
2860 case TH_STATE_STOPPED
:
2863 case TH_STATE_WAITING
:
2866 case TH_STATE_UNINTERRUPTIBLE
:
2867 state
= "uninterruptible";
2869 case TH_STATE_HALTED
:
2876 calcru(pick
, &utime
, &stime
, NULL
);
2879 /* Print command, pid, state, utime, and stime */
2880 ttyprintf(tp
, " cmd: %s %d %s %ld.%02du %ld.%02ds\n",
2884 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2885 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2891 * Returns 1 if p2 is "better" than p1
2893 * The algorithm for picking the "interesting" process is thus:
2895 * 1) Only foreground processes are eligible - implied.
2896 * 2) Runnable processes are favored over anything else. The runner
2897 * with the highest cpu utilization is picked (p_estcpu). Ties are
2898 * broken by picking the highest pid.
2899 * 3) The sleeper with the shortest sleep time is next.
2900 * 4) Further ties are broken by picking the highest pid.
2902 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2903 #define TESTAB(a, b) ((a)<<1 | (b))
2909 * Locks: pgrp_lock(p2) held on call to this function
2910 * tty_lock(tp) for p2's tty, for which p2 is the foreground
2911 * process, held on call to this function
2914 proc_compare(proc_t p1
, proc_t p2
)
2916 /* NOTE THIS FN needs to be NON BLOCKING */
2921 * see if at least one of them is runnable
2923 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2930 * tie - favor one with highest recent cpu utilization
2932 #ifdef _PROC_HAS_SCHEDINFO_
2933 /* Without the support the fields are always zero */
2934 if (p2
->p_estcpu
> p1
->p_estcpu
)
2936 if (p1
->p_estcpu
> p2
->p_estcpu
)
2938 #endif /* _PROC_HAS_SCHEDINFO_ */
2939 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2944 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2950 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2953 * pick the one with the smallest sleep time
2955 #ifdef _PROC_HAS_SCHEDINFO_
2956 /* Without the support the fields are always zero */
2957 if (p2
->p_slptime
> p1
->p_slptime
)
2959 if (p1
->p_slptime
> p2
->p_slptime
)
2961 #endif /* _PROC_HAS_SCHEDINFO_ */
2962 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2967 * Output char to tty; console putchar style.
2969 * Locks: Assumes tty_lock() is held prior to calling.
2971 * Notes: Only ever called from putchar() in subr_prf.c
2974 tputchar(int c
, struct tty
*tp
)
2976 TTY_LOCK_OWNED(tp
); /* debug assert */
2978 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2982 (void)ttyoutput('\r', tp
);
2983 (void)ttyoutput(c
, tp
);
2992 * Sleep on a wait channel waiting for an interrupt or a condition to come
2993 * true so that we are woken up.
2995 * Parameters: tp Tty going to sleep
2996 * chan The sleep channel (usually an address
2997 * of a structure member)
2998 * pri priority and flags
2999 * wmesg Wait message; shows up in debugger,
3000 * should show up in "ps", but doesn't
3001 * timo Timeout for the sleep
3003 * Returns: 0 Condition came true
3004 * ERESTART Upper layer must redrive the call;
3005 * this is usually done by the Libc
3006 * stub in user space
3007 * msleep0:EINTR Interrupted (usually a signal)
3008 * msleep0:ERESTART Interrupted (usually a masked signal)
3009 * msleep0:EWOULDBLOCK Timeout (timo) already expired
3011 * Locks: Assumes tty_lock() is held prior to calling.
3013 * Sleep on chan, returning ERESTART if tty changed while we napped and
3014 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by msleep0. If
3015 * the tty is revoked, restarting a pending call will redo validation done
3016 * at the start of the call.
3019 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
3027 /* Use of msleep0() avoids conversion timo/timespec/timo */
3028 error
= msleep0(chan
, &tp
->t_lock
, pri
, wmesg
, timo
, (int (*)(int))0);
3031 return (tp
->t_gen
== gen
? 0 : ERESTART
);
3036 * Allocate a tty structure and its associated buffers.
3040 * Returns: !NULL Address of new struct tty
3041 * NULL Error ("ENOMEM")
3043 * Locks: The tty_lock() of the returned tty is not held when it
3051 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
3053 /* XXX: default to TTYCLSIZE(1024) chars for now */
3054 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
3055 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
3056 /* output queue doesn't need quoting */
3057 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
3058 lck_mtx_init(&tp
->t_lock
, tty_lck_grp
, tty_lck_attr
);
3059 klist_init(&tp
->t_rsel
.si_note
);
3060 klist_init(&tp
->t_wsel
.si_note
);
3067 * Increment the reference count on a tty.
3070 ttyhold(struct tty
*tp
)
3077 * Drops a reference count on a tty structure; if the reference count reaches
3078 * zero, then also frees the structure and associated buffers.
3081 ttyfree(struct tty
*tp
)
3083 TTY_LOCK_NOTOWNED(tp
);
3086 if (--tp
->t_refcnt
== 0) {
3089 } else if (tp
->t_refcnt
< 0) {
3090 panic("%s: freeing free tty %p", __func__
, tp
);
3096 * Deallocate a tty structure and its buffers.
3098 * Locks: The tty_lock() is assumed to not be held at the time of
3099 * the free; this function destroys the mutex.
3102 ttydeallocate(struct tty
*tp
)
3104 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
3107 if (!(SLIST_EMPTY(&tp
->t_rsel
.si_note
) && SLIST_EMPTY(&tp
->t_wsel
.si_note
))) {
3108 panic("knotes hooked into a tty when the tty is freed.\n");
3112 clfree(&tp
->t_rawq
);
3113 clfree(&tp
->t_canq
);
3114 clfree(&tp
->t_outq
);
3115 lck_mtx_destroy(&tp
->t_lock
, tty_lck_grp
);
3121 * Locks: Assumes tty_lock() is held prior to calling.
3124 isbackground(proc_t p
, struct tty
*tp
)
3128 return (tp
->t_session
!= NULL
&& p
->p_pgrp
!= NULL
&& (p
->p_pgrp
!= tp
->t_pgrp
) && isctty_sp(p
, tp
, p
->p_pgrp
->pg_session
));
3132 isctty(proc_t p
, struct tty
*tp
)
3135 struct session
* sessp
;
3137 sessp
= proc_session(p
);
3138 retval
= (sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3139 session_rele(sessp
);
3144 isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
)
3146 return(sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);