tty.c revision 131092
1303231Sdim/*- 2303231Sdim * Copyright (c) 1982, 1986, 1990, 1991, 1993 3353358Sdim * The Regents of the University of California. All rights reserved. 4353358Sdim * (c) UNIX System Laboratories, Inc. 5353358Sdim * All or some portions of this file are derived from material licensed 6303231Sdim * to the University of California by American Telephone and Telegraph 7303231Sdim * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8303231Sdim * the permission of UNIX System Laboratories, Inc. 9303231Sdim * 10303231Sdim * Copyright (c) 2002 Networks Associates Technologies, Inc. 11303231Sdim * All rights reserved. 12303231Sdim * 13303231Sdim * Portions of this software were developed for the FreeBSD Project by 14303231Sdim * ThinkSec AS and NAI Labs, the Security Research Division of Network 15303231Sdim * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 16303231Sdim * ("CBOSS"), as part of the DARPA CHATS research program. 17303231Sdim * 18314564Sdim * Redistribution and use in source and binary forms, with or without 19303231Sdim * modification, are permitted provided that the following conditions 20314564Sdim * are met: 21314564Sdim * 1. Redistributions of source code must retain the above copyright 22303231Sdim * notice, this list of conditions and the following disclaimer. 23353358Sdim * 2. Redistributions in binary form must reproduce the above copyright 24314564Sdim * notice, this list of conditions and the following disclaimer in the 25314564Sdim * documentation and/or other materials provided with the distribution. 26321369Sdim * 4. Neither the name of the University nor the names of its contributors 27321369Sdim * may be used to endorse or promote products derived from this software 28321369Sdim * without specific prior written permission. 29314564Sdim * 30314564Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31314564Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32303231Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33303231Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34303231Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35314564Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36314564Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37303231Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38303231Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39303231Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40314564Sdim * SUCH DAMAGE. 41314564Sdim * 42303231Sdim * @(#)tty.c 8.8 (Berkeley) 1/21/94 43344779Sdim */ 44344779Sdim 45344779Sdim/*- 46303231Sdim * TODO: 47303231Sdim * o Fix races for sending the start char in ttyflush(). 48303231Sdim * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect(). 49344779Sdim * With luck, there will be MIN chars before select() returns(). 50303231Sdim * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it. 51303231Sdim * o Don't allow input in TS_ZOMBIE case. It would be visible through 52303231Sdim * FIONREAD. 53303231Sdim * o Do the new sio locking stuff here and use it to avoid special 54353358Sdim * case for EXTPROC? 55353358Sdim * o Lock PENDIN too? 56303231Sdim * o Move EXTPROC and/or PENDIN to t_state? 57303231Sdim * o Wrap most of ttioctl in spltty/splx. 58303231Sdim * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>. 59303231Sdim * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set. 60303231Sdim * o Don't allow certain termios flags to affect disciplines other 61303231Sdim * than TTYDISC. Cancel their effects before switch disciplines 62360784Sdim * and ignore them if they are set while we are in another 63360784Sdim * discipline. 64360784Sdim * o Now that historical speed conversions are handled here, don't 65303231Sdim * do them in drivers. 66344779Sdim * o Check for TS_CARR_ON being set while everything is closed and not 67344779Sdim * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open, 68344779Sdim * so it would live until the next open even if carrier drops. 69344779Sdim * o Restore TS_WOPEN since it is useful in pstat. It must be cleared 70344779Sdim * only when _all_ openers leave open(). 71344779Sdim */ 72344779Sdim 73344779Sdim#include <sys/cdefs.h> 74344779Sdim__FBSDID("$FreeBSD: head/sys/kern/tty.c 131092 2004-06-25 10:24:10Z phk $"); 75344779Sdim 76344779Sdim#include "opt_compat.h" 77344779Sdim#include "opt_tty.h" 78344779Sdim 79314564Sdim#include <sys/param.h> 80314564Sdim#include <sys/systm.h> 81314564Sdim#include <sys/filio.h> 82314564Sdim#include <sys/lock.h> 83314564Sdim#include <sys/mutex.h> 84314564Sdim#include <sys/namei.h> 85314564Sdim#include <sys/sx.h> 86314564Sdim#ifndef BURN_BRIDGES 87314564Sdim#if defined(COMPAT_43) 88321369Sdim#include <sys/ioctl_compat.h> 89327952Sdim#endif 90353358Sdim#endif 91353358Sdim#include <sys/proc.h> 92353358Sdim#define TTYDEFCHARS 93321369Sdim#include <sys/tty.h> 94353358Sdim#undef TTYDEFCHARS 95353358Sdim#include <sys/fcntl.h> 96353358Sdim#include <sys/conf.h> 97353358Sdim#include <sys/poll.h> 98314564Sdim#include <sys/kernel.h> 99314564Sdim#include <sys/vnode.h> 100314564Sdim#include <sys/serial.h> 101314564Sdim#include <sys/signalvar.h> 102321369Sdim#include <sys/resourcevar.h> 103314564Sdim#include <sys/malloc.h> 104314564Sdim#include <sys/filedesc.h> 105321369Sdim#include <sys/sched.h> 106321369Sdim#include <sys/sysctl.h> 107321369Sdim 108321369Sdim#include <vm/vm.h> 109314564Sdim#include <vm/pmap.h> 110314564Sdim#include <vm/vm_map.h> 111314564Sdim 112314564SdimMALLOC_DEFINE(M_TTYS, "ttys", "tty data structures"); 113321369Sdim 114321369Sdimlong tk_cancc; 115321369Sdimlong tk_nin; 116314564Sdimlong tk_nout; 117321369Sdimlong tk_rawcc; 118321369Sdim 119314564Sdimstatic int proc_compare(struct proc *p1, struct proc *p2); 120321369Sdimstatic int ttnread(struct tty *tp); 121353358Sdimstatic void ttyecho(int c, struct tty *tp); 122321369Sdimstatic int ttyoutput(int c, struct tty *tp); 123321369Sdimstatic void ttypend(struct tty *tp); 124314564Sdimstatic void ttyretype(struct tty *tp); 125314564Sdimstatic void ttyrub(int c, struct tty *tp); 126314564Sdimstatic void ttyrubo(struct tty *tp, int cnt); 127314564Sdimstatic void ttyunblock(struct tty *tp); 128314564Sdimstatic int ttywflush(struct tty *tp); 129314564Sdimstatic int filt_ttyread(struct knote *kn, long hint); 130321369Sdimstatic void filt_ttyrdetach(struct knote *kn); 131321369Sdimstatic int filt_ttywrite(struct knote *kn, long hint); 132321369Sdimstatic void filt_ttywdetach(struct knote *kn); 133314564Sdim 134314564Sdim/* 135314564Sdim * Table with character classes and parity. The 8th bit indicates parity, 136321369Sdim * the 7th bit indicates the character is an alphameric or underscore (for 137314564Sdim * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 138321369Sdim * are 0 then the character needs no special processing on output; classes 139321369Sdim * other than 0 might be translated or (not currently) require delays. 140321369Sdim */ 141321369Sdim#define E 0x00 /* Even parity. */ 142321369Sdim#define O 0x80 /* Odd parity. */ 143353358Sdim#define PARITY(c) (char_type[c] & O) 144321369Sdim 145321369Sdim#define ALPHA 0x40 /* Alpha or underscore. */ 146321369Sdim#define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 147321369Sdim 148321369Sdim#define CCLASSMASK 0x3f 149321369Sdim#define CCLASS(c) (char_type[c] & CCLASSMASK) 150321369Sdim 151327952Sdim#define BS BACKSPACE 152321369Sdim#define CC CONTROL 153353358Sdim#define CR RETURN 154314564Sdim#define NA ORDINARY | ALPHA 155314564Sdim#define NL NEWLINE 156321369Sdim#define NO ORDINARY 157321369Sdim#define TB TAB 158314564Sdim#define VT VTAB 159321369Sdim 160321369Sdimstatic u_char const char_type[] = { 161314564Sdim E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 162353358Sdim O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 163353358Sdim O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 164353358Sdim E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 165314564Sdim O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 166314564Sdim E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 167314564Sdim E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 168321369Sdim O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 169321369Sdim O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 170321369Sdim E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 171314564Sdim E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 172321369Sdim O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 173314564Sdim E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 174314564Sdim O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 175321369Sdim O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 176314564Sdim E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 177353358Sdim /* 178353358Sdim * Meta chars; should be settable per character set; 179353358Sdim * for now, treat them all as normal characters. 180314564Sdim */ 181321369Sdim NA, NA, NA, NA, NA, NA, NA, NA, 182321369Sdim NA, NA, NA, NA, NA, NA, NA, NA, 183321369Sdim NA, NA, NA, NA, NA, NA, NA, NA, 184321369Sdim NA, NA, NA, NA, NA, NA, NA, NA, 185314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 186314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 187314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 188314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 189314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 190314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 191314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 192314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 193314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 194314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 195314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 196314564Sdim NA, NA, NA, NA, NA, NA, NA, NA, 197314564Sdim}; 198314564Sdim#undef BS 199314564Sdim#undef CC 200314564Sdim#undef CR 201314564Sdim#undef NA 202314564Sdim#undef NL 203353358Sdim#undef NO 204353358Sdim#undef TB 205314564Sdim#undef VT 206314564Sdim 207314564Sdim/* Macros to clear/set/test flags. */ 208314564Sdim#define SET(t, f) (t) |= (f) 209314564Sdim#define CLR(t, f) (t) &= ~(f) 210314564Sdim#define ISSET(t, f) ((t) & (f)) 211314564Sdim 212314564Sdim#undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */ 213314564Sdim#define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */ 214314564Sdim 215314564Sdim/* 216314564Sdim * list of struct tty where pstat(8) can pick it up with sysctl 217314564Sdim * 218353358Sdim * The lock order is to grab the list mutex before the tty mutex. 219353358Sdim * Together with additions going on the tail of the list, this allows 220314564Sdim * the sysctl to avoid doing retries. 221314564Sdim */ 222314564Sdimstatic TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list); 223314564Sdimstatic struct mtx tty_list_mutex; 224353358Sdim 225360784Sdimstatic int drainwait = 5*60; 226314564SdimSYSCTL_INT(_kern, OID_AUTO, drainwait, CTLFLAG_RW, &drainwait, 227353358Sdim 0, "Output drain timeout in seconds"); 228314564Sdim 229314564Sdim/* 230314564Sdim * Initial open of tty, or (re)entry to standard tty line discipline. 231314564Sdim */ 232314564Sdimint 233314564Sdimttyopen(struct cdev *device, struct tty *tp) 234314564Sdim{ 235314564Sdim int s; 236314564Sdim 237314564Sdim s = spltty(); 238314564Sdim tp->t_dev = device; 239314564Sdim if (!ISSET(tp->t_state, TS_ISOPEN)) { 240314564Sdim ttyref(tp); 241341825Sdim SET(tp->t_state, TS_ISOPEN); 242341825Sdim if (ISSET(tp->t_cflag, CLOCAL)) 243341825Sdim SET(tp->t_state, TS_CONNECTED); 244341825Sdim bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 245341825Sdim } 246314564Sdim /* XXX don't hang forever on output */ 247314564Sdim if (tp->t_timeout < 0) 248314564Sdim tp->t_timeout = drainwait*hz; 249341825Sdim ttsetwater(tp); 250341825Sdim splx(s); 251314564Sdim return (0); 252314564Sdim} 253314564Sdim 254314564Sdim/* 255314564Sdim * Handle close() on a tty line: flush and set to initial state, 256314564Sdim * bumping generation number so that pending read/write calls 257314564Sdim * can detect recycling of the tty. 258314564Sdim * XXX our caller should have done `spltty(); l_close(); ttyclose();' 259314564Sdim * and l_close() should have flushed, but we repeat the spltty() and 260314564Sdim * the flush in case there are buggy callers. 261314564Sdim */ 262314564Sdimint 263314564Sdimttyclose(struct tty *tp) 264314564Sdim{ 265314564Sdim int s; 266314564Sdim 267314564Sdim funsetown(&tp->t_sigio); 268314564Sdim s = spltty(); 269314564Sdim if (constty == tp) 270314564Sdim constty_clear(); 271314564Sdim 272314564Sdim ttyflush(tp, FREAD | FWRITE); 273314564Sdim clist_free_cblocks(&tp->t_canq); 274314564Sdim clist_free_cblocks(&tp->t_outq); 275314564Sdim clist_free_cblocks(&tp->t_rawq); 276314564Sdim 277314564Sdim tp->t_gen++; 278314564Sdim tp->t_line = TTYDISC; 279314564Sdim tp->t_pgrp = NULL; 280314564Sdim tp->t_session = NULL; 281314564Sdim tp->t_state = 0; 282314564Sdim ttyrel(tp); 283314564Sdim splx(s); 284314564Sdim return (0); 285314564Sdim} 286314564Sdim 287314564Sdim#define FLUSHQ(q) { \ 288314564Sdim if ((q)->c_cc) \ 289314564Sdim ndflush(q, (q)->c_cc); \ 290314564Sdim} 291314564Sdim 292314564Sdim/* Is 'c' a line delimiter ("break" character)? */ 293314564Sdim#define TTBREAKC(c, lflag) \ 294314564Sdim ((c) == '\n' || (((c) == cc[VEOF] || \ 295314564Sdim (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \ 296314564Sdim (c) != _POSIX_VDISABLE)) 297314564Sdim 298314564Sdim/* 299314564Sdim * Process input of a single character received on a tty. 300314564Sdim */ 301360784Sdimint 302360784Sdimttyinput(int c, struct tty *tp) 303360784Sdim{ 304360784Sdim tcflag_t iflag, lflag; 305314564Sdim cc_t *cc; 306314564Sdim int i, err; 307314564Sdim 308314564Sdim /* 309360784Sdim * If input is pending take it first. 310360784Sdim */ 311314564Sdim lflag = tp->t_lflag; 312314564Sdim if (ISSET(lflag, PENDIN)) 313360784Sdim ttypend(tp); 314314564Sdim /* 315314564Sdim * Gather stats. 316314564Sdim */ 317314564Sdim if (ISSET(lflag, ICANON)) { 318314564Sdim ++tk_cancc; 319314564Sdim ++tp->t_cancc; 320314564Sdim } else { 321314564Sdim ++tk_rawcc; 322314564Sdim ++tp->t_rawcc; 323321369Sdim } 324321369Sdim ++tk_nin; 325321369Sdim 326321369Sdim /* 327321369Sdim * Block further input iff: 328321369Sdim * current input > threshold AND input is available to user program 329321369Sdim * AND input flow control is enabled and not yet invoked. 330321369Sdim * The 3 is slop for PARMRK. 331321369Sdim */ 332321369Sdim iflag = tp->t_iflag; 333314564Sdim if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 && 334314564Sdim (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) && 335314564Sdim (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) && 336314564Sdim !ISSET(tp->t_state, TS_TBLOCK)) 337314564Sdim ttyblock(tp); 338314564Sdim 339314564Sdim /* Handle exceptional conditions (break, parity, framing). */ 340314564Sdim cc = tp->t_cc; 341314564Sdim err = (ISSET(c, TTY_ERRORMASK)); 342314564Sdim if (err) { 343314564Sdim CLR(c, TTY_ERRORMASK); 344314564Sdim if (ISSET(err, TTY_BI)) { 345314564Sdim if (ISSET(iflag, IGNBRK)) 346314564Sdim return (0); 347314564Sdim if (ISSET(iflag, BRKINT)) { 348314564Sdim ttyflush(tp, FREAD | FWRITE); 349314564Sdim if (tp->t_pgrp != NULL) { 350314564Sdim PGRP_LOCK(tp->t_pgrp); 351314564Sdim pgsignal(tp->t_pgrp, SIGINT, 1); 352314564Sdim PGRP_UNLOCK(tp->t_pgrp); 353314564Sdim } 354314564Sdim goto endcase; 355321369Sdim } 356321369Sdim if (ISSET(iflag, PARMRK)) 357321369Sdim goto parmrk; 358321369Sdim } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK)) 359314564Sdim || ISSET(err, TTY_FE)) { 360314564Sdim if (ISSET(iflag, IGNPAR)) 361314564Sdim return (0); 362341825Sdim else if (ISSET(iflag, PARMRK)) { 363341825Sdimparmrk: 364341825Sdim if (tp->t_rawq.c_cc + tp->t_canq.c_cc > 365341825Sdim MAX_INPUT - 3) 366341825Sdim goto input_overflow; 367341825Sdim (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 368341825Sdim (void)putc(0 | TTY_QUOTE, &tp->t_rawq); 369341825Sdim (void)putc(c | TTY_QUOTE, &tp->t_rawq); 370314564Sdim goto endcase; 371314564Sdim } else 372314564Sdim c = 0; 373314564Sdim } 374314564Sdim } 375314564Sdim 376314564Sdim if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP)) 377314564Sdim CLR(c, 0x80); 378314564Sdim if (!ISSET(lflag, EXTPROC)) { 379314564Sdim /* 380314564Sdim * Check for literal nexting very first 381314564Sdim */ 382314564Sdim if (ISSET(tp->t_state, TS_LNCH)) { 383314564Sdim SET(c, TTY_QUOTE); 384314564Sdim CLR(tp->t_state, TS_LNCH); 385314564Sdim } 386314564Sdim /* 387314564Sdim * Scan for special characters. This code 388314564Sdim * is really just a big case statement with 389314564Sdim * non-constant cases. The bottom of the 390314564Sdim * case statement is labeled ``endcase'', so goto 391314564Sdim * it after a case match, or similar. 392314564Sdim */ 393314564Sdim 394314564Sdim /* 395314564Sdim * Control chars which aren't controlled 396314564Sdim * by ICANON, ISIG, or IXON. 397314564Sdim */ 398314564Sdim if (ISSET(lflag, IEXTEN)) { 399314564Sdim if (CCEQ(cc[VLNEXT], c)) { 400321369Sdim if (ISSET(lflag, ECHO)) { 401321369Sdim if (ISSET(lflag, ECHOE)) { 402321369Sdim (void)ttyoutput('^', tp); 403314564Sdim (void)ttyoutput('\b', tp); 404314564Sdim } else 405314564Sdim ttyecho(c, tp); 406314564Sdim } 407314564Sdim SET(tp->t_state, TS_LNCH); 408321369Sdim goto endcase; 409314564Sdim } 410321369Sdim if (CCEQ(cc[VDISCARD], c)) { 411321369Sdim if (ISSET(lflag, FLUSHO)) 412321369Sdim CLR(tp->t_lflag, FLUSHO); 413321369Sdim else { 414321369Sdim ttyflush(tp, FWRITE); 415321369Sdim ttyecho(c, tp); 416321369Sdim if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 417321369Sdim ttyretype(tp); 418314564Sdim SET(tp->t_lflag, FLUSHO); 419314564Sdim } 420314564Sdim goto startoutput; 421353358Sdim } 422353358Sdim } 423314564Sdim /* 424353358Sdim * Signals. 425353358Sdim */ 426314564Sdim if (ISSET(lflag, ISIG)) { 427344779Sdim if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 428344779Sdim if (!ISSET(lflag, NOFLSH)) 429344779Sdim ttyflush(tp, FREAD | FWRITE); 430314564Sdim ttyecho(c, tp); 431314564Sdim if (tp->t_pgrp != NULL) { 432314564Sdim PGRP_LOCK(tp->t_pgrp); 433314564Sdim pgsignal(tp->t_pgrp, 434314564Sdim CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 435314564Sdim PGRP_UNLOCK(tp->t_pgrp); 436321369Sdim } 437321369Sdim goto endcase; 438321369Sdim } 439314564Sdim if (CCEQ(cc[VSUSP], c)) { 440314564Sdim if (!ISSET(lflag, NOFLSH)) 441314564Sdim ttyflush(tp, FREAD); 442314564Sdim ttyecho(c, tp); 443314564Sdim if (tp->t_pgrp != NULL) { 444314564Sdim PGRP_LOCK(tp->t_pgrp); 445314564Sdim pgsignal(tp->t_pgrp, SIGTSTP, 1); 446314564Sdim PGRP_UNLOCK(tp->t_pgrp); 447314564Sdim } 448321369Sdim goto endcase; 449321369Sdim } 450321369Sdim } 451321369Sdim /* 452314564Sdim * Handle start/stop characters. 453314564Sdim */ 454314564Sdim if (ISSET(iflag, IXON)) { 455314564Sdim if (CCEQ(cc[VSTOP], c)) { 456314564Sdim if (!ISSET(tp->t_state, TS_TTSTOP)) { 457303231Sdim SET(tp->t_state, TS_TTSTOP); 458 (*tp->t_stop)(tp, 0); 459 return (0); 460 } 461 if (!CCEQ(cc[VSTART], c)) 462 return (0); 463 /* 464 * if VSTART == VSTOP then toggle 465 */ 466 goto endcase; 467 } 468 if (CCEQ(cc[VSTART], c)) 469 goto restartoutput; 470 } 471 /* 472 * IGNCR, ICRNL, & INLCR 473 */ 474 if (c == '\r') { 475 if (ISSET(iflag, IGNCR)) 476 return (0); 477 else if (ISSET(iflag, ICRNL)) 478 c = '\n'; 479 } else if (c == '\n' && ISSET(iflag, INLCR)) 480 c = '\r'; 481 } 482 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) { 483 /* 484 * From here on down canonical mode character 485 * processing takes place. 486 */ 487 /* 488 * erase or erase2 (^H / ^?) 489 */ 490 if (CCEQ(cc[VERASE], c) || CCEQ(cc[VERASE2], c) ) { 491 if (tp->t_rawq.c_cc) 492 ttyrub(unputc(&tp->t_rawq), tp); 493 goto endcase; 494 } 495 /* 496 * kill (^U) 497 */ 498 if (CCEQ(cc[VKILL], c)) { 499 if (ISSET(lflag, ECHOKE) && 500 tp->t_rawq.c_cc == tp->t_rocount && 501 !ISSET(lflag, ECHOPRT)) 502 while (tp->t_rawq.c_cc) 503 ttyrub(unputc(&tp->t_rawq), tp); 504 else { 505 ttyecho(c, tp); 506 if (ISSET(lflag, ECHOK) || 507 ISSET(lflag, ECHOKE)) 508 ttyecho('\n', tp); 509 FLUSHQ(&tp->t_rawq); 510 tp->t_rocount = 0; 511 } 512 CLR(tp->t_state, TS_LOCAL); 513 goto endcase; 514 } 515 /* 516 * word erase (^W) 517 */ 518 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) { 519 int ctype; 520 521 /* 522 * erase whitespace 523 */ 524 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 525 ttyrub(c, tp); 526 if (c == -1) 527 goto endcase; 528 /* 529 * erase last char of word and remember the 530 * next chars type (for ALTWERASE) 531 */ 532 ttyrub(c, tp); 533 c = unputc(&tp->t_rawq); 534 if (c == -1) 535 goto endcase; 536 if (c == ' ' || c == '\t') { 537 (void)putc(c, &tp->t_rawq); 538 goto endcase; 539 } 540 ctype = ISALPHA(c); 541 /* 542 * erase rest of word 543 */ 544 do { 545 ttyrub(c, tp); 546 c = unputc(&tp->t_rawq); 547 if (c == -1) 548 goto endcase; 549 } while (c != ' ' && c != '\t' && 550 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype)); 551 (void)putc(c, &tp->t_rawq); 552 goto endcase; 553 } 554 /* 555 * reprint line (^R) 556 */ 557 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) { 558 ttyretype(tp); 559 goto endcase; 560 } 561 /* 562 * ^T - kernel info and generate SIGINFO 563 */ 564 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) { 565 if (ISSET(lflag, ISIG) && tp->t_pgrp != NULL) { 566 PGRP_LOCK(tp->t_pgrp); 567 pgsignal(tp->t_pgrp, SIGINFO, 1); 568 PGRP_UNLOCK(tp->t_pgrp); 569 } 570 if (!ISSET(lflag, NOKERNINFO)) 571 ttyinfo(tp); 572 goto endcase; 573 } 574 } 575 /* 576 * Check for input buffer overflow 577 */ 578 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) { 579input_overflow: 580 if (ISSET(iflag, IMAXBEL)) { 581 if (tp->t_outq.c_cc < tp->t_ohiwat) 582 (void)ttyoutput(CTRL('g'), tp); 583 } 584 goto endcase; 585 } 586 587 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP) 588 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR)) 589 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 590 591 /* 592 * Put data char in q for user and 593 * wakeup on seeing a line delimiter. 594 */ 595 if (putc(c, &tp->t_rawq) >= 0) { 596 if (!ISSET(lflag, ICANON)) { 597 ttwakeup(tp); 598 ttyecho(c, tp); 599 goto endcase; 600 } 601 if (TTBREAKC(c, lflag)) { 602 tp->t_rocount = 0; 603 catq(&tp->t_rawq, &tp->t_canq); 604 ttwakeup(tp); 605 } else if (tp->t_rocount++ == 0) 606 tp->t_rocol = tp->t_column; 607 if (ISSET(tp->t_state, TS_ERASE)) { 608 /* 609 * end of prterase \.../ 610 */ 611 CLR(tp->t_state, TS_ERASE); 612 (void)ttyoutput('/', tp); 613 } 614 i = tp->t_column; 615 ttyecho(c, tp); 616 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) { 617 /* 618 * Place the cursor over the '^' of the ^D. 619 */ 620 i = imin(2, tp->t_column - i); 621 while (i > 0) { 622 (void)ttyoutput('\b', tp); 623 i--; 624 } 625 } 626 } 627endcase: 628 /* 629 * IXANY means allow any character to restart output. 630 */ 631 if (ISSET(tp->t_state, TS_TTSTOP) && 632 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) 633 return (0); 634restartoutput: 635 CLR(tp->t_lflag, FLUSHO); 636 CLR(tp->t_state, TS_TTSTOP); 637startoutput: 638 return (ttstart(tp)); 639} 640 641/* 642 * Output a single character on a tty, doing output processing 643 * as needed (expanding tabs, newline processing, etc.). 644 * Returns < 0 if succeeds, otherwise returns char to resend. 645 * Must be recursive. 646 */ 647static int 648ttyoutput(int c, struct tty *tp) 649{ 650 tcflag_t oflag; 651 int col, s; 652 653 oflag = tp->t_oflag; 654 if (!ISSET(oflag, OPOST)) { 655 if (ISSET(tp->t_lflag, FLUSHO)) 656 return (-1); 657 if (putc(c, &tp->t_outq)) 658 return (c); 659 tk_nout++; 660 tp->t_outcc++; 661 return (-1); 662 } 663 /* 664 * Do tab expansion if OXTABS is set. Special case if we external 665 * processing, we don't do the tab expansion because we'll probably 666 * get it wrong. If tab expansion needs to be done, let it happen 667 * externally. 668 */ 669 CLR(c, ~TTY_CHARMASK); 670 if (c == '\t' && 671 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) { 672 c = 8 - (tp->t_column & 7); 673 if (!ISSET(tp->t_lflag, FLUSHO)) { 674 s = spltty(); /* Don't interrupt tabs. */ 675 c -= b_to_q(" ", c, &tp->t_outq); 676 tk_nout += c; 677 tp->t_outcc += c; 678 splx(s); 679 } 680 tp->t_column += c; 681 return (c ? -1 : '\t'); 682 } 683 if (c == CEOT && ISSET(oflag, ONOEOT)) 684 return (-1); 685 686 /* 687 * Newline translation: if ONLCR is set, 688 * translate newline into "\r\n". 689 */ 690 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) { 691 tk_nout++; 692 tp->t_outcc++; 693 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq)) 694 return (c); 695 } 696 /* If OCRNL is set, translate "\r" into "\n". */ 697 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL)) 698 c = '\n'; 699 /* If ONOCR is set, don't transmit CRs when on column 0. */ 700 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0) 701 return (-1); 702 703 tk_nout++; 704 tp->t_outcc++; 705 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq)) 706 return (c); 707 708 col = tp->t_column; 709 switch (CCLASS(c)) { 710 case BACKSPACE: 711 if (col > 0) 712 --col; 713 break; 714 case CONTROL: 715 break; 716 case NEWLINE: 717 if (ISSET(tp->t_oflag, ONLCR | ONLRET)) 718 col = 0; 719 break; 720 case RETURN: 721 col = 0; 722 break; 723 case ORDINARY: 724 ++col; 725 break; 726 case TAB: 727 col = (col + 8) & ~7; 728 break; 729 } 730 tp->t_column = col; 731 return (-1); 732} 733 734/* 735 * Ioctls for all tty devices. Called after line-discipline specific ioctl 736 * has been called to do discipline-specific functions and/or reject any 737 * of these ioctl commands. 738 */ 739/* ARGSUSED */ 740int 741ttioctl(struct tty *tp, u_long cmd, void *data, int flag) 742{ 743 struct proc *p; 744 struct thread *td; 745 struct pgrp *pgrp; 746 int s, error, bits, sig, sig2; 747 748 td = curthread; /* XXX */ 749 p = td->td_proc; 750 751 /* If the ioctl involves modification, hang if in the background. */ 752 switch (cmd) { 753 case TIOCCBRK: 754 case TIOCCONS: 755 case TIOCDRAIN: 756 case TIOCEXCL: 757 case TIOCFLUSH: 758#ifdef TIOCHPCL 759 case TIOCHPCL: 760#endif 761 case TIOCNXCL: 762 case TIOCSBRK: 763 case TIOCSCTTY: 764 case TIOCSDRAINWAIT: 765 case TIOCSETA: 766 case TIOCSETAF: 767 case TIOCSETAW: 768 case TIOCSETD: 769 case TIOCSPGRP: 770 case TIOCSTART: 771 case TIOCSTAT: 772 case TIOCSTI: 773 case TIOCSTOP: 774 case TIOCSWINSZ: 775#ifndef BURN_BRIDGES 776#if defined(COMPAT_43) 777 case TIOCLBIC: 778 case TIOCLBIS: 779 case TIOCLSET: 780 case TIOCSETC: 781 case OTIOCSETD: 782 case TIOCSETN: 783 case TIOCSETP: 784 case TIOCSLTC: 785#endif 786#endif 787 sx_slock(&proctree_lock); 788 PROC_LOCK(p); 789 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) && 790 !SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTOU) && 791 !SIGISMEMBER(td->td_sigmask, SIGTTOU)) { 792 pgrp = p->p_pgrp; 793 PROC_UNLOCK(p); 794 if (pgrp->pg_jobc == 0) { 795 sx_sunlock(&proctree_lock); 796 return (EIO); 797 } 798 PGRP_LOCK(pgrp); 799 sx_sunlock(&proctree_lock); 800 pgsignal(pgrp, SIGTTOU, 1); 801 PGRP_UNLOCK(pgrp); 802 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1", 803 0); 804 if (error) 805 return (error); 806 sx_slock(&proctree_lock); 807 PROC_LOCK(p); 808 } 809 PROC_UNLOCK(p); 810 sx_sunlock(&proctree_lock); 811 break; 812 } 813 814 if (tp->t_break != NULL) { 815 switch (cmd) { 816 case TIOCSBRK: 817 tp->t_break(tp, 1); 818 return (0); 819 case TIOCCBRK: 820 tp->t_break(tp, 0); 821 return (0); 822 default: 823 break; 824 } 825 } 826 827 if (tp->t_modem != NULL) { 828 switch (cmd) { 829 case TIOCSDTR: 830 tp->t_modem(tp, SER_DTR, 0); 831 return (0); 832 case TIOCCDTR: 833 tp->t_modem(tp, 0, SER_DTR); 834 return (0); 835 case TIOCMSET: 836 bits = *(int *)data; 837 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1; 838 sig2 = ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1; 839 tp->t_modem(tp, sig, sig2); 840 return (0); 841 case TIOCMBIS: 842 bits = *(int *)data; 843 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1; 844 tp->t_modem(tp, sig, 0); 845 return (0); 846 case TIOCMBIC: 847 bits = *(int *)data; 848 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1; 849 tp->t_modem(tp, 0, sig); 850 return (0); 851 case TIOCMGET: 852 sig = tp->t_modem(tp, 0, 0); 853 /* See <sys/serial.h. for the "<< 1" stuff */ 854 bits = TIOCM_LE + (sig << 1); 855 *(int *)data = bits; 856 return (0); 857 default: 858 break; 859 } 860 } 861 862 switch (cmd) { /* Process the ioctl. */ 863 case FIOASYNC: /* set/clear async i/o */ 864 s = spltty(); 865 if (*(int *)data) 866 SET(tp->t_state, TS_ASYNC); 867 else 868 CLR(tp->t_state, TS_ASYNC); 869 splx(s); 870 break; 871 case FIONBIO: /* set/clear non-blocking i/o */ 872 break; /* XXX: delete. */ 873 case FIONREAD: /* get # bytes to read */ 874 s = spltty(); 875 *(int *)data = ttnread(tp); 876 splx(s); 877 break; 878 879 case FIOSETOWN: 880 /* 881 * Policy -- Don't allow FIOSETOWN on someone else's 882 * controlling tty 883 */ 884 if (tp->t_session != NULL && !isctty(p, tp)) 885 return (ENOTTY); 886 887 error = fsetown(*(int *)data, &tp->t_sigio); 888 if (error) 889 return (error); 890 break; 891 case FIOGETOWN: 892 if (tp->t_session != NULL && !isctty(p, tp)) 893 return (ENOTTY); 894 *(int *)data = fgetown(&tp->t_sigio); 895 break; 896 897 case TIOCEXCL: /* set exclusive use of tty */ 898 s = spltty(); 899 SET(tp->t_state, TS_XCLUDE); 900 splx(s); 901 break; 902 case TIOCFLUSH: { /* flush buffers */ 903 int flags = *(int *)data; 904 905 if (flags == 0) 906 flags = FREAD | FWRITE; 907 else 908 flags &= FREAD | FWRITE; 909 ttyflush(tp, flags); 910 break; 911 } 912 case TIOCCONS: /* become virtual console */ 913 if (*(int *)data) { 914 struct nameidata nid; 915 916 if (constty && constty != tp && 917 ISSET(constty->t_state, TS_CONNECTED)) 918 return (EBUSY); 919 920 /* Ensure user can open the real console. */ 921 NDINIT(&nid, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, 922 "/dev/console", td); 923 if ((error = namei(&nid)) != 0) 924 return (error); 925 NDFREE(&nid, NDF_ONLY_PNBUF); 926 error = VOP_ACCESS(nid.ni_vp, VREAD, td->td_ucred, td); 927 vput(nid.ni_vp); 928 if (error) 929 return (error); 930 931 constty_set(tp); 932 } else if (tp == constty) 933 constty_clear(); 934 break; 935 case TIOCDRAIN: /* wait till output drained */ 936 error = ttywait(tp); 937 if (error) 938 return (error); 939 break; 940 case TIOCGETA: { /* get termios struct */ 941 struct termios *t = (struct termios *)data; 942 943 bcopy(&tp->t_termios, t, sizeof(struct termios)); 944 break; 945 } 946 case TIOCGETD: /* get line discipline */ 947 *(int *)data = tp->t_line; 948 break; 949 case TIOCGWINSZ: /* get window size */ 950 *(struct winsize *)data = tp->t_winsize; 951 break; 952 case TIOCGPGRP: /* get pgrp of tty */ 953 if (!isctty(p, tp)) 954 return (ENOTTY); 955 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 956 break; 957#ifdef TIOCHPCL 958 case TIOCHPCL: /* hang up on last close */ 959 s = spltty(); 960 SET(tp->t_cflag, HUPCL); 961 splx(s); 962 break; 963#endif 964 case TIOCNXCL: /* reset exclusive use of tty */ 965 s = spltty(); 966 CLR(tp->t_state, TS_XCLUDE); 967 splx(s); 968 break; 969 case TIOCOUTQ: /* output queue size */ 970 *(int *)data = tp->t_outq.c_cc; 971 break; 972 case TIOCSETA: /* set termios struct */ 973 case TIOCSETAW: /* drain output, set */ 974 case TIOCSETAF: { /* drn out, fls in, set */ 975 struct termios *t = (struct termios *)data; 976 977 if (t->c_ispeed == 0) 978 t->c_ispeed = t->c_ospeed; 979 if (t->c_ispeed == 0) 980 t->c_ispeed = tp->t_ospeed; 981 if (t->c_ispeed == 0) 982 return (EINVAL); 983 s = spltty(); 984 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 985 error = ttywait(tp); 986 if (error) { 987 splx(s); 988 return (error); 989 } 990 if (cmd == TIOCSETAF) 991 ttyflush(tp, FREAD); 992 } 993 if (!ISSET(t->c_cflag, CIGNORE)) { 994 /* 995 * Set device hardware. 996 */ 997 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 998 splx(s); 999 return (error); 1000 } 1001 if (ISSET(t->c_cflag, CLOCAL) && 1002 !ISSET(tp->t_cflag, CLOCAL)) { 1003 /* 1004 * XXX disconnections would be too hard to 1005 * get rid of without this kludge. The only 1006 * way to get rid of controlling terminals 1007 * is to exit from the session leader. 1008 */ 1009 CLR(tp->t_state, TS_ZOMBIE); 1010 1011 wakeup(TSA_CARR_ON(tp)); 1012 ttwakeup(tp); 1013 ttwwakeup(tp); 1014 } 1015 if ((ISSET(tp->t_state, TS_CARR_ON) || 1016 ISSET(t->c_cflag, CLOCAL)) && 1017 !ISSET(tp->t_state, TS_ZOMBIE)) 1018 SET(tp->t_state, TS_CONNECTED); 1019 else 1020 CLR(tp->t_state, TS_CONNECTED); 1021 tp->t_cflag = t->c_cflag; 1022 tp->t_ispeed = t->c_ispeed; 1023 if (t->c_ospeed != 0) 1024 tp->t_ospeed = t->c_ospeed; 1025 ttsetwater(tp); 1026 } 1027 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) && 1028 cmd != TIOCSETAF) { 1029 if (ISSET(t->c_lflag, ICANON)) 1030 SET(tp->t_lflag, PENDIN); 1031 else { 1032 /* 1033 * XXX we really shouldn't allow toggling 1034 * ICANON while we're in a non-termios line 1035 * discipline. Now we have to worry about 1036 * panicing for a null queue. 1037 */ 1038 if (tp->t_canq.c_cbreserved > 0 && 1039 tp->t_rawq.c_cbreserved > 0) { 1040 catq(&tp->t_rawq, &tp->t_canq); 1041 /* 1042 * XXX the queue limits may be 1043 * different, so the old queue 1044 * swapping method no longer works. 1045 */ 1046 catq(&tp->t_canq, &tp->t_rawq); 1047 } 1048 CLR(tp->t_lflag, PENDIN); 1049 } 1050 ttwakeup(tp); 1051 } 1052 tp->t_iflag = t->c_iflag; 1053 tp->t_oflag = t->c_oflag; 1054 /* 1055 * Make the EXTPROC bit read only. 1056 */ 1057 if (ISSET(tp->t_lflag, EXTPROC)) 1058 SET(t->c_lflag, EXTPROC); 1059 else 1060 CLR(t->c_lflag, EXTPROC); 1061 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 1062 if (t->c_cc[VMIN] != tp->t_cc[VMIN] || 1063 t->c_cc[VTIME] != tp->t_cc[VTIME]) 1064 ttwakeup(tp); 1065 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 1066 splx(s); 1067 break; 1068 } 1069 case TIOCSETD: { /* set line discipline */ 1070 int t = *(int *)data; 1071 struct cdev *device = tp->t_dev; 1072 1073 if ((u_int)t >= nlinesw) 1074 return (ENXIO); 1075 if (t != tp->t_line) { 1076 s = spltty(); 1077 ttyld_close(tp, flag); 1078 error = (*linesw[t]->l_open)(device, tp); 1079 if (error) { 1080 (void)ttyld_open(tp, device); 1081 splx(s); 1082 return (error); 1083 } 1084 tp->t_line = t; 1085 splx(s); 1086 } 1087 break; 1088 } 1089 case TIOCSTART: /* start output, like ^Q */ 1090 s = spltty(); 1091 if (ISSET(tp->t_state, TS_TTSTOP) || 1092 ISSET(tp->t_lflag, FLUSHO)) { 1093 CLR(tp->t_lflag, FLUSHO); 1094 CLR(tp->t_state, TS_TTSTOP); 1095 ttstart(tp); 1096 } 1097 splx(s); 1098 break; 1099 case TIOCSTI: /* simulate terminal input */ 1100 if ((flag & FREAD) == 0 && suser(td)) 1101 return (EPERM); 1102 if (!isctty(p, tp) && suser(td)) 1103 return (EACCES); 1104 s = spltty(); 1105 ttyld_rint(tp, *(u_char *)data); 1106 splx(s); 1107 break; 1108 case TIOCSTOP: /* stop output, like ^S */ 1109 s = spltty(); 1110 if (!ISSET(tp->t_state, TS_TTSTOP)) { 1111 SET(tp->t_state, TS_TTSTOP); 1112 (*tp->t_stop)(tp, 0); 1113 } 1114 splx(s); 1115 break; 1116 case TIOCSCTTY: /* become controlling tty */ 1117 /* Session ctty vnode pointer set in vnode layer. */ 1118 sx_slock(&proctree_lock); 1119 if (!SESS_LEADER(p) || 1120 ((p->p_session->s_ttyvp || tp->t_session) && 1121 (tp->t_session != p->p_session))) { 1122 sx_sunlock(&proctree_lock); 1123 return (EPERM); 1124 } 1125 tp->t_session = p->p_session; 1126 tp->t_pgrp = p->p_pgrp; 1127 SESS_LOCK(p->p_session); 1128 ttyref(tp); /* ttyrel(): kern_proc.c:pgdelete() */ 1129 p->p_session->s_ttyp = tp; 1130 SESS_UNLOCK(p->p_session); 1131 PROC_LOCK(p); 1132 p->p_flag |= P_CONTROLT; 1133 PROC_UNLOCK(p); 1134 sx_sunlock(&proctree_lock); 1135 break; 1136 case TIOCSPGRP: { /* set pgrp of tty */ 1137 sx_slock(&proctree_lock); 1138 pgrp = pgfind(*(int *)data); 1139 if (!isctty(p, tp)) { 1140 if (pgrp != NULL) 1141 PGRP_UNLOCK(pgrp); 1142 sx_sunlock(&proctree_lock); 1143 return (ENOTTY); 1144 } 1145 if (pgrp == NULL) { 1146 sx_sunlock(&proctree_lock); 1147 return (EPERM); 1148 } 1149 PGRP_UNLOCK(pgrp); 1150 if (pgrp->pg_session != p->p_session) { 1151 sx_sunlock(&proctree_lock); 1152 return (EPERM); 1153 } 1154 sx_sunlock(&proctree_lock); 1155 tp->t_pgrp = pgrp; 1156 break; 1157 } 1158 case TIOCSTAT: /* simulate control-T */ 1159 s = spltty(); 1160 ttyinfo(tp); 1161 splx(s); 1162 break; 1163 case TIOCSWINSZ: /* set window size */ 1164 if (bcmp((caddr_t)&tp->t_winsize, data, 1165 sizeof (struct winsize))) { 1166 tp->t_winsize = *(struct winsize *)data; 1167 if (tp->t_pgrp != NULL) { 1168 PGRP_LOCK(tp->t_pgrp); 1169 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1170 PGRP_UNLOCK(tp->t_pgrp); 1171 } 1172 } 1173 break; 1174 case TIOCSDRAINWAIT: 1175 error = suser(td); 1176 if (error) 1177 return (error); 1178 tp->t_timeout = *(int *)data * hz; 1179 wakeup(TSA_OCOMPLETE(tp)); 1180 wakeup(TSA_OLOWAT(tp)); 1181 break; 1182 case TIOCGDRAINWAIT: 1183 *(int *)data = tp->t_timeout / hz; 1184 break; 1185 default: 1186#if defined(COMPAT_43) 1187#ifndef BURN_BRIDGES 1188 return (ttcompat(tp, cmd, data, flag)); 1189#else 1190 return (ENOIOCTL); 1191#endif 1192#else 1193 return (ENOIOCTL); 1194#endif 1195 } 1196 return (0); 1197} 1198 1199int 1200ttypoll(struct cdev *dev, int events, struct thread *td) 1201{ 1202 int s; 1203 int revents = 0; 1204 struct tty *tp; 1205 1206 KASSERT(devsw(dev)->d_flags & D_TTY, 1207 ("ttypoll() called on non D_TTY device (%s)", devtoname(dev))); 1208 tp = dev->si_tty; 1209 KASSERT(tp != NULL, 1210 ("ttypoll(): no tty pointer on device (%s)", devtoname(dev))); 1211 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */ 1212 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) 1213 | POLLHUP); 1214 1215 s = spltty(); 1216 if (events & (POLLIN | POLLRDNORM)) { 1217 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) 1218 revents |= events & (POLLIN | POLLRDNORM); 1219 else 1220 selrecord(td, &tp->t_rsel); 1221 } 1222 if (events & (POLLOUT | POLLWRNORM)) { 1223 if ((tp->t_outq.c_cc <= tp->t_olowat && 1224 ISSET(tp->t_state, TS_CONNECTED)) 1225 || ISSET(tp->t_state, TS_ZOMBIE)) 1226 revents |= events & (POLLOUT | POLLWRNORM); 1227 else 1228 selrecord(td, &tp->t_wsel); 1229 } 1230 splx(s); 1231 return (revents); 1232} 1233 1234static struct filterops ttyread_filtops = 1235 { 1, NULL, filt_ttyrdetach, filt_ttyread }; 1236static struct filterops ttywrite_filtops = 1237 { 1, NULL, filt_ttywdetach, filt_ttywrite }; 1238 1239int 1240ttykqfilter(struct cdev *dev, struct knote *kn) 1241{ 1242 struct tty *tp; 1243 struct klist *klist; 1244 int s; 1245 1246 KASSERT(devsw(dev)->d_flags & D_TTY, 1247 ("ttykqfilter() called on non D_TTY device (%s)", devtoname(dev))); 1248 tp = dev->si_tty; 1249 KASSERT(tp != NULL, 1250 ("ttykqfilter(): no tty pointer on device (%s)", devtoname(dev))); 1251 switch (kn->kn_filter) { 1252 case EVFILT_READ: 1253 klist = &tp->t_rsel.si_note; 1254 kn->kn_fop = &ttyread_filtops; 1255 break; 1256 case EVFILT_WRITE: 1257 klist = &tp->t_wsel.si_note; 1258 kn->kn_fop = &ttywrite_filtops; 1259 break; 1260 default: 1261 return (1); 1262 } 1263 1264 kn->kn_hook = (caddr_t)dev; 1265 1266 s = spltty(); 1267 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1268 splx(s); 1269 1270 return (0); 1271} 1272 1273static void 1274filt_ttyrdetach(struct knote *kn) 1275{ 1276 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty; 1277 int s = spltty(); 1278 1279 SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext); 1280 splx(s); 1281} 1282 1283static int 1284filt_ttyread(struct knote *kn, long hint) 1285{ 1286 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty; 1287 1288 kn->kn_data = ttnread(tp); 1289 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1290 kn->kn_flags |= EV_EOF; 1291 return (1); 1292 } 1293 return (kn->kn_data > 0); 1294} 1295 1296static void 1297filt_ttywdetach(struct knote *kn) 1298{ 1299 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty; 1300 int s = spltty(); 1301 1302 SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext); 1303 splx(s); 1304} 1305 1306static int 1307filt_ttywrite(struct knote *kn, long hint) 1308{ 1309 struct tty *tp = ((struct cdev *)kn->kn_hook)->si_tty; 1310 1311 kn->kn_data = tp->t_outq.c_cc; 1312 if (ISSET(tp->t_state, TS_ZOMBIE)) 1313 return (1); 1314 return (kn->kn_data <= tp->t_olowat && 1315 ISSET(tp->t_state, TS_CONNECTED)); 1316} 1317 1318/* 1319 * Must be called at spltty(). 1320 */ 1321static int 1322ttnread(struct tty *tp) 1323{ 1324 int nread; 1325 1326 if (ISSET(tp->t_lflag, PENDIN)) 1327 ttypend(tp); 1328 nread = tp->t_canq.c_cc; 1329 if (!ISSET(tp->t_lflag, ICANON)) { 1330 nread += tp->t_rawq.c_cc; 1331 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) 1332 nread = 0; 1333 } 1334 return (nread); 1335} 1336 1337/* 1338 * Wait for output to drain. 1339 */ 1340int 1341ttywait(struct tty *tp) 1342{ 1343 int error, s; 1344 1345 error = 0; 1346 s = spltty(); 1347 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1348 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) { 1349 (*tp->t_oproc)(tp); 1350 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1351 ISSET(tp->t_state, TS_CONNECTED)) { 1352 SET(tp->t_state, TS_SO_OCOMPLETE); 1353 error = ttysleep(tp, TSA_OCOMPLETE(tp), 1354 TTOPRI | PCATCH, "ttywai", 1355 tp->t_timeout); 1356 if (error) { 1357 if (error == EWOULDBLOCK) 1358 error = EIO; 1359 break; 1360 } 1361 } else 1362 break; 1363 } 1364 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY))) 1365 error = EIO; 1366 splx(s); 1367 return (error); 1368} 1369 1370/* 1371 * Flush if successfully wait. 1372 */ 1373static int 1374ttywflush(struct tty *tp) 1375{ 1376 int error; 1377 1378 if ((error = ttywait(tp)) == 0) 1379 ttyflush(tp, FREAD); 1380 return (error); 1381} 1382 1383/* 1384 * Flush tty read and/or write queues, notifying anyone waiting. 1385 */ 1386void 1387ttyflush(struct tty *tp, int rw) 1388{ 1389 int s; 1390 1391 s = spltty(); 1392#if 0 1393again: 1394#endif 1395 if (rw & FWRITE) { 1396 FLUSHQ(&tp->t_outq); 1397 CLR(tp->t_state, TS_TTSTOP); 1398 } 1399 (*tp->t_stop)(tp, rw); 1400 if (rw & FREAD) { 1401 FLUSHQ(&tp->t_canq); 1402 FLUSHQ(&tp->t_rawq); 1403 CLR(tp->t_lflag, PENDIN); 1404 tp->t_rocount = 0; 1405 tp->t_rocol = 0; 1406 CLR(tp->t_state, TS_LOCAL); 1407 ttwakeup(tp); 1408 if (ISSET(tp->t_state, TS_TBLOCK)) { 1409 if (rw & FWRITE) 1410 FLUSHQ(&tp->t_outq); 1411 ttyunblock(tp); 1412 1413 /* 1414 * Don't let leave any state that might clobber the 1415 * next line discipline (although we should do more 1416 * to send the START char). Not clearing the state 1417 * may have caused the "putc to a clist with no 1418 * reserved cblocks" panic/printf. 1419 */ 1420 CLR(tp->t_state, TS_TBLOCK); 1421 1422#if 0 /* forget it, sleeping isn't always safe and we don't know when it is */ 1423 if (ISSET(tp->t_iflag, IXOFF)) { 1424 /* 1425 * XXX wait a bit in the hope that the stop 1426 * character (if any) will go out. Waiting 1427 * isn't good since it allows races. This 1428 * will be fixed when the stop character is 1429 * put in a special queue. Don't bother with 1430 * the checks in ttywait() since the timeout 1431 * will save us. 1432 */ 1433 SET(tp->t_state, TS_SO_OCOMPLETE); 1434 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI, 1435 "ttyfls", hz / 10); 1436 /* 1437 * Don't try sending the stop character again. 1438 */ 1439 CLR(tp->t_state, TS_TBLOCK); 1440 goto again; 1441 } 1442#endif 1443 } 1444 } 1445 if (rw & FWRITE) { 1446 FLUSHQ(&tp->t_outq); 1447 ttwwakeup(tp); 1448 } 1449 splx(s); 1450} 1451 1452/* 1453 * Copy in the default termios characters. 1454 */ 1455void 1456termioschars(struct termios *t) 1457{ 1458 1459 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc); 1460} 1461 1462/* 1463 * Old interface. 1464 */ 1465void 1466ttychars(struct tty *tp) 1467{ 1468 1469 termioschars(&tp->t_termios); 1470} 1471 1472/* 1473 * Handle input high water. Send stop character for the IXOFF case. Turn 1474 * on our input flow control bit and propagate the changes to the driver. 1475 * XXX the stop character should be put in a special high priority queue. 1476 */ 1477void 1478ttyblock(struct tty *tp) 1479{ 1480 1481 SET(tp->t_state, TS_TBLOCK); 1482 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1483 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0) 1484 CLR(tp->t_state, TS_TBLOCK); /* try again later */ 1485 ttstart(tp); 1486} 1487 1488/* 1489 * Handle input low water. Send start character for the IXOFF case. Turn 1490 * off our input flow control bit and propagate the changes to the driver. 1491 * XXX the start character should be put in a special high priority queue. 1492 */ 1493static void 1494ttyunblock(struct tty *tp) 1495{ 1496 1497 CLR(tp->t_state, TS_TBLOCK); 1498 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE && 1499 putc(tp->t_cc[VSTART], &tp->t_outq) != 0) 1500 SET(tp->t_state, TS_TBLOCK); /* try again later */ 1501 ttstart(tp); 1502} 1503 1504#ifdef notyet 1505/* Not used by any current (i386) drivers. */ 1506/* 1507 * Restart after an inter-char delay. 1508 */ 1509void 1510ttrstrt(void *tp_arg) 1511{ 1512 struct tty *tp; 1513 int s; 1514 1515 KASSERT(tp_arg != NULL, ("ttrstrt")); 1516 1517 tp = tp_arg; 1518 s = spltty(); 1519 1520 CLR(tp->t_state, TS_TIMEOUT); 1521 ttstart(tp); 1522 1523 splx(s); 1524} 1525#endif 1526 1527int 1528ttstart(struct tty *tp) 1529{ 1530 1531 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1532 (*tp->t_oproc)(tp); 1533 return (0); 1534} 1535 1536/* 1537 * "close" a line discipline 1538 */ 1539int 1540ttylclose(struct tty *tp, int flag) 1541{ 1542 1543 if (flag & FNONBLOCK || ttywflush(tp)) 1544 ttyflush(tp, FREAD | FWRITE); 1545 return (0); 1546} 1547 1548/* 1549 * Handle modem control transition on a tty. 1550 * Flag indicates new state of carrier. 1551 * Returns 0 if the line should be turned off, otherwise 1. 1552 */ 1553int 1554ttymodem(struct tty *tp, int flag) 1555{ 1556 1557 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) { 1558 /* 1559 * MDMBUF: do flow control according to carrier flag 1560 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP 1561 * works if IXON and IXANY are clear. 1562 */ 1563 if (flag) { 1564 CLR(tp->t_state, TS_CAR_OFLOW); 1565 CLR(tp->t_state, TS_TTSTOP); 1566 ttstart(tp); 1567 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) { 1568 SET(tp->t_state, TS_CAR_OFLOW); 1569 SET(tp->t_state, TS_TTSTOP); 1570 (*tp->t_stop)(tp, 0); 1571 } 1572 } else if (flag == 0) { 1573 /* 1574 * Lost carrier. 1575 */ 1576 CLR(tp->t_state, TS_CARR_ON); 1577 if (ISSET(tp->t_state, TS_ISOPEN) && 1578 !ISSET(tp->t_cflag, CLOCAL)) { 1579 SET(tp->t_state, TS_ZOMBIE); 1580 CLR(tp->t_state, TS_CONNECTED); 1581 if (tp->t_session) { 1582 sx_slock(&proctree_lock); 1583 if (tp->t_session->s_leader) { 1584 struct proc *p; 1585 1586 p = tp->t_session->s_leader; 1587 PROC_LOCK(p); 1588 psignal(p, SIGHUP); 1589 PROC_UNLOCK(p); 1590 } 1591 sx_sunlock(&proctree_lock); 1592 } 1593 ttyflush(tp, FREAD | FWRITE); 1594 return (0); 1595 } 1596 } else { 1597 /* 1598 * Carrier now on. 1599 */ 1600 SET(tp->t_state, TS_CARR_ON); 1601 if (!ISSET(tp->t_state, TS_ZOMBIE)) 1602 SET(tp->t_state, TS_CONNECTED); 1603 wakeup(TSA_CARR_ON(tp)); 1604 ttwakeup(tp); 1605 ttwwakeup(tp); 1606 } 1607 return (1); 1608} 1609 1610/* 1611 * Reinput pending characters after state switch 1612 * call at spltty(). 1613 */ 1614static void 1615ttypend(struct tty *tp) 1616{ 1617 struct clist tq; 1618 int c; 1619 1620 CLR(tp->t_lflag, PENDIN); 1621 SET(tp->t_state, TS_TYPEN); 1622 /* 1623 * XXX this assumes too much about clist internals. It may even 1624 * fail if the cblock slush pool is empty. We can't allocate more 1625 * cblocks here because we are called from an interrupt handler 1626 * and clist_alloc_cblocks() can wait. 1627 */ 1628 tq = tp->t_rawq; 1629 bzero(&tp->t_rawq, sizeof tp->t_rawq); 1630 tp->t_rawq.c_cbmax = tq.c_cbmax; 1631 tp->t_rawq.c_cbreserved = tq.c_cbreserved; 1632 while ((c = getc(&tq)) >= 0) 1633 ttyinput(c, tp); 1634 CLR(tp->t_state, TS_TYPEN); 1635} 1636 1637/* 1638 * Process a read call on a tty device. 1639 */ 1640int 1641ttread(struct tty *tp, struct uio *uio, int flag) 1642{ 1643 struct clist *qp; 1644 int c; 1645 tcflag_t lflag; 1646 cc_t *cc = tp->t_cc; 1647 struct thread *td; 1648 struct proc *p; 1649 int s, first, error = 0; 1650 int has_stime = 0, last_cc = 0; 1651 long slp = 0; /* XXX this should be renamed `timo'. */ 1652 struct timeval stime; 1653 struct pgrp *pg; 1654 1655 td = curthread; 1656 p = td->td_proc; 1657loop: 1658 s = spltty(); 1659 lflag = tp->t_lflag; 1660 /* 1661 * take pending input first 1662 */ 1663 if (ISSET(lflag, PENDIN)) { 1664 ttypend(tp); 1665 splx(s); /* reduce latency */ 1666 s = spltty(); 1667 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */ 1668 } 1669 1670 /* 1671 * Hang process if it's in the background. 1672 */ 1673 if (isbackground(p, tp)) { 1674 splx(s); 1675 sx_slock(&proctree_lock); 1676 PROC_LOCK(p); 1677 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTIN) || 1678 SIGISMEMBER(td->td_sigmask, SIGTTIN) || 1679 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0) { 1680 PROC_UNLOCK(p); 1681 sx_sunlock(&proctree_lock); 1682 return (EIO); 1683 } 1684 pg = p->p_pgrp; 1685 PROC_UNLOCK(p); 1686 PGRP_LOCK(pg); 1687 sx_sunlock(&proctree_lock); 1688 pgsignal(pg, SIGTTIN, 1); 1689 PGRP_UNLOCK(pg); 1690 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0); 1691 if (error) 1692 return (error); 1693 goto loop; 1694 } 1695 1696 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1697 splx(s); 1698 return (0); /* EOF */ 1699 } 1700 1701 /* 1702 * If canonical, use the canonical queue, 1703 * else use the raw queue. 1704 * 1705 * (should get rid of clists...) 1706 */ 1707 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 1708 1709 if (flag & IO_NDELAY) { 1710 if (qp->c_cc > 0) 1711 goto read; 1712 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) { 1713 splx(s); 1714 return (0); 1715 } 1716 splx(s); 1717 return (EWOULDBLOCK); 1718 } 1719 if (!ISSET(lflag, ICANON)) { 1720 int m = cc[VMIN]; 1721 long t = cc[VTIME]; 1722 struct timeval timecopy; 1723 1724 /* 1725 * Check each of the four combinations. 1726 * (m > 0 && t == 0) is the normal read case. 1727 * It should be fairly efficient, so we check that and its 1728 * companion case (m == 0 && t == 0) first. 1729 * For the other two cases, we compute the target sleep time 1730 * into slp. 1731 */ 1732 if (t == 0) { 1733 if (qp->c_cc < m) 1734 goto sleep; 1735 if (qp->c_cc > 0) 1736 goto read; 1737 1738 /* m, t and qp->c_cc are all 0. 0 is enough input. */ 1739 splx(s); 1740 return (0); 1741 } 1742 t *= 100000; /* time in us */ 1743#define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1744 ((t1).tv_usec - (t2).tv_usec)) 1745 if (m > 0) { 1746 if (qp->c_cc <= 0) 1747 goto sleep; 1748 if (qp->c_cc >= m) 1749 goto read; 1750 getmicrotime(&timecopy); 1751 if (!has_stime) { 1752 /* first character, start timer */ 1753 has_stime = 1; 1754 stime = timecopy; 1755 slp = t; 1756 } else if (qp->c_cc > last_cc) { 1757 /* got a character, restart timer */ 1758 stime = timecopy; 1759 slp = t; 1760 } else { 1761 /* nothing, check expiration */ 1762 slp = t - diff(timecopy, stime); 1763 if (slp <= 0) 1764 goto read; 1765 } 1766 last_cc = qp->c_cc; 1767 } else { /* m == 0 */ 1768 if (qp->c_cc > 0) 1769 goto read; 1770 getmicrotime(&timecopy); 1771 if (!has_stime) { 1772 has_stime = 1; 1773 stime = timecopy; 1774 slp = t; 1775 } else { 1776 slp = t - diff(timecopy, stime); 1777 if (slp <= 0) { 1778 /* Timed out, but 0 is enough input. */ 1779 splx(s); 1780 return (0); 1781 } 1782 } 1783 } 1784#undef diff 1785 /* 1786 * Rounding down may make us wake up just short 1787 * of the target, so we round up. 1788 * The formula is ceiling(slp * hz/1000000). 1789 * 32-bit arithmetic is enough for hz < 169. 1790 * XXX see tvtohz() for how to avoid overflow if hz 1791 * is large (divide by `tick' and/or arrange to 1792 * use tvtohz() if hz is large). 1793 */ 1794 slp = (long) (((u_long)slp * hz) + 999999) / 1000000; 1795 goto sleep; 1796 } 1797 if (qp->c_cc <= 0) { 1798sleep: 1799 /* 1800 * There is no input, or not enough input and we can block. 1801 */ 1802 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH, 1803 ISSET(tp->t_state, TS_CONNECTED) ? 1804 "ttyin" : "ttyhup", (int)slp); 1805 splx(s); 1806 if (error == EWOULDBLOCK) 1807 error = 0; 1808 else if (error) 1809 return (error); 1810 /* 1811 * XXX what happens if another process eats some input 1812 * while we are asleep (not just here)? It would be 1813 * safest to detect changes and reset our state variables 1814 * (has_stime and last_cc). 1815 */ 1816 slp = 0; 1817 goto loop; 1818 } 1819read: 1820 splx(s); 1821 /* 1822 * Input present, check for input mapping and processing. 1823 */ 1824 first = 1; 1825 if (ISSET(lflag, ICANON | ISIG)) 1826 goto slowcase; 1827 for (;;) { 1828 char ibuf[IBUFSIZ]; 1829 int icc; 1830 1831 icc = imin(uio->uio_resid, IBUFSIZ); 1832 icc = q_to_b(qp, ibuf, icc); 1833 if (icc <= 0) { 1834 if (first) 1835 goto loop; 1836 break; 1837 } 1838 error = uiomove(ibuf, icc, uio); 1839 /* 1840 * XXX if there was an error then we should ungetc() the 1841 * unmoved chars and reduce icc here. 1842 */ 1843 if (error) 1844 break; 1845 if (uio->uio_resid == 0) 1846 break; 1847 first = 0; 1848 } 1849 goto out; 1850slowcase: 1851 for (;;) { 1852 c = getc(qp); 1853 if (c < 0) { 1854 if (first) 1855 goto loop; 1856 break; 1857 } 1858 /* 1859 * delayed suspend (^Y) 1860 */ 1861 if (CCEQ(cc[VDSUSP], c) && 1862 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) { 1863 if (tp->t_pgrp != NULL) { 1864 PGRP_LOCK(tp->t_pgrp); 1865 pgsignal(tp->t_pgrp, SIGTSTP, 1); 1866 PGRP_UNLOCK(tp->t_pgrp); 1867 } 1868 if (first) { 1869 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, 1870 "ttybg3", 0); 1871 if (error) 1872 break; 1873 goto loop; 1874 } 1875 break; 1876 } 1877 /* 1878 * Interpret EOF only in canonical mode. 1879 */ 1880 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 1881 break; 1882 /* 1883 * Give user character. 1884 */ 1885 error = ureadc(c, uio); 1886 if (error) 1887 /* XXX should ungetc(c, qp). */ 1888 break; 1889 if (uio->uio_resid == 0) 1890 break; 1891 /* 1892 * In canonical mode check for a "break character" 1893 * marking the end of a "line of input". 1894 */ 1895 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag)) 1896 break; 1897 first = 0; 1898 } 1899 1900out: 1901 /* 1902 * Look to unblock input now that (presumably) 1903 * the input queue has gone down. 1904 */ 1905 s = spltty(); 1906 if (ISSET(tp->t_state, TS_TBLOCK) && 1907 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat) 1908 ttyunblock(tp); 1909 splx(s); 1910 1911 return (error); 1912} 1913 1914/* 1915 * Check the output queue on tp for space for a kernel message (from uprintf 1916 * or tprintf). Allow some space over the normal hiwater mark so we don't 1917 * lose messages due to normal flow control, but don't let the tty run amok. 1918 * Sleeps here are not interruptible, but we return prematurely if new signals 1919 * arrive. 1920 */ 1921int 1922ttycheckoutq(struct tty *tp, int wait) 1923{ 1924 int hiwat, s; 1925 sigset_t oldmask; 1926 struct thread *td; 1927 struct proc *p; 1928 1929 td = curthread; 1930 p = td->td_proc; 1931 hiwat = tp->t_ohiwat; 1932 SIGEMPTYSET(oldmask); 1933 s = spltty(); 1934 if (wait) { 1935 PROC_LOCK(p); 1936 oldmask = td->td_siglist; 1937 PROC_UNLOCK(p); 1938 } 1939 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100) 1940 while (tp->t_outq.c_cc > hiwat) { 1941 ttstart(tp); 1942 if (tp->t_outq.c_cc <= hiwat) 1943 break; 1944 if (!wait) { 1945 splx(s); 1946 return (0); 1947 } 1948 PROC_LOCK(p); 1949 if (!SIGSETEQ(td->td_siglist, oldmask)) { 1950 PROC_UNLOCK(p); 1951 splx(s); 1952 return (0); 1953 } 1954 PROC_UNLOCK(p); 1955 SET(tp->t_state, TS_SO_OLOWAT); 1956 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz); 1957 } 1958 splx(s); 1959 return (1); 1960} 1961 1962/* 1963 * Process a write call on a tty device. 1964 */ 1965int 1966ttwrite(struct tty *tp, struct uio *uio, int flag) 1967{ 1968 char *cp = NULL; 1969 int cc, ce; 1970 struct thread *td; 1971 struct proc *p; 1972 int i, hiwat, cnt, error, s; 1973 char obuf[OBUFSIZ]; 1974 1975 hiwat = tp->t_ohiwat; 1976 cnt = uio->uio_resid; 1977 error = 0; 1978 cc = 0; 1979 td = curthread; 1980 p = td->td_proc; 1981loop: 1982 s = spltty(); 1983 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1984 splx(s); 1985 if (uio->uio_resid == cnt) 1986 error = EIO; 1987 goto out; 1988 } 1989 if (!ISSET(tp->t_state, TS_CONNECTED)) { 1990 if (flag & IO_NDELAY) { 1991 splx(s); 1992 error = EWOULDBLOCK; 1993 goto out; 1994 } 1995 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1996 "ttydcd", 0); 1997 splx(s); 1998 if (error) 1999 goto out; 2000 goto loop; 2001 } 2002 splx(s); 2003 /* 2004 * Hang the process if it's in the background. 2005 */ 2006 sx_slock(&proctree_lock); 2007 PROC_LOCK(p); 2008 if (isbackground(p, tp) && 2009 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) && 2010 !SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTOU) && 2011 !SIGISMEMBER(td->td_sigmask, SIGTTOU)) { 2012 if (p->p_pgrp->pg_jobc == 0) { 2013 PROC_UNLOCK(p); 2014 sx_sunlock(&proctree_lock); 2015 error = EIO; 2016 goto out; 2017 } 2018 PROC_UNLOCK(p); 2019 PGRP_LOCK(p->p_pgrp); 2020 sx_sunlock(&proctree_lock); 2021 pgsignal(p->p_pgrp, SIGTTOU, 1); 2022 PGRP_UNLOCK(p->p_pgrp); 2023 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0); 2024 if (error) 2025 goto out; 2026 goto loop; 2027 } else { 2028 PROC_UNLOCK(p); 2029 sx_sunlock(&proctree_lock); 2030 } 2031 /* 2032 * Process the user's data in at most OBUFSIZ chunks. Perform any 2033 * output translation. Keep track of high water mark, sleep on 2034 * overflow awaiting device aid in acquiring new space. 2035 */ 2036 while (uio->uio_resid > 0 || cc > 0) { 2037 if (ISSET(tp->t_lflag, FLUSHO)) { 2038 uio->uio_resid = 0; 2039 return (0); 2040 } 2041 if (tp->t_outq.c_cc > hiwat) 2042 goto ovhiwat; 2043 /* 2044 * Grab a hunk of data from the user, unless we have some 2045 * leftover from last time. 2046 */ 2047 if (cc == 0) { 2048 cc = imin(uio->uio_resid, OBUFSIZ); 2049 cp = obuf; 2050 error = uiomove(cp, cc, uio); 2051 if (error) { 2052 cc = 0; 2053 break; 2054 } 2055 } 2056 /* 2057 * If nothing fancy need be done, grab those characters we 2058 * can handle without any of ttyoutput's processing and 2059 * just transfer them to the output q. For those chars 2060 * which require special processing (as indicated by the 2061 * bits in char_type), call ttyoutput. After processing 2062 * a hunk of data, look for FLUSHO so ^O's will take effect 2063 * immediately. 2064 */ 2065 while (cc > 0) { 2066 if (!ISSET(tp->t_oflag, OPOST)) 2067 ce = cc; 2068 else { 2069 ce = cc - scanc((u_int)cc, (u_char *)cp, 2070 char_type, CCLASSMASK); 2071 /* 2072 * If ce is zero, then we're processing 2073 * a special character through ttyoutput. 2074 */ 2075 if (ce == 0) { 2076 tp->t_rocount = 0; 2077 if (ttyoutput(*cp, tp) >= 0) { 2078 /* No Clists, wait a bit. */ 2079 ttstart(tp); 2080 if (flag & IO_NDELAY) { 2081 error = EWOULDBLOCK; 2082 goto out; 2083 } 2084 error = ttysleep(tp, &lbolt, 2085 TTOPRI|PCATCH, 2086 "ttybf1", 0); 2087 if (error) 2088 goto out; 2089 goto loop; 2090 } 2091 cp++; 2092 cc--; 2093 if (ISSET(tp->t_lflag, FLUSHO) || 2094 tp->t_outq.c_cc > hiwat) 2095 goto ovhiwat; 2096 continue; 2097 } 2098 } 2099 /* 2100 * A bunch of normal characters have been found. 2101 * Transfer them en masse to the output queue and 2102 * continue processing at the top of the loop. 2103 * If there are any further characters in this 2104 * <= OBUFSIZ chunk, the first should be a character 2105 * requiring special handling by ttyoutput. 2106 */ 2107 tp->t_rocount = 0; 2108 i = b_to_q(cp, ce, &tp->t_outq); 2109 ce -= i; 2110 tp->t_column += ce; 2111 cp += ce, cc -= ce, tk_nout += ce; 2112 tp->t_outcc += ce; 2113 if (i > 0) { 2114 /* No Clists, wait a bit. */ 2115 ttstart(tp); 2116 if (flag & IO_NDELAY) { 2117 error = EWOULDBLOCK; 2118 goto out; 2119 } 2120 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, 2121 "ttybf2", 0); 2122 if (error) 2123 goto out; 2124 goto loop; 2125 } 2126 if (ISSET(tp->t_lflag, FLUSHO) || 2127 tp->t_outq.c_cc > hiwat) 2128 break; 2129 } 2130 ttstart(tp); 2131 } 2132out: 2133 /* 2134 * If cc is nonzero, we leave the uio structure inconsistent, as the 2135 * offset and iov pointers have moved forward, but it doesn't matter 2136 * (the call will either return short or restart with a new uio). 2137 */ 2138 uio->uio_resid += cc; 2139 return (error); 2140 2141ovhiwat: 2142 ttstart(tp); 2143 s = spltty(); 2144 /* 2145 * This can only occur if FLUSHO is set in t_lflag, 2146 * or if ttstart/oproc is synchronous (or very fast). 2147 */ 2148 if (tp->t_outq.c_cc <= hiwat) { 2149 splx(s); 2150 goto loop; 2151 } 2152 if (flag & IO_NDELAY) { 2153 splx(s); 2154 uio->uio_resid += cc; 2155 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 2156 } 2157 SET(tp->t_state, TS_SO_OLOWAT); 2158 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri", 2159 tp->t_timeout); 2160 splx(s); 2161 if (error == EWOULDBLOCK) 2162 error = EIO; 2163 if (error) 2164 goto out; 2165 goto loop; 2166} 2167 2168/* 2169 * Rubout one character from the rawq of tp 2170 * as cleanly as possible. 2171 */ 2172static void 2173ttyrub(int c, struct tty *tp) 2174{ 2175 char *cp; 2176 int savecol; 2177 int tabc, s; 2178 2179 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 2180 return; 2181 CLR(tp->t_lflag, FLUSHO); 2182 if (ISSET(tp->t_lflag, ECHOE)) { 2183 if (tp->t_rocount == 0) { 2184 /* 2185 * Screwed by ttwrite; retype 2186 */ 2187 ttyretype(tp); 2188 return; 2189 } 2190 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 2191 ttyrubo(tp, 2); 2192 else { 2193 CLR(c, ~TTY_CHARMASK); 2194 switch (CCLASS(c)) { 2195 case ORDINARY: 2196 ttyrubo(tp, 1); 2197 break; 2198 case BACKSPACE: 2199 case CONTROL: 2200 case NEWLINE: 2201 case RETURN: 2202 case VTAB: 2203 if (ISSET(tp->t_lflag, ECHOCTL)) 2204 ttyrubo(tp, 2); 2205 break; 2206 case TAB: 2207 if (tp->t_rocount < tp->t_rawq.c_cc) { 2208 ttyretype(tp); 2209 return; 2210 } 2211 s = spltty(); 2212 savecol = tp->t_column; 2213 SET(tp->t_state, TS_CNTTB); 2214 SET(tp->t_lflag, FLUSHO); 2215 tp->t_column = tp->t_rocol; 2216 cp = tp->t_rawq.c_cf; 2217 if (cp) 2218 tabc = *cp; /* XXX FIX NEXTC */ 2219 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 2220 ttyecho(tabc, tp); 2221 CLR(tp->t_lflag, FLUSHO); 2222 CLR(tp->t_state, TS_CNTTB); 2223 splx(s); 2224 2225 /* savecol will now be length of the tab. */ 2226 savecol -= tp->t_column; 2227 tp->t_column += savecol; 2228 if (savecol > 8) 2229 savecol = 8; /* overflow screw */ 2230 while (--savecol >= 0) 2231 (void)ttyoutput('\b', tp); 2232 break; 2233 default: /* XXX */ 2234#define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 2235 (void)printf(PANICSTR, c, CCLASS(c)); 2236#ifdef notdef 2237 panic(PANICSTR, c, CCLASS(c)); 2238#endif 2239 } 2240 } 2241 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 2242 if (!ISSET(tp->t_state, TS_ERASE)) { 2243 SET(tp->t_state, TS_ERASE); 2244 (void)ttyoutput('\\', tp); 2245 } 2246 ttyecho(c, tp); 2247 } else { 2248 ttyecho(tp->t_cc[VERASE], tp); 2249 /* 2250 * This code may be executed not only when an ERASE key 2251 * is pressed, but also when ^U (KILL) or ^W (WERASE) are. 2252 * So, I didn't think it was worthwhile to pass the extra 2253 * information (which would need an extra parameter, 2254 * changing every call) needed to distinguish the ERASE2 2255 * case from the ERASE. 2256 */ 2257 } 2258 --tp->t_rocount; 2259} 2260 2261/* 2262 * Back over cnt characters, erasing them. 2263 */ 2264static void 2265ttyrubo(struct tty *tp, int cnt) 2266{ 2267 2268 while (cnt-- > 0) { 2269 (void)ttyoutput('\b', tp); 2270 (void)ttyoutput(' ', tp); 2271 (void)ttyoutput('\b', tp); 2272 } 2273} 2274 2275/* 2276 * ttyretype -- 2277 * Reprint the rawq line. Note, it is assumed that c_cc has already 2278 * been checked. 2279 */ 2280static void 2281ttyretype(struct tty *tp) 2282{ 2283 char *cp; 2284 int s, c; 2285 2286 /* Echo the reprint character. */ 2287 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 2288 ttyecho(tp->t_cc[VREPRINT], tp); 2289 2290 (void)ttyoutput('\n', tp); 2291 2292 /* 2293 * XXX 2294 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 2295 * BIT OF FIRST CHAR. 2296 */ 2297 s = spltty(); 2298 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 2299 cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 2300 ttyecho(c, tp); 2301 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 2302 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 2303 ttyecho(c, tp); 2304 CLR(tp->t_state, TS_ERASE); 2305 splx(s); 2306 2307 tp->t_rocount = tp->t_rawq.c_cc; 2308 tp->t_rocol = 0; 2309} 2310 2311/* 2312 * Echo a typed character to the terminal. 2313 */ 2314static void 2315ttyecho(int c, struct tty *tp) 2316{ 2317 2318 if (!ISSET(tp->t_state, TS_CNTTB)) 2319 CLR(tp->t_lflag, FLUSHO); 2320 if ((!ISSET(tp->t_lflag, ECHO) && 2321 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) || 2322 ISSET(tp->t_lflag, EXTPROC)) 2323 return; 2324 if (ISSET(tp->t_lflag, ECHOCTL) && 2325 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') || 2326 ISSET(c, TTY_CHARMASK) == 0177)) { 2327 (void)ttyoutput('^', tp); 2328 CLR(c, ~TTY_CHARMASK); 2329 if (c == 0177) 2330 c = '?'; 2331 else 2332 c += 'A' - 1; 2333 } 2334 (void)ttyoutput(c, tp); 2335} 2336 2337/* 2338 * Wake up any readers on a tty. 2339 */ 2340void 2341ttwakeup(struct tty *tp) 2342{ 2343 2344 if (SEL_WAITING(&tp->t_rsel)) 2345 selwakeuppri(&tp->t_rsel, TTIPRI); 2346 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2347 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2348 wakeup(TSA_HUP_OR_INPUT(tp)); 2349 KNOTE(&tp->t_rsel.si_note, 0); 2350} 2351 2352/* 2353 * Wake up any writers on a tty. 2354 */ 2355void 2356ttwwakeup(struct tty *tp) 2357{ 2358 2359 if (SEL_WAITING(&tp->t_wsel) && tp->t_outq.c_cc <= tp->t_olowat) 2360 selwakeuppri(&tp->t_wsel, TTOPRI); 2361 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2362 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2363 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == 2364 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { 2365 CLR(tp->t_state, TS_SO_OCOMPLETE); 2366 wakeup(TSA_OCOMPLETE(tp)); 2367 } 2368 if (ISSET(tp->t_state, TS_SO_OLOWAT) && 2369 tp->t_outq.c_cc <= tp->t_olowat) { 2370 CLR(tp->t_state, TS_SO_OLOWAT); 2371 wakeup(TSA_OLOWAT(tp)); 2372 } 2373 KNOTE(&tp->t_wsel.si_note, 0); 2374} 2375 2376/* 2377 * Look up a code for a specified speed in a conversion table; 2378 * used by drivers to map software speed values to hardware parameters. 2379 */ 2380int 2381ttspeedtab(int speed, struct speedtab *table) 2382{ 2383 2384 for ( ; table->sp_speed != -1; table++) 2385 if (table->sp_speed == speed) 2386 return (table->sp_code); 2387 return (-1); 2388} 2389 2390/* 2391 * Set input and output watermarks and buffer sizes. For input, the 2392 * high watermark is about one second's worth of input above empty, the 2393 * low watermark is slightly below high water, and the buffer size is a 2394 * driver-dependent amount above high water. For output, the watermarks 2395 * are near the ends of the buffer, with about 1 second's worth of input 2396 * between them. All this only applies to the standard line discipline. 2397 */ 2398void 2399ttsetwater(struct tty *tp) 2400{ 2401 int cps, ttmaxhiwat, x; 2402 2403 /* Input. */ 2404 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512); 2405 switch (tp->t_ispeedwat) { 2406 case (speed_t)-1: 2407 cps = tp->t_ispeed / 10; 2408 break; 2409 case 0: 2410 /* 2411 * This case is for old drivers that don't know about 2412 * t_ispeedwat. Arrange for them to get the old buffer 2413 * sizes and watermarks. 2414 */ 2415 cps = TTYHOG - 2 * 256; 2416 tp->t_ififosize = 2 * 256; 2417 break; 2418 default: 2419 cps = tp->t_ispeedwat / 10; 2420 break; 2421 } 2422 tp->t_ihiwat = cps; 2423 tp->t_ilowat = 7 * cps / 8; 2424 x = cps + tp->t_ififosize; 2425 clist_alloc_cblocks(&tp->t_rawq, x, x); 2426 2427 /* Output. */ 2428 switch (tp->t_ospeedwat) { 2429 case (speed_t)-1: 2430 cps = tp->t_ospeed / 10; 2431 ttmaxhiwat = 2 * TTMAXHIWAT; 2432 break; 2433 case 0: 2434 cps = tp->t_ospeed / 10; 2435 ttmaxhiwat = TTMAXHIWAT; 2436 break; 2437 default: 2438 cps = tp->t_ospeedwat / 10; 2439 ttmaxhiwat = 8 * TTMAXHIWAT; 2440 break; 2441 } 2442#define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 2443 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 2444 x += cps; 2445 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */ 2446 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */ 2447 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */ 2448 x += OBUFSIZ + 100; 2449 clist_alloc_cblocks(&tp->t_outq, x, x); 2450#undef CLAMP 2451} 2452 2453/* 2454 * Report on state of foreground process group. 2455 */ 2456void 2457ttyinfo(struct tty *tp) 2458{ 2459 struct timeval utime, stime; 2460 struct proc *p, *pick; 2461 struct thread *td; 2462 const char *stateprefix, *state; 2463 long rss; 2464 int load, pctcpu; 2465 2466 if (ttycheckoutq(tp,0) == 0) 2467 return; 2468 2469 /* Print load average. */ 2470 load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 2471 ttyprintf(tp, "load: %d.%02d ", load / 100, load % 100); 2472 2473 /* 2474 * On return following a ttyprintf(), we set tp->t_rocount to 0 so 2475 * that pending input will be retyped on BS. 2476 */ 2477 if (tp->t_session == NULL) { 2478 ttyprintf(tp, "not a controlling terminal\n"); 2479 tp->t_rocount = 0; 2480 return; 2481 } 2482 if (tp->t_pgrp == NULL) { 2483 ttyprintf(tp, "no foreground process group\n"); 2484 tp->t_rocount = 0; 2485 return; 2486 } 2487 PGRP_LOCK(tp->t_pgrp); 2488 if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0) { 2489 PGRP_UNLOCK(tp->t_pgrp); 2490 ttyprintf(tp, "empty foreground process group\n"); 2491 tp->t_rocount = 0; 2492 return; 2493 } 2494 2495 /* 2496 * Pick the most interesting process and copy some of its 2497 * state for printing later. sched_lock must be held for 2498 * most parts of this. Holding it throughout is simplest 2499 * and prevents even unimportant inconsistencies in the 2500 * copy of the state, but may increase interrupt latency 2501 * too much. 2502 */ 2503 mtx_lock_spin(&sched_lock); 2504 for (pick = NULL; p != 0; p = LIST_NEXT(p, p_pglist)) 2505 if (proc_compare(pick, p)) 2506 pick = p; 2507 PGRP_UNLOCK(tp->t_pgrp); 2508 2509 td = FIRST_THREAD_IN_PROC(pick); /* XXXKSE */ 2510#if 0 2511 KASSERT(td != NULL, ("ttyinfo: no thread")); 2512#else 2513 if (td == NULL) { 2514 mtx_unlock_spin(&sched_lock); 2515 ttyprintf(tp, "foreground process without thread\n"); 2516 tp->t_rocount = 0; 2517 return; 2518 } 2519#endif 2520 stateprefix = ""; 2521 if (TD_IS_RUNNING(td)) 2522 state = "running"; 2523 else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td)) 2524 state = "runnable"; 2525 else if (TD_IS_SLEEPING(td)) { 2526 /* XXX: If we're sleeping, are we ever not in a queue? */ 2527 if (TD_ON_SLEEPQ(td)) 2528 state = td->td_wmesg; 2529 else 2530 state = "sleeping without queue"; 2531 } else if (TD_ON_LOCK(td)) { 2532 state = td->td_lockname; 2533 stateprefix = "*"; 2534 } else if (TD_IS_SUSPENDED(td)) 2535 state = "suspended"; 2536 else if (TD_AWAITING_INTR(td)) 2537 state = "intrwait"; 2538 else 2539 state = "unknown"; 2540 calcru(pick, &utime, &stime, NULL); 2541 pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT; 2542 if (pick->p_state == PRS_NEW || pick->p_state == PRS_ZOMBIE) 2543 rss = 0; 2544 else 2545 rss = pgtok(vmspace_resident_count(pick->p_vmspace)); 2546 mtx_unlock_spin(&sched_lock); 2547 2548 /* Print command, pid, state, utime, stime, %cpu, and rss. */ 2549 ttyprintf(tp, 2550 " cmd: %s %d [%s%s] %ld.%02ldu %ld.%02lds %d%% %ldk\n", 2551 pick->p_comm, pick->p_pid, stateprefix, state, 2552 (long)utime.tv_sec, utime.tv_usec / 10000, 2553 (long)stime.tv_sec, stime.tv_usec / 10000, 2554 pctcpu / 100, rss); 2555 tp->t_rocount = 0; 2556} 2557 2558/* 2559 * Returns 1 if p2 is "better" than p1 2560 * 2561 * The algorithm for picking the "interesting" process is thus: 2562 * 2563 * 1) Only foreground processes are eligible - implied. 2564 * 2) Runnable processes are favored over anything else. The runner 2565 * with the highest cpu utilization is picked (p_estcpu). Ties are 2566 * broken by picking the highest pid. 2567 * 3) The sleeper with the shortest sleep time is next. With ties, 2568 * we pick out just "short-term" sleepers (P_SINTR == 0). 2569 * 4) Further ties are broken by picking the highest pid. 2570 */ 2571#define ISRUN(p, val) \ 2572do { \ 2573 struct thread *td; \ 2574 val = 0; \ 2575 FOREACH_THREAD_IN_PROC(p, td) { \ 2576 if (TD_ON_RUNQ(td) || \ 2577 TD_IS_RUNNING(td)) { \ 2578 val = 1; \ 2579 break; \ 2580 } \ 2581 } \ 2582} while (0) 2583 2584#define TESTAB(a, b) ((a)<<1 | (b)) 2585#define ONLYA 2 2586#define ONLYB 1 2587#define BOTH 3 2588 2589static int 2590proc_compare(struct proc *p1, struct proc *p2) 2591{ 2592 2593 int esta, estb; 2594 struct ksegrp *kg; 2595 mtx_assert(&sched_lock, MA_OWNED); 2596 if (p1 == NULL) 2597 return (1); 2598 2599 ISRUN(p1, esta); 2600 ISRUN(p2, estb); 2601 2602 /* 2603 * see if at least one of them is runnable 2604 */ 2605 switch (TESTAB(esta, estb)) { 2606 case ONLYA: 2607 return (0); 2608 case ONLYB: 2609 return (1); 2610 case BOTH: 2611 /* 2612 * tie - favor one with highest recent cpu utilization 2613 */ 2614 esta = estb = 0; 2615 FOREACH_KSEGRP_IN_PROC(p1,kg) { 2616 esta += kg->kg_estcpu; 2617 } 2618 FOREACH_KSEGRP_IN_PROC(p2,kg) { 2619 estb += kg->kg_estcpu; 2620 } 2621 if (estb > esta) 2622 return (1); 2623 if (esta > estb) 2624 return (0); 2625 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2626 } 2627 /* 2628 * weed out zombies 2629 */ 2630 switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) { 2631 case ONLYA: 2632 return (1); 2633 case ONLYB: 2634 return (0); 2635 case BOTH: 2636 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2637 } 2638 2639#if 0 /* XXXKSE */ 2640 /* 2641 * pick the one with the smallest sleep time 2642 */ 2643 if (p2->p_slptime > p1->p_slptime) 2644 return (0); 2645 if (p1->p_slptime > p2->p_slptime) 2646 return (1); 2647 /* 2648 * favor one sleeping in a non-interruptible sleep 2649 */ 2650 if (p1->p_sflag & PS_SINTR && (p2->p_sflag & PS_SINTR) == 0) 2651 return (1); 2652 if (p2->p_sflag & PS_SINTR && (p1->p_sflag & PS_SINTR) == 0) 2653 return (0); 2654#endif 2655 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2656} 2657 2658/* 2659 * Output char to tty; console putchar style. 2660 */ 2661int 2662tputchar(int c, struct tty *tp) 2663{ 2664 int s; 2665 2666 s = spltty(); 2667 if (!ISSET(tp->t_state, TS_CONNECTED)) { 2668 splx(s); 2669 return (-1); 2670 } 2671 if (c == '\n') 2672 (void)ttyoutput('\r', tp); 2673 (void)ttyoutput(c, tp); 2674 ttstart(tp); 2675 splx(s); 2676 return (0); 2677} 2678 2679/* 2680 * Sleep on chan, returning ERESTART if tty changed while we napped and 2681 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If 2682 * the tty is revoked, restarting a pending call will redo validation done 2683 * at the start of the call. 2684 */ 2685int 2686ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo) 2687{ 2688 int error; 2689 int gen; 2690 2691 gen = tp->t_gen; 2692 error = tsleep(chan, pri, wmesg, timo); 2693 if (error) 2694 return (error); 2695 return (tp->t_gen == gen ? 0 : ERESTART); 2696} 2697 2698/* 2699 * Gain a reference to a TTY 2700 */ 2701int 2702ttyref(struct tty *tp) 2703{ 2704 int i; 2705 2706 mtx_lock(&tp->t_mtx); 2707 KASSERT(tp->t_refcnt > 0, 2708 ("ttyref(): tty refcnt is %d (%s)", 2709 tp->t_refcnt, tp->t_dev != NULL ? devtoname(tp->t_dev) : "??")); 2710 i = ++tp->t_refcnt; 2711 mtx_unlock(&tp->t_mtx); 2712 return (i); 2713} 2714 2715/* 2716 * Drop a reference to a TTY. 2717 * When reference count drops to zero, we free it. 2718 */ 2719int 2720ttyrel(struct tty *tp) 2721{ 2722 int i; 2723 2724 mtx_lock(&tty_list_mutex); 2725 mtx_lock(&tp->t_mtx); 2726 KASSERT(tp->t_refcnt > 0, 2727 ("ttyrel(): tty refcnt is %d (%s)", 2728 tp->t_refcnt, tp->t_dev != NULL ? devtoname(tp->t_dev) : "??")); 2729 i = --tp->t_refcnt; 2730 if (i != 0) { 2731 mtx_unlock(&tp->t_mtx); 2732 mtx_unlock(&tty_list_mutex); 2733 return (i); 2734 } 2735 TAILQ_REMOVE(&tty_list, tp, t_list); 2736 mtx_unlock(&tp->t_mtx); 2737 mtx_unlock(&tty_list_mutex); 2738 mtx_destroy(&tp->t_mtx); 2739 free(tp, M_TTYS); 2740 return (i); 2741} 2742 2743/* 2744 * Allocate a tty struct. Clists in the struct will be allocated by 2745 * ttyopen(). 2746 */ 2747struct tty * 2748ttymalloc(struct tty *tp) 2749{ 2750 static int once; 2751 2752 if (!once) { 2753 mtx_init(&tty_list_mutex, "ttylist", NULL, MTX_DEF); 2754 once++; 2755 } 2756 2757 if (tp) { 2758 /* 2759 * XXX: Either this argument should go away, or we should 2760 * XXX: require it and do a ttyrel(tp) here and allocate 2761 * XXX: a new tty. For now do nothing. 2762 */ 2763 return(tp); 2764 } 2765 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK | M_ZERO); 2766 tp->t_timeout = -1; 2767 mtx_init(&tp->t_mtx, "tty", NULL, MTX_DEF); 2768 tp->t_refcnt = 1; 2769 mtx_lock(&tty_list_mutex); 2770 TAILQ_INSERT_TAIL(&tty_list, tp, t_list); 2771 mtx_unlock(&tty_list_mutex); 2772 return (tp); 2773} 2774 2775static int 2776sysctl_kern_ttys(SYSCTL_HANDLER_ARGS) 2777{ 2778 struct tty *tp, *tp2; 2779 struct xtty xt; 2780 int error; 2781 2782 error = 0; 2783 mtx_lock(&tty_list_mutex); 2784 tp = TAILQ_FIRST(&tty_list); 2785 if (tp != NULL) 2786 ttyref(tp); 2787 mtx_unlock(&tty_list_mutex); 2788 while (tp != NULL) { 2789 bzero(&xt, sizeof xt); 2790 xt.xt_size = sizeof xt; 2791#define XT_COPY(field) xt.xt_##field = tp->t_##field 2792 xt.xt_rawcc = tp->t_rawq.c_cc; 2793 xt.xt_cancc = tp->t_canq.c_cc; 2794 xt.xt_outcc = tp->t_outq.c_cc; 2795 XT_COPY(line); 2796 if (tp->t_dev != NULL) 2797 xt.xt_dev = dev2udev(tp->t_dev); 2798 XT_COPY(state); 2799 XT_COPY(flags); 2800 XT_COPY(timeout); 2801 if (tp->t_pgrp != NULL) 2802 xt.xt_pgid = tp->t_pgrp->pg_id; 2803 if (tp->t_session != NULL) 2804 xt.xt_sid = tp->t_session->s_sid; 2805 XT_COPY(termios); 2806 XT_COPY(winsize); 2807 XT_COPY(column); 2808 XT_COPY(rocount); 2809 XT_COPY(rocol); 2810 XT_COPY(ififosize); 2811 XT_COPY(ihiwat); 2812 XT_COPY(ilowat); 2813 XT_COPY(ispeedwat); 2814 XT_COPY(ohiwat); 2815 XT_COPY(olowat); 2816 XT_COPY(ospeedwat); 2817#undef XT_COPY 2818 error = SYSCTL_OUT(req, &xt, sizeof xt); 2819 if (error != 0) { 2820 ttyrel(tp); 2821 return (error); 2822 } 2823 mtx_lock(&tty_list_mutex); 2824 tp2 = TAILQ_NEXT(tp, t_list); 2825 if (tp2 != NULL) 2826 ttyref(tp2); 2827 mtx_unlock(&tty_list_mutex); 2828 ttyrel(tp); 2829 tp = tp2; 2830 } 2831 return (0); 2832} 2833 2834SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD, 2835 0, 0, sysctl_kern_ttys, "S,xtty", "All ttys"); 2836SYSCTL_LONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD, 2837 &tk_nin, 0, "Total TTY in characters"); 2838SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD, 2839 &tk_nout, 0, "Total TTY out characters"); 2840 2841void 2842nottystop(struct tty *tp, int rw) 2843{ 2844 2845 return; 2846} 2847 2848int 2849ttyread(struct cdev *dev, struct uio *uio, int flag) 2850{ 2851 struct tty *tp; 2852 2853 KASSERT(devsw(dev)->d_flags & D_TTY, 2854 ("ttyread() called on non D_TTY device (%s)", devtoname(dev))); 2855 tp = dev->si_tty; 2856 KASSERT(tp != NULL, 2857 ("ttyread(): no tty pointer on device (%s)", devtoname(dev))); 2858 if (tp == NULL) 2859 return (ENODEV); 2860 return (ttyld_read(tp, uio, flag)); 2861} 2862 2863int 2864ttywrite(struct cdev *dev, struct uio *uio, int flag) 2865{ 2866 struct tty *tp; 2867 2868 KASSERT(devsw(dev)->d_flags & D_TTY, 2869 ("ttywrite() called on non D_TTY device (%s)", devtoname(dev))); 2870 tp = dev->si_tty; 2871 KASSERT(tp != NULL, 2872 ("ttywrite(): no tty pointer on device (%s)", devtoname(dev))); 2873 if (tp == NULL) 2874 return (ENODEV); 2875 return (ttyld_write(tp, uio, flag)); 2876} 2877 2878int 2879ttyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 2880{ 2881 struct tty *tp; 2882 int error; 2883 2884 tp = dev->si_tty; 2885 error = ttyld_ioctl(tp, cmd, data, flag, td); 2886 if (error == ENOIOCTL) 2887 error = ttioctl(tp, cmd, data, flag); 2888 if (error != ENOIOCTL) 2889 return (error); 2890 return (ENOTTY); 2891} 2892 2893int 2894ttyldoptim(struct tty *tp) 2895{ 2896 struct termios *t; 2897 2898 t = &tp->t_termios; 2899 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 2900 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 2901 && (!(t->c_iflag & PARMRK) 2902 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 2903 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 2904 && linesw[tp->t_line]->l_rint == ttyinput) 2905 tp->t_state |= TS_CAN_BYPASS_L_RINT; 2906 else 2907 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 2908 return (linesw[tp->t_line]->l_hotchar); 2909} 2910 2911 2912/* 2913 * Record the relationship between the serial ports notion of modem control 2914 * signals and the one used in certain ioctls in a way the compiler can enforce 2915 * XXX: We should define TIOCM_* in terms of SER_ if we can limit the 2916 * XXX: consequences of the #include work that would take. 2917 */ 2918CTASSERT(SER_DTR == TIOCM_DTR / 2); 2919CTASSERT(SER_RTS == TIOCM_RTS / 2); 2920CTASSERT(SER_STX == TIOCM_ST / 2); 2921CTASSERT(SER_SRX == TIOCM_SR / 2); 2922CTASSERT(SER_CTS == TIOCM_CTS / 2); 2923CTASSERT(SER_DCD == TIOCM_DCD / 2); 2924CTASSERT(SER_RI == TIOCM_RI / 2); 2925CTASSERT(SER_DSR == TIOCM_DSR / 2); 2926 2927