tty.c revision 56755
1230130Smav/*- 2230130Smav * Copyright (c) 1982, 1986, 1990, 1991, 1993 3230130Smav * The Regents of the University of California. All rights reserved. 4230130Smav * (c) UNIX System Laboratories, Inc. 5230130Smav * All or some portions of this file are derived from material licensed 6230130Smav * to the University of California by American Telephone and Telegraph 7230130Smav * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8230130Smav * the permission of UNIX System Laboratories, Inc. 9230130Smav * 10230130Smav * Redistribution and use in source and binary forms, with or without 11230130Smav * modification, are permitted provided that the following conditions 12230130Smav * are met: 13230130Smav * 1. Redistributions of source code must retain the above copyright 14230130Smav * notice, this list of conditions and the following disclaimer. 15230130Smav * 2. Redistributions in binary form must reproduce the above copyright 16230130Smav * notice, this list of conditions and the following disclaimer in the 17230130Smav * documentation and/or other materials provided with the distribution. 18230130Smav * 3. All advertising materials mentioning features or use of this software 19230130Smav * must display the following acknowledgement: 20230130Smav * This product includes software developed by the University of 21230130Smav * California, Berkeley and its contributors. 22230130Smav * 4. Neither the name of the University nor the names of its contributors 23230130Smav * may be used to endorse or promote products derived from this software 24230130Smav * without specific prior written permission. 25230130Smav * 26230130Smav * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27230130Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28230130Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29230130Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30230130Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31230130Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32230130Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33230130Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34230130Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35230130Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36230130Smav * SUCH DAMAGE. 37230130Smav * 38230130Smav * @(#)tty.c 8.8 (Berkeley) 1/21/94 39230130Smav * $FreeBSD: head/sys/kern/tty.c 56755 2000-01-28 17:11:07Z archie $ 40230130Smav */ 41230130Smav 42230130Smav/*- 43230130Smav * TODO: 44230130Smav * o Fix races for sending the start char in ttyflush(). 45230130Smav * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect(). 46230130Smav * With luck, there will be MIN chars before select() returns(). 47230130Smav * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it. 48230130Smav * o Don't allow input in TS_ZOMBIE case. It would be visible through 49230130Smav * FIONREAD. 50230130Smav * o Do the new sio locking stuff here and use it to avoid special 51230130Smav * case for EXTPROC? 52230130Smav * o Lock PENDIN too? 53230130Smav * o Move EXTPROC and/or PENDIN to t_state? 54230130Smav * o Wrap most of ttioctl in spltty/splx. 55230130Smav * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>. 56230130Smav * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set. 57230130Smav * o Don't allow certain termios flags to affect disciplines other 58230130Smav * than TTYDISC. Cancel their effects before switch disciplines 59230130Smav * and ignore them if they are set while we are in another 60230130Smav * discipline. 61230130Smav * o Now that historical speed conversions are handled here, don't 62230130Smav * do them in drivers. 63230130Smav * o Check for TS_CARR_ON being set while everything is closed and not 64230130Smav * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open, 65230130Smav * so it would live until the next open even if carrier drops. 66230130Smav * o Restore TS_WOPEN since it is useful in pstat. It must be cleared 67230130Smav * only when _all_ openers leave open(). 68230130Smav */ 69230130Smav 70230130Smav#include "snp.h" 71230130Smav#include "opt_compat.h" 72230130Smav#include "opt_uconsole.h" 73230130Smav 74264832Smarius#include <sys/param.h> 75230130Smav#include <sys/systm.h> 76230130Smav#include <sys/filio.h> 77230130Smav#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 78230130Smav#include <sys/ioctl_compat.h> 79230130Smav#endif 80230130Smav#include <sys/proc.h> 81230130Smav#define TTYDEFCHARS 82230130Smav#include <sys/tty.h> 83230130Smav#undef TTYDEFCHARS 84230130Smav#include <sys/fcntl.h> 85230130Smav#include <sys/conf.h> 86230130Smav#include <sys/dkstat.h> 87230130Smav#include <sys/poll.h> 88230130Smav#include <sys/kernel.h> 89230130Smav#include <sys/vnode.h> 90281544Srpaulo#include <sys/signalvar.h> 91230130Smav#include <sys/resourcevar.h> 92230130Smav#include <sys/malloc.h> 93230130Smav#include <sys/filedesc.h> 94230130Smav#if NSNP > 0 95230130Smav#include <sys/snoop.h> 96230571Smav#endif 97230571Smav#include <sys/sysctl.h> 98230130Smav 99230130Smav#include <vm/vm.h> 100230130Smav#include <sys/lock.h> 101230130Smav#include <vm/pmap.h> 102230130Smav#include <vm/vm_map.h> 103230130Smav 104230130SmavMALLOC_DEFINE(M_TTYS, "ttys", "tty data structures"); 105230130Smav 106230130Smavstatic int proc_compare __P((struct proc *p1, struct proc *p2)); 107230130Smavstatic int ttnread __P((struct tty *tp)); 108230130Smavstatic void ttyecho __P((int c, struct tty *tp)); 109230130Smavstatic int ttyoutput __P((int c, register struct tty *tp)); 110230130Smavstatic void ttypend __P((struct tty *tp)); 111230130Smavstatic void ttyretype __P((struct tty *tp)); 112230130Smavstatic void ttyrub __P((int c, struct tty *tp)); 113230130Smavstatic void ttyrubo __P((struct tty *tp, int cnt)); 114230130Smavstatic void ttyunblock __P((struct tty *tp)); 115230130Smavstatic int ttywflush __P((struct tty *tp)); 116230130Smav 117230130Smav/* 118230130Smav * Table with character classes and parity. The 8th bit indicates parity, 119230130Smav * the 7th bit indicates the character is an alphameric or underscore (for 120230130Smav * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 121230130Smav * are 0 then the character needs no special processing on output; classes 122230130Smav * other than 0 might be translated or (not currently) require delays. 123230130Smav */ 124230130Smav#define E 0x00 /* Even parity. */ 125230130Smav#define O 0x80 /* Odd parity. */ 126230130Smav#define PARITY(c) (char_type[c] & O) 127230130Smav 128230130Smav#define ALPHA 0x40 /* Alpha or underscore. */ 129230571Smav#define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 130230571Smav 131230571Smav#define CCLASSMASK 0x3f 132230571Smav#define CCLASS(c) (char_type[c] & CCLASSMASK) 133230130Smav 134230571Smav#define BS BACKSPACE 135230130Smav#define CC CONTROL 136230130Smav#define CR RETURN 137230130Smav#define NA ORDINARY | ALPHA 138230130Smav#define NL NEWLINE 139230130Smav#define NO ORDINARY 140230130Smav#define TB TAB 141230130Smav#define VT VTAB 142230130Smav 143230130Smavstatic u_char const char_type[] = { 144230130Smav E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 145230130Smav O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 146230130Smav O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 147230130Smav E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 148230130Smav O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 149230130Smav E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 150230130Smav E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 151230130Smav O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 152230130Smav O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 153230130Smav E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 154230130Smav E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 155230130Smav O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 156230130Smav E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 157230130Smav O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 158230130Smav O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 159230571Smav E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 160230571Smav /* 161230571Smav * Meta chars; should be settable per character set; 162230571Smav * for now, treat them all as normal characters. 163230130Smav */ 164230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 165230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 166230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 167230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 168230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 169230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 170230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 171230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 172230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 173230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 174230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 175230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 176230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 177230130Smav NA, NA, NA, NA, NA, NA, NA, NA, 178230571Smav NA, NA, NA, NA, NA, NA, NA, NA, 179230571Smav NA, NA, NA, NA, NA, NA, NA, NA, 180230571Smav}; 181230571Smav#undef BS 182230571Smav#undef CC 183230571Smav#undef CR 184230571Smav#undef NA 185230571Smav#undef NL 186230571Smav#undef NO 187230571Smav#undef TB 188230571Smav#undef VT 189230571Smav 190230130Smav/* Macros to clear/set/test flags. */ 191230130Smav#define SET(t, f) (t) |= (f) 192230130Smav#define CLR(t, f) (t) &= ~(f) 193230571Smav#define ISSET(t, f) ((t) & (f)) 194230571Smav 195230571Smav#undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */ 196230130Smav#define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */ 197230571Smav 198230130Smav/* 199230571Smav * list of struct tty where pstat(8) can pick it up with sysctl 200230130Smav */ 201230130Smavstatic SLIST_HEAD(, tty) tty_list; 202230130Smav 203230130Smav/* 204230130Smav * Initial open of tty, or (re)entry to standard tty line discipline. 205230130Smav */ 206230130Smavint 207230130Smavttyopen(device, tp) 208230130Smav dev_t device; 209230571Smav register struct tty *tp; 210230571Smav{ 211230571Smav int s; 212230571Smav 213230571Smav s = spltty(); 214230571Smav tp->t_dev = device; 215230571Smav if (!ISSET(tp->t_state, TS_ISOPEN)) { 216230571Smav SET(tp->t_state, TS_ISOPEN); 217230571Smav if (ISSET(tp->t_cflag, CLOCAL)) 218230571Smav SET(tp->t_state, TS_CONNECTED); 219230571Smav bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 220230571Smav } 221230571Smav ttsetwater(tp); 222230571Smav splx(s); 223230571Smav return (0); 224230571Smav} 225230571Smav 226230571Smav/* 227230571Smav * Handle close() on a tty line: flush and set to initial state, 228230571Smav * bumping generation number so that pending read/write calls 229230571Smav * can detect recycling of the tty. 230230571Smav * XXX our caller should have done `spltty(); l_close(); ttyclose();' 231230130Smav * and l_close() should have flushed, but we repeat the spltty() and 232230130Smav * the flush in case there are buggy callers. 233230130Smav */ 234230130Smavint 235230130Smavttyclose(tp) 236230130Smav register struct tty *tp; 237230130Smav{ 238230130Smav int s; 239230130Smav 240230130Smav funsetown(tp->t_sigio); 241230130Smav s = spltty(); 242230130Smav if (constty == tp) 243230130Smav constty = NULL; 244230130Smav 245230130Smav ttyflush(tp, FREAD | FWRITE); 246230130Smav clist_free_cblocks(&tp->t_canq); 247230130Smav clist_free_cblocks(&tp->t_outq); 248230130Smav clist_free_cblocks(&tp->t_rawq); 249230130Smav 250230130Smav#if NSNP > 0 251230130Smav if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 252230130Smav snpdown((struct snoop *)tp->t_sc); 253230130Smav#endif 254230130Smav 255230130Smav tp->t_gen++; 256230130Smav tp->t_line = TTYDISC; 257230130Smav tp->t_pgrp = NULL; 258230130Smav tp->t_session = NULL; 259230130Smav tp->t_state = 0; 260230130Smav splx(s); 261230130Smav return (0); 262230130Smav} 263230130Smav 264230130Smav#define FLUSHQ(q) { \ 265230130Smav if ((q)->c_cc) \ 266230130Smav ndflush(q, (q)->c_cc); \ 267230130Smav} 268230130Smav 269230130Smav/* Is 'c' a line delimiter ("break" character)? */ 270230130Smav#define TTBREAKC(c, lflag) \ 271230130Smav ((c) == '\n' || (((c) == cc[VEOF] || \ 272230130Smav (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \ 273230130Smav (c) != _POSIX_VDISABLE)) 274230130Smav 275230130Smav/* 276230130Smav * Process input of a single character received on a tty. 277230130Smav */ 278230130Smavint 279230130Smavttyinput(c, tp) 280230130Smav register int c; 281230130Smav register struct tty *tp; 282230130Smav{ 283230130Smav register tcflag_t iflag, lflag; 284230130Smav register cc_t *cc; 285230130Smav int i, err; 286230130Smav 287230130Smav /* 288230130Smav * If input is pending take it first. 289230130Smav */ 290230130Smav lflag = tp->t_lflag; 291230130Smav if (ISSET(lflag, PENDIN)) 292230130Smav ttypend(tp); 293230130Smav /* 294230130Smav * Gather stats. 295230130Smav */ 296230130Smav if (ISSET(lflag, ICANON)) { 297230130Smav ++tk_cancc; 298230130Smav ++tp->t_cancc; 299230130Smav } else { 300230130Smav ++tk_rawcc; 301230130Smav ++tp->t_rawcc; 302230130Smav } 303230130Smav ++tk_nin; 304230571Smav 305230571Smav /* 306230130Smav * Block further input iff: 307230130Smav * current input > threshold AND input is available to user program 308230130Smav * AND input flow control is enabled and not yet invoked. 309230130Smav * The 3 is slop for PARMRK. 310230130Smav */ 311243794Seadler iflag = tp->t_iflag; 312243793Seadler if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 && 313243794Seadler (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) && 314243794Seadler (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) && 315230130Smav !ISSET(tp->t_state, TS_TBLOCK)) 316230130Smav ttyblock(tp); 317230130Smav 318230130Smav /* Handle exceptional conditions (break, parity, framing). */ 319230130Smav cc = tp->t_cc; 320230130Smav err = (ISSET(c, TTY_ERRORMASK)); 321230130Smav if (err) { 322258170Smav CLR(c, TTY_ERRORMASK); 323281544Srpaulo if (ISSET(err, TTY_BI)) { 324230130Smav if (ISSET(iflag, IGNBRK)) 325230130Smav return (0); 326230130Smav if (ISSET(iflag, BRKINT)) { 327230130Smav ttyflush(tp, FREAD | FWRITE); 328230571Smav pgsignal(tp->t_pgrp, SIGINT, 1); 329230571Smav goto endcase; 330230331Smav } 331230571Smav if (ISSET(iflag, PARMRK)) 332230571Smav goto parmrk; 333230571Smav } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK)) 334230571Smav || ISSET(err, TTY_FE)) { 335230331Smav if (ISSET(iflag, IGNPAR)) 336230571Smav return (0); 337230571Smav else if (ISSET(iflag, PARMRK)) { 338230571Smavparmrk: 339230571Smav if (tp->t_rawq.c_cc + tp->t_canq.c_cc > 340230574Smav MAX_INPUT - 3) 341230571Smav goto input_overflow; 342230571Smav (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 343230331Smav (void)putc(0 | TTY_QUOTE, &tp->t_rawq); 344230331Smav (void)putc(c | TTY_QUOTE, &tp->t_rawq); 345230130Smav goto endcase; 346230130Smav } else 347230130Smav c = 0; 348230130Smav } 349230130Smav } 350230130Smav 351230130Smav if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP)) 352230130Smav CLR(c, 0x80); 353230130Smav if (!ISSET(lflag, EXTPROC)) { 354230130Smav /* 355230130Smav * Check for literal nexting very first 356230130Smav */ 357230130Smav if (ISSET(tp->t_state, TS_LNCH)) { 358230130Smav SET(c, TTY_QUOTE); 359230130Smav CLR(tp->t_state, TS_LNCH); 360230130Smav } 361230130Smav /* 362230130Smav * Scan for special characters. This code 363230130Smav * is really just a big case statement with 364230130Smav * non-constant cases. The bottom of the 365230130Smav * case statement is labeled ``endcase'', so goto 366230130Smav * it after a case match, or similar. 367230130Smav */ 368230130Smav 369230130Smav /* 370230130Smav * Control chars which aren't controlled 371230130Smav * by ICANON, ISIG, or IXON. 372230130Smav */ 373230130Smav if (ISSET(lflag, IEXTEN)) { 374230130Smav if (CCEQ(cc[VLNEXT], c)) { 375230130Smav if (ISSET(lflag, ECHO)) { 376230130Smav if (ISSET(lflag, ECHOE)) { 377230130Smav (void)ttyoutput('^', tp); 378230130Smav (void)ttyoutput('\b', tp); 379230130Smav } else 380230331Smav ttyecho(c, tp); 381230130Smav } 382230130Smav SET(tp->t_state, TS_LNCH); 383230130Smav goto endcase; 384230331Smav } 385264832Smarius if (CCEQ(cc[VDISCARD], c)) { 386230331Smav if (ISSET(lflag, FLUSHO)) 387230331Smav CLR(tp->t_lflag, FLUSHO); 388230331Smav else { 389230331Smav ttyflush(tp, FWRITE); 390230331Smav ttyecho(c, tp); 391230331Smav if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 392230331Smav ttyretype(tp); 393264832Smarius SET(tp->t_lflag, FLUSHO); 394230331Smav } 395230331Smav goto startoutput; 396230331Smav } 397230331Smav } 398230331Smav /* 399230331Smav * Signals. 400230331Smav */ 401230331Smav if (ISSET(lflag, ISIG)) { 402230130Smav if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 403230130Smav if (!ISSET(lflag, NOFLSH)) 404230130Smav ttyflush(tp, FREAD | FWRITE); 405230130Smav ttyecho(c, tp); 406230130Smav pgsignal(tp->t_pgrp, 407230130Smav CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 408230130Smav goto endcase; 409230130Smav } 410230130Smav if (CCEQ(cc[VSUSP], c)) { 411230130Smav if (!ISSET(lflag, NOFLSH)) 412230130Smav ttyflush(tp, FREAD); 413230130Smav ttyecho(c, tp); 414230130Smav pgsignal(tp->t_pgrp, SIGTSTP, 1); 415230130Smav goto endcase; 416230130Smav } 417230130Smav } 418230130Smav /* 419230130Smav * Handle start/stop characters. 420230130Smav */ 421230130Smav if (ISSET(iflag, IXON)) { 422230130Smav if (CCEQ(cc[VSTOP], c)) { 423230130Smav if (!ISSET(tp->t_state, TS_TTSTOP)) { 424230130Smav SET(tp->t_state, TS_TTSTOP); 425230130Smav (*tp->t_stop)(tp, 0); 426230130Smav return (0); 427230130Smav } 428230130Smav if (!CCEQ(cc[VSTART], c)) 429230130Smav return (0); 430230130Smav /* 431242352Smav * if VSTART == VSTOP then toggle 432230130Smav */ 433230130Smav goto endcase; 434230130Smav } 435230130Smav if (CCEQ(cc[VSTART], c)) 436230130Smav goto restartoutput; 437230130Smav } 438230130Smav /* 439230130Smav * IGNCR, ICRNL, & INLCR 440230130Smav */ 441230130Smav if (c == '\r') { 442230130Smav if (ISSET(iflag, IGNCR)) 443230130Smav return (0); 444230130Smav else if (ISSET(iflag, ICRNL)) 445230130Smav c = '\n'; 446230130Smav } else if (c == '\n' && ISSET(iflag, INLCR)) 447230130Smav c = '\r'; 448230130Smav } 449230130Smav if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) { 450230130Smav /* 451230130Smav * From here on down canonical mode character 452230130Smav * processing takes place. 453230130Smav */ 454230130Smav /* 455230130Smav * erase (^H / ^?) 456230130Smav */ 457230130Smav if (CCEQ(cc[VERASE], c)) { 458230130Smav if (tp->t_rawq.c_cc) 459230130Smav ttyrub(unputc(&tp->t_rawq), tp); 460230130Smav goto endcase; 461230130Smav } 462230130Smav /* 463230130Smav * kill (^U) 464230130Smav */ 465247910Sglebius if (CCEQ(cc[VKILL], c)) { 466247910Sglebius if (ISSET(lflag, ECHOKE) && 467230130Smav tp->t_rawq.c_cc == tp->t_rocount && 468247910Sglebius !ISSET(lflag, ECHOPRT)) 469247910Sglebius while (tp->t_rawq.c_cc) 470247910Sglebius ttyrub(unputc(&tp->t_rawq), tp); 471230130Smav else { 472230130Smav ttyecho(c, tp); 473230130Smav if (ISSET(lflag, ECHOK) || 474230130Smav ISSET(lflag, ECHOKE)) 475230130Smav ttyecho('\n', tp); 476230130Smav FLUSHQ(&tp->t_rawq); 477230130Smav tp->t_rocount = 0; 478230130Smav } 479230130Smav CLR(tp->t_state, TS_LOCAL); 480230130Smav goto endcase; 481230130Smav } 482230130Smav /* 483230130Smav * word erase (^W) 484230130Smav */ 485230130Smav if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) { 486230130Smav int ctype; 487230130Smav 488230130Smav /* 489230130Smav * erase whitespace 490230130Smav */ 491230130Smav while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 492230130Smav ttyrub(c, tp); 493230130Smav if (c == -1) 494230130Smav goto endcase; 495230130Smav /* 496230130Smav * erase last char of word and remember the 497230130Smav * next chars type (for ALTWERASE) 498230130Smav */ 499230130Smav ttyrub(c, tp); 500230130Smav c = unputc(&tp->t_rawq); 501230130Smav if (c == -1) 502230130Smav goto endcase; 503230130Smav if (c == ' ' || c == '\t') { 504230130Smav (void)putc(c, &tp->t_rawq); 505230130Smav goto endcase; 506230130Smav } 507230130Smav ctype = ISALPHA(c); 508230130Smav /* 509230130Smav * erase rest of word 510230130Smav */ 511230130Smav do { 512230130Smav ttyrub(c, tp); 513230130Smav c = unputc(&tp->t_rawq); 514230130Smav if (c == -1) 515230130Smav goto endcase; 516230130Smav } while (c != ' ' && c != '\t' && 517230130Smav (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype)); 518230130Smav (void)putc(c, &tp->t_rawq); 519242352Smav goto endcase; 520242352Smav } 521242352Smav /* 522242352Smav * reprint line (^R) 523230130Smav */ 524230130Smav if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) { 525230130Smav ttyretype(tp); 526230130Smav goto endcase; 527230130Smav } 528230130Smav /* 529230130Smav * ^T - kernel info and generate SIGINFO 530230130Smav */ 531230130Smav if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) { 532230130Smav if (ISSET(lflag, ISIG)) 533230130Smav pgsignal(tp->t_pgrp, SIGINFO, 1); 534230130Smav if (!ISSET(lflag, NOKERNINFO)) 535230130Smav ttyinfo(tp); 536230130Smav goto endcase; 537230130Smav } 538230130Smav } 539230130Smav /* 540230130Smav * Check for input buffer overflow 541230130Smav */ 542230130Smav if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) { 543230130Smavinput_overflow: 544230130Smav if (ISSET(iflag, IMAXBEL)) { 545230130Smav if (tp->t_outq.c_cc < tp->t_ohiwat) 546230130Smav (void)ttyoutput(CTRL('g'), tp); 547230130Smav } 548230130Smav goto endcase; 549230130Smav } 550230130Smav 551230130Smav if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP) 552230130Smav && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR)) 553230130Smav (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 554230130Smav 555230130Smav /* 556230130Smav * Put data char in q for user and 557230130Smav * wakeup on seeing a line delimiter. 558230130Smav */ 559230130Smav if (putc(c, &tp->t_rawq) >= 0) { 560230130Smav if (!ISSET(lflag, ICANON)) { 561230130Smav ttwakeup(tp); 562230130Smav ttyecho(c, tp); 563230130Smav goto endcase; 564230130Smav } 565230130Smav if (TTBREAKC(c, lflag)) { 566230130Smav tp->t_rocount = 0; 567230130Smav catq(&tp->t_rawq, &tp->t_canq); 568230130Smav ttwakeup(tp); 569230130Smav } else if (tp->t_rocount++ == 0) 570230130Smav tp->t_rocol = tp->t_column; 571230130Smav if (ISSET(tp->t_state, TS_ERASE)) { 572230326Smav /* 573230130Smav * end of prterase \.../ 574230130Smav */ 575230130Smav CLR(tp->t_state, TS_ERASE); 576230130Smav (void)ttyoutput('/', tp); 577230130Smav } 578230326Smav i = tp->t_column; 579230130Smav ttyecho(c, tp); 580230130Smav if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) { 581230130Smav /* 582230130Smav * Place the cursor over the '^' of the ^D. 583230130Smav */ 584230130Smav i = imin(2, tp->t_column - i); 585230130Smav while (i > 0) { 586230130Smav (void)ttyoutput('\b', tp); 587230130Smav i--; 588230130Smav } 589230130Smav } 590230130Smav } 591230130Smavendcase: 592230130Smav /* 593230130Smav * IXANY means allow any character to restart output. 594230130Smav */ 595230130Smav if (ISSET(tp->t_state, TS_TTSTOP) && 596230130Smav !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) 597230130Smav return (0); 598230130Smavrestartoutput: 599230130Smav CLR(tp->t_lflag, FLUSHO); 600230130Smav CLR(tp->t_state, TS_TTSTOP); 601230130Smavstartoutput: 602230130Smav return (ttstart(tp)); 603230130Smav} 604230130Smav 605230130Smav/* 606230130Smav * Output a single character on a tty, doing output processing 607230130Smav * as needed (expanding tabs, newline processing, etc.). 608230130Smav * Returns < 0 if succeeds, otherwise returns char to resend. 609230130Smav * Must be recursive. 610230130Smav */ 611230130Smavstatic int 612230130Smavttyoutput(c, tp) 613230130Smav register int c; 614230130Smav register struct tty *tp; 615230130Smav{ 616230130Smav register tcflag_t oflag; 617230130Smav register int col, s; 618230130Smav 619230130Smav oflag = tp->t_oflag; 620230130Smav if (!ISSET(oflag, OPOST)) { 621230130Smav if (ISSET(tp->t_lflag, FLUSHO)) 622230130Smav return (-1); 623230130Smav if (putc(c, &tp->t_outq)) 624230130Smav return (c); 625230130Smav tk_nout++; 626230130Smav tp->t_outcc++; 627230130Smav return (-1); 628230130Smav } 629231024Smav /* 630230130Smav * Do tab expansion if OXTABS is set. Special case if we external 631230130Smav * processing, we don't do the tab expansion because we'll probably 632230130Smav * get it wrong. If tab expansion needs to be done, let it happen 633230130Smav * externally. 634230130Smav */ 635230130Smav CLR(c, ~TTY_CHARMASK); 636230130Smav if (c == '\t' && 637230130Smav ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) { 638230130Smav c = 8 - (tp->t_column & 7); 639230130Smav if (!ISSET(tp->t_lflag, FLUSHO)) { 640230130Smav s = spltty(); /* Don't interrupt tabs. */ 641230130Smav c -= b_to_q(" ", c, &tp->t_outq); 642230130Smav tk_nout += c; 643230130Smav tp->t_outcc += c; 644230130Smav splx(s); 645230130Smav } 646230130Smav tp->t_column += c; 647230130Smav return (c ? -1 : '\t'); 648230130Smav } 649230130Smav if (c == CEOT && ISSET(oflag, ONOEOT)) 650230130Smav return (-1); 651230130Smav 652230130Smav /* 653230130Smav * Newline translation: if ONLCR is set, 654230130Smav * translate newline into "\r\n". 655230130Smav */ 656230130Smav if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) { 657230130Smav tk_nout++; 658230130Smav tp->t_outcc++; 659230130Smav if (putc('\r', &tp->t_outq)) 660230130Smav return (c); 661230130Smav } 662230130Smav tk_nout++; 663230130Smav tp->t_outcc++; 664230130Smav if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq)) 665230130Smav return (c); 666230130Smav 667230130Smav col = tp->t_column; 668230130Smav switch (CCLASS(c)) { 669230130Smav case BACKSPACE: 670230130Smav if (col > 0) 671230130Smav --col; 672230130Smav break; 673230130Smav case CONTROL: 674230130Smav break; 675230130Smav case NEWLINE: 676230130Smav case RETURN: 677230130Smav col = 0; 678230130Smav break; 679230130Smav case ORDINARY: 680230130Smav ++col; 681230130Smav break; 682230130Smav case TAB: 683230130Smav col = (col + 8) & ~7; 684230130Smav break; 685230130Smav } 686230130Smav tp->t_column = col; 687230130Smav return (-1); 688230130Smav} 689230130Smav 690230130Smav/* 691230130Smav * Ioctls for all tty devices. Called after line-discipline specific ioctl 692230130Smav * has been called to do discipline-specific functions and/or reject any 693230130Smav * of these ioctl commands. 694230130Smav */ 695230130Smav/* ARGSUSED */ 696230130Smavint 697230130Smavttioctl(tp, cmd, data, flag) 698230130Smav register struct tty *tp; 699230130Smav u_long cmd; 700230130Smav int flag; 701230130Smav void *data; 702230130Smav{ 703230130Smav register struct proc *p; 704230130Smav int s, error; 705230130Smav 706230130Smav p = curproc; /* XXX */ 707230130Smav 708230130Smav /* If the ioctl involves modification, hang if in the background. */ 709230130Smav switch (cmd) { 710230130Smav case TIOCCBRK: 711230130Smav case TIOCCONS: 712230130Smav case TIOCDRAIN: 713230130Smav case TIOCEXCL: 714230130Smav case TIOCFLUSH: 715230130Smav#ifdef TIOCHPCL 716230130Smav case TIOCHPCL: 717264832Smarius#endif 718230130Smav case TIOCNXCL: 719230130Smav case TIOCSBRK: 720230130Smav case TIOCSCTTY: 721230130Smav case TIOCSDRAINWAIT: 722230130Smav case TIOCSETA: 723230130Smav case TIOCSETAF: 724230130Smav case TIOCSETAW: 725230130Smav case TIOCSETD: 726230130Smav case TIOCSPGRP: 727230130Smav case TIOCSTART: 728264832Smarius case TIOCSTAT: 729 case TIOCSTI: 730 case TIOCSTOP: 731 case TIOCSWINSZ: 732#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 733 case TIOCLBIC: 734 case TIOCLBIS: 735 case TIOCLSET: 736 case TIOCSETC: 737 case OTIOCSETD: 738 case TIOCSETN: 739 case TIOCSETP: 740 case TIOCSLTC: 741#endif 742 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) && 743 !SIGISMEMBER(p->p_sigignore, SIGTTOU) && 744 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) { 745 if (p->p_pgrp->pg_jobc == 0) 746 return (EIO); 747 pgsignal(p->p_pgrp, SIGTTOU, 1); 748 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1", 749 0); 750 if (error) 751 return (error); 752 } 753 break; 754 } 755 756 switch (cmd) { /* Process the ioctl. */ 757 case FIOASYNC: /* set/clear async i/o */ 758 s = spltty(); 759 if (*(int *)data) 760 SET(tp->t_state, TS_ASYNC); 761 else 762 CLR(tp->t_state, TS_ASYNC); 763 splx(s); 764 break; 765 case FIONBIO: /* set/clear non-blocking i/o */ 766 break; /* XXX: delete. */ 767 case FIONREAD: /* get # bytes to read */ 768 s = spltty(); 769 *(int *)data = ttnread(tp); 770 splx(s); 771 break; 772 773 case FIOSETOWN: 774 /* 775 * Policy -- Don't allow FIOSETOWN on someone else's 776 * controlling tty 777 */ 778 if (tp->t_session != NULL && !isctty(p, tp)) 779 return (ENOTTY); 780 781 error = fsetown(*(int *)data, &tp->t_sigio); 782 if (error) 783 return (error); 784 break; 785 case FIOGETOWN: 786 if (tp->t_session != NULL && !isctty(p, tp)) 787 return (ENOTTY); 788 *(int *)data = fgetown(tp->t_sigio); 789 break; 790 791 case TIOCEXCL: /* set exclusive use of tty */ 792 s = spltty(); 793 SET(tp->t_state, TS_XCLUDE); 794 splx(s); 795 break; 796 case TIOCFLUSH: { /* flush buffers */ 797 register int flags = *(int *)data; 798 799 if (flags == 0) 800 flags = FREAD | FWRITE; 801 else 802 flags &= FREAD | FWRITE; 803 ttyflush(tp, flags); 804 break; 805 } 806 case TIOCCONS: /* become virtual console */ 807 if (*(int *)data) { 808 if (constty && constty != tp && 809 ISSET(constty->t_state, TS_CONNECTED)) 810 return (EBUSY); 811#ifndef UCONSOLE 812 if ((error = suser(p)) != 0) 813 return (error); 814#endif 815 constty = tp; 816 } else if (tp == constty) 817 constty = NULL; 818 break; 819 case TIOCDRAIN: /* wait till output drained */ 820 error = ttywait(tp); 821 if (error) 822 return (error); 823 break; 824 case TIOCGETA: { /* get termios struct */ 825 struct termios *t = (struct termios *)data; 826 827 bcopy(&tp->t_termios, t, sizeof(struct termios)); 828 break; 829 } 830 case TIOCGETD: /* get line discipline */ 831 *(int *)data = tp->t_line; 832 break; 833 case TIOCGWINSZ: /* get window size */ 834 *(struct winsize *)data = tp->t_winsize; 835 break; 836 case TIOCGPGRP: /* get pgrp of tty */ 837 if (!isctty(p, tp)) 838 return (ENOTTY); 839 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 840 break; 841#ifdef TIOCHPCL 842 case TIOCHPCL: /* hang up on last close */ 843 s = spltty(); 844 SET(tp->t_cflag, HUPCL); 845 splx(s); 846 break; 847#endif 848 case TIOCNXCL: /* reset exclusive use of tty */ 849 s = spltty(); 850 CLR(tp->t_state, TS_XCLUDE); 851 splx(s); 852 break; 853 case TIOCOUTQ: /* output queue size */ 854 *(int *)data = tp->t_outq.c_cc; 855 break; 856 case TIOCSETA: /* set termios struct */ 857 case TIOCSETAW: /* drain output, set */ 858 case TIOCSETAF: { /* drn out, fls in, set */ 859 register struct termios *t = (struct termios *)data; 860 861 if (t->c_ispeed == 0) 862 t->c_ispeed = t->c_ospeed; 863 if (t->c_ispeed == 0) 864 t->c_ispeed = tp->t_ospeed; 865 if (t->c_ispeed == 0) 866 return (EINVAL); 867 s = spltty(); 868 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 869 error = ttywait(tp); 870 if (error) { 871 splx(s); 872 return (error); 873 } 874 if (cmd == TIOCSETAF) 875 ttyflush(tp, FREAD); 876 } 877 if (!ISSET(t->c_cflag, CIGNORE)) { 878 /* 879 * Set device hardware. 880 */ 881 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 882 splx(s); 883 return (error); 884 } 885 if (ISSET(t->c_cflag, CLOCAL) && 886 !ISSET(tp->t_cflag, CLOCAL)) { 887 /* 888 * XXX disconnections would be too hard to 889 * get rid of without this kludge. The only 890 * way to get rid of controlling terminals 891 * is to exit from the session leader. 892 */ 893 CLR(tp->t_state, TS_ZOMBIE); 894 895 wakeup(TSA_CARR_ON(tp)); 896 ttwakeup(tp); 897 ttwwakeup(tp); 898 } 899 if ((ISSET(tp->t_state, TS_CARR_ON) || 900 ISSET(t->c_cflag, CLOCAL)) && 901 !ISSET(tp->t_state, TS_ZOMBIE)) 902 SET(tp->t_state, TS_CONNECTED); 903 else 904 CLR(tp->t_state, TS_CONNECTED); 905 tp->t_cflag = t->c_cflag; 906 tp->t_ispeed = t->c_ispeed; 907 if (t->c_ospeed != 0) 908 tp->t_ospeed = t->c_ospeed; 909 ttsetwater(tp); 910 } 911 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) && 912 cmd != TIOCSETAF) { 913 if (ISSET(t->c_lflag, ICANON)) 914 SET(tp->t_lflag, PENDIN); 915 else { 916 /* 917 * XXX we really shouldn't allow toggling 918 * ICANON while we're in a non-termios line 919 * discipline. Now we have to worry about 920 * panicing for a null queue. 921 */ 922 if (tp->t_canq.c_cbreserved > 0 && 923 tp->t_rawq.c_cbreserved > 0) { 924 catq(&tp->t_rawq, &tp->t_canq); 925 /* 926 * XXX the queue limits may be 927 * different, so the old queue 928 * swapping method no longer works. 929 */ 930 catq(&tp->t_canq, &tp->t_rawq); 931 } 932 CLR(tp->t_lflag, PENDIN); 933 } 934 ttwakeup(tp); 935 } 936 tp->t_iflag = t->c_iflag; 937 tp->t_oflag = t->c_oflag; 938 /* 939 * Make the EXTPROC bit read only. 940 */ 941 if (ISSET(tp->t_lflag, EXTPROC)) 942 SET(t->c_lflag, EXTPROC); 943 else 944 CLR(t->c_lflag, EXTPROC); 945 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 946 if (t->c_cc[VMIN] != tp->t_cc[VMIN] || 947 t->c_cc[VTIME] != tp->t_cc[VTIME]) 948 ttwakeup(tp); 949 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 950 splx(s); 951 break; 952 } 953 case TIOCSETD: { /* set line discipline */ 954 register int t = *(int *)data; 955 dev_t device = tp->t_dev; 956 957 if ((u_int)t >= nlinesw) 958 return (ENXIO); 959 if (t != tp->t_line) { 960 s = spltty(); 961 (*linesw[tp->t_line].l_close)(tp, flag); 962 error = (*linesw[t].l_open)(device, tp); 963 if (error) { 964 (void)(*linesw[tp->t_line].l_open)(device, tp); 965 splx(s); 966 return (error); 967 } 968 tp->t_line = t; 969 splx(s); 970 } 971 break; 972 } 973 case TIOCSTART: /* start output, like ^Q */ 974 s = spltty(); 975 if (ISSET(tp->t_state, TS_TTSTOP) || 976 ISSET(tp->t_lflag, FLUSHO)) { 977 CLR(tp->t_lflag, FLUSHO); 978 CLR(tp->t_state, TS_TTSTOP); 979 ttstart(tp); 980 } 981 splx(s); 982 break; 983 case TIOCSTI: /* simulate terminal input */ 984 if ((flag & FREAD) == 0 && suser(p)) 985 return (EPERM); 986 if (!isctty(p, tp) && suser(p)) 987 return (EACCES); 988 s = spltty(); 989 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 990 splx(s); 991 break; 992 case TIOCSTOP: /* stop output, like ^S */ 993 s = spltty(); 994 if (!ISSET(tp->t_state, TS_TTSTOP)) { 995 SET(tp->t_state, TS_TTSTOP); 996 (*tp->t_stop)(tp, 0); 997 } 998 splx(s); 999 break; 1000 case TIOCSCTTY: /* become controlling tty */ 1001 /* Session ctty vnode pointer set in vnode layer. */ 1002 if (!SESS_LEADER(p) || 1003 ((p->p_session->s_ttyvp || tp->t_session) && 1004 (tp->t_session != p->p_session))) 1005 return (EPERM); 1006 tp->t_session = p->p_session; 1007 tp->t_pgrp = p->p_pgrp; 1008 p->p_session->s_ttyp = tp; 1009 p->p_flag |= P_CONTROLT; 1010 break; 1011 case TIOCSPGRP: { /* set pgrp of tty */ 1012 register struct pgrp *pgrp = pgfind(*(int *)data); 1013 1014 if (!isctty(p, tp)) 1015 return (ENOTTY); 1016 else if (pgrp == NULL || pgrp->pg_session != p->p_session) 1017 return (EPERM); 1018 tp->t_pgrp = pgrp; 1019 break; 1020 } 1021 case TIOCSTAT: /* simulate control-T */ 1022 s = spltty(); 1023 ttyinfo(tp); 1024 splx(s); 1025 break; 1026 case TIOCSWINSZ: /* set window size */ 1027 if (bcmp((caddr_t)&tp->t_winsize, data, 1028 sizeof (struct winsize))) { 1029 tp->t_winsize = *(struct winsize *)data; 1030 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1031 } 1032 break; 1033 case TIOCSDRAINWAIT: 1034 error = suser(p); 1035 if (error) 1036 return (error); 1037 tp->t_timeout = *(int *)data * hz; 1038 wakeup(TSA_OCOMPLETE(tp)); 1039 wakeup(TSA_OLOWAT(tp)); 1040 break; 1041 case TIOCGDRAINWAIT: 1042 *(int *)data = tp->t_timeout / hz; 1043 break; 1044 default: 1045#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1046 return (ttcompat(tp, cmd, data, flag)); 1047#else 1048 return (ENOIOCTL); 1049#endif 1050 } 1051 return (0); 1052} 1053 1054int 1055ttypoll(dev, events, p) 1056 dev_t dev; 1057 int events; 1058 struct proc *p; 1059{ 1060 int s; 1061 int revents = 0; 1062 struct tty *tp; 1063 1064 tp = dev->si_tty; 1065 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */ 1066 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) 1067 | POLLHUP); 1068 1069 s = spltty(); 1070 if (events & (POLLIN | POLLRDNORM)) { 1071 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) 1072 revents |= events & (POLLIN | POLLRDNORM); 1073 else 1074 selrecord(p, &tp->t_rsel); 1075 } 1076 if (events & (POLLOUT | POLLWRNORM)) { 1077 if ((tp->t_outq.c_cc <= tp->t_olowat && 1078 ISSET(tp->t_state, TS_CONNECTED)) 1079 || ISSET(tp->t_state, TS_ZOMBIE)) 1080 revents |= events & (POLLOUT | POLLWRNORM); 1081 else 1082 selrecord(p, &tp->t_wsel); 1083 } 1084 splx(s); 1085 return (revents); 1086} 1087 1088/* 1089 * Must be called at spltty(). 1090 */ 1091static int 1092ttnread(tp) 1093 struct tty *tp; 1094{ 1095 int nread; 1096 1097 if (ISSET(tp->t_lflag, PENDIN)) 1098 ttypend(tp); 1099 nread = tp->t_canq.c_cc; 1100 if (!ISSET(tp->t_lflag, ICANON)) { 1101 nread += tp->t_rawq.c_cc; 1102 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) 1103 nread = 0; 1104 } 1105 return (nread); 1106} 1107 1108/* 1109 * Wait for output to drain. 1110 */ 1111int 1112ttywait(tp) 1113 register struct tty *tp; 1114{ 1115 int error, s; 1116 1117 error = 0; 1118 s = spltty(); 1119 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1120 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) { 1121 (*tp->t_oproc)(tp); 1122 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1123 ISSET(tp->t_state, TS_CONNECTED)) { 1124 SET(tp->t_state, TS_SO_OCOMPLETE); 1125 error = ttysleep(tp, TSA_OCOMPLETE(tp), 1126 TTOPRI | PCATCH, "ttywai", 1127 tp->t_timeout); 1128 if (error) { 1129 if (error == EWOULDBLOCK) 1130 error = EIO; 1131 break; 1132 } 1133 } else 1134 break; 1135 } 1136 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY))) 1137 error = EIO; 1138 splx(s); 1139 return (error); 1140} 1141 1142/* 1143 * Flush if successfully wait. 1144 */ 1145static int 1146ttywflush(tp) 1147 struct tty *tp; 1148{ 1149 int error; 1150 1151 if ((error = ttywait(tp)) == 0) 1152 ttyflush(tp, FREAD); 1153 return (error); 1154} 1155 1156/* 1157 * Flush tty read and/or write queues, notifying anyone waiting. 1158 */ 1159void 1160ttyflush(tp, rw) 1161 register struct tty *tp; 1162 int rw; 1163{ 1164 register int s; 1165 1166 s = spltty(); 1167#if 0 1168again: 1169#endif 1170 if (rw & FWRITE) { 1171 FLUSHQ(&tp->t_outq); 1172 CLR(tp->t_state, TS_TTSTOP); 1173 } 1174 (*tp->t_stop)(tp, rw); 1175 if (rw & FREAD) { 1176 FLUSHQ(&tp->t_canq); 1177 FLUSHQ(&tp->t_rawq); 1178 CLR(tp->t_lflag, PENDIN); 1179 tp->t_rocount = 0; 1180 tp->t_rocol = 0; 1181 CLR(tp->t_state, TS_LOCAL); 1182 ttwakeup(tp); 1183 if (ISSET(tp->t_state, TS_TBLOCK)) { 1184 if (rw & FWRITE) 1185 FLUSHQ(&tp->t_outq); 1186 ttyunblock(tp); 1187 1188 /* 1189 * Don't let leave any state that might clobber the 1190 * next line discipline (although we should do more 1191 * to send the START char). Not clearing the state 1192 * may have caused the "putc to a clist with no 1193 * reserved cblocks" panic/printf. 1194 */ 1195 CLR(tp->t_state, TS_TBLOCK); 1196 1197#if 0 /* forget it, sleeping isn't always safe and we don't know when it is */ 1198 if (ISSET(tp->t_iflag, IXOFF)) { 1199 /* 1200 * XXX wait a bit in the hope that the stop 1201 * character (if any) will go out. Waiting 1202 * isn't good since it allows races. This 1203 * will be fixed when the stop character is 1204 * put in a special queue. Don't bother with 1205 * the checks in ttywait() since the timeout 1206 * will save us. 1207 */ 1208 SET(tp->t_state, TS_SO_OCOMPLETE); 1209 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI, 1210 "ttyfls", hz / 10); 1211 /* 1212 * Don't try sending the stop character again. 1213 */ 1214 CLR(tp->t_state, TS_TBLOCK); 1215 goto again; 1216 } 1217#endif 1218 } 1219 } 1220 if (rw & FWRITE) { 1221 FLUSHQ(&tp->t_outq); 1222 ttwwakeup(tp); 1223 } 1224 splx(s); 1225} 1226 1227/* 1228 * Copy in the default termios characters. 1229 */ 1230void 1231termioschars(t) 1232 struct termios *t; 1233{ 1234 1235 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc); 1236} 1237 1238/* 1239 * Old interface. 1240 */ 1241void 1242ttychars(tp) 1243 struct tty *tp; 1244{ 1245 1246 termioschars(&tp->t_termios); 1247} 1248 1249/* 1250 * Handle input high water. Send stop character for the IXOFF case. Turn 1251 * on our input flow control bit and propagate the changes to the driver. 1252 * XXX the stop character should be put in a special high priority queue. 1253 */ 1254void 1255ttyblock(tp) 1256 struct tty *tp; 1257{ 1258 1259 SET(tp->t_state, TS_TBLOCK); 1260 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1261 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0) 1262 CLR(tp->t_state, TS_TBLOCK); /* try again later */ 1263 ttstart(tp); 1264} 1265 1266/* 1267 * Handle input low water. Send start character for the IXOFF case. Turn 1268 * off our input flow control bit and propagate the changes to the driver. 1269 * XXX the start character should be put in a special high priority queue. 1270 */ 1271static void 1272ttyunblock(tp) 1273 struct tty *tp; 1274{ 1275 1276 CLR(tp->t_state, TS_TBLOCK); 1277 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE && 1278 putc(tp->t_cc[VSTART], &tp->t_outq) != 0) 1279 SET(tp->t_state, TS_TBLOCK); /* try again later */ 1280 ttstart(tp); 1281} 1282 1283#ifdef notyet 1284/* Not used by any current (i386) drivers. */ 1285/* 1286 * Restart after an inter-char delay. 1287 */ 1288void 1289ttrstrt(tp_arg) 1290 void *tp_arg; 1291{ 1292 struct tty *tp; 1293 int s; 1294 1295 KASSERT(tp_arg != NULL, ("ttrstrt")); 1296 1297 tp = tp_arg; 1298 s = spltty(); 1299 1300 CLR(tp->t_state, TS_TIMEOUT); 1301 ttstart(tp); 1302 1303 splx(s); 1304} 1305#endif 1306 1307int 1308ttstart(tp) 1309 struct tty *tp; 1310{ 1311 1312 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1313 (*tp->t_oproc)(tp); 1314 return (0); 1315} 1316 1317/* 1318 * "close" a line discipline 1319 */ 1320int 1321ttylclose(tp, flag) 1322 struct tty *tp; 1323 int flag; 1324{ 1325 1326 if (flag & FNONBLOCK || ttywflush(tp)) 1327 ttyflush(tp, FREAD | FWRITE); 1328 return (0); 1329} 1330 1331/* 1332 * Handle modem control transition on a tty. 1333 * Flag indicates new state of carrier. 1334 * Returns 0 if the line should be turned off, otherwise 1. 1335 */ 1336int 1337ttymodem(tp, flag) 1338 register struct tty *tp; 1339 int flag; 1340{ 1341 1342 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) { 1343 /* 1344 * MDMBUF: do flow control according to carrier flag 1345 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP 1346 * works if IXON and IXANY are clear. 1347 */ 1348 if (flag) { 1349 CLR(tp->t_state, TS_CAR_OFLOW); 1350 CLR(tp->t_state, TS_TTSTOP); 1351 ttstart(tp); 1352 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) { 1353 SET(tp->t_state, TS_CAR_OFLOW); 1354 SET(tp->t_state, TS_TTSTOP); 1355 (*tp->t_stop)(tp, 0); 1356 } 1357 } else if (flag == 0) { 1358 /* 1359 * Lost carrier. 1360 */ 1361 CLR(tp->t_state, TS_CARR_ON); 1362 if (ISSET(tp->t_state, TS_ISOPEN) && 1363 !ISSET(tp->t_cflag, CLOCAL)) { 1364 SET(tp->t_state, TS_ZOMBIE); 1365 CLR(tp->t_state, TS_CONNECTED); 1366 if (tp->t_session && tp->t_session->s_leader) 1367 psignal(tp->t_session->s_leader, SIGHUP); 1368 ttyflush(tp, FREAD | FWRITE); 1369 return (0); 1370 } 1371 } else { 1372 /* 1373 * Carrier now on. 1374 */ 1375 SET(tp->t_state, TS_CARR_ON); 1376 if (!ISSET(tp->t_state, TS_ZOMBIE)) 1377 SET(tp->t_state, TS_CONNECTED); 1378 wakeup(TSA_CARR_ON(tp)); 1379 ttwakeup(tp); 1380 ttwwakeup(tp); 1381 } 1382 return (1); 1383} 1384 1385/* 1386 * Reinput pending characters after state switch 1387 * call at spltty(). 1388 */ 1389static void 1390ttypend(tp) 1391 register struct tty *tp; 1392{ 1393 struct clist tq; 1394 register int c; 1395 1396 CLR(tp->t_lflag, PENDIN); 1397 SET(tp->t_state, TS_TYPEN); 1398 /* 1399 * XXX this assumes too much about clist internals. It may even 1400 * fail if the cblock slush pool is empty. We can't allocate more 1401 * cblocks here because we are called from an interrupt handler 1402 * and clist_alloc_cblocks() can wait. 1403 */ 1404 tq = tp->t_rawq; 1405 bzero(&tp->t_rawq, sizeof tp->t_rawq); 1406 tp->t_rawq.c_cbmax = tq.c_cbmax; 1407 tp->t_rawq.c_cbreserved = tq.c_cbreserved; 1408 while ((c = getc(&tq)) >= 0) 1409 ttyinput(c, tp); 1410 CLR(tp->t_state, TS_TYPEN); 1411} 1412 1413/* 1414 * Process a read call on a tty device. 1415 */ 1416int 1417ttread(tp, uio, flag) 1418 register struct tty *tp; 1419 struct uio *uio; 1420 int flag; 1421{ 1422 register struct clist *qp; 1423 register int c; 1424 register tcflag_t lflag; 1425 register cc_t *cc = tp->t_cc; 1426 register struct proc *p = curproc; 1427 int s, first, error = 0; 1428 int has_stime = 0, last_cc = 0; 1429 long slp = 0; /* XXX this should be renamed `timo'. */ 1430 struct timeval stime; 1431 1432loop: 1433 s = spltty(); 1434 lflag = tp->t_lflag; 1435 /* 1436 * take pending input first 1437 */ 1438 if (ISSET(lflag, PENDIN)) { 1439 ttypend(tp); 1440 splx(s); /* reduce latency */ 1441 s = spltty(); 1442 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */ 1443 } 1444 1445 /* 1446 * Hang process if it's in the background. 1447 */ 1448 if (isbackground(p, tp)) { 1449 splx(s); 1450 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 1451 SIGISMEMBER(p->p_sigmask, SIGTTIN) || 1452 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0) 1453 return (EIO); 1454 pgsignal(p->p_pgrp, SIGTTIN, 1); 1455 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0); 1456 if (error) 1457 return (error); 1458 goto loop; 1459 } 1460 1461 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1462 splx(s); 1463 return (0); /* EOF */ 1464 } 1465 1466 /* 1467 * If canonical, use the canonical queue, 1468 * else use the raw queue. 1469 * 1470 * (should get rid of clists...) 1471 */ 1472 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 1473 1474 if (flag & IO_NDELAY) { 1475 if (qp->c_cc > 0) 1476 goto read; 1477 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) { 1478 splx(s); 1479 return (0); 1480 } 1481 splx(s); 1482 return (EWOULDBLOCK); 1483 } 1484 if (!ISSET(lflag, ICANON)) { 1485 int m = cc[VMIN]; 1486 long t = cc[VTIME]; 1487 struct timeval timecopy; 1488 1489 /* 1490 * Check each of the four combinations. 1491 * (m > 0 && t == 0) is the normal read case. 1492 * It should be fairly efficient, so we check that and its 1493 * companion case (m == 0 && t == 0) first. 1494 * For the other two cases, we compute the target sleep time 1495 * into slp. 1496 */ 1497 if (t == 0) { 1498 if (qp->c_cc < m) 1499 goto sleep; 1500 if (qp->c_cc > 0) 1501 goto read; 1502 1503 /* m, t and qp->c_cc are all 0. 0 is enough input. */ 1504 splx(s); 1505 return (0); 1506 } 1507 t *= 100000; /* time in us */ 1508#define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1509 ((t1).tv_usec - (t2).tv_usec)) 1510 if (m > 0) { 1511 if (qp->c_cc <= 0) 1512 goto sleep; 1513 if (qp->c_cc >= m) 1514 goto read; 1515 getmicrotime(&timecopy); 1516 if (!has_stime) { 1517 /* first character, start timer */ 1518 has_stime = 1; 1519 stime = timecopy; 1520 slp = t; 1521 } else if (qp->c_cc > last_cc) { 1522 /* got a character, restart timer */ 1523 stime = timecopy; 1524 slp = t; 1525 } else { 1526 /* nothing, check expiration */ 1527 slp = t - diff(timecopy, stime); 1528 if (slp <= 0) 1529 goto read; 1530 } 1531 last_cc = qp->c_cc; 1532 } else { /* m == 0 */ 1533 if (qp->c_cc > 0) 1534 goto read; 1535 getmicrotime(&timecopy); 1536 if (!has_stime) { 1537 has_stime = 1; 1538 stime = timecopy; 1539 slp = t; 1540 } else { 1541 slp = t - diff(timecopy, stime); 1542 if (slp <= 0) { 1543 /* Timed out, but 0 is enough input. */ 1544 splx(s); 1545 return (0); 1546 } 1547 } 1548 } 1549#undef diff 1550 /* 1551 * Rounding down may make us wake up just short 1552 * of the target, so we round up. 1553 * The formula is ceiling(slp * hz/1000000). 1554 * 32-bit arithmetic is enough for hz < 169. 1555 * XXX see tvtohz() for how to avoid overflow if hz 1556 * is large (divide by `tick' and/or arrange to 1557 * use tvtohz() if hz is large). 1558 */ 1559 slp = (long) (((u_long)slp * hz) + 999999) / 1000000; 1560 goto sleep; 1561 } 1562 if (qp->c_cc <= 0) { 1563sleep: 1564 /* 1565 * There is no input, or not enough input and we can block. 1566 */ 1567 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH, 1568 ISSET(tp->t_state, TS_CONNECTED) ? 1569 "ttyin" : "ttyhup", (int)slp); 1570 splx(s); 1571 if (error == EWOULDBLOCK) 1572 error = 0; 1573 else if (error) 1574 return (error); 1575 /* 1576 * XXX what happens if another process eats some input 1577 * while we are asleep (not just here)? It would be 1578 * safest to detect changes and reset our state variables 1579 * (has_stime and last_cc). 1580 */ 1581 slp = 0; 1582 goto loop; 1583 } 1584read: 1585 splx(s); 1586 /* 1587 * Input present, check for input mapping and processing. 1588 */ 1589 first = 1; 1590 if (ISSET(lflag, ICANON | ISIG)) 1591 goto slowcase; 1592 for (;;) { 1593 char ibuf[IBUFSIZ]; 1594 int icc; 1595 1596 icc = imin(uio->uio_resid, IBUFSIZ); 1597 icc = q_to_b(qp, ibuf, icc); 1598 if (icc <= 0) { 1599 if (first) 1600 goto loop; 1601 break; 1602 } 1603 error = uiomove(ibuf, icc, uio); 1604 /* 1605 * XXX if there was an error then we should ungetc() the 1606 * unmoved chars and reduce icc here. 1607 */ 1608#if NSNP > 0 1609 if (ISSET(tp->t_lflag, ECHO) && 1610 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1611 snpin((struct snoop *)tp->t_sc, ibuf, icc); 1612#endif 1613 if (error) 1614 break; 1615 if (uio->uio_resid == 0) 1616 break; 1617 first = 0; 1618 } 1619 goto out; 1620slowcase: 1621 for (;;) { 1622 c = getc(qp); 1623 if (c < 0) { 1624 if (first) 1625 goto loop; 1626 break; 1627 } 1628 /* 1629 * delayed suspend (^Y) 1630 */ 1631 if (CCEQ(cc[VDSUSP], c) && 1632 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) { 1633 pgsignal(tp->t_pgrp, SIGTSTP, 1); 1634 if (first) { 1635 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, 1636 "ttybg3", 0); 1637 if (error) 1638 break; 1639 goto loop; 1640 } 1641 break; 1642 } 1643 /* 1644 * Interpret EOF only in canonical mode. 1645 */ 1646 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 1647 break; 1648 /* 1649 * Give user character. 1650 */ 1651 error = ureadc(c, uio); 1652 if (error) 1653 /* XXX should ungetc(c, qp). */ 1654 break; 1655#if NSNP > 0 1656 /* 1657 * Only snoop directly on input in echo mode. Non-echoed 1658 * input will be snooped later iff the application echoes it. 1659 */ 1660 if (ISSET(tp->t_lflag, ECHO) && 1661 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1662 snpinc((struct snoop *)tp->t_sc, (char)c); 1663#endif 1664 if (uio->uio_resid == 0) 1665 break; 1666 /* 1667 * In canonical mode check for a "break character" 1668 * marking the end of a "line of input". 1669 */ 1670 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag)) 1671 break; 1672 first = 0; 1673 } 1674 1675out: 1676 /* 1677 * Look to unblock input now that (presumably) 1678 * the input queue has gone down. 1679 */ 1680 s = spltty(); 1681 if (ISSET(tp->t_state, TS_TBLOCK) && 1682 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat) 1683 ttyunblock(tp); 1684 splx(s); 1685 1686 return (error); 1687} 1688 1689/* 1690 * Check the output queue on tp for space for a kernel message (from uprintf 1691 * or tprintf). Allow some space over the normal hiwater mark so we don't 1692 * lose messages due to normal flow control, but don't let the tty run amok. 1693 * Sleeps here are not interruptible, but we return prematurely if new signals 1694 * arrive. 1695 */ 1696int 1697ttycheckoutq(tp, wait) 1698 register struct tty *tp; 1699 int wait; 1700{ 1701 int hiwat, s; 1702 sigset_t oldmask; 1703 1704 hiwat = tp->t_ohiwat; 1705 SIGEMPTYSET(oldmask); 1706 s = spltty(); 1707 if (wait) 1708 oldmask = curproc->p_siglist; 1709 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100) 1710 while (tp->t_outq.c_cc > hiwat) { 1711 ttstart(tp); 1712 if (tp->t_outq.c_cc <= hiwat) 1713 break; 1714 if (!(wait && SIGSETEQ(curproc->p_siglist, oldmask))) { 1715 splx(s); 1716 return (0); 1717 } 1718 SET(tp->t_state, TS_SO_OLOWAT); 1719 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz); 1720 } 1721 splx(s); 1722 return (1); 1723} 1724 1725/* 1726 * Process a write call on a tty device. 1727 */ 1728int 1729ttwrite(tp, uio, flag) 1730 register struct tty *tp; 1731 register struct uio *uio; 1732 int flag; 1733{ 1734 register char *cp = NULL; 1735 register int cc, ce; 1736 register struct proc *p; 1737 int i, hiwat, cnt, error, s; 1738 char obuf[OBUFSIZ]; 1739 1740 hiwat = tp->t_ohiwat; 1741 cnt = uio->uio_resid; 1742 error = 0; 1743 cc = 0; 1744loop: 1745 s = spltty(); 1746 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1747 splx(s); 1748 if (uio->uio_resid == cnt) 1749 error = EIO; 1750 goto out; 1751 } 1752 if (!ISSET(tp->t_state, TS_CONNECTED)) { 1753 if (flag & IO_NDELAY) { 1754 splx(s); 1755 error = EWOULDBLOCK; 1756 goto out; 1757 } 1758 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1759 "ttydcd", 0); 1760 splx(s); 1761 if (error) 1762 goto out; 1763 goto loop; 1764 } 1765 splx(s); 1766 /* 1767 * Hang the process if it's in the background. 1768 */ 1769 p = curproc; 1770 if (isbackground(p, tp) && 1771 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) && 1772 !SIGISMEMBER(p->p_sigignore, SIGTTOU) && 1773 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) { 1774 if (p->p_pgrp->pg_jobc == 0) { 1775 error = EIO; 1776 goto out; 1777 } 1778 pgsignal(p->p_pgrp, SIGTTOU, 1); 1779 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0); 1780 if (error) 1781 goto out; 1782 goto loop; 1783 } 1784 /* 1785 * Process the user's data in at most OBUFSIZ chunks. Perform any 1786 * output translation. Keep track of high water mark, sleep on 1787 * overflow awaiting device aid in acquiring new space. 1788 */ 1789 while (uio->uio_resid > 0 || cc > 0) { 1790 if (ISSET(tp->t_lflag, FLUSHO)) { 1791 uio->uio_resid = 0; 1792 return (0); 1793 } 1794 if (tp->t_outq.c_cc > hiwat) 1795 goto ovhiwat; 1796 /* 1797 * Grab a hunk of data from the user, unless we have some 1798 * leftover from last time. 1799 */ 1800 if (cc == 0) { 1801 cc = imin(uio->uio_resid, OBUFSIZ); 1802 cp = obuf; 1803 error = uiomove(cp, cc, uio); 1804 if (error) { 1805 cc = 0; 1806 break; 1807 } 1808#if NSNP > 0 1809 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1810 snpin((struct snoop *)tp->t_sc, cp, cc); 1811#endif 1812 } 1813 /* 1814 * If nothing fancy need be done, grab those characters we 1815 * can handle without any of ttyoutput's processing and 1816 * just transfer them to the output q. For those chars 1817 * which require special processing (as indicated by the 1818 * bits in char_type), call ttyoutput. After processing 1819 * a hunk of data, look for FLUSHO so ^O's will take effect 1820 * immediately. 1821 */ 1822 while (cc > 0) { 1823 if (!ISSET(tp->t_oflag, OPOST)) 1824 ce = cc; 1825 else { 1826 ce = cc - scanc((u_int)cc, (u_char *)cp, 1827 char_type, CCLASSMASK); 1828 /* 1829 * If ce is zero, then we're processing 1830 * a special character through ttyoutput. 1831 */ 1832 if (ce == 0) { 1833 tp->t_rocount = 0; 1834 if (ttyoutput(*cp, tp) >= 0) { 1835 /* No Clists, wait a bit. */ 1836 ttstart(tp); 1837 if (flag & IO_NDELAY) { 1838 error = EWOULDBLOCK; 1839 goto out; 1840 } 1841 error = ttysleep(tp, &lbolt, 1842 TTOPRI|PCATCH, 1843 "ttybf1", 0); 1844 if (error) 1845 goto out; 1846 goto loop; 1847 } 1848 cp++; 1849 cc--; 1850 if (ISSET(tp->t_lflag, FLUSHO) || 1851 tp->t_outq.c_cc > hiwat) 1852 goto ovhiwat; 1853 continue; 1854 } 1855 } 1856 /* 1857 * A bunch of normal characters have been found. 1858 * Transfer them en masse to the output queue and 1859 * continue processing at the top of the loop. 1860 * If there are any further characters in this 1861 * <= OBUFSIZ chunk, the first should be a character 1862 * requiring special handling by ttyoutput. 1863 */ 1864 tp->t_rocount = 0; 1865 i = b_to_q(cp, ce, &tp->t_outq); 1866 ce -= i; 1867 tp->t_column += ce; 1868 cp += ce, cc -= ce, tk_nout += ce; 1869 tp->t_outcc += ce; 1870 if (i > 0) { 1871 /* No Clists, wait a bit. */ 1872 ttstart(tp); 1873 if (flag & IO_NDELAY) { 1874 error = EWOULDBLOCK; 1875 goto out; 1876 } 1877 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, 1878 "ttybf2", 0); 1879 if (error) 1880 goto out; 1881 goto loop; 1882 } 1883 if (ISSET(tp->t_lflag, FLUSHO) || 1884 tp->t_outq.c_cc > hiwat) 1885 break; 1886 } 1887 ttstart(tp); 1888 } 1889out: 1890 /* 1891 * If cc is nonzero, we leave the uio structure inconsistent, as the 1892 * offset and iov pointers have moved forward, but it doesn't matter 1893 * (the call will either return short or restart with a new uio). 1894 */ 1895 uio->uio_resid += cc; 1896 return (error); 1897 1898ovhiwat: 1899 ttstart(tp); 1900 s = spltty(); 1901 /* 1902 * This can only occur if FLUSHO is set in t_lflag, 1903 * or if ttstart/oproc is synchronous (or very fast). 1904 */ 1905 if (tp->t_outq.c_cc <= hiwat) { 1906 splx(s); 1907 goto loop; 1908 } 1909 if (flag & IO_NDELAY) { 1910 splx(s); 1911 uio->uio_resid += cc; 1912 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 1913 } 1914 SET(tp->t_state, TS_SO_OLOWAT); 1915 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri", 1916 tp->t_timeout); 1917 splx(s); 1918 if (error == EWOULDBLOCK) 1919 error = EIO; 1920 if (error) 1921 goto out; 1922 goto loop; 1923} 1924 1925/* 1926 * Rubout one character from the rawq of tp 1927 * as cleanly as possible. 1928 */ 1929static void 1930ttyrub(c, tp) 1931 register int c; 1932 register struct tty *tp; 1933{ 1934 register char *cp; 1935 register int savecol; 1936 int tabc, s; 1937 1938 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 1939 return; 1940 CLR(tp->t_lflag, FLUSHO); 1941 if (ISSET(tp->t_lflag, ECHOE)) { 1942 if (tp->t_rocount == 0) { 1943 /* 1944 * Screwed by ttwrite; retype 1945 */ 1946 ttyretype(tp); 1947 return; 1948 } 1949 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 1950 ttyrubo(tp, 2); 1951 else { 1952 CLR(c, ~TTY_CHARMASK); 1953 switch (CCLASS(c)) { 1954 case ORDINARY: 1955 ttyrubo(tp, 1); 1956 break; 1957 case BACKSPACE: 1958 case CONTROL: 1959 case NEWLINE: 1960 case RETURN: 1961 case VTAB: 1962 if (ISSET(tp->t_lflag, ECHOCTL)) 1963 ttyrubo(tp, 2); 1964 break; 1965 case TAB: 1966 if (tp->t_rocount < tp->t_rawq.c_cc) { 1967 ttyretype(tp); 1968 return; 1969 } 1970 s = spltty(); 1971 savecol = tp->t_column; 1972 SET(tp->t_state, TS_CNTTB); 1973 SET(tp->t_lflag, FLUSHO); 1974 tp->t_column = tp->t_rocol; 1975 cp = tp->t_rawq.c_cf; 1976 if (cp) 1977 tabc = *cp; /* XXX FIX NEXTC */ 1978 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 1979 ttyecho(tabc, tp); 1980 CLR(tp->t_lflag, FLUSHO); 1981 CLR(tp->t_state, TS_CNTTB); 1982 splx(s); 1983 1984 /* savecol will now be length of the tab. */ 1985 savecol -= tp->t_column; 1986 tp->t_column += savecol; 1987 if (savecol > 8) 1988 savecol = 8; /* overflow screw */ 1989 while (--savecol >= 0) 1990 (void)ttyoutput('\b', tp); 1991 break; 1992 default: /* XXX */ 1993#define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 1994 (void)printf(PANICSTR, c, CCLASS(c)); 1995#ifdef notdef 1996 panic(PANICSTR, c, CCLASS(c)); 1997#endif 1998 } 1999 } 2000 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 2001 if (!ISSET(tp->t_state, TS_ERASE)) { 2002 SET(tp->t_state, TS_ERASE); 2003 (void)ttyoutput('\\', tp); 2004 } 2005 ttyecho(c, tp); 2006 } else 2007 ttyecho(tp->t_cc[VERASE], tp); 2008 --tp->t_rocount; 2009} 2010 2011/* 2012 * Back over cnt characters, erasing them. 2013 */ 2014static void 2015ttyrubo(tp, cnt) 2016 register struct tty *tp; 2017 int cnt; 2018{ 2019 2020 while (cnt-- > 0) { 2021 (void)ttyoutput('\b', tp); 2022 (void)ttyoutput(' ', tp); 2023 (void)ttyoutput('\b', tp); 2024 } 2025} 2026 2027/* 2028 * ttyretype -- 2029 * Reprint the rawq line. Note, it is assumed that c_cc has already 2030 * been checked. 2031 */ 2032static void 2033ttyretype(tp) 2034 register struct tty *tp; 2035{ 2036 register char *cp; 2037 int s, c; 2038 2039 /* Echo the reprint character. */ 2040 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 2041 ttyecho(tp->t_cc[VREPRINT], tp); 2042 2043 (void)ttyoutput('\n', tp); 2044 2045 /* 2046 * XXX 2047 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 2048 * BIT OF FIRST CHAR. 2049 */ 2050 s = spltty(); 2051 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 2052 cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 2053 ttyecho(c, tp); 2054 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 2055 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 2056 ttyecho(c, tp); 2057 CLR(tp->t_state, TS_ERASE); 2058 splx(s); 2059 2060 tp->t_rocount = tp->t_rawq.c_cc; 2061 tp->t_rocol = 0; 2062} 2063 2064/* 2065 * Echo a typed character to the terminal. 2066 */ 2067static void 2068ttyecho(c, tp) 2069 register int c; 2070 register struct tty *tp; 2071{ 2072 2073 if (!ISSET(tp->t_state, TS_CNTTB)) 2074 CLR(tp->t_lflag, FLUSHO); 2075 if ((!ISSET(tp->t_lflag, ECHO) && 2076 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) || 2077 ISSET(tp->t_lflag, EXTPROC)) 2078 return; 2079 if (ISSET(tp->t_lflag, ECHOCTL) && 2080 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') || 2081 ISSET(c, TTY_CHARMASK) == 0177)) { 2082 (void)ttyoutput('^', tp); 2083 CLR(c, ~TTY_CHARMASK); 2084 if (c == 0177) 2085 c = '?'; 2086 else 2087 c += 'A' - 1; 2088 } 2089 (void)ttyoutput(c, tp); 2090} 2091 2092/* 2093 * Wake up any readers on a tty. 2094 */ 2095void 2096ttwakeup(tp) 2097 register struct tty *tp; 2098{ 2099 2100 if (tp->t_rsel.si_pid != 0) 2101 selwakeup(&tp->t_rsel); 2102 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2103 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2104 wakeup(TSA_HUP_OR_INPUT(tp)); 2105} 2106 2107/* 2108 * Wake up any writers on a tty. 2109 */ 2110void 2111ttwwakeup(tp) 2112 register struct tty *tp; 2113{ 2114 2115 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat) 2116 selwakeup(&tp->t_wsel); 2117 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == 2118 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { 2119 CLR(tp->t_state, TS_SO_OCOMPLETE); 2120 wakeup(TSA_OCOMPLETE(tp)); 2121 } 2122 if (ISSET(tp->t_state, TS_SO_OLOWAT) && 2123 tp->t_outq.c_cc <= tp->t_olowat) { 2124 CLR(tp->t_state, TS_SO_OLOWAT); 2125 wakeup(TSA_OLOWAT(tp)); 2126 } 2127} 2128 2129/* 2130 * Look up a code for a specified speed in a conversion table; 2131 * used by drivers to map software speed values to hardware parameters. 2132 */ 2133int 2134ttspeedtab(speed, table) 2135 int speed; 2136 register struct speedtab *table; 2137{ 2138 2139 for ( ; table->sp_speed != -1; table++) 2140 if (table->sp_speed == speed) 2141 return (table->sp_code); 2142 return (-1); 2143} 2144 2145/* 2146 * Set input and output watermarks and buffer sizes. For input, the 2147 * high watermark is about one second's worth of input above empty, the 2148 * low watermark is slightly below high water, and the buffer size is a 2149 * driver-dependent amount above high water. For output, the watermarks 2150 * are near the ends of the buffer, with about 1 second's worth of input 2151 * between them. All this only applies to the standard line discipline. 2152 */ 2153void 2154ttsetwater(tp) 2155 struct tty *tp; 2156{ 2157 register int cps, ttmaxhiwat, x; 2158 2159 /* Input. */ 2160 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512); 2161 switch (tp->t_ispeedwat) { 2162 case (speed_t)-1: 2163 cps = tp->t_ispeed / 10; 2164 break; 2165 case 0: 2166 /* 2167 * This case is for old drivers that don't know about 2168 * t_ispeedwat. Arrange for them to get the old buffer 2169 * sizes and watermarks. 2170 */ 2171 cps = TTYHOG - 2 * 256; 2172 tp->t_ififosize = 2 * 256; 2173 break; 2174 default: 2175 cps = tp->t_ispeedwat / 10; 2176 break; 2177 } 2178 tp->t_ihiwat = cps; 2179 tp->t_ilowat = 7 * cps / 8; 2180 x = cps + tp->t_ififosize; 2181 clist_alloc_cblocks(&tp->t_rawq, x, x); 2182 2183 /* Output. */ 2184 switch (tp->t_ospeedwat) { 2185 case (speed_t)-1: 2186 cps = tp->t_ospeed / 10; 2187 ttmaxhiwat = 2 * TTMAXHIWAT; 2188 break; 2189 case 0: 2190 cps = tp->t_ospeed / 10; 2191 ttmaxhiwat = TTMAXHIWAT; 2192 break; 2193 default: 2194 cps = tp->t_ospeedwat / 10; 2195 ttmaxhiwat = 8 * TTMAXHIWAT; 2196 break; 2197 } 2198#define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 2199 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 2200 x += cps; 2201 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */ 2202 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */ 2203 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */ 2204 x += OBUFSIZ + 100; 2205 clist_alloc_cblocks(&tp->t_outq, x, x); 2206#undef CLAMP 2207} 2208 2209/* 2210 * Report on state of foreground process group. 2211 */ 2212void 2213ttyinfo(tp) 2214 register struct tty *tp; 2215{ 2216 register struct proc *p, *pick; 2217 struct timeval utime, stime; 2218 int tmp; 2219 2220 if (ttycheckoutq(tp,0) == 0) 2221 return; 2222 2223 /* Print load average. */ 2224 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 2225 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 2226 2227 if (tp->t_session == NULL) 2228 ttyprintf(tp, "not a controlling terminal\n"); 2229 else if (tp->t_pgrp == NULL) 2230 ttyprintf(tp, "no foreground process group\n"); 2231 else if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0) 2232 ttyprintf(tp, "empty foreground process group\n"); 2233 else { 2234 /* Pick interesting process. */ 2235 for (pick = NULL; p != 0; p = LIST_NEXT(p, p_pglist)) 2236 if (proc_compare(pick, p)) 2237 pick = p; 2238 2239 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 2240 pick->p_stat == SRUN ? "running" : 2241 pick->p_wmesg ? pick->p_wmesg : "iowait"); 2242 2243 if (pick->p_flag & P_INMEM) { 2244 calcru(pick, &utime, &stime, NULL); 2245 2246 /* Print user time. */ 2247 ttyprintf(tp, "%ld.%02ldu ", 2248 utime.tv_sec, utime.tv_usec / 10000); 2249 2250 /* Print system time. */ 2251 ttyprintf(tp, "%ld.%02lds ", 2252 stime.tv_sec, stime.tv_usec / 10000); 2253 } else 2254 ttyprintf(tp, "?.??u ?.??s "); 2255 2256 /* Print percentage cpu, resident set size. */ 2257 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT; 2258 ttyprintf(tp, "%d%% %ldk\n", 2259 tmp / 100, 2260 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 : 2261 (long)pgtok(vmspace_resident_count(pick->p_vmspace))); 2262 } 2263 tp->t_rocount = 0; /* so pending input will be retyped if BS */ 2264} 2265 2266/* 2267 * Returns 1 if p2 is "better" than p1 2268 * 2269 * The algorithm for picking the "interesting" process is thus: 2270 * 2271 * 1) Only foreground processes are eligible - implied. 2272 * 2) Runnable processes are favored over anything else. The runner 2273 * with the highest cpu utilization is picked (p_estcpu). Ties are 2274 * broken by picking the highest pid. 2275 * 3) The sleeper with the shortest sleep time is next. With ties, 2276 * we pick out just "short-term" sleepers (P_SINTR == 0). 2277 * 4) Further ties are broken by picking the highest pid. 2278 */ 2279#define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 2280#define TESTAB(a, b) ((a)<<1 | (b)) 2281#define ONLYA 2 2282#define ONLYB 1 2283#define BOTH 3 2284 2285static int 2286proc_compare(p1, p2) 2287 register struct proc *p1, *p2; 2288{ 2289 2290 if (p1 == NULL) 2291 return (1); 2292 /* 2293 * see if at least one of them is runnable 2294 */ 2295 switch (TESTAB(ISRUN(p1), ISRUN(p2))) { 2296 case ONLYA: 2297 return (0); 2298 case ONLYB: 2299 return (1); 2300 case BOTH: 2301 /* 2302 * tie - favor one with highest recent cpu utilization 2303 */ 2304 if (p2->p_estcpu > p1->p_estcpu) 2305 return (1); 2306 if (p1->p_estcpu > p2->p_estcpu) 2307 return (0); 2308 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2309 } 2310 /* 2311 * weed out zombies 2312 */ 2313 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { 2314 case ONLYA: 2315 return (1); 2316 case ONLYB: 2317 return (0); 2318 case BOTH: 2319 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2320 } 2321 /* 2322 * pick the one with the smallest sleep time 2323 */ 2324 if (p2->p_slptime > p1->p_slptime) 2325 return (0); 2326 if (p1->p_slptime > p2->p_slptime) 2327 return (1); 2328 /* 2329 * favor one sleeping in a non-interruptible sleep 2330 */ 2331 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0) 2332 return (1); 2333 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0) 2334 return (0); 2335 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2336} 2337 2338/* 2339 * Output char to tty; console putchar style. 2340 */ 2341int 2342tputchar(c, tp) 2343 int c; 2344 struct tty *tp; 2345{ 2346 register int s; 2347 2348 s = spltty(); 2349 if (!ISSET(tp->t_state, TS_CONNECTED)) { 2350 splx(s); 2351 return (-1); 2352 } 2353 if (c == '\n') 2354 (void)ttyoutput('\r', tp); 2355 (void)ttyoutput(c, tp); 2356 ttstart(tp); 2357 splx(s); 2358 return (0); 2359} 2360 2361/* 2362 * Sleep on chan, returning ERESTART if tty changed while we napped and 2363 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If 2364 * the tty is revoked, restarting a pending call will redo validation done 2365 * at the start of the call. 2366 */ 2367int 2368ttysleep(tp, chan, pri, wmesg, timo) 2369 struct tty *tp; 2370 void *chan; 2371 int pri, timo; 2372 char *wmesg; 2373{ 2374 int error; 2375 int gen; 2376 2377 gen = tp->t_gen; 2378 error = tsleep(chan, pri, wmesg, timo); 2379 if (error) 2380 return (error); 2381 return (tp->t_gen == gen ? 0 : ERESTART); 2382} 2383 2384/* 2385 * Allocate a tty struct. Clists in the struct will be allocated by 2386 * ttyopen(). 2387 */ 2388struct tty * 2389ttymalloc(tp) 2390 struct tty *tp; 2391{ 2392 2393 if (tp) 2394 return(tp); 2395 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK); 2396 bzero(tp, sizeof *tp); 2397 ttyregister(tp); 2398 return (tp); 2399} 2400 2401#if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */ 2402/* 2403 * Free a tty struct. Clists in the struct should have been freed by 2404 * ttyclose(). 2405 */ 2406void 2407ttyfree(tp) 2408 struct tty *tp; 2409{ 2410 free(tp, M_TTYS); 2411} 2412#endif /* 0 */ 2413 2414void 2415ttyregister(tp) 2416 struct tty *tp; 2417{ 2418 SLIST_INSERT_HEAD(&tty_list, tp, t_list); 2419} 2420 2421static int 2422sysctl_kern_ttys SYSCTL_HANDLER_ARGS 2423{ 2424 int error; 2425 struct tty *tp, t; 2426 SLIST_FOREACH(tp, &tty_list, t_list) { 2427 t = *tp; 2428 if (t.t_dev) 2429 t.t_dev = (dev_t)dev2udev(t.t_dev); 2430 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t)); 2431 if (error) 2432 return (error); 2433 } 2434 return (0); 2435} 2436 2437SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD, 2438 0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys"); 2439 2440void 2441nottystop(tp, rw) 2442 struct tty *tp; 2443 int rw; 2444{ 2445 2446 return; 2447} 2448 2449int 2450ttyread(dev, uio, flag) 2451 dev_t dev; 2452 struct uio *uio; 2453 int flag; 2454{ 2455 struct tty *tp; 2456 2457 tp = dev->si_tty; 2458 if (tp == NULL) 2459 return (ENODEV); 2460 return ((*linesw[tp->t_line].l_read)(tp, uio, flag)); 2461} 2462 2463int 2464ttywrite(dev, uio, flag) 2465 dev_t dev; 2466 struct uio *uio; 2467 int flag; 2468{ 2469 struct tty *tp; 2470 2471 tp = dev->si_tty; 2472 if (tp == NULL) 2473 return (ENODEV); 2474 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2475} 2476