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 int isctty(proc_t p
, struct tty
*tp
);
146 static int isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
);
149 * Table with character classes and parity. The 8th bit indicates parity,
150 * the 7th bit indicates the character is an alphameric or underscore (for
151 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
152 * are 0 then the character needs no special processing on output; classes
153 * other than 0 might be translated or (not currently) require delays.
155 #define E 0x00 /* Even parity. */
156 #define O 0x80 /* Odd parity. */
157 #define PARITY(c) (char_type[c] & O)
159 #define ALPHA 0x40 /* Alpha or underscore. */
160 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
162 #define CCLASSMASK 0x3f
163 #define CCLASS(c) (char_type[c] & CCLASSMASK)
164 /* 0b10xxxxxx is the mask for UTF-8 continuations */
165 #define CCONT(c) ((c & 0xc0) == 0x80)
170 #define NA ORDINARY | ALPHA
176 static u_char
const char_type
[] = {
177 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* nul - bel */
178 O
|BS
, E
|TB
, E
|NL
, O
|CC
, E
|VT
, O
|CR
, O
|CC
, E
|CC
, /* bs - si */
179 O
|CC
, E
|CC
, E
|CC
, O
|CC
, E
|CC
, O
|CC
, O
|CC
, E
|CC
, /* dle - etb */
180 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* can - us */
181 O
|NO
, E
|NO
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* sp - ' */
182 E
|NO
, O
|NO
, O
|NO
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|NO
, /* ( - / */
183 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* 0 - 7 */
184 O
|NA
, E
|NA
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* 8 - ? */
185 O
|NO
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* @ - G */
186 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* H - O */
187 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* P - W */
188 O
|NA
, E
|NA
, E
|NA
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, O
|NA
, /* X - _ */
189 E
|NO
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* ` - g */
190 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* h - o */
191 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* p - w */
192 E
|NA
, O
|NA
, O
|NA
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|CC
, /* x - del */
194 * Meta chars; should be settable per character set;
195 * for now, treat them all as normal characters.
197 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
198 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
199 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
200 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
201 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
202 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
203 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
204 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
205 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
206 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
207 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
208 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
209 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
210 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
211 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
212 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
223 /* Macros to clear/set/test flags. */
224 #define SET(t, f) (t) |= (f)
225 #define CLR(t, f) (t) &= ~(f)
226 #define ISSET(t, f) ((t) & (f))
229 * Input control starts when we would not be able to fit the maximum
230 * contents of the ping-pong buffers and finishes when we would be able
231 * to fit that much plus 1/8 more.
233 #define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */
234 #define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */
237 termios32to64(struct termios32
*in
, struct user_termios
*out
)
239 out
->c_iflag
= (user_tcflag_t
)in
->c_iflag
;
240 out
->c_oflag
= (user_tcflag_t
)in
->c_oflag
;
241 out
->c_cflag
= (user_tcflag_t
)in
->c_cflag
;
242 out
->c_lflag
= (user_tcflag_t
)in
->c_lflag
;
244 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
245 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
247 out
->c_ispeed
= (user_speed_t
)in
->c_ispeed
;
248 out
->c_ospeed
= (user_speed_t
)in
->c_ospeed
;
252 termios64to32(struct user_termios
*in
, struct termios32
*out
)
254 out
->c_iflag
= (tcflag_t
)in
->c_iflag
;
255 out
->c_oflag
= (tcflag_t
)in
->c_oflag
;
256 out
->c_cflag
= (tcflag_t
)in
->c_cflag
;
257 out
->c_lflag
= (tcflag_t
)in
->c_lflag
;
259 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
260 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
262 out
->c_ispeed
= (speed_t
)in
->c_ispeed
;
263 out
->c_ospeed
= (speed_t
)in
->c_ospeed
;
270 * Initialize the tty line discipline subsystem.
276 * Locks: No ttys can be allocated and no tty locks can be used
277 * until after this function is called
279 * Notes: The intent of this is to set up a log group attribute,
280 * lock group, and loc atribute for subsequent per-tty locks.
281 * This function is called early in bsd_init(), prior to the
282 * console device initialization.
287 tty_lck_grp_attr
= lck_grp_attr_alloc_init();
288 tty_lck_grp
= lck_grp_alloc_init("tty", tty_lck_grp_attr
);
289 tty_lck_attr
= lck_attr_alloc_init();
296 * Lock the requested tty structure.
298 * Parameters: tp The tty we want to lock
302 * Locks: On return, tp is locked
305 tty_lock(struct tty
*tp
)
307 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
308 lck_mtx_lock(&tp
->t_lock
);
315 * Unlock the requested tty structure.
317 * Parameters: tp The tty we want to unlock
321 * Locks: On return, tp is unlocked
324 tty_unlock(struct tty
*tp
)
326 TTY_LOCK_OWNED(tp
); /* debug assert */
327 lck_mtx_unlock(&tp
->t_lock
);
334 * Initial open of tty, or (re)entry to standard tty line discipline.
336 * Locks: Assumes tty_lock() is held prior to calling.
339 ttyopen(dev_t device
, struct tty
*tp
)
341 proc_t p
= current_proc();
342 struct pgrp
* pg
, * oldpg
;
343 struct session
*sessp
, *oldsess
;
345 TTY_LOCK_OWNED(tp
); /* debug assert */
349 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
350 SET(tp
->t_state
, TS_ISOPEN
);
351 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
352 SET(tp
->t_state
, TS_CONNECTED
); }
353 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
357 sessp
= proc_session(p
);
360 * First tty open affter setsid() call makes this tty its controlling
361 * tty, if the tty does not already have a session associated with it.
362 * Only do this if the process
364 if (SESS_LEADER(p
, sessp
) && /* process is session leader */
365 sessp
->s_ttyvp
== NULL
&& /* but has no controlling tty */
366 tp
->t_session
== NULL
) { /* and tty not controlling */
368 if ((sessp
->s_flags
& S_NOCTTY
) == 0) { /* and no O_NOCTTY */
369 /* Hold on to the reference */
370 sessp
->s_ttyp
= tp
; /* XXX NOT A REFERENCE */
371 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
372 session_unlock(sessp
);
375 oldsess
= tp
->t_session
;
376 if (oldsess
!= SESSION_NULL
)
377 oldsess
->s_ttypgrpid
= NO_PID
;
378 tp
->t_session
= sessp
;
380 sessp
->s_ttypgrpid
= pg
->pg_id
;
382 /* SAFE: All callers drop the lock on return */
384 if (oldpg
!= PGRP_NULL
)
386 if (oldsess
!= SESSION_NULL
)
387 session_rele(oldsess
);
391 session_unlock(sessp
);
394 /* SAFE: All callers drop the lock on return */
396 if (sessp
!= SESSION_NULL
)
404 /* XXX may be an error code */
411 * Handle close() on a tty line: flush and set to initial state,
412 * bumping generation number so that pending read/write calls
413 * can detect recycling of the tty.
414 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
415 * and l_close() should have flushed, but we repeat the spltty() and
416 * the flush in case there are buggy callers.
418 * Locks: Assumes tty_lock() is held prior to calling.
421 ttyclose(struct tty
*tp
)
424 struct session
* oldsessp
;
426 TTY_LOCK_OWNED(tp
); /* debug assert */
433 * Closing current console tty; disable printing of console
434 * messages at bottom-level driver.
436 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
437 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
440 ttyflush(tp
, FREAD
| FWRITE
);
443 tp
->t_line
= TTYDISC
;
446 oldsessp
= tp
->t_session
;
448 tp
->t_session
= NULL
;
449 if (oldsessp
!= SESSION_NULL
)
450 oldsessp
->s_ttypgrpid
= NO_PID
;
452 /* drop the reference on prev session and pgrp */
453 /* SAFE: All callers drop the lock on return */
455 if (oldsessp
!= SESSION_NULL
)
456 session_rele(oldsessp
);
457 if (oldpg
!= PGRP_NULL
)
461 selthreadclear(&tp
->t_wsel
);
462 selthreadclear(&tp
->t_rsel
);
466 #define FLUSHQ(q) { \
468 ndflush(q, (q)->c_cc); \
471 /* Is 'c' a line delimiter ("break" character)? */
472 #define TTBREAKC(c, lflag) \
473 ((c) == '\n' || (((c) == cc[VEOF] || \
474 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
475 (c) != _POSIX_VDISABLE))
480 * Process input of a single character received on a tty.
482 * Parameters: c The character received
483 * tp The tty on which it was received
487 * Locks: Assumes tty_lock() is held prior to calling.
490 ttyinput(int c
, struct tty
*tp
)
492 tcflag_t iflag
, lflag
;
495 int retval
= 0; /* default return value */
497 TTY_LOCK_OWNED(tp
); /* debug assert */
500 * If input is pending take it first.
503 if (ISSET(lflag
, PENDIN
))
508 if (ISSET(lflag
, ICANON
)) {
518 * Block further input iff:
519 * current input > threshold AND input is available to user program
520 * AND input flow control is enabled and not yet invoked.
521 * The 3 is slop for PARMRK.
524 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
525 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
526 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
527 !ISSET(tp
->t_state
, TS_TBLOCK
))
530 /* Handle exceptional conditions (break, parity, framing). */
532 err
= (ISSET(c
, TTY_ERRORMASK
));
534 CLR(c
, TTY_ERRORMASK
);
535 if (ISSET(err
, TTY_BI
)) {
536 if (ISSET(iflag
, IGNBRK
)) {
539 if (ISSET(iflag
, BRKINT
)) {
540 ttyflush(tp
, FREAD
| FWRITE
);
541 /* SAFE: All callers drop the lock on return */
543 tty_pgsignal(tp
, SIGINT
, 1);
547 if (ISSET(iflag
, PARMRK
))
549 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
550 || ISSET(err
, TTY_FE
)) {
551 if (ISSET(iflag
, IGNPAR
)) {
554 else if (ISSET(iflag
, PARMRK
)) {
556 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
559 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
560 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
561 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
568 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
570 if (!ISSET(lflag
, EXTPROC
)) {
572 * Check for literal nexting very first
574 if (ISSET(tp
->t_state
, TS_LNCH
)) {
576 CLR(tp
->t_state
, TS_LNCH
);
579 * Scan for special characters. This code
580 * is really just a big case statement with
581 * non-constant cases. The bottom of the
582 * case statement is labeled ``endcase'', so goto
583 * it after a case match, or similar.
587 * Control chars which aren't controlled
588 * by ICANON, ISIG, or IXON.
590 if (ISSET(lflag
, IEXTEN
)) {
591 if (CCEQ(cc
[VLNEXT
], c
)) {
592 if (ISSET(lflag
, ECHO
)) {
593 if (ISSET(lflag
, ECHOE
)) {
594 (void)ttyoutput('^', tp
);
595 (void)ttyoutput('\b', tp
);
599 SET(tp
->t_state
, TS_LNCH
);
602 if (CCEQ(cc
[VDISCARD
], c
)) {
603 if (ISSET(lflag
, FLUSHO
))
604 CLR(tp
->t_lflag
, FLUSHO
);
606 ttyflush(tp
, FWRITE
);
608 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
610 SET(tp
->t_lflag
, FLUSHO
);
618 if (ISSET(lflag
, ISIG
)) {
619 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
620 if (!ISSET(lflag
, NOFLSH
))
621 ttyflush(tp
, FREAD
| FWRITE
);
624 * SAFE: All callers drop the lock on return;
625 * SAFE: if we lose a threaded race on change
626 * SAFE: of the interrupt character, we could
627 * SAFE: have lost that race anyway due to the
628 * SAFE: scheduler executing threads in
629 * SAFE: priority order rather than "last
630 * SAFE: active thread" order (FEATURE).
634 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
638 if (CCEQ(cc
[VSUSP
], c
)) {
639 if (!ISSET(lflag
, NOFLSH
))
642 /* SAFE: All callers drop the lock on return */
644 tty_pgsignal(tp
, SIGTSTP
, 1);
650 * Handle start/stop characters.
652 if (ISSET(iflag
, IXON
)) {
653 if (CCEQ(cc
[VSTOP
], c
)) {
654 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
655 SET(tp
->t_state
, TS_TTSTOP
);
659 if (!CCEQ(cc
[VSTART
], c
)) {
663 * if VSTART == VSTOP then toggle
667 if (CCEQ(cc
[VSTART
], c
))
671 * IGNCR, ICRNL, & INLCR
674 if (ISSET(iflag
, IGNCR
)) {
677 else if (ISSET(iflag
, ICRNL
))
679 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
682 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
684 * From here on down canonical mode character
685 * processing takes place.
690 if (CCEQ(cc
[VERASE
], c
)) {
691 if (tp
->t_rawq
.c_cc
) {
692 if (ISSET(iflag
, IUTF8
)) {
694 ttyrub((c
= unputc(&tp
->t_rawq
)), tp
);
695 } while(tp
->t_rawq
.c_cc
&& CCONT(c
));
697 ttyrub(unputc(&tp
->t_rawq
), tp
);
705 if (CCEQ(cc
[VKILL
], c
)) {
706 if (ISSET(lflag
, ECHOKE
) &&
707 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
708 !ISSET(lflag
, ECHOPRT
))
709 while (tp
->t_rawq
.c_cc
)
710 ttyrub(unputc(&tp
->t_rawq
), tp
);
713 if (ISSET(lflag
, ECHOK
) ||
714 ISSET(lflag
, ECHOKE
))
719 CLR(tp
->t_state
, TS_LOCAL
);
725 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
731 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
736 * erase last char of word and remember the
737 * next chars type (for ALTWERASE)
740 c
= unputc(&tp
->t_rawq
);
743 if (c
== ' ' || c
== '\t') {
744 (void)putc(c
, &tp
->t_rawq
);
753 c
= unputc(&tp
->t_rawq
);
756 } while (c
!= ' ' && c
!= '\t' &&
757 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
758 (void)putc(c
, &tp
->t_rawq
);
764 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
769 * ^T - kernel info and generate SIGINFO
771 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
772 if (ISSET(lflag
, ISIG
)) {
773 /* SAFE: All callers drop the lock on return */
775 tty_pgsignal(tp
, SIGINFO
, 1);
778 if (!ISSET(lflag
, NOKERNINFO
))
784 * Check for input buffer overflow
786 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
788 if (ISSET(iflag
, IMAXBEL
)) {
789 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
790 (void)ttyoutput(CTRL('g'), tp
);
795 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
796 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
797 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
800 * Put data char in q for user and
801 * wakeup on seeing a line delimiter.
803 if (putc(c
, &tp
->t_rawq
) >= 0) {
804 if (!ISSET(lflag
, ICANON
)) {
809 if (TTBREAKC(c
, lflag
)) {
811 catq(&tp
->t_rawq
, &tp
->t_canq
);
813 } else if (tp
->t_rocount
++ == 0)
814 tp
->t_rocol
= tp
->t_column
;
815 if (ISSET(tp
->t_state
, TS_ERASE
)) {
817 * end of prterase \.../
819 CLR(tp
->t_state
, TS_ERASE
);
820 (void)ttyoutput('/', tp
);
824 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
826 * Place the cursor over the '^' of the ^D.
828 i
= min(2, tp
->t_column
- i
);
830 (void)ttyoutput('\b', tp
);
838 * IXANY means allow any character to restart output.
840 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
841 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
846 CLR(tp
->t_lflag
, FLUSHO
);
847 CLR(tp
->t_state
, TS_TTSTOP
);
850 /* Start the output */
851 retval
= ttstart(tp
);
861 * Output a single character on a tty, doing output processing
862 * as needed (expanding tabs, newline processing, etc.).
864 * Parameters: c The character to output
865 * tp The tty on which to output on the tty
867 * Returns: < 0 Success
868 * >= 0 Character to resend (failure)
870 * Locks: Assumes tp is locked on entry, remains locked on exit
872 * Notes: Must be recursive.
875 ttyoutput(int c
, struct tty
*tp
)
880 TTY_LOCK_OWNED(tp
); /* debug assert */
883 if (!ISSET(oflag
, OPOST
)) {
884 if (ISSET(tp
->t_lflag
, FLUSHO
))
886 if (putc(c
, &tp
->t_outq
))
893 * Do tab expansion if OXTABS is set. Special case if we external
894 * processing, we don't do the tab expansion because we'll probably
895 * get it wrong. If tab expansion needs to be done, let it happen
898 CLR(c
, ~TTY_CHARMASK
);
900 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
901 col
= c
= 8 - (tp
->t_column
& 7);
902 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
903 c
-= b_to_q((const u_char
*)" ", c
, &tp
->t_outq
);
908 return (c
== col
? -1 : '\t');
910 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
914 * Newline translation: if ONLCR is set,
915 * translate newline into "\r\n".
917 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
920 if (putc('\r', &tp
->t_outq
))
923 /* If OCRNL is set, translate "\r" into "\n". */
924 else if (c
== '\r' && ISSET(tp
->t_oflag
, OCRNL
))
926 /* If ONOCR is set, don't transmit CRs when on column 0. */
927 else if (c
== '\r' && ISSET(tp
->t_oflag
, ONOCR
) && tp
->t_column
== 0)
931 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
950 col
= (col
+ 8) & ~7;
961 * Identical to ttioctl_locked, only the lock is not held
963 * Parameters: <See ttioctl_locked()>
965 * Returns: <See ttioctl_locked()>
967 * Locks: This function assumes the tty_lock() is not held on entry;
968 * it takes the lock, and releases it before returning.
970 * Notes: This is supported to ensure the line discipline interfaces
971 * all have the same locking semantics.
973 * This function is called from
976 ttioctl(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
981 retval
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
991 * Ioctls for all tty devices.
993 * Parameters: tp Tty on which ioctl() is being called
994 * cmd ioctl() command parameter
995 * data ioctl() data argument (if any)
996 * flag fileglob open modes from fcntl.h;
997 * if called internally, this is usually
998 * set to 0, rather than something useful
999 * p Process context for the call; if the
1000 * call is proxied to a worker thread,
1001 * this will not be the current process!!!
1003 * Returns: 0 Success
1004 * EIO I/O error (no process group, job
1006 * EINTR Interrupted by signal
1007 * EBUSY Attempt to become the console while
1008 * the console is busy
1009 * ENOTTY TIOCGPGRP on a non-controlling tty
1010 * EINVAL Invalid baud rate
1011 * ENXIO TIOCSETD of invalid line discipline
1012 * EPERM TIOCSTI, not root, not open for read
1013 * EACCES TIOCSTI, not root, not your controlling
1015 * EPERM TIOCSCTTY failed
1016 * ENOTTY/EINVAL/EPERM TIOCSPGRP failed
1017 * EPERM TIOCSDRAINWAIT as non-root user
1018 * suser:EPERM Console control denied
1019 * ttywait:EIO t_timeout too small/expired
1020 * ttywait:ERESTART Upper layer must redrive the call;
1021 * this is usually done by the Libc
1022 * stub in user space
1023 * ttywait:EINTR Interrupted (usually a signal)
1027 * ttcompat:ENOTTY TIOCGSID, if no session or session
1029 * ttcompat:ENOTTY All unrecognized ioctls
1030 * *tp->t_param:? TIOCSETA* underlying function
1031 * *linesw[t].l_open:? TIOCSETD line discipline open failure
1034 * Locks: This function assumes that the tty_lock() is held for the
1035 * tp at the time of the call. The lock remains held on return.
1037 * Notes: This function is called after line-discipline specific ioctl
1038 * has been called to do discipline-specific functions and/or
1039 * reject any of these ioctl() commands.
1041 * This function calls ttcompat(), which can re-call ttioctl()
1042 * to a depth of one (FORTRAN style mutual recursion); at some
1043 * point, we should just in-line ttcompat() here.
1046 ttioctl_locked(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1050 struct pgrp
* pg
, *oldpg
;
1051 struct session
*sessp
, * oldsessp
;
1053 TTY_LOCK_OWNED(tp
); /* debug assert */
1055 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1056 /* If the ioctl involves modification, signal if in the background. */
1083 while (isbackground(p
, tp
) &&
1084 (p
->p_lflag
& P_LPPWAIT
) == 0 &&
1085 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1086 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1088 if (pg
== PGRP_NULL
) {
1092 /* SAFE: All callers drop the lock on return */
1094 if (pg
->pg_jobc
== 0) {
1100 pgsignal(pg
, SIGTTOU
, 1);
1106 * We signalled ourself, so we need to act as if we
1107 * have been "interrupted" from a "sleep" to act on
1108 * the signal. If it's a signal that stops the
1109 * process, that's handled in the signal sending code.
1117 switch (cmd
) { /* Process the ioctl. */
1118 case FIOASYNC
: /* set/clear async i/o */
1120 SET(tp
->t_state
, TS_ASYNC
);
1122 CLR(tp
->t_state
, TS_ASYNC
);
1124 case FIONBIO
: /* set/clear non-blocking i/o */
1125 break; /* XXX: delete. */
1126 case FIONREAD
: /* get # bytes to read */
1127 *(int *)data
= ttnread(tp
);
1129 case TIOCEXCL
: /* set exclusive use of tty */
1130 SET(tp
->t_state
, TS_XCLUDE
);
1132 case TIOCFLUSH
: { /* flush buffers */
1133 int flags
= *(int *)data
;
1136 flags
= FREAD
| FWRITE
;
1138 flags
&= FREAD
| FWRITE
;
1139 ttyflush(tp
, flags
);
1143 /* Set current console device to this line */
1145 data
= (caddr_t
) &bogusData
;
1147 /* No break - Fall through to BSD code */
1149 case TIOCCONS
: { /* become virtual console */
1151 if (constty
&& constty
!= tp
&&
1152 ISSET(constty
->t_state
, TS_CONNECTED
)) {
1156 if ( (error
= suser(kauth_cred_get(), &p
->p_acflag
)) )
1159 } else if (tp
== constty
) {
1163 (*cdevsw
[major(constty
->t_dev
)].d_ioctl
)
1164 (constty
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1166 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
1167 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1171 case TIOCDRAIN
: /* wait till output drained */
1172 error
= ttywait(tp
);
1176 case TIOCGETA_32
: /* get termios struct */
1178 termios64to32((struct user_termios
*)&tp
->t_termios
, (struct termios32
*)data
);
1180 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1183 case TIOCGETA_64
: /* get termios struct */
1185 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1187 termios32to64((struct termios32
*)&tp
->t_termios
, (struct user_termios
*)data
);
1190 case TIOCGETD
: /* get line discipline */
1191 *(int *)data
= tp
->t_line
;
1193 case TIOCGWINSZ
: /* get window size */
1194 *(struct winsize
*)data
= tp
->t_winsize
;
1196 case TIOCGPGRP
: /* get pgrp of tty */
1197 if (!isctty(p
, tp
)) {
1201 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
1204 case TIOCHPCL
: /* hang up on last close */
1205 SET(tp
->t_cflag
, HUPCL
);
1208 case TIOCNXCL
: /* reset exclusive use of tty */
1209 CLR(tp
->t_state
, TS_XCLUDE
);
1211 case TIOCOUTQ
: /* output queue size */
1212 *(int *)data
= tp
->t_outq
.c_cc
;
1214 case TIOCSETA_32
: /* set termios struct */
1216 case TIOCSETAW_32
: /* drain output, set */
1218 case TIOCSETAF_32
: /* drn out, fls in, set */
1220 { /* drn out, fls in, set */
1221 struct termios
*t
= (struct termios
*)data
;
1222 struct termios lcl_termios
;
1225 if (cmd
==TIOCSETA_32
|| cmd
==TIOCSETAW_32
|| cmd
==TIOCSETAF_32
) {
1226 termios32to64((struct termios32
*)data
, (struct user_termios
*)&lcl_termios
);
1230 if (cmd
==TIOCSETA_64
|| cmd
==TIOCSETAW_64
|| cmd
==TIOCSETAF_64
) {
1231 termios64to32((struct user_termios
*)data
, (struct termios32
*)&lcl_termios
);
1236 /* XXX bogus test; always false */
1237 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0) {
1241 #endif /* 0 - leave in; may end up being a conformance issue */
1242 if (t
->c_ispeed
== 0)
1243 t
->c_ispeed
= t
->c_ospeed
;
1244 if (cmd
== TIOCSETAW_32
|| cmd
== TIOCSETAF_32
||
1245 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
1246 error
= ttywait(tp
);
1250 if (cmd
== TIOCSETAF_32
|| cmd
== TIOCSETAF_64
)
1251 ttyflush(tp
, FREAD
);
1253 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1255 * Set device hardware.
1257 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1260 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1261 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1263 * XXX disconnections would be too hard to
1264 * get rid of without this kludge. The only
1265 * way to get rid of controlling terminals
1266 * is to exit from the session leader.
1268 CLR(tp
->t_state
, TS_ZOMBIE
);
1270 wakeup(TSA_CARR_ON(tp
));
1274 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1275 ISSET(t
->c_cflag
, CLOCAL
)) &&
1276 !ISSET(tp
->t_state
, TS_ZOMBIE
))
1277 SET(tp
->t_state
, TS_CONNECTED
);
1279 CLR(tp
->t_state
, TS_CONNECTED
);
1280 tp
->t_cflag
= t
->c_cflag
;
1281 tp
->t_ispeed
= t
->c_ispeed
;
1282 tp
->t_ospeed
= t
->c_ospeed
;
1285 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1286 cmd
!= TIOCSETAF_32
&& cmd
!= TIOCSETAF_64
) {
1287 if (ISSET(t
->c_lflag
, ICANON
))
1288 SET(tp
->t_lflag
, PENDIN
);
1291 * XXX we really shouldn't allow toggling
1292 * ICANON while we're in a non-termios line
1293 * discipline. Now we have to worry about
1294 * panicing for a null queue.
1296 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1299 catq(&tp
->t_rawq
, &tp
->t_canq
);
1301 tp
->t_rawq
= tp
->t_canq
;
1304 CLR(tp
->t_lflag
, PENDIN
);
1308 tp
->t_iflag
= t
->c_iflag
;
1309 tp
->t_oflag
= t
->c_oflag
;
1311 * Make the EXTPROC bit read only.
1313 if (ISSET(tp
->t_lflag
, EXTPROC
))
1314 SET(t
->c_lflag
, EXTPROC
);
1316 CLR(t
->c_lflag
, EXTPROC
);
1317 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1318 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1319 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1321 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1324 case TIOCSETD
: { /* set line discipline */
1325 int t
= *(int *)data
;
1326 dev_t device
= tp
->t_dev
;
1328 if (t
>= nlinesw
|| t
< 0) {
1333 * If the new line discipline is not equal to the old one,
1334 * close the old one and open the new one.
1336 if (t
!= tp
->t_line
) {
1337 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1338 error
= (*linesw
[t
].l_open
)(device
, tp
);
1340 /* This is racy; it's possible to lose both */
1341 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1348 case TIOCSTART
: /* start output, like ^Q */
1349 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1350 ISSET(tp
->t_lflag
, FLUSHO
)) {
1351 CLR(tp
->t_lflag
, FLUSHO
);
1352 CLR(tp
->t_state
, TS_TTSTOP
);
1356 case TIOCSTI
: /* simulate terminal input */
1357 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0) {
1361 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
)) {
1365 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1367 case TIOCSTOP
: /* stop output, like ^S */
1368 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1369 SET(tp
->t_state
, TS_TTSTOP
);
1379 case TIOCSCTTY
: /* become controlling tty */
1380 /* Session ctty vnode pointer set in vnode layer. */
1382 sessp
= proc_session(p
);
1383 if (!SESS_LEADER(p
, sessp
) ||
1384 ((sessp
->s_ttyvp
|| tp
->t_session
) &&
1385 (tp
->t_session
!= sessp
))) {
1386 /* SAFE: All callers drop the lock on return */
1388 if (sessp
!= SESSION_NULL
)
1389 session_rele(sessp
);
1390 if (pg
!= PGRP_NULL
)
1397 oldsessp
= tp
->t_session
;
1399 if (oldsessp
!= SESSION_NULL
)
1400 oldsessp
->s_ttypgrpid
= NO_PID
;
1401 /* do not drop refs on sessp and pg as tp holds them */
1402 tp
->t_session
= sessp
;
1403 sessp
->s_ttypgrpid
= pg
->pg_id
;
1406 session_lock(sessp
);
1407 sessp
->s_ttyp
= tp
; /* XXX NOT A REFERENCE */
1408 session_unlock(sessp
);
1409 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
1410 /* SAFE: All callers drop the lock on return */
1412 /* drop the reference on prev session and pgrp */
1413 if (oldsessp
!= SESSION_NULL
)
1414 session_rele(oldsessp
);
1415 if (oldpg
!= PGRP_NULL
)
1420 case TIOCSPGRP
: { /* set pgrp of tty */
1421 struct pgrp
*pgrp
= PGRP_NULL
;
1423 sessp
= proc_session(p
);
1424 if (!isctty_sp(p
, tp
, sessp
)) {
1425 if (sessp
!= SESSION_NULL
)
1426 session_rele(sessp
);
1430 else if ((pgrp
= pgfind(*(int *)data
)) == PGRP_NULL
) {
1431 if (sessp
!= SESSION_NULL
)
1432 session_rele(sessp
);
1435 } else if (pgrp
->pg_session
!= sessp
) {
1436 /* SAFE: All callers drop the lock on return */
1438 if (sessp
!= SESSION_NULL
)
1439 session_rele(sessp
);
1448 sessp
->s_ttypgrpid
= pgrp
->pg_id
;
1450 /* SAFE: All callers drop the lock on return */
1452 if (oldpg
!= PGRP_NULL
)
1454 if (sessp
!= SESSION_NULL
)
1455 session_rele(sessp
);
1459 case TIOCSTAT
: /* simulate control-T */
1462 case TIOCSWINSZ
: /* set window size */
1463 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1464 sizeof (struct winsize
))) {
1465 tp
->t_winsize
= *(struct winsize
*)data
;
1466 /* SAFE: All callers drop the lock on return */
1468 tty_pgsignal(tp
, SIGWINCH
, 1);
1472 case TIOCSDRAINWAIT
:
1473 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1477 tp
->t_timeout
= *(int *)data
* hz
;
1478 wakeup(TSA_OCOMPLETE(tp
));
1479 wakeup(TSA_OLOWAT(tp
));
1481 case TIOCGDRAINWAIT
:
1482 *(int *)data
= tp
->t_timeout
/ hz
;
1485 error
= ttcompat(tp
, cmd
, data
, flag
, p
);
1496 * Locks: Assumes tp is locked on entry, remains locked on exit
1499 ttyselect(struct tty
*tp
, int rw
, void *wql
, proc_t p
)
1506 TTY_LOCK_OWNED(tp
); /* debug assert */
1510 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1514 retval
= ttnread(tp
);
1519 selrecord(p
, &tp
->t_rsel
, wql
);
1522 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1526 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1527 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1528 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
1532 selrecord(p
, &tp
->t_wsel
, wql
);
1540 * This is a wrapper for compatibility with the select vector used by
1541 * cdevsw. It relies on a proper xxxdevtotty routine.
1543 * Locks: Assumes tty_lock() is not held prior to calling.
1546 ttselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
1549 struct tty
*tp
= cdevsw
[major(dev
)].d_ttys
[minor(dev
)];
1552 rv
= ttyselect(tp
, rw
, wql
, p
);
1560 * Locks: Assumes tp is locked on entry, remains locked on exit
1563 ttnread(struct tty
*tp
)
1567 TTY_LOCK_OWNED(tp
); /* debug assert */
1569 if (ISSET(tp
->t_lflag
, PENDIN
))
1571 nread
= tp
->t_canq
.c_cc
;
1572 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1573 nread
+= tp
->t_rawq
.c_cc
;
1574 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1584 * Wait for output to drain.
1586 * Parameters: tp Tty on which to wait for output to drain
1588 * Returns: 0 Success
1589 * EIO t_timeout too small/expired
1590 * ttysleep:ERESTART Upper layer must redrive the call;
1591 * this is usually done by the Libc
1592 * stub in user space
1593 * ttysleep:EINTR Interrupted (usually a signal)
1595 * Notes: Called from proc_exit() and vproc_exit().
1597 * Locks: Assumes tp is locked on entry, remains locked on exit
1600 ttywait(struct tty
*tp
)
1604 TTY_LOCK_OWNED(tp
); /* debug assert */
1607 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1608 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1610 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1611 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1612 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1613 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1614 TTOPRI
| PCATCH
, "ttywai",
1617 if (error
== EWOULDBLOCK
)
1624 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1630 * Stop the underlying device driver.
1632 * Locks: Assumes tty_lock() is held prior to calling.
1635 ttystop(struct tty
*tp
, int rw
)
1637 TTY_LOCK_OWNED(tp
); /* debug assert */
1639 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1643 * Flush if successfully wait.
1645 * Locks: Assumes tty_lock() is held prior to calling.
1648 ttywflush(struct tty
*tp
)
1652 TTY_LOCK_OWNED(tp
); /* debug assert */
1654 if ((error
= ttywait(tp
)) == 0)
1655 ttyflush(tp
, FREAD
);
1660 * Flush tty read and/or write queues, notifying anyone waiting.
1662 * Locks: Assumes tty_lock() is held prior to calling.
1665 ttyflush(struct tty
*tp
, int rw
)
1667 TTY_LOCK_OWNED(tp
); /* debug assert */
1673 FLUSHQ(&tp
->t_outq
);
1674 CLR(tp
->t_state
, TS_TTSTOP
);
1678 FLUSHQ(&tp
->t_canq
);
1679 FLUSHQ(&tp
->t_rawq
);
1680 CLR(tp
->t_lflag
, PENDIN
);
1683 CLR(tp
->t_state
, TS_LOCAL
);
1685 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1687 FLUSHQ(&tp
->t_outq
);
1691 * Don't let leave any state that might clobber the
1692 * next line discipline (although we should do more
1693 * to send the START char). Not clearing the state
1694 * may have caused the "putc to a clist with no
1695 * reserved cblocks" panic/printf.
1697 CLR(tp
->t_state
, TS_TBLOCK
);
1699 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1700 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1702 * XXX wait a bit in the hope that the stop
1703 * character (if any) will go out. Waiting
1704 * isn't good since it allows races. This
1705 * will be fixed when the stop character is
1706 * put in a special queue. Don't bother with
1707 * the checks in ttywait() since the timeout
1710 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1711 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1714 * Don't try sending the stop character again.
1716 CLR(tp
->t_state
, TS_TBLOCK
);
1723 FLUSHQ(&tp
->t_outq
);
1729 * Copy in the default termios characters.
1731 * Locks: Assumes tty_lock() is held prior to calling.
1733 * Notes: No assertion; tp is not in scope.
1736 termioschars(struct termios
*t
)
1738 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1743 * Handle input high water. Send stop character for the IXOFF case. Turn
1744 * on our input flow control bit and propagate the changes to the driver.
1745 * XXX the stop character should be put in a special high priority queue.
1747 * Locks: Assumes tty_lock() is held for the call.
1750 ttyblock(struct tty
*tp
)
1752 TTY_LOCK_OWNED(tp
); /* debug assert */
1754 SET(tp
->t_state
, TS_TBLOCK
);
1755 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1756 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1757 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1763 * Handle input low water. Send start character for the IXOFF case. Turn
1764 * off our input flow control bit and propagate the changes to the driver.
1765 * XXX the start character should be put in a special high priority queue.
1767 * Locks: Assumes tty_lock() is held for the call.
1770 ttyunblock(struct tty
*tp
)
1772 TTY_LOCK_OWNED(tp
); /* debug assert */
1774 CLR(tp
->t_state
, TS_TBLOCK
);
1775 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1776 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1777 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1787 * Parameters: tp tty on which to start output
1789 * Returns: 0 Success
1791 * Locks: Assumes tty_lock() is held for the call.
1793 * Notes: This function might as well be void; it always returns success
1795 * Called from ttioctl_locked(), LDISC routines, and
1796 * ttycheckoutq(), ttyblock(), ttyunblock(), and tputchar()
1799 ttstart(struct tty
*tp
)
1801 TTY_LOCK_OWNED(tp
); /* debug assert */
1803 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1813 * "close" a line discipline
1815 * Locks: Assumes tty_lock() is held prior to calling.
1818 ttylclose(struct tty
*tp
, int flag
)
1820 TTY_LOCK_OWNED(tp
); /* debug assert */
1822 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1823 ttyflush(tp
, FREAD
| FWRITE
);
1832 * Handle modem control transition on a tty.
1833 * Flag indicates new state of carrier.
1834 * Returns 0 if the line should be turned off, otherwise 1.
1836 * Locks: Assumes tty_lock() is held prior to calling.
1839 ttymodem(struct tty
*tp
, int flag
)
1841 int rval
= 1; /* default return value */
1843 TTY_LOCK_OWNED(tp
); /* debug assert */
1845 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1847 * MDMBUF: do flow control according to carrier flag
1848 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1849 * works if IXON and IXANY are clear.
1852 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1853 CLR(tp
->t_state
, TS_TTSTOP
);
1855 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1856 SET(tp
->t_state
, TS_CAR_OFLOW
);
1857 SET(tp
->t_state
, TS_TTSTOP
);
1860 } else if (flag
== 0) {
1864 CLR(tp
->t_state
, TS_CARR_ON
);
1865 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1866 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1867 SET(tp
->t_state
, TS_ZOMBIE
);
1868 CLR(tp
->t_state
, TS_CONNECTED
);
1869 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1870 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1871 ttyflush(tp
, FREAD
| FWRITE
);
1879 SET(tp
->t_state
, TS_CARR_ON
);
1880 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1881 SET(tp
->t_state
, TS_CONNECTED
);
1882 wakeup(TSA_CARR_ON(tp
));
1893 * Reinput pending characters after state switch
1896 * Locks: Assumes tty_lock() is held for the call.
1899 ttypend(struct tty
*tp
)
1904 TTY_LOCK_OWNED(tp
); /* debug assert */
1906 CLR(tp
->t_lflag
, PENDIN
);
1907 SET(tp
->t_state
, TS_TYPEN
);
1909 tp
->t_rawq
.c_cc
= 0;
1910 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= NULL
;
1911 while ((c
= getc(&tq
)) >= 0)
1913 CLR(tp
->t_state
, TS_TYPEN
);
1920 * Process a read call on a tty device.
1922 * Locks: Assumes tty_lock() is held prior to calling.
1925 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1930 cc_t
*cc
= tp
->t_cc
;
1931 proc_t p
= current_proc();
1932 int first
, error
= 0;
1933 int has_etime
= 0, last_cc
= 0;
1934 long slp
= 0; /* XXX this should be renamed `timo'. */
1938 TTY_LOCK_OWNED(tp
); /* debug assert */
1940 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1943 lflag
= tp
->t_lflag
;
1945 * take pending input first
1947 if (ISSET(lflag
, PENDIN
)) {
1949 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1953 * Signal the process if it's in the background.
1955 if (isbackground(p
, tp
)) {
1956 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1957 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1958 p
->p_lflag
& P_LPPWAIT
) {
1963 if (pg
== PGRP_NULL
) {
1967 if (pg
->pg_jobc
== 0) {
1968 /* SAFE: All callers drop the lock on return */
1975 /* SAFE: All callers drop the lock on return */
1977 pgsignal(pg
, SIGTTIN
, 1);
1982 * We signalled ourself, so we need to act as if we
1983 * have been "interrupted" from a "sleep" to act on
1984 * the signal. If it's a signal that stops the
1985 * process, that's handled in the signal sending code.
1991 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1992 /* EOF - returning 0 */
1997 * If canonical, use the canonical queue,
1998 * else use the raw queue.
2000 * (should get rid of clists...)
2002 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
2004 if (flag
& IO_NDELAY
) {
2007 if (ISSET(lflag
, ICANON
) || cc
[VMIN
] != 0) {
2008 error
= EWOULDBLOCK
;
2010 /* else polling - returning 0 */
2013 if (!ISSET(lflag
, ICANON
)) {
2016 struct timeval timecopy
;
2017 struct timeval etime
= {0, 0}; /* protected by !has_etime */
2020 * Check each of the four combinations.
2021 * (m > 0 && t == 0) is the normal read case.
2022 * It should be fairly efficient, so we check that and its
2023 * companion case (m == 0 && t == 0) first.
2024 * For the other two cases, we compute the target sleep time
2033 /* m, t and qp->c_cc are all 0. 0 is enough input. */
2036 t
*= 100000; /* time in us */
2037 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
2038 ((t1).tv_usec - (t2).tv_usec))
2044 microuptime(&timecopy
);
2046 /* first character, start timer */
2049 etime
.tv_sec
= t
/ 1000000;
2050 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2051 timeradd(&etime
, &timecopy
, &etime
);
2054 } else if (qp
->c_cc
> last_cc
) {
2055 /* got a character, restart timer */
2057 etime
.tv_sec
= t
/ 1000000;
2058 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2059 timeradd(&etime
, &timecopy
, &etime
);
2063 /* nothing, check expiration */
2064 if (timercmp(&etime
, &timecopy
, <=))
2067 slp
= diff(etime
, timecopy
);
2070 } else { /* m == 0 */
2073 microuptime(&timecopy
);
2077 etime
.tv_sec
= t
/ 1000000;
2078 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2079 timeradd(&etime
, &timecopy
, &etime
);
2083 if (timercmp(&etime
, &timecopy
, <=)) {
2084 /* Timed out, but 0 is enough input. */
2087 slp
= diff(etime
, timecopy
);
2092 * Rounding down may make us wake up just short
2093 * of the target, so we round up.
2094 * The formula is ceiling(slp * hz/1000000).
2095 * 32-bit arithmetic is enough for hz < 169.
2096 * XXX see hzto() for how to avoid overflow if hz
2097 * is large (divide by `tick' and/or arrange to
2098 * use hzto() if hz is large).
2100 slp
= (long) (((u_int32_t
)slp
* hz
) + 999999) / 1000000;
2103 if (qp
->c_cc
<= 0) {
2106 * There is no input, or not enough input and we can block.
2108 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
2109 ISSET(tp
->t_state
, TS_CONNECTED
) ?
2110 "ttyin" : "ttyhup", (int)slp
);
2111 if (error
== EWOULDBLOCK
)
2117 * XXX what happens if another process eats some input
2118 * while we are asleep (not just here)? It would be
2119 * safest to detect changes and reset our state variables
2120 * (has_stime and last_cc).
2127 * Input present, check for input mapping and processing.
2130 if (ISSET(lflag
, ICANON
)
2131 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
2137 icc
= min(uio_resid(uio
), IBUFSIZ
);
2138 icc
= q_to_b(qp
, (u_char
*)ibuf
, icc
);
2144 error
= uiomove(ibuf
, icc
, uio
);
2146 * XXX if there was an error then we should ungetc() the
2147 * unmoved chars and reduce icc here.
2151 if (uio_resid(uio
) == 0)
2165 * delayed suspend (^Y)
2167 if (CCEQ(cc
[VDSUSP
], c
) &&
2168 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
2170 * SAFE: All callers drop the lock on return and
2171 * SAFE: current thread will not change out from
2172 * SAFE: under us in the "goto loop" case.
2175 tty_pgsignal(tp
, SIGTSTP
, 1);
2178 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
,
2187 * Interpret EOF only in canonical mode.
2189 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
2192 * Give user character.
2194 error
= ureadc(c
, uio
);
2196 /* XXX should ungetc(c, qp). */
2198 if (uio_resid(uio
) == 0)
2201 * In canonical mode check for a "break character"
2202 * marking the end of a "line of input".
2204 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
2211 * Look to unblock input now that (presumably)
2212 * the input queue has gone down.
2214 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
2215 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
2224 * Check the output queue on tp for space for a kernel message (from uprintf
2225 * or tprintf). Allow some space over the normal hiwater mark so we don't
2226 * lose messages due to normal flow control, but don't let the tty run amok.
2227 * Sleeps here are not interruptible, but we return prematurely if new signals
2230 * Locks: Assumes tty_lock() is held before calling
2232 * Notes: This function is called from tprintf() in subr_prf.c
2235 ttycheckoutq(struct tty
*tp
, int wait
)
2241 TTY_LOCK_OWNED(tp
); /* debug assert */
2243 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2245 hiwat
= tp
->t_hiwat
;
2246 oldsig
= wait
? ut
->uu_siglist
: 0;
2247 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
2248 while (tp
->t_outq
.c_cc
> hiwat
) {
2250 if (tp
->t_outq
.c_cc
<= hiwat
)
2252 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
2255 SET(tp
->t_state
, TS_SO_OLOWAT
);
2256 ttysleep(tp
, TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
2265 * Process a write call on a tty device.
2267 * Locks: Assumes tty_lock() is held prior to calling.
2270 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
2275 int i
, hiwat
, error
;
2281 TTY_LOCK_OWNED(tp
); /* debug assert */
2283 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2284 hiwat
= tp
->t_hiwat
;
2285 count
= uio_resid(uio
);
2289 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2290 if (uio_resid(uio
) == count
)
2294 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2295 if (flag
& IO_NDELAY
) {
2296 error
= EWOULDBLOCK
;
2299 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2306 * Signal the process if it's in the background.
2309 if (isbackground(p
, tp
) &&
2310 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& P_LPPWAIT
) == 0 &&
2311 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2312 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2315 if (pg
== PGRP_NULL
) {
2319 if (pg
->pg_jobc
== 0) {
2320 /* SAFE: All callers drop the lock on return */
2327 /* SAFE: All callers drop the lock on return */
2329 pgsignal(pg
, SIGTTOU
, 1);
2333 * We signalled ourself, so we need to act as if we
2334 * have been "interrupted" from a "sleep" to act on
2335 * the signal. If it's a signal that stops the
2336 * process, that's handled in the signal sending code.
2342 * Process the user's data in at most OBUFSIZ chunks. Perform any
2343 * output translation. Keep track of high water mark, sleep on
2344 * overflow awaiting device aid in acquiring new space.
2346 while (uio_resid(uio
) > 0 || cc
> 0) {
2347 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2348 uio_setresid(uio
, 0);
2351 if (tp
->t_outq
.c_cc
> hiwat
)
2354 * Grab a hunk of data from the user, unless we have some
2355 * leftover from last time.
2358 cc
= min(uio_resid(uio
), OBUFSIZ
);
2360 error
= uiomove(cp
, cc
, uio
);
2367 * If nothing fancy need be done, grab those characters we
2368 * can handle without any of ttyoutput's processing and
2369 * just transfer them to the output q. For those chars
2370 * which require special processing (as indicated by the
2371 * bits in char_type), call ttyoutput. After processing
2372 * a hunk of data, look for FLUSHO so ^O's will take effect
2376 if (!ISSET(tp
->t_oflag
, OPOST
))
2379 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2380 char_type
, CCLASSMASK
);
2382 * If ce is zero, then we're processing
2383 * a special character through ttyoutput.
2387 if (ttyoutput(*cp
, tp
) >= 0) {
2393 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2394 tp
->t_outq
.c_cc
> hiwat
)
2400 * A bunch of normal characters have been found.
2401 * Transfer them en masse to the output queue and
2402 * continue processing at the top of the loop.
2403 * If there are any further characters in this
2404 * <= OBUFSIZ chunk, the first should be a character
2405 * requiring special handling by ttyoutput.
2408 i
= b_to_q((u_char
*)cp
, ce
, &tp
->t_outq
);
2411 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2417 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2418 tp
->t_outq
.c_cc
> hiwat
)
2425 * If cc is nonzero, we leave the uio structure inconsistent, as the
2426 * offset and iov pointers have moved forward, but it doesn't matter
2427 * (the call will either return short or restart with a new uio).
2429 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2435 * Since we are using ring buffers, if we can't insert any more into
2436 * the output queue, we can assume the ring is full and that someone
2437 * forgot to set the high water mark correctly. We set it and then
2438 * proceed as normal.
2440 hiwat
= tp
->t_outq
.c_cc
- 1;
2445 * This can only occur if FLUSHO is set in t_lflag,
2446 * or if ttstart/oproc is synchronous (or very fast).
2448 if (tp
->t_outq
.c_cc
<= hiwat
) {
2451 if (flag
& IO_NDELAY
) {
2452 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2453 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2455 SET(tp
->t_state
, TS_SO_OLOWAT
);
2456 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2458 if (error
== EWOULDBLOCK
)
2467 * Rubout one character from the rawq of tp
2468 * as cleanly as possible.
2470 * Locks: Assumes tty_lock() is held prior to calling.
2473 ttyrub(int c
, struct tty
*tp
)
2479 TTY_LOCK_OWNED(tp
); /* debug assert */
2481 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2483 CLR(tp
->t_lflag
, FLUSHO
);
2484 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2485 if (tp
->t_rocount
== 0) {
2487 * Messed up by ttwrite; retype
2492 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2495 CLR(c
, ~TTY_CHARMASK
);
2496 switch (CCLASS(c
)) {
2498 if(!(ISSET(tp
->t_iflag
, IUTF8
) && CCONT(c
))) {
2507 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2511 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2515 savecol
= tp
->t_column
;
2516 SET(tp
->t_state
, TS_CNTTB
);
2517 SET(tp
->t_lflag
, FLUSHO
);
2518 tp
->t_column
= tp
->t_rocol
;
2519 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2520 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2522 CLR(tp
->t_lflag
, FLUSHO
);
2523 CLR(tp
->t_state
, TS_CNTTB
);
2525 /* savecol will now be length of the tab. */
2526 savecol
-= tp
->t_column
;
2527 tp
->t_column
+= savecol
;
2529 savecol
= 8; /* overflow fixup */
2530 while (--savecol
>= 0)
2531 (void)ttyoutput('\b', tp
);
2534 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2535 printf(PANICSTR
, c
, CCLASS(c
));
2537 panic(PANICSTR
, c
, CCLASS(c
));
2541 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2542 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2543 SET(tp
->t_state
, TS_ERASE
);
2544 (void)ttyoutput('\\', tp
);
2548 ttyecho(tp
->t_cc
[VERASE
], tp
);
2554 * Back over count characters, erasing them.
2556 * Locks: Assumes tty_lock() is held prior to calling.
2559 ttyrubo(struct tty
*tp
, int count
)
2561 TTY_LOCK_OWNED(tp
); /* debug assert */
2563 while (count
-- > 0) {
2564 (void)ttyoutput('\b', tp
);
2565 (void)ttyoutput(' ', tp
);
2566 (void)ttyoutput('\b', tp
);
2573 * Reprint the rawq line. Note, it is assumed that c_cc has already
2576 * Locks: Assumes tty_lock() is held prior to calling.
2579 ttyretype(struct tty
*tp
)
2584 TTY_LOCK_OWNED(tp
); /* debug assert */
2586 /* Echo the reprint character. */
2587 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2588 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2590 (void)ttyoutput('\n', tp
);
2594 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2595 * BIT OF FIRST CHAR.
2597 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2599 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2601 CLR(tp
->t_state
, TS_ERASE
);
2603 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2609 * Echo a typed character to the terminal.
2611 * Locks: Assumes tty_lock() is held prior to calling.
2614 ttyecho(int c
, struct tty
*tp
)
2616 TTY_LOCK_OWNED(tp
); /* debug assert */
2618 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2619 CLR(tp
->t_lflag
, FLUSHO
);
2620 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2621 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2622 ISSET(tp
->t_lflag
, EXTPROC
))
2624 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2625 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2626 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2627 (void)ttyoutput('^', tp
);
2628 CLR(c
, ~TTY_CHARMASK
);
2634 (void)ttyoutput(c
, tp
);
2639 * Wake up any readers on a tty.
2641 * Locks: Assumes tty_lock() is held for the call.
2644 ttwakeup(struct tty
*tp
)
2646 TTY_LOCK_OWNED(tp
); /* debug assert */
2648 selwakeup(&tp
->t_rsel
);
2649 KNOTE(&tp
->t_rsel
.si_note
, 1);
2650 if (ISSET(tp
->t_state
, TS_ASYNC
)) {
2652 * XXX: Callers may not revalidate it the tty is closed
2653 * XXX: out from under them by another thread, but we do
2654 * XXX: not support queued signals. This should be safe,
2655 * XXX: since the process we intend to wakeup is in the
2656 * XXX: process group, and will wake up because of the
2657 * XXX: signal anyway.
2660 tty_pgsignal(tp
, SIGIO
, 1);
2663 wakeup(TSA_HUP_OR_INPUT(tp
));
2670 * Wake up any writers on a tty.
2672 * Locks: Assumes tty_lock() is held prior to calling.
2675 ttwwakeup(struct tty
*tp
)
2677 TTY_LOCK_OWNED(tp
); /* debug assert */
2679 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2680 selwakeup(&tp
->t_wsel
);
2681 KNOTE(&tp
->t_wsel
.si_note
, 1);
2683 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2684 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2685 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2686 wakeup(TSA_OCOMPLETE(tp
));
2688 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2689 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2690 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2691 wakeup(TSA_OLOWAT(tp
));
2697 * Look up a code for a specified speed in a conversion table;
2698 * used by drivers to map software speed values to hardware parameters.
2700 * Notes: No locks are assumed for this function; it does not
2701 * directly access struct tty.
2704 ttspeedtab(int speed
, struct speedtab
*table
)
2706 for ( ; table
->sp_speed
!= -1; table
++)
2707 if (table
->sp_speed
== speed
)
2708 return (table
->sp_code
);
2714 * Set tty hi and low water marks.
2716 * Try to arrange the dynamics so there's about one second
2717 * from hi to low water.
2719 * Locks: Assumes tty_lock() is held prior to calling.
2722 ttsetwater(struct tty
*tp
)
2727 TTY_LOCK_OWNED(tp
); /* debug assert */
2729 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2731 cps
= tp
->t_ospeed
/ 10;
2732 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2734 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2735 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2739 /* ttyinfo has been converted to the MACH kernel */
2740 #include <mach/thread_info.h>
2742 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2743 extern kern_return_t
thread_info_internal(thread_t thread
,
2744 thread_flavor_t flavor
,
2745 thread_info_t thread_info_out
,
2746 mach_msg_type_number_t
*thread_info_count
);
2750 * Report on state of foreground process group.
2752 * Locks: Assumes tty_lock() is held prior to calling.
2755 ttyinfo_locked(struct tty
*tp
)
2764 struct timeval utime
;
2765 struct timeval stime
;
2766 thread_basic_info_data_t basic_info
;
2767 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2770 TTY_LOCK_OWNED(tp
); /* debug assert */
2772 if (ttycheckoutq(tp
,0) == 0)
2775 /* Print load average. */
2776 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2777 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2780 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2781 * that pending input will be retyped on BS.
2783 if (tp
->t_session
== NULL
) {
2784 ttyprintf(tp
, "not a controlling terminal\n");
2788 if (tp
->t_pgrp
== NULL
) {
2789 ttyprintf(tp
, "no foreground process group\n");
2793 /* first process in process group */
2794 /* XXX is there a need for pgrp lock ? */
2795 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2796 ttyprintf(tp
, "empty foreground process group\n");
2802 * Pick the most interesting process and copy some of its
2803 * state for printing later.
2807 /* the proc_compare is non blocking fn, no need to use iterator */
2808 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2809 if (proc_compare(pick
, p
)) {
2813 pickpid
= pick
->p_pid
;
2817 /* SAFE: All callers drop the lock on return */
2822 pick
= proc_find(pickpid
);
2823 if (pick
== PROC_NULL
)
2826 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2827 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2828 (thread
= vfs_context_thread(&uthread
->uu_context
)) == NULL
||
2829 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2830 ttyprintf(tp
, "foreground process without thread\n");
2836 switch(basic_info
.run_state
) {
2837 case TH_STATE_RUNNING
:
2840 case TH_STATE_STOPPED
:
2843 case TH_STATE_WAITING
:
2846 case TH_STATE_UNINTERRUPTIBLE
:
2847 state
= "uninterruptible";
2849 case TH_STATE_HALTED
:
2856 calcru(pick
, &utime
, &stime
, NULL
);
2859 /* Print command, pid, state, utime, and stime */
2860 ttyprintf(tp
, " cmd: %s %d %s %ld.%02du %ld.%02ds\n",
2864 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2865 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2871 * Returns 1 if p2 is "better" than p1
2873 * The algorithm for picking the "interesting" process is thus:
2875 * 1) Only foreground processes are eligible - implied.
2876 * 2) Runnable processes are favored over anything else. The runner
2877 * with the highest cpu utilization is picked (p_estcpu). Ties are
2878 * broken by picking the highest pid.
2879 * 3) The sleeper with the shortest sleep time is next.
2880 * 4) Further ties are broken by picking the highest pid.
2882 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2883 #define TESTAB(a, b) ((a)<<1 | (b))
2889 * Locks: pgrp_lock(p2) held on call to this function
2890 * tty_lock(tp) for p2's tty, for which p2 is the foreground
2891 * process, held on call to this function
2894 proc_compare(proc_t p1
, proc_t p2
)
2896 /* NOTE THIS FN needs to be NON BLOCKING */
2901 * see if at least one of them is runnable
2903 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2910 * tie - favor one with highest recent cpu utilization
2912 #ifdef _PROC_HAS_SCHEDINFO_
2913 /* Without the support the fields are always zero */
2914 if (p2
->p_estcpu
> p1
->p_estcpu
)
2916 if (p1
->p_estcpu
> p2
->p_estcpu
)
2918 #endif /* _PROC_HAS_SCHEDINFO_ */
2919 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2924 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2930 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2933 * pick the one with the smallest sleep time
2935 #ifdef _PROC_HAS_SCHEDINFO_
2936 /* Without the support the fields are always zero */
2937 if (p2
->p_slptime
> p1
->p_slptime
)
2939 if (p1
->p_slptime
> p2
->p_slptime
)
2941 #endif /* _PROC_HAS_SCHEDINFO_ */
2942 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2947 * Output char to tty; console putchar style.
2949 * Locks: Assumes tty_lock() is held prior to calling.
2951 * Notes: Only ever called from putchar() in subr_prf.c
2954 tputchar(int c
, struct tty
*tp
)
2956 TTY_LOCK_OWNED(tp
); /* debug assert */
2958 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2962 (void)ttyoutput('\r', tp
);
2963 (void)ttyoutput(c
, tp
);
2972 * Sleep on a wait channel waiting for an interrupt or a condition to come
2973 * true so that we are woken up.
2975 * Parameters: tp Tty going to sleep
2976 * chan The sleep channel (usually an address
2977 * of a structure member)
2978 * pri priority and flags
2979 * wmesg Wait message; shows up in debugger,
2980 * should show up in "ps", but doesn't
2981 * timo Timeout for the sleep
2983 * Returns: 0 Condition came true
2984 * ERESTART Upper layer must redrive the call;
2985 * this is usually done by the Libc
2986 * stub in user space
2987 * msleep0:EINTR Interrupted (usually a signal)
2988 * msleep0:ERESTART Interrupted (usually a masked signal)
2989 * msleep0:EWOULDBLOCK Timeout (timo) already expired
2991 * Locks: Assumes tty_lock() is held prior to calling.
2993 * Sleep on chan, returning ERESTART if tty changed while we napped and
2994 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by msleep0. If
2995 * the tty is revoked, restarting a pending call will redo validation done
2996 * at the start of the call.
2999 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
3007 /* Use of msleep0() avoids conversion timo/timespec/timo */
3008 error
= msleep0(chan
, &tp
->t_lock
, pri
, wmesg
, timo
, (int (*)(int))0);
3011 return (tp
->t_gen
== gen
? 0 : ERESTART
);
3016 * Allocate a tty structure and its associated buffers.
3020 * Returns: !NULL Address of new struct tty
3021 * NULL Error ("ENOMEM")
3023 * Locks: The tty_lock() of the returned tty is not held when it
3031 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
3033 /* XXX: default to TTYCLSIZE(1024) chars for now */
3034 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
3035 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
3036 /* output queue doesn't need quoting */
3037 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
3038 lck_mtx_init(&tp
->t_lock
, tty_lck_grp
, tty_lck_attr
);
3039 klist_init(&tp
->t_rsel
.si_note
);
3040 klist_init(&tp
->t_wsel
.si_note
);
3047 * Free a tty structure and its buffers.
3049 * Locks: The tty_lock() is assumed to not be held at the time of
3050 * the free; this functions destroys the mutex.
3053 ttyfree(struct tty
*tp
)
3055 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
3058 if (!(SLIST_EMPTY(&tp
->t_rsel
.si_note
) && SLIST_EMPTY(&tp
->t_wsel
.si_note
))) {
3059 panic("knotes hooked into a tty when the tty is freed.\n");
3063 clfree(&tp
->t_rawq
);
3064 clfree(&tp
->t_canq
);
3065 clfree(&tp
->t_outq
);
3066 lck_mtx_destroy(&tp
->t_lock
, tty_lck_grp
);
3072 * Locks: Assumes tty_lock() is held prior to calling.
3075 isbackground(proc_t p
, struct tty
*tp
)
3079 return (tp
->t_session
!= NULL
&& p
->p_pgrp
!= NULL
&& (p
->p_pgrp
!= tp
->t_pgrp
) && isctty_sp(p
, tp
, p
->p_pgrp
->pg_session
));
3083 isctty(proc_t p
, struct tty
*tp
)
3086 struct session
* sessp
;
3088 sessp
= proc_session(p
);
3089 retval
= (sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3090 session_rele(sessp
);
3095 isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
)
3097 return(sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);