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 static 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 proc_t p
= current_proc();
345 struct pgrp
*pg
, *oldpg
;
346 struct session
*sessp
, *oldsess
;
349 TTY_LOCK_OWNED(tp
); /* debug assert */
353 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
354 SET(tp
->t_state
, TS_ISOPEN
);
355 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
356 SET(tp
->t_state
, TS_CONNECTED
); }
357 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
361 sessp
= proc_session(p
);
364 * First tty open affter setsid() call makes this tty its controlling
365 * tty, if the tty does not already have a session associated with it.
367 if (SESS_LEADER(p
, sessp
) && /* the process is the session leader */
368 sessp
->s_ttyvp
== NULL
&& /* but has no controlling tty */
369 tp
->t_session
== NULL
) { /* and tty not controlling */
371 if ((sessp
->s_flags
& S_NOCTTY
) == 0) { /* and no O_NOCTTY */
372 oldtp
= sessp
->s_ttyp
;
375 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
376 session_unlock(sessp
);
379 oldsess
= tp
->t_session
;
380 if (oldsess
!= SESSION_NULL
)
381 oldsess
->s_ttypgrpid
= NO_PID
;
382 tp
->t_session
= sessp
;
384 sessp
->s_ttypgrpid
= pg
->pg_id
;
386 /* SAFE: All callers drop the lock on return */
388 if (oldpg
!= PGRP_NULL
)
390 if (oldsess
!= SESSION_NULL
)
391 session_rele(oldsess
);
397 session_unlock(sessp
);
400 /* SAFE: All callers drop the lock on return */
402 if (sessp
!= SESSION_NULL
)
410 /* XXX may be an error code */
417 * Handle close() on a tty line: flush and set to initial state,
418 * bumping generation number so that pending read/write calls
419 * can detect recycling of the tty.
420 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
421 * and l_close() should have flushed, but we repeat the spltty() and
422 * the flush in case there are buggy callers.
424 * Locks: Assumes tty_lock() is held prior to calling.
427 ttyclose(struct tty
*tp
)
430 struct session
* oldsessp
;
432 TTY_LOCK_OWNED(tp
); /* debug assert */
439 * Closing current console tty; disable printing of console
440 * messages at bottom-level driver.
442 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
443 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
446 ttyflush(tp
, FREAD
| FWRITE
);
449 tp
->t_line
= TTYDISC
;
452 oldsessp
= tp
->t_session
;
454 tp
->t_session
= NULL
;
455 if (oldsessp
!= SESSION_NULL
)
456 oldsessp
->s_ttypgrpid
= NO_PID
;
458 /* drop the reference on prev session and pgrp */
459 /* SAFE: All callers drop the lock on return */
461 if (oldsessp
!= SESSION_NULL
)
462 session_rele(oldsessp
);
463 if (oldpg
!= PGRP_NULL
)
467 selthreadclear(&tp
->t_wsel
);
468 selthreadclear(&tp
->t_rsel
);
472 #define FLUSHQ(q) { \
474 ndflush(q, (q)->c_cc); \
477 /* Is 'c' a line delimiter ("break" character)? */
478 #define TTBREAKC(c, lflag) \
479 ((c) == '\n' || (((c) == cc[VEOF] || \
480 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
481 (c) != _POSIX_VDISABLE))
486 * Process input of a single character received on a tty.
488 * Parameters: c The character received
489 * tp The tty on which it was received
493 * Locks: Assumes tty_lock() is held prior to calling.
496 ttyinput(int c
, struct tty
*tp
)
498 tcflag_t iflag
, lflag
;
501 int retval
= 0; /* default return value */
503 TTY_LOCK_OWNED(tp
); /* debug assert */
506 * If input is pending take it first.
509 if (ISSET(lflag
, PENDIN
))
514 if (ISSET(lflag
, ICANON
)) {
524 * Block further input iff:
525 * current input > threshold AND input is available to user program
526 * AND input flow control is enabled and not yet invoked.
527 * The 3 is slop for PARMRK.
530 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
531 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
532 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
533 !ISSET(tp
->t_state
, TS_TBLOCK
))
536 /* Handle exceptional conditions (break, parity, framing). */
538 err
= (ISSET(c
, TTY_ERRORMASK
));
540 CLR(c
, TTY_ERRORMASK
);
541 if (ISSET(err
, TTY_BI
)) {
542 if (ISSET(iflag
, IGNBRK
)) {
545 if (ISSET(iflag
, BRKINT
)) {
546 ttyflush(tp
, FREAD
| FWRITE
);
547 /* SAFE: All callers drop the lock on return */
549 tty_pgsignal(tp
, SIGINT
, 1);
553 if (ISSET(iflag
, PARMRK
))
555 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
556 || ISSET(err
, TTY_FE
)) {
557 if (ISSET(iflag
, IGNPAR
)) {
560 else if (ISSET(iflag
, PARMRK
)) {
562 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
565 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
566 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
567 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
574 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
576 if (!ISSET(lflag
, EXTPROC
)) {
578 * Check for literal nexting very first
580 if (ISSET(tp
->t_state
, TS_LNCH
)) {
582 CLR(tp
->t_state
, TS_LNCH
);
585 * Scan for special characters. This code
586 * is really just a big case statement with
587 * non-constant cases. The bottom of the
588 * case statement is labeled ``endcase'', so goto
589 * it after a case match, or similar.
593 * Control chars which aren't controlled
594 * by ICANON, ISIG, or IXON.
596 if (ISSET(lflag
, IEXTEN
)) {
597 if (CCEQ(cc
[VLNEXT
], c
)) {
598 if (ISSET(lflag
, ECHO
)) {
599 if (ISSET(lflag
, ECHOE
)) {
600 (void)ttyoutput('^', tp
);
601 (void)ttyoutput('\b', tp
);
605 SET(tp
->t_state
, TS_LNCH
);
608 if (CCEQ(cc
[VDISCARD
], c
)) {
609 if (ISSET(lflag
, FLUSHO
))
610 CLR(tp
->t_lflag
, FLUSHO
);
612 ttyflush(tp
, FWRITE
);
614 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
616 SET(tp
->t_lflag
, FLUSHO
);
624 if (ISSET(lflag
, ISIG
)) {
625 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
626 if (!ISSET(lflag
, NOFLSH
))
627 ttyflush(tp
, FREAD
| FWRITE
);
630 * SAFE: All callers drop the lock on return;
631 * SAFE: if we lose a threaded race on change
632 * SAFE: of the interrupt character, we could
633 * SAFE: have lost that race anyway due to the
634 * SAFE: scheduler executing threads in
635 * SAFE: priority order rather than "last
636 * SAFE: active thread" order (FEATURE).
640 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
644 if (CCEQ(cc
[VSUSP
], c
)) {
645 if (!ISSET(lflag
, NOFLSH
))
648 /* SAFE: All callers drop the lock on return */
650 tty_pgsignal(tp
, SIGTSTP
, 1);
656 * Handle start/stop characters.
658 if (ISSET(iflag
, IXON
)) {
659 if (CCEQ(cc
[VSTOP
], c
)) {
660 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
661 SET(tp
->t_state
, TS_TTSTOP
);
665 if (!CCEQ(cc
[VSTART
], c
)) {
669 * if VSTART == VSTOP then toggle
673 if (CCEQ(cc
[VSTART
], c
))
677 * IGNCR, ICRNL, & INLCR
680 if (ISSET(iflag
, IGNCR
)) {
683 else if (ISSET(iflag
, ICRNL
))
685 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
688 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
690 * From here on down canonical mode character
691 * processing takes place.
696 if (CCEQ(cc
[VERASE
], c
)) {
697 if (tp
->t_rawq
.c_cc
) {
698 if (ISSET(iflag
, IUTF8
)) {
700 ttyrub((c
= unputc(&tp
->t_rawq
)), tp
);
701 } while(tp
->t_rawq
.c_cc
&& CCONT(c
));
703 ttyrub(unputc(&tp
->t_rawq
), tp
);
711 if (CCEQ(cc
[VKILL
], c
)) {
712 if (ISSET(lflag
, ECHOKE
) &&
713 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
714 !ISSET(lflag
, ECHOPRT
))
715 while (tp
->t_rawq
.c_cc
)
716 ttyrub(unputc(&tp
->t_rawq
), tp
);
719 if (ISSET(lflag
, ECHOK
) ||
720 ISSET(lflag
, ECHOKE
))
725 CLR(tp
->t_state
, TS_LOCAL
);
731 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
737 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
742 * erase last char of word and remember the
743 * next chars type (for ALTWERASE)
746 c
= unputc(&tp
->t_rawq
);
749 if (c
== ' ' || c
== '\t') {
750 (void)putc(c
, &tp
->t_rawq
);
759 c
= unputc(&tp
->t_rawq
);
762 } while (c
!= ' ' && c
!= '\t' &&
763 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
764 (void)putc(c
, &tp
->t_rawq
);
770 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
775 * ^T - kernel info and generate SIGINFO
777 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
778 if (ISSET(lflag
, ISIG
)) {
779 /* SAFE: All callers drop the lock on return */
781 tty_pgsignal(tp
, SIGINFO
, 1);
784 if (!ISSET(lflag
, NOKERNINFO
))
790 * Check for input buffer overflow
792 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
794 if (ISSET(iflag
, IMAXBEL
)) {
795 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
796 (void)ttyoutput(CTRL('g'), tp
);
801 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
802 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
803 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
806 * Put data char in q for user and
807 * wakeup on seeing a line delimiter.
809 if (putc(c
, &tp
->t_rawq
) >= 0) {
810 if (!ISSET(lflag
, ICANON
)) {
815 if (TTBREAKC(c
, lflag
)) {
817 catq(&tp
->t_rawq
, &tp
->t_canq
);
819 } else if (tp
->t_rocount
++ == 0)
820 tp
->t_rocol
= tp
->t_column
;
821 if (ISSET(tp
->t_state
, TS_ERASE
)) {
823 * end of prterase \.../
825 CLR(tp
->t_state
, TS_ERASE
);
826 (void)ttyoutput('/', tp
);
830 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
832 * Place the cursor over the '^' of the ^D.
834 i
= min(2, tp
->t_column
- i
);
836 (void)ttyoutput('\b', tp
);
844 * IXANY means allow any character to restart output.
846 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
847 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
852 CLR(tp
->t_lflag
, FLUSHO
);
853 CLR(tp
->t_state
, TS_TTSTOP
);
856 /* Start the output */
857 retval
= ttstart(tp
);
867 * Output a single character on a tty, doing output processing
868 * as needed (expanding tabs, newline processing, etc.).
870 * Parameters: c The character to output
871 * tp The tty on which to output on the tty
873 * Returns: < 0 Success
874 * >= 0 Character to resend (failure)
876 * Locks: Assumes tp is locked on entry, remains locked on exit
878 * Notes: Must be recursive.
881 ttyoutput(int c
, struct tty
*tp
)
886 TTY_LOCK_OWNED(tp
); /* debug assert */
889 if (!ISSET(oflag
, OPOST
)) {
890 if (ISSET(tp
->t_lflag
, FLUSHO
))
892 if (putc(c
, &tp
->t_outq
))
899 * Do tab expansion if OXTABS is set. Special case if we external
900 * processing, we don't do the tab expansion because we'll probably
901 * get it wrong. If tab expansion needs to be done, let it happen
904 CLR(c
, ~TTY_CHARMASK
);
906 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
907 col
= c
= 8 - (tp
->t_column
& 7);
908 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
909 c
-= b_to_q((const u_char
*)" ", c
, &tp
->t_outq
);
914 return (c
== col
? -1 : '\t');
916 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
920 * Newline translation: if ONLCR is set,
921 * translate newline into "\r\n".
923 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
926 if (putc('\r', &tp
->t_outq
))
929 /* If OCRNL is set, translate "\r" into "\n". */
930 else if (c
== '\r' && ISSET(tp
->t_oflag
, OCRNL
))
932 /* If ONOCR is set, don't transmit CRs when on column 0. */
933 else if (c
== '\r' && ISSET(tp
->t_oflag
, ONOCR
) && tp
->t_column
== 0)
937 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
956 col
= (col
+ 8) & ~7;
967 * Identical to ttioctl_locked, only the lock is not held
969 * Parameters: <See ttioctl_locked()>
971 * Returns: <See ttioctl_locked()>
973 * Locks: This function assumes the tty_lock() is not held on entry;
974 * it takes the lock, and releases it before returning.
976 * Notes: This is supported to ensure the line discipline interfaces
977 * all have the same locking semantics.
979 * This function is called from
982 ttioctl(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
987 retval
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
997 * Ioctls for all tty devices.
999 * Parameters: tp Tty on which ioctl() is being called
1000 * cmd ioctl() command parameter
1001 * data ioctl() data argument (if any)
1002 * flag fileglob open modes from fcntl.h;
1003 * if called internally, this is usually
1004 * set to 0, rather than something useful
1005 * p Process context for the call; if the
1006 * call is proxied to a worker thread,
1007 * this will not be the current process!!!
1009 * Returns: 0 Success
1010 * EIO I/O error (no process group, job
1012 * EINTR Interrupted by signal
1013 * EBUSY Attempt to become the console while
1014 * the console is busy
1015 * ENOTTY TIOCGPGRP on a non-controlling tty
1016 * EINVAL Invalid baud rate
1017 * ENXIO TIOCSETD of invalid line discipline
1018 * EPERM TIOCSTI, not root, not open for read
1019 * EACCES TIOCSTI, not root, not your controlling
1021 * EPERM TIOCSCTTY failed
1022 * ENOTTY/EINVAL/EPERM TIOCSPGRP failed
1023 * EPERM TIOCSDRAINWAIT as non-root user
1024 * suser:EPERM Console control denied
1025 * ttywait:EIO t_timeout too small/expired
1026 * ttywait:ERESTART Upper layer must redrive the call;
1027 * this is usually done by the Libc
1028 * stub in user space
1029 * ttywait:EINTR Interrupted (usually a signal)
1033 * ttcompat:ENOTTY TIOCGSID, if no session or session
1035 * ttcompat:ENOTTY All unrecognized ioctls
1036 * *tp->t_param:? TIOCSETA* underlying function
1037 * *linesw[t].l_open:? TIOCSETD line discipline open failure
1040 * Locks: This function assumes that the tty_lock() is held for the
1041 * tp at the time of the call. The lock remains held on return.
1043 * Notes: This function is called after line-discipline specific ioctl
1044 * has been called to do discipline-specific functions and/or
1045 * reject any of these ioctl() commands.
1047 * This function calls ttcompat(), which can re-call ttioctl()
1048 * to a depth of one (FORTRAN style mutual recursion); at some
1049 * point, we should just in-line ttcompat() here.
1052 ttioctl_locked(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1056 struct pgrp
*pg
, *oldpg
;
1057 struct session
*sessp
, *oldsessp
;
1060 TTY_LOCK_OWNED(tp
); /* debug assert */
1062 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1063 /* If the ioctl involves modification, signal if in the background. */
1090 while (isbackground(p
, tp
) &&
1091 (p
->p_lflag
& P_LPPWAIT
) == 0 &&
1092 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1093 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1095 if (pg
== PGRP_NULL
) {
1099 /* SAFE: All callers drop the lock on return */
1101 if (pg
->pg_jobc
== 0) {
1107 pgsignal(pg
, SIGTTOU
, 1);
1113 * We signalled ourself, so we need to act as if we
1114 * have been "interrupted" from a "sleep" to act on
1115 * the signal. If it's a signal that stops the
1116 * process, that's handled in the signal sending code.
1124 switch (cmd
) { /* Process the ioctl. */
1125 case FIOASYNC
: /* set/clear async i/o */
1127 SET(tp
->t_state
, TS_ASYNC
);
1129 CLR(tp
->t_state
, TS_ASYNC
);
1131 case FIONBIO
: /* set/clear non-blocking i/o */
1132 break; /* XXX: delete. */
1133 case FIONREAD
: /* get # bytes to read */
1134 *(int *)data
= ttnread(tp
);
1136 case TIOCEXCL
: /* set exclusive use of tty */
1137 SET(tp
->t_state
, TS_XCLUDE
);
1139 case TIOCFLUSH
: { /* flush buffers */
1140 int flags
= *(int *)data
;
1143 flags
= FREAD
| FWRITE
;
1145 flags
&= FREAD
| FWRITE
;
1146 ttyflush(tp
, flags
);
1150 /* Set current console device to this line */
1152 data
= (caddr_t
) &bogusData
;
1154 /* No break - Fall through to BSD code */
1156 case TIOCCONS
: { /* become virtual console */
1158 if (constty
&& constty
!= tp
&&
1159 ISSET(constty
->t_state
, TS_CONNECTED
)) {
1163 if ( (error
= suser(kauth_cred_get(), &p
->p_acflag
)) )
1166 } else if (tp
== constty
) {
1170 (*cdevsw
[major(constty
->t_dev
)].d_ioctl
)
1171 (constty
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1173 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
1174 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1178 case TIOCDRAIN
: /* wait till output drained */
1179 error
= ttywait(tp
);
1183 case TIOCGETA_32
: /* get termios struct */
1185 termios64to32((struct user_termios
*)&tp
->t_termios
, (struct termios32
*)data
);
1187 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1190 case TIOCGETA_64
: /* get termios struct */
1192 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1194 termios32to64((struct termios32
*)&tp
->t_termios
, (struct user_termios
*)data
);
1197 case TIOCGETD
: /* get line discipline */
1198 *(int *)data
= tp
->t_line
;
1200 case TIOCGWINSZ
: /* get window size */
1201 *(struct winsize
*)data
= tp
->t_winsize
;
1203 case TIOCGPGRP
: /* get pgrp of tty */
1204 if (!isctty(p
, tp
)) {
1208 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
1211 case TIOCHPCL
: /* hang up on last close */
1212 SET(tp
->t_cflag
, HUPCL
);
1215 case TIOCNXCL
: /* reset exclusive use of tty */
1216 CLR(tp
->t_state
, TS_XCLUDE
);
1218 case TIOCOUTQ
: /* output queue size */
1219 *(int *)data
= tp
->t_outq
.c_cc
;
1221 case TIOCSETA_32
: /* set termios struct */
1223 case TIOCSETAW_32
: /* drain output, set */
1225 case TIOCSETAF_32
: /* drn out, fls in, set */
1227 { /* drn out, fls in, set */
1228 struct termios
*t
= (struct termios
*)data
;
1229 struct termios lcl_termios
;
1232 if (cmd
==TIOCSETA_32
|| cmd
==TIOCSETAW_32
|| cmd
==TIOCSETAF_32
) {
1233 termios32to64((struct termios32
*)data
, (struct user_termios
*)&lcl_termios
);
1237 if (cmd
==TIOCSETA_64
|| cmd
==TIOCSETAW_64
|| cmd
==TIOCSETAF_64
) {
1238 termios64to32((struct user_termios
*)data
, (struct termios32
*)&lcl_termios
);
1243 /* XXX bogus test; always false */
1244 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0) {
1248 #endif /* 0 - leave in; may end up being a conformance issue */
1249 if (t
->c_ispeed
== 0)
1250 t
->c_ispeed
= t
->c_ospeed
;
1251 if (cmd
== TIOCSETAW_32
|| cmd
== TIOCSETAF_32
||
1252 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
1253 error
= ttywait(tp
);
1257 if (cmd
== TIOCSETAF_32
|| cmd
== TIOCSETAF_64
)
1258 ttyflush(tp
, FREAD
);
1260 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1262 * Set device hardware.
1264 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1267 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1268 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1270 * XXX disconnections would be too hard to
1271 * get rid of without this kludge. The only
1272 * way to get rid of controlling terminals
1273 * is to exit from the session leader.
1275 CLR(tp
->t_state
, TS_ZOMBIE
);
1277 wakeup(TSA_CARR_ON(tp
));
1281 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1282 ISSET(t
->c_cflag
, CLOCAL
)) &&
1283 !ISSET(tp
->t_state
, TS_ZOMBIE
))
1284 SET(tp
->t_state
, TS_CONNECTED
);
1286 CLR(tp
->t_state
, TS_CONNECTED
);
1287 tp
->t_cflag
= t
->c_cflag
;
1288 tp
->t_ispeed
= t
->c_ispeed
;
1289 tp
->t_ospeed
= t
->c_ospeed
;
1292 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1293 cmd
!= TIOCSETAF_32
&& cmd
!= TIOCSETAF_64
) {
1294 if (ISSET(t
->c_lflag
, ICANON
))
1295 SET(tp
->t_lflag
, PENDIN
);
1298 * XXX we really shouldn't allow toggling
1299 * ICANON while we're in a non-termios line
1300 * discipline. Now we have to worry about
1301 * panicing for a null queue.
1303 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1306 catq(&tp
->t_rawq
, &tp
->t_canq
);
1308 tp
->t_rawq
= tp
->t_canq
;
1311 CLR(tp
->t_lflag
, PENDIN
);
1315 tp
->t_iflag
= t
->c_iflag
;
1316 tp
->t_oflag
= t
->c_oflag
;
1318 * Make the EXTPROC bit read only.
1320 if (ISSET(tp
->t_lflag
, EXTPROC
))
1321 SET(t
->c_lflag
, EXTPROC
);
1323 CLR(t
->c_lflag
, EXTPROC
);
1324 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1325 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1326 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1328 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1331 case TIOCSETD
: { /* set line discipline */
1332 int t
= *(int *)data
;
1333 dev_t device
= tp
->t_dev
;
1335 if (t
>= nlinesw
|| t
< 0) {
1340 * If the new line discipline is not equal to the old one,
1341 * close the old one and open the new one.
1343 if (t
!= tp
->t_line
) {
1344 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1345 error
= (*linesw
[t
].l_open
)(device
, tp
);
1347 /* This is racy; it's possible to lose both */
1348 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1355 case TIOCSTART
: /* start output, like ^Q */
1356 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1357 ISSET(tp
->t_lflag
, FLUSHO
)) {
1358 CLR(tp
->t_lflag
, FLUSHO
);
1359 CLR(tp
->t_state
, TS_TTSTOP
);
1363 case TIOCSTI
: /* simulate terminal input */
1364 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0) {
1368 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
)) {
1372 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1374 case TIOCSTOP
: /* stop output, like ^S */
1375 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1376 SET(tp
->t_state
, TS_TTSTOP
);
1386 case TIOCSCTTY
: /* become controlling tty */
1387 /* Session ctty vnode pointer set in vnode layer. */
1389 sessp
= proc_session(p
);
1390 if (!SESS_LEADER(p
, sessp
) ||
1391 ((sessp
->s_ttyvp
|| tp
->t_session
) &&
1392 (tp
->t_session
!= sessp
))) {
1393 /* SAFE: All callers drop the lock on return */
1395 if (sessp
!= SESSION_NULL
)
1396 session_rele(sessp
);
1397 if (pg
!= PGRP_NULL
)
1404 oldsessp
= tp
->t_session
;
1406 if (oldsessp
!= SESSION_NULL
)
1407 oldsessp
->s_ttypgrpid
= NO_PID
;
1408 /* do not drop refs on sessp and pg as tp holds them */
1409 tp
->t_session
= sessp
;
1410 sessp
->s_ttypgrpid
= pg
->pg_id
;
1413 session_lock(sessp
);
1414 oldtp
= sessp
->s_ttyp
;
1417 session_unlock(sessp
);
1418 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
1419 /* SAFE: All callers drop the lock on return */
1421 /* drop the reference on prev session and pgrp */
1422 if (oldsessp
!= SESSION_NULL
)
1423 session_rele(oldsessp
);
1424 if (oldpg
!= PGRP_NULL
)
1431 case TIOCSPGRP
: { /* set pgrp of tty */
1432 struct pgrp
*pgrp
= PGRP_NULL
;
1434 sessp
= proc_session(p
);
1435 if (!isctty_sp(p
, tp
, sessp
)) {
1436 if (sessp
!= SESSION_NULL
)
1437 session_rele(sessp
);
1441 else if ((pgrp
= pgfind(*(int *)data
)) == PGRP_NULL
) {
1442 if (sessp
!= SESSION_NULL
)
1443 session_rele(sessp
);
1446 } else if (pgrp
->pg_session
!= sessp
) {
1447 /* SAFE: All callers drop the lock on return */
1449 if (sessp
!= SESSION_NULL
)
1450 session_rele(sessp
);
1459 sessp
->s_ttypgrpid
= pgrp
->pg_id
;
1461 /* SAFE: All callers drop the lock on return */
1463 if (oldpg
!= PGRP_NULL
)
1465 if (sessp
!= SESSION_NULL
)
1466 session_rele(sessp
);
1470 case TIOCSTAT
: /* simulate control-T */
1473 case TIOCSWINSZ
: /* set window size */
1474 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1475 sizeof (struct winsize
))) {
1476 tp
->t_winsize
= *(struct winsize
*)data
;
1477 /* SAFE: All callers drop the lock on return */
1479 tty_pgsignal(tp
, SIGWINCH
, 1);
1483 case TIOCSDRAINWAIT
:
1484 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1488 tp
->t_timeout
= *(int *)data
* hz
;
1489 wakeup(TSA_OCOMPLETE(tp
));
1490 wakeup(TSA_OLOWAT(tp
));
1492 case TIOCGDRAINWAIT
:
1493 *(int *)data
= tp
->t_timeout
/ hz
;
1496 error
= ttcompat(tp
, cmd
, data
, flag
, p
);
1507 * Locks: Assumes tp is locked on entry, remains locked on exit
1510 ttyselect(struct tty
*tp
, int rw
, void *wql
, proc_t p
)
1517 TTY_LOCK_OWNED(tp
); /* debug assert */
1521 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1525 retval
= ttnread(tp
);
1530 selrecord(p
, &tp
->t_rsel
, wql
);
1533 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1537 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1538 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1539 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
1543 selrecord(p
, &tp
->t_wsel
, wql
);
1551 * This is a wrapper for compatibility with the select vector used by
1552 * cdevsw. It relies on a proper xxxdevtotty routine.
1554 * Locks: Assumes tty_lock() is not held prior to calling.
1557 ttselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
1560 struct tty
*tp
= cdevsw
[major(dev
)].d_ttys
[minor(dev
)];
1563 rv
= ttyselect(tp
, rw
, wql
, p
);
1571 * Locks: Assumes tp is locked on entry, remains locked on exit
1574 ttnread(struct tty
*tp
)
1578 TTY_LOCK_OWNED(tp
); /* debug assert */
1580 if (ISSET(tp
->t_lflag
, PENDIN
))
1582 nread
= tp
->t_canq
.c_cc
;
1583 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1584 nread
+= tp
->t_rawq
.c_cc
;
1585 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1595 * Wait for output to drain.
1597 * Parameters: tp Tty on which to wait for output to drain
1599 * Returns: 0 Success
1600 * EIO t_timeout too small/expired
1601 * ttysleep:ERESTART Upper layer must redrive the call;
1602 * this is usually done by the Libc
1603 * stub in user space
1604 * ttysleep:EINTR Interrupted (usually a signal)
1606 * Notes: Called from proc_exit() and vproc_exit().
1608 * Locks: Assumes tp is locked on entry, remains locked on exit
1611 ttywait(struct tty
*tp
)
1615 TTY_LOCK_OWNED(tp
); /* debug assert */
1618 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1619 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1621 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1622 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1623 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1624 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1625 TTOPRI
| PCATCH
, "ttywai",
1628 if (error
== EWOULDBLOCK
)
1635 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1641 * Stop the underlying device driver.
1643 * Locks: Assumes tty_lock() is held prior to calling.
1646 ttystop(struct tty
*tp
, int rw
)
1648 TTY_LOCK_OWNED(tp
); /* debug assert */
1650 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1654 * Flush if successfully wait.
1656 * Locks: Assumes tty_lock() is held prior to calling.
1659 ttywflush(struct tty
*tp
)
1663 TTY_LOCK_OWNED(tp
); /* debug assert */
1665 if ((error
= ttywait(tp
)) == 0)
1666 ttyflush(tp
, FREAD
);
1671 * Flush tty read and/or write queues, notifying anyone waiting.
1673 * Locks: Assumes tty_lock() is held prior to calling.
1676 ttyflush(struct tty
*tp
, int rw
)
1678 TTY_LOCK_OWNED(tp
); /* debug assert */
1684 FLUSHQ(&tp
->t_outq
);
1685 CLR(tp
->t_state
, TS_TTSTOP
);
1689 FLUSHQ(&tp
->t_canq
);
1690 FLUSHQ(&tp
->t_rawq
);
1691 CLR(tp
->t_lflag
, PENDIN
);
1694 CLR(tp
->t_state
, TS_LOCAL
);
1696 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1698 FLUSHQ(&tp
->t_outq
);
1702 * Don't let leave any state that might clobber the
1703 * next line discipline (although we should do more
1704 * to send the START char). Not clearing the state
1705 * may have caused the "putc to a clist with no
1706 * reserved cblocks" panic/printf.
1708 CLR(tp
->t_state
, TS_TBLOCK
);
1710 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1711 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1713 * XXX wait a bit in the hope that the stop
1714 * character (if any) will go out. Waiting
1715 * isn't good since it allows races. This
1716 * will be fixed when the stop character is
1717 * put in a special queue. Don't bother with
1718 * the checks in ttywait() since the timeout
1721 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1722 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1725 * Don't try sending the stop character again.
1727 CLR(tp
->t_state
, TS_TBLOCK
);
1734 FLUSHQ(&tp
->t_outq
);
1740 * Copy in the default termios characters.
1742 * Locks: Assumes tty_lock() is held prior to calling.
1744 * Notes: No assertion; tp is not in scope.
1747 termioschars(struct termios
*t
)
1749 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1754 * Handle input high water. Send stop character for the IXOFF case. Turn
1755 * on our input flow control bit and propagate the changes to the driver.
1756 * XXX the stop character should be put in a special high priority queue.
1758 * Locks: Assumes tty_lock() is held for the call.
1761 ttyblock(struct tty
*tp
)
1763 TTY_LOCK_OWNED(tp
); /* debug assert */
1765 SET(tp
->t_state
, TS_TBLOCK
);
1766 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1767 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1768 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1774 * Handle input low water. Send start character for the IXOFF case. Turn
1775 * off our input flow control bit and propagate the changes to the driver.
1776 * XXX the start character should be put in a special high priority queue.
1778 * Locks: Assumes tty_lock() is held for the call.
1781 ttyunblock(struct tty
*tp
)
1783 TTY_LOCK_OWNED(tp
); /* debug assert */
1785 CLR(tp
->t_state
, TS_TBLOCK
);
1786 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1787 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1788 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1798 * Parameters: tp tty on which to start output
1800 * Returns: 0 Success
1802 * Locks: Assumes tty_lock() is held for the call.
1804 * Notes: This function might as well be void; it always returns success
1806 * Called from ttioctl_locked(), LDISC routines, and
1807 * ttycheckoutq(), ttyblock(), ttyunblock(), and tputchar()
1810 ttstart(struct tty
*tp
)
1812 TTY_LOCK_OWNED(tp
); /* debug assert */
1814 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1824 * "close" a line discipline
1826 * Locks: Assumes tty_lock() is held prior to calling.
1829 ttylclose(struct tty
*tp
, int flag
)
1831 TTY_LOCK_OWNED(tp
); /* debug assert */
1833 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1834 ttyflush(tp
, FREAD
| FWRITE
);
1843 * Handle modem control transition on a tty.
1844 * Flag indicates new state of carrier.
1845 * Returns 0 if the line should be turned off, otherwise 1.
1847 * Locks: Assumes tty_lock() is held prior to calling.
1850 ttymodem(struct tty
*tp
, int flag
)
1852 int rval
= 1; /* default return value */
1854 TTY_LOCK_OWNED(tp
); /* debug assert */
1856 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1858 * MDMBUF: do flow control according to carrier flag
1859 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1860 * works if IXON and IXANY are clear.
1863 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1864 CLR(tp
->t_state
, TS_TTSTOP
);
1866 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1867 SET(tp
->t_state
, TS_CAR_OFLOW
);
1868 SET(tp
->t_state
, TS_TTSTOP
);
1871 } else if (flag
== 0) {
1875 CLR(tp
->t_state
, TS_CARR_ON
);
1876 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1877 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1878 SET(tp
->t_state
, TS_ZOMBIE
);
1879 CLR(tp
->t_state
, TS_CONNECTED
);
1880 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1881 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1882 ttyflush(tp
, FREAD
| FWRITE
);
1890 SET(tp
->t_state
, TS_CARR_ON
);
1891 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1892 SET(tp
->t_state
, TS_CONNECTED
);
1893 wakeup(TSA_CARR_ON(tp
));
1904 * Reinput pending characters after state switch
1907 * Locks: Assumes tty_lock() is held for the call.
1910 ttypend(struct tty
*tp
)
1915 TTY_LOCK_OWNED(tp
); /* debug assert */
1917 CLR(tp
->t_lflag
, PENDIN
);
1918 SET(tp
->t_state
, TS_TYPEN
);
1920 tp
->t_rawq
.c_cc
= 0;
1921 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= NULL
;
1922 while ((c
= getc(&tq
)) >= 0)
1924 CLR(tp
->t_state
, TS_TYPEN
);
1931 * Process a read call on a tty device.
1933 * Locks: Assumes tty_lock() is held prior to calling.
1936 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1941 cc_t
*cc
= tp
->t_cc
;
1942 proc_t p
= current_proc();
1943 int first
, error
= 0;
1944 int has_etime
= 0, last_cc
= 0;
1945 long slp
= 0; /* XXX this should be renamed `timo'. */
1949 TTY_LOCK_OWNED(tp
); /* debug assert */
1951 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1954 lflag
= tp
->t_lflag
;
1956 * take pending input first
1958 if (ISSET(lflag
, PENDIN
)) {
1960 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1964 * Signal the process if it's in the background.
1966 if (isbackground(p
, tp
)) {
1967 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1968 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1969 p
->p_lflag
& P_LPPWAIT
) {
1974 if (pg
== PGRP_NULL
) {
1978 if (pg
->pg_jobc
== 0) {
1979 /* SAFE: All callers drop the lock on return */
1986 /* SAFE: All callers drop the lock on return */
1988 pgsignal(pg
, SIGTTIN
, 1);
1993 * We signalled ourself, so we need to act as if we
1994 * have been "interrupted" from a "sleep" to act on
1995 * the signal. If it's a signal that stops the
1996 * process, that's handled in the signal sending code.
2002 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2003 /* EOF - returning 0 */
2008 * If canonical, use the canonical queue,
2009 * else use the raw queue.
2011 * (should get rid of clists...)
2013 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
2015 if (flag
& IO_NDELAY
) {
2018 if (ISSET(lflag
, ICANON
) || cc
[VMIN
] != 0) {
2019 error
= EWOULDBLOCK
;
2021 /* else polling - returning 0 */
2024 if (!ISSET(lflag
, ICANON
)) {
2027 struct timeval timecopy
;
2028 struct timeval etime
= {0, 0}; /* protected by !has_etime */
2031 * Check each of the four combinations.
2032 * (m > 0 && t == 0) is the normal read case.
2033 * It should be fairly efficient, so we check that and its
2034 * companion case (m == 0 && t == 0) first.
2035 * For the other two cases, we compute the target sleep time
2044 /* m, t and qp->c_cc are all 0. 0 is enough input. */
2047 t
*= 100000; /* time in us */
2048 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
2049 ((t1).tv_usec - (t2).tv_usec))
2055 microuptime(&timecopy
);
2057 /* first character, start timer */
2060 etime
.tv_sec
= t
/ 1000000;
2061 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2062 timeradd(&etime
, &timecopy
, &etime
);
2065 } else if (qp
->c_cc
> last_cc
) {
2066 /* got a character, restart timer */
2068 etime
.tv_sec
= t
/ 1000000;
2069 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2070 timeradd(&etime
, &timecopy
, &etime
);
2074 /* nothing, check expiration */
2075 if (timercmp(&etime
, &timecopy
, <=))
2078 slp
= diff(etime
, timecopy
);
2081 } else { /* m == 0 */
2084 microuptime(&timecopy
);
2088 etime
.tv_sec
= t
/ 1000000;
2089 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2090 timeradd(&etime
, &timecopy
, &etime
);
2094 if (timercmp(&etime
, &timecopy
, <=)) {
2095 /* Timed out, but 0 is enough input. */
2098 slp
= diff(etime
, timecopy
);
2103 * Rounding down may make us wake up just short
2104 * of the target, so we round up.
2105 * The formula is ceiling(slp * hz/1000000).
2106 * 32-bit arithmetic is enough for hz < 169.
2107 * XXX see hzto() for how to avoid overflow if hz
2108 * is large (divide by `tick' and/or arrange to
2109 * use hzto() if hz is large).
2111 slp
= (long) (((u_int32_t
)slp
* hz
) + 999999) / 1000000;
2114 if (qp
->c_cc
<= 0) {
2117 * There is no input, or not enough input and we can block.
2119 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
2120 ISSET(tp
->t_state
, TS_CONNECTED
) ?
2121 "ttyin" : "ttyhup", (int)slp
);
2122 if (error
== EWOULDBLOCK
)
2128 * XXX what happens if another process eats some input
2129 * while we are asleep (not just here)? It would be
2130 * safest to detect changes and reset our state variables
2131 * (has_stime and last_cc).
2138 * Input present, check for input mapping and processing.
2141 if (ISSET(lflag
, ICANON
)
2142 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
2148 icc
= MIN(uio_resid(uio
), IBUFSIZ
);
2149 icc
= q_to_b(qp
, (u_char
*)ibuf
, icc
);
2155 error
= uiomove(ibuf
, icc
, uio
);
2157 * XXX if there was an error then we should ungetc() the
2158 * unmoved chars and reduce icc here.
2162 if (uio_resid(uio
) == 0)
2176 * delayed suspend (^Y)
2178 if (CCEQ(cc
[VDSUSP
], c
) &&
2179 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
2181 * SAFE: All callers drop the lock on return and
2182 * SAFE: current thread will not change out from
2183 * SAFE: under us in the "goto loop" case.
2186 tty_pgsignal(tp
, SIGTSTP
, 1);
2189 error
= ttysleep(tp
, &ttread
, TTIPRI
| PCATCH
,
2198 * Interpret EOF only in canonical mode.
2200 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
2203 * Give user character.
2205 error
= ureadc(c
, uio
);
2207 /* XXX should ungetc(c, qp). */
2209 if (uio_resid(uio
) == 0)
2212 * In canonical mode check for a "break character"
2213 * marking the end of a "line of input".
2215 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
2222 * Look to unblock input now that (presumably)
2223 * the input queue has gone down.
2225 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
2226 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
2235 * Check the output queue on tp for space for a kernel message (from uprintf
2236 * or tprintf). Allow some space over the normal hiwater mark so we don't
2237 * lose messages due to normal flow control, but don't let the tty run amok.
2238 * Sleeps here are not interruptible, but we return prematurely if new signals
2241 * Locks: Assumes tty_lock() is held before calling
2243 * Notes: This function is called from tprintf() in subr_prf.c
2246 ttycheckoutq(struct tty
*tp
, int wait
)
2252 TTY_LOCK_OWNED(tp
); /* debug assert */
2254 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2256 hiwat
= tp
->t_hiwat
;
2257 oldsig
= wait
? ut
->uu_siglist
: 0;
2258 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
2259 while (tp
->t_outq
.c_cc
> hiwat
) {
2261 if (tp
->t_outq
.c_cc
<= hiwat
)
2263 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
2266 SET(tp
->t_state
, TS_SO_OLOWAT
);
2267 ttysleep(tp
, TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
2276 * Process a write call on a tty device.
2278 * Locks: Assumes tty_lock() is held prior to calling.
2281 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
2286 int i
, hiwat
, error
;
2292 TTY_LOCK_OWNED(tp
); /* debug assert */
2294 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2295 hiwat
= tp
->t_hiwat
;
2296 count
= uio_resid(uio
);
2300 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2301 if (uio_resid(uio
) == count
)
2305 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2306 if (flag
& IO_NDELAY
) {
2307 error
= EWOULDBLOCK
;
2310 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2317 * Signal the process if it's in the background.
2320 if (isbackground(p
, tp
) &&
2321 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& P_LPPWAIT
) == 0 &&
2322 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2323 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2326 if (pg
== PGRP_NULL
) {
2330 if (pg
->pg_jobc
== 0) {
2331 /* SAFE: All callers drop the lock on return */
2338 /* SAFE: All callers drop the lock on return */
2340 pgsignal(pg
, SIGTTOU
, 1);
2344 * We signalled ourself, so we need to act as if we
2345 * have been "interrupted" from a "sleep" to act on
2346 * the signal. If it's a signal that stops the
2347 * process, that's handled in the signal sending code.
2353 * Process the user's data in at most OBUFSIZ chunks. Perform any
2354 * output translation. Keep track of high water mark, sleep on
2355 * overflow awaiting device aid in acquiring new space.
2357 while (uio_resid(uio
) > 0 || cc
> 0) {
2358 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2359 uio_setresid(uio
, 0);
2362 if (tp
->t_outq
.c_cc
> hiwat
)
2365 * Grab a hunk of data from the user, unless we have some
2366 * leftover from last time.
2369 cc
= MIN(uio_resid(uio
), OBUFSIZ
);
2371 error
= uiomove(cp
, cc
, uio
);
2378 * If nothing fancy need be done, grab those characters we
2379 * can handle without any of ttyoutput's processing and
2380 * just transfer them to the output q. For those chars
2381 * which require special processing (as indicated by the
2382 * bits in char_type), call ttyoutput. After processing
2383 * a hunk of data, look for FLUSHO so ^O's will take effect
2387 if (!ISSET(tp
->t_oflag
, OPOST
))
2390 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2391 char_type
, CCLASSMASK
);
2393 * If ce is zero, then we're processing
2394 * a special character through ttyoutput.
2398 if (ttyoutput(*cp
, tp
) >= 0) {
2404 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2405 tp
->t_outq
.c_cc
> hiwat
)
2411 * A bunch of normal characters have been found.
2412 * Transfer them en masse to the output queue and
2413 * continue processing at the top of the loop.
2414 * If there are any further characters in this
2415 * <= OBUFSIZ chunk, the first should be a character
2416 * requiring special handling by ttyoutput.
2419 i
= b_to_q((u_char
*)cp
, ce
, &tp
->t_outq
);
2422 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2428 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2429 tp
->t_outq
.c_cc
> hiwat
)
2436 * If cc is nonzero, we leave the uio structure inconsistent, as the
2437 * offset and iov pointers have moved forward, but it doesn't matter
2438 * (the call will either return short or restart with a new uio).
2440 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2446 * Since we are using ring buffers, if we can't insert any more into
2447 * the output queue, we can assume the ring is full and that someone
2448 * forgot to set the high water mark correctly. We set it and then
2449 * proceed as normal.
2451 hiwat
= tp
->t_outq
.c_cc
- 1;
2456 * This can only occur if FLUSHO is set in t_lflag,
2457 * or if ttstart/oproc is synchronous (or very fast).
2459 if (tp
->t_outq
.c_cc
<= hiwat
) {
2462 if (flag
& IO_NDELAY
) {
2463 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2464 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2466 SET(tp
->t_state
, TS_SO_OLOWAT
);
2467 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2469 if (error
== EWOULDBLOCK
)
2478 * Rubout one character from the rawq of tp
2479 * as cleanly as possible.
2481 * Locks: Assumes tty_lock() is held prior to calling.
2484 ttyrub(int c
, struct tty
*tp
)
2490 TTY_LOCK_OWNED(tp
); /* debug assert */
2492 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2494 CLR(tp
->t_lflag
, FLUSHO
);
2495 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2496 if (tp
->t_rocount
== 0) {
2498 * Messed up by ttwrite; retype
2503 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2506 CLR(c
, ~TTY_CHARMASK
);
2507 switch (CCLASS(c
)) {
2509 if(!(ISSET(tp
->t_iflag
, IUTF8
) && CCONT(c
))) {
2518 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2522 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2526 savecol
= tp
->t_column
;
2527 SET(tp
->t_state
, TS_CNTTB
);
2528 SET(tp
->t_lflag
, FLUSHO
);
2529 tp
->t_column
= tp
->t_rocol
;
2530 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2531 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2533 CLR(tp
->t_lflag
, FLUSHO
);
2534 CLR(tp
->t_state
, TS_CNTTB
);
2536 /* savecol will now be length of the tab. */
2537 savecol
-= tp
->t_column
;
2538 tp
->t_column
+= savecol
;
2540 savecol
= 8; /* overflow fixup */
2541 while (--savecol
>= 0)
2542 (void)ttyoutput('\b', tp
);
2545 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2546 printf(PANICSTR
, c
, CCLASS(c
));
2548 panic(PANICSTR
, c
, CCLASS(c
));
2552 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2553 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2554 SET(tp
->t_state
, TS_ERASE
);
2555 (void)ttyoutput('\\', tp
);
2559 ttyecho(tp
->t_cc
[VERASE
], tp
);
2565 * Back over count characters, erasing them.
2567 * Locks: Assumes tty_lock() is held prior to calling.
2570 ttyrubo(struct tty
*tp
, int count
)
2572 TTY_LOCK_OWNED(tp
); /* debug assert */
2574 while (count
-- > 0) {
2575 (void)ttyoutput('\b', tp
);
2576 (void)ttyoutput(' ', tp
);
2577 (void)ttyoutput('\b', tp
);
2584 * Reprint the rawq line. Note, it is assumed that c_cc has already
2587 * Locks: Assumes tty_lock() is held prior to calling.
2590 ttyretype(struct tty
*tp
)
2595 TTY_LOCK_OWNED(tp
); /* debug assert */
2597 /* Echo the reprint character. */
2598 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2599 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2601 (void)ttyoutput('\n', tp
);
2605 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2606 * BIT OF FIRST CHAR.
2608 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2610 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2612 CLR(tp
->t_state
, TS_ERASE
);
2614 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2620 * Echo a typed character to the terminal.
2622 * Locks: Assumes tty_lock() is held prior to calling.
2625 ttyecho(int c
, struct tty
*tp
)
2627 TTY_LOCK_OWNED(tp
); /* debug assert */
2629 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2630 CLR(tp
->t_lflag
, FLUSHO
);
2631 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2632 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2633 ISSET(tp
->t_lflag
, EXTPROC
))
2635 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2636 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2637 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2638 (void)ttyoutput('^', tp
);
2639 CLR(c
, ~TTY_CHARMASK
);
2645 (void)ttyoutput(c
, tp
);
2650 * Wake up any readers on a tty.
2652 * Locks: Assumes tty_lock() is held for the call.
2655 ttwakeup(struct tty
*tp
)
2657 TTY_LOCK_OWNED(tp
); /* debug assert */
2659 selwakeup(&tp
->t_rsel
);
2660 KNOTE(&tp
->t_rsel
.si_note
, 1);
2661 if (ISSET(tp
->t_state
, TS_ASYNC
)) {
2663 * XXX: Callers may not revalidate it the tty is closed
2664 * XXX: out from under them by another thread, but we do
2665 * XXX: not support queued signals. This should be safe,
2666 * XXX: since the process we intend to wakeup is in the
2667 * XXX: process group, and will wake up because of the
2668 * XXX: signal anyway.
2671 tty_pgsignal(tp
, SIGIO
, 1);
2674 wakeup(TSA_HUP_OR_INPUT(tp
));
2681 * Wake up any writers on a tty.
2683 * Locks: Assumes tty_lock() is held prior to calling.
2686 ttwwakeup(struct tty
*tp
)
2688 TTY_LOCK_OWNED(tp
); /* debug assert */
2690 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2691 selwakeup(&tp
->t_wsel
);
2692 KNOTE(&tp
->t_wsel
.si_note
, 1);
2694 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2695 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2696 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2697 wakeup(TSA_OCOMPLETE(tp
));
2699 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2700 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2701 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2702 wakeup(TSA_OLOWAT(tp
));
2708 * Look up a code for a specified speed in a conversion table;
2709 * used by drivers to map software speed values to hardware parameters.
2711 * Notes: No locks are assumed for this function; it does not
2712 * directly access struct tty.
2715 ttspeedtab(int speed
, struct speedtab
*table
)
2717 for ( ; table
->sp_speed
!= -1; table
++)
2718 if (table
->sp_speed
== speed
)
2719 return (table
->sp_code
);
2725 * Set tty hi and low water marks.
2727 * Try to arrange the dynamics so there's about one second
2728 * from hi to low water.
2730 * Locks: Assumes tty_lock() is held prior to calling.
2733 ttsetwater(struct tty
*tp
)
2738 TTY_LOCK_OWNED(tp
); /* debug assert */
2740 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2742 cps
= tp
->t_ospeed
/ 10;
2743 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2745 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2746 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2750 /* ttyinfo has been converted to the MACH kernel */
2751 #include <mach/thread_info.h>
2753 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2754 extern kern_return_t
thread_info_internal(thread_t thread
,
2755 thread_flavor_t flavor
,
2756 thread_info_t thread_info_out
,
2757 mach_msg_type_number_t
*thread_info_count
);
2761 * Report on state of foreground process group.
2763 * Locks: Assumes tty_lock() is held prior to calling.
2766 ttyinfo_locked(struct tty
*tp
)
2775 struct timeval utime
;
2776 struct timeval stime
;
2777 thread_basic_info_data_t basic_info
;
2778 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2781 TTY_LOCK_OWNED(tp
); /* debug assert */
2783 if (ttycheckoutq(tp
,0) == 0)
2786 /* Print load average. */
2787 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2788 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2791 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2792 * that pending input will be retyped on BS.
2794 if (tp
->t_session
== NULL
) {
2795 ttyprintf(tp
, "not a controlling terminal\n");
2799 if (tp
->t_pgrp
== NULL
) {
2800 ttyprintf(tp
, "no foreground process group\n");
2804 /* first process in process group */
2805 /* XXX is there a need for pgrp lock ? */
2806 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2807 ttyprintf(tp
, "empty foreground process group\n");
2813 * Pick the most interesting process and copy some of its
2814 * state for printing later.
2818 /* the proc_compare is non blocking fn, no need to use iterator */
2819 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2820 if (proc_compare(pick
, p
)) {
2824 pickpid
= pick
->p_pid
;
2828 /* SAFE: All callers drop the lock on return */
2833 pick
= proc_find(pickpid
);
2834 if (pick
== PROC_NULL
)
2837 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2838 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2839 (thread
= vfs_context_thread(&uthread
->uu_context
)) == NULL
||
2840 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2841 ttyprintf(tp
, "foreground process without thread\n");
2847 switch(basic_info
.run_state
) {
2848 case TH_STATE_RUNNING
:
2851 case TH_STATE_STOPPED
:
2854 case TH_STATE_WAITING
:
2857 case TH_STATE_UNINTERRUPTIBLE
:
2858 state
= "uninterruptible";
2860 case TH_STATE_HALTED
:
2867 calcru(pick
, &utime
, &stime
, NULL
);
2870 /* Print command, pid, state, utime, and stime */
2871 ttyprintf(tp
, " cmd: %s %d %s %ld.%02du %ld.%02ds\n",
2875 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2876 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2882 * Returns 1 if p2 is "better" than p1
2884 * The algorithm for picking the "interesting" process is thus:
2886 * 1) Only foreground processes are eligible - implied.
2887 * 2) Runnable processes are favored over anything else. The runner
2888 * with the highest cpu utilization is picked (p_estcpu). Ties are
2889 * broken by picking the highest pid.
2890 * 3) The sleeper with the shortest sleep time is next.
2891 * 4) Further ties are broken by picking the highest pid.
2893 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2894 #define TESTAB(a, b) ((a)<<1 | (b))
2900 * Locks: pgrp_lock(p2) held on call to this function
2901 * tty_lock(tp) for p2's tty, for which p2 is the foreground
2902 * process, held on call to this function
2905 proc_compare(proc_t p1
, proc_t p2
)
2907 /* NOTE THIS FN needs to be NON BLOCKING */
2912 * see if at least one of them is runnable
2914 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2921 * tie - favor one with highest recent cpu utilization
2923 #ifdef _PROC_HAS_SCHEDINFO_
2924 /* Without the support the fields are always zero */
2925 if (p2
->p_estcpu
> p1
->p_estcpu
)
2927 if (p1
->p_estcpu
> p2
->p_estcpu
)
2929 #endif /* _PROC_HAS_SCHEDINFO_ */
2930 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2935 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2941 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2944 * pick the one with the smallest sleep time
2946 #ifdef _PROC_HAS_SCHEDINFO_
2947 /* Without the support the fields are always zero */
2948 if (p2
->p_slptime
> p1
->p_slptime
)
2950 if (p1
->p_slptime
> p2
->p_slptime
)
2952 #endif /* _PROC_HAS_SCHEDINFO_ */
2953 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2958 * Output char to tty; console putchar style.
2960 * Locks: Assumes tty_lock() is held prior to calling.
2962 * Notes: Only ever called from putchar() in subr_prf.c
2965 tputchar(int c
, struct tty
*tp
)
2967 TTY_LOCK_OWNED(tp
); /* debug assert */
2969 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2973 (void)ttyoutput('\r', tp
);
2974 (void)ttyoutput(c
, tp
);
2983 * Sleep on a wait channel waiting for an interrupt or a condition to come
2984 * true so that we are woken up.
2986 * Parameters: tp Tty going to sleep
2987 * chan The sleep channel (usually an address
2988 * of a structure member)
2989 * pri priority and flags
2990 * wmesg Wait message; shows up in debugger,
2991 * should show up in "ps", but doesn't
2992 * timo Timeout for the sleep
2994 * Returns: 0 Condition came true
2995 * ERESTART Upper layer must redrive the call;
2996 * this is usually done by the Libc
2997 * stub in user space
2998 * msleep0:EINTR Interrupted (usually a signal)
2999 * msleep0:ERESTART Interrupted (usually a masked signal)
3000 * msleep0:EWOULDBLOCK Timeout (timo) already expired
3002 * Locks: Assumes tty_lock() is held prior to calling.
3004 * Sleep on chan, returning ERESTART if tty changed while we napped and
3005 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by msleep0. If
3006 * the tty is revoked, restarting a pending call will redo validation done
3007 * at the start of the call.
3010 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
3018 /* Use of msleep0() avoids conversion timo/timespec/timo */
3019 error
= msleep0(chan
, &tp
->t_lock
, pri
, wmesg
, timo
, (int (*)(int))0);
3022 return (tp
->t_gen
== gen
? 0 : ERESTART
);
3027 * Allocate a tty structure and its associated buffers.
3031 * Returns: !NULL Address of new struct tty
3032 * NULL Error ("ENOMEM")
3034 * Locks: The tty_lock() of the returned tty is not held when it
3042 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
3044 /* XXX: default to TTYCLSIZE(1024) chars for now */
3045 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
3046 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
3047 /* output queue doesn't need quoting */
3048 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
3049 lck_mtx_init(&tp
->t_lock
, tty_lck_grp
, tty_lck_attr
);
3050 klist_init(&tp
->t_rsel
.si_note
);
3051 klist_init(&tp
->t_wsel
.si_note
);
3058 * Increment the reference count on a tty.
3061 ttyhold(struct tty
*tp
)
3068 * Drops a reference count on a tty structure; if the reference count reaches
3069 * zero, then also frees the structure and associated buffers.
3072 ttyfree(struct tty
*tp
)
3074 TTY_LOCK_NOTOWNED(tp
);
3077 if (--tp
->t_refcnt
== 0) {
3080 } else if (tp
->t_refcnt
< 0) {
3081 panic("%s: freeing free tty %p", __func__
, tp
);
3087 * Deallocate a tty structure and its buffers.
3089 * Locks: The tty_lock() is assumed to not be held at the time of
3090 * the free; this function destroys the mutex.
3093 ttydeallocate(struct tty
*tp
)
3095 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
3098 if (!(SLIST_EMPTY(&tp
->t_rsel
.si_note
) && SLIST_EMPTY(&tp
->t_wsel
.si_note
))) {
3099 panic("knotes hooked into a tty when the tty is freed.\n");
3103 clfree(&tp
->t_rawq
);
3104 clfree(&tp
->t_canq
);
3105 clfree(&tp
->t_outq
);
3106 lck_mtx_destroy(&tp
->t_lock
, tty_lck_grp
);
3112 * Locks: Assumes tty_lock() is held prior to calling.
3115 isbackground(proc_t p
, struct tty
*tp
)
3119 return (tp
->t_session
!= NULL
&& p
->p_pgrp
!= NULL
&& (p
->p_pgrp
!= tp
->t_pgrp
) && isctty_sp(p
, tp
, p
->p_pgrp
->pg_session
));
3123 isctty(proc_t p
, struct tty
*tp
)
3126 struct session
* sessp
;
3128 sessp
= proc_session(p
);
3129 retval
= (sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3130 session_rele(sessp
);
3135 isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
)
3137 return(sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);