2 * Copyright (c) 2000-2004 Apple Computer, 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@
28 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1990, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)tty.c 8.8 (Berkeley) 1/21/94
70 * o Fix races for sending the start char in ttyflush().
71 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
72 * With luck, there will be MIN chars before select() returns().
73 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
74 * o Don't allow input in TS_ZOMBIE case. It would be visible through
76 * o Do the new sio locking stuff here and use it to avoid special
79 * o Move EXTPROC and/or PENDIN to t_state?
80 * o Wrap most of ttioctl in spltty/splx.
81 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
82 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
83 * o Don't allow certain termios flags to affect disciplines other
84 * than TTYDISC. Cancel their effects before switch disciplines
85 * and ignore them if they are set while we are in another
87 * o Handle c_ispeed = 0 to c_ispeed = c_ospeed conversion here instead
88 * of in drivers and fix drivers that write to tp->t_termios.
89 * o Check for TS_CARR_ON being set while everything is closed and not
90 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
91 * so it would live until the next open even if carrier drops.
92 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
93 * only when _all_ openers leave open().
99 #include "opt_uconsole.h"
102 #include <sys/param.h>
103 #define TTYDEFCHARS 1
104 #include <sys/systm.h>
106 #include <sys/ioctl.h>
107 #include <sys/proc_internal.h>
108 #include <sys/kauth.h>
109 #include <sys/file_internal.h>
110 #include <sys/conf.h>
111 #include <sys/dkstat.h>
113 #include <sys/kernel.h>
114 #include <sys/vnode.h>
115 #include <sys/syslog.h>
116 #include <sys/user.h>
117 #include <sys/signalvar.h>
118 #include <sys/signalvar.h>
120 #include <sys/resourcevar.h>
122 #include <sys/malloc.h>
124 #include <sys/snoop.h>
129 #include <vm/vm_param.h>
130 #include <vm/vm_prot.h>
133 #include <vm/vm_map.h>
135 #include <dev/kmreg_com.h>
136 #include <machine/cons.h>
137 #include <machine/spl.h>
139 #include <machdep/machine/pmap.h>
142 #include <sys/resource.h> /* averunnable */
145 static int proc_compare(struct proc
*p1
, struct proc
*p2
);
147 static int ttnread(struct tty
*tp
);
148 static void ttyecho(int c
, struct tty
*tp
);
149 static int ttyoutput(int c
, register struct tty
*tp
);
150 static void ttypend(struct tty
*tp
);
151 static void ttyretype(struct tty
*tp
);
152 static void ttyrub(int c
, struct tty
*tp
);
153 static void ttyrubo(struct tty
*tp
, int count
);
154 static void ttystop(struct tty
*tp
, int rw
);
155 static void ttyunblock(struct tty
*tp
);
156 static int ttywflush(struct tty
*tp
);
157 static int proc_compare(struct proc
*p1
, struct proc
*p2
);
160 * Table with character classes and parity. The 8th bit indicates parity,
161 * the 7th bit indicates the character is an alphameric or underscore (for
162 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
163 * are 0 then the character needs no special processing on output; classes
164 * other than 0 might be translated or (not currently) require delays.
166 #define E 0x00 /* Even parity. */
167 #define O 0x80 /* Odd parity. */
168 #define PARITY(c) (char_type[c] & O)
170 #define ALPHA 0x40 /* Alpha or underscore. */
171 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
173 #define CCLASSMASK 0x3f
174 #define CCLASS(c) (char_type[c] & CCLASSMASK)
179 #define NA ORDINARY | ALPHA
185 static u_char
const char_type
[] = {
186 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* nul - bel */
187 O
|BS
, E
|TB
, E
|NL
, O
|CC
, E
|VT
, O
|CR
, O
|CC
, E
|CC
, /* bs - si */
188 O
|CC
, E
|CC
, E
|CC
, O
|CC
, E
|CC
, O
|CC
, O
|CC
, E
|CC
, /* dle - etb */
189 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* can - us */
190 O
|NO
, E
|NO
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* sp - ' */
191 E
|NO
, O
|NO
, O
|NO
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|NO
, /* ( - / */
192 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* 0 - 7 */
193 O
|NA
, E
|NA
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* 8 - ? */
194 O
|NO
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* @ - G */
195 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* H - O */
196 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* P - W */
197 O
|NA
, E
|NA
, E
|NA
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, O
|NA
, /* X - _ */
198 E
|NO
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* ` - g */
199 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* h - o */
200 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* p - w */
201 E
|NA
, O
|NA
, O
|NA
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|CC
, /* x - del */
203 * Meta chars; should be settable per character set;
204 * for now, treat them all as normal characters.
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
,
216 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
217 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
218 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
219 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
220 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
221 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
232 /* Macros to clear/set/test flags. */
233 #define SET(t, f) (t) |= (f)
234 #define CLR(t, f) (t) &= ~(f)
235 #define ISSET(t, f) ((t) & (f))
238 * Input control starts when we would not be able to fit the maximum
239 * contents of the ping-pong buffers and finishes when we would be able
240 * to fit that much plus 1/8 more.
242 #define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */
243 #define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */
245 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
246 #define MAX_INPUT TTYHOG
249 termios32to64(struct termios
*in
, struct user_termios
*out
)
251 out
->c_iflag
= (user_tcflag_t
)in
->c_iflag
;
252 out
->c_oflag
= (user_tcflag_t
)in
->c_oflag
;
253 out
->c_cflag
= (user_tcflag_t
)in
->c_cflag
;
254 out
->c_lflag
= (user_tcflag_t
)in
->c_lflag
;
256 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
257 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
259 out
->c_ispeed
= (user_speed_t
)in
->c_ispeed
;
260 out
->c_ospeed
= (user_speed_t
)in
->c_ospeed
;
264 termios64to32(struct user_termios
*in
, struct termios
*out
)
266 out
->c_iflag
= (tcflag_t
)in
->c_iflag
;
267 out
->c_oflag
= (tcflag_t
)in
->c_oflag
;
268 out
->c_cflag
= (tcflag_t
)in
->c_cflag
;
269 out
->c_lflag
= (tcflag_t
)in
->c_lflag
;
271 /* bcopy is OK, since this type is ILP32/LP64 size invariant */
272 bcopy(in
->c_cc
, out
->c_cc
, sizeof(in
->c_cc
));
274 out
->c_ispeed
= (speed_t
)in
->c_ispeed
;
275 out
->c_ospeed
= (speed_t
)in
->c_ospeed
;
280 * Initial open of tty, or (re)entry to standard tty line discipline.
285 register struct tty
*tp
;
288 boolean_t funnel_state
;
290 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
293 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
294 SET(tp
->t_state
, TS_ISOPEN
);
295 if (ISSET(tp
->t_cflag
, CLOCAL
)) {
296 SET(tp
->t_state
, TS_CONNECTED
); }
297 bzero(&tp
->t_winsize
, sizeof(tp
->t_winsize
));
302 * Initialize or restore a cblock allocation policy suitable for
303 * the standard line discipline.
305 clist_alloc_cblocks(&tp
->t_canq
, TTYHOG
, 512);
306 clist_alloc_cblocks(&tp
->t_outq
, TTMAXHIWAT
+ OBUFSIZ
+ 100,
307 TTMAXHIWAT
+ OBUFSIZ
+ 100);
308 clist_alloc_cblocks(&tp
->t_rawq
, TTYHOG
, TTYHOG
);
312 thread_funnel_set(kernel_flock
, funnel_state
);
317 * Handle close() on a tty line: flush and set to initial state,
318 * bumping generation number so that pending read/write calls
319 * can detect recycling of the tty.
320 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
321 * and l_close() should have flushed, but we repeat the spltty() and
322 * the flush in case there are buggy callers.
326 register struct tty
*tp
;
339 * Closing current console tty; disable printing of console
340 * messages at bottom-level driver.
342 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
343 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, current_proc());
347 ttyflush(tp
, FREAD
| FWRITE
);
349 clist_free_cblocks(&tp
->t_canq
);
350 clist_free_cblocks(&tp
->t_outq
);
351 clist_free_cblocks(&tp
->t_rawq
);
355 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
356 snpdown((struct snoop
*)tp
->t_sc
);
360 tp
->t_line
= TTYDISC
;
362 tp
->t_session
= NULL
;
365 selthreadclear(&tp
->t_wsel
);
366 selthreadclear(&tp
->t_rsel
);
372 #define FLUSHQ(q) { \
374 ndflush(q, (q)->c_cc); \
377 /* Is 'c' a line delimiter ("break" character)? */
378 #define TTBREAKC(c, lflag) \
379 ((c) == '\n' || (((c) == cc[VEOF] || \
380 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
381 (c) != _POSIX_VDISABLE))
384 * Process input of a single character received on a tty.
389 register struct tty
*tp
;
391 register tcflag_t iflag
, lflag
;
394 boolean_t funnel_state
;
396 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
399 * If input is pending take it first.
402 if (ISSET(lflag
, PENDIN
))
407 if (ISSET(lflag
, ICANON
)) {
417 * Block further input iff:
418 * current input > threshold AND input is available to user program
419 * AND input flow control is enabled and not yet invoked.
420 * The 3 is slop for PARMRK.
423 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
> I_HIGH_WATER
- 3 &&
424 (!ISSET(lflag
, ICANON
) || tp
->t_canq
.c_cc
!= 0) &&
425 (ISSET(tp
->t_cflag
, CRTS_IFLOW
) || ISSET(iflag
, IXOFF
)) &&
426 !ISSET(tp
->t_state
, TS_TBLOCK
))
429 /* Handle exceptional conditions (break, parity, framing). */
431 err
= (ISSET(c
, TTY_ERRORMASK
));
433 CLR(c
, TTY_ERRORMASK
);
434 if (ISSET(err
, TTY_BI
)) {
435 if (ISSET(iflag
, IGNBRK
)) {
436 thread_funnel_set(kernel_flock
, funnel_state
);
439 if (ISSET(iflag
, BRKINT
)) {
440 ttyflush(tp
, FREAD
| FWRITE
);
441 pgsignal(tp
->t_pgrp
, SIGINT
, 1);
444 if (ISSET(iflag
, PARMRK
))
446 } else if ((ISSET(err
, TTY_PE
) && ISSET(iflag
, INPCK
))
447 || ISSET(err
, TTY_FE
)) {
448 if (ISSET(iflag
, IGNPAR
)) {
449 thread_funnel_set(kernel_flock
, funnel_state
);
452 else if (ISSET(iflag
, PARMRK
)) {
454 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>
457 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
458 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
459 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
466 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
468 if (!ISSET(lflag
, EXTPROC
)) {
470 * Check for literal nexting very first
472 if (ISSET(tp
->t_state
, TS_LNCH
)) {
474 CLR(tp
->t_state
, TS_LNCH
);
477 * Scan for special characters. This code
478 * is really just a big case statement with
479 * non-constant cases. The bottom of the
480 * case statement is labeled ``endcase'', so goto
481 * it after a case match, or similar.
485 * Control chars which aren't controlled
486 * by ICANON, ISIG, or IXON.
488 if (ISSET(lflag
, IEXTEN
)) {
489 if (CCEQ(cc
[VLNEXT
], c
)) {
490 if (ISSET(lflag
, ECHO
)) {
491 if (ISSET(lflag
, ECHOE
)) {
492 (void)ttyoutput('^', tp
);
493 (void)ttyoutput('\b', tp
);
497 SET(tp
->t_state
, TS_LNCH
);
500 if (CCEQ(cc
[VDISCARD
], c
)) {
501 if (ISSET(lflag
, FLUSHO
))
502 CLR(tp
->t_lflag
, FLUSHO
);
504 ttyflush(tp
, FWRITE
);
506 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
508 SET(tp
->t_lflag
, FLUSHO
);
516 if (ISSET(lflag
, ISIG
)) {
517 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
518 if (!ISSET(lflag
, NOFLSH
))
519 ttyflush(tp
, FREAD
| FWRITE
);
522 CCEQ(cc
[VINTR
], c
) ? SIGINT
: SIGQUIT
, 1);
525 if (CCEQ(cc
[VSUSP
], c
)) {
526 if (!ISSET(lflag
, NOFLSH
))
529 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
534 * Handle start/stop characters.
536 if (ISSET(iflag
, IXON
)) {
537 if (CCEQ(cc
[VSTOP
], c
)) {
538 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
539 SET(tp
->t_state
, TS_TTSTOP
);
541 thread_funnel_set(kernel_flock
, funnel_state
);
544 if (!CCEQ(cc
[VSTART
], c
)) {
545 thread_funnel_set(kernel_flock
, funnel_state
);
549 * if VSTART == VSTOP then toggle
553 if (CCEQ(cc
[VSTART
], c
))
557 * IGNCR, ICRNL, & INLCR
560 if (ISSET(iflag
, IGNCR
)) {
561 thread_funnel_set(kernel_flock
, funnel_state
);
564 else if (ISSET(iflag
, ICRNL
))
566 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
569 if (!ISSET(tp
->t_lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
571 * From here on down canonical mode character
572 * processing takes place.
577 if (CCEQ(cc
[VERASE
], c
)) {
579 ttyrub(unputc(&tp
->t_rawq
), tp
);
585 if (CCEQ(cc
[VKILL
], c
)) {
586 if (ISSET(lflag
, ECHOKE
) &&
587 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
588 !ISSET(lflag
, ECHOPRT
))
589 while (tp
->t_rawq
.c_cc
)
590 ttyrub(unputc(&tp
->t_rawq
), tp
);
593 if (ISSET(lflag
, ECHOK
) ||
594 ISSET(lflag
, ECHOKE
))
599 CLR(tp
->t_state
, TS_LOCAL
);
605 if (CCEQ(cc
[VWERASE
], c
) && ISSET(lflag
, IEXTEN
)) {
611 while ((c
= unputc(&tp
->t_rawq
)) == ' ' || c
== '\t')
616 * erase last char of word and remember the
617 * next chars type (for ALTWERASE)
620 c
= unputc(&tp
->t_rawq
);
623 if (c
== ' ' || c
== '\t') {
624 (void)putc(c
, &tp
->t_rawq
);
633 c
= unputc(&tp
->t_rawq
);
636 } while (c
!= ' ' && c
!= '\t' &&
637 (!ISSET(lflag
, ALTWERASE
) || ISALPHA(c
) == ctype
));
638 (void)putc(c
, &tp
->t_rawq
);
644 if (CCEQ(cc
[VREPRINT
], c
) && ISSET(lflag
, IEXTEN
)) {
649 * ^T - kernel info and generate SIGINFO
651 if (CCEQ(cc
[VSTATUS
], c
) && ISSET(lflag
, IEXTEN
)) {
652 if (ISSET(lflag
, ISIG
))
653 pgsignal(tp
->t_pgrp
, SIGINFO
, 1);
654 if (!ISSET(lflag
, NOKERNINFO
))
660 * Check for input buffer overflow
662 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= MAX_INPUT
) {
664 if (ISSET(iflag
, IMAXBEL
)) {
665 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
666 (void)ttyoutput(CTRL('g'), tp
);
671 if ( c
== 0377 && ISSET(iflag
, PARMRK
) && !ISSET(iflag
, ISTRIP
)
672 && ISSET(iflag
, IGNBRK
|IGNPAR
) != (IGNBRK
|IGNPAR
))
673 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
676 * Put data char in q for user and
677 * wakeup on seeing a line delimiter.
679 if (putc(c
, &tp
->t_rawq
) >= 0) {
680 if (!ISSET(lflag
, ICANON
)) {
685 if (TTBREAKC(c
, lflag
)) {
687 catq(&tp
->t_rawq
, &tp
->t_canq
);
689 } else if (tp
->t_rocount
++ == 0)
690 tp
->t_rocol
= tp
->t_column
;
691 if (ISSET(tp
->t_state
, TS_ERASE
)) {
693 * end of prterase \.../
695 CLR(tp
->t_state
, TS_ERASE
);
696 (void)ttyoutput('/', tp
);
700 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
702 * Place the cursor over the '^' of the ^D.
704 i
= min(2, tp
->t_column
- i
);
706 (void)ttyoutput('\b', tp
);
713 * IXANY means allow any character to restart output.
715 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
716 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
717 thread_funnel_set(kernel_flock
, funnel_state
);
721 CLR(tp
->t_lflag
, FLUSHO
);
722 CLR(tp
->t_state
, TS_TTSTOP
);
724 retval
= ttstart(tp
);
725 thread_funnel_set(kernel_flock
, funnel_state
);
730 * Output a single character on a tty, doing output processing
731 * as needed (expanding tabs, newline processing, etc.).
732 * Returns < 0 if succeeds, otherwise returns char to resend.
738 register struct tty
*tp
;
740 register tcflag_t oflag
;
744 if (!ISSET(oflag
, OPOST
)) {
745 if (ISSET(tp
->t_lflag
, FLUSHO
))
747 if (putc(c
, &tp
->t_outq
))
754 * Do tab expansion if OXTABS is set. Special case if we external
755 * processing, we don't do the tab expansion because we'll probably
756 * get it wrong. If tab expansion needs to be done, let it happen
759 CLR(c
, ~TTY_CHARMASK
);
761 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
762 c
= 8 - (tp
->t_column
& 7);
763 if (!ISSET(tp
->t_lflag
, FLUSHO
)) {
764 s
= spltty(); /* Don't interrupt tabs. */
765 c
-= b_to_q(" ", c
, &tp
->t_outq
);
771 return (c
? -1 : '\t');
773 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
777 * Newline translation: if ONLCR is set,
778 * translate newline into "\r\n".
780 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
783 if (putc('\r', &tp
->t_outq
))
788 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
807 col
= (col
+ 8) & ~7;
815 * Ioctls for all tty devices. Called after line-discipline specific ioctl
816 * has been called to do discipline-specific functions and/or reject any
817 * of these ioctl commands.
821 ttioctl(register struct tty
*tp
,
822 u_long cmd
, caddr_t data
, int flag
,
828 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
829 /* If the ioctl involves modification, hang if in the background. */
845 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
855 while (isbackground(p
, tp
) &&
856 (p
->p_flag
& P_PPWAIT
) == 0 &&
857 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
858 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
859 if (p
->p_pgrp
->pg_jobc
== 0)
861 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
862 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
| PTTYBLOCK
, "ttybg1",
870 switch (cmd
) { /* Process the ioctl. */
871 case FIOASYNC
: /* set/clear async i/o */
874 SET(tp
->t_state
, TS_ASYNC
);
876 CLR(tp
->t_state
, TS_ASYNC
);
879 case FIONBIO
: /* set/clear non-blocking i/o */
880 break; /* XXX: delete. */
881 case FIONREAD
: /* get # bytes to read */
883 *(int *)data
= ttnread(tp
);
886 case TIOCEXCL
: /* set exclusive use of tty */
888 SET(tp
->t_state
, TS_XCLUDE
);
891 case TIOCFLUSH
: { /* flush buffers */
892 register int flags
= *(int *)data
;
895 flags
= FREAD
| FWRITE
;
897 flags
&= FREAD
| FWRITE
;
903 /* Set current console device to this line */
905 data
= (caddr_t
) &bogusData
;
907 /* No break - Fall through to BSD code */
910 case TIOCCONS
: { /* become virtual console */
912 if (constty
&& constty
!= tp
&&
913 ISSET(constty
->t_state
, TS_CONNECTED
)) {
916 #if defined(NeXT) || !defined(UCONSOLE)
917 if ( (error
= suser(kauth_cred_get(), &p
->p_acflag
)) )
921 } else if (tp
== constty
) {
926 (*cdevsw
[major(cons
.t_dev
)].d_ioctl
)
927 (cons
.t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
929 (*cdevsw
[major(tp
->t_dev
)].d_ioctl
)
930 (tp
->t_dev
, KMIOCDISABLCONS
, NULL
, 0, p
);
935 case TIOCDRAIN
: /* wait till output drained */
940 case TIOCGETA
: /* get termios struct */
941 case TIOCGETA_64
: { /* get termios struct */
942 if (IS_64BIT_PROCESS(p
)) {
943 termios32to64(&tp
->t_termios
, (struct user_termios
*)data
);
945 bcopy(&tp
->t_termios
, data
, sizeof(struct termios
));
949 case TIOCGETD
: /* get line discipline */
950 *(int *)data
= tp
->t_line
;
952 case TIOCGWINSZ
: /* get window size */
953 *(struct winsize
*)data
= tp
->t_winsize
;
955 case TIOCGPGRP
: /* get pgrp of tty */
958 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PID
;
961 case TIOCHPCL
: /* hang up on last close */
963 SET(tp
->t_cflag
, HUPCL
);
967 case TIOCNXCL
: /* reset exclusive use of tty */
969 CLR(tp
->t_state
, TS_XCLUDE
);
972 case TIOCOUTQ
: /* output queue size */
973 *(int *)data
= tp
->t_outq
.c_cc
;
975 case TIOCSETA
: /* set termios struct */
977 case TIOCSETAW
: /* drain output, set */
979 case TIOCSETAF
: /* drn out, fls in, set */
980 case TIOCSETAF_64
: { /* drn out, fls in, set */
981 register struct termios
*t
= (struct termios
*)data
;
982 struct termios lcl_termios
;
984 if (IS_64BIT_PROCESS(p
)) {
985 termios64to32((struct user_termios
*)data
, &lcl_termios
);
988 if (t
->c_ispeed
< 0 || t
->c_ospeed
< 0)
991 if (cmd
== TIOCSETAW
|| cmd
== TIOCSETAF
||
992 cmd
== TIOCSETAW_64
|| cmd
== TIOCSETAF_64
) {
998 if (cmd
== TIOCSETAF
|| cmd
== TIOCSETAF_64
)
1001 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1003 * Set device hardware.
1005 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1009 if (ISSET(t
->c_cflag
, CLOCAL
) &&
1010 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1012 * XXX disconnections would be too hard to
1013 * get rid of without this kludge. The only
1014 * way to get rid of controlling terminals
1015 * is to exit from the session leader.
1017 CLR(tp
->t_state
, TS_ZOMBIE
);
1019 wakeup(TSA_CARR_ON(tp
));
1023 if ((ISSET(tp
->t_state
, TS_CARR_ON
) ||
1024 ISSET(t
->c_cflag
, CLOCAL
)) &&
1025 !ISSET(tp
->t_state
, TS_ZOMBIE
))
1026 SET(tp
->t_state
, TS_CONNECTED
);
1028 CLR(tp
->t_state
, TS_CONNECTED
);
1029 tp
->t_cflag
= t
->c_cflag
;
1030 tp
->t_ispeed
= t
->c_ispeed
;
1031 tp
->t_ospeed
= t
->c_ospeed
;
1034 if (ISSET(t
->c_lflag
, ICANON
) != ISSET(tp
->t_lflag
, ICANON
) &&
1035 cmd
!= TIOCSETAF
&& cmd
!= TIOCSETAF_64
) {
1036 if (ISSET(t
->c_lflag
, ICANON
))
1037 SET(tp
->t_lflag
, PENDIN
);
1040 * XXX we really shouldn't allow toggling
1041 * ICANON while we're in a non-termios line
1042 * discipline. Now we have to worry about
1043 * panicing for a null queue.
1046 if (tp
->t_canq
.c_cbreserved
> 0 &&
1047 tp
->t_rawq
.c_cbreserved
> 0) {
1048 catq(&tp
->t_rawq
, &tp
->t_canq
);
1050 * XXX the queue limits may be
1051 * different, so the old queue
1052 * swapping method no longer works.
1054 catq(&tp
->t_canq
, &tp
->t_rawq
);
1057 if (tp
->t_rawq
.c_cs
&& tp
->t_canq
.c_cs
) {
1060 catq(&tp
->t_rawq
, &tp
->t_canq
);
1062 tp
->t_rawq
= tp
->t_canq
;
1066 CLR(tp
->t_lflag
, PENDIN
);
1070 tp
->t_iflag
= t
->c_iflag
;
1071 tp
->t_oflag
= t
->c_oflag
;
1073 * Make the EXTPROC bit read only.
1075 if (ISSET(tp
->t_lflag
, EXTPROC
))
1076 SET(t
->c_lflag
, EXTPROC
);
1078 CLR(t
->c_lflag
, EXTPROC
);
1079 tp
->t_lflag
= t
->c_lflag
| ISSET(tp
->t_lflag
, PENDIN
);
1080 if (t
->c_cc
[VMIN
] != tp
->t_cc
[VMIN
] ||
1081 t
->c_cc
[VTIME
] != tp
->t_cc
[VTIME
])
1083 bcopy(t
->c_cc
, tp
->t_cc
, sizeof(t
->c_cc
));
1087 case TIOCSETD
: { /* set line discipline */
1088 register int t
= *(int *)data
;
1089 dev_t device
= tp
->t_dev
;
1093 if (t
!= tp
->t_line
) {
1095 (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
1096 error
= (*linesw
[t
].l_open
)(device
, tp
);
1098 (void)(*linesw
[tp
->t_line
].l_open
)(device
, tp
);
1107 case TIOCSTART
: /* start output, like ^Q */
1109 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1110 ISSET(tp
->t_lflag
, FLUSHO
)) {
1111 CLR(tp
->t_lflag
, FLUSHO
);
1112 CLR(tp
->t_state
, TS_TTSTOP
);
1117 case TIOCSTI
: /* simulate terminal input */
1118 if (suser(kauth_cred_get(), NULL
) && (flag
& FREAD
) == 0)
1120 if (suser(kauth_cred_get(), NULL
) && !isctty(p
, tp
))
1123 (*linesw
[tp
->t_line
].l_rint
)(*(u_char
*)data
, tp
);
1126 case TIOCSTOP
: /* stop output, like ^S */
1128 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1129 SET(tp
->t_state
, TS_TTSTOP
);
1134 case TIOCSCTTY
: /* become controlling tty */
1135 /* Session ctty vnode pointer set in vnode layer. */
1136 if (!SESS_LEADER(p
) ||
1137 ((p
->p_session
->s_ttyvp
|| tp
->t_session
) &&
1138 (tp
->t_session
!= p
->p_session
)))
1140 tp
->t_session
= p
->p_session
;
1141 tp
->t_pgrp
= p
->p_pgrp
;
1142 p
->p_session
->s_ttyp
= tp
;
1143 p
->p_flag
|= P_CONTROLT
;
1144 /* The backgrounded process blocking on tty now
1145 * could be foregound process. Wake such processes
1147 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1149 case TIOCSPGRP
: { /* set pgrp of tty */
1150 register struct pgrp
*pgrp
= pgfind(*(int *)data
);
1154 else if (pgrp
== NULL
|| pgrp
->pg_session
!= p
->p_session
)
1157 /* The backgrounded process blocking on tty now
1158 * could be foregound process. Wake such processes
1160 tty_pgsignal(tp
->t_pgrp
, SIGCONT
);
1163 case TIOCSTAT
: /* simulate control-T */
1168 case TIOCSWINSZ
: /* set window size */
1169 if (bcmp((caddr_t
)&tp
->t_winsize
, data
,
1170 sizeof (struct winsize
))) {
1171 tp
->t_winsize
= *(struct winsize
*)data
;
1172 pgsignal(tp
->t_pgrp
, SIGWINCH
, 1);
1175 case TIOCSDRAINWAIT
:
1176 error
= suser(kauth_cred_get(), &p
->p_acflag
);
1179 tp
->t_timeout
= *(int *)data
* hz
;
1180 wakeup(TSA_OCOMPLETE(tp
));
1181 wakeup(TSA_OLOWAT(tp
));
1183 case TIOCGDRAINWAIT
:
1184 *(int *)data
= tp
->t_timeout
/ hz
;
1187 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
1189 return (ttcompat(tp
, cmd
, data
, flag
, p
));
1191 return (ttcompat(tp
, cmd
, data
, flag
));
1202 ttyselect(tp
, rw
, wql
, p
)
1216 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
))
1218 selrecord(p
, &tp
->t_rsel
, wql
);
1221 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1222 ISSET(tp
->t_state
, TS_CONNECTED
))
1223 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1227 selrecord(p
, &tp
->t_wsel
, wql
);
1235 * This is a wrapper for compatibility with the select vector used by
1236 * cdevsw. It relies on a proper xxxdevtotty routine.
1239 ttselect(dev
, rw
, wql
, p
)
1246 return ttyselect((*cdevsw
[major(dev
)]->d_devtotty
)(dev
), rw
, wql
, p
);
1248 return ttyselect(cdevsw
[major(dev
)].d_ttys
[minor(dev
)], rw
, wql
, p
);
1253 * Must be called at spltty().
1261 if (ISSET(tp
->t_lflag
, PENDIN
))
1263 nread
= tp
->t_canq
.c_cc
;
1264 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1265 nread
+= tp
->t_rawq
.c_cc
;
1266 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1273 * Wait for output to drain.
1277 register struct tty
*tp
;
1283 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1284 ISSET(tp
->t_state
, TS_CONNECTED
) && tp
->t_oproc
) {
1286 if ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1287 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1288 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1289 error
= ttysleep(tp
, TSA_OCOMPLETE(tp
),
1290 TTOPRI
| PCATCH
, "ttywai",
1293 if (error
== EWOULDBLOCK
)
1300 if (!error
&& (tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)))
1311 #ifdef sun4c /* XXX */
1312 (*tp
->t_stop
)(tp
, rw
);
1314 (*cdevsw
[major(tp
->t_dev
)].d_stop
)(tp
, rw
);
1316 (*cdevsw
[major(tp
->t_dev
)]->d_stop
)(tp
, rw
);
1321 * Flush if successfully wait.
1329 if ((error
= ttywait(tp
)) == 0)
1330 ttyflush(tp
, FREAD
);
1335 * Flush tty read and/or write queues, notifying anyone waiting.
1339 register struct tty
*tp
;
1349 FLUSHQ(&tp
->t_outq
);
1350 CLR(tp
->t_state
, TS_TTSTOP
);
1354 FLUSHQ(&tp
->t_canq
);
1355 FLUSHQ(&tp
->t_rawq
);
1356 CLR(tp
->t_lflag
, PENDIN
);
1359 CLR(tp
->t_state
, TS_LOCAL
);
1361 if (ISSET(tp
->t_state
, TS_TBLOCK
)) {
1363 FLUSHQ(&tp
->t_outq
);
1367 * Don't let leave any state that might clobber the
1368 * next line discipline (although we should do more
1369 * to send the START char). Not clearing the state
1370 * may have caused the "putc to a clist with no
1371 * reserved cblocks" panic/printf.
1373 CLR(tp
->t_state
, TS_TBLOCK
);
1375 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1376 if (ISSET(tp
->t_iflag
, IXOFF
)) {
1378 * XXX wait a bit in the hope that the stop
1379 * character (if any) will go out. Waiting
1380 * isn't good since it allows races. This
1381 * will be fixed when the stop character is
1382 * put in a special queue. Don't bother with
1383 * the checks in ttywait() since the timeout
1386 SET(tp
->t_state
, TS_SO_OCOMPLETE
);
1387 ttysleep(tp
, TSA_OCOMPLETE(tp
), TTOPRI
,
1390 * Don't try sending the stop character again.
1392 CLR(tp
->t_state
, TS_TBLOCK
);
1399 FLUSHQ(&tp
->t_outq
);
1406 * Copy in the default termios characters.
1413 bcopy(ttydefchars
, t
->c_cc
, sizeof t
->c_cc
);
1424 termioschars(&tp
->t_termios
);
1428 * Handle input high water. Send stop character for the IXOFF case. Turn
1429 * on our input flow control bit and propagate the changes to the driver.
1430 * XXX the stop character should be put in a special high priority queue.
1437 SET(tp
->t_state
, TS_TBLOCK
);
1438 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1439 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) != 0)
1440 CLR(tp
->t_state
, TS_TBLOCK
); /* try again later */
1445 * Handle input low water. Send start character for the IXOFF case. Turn
1446 * off our input flow control bit and propagate the changes to the driver.
1447 * XXX the start character should be put in a special high priority queue.
1454 CLR(tp
->t_state
, TS_TBLOCK
);
1455 if (ISSET(tp
->t_iflag
, IXOFF
) && tp
->t_cc
[VSTART
] != _POSIX_VDISABLE
&&
1456 putc(tp
->t_cc
[VSTART
], &tp
->t_outq
) != 0)
1457 SET(tp
->t_state
, TS_TBLOCK
); /* try again later */
1461 #if defined(NeXT) || defined(notyet)
1462 /* FreeBSD: Not used by any current (i386) drivers. */
1464 * Restart after an inter-char delay.
1480 CLR(tp
->t_state
, TS_TIMEOUT
);
1485 #endif /* NeXT || notyet */
1491 boolean_t funnel_state
;
1493 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1495 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1497 thread_funnel_set(kernel_flock
, funnel_state
);
1502 * "close" a line discipline
1509 boolean_t funnel_state
;
1511 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1512 if ( (flag
& FNONBLOCK
) || ttywflush(tp
))
1513 ttyflush(tp
, FREAD
| FWRITE
);
1514 thread_funnel_set(kernel_flock
, funnel_state
);
1519 * Handle modem control transition on a tty.
1520 * Flag indicates new state of carrier.
1521 * Returns 0 if the line should be turned off, otherwise 1.
1525 register struct tty
*tp
;
1528 boolean_t funnel_state
;
1530 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1532 if (ISSET(tp
->t_state
, TS_CARR_ON
) && ISSET(tp
->t_cflag
, MDMBUF
)) {
1534 * MDMBUF: do flow control according to carrier flag
1535 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1536 * works if IXON and IXANY are clear.
1539 CLR(tp
->t_state
, TS_CAR_OFLOW
);
1540 CLR(tp
->t_state
, TS_TTSTOP
);
1542 } else if (!ISSET(tp
->t_state
, TS_CAR_OFLOW
)) {
1543 SET(tp
->t_state
, TS_CAR_OFLOW
);
1544 SET(tp
->t_state
, TS_TTSTOP
);
1547 } else if (flag
== 0) {
1551 CLR(tp
->t_state
, TS_CARR_ON
);
1552 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
1553 !ISSET(tp
->t_cflag
, CLOCAL
)) {
1554 SET(tp
->t_state
, TS_ZOMBIE
);
1555 CLR(tp
->t_state
, TS_CONNECTED
);
1556 if (tp
->t_session
&& tp
->t_session
->s_leader
)
1557 psignal(tp
->t_session
->s_leader
, SIGHUP
);
1558 ttyflush(tp
, FREAD
| FWRITE
);
1559 thread_funnel_set(kernel_flock
, funnel_state
);
1566 SET(tp
->t_state
, TS_CARR_ON
);
1567 if (!ISSET(tp
->t_state
, TS_ZOMBIE
))
1568 SET(tp
->t_state
, TS_CONNECTED
);
1569 wakeup(TSA_CARR_ON(tp
));
1573 thread_funnel_set(kernel_flock
, funnel_state
);
1578 * Reinput pending characters after state switch
1583 register struct tty
*tp
;
1588 CLR(tp
->t_lflag
, PENDIN
);
1589 SET(tp
->t_state
, TS_TYPEN
);
1592 * XXX this assumes too much about clist internals. It may even
1593 * fail if the cblock slush pool is empty. We can't allocate more
1594 * cblocks here because we are called from an interrupt handler
1595 * and clist_alloc_cblocks() can wait.
1598 bzero(&tp
->t_rawq
, sizeof tp
->t_rawq
);
1599 tp
->t_rawq
.c_cbmax
= tq
.c_cbmax
;
1600 tp
->t_rawq
.c_cbreserved
= tq
.c_cbreserved
;
1603 tp
->t_rawq
.c_cc
= 0;
1604 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
1606 while ((c
= getc(&tq
)) >= 0)
1608 CLR(tp
->t_state
, TS_TYPEN
);
1612 * Process a read call on a tty device.
1615 ttread(tp
, uio
, flag
)
1616 register struct tty
*tp
;
1620 register struct clist
*qp
;
1622 register tcflag_t lflag
;
1623 register cc_t
*cc
= tp
->t_cc
;
1624 register struct proc
*p
= current_proc();
1625 int s
, first
, error
= 0;
1626 int has_etime
= 0, last_cc
= 0;
1627 long slp
= 0; /* XXX this should be renamed `timo'. */
1628 boolean_t funnel_state
;
1631 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1633 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1637 lflag
= tp
->t_lflag
;
1639 * take pending input first
1641 if (ISSET(lflag
, PENDIN
)) {
1643 splx(s
); /* reduce latency */
1645 lflag
= tp
->t_lflag
; /* XXX ttypend() clobbers it */
1649 * Hang process if it's in the background.
1651 if (isbackground(p
, tp
)) {
1653 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
1654 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
1655 p
->p_flag
& P_PPWAIT
|| p
->p_pgrp
->pg_jobc
== 0) {
1656 thread_funnel_set(kernel_flock
, funnel_state
);
1659 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
1660 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg2", 0);
1662 thread_funnel_set(kernel_flock
, funnel_state
);
1668 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1670 thread_funnel_set(kernel_flock
, funnel_state
);
1671 return (0); /* EOF */
1675 * If canonical, use the canonical queue,
1676 * else use the raw queue.
1678 * (should get rid of clists...)
1680 qp
= ISSET(lflag
, ICANON
) ? &tp
->t_canq
: &tp
->t_rawq
;
1682 if (flag
& IO_NDELAY
) {
1685 if (!ISSET(lflag
, ICANON
) && cc
[VMIN
] == 0) {
1687 thread_funnel_set(kernel_flock
, funnel_state
);
1691 thread_funnel_set(kernel_flock
, funnel_state
);
1692 return (EWOULDBLOCK
);
1694 if (!ISSET(lflag
, ICANON
)) {
1697 struct timeval etime
, timecopy
;
1700 * Check each of the four combinations.
1701 * (m > 0 && t == 0) is the normal read case.
1702 * It should be fairly efficient, so we check that and its
1703 * companion case (m == 0 && t == 0) first.
1704 * For the other two cases, we compute the target sleep time
1713 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1715 thread_funnel_set(kernel_flock
, funnel_state
);
1718 t
*= 100000; /* time in us */
1719 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1720 ((t1).tv_usec - (t2).tv_usec))
1726 microuptime(&timecopy
);
1728 /* first character, start timer */
1731 etime
.tv_sec
= t
/ 1000000;
1732 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1733 timeradd(&etime
, &timecopy
, &etime
);
1736 } else if (qp
->c_cc
> last_cc
) {
1737 /* got a character, restart timer */
1739 etime
.tv_sec
= t
/ 1000000;
1740 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1741 timeradd(&etime
, &timecopy
, &etime
);
1745 /* nothing, check expiration */
1746 if (timercmp(&etime
, &timecopy
, <=))
1749 slp
= diff(etime
, timecopy
);
1752 } else { /* m == 0 */
1755 microuptime(&timecopy
);
1759 etime
.tv_sec
= t
/ 1000000;
1760 etime
.tv_usec
= (t
- (etime
.tv_sec
* 1000000));
1761 timeradd(&etime
, &timecopy
, &etime
);
1765 if (timercmp(&etime
, &timecopy
, <=)) {
1766 /* Timed out, but 0 is enough input. */
1768 thread_funnel_set(kernel_flock
, funnel_state
);
1771 slp
= diff(etime
, timecopy
);
1776 * Rounding down may make us wake up just short
1777 * of the target, so we round up.
1778 * The formula is ceiling(slp * hz/1000000).
1779 * 32-bit arithmetic is enough for hz < 169.
1780 * XXX see hzto() for how to avoid overflow if hz
1781 * is large (divide by `tick' and/or arrange to
1782 * use hzto() if hz is large).
1784 slp
= (long) (((u_long
)slp
* hz
) + 999999) / 1000000;
1787 if (qp
->c_cc
<= 0) {
1790 * There is no input, or not enough input and we can block.
1792 error
= ttysleep(tp
, TSA_HUP_OR_INPUT(tp
), TTIPRI
| PCATCH
,
1793 ISSET(tp
->t_state
, TS_CONNECTED
) ?
1794 "ttyin" : "ttyhup", (int)slp
);
1796 if (error
== EWOULDBLOCK
)
1799 thread_funnel_set(kernel_flock
, funnel_state
);
1803 * XXX what happens if another process eats some input
1804 * while we are asleep (not just here)? It would be
1805 * safest to detect changes and reset our state variables
1806 * (has_stime and last_cc).
1814 * Input present, check for input mapping and processing.
1818 if (ISSET(lflag
, ICANON
)
1819 || (ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) )
1821 if (ISSET(lflag
, ICANON
| ISIG
))
1828 icc
= min(uio_resid(uio
), IBUFSIZ
);
1829 icc
= q_to_b(qp
, ibuf
, icc
);
1835 error
= uiomove(ibuf
, icc
, uio
);
1837 * XXX if there was an error then we should ungetc() the
1838 * unmoved chars and reduce icc here.
1841 if (ISSET(tp
->t_lflag
, ECHO
) &&
1842 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1843 snpin((struct snoop
*)tp
->t_sc
, ibuf
, icc
);
1847 if (uio_resid(uio
) == 0)
1861 * delayed suspend (^Y)
1863 if (CCEQ(cc
[VDSUSP
], c
) &&
1864 ISSET(lflag
, IEXTEN
| ISIG
) == (IEXTEN
| ISIG
)) {
1865 pgsignal(tp
->t_pgrp
, SIGTSTP
, 1);
1867 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
,
1876 * Interpret EOF only in canonical mode.
1878 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
1881 * Give user character.
1883 error
= ureadc(c
, uio
);
1885 /* XXX should ungetc(c, qp). */
1889 * Only snoop directly on input in echo mode. Non-echoed
1890 * input will be snooped later iff the application echoes it.
1892 if (ISSET(tp
->t_lflag
, ECHO
) &&
1893 ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
1894 snpinc((struct snoop
*)tp
->t_sc
, (char)c
);
1896 if (uio_resid(uio
) == 0)
1899 * In canonical mode check for a "break character"
1900 * marking the end of a "line of input".
1902 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
1909 * Look to unblock input now that (presumably)
1910 * the input queue has gone down.
1913 if (ISSET(tp
->t_state
, TS_TBLOCK
) &&
1914 tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
<= I_LOW_WATER
)
1918 thread_funnel_set(kernel_flock
, funnel_state
);
1923 * Check the output queue on tp for space for a kernel message (from uprintf
1924 * or tprintf). Allow some space over the normal hiwater mark so we don't
1925 * lose messages due to normal flow control, but don't let the tty run amok.
1926 * Sleeps here are not interruptible, but we return prematurely if new signals
1930 ttycheckoutq(tp
, wait
)
1931 register struct tty
*tp
;
1938 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1940 hiwat
= tp
->t_hiwat
;
1942 oldsig
= wait
? ut
->uu_siglist
: 0;
1943 if (tp
->t_outq
.c_cc
> hiwat
+ OBUFSIZ
+ 100)
1944 while (tp
->t_outq
.c_cc
> hiwat
) {
1946 if (tp
->t_outq
.c_cc
<= hiwat
)
1948 if (wait
== 0 || ut
->uu_siglist
!= oldsig
) {
1952 SET(tp
->t_state
, TS_SO_OLOWAT
);
1953 tsleep(TSA_OLOWAT(tp
), PZERO
- 1, "ttoutq", hz
);
1960 * Process a write call on a tty device.
1963 ttwrite(tp
, uio
, flag
)
1964 register struct tty
*tp
;
1965 register struct uio
*uio
;
1968 register char *cp
= NULL
;
1969 register int cc
, ce
;
1970 register struct proc
*p
;
1971 int i
, hiwat
, count
, error
, s
;
1973 boolean_t funnel_state
;
1976 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1978 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1979 hiwat
= tp
->t_hiwat
;
1980 // LP64todo - fix this!
1981 count
= uio_resid(uio
);
1986 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1988 if (uio_resid(uio
) == count
)
1992 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
1993 if (flag
& IO_NDELAY
) {
1995 error
= EWOULDBLOCK
;
1998 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
2007 * Hang the process if it's in the background.
2010 if (isbackground(p
, tp
) &&
2011 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_flag
& P_PPWAIT
) == 0 &&
2012 (p
->p_sigignore
& sigmask(SIGTTOU
)) == 0 &&
2013 (ut
->uu_sigmask
& sigmask(SIGTTOU
)) == 0) {
2014 if (p
->p_pgrp
->pg_jobc
== 0) {
2018 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
2019 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ttybg4", 0);
2025 * Process the user's data in at most OBUFSIZ chunks. Perform any
2026 * output translation. Keep track of high water mark, sleep on
2027 * overflow awaiting device aid in acquiring new space.
2029 while (uio_resid(uio
) > 0 || cc
> 0) {
2030 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
2031 uio_setresid(uio
, 0);
2032 thread_funnel_set(kernel_flock
, funnel_state
);
2035 if (tp
->t_outq
.c_cc
> hiwat
)
2038 * Grab a hunk of data from the user, unless we have some
2039 * leftover from last time.
2042 cc
= min(uio_resid(uio
), OBUFSIZ
);
2044 error
= uiomove(cp
, cc
, uio
);
2050 if (ISSET(tp
->t_state
, TS_SNOOP
) && tp
->t_sc
!= NULL
)
2051 snpin((struct snoop
*)tp
->t_sc
, cp
, cc
);
2055 * If nothing fancy need be done, grab those characters we
2056 * can handle without any of ttyoutput's processing and
2057 * just transfer them to the output q. For those chars
2058 * which require special processing (as indicated by the
2059 * bits in char_type), call ttyoutput. After processing
2060 * a hunk of data, look for FLUSHO so ^O's will take effect
2064 if (!ISSET(tp
->t_oflag
, OPOST
))
2067 ce
= cc
- scanc((u_int
)cc
, (u_char
*)cp
,
2068 char_type
, CCLASSMASK
);
2070 * If ce is zero, then we're processing
2071 * a special character through ttyoutput.
2075 if (ttyoutput(*cp
, tp
) >= 0) {
2080 /* No Clists, wait a bit. */
2082 if (flag
& IO_NDELAY
) {
2083 error
= EWOULDBLOCK
;
2086 error
= ttysleep(tp
, &lbolt
,
2096 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2097 tp
->t_outq
.c_cc
> hiwat
)
2103 * A bunch of normal characters have been found.
2104 * Transfer them en masse to the output queue and
2105 * continue processing at the top of the loop.
2106 * If there are any further characters in this
2107 * <= OBUFSIZ chunk, the first should be a character
2108 * requiring special handling by ttyoutput.
2111 i
= b_to_q(cp
, ce
, &tp
->t_outq
);
2114 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2121 /* No Clists, wait a bit. */
2123 if (flag
& IO_NDELAY
) {
2124 error
= EWOULDBLOCK
;
2127 error
= ttysleep(tp
, &lbolt
, TTOPRI
| PCATCH
,
2134 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2135 tp
->t_outq
.c_cc
> hiwat
)
2142 * If cc is nonzero, we leave the uio structure inconsistent, as the
2143 * offset and iov pointers have moved forward, but it doesn't matter
2144 * (the call will either return short or restart with a new uio).
2146 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2147 thread_funnel_set(kernel_flock
, funnel_state
);
2154 * Since we are using ring buffers, if we can't insert any more into
2155 * the output queue, we can assume the ring is full and that someone
2156 * forgot to set the high water mark correctly. We set it and then
2157 * proceed as normal.
2159 hiwat
= tp
->t_outq
.c_cc
- 1;
2166 * This can only occur if FLUSHO is set in t_lflag,
2167 * or if ttstart/oproc is synchronous (or very fast).
2169 if (tp
->t_outq
.c_cc
<= hiwat
) {
2173 if (flag
& IO_NDELAY
) {
2175 uio_setresid(uio
, (uio_resid(uio
) + cc
));
2176 thread_funnel_set(kernel_flock
, funnel_state
);
2177 return (uio_resid(uio
) == count
? EWOULDBLOCK
: 0);
2179 SET(tp
->t_state
, TS_SO_OLOWAT
);
2180 error
= ttysleep(tp
, TSA_OLOWAT(tp
), TTOPRI
| PCATCH
, "ttywri",
2183 if (error
== EWOULDBLOCK
)
2191 * Rubout one character from the rawq of tp
2192 * as cleanly as possible.
2197 register struct tty
*tp
;
2199 register u_char
*cp
;
2200 register int savecol
;
2203 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2205 CLR(tp
->t_lflag
, FLUSHO
);
2206 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2207 if (tp
->t_rocount
== 0) {
2209 * Messed up by ttwrite; retype
2214 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2217 CLR(c
, ~TTY_CHARMASK
);
2218 switch (CCLASS(c
)) {
2227 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2231 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2236 savecol
= tp
->t_column
;
2237 SET(tp
->t_state
, TS_CNTTB
);
2238 SET(tp
->t_lflag
, FLUSHO
);
2239 tp
->t_column
= tp
->t_rocol
;
2241 cp
= tp
->t_rawq
.c_cf
;
2243 tabc
= *cp
; /* XXX FIX NEXTC */
2244 for (; cp
; cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2247 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2248 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2251 CLR(tp
->t_lflag
, FLUSHO
);
2252 CLR(tp
->t_state
, TS_CNTTB
);
2255 /* savecol will now be length of the tab. */
2256 savecol
-= tp
->t_column
;
2257 tp
->t_column
+= savecol
;
2259 savecol
= 8; /* overflow fixup */
2260 while (--savecol
>= 0)
2261 (void)ttyoutput('\b', tp
);
2264 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2265 (void)printf(PANICSTR
, c
, CCLASS(c
));
2267 panic(PANICSTR
, c
, CCLASS(c
));
2271 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2272 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2273 SET(tp
->t_state
, TS_ERASE
);
2274 (void)ttyoutput('\\', tp
);
2278 ttyecho(tp
->t_cc
[VERASE
], tp
);
2283 * Back over count characters, erasing them.
2286 ttyrubo(struct tty
*tp
, int count
)
2289 while (count
-- > 0) {
2290 (void)ttyoutput('\b', tp
);
2291 (void)ttyoutput(' ', tp
);
2292 (void)ttyoutput('\b', tp
);
2298 * Reprint the rawq line. Note, it is assumed that c_cc has already
2303 register struct tty
*tp
;
2305 register u_char
*cp
;
2308 /* Echo the reprint character. */
2309 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2310 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2312 (void)ttyoutput('\n', tp
);
2316 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2317 * BIT OF FIRST CHAR.
2321 for (cp
= tp
->t_canq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2322 cp
!= NULL
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2324 for (cp
= tp
->t_rawq
.c_cf
, c
= (cp
!= NULL
? *cp
: 0);
2325 cp
!= NULL
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2328 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2330 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2333 CLR(tp
->t_state
, TS_ERASE
);
2336 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2341 * Echo a typed character to the terminal.
2346 register struct tty
*tp
;
2349 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2350 CLR(tp
->t_lflag
, FLUSHO
);
2351 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2352 (c
!= '\n' || !ISSET(tp
->t_lflag
, ECHONL
))) ||
2353 ISSET(tp
->t_lflag
, EXTPROC
))
2355 if (ISSET(tp
->t_lflag
, ECHOCTL
) &&
2356 ((ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n') ||
2357 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2358 (void)ttyoutput('^', tp
);
2359 CLR(c
, ~TTY_CHARMASK
);
2365 (void)ttyoutput(c
, tp
);
2369 * Wake up any readers on a tty.
2373 register struct tty
*tp
;
2377 if (tp
->t_rsel
.si_pid
!= 0)
2379 selwakeup(&tp
->t_rsel
);
2380 if (ISSET(tp
->t_state
, TS_ASYNC
))
2381 pgsignal(tp
->t_pgrp
, SIGIO
, 1);
2382 wakeup(TSA_HUP_OR_INPUT(tp
));
2386 * Wake up any writers on a tty.
2390 register struct tty
*tp
;
2393 if (tp
->t_wsel
.si_pid
!= 0 && tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2395 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
)
2397 selwakeup(&tp
->t_wsel
);
2398 if (ISSET(tp
->t_state
, TS_BUSY
| TS_SO_OCOMPLETE
) ==
2399 TS_SO_OCOMPLETE
&& tp
->t_outq
.c_cc
== 0) {
2400 CLR(tp
->t_state
, TS_SO_OCOMPLETE
);
2401 wakeup(TSA_OCOMPLETE(tp
));
2403 if (ISSET(tp
->t_state
, TS_SO_OLOWAT
) &&
2404 tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2405 CLR(tp
->t_state
, TS_SO_OLOWAT
);
2406 wakeup(TSA_OLOWAT(tp
));
2411 * Look up a code for a specified speed in a conversion table;
2412 * used by drivers to map software speed values to hardware parameters.
2415 ttspeedtab(speed
, table
)
2417 register struct speedtab
*table
;
2420 for ( ; table
->sp_speed
!= -1; table
++)
2421 if (table
->sp_speed
== speed
)
2422 return (table
->sp_code
);
2427 * Set tty hi and low water marks.
2429 * Try to arrange the dynamics so there's about one second
2430 * from hi to low water.
2434 ttsetwater(struct tty
*tp
)
2439 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2441 cps
= tp
->t_ospeed
/ 10;
2442 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2444 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2445 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2449 /* NeXT ttyinfo has been converted to the MACH kernel */
2450 #include <mach/thread_info.h>
2452 /* XXX Should be in Mach header <kern/thread.h>, but doesn't work */
2453 extern kern_return_t
thread_info_internal(thread_t thread
,
2454 thread_flavor_t flavor
,
2455 thread_info_t thread_info_out
,
2456 mach_msg_type_number_t
*thread_info_count
);
2459 * Report on state of foreground process group.
2462 ttyinfo(struct tty
*tp
)
2470 struct timeval utime
;
2471 struct timeval stime
;
2472 thread_basic_info_data_t basic_info
;
2473 mach_msg_type_number_t mmtn
= THREAD_BASIC_INFO_COUNT
;
2475 if (ttycheckoutq(tp
,0) == 0)
2478 /* Print load average. */
2479 load
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2480 ttyprintf(tp
, "load: %d.%02d ", load
/ 100, load
% 100);
2483 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2484 * that pending input will be retyped on BS.
2486 if (tp
->t_session
== NULL
) {
2487 ttyprintf(tp
, "not a controlling terminal\n");
2491 if (tp
->t_pgrp
== NULL
) {
2492 ttyprintf(tp
, "no foreground process group\n");
2496 /* first process in process group */
2497 if ((p
= tp
->t_pgrp
->pg_members
.lh_first
) == NULL
) {
2498 ttyprintf(tp
, "empty foreground process group\n");
2504 * Pick the most interesting process and copy some of its
2505 * state for printing later.
2507 for (pick
= NULL
; p
!= NULL
; p
= p
->p_pglist
.le_next
) {
2508 if (proc_compare(pick
, p
))
2512 if (TAILQ_EMPTY(&pick
->p_uthlist
) ||
2513 (uthread
= TAILQ_FIRST(&pick
->p_uthlist
)) == NULL
||
2514 (thread
= uthread
->uu_act
) == NULL
||
2515 (thread_info_internal(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &mmtn
) != KERN_SUCCESS
)) {
2516 ttyprintf(tp
, "foreground process without thread\n");
2521 switch(basic_info
.run_state
) {
2522 case TH_STATE_RUNNING
:
2525 case TH_STATE_STOPPED
:
2528 case TH_STATE_WAITING
:
2531 case TH_STATE_UNINTERRUPTIBLE
:
2532 state
= "uninterruptible";
2534 case TH_STATE_HALTED
:
2541 calcru(pick
, &utime
, &stime
, NULL
);
2543 /* Print command, pid, state, utime, and stime */
2544 ttyprintf(tp
, " cmd: %s %d %s %ld.%02ldu %ld.%02lds\n",
2548 (long)utime
.tv_sec
, utime
.tv_usec
/ 10000,
2549 (long)stime
.tv_sec
, stime
.tv_usec
/ 10000);
2554 * Returns 1 if p2 is "better" than p1
2556 * The algorithm for picking the "interesting" process is thus:
2558 * 1) Only foreground processes are eligible - implied.
2559 * 2) Runnable processes are favored over anything else. The runner
2560 * with the highest cpu utilization is picked (p_estcpu). Ties are
2561 * broken by picking the highest pid.
2562 * 3) The sleeper with the shortest sleep time is next.
2563 * 4) Further ties are broken by picking the highest pid.
2565 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2566 #define TESTAB(a, b) ((a)<<1 | (b))
2572 proc_compare(p1
, p2
)
2573 register struct proc
*p1
, *p2
;
2579 * see if at least one of them is runnable
2581 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2588 * tie - favor one with highest recent cpu utilization
2590 if (p2
->p_estcpu
> p1
->p_estcpu
)
2592 if (p1
->p_estcpu
> p2
->p_estcpu
)
2594 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2599 switch (TESTAB(p1
->p_stat
== SZOMB
, p2
->p_stat
== SZOMB
)) {
2605 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2608 * pick the one with the smallest sleep time
2610 if (p2
->p_slptime
> p1
->p_slptime
)
2612 if (p1
->p_slptime
> p2
->p_slptime
)
2614 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2618 * Output char to tty; console putchar style.
2628 if (!ISSET(tp
->t_state
, TS_CONNECTED
)) {
2633 (void)ttyoutput('\r', tp
);
2634 (void)ttyoutput(c
, tp
);
2641 * Sleep on chan, returning ERESTART if tty changed while we napped and
2642 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2643 * the tty is revoked, restarting a pending call will redo validation done
2644 * at the start of the call.
2647 ttysleep(struct tty
*tp
, void *chan
, int pri
, const char *wmesg
, int timo
)
2653 error
= tsleep(chan
, pri
, wmesg
, timo
);
2656 return (tp
->t_gen
== gen
? 0 : ERESTART
);
2661 * Allocate a tty structure and its associated buffers.
2668 MALLOC(tp
, struct tty
*, sizeof(struct tty
), M_TTYS
, M_WAITOK
|M_ZERO
);
2670 /* XXX: default to TTYCLSIZE(1024) chars for now */
2671 clalloc(&tp
->t_rawq
, TTYCLSIZE
, 1);
2672 clalloc(&tp
->t_canq
, TTYCLSIZE
, 1);
2673 /* output queue doesn't need quoting */
2674 clalloc(&tp
->t_outq
, TTYCLSIZE
, 0);
2680 * Free a tty structure and its buffers.
2686 clfree(&tp
->t_rawq
);
2687 clfree(&tp
->t_canq
);
2688 clfree(&tp
->t_outq
);
2696 * XXX this is usable not useful or used. Most tty drivers have
2697 * ifdefs for using ttymalloc() but assume a different interface.
2700 * Allocate a tty struct. Clists in the struct will be allocated by
2708 MALLOC(tp
, struct tty
*, sizeof *tp
, M_TTYS
, M_WAITOK
|M_ZERO
);
2713 #if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */
2715 * Free a tty struct. Clists in the struct should have been freed by