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
);
917 * Also wake up sleeping readers which may or may not belong to the
918 * current foreground process group.
920 * This forces any non-fg readers (which entered read when
921 * that process group was in the fg) to return with EIO (if they're
922 * catching SIGTTIN or with SIGTTIN). The ones which do belong to the fg
923 * process group will promptly go back to sleep and get a SIGHUP shortly
924 * This would normally happen as part of the close in revoke but if
925 * there is a sleeping reader from a non-fg process group we never get
926 * to the close because the sleeping reader holds an iocount on the
927 * vnode of the terminal which is going to get revoked->reclaimed.
929 wakeup(TSA_HUP_OR_INPUT(tp
));
933 * Locks : Assumes tty lock held on entry
936 ttyclrpgrphup(struct tty
*tp
)
938 TTY_LOCK_OWNED(tp
); /* debug assert */
939 CLR(tp
->t_state
, TS_PGRPHUP
);
945 * Identical to ttioctl_locked, only the lock is not held
947 * Parameters: <See ttioctl_locked()>
949 * Returns: <See ttioctl_locked()>
951 * Locks: This function assumes the tty_lock() is not held on entry;
952 * it takes the lock, and releases it before returning.
954 * Notes: This is supported to ensure the line discipline interfaces
955 * all have the same locking semantics.
957 * This function is called from
960 ttioctl(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
965 retval
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
975 * Ioctls for all tty devices.
977 * Parameters: tp Tty on which ioctl() is being called
978 * cmd ioctl() command parameter
979 * data ioctl() data argument (if any)
980 * flag fileglob open modes from fcntl.h;
981 * if called internally, this is usually
982 * set to 0, rather than something useful
983 * p Process context for the call; if the
984 * call is proxied to a worker thread,
985 * this will not be the current process!!!
988 * EIO I/O error (no process group, job
990 * EINTR Interrupted by signal
991 * EBUSY Attempt to become the console while
992 * the console is busy
993 * ENOTTY TIOCGPGRP on a non-controlling tty
994 * EINVAL Invalid baud rate
995 * ENXIO TIOCSETD of invalid line discipline
996 * EPERM TIOCSTI, not root, not open for read
997 * EACCES TIOCSTI, not root, not your controlling
999 * EPERM TIOCSCTTY failed
1000 * ENOTTY/EINVAL/EPERM TIOCSPGRP failed
1001 * EPERM TIOCSDRAINWAIT as non-root user
1002 * suser:EPERM Console control denied
1003 * ttywait:EIO t_timeout too small/expired
1004 * ttywait:ERESTART Upper layer must redrive the call;
1005 * this is usually done by the Libc
1006 * stub in user space
1007 * ttywait:EINTR Interrupted (usually a signal)
1011 * ttcompat:ENOTTY TIOCGSID, if no session or session
1013 * ttcompat:ENOTTY All unrecognized ioctls
1014 * *tp->t_param:? TIOCSETA* underlying function
1015 * *linesw[t].l_open:? TIOCSETD line discipline open failure
1018 * Locks: This function assumes that the tty_lock() is held for the
1019 * tp at the time of the call. The lock remains held on return.
1021 * Notes: This function is called after line-discipline specific ioctl
1022 * has been called to do discipline-specific functions and/or
1023 * reject any of these ioctl() commands.
1025 * This function calls ttcompat(), which can re-call ttioctl()
1026 * to a depth of one (FORTRAN style mutual recursion); at some
1027 * point, we should just in-line ttcompat() here.
1030 ttioctl_locked(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1035 struct pgrp
*pg
, *oldpg
;
1036 struct session
*sessp
, *oldsessp
;
1039 TTY_LOCK_OWNED(tp
); /* debug assert */
1041 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1042 /* If the ioctl involves modification, signal if in the background. */
1069 while (isbackground(p
, tp
) &&
1070 (p
->p_lflag
& P_LPPWAIT
) == 0 &&
1071 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1072 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1074 if (pg
== PGRP_NULL
) {
1078 /* SAFE: All callers drop the lock on return */
1080 if (pg
->pg_jobc
== 0) {
1086 pgsignal(pg
, SIGTTOU
, 1);
1092 * We signalled ourself, so we need to act as if we
1093 * have been "interrupted" from a "sleep" to act on
1094 * the signal. If it's a signal that stops the
1095 * process, that's handled in the signal sending code.
1103 switch (cmd
) { /* Process the ioctl. */
1104 case FIOASYNC
: /* set/clear async i/o */
1106 SET(tp
->t_state
, TS_ASYNC
);
1108 CLR(tp
->t_state
, TS_ASYNC
);
1110 case FIONBIO
: /* set/clear non-blocking i/o */
1111 break; /* XXX: delete. */
1112 case FIONREAD
: /* get # bytes to read */
1113 *(int *)data
= ttnread(tp
);
1115 case TIOCEXCL
: /* set exclusive use of tty */
1116 SET(tp
->t_state
, TS_XCLUDE
);
1118 case TIOCFLUSH
: { /* flush buffers */
1119 int flags
= *(int *)data
;
1122 flags
= FREAD
| FWRITE
;
1124 flags
&= FREAD
| FWRITE
;
1125 ttyflush(tp
, flags
);
1129 /* Set current console device to this line */
1130 data
= (caddr_t
) &bogusData
;
1132 /* No break - Fall through to BSD code */
1134 case TIOCCONS
: { /* become virtual console */
1136 if (constty
&& constty
!= tp
&&
1137 ISSET(constty
->t_state
, TS_CONNECTED
)) {
1141 if ( (error
= suser(kauth_cred_get(), &p
->p_acflag
)) )
1144 } else if (tp
== constty
) {
1148 (*cdevsw
[major(constty
->t_dev
)].d_ioctl
)
1149 (constty
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1151 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
1152 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1156 case TIOCDRAIN
: /* wait till output drained */
1157 error
= ttywait(tp
);
1161 case TIOCGETA_32
: /* get termios struct */
1163 termios64to32((struct user_termios
*)&tp
->t_termios
, (struct termios32
*)data
);
1165 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1168 case TIOCGETA_64
: /* get termios struct */
1170 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1172 termios32to64((struct termios32
*)&tp
->t_termios
, (struct user_termios
*)data
);
1175 case TIOCGETD
: /* get line discipline */
1176 *(int *)data
= tp
->t_line
;
1178 case TIOCGWINSZ
: /* get window size */
1179 *(struct winsize
*)data
= tp
->t_winsize
;
1181 case TIOCGPGRP
: /* get pgrp of tty */
1182 if (!isctty(p
, tp
)) {
1186 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
1189 case TIOCHPCL
: /* hang up on last close */
1190 SET(tp
->t_cflag
, HUPCL
);
1193 case TIOCNXCL
: /* reset exclusive use of tty */
1194 CLR(tp
->t_state
, TS_XCLUDE
);
1196 case TIOCOUTQ
: /* output queue size */
1197 *(int *)data
= tp
->t_outq
.c_cc
;
1199 case TIOCSETA_32
: /* set termios struct */
1201 case TIOCSETAW_32
: /* drain output, set */
1203 case TIOCSETAF_32
: /* drn out, fls in, set */
1205 { /* drn out, fls in, set */
1206 struct termios
*t
= (struct termios
*)data
;
1207 struct termios lcl_termios
;
1210 if (cmd
==TIOCSETA_32
|| cmd
==TIOCSETAW_32
|| cmd
==TIOCSETAF_32
) {
1211 termios32to64((struct termios32
*)data
, (struct user_termios
*)&lcl_termios
);
1215 if (cmd
==TIOCSETA_64
|| cmd
==TIOCSETAW_64
|| cmd
==TIOCSETAF_64
) {
1216 termios64to32((struct user_termios
*)data
, (struct termios32
*)&lcl_termios
);
1221 /* XXX bogus test; always false */
1222 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0) {
1226 #endif /* 0 - leave in; may end up being a conformance issue */
1227 if (t
->c_ispeed
== 0)
1228 t
->c_ispeed
= t
->c_ospeed
;
1229 if (cmd
== TIOCSETAW_32
|| cmd
== TIOCSETAF_32
||
1230 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
1231 error
= ttywait(tp
);
1235 if (cmd
== TIOCSETAF_32
|| cmd
== TIOCSETAF_64
)
1236 ttyflush(tp
, FREAD
);
1238 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1240 * Set device hardware.
1242 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1245 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1246 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1248 * XXX disconnections would be too hard to
1249 * get rid of without this kludge. The only
1250 * way to get rid of controlling terminals
1251 * is to exit from the session leader.
1253 CLR(tp
->t_state
, TS_ZOMBIE
);
1255 wakeup(TSA_CARR_ON(tp
));
1259 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1260 ISSET(t
->c_cflag
, CLOCAL
)) &&
1261 !ISSET(tp
->t_state
, TS_ZOMBIE
))
1262 SET(tp
->t_state
, TS_CONNECTED
);
1264 CLR(tp
->t_state
, TS_CONNECTED
);
1265 tp
->t_cflag
= t
->c_cflag
;
1266 tp
->t_ispeed
= t
->c_ispeed
;
1267 tp
->t_ospeed
= t
->c_ospeed
;
1270 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1271 cmd
!= TIOCSETAF_32
&& cmd
!= TIOCSETAF_64
) {
1272 if (ISSET(t
->c_lflag
, ICANON
))
1273 SET(tp
->t_lflag
, PENDIN
);
1276 * XXX we really shouldn't allow toggling
1277 * ICANON while we're in a non-termios line
1278 * discipline. Now we have to worry about
1279 * panicing for a null queue.
1281 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1284 catq(&tp
->t_rawq
, &tp
->t_canq
);
1286 tp
->t_rawq
= tp
->t_canq
;
1289 CLR(tp
->t_lflag
, PENDIN
);
1293 tp
->t_iflag
= t
->c_iflag
;
1294 tp
->t_oflag
= t
->c_oflag
;
1296 * Make the EXTPROC bit read only.
1298 if (ISSET(tp
->t_lflag
, EXTPROC
))
1299 SET(t
->c_lflag
, EXTPROC
);
1301 CLR(t
->c_lflag
, EXTPROC
);
1302 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1303 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1304 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1306 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1309 case TIOCSETD
: { /* set line discipline */
1310 int t
= *(int *)data
;
1311 dev_t device
= tp
->t_dev
;
1313 if (t
>= nlinesw
|| t
< 0) {
1318 * If the new line discipline is not equal to the old one,
1319 * close the old one and open the new one.
1321 if (t
!= tp
->t_line
) {
1322 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1323 error
= (*linesw
[t
].l_open
)(device
, tp
);
1325 /* This is racy; it's possible to lose both */
1326 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1333 case TIOCSTART
: /* start output, like ^Q */
1334 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1335 ISSET(tp
->t_lflag
, FLUSHO
)) {
1336 CLR(tp
->t_lflag
, FLUSHO
);
1337 CLR(tp
->t_state
, TS_TTSTOP
);
1341 case TIOCSTI
: /* simulate terminal input */
1342 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0) {
1346 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
)) {
1350 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1352 case TIOCSTOP
: /* stop output, like ^S */
1353 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1354 SET(tp
->t_state
, TS_TTSTOP
);
1364 case TIOCSCTTY
: /* become controlling tty */
1365 /* Session ctty vnode pointer set in vnode layer. */
1366 sessp
= proc_session(p
);
1367 if (sessp
== SESSION_NULL
) {
1373 * This can only be done by a session leader.
1375 if (!SESS_LEADER(p
, sessp
)) {
1376 /* SAFE: All callers drop the lock on return */
1378 session_rele(sessp
);
1384 * If this terminal is already the controlling terminal for the
1385 * session, nothing to do here.
1387 if (tp
->t_session
== sessp
) {
1388 /* SAFE: All callers drop the lock on return */
1390 session_rele(sessp
);
1397 * Deny if the terminal is already attached to another session or
1398 * the session already has a terminal vnode.
1400 session_lock(sessp
);
1401 if (sessp
->s_ttyvp
|| tp
->t_session
) {
1402 session_unlock(sessp
);
1403 /* SAFE: All callers drop the lock on return */
1405 if (pg
!= PGRP_NULL
) {
1408 session_rele(sessp
);
1413 sessp
->s_ttypgrpid
= pg
->pg_id
;
1414 oldtp
= sessp
->s_ttyp
;
1417 session_unlock(sessp
);
1419 oldsessp
= tp
->t_session
;
1421 if (oldsessp
!= SESSION_NULL
)
1422 oldsessp
->s_ttypgrpid
= NO_PID
;
1423 /* do not drop refs on sessp and pg as tp holds them */
1424 tp
->t_session
= sessp
;
1427 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
1428 /* SAFE: All callers drop the lock on return */
1430 /* drop the reference on prev session and pgrp */
1431 if (oldsessp
!= SESSION_NULL
)
1432 session_rele(oldsessp
);
1433 if (oldpg
!= PGRP_NULL
)
1440 case TIOCSPGRP
: { /* set pgrp of tty */
1441 struct pgrp
*pgrp
= PGRP_NULL
;
1443 sessp
= proc_session(p
);
1444 if (!isctty_sp(p
, tp
, sessp
)) {
1445 if (sessp
!= SESSION_NULL
)
1446 session_rele(sessp
);
1450 else if ((pgrp
= pgfind(*(int *)data
)) == PGRP_NULL
) {
1451 if (sessp
!= SESSION_NULL
)
1452 session_rele(sessp
);
1455 } else if (pgrp
->pg_session
!= sessp
) {
1456 /* SAFE: All callers drop the lock on return */
1458 if (sessp
!= SESSION_NULL
)
1459 session_rele(sessp
);
1466 * The session leader is going away and is possibly going to revoke
1467 * the terminal, we can't change the process group when that is the
1470 if (ISSET(tp
->t_state
, TS_PGRPHUP
)) {
1471 if (sessp
!= SESSION_NULL
)
1472 session_rele(sessp
);
1480 sessp
->s_ttypgrpid
= pgrp
->pg_id
;
1484 * Wakeup readers to recheck if they are still the foreground
1487 * ttwakeup() isn't called because the readers aren't getting
1488 * woken up becuse there is something to read but to force
1489 * the re-evaluation of their foreground process group status.
1491 * Ordinarily leaving these readers waiting wouldn't be an issue
1492 * as launchd would send them a termination signal eventually
1493 * (if nobody else does). But if this terminal happens to be
1494 * /dev/console, launchd itself could get blocked forever behind
1495 * a revoke of /dev/console and leave the system deadlocked.
1497 wakeup(TSA_HUP_OR_INPUT(tp
));
1499 /* SAFE: All callers drop the lock on return */
1501 if (oldpg
!= PGRP_NULL
)
1503 if (sessp
!= SESSION_NULL
)
1504 session_rele(sessp
);
1508 case TIOCSTAT
: /* simulate control-T */
1511 case TIOCSWINSZ
: /* set window size */
1512 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1513 sizeof (struct winsize
))) {
1514 tp
->t_winsize
= *(struct winsize
*)data
;
1515 /* SAFE: All callers drop the lock on return */
1517 tty_pgsignal(tp
, SIGWINCH
, 1);
1521 case TIOCSDRAINWAIT
:
1522 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1526 tp
->t_timeout
= *(int *)data
* hz
;
1527 wakeup(TSA_OCOMPLETE(tp
));
1528 wakeup(TSA_OLOWAT(tp
));
1530 case TIOCGDRAINWAIT
:
1531 *(int *)data
= tp
->t_timeout
/ hz
;
1534 error
= ttcompat(tp
, cmd
, data
, flag
, p
);
1545 * Locks: Assumes tp is locked on entry, remains locked on exit
1548 ttyselect(struct tty
*tp
, int rw
, void *wql
, proc_t p
)
1552 * Attaching knotes to TTYs needs to call selrecord in order to hook
1553 * up the waitq to the selinfo, regardless of data being ready. See
1556 bool needs_selrecord
= rw
& FMARK
;
1565 if (tp
->t_state
& TS_ZOMBIE
) {
1572 retval
= ttnread(tp
);
1577 selrecord(p
, &tp
->t_rsel
, wql
);
1580 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1581 (tp
->t_state
& TS_CONNECTED
)) {
1582 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
1586 selrecord(p
, &tp
->t_wsel
, wql
);
1591 if (retval
> 0 && needs_selrecord
) {
1594 selrecord(p
, &tp
->t_rsel
, wql
);
1597 selrecord(p
, &tp
->t_wsel
, wql
);
1607 * This is a wrapper for compatibility with the select vector used by
1608 * cdevsw. It relies on a proper xxxdevtotty routine.
1610 * Locks: Assumes tty_lock() is not held prior to calling.
1613 ttselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
1616 struct tty
*tp
= cdevsw
[major(dev
)].d_ttys
[minor(dev
)];
1619 rv
= ttyselect(tp
, rw
, wql
, p
);
1627 * Locks: Assumes tp is locked on entry, remains locked on exit
1629 __private_extern__
int
1630 ttnread(struct tty
*tp
)
1634 TTY_LOCK_OWNED(tp
); /* debug assert */
1636 if (ISSET(tp
->t_lflag
, PENDIN
))
1638 nread
= tp
->t_canq
.c_cc
;
1639 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1640 nread
+= tp
->t_rawq
.c_cc
;
1641 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1651 * Wait for output to drain.
1653 * Parameters: tp Tty on which to wait for output to drain
1655 * Returns: 0 Success
1656 * EIO t_timeout too small/expired
1657 * ttysleep:ERESTART Upper layer must redrive the call;
1658 * this is usually done by the Libc
1659 * stub in user space
1660 * ttysleep:EINTR Interrupted (usually a signal)
1662 * Notes: Called from proc_exit() and vproc_exit().
1664 * Locks: Assumes tp is locked on entry, remains locked on exit
1667 ttywait(struct tty
*tp
)
1671 TTY_LOCK_OWNED(tp
); /* debug assert */
1674 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1675 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1677 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1678 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1679 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1680 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1681 TTOPRI
| PCATCH
, "ttywai",
1684 if (error
== EWOULDBLOCK
)
1691 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1697 * Stop the underlying device driver.
1699 * Locks: Assumes tty_lock() is held prior to calling.
1702 ttystop(struct tty
*tp
, int rw
)
1704 TTY_LOCK_OWNED(tp
); /* debug assert */
1706 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1710 * Flush if successfully wait.
1712 * Locks: Assumes tty_lock() is held prior to calling.
1715 ttywflush(struct tty
*tp
)
1719 TTY_LOCK_OWNED(tp
); /* debug assert */
1721 if ((error
= ttywait(tp
)) == 0)
1722 ttyflush(tp
, FREAD
);
1727 * Flush tty read and/or write queues, notifying anyone waiting.
1729 * Locks: Assumes tty_lock() is held prior to calling.
1732 ttyflush(struct tty
*tp
, int rw
)
1734 TTY_LOCK_OWNED(tp
); /* debug assert */
1740 FLUSHQ(&tp
->t_outq
);
1741 CLR(tp
->t_state
, TS_TTSTOP
);
1745 FLUSHQ(&tp
->t_canq
);
1746 FLUSHQ(&tp
->t_rawq
);
1747 CLR(tp
->t_lflag
, PENDIN
);
1750 CLR(tp
->t_state
, TS_LOCAL
);
1752 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1754 FLUSHQ(&tp
->t_outq
);
1758 * Don't let leave any state that might clobber the
1759 * next line discipline (although we should do more
1760 * to send the START char). Not clearing the state
1761 * may have caused the "putc to a clist with no
1762 * reserved cblocks" panic/printf.
1764 CLR(tp
->t_state
, TS_TBLOCK
);
1766 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1767 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1769 * XXX wait a bit in the hope that the stop
1770 * character (if any) will go out. Waiting
1771 * isn't good since it allows races. This
1772 * will be fixed when the stop character is
1773 * put in a special queue. Don't bother with
1774 * the checks in ttywait() since the timeout
1777 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1778 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1781 * Don't try sending the stop character again.
1783 CLR(tp
->t_state
, TS_TBLOCK
);
1790 FLUSHQ(&tp
->t_outq
);
1796 * Copy in the default termios characters.
1798 * Locks: Assumes tty_lock() is held prior to calling.
1800 * Notes: No assertion; tp is not in scope.
1803 termioschars(struct termios
*t
)
1805 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1810 * Handle input high water. Send stop character for the IXOFF case. Turn
1811 * on our input flow control bit and propagate the changes to the driver.
1812 * XXX the stop character should be put in a special high priority queue.
1814 * Locks: Assumes tty_lock() is held for the call.
1817 ttyblock(struct tty
*tp
)
1819 TTY_LOCK_OWNED(tp
); /* debug assert */
1821 SET(tp
->t_state
, TS_TBLOCK
);
1822 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1823 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1824 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1830 * Handle input low water. Send start character for the IXOFF case. Turn
1831 * off our input flow control bit and propagate the changes to the driver.
1832 * XXX the start character should be put in a special high priority queue.
1834 * Locks: Assumes tty_lock() is held for the call.
1837 ttyunblock(struct tty
*tp
)
1839 TTY_LOCK_OWNED(tp
); /* debug assert */
1841 CLR(tp
->t_state
, TS_TBLOCK
);
1842 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1843 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1844 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1854 * Parameters: tp tty on which to start output
1856 * Returns: 0 Success
1858 * Locks: Assumes tty_lock() is held for the call.
1860 * Notes: This function might as well be void; it always returns success
1862 * Called from ttioctl_locked(), LDISC routines, and
1863 * ttycheckoutq(), ttyblock(), ttyunblock(), and tputchar()
1866 ttstart(struct tty
*tp
)
1868 TTY_LOCK_OWNED(tp
); /* debug assert */
1870 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1880 * "close" a line discipline
1882 * Locks: Assumes tty_lock() is held prior to calling.
1885 ttylclose(struct tty
*tp
, int flag
)
1887 TTY_LOCK_OWNED(tp
); /* debug assert */
1889 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1890 ttyflush(tp
, FREAD
| FWRITE
);
1899 * Handle modem control transition on a tty.
1900 * Flag indicates new state of carrier.
1901 * Returns 0 if the line should be turned off, otherwise 1.
1903 * Locks: Assumes tty_lock() is held prior to calling.
1906 ttymodem(struct tty
*tp
, int flag
)
1908 int rval
= 1; /* default return value */
1910 TTY_LOCK_OWNED(tp
); /* debug assert */
1912 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1914 * MDMBUF: do flow control according to carrier flag
1915 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1916 * works if IXON and IXANY are clear.
1919 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1920 CLR(tp
->t_state
, TS_TTSTOP
);
1922 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1923 SET(tp
->t_state
, TS_CAR_OFLOW
);
1924 SET(tp
->t_state
, TS_TTSTOP
);
1927 } else if (flag
== 0) {
1931 CLR(tp
->t_state
, TS_CARR_ON
);
1932 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1933 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1934 SET(tp
->t_state
, TS_ZOMBIE
);
1935 CLR(tp
->t_state
, TS_CONNECTED
);
1936 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1937 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1938 ttyflush(tp
, FREAD
| FWRITE
);
1946 SET(tp
->t_state
, TS_CARR_ON
);
1947 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1948 SET(tp
->t_state
, TS_CONNECTED
);
1949 wakeup(TSA_CARR_ON(tp
));
1960 * Reinput pending characters after state switch
1963 * Locks: Assumes tty_lock() is held for the call.
1966 ttypend(struct tty
*tp
)
1971 TTY_LOCK_OWNED(tp
); /* debug assert */
1973 CLR(tp
->t_lflag
, PENDIN
);
1974 SET(tp
->t_state
, TS_TYPEN
);
1976 tp
->t_rawq
.c_cc
= 0;
1977 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= NULL
;
1978 while ((c
= getc(&tq
)) >= 0)
1980 CLR(tp
->t_state
, TS_TYPEN
);
1987 * Process a read call on a tty device.
1989 * Locks: Assumes tty_lock() is held prior to calling.
1992 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1997 cc_t
*cc
= tp
->t_cc
;
1998 proc_t p
= current_proc();
1999 int first
, error
= 0;
2000 int has_etime
= 0, last_cc
= 0;
2001 long slp
= 0; /* XXX this should be renamed `timo'. */
2005 TTY_LOCK_OWNED(tp
); /* debug assert */
2007 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2010 lflag
= tp
->t_lflag
;
2012 * take pending input first
2014 if (ISSET(lflag
, PENDIN
)) {
2016 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
2020 * Signal the process if it's in the background.
2022 if (isbackground(p
, tp
)) {
2023 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
2024 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
2025 p
->p_lflag
& P_LPPWAIT
) {
2030 if (pg
== PGRP_NULL
) {
2034 if (pg
->pg_jobc
== 0) {
2035 /* SAFE: All callers drop the lock on return */
2042 /* SAFE: All callers drop the lock on return */
2044 pgsignal(pg
, SIGTTIN
, 1);
2049 * We signalled ourself, so we need to act as if we
2050 * have been "interrupted" from a "sleep" to act on
2051 * the signal. If it's a signal that stops the
2052 * process, that's handled in the signal sending code.
2058 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2059 /* EOF - returning 0 */
2064 * If canonical, use the canonical queue,
2065 * else use the raw queue.
2067 * (should get rid of clists...)
2069 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
2071 if (flag
& IO_NDELAY
) {
2074 if (ISSET(lflag
, ICANON
) || cc
[VMIN
] != 0) {
2075 error
= EWOULDBLOCK
;
2077 /* else polling - returning 0 */
2080 if (!ISSET(lflag
, ICANON
)) {
2083 struct timeval timecopy
;
2084 struct timeval etime
= {0, 0}; /* protected by !has_etime */
2087 * Check each of the four combinations.
2088 * (m > 0 && t == 0) is the normal read case.
2089 * It should be fairly efficient, so we check that and its
2090 * companion case (m == 0 && t == 0) first.
2091 * For the other two cases, we compute the target sleep time
2100 /* m, t and qp->c_cc are all 0. 0 is enough input. */
2103 t
*= 100000; /* time in us */
2104 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
2105 ((t1).tv_usec - (t2).tv_usec))
2111 microuptime(&timecopy
);
2113 /* first character, start timer */
2116 etime
.tv_sec
= t
/ 1000000;
2117 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2118 timeradd(&etime
, &timecopy
, &etime
);
2121 } else if (qp
->c_cc
> last_cc
) {
2122 /* got a character, restart timer */
2124 etime
.tv_sec
= t
/ 1000000;
2125 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2126 timeradd(&etime
, &timecopy
, &etime
);
2130 /* nothing, check expiration */
2131 if (timercmp(&etime
, &timecopy
, <=))
2134 slp
= diff(etime
, timecopy
);
2137 } else { /* m == 0 */
2140 microuptime(&timecopy
);
2144 etime
.tv_sec
= t
/ 1000000;
2145 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
2146 timeradd(&etime
, &timecopy
, &etime
);
2150 if (timercmp(&etime
, &timecopy
, <=)) {
2151 /* Timed out, but 0 is enough input. */
2154 slp
= diff(etime
, timecopy
);
2159 * Rounding down may make us wake up just short
2160 * of the target, so we round up.
2161 * The formula is ceiling(slp * hz/1000000).
2162 * 32-bit arithmetic is enough for hz < 169.
2163 * XXX see hzto() for how to avoid overflow if hz
2164 * is large (divide by `tick' and/or arrange to
2165 * use hzto() if hz is large).
2167 slp
= (long) (((u_int32_t
)slp
* hz
) + 999999) / 1000000;
2170 if (qp
->c_cc
<= 0) {
2173 * There is no input, or not enough input and we can block.
2175 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
2176 ISSET(tp
->t_state
, TS_CONNECTED
) ?
2177 "ttyin" : "ttyhup", (int)slp
);
2178 if (error
== EWOULDBLOCK
)
2184 * XXX what happens if another process eats some input
2185 * while we are asleep (not just here)? It would be
2186 * safest to detect changes and reset our state variables
2187 * (has_stime and last_cc).
2194 * Input present, check for input mapping and processing.
2197 if (ISSET(lflag
, ICANON
)
2198 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
2204 icc
= MIN(uio_resid(uio
), IBUFSIZ
);
2205 icc
= q_to_b(qp
, (u_char
*)ibuf
, icc
);
2211 error
= uiomove(ibuf
, icc
, uio
);
2213 * XXX if there was an error then we should ungetc() the
2214 * unmoved chars and reduce icc here.
2218 if (uio_resid(uio
) == 0)
2232 * delayed suspend (^Y)
2234 if (CCEQ(cc
[VDSUSP
], c
) &&
2235 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
2237 * SAFE: All callers drop the lock on return and
2238 * SAFE: current thread will not change out from
2239 * SAFE: under us in the "goto loop" case.
2242 tty_pgsignal(tp
, SIGTSTP
, 1);
2245 error
= ttysleep(tp
, &ttread
, TTIPRI
| PCATCH
,
2254 * Interpret EOF only in canonical mode.
2256 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
2259 * Give user character.
2261 error
= ureadc(c
, uio
);
2263 /* XXX should ungetc(c, qp). */
2265 if (uio_resid(uio
) == 0)
2268 * In canonical mode check for a "break character"
2269 * marking the end of a "line of input".
2271 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
2278 * Look to unblock input now that (presumably)
2279 * the input queue has gone down.
2281 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
2282 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
2291 * Check the output queue on tp for space for a kernel message (from uprintf
2292 * or tprintf). Allow some space over the normal hiwater mark so we don't
2293 * lose messages due to normal flow control, but don't let the tty run amok.
2294 * Sleeps here are not interruptible, but we return prematurely if new signals
2297 * Locks: Assumes tty_lock() is held before calling
2299 * Notes: This function is called from tprintf() in subr_prf.c
2302 ttycheckoutq(struct tty
*tp
, int wait
)
2308 TTY_LOCK_OWNED(tp
); /* debug assert */
2310 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2312 hiwat
= tp
->t_hiwat
;
2313 oldsig
= wait
? ut
->uu_siglist
: 0;
2314 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
2315 while (tp
->t_outq
.c_cc
> hiwat
) {
2317 if (tp
->t_outq
.c_cc
<= hiwat
)
2319 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
2322 SET(tp
->t_state
, TS_SO_OLOWAT
);
2323 ttysleep(tp
, TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
2332 * Process a write call on a tty device.
2334 * Locks: Assumes tty_lock() is held prior to calling.
2337 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
2342 int i
, hiwat
, error
;
2348 TTY_LOCK_OWNED(tp
); /* debug assert */
2350 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2351 hiwat
= tp
->t_hiwat
;
2352 count
= uio_resid(uio
);
2356 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2357 if (uio_resid(uio
) == count
)
2361 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2362 if (flag
& IO_NDELAY
) {
2363 error
= EWOULDBLOCK
;
2366 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2373 * Signal the process if it's in the background.
2376 if (isbackground(p
, tp
) &&
2377 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& P_LPPWAIT
) == 0 &&
2378 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2379 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2382 if (pg
== PGRP_NULL
) {
2386 if (pg
->pg_jobc
== 0) {
2387 /* SAFE: All callers drop the lock on return */
2394 /* SAFE: All callers drop the lock on return */
2396 pgsignal(pg
, SIGTTOU
, 1);
2400 * We signalled ourself, so we need to act as if we
2401 * have been "interrupted" from a "sleep" to act on
2402 * the signal. If it's a signal that stops the
2403 * process, that's handled in the signal sending code.
2409 * Process the user's data in at most OBUFSIZ chunks. Perform any
2410 * output translation. Keep track of high water mark, sleep on
2411 * overflow awaiting device aid in acquiring new space.
2413 while (uio_resid(uio
) > 0 || cc
> 0) {
2414 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2415 uio_setresid(uio
, 0);
2418 if (tp
->t_outq
.c_cc
> hiwat
)
2421 * Grab a hunk of data from the user, unless we have some
2422 * leftover from last time.
2425 cc
= MIN(uio_resid(uio
), OBUFSIZ
);
2427 error
= uiomove(cp
, cc
, uio
);
2434 * If nothing fancy need be done, grab those characters we
2435 * can handle without any of ttyoutput's processing and
2436 * just transfer them to the output q. For those chars
2437 * which require special processing (as indicated by the
2438 * bits in char_type), call ttyoutput. After processing
2439 * a hunk of data, look for FLUSHO so ^O's will take effect
2443 if (!ISSET(tp
->t_oflag
, OPOST
))
2446 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2447 char_type
, CCLASSMASK
);
2449 * If ce is zero, then we're processing
2450 * a special character through ttyoutput.
2454 if (ttyoutput(*cp
, tp
) >= 0) {
2460 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2461 tp
->t_outq
.c_cc
> hiwat
)
2467 * A bunch of normal characters have been found.
2468 * Transfer them en masse to the output queue and
2469 * continue processing at the top of the loop.
2470 * If there are any further characters in this
2471 * <= OBUFSIZ chunk, the first should be a character
2472 * requiring special handling by ttyoutput.
2475 i
= b_to_q((u_char
*)cp
, ce
, &tp
->t_outq
);
2486 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2487 tp
->t_outq
.c_cc
> hiwat
)
2494 * If cc is nonzero, we leave the uio structure inconsistent, as the
2495 * offset and iov pointers have moved forward, but it doesn't matter
2496 * (the call will either return short or restart with a new uio).
2498 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2504 * Since we are using ring buffers, if we can't insert any more into
2505 * the output queue, we can assume the ring is full and that someone
2506 * forgot to set the high water mark correctly. We set it and then
2507 * proceed as normal.
2509 hiwat
= tp
->t_outq
.c_cc
- 1;
2514 * This can only occur if FLUSHO is set in t_lflag,
2515 * or if ttstart/oproc is synchronous (or very fast).
2517 if (tp
->t_outq
.c_cc
<= hiwat
) {
2520 if (flag
& IO_NDELAY
) {
2521 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2522 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2524 SET(tp
->t_state
, TS_SO_OLOWAT
);
2525 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2527 if (error
== EWOULDBLOCK
)
2536 * Rubout one character from the rawq of tp
2537 * as cleanly as possible.
2539 * Locks: Assumes tty_lock() is held prior to calling.
2542 ttyrub(int c
, struct tty
*tp
)
2548 TTY_LOCK_OWNED(tp
); /* debug assert */
2550 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2552 CLR(tp
->t_lflag
, FLUSHO
);
2553 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2554 if (tp
->t_rocount
== 0) {
2556 * Messed up by ttwrite; retype
2561 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2564 CLR(c
, ~TTY_CHARMASK
);
2565 switch (CCLASS(c
)) {
2567 if(!(ISSET(tp
->t_iflag
, IUTF8
) && CCONT(c
))) {
2576 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2580 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2584 savecol
= tp
->t_column
;
2585 SET(tp
->t_state
, TS_CNTTB
);
2586 SET(tp
->t_lflag
, FLUSHO
);
2587 tp
->t_column
= tp
->t_rocol
;
2588 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2589 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2591 CLR(tp
->t_lflag
, FLUSHO
);
2592 CLR(tp
->t_state
, TS_CNTTB
);
2594 /* savecol will now be length of the tab. */
2595 savecol
-= tp
->t_column
;
2596 tp
->t_column
+= savecol
;
2598 savecol
= 8; /* overflow fixup */
2599 while (--savecol
>= 0)
2600 (void)ttyoutput('\b', tp
);
2603 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2604 printf(PANICSTR
, c
, CCLASS(c
));
2606 panic(PANICSTR
, c
, CCLASS(c
));
2610 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2611 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2612 SET(tp
->t_state
, TS_ERASE
);
2613 (void)ttyoutput('\\', tp
);
2617 ttyecho(tp
->t_cc
[VERASE
], tp
);
2623 * Back over count characters, erasing them.
2625 * Locks: Assumes tty_lock() is held prior to calling.
2628 ttyrubo(struct tty
*tp
, int count
)
2630 TTY_LOCK_OWNED(tp
); /* debug assert */
2632 while (count
-- > 0) {
2633 (void)ttyoutput('\b', tp
);
2634 (void)ttyoutput(' ', tp
);
2635 (void)ttyoutput('\b', tp
);
2642 * Reprint the rawq line. Note, it is assumed that c_cc has already
2645 * Locks: Assumes tty_lock() is held prior to calling.
2648 ttyretype(struct tty
*tp
)
2653 TTY_LOCK_OWNED(tp
); /* debug assert */
2655 /* Echo the reprint character. */
2656 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2657 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2659 (void)ttyoutput('\n', tp
);
2663 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2664 * BIT OF FIRST CHAR.
2666 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2668 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2670 CLR(tp
->t_state
, TS_ERASE
);
2672 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2678 * Echo a typed character to the terminal.
2680 * Locks: Assumes tty_lock() is held prior to calling.
2683 ttyecho(int c
, struct tty
*tp
)
2685 TTY_LOCK_OWNED(tp
); /* debug assert */
2687 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2688 CLR(tp
->t_lflag
, FLUSHO
);
2689 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2690 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2691 ISSET(tp
->t_lflag
, EXTPROC
))
2693 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2694 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2695 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2696 (void)ttyoutput('^', tp
);
2697 CLR(c
, ~TTY_CHARMASK
);
2703 (void)ttyoutput(c
, tp
);
2708 * Wake up any readers on a tty.
2710 * Locks: Assumes tty_lock() is held for the call.
2713 ttwakeup(struct tty
*tp
)
2715 TTY_LOCK_OWNED(tp
); /* debug assert */
2717 selwakeup(&tp
->t_rsel
);
2718 KNOTE(&tp
->t_rsel
.si_note
, 1);
2719 if (ISSET(tp
->t_state
, TS_ASYNC
)) {
2721 * XXX: Callers may not revalidate it the tty is closed
2722 * XXX: out from under them by another thread, but we do
2723 * XXX: not support queued signals. This should be safe,
2724 * XXX: since the process we intend to wakeup is in the
2725 * XXX: process group, and will wake up because of the
2726 * XXX: signal anyway.
2729 tty_pgsignal(tp
, SIGIO
, 1);
2732 wakeup(TSA_HUP_OR_INPUT(tp
));
2739 * Wake up any writers on a tty.
2741 * Locks: Assumes tty_lock() is held prior to calling.
2744 ttwwakeup(struct tty
*tp
)
2746 TTY_LOCK_OWNED(tp
); /* debug assert */
2748 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2749 selwakeup(&tp
->t_wsel
);
2750 KNOTE(&tp
->t_wsel
.si_note
, 1);
2752 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2753 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2754 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2755 wakeup(TSA_OCOMPLETE(tp
));
2757 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2758 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2759 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2760 wakeup(TSA_OLOWAT(tp
));
2766 * Look up a code for a specified speed in a conversion table;
2767 * used by drivers to map software speed values to hardware parameters.
2769 * Notes: No locks are assumed for this function; it does not
2770 * directly access struct tty.
2773 ttspeedtab(int speed
, struct speedtab
*table
)
2775 for ( ; table
->sp_speed
!= -1; table
++)
2776 if (table
->sp_speed
== speed
)
2777 return (table
->sp_code
);
2783 * Set tty hi and low water marks.
2785 * Try to arrange the dynamics so there's about one second
2786 * from hi to low water.
2788 * Locks: Assumes tty_lock() is held prior to calling.
2791 ttsetwater(struct tty
*tp
)
2796 TTY_LOCK_OWNED(tp
); /* debug assert */
2798 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2800 cps
= tp
->t_ospeed
/ 10;
2801 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2803 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2804 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2808 /* ttyinfo has been converted to the MACH kernel */
2809 #include <mach/thread_info.h>
2811 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2812 extern kern_return_t
thread_info_internal(thread_t thread
,
2813 thread_flavor_t flavor
,
2814 thread_info_t thread_info_out
,
2815 mach_msg_type_number_t
*thread_info_count
);
2819 * Report on state of foreground process group.
2821 * Locks: Assumes tty_lock() is held prior to calling.
2824 ttyinfo_locked(struct tty
*tp
)
2833 struct timeval utime
;
2834 struct timeval stime
;
2835 thread_basic_info_data_t basic_info
;
2836 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2839 TTY_LOCK_OWNED(tp
); /* debug assert */
2841 if (ttycheckoutq(tp
,0) == 0)
2844 /* Print load average. */
2845 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2846 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2849 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2850 * that pending input will be retyped on BS.
2852 if (tp
->t_session
== NULL
) {
2853 ttyprintf(tp
, "not a controlling terminal\n");
2857 if (tp
->t_pgrp
== NULL
) {
2858 ttyprintf(tp
, "no foreground process group\n");
2862 /* first process in process group */
2863 /* XXX is there a need for pgrp lock ? */
2864 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2865 ttyprintf(tp
, "empty foreground process group\n");
2871 * Pick the most interesting process and copy some of its
2872 * state for printing later.
2876 /* the proc_compare is non blocking fn, no need to use iterator */
2877 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2878 if (proc_compare(pick
, p
)) {
2882 pickpid
= pick
->p_pid
;
2886 /* SAFE: All callers drop the lock on return */
2891 pick
= proc_find(pickpid
);
2892 if (pick
== PROC_NULL
)
2895 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2896 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2897 (thread
= vfs_context_thread(&uthread
->uu_context
)) == NULL
||
2898 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2899 ttyprintf(tp
, "foreground process without thread\n");
2905 switch(basic_info
.run_state
) {
2906 case TH_STATE_RUNNING
:
2909 case TH_STATE_STOPPED
:
2912 case TH_STATE_WAITING
:
2915 case TH_STATE_UNINTERRUPTIBLE
:
2916 state
= "uninterruptible";
2918 case TH_STATE_HALTED
:
2925 calcru(pick
, &utime
, &stime
, NULL
);
2928 /* Print command, pid, state, utime, and stime */
2929 ttyprintf(tp
, " cmd: %s %d %s %ld.%02du %ld.%02ds\n",
2933 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2934 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2940 * Returns 1 if p2 is "better" than p1
2942 * The algorithm for picking the "interesting" process is thus:
2944 * 1) Only foreground processes are eligible - implied.
2945 * 2) Runnable processes are favored over anything else. The runner
2946 * with the highest cpu utilization is picked (p_estcpu). Ties are
2947 * broken by picking the highest pid.
2948 * 3) The sleeper with the shortest sleep time is next.
2949 * 4) Further ties are broken by picking the highest pid.
2951 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2952 #define TESTAB(a, b) ((a)<<1 | (b))
2958 * Locks: pgrp_lock(p2) held on call to this function
2959 * tty_lock(tp) for p2's tty, for which p2 is the foreground
2960 * process, held on call to this function
2963 proc_compare(proc_t p1
, proc_t p2
)
2965 /* NOTE THIS FN needs to be NON BLOCKING */
2970 * see if at least one of them is runnable
2972 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2979 * tie - favor one with highest recent cpu utilization
2981 #ifdef _PROC_HAS_SCHEDINFO_
2982 /* Without the support the fields are always zero */
2983 if (p2
->p_estcpu
> p1
->p_estcpu
)
2985 if (p1
->p_estcpu
> p2
->p_estcpu
)
2987 #endif /* _PROC_HAS_SCHEDINFO_ */
2988 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2993 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2999 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
3002 * pick the one with the smallest sleep time
3004 #ifdef _PROC_HAS_SCHEDINFO_
3005 /* Without the support the fields are always zero */
3006 if (p2
->p_slptime
> p1
->p_slptime
)
3008 if (p1
->p_slptime
> p2
->p_slptime
)
3010 #endif /* _PROC_HAS_SCHEDINFO_ */
3011 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
3016 * Output char to tty; console putchar style.
3018 * Locks: Assumes tty_lock() is held prior to calling.
3020 * Notes: Only ever called from putchar() in subr_prf.c
3023 tputchar(int c
, struct tty
*tp
)
3025 TTY_LOCK_OWNED(tp
); /* debug assert */
3027 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
3031 (void)ttyoutput('\r', tp
);
3032 (void)ttyoutput(c
, tp
);
3041 * Sleep on a wait channel waiting for an interrupt or a condition to come
3042 * true so that we are woken up.
3044 * Parameters: tp Tty going to sleep
3045 * chan The sleep channel (usually an address
3046 * of a structure member)
3047 * pri priority and flags
3048 * wmesg Wait message; shows up in debugger,
3049 * should show up in "ps", but doesn't
3050 * timo Timeout for the sleep
3052 * Returns: 0 Condition came true
3053 * ERESTART Upper layer must redrive the call;
3054 * this is usually done by the Libc
3055 * stub in user space
3056 * msleep0:EINTR Interrupted (usually a signal)
3057 * msleep0:ERESTART Interrupted (usually a masked signal)
3058 * msleep0:EWOULDBLOCK Timeout (timo) already expired
3060 * Locks: Assumes tty_lock() is held prior to calling.
3062 * Sleep on chan, returning ERESTART if tty changed while we napped and
3063 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by msleep0. If
3064 * the tty is revoked, restarting a pending call will redo validation done
3065 * at the start of the call.
3068 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
3076 /* Use of msleep0() avoids conversion timo/timespec/timo */
3077 error
= msleep0(chan
, &tp
->t_lock
, pri
, wmesg
, timo
, (int (*)(int))0);
3080 return (tp
->t_gen
== gen
? 0 : ERESTART
);
3085 * Allocate a tty structure and its associated buffers.
3089 * Returns: !NULL Address of new struct tty
3090 * NULL Error ("ENOMEM")
3092 * Locks: The tty_lock() of the returned tty is not held when it
3100 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
3102 /* XXX: default to TTYCLSIZE(1024) chars for now */
3103 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
3104 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
3105 /* output queue doesn't need quoting */
3106 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
3107 lck_mtx_init(&tp
->t_lock
, tty_lck_grp
, tty_lck_attr
);
3108 klist_init(&tp
->t_rsel
.si_note
);
3109 klist_init(&tp
->t_wsel
.si_note
);
3116 * Increment the reference count on a tty.
3119 ttyhold(struct tty
*tp
)
3126 * Drops a reference count on a tty structure; if the reference count reaches
3127 * zero, then also frees the structure and associated buffers.
3130 ttyfree(struct tty
*tp
)
3132 TTY_LOCK_NOTOWNED(tp
);
3135 if (--tp
->t_refcnt
== 0) {
3138 } else if (tp
->t_refcnt
< 0) {
3139 panic("%s: freeing free tty %p", __func__
, tp
);
3145 * Deallocate a tty structure and its buffers.
3147 * Locks: The tty_lock() is assumed to not be held at the time of
3148 * the free; this function destroys the mutex.
3151 ttydeallocate(struct tty
*tp
)
3153 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
3156 if (!(SLIST_EMPTY(&tp
->t_rsel
.si_note
) && SLIST_EMPTY(&tp
->t_wsel
.si_note
))) {
3157 panic("knotes hooked into a tty when the tty is freed.\n");
3161 clfree(&tp
->t_rawq
);
3162 clfree(&tp
->t_canq
);
3163 clfree(&tp
->t_outq
);
3164 lck_mtx_destroy(&tp
->t_lock
, tty_lck_grp
);
3170 * Locks: Assumes tty_lock() is held prior to calling.
3173 isbackground(proc_t p
, struct tty
*tp
)
3177 return (tp
->t_session
!= NULL
&& p
->p_pgrp
!= NULL
&& (p
->p_pgrp
!= tp
->t_pgrp
) && isctty_sp(p
, tp
, p
->p_pgrp
->pg_session
));
3181 isctty(proc_t p
, struct tty
*tp
)
3184 struct session
* sessp
;
3186 sessp
= proc_session(p
);
3187 retval
= (sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3188 session_rele(sessp
);
3193 isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
)
3195 return(sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3200 static int filt_ttyattach(struct knote
*kn
, struct kevent_internal_s
*kev
);
3201 static void filt_ttydetach(struct knote
*kn
);
3202 static int filt_ttyevent(struct knote
*kn
, long hint
);
3203 static int filt_ttytouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
3204 static int filt_ttyprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
3206 SECURITY_READ_ONLY_EARLY(struct filterops
) tty_filtops
= {
3208 .f_attach
= filt_ttyattach
,
3209 .f_detach
= filt_ttydetach
,
3210 .f_event
= filt_ttyevent
,
3211 .f_touch
= filt_ttytouch
,
3212 .f_process
= filt_ttyprocess
3216 * Called with struct tty locked. Returns non-zero if there is data to be read
3220 filt_tty_common(struct knote
*kn
, struct tty
*tp
)
3224 TTY_LOCK_OWNED(tp
); /* debug assert */
3226 if (tp
->t_state
& TS_ZOMBIE
) {
3227 kn
->kn_flags
|= EV_EOF
;
3231 switch (knote_get_seltype(kn
)) {
3233 retval
= ttnread(tp
);
3236 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
3237 (tp
->t_state
& TS_CONNECTED
)) {
3238 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
3243 kn
->kn_data
= retval
;
3246 * TODO(mwidmann, jandrus): For native knote low watermark support,
3247 * check the kn_sfflags for NOTE_LOWAT and check against kn_sdata.
3249 * res = ((kn->kn_sfflags & NOTE_LOWAT) != 0) ?
3250 * (kn->kn_data >= kn->kn_sdata) : kn->kn_data;
3257 * Find the struct tty from a waitq, which is a member of one of the two struct
3258 * selinfos inside the struct tty. Use the seltype to determine which selinfo.
3261 tty_from_waitq(struct waitq
*wq
, int seltype
)
3264 struct tty
*tp
= NULL
;
3267 * The waitq is part of the selinfo structure managed by the driver. For
3268 * certain drivers, we want to hook the knote into the selinfo
3269 * structure's si_note field so selwakeup can call KNOTE.
3271 * While 'wq' is not really a queue element, this macro only uses the
3272 * pointer to calculate the offset into a structure given an element
3275 si
= qe_element(wq
, struct selinfo
, si_waitq
);
3278 * For TTY drivers, the selinfo structure is somewhere in the struct
3279 * tty. There are two different selinfo structures, and the one used
3280 * corresponds to the type of filter requested.
3282 * While 'si' is not really a queue element, this macro only uses the
3283 * pointer to calculate the offset into a structure given an element
3288 tp
= qe_element(si
, struct tty
, t_rsel
);
3291 tp
= qe_element(si
, struct tty
, t_wsel
);
3299 tty_from_knote(struct knote
*kn
)
3301 return (struct tty
*)kn
->kn_hook
;
3305 * Try to lock the TTY structure associated with a knote.
3307 * On success, this function returns a locked TTY structure. Otherwise, NULL is
3310 __attribute__((warn_unused_result
))
3312 tty_lock_from_knote(struct knote
*kn
)
3314 struct tty
*tp
= tty_from_knote(kn
);
3323 * Set the knote's struct tty to the kn_hook field.
3325 * The idea is to fake a call to select with our own waitq set. If the driver
3326 * calls selrecord, we'll get a link to their waitq and access to the tty
3329 * Returns -1 on failure, with the error set in the knote, or selres on success.
3332 tty_set_knote_hook(struct knote
*kn
)
3338 struct waitq
*wq
= NULL
;
3339 struct waitq_set
*old_wqs
;
3340 struct waitq_set tmp_wqs
;
3341 uint64_t rsvd
, rsvd_arg
;
3342 uint64_t *rlptr
= NULL
;
3346 uth
= get_bsdthread_info(current_thread());
3348 ctx
= vfs_context_current();
3349 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
3352 * Reserve a link element to avoid potential allocation under
3355 rsvd
= rsvd_arg
= waitq_link_reserve(NULL
);
3356 rlptr
= (void *)&rsvd_arg
;
3359 * Trick selrecord into hooking a known waitq set into the device's selinfo
3360 * waitq. Once the link is in place, we can get back into the selinfo from
3361 * the waitq and subsequently the tty (see tty_from_waitq).
3363 * We can't use a real waitq set (such as the kqueue's) because wakeups
3364 * might happen before we can unlink it.
3366 kr
= waitq_set_init(&tmp_wqs
, SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
, NULL
,
3368 assert(kr
== KERN_SUCCESS
);
3371 * Lazy allocate the waitqset to avoid potential allocation under
3374 waitq_set_lazy_init_link(&tmp_wqs
);
3376 old_wqs
= uth
->uu_wqset
;
3377 uth
->uu_wqset
= &tmp_wqs
;
3379 * FMARK forces selects to always call selrecord, even if data is
3380 * available. See ttselect, ptsselect, ptcselect.
3382 * selres also contains the data currently available in the tty.
3384 selres
= VNOP_SELECT(vp
, knote_get_seltype(kn
) | FMARK
, 0, rlptr
, ctx
);
3385 uth
->uu_wqset
= old_wqs
;
3388 * Make sure to cleanup the reserved link - this guards against
3389 * drivers that may not actually call selrecord().
3391 waitq_link_release(rsvd
);
3392 if (rsvd
== rsvd_arg
) {
3394 * The driver didn't call selrecord -- there's no tty hooked up so we
3397 knote_set_error(kn
, ENOTTY
);
3402 /* rlptr may not point to a properly aligned pointer */
3403 memcpy(&wq
, rlptr
, sizeof(void *));
3405 tp
= tty_from_waitq(wq
, knote_get_seltype(kn
));
3409 * Take a reference and stash the tty in the knote.
3418 * Cleaning up the wqset will unlink its waitq and clean up any preposts
3419 * that occurred as a result of data coming in while the tty was attached.
3421 waitq_set_deinit(&tmp_wqs
);
3427 filt_ttyattach(struct knote
*kn
, __unused
struct kevent_internal_s
*kev
)
3433 * This function should be called from filt_specattach (spec_vnops.c),
3434 * so most of the knote data structure should already be initialized.
3437 /* don't support offsets in ttys or drivers that don't use struct tty */
3438 if (kn
->kn_vnode_use_ofst
|| !kn
->kn_vnode_kqok
) {
3439 knote_set_error(kn
, ENOTSUP
);
3444 * Connect the struct tty to the knote through the selinfo structure
3445 * referenced by the waitq within the selinfo.
3447 selres
= tty_set_knote_hook(kn
);
3453 * Attach the knote to selinfo's klist.
3455 tp
= tty_lock_from_knote(kn
);
3457 knote_set_error(kn
, ENOENT
);
3461 switch (knote_get_seltype(kn
)) {
3463 KNOTE_ATTACH(&tp
->t_rsel
.si_note
, kn
);
3466 KNOTE_ATTACH(&tp
->t_wsel
.si_note
, kn
);
3476 filt_ttydetach(struct knote
*kn
)
3480 tp
= tty_lock_from_knote(kn
);
3482 knote_set_error(kn
, ENOENT
);
3486 struct selinfo
*si
= NULL
;
3487 switch (knote_get_seltype(kn
)) {
3494 /* knote_get_seltype will panic on default */
3497 KNOTE_DETACH(&si
->si_note
, kn
);
3505 filt_ttyevent(struct knote
*kn
, long hint
)
3509 bool revoked
= hint
& NOTE_REVOKE
;
3510 hint
&= ~NOTE_REVOKE
;
3512 tp
= tty_from_knote(kn
);
3514 knote_set_error(kn
, ENOENT
);
3523 kn
->kn_flags
|= EV_EOF
| EV_ONESHOT
;
3526 ret
= filt_tty_common(kn
, tp
);
3537 filt_ttytouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
3542 tp
= tty_lock_from_knote(kn
);
3544 knote_set_error(kn
, ENOENT
);
3548 kn
->kn_sdata
= kev
->data
;
3549 kn
->kn_sfflags
= kev
->fflags
;
3551 if (kn
->kn_vnode_kqok
) {
3552 res
= filt_tty_common(kn
, tp
);
3561 filt_ttyprocess(struct knote
*kn
, __unused
struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
3566 tp
= tty_lock_from_knote(kn
);
3568 knote_set_error(kn
, ENOENT
);
3572 res
= filt_tty_common(kn
, tp
);
3575 *kev
= kn
->kn_kevent
;
3576 if (kn
->kn_flags
& EV_CLEAR
) {