2 * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Copyright (c) 2002 Networks Associates Technologies, Inc.
11 * All rights reserved.
13 * Portions of this software were developed for the FreeBSD Project by
14 * ThinkSec AS and NAI Labs, the Security Research Division of Network
15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
16 * ("CBOSS"), as part of the DARPA CHATS research program.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * @(#)tty.c 8.8 (Berkeley) 1/21/94
47 * o Fix races for sending the start char in ttyflush().
48 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
49 * With luck, there will be MIN chars before select() returns().
50 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
51 * o Don't allow input in TS_ZOMBIE case. It would be visible through
53 * o Do the new sio locking stuff here and use it to avoid special
56 * o Move EXTPROC and/or PENDIN to t_state?
57 * o Wrap most of ttioctl in spltty/splx.
58 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
59 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
60 * o Don't allow certain termios flags to affect disciplines other
61 * than TTYDISC. Cancel their effects before switch disciplines
62 * and ignore them if they are set while we are in another
64 * o Now that historical speed conversions are handled here, don't
66 * o Check for TS_CARR_ON being set while everything is closed and not
67 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
68 * so it would live until the next open even if carrier drops.
69 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
70 * only when _all_ openers leave open().
73 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD$");
76 #include "opt_compat.h"
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/filio.h>
83 #include <sys/mutex.h>
84 #include <sys/namei.h>
87 #if defined(COMPAT_43)
88 #include <sys/ioctl_compat.h>
95 #include <sys/fcntl.h>
98 #include <sys/kernel.h>
99 #include <sys/vnode.h>
100 #include <sys/serial.h>
101 #include <sys/signalvar.h>
102 #include <sys/resourcevar.h>
103 #include <sys/malloc.h>
104 #include <sys/filedesc.h>
105 #include <sys/sched.h>
106 #include <sys/sysctl.h>
107 #include <sys/timepps.h>
109 #include <machine/stdarg.h>
113 #include <vm/vm_map.h>
115 MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures");
122 static d_open_t ttysopen;
123 static d_close_t ttysclose;
124 static d_read_t ttysrdwr;
125 static d_ioctl_t ttysioctl;
126 static d_purge_t ttypurge;
128 /* Default cdevsw for common tty devices */
129 static struct cdevsw tty_cdevsw = {
130 .d_version = D_VERSION,
136 .d_flags = D_TTY | D_NEEDGIANT,
139 /* Cdevsw for slave tty devices */
140 static struct cdevsw ttys_cdevsw = {
141 .d_version = D_VERSION,
143 .d_close = ttysclose,
146 .d_ioctl = ttysioctl,
148 .d_flags = D_TTY | D_NEEDGIANT,
151 static int proc_compare(struct proc *p1, struct proc *p2);
152 static int ttnread(struct tty *tp);
153 static void ttyecho(int c, struct tty *tp);
154 static int ttyoutput(int c, struct tty *tp);
155 static void ttypend(struct tty *tp);
156 static void ttyretype(struct tty *tp);
157 static void ttyrub(int c, struct tty *tp);
158 static void ttyrubo(struct tty *tp, int cnt);
159 static void ttyunblock(struct tty *tp);
160 static int ttywflush(struct tty *tp);
161 static int filt_ttyread(struct knote *kn, long hint);
162 static void filt_ttyrdetach(struct knote *kn);
163 static int filt_ttywrite(struct knote *kn, long hint);
164 static void filt_ttywdetach(struct knote *kn);
167 * Table with character classes and parity. The 8th bit indicates parity,
168 * the 7th bit indicates the character is an alphameric or underscore (for
169 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
170 * are 0 then the character needs no special processing on output; classes
171 * other than 0 might be translated or (not currently) require delays.
173 #define E 0x00 /* Even parity. */
174 #define O 0x80 /* Odd parity. */
175 #define PARITY(c) (char_type[c] & O)
177 #define ALPHA 0x40 /* Alpha or underscore. */
178 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
180 #define CCLASSMASK 0x3f
181 #define CCLASS(c) (char_type[c] & CCLASSMASK)
186 #define NA ORDINARY | ALPHA
192 static u_char const char_type[] = {
193 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
194 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
195 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
196 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
197 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
198 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
199 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
200 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
201 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
202 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
203 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
204 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
205 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
206 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
207 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
208 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
210 * Meta chars; should be settable per character set;
211 * for now, treat them all as normal characters.
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,
222 NA, NA, NA, NA, NA, NA, NA, NA,
223 NA, NA, NA, NA, NA, NA, NA, NA,
224 NA, NA, NA, NA, NA, NA, NA, NA,
225 NA, NA, NA, NA, NA, NA, NA, NA,
226 NA, NA, NA, NA, NA, NA, NA, NA,
227 NA, NA, NA, NA, NA, NA, NA, NA,
228 NA, NA, NA, NA, NA, NA, NA, NA,
239 /* Macros to clear/set/test flags. */
240 #define SET(t, f) (t) |= (f)
241 #define CLR(t, f) (t) &= ~(f)
242 #define ISSET(t, f) ((t) & (f))
244 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
245 #define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */
248 * list of struct tty where pstat(8) can pick it up with sysctl
250 * The lock order is to grab the list mutex before the tty mutex.
251 * Together with additions going on the tail of the list, this allows
252 * the sysctl to avoid doing retries.
254 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
255 static struct mtx tty_list_mutex;
257 static struct unrhdr *tty_unit;
259 static int drainwait = 5*60;
260 SYSCTL_INT(_kern, OID_AUTO, drainwait, CTLFLAG_RW, &drainwait,
261 0, "Output drain timeout in seconds");
264 tty_gettp(struct cdev *dev)
269 csw = dev_refthread(dev);
270 KASSERT(csw != NULL, ("No cdevsw in ttycode (%s)", devtoname(dev)));
271 KASSERT(csw->d_flags & D_TTY,
272 ("non D_TTY (%s) in tty code", devtoname(dev)));
276 ("no tty pointer on (%s) in tty code", devtoname(dev)));
281 * Initial open of tty, or (re)entry to standard tty line discipline.
284 tty_open(struct cdev *device, struct tty *tp)
291 if (!ISSET(tp->t_state, TS_ISOPEN)) {
293 SET(tp->t_state, TS_ISOPEN);
294 if (ISSET(tp->t_cflag, CLOCAL))
295 SET(tp->t_state, TS_CONNECTED);
296 bzero(&tp->t_winsize, sizeof(tp->t_winsize));
298 /* XXX don't hang forever on output */
299 if (tp->t_timeout < 0)
300 tp->t_timeout = drainwait*hz;
307 * Handle close() on a tty line: flush and set to initial state,
308 * bumping generation number so that pending read/write calls
309 * can detect recycling of the tty.
310 * XXX our caller should have done `spltty(); l_close(); tty_close();'
311 * and l_close() should have flushed, but we repeat the spltty() and
312 * the flush in case there are buggy callers.
315 tty_close(struct tty *tp)
319 funsetown(&tp->t_sigio);
324 ttyflush(tp, FREAD | FWRITE);
325 clist_free_cblocks(&tp->t_canq);
326 clist_free_cblocks(&tp->t_outq);
327 clist_free_cblocks(&tp->t_rawq);
330 tp->t_line = TTYDISC;
333 tp->t_session = NULL;
335 knlist_clear(&tp->t_rsel.si_note, 0);
336 knlist_clear(&tp->t_wsel.si_note, 0);
342 #define FLUSHQ(q) { \
344 ndflush(q, (q)->c_cc); \
347 /* Is 'c' a line delimiter ("break" character)? */
348 #define TTBREAKC(c, lflag) \
349 ((c) == '\n' || (((c) == cc[VEOF] || \
350 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
351 (c) != _POSIX_VDISABLE))
354 * Process input of a single character received on a tty.
357 ttyinput(int c, struct tty *tp)
359 tcflag_t iflag, lflag;
364 * If input is pending take it first.
367 if (ISSET(lflag, PENDIN))
372 if (ISSET(lflag, ICANON)) {
382 * Block further input iff:
383 * current input > threshold AND input is available to user program
384 * AND input flow control is enabled and not yet invoked.
385 * The 3 is slop for PARMRK.
388 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 &&
389 (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) &&
390 (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) &&
391 !ISSET(tp->t_state, TS_TBLOCK))
394 /* Handle exceptional conditions (break, parity, framing). */
396 err = (ISSET(c, TTY_ERRORMASK));
398 CLR(c, TTY_ERRORMASK);
399 if (ISSET(err, TTY_BI)) {
400 if (ISSET(iflag, IGNBRK))
402 if (ISSET(iflag, BRKINT)) {
403 ttyflush(tp, FREAD | FWRITE);
404 if (tp->t_pgrp != NULL) {
405 PGRP_LOCK(tp->t_pgrp);
406 pgsignal(tp->t_pgrp, SIGINT, 1);
407 PGRP_UNLOCK(tp->t_pgrp);
411 if (ISSET(iflag, PARMRK))
413 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK))
414 || ISSET(err, TTY_FE)) {
415 if (ISSET(iflag, IGNPAR))
417 else if (ISSET(iflag, PARMRK)) {
419 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >
422 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
423 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
424 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
431 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
433 if (!ISSET(lflag, EXTPROC)) {
435 * Check for literal nexting very first
437 if (ISSET(tp->t_state, TS_LNCH)) {
439 CLR(tp->t_state, TS_LNCH);
442 * Scan for special characters. This code
443 * is really just a big case statement with
444 * non-constant cases. The bottom of the
445 * case statement is labeled ``endcase'', so goto
446 * it after a case match, or similar.
450 * Control chars which aren't controlled
451 * by ICANON, ISIG, or IXON.
453 if (ISSET(lflag, IEXTEN)) {
454 if (CCEQ(cc[VLNEXT], c)) {
455 if (ISSET(lflag, ECHO)) {
456 if (ISSET(lflag, ECHOE)) {
457 (void)ttyoutput('^', tp);
458 (void)ttyoutput('\b', tp);
462 SET(tp->t_state, TS_LNCH);
465 if (CCEQ(cc[VDISCARD], c)) {
466 if (ISSET(lflag, FLUSHO))
467 CLR(tp->t_lflag, FLUSHO);
469 ttyflush(tp, FWRITE);
471 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
473 SET(tp->t_lflag, FLUSHO);
481 if (ISSET(lflag, ISIG)) {
482 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
483 if (!ISSET(lflag, NOFLSH))
484 ttyflush(tp, FREAD | FWRITE);
486 if (tp->t_pgrp != NULL) {
487 PGRP_LOCK(tp->t_pgrp);
489 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
490 PGRP_UNLOCK(tp->t_pgrp);
494 if (CCEQ(cc[VSUSP], c)) {
495 if (!ISSET(lflag, NOFLSH))
498 if (tp->t_pgrp != NULL) {
499 PGRP_LOCK(tp->t_pgrp);
500 pgsignal(tp->t_pgrp, SIGTSTP, 1);
501 PGRP_UNLOCK(tp->t_pgrp);
507 * Handle start/stop characters.
509 if (ISSET(iflag, IXON)) {
510 if (CCEQ(cc[VSTOP], c)) {
511 if (!ISSET(tp->t_state, TS_TTSTOP)) {
512 SET(tp->t_state, TS_TTSTOP);
513 (*tp->t_stop)(tp, 0);
516 if (!CCEQ(cc[VSTART], c))
519 * if VSTART == VSTOP then toggle
523 if (CCEQ(cc[VSTART], c))
527 * IGNCR, ICRNL, & INLCR
530 if (ISSET(iflag, IGNCR))
532 else if (ISSET(iflag, ICRNL))
534 } else if (c == '\n' && ISSET(iflag, INLCR))
537 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
539 * From here on down canonical mode character
540 * processing takes place.
543 * erase or erase2 (^H / ^?)
545 if (CCEQ(cc[VERASE], c) || CCEQ(cc[VERASE2], c) ) {
547 ttyrub(unputc(&tp->t_rawq), tp);
553 if (CCEQ(cc[VKILL], c)) {
554 if (ISSET(lflag, ECHOKE) &&
555 tp->t_rawq.c_cc == tp->t_rocount &&
556 !ISSET(lflag, ECHOPRT))
557 while (tp->t_rawq.c_cc)
558 ttyrub(unputc(&tp->t_rawq), tp);
561 if (ISSET(lflag, ECHOK) ||
562 ISSET(lflag, ECHOKE))
567 CLR(tp->t_state, TS_LOCAL);
573 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) {
579 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
584 * erase last char of word and remember the
585 * next chars type (for ALTWERASE)
588 c = unputc(&tp->t_rawq);
591 if (c == ' ' || c == '\t') {
592 (void)putc(c, &tp->t_rawq);
601 c = unputc(&tp->t_rawq);
604 } while (c != ' ' && c != '\t' &&
605 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype));
606 (void)putc(c, &tp->t_rawq);
612 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) {
617 * ^T - kernel info and generate SIGINFO
619 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) {
620 if (ISSET(lflag, ISIG) && tp->t_pgrp != NULL) {
621 PGRP_LOCK(tp->t_pgrp);
622 pgsignal(tp->t_pgrp, SIGINFO, 1);
623 PGRP_UNLOCK(tp->t_pgrp);
625 if (!ISSET(lflag, NOKERNINFO))
631 * Check for input buffer overflow
633 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) {
635 if (ISSET(iflag, IMAXBEL)) {
636 if (tp->t_outq.c_cc < tp->t_ohiwat)
637 (void)ttyoutput(CTRL('g'), tp);
642 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP)
643 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR))
644 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
647 * Put data char in q for user and
648 * wakeup on seeing a line delimiter.
650 if (putc(c, &tp->t_rawq) >= 0) {
651 if (!ISSET(lflag, ICANON)) {
656 if (TTBREAKC(c, lflag)) {
658 catq(&tp->t_rawq, &tp->t_canq);
660 } else if (tp->t_rocount++ == 0)
661 tp->t_rocol = tp->t_column;
662 if (ISSET(tp->t_state, TS_ERASE)) {
664 * end of prterase \.../
666 CLR(tp->t_state, TS_ERASE);
667 (void)ttyoutput('/', tp);
671 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
673 * Place the cursor over the '^' of the ^D.
675 i = imin(2, tp->t_column - i);
677 (void)ttyoutput('\b', tp);
684 * IXANY means allow any character to restart output.
686 if (ISSET(tp->t_state, TS_TTSTOP) &&
687 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
690 CLR(tp->t_lflag, FLUSHO);
691 CLR(tp->t_state, TS_TTSTOP);
693 return (ttstart(tp));
697 * Output a single character on a tty, doing output processing
698 * as needed (expanding tabs, newline processing, etc.).
699 * Returns < 0 if succeeds, otherwise returns char to resend.
703 ttyoutput(int c, struct tty *tp)
709 if (!ISSET(oflag, OPOST)) {
710 if (ISSET(tp->t_lflag, FLUSHO))
712 if (putc(c, &tp->t_outq))
719 * Do tab expansion if OXTABS is set. Special case if we external
720 * processing, we don't do the tab expansion because we'll probably
721 * get it wrong. If tab expansion needs to be done, let it happen
724 CLR(c, ~TTY_CHARMASK);
726 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
727 c = 8 - (tp->t_column & 7);
728 if (!ISSET(tp->t_lflag, FLUSHO)) {
729 s = spltty(); /* Don't interrupt tabs. */
730 c -= b_to_q(" ", c, &tp->t_outq);
736 return (c ? -1 : '\t');
738 if (c == CEOT && ISSET(oflag, ONOEOT))
742 * Newline translation: if ONLCR is set,
743 * translate newline into "\r\n".
745 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
748 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
751 /* If OCRNL is set, translate "\r" into "\n". */
752 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
754 /* If ONOCR is set, don't transmit CRs when on column 0. */
755 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
760 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
772 if (ISSET(tp->t_oflag, ONLCR | ONLRET))
782 col = (col + 8) & ~7;
790 * Ioctls for all tty devices. Called after line-discipline specific ioctl
791 * has been called to do discipline-specific functions and/or reject any
792 * of these ioctl commands.
796 ttioctl(struct tty *tp, u_long cmd, void *data, int flag)
801 int s, error, bits, sig, sig2;
803 td = curthread; /* XXX */
806 /* If the ioctl involves modification, hang if in the background. */
831 #if defined(COMPAT_43)
842 sx_slock(&proctree_lock);
844 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) &&
845 !SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTOU) &&
846 !SIGISMEMBER(td->td_sigmask, SIGTTOU)) {
849 if (pgrp->pg_jobc == 0) {
850 sx_sunlock(&proctree_lock);
854 sx_sunlock(&proctree_lock);
855 pgsignal(pgrp, SIGTTOU, 1);
857 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1",
861 sx_slock(&proctree_lock);
865 sx_sunlock(&proctree_lock);
869 if (tp->t_break != NULL) {
882 if (tp->t_modem != NULL) {
885 tp->t_modem(tp, SER_DTR, 0);
888 tp->t_modem(tp, 0, SER_DTR);
892 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1;
893 sig2 = ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1;
894 tp->t_modem(tp, sig, sig2);
898 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1;
899 tp->t_modem(tp, sig, 0);
903 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1;
904 tp->t_modem(tp, 0, sig);
907 sig = tp->t_modem(tp, 0, 0);
908 /* See <sys/serial.h. for the "<< 1" stuff */
909 bits = TIOCM_LE + (sig << 1);
917 if (tp->t_pps != NULL) {
918 error = pps_ioctl(cmd, data, tp->t_pps);
919 if (error != ENOIOCTL)
923 switch (cmd) { /* Process the ioctl. */
924 case FIOASYNC: /* set/clear async i/o */
927 SET(tp->t_state, TS_ASYNC);
929 CLR(tp->t_state, TS_ASYNC);
932 case FIONBIO: /* set/clear non-blocking i/o */
933 break; /* XXX: delete. */
934 case FIONREAD: /* get # bytes to read */
936 *(int *)data = ttnread(tp);
942 * Policy -- Don't allow FIOSETOWN on someone else's
945 if (tp->t_session != NULL && !isctty(p, tp))
948 error = fsetown(*(int *)data, &tp->t_sigio);
953 if (tp->t_session != NULL && !isctty(p, tp))
955 *(int *)data = fgetown(&tp->t_sigio);
958 case TIOCEXCL: /* set exclusive use of tty */
960 SET(tp->t_state, TS_XCLUDE);
963 case TIOCFLUSH: { /* flush buffers */
964 int flags = *(int *)data;
967 flags = FREAD | FWRITE;
969 flags &= FREAD | FWRITE;
973 case TIOCCONS: /* become virtual console */
975 struct nameidata nid;
977 if (constty && constty != tp &&
978 ISSET(constty->t_state, TS_CONNECTED))
981 /* Ensure user can open the real console. */
982 NDINIT(&nid, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE,
984 if ((error = namei(&nid)) != 0)
986 NDFREE(&nid, NDF_ONLY_PNBUF);
987 error = VOP_ACCESS(nid.ni_vp, VREAD, td->td_ucred, td);
993 } else if (tp == constty)
996 case TIOCDRAIN: /* wait till output drained */
1001 case TIOCGETA: { /* get termios struct */
1002 struct termios *t = (struct termios *)data;
1004 bcopy(&tp->t_termios, t, sizeof(struct termios));
1007 case TIOCGETD: /* get line discipline */
1008 *(int *)data = tp->t_line;
1010 case TIOCGWINSZ: /* get window size */
1011 *(struct winsize *)data = tp->t_winsize;
1013 case TIOCGPGRP: /* get pgrp of tty */
1016 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1019 case TIOCHPCL: /* hang up on last close */
1021 SET(tp->t_cflag, HUPCL);
1026 *(int *)data = tp->t_dtr_wait * 100 / hz;
1029 /* must be root since the wait applies to following logins */
1033 tp->t_dtr_wait = *(int *)data * hz / 100;
1035 case TIOCNXCL: /* reset exclusive use of tty */
1037 CLR(tp->t_state, TS_XCLUDE);
1040 case TIOCOUTQ: /* output queue size */
1041 *(int *)data = tp->t_outq.c_cc;
1043 case TIOCSETA: /* set termios struct */
1044 case TIOCSETAW: /* drain output, set */
1045 case TIOCSETAF: { /* drn out, fls in, set */
1046 struct termios *t = (struct termios *)data;
1048 if (t->c_ispeed == 0)
1049 t->c_ispeed = t->c_ospeed;
1050 if (t->c_ispeed == 0)
1051 t->c_ispeed = tp->t_ospeed;
1052 if (t->c_ispeed == 0)
1055 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1056 error = ttywait(tp);
1061 if (cmd == TIOCSETAF)
1062 ttyflush(tp, FREAD);
1064 if (!ISSET(t->c_cflag, CIGNORE)) {
1066 * Set device hardware.
1068 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
1072 if (ISSET(t->c_cflag, CLOCAL) &&
1073 !ISSET(tp->t_cflag, CLOCAL)) {
1075 * XXX disconnections would be too hard to
1076 * get rid of without this kludge. The only
1077 * way to get rid of controlling terminals
1078 * is to exit from the session leader.
1080 CLR(tp->t_state, TS_ZOMBIE);
1082 wakeup(TSA_CARR_ON(tp));
1086 if ((ISSET(tp->t_state, TS_CARR_ON) ||
1087 ISSET(t->c_cflag, CLOCAL)) &&
1088 !ISSET(tp->t_state, TS_ZOMBIE))
1089 SET(tp->t_state, TS_CONNECTED);
1091 CLR(tp->t_state, TS_CONNECTED);
1092 tp->t_cflag = t->c_cflag;
1093 tp->t_ispeed = t->c_ispeed;
1094 if (t->c_ospeed != 0)
1095 tp->t_ospeed = t->c_ospeed;
1098 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) &&
1100 if (ISSET(t->c_lflag, ICANON))
1101 SET(tp->t_lflag, PENDIN);
1104 * XXX we really shouldn't allow toggling
1105 * ICANON while we're in a non-termios line
1106 * discipline. Now we have to worry about
1107 * panicing for a null queue.
1109 if (tp->t_canq.c_cbreserved > 0 &&
1110 tp->t_rawq.c_cbreserved > 0) {
1111 catq(&tp->t_rawq, &tp->t_canq);
1113 * XXX the queue limits may be
1114 * different, so the old queue
1115 * swapping method no longer works.
1117 catq(&tp->t_canq, &tp->t_rawq);
1119 CLR(tp->t_lflag, PENDIN);
1123 tp->t_iflag = t->c_iflag;
1124 tp->t_oflag = t->c_oflag;
1126 * Make the EXTPROC bit read only.
1128 if (ISSET(tp->t_lflag, EXTPROC))
1129 SET(t->c_lflag, EXTPROC);
1131 CLR(t->c_lflag, EXTPROC);
1132 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
1133 if (t->c_cc[VMIN] != tp->t_cc[VMIN] ||
1134 t->c_cc[VTIME] != tp->t_cc[VTIME])
1136 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
1140 case TIOCSETD: { /* set line discipline */
1141 int t = *(int *)data;
1143 if ((u_int)t >= nlinesw)
1145 if (t == tp->t_line)
1148 ttyld_close(tp, flag);
1150 /* XXX: we should use the correct cdev here */
1151 error = ttyld_open(tp, tp->t_dev);
1154 * If we fail to switch line discipline we cannot
1155 * fall back to the previous, because we can not
1156 * trust that ldisc to open successfully either.
1157 * Fall back to the default ldisc which we know
1158 * will allways succeed.
1160 tp->t_line = TTYDISC;
1161 (void)ttyld_open(tp, tp->t_dev);
1167 case TIOCSTART: /* start output, like ^Q */
1169 if (ISSET(tp->t_state, TS_TTSTOP) ||
1170 ISSET(tp->t_lflag, FLUSHO)) {
1171 CLR(tp->t_lflag, FLUSHO);
1172 CLR(tp->t_state, TS_TTSTOP);
1177 case TIOCSTI: /* simulate terminal input */
1178 if ((flag & FREAD) == 0 && suser(td))
1180 if (!isctty(p, tp) && suser(td))
1183 ttyld_rint(tp, *(u_char *)data);
1186 case TIOCSTOP: /* stop output, like ^S */
1188 if (!ISSET(tp->t_state, TS_TTSTOP)) {
1189 SET(tp->t_state, TS_TTSTOP);
1190 (*tp->t_stop)(tp, 0);
1194 case TIOCSCTTY: /* become controlling tty */
1195 /* Session ctty vnode pointer set in vnode layer. */
1196 sx_slock(&proctree_lock);
1197 if (!SESS_LEADER(p) ||
1198 ((p->p_session->s_ttyvp || tp->t_session) &&
1199 (tp->t_session != p->p_session))) {
1200 sx_sunlock(&proctree_lock);
1203 tp->t_session = p->p_session;
1204 tp->t_pgrp = p->p_pgrp;
1205 SESS_LOCK(p->p_session);
1206 ttyref(tp); /* ttyrel(): kern_proc.c:pgdelete() */
1207 p->p_session->s_ttyp = tp;
1208 SESS_UNLOCK(p->p_session);
1210 p->p_flag |= P_CONTROLT;
1212 sx_sunlock(&proctree_lock);
1214 case TIOCSPGRP: { /* set pgrp of tty */
1215 sx_slock(&proctree_lock);
1216 pgrp = pgfind(*(int *)data);
1217 if (!isctty(p, tp)) {
1220 sx_sunlock(&proctree_lock);
1224 sx_sunlock(&proctree_lock);
1228 if (pgrp->pg_session != p->p_session) {
1229 sx_sunlock(&proctree_lock);
1232 sx_sunlock(&proctree_lock);
1236 case TIOCSTAT: /* simulate control-T */
1241 case TIOCSWINSZ: /* set window size */
1242 if (bcmp((caddr_t)&tp->t_winsize, data,
1243 sizeof (struct winsize))) {
1244 tp->t_winsize = *(struct winsize *)data;
1245 if (tp->t_pgrp != NULL) {
1246 PGRP_LOCK(tp->t_pgrp);
1247 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1248 PGRP_UNLOCK(tp->t_pgrp);
1252 case TIOCSDRAINWAIT:
1256 tp->t_timeout = *(int *)data * hz;
1257 wakeup(TSA_OCOMPLETE(tp));
1258 wakeup(TSA_OLOWAT(tp));
1260 case TIOCGDRAINWAIT:
1261 *(int *)data = tp->t_timeout / hz;
1264 #if defined(COMPAT_43)
1265 #ifndef BURN_BRIDGES
1266 return (ttcompat(tp, cmd, data, flag));
1278 ttypoll(struct cdev *dev, int events, struct thread *td)
1284 tp = tty_gettp(dev);
1286 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */
1287 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM))
1291 if (events & (POLLIN | POLLRDNORM)) {
1292 if (ISSET(tp->t_state, TS_ZOMBIE))
1293 revents |= (events & (POLLIN | POLLRDNORM)) |
1295 else if (ttnread(tp) > 0)
1296 revents |= events & (POLLIN | POLLRDNORM);
1298 selrecord(td, &tp->t_rsel);
1300 if (events & POLLOUT) {
1301 if (ISSET(tp->t_state, TS_ZOMBIE))
1303 else if (tp->t_outq.c_cc <= tp->t_olowat &&
1304 ISSET(tp->t_state, TS_CONNECTED))
1305 revents |= events & POLLOUT;
1307 selrecord(td, &tp->t_wsel);
1313 static struct filterops ttyread_filtops =
1314 { 1, NULL, filt_ttyrdetach, filt_ttyread };
1315 static struct filterops ttywrite_filtops =
1316 { 1, NULL, filt_ttywdetach, filt_ttywrite };
1319 ttykqfilter(struct cdev *dev, struct knote *kn)
1322 struct knlist *klist;
1325 tp = tty_gettp(dev);
1327 switch (kn->kn_filter) {
1329 klist = &tp->t_rsel.si_note;
1330 kn->kn_fop = &ttyread_filtops;
1333 klist = &tp->t_wsel.si_note;
1334 kn->kn_fop = &ttywrite_filtops;
1340 kn->kn_hook = (caddr_t)dev;
1343 knlist_add(klist, kn, 0);
1350 filt_ttyrdetach(struct knote *kn)
1352 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty;
1355 knlist_remove(&tp->t_rsel.si_note, kn, 0);
1360 filt_ttyread(struct knote *kn, long hint)
1362 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty;
1364 kn->kn_data = ttnread(tp);
1365 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1366 kn->kn_flags |= EV_EOF;
1369 return (kn->kn_data > 0);
1373 filt_ttywdetach(struct knote *kn)
1375 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty;
1378 knlist_remove(&tp->t_wsel.si_note, kn, 0);
1383 filt_ttywrite(struct knote *kn, long hint)
1385 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty;
1387 kn->kn_data = tp->t_outq.c_cc;
1388 if (ISSET(tp->t_state, TS_ZOMBIE))
1390 return (kn->kn_data <= tp->t_olowat &&
1391 ISSET(tp->t_state, TS_CONNECTED));
1395 * Must be called at spltty().
1398 ttnread(struct tty *tp)
1402 if (ISSET(tp->t_lflag, PENDIN))
1404 nread = tp->t_canq.c_cc;
1405 if (!ISSET(tp->t_lflag, ICANON)) {
1406 nread += tp->t_rawq.c_cc;
1407 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0)
1414 * Wait for output to drain.
1417 ttywait(struct tty *tp)
1423 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1424 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) {
1426 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1427 ISSET(tp->t_state, TS_CONNECTED)) {
1428 SET(tp->t_state, TS_SO_OCOMPLETE);
1429 error = ttysleep(tp, TSA_OCOMPLETE(tp),
1430 TTOPRI | PCATCH, "ttywai",
1433 if (error == EWOULDBLOCK)
1440 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)))
1447 * Flush if successfully wait.
1450 ttywflush(struct tty *tp)
1454 if ((error = ttywait(tp)) == 0)
1455 ttyflush(tp, FREAD);
1460 * Flush tty read and/or write queues, notifying anyone waiting.
1463 ttyflush(struct tty *tp, int rw)
1472 FLUSHQ(&tp->t_outq);
1473 CLR(tp->t_state, TS_TTSTOP);
1475 (*tp->t_stop)(tp, rw);
1477 FLUSHQ(&tp->t_canq);
1478 FLUSHQ(&tp->t_rawq);
1479 CLR(tp->t_lflag, PENDIN);
1482 CLR(tp->t_state, TS_LOCAL);
1484 if (ISSET(tp->t_state, TS_TBLOCK)) {
1486 FLUSHQ(&tp->t_outq);
1490 * Don't let leave any state that might clobber the
1491 * next line discipline (although we should do more
1492 * to send the START char). Not clearing the state
1493 * may have caused the "putc to a clist with no
1494 * reserved cblocks" panic/printf.
1496 CLR(tp->t_state, TS_TBLOCK);
1498 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1499 if (ISSET(tp->t_iflag, IXOFF)) {
1501 * XXX wait a bit in the hope that the stop
1502 * character (if any) will go out. Waiting
1503 * isn't good since it allows races. This
1504 * will be fixed when the stop character is
1505 * put in a special queue. Don't bother with
1506 * the checks in ttywait() since the timeout
1509 SET(tp->t_state, TS_SO_OCOMPLETE);
1510 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI,
1513 * Don't try sending the stop character again.
1515 CLR(tp->t_state, TS_TBLOCK);
1522 FLUSHQ(&tp->t_outq);
1529 * Copy in the default termios characters.
1532 termioschars(struct termios *t)
1535 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc);
1542 ttychars(struct tty *tp)
1545 termioschars(&tp->t_termios);
1549 * Handle input high water. Send stop character for the IXOFF case. Turn
1550 * on our input flow control bit and propagate the changes to the driver.
1551 * XXX the stop character should be put in a special high priority queue.
1554 ttyblock(struct tty *tp)
1557 SET(tp->t_state, TS_TBLOCK);
1558 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1559 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0)
1560 CLR(tp->t_state, TS_TBLOCK); /* try again later */
1565 * Handle input low water. Send start character for the IXOFF case. Turn
1566 * off our input flow control bit and propagate the changes to the driver.
1567 * XXX the start character should be put in a special high priority queue.
1570 ttyunblock(struct tty *tp)
1573 CLR(tp->t_state, TS_TBLOCK);
1574 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE &&
1575 putc(tp->t_cc[VSTART], &tp->t_outq) != 0)
1576 SET(tp->t_state, TS_TBLOCK); /* try again later */
1581 /* Not used by any current (i386) drivers. */
1583 * Restart after an inter-char delay.
1586 ttrstrt(void *tp_arg)
1591 KASSERT(tp_arg != NULL, ("ttrstrt"));
1596 CLR(tp->t_state, TS_TIMEOUT);
1604 ttstart(struct tty *tp)
1607 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1613 * "close" a line discipline
1616 ttylclose(struct tty *tp, int flag)
1619 if (flag & FNONBLOCK || ttywflush(tp))
1620 ttyflush(tp, FREAD | FWRITE);
1625 * Handle modem control transition on a tty.
1626 * Flag indicates new state of carrier.
1627 * Returns 0 if the line should be turned off, otherwise 1.
1630 ttymodem(struct tty *tp, int flag)
1633 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) {
1635 * MDMBUF: do flow control according to carrier flag
1636 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1637 * works if IXON and IXANY are clear.
1640 CLR(tp->t_state, TS_CAR_OFLOW);
1641 CLR(tp->t_state, TS_TTSTOP);
1643 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) {
1644 SET(tp->t_state, TS_CAR_OFLOW);
1645 SET(tp->t_state, TS_TTSTOP);
1646 (*tp->t_stop)(tp, 0);
1648 } else if (flag == 0) {
1652 CLR(tp->t_state, TS_CARR_ON);
1653 if (ISSET(tp->t_state, TS_ISOPEN) &&
1654 !ISSET(tp->t_cflag, CLOCAL)) {
1655 SET(tp->t_state, TS_ZOMBIE);
1656 CLR(tp->t_state, TS_CONNECTED);
1657 if (tp->t_session) {
1658 sx_slock(&proctree_lock);
1659 if (tp->t_session->s_leader) {
1662 p = tp->t_session->s_leader;
1667 sx_sunlock(&proctree_lock);
1669 ttyflush(tp, FREAD | FWRITE);
1676 SET(tp->t_state, TS_CARR_ON);
1677 if (!ISSET(tp->t_state, TS_ZOMBIE))
1678 SET(tp->t_state, TS_CONNECTED);
1679 wakeup(TSA_CARR_ON(tp));
1687 * Reinput pending characters after state switch
1691 ttypend(struct tty *tp)
1696 CLR(tp->t_lflag, PENDIN);
1697 SET(tp->t_state, TS_TYPEN);
1699 * XXX this assumes too much about clist internals. It may even
1700 * fail if the cblock slush pool is empty. We can't allocate more
1701 * cblocks here because we are called from an interrupt handler
1702 * and clist_alloc_cblocks() can wait.
1705 bzero(&tp->t_rawq, sizeof tp->t_rawq);
1706 tp->t_rawq.c_cbmax = tq.c_cbmax;
1707 tp->t_rawq.c_cbreserved = tq.c_cbreserved;
1708 while ((c = getc(&tq)) >= 0)
1710 CLR(tp->t_state, TS_TYPEN);
1714 * Process a read call on a tty device.
1717 ttread(struct tty *tp, struct uio *uio, int flag)
1722 cc_t *cc = tp->t_cc;
1725 int s, first, error = 0;
1726 int has_stime = 0, last_cc = 0;
1727 long slp = 0; /* XXX this should be renamed `timo'. */
1728 struct timeval stime;
1735 lflag = tp->t_lflag;
1737 * take pending input first
1739 if (ISSET(lflag, PENDIN)) {
1741 splx(s); /* reduce latency */
1743 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */
1747 * Hang process if it's in the background.
1749 if (isbackground(p, tp)) {
1751 sx_slock(&proctree_lock);
1753 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTIN) ||
1754 SIGISMEMBER(td->td_sigmask, SIGTTIN) ||
1755 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0) {
1757 sx_sunlock(&proctree_lock);
1763 sx_sunlock(&proctree_lock);
1764 pgsignal(pg, SIGTTIN, 1);
1766 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0);
1772 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1774 return (0); /* EOF */
1778 * If canonical, use the canonical queue,
1779 * else use the raw queue.
1781 * (should get rid of clists...)
1783 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq;
1785 if (flag & IO_NDELAY) {
1788 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) {
1793 return (EWOULDBLOCK);
1795 if (!ISSET(lflag, ICANON)) {
1798 struct timeval timecopy;
1801 * Check each of the four combinations.
1802 * (m > 0 && t == 0) is the normal read case.
1803 * It should be fairly efficient, so we check that and its
1804 * companion case (m == 0 && t == 0) first.
1805 * For the other two cases, we compute the target sleep time
1814 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1818 t *= 100000; /* time in us */
1819 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1820 ((t1).tv_usec - (t2).tv_usec))
1826 getmicrotime(&timecopy);
1828 /* first character, start timer */
1832 } else if (qp->c_cc > last_cc) {
1833 /* got a character, restart timer */
1837 /* nothing, check expiration */
1838 slp = t - diff(timecopy, stime);
1843 } else { /* m == 0 */
1846 getmicrotime(&timecopy);
1852 slp = t - diff(timecopy, stime);
1854 /* Timed out, but 0 is enough input. */
1862 struct timeval tv; /* XXX style bug. */
1864 tv.tv_sec = slp / 1000000;
1865 tv.tv_usec = slp % 1000000;
1868 * XXX bad variable names. slp was the timeout in
1869 * usec. Now it is the timeout in ticks.
1874 if (qp->c_cc <= 0) {
1877 * There is no input, or not enough input and we can block.
1879 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH,
1880 ISSET(tp->t_state, TS_CONNECTED) ?
1881 "ttyin" : "ttyhup", (int)slp);
1883 if (error == EWOULDBLOCK)
1888 * XXX what happens if another process eats some input
1889 * while we are asleep (not just here)? It would be
1890 * safest to detect changes and reset our state variables
1891 * (has_stime and last_cc).
1899 * Input present, check for input mapping and processing.
1902 if (ISSET(lflag, ICANON | ISIG))
1908 icc = imin(uio->uio_resid, IBUFSIZ);
1909 icc = q_to_b(qp, ibuf, icc);
1915 error = uiomove(ibuf, icc, uio);
1917 * XXX if there was an error then we should ungetc() the
1918 * unmoved chars and reduce icc here.
1922 if (uio->uio_resid == 0)
1936 * delayed suspend (^Y)
1938 if (CCEQ(cc[VDSUSP], c) &&
1939 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) {
1940 if (tp->t_pgrp != NULL) {
1941 PGRP_LOCK(tp->t_pgrp);
1942 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1943 PGRP_UNLOCK(tp->t_pgrp);
1946 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH,
1955 * Interpret EOF only in canonical mode.
1957 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1960 * Give user character.
1962 error = ureadc(c, uio);
1964 /* XXX should ungetc(c, qp). */
1966 if (uio->uio_resid == 0)
1969 * In canonical mode check for a "break character"
1970 * marking the end of a "line of input".
1972 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1979 * Look to unblock input now that (presumably)
1980 * the input queue has gone down.
1983 if (ISSET(tp->t_state, TS_TBLOCK) &&
1984 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat)
1992 * Check the output queue on tp for space for a kernel message (from uprintf
1993 * or tprintf). Allow some space over the normal hiwater mark so we don't
1994 * lose messages due to normal flow control, but don't let the tty run amok.
1995 * Sleeps here are not interruptible, but we return prematurely if new signals
1999 ttycheckoutq(struct tty *tp, int wait)
2008 hiwat = tp->t_ohiwat;
2009 SIGEMPTYSET(oldmask);
2013 oldmask = td->td_siglist;
2016 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100)
2017 while (tp->t_outq.c_cc > hiwat) {
2019 if (tp->t_outq.c_cc <= hiwat)
2026 if (!SIGSETEQ(td->td_siglist, oldmask)) {
2032 SET(tp->t_state, TS_SO_OLOWAT);
2033 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz);
2040 * Process a write call on a tty device.
2043 ttwrite(struct tty *tp, struct uio *uio, int flag)
2049 int i, hiwat, cnt, error, s;
2052 hiwat = tp->t_ohiwat;
2053 cnt = uio->uio_resid;
2060 if (ISSET(tp->t_state, TS_ZOMBIE)) {
2062 if (uio->uio_resid == cnt)
2066 if (!ISSET(tp->t_state, TS_CONNECTED)) {
2067 if (flag & IO_NDELAY) {
2069 error = EWOULDBLOCK;
2072 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
2081 * Hang the process if it's in the background.
2083 sx_slock(&proctree_lock);
2085 if (isbackground(p, tp) &&
2086 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) &&
2087 !SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTOU) &&
2088 !SIGISMEMBER(td->td_sigmask, SIGTTOU)) {
2089 if (p->p_pgrp->pg_jobc == 0) {
2091 sx_sunlock(&proctree_lock);
2096 PGRP_LOCK(p->p_pgrp);
2097 sx_sunlock(&proctree_lock);
2098 pgsignal(p->p_pgrp, SIGTTOU, 1);
2099 PGRP_UNLOCK(p->p_pgrp);
2100 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0);
2106 sx_sunlock(&proctree_lock);
2109 * Process the user's data in at most OBUFSIZ chunks. Perform any
2110 * output translation. Keep track of high water mark, sleep on
2111 * overflow awaiting device aid in acquiring new space.
2113 while (uio->uio_resid > 0 || cc > 0) {
2114 if (ISSET(tp->t_lflag, FLUSHO)) {
2118 if (tp->t_outq.c_cc > hiwat)
2121 * Grab a hunk of data from the user, unless we have some
2122 * leftover from last time.
2125 cc = imin(uio->uio_resid, OBUFSIZ);
2127 error = uiomove(cp, cc, uio);
2134 * If nothing fancy need be done, grab those characters we
2135 * can handle without any of ttyoutput's processing and
2136 * just transfer them to the output q. For those chars
2137 * which require special processing (as indicated by the
2138 * bits in char_type), call ttyoutput. After processing
2139 * a hunk of data, look for FLUSHO so ^O's will take effect
2143 if (!ISSET(tp->t_oflag, OPOST))
2146 ce = cc - scanc((u_int)cc, (u_char *)cp,
2147 char_type, CCLASSMASK);
2149 * If ce is zero, then we're processing
2150 * a special character through ttyoutput.
2154 if (ttyoutput(*cp, tp) >= 0) {
2155 /* No Clists, wait a bit. */
2157 if (flag & IO_NDELAY) {
2158 error = EWOULDBLOCK;
2161 error = ttysleep(tp, &lbolt,
2170 if (ISSET(tp->t_lflag, FLUSHO) ||
2171 tp->t_outq.c_cc > hiwat)
2177 * A bunch of normal characters have been found.
2178 * Transfer them en masse to the output queue and
2179 * continue processing at the top of the loop.
2180 * If there are any further characters in this
2181 * <= OBUFSIZ chunk, the first should be a character
2182 * requiring special handling by ttyoutput.
2185 i = b_to_q(cp, ce, &tp->t_outq);
2188 cp += ce, cc -= ce, tk_nout += ce;
2191 /* No Clists, wait a bit. */
2193 if (flag & IO_NDELAY) {
2194 error = EWOULDBLOCK;
2197 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH,
2203 if (ISSET(tp->t_lflag, FLUSHO) ||
2204 tp->t_outq.c_cc > hiwat)
2211 * If cc is nonzero, we leave the uio structure inconsistent, as the
2212 * offset and iov pointers have moved forward, but it doesn't matter
2213 * (the call will either return short or restart with a new uio).
2215 uio->uio_resid += cc;
2222 * This can only occur if FLUSHO is set in t_lflag,
2223 * or if ttstart/oproc is synchronous (or very fast).
2225 if (tp->t_outq.c_cc <= hiwat) {
2229 if (flag & IO_NDELAY) {
2231 uio->uio_resid += cc;
2232 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
2234 SET(tp->t_state, TS_SO_OLOWAT);
2235 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri",
2238 if (error == EWOULDBLOCK)
2246 * Rubout one character from the rawq of tp
2247 * as cleanly as possible.
2250 ttyrub(int c, struct tty *tp)
2256 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
2258 CLR(tp->t_lflag, FLUSHO);
2259 if (ISSET(tp->t_lflag, ECHOE)) {
2260 if (tp->t_rocount == 0) {
2262 * Screwed by ttwrite; retype
2267 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
2270 CLR(c, ~TTY_CHARMASK);
2271 switch (CCLASS(c)) {
2280 if (ISSET(tp->t_lflag, ECHOCTL))
2284 if (tp->t_rocount < tp->t_rawq.c_cc) {
2289 savecol = tp->t_column;
2290 SET(tp->t_state, TS_CNTTB);
2291 SET(tp->t_lflag, FLUSHO);
2292 tp->t_column = tp->t_rocol;
2293 cp = tp->t_rawq.c_cf;
2295 tabc = *cp; /* XXX FIX NEXTC */
2296 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc))
2298 CLR(tp->t_lflag, FLUSHO);
2299 CLR(tp->t_state, TS_CNTTB);
2302 /* savecol will now be length of the tab. */
2303 savecol -= tp->t_column;
2304 tp->t_column += savecol;
2306 savecol = 8; /* overflow screw */
2307 while (--savecol >= 0)
2308 (void)ttyoutput('\b', tp);
2311 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2312 (void)printf(PANICSTR, c, CCLASS(c));
2314 panic(PANICSTR, c, CCLASS(c));
2318 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
2319 if (!ISSET(tp->t_state, TS_ERASE)) {
2320 SET(tp->t_state, TS_ERASE);
2321 (void)ttyoutput('\\', tp);
2325 ttyecho(tp->t_cc[VERASE], tp);
2327 * This code may be executed not only when an ERASE key
2328 * is pressed, but also when ^U (KILL) or ^W (WERASE) are.
2329 * So, I didn't think it was worthwhile to pass the extra
2330 * information (which would need an extra parameter,
2331 * changing every call) needed to distinguish the ERASE2
2332 * case from the ERASE.
2339 * Back over cnt characters, erasing them.
2342 ttyrubo(struct tty *tp, int cnt)
2346 (void)ttyoutput('\b', tp);
2347 (void)ttyoutput(' ', tp);
2348 (void)ttyoutput('\b', tp);
2354 * Reprint the rawq line. Note, it is assumed that c_cc has already
2358 ttyretype(struct tty *tp)
2363 /* Echo the reprint character. */
2364 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
2365 ttyecho(tp->t_cc[VREPRINT], tp);
2367 (void)ttyoutput('\n', tp);
2371 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2372 * BIT OF FIRST CHAR.
2375 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0);
2376 cp != NULL; cp = nextc(&tp->t_canq, cp, &c))
2378 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0);
2379 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c))
2381 CLR(tp->t_state, TS_ERASE);
2384 tp->t_rocount = tp->t_rawq.c_cc;
2389 * Echo a typed character to the terminal.
2392 ttyecho(int c, struct tty *tp)
2395 if (!ISSET(tp->t_state, TS_CNTTB))
2396 CLR(tp->t_lflag, FLUSHO);
2397 if ((!ISSET(tp->t_lflag, ECHO) &&
2398 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) ||
2399 ISSET(tp->t_lflag, EXTPROC))
2401 if (ISSET(tp->t_lflag, ECHOCTL) &&
2402 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') ||
2403 ISSET(c, TTY_CHARMASK) == 0177)) {
2404 (void)ttyoutput('^', tp);
2405 CLR(c, ~TTY_CHARMASK);
2411 (void)ttyoutput(c, tp);
2415 * Wake up any readers on a tty.
2418 ttwakeup(struct tty *tp)
2421 if (SEL_WAITING(&tp->t_rsel))
2422 selwakeuppri(&tp->t_rsel, TTIPRI);
2423 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2424 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
2425 wakeup(TSA_HUP_OR_INPUT(tp));
2426 KNOTE_UNLOCKED(&tp->t_rsel.si_note, 0);
2430 * Wake up any writers on a tty.
2433 ttwwakeup(struct tty *tp)
2436 if (SEL_WAITING(&tp->t_wsel) && tp->t_outq.c_cc <= tp->t_olowat)
2437 selwakeuppri(&tp->t_wsel, TTOPRI);
2438 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2439 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
2440 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
2441 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
2442 CLR(tp->t_state, TS_SO_OCOMPLETE);
2443 wakeup(TSA_OCOMPLETE(tp));
2445 if (ISSET(tp->t_state, TS_SO_OLOWAT) &&
2446 tp->t_outq.c_cc <= tp->t_olowat) {
2447 CLR(tp->t_state, TS_SO_OLOWAT);
2448 wakeup(TSA_OLOWAT(tp));
2450 KNOTE_UNLOCKED(&tp->t_wsel.si_note, 0);
2454 * Look up a code for a specified speed in a conversion table;
2455 * used by drivers to map software speed values to hardware parameters.
2458 ttspeedtab(int speed, struct speedtab *table)
2461 for ( ; table->sp_speed != -1; table++)
2462 if (table->sp_speed == speed)
2463 return (table->sp_code);
2468 * Set input and output watermarks and buffer sizes. For input, the
2469 * high watermark is about one second's worth of input above empty, the
2470 * low watermark is slightly below high water, and the buffer size is a
2471 * driver-dependent amount above high water. For output, the watermarks
2472 * are near the ends of the buffer, with about 1 second's worth of input
2473 * between them. All this only applies to the standard line discipline.
2476 ttsetwater(struct tty *tp)
2478 int cps, ttmaxhiwat, x;
2481 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512);
2482 switch (tp->t_ispeedwat) {
2484 cps = tp->t_ispeed / 10;
2488 * This case is for old drivers that don't know about
2489 * t_ispeedwat. Arrange for them to get the old buffer
2490 * sizes and watermarks.
2492 cps = TTYHOG - 2 * 256;
2493 tp->t_ififosize = 2 * 256;
2496 cps = tp->t_ispeedwat / 10;
2500 tp->t_ilowat = 7 * cps / 8;
2501 x = cps + tp->t_ififosize;
2502 clist_alloc_cblocks(&tp->t_rawq, x, x);
2505 switch (tp->t_ospeedwat) {
2507 cps = tp->t_ospeed / 10;
2508 ttmaxhiwat = 2 * TTMAXHIWAT;
2511 cps = tp->t_ospeed / 10;
2512 ttmaxhiwat = TTMAXHIWAT;
2515 cps = tp->t_ospeedwat / 10;
2516 ttmaxhiwat = 8 * TTMAXHIWAT;
2519 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2520 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
2522 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */
2523 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */
2524 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */
2526 clist_alloc_cblocks(&tp->t_outq, x, x);
2531 * Report on state of foreground process group.
2534 ttyinfo(struct tty *tp)
2536 struct timeval utime, stime;
2537 struct proc *p, *pick;
2539 const char *stateprefix, *state;
2543 if (ttycheckoutq(tp,0) == 0)
2546 /* Print load average. */
2547 load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
2548 ttyprintf(tp, "load: %d.%02d ", load / 100, load % 100);
2551 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2552 * that pending input will be retyped on BS.
2554 if (tp->t_session == NULL) {
2555 ttyprintf(tp, "not a controlling terminal\n");
2559 if (tp->t_pgrp == NULL) {
2560 ttyprintf(tp, "no foreground process group\n");
2564 sx_slock(&proctree_lock);
2565 if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == NULL) {
2566 sx_sunlock(&proctree_lock);
2567 ttyprintf(tp, "empty foreground process group\n");
2573 * Pick the most interesting process and copy some of its
2574 * state for printing later. sched_lock must be held for
2575 * most parts of this. Holding it throughout is simplest
2576 * and prevents even unimportant inconsistencies in the
2577 * copy of the state, but may increase interrupt latency
2580 mtx_lock_spin(&sched_lock);
2581 for (pick = NULL; p != NULL; p = LIST_NEXT(p, p_pglist))
2582 if (proc_compare(pick, p))
2585 td = FIRST_THREAD_IN_PROC(pick); /* XXXKSE */
2587 KASSERT(td != NULL, ("ttyinfo: no thread"));
2590 mtx_unlock_spin(&sched_lock);
2591 sx_sunlock(&proctree_lock);
2592 ttyprintf(tp, "foreground process without thread\n");
2598 if (TD_IS_RUNNING(td))
2600 else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
2602 else if (TD_IS_SLEEPING(td)) {
2603 /* XXX: If we're sleeping, are we ever not in a queue? */
2604 if (TD_ON_SLEEPQ(td))
2605 state = td->td_wmesg;
2607 state = "sleeping without queue";
2608 } else if (TD_ON_LOCK(td)) {
2609 state = td->td_lockname;
2611 } else if (TD_IS_SUSPENDED(td))
2612 state = "suspended";
2613 else if (TD_AWAITING_INTR(td))
2617 pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
2618 if (pick->p_state == PRS_NEW || pick->p_state == PRS_ZOMBIE)
2621 rss = pgtok(vmspace_resident_count(pick->p_vmspace));
2622 mtx_unlock_spin(&sched_lock);
2624 calcru(pick, &utime, &stime);
2627 /* Print command, pid, state, utime, stime, %cpu, and rss. */
2629 " cmd: %s %d [%s%s] %ld.%02ldu %ld.%02lds %d%% %ldk\n",
2630 pick->p_comm, pick->p_pid, stateprefix, state,
2631 (long)utime.tv_sec, utime.tv_usec / 10000,
2632 (long)stime.tv_sec, stime.tv_usec / 10000,
2635 sx_sunlock(&proctree_lock);
2639 * Returns 1 if p2 is "better" than p1
2641 * The algorithm for picking the "interesting" process is thus:
2643 * 1) Only foreground processes are eligible - implied.
2644 * 2) Runnable processes are favored over anything else. The runner
2645 * with the highest cpu utilization is picked (p_estcpu). Ties are
2646 * broken by picking the highest pid.
2647 * 3) The sleeper with the shortest sleep time is next. With ties,
2648 * we pick out just "short-term" sleepers (P_SINTR == 0).
2649 * 4) Further ties are broken by picking the highest pid.
2651 #define ISRUN(p, val) \
2653 struct thread *td; \
2655 FOREACH_THREAD_IN_PROC(p, td) { \
2656 if (TD_ON_RUNQ(td) || \
2657 TD_IS_RUNNING(td)) { \
2664 #define TESTAB(a, b) ((a)<<1 | (b))
2670 proc_compare(struct proc *p1, struct proc *p2)
2675 mtx_assert(&sched_lock, MA_OWNED);
2683 * see if at least one of them is runnable
2685 switch (TESTAB(esta, estb)) {
2692 * tie - favor one with highest recent cpu utilization
2695 FOREACH_KSEGRP_IN_PROC(p1,kg) {
2696 esta += kg->kg_estcpu;
2698 FOREACH_KSEGRP_IN_PROC(p2,kg) {
2699 estb += kg->kg_estcpu;
2705 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2710 switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) {
2716 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2721 * pick the one with the smallest sleep time
2723 if (p2->p_slptime > p1->p_slptime)
2725 if (p1->p_slptime > p2->p_slptime)
2728 * favor one sleeping in a non-interruptible sleep
2730 if (p1->p_sflag & PS_SINTR && (p2->p_sflag & PS_SINTR) == 0)
2732 if (p2->p_sflag & PS_SINTR && (p1->p_sflag & PS_SINTR) == 0)
2735 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2739 * Output char to tty; console putchar style.
2742 tputchar(int c, struct tty *tp)
2747 if (!ISSET(tp->t_state, TS_CONNECTED)) {
2752 (void)ttyoutput('\r', tp);
2753 (void)ttyoutput(c, tp);
2760 * Sleep on chan, returning ERESTART if tty changed while we napped and
2761 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2762 * the tty is revoked, restarting a pending call will redo validation done
2763 * at the start of the call.
2766 ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo)
2772 error = tsleep(chan, pri, wmesg, timo);
2773 if (tp->t_state & TS_GONE)
2777 return (tp->t_gen == gen ? 0 : ERESTART);
2781 * Gain a reference to a TTY
2784 ttyref(struct tty *tp)
2788 mtx_lock(&tp->t_mtx);
2789 KASSERT(tp->t_refcnt > 0,
2790 ("ttyref(): tty refcnt is %d (%s)",
2791 tp->t_refcnt, tp->t_dev != NULL ? devtoname(tp->t_dev) : "??"));
2793 mtx_unlock(&tp->t_mtx);
2798 * Drop a reference to a TTY.
2799 * When reference count drops to zero, we free it.
2802 ttyrel(struct tty *tp)
2806 mtx_lock(&tty_list_mutex);
2807 mtx_lock(&tp->t_mtx);
2808 KASSERT(tp->t_refcnt > 0,
2809 ("ttyrel(): tty refcnt is %d (%s)",
2810 tp->t_refcnt, tp->t_dev != NULL ? devtoname(tp->t_dev) : "??"));
2813 mtx_unlock(&tp->t_mtx);
2814 mtx_unlock(&tty_list_mutex);
2817 TAILQ_REMOVE(&tty_list, tp, t_list);
2818 mtx_unlock(&tp->t_mtx);
2819 mtx_unlock(&tty_list_mutex);
2820 knlist_destroy(&tp->t_rsel.si_note);
2821 knlist_destroy(&tp->t_wsel.si_note);
2822 mtx_destroy(&tp->t_mtx);
2828 * Allocate a tty struct. Clists in the struct will be allocated by
2832 ttymalloc(struct tty *tp)
2837 mtx_init(&tty_list_mutex, "ttylist", NULL, MTX_DEF);
2843 * XXX: Either this argument should go away, or we should
2844 * XXX: require it and do a ttyrel(tp) here and allocate
2845 * XXX: a new tty. For now do nothing.
2849 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK | M_ZERO);
2850 mtx_init(&tp->t_mtx, "tty", NULL, MTX_DEF);
2853 * Set up the initial state
2857 tp->t_dtr_wait = 3 * hz;
2859 ttyinitmode(tp, 0, 0);
2860 bcopy(ttydefchars, tp->t_init_in.c_cc, sizeof tp->t_init_in.c_cc);
2862 /* Make callout the same as callin */
2863 tp->t_init_out = tp->t_init_in;
2865 mtx_lock(&tty_list_mutex);
2866 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
2867 mtx_unlock(&tty_list_mutex);
2868 knlist_init(&tp->t_rsel.si_note, &tp->t_mtx);
2869 knlist_init(&tp->t_wsel.si_note, &tp->t_mtx);
2877 return (ttymalloc(NULL));
2881 ttypurge(struct cdev *dev)
2884 if (dev->si_tty == NULL)
2886 ttygone(dev->si_tty);
2892 * Create the device entries for this tty thereby opening it for business.
2894 * The flags argument controls if "cua" units are created.
2896 * The t_sc filed is copied to si_drv1 in the created cdevs. This
2897 * is particularly important for ->t_cioctl() users.
2899 * XXX: implement the init and lock devices by cloning.
2903 ttycreate(struct tty *tp, struct cdevsw *csw, int unit, int flags, const char *fmt, ...)
2905 char namebuf[SPECNAMELEN - 3]; /* XXX space for "tty" */
2908 int i, minor, sminor, sunit;
2910 mtx_assert(&Giant, MA_OWNED);
2912 if (tty_unit == NULL)
2913 tty_unit = new_unrhdr(0, 0xffff, NULL);
2915 sunit = alloc_unr(tty_unit);
2916 tp->t_devunit = sunit;
2922 KASSERT(csw->d_purge == NULL || csw->d_purge == ttypurge,
2923 ("tty should not have d_purge"));
2925 csw->d_purge = ttypurge;
2927 minor = unit2minor(unit);
2928 sminor = unit2minor(sunit);
2930 i = vsnrprintf(namebuf, sizeof namebuf, 32, fmt, ap);
2932 KASSERT(i < sizeof namebuf, ("Too long tty name (%s)", namebuf));
2934 cp = make_dev(csw, minor,
2935 UID_ROOT, GID_WHEEL, 0600, "tty%s", namebuf);
2939 cp->si_drv1 = tp->t_sc;
2941 cp = make_dev(&ttys_cdevsw, sminor | MINOR_INIT,
2942 UID_ROOT, GID_WHEEL, 0600, "tty%s.init", namebuf);
2943 dev_depends(tp->t_dev, cp);
2944 cp->si_drv1 = tp->t_sc;
2945 cp->si_drv2 = &tp->t_init_in;
2948 cp = make_dev(&ttys_cdevsw, sminor | MINOR_LOCK,
2949 UID_ROOT, GID_WHEEL, 0600, "tty%s.lock", namebuf);
2950 dev_depends(tp->t_dev, cp);
2951 cp->si_drv1 = tp->t_sc;
2952 cp->si_drv2 = &tp->t_lock_in;
2955 if (flags & MINOR_CALLOUT) {
2956 cp = make_dev(csw, minor | MINOR_CALLOUT,
2957 UID_UUCP, GID_DIALER, 0660, "cua%s", namebuf);
2958 dev_depends(tp->t_dev, cp);
2959 cp->si_drv1 = tp->t_sc;
2962 cp = make_dev(&ttys_cdevsw, sminor | MINOR_CALLOUT | MINOR_INIT,
2963 UID_UUCP, GID_DIALER, 0660, "cua%s.init", namebuf);
2964 dev_depends(tp->t_dev, cp);
2965 cp->si_drv1 = tp->t_sc;
2966 cp->si_drv2 = &tp->t_init_out;
2969 cp = make_dev(&ttys_cdevsw, sminor | MINOR_CALLOUT | MINOR_LOCK,
2970 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", namebuf);
2971 dev_depends(tp->t_dev, cp);
2972 cp->si_drv1 = tp->t_sc;
2973 cp->si_drv2 = &tp->t_lock_out;
2981 * This function is called when the hardware disappears. We set a flag
2982 * and wake up stuff so all sleeping threads will notice.
2985 ttygone(struct tty *tp)
2988 tp->t_state |= TS_GONE;
2989 wakeup(&tp->t_dtr_wait);
2990 wakeup(TSA_CARR_ON(tp));
2991 wakeup(TSA_HUP_OR_INPUT(tp));
2992 wakeup(TSA_OCOMPLETE(tp));
2993 wakeup(TSA_OLOWAT(tp));
2994 if (tp->t_purge != NULL)
3001 * Called when the driver is ready to free the tty structure.
3003 * XXX: This shall sleep until all threads have left the driver.
3007 ttyfree(struct tty *tp)
3011 mtx_assert(&Giant, MA_OWNED);
3013 unit = tp->t_devunit;
3014 destroy_dev(tp->t_mdev);
3015 free_unr(tty_unit, unit);
3019 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
3021 struct tty *tp, *tp2;
3026 mtx_lock(&tty_list_mutex);
3027 tp = TAILQ_FIRST(&tty_list);
3030 mtx_unlock(&tty_list_mutex);
3031 while (tp != NULL) {
3032 bzero(&xt, sizeof xt);
3033 xt.xt_size = sizeof xt;
3034 #define XT_COPY(field) xt.xt_##field = tp->t_##field
3035 xt.xt_rawcc = tp->t_rawq.c_cc;
3036 xt.xt_cancc = tp->t_canq.c_cc;
3037 xt.xt_outcc = tp->t_outq.c_cc;
3039 if (tp->t_dev != NULL)
3040 xt.xt_dev = dev2udev(tp->t_dev);
3044 if (tp->t_pgrp != NULL)
3045 xt.xt_pgid = tp->t_pgrp->pg_id;
3046 if (tp->t_session != NULL)
3047 xt.xt_sid = tp->t_session->s_sid;
3061 error = SYSCTL_OUT(req, &xt, sizeof xt);
3066 mtx_lock(&tty_list_mutex);
3067 tp2 = TAILQ_NEXT(tp, t_list);
3070 mtx_unlock(&tty_list_mutex);
3077 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD,
3078 0, 0, sysctl_kern_ttys, "S,xtty", "All ttys");
3079 SYSCTL_LONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
3080 &tk_nin, 0, "Total TTY in characters");
3081 SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
3082 &tk_nout, 0, "Total TTY out characters");
3085 nottystop(struct tty *tp, int rw)
3092 ttyopen(struct cdev *dev, int flag, int mode, struct thread *td)
3101 * We jump to this label after all non-interrupted sleeps to pick
3102 * up any changes of the device state.
3105 if (tp->t_state & TS_GONE)
3107 error = ttydtrwaitsleep(tp);
3110 if (tp->t_state & TS_ISOPEN) {
3112 * The device is open, so everything has been initialized.
3115 if (ISCALLOUT(dev) && !tp->t_actout)
3117 if (tp->t_actout && !ISCALLOUT(dev)) {
3118 if (flag & O_NONBLOCK)
3120 error = tsleep(&tp->t_actout,
3121 TTIPRI | PCATCH, "ttybi", 0);
3122 if (error != 0 || (tp->t_flags & TS_GONE))
3126 if (tp->t_state & TS_XCLUDE && suser(td))
3130 * The device isn't open, so there are no conflicts.
3131 * Initialize it. Initialization is done twice in many
3132 * cases: to preempt sleeping callin opens if we are
3133 * callout, and to complete a callin open after DCD rises.
3135 tp->t_termios = ISCALLOUT(dev) ? tp->t_init_out : tp->t_init_in;
3136 tp->t_cflag = tp->t_termios.c_cflag;
3137 if (tp->t_modem != NULL)
3138 tp->t_modem(tp, SER_DTR | SER_RTS, 0);
3140 error = tp->t_param(tp, &tp->t_termios);
3142 if (error == 0 && tp->t_open != NULL)
3143 error = tp->t_open(tp, dev);
3146 if (ISCALLOUT(dev) || (tp->t_modem != NULL &&
3147 (tp->t_modem(tp, 0, 0) & SER_DCD)))
3151 * Wait for DCD if necessary.
3153 if (!(tp->t_state & TS_CARR_ON) && !ISCALLOUT(dev)
3154 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
3156 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "ttydcd", 0);
3158 if (error != 0 || (tp->t_state & TS_GONE))
3162 error = ttyld_open(tp, dev);
3164 if (tp->t_state & TS_ISOPEN && ISCALLOUT(dev))
3165 tp->t_actout = TRUE;
3168 if (!(tp->t_state & TS_ISOPEN) && tp->t_wopeners == 0 &&
3169 tp->t_close != NULL)
3175 ttyclose(struct cdev *dev, int flag, int mode, struct thread *td)
3180 ttyld_close(tp, flag);
3182 if (tp->t_close != NULL)
3184 tp->t_do_timestamp = 0;
3185 if (tp->t_pps != NULL)
3186 tp->t_pps->ppsparam.mode = 0;
3192 ttyread(struct cdev *dev, struct uio *uio, int flag)
3196 tp = tty_gettp(dev);
3198 if (tp->t_state & TS_GONE)
3200 return (ttyld_read(tp, uio, flag));
3204 ttywrite(struct cdev *dev, struct uio *uio, int flag)
3208 tp = tty_gettp(dev);
3210 if (tp->t_state & TS_GONE)
3212 return (ttyld_write(tp, uio, flag));
3216 ttyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
3223 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
3225 struct termios *dt = (struct termios *)data;
3226 struct termios *lt =
3227 ISCALLOUT(dev) ? &tp->t_lock_out : &tp->t_lock_in;
3229 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
3230 | (dt->c_iflag & ~lt->c_iflag);
3231 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
3232 | (dt->c_oflag & ~lt->c_oflag);
3233 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
3234 | (dt->c_cflag & ~lt->c_cflag);
3235 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
3236 | (dt->c_lflag & ~lt->c_lflag);
3237 for (cc = 0; cc < NCCS; ++cc)
3238 if (lt->c_cc[cc] != 0)
3239 dt->c_cc[cc] = tp->t_cc[cc];
3240 if (lt->c_ispeed != 0)
3241 dt->c_ispeed = tp->t_ispeed;
3242 if (lt->c_ospeed != 0)
3243 dt->c_ospeed = tp->t_ospeed;
3246 error = ttyld_ioctl(tp, cmd, data, flag, td);
3247 if (error == ENOIOCTL)
3248 error = ttioctl(tp, cmd, data, flag);
3250 if (error != ENOIOCTL)
3256 ttyldoptim(struct tty *tp)
3261 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
3262 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
3263 && (!(t->c_iflag & PARMRK)
3264 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
3265 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
3266 && linesw[tp->t_line]->l_rint == ttyinput)
3267 tp->t_state |= TS_CAN_BYPASS_L_RINT;
3269 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
3273 ttydtrwaitwakeup(void *arg)
3278 tp->t_state &= ~TS_DTR_WAIT;
3279 wakeup(&tp->t_dtr_wait);
3284 ttydtrwaitstart(struct tty *tp)
3287 if (tp->t_dtr_wait == 0)
3289 if (tp->t_state & TS_DTR_WAIT)
3291 timeout(ttydtrwaitwakeup, tp, tp->t_dtr_wait);
3292 tp->t_state |= TS_DTR_WAIT;
3296 ttydtrwaitsleep(struct tty *tp)
3301 while (error == 0) {
3302 if (tp->t_state & TS_GONE)
3304 else if (!(tp->t_state & TS_DTR_WAIT))
3307 error = tsleep(&tp->t_dtr_wait, TTIPRI | PCATCH,
3314 ttysopen(struct cdev *dev, int flag, int mode, struct thread *td)
3320 ("ttysopen(): no tty pointer on device (%s)", devtoname(dev)));
3321 if (tp->t_state & TS_GONE)
3327 ttysclose(struct cdev *dev, int flag, int mode, struct thread *td)
3334 ttysrdwr(struct cdev *dev, struct uio *uio, int flag)
3341 ttysioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
3349 ("ttysopen(): no tty pointer on device (%s)", devtoname(dev)));
3350 if (tp->t_state & TS_GONE)
3358 *ct = *(struct termios *)data;
3361 *(struct termios *)data = *ct;
3364 *(int *)data = TTYDISC;
3367 bzero(data, sizeof(struct winsize));
3370 if (tp->t_cioctl != NULL)
3371 return(tp->t_cioctl(dev, cmd, data, flag, td));
3377 * Initialize a tty to sane modes.
3380 ttyinitmode(struct tty *tp, int echo, int speed)
3384 speed = TTYDEF_SPEED;
3385 tp->t_init_in.c_iflag = TTYDEF_IFLAG;
3386 tp->t_init_in.c_oflag = TTYDEF_OFLAG;
3387 tp->t_init_in.c_cflag = TTYDEF_CFLAG;
3389 tp->t_init_in.c_lflag = TTYDEF_LFLAG_ECHO;
3391 tp->t_init_in.c_lflag = TTYDEF_LFLAG_NOECHO;
3393 tp->t_init_in.c_ispeed = tp->t_init_in.c_ospeed = speed;
3394 termioschars(&tp->t_init_in);
3395 tp->t_init_out = tp->t_init_in;
3396 tp->t_termios = tp->t_init_in;
3400 * Use more "normal" termios paramters for consoles.
3403 ttyconsolemode(struct tty *tp, int speed)
3407 speed = TTYDEF_SPEED;
3408 ttyinitmode(tp, 1, speed);
3409 tp->t_init_in.c_cflag |= CLOCAL;
3410 tp->t_lock_out.c_cflag = tp->t_lock_in.c_cflag = CLOCAL;
3411 tp->t_lock_out.c_ispeed = tp->t_lock_out.c_ospeed =
3412 tp->t_lock_in.c_ispeed = tp->t_lock_in.c_ospeed = speed;
3413 tp->t_init_out = tp->t_init_in;
3414 tp->t_termios = tp->t_init_in;
3418 * Record the relationship between the serial ports notion of modem control
3419 * signals and the one used in certain ioctls in a way the compiler can enforce
3420 * XXX: We should define TIOCM_* in terms of SER_ if we can limit the
3421 * XXX: consequences of the #include work that would take.
3423 CTASSERT(SER_DTR == TIOCM_DTR / 2);
3424 CTASSERT(SER_RTS == TIOCM_RTS / 2);
3425 CTASSERT(SER_STX == TIOCM_ST / 2);
3426 CTASSERT(SER_SRX == TIOCM_SR / 2);
3427 CTASSERT(SER_CTS == TIOCM_CTS / 2);
3428 CTASSERT(SER_DCD == TIOCM_DCD / 2);
3429 CTASSERT(SER_RI == TIOCM_RI / 2);
3430 CTASSERT(SER_DSR == TIOCM_DSR / 2);