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
)
1504 TTY_LOCK_OWNED(tp
); /* debug assert */
1508 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
))
1510 selrecord(p
, &tp
->t_rsel
, wql
);
1513 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1514 ISSET(tp
->t_state
, TS_CONNECTED
))
1515 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1518 selrecord(p
, &tp
->t_wsel
, wql
);
1526 * This is a wrapper for compatibility with the select vector used by
1527 * cdevsw. It relies on a proper xxxdevtotty routine.
1529 * Locks: Assumes tty_lock() is not held prior to calling.
1532 ttselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
1535 struct tty
*tp
= cdevsw
[major(dev
)].d_ttys
[minor(dev
)];
1538 rv
= ttyselect(tp
, rw
, wql
, p
);
1546 * Locks: Assumes tp is locked on entry, remains locked on exit
1549 ttnread(struct tty
*tp
)
1553 TTY_LOCK_OWNED(tp
); /* debug assert */
1555 if (ISSET(tp
->t_lflag
, PENDIN
))
1557 nread
= tp
->t_canq
.c_cc
;
1558 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1559 nread
+= tp
->t_rawq
.c_cc
;
1560 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1570 * Wait for output to drain.
1572 * Parameters: tp Tty on which to wait for output to drain
1574 * Returns: 0 Success
1575 * EIO t_timeout too small/expired
1576 * ttysleep:ERESTART Upper layer must redrive the call;
1577 * this is usually done by the Libc
1578 * stub in user space
1579 * ttysleep:EINTR Interrupted (usually a signal)
1581 * Notes: Called from proc_exit() and vproc_exit().
1583 * Locks: Assumes tp is locked on entry, remains locked on exit
1586 ttywait(struct tty
*tp
)
1590 TTY_LOCK_OWNED(tp
); /* debug assert */
1593 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1594 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1596 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1597 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1598 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1599 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1600 TTOPRI
| PCATCH
, "ttywai",
1603 if (error
== EWOULDBLOCK
)
1610 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1616 * Stop the underlying device driver.
1618 * Locks: Assumes tty_lock() is held prior to calling.
1621 ttystop(struct tty
*tp
, int rw
)
1623 TTY_LOCK_OWNED(tp
); /* debug assert */
1625 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1629 * Flush if successfully wait.
1631 * Locks: Assumes tty_lock() is held prior to calling.
1634 ttywflush(struct tty
*tp
)
1638 TTY_LOCK_OWNED(tp
); /* debug assert */
1640 if ((error
= ttywait(tp
)) == 0)
1641 ttyflush(tp
, FREAD
);
1646 * Flush tty read and/or write queues, notifying anyone waiting.
1648 * Locks: Assumes tty_lock() is held prior to calling.
1651 ttyflush(struct tty
*tp
, int rw
)
1653 TTY_LOCK_OWNED(tp
); /* debug assert */
1659 FLUSHQ(&tp
->t_outq
);
1660 CLR(tp
->t_state
, TS_TTSTOP
);
1664 FLUSHQ(&tp
->t_canq
);
1665 FLUSHQ(&tp
->t_rawq
);
1666 CLR(tp
->t_lflag
, PENDIN
);
1669 CLR(tp
->t_state
, TS_LOCAL
);
1671 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1673 FLUSHQ(&tp
->t_outq
);
1677 * Don't let leave any state that might clobber the
1678 * next line discipline (although we should do more
1679 * to send the START char). Not clearing the state
1680 * may have caused the "putc to a clist with no
1681 * reserved cblocks" panic/printf.
1683 CLR(tp
->t_state
, TS_TBLOCK
);
1685 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1686 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1688 * XXX wait a bit in the hope that the stop
1689 * character (if any) will go out. Waiting
1690 * isn't good since it allows races. This
1691 * will be fixed when the stop character is
1692 * put in a special queue. Don't bother with
1693 * the checks in ttywait() since the timeout
1696 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1697 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1700 * Don't try sending the stop character again.
1702 CLR(tp
->t_state
, TS_TBLOCK
);
1709 FLUSHQ(&tp
->t_outq
);
1715 * Copy in the default termios characters.
1717 * Locks: Assumes tty_lock() is held prior to calling.
1719 * Notes: No assertion; tp is not in scope.
1722 termioschars(struct termios
*t
)
1724 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1729 * Handle input high water. Send stop character for the IXOFF case. Turn
1730 * on our input flow control bit and propagate the changes to the driver.
1731 * XXX the stop character should be put in a special high priority queue.
1733 * Locks: Assumes tty_lock() is held for the call.
1736 ttyblock(struct tty
*tp
)
1738 TTY_LOCK_OWNED(tp
); /* debug assert */
1740 SET(tp
->t_state
, TS_TBLOCK
);
1741 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1742 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1743 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1749 * Handle input low water. Send start character for the IXOFF case. Turn
1750 * off our input flow control bit and propagate the changes to the driver.
1751 * XXX the start character should be put in a special high priority queue.
1753 * Locks: Assumes tty_lock() is held for the call.
1756 ttyunblock(struct tty
*tp
)
1758 TTY_LOCK_OWNED(tp
); /* debug assert */
1760 CLR(tp
->t_state
, TS_TBLOCK
);
1761 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1762 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1763 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1773 * Parameters: tp tty on which to start output
1775 * Returns: 0 Success
1777 * Locks: Assumes tty_lock() is held for the call.
1779 * Notes: This function might as well be void; it always returns success
1781 * Called from ttioctl_locked(), LDISC routines, and
1782 * ttycheckoutq(), ttyblock(), ttyunblock(), and tputchar()
1785 ttstart(struct tty
*tp
)
1787 TTY_LOCK_OWNED(tp
); /* debug assert */
1789 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1799 * "close" a line discipline
1801 * Locks: Assumes tty_lock() is held prior to calling.
1804 ttylclose(struct tty
*tp
, int flag
)
1806 TTY_LOCK_OWNED(tp
); /* debug assert */
1808 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1809 ttyflush(tp
, FREAD
| FWRITE
);
1818 * Handle modem control transition on a tty.
1819 * Flag indicates new state of carrier.
1820 * Returns 0 if the line should be turned off, otherwise 1.
1822 * Locks: Assumes tty_lock() is held prior to calling.
1825 ttymodem(struct tty
*tp
, int flag
)
1827 int rval
= 1; /* default return value */
1829 TTY_LOCK_OWNED(tp
); /* debug assert */
1831 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1833 * MDMBUF: do flow control according to carrier flag
1834 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1835 * works if IXON and IXANY are clear.
1838 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1839 CLR(tp
->t_state
, TS_TTSTOP
);
1841 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1842 SET(tp
->t_state
, TS_CAR_OFLOW
);
1843 SET(tp
->t_state
, TS_TTSTOP
);
1846 } else if (flag
== 0) {
1850 CLR(tp
->t_state
, TS_CARR_ON
);
1851 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1852 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1853 SET(tp
->t_state
, TS_ZOMBIE
);
1854 CLR(tp
->t_state
, TS_CONNECTED
);
1855 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1856 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1857 ttyflush(tp
, FREAD
| FWRITE
);
1865 SET(tp
->t_state
, TS_CARR_ON
);
1866 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1867 SET(tp
->t_state
, TS_CONNECTED
);
1868 wakeup(TSA_CARR_ON(tp
));
1879 * Reinput pending characters after state switch
1882 * Locks: Assumes tty_lock() is held for the call.
1885 ttypend(struct tty
*tp
)
1890 TTY_LOCK_OWNED(tp
); /* debug assert */
1892 CLR(tp
->t_lflag
, PENDIN
);
1893 SET(tp
->t_state
, TS_TYPEN
);
1895 tp
->t_rawq
.c_cc
= 0;
1896 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= NULL
;
1897 while ((c
= getc(&tq
)) >= 0)
1899 CLR(tp
->t_state
, TS_TYPEN
);
1906 * Process a read call on a tty device.
1908 * Locks: Assumes tty_lock() is held prior to calling.
1911 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1916 cc_t
*cc
= tp
->t_cc
;
1917 proc_t p
= current_proc();
1918 int first
, error
= 0;
1919 int has_etime
= 0, last_cc
= 0;
1920 long slp
= 0; /* XXX this should be renamed `timo'. */
1924 TTY_LOCK_OWNED(tp
); /* debug assert */
1926 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1929 lflag
= tp
->t_lflag
;
1931 * take pending input first
1933 if (ISSET(lflag
, PENDIN
)) {
1935 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1939 * Signal the process if it's in the background.
1941 if (isbackground(p
, tp
)) {
1942 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1943 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1944 p
->p_lflag
& P_LPPWAIT
) {
1949 if (pg
== PGRP_NULL
) {
1953 if (pg
->pg_jobc
== 0) {
1954 /* SAFE: All callers drop the lock on return */
1961 /* SAFE: All callers drop the lock on return */
1963 pgsignal(pg
, SIGTTIN
, 1);
1968 * We signalled ourself, so we need to act as if we
1969 * have been "interrupted" from a "sleep" to act on
1970 * the signal. If it's a signal that stops the
1971 * process, that's handled in the signal sending code.
1977 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1978 /* EOF - returning 0 */
1983 * If canonical, use the canonical queue,
1984 * else use the raw queue.
1986 * (should get rid of clists...)
1988 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
1990 if (flag
& IO_NDELAY
) {
1993 if (ISSET(lflag
, ICANON
) || cc
[VMIN
] != 0) {
1994 error
= EWOULDBLOCK
;
1996 /* else polling - returning 0 */
1999 if (!ISSET(lflag
, ICANON
)) {
2002 struct timeval timecopy
;
2003 struct timeval etime
= {0, 0}; /* protected by !has_etime */
2006 * Check each of the four combinations.
2007 * (m > 0 && t == 0) is the normal read case.
2008 * It should be fairly efficient, so we check that and its
2009 * companion case (m == 0 && t == 0) first.
2010 * For the other two cases, we compute the target sleep time
2019 /* m, t and qp->c_cc are all 0. 0 is enough input. */
2022 t
*= 100000; /* time in us */
2023 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
2024 ((t1).tv_usec - (t2).tv_usec))
2030 microuptime(&timecopy
);
2032 /* first character, start timer */
2035 etime
.tv_sec
= t
/ 1000000;
2036 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2037 timeradd(&etime
, &timecopy
, &etime
);
2040 } else if (qp
->c_cc
> last_cc
) {
2041 /* got a character, restart timer */
2043 etime
.tv_sec
= t
/ 1000000;
2044 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2045 timeradd(&etime
, &timecopy
, &etime
);
2049 /* nothing, check expiration */
2050 if (timercmp(&etime
, &timecopy
, <=))
2053 slp
= diff(etime
, timecopy
);
2056 } else { /* m == 0 */
2059 microuptime(&timecopy
);
2063 etime
.tv_sec
= t
/ 1000000;
2064 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2065 timeradd(&etime
, &timecopy
, &etime
);
2069 if (timercmp(&etime
, &timecopy
, <=)) {
2070 /* Timed out, but 0 is enough input. */
2073 slp
= diff(etime
, timecopy
);
2078 * Rounding down may make us wake up just short
2079 * of the target, so we round up.
2080 * The formula is ceiling(slp * hz/1000000).
2081 * 32-bit arithmetic is enough for hz < 169.
2082 * XXX see hzto() for how to avoid overflow if hz
2083 * is large (divide by `tick' and/or arrange to
2084 * use hzto() if hz is large).
2086 slp
= (long) (((u_int32_t
)slp
* hz
) + 999999) / 1000000;
2089 if (qp
->c_cc
<= 0) {
2092 * There is no input, or not enough input and we can block.
2094 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
2095 ISSET(tp
->t_state
, TS_CONNECTED
) ?
2096 "ttyin" : "ttyhup", (int)slp
);
2097 if (error
== EWOULDBLOCK
)
2103 * XXX what happens if another process eats some input
2104 * while we are asleep (not just here)? It would be
2105 * safest to detect changes and reset our state variables
2106 * (has_stime and last_cc).
2113 * Input present, check for input mapping and processing.
2116 if (ISSET(lflag
, ICANON
)
2117 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
2123 icc
= min(uio_resid(uio
), IBUFSIZ
);
2124 icc
= q_to_b(qp
, (u_char
*)ibuf
, icc
);
2130 error
= uiomove(ibuf
, icc
, uio
);
2132 * XXX if there was an error then we should ungetc() the
2133 * unmoved chars and reduce icc here.
2137 if (uio_resid(uio
) == 0)
2151 * delayed suspend (^Y)
2153 if (CCEQ(cc
[VDSUSP
], c
) &&
2154 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
2156 * SAFE: All callers drop the lock on return and
2157 * SAFE: current thread will not change out from
2158 * SAFE: under us in the "goto loop" case.
2161 tty_pgsignal(tp
, SIGTSTP
, 1);
2164 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
,
2173 * Interpret EOF only in canonical mode.
2175 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
2178 * Give user character.
2180 error
= ureadc(c
, uio
);
2182 /* XXX should ungetc(c, qp). */
2184 if (uio_resid(uio
) == 0)
2187 * In canonical mode check for a "break character"
2188 * marking the end of a "line of input".
2190 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
2197 * Look to unblock input now that (presumably)
2198 * the input queue has gone down.
2200 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
2201 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
2210 * Check the output queue on tp for space for a kernel message (from uprintf
2211 * or tprintf). Allow some space over the normal hiwater mark so we don't
2212 * lose messages due to normal flow control, but don't let the tty run amok.
2213 * Sleeps here are not interruptible, but we return prematurely if new signals
2216 * Locks: Assumes tty_lock() is held before calling
2218 * Notes: This function is called from tprintf() in subr_prf.c
2221 ttycheckoutq(struct tty
*tp
, int wait
)
2227 TTY_LOCK_OWNED(tp
); /* debug assert */
2229 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2231 hiwat
= tp
->t_hiwat
;
2232 oldsig
= wait
? ut
->uu_siglist
: 0;
2233 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
2234 while (tp
->t_outq
.c_cc
> hiwat
) {
2236 if (tp
->t_outq
.c_cc
<= hiwat
)
2238 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
2241 SET(tp
->t_state
, TS_SO_OLOWAT
);
2242 ttysleep(tp
, TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
2251 * Process a write call on a tty device.
2253 * Locks: Assumes tty_lock() is held prior to calling.
2256 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
2261 int i
, hiwat
, error
;
2267 TTY_LOCK_OWNED(tp
); /* debug assert */
2269 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2270 hiwat
= tp
->t_hiwat
;
2271 count
= uio_resid(uio
);
2275 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2276 if (uio_resid(uio
) == count
)
2280 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2281 if (flag
& IO_NDELAY
) {
2282 error
= EWOULDBLOCK
;
2285 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2292 * Signal the process if it's in the background.
2295 if (isbackground(p
, tp
) &&
2296 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& P_LPPWAIT
) == 0 &&
2297 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2298 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2301 if (pg
== PGRP_NULL
) {
2305 if (pg
->pg_jobc
== 0) {
2306 /* SAFE: All callers drop the lock on return */
2313 /* SAFE: All callers drop the lock on return */
2315 pgsignal(pg
, SIGTTOU
, 1);
2319 * We signalled ourself, so we need to act as if we
2320 * have been "interrupted" from a "sleep" to act on
2321 * the signal. If it's a signal that stops the
2322 * process, that's handled in the signal sending code.
2328 * Process the user's data in at most OBUFSIZ chunks. Perform any
2329 * output translation. Keep track of high water mark, sleep on
2330 * overflow awaiting device aid in acquiring new space.
2332 while (uio_resid(uio
) > 0 || cc
> 0) {
2333 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2334 uio_setresid(uio
, 0);
2337 if (tp
->t_outq
.c_cc
> hiwat
)
2340 * Grab a hunk of data from the user, unless we have some
2341 * leftover from last time.
2344 cc
= min(uio_resid(uio
), OBUFSIZ
);
2346 error
= uiomove(cp
, cc
, uio
);
2353 * If nothing fancy need be done, grab those characters we
2354 * can handle without any of ttyoutput's processing and
2355 * just transfer them to the output q. For those chars
2356 * which require special processing (as indicated by the
2357 * bits in char_type), call ttyoutput. After processing
2358 * a hunk of data, look for FLUSHO so ^O's will take effect
2362 if (!ISSET(tp
->t_oflag
, OPOST
))
2365 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2366 char_type
, CCLASSMASK
);
2368 * If ce is zero, then we're processing
2369 * a special character through ttyoutput.
2373 if (ttyoutput(*cp
, tp
) >= 0) {
2379 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2380 tp
->t_outq
.c_cc
> hiwat
)
2386 * A bunch of normal characters have been found.
2387 * Transfer them en masse to the output queue and
2388 * continue processing at the top of the loop.
2389 * If there are any further characters in this
2390 * <= OBUFSIZ chunk, the first should be a character
2391 * requiring special handling by ttyoutput.
2394 i
= b_to_q((u_char
*)cp
, ce
, &tp
->t_outq
);
2397 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2403 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2404 tp
->t_outq
.c_cc
> hiwat
)
2411 * If cc is nonzero, we leave the uio structure inconsistent, as the
2412 * offset and iov pointers have moved forward, but it doesn't matter
2413 * (the call will either return short or restart with a new uio).
2415 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2421 * Since we are using ring buffers, if we can't insert any more into
2422 * the output queue, we can assume the ring is full and that someone
2423 * forgot to set the high water mark correctly. We set it and then
2424 * proceed as normal.
2426 hiwat
= tp
->t_outq
.c_cc
- 1;
2431 * This can only occur if FLUSHO is set in t_lflag,
2432 * or if ttstart/oproc is synchronous (or very fast).
2434 if (tp
->t_outq
.c_cc
<= hiwat
) {
2437 if (flag
& IO_NDELAY
) {
2438 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2439 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2441 SET(tp
->t_state
, TS_SO_OLOWAT
);
2442 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2444 if (error
== EWOULDBLOCK
)
2453 * Rubout one character from the rawq of tp
2454 * as cleanly as possible.
2456 * Locks: Assumes tty_lock() is held prior to calling.
2459 ttyrub(int c
, struct tty
*tp
)
2465 TTY_LOCK_OWNED(tp
); /* debug assert */
2467 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2469 CLR(tp
->t_lflag
, FLUSHO
);
2470 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2471 if (tp
->t_rocount
== 0) {
2473 * Messed up by ttwrite; retype
2478 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2481 CLR(c
, ~TTY_CHARMASK
);
2482 switch (CCLASS(c
)) {
2484 if(!(ISSET(tp
->t_iflag
, IUTF8
) && CCONT(c
))) {
2493 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2497 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2501 savecol
= tp
->t_column
;
2502 SET(tp
->t_state
, TS_CNTTB
);
2503 SET(tp
->t_lflag
, FLUSHO
);
2504 tp
->t_column
= tp
->t_rocol
;
2505 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2506 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2508 CLR(tp
->t_lflag
, FLUSHO
);
2509 CLR(tp
->t_state
, TS_CNTTB
);
2511 /* savecol will now be length of the tab. */
2512 savecol
-= tp
->t_column
;
2513 tp
->t_column
+= savecol
;
2515 savecol
= 8; /* overflow fixup */
2516 while (--savecol
>= 0)
2517 (void)ttyoutput('\b', tp
);
2520 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2521 printf(PANICSTR
, c
, CCLASS(c
));
2523 panic(PANICSTR
, c
, CCLASS(c
));
2527 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2528 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2529 SET(tp
->t_state
, TS_ERASE
);
2530 (void)ttyoutput('\\', tp
);
2534 ttyecho(tp
->t_cc
[VERASE
], tp
);
2540 * Back over count characters, erasing them.
2542 * Locks: Assumes tty_lock() is held prior to calling.
2545 ttyrubo(struct tty
*tp
, int count
)
2547 TTY_LOCK_OWNED(tp
); /* debug assert */
2549 while (count
-- > 0) {
2550 (void)ttyoutput('\b', tp
);
2551 (void)ttyoutput(' ', tp
);
2552 (void)ttyoutput('\b', tp
);
2559 * Reprint the rawq line. Note, it is assumed that c_cc has already
2562 * Locks: Assumes tty_lock() is held prior to calling.
2565 ttyretype(struct tty
*tp
)
2570 TTY_LOCK_OWNED(tp
); /* debug assert */
2572 /* Echo the reprint character. */
2573 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2574 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2576 (void)ttyoutput('\n', tp
);
2580 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2581 * BIT OF FIRST CHAR.
2583 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2585 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2587 CLR(tp
->t_state
, TS_ERASE
);
2589 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2595 * Echo a typed character to the terminal.
2597 * Locks: Assumes tty_lock() is held prior to calling.
2600 ttyecho(int c
, struct tty
*tp
)
2602 TTY_LOCK_OWNED(tp
); /* debug assert */
2604 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2605 CLR(tp
->t_lflag
, FLUSHO
);
2606 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2607 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2608 ISSET(tp
->t_lflag
, EXTPROC
))
2610 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2611 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2612 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2613 (void)ttyoutput('^', tp
);
2614 CLR(c
, ~TTY_CHARMASK
);
2620 (void)ttyoutput(c
, tp
);
2625 * Wake up any readers on a tty.
2627 * Locks: Assumes tty_lock() is held for the call.
2630 ttwakeup(struct tty
*tp
)
2632 TTY_LOCK_OWNED(tp
); /* debug assert */
2634 selwakeup(&tp
->t_rsel
);
2635 KNOTE(&tp
->t_rsel
.si_note
, 1);
2636 if (ISSET(tp
->t_state
, TS_ASYNC
)) {
2638 * XXX: Callers may not revalidate it the tty is closed
2639 * XXX: out from under them by another thread, but we do
2640 * XXX: not support queued signals. This should be safe,
2641 * XXX: since the process we intend to wakeup is in the
2642 * XXX: process group, and will wake up because of the
2643 * XXX: signal anyway.
2646 tty_pgsignal(tp
, SIGIO
, 1);
2649 wakeup(TSA_HUP_OR_INPUT(tp
));
2656 * Wake up any writers on a tty.
2658 * Locks: Assumes tty_lock() is held prior to calling.
2661 ttwwakeup(struct tty
*tp
)
2663 TTY_LOCK_OWNED(tp
); /* debug assert */
2665 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2666 selwakeup(&tp
->t_wsel
);
2667 KNOTE(&tp
->t_wsel
.si_note
, 1);
2669 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2670 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2671 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2672 wakeup(TSA_OCOMPLETE(tp
));
2674 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2675 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2676 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2677 wakeup(TSA_OLOWAT(tp
));
2683 * Look up a code for a specified speed in a conversion table;
2684 * used by drivers to map software speed values to hardware parameters.
2686 * Notes: No locks are assumed for this function; it does not
2687 * directly access struct tty.
2690 ttspeedtab(int speed
, struct speedtab
*table
)
2692 for ( ; table
->sp_speed
!= -1; table
++)
2693 if (table
->sp_speed
== speed
)
2694 return (table
->sp_code
);
2700 * Set tty hi and low water marks.
2702 * Try to arrange the dynamics so there's about one second
2703 * from hi to low water.
2705 * Locks: Assumes tty_lock() is held prior to calling.
2708 ttsetwater(struct tty
*tp
)
2713 TTY_LOCK_OWNED(tp
); /* debug assert */
2715 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2717 cps
= tp
->t_ospeed
/ 10;
2718 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2720 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2721 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2725 /* ttyinfo has been converted to the MACH kernel */
2726 #include <mach/thread_info.h>
2728 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2729 extern kern_return_t
thread_info_internal(thread_t thread
,
2730 thread_flavor_t flavor
,
2731 thread_info_t thread_info_out
,
2732 mach_msg_type_number_t
*thread_info_count
);
2736 * Report on state of foreground process group.
2738 * Locks: Assumes tty_lock() is held prior to calling.
2741 ttyinfo_locked(struct tty
*tp
)
2750 struct timeval utime
;
2751 struct timeval stime
;
2752 thread_basic_info_data_t basic_info
;
2753 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2756 TTY_LOCK_OWNED(tp
); /* debug assert */
2758 if (ttycheckoutq(tp
,0) == 0)
2761 /* Print load average. */
2762 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2763 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2766 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2767 * that pending input will be retyped on BS.
2769 if (tp
->t_session
== NULL
) {
2770 ttyprintf(tp
, "not a controlling terminal\n");
2774 if (tp
->t_pgrp
== NULL
) {
2775 ttyprintf(tp
, "no foreground process group\n");
2779 /* first process in process group */
2780 /* XXX is there a need for pgrp lock ? */
2781 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2782 ttyprintf(tp
, "empty foreground process group\n");
2788 * Pick the most interesting process and copy some of its
2789 * state for printing later.
2793 /* the proc_compare is non blocking fn, no need to use iterator */
2794 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2795 if (proc_compare(pick
, p
)) {
2799 pickpid
= pick
->p_pid
;
2803 /* SAFE: All callers drop the lock on return */
2808 pick
= proc_find(pickpid
);
2809 if (pick
== PROC_NULL
)
2812 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2813 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2814 (thread
= vfs_context_thread(&uthread
->uu_context
)) == NULL
||
2815 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2816 ttyprintf(tp
, "foreground process without thread\n");
2822 switch(basic_info
.run_state
) {
2823 case TH_STATE_RUNNING
:
2826 case TH_STATE_STOPPED
:
2829 case TH_STATE_WAITING
:
2832 case TH_STATE_UNINTERRUPTIBLE
:
2833 state
= "uninterruptible";
2835 case TH_STATE_HALTED
:
2842 calcru(pick
, &utime
, &stime
, NULL
);
2845 /* Print command, pid, state, utime, and stime */
2846 ttyprintf(tp
, " cmd: %s %d %s %ld.%02du %ld.%02ds\n",
2850 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2851 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2857 * Returns 1 if p2 is "better" than p1
2859 * The algorithm for picking the "interesting" process is thus:
2861 * 1) Only foreground processes are eligible - implied.
2862 * 2) Runnable processes are favored over anything else. The runner
2863 * with the highest cpu utilization is picked (p_estcpu). Ties are
2864 * broken by picking the highest pid.
2865 * 3) The sleeper with the shortest sleep time is next.
2866 * 4) Further ties are broken by picking the highest pid.
2868 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2869 #define TESTAB(a, b) ((a)<<1 | (b))
2875 * Locks: pgrp_lock(p2) held on call to this function
2876 * tty_lock(tp) for p2's tty, for which p2 is the foreground
2877 * process, held on call to this function
2880 proc_compare(proc_t p1
, proc_t p2
)
2882 /* NOTE THIS FN needs to be NON BLOCKING */
2887 * see if at least one of them is runnable
2889 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2896 * tie - favor one with highest recent cpu utilization
2898 #ifdef _PROC_HAS_SCHEDINFO_
2899 /* Without the support the fields are always zero */
2900 if (p2
->p_estcpu
> p1
->p_estcpu
)
2902 if (p1
->p_estcpu
> p2
->p_estcpu
)
2904 #endif /* _PROC_HAS_SCHEDINFO_ */
2905 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2910 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2916 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2919 * pick the one with the smallest sleep time
2921 #ifdef _PROC_HAS_SCHEDINFO_
2922 /* Without the support the fields are always zero */
2923 if (p2
->p_slptime
> p1
->p_slptime
)
2925 if (p1
->p_slptime
> p2
->p_slptime
)
2927 #endif /* _PROC_HAS_SCHEDINFO_ */
2928 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2933 * Output char to tty; console putchar style.
2935 * Locks: Assumes tty_lock() is held prior to calling.
2937 * Notes: Only ever called from putchar() in subr_prf.c
2940 tputchar(int c
, struct tty
*tp
)
2942 TTY_LOCK_OWNED(tp
); /* debug assert */
2944 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2948 (void)ttyoutput('\r', tp
);
2949 (void)ttyoutput(c
, tp
);
2958 * Sleep on a wait channel waiting for an interrupt or a condition to come
2959 * true so that we are woken up.
2961 * Parameters: tp Tty going to sleep
2962 * chan The sleep channel (usually an address
2963 * of a structure member)
2964 * pri priority and flags
2965 * wmesg Wait message; shows up in debugger,
2966 * should show up in "ps", but doesn't
2967 * timo Timeout for the sleep
2969 * Returns: 0 Condition came true
2970 * ERESTART Upper layer must redrive the call;
2971 * this is usually done by the Libc
2972 * stub in user space
2973 * msleep0:EINTR Interrupted (usually a signal)
2974 * msleep0:ERESTART Interrupted (usually a masked signal)
2975 * msleep0:EWOULDBLOCK Timeout (timo) already expired
2977 * Locks: Assumes tty_lock() is held prior to calling.
2979 * Sleep on chan, returning ERESTART if tty changed while we napped and
2980 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by msleep0. If
2981 * the tty is revoked, restarting a pending call will redo validation done
2982 * at the start of the call.
2985 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
2993 /* Use of msleep0() avoids conversion timo/timespec/timo */
2994 error
= msleep0(chan
, &tp
->t_lock
, pri
, wmesg
, timo
, (int (*)(int))0);
2997 return (tp
->t_gen
== gen
? 0 : ERESTART
);
3002 * Allocate a tty structure and its associated buffers.
3006 * Returns: !NULL Address of new struct tty
3007 * NULL Error ("ENOMEM")
3009 * Locks: The tty_lock() of the returned tty is not held when it
3017 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
3019 /* XXX: default to TTYCLSIZE(1024) chars for now */
3020 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
3021 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
3022 /* output queue doesn't need quoting */
3023 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
3024 lck_mtx_init(&tp
->t_lock
, tty_lck_grp
, tty_lck_attr
);
3031 * Free a tty structure and its buffers.
3033 * Locks: The tty_lock() is assumed to not be held at the time of
3034 * the free; this functions destroys the mutex.
3037 ttyfree(struct tty
*tp
)
3039 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
3041 clfree(&tp
->t_rawq
);
3042 clfree(&tp
->t_canq
);
3043 clfree(&tp
->t_outq
);
3044 lck_mtx_destroy(&tp
->t_lock
, tty_lck_grp
);
3050 * Locks: Assumes tty_lock() is held prior to calling.
3053 isbackground(proc_t p
, struct tty
*tp
)
3057 return (tp
->t_session
!= NULL
&& p
->p_pgrp
!= NULL
&& (p
->p_pgrp
!= tp
->t_pgrp
) && isctty_sp(p
, tp
, p
->p_pgrp
->pg_session
));
3061 isctty(proc_t p
, struct tty
*tp
)
3064 struct session
* sessp
;
3066 sessp
= proc_session(p
);
3067 retval
= (sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3068 session_rele(sessp
);
3073 isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
)
3075 return(sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);