2 * Copyright (c) 1997-2017 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 */
116 #include <kern/waitq.h>
117 #include <libkern/section_keywords.h>
119 static lck_grp_t
*tty_lck_grp
;
120 static lck_grp_attr_t
*tty_lck_grp_attr
;
121 static lck_attr_t
*tty_lck_attr
;
123 __private_extern__
int ttnread(struct tty
*tp
);
124 static void ttyecho(int c
, struct tty
*tp
);
125 static int ttyoutput(int c
, struct tty
*tp
);
126 static void ttypend(struct tty
*tp
);
127 static void ttyretype(struct tty
*tp
);
128 static void ttyrub(int c
, struct tty
*tp
);
129 static void ttyrubo(struct tty
*tp
, int count
);
130 static void ttystop(struct tty
*tp
, int rw
);
131 static void ttyunblock(struct tty
*tp
);
132 static int ttywflush(struct tty
*tp
);
133 static int proc_compare(proc_t p1
, proc_t p2
);
135 void ttyhold(struct tty
*tp
);
136 static void ttydeallocate(struct tty
*tp
);
138 static int isctty(proc_t p
, struct tty
*tp
);
139 static int isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
);
142 * Table with character classes and parity. The 8th bit indicates parity,
143 * the 7th bit indicates the character is an alphameric or underscore (for
144 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
145 * are 0 then the character needs no special processing on output; classes
146 * other than 0 might be translated or (not currently) require delays.
148 #define E 0x00 /* Even parity. */
149 #define O 0x80 /* Odd parity. */
150 #define PARITY(c) (char_type[c] & O)
152 #define ALPHA 0x40 /* Alpha or underscore. */
153 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
155 #define CCLASSMASK 0x3f
156 #define CCLASS(c) (char_type[c] & CCLASSMASK)
157 /* 0b10xxxxxx is the mask for UTF-8 continuations */
158 #define CCONT(c) ((c & 0xc0) == 0x80)
163 #define NA ORDINARY | ALPHA
169 static u_char
const char_type
[] = {
170 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* nul - bel */
171 O
|BS
, E
|TB
, E
|NL
, O
|CC
, E
|VT
, O
|CR
, O
|CC
, E
|CC
, /* bs - si */
172 O
|CC
, E
|CC
, E
|CC
, O
|CC
, E
|CC
, O
|CC
, O
|CC
, E
|CC
, /* dle - etb */
173 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* can - us */
174 O
|NO
, E
|NO
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* sp - ' */
175 E
|NO
, O
|NO
, O
|NO
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|NO
, /* ( - / */
176 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* 0 - 7 */
177 O
|NA
, E
|NA
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* 8 - ? */
178 O
|NO
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* @ - G */
179 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* H - O */
180 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* P - W */
181 O
|NA
, E
|NA
, E
|NA
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, O
|NA
, /* X - _ */
182 E
|NO
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* ` - g */
183 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* h - o */
184 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* p - w */
185 E
|NA
, O
|NA
, O
|NA
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|CC
, /* x - del */
187 * Meta chars; should be settable per character set;
188 * for now, treat them all as normal characters.
190 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
191 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
192 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
193 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
194 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
195 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
196 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
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
,
216 /* Macros to clear/set/test flags. */
217 #define SET(t, f) (t) |= (f)
218 #define CLR(t, f) (t) &= ~(f)
219 #define ISSET(t, f) ((t) & (f))
222 * Input control starts when we would not be able to fit the maximum
223 * contents of the ping-pong buffers and finishes when we would be able
224 * to fit that much plus 1/8 more.
226 #define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */
227 #define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */
230 termios32to64(struct termios32
*in
, struct user_termios
*out
)
232 out
->c_iflag
= (user_tcflag_t
)in
->c_iflag
;
233 out
->c_oflag
= (user_tcflag_t
)in
->c_oflag
;
234 out
->c_cflag
= (user_tcflag_t
)in
->c_cflag
;
235 out
->c_lflag
= (user_tcflag_t
)in
->c_lflag
;
237 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
238 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
240 out
->c_ispeed
= (user_speed_t
)in
->c_ispeed
;
241 out
->c_ospeed
= (user_speed_t
)in
->c_ospeed
;
245 termios64to32(struct user_termios
*in
, struct termios32
*out
)
247 out
->c_iflag
= (tcflag_t
)in
->c_iflag
;
248 out
->c_oflag
= (tcflag_t
)in
->c_oflag
;
249 out
->c_cflag
= (tcflag_t
)in
->c_cflag
;
250 out
->c_lflag
= (tcflag_t
)in
->c_lflag
;
252 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
253 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
255 out
->c_ispeed
= (speed_t
)in
->c_ispeed
;
256 out
->c_ospeed
= (speed_t
)in
->c_ospeed
;
263 * Initialize the tty line discipline subsystem.
269 * Locks: No ttys can be allocated and no tty locks can be used
270 * until after this function is called
272 * Notes: The intent of this is to set up a log group attribute,
273 * lock group, and loc atribute for subsequent per-tty locks.
274 * This function is called early in bsd_init(), prior to the
275 * console device initialization.
280 tty_lck_grp_attr
= lck_grp_attr_alloc_init();
281 tty_lck_grp
= lck_grp_alloc_init("tty", tty_lck_grp_attr
);
282 tty_lck_attr
= lck_attr_alloc_init();
289 * Lock the requested tty structure.
291 * Parameters: tp The tty we want to lock
295 * Locks: On return, tp is locked
298 tty_lock(struct tty
*tp
)
300 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
301 lck_mtx_lock(&tp
->t_lock
);
308 * Unlock the requested tty structure.
310 * Parameters: tp The tty we want to unlock
314 * Locks: On return, tp is unlocked
317 tty_unlock(struct tty
*tp
)
319 TTY_LOCK_OWNED(tp
); /* debug assert */
320 lck_mtx_unlock(&tp
->t_lock
);
326 * Initial open of tty, or (re)entry to standard tty line discipline.
328 * Locks: Assumes tty_lock() is held prior to calling.
331 ttyopen(dev_t device
, struct tty
*tp
)
333 TTY_LOCK_OWNED(tp
); /* debug assert */
337 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
338 SET(tp
->t_state
, TS_ISOPEN
);
339 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
340 SET(tp
->t_state
, TS_CONNECTED
); }
341 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
350 * Handle close() on a tty line: flush and set to initial state,
351 * bumping generation number so that pending read/write calls
352 * can detect recycling of the tty.
353 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
354 * and l_close() should have flushed, but we repeat the spltty() and
355 * the flush in case there are buggy callers.
357 * Locks: Assumes tty_lock() is held prior to calling.
360 ttyclose(struct tty
*tp
)
363 struct session
* oldsessp
;
366 TTY_LOCK_OWNED(tp
); /* debug assert */
373 * Closing current console tty; disable printing of console
374 * messages at bottom-level driver.
376 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
377 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
380 ttyflush(tp
, FREAD
| FWRITE
);
383 tp
->t_line
= TTYDISC
;
386 oldsessp
= tp
->t_session
;
388 tp
->t_session
= NULL
;
389 if (oldsessp
!= SESSION_NULL
)
390 oldsessp
->s_ttypgrpid
= NO_PID
;
392 /* drop the reference on prev session and pgrp */
393 /* SAFE: All callers drop the lock on return */
395 if (oldsessp
!= SESSION_NULL
)
396 session_rele(oldsessp
);
397 if (oldpg
!= PGRP_NULL
)
401 SLIST_FOREACH(kn
, &tp
->t_wsel
.si_note
, kn_selnext
) {
402 KNOTE_DETACH(&tp
->t_wsel
.si_note
, kn
);
404 selthreadclear(&tp
->t_wsel
);
405 SLIST_FOREACH(kn
, &tp
->t_rsel
.si_note
, kn_selnext
) {
406 KNOTE_DETACH(&tp
->t_rsel
.si_note
, kn
);
408 selthreadclear(&tp
->t_rsel
);
413 #define FLUSHQ(q) { \
415 ndflush(q, (q)->c_cc); \
418 /* Is 'c' a line delimiter ("break" character)? */
419 #define TTBREAKC(c, lflag) \
420 ((c) == '\n' || (((c) == cc[VEOF] || \
421 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
422 (c) != _POSIX_VDISABLE))
427 * Process input of a single character received on a tty.
429 * Parameters: c The character received
430 * tp The tty on which it was received
434 * Locks: Assumes tty_lock() is held prior to calling.
437 ttyinput(int c
, struct tty
*tp
)
439 tcflag_t iflag
, lflag
;
442 int retval
= 0; /* default return value */
444 TTY_LOCK_OWNED(tp
); /* debug assert */
447 * If input is pending take it first.
450 if (ISSET(lflag
, PENDIN
))
455 if (ISSET(lflag
, ICANON
)) {
465 * Block further input iff:
466 * current input > threshold AND input is available to user program
467 * AND input flow control is enabled and not yet invoked.
468 * The 3 is slop for PARMRK.
471 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
472 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
473 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
474 !ISSET(tp
->t_state
, TS_TBLOCK
))
477 /* Handle exceptional conditions (break, parity, framing). */
479 err
= (ISSET(c
, TTY_ERRORMASK
));
481 CLR(c
, TTY_ERRORMASK
);
482 if (ISSET(err
, TTY_BI
)) {
483 if (ISSET(iflag
, IGNBRK
)) {
486 if (ISSET(iflag
, BRKINT
)) {
487 ttyflush(tp
, FREAD
| FWRITE
);
488 /* SAFE: All callers drop the lock on return */
490 tty_pgsignal(tp
, SIGINT
, 1);
494 if (ISSET(iflag
, PARMRK
))
496 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
497 || ISSET(err
, TTY_FE
)) {
498 if (ISSET(iflag
, IGNPAR
)) {
501 else if (ISSET(iflag
, PARMRK
)) {
503 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
506 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
507 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
508 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
515 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
517 if (!ISSET(lflag
, EXTPROC
)) {
519 * Check for literal nexting very first
521 if (ISSET(tp
->t_state
, TS_LNCH
)) {
523 CLR(tp
->t_state
, TS_LNCH
);
526 * Scan for special characters. This code
527 * is really just a big case statement with
528 * non-constant cases. The bottom of the
529 * case statement is labeled ``endcase'', so goto
530 * it after a case match, or similar.
534 * Control chars which aren't controlled
535 * by ICANON, ISIG, or IXON.
537 if (ISSET(lflag
, IEXTEN
)) {
538 if (CCEQ(cc
[VLNEXT
], c
)) {
539 if (ISSET(lflag
, ECHO
)) {
540 if (ISSET(lflag
, ECHOE
)) {
541 (void)ttyoutput('^', tp
);
542 (void)ttyoutput('\b', tp
);
546 SET(tp
->t_state
, TS_LNCH
);
549 if (CCEQ(cc
[VDISCARD
], c
)) {
550 if (ISSET(lflag
, FLUSHO
))
551 CLR(tp
->t_lflag
, FLUSHO
);
553 ttyflush(tp
, FWRITE
);
555 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
557 SET(tp
->t_lflag
, FLUSHO
);
565 if (ISSET(lflag
, ISIG
)) {
566 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
567 if (!ISSET(lflag
, NOFLSH
))
568 ttyflush(tp
, FREAD
| FWRITE
);
571 * SAFE: All callers drop the lock on return;
572 * SAFE: if we lose a threaded race on change
573 * SAFE: of the interrupt character, we could
574 * SAFE: have lost that race anyway due to the
575 * SAFE: scheduler executing threads in
576 * SAFE: priority order rather than "last
577 * SAFE: active thread" order (FEATURE).
581 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
585 if (CCEQ(cc
[VSUSP
], c
)) {
586 if (!ISSET(lflag
, NOFLSH
))
589 /* SAFE: All callers drop the lock on return */
591 tty_pgsignal(tp
, SIGTSTP
, 1);
597 * Handle start/stop characters.
599 if (ISSET(iflag
, IXON
)) {
600 if (CCEQ(cc
[VSTOP
], c
)) {
601 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
602 SET(tp
->t_state
, TS_TTSTOP
);
606 if (!CCEQ(cc
[VSTART
], c
)) {
610 * if VSTART == VSTOP then toggle
614 if (CCEQ(cc
[VSTART
], c
))
618 * IGNCR, ICRNL, & INLCR
621 if (ISSET(iflag
, IGNCR
)) {
624 else if (ISSET(iflag
, ICRNL
))
626 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
629 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
631 * From here on down canonical mode character
632 * processing takes place.
637 if (CCEQ(cc
[VERASE
], c
)) {
638 if (tp
->t_rawq
.c_cc
) {
639 if (ISSET(iflag
, IUTF8
)) {
641 ttyrub((c
= unputc(&tp
->t_rawq
)), tp
);
642 } while(tp
->t_rawq
.c_cc
&& CCONT(c
));
644 ttyrub(unputc(&tp
->t_rawq
), tp
);
652 if (CCEQ(cc
[VKILL
], c
)) {
653 if (ISSET(lflag
, ECHOKE
) &&
654 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
655 !ISSET(lflag
, ECHOPRT
))
656 while (tp
->t_rawq
.c_cc
)
657 ttyrub(unputc(&tp
->t_rawq
), tp
);
660 if (ISSET(lflag
, ECHOK
) ||
661 ISSET(lflag
, ECHOKE
))
666 CLR(tp
->t_state
, TS_LOCAL
);
672 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
678 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
683 * erase last char of word and remember the
684 * next chars type (for ALTWERASE)
687 c
= unputc(&tp
->t_rawq
);
690 if (c
== ' ' || c
== '\t') {
691 (void)putc(c
, &tp
->t_rawq
);
700 c
= unputc(&tp
->t_rawq
);
703 } while (c
!= ' ' && c
!= '\t' &&
704 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
705 (void)putc(c
, &tp
->t_rawq
);
711 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
716 * ^T - kernel info and generate SIGINFO
718 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
719 if (ISSET(lflag
, ISIG
)) {
720 /* SAFE: All callers drop the lock on return */
722 tty_pgsignal(tp
, SIGINFO
, 1);
725 if (!ISSET(lflag
, NOKERNINFO
))
731 * Check for input buffer overflow
733 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
735 if (ISSET(iflag
, IMAXBEL
)) {
736 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
737 (void)ttyoutput(CTRL('g'), tp
);
742 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
743 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
744 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
747 * Put data char in q for user and
748 * wakeup on seeing a line delimiter.
750 if (putc(c
, &tp
->t_rawq
) >= 0) {
751 if (!ISSET(lflag
, ICANON
)) {
756 if (TTBREAKC(c
, lflag
)) {
758 catq(&tp
->t_rawq
, &tp
->t_canq
);
760 } else if (tp
->t_rocount
++ == 0)
761 tp
->t_rocol
= tp
->t_column
;
762 if (ISSET(tp
->t_state
, TS_ERASE
)) {
764 * end of prterase \.../
766 CLR(tp
->t_state
, TS_ERASE
);
767 (void)ttyoutput('/', tp
);
771 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
773 * Place the cursor over the '^' of the ^D.
775 i
= min(2, tp
->t_column
- i
);
777 (void)ttyoutput('\b', tp
);
785 * IXANY means allow any character to restart output.
787 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
788 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
793 CLR(tp
->t_lflag
, FLUSHO
);
794 CLR(tp
->t_state
, TS_TTSTOP
);
797 /* Start the output */
798 retval
= ttstart(tp
);
808 * Output a single character on a tty, doing output processing
809 * as needed (expanding tabs, newline processing, etc.).
811 * Parameters: c The character to output
812 * tp The tty on which to output on the tty
814 * Returns: < 0 Success
815 * >= 0 Character to resend (failure)
817 * Locks: Assumes tp is locked on entry, remains locked on exit
819 * Notes: Must be recursive.
822 ttyoutput(int c
, struct tty
*tp
)
827 TTY_LOCK_OWNED(tp
); /* debug assert */
830 if (!ISSET(oflag
, OPOST
)) {
831 if (ISSET(tp
->t_lflag
, FLUSHO
))
833 if (putc(c
, &tp
->t_outq
))
840 * Do tab expansion if OXTABS is set. Special case if we external
841 * processing, we don't do the tab expansion because we'll probably
842 * get it wrong. If tab expansion needs to be done, let it happen
845 CLR(c
, ~TTY_CHARMASK
);
847 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
848 col
= c
= 8 - (tp
->t_column
& 7);
849 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
850 c
-= b_to_q((const u_char
*)" ", c
, &tp
->t_outq
);
855 return (c
== col
? -1 : '\t');
857 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
861 * Newline translation: if ONLCR is set,
862 * translate newline into "\r\n".
864 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
867 if (putc('\r', &tp
->t_outq
))
870 /* If OCRNL is set, translate "\r" into "\n". */
871 else if (c
== '\r' && ISSET(tp
->t_oflag
, OCRNL
))
873 /* If ONOCR is set, don't transmit CRs when on column 0. */
874 else if (c
== '\r' && ISSET(tp
->t_oflag
, ONOCR
) && tp
->t_column
== 0)
878 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
897 col
= (col
+ 8) & ~7;
905 * Sets the tty state to not allow any more changes of foreground process
906 * group. This is required to be done so that a subsequent revoke on a vnode
907 * is able to always successfully complete.
909 * Locks : Assumes tty_lock held on entry
912 ttysetpgrphup(struct tty
*tp
)
914 TTY_LOCK_OWNED(tp
); /* debug assert */
915 SET(tp
->t_state
, TS_PGRPHUP
);
919 * Locks : Assumes tty lock held on entry
922 ttyclrpgrphup(struct tty
*tp
)
924 TTY_LOCK_OWNED(tp
); /* debug assert */
925 CLR(tp
->t_state
, TS_PGRPHUP
);
931 * Identical to ttioctl_locked, only the lock is not held
933 * Parameters: <See ttioctl_locked()>
935 * Returns: <See ttioctl_locked()>
937 * Locks: This function assumes the tty_lock() is not held on entry;
938 * it takes the lock, and releases it before returning.
940 * Notes: This is supported to ensure the line discipline interfaces
941 * all have the same locking semantics.
943 * This function is called from
946 ttioctl(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
951 retval
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
961 * Ioctls for all tty devices.
963 * Parameters: tp Tty on which ioctl() is being called
964 * cmd ioctl() command parameter
965 * data ioctl() data argument (if any)
966 * flag fileglob open modes from fcntl.h;
967 * if called internally, this is usually
968 * set to 0, rather than something useful
969 * p Process context for the call; if the
970 * call is proxied to a worker thread,
971 * this will not be the current process!!!
974 * EIO I/O error (no process group, job
976 * EINTR Interrupted by signal
977 * EBUSY Attempt to become the console while
978 * the console is busy
979 * ENOTTY TIOCGPGRP on a non-controlling tty
980 * EINVAL Invalid baud rate
981 * ENXIO TIOCSETD of invalid line discipline
982 * EPERM TIOCSTI, not root, not open for read
983 * EACCES TIOCSTI, not root, not your controlling
985 * EPERM TIOCSCTTY failed
986 * ENOTTY/EINVAL/EPERM TIOCSPGRP failed
987 * EPERM TIOCSDRAINWAIT as non-root user
988 * suser:EPERM Console control denied
989 * ttywait:EIO t_timeout too small/expired
990 * ttywait:ERESTART Upper layer must redrive the call;
991 * this is usually done by the Libc
993 * ttywait:EINTR Interrupted (usually a signal)
997 * ttcompat:ENOTTY TIOCGSID, if no session or session
999 * ttcompat:ENOTTY All unrecognized ioctls
1000 * *tp->t_param:? TIOCSETA* underlying function
1001 * *linesw[t].l_open:? TIOCSETD line discipline open failure
1004 * Locks: This function assumes that the tty_lock() is held for the
1005 * tp at the time of the call. The lock remains held on return.
1007 * Notes: This function is called after line-discipline specific ioctl
1008 * has been called to do discipline-specific functions and/or
1009 * reject any of these ioctl() commands.
1011 * This function calls ttcompat(), which can re-call ttioctl()
1012 * to a depth of one (FORTRAN style mutual recursion); at some
1013 * point, we should just in-line ttcompat() here.
1016 ttioctl_locked(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1021 struct pgrp
*pg
, *oldpg
;
1022 struct session
*sessp
, *oldsessp
;
1025 TTY_LOCK_OWNED(tp
); /* debug assert */
1027 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1028 /* If the ioctl involves modification, signal if in the background. */
1055 while (isbackground(p
, tp
) &&
1056 (p
->p_lflag
& P_LPPWAIT
) == 0 &&
1057 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1058 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1060 if (pg
== PGRP_NULL
) {
1064 /* SAFE: All callers drop the lock on return */
1066 if (pg
->pg_jobc
== 0) {
1072 pgsignal(pg
, SIGTTOU
, 1);
1078 * We signalled ourself, so we need to act as if we
1079 * have been "interrupted" from a "sleep" to act on
1080 * the signal. If it's a signal that stops the
1081 * process, that's handled in the signal sending code.
1089 switch (cmd
) { /* Process the ioctl. */
1090 case FIOASYNC
: /* set/clear async i/o */
1092 SET(tp
->t_state
, TS_ASYNC
);
1094 CLR(tp
->t_state
, TS_ASYNC
);
1096 case FIONBIO
: /* set/clear non-blocking i/o */
1097 break; /* XXX: delete. */
1098 case FIONREAD
: /* get # bytes to read */
1099 *(int *)data
= ttnread(tp
);
1101 case TIOCEXCL
: /* set exclusive use of tty */
1102 SET(tp
->t_state
, TS_XCLUDE
);
1104 case TIOCFLUSH
: { /* flush buffers */
1105 int flags
= *(int *)data
;
1108 flags
= FREAD
| FWRITE
;
1110 flags
&= FREAD
| FWRITE
;
1111 ttyflush(tp
, flags
);
1115 /* Set current console device to this line */
1116 data
= (caddr_t
) &bogusData
;
1118 /* No break - Fall through to BSD code */
1120 case TIOCCONS
: { /* become virtual console */
1122 if (constty
&& constty
!= tp
&&
1123 ISSET(constty
->t_state
, TS_CONNECTED
)) {
1127 if ( (error
= suser(kauth_cred_get(), &p
->p_acflag
)) )
1130 } else if (tp
== constty
) {
1134 (*cdevsw
[major(constty
->t_dev
)].d_ioctl
)
1135 (constty
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1137 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
1138 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1142 case TIOCDRAIN
: /* wait till output drained */
1143 error
= ttywait(tp
);
1147 case TIOCGETA_32
: /* get termios struct */
1149 termios64to32((struct user_termios
*)&tp
->t_termios
, (struct termios32
*)data
);
1151 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1154 case TIOCGETA_64
: /* get termios struct */
1156 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1158 termios32to64((struct termios32
*)&tp
->t_termios
, (struct user_termios
*)data
);
1161 case TIOCGETD
: /* get line discipline */
1162 *(int *)data
= tp
->t_line
;
1164 case TIOCGWINSZ
: /* get window size */
1165 *(struct winsize
*)data
= tp
->t_winsize
;
1167 case TIOCGPGRP
: /* get pgrp of tty */
1168 if (!isctty(p
, tp
)) {
1172 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
1175 case TIOCHPCL
: /* hang up on last close */
1176 SET(tp
->t_cflag
, HUPCL
);
1179 case TIOCNXCL
: /* reset exclusive use of tty */
1180 CLR(tp
->t_state
, TS_XCLUDE
);
1182 case TIOCOUTQ
: /* output queue size */
1183 *(int *)data
= tp
->t_outq
.c_cc
;
1185 case TIOCSETA_32
: /* set termios struct */
1187 case TIOCSETAW_32
: /* drain output, set */
1189 case TIOCSETAF_32
: /* drn out, fls in, set */
1191 { /* drn out, fls in, set */
1192 struct termios
*t
= (struct termios
*)data
;
1193 struct termios lcl_termios
;
1196 if (cmd
==TIOCSETA_32
|| cmd
==TIOCSETAW_32
|| cmd
==TIOCSETAF_32
) {
1197 termios32to64((struct termios32
*)data
, (struct user_termios
*)&lcl_termios
);
1201 if (cmd
==TIOCSETA_64
|| cmd
==TIOCSETAW_64
|| cmd
==TIOCSETAF_64
) {
1202 termios64to32((struct user_termios
*)data
, (struct termios32
*)&lcl_termios
);
1207 /* XXX bogus test; always false */
1208 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0) {
1212 #endif /* 0 - leave in; may end up being a conformance issue */
1213 if (t
->c_ispeed
== 0)
1214 t
->c_ispeed
= t
->c_ospeed
;
1215 if (cmd
== TIOCSETAW_32
|| cmd
== TIOCSETAF_32
||
1216 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
1217 error
= ttywait(tp
);
1221 if (cmd
== TIOCSETAF_32
|| cmd
== TIOCSETAF_64
)
1222 ttyflush(tp
, FREAD
);
1224 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1226 * Set device hardware.
1228 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1231 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1232 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1234 * XXX disconnections would be too hard to
1235 * get rid of without this kludge. The only
1236 * way to get rid of controlling terminals
1237 * is to exit from the session leader.
1239 CLR(tp
->t_state
, TS_ZOMBIE
);
1241 wakeup(TSA_CARR_ON(tp
));
1245 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1246 ISSET(t
->c_cflag
, CLOCAL
)) &&
1247 !ISSET(tp
->t_state
, TS_ZOMBIE
))
1248 SET(tp
->t_state
, TS_CONNECTED
);
1250 CLR(tp
->t_state
, TS_CONNECTED
);
1251 tp
->t_cflag
= t
->c_cflag
;
1252 tp
->t_ispeed
= t
->c_ispeed
;
1253 tp
->t_ospeed
= t
->c_ospeed
;
1256 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1257 cmd
!= TIOCSETAF_32
&& cmd
!= TIOCSETAF_64
) {
1258 if (ISSET(t
->c_lflag
, ICANON
))
1259 SET(tp
->t_lflag
, PENDIN
);
1262 * XXX we really shouldn't allow toggling
1263 * ICANON while we're in a non-termios line
1264 * discipline. Now we have to worry about
1265 * panicing for a null queue.
1267 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1270 catq(&tp
->t_rawq
, &tp
->t_canq
);
1272 tp
->t_rawq
= tp
->t_canq
;
1275 CLR(tp
->t_lflag
, PENDIN
);
1279 tp
->t_iflag
= t
->c_iflag
;
1280 tp
->t_oflag
= t
->c_oflag
;
1282 * Make the EXTPROC bit read only.
1284 if (ISSET(tp
->t_lflag
, EXTPROC
))
1285 SET(t
->c_lflag
, EXTPROC
);
1287 CLR(t
->c_lflag
, EXTPROC
);
1288 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1289 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1290 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1292 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1295 case TIOCSETD
: { /* set line discipline */
1296 int t
= *(int *)data
;
1297 dev_t device
= tp
->t_dev
;
1299 if (t
>= nlinesw
|| t
< 0) {
1304 * If the new line discipline is not equal to the old one,
1305 * close the old one and open the new one.
1307 if (t
!= tp
->t_line
) {
1308 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1309 error
= (*linesw
[t
].l_open
)(device
, tp
);
1311 /* This is racy; it's possible to lose both */
1312 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1319 case TIOCSTART
: /* start output, like ^Q */
1320 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1321 ISSET(tp
->t_lflag
, FLUSHO
)) {
1322 CLR(tp
->t_lflag
, FLUSHO
);
1323 CLR(tp
->t_state
, TS_TTSTOP
);
1327 case TIOCSTI
: /* simulate terminal input */
1328 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0) {
1332 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
)) {
1336 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1338 case TIOCSTOP
: /* stop output, like ^S */
1339 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1340 SET(tp
->t_state
, TS_TTSTOP
);
1350 case TIOCSCTTY
: /* become controlling tty */
1351 /* Session ctty vnode pointer set in vnode layer. */
1352 sessp
= proc_session(p
);
1353 if (sessp
== SESSION_NULL
) {
1359 * This can only be done by a session leader.
1361 if (!SESS_LEADER(p
, sessp
)) {
1362 /* SAFE: All callers drop the lock on return */
1364 session_rele(sessp
);
1370 * If this terminal is already the controlling terminal for the
1371 * session, nothing to do here.
1373 if (tp
->t_session
== sessp
) {
1374 /* SAFE: All callers drop the lock on return */
1376 session_rele(sessp
);
1383 * Deny if the terminal is already attached to another session or
1384 * the session already has a terminal vnode.
1386 session_lock(sessp
);
1387 if (sessp
->s_ttyvp
|| tp
->t_session
) {
1388 session_unlock(sessp
);
1389 /* SAFE: All callers drop the lock on return */
1391 if (pg
!= PGRP_NULL
) {
1394 session_rele(sessp
);
1399 sessp
->s_ttypgrpid
= pg
->pg_id
;
1400 oldtp
= sessp
->s_ttyp
;
1403 session_unlock(sessp
);
1405 oldsessp
= tp
->t_session
;
1407 if (oldsessp
!= SESSION_NULL
)
1408 oldsessp
->s_ttypgrpid
= NO_PID
;
1409 /* do not drop refs on sessp and pg as tp holds them */
1410 tp
->t_session
= sessp
;
1413 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
1414 /* SAFE: All callers drop the lock on return */
1416 /* drop the reference on prev session and pgrp */
1417 if (oldsessp
!= SESSION_NULL
)
1418 session_rele(oldsessp
);
1419 if (oldpg
!= PGRP_NULL
)
1426 case TIOCSPGRP
: { /* set pgrp of tty */
1427 struct pgrp
*pgrp
= PGRP_NULL
;
1429 sessp
= proc_session(p
);
1430 if (!isctty_sp(p
, tp
, sessp
)) {
1431 if (sessp
!= SESSION_NULL
)
1432 session_rele(sessp
);
1436 else if ((pgrp
= pgfind(*(int *)data
)) == PGRP_NULL
) {
1437 if (sessp
!= SESSION_NULL
)
1438 session_rele(sessp
);
1441 } else if (pgrp
->pg_session
!= sessp
) {
1442 /* SAFE: All callers drop the lock on return */
1444 if (sessp
!= SESSION_NULL
)
1445 session_rele(sessp
);
1452 * The session leader is going away and is possibly going to revoke
1453 * the terminal, we can't change the process group when that is the
1456 if (ISSET(tp
->t_state
, TS_PGRPHUP
)) {
1463 sessp
->s_ttypgrpid
= pgrp
->pg_id
;
1465 /* SAFE: All callers drop the lock on return */
1467 if (oldpg
!= PGRP_NULL
)
1469 if (sessp
!= SESSION_NULL
)
1470 session_rele(sessp
);
1474 case TIOCSTAT
: /* simulate control-T */
1477 case TIOCSWINSZ
: /* set window size */
1478 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1479 sizeof (struct winsize
))) {
1480 tp
->t_winsize
= *(struct winsize
*)data
;
1481 /* SAFE: All callers drop the lock on return */
1483 tty_pgsignal(tp
, SIGWINCH
, 1);
1487 case TIOCSDRAINWAIT
:
1488 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1492 tp
->t_timeout
= *(int *)data
* hz
;
1493 wakeup(TSA_OCOMPLETE(tp
));
1494 wakeup(TSA_OLOWAT(tp
));
1496 case TIOCGDRAINWAIT
:
1497 *(int *)data
= tp
->t_timeout
/ hz
;
1500 error
= ttcompat(tp
, cmd
, data
, flag
, p
);
1511 * Locks: Assumes tp is locked on entry, remains locked on exit
1514 ttyselect(struct tty
*tp
, int rw
, void *wql
, proc_t p
)
1518 * Attaching knotes to TTYs needs to call selrecord in order to hook
1519 * up the waitq to the selinfo, regardless of data being ready. See
1522 bool needs_selrecord
= rw
& FMARK
;
1531 if (tp
->t_state
& TS_ZOMBIE
) {
1538 retval
= ttnread(tp
);
1543 selrecord(p
, &tp
->t_rsel
, wql
);
1546 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1547 (tp
->t_state
& TS_CONNECTED
)) {
1548 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
1552 selrecord(p
, &tp
->t_wsel
, wql
);
1557 if (retval
> 0 && needs_selrecord
) {
1560 selrecord(p
, &tp
->t_rsel
, wql
);
1563 selrecord(p
, &tp
->t_wsel
, wql
);
1573 * This is a wrapper for compatibility with the select vector used by
1574 * cdevsw. It relies on a proper xxxdevtotty routine.
1576 * Locks: Assumes tty_lock() is not held prior to calling.
1579 ttselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
1582 struct tty
*tp
= cdevsw
[major(dev
)].d_ttys
[minor(dev
)];
1585 rv
= ttyselect(tp
, rw
, wql
, p
);
1593 * Locks: Assumes tp is locked on entry, remains locked on exit
1595 __private_extern__
int
1596 ttnread(struct tty
*tp
)
1600 TTY_LOCK_OWNED(tp
); /* debug assert */
1602 if (ISSET(tp
->t_lflag
, PENDIN
))
1604 nread
= tp
->t_canq
.c_cc
;
1605 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1606 nread
+= tp
->t_rawq
.c_cc
;
1607 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1617 * Wait for output to drain.
1619 * Parameters: tp Tty on which to wait for output to drain
1621 * Returns: 0 Success
1622 * EIO t_timeout too small/expired
1623 * ttysleep:ERESTART Upper layer must redrive the call;
1624 * this is usually done by the Libc
1625 * stub in user space
1626 * ttysleep:EINTR Interrupted (usually a signal)
1628 * Notes: Called from proc_exit() and vproc_exit().
1630 * Locks: Assumes tp is locked on entry, remains locked on exit
1633 ttywait(struct tty
*tp
)
1637 TTY_LOCK_OWNED(tp
); /* debug assert */
1640 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1641 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1643 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1644 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1645 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1646 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1647 TTOPRI
| PCATCH
, "ttywai",
1650 if (error
== EWOULDBLOCK
)
1657 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1663 * Stop the underlying device driver.
1665 * Locks: Assumes tty_lock() is held prior to calling.
1668 ttystop(struct tty
*tp
, int rw
)
1670 TTY_LOCK_OWNED(tp
); /* debug assert */
1672 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1676 * Flush if successfully wait.
1678 * Locks: Assumes tty_lock() is held prior to calling.
1681 ttywflush(struct tty
*tp
)
1685 TTY_LOCK_OWNED(tp
); /* debug assert */
1687 if ((error
= ttywait(tp
)) == 0)
1688 ttyflush(tp
, FREAD
);
1693 * Flush tty read and/or write queues, notifying anyone waiting.
1695 * Locks: Assumes tty_lock() is held prior to calling.
1698 ttyflush(struct tty
*tp
, int rw
)
1700 TTY_LOCK_OWNED(tp
); /* debug assert */
1706 FLUSHQ(&tp
->t_outq
);
1707 CLR(tp
->t_state
, TS_TTSTOP
);
1711 FLUSHQ(&tp
->t_canq
);
1712 FLUSHQ(&tp
->t_rawq
);
1713 CLR(tp
->t_lflag
, PENDIN
);
1716 CLR(tp
->t_state
, TS_LOCAL
);
1718 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1720 FLUSHQ(&tp
->t_outq
);
1724 * Don't let leave any state that might clobber the
1725 * next line discipline (although we should do more
1726 * to send the START char). Not clearing the state
1727 * may have caused the "putc to a clist with no
1728 * reserved cblocks" panic/printf.
1730 CLR(tp
->t_state
, TS_TBLOCK
);
1732 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1733 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1735 * XXX wait a bit in the hope that the stop
1736 * character (if any) will go out. Waiting
1737 * isn't good since it allows races. This
1738 * will be fixed when the stop character is
1739 * put in a special queue. Don't bother with
1740 * the checks in ttywait() since the timeout
1743 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1744 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1747 * Don't try sending the stop character again.
1749 CLR(tp
->t_state
, TS_TBLOCK
);
1756 FLUSHQ(&tp
->t_outq
);
1762 * Copy in the default termios characters.
1764 * Locks: Assumes tty_lock() is held prior to calling.
1766 * Notes: No assertion; tp is not in scope.
1769 termioschars(struct termios
*t
)
1771 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1776 * Handle input high water. Send stop character for the IXOFF case. Turn
1777 * on our input flow control bit and propagate the changes to the driver.
1778 * XXX the stop character should be put in a special high priority queue.
1780 * Locks: Assumes tty_lock() is held for the call.
1783 ttyblock(struct tty
*tp
)
1785 TTY_LOCK_OWNED(tp
); /* debug assert */
1787 SET(tp
->t_state
, TS_TBLOCK
);
1788 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1789 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1790 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1796 * Handle input low water. Send start character for the IXOFF case. Turn
1797 * off our input flow control bit and propagate the changes to the driver.
1798 * XXX the start character should be put in a special high priority queue.
1800 * Locks: Assumes tty_lock() is held for the call.
1803 ttyunblock(struct tty
*tp
)
1805 TTY_LOCK_OWNED(tp
); /* debug assert */
1807 CLR(tp
->t_state
, TS_TBLOCK
);
1808 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1809 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1810 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1820 * Parameters: tp tty on which to start output
1822 * Returns: 0 Success
1824 * Locks: Assumes tty_lock() is held for the call.
1826 * Notes: This function might as well be void; it always returns success
1828 * Called from ttioctl_locked(), LDISC routines, and
1829 * ttycheckoutq(), ttyblock(), ttyunblock(), and tputchar()
1832 ttstart(struct tty
*tp
)
1834 TTY_LOCK_OWNED(tp
); /* debug assert */
1836 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1846 * "close" a line discipline
1848 * Locks: Assumes tty_lock() is held prior to calling.
1851 ttylclose(struct tty
*tp
, int flag
)
1853 TTY_LOCK_OWNED(tp
); /* debug assert */
1855 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1856 ttyflush(tp
, FREAD
| FWRITE
);
1865 * Handle modem control transition on a tty.
1866 * Flag indicates new state of carrier.
1867 * Returns 0 if the line should be turned off, otherwise 1.
1869 * Locks: Assumes tty_lock() is held prior to calling.
1872 ttymodem(struct tty
*tp
, int flag
)
1874 int rval
= 1; /* default return value */
1876 TTY_LOCK_OWNED(tp
); /* debug assert */
1878 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1880 * MDMBUF: do flow control according to carrier flag
1881 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1882 * works if IXON and IXANY are clear.
1885 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1886 CLR(tp
->t_state
, TS_TTSTOP
);
1888 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1889 SET(tp
->t_state
, TS_CAR_OFLOW
);
1890 SET(tp
->t_state
, TS_TTSTOP
);
1893 } else if (flag
== 0) {
1897 CLR(tp
->t_state
, TS_CARR_ON
);
1898 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1899 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1900 SET(tp
->t_state
, TS_ZOMBIE
);
1901 CLR(tp
->t_state
, TS_CONNECTED
);
1902 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1903 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1904 ttyflush(tp
, FREAD
| FWRITE
);
1912 SET(tp
->t_state
, TS_CARR_ON
);
1913 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1914 SET(tp
->t_state
, TS_CONNECTED
);
1915 wakeup(TSA_CARR_ON(tp
));
1926 * Reinput pending characters after state switch
1929 * Locks: Assumes tty_lock() is held for the call.
1932 ttypend(struct tty
*tp
)
1937 TTY_LOCK_OWNED(tp
); /* debug assert */
1939 CLR(tp
->t_lflag
, PENDIN
);
1940 SET(tp
->t_state
, TS_TYPEN
);
1942 tp
->t_rawq
.c_cc
= 0;
1943 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= NULL
;
1944 while ((c
= getc(&tq
)) >= 0)
1946 CLR(tp
->t_state
, TS_TYPEN
);
1953 * Process a read call on a tty device.
1955 * Locks: Assumes tty_lock() is held prior to calling.
1958 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1963 cc_t
*cc
= tp
->t_cc
;
1964 proc_t p
= current_proc();
1965 int first
, error
= 0;
1966 int has_etime
= 0, last_cc
= 0;
1967 long slp
= 0; /* XXX this should be renamed `timo'. */
1971 TTY_LOCK_OWNED(tp
); /* debug assert */
1973 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1976 lflag
= tp
->t_lflag
;
1978 * take pending input first
1980 if (ISSET(lflag
, PENDIN
)) {
1982 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1986 * Signal the process if it's in the background.
1988 if (isbackground(p
, tp
)) {
1989 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1990 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1991 p
->p_lflag
& P_LPPWAIT
) {
1996 if (pg
== PGRP_NULL
) {
2000 if (pg
->pg_jobc
== 0) {
2001 /* SAFE: All callers drop the lock on return */
2008 /* SAFE: All callers drop the lock on return */
2010 pgsignal(pg
, SIGTTIN
, 1);
2015 * We signalled ourself, so we need to act as if we
2016 * have been "interrupted" from a "sleep" to act on
2017 * the signal. If it's a signal that stops the
2018 * process, that's handled in the signal sending code.
2024 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2025 /* EOF - returning 0 */
2030 * If canonical, use the canonical queue,
2031 * else use the raw queue.
2033 * (should get rid of clists...)
2035 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
2037 if (flag
& IO_NDELAY
) {
2040 if (ISSET(lflag
, ICANON
) || cc
[VMIN
] != 0) {
2041 error
= EWOULDBLOCK
;
2043 /* else polling - returning 0 */
2046 if (!ISSET(lflag
, ICANON
)) {
2049 struct timeval timecopy
;
2050 struct timeval etime
= {0, 0}; /* protected by !has_etime */
2053 * Check each of the four combinations.
2054 * (m > 0 && t == 0) is the normal read case.
2055 * It should be fairly efficient, so we check that and its
2056 * companion case (m == 0 && t == 0) first.
2057 * For the other two cases, we compute the target sleep time
2066 /* m, t and qp->c_cc are all 0. 0 is enough input. */
2069 t
*= 100000; /* time in us */
2070 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
2071 ((t1).tv_usec - (t2).tv_usec))
2077 microuptime(&timecopy
);
2079 /* first character, start timer */
2082 etime
.tv_sec
= t
/ 1000000;
2083 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2084 timeradd(&etime
, &timecopy
, &etime
);
2087 } else if (qp
->c_cc
> last_cc
) {
2088 /* got a character, restart timer */
2090 etime
.tv_sec
= t
/ 1000000;
2091 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2092 timeradd(&etime
, &timecopy
, &etime
);
2096 /* nothing, check expiration */
2097 if (timercmp(&etime
, &timecopy
, <=))
2100 slp
= diff(etime
, timecopy
);
2103 } else { /* m == 0 */
2106 microuptime(&timecopy
);
2110 etime
.tv_sec
= t
/ 1000000;
2111 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2112 timeradd(&etime
, &timecopy
, &etime
);
2116 if (timercmp(&etime
, &timecopy
, <=)) {
2117 /* Timed out, but 0 is enough input. */
2120 slp
= diff(etime
, timecopy
);
2125 * Rounding down may make us wake up just short
2126 * of the target, so we round up.
2127 * The formula is ceiling(slp * hz/1000000).
2128 * 32-bit arithmetic is enough for hz < 169.
2129 * XXX see hzto() for how to avoid overflow if hz
2130 * is large (divide by `tick' and/or arrange to
2131 * use hzto() if hz is large).
2133 slp
= (long) (((u_int32_t
)slp
* hz
) + 999999) / 1000000;
2136 if (qp
->c_cc
<= 0) {
2139 * There is no input, or not enough input and we can block.
2141 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
2142 ISSET(tp
->t_state
, TS_CONNECTED
) ?
2143 "ttyin" : "ttyhup", (int)slp
);
2144 if (error
== EWOULDBLOCK
)
2150 * XXX what happens if another process eats some input
2151 * while we are asleep (not just here)? It would be
2152 * safest to detect changes and reset our state variables
2153 * (has_stime and last_cc).
2160 * Input present, check for input mapping and processing.
2163 if (ISSET(lflag
, ICANON
)
2164 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
2170 icc
= MIN(uio_resid(uio
), IBUFSIZ
);
2171 icc
= q_to_b(qp
, (u_char
*)ibuf
, icc
);
2177 error
= uiomove(ibuf
, icc
, uio
);
2179 * XXX if there was an error then we should ungetc() the
2180 * unmoved chars and reduce icc here.
2184 if (uio_resid(uio
) == 0)
2198 * delayed suspend (^Y)
2200 if (CCEQ(cc
[VDSUSP
], c
) &&
2201 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
2203 * SAFE: All callers drop the lock on return and
2204 * SAFE: current thread will not change out from
2205 * SAFE: under us in the "goto loop" case.
2208 tty_pgsignal(tp
, SIGTSTP
, 1);
2211 error
= ttysleep(tp
, &ttread
, TTIPRI
| PCATCH
,
2220 * Interpret EOF only in canonical mode.
2222 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
2225 * Give user character.
2227 error
= ureadc(c
, uio
);
2229 /* XXX should ungetc(c, qp). */
2231 if (uio_resid(uio
) == 0)
2234 * In canonical mode check for a "break character"
2235 * marking the end of a "line of input".
2237 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
2244 * Look to unblock input now that (presumably)
2245 * the input queue has gone down.
2247 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
2248 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
2257 * Check the output queue on tp for space for a kernel message (from uprintf
2258 * or tprintf). Allow some space over the normal hiwater mark so we don't
2259 * lose messages due to normal flow control, but don't let the tty run amok.
2260 * Sleeps here are not interruptible, but we return prematurely if new signals
2263 * Locks: Assumes tty_lock() is held before calling
2265 * Notes: This function is called from tprintf() in subr_prf.c
2268 ttycheckoutq(struct tty
*tp
, int wait
)
2274 TTY_LOCK_OWNED(tp
); /* debug assert */
2276 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2278 hiwat
= tp
->t_hiwat
;
2279 oldsig
= wait
? ut
->uu_siglist
: 0;
2280 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
2281 while (tp
->t_outq
.c_cc
> hiwat
) {
2283 if (tp
->t_outq
.c_cc
<= hiwat
)
2285 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
2288 SET(tp
->t_state
, TS_SO_OLOWAT
);
2289 ttysleep(tp
, TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
2298 * Process a write call on a tty device.
2300 * Locks: Assumes tty_lock() is held prior to calling.
2303 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
2308 int i
, hiwat
, error
;
2314 TTY_LOCK_OWNED(tp
); /* debug assert */
2316 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2317 hiwat
= tp
->t_hiwat
;
2318 count
= uio_resid(uio
);
2322 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2323 if (uio_resid(uio
) == count
)
2327 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2328 if (flag
& IO_NDELAY
) {
2329 error
= EWOULDBLOCK
;
2332 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2339 * Signal the process if it's in the background.
2342 if (isbackground(p
, tp
) &&
2343 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& P_LPPWAIT
) == 0 &&
2344 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2345 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2348 if (pg
== PGRP_NULL
) {
2352 if (pg
->pg_jobc
== 0) {
2353 /* SAFE: All callers drop the lock on return */
2360 /* SAFE: All callers drop the lock on return */
2362 pgsignal(pg
, SIGTTOU
, 1);
2366 * We signalled ourself, so we need to act as if we
2367 * have been "interrupted" from a "sleep" to act on
2368 * the signal. If it's a signal that stops the
2369 * process, that's handled in the signal sending code.
2375 * Process the user's data in at most OBUFSIZ chunks. Perform any
2376 * output translation. Keep track of high water mark, sleep on
2377 * overflow awaiting device aid in acquiring new space.
2379 while (uio_resid(uio
) > 0 || cc
> 0) {
2380 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2381 uio_setresid(uio
, 0);
2384 if (tp
->t_outq
.c_cc
> hiwat
)
2387 * Grab a hunk of data from the user, unless we have some
2388 * leftover from last time.
2391 cc
= MIN(uio_resid(uio
), OBUFSIZ
);
2393 error
= uiomove(cp
, cc
, uio
);
2400 * If nothing fancy need be done, grab those characters we
2401 * can handle without any of ttyoutput's processing and
2402 * just transfer them to the output q. For those chars
2403 * which require special processing (as indicated by the
2404 * bits in char_type), call ttyoutput. After processing
2405 * a hunk of data, look for FLUSHO so ^O's will take effect
2409 if (!ISSET(tp
->t_oflag
, OPOST
))
2412 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2413 char_type
, CCLASSMASK
);
2415 * If ce is zero, then we're processing
2416 * a special character through ttyoutput.
2420 if (ttyoutput(*cp
, tp
) >= 0) {
2426 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2427 tp
->t_outq
.c_cc
> hiwat
)
2433 * A bunch of normal characters have been found.
2434 * Transfer them en masse to the output queue and
2435 * continue processing at the top of the loop.
2436 * If there are any further characters in this
2437 * <= OBUFSIZ chunk, the first should be a character
2438 * requiring special handling by ttyoutput.
2441 i
= b_to_q((u_char
*)cp
, ce
, &tp
->t_outq
);
2452 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2453 tp
->t_outq
.c_cc
> hiwat
)
2460 * If cc is nonzero, we leave the uio structure inconsistent, as the
2461 * offset and iov pointers have moved forward, but it doesn't matter
2462 * (the call will either return short or restart with a new uio).
2464 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2470 * Since we are using ring buffers, if we can't insert any more into
2471 * the output queue, we can assume the ring is full and that someone
2472 * forgot to set the high water mark correctly. We set it and then
2473 * proceed as normal.
2475 hiwat
= tp
->t_outq
.c_cc
- 1;
2480 * This can only occur if FLUSHO is set in t_lflag,
2481 * or if ttstart/oproc is synchronous (or very fast).
2483 if (tp
->t_outq
.c_cc
<= hiwat
) {
2486 if (flag
& IO_NDELAY
) {
2487 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2488 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2490 SET(tp
->t_state
, TS_SO_OLOWAT
);
2491 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2493 if (error
== EWOULDBLOCK
)
2502 * Rubout one character from the rawq of tp
2503 * as cleanly as possible.
2505 * Locks: Assumes tty_lock() is held prior to calling.
2508 ttyrub(int c
, struct tty
*tp
)
2514 TTY_LOCK_OWNED(tp
); /* debug assert */
2516 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2518 CLR(tp
->t_lflag
, FLUSHO
);
2519 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2520 if (tp
->t_rocount
== 0) {
2522 * Messed up by ttwrite; retype
2527 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2530 CLR(c
, ~TTY_CHARMASK
);
2531 switch (CCLASS(c
)) {
2533 if(!(ISSET(tp
->t_iflag
, IUTF8
) && CCONT(c
))) {
2542 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2546 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2550 savecol
= tp
->t_column
;
2551 SET(tp
->t_state
, TS_CNTTB
);
2552 SET(tp
->t_lflag
, FLUSHO
);
2553 tp
->t_column
= tp
->t_rocol
;
2554 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2555 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2557 CLR(tp
->t_lflag
, FLUSHO
);
2558 CLR(tp
->t_state
, TS_CNTTB
);
2560 /* savecol will now be length of the tab. */
2561 savecol
-= tp
->t_column
;
2562 tp
->t_column
+= savecol
;
2564 savecol
= 8; /* overflow fixup */
2565 while (--savecol
>= 0)
2566 (void)ttyoutput('\b', tp
);
2569 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2570 printf(PANICSTR
, c
, CCLASS(c
));
2572 panic(PANICSTR
, c
, CCLASS(c
));
2576 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2577 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2578 SET(tp
->t_state
, TS_ERASE
);
2579 (void)ttyoutput('\\', tp
);
2583 ttyecho(tp
->t_cc
[VERASE
], tp
);
2589 * Back over count characters, erasing them.
2591 * Locks: Assumes tty_lock() is held prior to calling.
2594 ttyrubo(struct tty
*tp
, int count
)
2596 TTY_LOCK_OWNED(tp
); /* debug assert */
2598 while (count
-- > 0) {
2599 (void)ttyoutput('\b', tp
);
2600 (void)ttyoutput(' ', tp
);
2601 (void)ttyoutput('\b', tp
);
2608 * Reprint the rawq line. Note, it is assumed that c_cc has already
2611 * Locks: Assumes tty_lock() is held prior to calling.
2614 ttyretype(struct tty
*tp
)
2619 TTY_LOCK_OWNED(tp
); /* debug assert */
2621 /* Echo the reprint character. */
2622 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2623 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2625 (void)ttyoutput('\n', tp
);
2629 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2630 * BIT OF FIRST CHAR.
2632 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2634 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2636 CLR(tp
->t_state
, TS_ERASE
);
2638 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2644 * Echo a typed character to the terminal.
2646 * Locks: Assumes tty_lock() is held prior to calling.
2649 ttyecho(int c
, struct tty
*tp
)
2651 TTY_LOCK_OWNED(tp
); /* debug assert */
2653 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2654 CLR(tp
->t_lflag
, FLUSHO
);
2655 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2656 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2657 ISSET(tp
->t_lflag
, EXTPROC
))
2659 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2660 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2661 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2662 (void)ttyoutput('^', tp
);
2663 CLR(c
, ~TTY_CHARMASK
);
2669 (void)ttyoutput(c
, tp
);
2674 * Wake up any readers on a tty.
2676 * Locks: Assumes tty_lock() is held for the call.
2679 ttwakeup(struct tty
*tp
)
2681 TTY_LOCK_OWNED(tp
); /* debug assert */
2683 selwakeup(&tp
->t_rsel
);
2684 KNOTE(&tp
->t_rsel
.si_note
, 1);
2685 if (ISSET(tp
->t_state
, TS_ASYNC
)) {
2687 * XXX: Callers may not revalidate it the tty is closed
2688 * XXX: out from under them by another thread, but we do
2689 * XXX: not support queued signals. This should be safe,
2690 * XXX: since the process we intend to wakeup is in the
2691 * XXX: process group, and will wake up because of the
2692 * XXX: signal anyway.
2695 tty_pgsignal(tp
, SIGIO
, 1);
2698 wakeup(TSA_HUP_OR_INPUT(tp
));
2705 * Wake up any writers on a tty.
2707 * Locks: Assumes tty_lock() is held prior to calling.
2710 ttwwakeup(struct tty
*tp
)
2712 TTY_LOCK_OWNED(tp
); /* debug assert */
2714 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2715 selwakeup(&tp
->t_wsel
);
2716 KNOTE(&tp
->t_wsel
.si_note
, 1);
2718 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2719 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2720 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2721 wakeup(TSA_OCOMPLETE(tp
));
2723 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2724 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2725 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2726 wakeup(TSA_OLOWAT(tp
));
2732 * Look up a code for a specified speed in a conversion table;
2733 * used by drivers to map software speed values to hardware parameters.
2735 * Notes: No locks are assumed for this function; it does not
2736 * directly access struct tty.
2739 ttspeedtab(int speed
, struct speedtab
*table
)
2741 for ( ; table
->sp_speed
!= -1; table
++)
2742 if (table
->sp_speed
== speed
)
2743 return (table
->sp_code
);
2749 * Set tty hi and low water marks.
2751 * Try to arrange the dynamics so there's about one second
2752 * from hi to low water.
2754 * Locks: Assumes tty_lock() is held prior to calling.
2757 ttsetwater(struct tty
*tp
)
2762 TTY_LOCK_OWNED(tp
); /* debug assert */
2764 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2766 cps
= tp
->t_ospeed
/ 10;
2767 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2769 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2770 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2774 /* ttyinfo has been converted to the MACH kernel */
2775 #include <mach/thread_info.h>
2777 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2778 extern kern_return_t
thread_info_internal(thread_t thread
,
2779 thread_flavor_t flavor
,
2780 thread_info_t thread_info_out
,
2781 mach_msg_type_number_t
*thread_info_count
);
2785 * Report on state of foreground process group.
2787 * Locks: Assumes tty_lock() is held prior to calling.
2790 ttyinfo_locked(struct tty
*tp
)
2799 struct timeval utime
;
2800 struct timeval stime
;
2801 thread_basic_info_data_t basic_info
;
2802 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2805 TTY_LOCK_OWNED(tp
); /* debug assert */
2807 if (ttycheckoutq(tp
,0) == 0)
2810 /* Print load average. */
2811 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2812 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2815 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2816 * that pending input will be retyped on BS.
2818 if (tp
->t_session
== NULL
) {
2819 ttyprintf(tp
, "not a controlling terminal\n");
2823 if (tp
->t_pgrp
== NULL
) {
2824 ttyprintf(tp
, "no foreground process group\n");
2828 /* first process in process group */
2829 /* XXX is there a need for pgrp lock ? */
2830 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2831 ttyprintf(tp
, "empty foreground process group\n");
2837 * Pick the most interesting process and copy some of its
2838 * state for printing later.
2842 /* the proc_compare is non blocking fn, no need to use iterator */
2843 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2844 if (proc_compare(pick
, p
)) {
2848 pickpid
= pick
->p_pid
;
2852 /* SAFE: All callers drop the lock on return */
2857 pick
= proc_find(pickpid
);
2858 if (pick
== PROC_NULL
)
2861 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2862 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2863 (thread
= vfs_context_thread(&uthread
->uu_context
)) == NULL
||
2864 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2865 ttyprintf(tp
, "foreground process without thread\n");
2871 switch(basic_info
.run_state
) {
2872 case TH_STATE_RUNNING
:
2875 case TH_STATE_STOPPED
:
2878 case TH_STATE_WAITING
:
2881 case TH_STATE_UNINTERRUPTIBLE
:
2882 state
= "uninterruptible";
2884 case TH_STATE_HALTED
:
2891 calcru(pick
, &utime
, &stime
, NULL
);
2894 /* Print command, pid, state, utime, and stime */
2895 ttyprintf(tp
, " cmd: %s %d %s %ld.%02du %ld.%02ds\n",
2899 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2900 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2906 * Returns 1 if p2 is "better" than p1
2908 * The algorithm for picking the "interesting" process is thus:
2910 * 1) Only foreground processes are eligible - implied.
2911 * 2) Runnable processes are favored over anything else. The runner
2912 * with the highest cpu utilization is picked (p_estcpu). Ties are
2913 * broken by picking the highest pid.
2914 * 3) The sleeper with the shortest sleep time is next.
2915 * 4) Further ties are broken by picking the highest pid.
2917 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2918 #define TESTAB(a, b) ((a)<<1 | (b))
2924 * Locks: pgrp_lock(p2) held on call to this function
2925 * tty_lock(tp) for p2's tty, for which p2 is the foreground
2926 * process, held on call to this function
2929 proc_compare(proc_t p1
, proc_t p2
)
2931 /* NOTE THIS FN needs to be NON BLOCKING */
2936 * see if at least one of them is runnable
2938 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2945 * tie - favor one with highest recent cpu utilization
2947 #ifdef _PROC_HAS_SCHEDINFO_
2948 /* Without the support the fields are always zero */
2949 if (p2
->p_estcpu
> p1
->p_estcpu
)
2951 if (p1
->p_estcpu
> p2
->p_estcpu
)
2953 #endif /* _PROC_HAS_SCHEDINFO_ */
2954 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2959 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2965 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2968 * pick the one with the smallest sleep time
2970 #ifdef _PROC_HAS_SCHEDINFO_
2971 /* Without the support the fields are always zero */
2972 if (p2
->p_slptime
> p1
->p_slptime
)
2974 if (p1
->p_slptime
> p2
->p_slptime
)
2976 #endif /* _PROC_HAS_SCHEDINFO_ */
2977 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2982 * Output char to tty; console putchar style.
2984 * Locks: Assumes tty_lock() is held prior to calling.
2986 * Notes: Only ever called from putchar() in subr_prf.c
2989 tputchar(int c
, struct tty
*tp
)
2991 TTY_LOCK_OWNED(tp
); /* debug assert */
2993 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2997 (void)ttyoutput('\r', tp
);
2998 (void)ttyoutput(c
, tp
);
3007 * Sleep on a wait channel waiting for an interrupt or a condition to come
3008 * true so that we are woken up.
3010 * Parameters: tp Tty going to sleep
3011 * chan The sleep channel (usually an address
3012 * of a structure member)
3013 * pri priority and flags
3014 * wmesg Wait message; shows up in debugger,
3015 * should show up in "ps", but doesn't
3016 * timo Timeout for the sleep
3018 * Returns: 0 Condition came true
3019 * ERESTART Upper layer must redrive the call;
3020 * this is usually done by the Libc
3021 * stub in user space
3022 * msleep0:EINTR Interrupted (usually a signal)
3023 * msleep0:ERESTART Interrupted (usually a masked signal)
3024 * msleep0:EWOULDBLOCK Timeout (timo) already expired
3026 * Locks: Assumes tty_lock() is held prior to calling.
3028 * Sleep on chan, returning ERESTART if tty changed while we napped and
3029 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by msleep0. If
3030 * the tty is revoked, restarting a pending call will redo validation done
3031 * at the start of the call.
3034 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
3042 /* Use of msleep0() avoids conversion timo/timespec/timo */
3043 error
= msleep0(chan
, &tp
->t_lock
, pri
, wmesg
, timo
, (int (*)(int))0);
3046 return (tp
->t_gen
== gen
? 0 : ERESTART
);
3051 * Allocate a tty structure and its associated buffers.
3055 * Returns: !NULL Address of new struct tty
3056 * NULL Error ("ENOMEM")
3058 * Locks: The tty_lock() of the returned tty is not held when it
3066 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
3068 /* XXX: default to TTYCLSIZE(1024) chars for now */
3069 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
3070 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
3071 /* output queue doesn't need quoting */
3072 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
3073 lck_mtx_init(&tp
->t_lock
, tty_lck_grp
, tty_lck_attr
);
3074 klist_init(&tp
->t_rsel
.si_note
);
3075 klist_init(&tp
->t_wsel
.si_note
);
3082 * Increment the reference count on a tty.
3085 ttyhold(struct tty
*tp
)
3092 * Drops a reference count on a tty structure; if the reference count reaches
3093 * zero, then also frees the structure and associated buffers.
3096 ttyfree(struct tty
*tp
)
3098 TTY_LOCK_NOTOWNED(tp
);
3101 if (--tp
->t_refcnt
== 0) {
3104 } else if (tp
->t_refcnt
< 0) {
3105 panic("%s: freeing free tty %p", __func__
, tp
);
3111 * Deallocate a tty structure and its buffers.
3113 * Locks: The tty_lock() is assumed to not be held at the time of
3114 * the free; this function destroys the mutex.
3117 ttydeallocate(struct tty
*tp
)
3119 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
3122 if (!(SLIST_EMPTY(&tp
->t_rsel
.si_note
) && SLIST_EMPTY(&tp
->t_wsel
.si_note
))) {
3123 panic("knotes hooked into a tty when the tty is freed.\n");
3127 clfree(&tp
->t_rawq
);
3128 clfree(&tp
->t_canq
);
3129 clfree(&tp
->t_outq
);
3130 lck_mtx_destroy(&tp
->t_lock
, tty_lck_grp
);
3136 * Locks: Assumes tty_lock() is held prior to calling.
3139 isbackground(proc_t p
, struct tty
*tp
)
3143 return (tp
->t_session
!= NULL
&& p
->p_pgrp
!= NULL
&& (p
->p_pgrp
!= tp
->t_pgrp
) && isctty_sp(p
, tp
, p
->p_pgrp
->pg_session
));
3147 isctty(proc_t p
, struct tty
*tp
)
3150 struct session
* sessp
;
3152 sessp
= proc_session(p
);
3153 retval
= (sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3154 session_rele(sessp
);
3159 isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
)
3161 return(sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3166 static int filt_ttyattach(struct knote
*kn
, struct kevent_internal_s
*kev
);
3167 static void filt_ttydetach(struct knote
*kn
);
3168 static int filt_ttyevent(struct knote
*kn
, long hint
);
3169 static int filt_ttytouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
3170 static int filt_ttyprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
3172 SECURITY_READ_ONLY_EARLY(struct filterops
) tty_filtops
= {
3174 .f_attach
= filt_ttyattach
,
3175 .f_detach
= filt_ttydetach
,
3176 .f_event
= filt_ttyevent
,
3177 .f_touch
= filt_ttytouch
,
3178 .f_process
= filt_ttyprocess
3182 * Called with struct tty locked. Returns non-zero if there is data to be read
3186 filt_tty_common(struct knote
*kn
, struct tty
*tp
)
3190 TTY_LOCK_OWNED(tp
); /* debug assert */
3192 if (tp
->t_state
& TS_ZOMBIE
) {
3193 kn
->kn_flags
|= EV_EOF
;
3197 switch (knote_get_seltype(kn
)) {
3199 retval
= ttnread(tp
);
3202 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
3203 (tp
->t_state
& TS_CONNECTED
)) {
3204 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
3209 kn
->kn_data
= retval
;
3212 * TODO(mwidmann, jandrus): For native knote low watermark support,
3213 * check the kn_sfflags for NOTE_LOWAT and check against kn_sdata.
3215 * res = ((kn->kn_sfflags & NOTE_LOWAT) != 0) ?
3216 * (kn->kn_data >= kn->kn_sdata) : kn->kn_data;
3223 * Find the struct tty from a waitq, which is a member of one of the two struct
3224 * selinfos inside the struct tty. Use the seltype to determine which selinfo.
3227 tty_from_waitq(struct waitq
*wq
, int seltype
)
3230 struct tty
*tp
= NULL
;
3233 * The waitq is part of the selinfo structure managed by the driver. For
3234 * certain drivers, we want to hook the knote into the selinfo
3235 * structure's si_note field so selwakeup can call KNOTE.
3237 * While 'wq' is not really a queue element, this macro only uses the
3238 * pointer to calculate the offset into a structure given an element
3241 si
= qe_element(wq
, struct selinfo
, si_waitq
);
3244 * For TTY drivers, the selinfo structure is somewhere in the struct
3245 * tty. There are two different selinfo structures, and the one used
3246 * corresponds to the type of filter requested.
3248 * While 'si' is not really a queue element, this macro only uses the
3249 * pointer to calculate the offset into a structure given an element
3254 tp
= qe_element(si
, struct tty
, t_rsel
);
3257 tp
= qe_element(si
, struct tty
, t_wsel
);
3265 tty_from_knote(struct knote
*kn
)
3267 return (struct tty
*)kn
->kn_hook
;
3271 * Try to lock the TTY structure associated with a knote.
3273 * On success, this function returns a locked TTY structure. Otherwise, NULL is
3276 __attribute__((warn_unused_result
))
3278 tty_lock_from_knote(struct knote
*kn
)
3280 struct tty
*tp
= tty_from_knote(kn
);
3289 * Set the knote's struct tty to the kn_hook field.
3291 * The idea is to fake a call to select with our own waitq set. If the driver
3292 * calls selrecord, we'll get a link to their waitq and access to the tty
3295 * Returns -1 on failure, with the error set in the knote, or selres on success.
3298 tty_set_knote_hook(struct knote
*kn
)
3304 struct waitq
*wq
= NULL
;
3305 struct waitq_set
*old_wqs
;
3306 struct waitq_set tmp_wqs
;
3307 uint64_t rsvd
, rsvd_arg
;
3308 uint64_t *rlptr
= NULL
;
3312 uth
= get_bsdthread_info(current_thread());
3314 ctx
= vfs_context_current();
3315 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
3318 * Reserve a link element to avoid potential allocation under
3321 rsvd
= rsvd_arg
= waitq_link_reserve(NULL
);
3322 rlptr
= (void *)&rsvd_arg
;
3325 * Trick selrecord into hooking a known waitq set into the device's selinfo
3326 * waitq. Once the link is in place, we can get back into the selinfo from
3327 * the waitq and subsequently the tty (see tty_from_waitq).
3329 * We can't use a real waitq set (such as the kqueue's) because wakeups
3330 * might happen before we can unlink it.
3332 kr
= waitq_set_init(&tmp_wqs
, SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
, NULL
,
3334 assert(kr
== KERN_SUCCESS
);
3336 old_wqs
= uth
->uu_wqset
;
3337 uth
->uu_wqset
= &tmp_wqs
;
3339 * FMARK forces selects to always call selrecord, even if data is
3340 * available. See ttselect, ptsselect, ptcselect.
3342 * selres also contains the data currently available in the tty.
3344 selres
= VNOP_SELECT(vp
, knote_get_seltype(kn
) | FMARK
, 0, rlptr
, ctx
);
3345 uth
->uu_wqset
= old_wqs
;
3348 * Make sure to cleanup the reserved link - this guards against
3349 * drivers that may not actually call selrecord().
3351 waitq_link_release(rsvd
);
3352 if (rsvd
== rsvd_arg
) {
3354 * The driver didn't call selrecord -- there's no tty hooked up so we
3357 knote_set_error(kn
, ENOTTY
);
3362 /* rlptr may not point to a properly aligned pointer */
3363 memcpy(&wq
, rlptr
, sizeof(void *));
3365 tp
= tty_from_waitq(wq
, knote_get_seltype(kn
));
3369 * Take a reference and stash the tty in the knote.
3378 * Cleaning up the wqset will unlink its waitq and clean up any preposts
3379 * that occurred as a result of data coming in while the tty was attached.
3381 waitq_set_deinit(&tmp_wqs
);
3387 filt_ttyattach(struct knote
*kn
, __unused
struct kevent_internal_s
*kev
)
3393 * This function should be called from filt_specattach (spec_vnops.c),
3394 * so most of the knote data structure should already be initialized.
3397 /* don't support offsets in ttys or drivers that don't use struct tty */
3398 if (kn
->kn_vnode_use_ofst
|| !kn
->kn_vnode_kqok
) {
3399 knote_set_error(kn
, ENOTSUP
);
3404 * Connect the struct tty to the knote through the selinfo structure
3405 * referenced by the waitq within the selinfo.
3407 selres
= tty_set_knote_hook(kn
);
3413 * Attach the knote to selinfo's klist.
3415 tp
= tty_lock_from_knote(kn
);
3417 knote_set_error(kn
, ENOENT
);
3421 switch (knote_get_seltype(kn
)) {
3423 KNOTE_ATTACH(&tp
->t_rsel
.si_note
, kn
);
3426 KNOTE_ATTACH(&tp
->t_wsel
.si_note
, kn
);
3436 filt_ttydetach(struct knote
*kn
)
3440 tp
= tty_lock_from_knote(kn
);
3442 knote_set_error(kn
, ENOENT
);
3446 struct selinfo
*si
= NULL
;
3447 switch (knote_get_seltype(kn
)) {
3454 /* knote_get_seltype will panic on default */
3457 KNOTE_DETACH(&si
->si_note
, kn
);
3465 filt_ttyevent(struct knote
*kn
, long hint
)
3469 bool revoked
= hint
& NOTE_REVOKE
;
3470 hint
&= ~NOTE_REVOKE
;
3472 tp
= tty_from_knote(kn
);
3474 knote_set_error(kn
, ENOENT
);
3483 kn
->kn_flags
|= EV_EOF
| EV_ONESHOT
;
3486 ret
= filt_tty_common(kn
, tp
);
3497 filt_ttytouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
3502 tp
= tty_lock_from_knote(kn
);
3504 knote_set_error(kn
, ENOENT
);
3508 kn
->kn_sdata
= kev
->data
;
3509 kn
->kn_sfflags
= kev
->fflags
;
3510 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
3511 kn
->kn_udata
= kev
->udata
;
3513 if (kn
->kn_vnode_kqok
) {
3514 res
= filt_tty_common(kn
, tp
);
3523 filt_ttyprocess(struct knote
*kn
, __unused
struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
3528 tp
= tty_lock_from_knote(kn
);
3530 knote_set_error(kn
, ENOENT
);
3534 res
= filt_tty_common(kn
, tp
);
3537 *kev
= kn
->kn_kevent
;
3538 if (kn
->kn_flags
& EV_CLEAR
) {