1 /* $NetBSD: tty.c,v 1.233 2009/10/02 23:58:53 elad Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
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. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)tty.c 8.13 (Berkeley) 1/9/95
65 #include <sys/cdefs.h>
66 __KERNEL_RCSID(0, "$NetBSD: tty.c,v 1.233 2009/10/02 23:58:53 elad Exp $");
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/ioctl.h>
77 #include <sys/dkstat.h>
79 #include <sys/kernel.h>
80 #include <sys/vnode.h>
81 #include <sys/syslog.h>
83 #include <sys/signalvar.h>
84 #include <sys/resourcevar.h>
86 #include <sys/kprintf.h>
87 #include <sys/namei.h>
88 #include <sys/sysctl.h>
89 #include <sys/kauth.h>
91 #include <sys/ioctl_compat.h>
92 #include <sys/module.h>
94 #include <machine/stdarg.h>
96 static int ttnread(struct tty
*);
97 static void ttyblock(struct tty
*);
98 static void ttyecho(int, struct tty
*);
99 static void ttyrubo(struct tty
*, int);
100 static void ttyprintf_nolock(struct tty
*, const char *fmt
, ...)
101 __attribute__((__format__(__printf__
,2,3)));
102 static int proc_compare(struct proc
*, struct proc
*);
103 static void ttysigintr(void *);
105 /* Symbolic sleep message strings. */
106 const char ttclos
[] = "ttycls";
107 const char ttopen
[] = "ttyopn";
108 const char ttybg
[] = "ttybg";
109 const char ttyin
[] = "ttyin";
110 const char ttyout
[] = "ttyout";
113 * Used to determine whether we still have a connection. This is true in
115 * 1) We have carrier.
116 * 2) It's a locally attached terminal, and we are therefore ignoring carrier.
117 * 3) We're using a flow control mechanism that overloads the carrier signal.
119 #define CONNECTED(tp) (ISSET(tp->t_state, TS_CARR_ON) || \
120 ISSET(tp->t_cflag, CLOCAL | MDMBUF))
123 * Table with character classes and parity. The 8th bit indicates parity,
124 * the 7th bit indicates the character is an alphameric or underscore (for
125 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
126 * are 0 then the character needs no special processing on output; classes
127 * other than 0 might be translated or (not currently) require delays.
129 #define E 0x00 /* Even parity. */
130 #define O 0x80 /* Odd parity. */
131 #define PARITY(c) (char_type[c] & O)
133 #define ALPHA 0x40 /* Alpha or underscore. */
134 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
136 #define CCLASSMASK 0x3f
137 #define CCLASS(c) (char_type[c] & CCLASSMASK)
142 #define NA ORDINARY | ALPHA
148 unsigned char const char_type
[] = {
149 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* nul - bel */
150 O
|BS
, E
|TB
, E
|NL
, O
|CC
, E
|VT
, O
|CR
, O
|CC
, E
|CC
, /* bs - si */
151 O
|CC
, E
|CC
, E
|CC
, O
|CC
, E
|CC
, O
|CC
, O
|CC
, E
|CC
, /* dle - etb */
152 E
|CC
, O
|CC
, O
|CC
, E
|CC
, O
|CC
, E
|CC
, E
|CC
, O
|CC
, /* can - us */
153 O
|NO
, E
|NO
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* sp - ' */
154 E
|NO
, O
|NO
, O
|NO
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|NO
, /* ( - / */
155 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* 0 - 7 */
156 O
|NA
, E
|NA
, E
|NO
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, E
|NO
, /* 8 - ? */
157 O
|NO
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* @ - G */
158 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* H - O */
159 E
|NA
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* P - W */
160 O
|NA
, E
|NA
, E
|NA
, O
|NO
, E
|NO
, O
|NO
, O
|NO
, O
|NA
, /* X - _ */
161 E
|NO
, O
|NA
, O
|NA
, E
|NA
, O
|NA
, E
|NA
, E
|NA
, O
|NA
, /* ` - g */
162 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* h - o */
163 O
|NA
, E
|NA
, E
|NA
, O
|NA
, E
|NA
, O
|NA
, O
|NA
, E
|NA
, /* p - w */
164 E
|NA
, O
|NA
, O
|NA
, E
|NO
, O
|NO
, E
|NO
, E
|NO
, O
|CC
, /* x - del */
166 * Meta chars; should be settable per character set;
167 * for now, treat them all as normal characters.
169 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
170 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
171 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
172 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
173 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
174 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
175 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
176 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
177 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
178 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
179 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
180 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
181 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
182 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
183 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
184 NA
, NA
, NA
, NA
, NA
, NA
, NA
, NA
,
195 static struct ttylist_head tty_sigqueue
= TAILQ_HEAD_INITIALIZER(tty_sigqueue
);
196 static void *tty_sigsih
;
198 struct ttylist_head ttylist
= TAILQ_HEAD_INITIALIZER(ttylist
);
201 krwlock_t ttcompat_lock
;
202 int (*ttcompatvec
)(struct tty
*, u_long
, void *, int, struct lwp
*);
209 static kauth_listener_t tty_listener
;
211 SYSCTL_SETUP(sysctl_kern_tkstat_setup
, "sysctl kern.tkstat subtree setup")
214 sysctl_createv(clog
, 0, NULL
, NULL
,
216 CTLTYPE_NODE
, "kern", NULL
,
219 sysctl_createv(clog
, 0, NULL
, NULL
,
221 CTLTYPE_NODE
, "tkstat",
222 SYSCTL_DESCR("Number of characters sent and and "
225 CTL_KERN
, KERN_TKSTAT
, CTL_EOL
);
227 sysctl_createv(clog
, 0, NULL
, NULL
,
230 SYSCTL_DESCR("Total number of tty input characters"),
232 CTL_KERN
, KERN_TKSTAT
, KERN_TKSTAT_NIN
, CTL_EOL
);
233 sysctl_createv(clog
, 0, NULL
, NULL
,
235 CTLTYPE_QUAD
, "nout",
236 SYSCTL_DESCR("Total number of tty output characters"),
237 NULL
, 0, &tk_nout
, 0,
238 CTL_KERN
, KERN_TKSTAT
, KERN_TKSTAT_NOUT
, CTL_EOL
);
239 sysctl_createv(clog
, 0, NULL
, NULL
,
241 CTLTYPE_QUAD
, "cancc",
242 SYSCTL_DESCR("Number of canonical tty input characters"),
243 NULL
, 0, &tk_cancc
, 0,
244 CTL_KERN
, KERN_TKSTAT
, KERN_TKSTAT_CANCC
, CTL_EOL
);
245 sysctl_createv(clog
, 0, NULL
, NULL
,
247 CTLTYPE_QUAD
, "rawcc",
248 SYSCTL_DESCR("Number of raw tty input characters"),
249 NULL
, 0, &tk_rawcc
, 0,
250 CTL_KERN
, KERN_TKSTAT
, KERN_TKSTAT_RAWCC
, CTL_EOL
);
254 ttyopen(struct tty
*tp
, int dialout
, int nonblock
)
260 mutex_spin_enter(&tty_lock
);
264 * If the device is already open for non-dialout, fail.
265 * Otherwise, set TS_DIALOUT to block any pending non-dialout
268 if (ISSET(tp
->t_state
, TS_ISOPEN
) &&
269 !ISSET(tp
->t_state
, TS_DIALOUT
)) {
273 SET(tp
->t_state
, TS_DIALOUT
);
277 * Wait for carrier. Also wait for any dialout
278 * processes to close the tty first.
280 while (ISSET(tp
->t_state
, TS_DIALOUT
) ||
283 error
= ttysleep(tp
, &tp
->t_rawcv
, true, 0);
290 * Don't allow a non-blocking non-dialout open if the
291 * device is already open for dialout.
293 if (ISSET(tp
->t_state
, TS_DIALOUT
)) {
301 mutex_spin_exit(&tty_lock
);
306 * Initial open of tty, or (re)entry to standard tty line discipline.
309 ttylopen(dev_t device
, struct tty
*tp
)
312 mutex_spin_enter(&tty_lock
);
314 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
315 SET(tp
->t_state
, TS_ISOPEN
);
316 memset(&tp
->t_winsize
, 0, sizeof(tp
->t_winsize
));
319 mutex_spin_exit(&tty_lock
);
324 * Handle close() on a tty line: flush and set to initial state,
325 * bumping generation number so that pending read/write calls
326 * can detect recycling of the tty.
329 ttyclose(struct tty
*tp
)
331 extern struct tty
*constty
; /* Temporary virtual console. */
332 struct session
*sess
;
334 mutex_spin_enter(&tty_lock
);
339 ttyflush(tp
, FREAD
| FWRITE
);
344 sess
= tp
->t_session
;
345 tp
->t_session
= NULL
;
347 mutex_spin_exit(&tty_lock
);
350 mutex_enter(proc_lock
);
351 /* Releases proc_lock. */
357 #define FLUSHQ(q) { \
359 ndflush(q, (q)->c_cc); \
363 * This macro is used in canonical mode input processing, where a read
364 * request shall not return unless a 'line delimiter' ('\n') or 'break'
365 * (EOF, EOL, EOL2) character (or a signal) has been received. As EOL2
366 * is an extension to the POSIX.1 defined set of special characters,
367 * recognize it only if IEXTEN is set in the set of local flags.
369 #define TTBREAKC(c, lflg) \
370 ((c) == '\n' || (((c) == cc[VEOF] || (c) == cc[VEOL] || \
371 ((c) == cc[VEOL2] && ISSET(lflg, IEXTEN))) && (c) != _POSIX_VDISABLE))
377 * Call with the tty lock held.
380 ttyinput_wlock(int c
, struct tty
*tp
)
382 int iflag
, lflag
, i
, error
;
385 KASSERT(mutex_owned(&tty_lock
));
388 * If input is pending take it first.
391 if (ISSET(lflag
, PENDIN
))
396 if (ISSET(lflag
, ICANON
)) {
408 * Handle exceptional conditions (break, parity, framing).
411 if ((error
= (ISSET(c
, TTY_ERRORMASK
))) != 0) {
412 CLR(c
, TTY_ERRORMASK
);
413 if (ISSET(error
, TTY_FE
) && c
== 0) { /* Break. */
414 if (ISSET(iflag
, IGNBRK
))
416 else if (ISSET(iflag
, BRKINT
)) {
417 ttyflush(tp
, FREAD
| FWRITE
);
418 ttysig(tp
, TTYSIG_PG1
, SIGINT
);
420 } else if (ISSET(iflag
, PARMRK
))
422 } else if ((ISSET(error
, TTY_PE
) && ISSET(iflag
, INPCK
)) ||
423 ISSET(error
, TTY_FE
)) {
424 if (ISSET(iflag
, IGNPAR
))
426 else if (ISSET(iflag
, PARMRK
)) {
427 parmrk
: (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
428 (void)putc(0 | TTY_QUOTE
, &tp
->t_rawq
);
429 (void)putc(c
| TTY_QUOTE
, &tp
->t_rawq
);
434 } else if (c
== 0377 &&
435 ISSET(iflag
, ISTRIP
|IGNPAR
|INPCK
|PARMRK
) == (INPCK
|PARMRK
)) {
436 /* "Escape" a valid character of '\377'. */
437 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
438 (void)putc(0377 | TTY_QUOTE
, &tp
->t_rawq
);
443 * In tandem mode, check high water mark.
445 if (ISSET(iflag
, IXOFF
) || ISSET(tp
->t_cflag
, CHWFLOW
))
447 if (!ISSET(tp
->t_state
, TS_TYPEN
) && ISSET(iflag
, ISTRIP
))
449 if (!ISSET(lflag
, EXTPROC
)) {
451 * Check for literal nexting very first
453 if (ISSET(tp
->t_state
, TS_LNCH
)) {
455 CLR(tp
->t_state
, TS_LNCH
);
458 * Scan for special characters. This code
459 * is really just a big case statement with
460 * non-constant cases. The bottom of the
461 * case statement is labeled ``endcase'', so goto
462 * it after a case match, or similar.
466 * Control chars which aren't controlled
467 * by ICANON, ISIG, or IXON.
469 if (ISSET(lflag
, IEXTEN
)) {
470 if (CCEQ(cc
[VLNEXT
], c
)) {
471 if (ISSET(lflag
, ECHO
)) {
472 if (ISSET(lflag
, ECHOE
)) {
473 (void)ttyoutput('^', tp
);
474 (void)ttyoutput('\b', tp
);
478 SET(tp
->t_state
, TS_LNCH
);
481 if (CCEQ(cc
[VDISCARD
], c
)) {
482 if (ISSET(lflag
, FLUSHO
))
483 CLR(tp
->t_lflag
, FLUSHO
);
485 ttyflush(tp
, FWRITE
);
487 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
)
489 SET(tp
->t_lflag
, FLUSHO
);
497 if (ISSET(lflag
, ISIG
)) {
498 if (CCEQ(cc
[VINTR
], c
) || CCEQ(cc
[VQUIT
], c
)) {
499 if (!ISSET(lflag
, NOFLSH
))
500 ttyflush(tp
, FREAD
| FWRITE
);
502 ttysig(tp
, TTYSIG_PG1
, CCEQ(cc
[VINTR
], c
) ?
506 if (CCEQ(cc
[VSUSP
], c
)) {
507 if (!ISSET(lflag
, NOFLSH
))
510 ttysig(tp
, TTYSIG_PG1
, SIGTSTP
);
515 * Handle start/stop characters.
517 if (ISSET(iflag
, IXON
)) {
518 if (CCEQ(cc
[VSTOP
], c
)) {
519 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
520 SET(tp
->t_state
, TS_TTSTOP
);
524 if (!CCEQ(cc
[VSTART
], c
))
527 * if VSTART == VSTOP then toggle
531 if (CCEQ(cc
[VSTART
], c
))
535 * IGNCR, ICRNL, & INLCR
538 if (ISSET(iflag
, IGNCR
))
540 else if (ISSET(iflag
, ICRNL
))
542 } else if (c
== '\n' && ISSET(iflag
, INLCR
))
545 if (!ISSET(lflag
, EXTPROC
) && ISSET(lflag
, ICANON
)) {
547 * From here on down canonical mode character
548 * processing takes place.
553 if (CCEQ(cc
[VERASE
], c
)) {
555 ttyrub(unputc(&tp
->t_rawq
), tp
);
561 if (CCEQ(cc
[VKILL
], c
)) {
562 if (ISSET(lflag
, ECHOKE
) &&
563 tp
->t_rawq
.c_cc
== tp
->t_rocount
&&
564 !ISSET(lflag
, ECHOPRT
))
565 while (tp
->t_rawq
.c_cc
)
566 ttyrub(unputc(&tp
->t_rawq
), tp
);
569 if (ISSET(lflag
, ECHOK
) ||
570 ISSET(lflag
, ECHOKE
))
575 CLR(tp
->t_state
, TS_LOCAL
);
579 * Extensions to the POSIX.1 GTI set of functions.
581 if (ISSET(lflag
, IEXTEN
)) {
585 if (CCEQ(cc
[VWERASE
], c
)) {
586 int alt
= ISSET(lflag
, ALTWERASE
);
592 while ((c
= unputc(&tp
->t_rawq
)) == ' ' ||
598 * erase last char of word and remember the
599 * next chars type (for ALTWERASE)
602 c
= unputc(&tp
->t_rawq
);
605 if (c
== ' ' || c
== '\t') {
606 (void)putc(c
, &tp
->t_rawq
);
615 c
= unputc(&tp
->t_rawq
);
618 } while (c
!= ' ' && c
!= '\t' &&
619 (alt
== 0 || ISALPHA(c
) == ctype
));
620 (void)putc(c
, &tp
->t_rawq
);
626 if (CCEQ(cc
[VREPRINT
], c
)) {
631 * ^T - kernel info and generate SIGINFO
633 if (CCEQ(cc
[VSTATUS
], c
)) {
634 ttysig(tp
, TTYSIG_PG1
, SIGINFO
);
640 * Check for input buffer overflow
642 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
>= TTYHOG
) {
643 if (ISSET(iflag
, IMAXBEL
)) {
644 if (tp
->t_outq
.c_cc
< tp
->t_hiwat
)
645 (void)ttyoutput(CTRL('g'), tp
);
647 ttyflush(tp
, FREAD
| FWRITE
);
651 * Put data char in q for user and
652 * wakeup on seeing a line delimiter.
654 if (putc(c
, &tp
->t_rawq
) >= 0) {
655 if (!ISSET(lflag
, ICANON
)) {
660 if (TTBREAKC(c
, lflag
)) {
662 catq(&tp
->t_rawq
, &tp
->t_canq
);
664 } else if (tp
->t_rocount
++ == 0)
665 tp
->t_rocol
= tp
->t_column
;
666 if (ISSET(tp
->t_state
, TS_ERASE
)) {
668 * end of prterase \.../
670 CLR(tp
->t_state
, TS_ERASE
);
671 (void)ttyoutput('/', tp
);
675 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ECHO
)) {
677 * Place the cursor over the '^' of the ^D.
679 i
= min(2, tp
->t_column
- i
);
681 (void)ttyoutput('\b', tp
);
688 * IXANY means allow any character to restart output.
690 if (ISSET(tp
->t_state
, TS_TTSTOP
) &&
691 !ISSET(iflag
, IXANY
) && cc
[VSTART
] != cc
[VSTOP
]) {
695 CLR(tp
->t_lflag
, FLUSHO
);
696 CLR(tp
->t_state
, TS_TTSTOP
);
698 return (ttstart(tp
));
702 * Process input of a single character received on a tty.
704 * XXX - this is a hack, all drivers must changed to acquire the
705 * lock before calling linesw->l_rint()
708 ttyinput(int c
, struct tty
*tp
)
713 * Unless the receiver is enabled, drop incoming data.
715 if (!ISSET(tp
->t_cflag
, CREAD
))
718 mutex_spin_enter(&tty_lock
);
719 error
= ttyinput_wlock(c
, tp
);
720 mutex_spin_exit(&tty_lock
);
726 * Output a single character on a tty, doing output processing
727 * as needed (expanding tabs, newline processing, etc.).
728 * Returns < 0 if succeeds, otherwise returns char to resend.
731 * Call with tty lock held.
734 ttyoutput(int c
, struct tty
*tp
)
739 KASSERT(mutex_owned(&tty_lock
));
742 if (!ISSET(oflag
, OPOST
)) {
745 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
750 * Do tab expansion if OXTABS is set. Special case if we do external
751 * processing, we don't do the tab expansion because we'll probably
752 * get it wrong. If tab expansion needs to be done, let it happen
755 CLR(c
, ~TTY_CHARMASK
);
757 ISSET(oflag
, OXTABS
) && !ISSET(tp
->t_lflag
, EXTPROC
)) {
758 c
= 8 - (tp
->t_column
& 7);
759 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
762 notout
= b_to_q(" ", c
, &tp
->t_outq
);
768 return (notout
? '\t' : -1);
770 if (c
== CEOT
&& ISSET(oflag
, ONOEOT
))
774 * Newline translation: if ONLCR is set,
775 * translate newline into "\r\n".
777 if (c
== '\n' && ISSET(tp
->t_oflag
, ONLCR
)) {
780 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc('\r', &tp
->t_outq
))
783 /* If OCRNL is set, translate "\r" into "\n". */
784 else if (c
== '\r' && ISSET(tp
->t_oflag
, OCRNL
))
786 /* If ONOCR is set, don't transmit CRs when on column 0. */
787 else if (c
== '\r' && ISSET(tp
->t_oflag
, ONOCR
) && tp
->t_column
== 0)
792 if (!ISSET(tp
->t_lflag
, FLUSHO
) && putc(c
, &tp
->t_outq
))
804 if (ISSET(tp
->t_oflag
, ONLCR
| ONLRET
))
814 col
= (col
+ 8) & ~7;
822 * Ioctls for all tty devices. Called after line-discipline specific ioctl
823 * has been called to do discipline-specific functions and/or reject any
824 * of these ioctl commands.
828 ttioctl(struct tty
*tp
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
830 extern struct tty
*constty
; /* Temporary virtual console. */
831 struct proc
*p
= l
? l
->l_proc
: NULL
;
837 /* If the ioctl involves modification, hang if in the background. */
864 mutex_spin_enter(&tty_lock
);
865 while (isbackground(curproc
, tp
) &&
866 p
->p_pgrp
->pg_jobc
&& (p
->p_lflag
& PL_PPWAIT
) == 0 &&
867 !sigismasked(l
, SIGTTOU
)) {
868 mutex_spin_exit(&tty_lock
);
870 mutex_enter(proc_lock
);
871 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
872 mutex_exit(proc_lock
);
874 mutex_spin_enter(&tty_lock
);
875 error
= ttysleep(tp
, &lbolt
, true, 0);
877 mutex_spin_exit(&tty_lock
);
881 mutex_spin_exit(&tty_lock
);
885 switch (cmd
) { /* Process the ioctl. */
886 case FIOASYNC
: /* set/clear async i/o */
887 mutex_spin_enter(&tty_lock
);
889 SET(tp
->t_state
, TS_ASYNC
);
891 CLR(tp
->t_state
, TS_ASYNC
);
892 mutex_spin_exit(&tty_lock
);
894 case FIONBIO
: /* set/clear non-blocking i/o */
895 break; /* XXX: delete. */
896 case FIONREAD
: /* get # bytes to read */
897 mutex_spin_enter(&tty_lock
);
898 *(int *)data
= ttnread(tp
);
899 mutex_spin_exit(&tty_lock
);
901 case FIONWRITE
: /* get # bytes to written & unsent */
902 mutex_spin_enter(&tty_lock
);
903 *(int *)data
= tp
->t_outq
.c_cc
;
904 mutex_spin_exit(&tty_lock
);
906 case FIONSPACE
: /* get # bytes to written & unsent */
907 mutex_spin_enter(&tty_lock
);
908 *(int *)data
= tp
->t_outq
.c_cn
- tp
->t_outq
.c_cc
;
909 mutex_spin_exit(&tty_lock
);
911 case TIOCEXCL
: /* set exclusive use of tty */
912 mutex_spin_enter(&tty_lock
);
913 SET(tp
->t_state
, TS_XCLUDE
);
914 mutex_spin_exit(&tty_lock
);
916 case TIOCFLUSH
: { /* flush buffers */
917 int flags
= *(int *)data
;
920 flags
= FREAD
| FWRITE
;
922 flags
&= FREAD
| FWRITE
;
923 mutex_spin_enter(&tty_lock
);
925 mutex_spin_exit(&tty_lock
);
928 case TIOCCONS
: /* become virtual console */
930 if (constty
&& constty
!= tp
&&
931 ISSET(constty
->t_state
, TS_CARR_ON
| TS_ISOPEN
) ==
932 (TS_CARR_ON
| TS_ISOPEN
))
935 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
937 if ((error
= namei(&nd
)) != 0)
939 error
= VOP_ACCESS(nd
.ni_vp
, VREAD
, l
->l_cred
);
945 } else if (tp
== constty
)
948 case TIOCDRAIN
: /* wait till output drained */
949 if ((error
= ttywait(tp
)) != 0)
952 case TIOCGETA
: { /* get termios struct */
953 struct termios
*t
= (struct termios
*)data
;
955 memcpy(t
, &tp
->t_termios
, sizeof(struct termios
));
958 case TIOCGETD
: /* get line discipline (old) */
959 *(int *)data
= tp
->t_linesw
->l_no
;
961 case TIOCGLINED
: /* get line discipline (new) */
962 (void)strncpy((char *)data
, tp
->t_linesw
->l_name
,
965 case TIOCGWINSZ
: /* get window size */
966 *(struct winsize
*)data
= tp
->t_winsize
;
969 mutex_enter(proc_lock
);
970 if (tp
->t_session
!= NULL
&& !isctty(p
, tp
)) {
971 mutex_exit(proc_lock
);
974 *(int *)data
= tp
->t_pgrp
? -tp
->t_pgrp
->pg_id
: 0;
975 mutex_exit(proc_lock
);
977 case TIOCGPGRP
: /* get pgrp of tty */
978 mutex_enter(proc_lock
);
979 if (!isctty(p
, tp
)) {
980 mutex_exit(proc_lock
);
983 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: NO_PGID
;
984 mutex_exit(proc_lock
);
986 case TIOCGSID
: /* get sid of tty */
987 mutex_enter(proc_lock
);
988 if (!isctty(p
, tp
)) {
989 mutex_exit(proc_lock
);
992 *(int *)data
= tp
->t_session
->s_sid
;
993 mutex_exit(proc_lock
);
996 case TIOCHPCL
: /* hang up on last close */
997 mutex_spin_enter(&tty_lock
);
998 SET(tp
->t_cflag
, HUPCL
);
999 mutex_spin_exit(&tty_lock
);
1002 case TIOCNXCL
: /* reset exclusive use of tty */
1003 mutex_spin_enter(&tty_lock
);
1004 CLR(tp
->t_state
, TS_XCLUDE
);
1005 mutex_spin_exit(&tty_lock
);
1007 case TIOCOUTQ
: /* output queue size */
1008 *(int *)data
= tp
->t_outq
.c_cc
;
1010 case TIOCSETA
: /* set termios struct */
1011 case TIOCSETAW
: /* drain output, set */
1012 case TIOCSETAF
: { /* drn out, fls in, set */
1013 struct termios
*t
= (struct termios
*)data
;
1015 if (cmd
== TIOCSETAW
|| cmd
== TIOCSETAF
) {
1016 if ((error
= ttywait(tp
)) != 0)
1019 if (cmd
== TIOCSETAF
) {
1020 mutex_spin_enter(&tty_lock
);
1021 ttyflush(tp
, FREAD
);
1022 mutex_spin_exit(&tty_lock
);
1028 * XXXSMP - some drivers call back on us from t_param(), so
1029 * don't take the tty spin lock here.
1030 * require t_param() to unlock upon callback?
1032 /* wanted here: mutex_spin_enter(&tty_lock); */
1033 if (!ISSET(t
->c_cflag
, CIGNORE
)) {
1035 * Set device hardware.
1037 if (tp
->t_param
&& (error
= (*tp
->t_param
)(tp
, t
))) {
1038 /* wanted here: mutex_spin_exit(&tty_lock); */
1042 tp
->t_cflag
= t
->c_cflag
;
1043 tp
->t_ispeed
= t
->c_ispeed
;
1044 tp
->t_ospeed
= t
->c_ospeed
;
1045 if (t
->c_ospeed
== 0)
1046 ttysig(tp
, TTYSIG_LEADER
, SIGHUP
);
1051 /* delayed lock acquiring */
1052 mutex_spin_enter(&tty_lock
);
1053 if (cmd
!= TIOCSETAF
) {
1054 if (ISSET(t
->c_lflag
, ICANON
) !=
1055 ISSET(tp
->t_lflag
, ICANON
)) {
1056 if (ISSET(t
->c_lflag
, ICANON
)) {
1057 SET(tp
->t_lflag
, PENDIN
);
1062 catq(&tp
->t_rawq
, &tp
->t_canq
);
1064 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 memcpy(tp
->t_cc
, t
->c_cc
, sizeof(t
->c_cc
));
1081 mutex_spin_exit(&tty_lock
);
1085 case TIOCSETD
: /* set line discipline (old) */
1086 lp
= ttyldisc_lookup_bynum(*(int *)data
);
1089 case TIOCSLINED
: { /* set line discipline (new) */
1090 char *name
= (char *)data
;
1093 /* Null terminate to prevent buffer overflow */
1094 name
[TTLINEDNAMELEN
- 1] = '\0';
1095 lp
= ttyldisc_lookup(name
);
1100 if (lp
!= tp
->t_linesw
) {
1103 (*tp
->t_linesw
->l_close
)(tp
, flag
);
1104 error
= (*lp
->l_open
)(device
, tp
);
1106 (void)(*tp
->t_linesw
->l_open
)(device
, tp
);
1108 ttyldisc_release(lp
);
1111 ttyldisc_release(tp
->t_linesw
);
1115 /* Drop extra reference. */
1116 ttyldisc_release(lp
);
1120 case TIOCSTART
: /* start output, like ^Q */
1121 mutex_spin_enter(&tty_lock
);
1122 if (ISSET(tp
->t_state
, TS_TTSTOP
) ||
1123 ISSET(tp
->t_lflag
, FLUSHO
)) {
1124 CLR(tp
->t_lflag
, FLUSHO
);
1125 CLR(tp
->t_state
, TS_TTSTOP
);
1128 mutex_spin_exit(&tty_lock
);
1130 case TIOCSTI
: /* simulate terminal input */
1131 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_STI
,
1133 if (!ISSET(flag
, FREAD
))
1138 (*tp
->t_linesw
->l_rint
)(*(u_char
*)data
, tp
);
1140 case TIOCSTOP
: /* stop output, like ^S */
1142 mutex_spin_enter(&tty_lock
);
1143 if (!ISSET(tp
->t_state
, TS_TTSTOP
)) {
1144 SET(tp
->t_state
, TS_TTSTOP
);
1147 mutex_spin_exit(&tty_lock
);
1150 case TIOCSCTTY
: /* become controlling tty */
1151 mutex_enter(proc_lock
);
1152 mutex_spin_enter(&tty_lock
);
1154 /* Session ctty vnode pointer set in vnode layer. */
1155 if (!SESS_LEADER(p
) ||
1156 ((p
->p_session
->s_ttyvp
|| tp
->t_session
) &&
1157 (tp
->t_session
!= p
->p_session
))) {
1158 mutex_spin_exit(&tty_lock
);
1159 mutex_exit(proc_lock
);
1164 * `p_session' acquires a reference.
1165 * But note that if `t_session' is set at this point,
1166 * it must equal `p_session', in which case the session
1167 * already has the correct reference count.
1169 if (tp
->t_session
== NULL
) {
1170 proc_sesshold(p
->p_session
);
1172 tp
->t_session
= p
->p_session
;
1173 tp
->t_pgrp
= p
->p_pgrp
;
1174 p
->p_session
->s_ttyp
= tp
;
1175 p
->p_lflag
|= PL_CONTROLT
;
1176 mutex_spin_exit(&tty_lock
);
1177 mutex_exit(proc_lock
);
1179 case FIOSETOWN
: { /* set pgrp of tty */
1180 pid_t pgid
= *(int *)data
;
1183 mutex_enter(proc_lock
);
1184 if (tp
->t_session
!= NULL
&& !isctty(p
, tp
)) {
1185 mutex_exit(proc_lock
);
1190 pgrp
= pg_find(-pgid
, PFIND_LOCKED
| PFIND_UNLOCK_FAIL
);
1195 p1
= p_find(pgid
, PFIND_LOCKED
| PFIND_UNLOCK_FAIL
);
1201 if (pgrp
->pg_session
!= p
->p_session
) {
1202 mutex_exit(proc_lock
);
1205 mutex_spin_enter(&tty_lock
);
1207 mutex_spin_exit(&tty_lock
);
1208 mutex_exit(proc_lock
);
1211 case TIOCSPGRP
: { /* set pgrp of tty */
1214 mutex_enter(proc_lock
);
1215 if (!isctty(p
, tp
)) {
1216 mutex_exit(proc_lock
);
1219 pgrp
= pg_find(*(int *)data
, PFIND_LOCKED
| PFIND_UNLOCK_FAIL
);
1222 if (pgrp
->pg_session
!= p
->p_session
) {
1223 mutex_exit(proc_lock
);
1226 mutex_spin_enter(&tty_lock
);
1228 mutex_spin_exit(&tty_lock
);
1229 mutex_exit(proc_lock
);
1232 case TIOCSTAT
: /* get load avg stats */
1233 mutex_enter(proc_lock
);
1234 ttygetinfo(tp
, 0, infobuf
, sizeof(infobuf
));
1235 mutex_exit(proc_lock
);
1237 mutex_spin_enter(&tty_lock
);
1238 ttyputinfo(tp
, infobuf
);
1239 mutex_spin_exit(&tty_lock
);
1241 case TIOCSWINSZ
: /* set window size */
1242 mutex_spin_enter(&tty_lock
);
1243 if (memcmp((void *)&tp
->t_winsize
, data
,
1244 sizeof(struct winsize
))) {
1245 tp
->t_winsize
= *(struct winsize
*)data
;
1246 ttysig(tp
, TTYSIG_PG1
, SIGWINCH
);
1248 mutex_spin_exit(&tty_lock
);
1251 /* We may have to load the compat module for this. */
1253 rw_enter(&ttcompat_lock
, RW_READER
);
1254 if (ttcompatvec
!= NULL
) {
1257 rw_exit(&ttcompat_lock
);
1258 mutex_enter(&module_lock
);
1259 (void)module_autoload("compat", MODULE_CLASS_ANY
);
1260 if (ttcompatvec
== NULL
) {
1261 mutex_exit(&module_lock
);
1262 return EPASSTHROUGH
;
1264 mutex_exit(&module_lock
);
1266 error
= (*ttcompatvec
)(tp
, cmd
, data
, flag
, l
);
1267 rw_exit(&ttcompat_lock
);
1274 ttpoll(struct tty
*tp
, int events
, struct lwp
*l
)
1279 mutex_spin_enter(&tty_lock
);
1280 if (events
& (POLLIN
| POLLRDNORM
))
1281 if (ttnread(tp
) > 0)
1282 revents
|= events
& (POLLIN
| POLLRDNORM
);
1284 if (events
& (POLLOUT
| POLLWRNORM
))
1285 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
)
1286 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1288 if (events
& POLLHUP
)
1293 if (events
& (POLLIN
| POLLHUP
| POLLRDNORM
))
1294 selrecord(l
, &tp
->t_rsel
);
1296 if (events
& (POLLOUT
| POLLWRNORM
))
1297 selrecord(l
, &tp
->t_wsel
);
1300 mutex_spin_exit(&tty_lock
);
1306 filt_ttyrdetach(struct knote
*kn
)
1311 mutex_spin_enter(&tty_lock
);
1312 SLIST_REMOVE(&tp
->t_rsel
.sel_klist
, kn
, knote
, kn_selnext
);
1313 mutex_spin_exit(&tty_lock
);
1317 filt_ttyread(struct knote
*kn
, long hint
)
1322 if ((hint
& NOTE_SUBMIT
) == 0)
1323 mutex_spin_enter(&tty_lock
);
1324 kn
->kn_data
= ttnread(tp
);
1325 if ((hint
& NOTE_SUBMIT
) == 0)
1326 mutex_spin_exit(&tty_lock
);
1327 return (kn
->kn_data
> 0);
1331 filt_ttywdetach(struct knote
*kn
)
1336 mutex_spin_enter(&tty_lock
);
1337 SLIST_REMOVE(&tp
->t_wsel
.sel_klist
, kn
, knote
, kn_selnext
);
1338 mutex_spin_exit(&tty_lock
);
1342 filt_ttywrite(struct knote
*kn
, long hint
)
1348 if ((hint
& NOTE_SUBMIT
) == 0)
1349 mutex_spin_enter(&tty_lock
);
1350 kn
->kn_data
= tp
->t_outq
.c_cn
- tp
->t_outq
.c_cc
;
1351 canwrite
= (tp
->t_outq
.c_cc
<= tp
->t_lowat
) && CONNECTED(tp
);
1352 if ((hint
& NOTE_SUBMIT
) == 0)
1353 mutex_spin_exit(&tty_lock
);
1357 static const struct filterops ttyread_filtops
=
1358 { 1, NULL
, filt_ttyrdetach
, filt_ttyread
};
1359 static const struct filterops ttywrite_filtops
=
1360 { 1, NULL
, filt_ttywdetach
, filt_ttywrite
};
1363 ttykqfilter(dev_t dev
, struct knote
*kn
)
1366 struct klist
*klist
;
1368 if ((tp
= cdev_tty(dev
)) == NULL
)
1371 switch (kn
->kn_filter
) {
1373 klist
= &tp
->t_rsel
.sel_klist
;
1374 kn
->kn_fop
= &ttyread_filtops
;
1377 klist
= &tp
->t_wsel
.sel_klist
;
1378 kn
->kn_fop
= &ttywrite_filtops
;
1386 mutex_spin_enter(&tty_lock
);
1387 SLIST_INSERT_HEAD(klist
, kn
, kn_selnext
);
1388 mutex_spin_exit(&tty_lock
);
1394 * Find the number of chars ready to be read from this tty.
1395 * Call with the tty lock held.
1398 ttnread(struct tty
*tp
)
1402 KASSERT(mutex_owned(&tty_lock
));
1404 if (ISSET(tp
->t_lflag
, PENDIN
))
1406 nread
= tp
->t_canq
.c_cc
;
1407 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1408 nread
+= tp
->t_rawq
.c_cc
;
1409 if (nread
< tp
->t_cc
[VMIN
] && !tp
->t_cc
[VTIME
])
1416 * Wait for output to drain.
1419 ttywait(struct tty
*tp
)
1425 mutex_spin_enter(&tty_lock
);
1426 while ((tp
->t_outq
.c_cc
|| ISSET(tp
->t_state
, TS_BUSY
)) &&
1427 CONNECTED(tp
) && tp
->t_oproc
) {
1429 error
= ttysleep(tp
, &tp
->t_outcv
, true, 0);
1433 mutex_spin_exit(&tty_lock
);
1439 * Flush if successfully wait.
1442 ttywflush(struct tty
*tp
)
1446 if ((error
= ttywait(tp
)) == 0) {
1447 mutex_spin_enter(&tty_lock
);
1448 ttyflush(tp
, FREAD
);
1449 mutex_spin_exit(&tty_lock
);
1455 * Flush tty read and/or write queues, notifying anyone waiting.
1456 * Call with the tty lock held.
1459 ttyflush(struct tty
*tp
, int rw
)
1462 KASSERT(mutex_owned(&tty_lock
));
1465 FLUSHQ(&tp
->t_canq
);
1466 FLUSHQ(&tp
->t_rawq
);
1469 CLR(tp
->t_state
, TS_LOCAL
);
1473 CLR(tp
->t_state
, TS_TTSTOP
);
1475 FLUSHQ(&tp
->t_outq
);
1476 cv_broadcast(&tp
->t_outcv
);
1477 selnotify(&tp
->t_wsel
, 0, NOTE_SUBMIT
);
1482 * Copy in the default termios characters.
1485 ttychars(struct tty
*tp
)
1488 memcpy(tp
->t_cc
, ttydefchars
, sizeof(ttydefchars
));
1492 * Send stop character on input overflow.
1493 * Call with the tty lock held.
1496 ttyblock(struct tty
*tp
)
1500 KASSERT(mutex_owned(&tty_lock
));
1502 total
= tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
;
1503 if (tp
->t_rawq
.c_cc
> TTYHOG
) {
1504 ttyflush(tp
, FREAD
| FWRITE
);
1505 CLR(tp
->t_state
, TS_TBLOCK
);
1508 * Block further input iff: current input > threshold
1509 * AND input is available to user program.
1511 if (total
>= TTYHOG
/ 2 &&
1512 !ISSET(tp
->t_state
, TS_TBLOCK
) &&
1513 (!ISSET(tp
->t_lflag
, ICANON
) || tp
->t_canq
.c_cc
> 0)) {
1514 if (ISSET(tp
->t_iflag
, IXOFF
) &&
1515 tp
->t_cc
[VSTOP
] != _POSIX_VDISABLE
&&
1516 putc(tp
->t_cc
[VSTOP
], &tp
->t_outq
) == 0) {
1517 SET(tp
->t_state
, TS_TBLOCK
);
1520 /* Try to block remote output via hardware flow control. */
1521 if (ISSET(tp
->t_cflag
, CHWFLOW
) && tp
->t_hwiflow
&&
1522 (*tp
->t_hwiflow
)(tp
, 1) != 0)
1523 SET(tp
->t_state
, TS_TBLOCK
);
1528 * Delayed line discipline output
1531 ttrstrt(void *tp_arg
)
1540 mutex_spin_enter(&tty_lock
);
1542 CLR(tp
->t_state
, TS_TIMEOUT
);
1543 ttstart(tp
); /* XXX - Shouldn't this be tp->l_start(tp)? */
1545 mutex_spin_exit(&tty_lock
);
1549 * start a line discipline
1550 * Always call with tty lock held?
1553 ttstart(struct tty
*tp
)
1556 if (tp
->t_oproc
!= NULL
) /* XXX: Kludge for pty. */
1562 * "close" a line discipline
1565 ttylclose(struct tty
*tp
, int flag
)
1568 if (flag
& FNONBLOCK
) {
1569 mutex_spin_enter(&tty_lock
);
1570 ttyflush(tp
, FREAD
| FWRITE
);
1571 mutex_spin_exit(&tty_lock
);
1578 * Handle modem control transition on a tty.
1579 * Flag indicates new state of carrier.
1580 * Returns 0 if the line should be turned off, otherwise 1.
1583 ttymodem(struct tty
*tp
, int flag
)
1586 mutex_spin_enter(&tty_lock
);
1588 if (ISSET(tp
->t_state
, TS_CARR_ON
)) {
1592 CLR(tp
->t_state
, TS_CARR_ON
);
1593 if (ISSET(tp
->t_state
, TS_ISOPEN
) && !CONNECTED(tp
)) {
1594 ttysig(tp
, TTYSIG_LEADER
, SIGHUP
);
1595 ttyflush(tp
, FREAD
| FWRITE
);
1596 mutex_spin_exit(&tty_lock
);
1601 if (!ISSET(tp
->t_state
, TS_CARR_ON
)) {
1605 SET(tp
->t_state
, TS_CARR_ON
);
1609 mutex_spin_exit(&tty_lock
);
1615 * Default modem control routine (for other line disciplines).
1616 * Return argument flag, to turn off device on carrier drop.
1619 nullmodem(struct tty
*tp
, int flag
)
1622 mutex_spin_enter(&tty_lock
);
1624 SET(tp
->t_state
, TS_CARR_ON
);
1626 CLR(tp
->t_state
, TS_CARR_ON
);
1627 if (!CONNECTED(tp
)) {
1628 ttysig(tp
, TTYSIG_LEADER
, SIGHUP
);
1629 mutex_spin_exit(&tty_lock
);
1633 mutex_spin_exit(&tty_lock
);
1639 * Reinput pending characters after state switch.
1642 ttypend(struct tty
*tp
)
1647 KASSERT(mutex_owned(&tty_lock
));
1649 CLR(tp
->t_lflag
, PENDIN
);
1650 SET(tp
->t_state
, TS_TYPEN
);
1652 tp
->t_rawq
.c_cc
= 0;
1653 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
1654 while ((c
= getc(&tq
)) >= 0)
1655 ttyinput_wlock(c
, tp
);
1656 CLR(tp
->t_state
, TS_TYPEN
);
1660 * Process a read call on a tty device.
1663 ttread(struct tty
*tp
, struct uio
*uio
, int flag
)
1668 int c
, first
, error
, has_stime
, last_cc
;
1670 struct timeval now
, stime
;
1672 if (uio
->uio_resid
== 0)
1675 stime
.tv_usec
= 0; /* XXX gcc */
1676 stime
.tv_sec
= 0; /* XXX gcc */
1686 mutex_spin_enter(&tty_lock
);
1687 lflag
= tp
->t_lflag
;
1689 * take pending input first
1691 if (ISSET(lflag
, PENDIN
))
1695 * Hang process if it's in the background.
1697 if (isbackground(p
, tp
)) {
1698 if (sigismasked(curlwp
, SIGTTIN
) ||
1699 p
->p_lflag
& PL_PPWAIT
|| p
->p_pgrp
->pg_jobc
== 0) {
1700 mutex_spin_exit(&tty_lock
);
1703 mutex_spin_exit(&tty_lock
);
1705 mutex_enter(proc_lock
);
1706 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
1707 mutex_exit(proc_lock
);
1709 mutex_spin_enter(&tty_lock
);
1710 error
= ttysleep(tp
, &lbolt
, true, 0);
1711 mutex_spin_exit(&tty_lock
);
1717 if (!ISSET(lflag
, ICANON
)) {
1723 * Check each of the four combinations.
1724 * (m > 0 && t == 0) is the normal read case.
1725 * It should be fairly efficient, so we check that and its
1726 * companion case (m == 0 && t == 0) first.
1727 * For the other two cases, we compute the target sleep time
1735 t
*= hz
; /* time in deca-ticks */
1737 * Time difference in deca-ticks, split division to avoid numeric overflow.
1738 * Ok for hz < ~200kHz
1740 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 10 * hz + \
1741 ((t1).tv_usec - (t2).tv_usec) / 100 * hz / 1000)
1748 /* first character, start timer */
1750 getmicrotime(&stime
);
1752 } else if (qp
->c_cc
> last_cc
) {
1753 /* got a character, restart timer */
1754 getmicrotime(&stime
);
1757 /* nothing, check expiration */
1759 slp
= t
- diff(now
, stime
);
1761 } else { /* m == 0 */
1766 getmicrotime(&stime
);
1770 slp
= t
- diff(now
, stime
);
1777 * Convert deca-ticks back to ticks.
1778 * Rounding down may make us wake up just short
1779 * of the target, so we round up.
1780 * Maybe we should do 'slp/10 + 1' because the
1781 * first tick maybe almost immediate.
1782 * However it is more useful for a program that sets
1783 * VTIME=10 to wakeup every second not every 1.01
1784 * seconds (if hz=100).
1786 slp
= (slp
+ 9)/ 10;
1789 } else if ((qp
= &tp
->t_canq
)->c_cc
<= 0) {
1794 * If there is no input, sleep on rawq
1795 * awaiting hardware receipt and notification.
1796 * If we have data, we don't need to check for carrier.
1798 carrier
= CONNECTED(tp
);
1799 if (!carrier
&& ISSET(tp
->t_state
, TS_ISOPEN
)) {
1800 mutex_spin_exit(&tty_lock
);
1801 return (0); /* EOF */
1803 if (!has_stime
|| slp
<= 0) {
1804 if (flag
& IO_NDELAY
) {
1805 mutex_spin_exit(&tty_lock
);
1806 return (EWOULDBLOCK
);
1809 error
= ttysleep(tp
, &tp
->t_rawcv
, true, slp
);
1810 mutex_spin_exit(&tty_lock
);
1811 /* VMIN == 0: any quantity read satisfies */
1812 if (cc
[VMIN
] == 0 && error
== EWOULDBLOCK
)
1814 if (error
&& error
!= EWOULDBLOCK
)
1819 mutex_spin_exit(&tty_lock
);
1822 * Input present, check for input mapping and processing.
1825 while ((c
= getc(qp
)) >= 0) {
1827 * delayed suspend (^Y)
1829 if (CCEQ(cc
[VDSUSP
], c
) &&
1830 ISSET(lflag
, IEXTEN
|ISIG
) == (IEXTEN
|ISIG
)) {
1831 mutex_spin_enter(&tty_lock
);
1832 ttysig(tp
, TTYSIG_PG1
, SIGTSTP
);
1834 error
= ttysleep(tp
, &lbolt
, true, 0);
1835 mutex_spin_exit(&tty_lock
);
1840 mutex_spin_exit(&tty_lock
);
1844 * Interpret EOF only in canonical mode.
1846 if (CCEQ(cc
[VEOF
], c
) && ISSET(lflag
, ICANON
))
1849 * Give user character.
1851 error
= ureadc(c
, uio
);
1854 if (uio
->uio_resid
== 0)
1857 * In canonical mode check for a "break character"
1858 * marking the end of a "line of input".
1860 if (ISSET(lflag
, ICANON
) && TTBREAKC(c
, lflag
))
1865 * Look to unblock output now that (presumably)
1866 * the input queue has gone down.
1868 mutex_spin_enter(&tty_lock
);
1869 if (ISSET(tp
->t_state
, TS_TBLOCK
) && tp
->t_rawq
.c_cc
< TTYHOG
/ 5) {
1870 if (ISSET(tp
->t_iflag
, IXOFF
) &&
1871 cc
[VSTART
] != _POSIX_VDISABLE
&&
1872 putc(cc
[VSTART
], &tp
->t_outq
) == 0) {
1873 CLR(tp
->t_state
, TS_TBLOCK
);
1876 /* Try to unblock remote output via hardware flow control. */
1877 if (ISSET(tp
->t_cflag
, CHWFLOW
) && tp
->t_hwiflow
&&
1878 (*tp
->t_hwiflow
)(tp
, 0) != 0)
1879 CLR(tp
->t_state
, TS_TBLOCK
);
1881 mutex_spin_exit(&tty_lock
);
1887 * Check the output queue on tp for space for a kernel message (from uprintf
1888 * or tprintf). Allow some space over the normal hiwater mark so we don't
1889 * lose messages due to normal flow control, but don't let the tty run amok.
1890 * Sleeps here are not interruptible, but we return prematurely if new signals
1892 * Call with tty lock held.
1895 ttycheckoutq_wlock(struct tty
*tp
, int wait
)
1899 KASSERT(mutex_owned(&tty_lock
));
1901 hiwat
= tp
->t_hiwat
;
1902 if (tp
->t_outq
.c_cc
> hiwat
+ 200)
1903 while (tp
->t_outq
.c_cc
> hiwat
) {
1907 error
= ttysleep(tp
, &tp
->t_outcv
, true, hz
);
1916 ttycheckoutq(struct tty
*tp
, int wait
)
1920 mutex_spin_enter(&tty_lock
);
1921 r
= ttycheckoutq_wlock(tp
, wait
);
1922 mutex_spin_exit(&tty_lock
);
1928 * Process a write call on a tty device.
1931 ttwrite(struct tty
*tp
, struct uio
*uio
, int flag
)
1935 int cc
, ce
, i
, hiwat
, error
;
1936 u_char obuf
[OBUFSIZ
];
1939 hiwat
= tp
->t_hiwat
;
1943 mutex_spin_enter(&tty_lock
);
1944 if (!CONNECTED(tp
)) {
1945 if (ISSET(tp
->t_state
, TS_ISOPEN
)) {
1946 mutex_spin_exit(&tty_lock
);
1948 } else if (flag
& IO_NDELAY
) {
1949 mutex_spin_exit(&tty_lock
);
1950 error
= EWOULDBLOCK
;
1953 /* Sleep awaiting carrier. */
1954 error
= ttysleep(tp
, &tp
->t_rawcv
, true, 0);
1955 mutex_spin_exit(&tty_lock
);
1963 * Hang the process if it's in the background.
1966 if (isbackground(p
, tp
) &&
1967 ISSET(tp
->t_lflag
, TOSTOP
) && (p
->p_lflag
& PL_PPWAIT
) == 0 &&
1968 !sigismasked(curlwp
, SIGTTOU
)) {
1969 if (p
->p_pgrp
->pg_jobc
== 0) {
1971 mutex_spin_exit(&tty_lock
);
1974 mutex_spin_exit(&tty_lock
);
1976 mutex_enter(proc_lock
);
1977 pgsignal(p
->p_pgrp
, SIGTTOU
, 1);
1978 mutex_exit(proc_lock
);
1980 mutex_spin_enter(&tty_lock
);
1981 error
= ttysleep(tp
, &lbolt
, true, 0);
1982 mutex_spin_exit(&tty_lock
);
1987 mutex_spin_exit(&tty_lock
);
1990 * Process the user's data in at most OBUFSIZ chunks. Perform any
1991 * output translation. Keep track of high water mark, sleep on
1992 * overflow awaiting device aid in acquiring new space.
1994 while (uio
->uio_resid
> 0 || cc
> 0) {
1995 if (ISSET(tp
->t_lflag
, FLUSHO
)) {
1999 if (tp
->t_outq
.c_cc
> hiwat
)
2002 * Grab a hunk of data from the user, unless we have some
2003 * leftover from last time.
2006 cc
= min(uio
->uio_resid
, OBUFSIZ
);
2008 error
= uiomove(cp
, cc
, uio
);
2015 * If nothing fancy need be done, grab those characters we
2016 * can handle without any of ttyoutput's processing and
2017 * just transfer them to the output q. For those chars
2018 * which require special processing (as indicated by the
2019 * bits in char_type), call ttyoutput. After processing
2020 * a hunk of data, look for FLUSHO so ^O's will take effect
2023 mutex_spin_enter(&tty_lock
);
2025 if (!ISSET(tp
->t_oflag
, OPOST
))
2028 ce
= cc
- scanc((u_int
)cc
, cp
, char_type
,
2031 * If ce is zero, then we're processing
2032 * a special character through ttyoutput.
2036 if (ttyoutput(*cp
, tp
) >= 0) {
2038 mutex_spin_exit(&tty_lock
);
2043 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2044 tp
->t_outq
.c_cc
> hiwat
) {
2045 mutex_spin_exit(&tty_lock
);
2052 * A bunch of normal characters have been found.
2053 * Transfer them en masse to the output queue and
2054 * continue processing at the top of the loop.
2055 * If there are any further characters in this
2056 * <= OBUFSIZ chunk, the first should be a character
2057 * requiring special handling by ttyoutput.
2060 i
= b_to_q(cp
, ce
, &tp
->t_outq
);
2063 cp
+= ce
, cc
-= ce
, tk_nout
+= ce
;
2067 mutex_spin_exit(&tty_lock
);
2070 if (ISSET(tp
->t_lflag
, FLUSHO
) ||
2071 tp
->t_outq
.c_cc
> hiwat
)
2075 mutex_spin_exit(&tty_lock
);
2080 * If cc is nonzero, we leave the uio structure inconsistent, as the
2081 * offset and iov pointers have moved forward, but it doesn't matter
2082 * (the call will either return short or restart with a new uio).
2084 uio
->uio_resid
+= cc
;
2089 * Since we are using ring buffers, if we can't insert any more into
2090 * the output queue, we can assume the ring is full and that someone
2091 * forgot to set the high water mark correctly. We set it and then
2092 * proceed as normal.
2094 hiwat
= tp
->t_outq
.c_cc
- 1;
2097 mutex_spin_enter(&tty_lock
);
2100 * This can only occur if FLUSHO is set in t_lflag,
2101 * or if ttstart/oproc is synchronous (or very fast).
2103 if (tp
->t_outq
.c_cc
<= hiwat
) {
2104 mutex_spin_exit(&tty_lock
);
2107 if (flag
& IO_NDELAY
) {
2108 mutex_spin_exit(&tty_lock
);
2109 error
= EWOULDBLOCK
;
2112 error
= ttysleep(tp
, &tp
->t_outcv
, true, 0);
2113 mutex_spin_exit(&tty_lock
);
2120 * Try to pull more output from the producer. Return non-zero if
2121 * there is output ready to be sent.
2124 ttypull(struct tty
*tp
)
2127 /* XXXSMP not yet KASSERT(mutex_owned(&tty_lock)); */
2129 if (tp
->t_outq
.c_cc
<= tp
->t_lowat
) {
2130 cv_broadcast(&tp
->t_outcv
);
2131 selnotify(&tp
->t_wsel
, 0, NOTE_SUBMIT
);
2133 return tp
->t_outq
.c_cc
!= 0;
2137 * Rubout one character from the rawq of tp
2138 * as cleanly as possible.
2139 * Called with tty lock held.
2142 ttyrub(int c
, struct tty
*tp
)
2147 KASSERT(mutex_owned(&tty_lock
));
2149 if (!ISSET(tp
->t_lflag
, ECHO
) || ISSET(tp
->t_lflag
, EXTPROC
))
2151 CLR(tp
->t_lflag
, FLUSHO
);
2152 if (ISSET(tp
->t_lflag
, ECHOE
)) {
2153 if (tp
->t_rocount
== 0) {
2155 * Screwed by ttwrite; retype
2160 if (c
== ('\t' | TTY_QUOTE
) || c
== ('\n' | TTY_QUOTE
))
2163 CLR(c
, ~TTY_CHARMASK
);
2164 switch (CCLASS(c
)) {
2173 if (ISSET(tp
->t_lflag
, ECHOCTL
))
2177 if (tp
->t_rocount
< tp
->t_rawq
.c_cc
) {
2181 savecol
= tp
->t_column
;
2182 SET(tp
->t_state
, TS_CNTTB
);
2183 SET(tp
->t_lflag
, FLUSHO
);
2184 tp
->t_column
= tp
->t_rocol
;
2185 for (cp
= firstc(&tp
->t_rawq
, &tabc
); cp
;
2186 cp
= nextc(&tp
->t_rawq
, cp
, &tabc
))
2188 CLR(tp
->t_lflag
, FLUSHO
);
2189 CLR(tp
->t_state
, TS_CNTTB
);
2191 /* savecol will now be length of the tab. */
2192 savecol
-= tp
->t_column
;
2193 tp
->t_column
+= savecol
;
2195 savecol
= 8; /* overflow screw */
2196 while (--savecol
>= 0)
2197 (void)ttyoutput('\b', tp
);
2200 (void)printf("ttyrub: would panic c = %d, "
2201 "val = %d\n", c
, CCLASS(c
));
2204 } else if (ISSET(tp
->t_lflag
, ECHOPRT
)) {
2205 if (!ISSET(tp
->t_state
, TS_ERASE
)) {
2206 SET(tp
->t_state
, TS_ERASE
);
2207 (void)ttyoutput('\\', tp
);
2211 ttyecho(tp
->t_cc
[VERASE
], tp
);
2216 * Back over cnt characters, erasing them.
2217 * Called with tty lock held.
2220 ttyrubo(struct tty
*tp
, int cnt
)
2223 KASSERT(mutex_owned(&tty_lock
));
2226 (void)ttyoutput('\b', tp
);
2227 (void)ttyoutput(' ', tp
);
2228 (void)ttyoutput('\b', tp
);
2234 * Reprint the rawq line. Note, it is assumed that c_cc has already
2237 * Called with tty lock held.
2240 ttyretype(struct tty
*tp
)
2245 KASSERT(mutex_owned(&tty_lock
));
2247 /* Echo the reprint character. */
2248 if (tp
->t_cc
[VREPRINT
] != _POSIX_VDISABLE
)
2249 ttyecho(tp
->t_cc
[VREPRINT
], tp
);
2251 (void)ttyoutput('\n', tp
);
2253 for (cp
= firstc(&tp
->t_canq
, &c
); cp
; cp
= nextc(&tp
->t_canq
, cp
, &c
))
2255 for (cp
= firstc(&tp
->t_rawq
, &c
); cp
; cp
= nextc(&tp
->t_rawq
, cp
, &c
))
2257 CLR(tp
->t_state
, TS_ERASE
);
2259 tp
->t_rocount
= tp
->t_rawq
.c_cc
;
2264 * Echo a typed character to the terminal.
2265 * Called with tty lock held.
2268 ttyecho(int c
, struct tty
*tp
)
2271 KASSERT(mutex_owned(&tty_lock
));
2273 if (!ISSET(tp
->t_state
, TS_CNTTB
))
2274 CLR(tp
->t_lflag
, FLUSHO
);
2275 if ((!ISSET(tp
->t_lflag
, ECHO
) &&
2276 (!ISSET(tp
->t_lflag
, ECHONL
) || c
!= '\n')) ||
2277 ISSET(tp
->t_lflag
, EXTPROC
))
2279 if (((ISSET(tp
->t_lflag
, ECHOCTL
) &&
2280 (ISSET(c
, TTY_CHARMASK
) <= 037 && c
!= '\t' && c
!= '\n')) ||
2281 ISSET(c
, TTY_CHARMASK
) == 0177)) {
2282 (void)ttyoutput('^', tp
);
2283 CLR(c
, ~TTY_CHARMASK
);
2289 (void)ttyoutput(c
, tp
);
2293 * Wake up any readers on a tty.
2294 * Called with tty lock held.
2297 ttwakeup(struct tty
*tp
)
2300 KASSERT(mutex_owned(&tty_lock
));
2302 selnotify(&tp
->t_rsel
, 0, NOTE_SUBMIT
);
2303 if (ISSET(tp
->t_state
, TS_ASYNC
))
2304 ttysig(tp
, TTYSIG_PG2
, SIGIO
);
2305 cv_broadcast(&tp
->t_rawcv
);
2309 * Look up a code for a specified speed in a conversion table;
2310 * used by drivers to map software speed values to hardware parameters.
2313 ttspeedtab(int speed
, const struct speedtab
*table
)
2316 for (; table
->sp_speed
!= -1; table
++)
2317 if (table
->sp_speed
== speed
)
2318 return (table
->sp_code
);
2323 * Set tty hi and low water marks.
2325 * Try to arrange the dynamics so there's about one second
2326 * from hi to low water.
2329 ttsetwater(struct tty
*tp
)
2333 /* XXX not yet KASSERT(mutex_owned(&tty_lock)); */
2335 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2337 cps
= tp
->t_ospeed
/ 10;
2338 tp
->t_lowat
= x
= CLAMP(cps
/ 2, TTMAXLOWAT
, TTMINLOWAT
);
2340 x
= CLAMP(x
, TTMAXHIWAT
, TTMINHIWAT
);
2341 tp
->t_hiwat
= roundup(x
, CBSIZE
);
2346 * Prepare report on state of foreground process group.
2347 * Call with proc_lock held.
2350 ttygetinfo(struct tty
*tp
, int fromsig
, char *buf
, size_t bufsz
)
2353 struct proc
*p
, *pick
= NULL
;
2354 struct timeval utime
, stime
;
2361 KASSERT(mutex_owned(proc_lock
));
2365 if (tp
->t_session
== NULL
)
2366 msg
= "not a controlling terminal\n";
2367 else if (tp
->t_pgrp
== NULL
)
2368 msg
= "no foreground process group\n";
2369 else if ((p
= LIST_FIRST(&tp
->t_pgrp
->pg_members
)) == NULL
)
2370 msg
= "empty foreground process group\n";
2372 /* Pick interesting process. */
2373 for (; p
!= NULL
; p
= LIST_NEXT(p
, p_pglist
)) {
2374 struct proc
*oldpick
;
2380 if (pick
->p_lock
< p
->p_lock
) {
2381 mutex_enter(pick
->p_lock
);
2382 mutex_enter(p
->p_lock
);
2383 } else if (pick
->p_lock
> p
->p_lock
) {
2384 mutex_enter(p
->p_lock
);
2385 mutex_enter(pick
->p_lock
);
2387 mutex_enter(p
->p_lock
);
2389 if (proc_compare(pick
, p
))
2391 mutex_exit(p
->p_lock
);
2392 if (p
->p_lock
!= oldpick
->p_lock
)
2393 mutex_exit(oldpick
->p_lock
);
2396 (SIGACTION_PS(pick
->p_sigacts
, SIGINFO
).sa_flags
&
2402 /* Print load average. */
2403 tmp
= (averunnable
.ldavg
[0] * 100 + FSCALE
/ 2) >> FSHIFT
;
2404 snprintf(lmsg
, sizeof(lmsg
), "load: %d.%02d ", tmp
/ 100, tmp
% 100);
2405 strlcat(buf
, lmsg
, bufsz
);
2408 strlcat(buf
, msg
, bufsz
);
2412 snprintf(lmsg
, sizeof(lmsg
), " cmd: %s %d [", pick
->p_comm
,
2414 strlcat(buf
, lmsg
, bufsz
);
2416 mutex_enter(pick
->p_lock
);
2417 LIST_FOREACH(l
, &pick
->p_lwps
, l_sibling
) {
2419 snprintf(lmsg
, sizeof(lmsg
), "%s%s",
2420 l
->l_stat
== LSONPROC
? "running" :
2421 l
->l_stat
== LSRUN
? "runnable" :
2422 l
->l_wchan
? l
->l_wmesg
: "iowait",
2423 (LIST_NEXT(l
, l_sibling
) != NULL
) ? " " : "] ");
2425 strlcat(buf
, lmsg
, bufsz
);
2426 pctcpu
+= l
->l_pctcpu
;
2428 pctcpu
+= pick
->p_pctcpu
;
2429 calcru(pick
, &utime
, &stime
, NULL
, NULL
);
2430 mutex_exit(pick
->p_lock
);
2432 /* Round up and print user+system time, %CPU and RSS. */
2433 utime
.tv_usec
+= 5000;
2434 if (utime
.tv_usec
>= 1000000) {
2436 utime
.tv_usec
-= 1000000;
2438 stime
.tv_usec
+= 5000;
2439 if (stime
.tv_usec
>= 1000000) {
2441 stime
.tv_usec
-= 1000000;
2443 #define pgtok(a) (((u_long) ((a) * PAGE_SIZE) / 1024))
2444 tmp
= (pctcpu
* 10000 + FSCALE
/ 2) >> FSHIFT
;
2445 if (pick
->p_stat
== SIDL
|| P_ZOMBIE(pick
))
2448 rss
= pgtok(vm_resident_count(pick
->p_vmspace
));
2450 snprintf(lmsg
, sizeof(lmsg
), "%ld.%02ldu %ld.%02lds %d%% %ldk",
2451 (long)utime
.tv_sec
, (long)utime
.tv_usec
/ 10000,
2452 (long)stime
.tv_sec
, (long)stime
.tv_usec
/ 10000,
2454 strlcat(buf
, lmsg
, bufsz
);
2458 * Print report on state of foreground process group.
2459 * Call with tty_lock held.
2462 ttyputinfo(struct tty
*tp
, char *buf
)
2465 KASSERT(mutex_owned(&tty_lock
));
2467 if (ttycheckoutq_wlock(tp
, 0) == 0)
2469 ttyprintf_nolock(tp
, "%s\n", buf
);
2470 tp
->t_rocount
= 0; /* so pending input will be retyped if BS */
2474 * Returns 1 if p2 is "better" than p1
2476 * The algorithm for picking the "interesting" process is thus:
2478 * 1) Only foreground processes are eligible - implied.
2479 * 2) Runnable processes are favored over anything else. The runner
2480 * with the highest CPU utilization is picked (l_pctcpu). Ties are
2481 * broken by picking the highest pid.
2482 * 3) The sleeper with the shortest sleep time is next. With ties,
2483 * we pick out just "short-term" sleepers (P_SINTR == 0).
2484 * 4) Further ties are broken by picking the highest pid.
2486 #define ISRUN(p) ((p)->p_nrlwps > 0)
2487 #define TESTAB(a, b) ((a)<<1 | (b))
2493 proc_compare(struct proc
*p1
, struct proc
*p2
)
2497 KASSERT(mutex_owned(p1
->p_lock
));
2498 KASSERT(mutex_owned(p2
->p_lock
));
2500 if ((l1
= LIST_FIRST(&p1
->p_lwps
)) == NULL
)
2502 if ((l2
= LIST_FIRST(&p2
->p_lwps
)) == NULL
)
2505 * see if at least one of them is runnable
2507 switch (TESTAB(ISRUN(p1
), ISRUN(p2
))) {
2514 * tie - favor one with highest recent CPU utilization
2516 if (l2
->l_pctcpu
> l1
->l_pctcpu
)
2518 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2523 switch (TESTAB(P_ZOMBIE(p1
), P_ZOMBIE(p2
))) {
2529 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2532 * pick the one with the smallest sleep time
2534 if (l2
->l_slptime
> l2
->l_slptime
)
2536 if (l2
->l_slptime
> l2
->l_slptime
)
2539 * favor one sleeping in a non-interruptible sleep
2541 if (l2
->l_flag
& LW_SINTR
&& (l2
->l_flag
& LW_SINTR
) == 0)
2543 if (l2
->l_flag
& LW_SINTR
&& (l2
->l_flag
& LW_SINTR
) == 0)
2545 return (p2
->p_pid
> p1
->p_pid
); /* tie - return highest pid */
2549 * Output char to tty; console putchar style.
2550 * Can be called with tty lock held through kprintf() machinery..
2553 tputchar(int c
, int flags
, struct tty
*tp
)
2557 if ((flags
& NOLOCK
) == 0)
2558 mutex_spin_enter(&tty_lock
);
2559 if (!CONNECTED(tp
)) {
2564 (void)ttyoutput('\r', tp
);
2565 (void)ttyoutput(c
, tp
);
2568 if ((flags
& NOLOCK
) == 0)
2569 mutex_spin_exit(&tty_lock
);
2574 * Sleep on chan, returning ERESTART if tty changed while we napped and
2575 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
2576 * the tty is revoked, restarting a pending call will redo validation done
2577 * at the start of the call.
2579 * Must be called with the tty lock held.
2582 ttysleep(struct tty
*tp
, kcondvar_t
*cv
, bool catch, int timo
)
2587 KASSERT(mutex_owned(&tty_lock
));
2591 error
= cv_timedwait_sig(cv
, &tty_lock
, timo
);
2593 error
= cv_timedwait(cv
, &tty_lock
, timo
);
2596 return (tp
->t_gen
== gen
? 0 : ERESTART
);
2600 * Attach a tty to the tty list.
2602 * This should be called ONLY once per real tty (including pty's).
2603 * eg, on the sparc, the keyboard and mouse have struct tty's that are
2604 * distinctly NOT usable as tty's, and thus should not be attached to
2605 * the ttylist. This is why this call is not done from ttymalloc().
2607 * Device drivers should attach tty's at a similar time that they are
2608 * ttymalloc()'ed, or, for the case of statically allocated struct tty's
2609 * either in the attach or (first) open routine.
2612 tty_attach(struct tty
*tp
)
2615 mutex_spin_enter(&tty_lock
);
2616 TAILQ_INSERT_TAIL(&ttylist
, tp
, tty_link
);
2618 mutex_spin_exit(&tty_lock
);
2622 * Remove a tty from the tty list.
2625 tty_detach(struct tty
*tp
)
2628 mutex_spin_enter(&tty_lock
);
2632 panic("tty_detach: tty_count < 0");
2634 TAILQ_REMOVE(&ttylist
, tp
, tty_link
);
2635 mutex_spin_exit(&tty_lock
);
2639 * Allocate a tty structure and its associated buffers.
2647 tp
= kmem_zalloc(sizeof(*tp
), KM_SLEEP
);
2648 callout_init(&tp
->t_rstrt_ch
, 0);
2649 callout_setfunc(&tp
->t_rstrt_ch
, ttrstrt
, tp
);
2650 /* XXX: default to 1024 chars for now */
2651 clalloc(&tp
->t_rawq
, 1024, 1);
2652 cv_init(&tp
->t_rawcv
, "ttyraw");
2653 cv_init(&tp
->t_rawcvf
, "ttyrawf");
2654 clalloc(&tp
->t_canq
, 1024, 1);
2655 cv_init(&tp
->t_cancv
, "ttycan");
2656 cv_init(&tp
->t_cancvf
, "ttycanf");
2657 /* output queue doesn't need quoting */
2658 clalloc(&tp
->t_outq
, 1024, 0);
2659 cv_init(&tp
->t_outcv
, "ttyout");
2660 cv_init(&tp
->t_outcvf
, "ttyoutf");
2661 /* Set default line discipline. */
2662 tp
->t_linesw
= ttyldisc_default();
2663 selinit(&tp
->t_rsel
);
2664 selinit(&tp
->t_wsel
);
2665 for (i
= 0; i
< TTYSIG_COUNT
; i
++)
2666 sigemptyset(&tp
->t_sigs
[i
]);
2671 * Free a tty structure and its buffers.
2673 * Be sure to call tty_detach() for any tty that has been
2677 ttyfree(struct tty
*tp
)
2681 mutex_enter(proc_lock
);
2682 mutex_enter(&tty_lock
);
2683 for (i
= 0; i
< TTYSIG_COUNT
; i
++)
2684 sigemptyset(&tp
->t_sigs
[i
]);
2685 if (tp
->t_sigcount
!= 0)
2686 TAILQ_REMOVE(&tty_sigqueue
, tp
, t_sigqueue
);
2687 mutex_exit(&tty_lock
);
2688 mutex_exit(proc_lock
);
2690 callout_halt(&tp
->t_rstrt_ch
, NULL
);
2691 callout_destroy(&tp
->t_rstrt_ch
);
2692 ttyldisc_release(tp
->t_linesw
);
2693 clfree(&tp
->t_rawq
);
2694 clfree(&tp
->t_canq
);
2695 clfree(&tp
->t_outq
);
2696 cv_destroy(&tp
->t_rawcv
);
2697 cv_destroy(&tp
->t_rawcvf
);
2698 cv_destroy(&tp
->t_cancv
);
2699 cv_destroy(&tp
->t_cancvf
);
2700 cv_destroy(&tp
->t_outcv
);
2701 cv_destroy(&tp
->t_outcvf
);
2702 seldestroy(&tp
->t_rsel
);
2703 seldestroy(&tp
->t_wsel
);
2704 kmem_free(tp
, sizeof(*tp
));
2708 * ttyprintf_nolock: send a message to a specific tty, without locking.
2710 * => should be used only by tty driver or anything that knows the
2711 * underlying tty will not be revoked(2)'d away. [otherwise,
2715 ttyprintf_nolock(struct tty
*tp
, const char *fmt
, ...)
2719 /* No mutex needed; going to process TTY. */
2721 kprintf(fmt
, TOTTY
|NOLOCK
, tp
, NULL
, ap
);
2726 tty_listener_cb(kauth_cred_t cred
, kauth_action_t action
, void *cookie
,
2727 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
2732 result
= KAUTH_RESULT_DEFER
;
2734 if (action
!= KAUTH_DEVICE_TTY_OPEN
)
2739 /* If it's not opened, we allow. */
2740 if ((tty
->t_state
& TS_ISOPEN
) == 0)
2741 result
= KAUTH_RESULT_ALLOW
;
2744 * If it's opened, we can only allow if it's not exclusively
2745 * opened; otherwise, that's a privileged operation and we
2746 * let the secmodel handle it.
2748 if ((tty
->t_state
& TS_XCLUDE
) == 0)
2749 result
= KAUTH_RESULT_ALLOW
;
2756 * Initialize the tty subsystem.
2762 mutex_init(&tty_lock
, MUTEX_DEFAULT
, IPL_VM
);
2763 rw_init(&ttcompat_lock
);
2764 tty_sigsih
= softint_establish(SOFTINT_CLOCK
, ttysigintr
, NULL
);
2765 KASSERT(tty_sigsih
!= NULL
);
2767 tty_listener
= kauth_listen_scope(KAUTH_SCOPE_DEVICE
,
2768 tty_listener_cb
, NULL
);
2772 * Send a signal from a tty to its process group or session leader.
2773 * Handoff to the target is deferred to a soft interrupt.
2776 ttysig(struct tty
*tp
, enum ttysigtype st
, int sig
)
2780 /* XXXSMP not yet KASSERT(mutex_owned(&tty_lock)); */
2782 sp
= &tp
->t_sigs
[st
];
2783 if (sigismember(sp
, sig
))
2786 if (tp
->t_sigcount
++ == 0)
2787 TAILQ_INSERT_TAIL(&tty_sigqueue
, tp
, t_sigqueue
);
2788 softint_schedule(tty_sigsih
);
2792 * Deliver deferred signals from ttys. Note that the process groups
2793 * and sessions associated with the ttys may have changed from when
2794 * the signal was originally sent, but in practice it should not matter.
2795 * For signals produced as a result of a syscall, the soft interrupt
2796 * will fire before the syscall returns to the user.
2799 ttysigintr(void *cookie
)
2804 struct session
*sess
;
2808 mutex_enter(proc_lock
);
2809 mutex_spin_enter(&tty_lock
);
2810 while ((tp
= TAILQ_FIRST(&tty_sigqueue
)) != NULL
) {
2811 KASSERT(tp
->t_sigcount
> 0);
2812 for (st
= 0; st
< TTYSIG_COUNT
; st
++) {
2813 if ((sig
= firstsig(&tp
->t_sigs
[st
])) != 0)
2816 KASSERT(st
< TTYSIG_COUNT
);
2817 sigdelset(&tp
->t_sigs
[st
], sig
);
2818 if (--tp
->t_sigcount
== 0)
2819 TAILQ_REMOVE(&tty_sigqueue
, tp
, t_sigqueue
);
2821 sess
= tp
->t_session
;
2822 lflag
= tp
->t_lflag
;
2823 if (sig
== SIGINFO
) {
2824 if (ISSET(tp
->t_state
, TS_SIGINFO
)) {
2825 /* Via ioctl: ignore tty option. */
2826 tp
->t_state
&= ~TS_SIGINFO
;
2829 if (!ISSET(lflag
, NOKERNINFO
)) {
2830 mutex_spin_exit(&tty_lock
);
2831 ttygetinfo(tp
, 1, infobuf
, sizeof(infobuf
));
2832 mutex_spin_enter(&tty_lock
);
2833 ttyputinfo(tp
, infobuf
);
2835 if (!ISSET(lflag
, ISIG
))
2838 mutex_spin_exit(&tty_lock
);
2843 pgsignal(pgrp
, sig
, 1);
2847 pgsignal(pgrp
, sig
, sess
!= NULL
);
2850 if (sess
!= NULL
&& sess
->s_leader
!= NULL
)
2851 psignal(sess
->s_leader
, sig
);
2857 mutex_spin_enter(&tty_lock
);
2859 mutex_spin_exit(&tty_lock
);
2860 mutex_exit(proc_lock
);