2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1990, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tty.c 8.8 (Berkeley) 1/21/94
64 * o Fix races for sending the start char in ttyflush().
65 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
66 * With luck, there will be MIN chars before select() returns().
67 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
68 * o Don't allow input in TS_ZOMBIE case. It would be visible through
70 * o Do the new sio locking stuff here and use it to avoid special
73 * o Move EXTPROC and/or PENDIN to t_state?
74 * o Wrap most of ttioctl in spltty/splx.
75 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
76 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
77 * o Don't allow certain termios flags to affect disciplines other
78 * than TTYDISC. Cancel their effects before switch disciplines
79 * and ignore them if they are set while we are in another
81 * o Handle c_ispeed = 0 to c_ispeed = c_ospeed conversion here instead
82 * of in drivers and fix drivers that write to tp->t_termios.
83 * o Check for TS_CARR_ON being set while everything is closed and not
84 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
85 * so it would live until the next open even if carrier drops.
86 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
87 * only when _all_ openers leave open().
93 #include "opt_uconsole.h"
96 #include <sys/param.h>
98 #include <sys/systm.h>
100 #include <sys/ioctl.h>
101 #include <sys/proc_internal.h>
102 #include <sys/kauth.h>
103 #include <sys/file_internal.h>
104 #include <sys/conf.h>
105 #include <sys/dkstat.h>
107 #include <sys/kernel.h>
108 #include <sys/vnode.h>
109 #include <sys/syslog.h>
110 #include <sys/user.h>
111 #include <sys/signalvar.h>
112 #include <sys/signalvar.h>
114 #include <sys/resourcevar.h>
116 #include <sys/malloc.h>
118 #include <sys/snoop.h>
123 #include <vm/vm_param.h>
124 #include <vm/vm_prot.h>
127 #include <vm/vm_map.h>
129 #include <dev/kmreg_com.h>
130 #include <machine/cons.h>
131 #include <machine/spl.h>
133 #include <machdep/machine/pmap.h>
136 #include <sys/resource.h> /* averunnable */
139 static int proc_compare(struct proc
*p1
, struct proc
*p2
);
141 static int ttnread(struct tty
*tp
);
142 static void ttyecho(int c
, struct tty
*tp
);
143 static int ttyoutput(int c
, register struct tty
*tp
);
144 static void ttypend(struct tty
*tp
);
145 static void ttyretype(struct tty
*tp
);
146 static void ttyrub(int c
, struct tty
*tp
);
147 static void ttyrubo(struct tty
*tp
, int count
);
148 static void ttystop(struct tty
*tp
, int rw
);
149 static void ttyunblock(struct tty
*tp
);
150 static int ttywflush(struct tty
*tp
);
151 static int proc_compare(struct proc
*p1
, struct proc
*p2
);
154 * Table with character classes and parity. The 8th bit indicates parity,
155 * the 7th bit indicates the character is an alphameric or underscore (for
156 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
157 * are 0 then the character needs no special processing on output; classes
158 * other than 0 might be translated or (not currently) require delays.
160 #define E 0x00 /* Even parity. */
161 #define O 0x80 /* Odd parity. */
162 #define PARITY(c) (char_type[c] & O)
164 #define ALPHA 0x40 /* Alpha or underscore. */
165 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
167 #define CCLASSMASK 0x3f
168 #define CCLASS(c) (char_type[c] & CCLASSMASK)
173 #define NA ORDINARY | ALPHA
179 static u_char
const char_type
[] = {
180 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* nul - bel */
181 O
|BS
, E
|TB
, E
|NL
, O
|CC
, E
|VT
, O
|CR
, O
|CC
, E
|CC
, /* bs - si */
182 O
|CC
, E
|CC
, E
|CC
, O
|CC
, E
|CC
, O
|CC
, O
|CC
, E
|CC
, /* dle - etb */
183 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* can - us */
184 O
|NO
, E
|NO
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* sp - ' */
185 E
|NO
, O
|NO
, O
|NO
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|NO
, /* ( - / */
186 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* 0 - 7 */
187 O
|NA
, E
|NA
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* 8 - ? */
188 O
|NO
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* @ - G */
189 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* H - O */
190 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* P - W */
191 O
|NA
, E
|NA
, E
|NA
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, O
|NA
, /* X - _ */
192 E
|NO
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* ` - g */
193 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* h - o */
194 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* p - w */
195 E
|NA
, O
|NA
, O
|NA
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|CC
, /* x - del */
197 * Meta chars; should be settable per character set;
198 * for now, treat them all as normal characters.
200 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
201 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
202 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
203 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
204 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
205 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
206 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
207 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
208 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
209 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
210 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
211 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
212 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
213 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
214 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
215 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
226 /* Macros to clear/set/test flags. */
227 #define SET(t, f) (t) |= (f)
228 #define CLR(t, f) (t) &= ~(f)
229 #define ISSET(t, f) ((t) & (f))
232 * Input control starts when we would not be able to fit the maximum
233 * contents of the ping-pong buffers and finishes when we would be able
234 * to fit that much plus 1/8 more.
236 #define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */
237 #define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */
239 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
240 #define MAX_INPUT TTYHOG
243 termios32to64(struct termios
*in
, struct user_termios
*out
)
245 out
->c_iflag
= (user_tcflag_t
)in
->c_iflag
;
246 out
->c_oflag
= (user_tcflag_t
)in
->c_oflag
;
247 out
->c_cflag
= (user_tcflag_t
)in
->c_cflag
;
248 out
->c_lflag
= (user_tcflag_t
)in
->c_lflag
;
250 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
251 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
253 out
->c_ispeed
= (user_speed_t
)in
->c_ispeed
;
254 out
->c_ospeed
= (user_speed_t
)in
->c_ospeed
;
258 termios64to32(struct user_termios
*in
, struct termios
*out
)
260 out
->c_iflag
= (tcflag_t
)in
->c_iflag
;
261 out
->c_oflag
= (tcflag_t
)in
->c_oflag
;
262 out
->c_cflag
= (tcflag_t
)in
->c_cflag
;
263 out
->c_lflag
= (tcflag_t
)in
->c_lflag
;
265 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
266 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
268 out
->c_ispeed
= (speed_t
)in
->c_ispeed
;
269 out
->c_ospeed
= (speed_t
)in
->c_ospeed
;
274 * Initial open of tty, or (re)entry to standard tty line discipline.
279 register struct tty
*tp
;
282 boolean_t funnel_state
;
284 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
287 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
288 SET(tp
->t_state
, TS_ISOPEN
);
289 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
290 SET(tp
->t_state
, TS_CONNECTED
); }
291 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
296 * Initialize or restore a cblock allocation policy suitable for
297 * the standard line discipline.
299 clist_alloc_cblocks(&tp
->t_canq
, TTYHOG
, 512);
300 clist_alloc_cblocks(&tp
->t_outq
, TTMAXHIWAT
+ OBUFSIZ
+ 100,
301 TTMAXHIWAT
+ OBUFSIZ
+ 100);
302 clist_alloc_cblocks(&tp
->t_rawq
, TTYHOG
, TTYHOG
);
306 thread_funnel_set(kernel_flock
, funnel_state
);
311 * Handle close() on a tty line: flush and set to initial state,
312 * bumping generation number so that pending read/write calls
313 * can detect recycling of the tty.
314 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
315 * and l_close() should have flushed, but we repeat the spltty() and
316 * the flush in case there are buggy callers.
320 register struct tty
*tp
;
333 * Closing current console tty; disable printing of console
334 * messages at bottom-level driver.
336 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
337 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
341 ttyflush(tp
, FREAD
| FWRITE
);
343 clist_free_cblocks(&tp
->t_canq
);
344 clist_free_cblocks(&tp
->t_outq
);
345 clist_free_cblocks(&tp
->t_rawq
);
349 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
350 snpdown((struct snoop
*)tp
->t_sc
);
354 tp
->t_line
= TTYDISC
;
356 tp
->t_session
= NULL
;
359 selthreadclear(&tp
->t_wsel
);
360 selthreadclear(&tp
->t_rsel
);
366 #define FLUSHQ(q) { \
368 ndflush(q, (q)->c_cc); \
371 /* Is 'c' a line delimiter ("break" character)? */
372 #define TTBREAKC(c, lflag) \
373 ((c) == '\n' || (((c) == cc[VEOF] || \
374 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
375 (c) != _POSIX_VDISABLE))
378 * Process input of a single character received on a tty.
383 register struct tty
*tp
;
385 register tcflag_t iflag
, lflag
;
388 boolean_t funnel_state
;
390 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
393 * If input is pending take it first.
396 if (ISSET(lflag
, PENDIN
))
401 if (ISSET(lflag
, ICANON
)) {
411 * Block further input iff:
412 * current input > threshold AND input is available to user program
413 * AND input flow control is enabled and not yet invoked.
414 * The 3 is slop for PARMRK.
417 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
418 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
419 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
420 !ISSET(tp
->t_state
, TS_TBLOCK
))
423 /* Handle exceptional conditions (break, parity, framing). */
425 err
= (ISSET(c
, TTY_ERRORMASK
));
427 CLR(c
, TTY_ERRORMASK
);
428 if (ISSET(err
, TTY_BI
)) {
429 if (ISSET(iflag
, IGNBRK
)) {
430 thread_funnel_set(kernel_flock
, funnel_state
);
433 if (ISSET(iflag
, BRKINT
)) {
434 ttyflush(tp
, FREAD
| FWRITE
);
435 pgsignal(tp
->t_pgrp
, SIGINT
, 1);
438 if (ISSET(iflag
, PARMRK
))
440 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
441 || ISSET(err
, TTY_FE
)) {
442 if (ISSET(iflag
, IGNPAR
)) {
443 thread_funnel_set(kernel_flock
, funnel_state
);
446 else if (ISSET(iflag
, PARMRK
)) {
448 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
451 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
452 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
453 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
460 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
462 if (!ISSET(lflag
, EXTPROC
)) {
464 * Check for literal nexting very first
466 if (ISSET(tp
->t_state
, TS_LNCH
)) {
468 CLR(tp
->t_state
, TS_LNCH
);
471 * Scan for special characters. This code
472 * is really just a big case statement with
473 * non-constant cases. The bottom of the
474 * case statement is labeled ``endcase'', so goto
475 * it after a case match, or similar.
479 * Control chars which aren't controlled
480 * by ICANON, ISIG, or IXON.
482 if (ISSET(lflag
, IEXTEN
)) {
483 if (CCEQ(cc
[VLNEXT
], c
)) {
484 if (ISSET(lflag
, ECHO
)) {
485 if (ISSET(lflag
, ECHOE
)) {
486 (void)ttyoutput('^', tp
);
487 (void)ttyoutput('\b', tp
);
491 SET(tp
->t_state
, TS_LNCH
);
494 if (CCEQ(cc
[VDISCARD
], c
)) {
495 if (ISSET(lflag
, FLUSHO
))
496 CLR(tp
->t_lflag
, FLUSHO
);
498 ttyflush(tp
, FWRITE
);
500 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
502 SET(tp
->t_lflag
, FLUSHO
);
510 if (ISSET(lflag
, ISIG
)) {
511 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
512 if (!ISSET(lflag
, NOFLSH
))
513 ttyflush(tp
, FREAD
| FWRITE
);
516 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
519 if (CCEQ(cc
[VSUSP
], c
)) {
520 if (!ISSET(lflag
, NOFLSH
))
523 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
528 * Handle start/stop characters.
530 if (ISSET(iflag
, IXON
)) {
531 if (CCEQ(cc
[VSTOP
], c
)) {
532 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
533 SET(tp
->t_state
, TS_TTSTOP
);
535 thread_funnel_set(kernel_flock
, funnel_state
);
538 if (!CCEQ(cc
[VSTART
], c
)) {
539 thread_funnel_set(kernel_flock
, funnel_state
);
543 * if VSTART == VSTOP then toggle
547 if (CCEQ(cc
[VSTART
], c
))
551 * IGNCR, ICRNL, & INLCR
554 if (ISSET(iflag
, IGNCR
)) {
555 thread_funnel_set(kernel_flock
, funnel_state
);
558 else if (ISSET(iflag
, ICRNL
))
560 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
563 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
565 * From here on down canonical mode character
566 * processing takes place.
571 if (CCEQ(cc
[VERASE
], c
)) {
573 ttyrub(unputc(&tp
->t_rawq
), tp
);
579 if (CCEQ(cc
[VKILL
], c
)) {
580 if (ISSET(lflag
, ECHOKE
) &&
581 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
582 !ISSET(lflag
, ECHOPRT
))
583 while (tp
->t_rawq
.c_cc
)
584 ttyrub(unputc(&tp
->t_rawq
), tp
);
587 if (ISSET(lflag
, ECHOK
) ||
588 ISSET(lflag
, ECHOKE
))
593 CLR(tp
->t_state
, TS_LOCAL
);
599 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
605 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
610 * erase last char of word and remember the
611 * next chars type (for ALTWERASE)
614 c
= unputc(&tp
->t_rawq
);
617 if (c
== ' ' || c
== '\t') {
618 (void)putc(c
, &tp
->t_rawq
);
627 c
= unputc(&tp
->t_rawq
);
630 } while (c
!= ' ' && c
!= '\t' &&
631 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
632 (void)putc(c
, &tp
->t_rawq
);
638 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
643 * ^T - kernel info and generate SIGINFO
645 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
646 if (ISSET(lflag
, ISIG
))
647 pgsignal(tp
->t_pgrp
, SIGINFO
, 1);
648 if (!ISSET(lflag
, NOKERNINFO
))
654 * Check for input buffer overflow
656 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
658 if (ISSET(iflag
, IMAXBEL
)) {
659 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
660 (void)ttyoutput(CTRL('g'), tp
);
665 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
666 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
667 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
670 * Put data char in q for user and
671 * wakeup on seeing a line delimiter.
673 if (putc(c
, &tp
->t_rawq
) >= 0) {
674 if (!ISSET(lflag
, ICANON
)) {
679 if (TTBREAKC(c
, lflag
)) {
681 catq(&tp
->t_rawq
, &tp
->t_canq
);
683 } else if (tp
->t_rocount
++ == 0)
684 tp
->t_rocol
= tp
->t_column
;
685 if (ISSET(tp
->t_state
, TS_ERASE
)) {
687 * end of prterase \.../
689 CLR(tp
->t_state
, TS_ERASE
);
690 (void)ttyoutput('/', tp
);
694 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
696 * Place the cursor over the '^' of the ^D.
698 i
= min(2, tp
->t_column
- i
);
700 (void)ttyoutput('\b', tp
);
707 * IXANY means allow any character to restart output.
709 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
710 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
711 thread_funnel_set(kernel_flock
, funnel_state
);
715 CLR(tp
->t_lflag
, FLUSHO
);
716 CLR(tp
->t_state
, TS_TTSTOP
);
718 retval
= ttstart(tp
);
719 thread_funnel_set(kernel_flock
, funnel_state
);
724 * Output a single character on a tty, doing output processing
725 * as needed (expanding tabs, newline processing, etc.).
726 * Returns < 0 if succeeds, otherwise returns char to resend.
732 register struct tty
*tp
;
734 register tcflag_t oflag
;
738 if (!ISSET(oflag
, OPOST
)) {
739 if (ISSET(tp
->t_lflag
, FLUSHO
))
741 if (putc(c
, &tp
->t_outq
))
748 * Do tab expansion if OXTABS is set. Special case if we external
749 * processing, we don't do the tab expansion because we'll probably
750 * get it wrong. If tab expansion needs to be done, let it happen
753 CLR(c
, ~TTY_CHARMASK
);
755 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
756 c
= 8 - (tp
->t_column
& 7);
757 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
758 s
= spltty(); /* Don't interrupt tabs. */
759 c
-= b_to_q(" ", c
, &tp
->t_outq
);
765 return (c
? -1 : '\t');
767 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
771 * Newline translation: if ONLCR is set,
772 * translate newline into "\r\n".
774 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
777 if (putc('\r', &tp
->t_outq
))
782 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
801 col
= (col
+ 8) & ~7;
809 * Ioctls for all tty devices. Called after line-discipline specific ioctl
810 * has been called to do discipline-specific functions and/or reject any
811 * of these ioctl commands.
815 ttioctl(register struct tty
*tp
,
816 u_long cmd
, caddr_t data
, int flag
,
822 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
823 /* If the ioctl involves modification, hang if in the background. */
839 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
849 while (isbackground(p
, tp
) &&
850 (p
->p_flag
& P_PPWAIT
) == 0 &&
851 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
852 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
853 if (p
->p_pgrp
->pg_jobc
== 0)
855 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
856 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
| PTTYBLOCK
, "ttybg1",
864 switch (cmd
) { /* Process the ioctl. */
865 case FIOASYNC
: /* set/clear async i/o */
868 SET(tp
->t_state
, TS_ASYNC
);
870 CLR(tp
->t_state
, TS_ASYNC
);
873 case FIONBIO
: /* set/clear non-blocking i/o */
874 break; /* XXX: delete. */
875 case FIONREAD
: /* get # bytes to read */
877 *(int *)data
= ttnread(tp
);
880 case TIOCEXCL
: /* set exclusive use of tty */
882 SET(tp
->t_state
, TS_XCLUDE
);
885 case TIOCFLUSH
: { /* flush buffers */
886 register int flags
= *(int *)data
;
889 flags
= FREAD
| FWRITE
;
891 flags
&= FREAD
| FWRITE
;
897 /* Set current console device to this line */
899 data
= (caddr_t
) &bogusData
;
901 /* No break - Fall through to BSD code */
904 case TIOCCONS
: { /* become virtual console */
906 if (constty
&& constty
!= tp
&&
907 ISSET(constty
->t_state
, TS_CONNECTED
)) {
910 #if defined(NeXT) || !defined(UCONSOLE)
911 if ( (error
= suser(kauth_cred_get(), &p
->p_acflag
)) )
915 } else if (tp
== constty
) {
920 (*cdevsw
[major(cons
.t_dev
)].d_ioctl
)
921 (cons
.t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
923 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
924 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
929 case TIOCDRAIN
: /* wait till output drained */
934 case TIOCGETA
: /* get termios struct */
935 case TIOCGETA_64
: { /* get termios struct */
936 if (IS_64BIT_PROCESS(p
)) {
937 termios32to64(&tp
->t_termios
, (struct user_termios
*)data
);
939 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
943 case TIOCGETD
: /* get line discipline */
944 *(int *)data
= tp
->t_line
;
946 case TIOCGWINSZ
: /* get window size */
947 *(struct winsize
*)data
= tp
->t_winsize
;
949 case TIOCGPGRP
: /* get pgrp of tty */
952 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
955 case TIOCHPCL
: /* hang up on last close */
957 SET(tp
->t_cflag
, HUPCL
);
961 case TIOCNXCL
: /* reset exclusive use of tty */
963 CLR(tp
->t_state
, TS_XCLUDE
);
966 case TIOCOUTQ
: /* output queue size */
967 *(int *)data
= tp
->t_outq
.c_cc
;
969 case TIOCSETA
: /* set termios struct */
971 case TIOCSETAW
: /* drain output, set */
973 case TIOCSETAF
: /* drn out, fls in, set */
974 case TIOCSETAF_64
: { /* drn out, fls in, set */
975 register struct termios
*t
= (struct termios
*)data
;
976 struct termios lcl_termios
;
978 if (IS_64BIT_PROCESS(p
)) {
979 termios64to32((struct user_termios
*)data
, &lcl_termios
);
982 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0)
985 if (cmd
== TIOCSETAW
|| cmd
== TIOCSETAF
||
986 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
992 if (cmd
== TIOCSETAF
|| cmd
== TIOCSETAF_64
)
995 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
997 * Set device hardware.
999 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1003 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1004 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1006 * XXX disconnections would be too hard to
1007 * get rid of without this kludge. The only
1008 * way to get rid of controlling terminals
1009 * is to exit from the session leader.
1011 CLR(tp
->t_state
, TS_ZOMBIE
);
1013 wakeup(TSA_CARR_ON(tp
));
1017 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1018 ISSET(t
->c_cflag
, CLOCAL
)) &&
1019 !ISSET(tp
->t_state
, TS_ZOMBIE
))
1020 SET(tp
->t_state
, TS_CONNECTED
);
1022 CLR(tp
->t_state
, TS_CONNECTED
);
1023 tp
->t_cflag
= t
->c_cflag
;
1024 tp
->t_ispeed
= t
->c_ispeed
;
1025 tp
->t_ospeed
= t
->c_ospeed
;
1028 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1029 cmd
!= TIOCSETAF
&& cmd
!= TIOCSETAF_64
) {
1030 if (ISSET(t
->c_lflag
, ICANON
))
1031 SET(tp
->t_lflag
, PENDIN
);
1034 * XXX we really shouldn't allow toggling
1035 * ICANON while we're in a non-termios line
1036 * discipline. Now we have to worry about
1037 * panicing for a null queue.
1040 if (tp
->t_canq
.c_cbreserved
> 0 &&
1041 tp
->t_rawq
.c_cbreserved
> 0) {
1042 catq(&tp
->t_rawq
, &tp
->t_canq
);
1044 * XXX the queue limits may be
1045 * different, so the old queue
1046 * swapping method no longer works.
1048 catq(&tp
->t_canq
, &tp
->t_rawq
);
1051 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1054 catq(&tp
->t_rawq
, &tp
->t_canq
);
1056 tp
->t_rawq
= tp
->t_canq
;
1060 CLR(tp
->t_lflag
, PENDIN
);
1064 tp
->t_iflag
= t
->c_iflag
;
1065 tp
->t_oflag
= t
->c_oflag
;
1067 * Make the EXTPROC bit read only.
1069 if (ISSET(tp
->t_lflag
, EXTPROC
))
1070 SET(t
->c_lflag
, EXTPROC
);
1072 CLR(t
->c_lflag
, EXTPROC
);
1073 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1074 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1075 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1077 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1081 case TIOCSETD
: { /* set line discipline */
1082 register int t
= *(int *)data
;
1083 dev_t device
= tp
->t_dev
;
1087 if (t
!= tp
->t_line
) {
1089 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1090 error
= (*linesw
[t
].l_open
)(device
, tp
);
1092 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1101 case TIOCSTART
: /* start output, like ^Q */
1103 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1104 ISSET(tp
->t_lflag
, FLUSHO
)) {
1105 CLR(tp
->t_lflag
, FLUSHO
);
1106 CLR(tp
->t_state
, TS_TTSTOP
);
1111 case TIOCSTI
: /* simulate terminal input */
1112 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0)
1114 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
))
1117 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1120 case TIOCSTOP
: /* stop output, like ^S */
1122 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1123 SET(tp
->t_state
, TS_TTSTOP
);
1128 case TIOCSCTTY
: /* become controlling tty */
1129 /* Session ctty vnode pointer set in vnode layer. */
1130 if (!SESS_LEADER(p
) ||
1131 ((p
->p_session
->s_ttyvp
|| tp
->t_session
) &&
1132 (tp
->t_session
!= p
->p_session
)))
1134 tp
->t_session
= p
->p_session
;
1135 tp
->t_pgrp
= p
->p_pgrp
;
1136 p
->p_session
->s_ttyp
= tp
;
1137 p
->p_flag
|= P_CONTROLT
;
1138 /* The backgrounded process blocking on tty now
1139 * could be foregound process. Wake such processes
1141 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1143 case TIOCSPGRP
: { /* set pgrp of tty */
1144 register struct pgrp
*pgrp
= pgfind(*(int *)data
);
1148 else if (pgrp
== NULL
|| pgrp
->pg_session
!= p
->p_session
)
1151 /* The backgrounded process blocking on tty now
1152 * could be foregound process. Wake such processes
1154 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1157 case TIOCSTAT
: /* simulate control-T */
1162 case TIOCSWINSZ
: /* set window size */
1163 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1164 sizeof (struct winsize
))) {
1165 tp
->t_winsize
= *(struct winsize
*)data
;
1166 pgsignal(tp
->t_pgrp
, SIGWINCH
, 1);
1169 case TIOCSDRAINWAIT
:
1170 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1173 tp
->t_timeout
= *(int *)data
* hz
;
1174 wakeup(TSA_OCOMPLETE(tp
));
1175 wakeup(TSA_OLOWAT(tp
));
1177 case TIOCGDRAINWAIT
:
1178 *(int *)data
= tp
->t_timeout
/ hz
;
1181 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
1183 return (ttcompat(tp
, cmd
, data
, flag
, p
));
1185 return (ttcompat(tp
, cmd
, data
, flag
));
1196 ttyselect(tp
, rw
, wql
, p
)
1210 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
))
1212 selrecord(p
, &tp
->t_rsel
, wql
);
1215 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1216 ISSET(tp
->t_state
, TS_CONNECTED
))
1217 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1221 selrecord(p
, &tp
->t_wsel
, wql
);
1229 * This is a wrapper for compatibility with the select vector used by
1230 * cdevsw. It relies on a proper xxxdevtotty routine.
1233 ttselect(dev
, rw
, wql
, p
)
1240 return ttyselect((*cdevsw
[major(dev
)]->d_devtotty
)(dev
), rw
, wql
, p
);
1242 return ttyselect(cdevsw
[major(dev
)].d_ttys
[minor(dev
)], rw
, wql
, p
);
1247 * Must be called at spltty().
1255 if (ISSET(tp
->t_lflag
, PENDIN
))
1257 nread
= tp
->t_canq
.c_cc
;
1258 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1259 nread
+= tp
->t_rawq
.c_cc
;
1260 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1267 * Wait for output to drain.
1271 register struct tty
*tp
;
1277 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1278 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1280 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1281 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1282 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1283 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1284 TTOPRI
| PCATCH
, "ttywai",
1287 if (error
== EWOULDBLOCK
)
1294 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1305 #ifdef sun4c /* XXX */
1306 (*tp
->t_stop
)(tp
, rw
);
1308 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1310 (*cdevsw
[major(tp
->t_dev
)]->d_stop
)(tp
, rw
);
1315 * Flush if successfully wait.
1323 if ((error
= ttywait(tp
)) == 0)
1324 ttyflush(tp
, FREAD
);
1329 * Flush tty read and/or write queues, notifying anyone waiting.
1333 register struct tty
*tp
;
1343 FLUSHQ(&tp
->t_outq
);
1344 CLR(tp
->t_state
, TS_TTSTOP
);
1348 FLUSHQ(&tp
->t_canq
);
1349 FLUSHQ(&tp
->t_rawq
);
1350 CLR(tp
->t_lflag
, PENDIN
);
1353 CLR(tp
->t_state
, TS_LOCAL
);
1355 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1357 FLUSHQ(&tp
->t_outq
);
1361 * Don't let leave any state that might clobber the
1362 * next line discipline (although we should do more
1363 * to send the START char). Not clearing the state
1364 * may have caused the "putc to a clist with no
1365 * reserved cblocks" panic/printf.
1367 CLR(tp
->t_state
, TS_TBLOCK
);
1369 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1370 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1372 * XXX wait a bit in the hope that the stop
1373 * character (if any) will go out. Waiting
1374 * isn't good since it allows races. This
1375 * will be fixed when the stop character is
1376 * put in a special queue. Don't bother with
1377 * the checks in ttywait() since the timeout
1380 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1381 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1384 * Don't try sending the stop character again.
1386 CLR(tp
->t_state
, TS_TBLOCK
);
1393 FLUSHQ(&tp
->t_outq
);
1400 * Copy in the default termios characters.
1407 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1418 termioschars(&tp
->t_termios
);
1422 * Handle input high water. Send stop character for the IXOFF case. Turn
1423 * on our input flow control bit and propagate the changes to the driver.
1424 * XXX the stop character should be put in a special high priority queue.
1431 SET(tp
->t_state
, TS_TBLOCK
);
1432 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1433 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1434 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1439 * Handle input low water. Send start character for the IXOFF case. Turn
1440 * off our input flow control bit and propagate the changes to the driver.
1441 * XXX the start character should be put in a special high priority queue.
1448 CLR(tp
->t_state
, TS_TBLOCK
);
1449 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1450 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1451 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1455 #if defined(NeXT) || defined(notyet)
1456 /* FreeBSD: Not used by any current (i386) drivers. */
1458 * Restart after an inter-char delay.
1474 CLR(tp
->t_state
, TS_TIMEOUT
);
1479 #endif /* NeXT || notyet */
1485 boolean_t funnel_state
;
1487 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1489 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1491 thread_funnel_set(kernel_flock
, funnel_state
);
1496 * "close" a line discipline
1503 boolean_t funnel_state
;
1505 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1506 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1507 ttyflush(tp
, FREAD
| FWRITE
);
1508 thread_funnel_set(kernel_flock
, funnel_state
);
1513 * Handle modem control transition on a tty.
1514 * Flag indicates new state of carrier.
1515 * Returns 0 if the line should be turned off, otherwise 1.
1519 register struct tty
*tp
;
1522 boolean_t funnel_state
;
1524 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1526 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1528 * MDMBUF: do flow control according to carrier flag
1529 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1530 * works if IXON and IXANY are clear.
1533 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1534 CLR(tp
->t_state
, TS_TTSTOP
);
1536 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1537 SET(tp
->t_state
, TS_CAR_OFLOW
);
1538 SET(tp
->t_state
, TS_TTSTOP
);
1541 } else if (flag
== 0) {
1545 CLR(tp
->t_state
, TS_CARR_ON
);
1546 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1547 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1548 SET(tp
->t_state
, TS_ZOMBIE
);
1549 CLR(tp
->t_state
, TS_CONNECTED
);
1550 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1551 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1552 ttyflush(tp
, FREAD
| FWRITE
);
1553 thread_funnel_set(kernel_flock
, funnel_state
);
1560 SET(tp
->t_state
, TS_CARR_ON
);
1561 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1562 SET(tp
->t_state
, TS_CONNECTED
);
1563 wakeup(TSA_CARR_ON(tp
));
1567 thread_funnel_set(kernel_flock
, funnel_state
);
1572 * Reinput pending characters after state switch
1577 register struct tty
*tp
;
1582 CLR(tp
->t_lflag
, PENDIN
);
1583 SET(tp
->t_state
, TS_TYPEN
);
1586 * XXX this assumes too much about clist internals. It may even
1587 * fail if the cblock slush pool is empty. We can't allocate more
1588 * cblocks here because we are called from an interrupt handler
1589 * and clist_alloc_cblocks() can wait.
1592 bzero(&tp
->t_rawq
, sizeof tp
->t_rawq
);
1593 tp
->t_rawq
.c_cbmax
= tq
.c_cbmax
;
1594 tp
->t_rawq
.c_cbreserved
= tq
.c_cbreserved
;
1597 tp
->t_rawq
.c_cc
= 0;
1598 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
1600 while ((c
= getc(&tq
)) >= 0)
1602 CLR(tp
->t_state
, TS_TYPEN
);
1606 * Process a read call on a tty device.
1609 ttread(tp
, uio
, flag
)
1610 register struct tty
*tp
;
1614 register struct clist
*qp
;
1616 register tcflag_t lflag
;
1617 register cc_t
*cc
= tp
->t_cc
;
1618 register struct proc
*p
= current_proc();
1619 int s
, first
, error
= 0;
1620 int has_etime
= 0, last_cc
= 0;
1621 long slp
= 0; /* XXX this should be renamed `timo'. */
1622 boolean_t funnel_state
;
1625 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1627 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1631 lflag
= tp
->t_lflag
;
1633 * take pending input first
1635 if (ISSET(lflag
, PENDIN
)) {
1637 splx(s
); /* reduce latency */
1639 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1643 * Hang process if it's in the background.
1645 if (isbackground(p
, tp
)) {
1647 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1648 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1649 p
->p_flag
& P_PPWAIT
|| p
->p_pgrp
->pg_jobc
== 0) {
1650 thread_funnel_set(kernel_flock
, funnel_state
);
1653 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
1654 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg2", 0);
1656 thread_funnel_set(kernel_flock
, funnel_state
);
1662 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1664 thread_funnel_set(kernel_flock
, funnel_state
);
1665 return (0); /* EOF */
1669 * If canonical, use the canonical queue,
1670 * else use the raw queue.
1672 * (should get rid of clists...)
1674 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
1676 if (flag
& IO_NDELAY
) {
1679 if (!ISSET(lflag
, ICANON
) && cc
[VMIN
] == 0) {
1681 thread_funnel_set(kernel_flock
, funnel_state
);
1685 thread_funnel_set(kernel_flock
, funnel_state
);
1686 return (EWOULDBLOCK
);
1688 if (!ISSET(lflag
, ICANON
)) {
1691 struct timeval etime
, timecopy
;
1694 * Check each of the four combinations.
1695 * (m > 0 && t == 0) is the normal read case.
1696 * It should be fairly efficient, so we check that and its
1697 * companion case (m == 0 && t == 0) first.
1698 * For the other two cases, we compute the target sleep time
1707 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1709 thread_funnel_set(kernel_flock
, funnel_state
);
1712 t
*= 100000; /* time in us */
1713 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1714 ((t1).tv_usec - (t2).tv_usec))
1720 microuptime(&timecopy
);
1722 /* first character, start timer */
1725 etime
.tv_sec
= t
/ 1000000;
1726 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1727 timeradd(&etime
, &timecopy
, &etime
);
1730 } else if (qp
->c_cc
> last_cc
) {
1731 /* got a character, restart timer */
1733 etime
.tv_sec
= t
/ 1000000;
1734 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1735 timeradd(&etime
, &timecopy
, &etime
);
1739 /* nothing, check expiration */
1740 if (timercmp(&etime
, &timecopy
, <=))
1743 slp
= diff(etime
, timecopy
);
1746 } else { /* m == 0 */
1749 microuptime(&timecopy
);
1753 etime
.tv_sec
= t
/ 1000000;
1754 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1755 timeradd(&etime
, &timecopy
, &etime
);
1759 if (timercmp(&etime
, &timecopy
, <=)) {
1760 /* Timed out, but 0 is enough input. */
1762 thread_funnel_set(kernel_flock
, funnel_state
);
1765 slp
= diff(etime
, timecopy
);
1770 * Rounding down may make us wake up just short
1771 * of the target, so we round up.
1772 * The formula is ceiling(slp * hz/1000000).
1773 * 32-bit arithmetic is enough for hz < 169.
1774 * XXX see hzto() for how to avoid overflow if hz
1775 * is large (divide by `tick' and/or arrange to
1776 * use hzto() if hz is large).
1778 slp
= (long) (((u_long
)slp
* hz
) + 999999) / 1000000;
1781 if (qp
->c_cc
<= 0) {
1784 * There is no input, or not enough input and we can block.
1786 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
1787 ISSET(tp
->t_state
, TS_CONNECTED
) ?
1788 "ttyin" : "ttyhup", (int)slp
);
1790 if (error
== EWOULDBLOCK
)
1793 thread_funnel_set(kernel_flock
, funnel_state
);
1797 * XXX what happens if another process eats some input
1798 * while we are asleep (not just here)? It would be
1799 * safest to detect changes and reset our state variables
1800 * (has_stime and last_cc).
1808 * Input present, check for input mapping and processing.
1812 if (ISSET(lflag
, ICANON
)
1813 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
1815 if (ISSET(lflag
, ICANON
| ISIG
))
1822 icc
= min(uio_resid(uio
), IBUFSIZ
);
1823 icc
= q_to_b(qp
, ibuf
, icc
);
1829 error
= uiomove(ibuf
, icc
, uio
);
1831 * XXX if there was an error then we should ungetc() the
1832 * unmoved chars and reduce icc here.
1835 if (ISSET(tp
->t_lflag
, ECHO
) &&
1836 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1837 snpin((struct snoop
*)tp
->t_sc
, ibuf
, icc
);
1841 if (uio_resid(uio
) == 0)
1855 * delayed suspend (^Y)
1857 if (CCEQ(cc
[VDSUSP
], c
) &&
1858 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
1859 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
1861 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
,
1870 * Interpret EOF only in canonical mode.
1872 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
1875 * Give user character.
1877 error
= ureadc(c
, uio
);
1879 /* XXX should ungetc(c, qp). */
1883 * Only snoop directly on input in echo mode. Non-echoed
1884 * input will be snooped later iff the application echoes it.
1886 if (ISSET(tp
->t_lflag
, ECHO
) &&
1887 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1888 snpinc((struct snoop
*)tp
->t_sc
, (char)c
);
1890 if (uio_resid(uio
) == 0)
1893 * In canonical mode check for a "break character"
1894 * marking the end of a "line of input".
1896 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
1903 * Look to unblock input now that (presumably)
1904 * the input queue has gone down.
1907 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
1908 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
1912 thread_funnel_set(kernel_flock
, funnel_state
);
1917 * Check the output queue on tp for space for a kernel message (from uprintf
1918 * or tprintf). Allow some space over the normal hiwater mark so we don't
1919 * lose messages due to normal flow control, but don't let the tty run amok.
1920 * Sleeps here are not interruptible, but we return prematurely if new signals
1924 ttycheckoutq(tp
, wait
)
1925 register struct tty
*tp
;
1932 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1934 hiwat
= tp
->t_hiwat
;
1936 oldsig
= wait
? ut
->uu_siglist
: 0;
1937 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
1938 while (tp
->t_outq
.c_cc
> hiwat
) {
1940 if (tp
->t_outq
.c_cc
<= hiwat
)
1942 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
1946 SET(tp
->t_state
, TS_SO_OLOWAT
);
1947 tsleep(TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
1954 * Process a write call on a tty device.
1957 ttwrite(tp
, uio
, flag
)
1958 register struct tty
*tp
;
1959 register struct uio
*uio
;
1962 register char *cp
= NULL
;
1963 register int cc
, ce
;
1964 register struct proc
*p
;
1965 int i
, hiwat
, count
, error
, s
;
1967 boolean_t funnel_state
;
1970 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1972 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1973 hiwat
= tp
->t_hiwat
;
1974 // LP64todo - fix this!
1975 count
= uio_resid(uio
);
1980 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1982 if (uio_resid(uio
) == count
)
1986 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
1987 if (flag
& IO_NDELAY
) {
1989 error
= EWOULDBLOCK
;
1992 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2001 * Hang the process if it's in the background.
2004 if (isbackground(p
, tp
) &&
2005 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_flag
& P_PPWAIT
) == 0 &&
2006 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2007 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2008 if (p
->p_pgrp
->pg_jobc
== 0) {
2012 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
2013 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg4", 0);
2019 * Process the user's data in at most OBUFSIZ chunks. Perform any
2020 * output translation. Keep track of high water mark, sleep on
2021 * overflow awaiting device aid in acquiring new space.
2023 while (uio_resid(uio
) > 0 || cc
> 0) {
2024 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2025 uio_setresid(uio
, 0);
2026 thread_funnel_set(kernel_flock
, funnel_state
);
2029 if (tp
->t_outq
.c_cc
> hiwat
)
2032 * Grab a hunk of data from the user, unless we have some
2033 * leftover from last time.
2036 cc
= min(uio_resid(uio
), OBUFSIZ
);
2038 error
= uiomove(cp
, cc
, uio
);
2044 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
2045 snpin((struct snoop
*)tp
->t_sc
, cp
, cc
);
2049 * If nothing fancy need be done, grab those characters we
2050 * can handle without any of ttyoutput's processing and
2051 * just transfer them to the output q. For those chars
2052 * which require special processing (as indicated by the
2053 * bits in char_type), call ttyoutput. After processing
2054 * a hunk of data, look for FLUSHO so ^O's will take effect
2058 if (!ISSET(tp
->t_oflag
, OPOST
))
2061 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2062 char_type
, CCLASSMASK
);
2064 * If ce is zero, then we're processing
2065 * a special character through ttyoutput.
2069 if (ttyoutput(*cp
, tp
) >= 0) {
2074 /* No Clists, wait a bit. */
2076 if (flag
& IO_NDELAY
) {
2077 error
= EWOULDBLOCK
;
2080 error
= ttysleep(tp
, &lbolt
,
2090 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2091 tp
->t_outq
.c_cc
> hiwat
)
2097 * A bunch of normal characters have been found.
2098 * Transfer them en masse to the output queue and
2099 * continue processing at the top of the loop.
2100 * If there are any further characters in this
2101 * <= OBUFSIZ chunk, the first should be a character
2102 * requiring special handling by ttyoutput.
2105 i
= b_to_q(cp
, ce
, &tp
->t_outq
);
2108 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2115 /* No Clists, wait a bit. */
2117 if (flag
& IO_NDELAY
) {
2118 error
= EWOULDBLOCK
;
2121 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
,
2128 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2129 tp
->t_outq
.c_cc
> hiwat
)
2136 * If cc is nonzero, we leave the uio structure inconsistent, as the
2137 * offset and iov pointers have moved forward, but it doesn't matter
2138 * (the call will either return short or restart with a new uio).
2140 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2141 thread_funnel_set(kernel_flock
, funnel_state
);
2148 * Since we are using ring buffers, if we can't insert any more into
2149 * the output queue, we can assume the ring is full and that someone
2150 * forgot to set the high water mark correctly. We set it and then
2151 * proceed as normal.
2153 hiwat
= tp
->t_outq
.c_cc
- 1;
2160 * This can only occur if FLUSHO is set in t_lflag,
2161 * or if ttstart/oproc is synchronous (or very fast).
2163 if (tp
->t_outq
.c_cc
<= hiwat
) {
2167 if (flag
& IO_NDELAY
) {
2169 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2170 thread_funnel_set(kernel_flock
, funnel_state
);
2171 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2173 SET(tp
->t_state
, TS_SO_OLOWAT
);
2174 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2177 if (error
== EWOULDBLOCK
)
2185 * Rubout one character from the rawq of tp
2186 * as cleanly as possible.
2191 register struct tty
*tp
;
2193 register u_char
*cp
;
2194 register int savecol
;
2197 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2199 CLR(tp
->t_lflag
, FLUSHO
);
2200 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2201 if (tp
->t_rocount
== 0) {
2203 * Messed up by ttwrite; retype
2208 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2211 CLR(c
, ~TTY_CHARMASK
);
2212 switch (CCLASS(c
)) {
2221 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2225 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2230 savecol
= tp
->t_column
;
2231 SET(tp
->t_state
, TS_CNTTB
);
2232 SET(tp
->t_lflag
, FLUSHO
);
2233 tp
->t_column
= tp
->t_rocol
;
2235 cp
= tp
->t_rawq
.c_cf
;
2237 tabc
= *cp
; /* XXX FIX NEXTC */
2238 for (; cp
; cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2241 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2242 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2245 CLR(tp
->t_lflag
, FLUSHO
);
2246 CLR(tp
->t_state
, TS_CNTTB
);
2249 /* savecol will now be length of the tab. */
2250 savecol
-= tp
->t_column
;
2251 tp
->t_column
+= savecol
;
2253 savecol
= 8; /* overflow fixup */
2254 while (--savecol
>= 0)
2255 (void)ttyoutput('\b', tp
);
2258 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2259 (void)printf(PANICSTR
, c
, CCLASS(c
));
2261 panic(PANICSTR
, c
, CCLASS(c
));
2265 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2266 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2267 SET(tp
->t_state
, TS_ERASE
);
2268 (void)ttyoutput('\\', tp
);
2272 ttyecho(tp
->t_cc
[VERASE
], tp
);
2277 * Back over count characters, erasing them.
2280 ttyrubo(struct tty
*tp
, int count
)
2283 while (count
-- > 0) {
2284 (void)ttyoutput('\b', tp
);
2285 (void)ttyoutput(' ', tp
);
2286 (void)ttyoutput('\b', tp
);
2292 * Reprint the rawq line. Note, it is assumed that c_cc has already
2297 register struct tty
*tp
;
2299 register u_char
*cp
;
2302 /* Echo the reprint character. */
2303 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2304 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2306 (void)ttyoutput('\n', tp
);
2310 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2311 * BIT OF FIRST CHAR.
2315 for (cp
= tp
->t_canq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2316 cp
!= NULL
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2318 for (cp
= tp
->t_rawq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2319 cp
!= NULL
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2322 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2324 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2327 CLR(tp
->t_state
, TS_ERASE
);
2330 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2335 * Echo a typed character to the terminal.
2340 register struct tty
*tp
;
2343 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2344 CLR(tp
->t_lflag
, FLUSHO
);
2345 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2346 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2347 ISSET(tp
->t_lflag
, EXTPROC
))
2349 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2350 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2351 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2352 (void)ttyoutput('^', tp
);
2353 CLR(c
, ~TTY_CHARMASK
);
2359 (void)ttyoutput(c
, tp
);
2363 * Wake up any readers on a tty.
2367 register struct tty
*tp
;
2371 if (tp
->t_rsel
.si_pid
!= 0)
2373 selwakeup(&tp
->t_rsel
);
2374 if (ISSET(tp
->t_state
, TS_ASYNC
))
2375 pgsignal(tp
->t_pgrp
, SIGIO
, 1);
2376 wakeup(TSA_HUP_OR_INPUT(tp
));
2380 * Wake up any writers on a tty.
2384 register struct tty
*tp
;
2387 if (tp
->t_wsel
.si_pid
!= 0 && tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2389 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2391 selwakeup(&tp
->t_wsel
);
2392 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2393 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2394 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2395 wakeup(TSA_OCOMPLETE(tp
));
2397 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2398 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2399 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2400 wakeup(TSA_OLOWAT(tp
));
2405 * Look up a code for a specified speed in a conversion table;
2406 * used by drivers to map software speed values to hardware parameters.
2409 ttspeedtab(speed
, table
)
2411 register struct speedtab
*table
;
2414 for ( ; table
->sp_speed
!= -1; table
++)
2415 if (table
->sp_speed
== speed
)
2416 return (table
->sp_code
);
2421 * Set tty hi and low water marks.
2423 * Try to arrange the dynamics so there's about one second
2424 * from hi to low water.
2428 ttsetwater(struct tty
*tp
)
2433 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2435 cps
= tp
->t_ospeed
/ 10;
2436 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2438 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2439 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2443 /* NeXT ttyinfo has been converted to the MACH kernel */
2444 #include <mach/thread_info.h>
2446 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2447 extern kern_return_t
thread_info_internal(thread_t thread
,
2448 thread_flavor_t flavor
,
2449 thread_info_t thread_info_out
,
2450 mach_msg_type_number_t
*thread_info_count
);
2453 * Report on state of foreground process group.
2456 ttyinfo(struct tty
*tp
)
2464 struct timeval utime
;
2465 struct timeval stime
;
2466 thread_basic_info_data_t basic_info
;
2467 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2469 if (ttycheckoutq(tp
,0) == 0)
2472 /* Print load average. */
2473 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2474 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2477 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2478 * that pending input will be retyped on BS.
2480 if (tp
->t_session
== NULL
) {
2481 ttyprintf(tp
, "not a controlling terminal\n");
2485 if (tp
->t_pgrp
== NULL
) {
2486 ttyprintf(tp
, "no foreground process group\n");
2490 /* first process in process group */
2491 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2492 ttyprintf(tp
, "empty foreground process group\n");
2498 * Pick the most interesting process and copy some of its
2499 * state for printing later.
2501 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2502 if (proc_compare(pick
, p
))
2506 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2507 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2508 (thread
= uthread
->uu_act
) == NULL
||
2509 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2510 ttyprintf(tp
, "foreground process without thread\n");
2515 switch(basic_info
.run_state
) {
2516 case TH_STATE_RUNNING
:
2519 case TH_STATE_STOPPED
:
2522 case TH_STATE_WAITING
:
2525 case TH_STATE_UNINTERRUPTIBLE
:
2526 state
= "uninterruptible";
2528 case TH_STATE_HALTED
:
2535 calcru(pick
, &utime
, &stime
, NULL
);
2537 /* Print command, pid, state, utime, and stime */
2538 ttyprintf(tp
, " cmd: %s %d %s %ld.%02ldu %ld.%02lds\n",
2542 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2543 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2548 * Returns 1 if p2 is "better" than p1
2550 * The algorithm for picking the "interesting" process is thus:
2552 * 1) Only foreground processes are eligible - implied.
2553 * 2) Runnable processes are favored over anything else. The runner
2554 * with the highest cpu utilization is picked (p_estcpu). Ties are
2555 * broken by picking the highest pid.
2556 * 3) The sleeper with the shortest sleep time is next.
2557 * 4) Further ties are broken by picking the highest pid.
2559 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2560 #define TESTAB(a, b) ((a)<<1 | (b))
2566 proc_compare(p1
, p2
)
2567 register struct proc
*p1
, *p2
;
2573 * see if at least one of them is runnable
2575 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2582 * tie - favor one with highest recent cpu utilization
2584 if (p2
->p_estcpu
> p1
->p_estcpu
)
2586 if (p1
->p_estcpu
> p2
->p_estcpu
)
2588 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2593 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2599 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2602 * pick the one with the smallest sleep time
2604 if (p2
->p_slptime
> p1
->p_slptime
)
2606 if (p1
->p_slptime
> p2
->p_slptime
)
2608 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2612 * Output char to tty; console putchar style.
2622 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2627 (void)ttyoutput('\r', tp
);
2628 (void)ttyoutput(c
, tp
);
2635 * Sleep on chan, returning ERESTART if tty changed while we napped and
2636 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2637 * the tty is revoked, restarting a pending call will redo validation done
2638 * at the start of the call.
2641 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
2647 error
= tsleep(chan
, pri
, wmesg
, timo
);
2650 return (tp
->t_gen
== gen
? 0 : ERESTART
);
2655 * Allocate a tty structure and its associated buffers.
2662 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
2664 /* XXX: default to TTYCLSIZE(1024) chars for now */
2665 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
2666 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
2667 /* output queue doesn't need quoting */
2668 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
2674 * Free a tty structure and its buffers.
2680 clfree(&tp
->t_rawq
);
2681 clfree(&tp
->t_canq
);
2682 clfree(&tp
->t_outq
);
2690 * XXX this is usable not useful or used. Most tty drivers have
2691 * ifdefs for using ttymalloc() but assume a different interface.
2694 * Allocate a tty struct. Clists in the struct will be allocated by
2702 MALLOC(tp
, struct tty
*, sizeof *tp
, M_TTYS
, M_WAITOK
|M_ZERO
);
2707 #if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */
2709 * Free a tty struct. Clists in the struct should have been freed by