2 * Copyright (c) 1997-2019 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_DECLARE(tty_lck_grp
, "tty");
121 __private_extern__
int ttnread(struct tty
*tp
);
122 static void ttyecho(int c
, struct tty
*tp
);
123 static int ttyoutput(int c
, struct tty
*tp
);
124 static void ttypend(struct tty
*tp
);
125 static void ttyretype(struct tty
*tp
);
126 static void ttyrub(int c
, struct tty
*tp
);
127 static void ttyrubo(struct tty
*tp
, int count
);
128 static void ttystop(struct tty
*tp
, int rw
);
129 static void ttyunblock(struct tty
*tp
);
130 static int ttywflush(struct tty
*tp
);
131 static int proc_compare(proc_t p1
, proc_t p2
);
133 void ttyhold(struct tty
*tp
);
134 static void ttydeallocate(struct tty
*tp
);
136 static int isctty(proc_t p
, struct tty
*tp
);
137 static int isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
);
139 __private_extern__
void termios32to64(struct termios32
*in
, struct user_termios
*out
);
140 __private_extern__
void termios64to32(struct user_termios
*in
, struct termios32
*out
);
143 * Table with character classes and parity. The 8th bit indicates parity,
144 * the 7th bit indicates the character is an alphameric or underscore (for
145 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
146 * are 0 then the character needs no special processing on output; classes
147 * other than 0 might be translated or (not currently) require delays.
149 #define E 0x00 /* Even parity. */
150 #define O 0x80 /* Odd parity. */
151 #define PARITY(c) (char_type[c] & O)
153 #define ALPHA 0x40 /* Alpha or underscore. */
154 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
156 #define CCLASSMASK 0x3f
157 #define CCLASS(c) (char_type[c] & CCLASSMASK)
158 /* 0b10xxxxxx is the mask for UTF-8 continuations */
159 #define CCONT(c) ((c & 0xc0) == 0x80)
164 #define NA ORDINARY | ALPHA
170 static u_char
const char_type
[] = {
171 E
| CC
, O
| CC
, O
| CC
, E
| CC
, O
| CC
, E
| CC
, E
| CC
, O
| CC
, /* nul - bel */
172 O
| BS
, E
| TB
, E
| NL
, O
| CC
, E
| VT
, O
| CR
, O
| CC
, E
| CC
, /* bs - si */
173 O
| CC
, E
| CC
, E
| CC
, O
| CC
, E
| CC
, O
| CC
, O
| CC
, E
| CC
, /* dle - etb */
174 E
| CC
, O
| CC
, O
| CC
, E
| CC
, O
| CC
, E
| CC
, E
| CC
, O
| CC
, /* can - us */
175 O
| NO
, E
| NO
, E
| NO
, O
| NO
, E
| NO
, O
| NO
, O
| NO
, E
| NO
, /* sp - ' */
176 E
| NO
, O
| NO
, O
| NO
, E
| NO
, O
| NO
, E
| NO
, E
| NO
, O
| NO
, /* ( - / */
177 E
| NA
, O
| NA
, O
| NA
, E
| NA
, O
| NA
, E
| NA
, E
| NA
, O
| NA
, /* 0 - 7 */
178 O
| NA
, E
| NA
, E
| NO
, O
| NO
, E
| NO
, O
| NO
, O
| NO
, E
| NO
, /* 8 - ? */
179 O
| NO
, E
| NA
, E
| NA
, O
| NA
, E
| NA
, O
| NA
, O
| NA
, E
| NA
, /* @ - G */
180 E
| NA
, O
| NA
, O
| NA
, E
| NA
, O
| NA
, E
| NA
, E
| NA
, O
| NA
, /* H - O */
181 E
| NA
, O
| NA
, O
| NA
, E
| NA
, O
| NA
, E
| NA
, E
| NA
, O
| NA
, /* P - W */
182 O
| NA
, E
| NA
, E
| NA
, O
| NO
, E
| NO
, O
| NO
, O
| NO
, O
| NA
, /* X - _ */
183 E
| NO
, O
| NA
, O
| NA
, E
| NA
, O
| NA
, E
| NA
, E
| NA
, O
| NA
, /* ` - g */
184 O
| NA
, E
| NA
, E
| NA
, O
| NA
, E
| NA
, O
| NA
, O
| NA
, E
| NA
, /* h - o */
185 O
| NA
, E
| NA
, E
| NA
, O
| NA
, E
| NA
, O
| NA
, O
| NA
, E
| NA
, /* p - w */
186 E
| NA
, O
| NA
, O
| NA
, E
| NO
, O
| NO
, E
| NO
, E
| NO
, O
| CC
, /* x - del */
188 * Meta chars; should be settable per character set;
189 * for now, treat them all as normal characters.
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
,
206 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
217 /* Macros to clear/set/test flags. */
218 #define SET(t, f) (t) |= (f)
219 #define CLR(t, f) (t) &= ~(f)
220 #define ISSET(t, f) ((t) & (f))
223 * Input control starts when we would not be able to fit the maximum
224 * contents of the ping-pong buffers and finishes when we would be able
225 * to fit that much plus 1/8 more.
227 #define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */
228 #define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */
230 __private_extern__
void
231 termios32to64(struct termios32
*in
, struct user_termios
*out
)
233 out
->c_iflag
= (user_tcflag_t
)in
->c_iflag
;
234 out
->c_oflag
= (user_tcflag_t
)in
->c_oflag
;
235 out
->c_cflag
= (user_tcflag_t
)in
->c_cflag
;
236 out
->c_lflag
= (user_tcflag_t
)in
->c_lflag
;
238 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
239 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
241 out
->c_ispeed
= (user_speed_t
)in
->c_ispeed
;
242 out
->c_ospeed
= (user_speed_t
)in
->c_ospeed
;
245 __private_extern__
void
246 termios64to32(struct user_termios
*in
, struct termios32
*out
)
248 out
->c_iflag
= (uint32_t)in
->c_iflag
;
249 out
->c_oflag
= (uint32_t)in
->c_oflag
;
250 out
->c_cflag
= (uint32_t)in
->c_cflag
;
251 out
->c_lflag
= (uint32_t)in
->c_lflag
;
253 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
254 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
256 out
->c_ispeed
= (uint32_t)MIN(in
->c_ispeed
, UINT32_MAX
);
257 out
->c_ospeed
= (uint32_t)MIN(in
->c_ospeed
, UINT32_MAX
);
264 * Lock the requested tty structure.
266 * Parameters: tp The tty we want to lock
270 * Locks: On return, tp is locked
273 tty_lock(struct tty
*tp
)
275 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
276 lck_mtx_lock(&tp
->t_lock
);
283 * Unlock the requested tty structure.
285 * Parameters: tp The tty we want to unlock
289 * Locks: On return, tp is unlocked
292 tty_unlock(struct tty
*tp
)
294 TTY_LOCK_OWNED(tp
); /* debug assert */
295 lck_mtx_unlock(&tp
->t_lock
);
301 * Initial open of tty, or (re)entry to standard tty line discipline.
303 * Locks: Assumes tty_lock() is held prior to calling.
306 ttyopen(dev_t device
, struct tty
*tp
)
308 TTY_LOCK_OWNED(tp
); /* debug assert */
312 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
313 SET(tp
->t_state
, TS_ISOPEN
);
314 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
315 SET(tp
->t_state
, TS_CONNECTED
);
317 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
326 * Handle close() on a tty line: flush and set to initial state,
327 * bumping generation number so that pending read/write calls
328 * can detect recycling of the tty.
329 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
330 * and l_close() should have flushed, but we repeat the spltty() and
331 * the flush in case there are buggy callers.
333 * Locks: Assumes tty_lock() is held prior to calling.
336 ttyclose(struct tty
*tp
)
339 struct session
* oldsessp
;
342 TTY_LOCK_OWNED(tp
); /* debug assert */
349 * Closing current console tty; disable printing of console
350 * messages at bottom-level driver.
352 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
353 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
356 ttyflush(tp
, FREAD
| FWRITE
);
359 tp
->t_line
= TTYDISC
;
362 oldsessp
= tp
->t_session
;
364 tp
->t_session
= NULL
;
365 if (oldsessp
!= SESSION_NULL
) {
366 oldsessp
->s_ttypgrpid
= NO_PID
;
369 /* drop the reference on prev session and pgrp */
370 /* SAFE: All callers drop the lock on return */
372 if (oldsessp
!= SESSION_NULL
) {
373 session_rele(oldsessp
);
375 if (oldpg
!= PGRP_NULL
) {
380 SLIST_FOREACH(kn
, &tp
->t_wsel
.si_note
, kn_selnext
) {
381 KNOTE_DETACH(&tp
->t_wsel
.si_note
, kn
);
383 selthreadclear(&tp
->t_wsel
);
384 SLIST_FOREACH(kn
, &tp
->t_rsel
.si_note
, kn_selnext
) {
385 KNOTE_DETACH(&tp
->t_rsel
.si_note
, kn
);
387 selthreadclear(&tp
->t_rsel
);
392 #define FLUSHQ(q) { \
394 ndflush(q, (q)->c_cc); \
397 /* Is 'c' a line delimiter ("break" character)? */
398 #define TTBREAKC(c, lflag) \
399 ((c) == '\n' || (((c) == cc[VEOF] || \
400 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
401 (c) != _POSIX_VDISABLE))
406 * Process input of a single character received on a tty.
408 * Parameters: c The character received
409 * tp The tty on which it was received
413 * Locks: Assumes tty_lock() is held prior to calling.
416 ttyinput(int c
, struct tty
*tp
)
418 tcflag_t iflag
, lflag
;
421 int retval
= 0; /* default return value */
423 TTY_LOCK_OWNED(tp
); /* debug assert */
426 * If input is pending take it first.
429 if (ISSET(lflag
, PENDIN
)) {
435 if (ISSET(lflag
, ICANON
)) {
445 * Block further input iff:
446 * current input > threshold AND input is available to user program
447 * AND input flow control is enabled and not yet invoked.
448 * The 3 is slop for PARMRK.
451 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
452 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
453 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
454 !ISSET(tp
->t_state
, TS_TBLOCK
)) {
458 /* Handle exceptional conditions (break, parity, framing). */
460 err
= (ISSET(c
, TTY_ERRORMASK
));
462 CLR(c
, TTY_ERRORMASK
);
463 if (ISSET(err
, TTY_BI
)) {
464 if (ISSET(iflag
, IGNBRK
)) {
467 if (ISSET(iflag
, BRKINT
)) {
468 ttyflush(tp
, FREAD
| FWRITE
);
469 /* SAFE: All callers drop the lock on return */
471 tty_pgsignal(tp
, SIGINT
, 1);
475 if (ISSET(iflag
, PARMRK
)) {
478 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
479 || ISSET(err
, TTY_FE
)) {
480 if (ISSET(iflag
, IGNPAR
)) {
482 } else if (ISSET(iflag
, PARMRK
)) {
484 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
488 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
489 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
490 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
498 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
)) {
501 if (!ISSET(lflag
, EXTPROC
)) {
503 * Check for literal nexting very first
505 if (ISSET(tp
->t_state
, TS_LNCH
)) {
507 CLR(tp
->t_state
, TS_LNCH
);
510 * Scan for special characters. This code
511 * is really just a big case statement with
512 * non-constant cases. The bottom of the
513 * case statement is labeled ``endcase'', so goto
514 * it after a case match, or similar.
518 * Control chars which aren't controlled
519 * by ICANON, ISIG, or IXON.
521 if (ISSET(lflag
, IEXTEN
)) {
522 if (CCEQ(cc
[VLNEXT
], c
)) {
523 if (ISSET(lflag
, ECHO
)) {
524 if (ISSET(lflag
, ECHOE
)) {
525 (void)ttyoutput('^', tp
);
526 (void)ttyoutput('\b', tp
);
531 SET(tp
->t_state
, TS_LNCH
);
534 if (CCEQ(cc
[VDISCARD
], c
)) {
535 if (ISSET(lflag
, FLUSHO
)) {
536 CLR(tp
->t_lflag
, FLUSHO
);
538 ttyflush(tp
, FWRITE
);
540 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) {
543 SET(tp
->t_lflag
, FLUSHO
);
551 if (ISSET(lflag
, ISIG
)) {
552 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
553 if (!ISSET(lflag
, NOFLSH
)) {
554 ttyflush(tp
, FREAD
| FWRITE
);
558 * SAFE: All callers drop the lock on return;
559 * SAFE: if we lose a threaded race on change
560 * SAFE: of the interrupt character, we could
561 * SAFE: have lost that race anyway due to the
562 * SAFE: scheduler executing threads in
563 * SAFE: priority order rather than "last
564 * SAFE: active thread" order (FEATURE).
568 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
572 if (CCEQ(cc
[VSUSP
], c
)) {
573 if (!ISSET(lflag
, NOFLSH
)) {
577 /* SAFE: All callers drop the lock on return */
579 tty_pgsignal(tp
, SIGTSTP
, 1);
585 * Handle start/stop characters.
587 if (ISSET(iflag
, IXON
)) {
588 if (CCEQ(cc
[VSTOP
], c
)) {
589 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
590 SET(tp
->t_state
, TS_TTSTOP
);
594 if (!CCEQ(cc
[VSTART
], c
)) {
598 * if VSTART == VSTOP then toggle
602 if (CCEQ(cc
[VSTART
], c
)) {
607 * IGNCR, ICRNL, & INLCR
610 if (ISSET(iflag
, IGNCR
)) {
612 } else if (ISSET(iflag
, ICRNL
)) {
615 } else if (c
== '\n' && ISSET(iflag
, INLCR
)) {
619 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
621 * From here on down canonical mode character
622 * processing takes place.
627 if (CCEQ(cc
[VERASE
], c
)) {
628 if (tp
->t_rawq
.c_cc
) {
629 if (ISSET(iflag
, IUTF8
)) {
631 ttyrub((c
= unputc(&tp
->t_rawq
)), tp
);
632 } while (tp
->t_rawq
.c_cc
&& CCONT(c
));
634 ttyrub(unputc(&tp
->t_rawq
), tp
);
642 if (CCEQ(cc
[VKILL
], c
)) {
643 if (ISSET(lflag
, ECHOKE
) &&
644 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
645 !ISSET(lflag
, ECHOPRT
)) {
646 while (tp
->t_rawq
.c_cc
) {
647 ttyrub(unputc(&tp
->t_rawq
), tp
);
651 if (ISSET(lflag
, ECHOK
) ||
652 ISSET(lflag
, ECHOKE
)) {
658 CLR(tp
->t_state
, TS_LOCAL
);
664 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
670 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t') {
677 * erase last char of word and remember the
678 * next chars type (for ALTWERASE)
681 c
= unputc(&tp
->t_rawq
);
685 if (c
== ' ' || c
== '\t') {
686 (void)putc(c
, &tp
->t_rawq
);
695 c
= unputc(&tp
->t_rawq
);
699 } while (c
!= ' ' && c
!= '\t' &&
700 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
701 (void)putc(c
, &tp
->t_rawq
);
707 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
712 * ^T - kernel info and generate SIGINFO
714 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
715 if (ISSET(lflag
, ISIG
)) {
716 /* SAFE: All callers drop the lock on return */
718 tty_pgsignal(tp
, SIGINFO
, 1);
721 if (!ISSET(lflag
, NOKERNINFO
)) {
728 * Check for input buffer overflow
730 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
732 if (ISSET(iflag
, IMAXBEL
)) {
733 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
) {
734 (void)ttyoutput(CTRL('g'), tp
);
740 if (c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
741 && ISSET(iflag
, IGNBRK
| IGNPAR
) != (IGNBRK
| IGNPAR
)) {
742 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
746 * Put data char in q for user and
747 * wakeup on seeing a line delimiter.
749 if (putc(c
, &tp
->t_rawq
) >= 0) {
750 if (!ISSET(lflag
, ICANON
)) {
755 if (TTBREAKC(c
, lflag
)) {
757 catq(&tp
->t_rawq
, &tp
->t_canq
);
759 } else if (tp
->t_rocount
++ == 0) {
760 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
)) {
834 if (putc(c
, &tp
->t_outq
)) {
842 * Do tab expansion if OXTABS is set. Special case if we external
843 * processing, we don't do the tab expansion because we'll probably
844 * get it wrong. If tab expansion needs to be done, let it happen
847 CLR(c
, ~TTY_CHARMASK
);
849 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
850 col
= c
= 8 - (tp
->t_column
& 7);
851 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
852 c
-= b_to_q((const u_char
*)" ", c
, &tp
->t_outq
);
857 return c
== col
? -1 : '\t';
859 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
)) {
864 * Newline translation: if ONLCR is set,
865 * translate newline into "\r\n".
867 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
870 if (putc('\r', &tp
->t_outq
)) {
874 /* If OCRNL is set, translate "\r" into "\n". */
875 else if (c
== '\r' && ISSET(tp
->t_oflag
, OCRNL
)) {
878 /* If ONOCR is set, don't transmit CRs when on column 0. */
879 else if (c
== '\r' && ISSET(tp
->t_oflag
, ONOCR
) && tp
->t_column
== 0) {
884 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
)) {
905 col
= (col
+ 8) & ~7;
915 * Identical to ttioctl_locked, only the lock is not held
917 * Parameters: <See ttioctl_locked()>
919 * Returns: <See ttioctl_locked()>
921 * Locks: This function assumes the tty_lock() is not held on entry;
922 * it takes the lock, and releases it before returning.
924 * Notes: This is supported to ensure the line discipline interfaces
925 * all have the same locking semantics.
927 * This function is called from
930 ttioctl(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
935 retval
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
945 * Ioctls for all tty devices.
947 * Parameters: tp Tty on which ioctl() is being called
948 * cmd ioctl() command parameter
949 * data ioctl() data argument (if any)
950 * flag fileglob open modes from fcntl.h;
951 * if called internally, this is usually
952 * set to 0, rather than something useful
953 * p Process context for the call; if the
954 * call is proxied to a worker thread,
955 * this will not be the current process!!!
958 * EIO I/O error (no process group, job
960 * EINTR Interrupted by signal
961 * EBUSY Attempt to become the console while
962 * the console is busy
963 * ENOTTY TIOCGPGRP on a non-controlling tty
964 * EINVAL Invalid baud rate
965 * ENXIO TIOCSETD of invalid line discipline
966 * EPERM TIOCSTI, not root, not open for read
967 * EACCES TIOCSTI, not root, not your controlling
969 * EPERM TIOCSCTTY failed
970 * ENOTTY/EINVAL/EPERM TIOCSPGRP failed
971 * EPERM TIOCSDRAINWAIT as non-root user
972 * suser:EPERM Console control denied
973 * ttywait:EIO t_timeout too small/expired
974 * ttywait:ERESTART Upper layer must redrive the call;
975 * this is usually done by the Libc
977 * ttywait:EINTR Interrupted (usually a signal)
981 * ttcompat:ENOTTY TIOCGSID, if no session or session
983 * ttcompat:ENOTTY All unrecognized ioctls
984 * *tp->t_param:? TIOCSETA* underlying function
985 * *linesw[t].l_open:? TIOCSETD line discipline open failure
988 * Locks: This function assumes that the tty_lock() is held for the
989 * tp at the time of the call. The lock remains held on return.
991 * Notes: This function is called after line-discipline specific ioctl
992 * has been called to do discipline-specific functions and/or
993 * reject any of these ioctl() commands.
995 * This function calls ttcompat(), which can re-call ttioctl()
996 * to a depth of one (FORTRAN style mutual recursion); at some
997 * point, we should just in-line ttcompat() here.
1000 ttioctl_locked(struct tty
*tp
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1005 struct pgrp
*pg
, *oldpg
;
1006 struct session
*sessp
, *oldsessp
;
1009 TTY_LOCK_OWNED(tp
); /* debug assert */
1011 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1012 /* If the ioctl involves modification, signal if in the background. */
1039 while (isbackground(p
, tp
) &&
1040 (p
->p_lflag
& P_LPPWAIT
) == 0 &&
1041 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
1042 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
1044 if (pg
== PGRP_NULL
) {
1048 /* SAFE: All callers drop the lock on return */
1050 if (pg
->pg_jobc
== 0) {
1056 pgsignal(pg
, SIGTTOU
, 1);
1062 * We signalled ourself, so we need to act as if we
1063 * have been "interrupted" from a "sleep" to act on
1064 * the signal. If it's a signal that stops the
1065 * process, that's handled in the signal sending code.
1073 switch (cmd
) { /* Process the ioctl. */
1074 case FIOASYNC
: /* set/clear async i/o */
1076 SET(tp
->t_state
, TS_ASYNC
);
1078 CLR(tp
->t_state
, TS_ASYNC
);
1081 case FIONBIO
: /* set/clear non-blocking i/o */
1082 break; /* XXX: delete. */
1083 case FIONREAD
: /* get # bytes to read */
1084 *(int *)data
= ttnread(tp
);
1086 case TIOCEXCL
: /* set exclusive use of tty */
1087 SET(tp
->t_state
, TS_XCLUDE
);
1089 case TIOCFLUSH
: { /* flush buffers */
1090 int flags
= *(int *)data
;
1093 flags
= FREAD
| FWRITE
;
1095 flags
&= FREAD
| FWRITE
;
1097 ttyflush(tp
, flags
);
1101 /* Set current console device to this line */
1102 data
= (caddr_t
) &bogusData
;
1105 case TIOCCONS
: { /* become virtual console */
1107 if (constty
&& constty
!= tp
&&
1108 ISSET(constty
->t_state
, TS_CONNECTED
)) {
1112 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
1116 } else if (tp
== constty
) {
1120 (*cdevsw
[major(constty
->t_dev
)].d_ioctl
)
1121 (constty
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1123 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
1124 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
1128 case TIOCDRAIN
: /* wait till output drained */
1129 error
= ttywait(tp
);
1134 case TIOCGETA_32
: /* get termios struct */
1136 termios64to32((struct user_termios
*)&tp
->t_termios
, (struct termios32
*)data
);
1138 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1141 case TIOCGETA_64
: /* get termios struct */
1143 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
1145 termios32to64((struct termios32
*)&tp
->t_termios
, (struct user_termios
*)data
);
1148 case TIOCGETD
: /* get line discipline */
1149 *(int *)data
= tp
->t_line
;
1151 case TIOCGWINSZ
: /* get window size */
1152 *(struct winsize
*)data
= tp
->t_winsize
;
1154 case TIOCGPGRP
: /* get pgrp of tty */
1155 if (!isctty(p
, tp
)) {
1159 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
1162 case TIOCHPCL
: /* hang up on last close */
1163 SET(tp
->t_cflag
, HUPCL
);
1166 case TIOCNXCL
: /* reset exclusive use of tty */
1167 CLR(tp
->t_state
, TS_XCLUDE
);
1169 case TIOCOUTQ
: /* output queue size */
1170 *(int *)data
= tp
->t_outq
.c_cc
;
1172 case TIOCSETA_32
: /* set termios struct */
1174 case TIOCSETAW_32
: /* drain output, set */
1176 case TIOCSETAF_32
: /* drn out, fls in, set */
1178 { /* drn out, fls in, set */
1179 struct termios
*t
= (struct termios
*)data
;
1180 struct termios lcl_termios
;
1183 if (cmd
== TIOCSETA_32
|| cmd
== TIOCSETAW_32
|| cmd
== TIOCSETAF_32
) {
1184 termios32to64((struct termios32
*)data
, (struct user_termios
*)&lcl_termios
);
1188 if (cmd
== TIOCSETA_64
|| cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
1189 termios64to32((struct user_termios
*)data
, (struct termios32
*)&lcl_termios
);
1194 /* XXX bogus test; always false */
1195 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0) {
1199 #endif /* 0 - leave in; may end up being a conformance issue */
1200 if (t
->c_ispeed
== 0) {
1201 t
->c_ispeed
= t
->c_ospeed
;
1203 if (cmd
== TIOCSETAW_32
|| cmd
== TIOCSETAF_32
||
1204 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
1205 error
= ttywait(tp
);
1209 if (cmd
== TIOCSETAF_32
|| cmd
== TIOCSETAF_64
) {
1210 ttyflush(tp
, FREAD
);
1213 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1215 * Set device hardware.
1217 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1220 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1221 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1223 * XXX disconnections would be too hard to
1224 * get rid of without this kludge. The only
1225 * way to get rid of controlling terminals
1226 * is to exit from the session leader.
1228 CLR(tp
->t_state
, TS_ZOMBIE
);
1230 wakeup(TSA_CARR_ON(tp
));
1234 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1235 ISSET(t
->c_cflag
, CLOCAL
)) &&
1236 !ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1237 SET(tp
->t_state
, TS_CONNECTED
);
1239 CLR(tp
->t_state
, TS_CONNECTED
);
1241 tp
->t_cflag
= t
->c_cflag
;
1242 tp
->t_ispeed
= t
->c_ispeed
;
1243 tp
->t_ospeed
= t
->c_ospeed
;
1246 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1247 cmd
!= TIOCSETAF_32
&& cmd
!= TIOCSETAF_64
) {
1248 if (ISSET(t
->c_lflag
, ICANON
)) {
1249 SET(tp
->t_lflag
, PENDIN
);
1252 * XXX we really shouldn't allow toggling
1253 * ICANON while we're in a non-termios line
1254 * discipline. Now we have to worry about
1255 * panicing for a null queue.
1257 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1260 catq(&tp
->t_rawq
, &tp
->t_canq
);
1262 tp
->t_rawq
= tp
->t_canq
;
1265 CLR(tp
->t_lflag
, PENDIN
);
1269 tp
->t_iflag
= t
->c_iflag
;
1270 tp
->t_oflag
= t
->c_oflag
;
1272 * Make the EXTPROC bit read only.
1274 if (ISSET(tp
->t_lflag
, EXTPROC
)) {
1275 SET(t
->c_lflag
, EXTPROC
);
1277 CLR(t
->c_lflag
, EXTPROC
);
1279 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1280 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1281 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
]) {
1284 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1287 case TIOCSETD
: { /* set line discipline */
1288 int t
= *(int *)data
;
1289 dev_t device
= tp
->t_dev
;
1291 if (t
>= nlinesw
|| t
< 0) {
1296 * If the new line discipline is not equal to the old one,
1297 * close the old one and open the new one.
1299 if (t
!= tp
->t_line
) {
1300 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1301 error
= (*linesw
[t
].l_open
)(device
, tp
);
1303 /* This is racy; it's possible to lose both */
1304 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1311 case TIOCSTART
: /* start output, like ^Q */
1312 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1313 ISSET(tp
->t_lflag
, FLUSHO
)) {
1314 CLR(tp
->t_lflag
, FLUSHO
);
1315 CLR(tp
->t_state
, TS_TTSTOP
);
1319 case TIOCSTI
: /* simulate terminal input */
1320 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0) {
1324 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
)) {
1328 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1330 case TIOCSTOP
: /* stop output, like ^S */
1331 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1332 SET(tp
->t_state
, TS_TTSTOP
);
1342 case TIOCSCTTY
: /* become controlling tty */
1343 /* Session ctty vnode pointer set in vnode layer. */
1344 sessp
= proc_session(p
);
1345 if (sessp
== SESSION_NULL
) {
1351 * This can only be done by a session leader.
1353 if (!SESS_LEADER(p
, sessp
)) {
1354 /* SAFE: All callers drop the lock on return */
1356 session_rele(sessp
);
1362 * If this terminal is already the controlling terminal for the
1363 * session, nothing to do here.
1365 if (tp
->t_session
== sessp
) {
1366 /* SAFE: All callers drop the lock on return */
1368 session_rele(sessp
);
1375 * Deny if the terminal is already attached to another session or
1376 * the session already has a terminal vnode.
1378 session_lock(sessp
);
1379 if (sessp
->s_ttyvp
|| tp
->t_session
) {
1380 session_unlock(sessp
);
1381 /* SAFE: All callers drop the lock on return */
1383 if (pg
!= PGRP_NULL
) {
1386 session_rele(sessp
);
1391 sessp
->s_ttypgrpid
= pg
->pg_id
;
1392 oldtp
= sessp
->s_ttyp
;
1395 session_unlock(sessp
);
1397 oldsessp
= tp
->t_session
;
1399 if (oldsessp
!= SESSION_NULL
) {
1400 oldsessp
->s_ttypgrpid
= NO_PID
;
1402 /* do not drop refs on sessp and pg as tp holds them */
1403 tp
->t_session
= sessp
;
1406 OSBitOrAtomic(P_CONTROLT
, &p
->p_flag
);
1407 /* SAFE: All callers drop the lock on return */
1409 /* drop the reference on prev session and pgrp */
1410 if (oldsessp
!= SESSION_NULL
) {
1411 session_rele(oldsessp
);
1413 if (oldpg
!= PGRP_NULL
) {
1416 if (NULL
!= oldtp
) {
1422 case TIOCSPGRP
: { /* set pgrp of tty */
1423 struct pgrp
*pgrp
= PGRP_NULL
;
1425 sessp
= proc_session(p
);
1426 if (!isctty_sp(p
, tp
, sessp
)) {
1427 if (sessp
!= SESSION_NULL
) {
1428 session_rele(sessp
);
1432 } else if ((pgrp
= pgfind(*(int *)data
)) == PGRP_NULL
) {
1433 if (sessp
!= SESSION_NULL
) {
1434 session_rele(sessp
);
1438 } else if (pgrp
->pg_session
!= sessp
) {
1439 /* SAFE: All callers drop the lock on return */
1441 if (sessp
!= SESSION_NULL
) {
1442 session_rele(sessp
);
1453 sessp
->s_ttypgrpid
= pgrp
->pg_id
;
1457 * Wakeup readers to recheck if they are still the foreground
1460 * ttwakeup() isn't called because the readers aren't getting
1461 * woken up because there is something to read but to force
1462 * the re-evaluation of their foreground process group status.
1464 * Ordinarily leaving these readers waiting wouldn't be an issue
1465 * as launchd would send them a termination signal eventually
1466 * (if nobody else does). But if this terminal happens to be
1467 * /dev/console, launchd itself could get blocked forever behind
1468 * a revoke of /dev/console and leave the system deadlocked.
1470 wakeup(TSA_HUP_OR_INPUT(tp
));
1472 /* SAFE: All callers drop the lock on return */
1474 if (oldpg
!= PGRP_NULL
) {
1477 if (sessp
!= SESSION_NULL
) {
1478 session_rele(sessp
);
1483 case TIOCSTAT
: /* simulate control-T */
1486 case TIOCSWINSZ
: /* set window size */
1487 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1488 sizeof(struct winsize
))) {
1489 tp
->t_winsize
= *(struct winsize
*)data
;
1490 /* SAFE: All callers drop the lock on return */
1492 tty_pgsignal(tp
, SIGWINCH
, 1);
1496 case TIOCSDRAINWAIT
:
1497 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1501 tp
->t_timeout
= *(int *)data
* hz
;
1502 wakeup(TSA_OCOMPLETE(tp
));
1503 wakeup(TSA_OLOWAT(tp
));
1505 case TIOCGDRAINWAIT
:
1506 *(int *)data
= tp
->t_timeout
/ hz
;
1509 SET(tp
->t_state
, TS_REVOKE
);
1512 * At this time, only this wait channel is woken up as only
1513 * ttread has been problematic. It is possible we may need
1514 * to add wake up other tty wait addresses as well.
1516 wakeup(TSA_HUP_OR_INPUT(tp
));
1518 case TIOCREVOKECLEAR
:
1519 CLR(tp
->t_state
, TS_REVOKE
);
1522 error
= ttcompat(tp
, cmd
, data
, flag
, p
);
1533 * Locks: Assumes tp is locked on entry, remains locked on exit
1536 ttyselect(struct tty
*tp
, int rw
, void *wql
, proc_t p
)
1540 * Attaching knotes to TTYs needs to call selrecord in order to hook
1541 * up the waitq to the selinfo, regardless of data being ready. See
1544 bool needs_selrecord
= rw
& FMARK
;
1553 if (tp
->t_state
& TS_ZOMBIE
) {
1560 retval
= ttnread(tp
);
1565 selrecord(p
, &tp
->t_rsel
, wql
);
1568 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1569 (tp
->t_state
& TS_CONNECTED
)) {
1570 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
1574 selrecord(p
, &tp
->t_wsel
, wql
);
1579 if (retval
> 0 && needs_selrecord
) {
1582 selrecord(p
, &tp
->t_rsel
, wql
);
1585 selrecord(p
, &tp
->t_wsel
, wql
);
1595 * This is a wrapper for compatibility with the select vector used by
1596 * cdevsw. It relies on a proper xxxdevtotty routine.
1598 * Locks: Assumes tty_lock() is not held prior to calling.
1601 ttselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
1604 struct tty
*tp
= cdevsw
[major(dev
)].d_ttys
[minor(dev
)];
1607 rv
= ttyselect(tp
, rw
, wql
, p
);
1615 * Locks: Assumes tp is locked on entry, remains locked on exit
1617 __private_extern__
int
1618 ttnread(struct tty
*tp
)
1622 TTY_LOCK_OWNED(tp
); /* debug assert */
1624 if (ISSET(tp
->t_lflag
, PENDIN
)) {
1627 nread
= tp
->t_canq
.c_cc
;
1628 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1629 nread
+= tp
->t_rawq
.c_cc
;
1630 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0) {
1641 * Wait for output to drain.
1643 * Parameters: tp Tty on which to wait for output to drain
1645 * Returns: 0 Success
1646 * EIO t_timeout too small/expired
1647 * ttysleep:ERESTART Upper layer must redrive the call;
1648 * this is usually done by the Libc
1649 * stub in user space
1650 * ttysleep:EINTR Interrupted (usually a signal)
1652 * Notes: Called from proc_exit() and vproc_exit().
1654 * Locks: Assumes tp is locked on entry, remains locked on exit
1657 ttywait(struct tty
*tp
)
1661 TTY_LOCK_OWNED(tp
); /* debug assert */
1664 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1665 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1667 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1668 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1669 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1670 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1671 TTOPRI
| PCATCH
, "ttywai",
1674 if (error
== EWOULDBLOCK
) {
1683 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
))) {
1690 * Stop the underlying device driver.
1692 * Locks: Assumes tty_lock() is held prior to calling.
1695 ttystop(struct tty
*tp
, int rw
)
1697 TTY_LOCK_OWNED(tp
); /* debug assert */
1699 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1703 * Flush if successfully wait.
1705 * Locks: Assumes tty_lock() is held prior to calling.
1708 ttywflush(struct tty
*tp
)
1712 TTY_LOCK_OWNED(tp
); /* debug assert */
1714 if ((error
= ttywait(tp
)) == 0) {
1715 ttyflush(tp
, FREAD
);
1721 * Flush tty read and/or write queues, notifying anyone waiting.
1723 * Locks: Assumes tty_lock() is held prior to calling.
1726 ttyflush(struct tty
*tp
, int rw
)
1728 TTY_LOCK_OWNED(tp
); /* debug assert */
1734 FLUSHQ(&tp
->t_outq
);
1735 CLR(tp
->t_state
, TS_TTSTOP
);
1739 FLUSHQ(&tp
->t_canq
);
1740 FLUSHQ(&tp
->t_rawq
);
1741 CLR(tp
->t_lflag
, PENDIN
);
1744 CLR(tp
->t_state
, TS_LOCAL
);
1746 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1748 FLUSHQ(&tp
->t_outq
);
1753 * Don't let leave any state that might clobber the
1754 * next line discipline (although we should do more
1755 * to send the START char). Not clearing the state
1756 * may have caused the "putc to a clist with no
1757 * reserved cblocks" panic/printf.
1759 CLR(tp
->t_state
, TS_TBLOCK
);
1761 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1762 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1764 * XXX wait a bit in the hope that the stop
1765 * character (if any) will go out. Waiting
1766 * isn't good since it allows races. This
1767 * will be fixed when the stop character is
1768 * put in a special queue. Don't bother with
1769 * the checks in ttywait() since the timeout
1772 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1773 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1776 * Don't try sending the stop character again.
1778 CLR(tp
->t_state
, TS_TBLOCK
);
1785 FLUSHQ(&tp
->t_outq
);
1791 * Copy in the default termios characters.
1793 * Locks: Assumes tty_lock() is held prior to calling.
1795 * Notes: No assertion; tp is not in scope.
1798 termioschars(struct termios
*t
)
1800 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1805 * Handle input high water. Send stop character for the IXOFF case. Turn
1806 * on our input flow control bit and propagate the changes to the driver.
1807 * XXX the stop character should be put in a special high priority queue.
1809 * Locks: Assumes tty_lock() is held for the call.
1812 ttyblock(struct tty
*tp
)
1814 TTY_LOCK_OWNED(tp
); /* debug assert */
1816 SET(tp
->t_state
, TS_TBLOCK
);
1817 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1818 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0) {
1819 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1826 * Handle input low water. Send start character for the IXOFF case. Turn
1827 * off our input flow control bit and propagate the changes to the driver.
1828 * XXX the start character should be put in a special high priority queue.
1830 * Locks: Assumes tty_lock() is held for the call.
1833 ttyunblock(struct tty
*tp
)
1835 TTY_LOCK_OWNED(tp
); /* debug assert */
1837 CLR(tp
->t_state
, TS_TBLOCK
);
1838 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1839 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0) {
1840 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1851 * Parameters: tp tty on which to start output
1853 * Returns: 0 Success
1855 * Locks: Assumes tty_lock() is held for the call.
1857 * Notes: This function might as well be void; it always returns success
1859 * Called from ttioctl_locked(), LDISC routines, and
1860 * ttycheckoutq(), ttyblock(), ttyunblock(), and tputchar()
1863 ttstart(struct tty
*tp
)
1865 TTY_LOCK_OWNED(tp
); /* debug assert */
1867 if (tp
->t_oproc
!= NULL
) { /* XXX: Kludge for pty. */
1878 * "close" a line discipline
1880 * Locks: Assumes tty_lock() is held prior to calling.
1883 ttylclose(struct tty
*tp
, int flag
)
1885 TTY_LOCK_OWNED(tp
); /* debug assert */
1887 if ((flag
& FNONBLOCK
) || ttywflush(tp
)) {
1888 ttyflush(tp
, FREAD
| FWRITE
);
1898 * Handle modem control transition on a tty.
1899 * Flag indicates new state of carrier.
1900 * Returns 0 if the line should be turned off, otherwise 1.
1902 * Locks: Assumes tty_lock() is held prior to calling.
1905 ttymodem(struct tty
*tp
, int flag
)
1907 int rval
= 1; /* default return value */
1909 TTY_LOCK_OWNED(tp
); /* debug assert */
1911 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1913 * MDMBUF: do flow control according to carrier flag
1914 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1915 * works if IXON and IXANY are clear.
1918 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1919 CLR(tp
->t_state
, TS_TTSTOP
);
1921 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1922 SET(tp
->t_state
, TS_CAR_OFLOW
);
1923 SET(tp
->t_state
, TS_TTSTOP
);
1926 } else if (flag
== 0) {
1930 CLR(tp
->t_state
, TS_CARR_ON
);
1931 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1932 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1933 SET(tp
->t_state
, TS_ZOMBIE
);
1934 CLR(tp
->t_state
, TS_CONNECTED
);
1935 if (tp
->t_session
&& tp
->t_session
->s_leader
) {
1936 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
);
1950 wakeup(TSA_CARR_ON(tp
));
1961 * Reinput pending characters after state switch
1964 * Locks: Assumes tty_lock() is held for the call.
1967 ttypend(struct tty
*tp
)
1972 TTY_LOCK_OWNED(tp
); /* debug assert */
1974 CLR(tp
->t_lflag
, PENDIN
);
1975 SET(tp
->t_state
, TS_TYPEN
);
1977 tp
->t_rawq
.c_cc
= 0;
1978 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= NULL
;
1979 while ((c
= getc(&tq
)) >= 0) {
1982 CLR(tp
->t_state
, TS_TYPEN
);
1989 * Process a read call on a tty device.
1991 * Locks: Assumes tty_lock() is held prior to calling.
1994 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1999 cc_t
*cc
= tp
->t_cc
;
2000 proc_t p
= current_proc();
2001 int first
, error
= 0;
2002 int has_etime
= 0, last_cc
= 0;
2003 long slp
= 0; /* XXX this should be renamed `timo'. */
2007 TTY_LOCK_OWNED(tp
); /* debug assert */
2009 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2012 lflag
= tp
->t_lflag
;
2014 * take pending input first
2016 if (ISSET(lflag
, PENDIN
)) {
2018 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
2022 * Signal the process if it's in the background. If the terminal is
2023 * getting revoked, everybody is in the background.
2025 if (isbackground(p
, tp
) || ISSET(tp
->t_state
, TS_REVOKE
)) {
2026 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
2027 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
2028 p
->p_lflag
& P_LPPWAIT
) {
2033 if (pg
== PGRP_NULL
) {
2037 if (pg
->pg_jobc
== 0) {
2038 /* SAFE: All callers drop the lock on return */
2045 /* SAFE: All callers drop the lock on return */
2047 pgsignal(pg
, SIGTTIN
, 1);
2052 * We signalled ourself, so we need to act as if we
2053 * have been "interrupted" from a "sleep" to act on
2054 * the signal. If it's a signal that stops the
2055 * process, that's handled in the signal sending code.
2061 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2062 /* EOF - returning 0 */
2067 * If canonical, use the canonical queue,
2068 * else use the raw queue.
2070 * (should get rid of clists...)
2072 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
2074 if (flag
& IO_NDELAY
) {
2078 if (ISSET(lflag
, ICANON
) || cc
[VMIN
] != 0) {
2079 error
= EWOULDBLOCK
;
2081 /* else polling - returning 0 */
2084 if (!ISSET(lflag
, ICANON
)) {
2087 struct timeval timecopy
;
2088 struct timeval etime
= {.tv_sec
= 0, .tv_usec
= 0}; /* protected by !has_etime */
2091 * Check each of the four combinations.
2092 * (m > 0 && t == 0) is the normal read case.
2093 * It should be fairly efficient, so we check that and its
2094 * companion case (m == 0 && t == 0) first.
2095 * For the other two cases, we compute the target sleep time
2106 /* m, t and qp->c_cc are all 0. 0 is enough input. */
2109 t
*= 100000; /* time in us */
2110 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
2111 ((t1).tv_usec - (t2).tv_usec))
2113 if (qp
->c_cc
<= 0) {
2116 if (qp
->c_cc
>= m
) {
2119 microuptime(&timecopy
);
2120 if (!has_etime
|| qp
->c_cc
> last_cc
) {
2121 /* first character or got a character, start timer */
2124 etime
.tv_sec
= t
/ 1000000;
2126 (__darwin_suseconds_t
)(t
- (etime
.tv_sec
* 1000000));
2127 timeradd(&etime
, &timecopy
, &etime
);
2131 /* nothing, check expiration */
2132 if (timercmp(&etime
, &timecopy
, <=)) {
2136 slp
= diff(etime
, timecopy
);
2139 } else { /* m == 0 */
2143 microuptime(&timecopy
);
2147 etime
.tv_sec
= t
/ 1000000;
2149 (__darwin_suseconds_t
)(t
- (etime
.tv_sec
* 1000000));
2150 timeradd(&etime
, &timecopy
, &etime
);
2154 if (timercmp(&etime
, &timecopy
, <=)) {
2155 /* Timed out, but 0 is enough input. */
2158 slp
= diff(etime
, timecopy
);
2163 * Rounding down may make us wake up just short
2164 * of the target, so we round up.
2165 * The formula is ceiling(slp * hz/1000000).
2166 * 32-bit arithmetic is enough for hz < 169.
2167 * XXX see hzto() for how to avoid overflow if hz
2168 * is large (divide by `tick' and/or arrange to
2169 * use hzto() if hz is large).
2171 slp
= (long) (((u_int32_t
)slp
* hz
) + 999999) / 1000000;
2174 if (qp
->c_cc
<= 0) {
2177 * There is no input, or not enough input and we can block.
2179 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
2180 ISSET(tp
->t_state
, TS_CONNECTED
) ?
2181 "ttyin" : "ttyhup", (int)slp
);
2182 if (error
== EWOULDBLOCK
) {
2188 * XXX what happens if another process eats some input
2189 * while we are asleep (not just here)? It would be
2190 * safest to detect changes and reset our state variables
2191 * (has_stime and last_cc).
2198 * Input present, check for input mapping and processing.
2201 if (ISSET(lflag
, ICANON
)
2202 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
))) {
2208 ssize_t size
= uio_resid(uio
);
2214 icc
= (int)MIN(size
, IBUFSIZ
);
2215 icc
= q_to_b(qp
, (u_char
*)ibuf
, icc
);
2222 error
= uiomove(ibuf
, icc
, uio
);
2224 * XXX if there was an error then we should ungetc() the
2225 * unmoved chars and reduce icc here.
2230 if (uio_resid(uio
) == 0) {
2246 * delayed suspend (^Y)
2248 if (CCEQ(cc
[VDSUSP
], c
) &&
2249 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
2251 * SAFE: All callers drop the lock on return and
2252 * SAFE: current thread will not change out from
2253 * SAFE: under us in the "goto loop" case.
2256 tty_pgsignal(tp
, SIGTSTP
, 1);
2259 error
= ttysleep(tp
, &ttread
, TTIPRI
| PCATCH
,
2269 * Interpret EOF only in canonical mode.
2271 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
)) {
2275 * Give user character.
2277 error
= ureadc(c
, uio
);
2279 /* XXX should ungetc(c, qp). */
2282 if (uio_resid(uio
) == 0) {
2286 * In canonical mode check for a "break character"
2287 * marking the end of a "line of input".
2289 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
)) {
2297 * Look to unblock input now that (presumably)
2298 * the input queue has gone down.
2300 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
2301 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
) {
2311 * Check the output queue on tp for space for a kernel message (from uprintf
2312 * or tprintf). Allow some space over the normal hiwater mark so we don't
2313 * lose messages due to normal flow control, but don't let the tty run amok.
2314 * Sleeps here are not interruptible, but we return prematurely if new signals
2317 * Locks: Assumes tty_lock() is held before calling
2319 * Notes: This function is called from tprintf() in subr_prf.c
2322 ttycheckoutq(struct tty
*tp
, int wait
)
2328 TTY_LOCK_OWNED(tp
); /* debug assert */
2330 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2332 hiwat
= tp
->t_hiwat
;
2333 oldsig
= wait
? ut
->uu_siglist
: 0;
2334 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100) {
2335 while (tp
->t_outq
.c_cc
> hiwat
) {
2337 if (tp
->t_outq
.c_cc
<= hiwat
) {
2340 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
2343 SET(tp
->t_state
, TS_SO_OLOWAT
);
2344 ttysleep(tp
, TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
2354 * Process a write call on a tty device.
2356 * Locks: Assumes tty_lock() is held prior to calling.
2359 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
2364 int i
, hiwat
, error
;
2370 TTY_LOCK_OWNED(tp
); /* debug assert */
2372 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
2373 hiwat
= tp
->t_hiwat
;
2374 count
= uio_resid(uio
);
2378 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
2379 if (uio_resid(uio
) == count
) {
2384 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2385 if (flag
& IO_NDELAY
) {
2386 error
= EWOULDBLOCK
;
2389 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2397 * Signal the process if it's in the background.
2400 if (isbackground(p
, tp
) &&
2401 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& P_LPPWAIT
) == 0 &&
2402 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2403 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2405 if (pg
== PGRP_NULL
) {
2409 if (pg
->pg_jobc
== 0) {
2410 /* SAFE: All callers drop the lock on return */
2417 /* SAFE: All callers drop the lock on return */
2419 pgsignal(pg
, SIGTTOU
, 1);
2423 * We signalled ourself, so we need to act as if we
2424 * have been "interrupted" from a "sleep" to act on
2425 * the signal. If it's a signal that stops the
2426 * process, that's handled in the signal sending code.
2432 * Process the user's data in at most OBUFSIZ chunks. Perform any
2433 * output translation. Keep track of high water mark, sleep on
2434 * overflow awaiting device aid in acquiring new space.
2436 while (uio_resid(uio
) > 0 || cc
> 0) {
2437 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2438 uio_setresid(uio
, 0);
2441 if (tp
->t_outq
.c_cc
> hiwat
) {
2445 * Grab a hunk of data from the user, unless we have some
2446 * leftover from last time.
2449 ssize_t size
= uio_resid(uio
);
2454 cc
= (int)MIN((size_t)size
, OBUFSIZ
);
2456 error
= uiomove(cp
, cc
, uio
);
2463 * If nothing fancy need be done, grab those characters we
2464 * can handle without any of ttyoutput's processing and
2465 * just transfer them to the output q. For those chars
2466 * which require special processing (as indicated by the
2467 * bits in char_type), call ttyoutput. After processing
2468 * a hunk of data, look for FLUSHO so ^O's will take effect
2472 if (!ISSET(tp
->t_oflag
, OPOST
)) {
2475 ce
= (int)((size_t)cc
- scanc((size_t)cc
,
2476 (u_char
*)cp
, char_type
, CCLASSMASK
));
2478 * If ce is zero, then we're processing
2479 * a special character through ttyoutput.
2483 if (ttyoutput(*cp
, tp
) >= 0) {
2489 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2490 tp
->t_outq
.c_cc
> hiwat
) {
2497 * A bunch of normal characters have been found.
2498 * Transfer them en masse to the output queue and
2499 * continue processing at the top of the loop.
2500 * If there are any further characters in this
2501 * <= OBUFSIZ chunk, the first should be a character
2502 * requiring special handling by ttyoutput.
2505 i
= b_to_q((u_char
*)cp
, ce
, &tp
->t_outq
);
2516 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2517 tp
->t_outq
.c_cc
> hiwat
) {
2525 * If cc is nonzero, we leave the uio structure inconsistent, as the
2526 * offset and iov pointers have moved forward, but it doesn't matter
2527 * (the call will either return short or restart with a new uio).
2529 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2535 * Since we are using ring buffers, if we can't insert any more into
2536 * the output queue, we can assume the ring is full and that someone
2537 * forgot to set the high water mark correctly. We set it and then
2538 * proceed as normal.
2540 hiwat
= tp
->t_outq
.c_cc
- 1;
2545 * This can only occur if FLUSHO is set in t_lflag,
2546 * or if ttstart/oproc is synchronous (or very fast).
2548 if (tp
->t_outq
.c_cc
<= hiwat
) {
2551 if (flag
& IO_NDELAY
) {
2552 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2553 return uio_resid(uio
) == count
? EWOULDBLOCK
: 0;
2555 SET(tp
->t_state
, TS_SO_OLOWAT
);
2556 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2558 if (error
== EWOULDBLOCK
) {
2569 * Rubout one character from the rawq of tp
2570 * as cleanly as possible.
2572 * Locks: Assumes tty_lock() is held prior to calling.
2575 ttyrub(int c
, struct tty
*tp
)
2581 TTY_LOCK_OWNED(tp
); /* debug assert */
2583 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
)) {
2586 CLR(tp
->t_lflag
, FLUSHO
);
2587 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2588 if (tp
->t_rocount
== 0) {
2590 * Messed up by ttwrite; retype
2595 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
)) {
2598 CLR(c
, ~TTY_CHARMASK
);
2599 switch (CCLASS(c
)) {
2601 if (!(ISSET(tp
->t_iflag
, IUTF8
) && CCONT(c
))) {
2610 if (ISSET(tp
->t_lflag
, ECHOCTL
)) {
2615 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2619 savecol
= tp
->t_column
;
2620 SET(tp
->t_state
, TS_CNTTB
);
2621 SET(tp
->t_lflag
, FLUSHO
);
2622 tp
->t_column
= tp
->t_rocol
;
2623 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2624 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
)) {
2627 CLR(tp
->t_lflag
, FLUSHO
);
2628 CLR(tp
->t_state
, TS_CNTTB
);
2630 /* savecol will now be length of the tab. */
2631 savecol
-= tp
->t_column
;
2632 tp
->t_column
+= savecol
;
2634 savecol
= 8; /* overflow fixup */
2636 while (--savecol
>= 0) {
2637 (void)ttyoutput('\b', tp
);
2641 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2642 printf(PANICSTR
, c
, CCLASS(c
));
2644 panic(PANICSTR
, c
, CCLASS(c
));
2648 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2649 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2650 SET(tp
->t_state
, TS_ERASE
);
2651 (void)ttyoutput('\\', tp
);
2655 ttyecho(tp
->t_cc
[VERASE
], tp
);
2662 * Back over count characters, erasing them.
2664 * Locks: Assumes tty_lock() is held prior to calling.
2667 ttyrubo(struct tty
*tp
, int count
)
2669 TTY_LOCK_OWNED(tp
); /* debug assert */
2671 while (count
-- > 0) {
2672 (void)ttyoutput('\b', tp
);
2673 (void)ttyoutput(' ', tp
);
2674 (void)ttyoutput('\b', tp
);
2681 * Reprint the rawq line. Note, it is assumed that c_cc has already
2684 * Locks: Assumes tty_lock() is held prior to calling.
2687 ttyretype(struct tty
*tp
)
2692 TTY_LOCK_OWNED(tp
); /* debug assert */
2694 /* Echo the reprint character. */
2695 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
) {
2696 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2699 (void)ttyoutput('\n', tp
);
2703 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2704 * BIT OF FIRST CHAR.
2706 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
)) {
2709 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
)) {
2712 CLR(tp
->t_state
, TS_ERASE
);
2714 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2720 * Echo a typed character to the terminal.
2722 * Locks: Assumes tty_lock() is held prior to calling.
2725 ttyecho(int c
, struct tty
*tp
)
2727 TTY_LOCK_OWNED(tp
); /* debug assert */
2729 if (!ISSET(tp
->t_state
, TS_CNTTB
)) {
2730 CLR(tp
->t_lflag
, FLUSHO
);
2732 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2733 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2734 ISSET(tp
->t_lflag
, EXTPROC
)) {
2737 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2738 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2739 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2740 (void)ttyoutput('^', tp
);
2741 CLR(c
, ~TTY_CHARMASK
);
2748 (void)ttyoutput(c
, tp
);
2752 ttwakeup_knote(struct selinfo
*sip
, long hint
)
2754 if ((sip
->si_flags
& SI_KNPOSTING
) == 0) {
2755 sip
->si_flags
|= SI_KNPOSTING
;
2756 KNOTE(&sip
->si_note
, hint
);
2757 sip
->si_flags
&= ~SI_KNPOSTING
;
2763 * Wake up any readers on a tty.
2765 * Locks: Assumes tty_lock() is held for the call.
2768 ttwakeup(struct tty
*tp
)
2770 TTY_LOCK_OWNED(tp
); /* debug assert */
2772 selwakeup(&tp
->t_rsel
);
2773 ttwakeup_knote(&tp
->t_rsel
, 0);
2774 if (ISSET(tp
->t_state
, TS_ASYNC
)) {
2776 * XXX: Callers may not revalidate it the tty is closed
2777 * XXX: out from under them by another thread, but we do
2778 * XXX: not support queued signals. This should be safe,
2779 * XXX: since the process we intend to wakeup is in the
2780 * XXX: process group, and will wake up because of the
2781 * XXX: signal anyway.
2784 tty_pgsignal(tp
, SIGIO
, 1);
2787 wakeup(TSA_HUP_OR_INPUT(tp
));
2794 * Wake up any writers on a tty.
2796 * Locks: Assumes tty_lock() is held prior to calling.
2799 ttwwakeup(struct tty
*tp
)
2801 TTY_LOCK_OWNED(tp
); /* debug assert */
2803 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2804 selwakeup(&tp
->t_wsel
);
2805 ttwakeup_knote(&tp
->t_wsel
, 0);
2807 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2808 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2809 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2810 wakeup(TSA_OCOMPLETE(tp
));
2812 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2813 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2814 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2815 wakeup(TSA_OLOWAT(tp
));
2821 * Look up a code for a specified speed in a conversion table;
2822 * used by drivers to map software speed values to hardware parameters.
2824 * Notes: No locks are assumed for this function; it does not
2825 * directly access struct tty.
2828 ttspeedtab(int speed
, struct speedtab
*table
)
2830 for (; table
->sp_speed
!= -1; table
++) {
2831 if (table
->sp_speed
== speed
) {
2832 return table
->sp_code
;
2840 * Set tty hi and low water marks.
2842 * Try to arrange the dynamics so there's about one second
2843 * from hi to low water.
2845 * Locks: Assumes tty_lock() is held prior to calling.
2848 ttsetwater(struct tty
*tp
)
2853 TTY_LOCK_OWNED(tp
); /* debug assert */
2855 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2857 cps
= tp
->t_ospeed
/ 10;
2858 static_assert(TTMAXLOWAT
<= UINT_MAX
, "max low water fits in unsigned int");
2859 static_assert(TTMINLOWAT
<= UINT_MAX
, "min low water fits in unsigned int");
2860 tp
->t_lowat
= x
= (unsigned int)CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2862 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2863 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2867 /* ttyinfo has been converted to the MACH kernel */
2868 #include <mach/thread_info.h>
2870 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2871 extern kern_return_t
thread_info_internal(thread_t thread
,
2872 thread_flavor_t flavor
,
2873 thread_info_t thread_info_out
,
2874 mach_msg_type_number_t
*thread_info_count
);
2878 * Report on state of foreground process group.
2880 * Locks: Assumes tty_lock() is held prior to calling.
2883 ttyinfo_locked(struct tty
*tp
)
2892 struct timeval utime
;
2893 struct timeval stime
;
2894 thread_basic_info_data_t basic_info
;
2895 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2898 TTY_LOCK_OWNED(tp
); /* debug assert */
2900 if (ttycheckoutq(tp
, 0) == 0) {
2904 /* Print load average. */
2905 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2906 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2909 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2910 * that pending input will be retyped on BS.
2912 if (tp
->t_session
== NULL
) {
2913 ttyprintf(tp
, "not a controlling terminal\n");
2917 if (tp
->t_pgrp
== NULL
) {
2918 ttyprintf(tp
, "no foreground process group\n");
2922 /* first process in process group */
2923 /* XXX is there a need for pgrp lock ? */
2924 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2925 ttyprintf(tp
, "empty foreground process group\n");
2931 * Pick the most interesting process and copy some of its
2932 * state for printing later.
2936 /* the proc_compare is non blocking fn, no need to use iterator */
2937 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2938 if (proc_compare(pick
, p
)) {
2942 pickpid
= pick
->p_pid
;
2946 /* SAFE: All callers drop the lock on return */
2951 pick
= proc_find(pickpid
);
2952 if (pick
== PROC_NULL
) {
2956 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2957 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2958 (thread
= vfs_context_thread(&uthread
->uu_context
)) == NULL
||
2959 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2960 ttyprintf(tp
, "foreground process without thread\n");
2966 switch (basic_info
.run_state
) {
2967 case TH_STATE_RUNNING
:
2970 case TH_STATE_STOPPED
:
2973 case TH_STATE_WAITING
:
2976 case TH_STATE_UNINTERRUPTIBLE
:
2977 state
= "uninterruptible";
2979 case TH_STATE_HALTED
:
2986 calcru(pick
, &utime
, &stime
, NULL
);
2988 /* Print command, pid, state, utime, and stime */
2989 ttyprintf(tp
, " cmd: %s %d %s %ld.%02du %ld.%02ds\n",
2993 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2994 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
3002 * Returns 1 if p2 is "better" than p1
3004 * The algorithm for picking the "interesting" process is thus:
3006 * 1) Only foreground processes are eligible - implied.
3007 * 2) Runnable processes are favored over anything else. The runner
3008 * with the highest cpu utilization is picked (p_estcpu). Ties are
3009 * broken by picking the highest pid.
3010 * 3) The sleeper with the shortest sleep time is next.
3011 * 4) Further ties are broken by picking the highest pid.
3013 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
3014 #define TESTAB(a, b) ((a)<<1 | (b))
3020 * Locks: pgrp_lock(p2) held on call to this function
3021 * tty_lock(tp) for p2's tty, for which p2 is the foreground
3022 * process, held on call to this function
3025 proc_compare(proc_t p1
, proc_t p2
)
3027 /* NOTE THIS FN needs to be NON BLOCKING */
3033 * see if at least one of them is runnable
3035 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
3042 * tie - favor one with highest recent cpu utilization
3044 #ifdef _PROC_HAS_SCHEDINFO_
3045 /* Without the support the fields are always zero */
3046 if (p2
->p_estcpu
> p1
->p_estcpu
) {
3049 if (p1
->p_estcpu
> p2
->p_estcpu
) {
3052 #endif /* _PROC_HAS_SCHEDINFO_ */
3053 return p2
->p_pid
> p1
->p_pid
; /* tie - return highest pid */
3058 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
3064 return p2
->p_pid
> p1
->p_pid
; /* tie - return highest pid */
3067 * pick the one with the smallest sleep time
3069 #ifdef _PROC_HAS_SCHEDINFO_
3070 /* Without the support the fields are always zero */
3071 if (p2
->p_slptime
> p1
->p_slptime
) {
3074 if (p1
->p_slptime
> p2
->p_slptime
) {
3077 #endif /* _PROC_HAS_SCHEDINFO_ */
3078 return p2
->p_pid
> p1
->p_pid
; /* tie - return highest pid */
3083 * Output char to tty; console putchar style.
3085 * Locks: Assumes tty_lock() is held prior to calling.
3087 * Notes: Only ever called from putchar() in subr_prf.c
3090 tputchar(int c
, struct tty
*tp
)
3092 TTY_LOCK_OWNED(tp
); /* debug assert */
3094 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
3098 (void)ttyoutput('\r', tp
);
3100 (void)ttyoutput(c
, tp
);
3109 * Sleep on a wait channel waiting for an interrupt or a condition to come
3110 * true so that we are woken up.
3112 * Parameters: tp Tty going to sleep
3113 * chan The sleep channel (usually an address
3114 * of a structure member)
3115 * pri priority and flags
3116 * wmesg Wait message; shows up in debugger,
3117 * should show up in "ps", but doesn't
3118 * timo Timeout for the sleep
3120 * Returns: 0 Condition came true
3121 * ERESTART Upper layer must redrive the call;
3122 * this is usually done by the Libc
3123 * stub in user space
3124 * msleep0:EINTR Interrupted (usually a signal)
3125 * msleep0:ERESTART Interrupted (usually a masked signal)
3126 * msleep0:EWOULDBLOCK Timeout (timo) already expired
3128 * Locks: Assumes tty_lock() is held prior to calling.
3130 * Sleep on chan, returning ERESTART if tty changed while we napped and
3131 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by msleep0. If
3132 * the tty is revoked, restarting a pending call will redo validation done
3133 * at the start of the call.
3136 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
3143 if (tp
->t_state
& TS_REVOKE
) {
3148 /* Use of msleep0() avoids conversion timo/timespec/timo */
3149 error
= msleep0(chan
, &tp
->t_lock
, pri
, wmesg
, timo
, (int (*)(int))0);
3153 return tp
->t_gen
== gen
? 0 : ERESTART
;
3158 * Allocate a tty structure and its associated buffers.
3162 * Returns: !NULL Address of new struct tty
3163 * NULL Error ("ENOMEM")
3165 * Locks: The tty_lock() of the returned tty is not held when it
3173 tp
= kheap_alloc(KM_TTYS
, sizeof(struct tty
), Z_WAITOK
| Z_ZERO
);
3175 /* XXX: default to TTYCLSIZE(1024) chars for now */
3176 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
3177 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
3178 /* output queue doesn't need quoting */
3179 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
3180 lck_mtx_init(&tp
->t_lock
, &tty_lck_grp
, LCK_ATTR_NULL
);
3181 klist_init(&tp
->t_rsel
.si_note
);
3182 klist_init(&tp
->t_wsel
.si_note
);
3189 * Increment the reference count on a tty.
3192 ttyhold(struct tty
*tp
)
3199 * Drops a reference count on a tty structure; if the reference count reaches
3200 * zero, then also frees the structure and associated buffers.
3203 ttyfree(struct tty
*tp
)
3205 TTY_LOCK_NOTOWNED(tp
);
3208 if (--tp
->t_refcnt
== 0) {
3211 } else if (tp
->t_refcnt
< 0) {
3212 panic("%s: freeing free tty %p", __func__
, tp
);
3219 * Deallocate a tty structure and its buffers.
3221 * Locks: The tty_lock() is assumed to not be held at the time of
3222 * the free; this function destroys the mutex.
3225 ttydeallocate(struct tty
*tp
)
3227 TTY_LOCK_NOTOWNED(tp
); /* debug assert */
3230 if (!(SLIST_EMPTY(&tp
->t_rsel
.si_note
) && SLIST_EMPTY(&tp
->t_wsel
.si_note
))) {
3231 panic("knotes hooked into a tty when the tty is freed.\n");
3235 clfree(&tp
->t_rawq
);
3236 clfree(&tp
->t_canq
);
3237 clfree(&tp
->t_outq
);
3238 lck_mtx_destroy(&tp
->t_lock
, &tty_lck_grp
);
3239 kheap_free(KM_TTYS
, tp
, sizeof(struct tty
));
3244 * Locks: Assumes tty_lock() is held prior to calling.
3247 isbackground(proc_t p
, struct tty
*tp
)
3251 return tp
->t_session
!= NULL
&& p
->p_pgrp
!= NULL
&& (p
->p_pgrp
!= tp
->t_pgrp
) && isctty_sp(p
, tp
, p
->p_pgrp
->pg_session
);
3255 isctty(proc_t p
, struct tty
*tp
)
3258 struct session
* sessp
;
3260 sessp
= proc_session(p
);
3261 retval
= (sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
);
3262 session_rele(sessp
);
3267 isctty_sp(proc_t p
, struct tty
*tp
, struct session
*sessp
)
3269 return sessp
== tp
->t_session
&& p
->p_flag
& P_CONTROLT
;
3273 static int filt_ttyattach(struct knote
*kn
, struct kevent_qos_s
*kev
);
3274 static void filt_ttydetach(struct knote
*kn
);
3275 static int filt_ttyevent(struct knote
*kn
, long hint
);
3276 static int filt_ttytouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
3277 static int filt_ttyprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
3279 SECURITY_READ_ONLY_EARLY(struct filterops
) tty_filtops
= {
3281 .f_attach
= filt_ttyattach
,
3282 .f_detach
= filt_ttydetach
,
3283 .f_event
= filt_ttyevent
,
3284 .f_touch
= filt_ttytouch
,
3285 .f_process
= filt_ttyprocess
3289 * Called with struct tty locked. Returns non-zero if there is data to be read
3293 filt_tty_common(struct knote
*kn
, struct kevent_qos_s
*kev
, struct tty
*tp
)
3298 TTY_LOCK_OWNED(tp
); /* debug assert */
3300 switch (kn
->kn_filter
) {
3303 * ttnread can change the tty state,
3304 * hence must be done upfront, before any other check.
3307 retval
= (data
!= 0);
3310 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
3311 (tp
->t_state
& TS_CONNECTED
)) {
3312 data
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
3313 retval
= (data
!= 0);
3317 panic("tty kevent: unexpected filter: %d, kn = %p, tty = %p",
3318 kn
->kn_filter
, kn
, tp
);
3323 * TODO(mwidmann, jandrus): For native knote low watermark support,
3324 * check the kn_sfflags for NOTE_LOWAT and check against kn_sdata.
3326 * res = ((kn->kn_sfflags & NOTE_LOWAT) != 0) ?
3327 * (kn->kn_data >= kn->kn_sdata) : kn->kn_data;
3330 if (tp
->t_state
& TS_ZOMBIE
) {
3331 kn
->kn_flags
|= EV_EOF
;
3333 if (kn
->kn_flags
& EV_EOF
) {
3336 if (retval
&& kev
) {
3337 knote_fill_kevent(kn
, kev
, data
);
3344 * Find the struct tty from a waitq, which is a member of one of the two struct
3345 * selinfos inside the struct tty. Use the seltype to determine which selinfo.
3348 tty_from_waitq(struct waitq
*wq
, int seltype
)
3351 struct tty
*tp
= NULL
;
3354 * The waitq is part of the selinfo structure managed by the driver. For
3355 * certain drivers, we want to hook the knote into the selinfo
3356 * structure's si_note field so selwakeup can call KNOTE.
3358 * While 'wq' is not really a queue element, this macro only uses the
3359 * pointer to calculate the offset into a structure given an element
3362 si
= qe_element(wq
, struct selinfo
, si_waitq
);
3365 * For TTY drivers, the selinfo structure is somewhere in the struct
3366 * tty. There are two different selinfo structures, and the one used
3367 * corresponds to the type of filter requested.
3369 * While 'si' is not really a queue element, this macro only uses the
3370 * pointer to calculate the offset into a structure given an element
3375 tp
= qe_element(si
, struct tty
, t_rsel
);
3378 tp
= qe_element(si
, struct tty
, t_wsel
);
3386 tty_from_knote(struct knote
*kn
)
3388 return (struct tty
*)kn
->kn_hook
;
3392 * Set the knote's struct tty to the kn_hook field.
3394 * The idea is to fake a call to select with our own waitq set. If the driver
3395 * calls selrecord, we'll get a link to their waitq and access to the tty
3398 * Returns -1 on failure, with the error set in the knote, or selres on success.
3401 tty_set_knote_hook(struct knote
*kn
)
3407 struct waitq
*wq
= NULL
;
3408 struct waitq_set
*old_wqs
;
3409 struct waitq_set tmp_wqs
;
3410 uint64_t rsvd
, rsvd_arg
;
3411 uint64_t *rlptr
= NULL
;
3415 uth
= get_bsdthread_info(current_thread());
3417 ctx
= vfs_context_current();
3418 vp
= (vnode_t
)kn
->kn_fp
->fp_glob
->fg_data
;
3421 * Reserve a link element to avoid potential allocation under
3424 rsvd
= rsvd_arg
= waitq_link_reserve(NULL
);
3425 rlptr
= (void *)&rsvd_arg
;
3428 * Trick selrecord into hooking a known waitq set into the device's selinfo
3429 * waitq. Once the link is in place, we can get back into the selinfo from
3430 * the waitq and subsequently the tty (see tty_from_waitq).
3432 * We can't use a real waitq set (such as the kqueue's) because wakeups
3433 * might happen before we can unlink it.
3435 kr
= waitq_set_init(&tmp_wqs
, SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
, NULL
,
3437 assert(kr
== KERN_SUCCESS
);
3440 * Lazy allocate the waitqset to avoid potential allocation under
3443 waitq_set_lazy_init_link(&tmp_wqs
);
3445 old_wqs
= uth
->uu_wqset
;
3446 uth
->uu_wqset
= &tmp_wqs
;
3448 * FMARK forces selects to always call selrecord, even if data is
3449 * available. See ttselect, ptsselect, ptcselect.
3451 * selres also contains the data currently available in the tty.
3453 selres
= VNOP_SELECT(vp
, knote_get_seltype(kn
) | FMARK
, 0, rlptr
, ctx
);
3454 uth
->uu_wqset
= old_wqs
;
3457 * Make sure to cleanup the reserved link - this guards against
3458 * drivers that may not actually call selrecord().
3460 waitq_link_release(rsvd
);
3461 if (rsvd
== rsvd_arg
) {
3463 * The driver didn't call selrecord -- there's no tty hooked up so we
3466 knote_set_error(kn
, ENOTTY
);
3471 /* rlptr may not point to a properly aligned pointer */
3472 memcpy(&wq
, rlptr
, sizeof(void *));
3474 tp
= tty_from_waitq(wq
, knote_get_seltype(kn
));
3478 * Take a reference and stash the tty in the knote.
3487 * Cleaning up the wqset will unlink its waitq and clean up any preposts
3488 * that occurred as a result of data coming in while the tty was attached.
3490 waitq_set_deinit(&tmp_wqs
);
3496 filt_ttyattach(struct knote
*kn
, __unused
struct kevent_qos_s
*kev
)
3502 * This function should be called from filt_specattach (spec_vnops.c),
3503 * so most of the knote data structure should already be initialized.
3506 /* don't support offsets in ttys or drivers that don't use struct tty */
3507 if (kn
->kn_vnode_use_ofst
|| !kn
->kn_vnode_kqok
) {
3508 knote_set_error(kn
, ENOTSUP
);
3513 * Connect the struct tty to the knote through the selinfo structure
3514 * referenced by the waitq within the selinfo.
3516 selres
= tty_set_knote_hook(kn
);
3522 * Attach the knote to selinfo's klist.
3524 tp
= tty_from_knote(kn
);
3527 switch (kn
->kn_filter
) {
3529 KNOTE_ATTACH(&tp
->t_rsel
.si_note
, kn
);
3532 KNOTE_ATTACH(&tp
->t_wsel
.si_note
, kn
);
3535 panic("invalid knote %p attach, filter: %d", kn
, kn
->kn_filter
);
3544 filt_ttydetach(struct knote
*kn
)
3546 struct tty
*tp
= tty_from_knote(kn
);
3550 switch (kn
->kn_filter
) {
3552 KNOTE_DETACH(&tp
->t_rsel
.si_note
, kn
);
3555 KNOTE_DETACH(&tp
->t_wsel
.si_note
, kn
);
3558 panic("invalid knote %p detach, filter: %d", kn
, kn
->kn_filter
);
3567 filt_ttyevent(struct knote
*kn
, long hint
)
3569 struct tty
*tp
= tty_from_knote(kn
);
3574 if (hint
& NOTE_REVOKE
) {
3575 kn
->kn_flags
|= EV_EOF
| EV_ONESHOT
;
3578 ret
= filt_tty_common(kn
, NULL
, tp
);
3585 filt_ttytouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
3587 struct tty
*tp
= tty_from_knote(kn
);
3592 kn
->kn_sdata
= kev
->data
;
3593 kn
->kn_sfflags
= kev
->fflags
;
3595 if (kn
->kn_vnode_kqok
) {
3596 res
= filt_tty_common(kn
, NULL
, tp
);
3605 filt_ttyprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
3607 struct tty
*tp
= tty_from_knote(kn
);
3612 res
= filt_tty_common(kn
, kev
, tp
);