tty.c revision 51658
1963Sats/*- 24435Sgibbs * Copyright (c) 1982, 1986, 1990, 1991, 1993 38876Srgrimes * The Regents of the University of California. All rights reserved. 4963Sats * (c) UNIX System Laboratories, Inc. 54435Sgibbs * All or some portions of this file are derived from material licensed 64435Sgibbs * to the University of California by American Telephone and Telegraph 74435Sgibbs * Co. or Unix System Laboratories, Inc. and are reproduced herein with 84435Sgibbs * the permission of UNIX System Laboratories, Inc. 913765Smpp * 108876Srgrimes * Redistribution and use in source and binary forms, with or without 114435Sgibbs * modification, are permitted provided that the following conditions 124435Sgibbs * are met: 134435Sgibbs * 1. Redistributions of source code must retain the above copyright 144435Sgibbs * notice, this list of conditions and the following disclaimer. 154435Sgibbs * 2. Redistributions in binary form must reproduce the above copyright 164435Sgibbs * notice, this list of conditions and the following disclaimer in the 174435Sgibbs * documentation and/or other materials provided with the distribution. 184435Sgibbs * 3. All advertising materials mentioning features or use of this software 194435Sgibbs * must display the following acknowledgement: 204435Sgibbs * This product includes software developed by the University of 218876Srgrimes * California, Berkeley and its contributors. 2250477Speter * 4. Neither the name of the University nor the names of its contributors 237510Sjkh * may be used to endorse or promote products derived from this software 244435Sgibbs * without specific prior written permission. 254435Sgibbs * 2651673Smdodd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2751673Smdodd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2816374Snate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2951673Smdodd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3051673Smdodd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3116374Snate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3216374Snate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 334435Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 344435Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 354435Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 364435Sgibbs * SUCH DAMAGE. 374435Sgibbs * 38117700Smarkm * @(#)tty.c 8.8 (Berkeley) 1/21/94 3930398Sitojun * $FreeBSD: head/sys/kern/tty.c 51658 1999-09-25 18:24:47Z phk $ 404435Sgibbs */ 414435Sgibbs 424435Sgibbs/*- 434435Sgibbs * TODO: 444435Sgibbs * o Fix races for sending the start char in ttyflush(). 4514259Sgibbs * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect(). 464435Sgibbs * With luck, there will be MIN chars before select() returns(). 474435Sgibbs * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it. 484435Sgibbs * o Don't allow input in TS_ZOMBIE case. It would be visible through 494435Sgibbs * FIONREAD. 504435Sgibbs * o Do the new sio locking stuff here and use it to avoid special 514435Sgibbs * case for EXTPROC? 524435Sgibbs * o Lock PENDIN too? 534435Sgibbs * o Move EXTPROC and/or PENDIN to t_state? 544435Sgibbs * o Wrap most of ttioctl in spltty/splx. 554435Sgibbs * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>. 564435Sgibbs * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set. 574435Sgibbs * o Don't allow certain termios flags to affect disciplines other 584435Sgibbs * than TTYDISC. Cancel their effects before switch disciplines 594435Sgibbs * and ignore them if they are set while we are in another 604435Sgibbs * discipline. 614435Sgibbs * o Now that historical speed conversions are handled here, don't 62121492Simp * do them in drivers. 63121588Simp * o Check for TS_CARR_ON being set while everything is closed and not 644435Sgibbs * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open, 65963Sats * so it would live until the next open even if carrier drops. 664435Sgibbs * o Restore TS_WOPEN since it is useful in pstat. It must be cleared 67963Sats * only when _all_ openers leave open(). 6813765Smpp */ 69963Sats 70963Sats#include "snp.h" 71963Sats#include "opt_compat.h" 72963Sats#include "opt_uconsole.h" 73963Sats 74963Sats#include <sys/param.h> 75963Sats#include <sys/systm.h> 76963Sats#include <sys/filio.h> 77117700Smarkm#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 78117700Smarkm#include <sys/ioctl_compat.h> 79117700Smarkm#endif 80963Sats#include <sys/proc.h> 81963Sats#define TTYDEFCHARS 82117700Smarkm#include <sys/tty.h> 83117700Smarkm#undef TTYDEFCHARS 844435Sgibbs#include <sys/fcntl.h> 8554201Smdodd#include <sys/conf.h> 8654201Smdodd#include <sys/dkstat.h> 8754201Smdodd#include <sys/poll.h> 8854201Smdodd#include <sys/kernel.h> 8954201Smdodd#include <sys/vnode.h> 9054201Smdodd#include <sys/signalvar.h> 9154201Smdodd#include <sys/resourcevar.h> 92117700Smarkm#include <sys/malloc.h> 93117700Smarkm#include <sys/filedesc.h> 9454201Smdodd#if NSNP > 0 9554201Smdodd#include <sys/snoop.h> 9654201Smdodd#endif 97117700Smarkm#include <sys/sysctl.h> 98117700Smarkm 99117700Smarkm#include <vm/vm.h> 100117700Smarkm#include <sys/lock.h> 101117700Smarkm#include <vm/pmap.h> 102117700Smarkm#include <vm/vm_map.h> 103117700Smarkm 10454201SmdoddMALLOC_DEFINE(M_TTYS, "ttys", "tty data structures"); 105117700Smarkm 106117700Smarkmstatic int proc_compare __P((struct proc *p1, struct proc *p2)); 107117700Smarkmstatic int ttnread __P((struct tty *tp)); 108117700Smarkmstatic void ttyecho __P((int c, struct tty *tp)); 109117700Smarkmstatic int ttyoutput __P((int c, register struct tty *tp)); 110963Satsstatic void ttypend __P((struct tty *tp)); 11154201Smdoddstatic void ttyretype __P((struct tty *tp)); 11254201Smdoddstatic void ttyrub __P((int c, struct tty *tp)); 113963Satsstatic void ttyrubo __P((struct tty *tp, int cnt)); 114963Satsstatic void ttyunblock __P((struct tty *tp)); 115963Satsstatic int ttywflush __P((struct tty *tp)); 116963Sats 117963Sats/* 118963Sats * Table with character classes and parity. The 8th bit indicates parity, 119963Sats * the 7th bit indicates the character is an alphameric or underscore (for 120963Sats * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 121963Sats * are 0 then the character needs no special processing on output; classes 1224435Sgibbs * other than 0 might be translated or (not currently) require delays. 1234435Sgibbs */ 1244435Sgibbs#define E 0x00 /* Even parity. */ 1254435Sgibbs#define O 0x80 /* Odd parity. */ 1264435Sgibbs#define PARITY(c) (char_type[c] & O) 1274435Sgibbs 128963Sats#define ALPHA 0x40 /* Alpha or underscore. */ 129963Sats#define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 130963Sats 1314435Sgibbs#define CCLASSMASK 0x3f 132963Sats#define CCLASS(c) (char_type[c] & CCLASSMASK) 133963Sats 134963Sats#define BS BACKSPACE 135963Sats#define CC CONTROL 136963Sats#define CR RETURN 137121492Simp#define NA ORDINARY | ALPHA 138963Sats#define NL NEWLINE 139963Sats#define NO ORDINARY 140963Sats#define TB TAB 141963Sats#define VT VTAB 142963Sats 143963Satsstatic u_char const char_type[] = { 1444435Sgibbs E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 145963Sats O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 146963Sats O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 1474435Sgibbs E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 148963Sats O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 1494435Sgibbs E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 1504435Sgibbs E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 151963Sats O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 152963Sats O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 153963Sats E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 154963Sats E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 155963Sats O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 156963Sats E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 157963Sats O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 1584435Sgibbs O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 159963Sats E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 160963Sats /* 161963Sats * Meta chars; should be settable per character set; 162963Sats * for now, treat them all as normal characters. 163963Sats */ 164963Sats NA, NA, NA, NA, NA, NA, NA, NA, 165963Sats NA, NA, NA, NA, NA, NA, NA, NA, 1664435Sgibbs NA, NA, NA, NA, NA, NA, NA, NA, 167963Sats NA, NA, NA, NA, NA, NA, NA, NA, 168963Sats NA, NA, NA, NA, NA, NA, NA, NA, 1694435Sgibbs NA, NA, NA, NA, NA, NA, NA, NA, 170963Sats NA, NA, NA, NA, NA, NA, NA, NA, 171963Sats NA, NA, NA, NA, NA, NA, NA, NA, 17249070Shosokawa NA, NA, NA, NA, NA, NA, NA, NA, 173963Sats NA, NA, NA, NA, NA, NA, NA, NA, 174963Sats NA, NA, NA, NA, NA, NA, NA, NA, 175963Sats NA, NA, NA, NA, NA, NA, NA, NA, 176963Sats NA, NA, NA, NA, NA, NA, NA, NA, 1774435Sgibbs NA, NA, NA, NA, NA, NA, NA, NA, 178963Sats NA, NA, NA, NA, NA, NA, NA, NA, 179963Sats NA, NA, NA, NA, NA, NA, NA, NA, 180963Sats}; 181963Sats#undef BS 182963Sats#undef CC 183963Sats#undef CR 184963Sats#undef NA 185963Sats#undef NL 186963Sats#undef NO 187963Sats#undef TB 1884435Sgibbs#undef VT 189963Sats 190963Sats/* Macros to clear/set/test flags. */ 191963Sats#define SET(t, f) (t) |= (f) 192963Sats#define CLR(t, f) (t) &= ~(f) 193963Sats#define ISSET(t, f) ((t) & (f)) 194963Sats 195963Sats#undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */ 196963Sats#define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */ 197963Sats 198963Sats/* 1994435Sgibbs * list of struct tty where pstat(8) can pick it up with sysctl 200963Sats */ 201963Satsstatic SLIST_HEAD(, tty) tty_list; 202963Sats 203963Sats/* 204963Sats * Initial open of tty, or (re)entry to standard tty line discipline. 205963Sats */ 206963Satsint 207963Satsttyopen(device, tp) 208963Sats dev_t device; 209963Sats register struct tty *tp; 210963Sats{ 211963Sats int s; 212963Sats 213963Sats s = spltty(); 214963Sats tp->t_dev = device; 215963Sats if (!ISSET(tp->t_state, TS_ISOPEN)) { 216963Sats SET(tp->t_state, TS_ISOPEN); 217963Sats if (ISSET(tp->t_cflag, CLOCAL)) 218117700Smarkm SET(tp->t_state, TS_CONNECTED); 21955834Smdodd bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 22055834Smdodd } 22155834Smdodd ttsetwater(tp); 22255834Smdodd splx(s); 22355834Smdodd return (0); 224117700Smarkm} 22555834Smdodd 22655834Smdodd/* 227963Sats * Handle close() on a tty line: flush and set to initial state, 228963Sats * bumping generation number so that pending read/write calls 229963Sats * can detect recycling of the tty. 230963Sats * XXX our caller should have done `spltty(); l_close(); ttyclose();' 231963Sats * and l_close() should have flushed, but we repeat the spltty() and 232963Sats * the flush in case there are buggy callers. 233963Sats */ 234963Satsint 2354435Sgibbsttyclose(tp) 2364435Sgibbs register struct tty *tp; 237963Sats{ 2384435Sgibbs int s; 2394435Sgibbs 2404435Sgibbs funsetown(tp->t_sigio); 2414435Sgibbs s = spltty(); 2424435Sgibbs if (constty == tp) 2434435Sgibbs constty = NULL; 2444435Sgibbs 2454435Sgibbs ttyflush(tp, FREAD | FWRITE); 246963Sats clist_free_cblocks(&tp->t_canq); 247963Sats clist_free_cblocks(&tp->t_outq); 248963Sats clist_free_cblocks(&tp->t_rawq); 249963Sats 250963Sats#if NSNP > 0 251963Sats if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 252963Sats snpdown((struct snoop *)tp->t_sc); 253121492Simp#endif 254121492Simp 255121492Simp tp->t_gen++; 256121492Simp tp->t_line = TTYDISC; 257121492Simp tp->t_pgrp = NULL; 258121492Simp tp->t_session = NULL; 259121492Simp tp->t_state = 0; 260121492Simp splx(s); 261121492Simp return (0); 262121492Simp} 263121492Simp 264121492Simp#define FLUSHQ(q) { \ 265121492Simp if ((q)->c_cc) \ 266963Sats ndflush(q, (q)->c_cc); \ 267963Sats} 268963Sats 269121492Simp/* Is 'c' a line delimiter ("break" character)? */ 270121492Simp#define TTBREAKC(c, lflag) \ 271121492Simp ((c) == '\n' || (((c) == cc[VEOF] || \ 272121492Simp (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \ 273963Sats (c) != _POSIX_VDISABLE)) 274963Sats 275963Sats/* 276963Sats * Process input of a single character received on a tty. 277963Sats */ 278963Satsint 279963Satsttyinput(c, tp) 280963Sats register int c; 281963Sats register struct tty *tp; 282963Sats{ 283963Sats register tcflag_t iflag, lflag; 284963Sats register cc_t *cc; 285963Sats int i, err; 286963Sats 287963Sats /* 288963Sats * If input is pending take it first. 289963Sats */ 290963Sats lflag = tp->t_lflag; 291963Sats if (ISSET(lflag, PENDIN)) 292963Sats ttypend(tp); 293963Sats /* 294963Sats * Gather stats. 295963Sats */ 296963Sats if (ISSET(lflag, ICANON)) { 297963Sats ++tk_cancc; 298963Sats ++tp->t_cancc; 299963Sats } else { 300963Sats ++tk_rawcc; 301963Sats ++tp->t_rawcc; 302963Sats } 303963Sats ++tk_nin; 304963Sats 305963Sats /* 306117700Smarkm * Block further input iff: 3074435Sgibbs * current input > threshold AND input is available to user program 3084435Sgibbs * AND input flow control is enabled and not yet invoked. 309963Sats * The 3 is slop for PARMRK. 310963Sats */ 311121515Simp iflag = tp->t_iflag; 312121492Simp if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 && 3137510Sjkh (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) && 3147510Sjkh (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) && 3157510Sjkh !ISSET(tp->t_state, TS_TBLOCK)) 3167510Sjkh ttyblock(tp); 3177510Sjkh 3187510Sjkh /* Handle exceptional conditions (break, parity, framing). */ 3197510Sjkh cc = tp->t_cc; 3207510Sjkh err = (ISSET(c, TTY_ERRORMASK)); 3217510Sjkh if (err) { 3227510Sjkh CLR(c, TTY_ERRORMASK); 3237510Sjkh if (ISSET(err, TTY_BI)) { 3247510Sjkh if (ISSET(iflag, IGNBRK)) 3257510Sjkh return (0); 3267510Sjkh if (ISSET(iflag, BRKINT)) { 327121492Simp ttyflush(tp, FREAD | FWRITE); 328121492Simp pgsignal(tp->t_pgrp, SIGINT, 1); 329121492Simp goto endcase; 3307510Sjkh } 331963Sats if (ISSET(iflag, PARMRK)) 3328876Srgrimes goto parmrk; 3334435Sgibbs } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK)) 334963Sats || ISSET(err, TTY_FE)) { 335963Sats if (ISSET(iflag, IGNPAR)) 336963Sats return (0); 337963Sats else if (ISSET(iflag, PARMRK)) { 338963Satsparmrk: 339963Sats if (tp->t_rawq.c_cc + tp->t_canq.c_cc > 340963Sats MAX_INPUT - 3) 341963Sats goto input_overflow; 342963Sats (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 343963Sats (void)putc(0 | TTY_QUOTE, &tp->t_rawq); 344963Sats (void)putc(c | TTY_QUOTE, &tp->t_rawq); 345963Sats goto endcase; 346963Sats } else 347963Sats c = 0; 3484435Sgibbs } 3494435Sgibbs } 3504435Sgibbs 3514435Sgibbs if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP)) 3524435Sgibbs CLR(c, 0x80); 3534435Sgibbs if (!ISSET(lflag, EXTPROC)) { 3544435Sgibbs /* 3554435Sgibbs * Check for literal nexting very first 356963Sats */ 357963Sats if (ISSET(tp->t_state, TS_LNCH)) { 3588876Srgrimes SET(c, TTY_QUOTE); 3594435Sgibbs CLR(tp->t_state, TS_LNCH); 360963Sats } 361963Sats /* 362963Sats * Scan for special characters. This code 363963Sats * is really just a big case statement with 364963Sats * non-constant cases. The bottom of the 365963Sats * case statement is labeled ``endcase'', so goto 366963Sats * it after a case match, or similar. 367963Sats */ 368963Sats 369963Sats /* 370963Sats * Control chars which aren't controlled 371963Sats * by ICANON, ISIG, or IXON. 372963Sats */ 373963Sats if (ISSET(lflag, IEXTEN)) { 374963Sats if (CCEQ(cc[VLNEXT], c)) { 375963Sats if (ISSET(lflag, ECHO)) { 3764435Sgibbs if (ISSET(lflag, ECHOE)) { 377963Sats (void)ttyoutput('^', tp); 378963Sats (void)ttyoutput('\b', tp); 379963Sats } else 380963Sats ttyecho(c, tp); 381963Sats } 382963Sats SET(tp->t_state, TS_LNCH); 3838876Srgrimes goto endcase; 3844435Sgibbs } 3854435Sgibbs if (CCEQ(cc[VDISCARD], c)) { 3864435Sgibbs if (ISSET(lflag, FLUSHO)) 3874435Sgibbs CLR(tp->t_lflag, FLUSHO); 3884435Sgibbs else { 3894435Sgibbs ttyflush(tp, FWRITE); 3904435Sgibbs ttyecho(c, tp); 3914435Sgibbs if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 3924435Sgibbs ttyretype(tp); 3934435Sgibbs SET(tp->t_lflag, FLUSHO); 394121492Simp } 3958876Srgrimes goto startoutput; 3964435Sgibbs } 3974435Sgibbs } 3984435Sgibbs /* 399121492Simp * Signals. 400121492Simp */ 401121492Simp if (ISSET(lflag, ISIG)) { 402121492Simp if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 4034435Sgibbs if (!ISSET(lflag, NOFLSH)) 4044435Sgibbs ttyflush(tp, FREAD | FWRITE); 405963Sats ttyecho(c, tp); 406963Sats pgsignal(tp->t_pgrp, 407117700Smarkm CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 408117700Smarkm goto endcase; 409117700Smarkm } 4104435Sgibbs if (CCEQ(cc[VSUSP], c)) { 4114435Sgibbs if (!ISSET(lflag, NOFLSH)) 412963Sats ttyflush(tp, FREAD); 413963Sats ttyecho(c, tp); 414963Sats pgsignal(tp->t_pgrp, SIGTSTP, 1); 4154435Sgibbs goto endcase; 416963Sats } 417 } 418 /* 419 * Handle start/stop characters. 420 */ 421 if (ISSET(iflag, IXON)) { 422 if (CCEQ(cc[VSTOP], c)) { 423 if (!ISSET(tp->t_state, TS_TTSTOP)) { 424 SET(tp->t_state, TS_TTSTOP); 425 (*tp->t_stop)(tp, 0); 426 return (0); 427 } 428 if (!CCEQ(cc[VSTART], c)) 429 return (0); 430 /* 431 * if VSTART == VSTOP then toggle 432 */ 433 goto endcase; 434 } 435 if (CCEQ(cc[VSTART], c)) 436 goto restartoutput; 437 } 438 /* 439 * IGNCR, ICRNL, & INLCR 440 */ 441 if (c == '\r') { 442 if (ISSET(iflag, IGNCR)) 443 return (0); 444 else if (ISSET(iflag, ICRNL)) 445 c = '\n'; 446 } else if (c == '\n' && ISSET(iflag, INLCR)) 447 c = '\r'; 448 } 449 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) { 450 /* 451 * From here on down canonical mode character 452 * processing takes place. 453 */ 454 /* 455 * erase (^H / ^?) 456 */ 457 if (CCEQ(cc[VERASE], c)) { 458 if (tp->t_rawq.c_cc) 459 ttyrub(unputc(&tp->t_rawq), tp); 460 goto endcase; 461 } 462 /* 463 * kill (^U) 464 */ 465 if (CCEQ(cc[VKILL], c)) { 466 if (ISSET(lflag, ECHOKE) && 467 tp->t_rawq.c_cc == tp->t_rocount && 468 !ISSET(lflag, ECHOPRT)) 469 while (tp->t_rawq.c_cc) 470 ttyrub(unputc(&tp->t_rawq), tp); 471 else { 472 ttyecho(c, tp); 473 if (ISSET(lflag, ECHOK) || 474 ISSET(lflag, ECHOKE)) 475 ttyecho('\n', tp); 476 FLUSHQ(&tp->t_rawq); 477 tp->t_rocount = 0; 478 } 479 CLR(tp->t_state, TS_LOCAL); 480 goto endcase; 481 } 482 /* 483 * word erase (^W) 484 */ 485 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) { 486 int ctype; 487 488 /* 489 * erase whitespace 490 */ 491 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 492 ttyrub(c, tp); 493 if (c == -1) 494 goto endcase; 495 /* 496 * erase last char of word and remember the 497 * next chars type (for ALTWERASE) 498 */ 499 ttyrub(c, tp); 500 c = unputc(&tp->t_rawq); 501 if (c == -1) 502 goto endcase; 503 if (c == ' ' || c == '\t') { 504 (void)putc(c, &tp->t_rawq); 505 goto endcase; 506 } 507 ctype = ISALPHA(c); 508 /* 509 * erase rest of word 510 */ 511 do { 512 ttyrub(c, tp); 513 c = unputc(&tp->t_rawq); 514 if (c == -1) 515 goto endcase; 516 } while (c != ' ' && c != '\t' && 517 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype)); 518 (void)putc(c, &tp->t_rawq); 519 goto endcase; 520 } 521 /* 522 * reprint line (^R) 523 */ 524 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) { 525 ttyretype(tp); 526 goto endcase; 527 } 528 /* 529 * ^T - kernel info and generate SIGINFO 530 */ 531 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) { 532 if (ISSET(lflag, ISIG)) 533 pgsignal(tp->t_pgrp, SIGINFO, 1); 534 if (!ISSET(lflag, NOKERNINFO)) 535 ttyinfo(tp); 536 goto endcase; 537 } 538 } 539 /* 540 * Check for input buffer overflow 541 */ 542 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) { 543input_overflow: 544 if (ISSET(iflag, IMAXBEL)) { 545 if (tp->t_outq.c_cc < tp->t_ohiwat) 546 (void)ttyoutput(CTRL('g'), tp); 547 } 548 goto endcase; 549 } 550 551 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP) 552 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR)) 553 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 554 555 /* 556 * Put data char in q for user and 557 * wakeup on seeing a line delimiter. 558 */ 559 if (putc(c, &tp->t_rawq) >= 0) { 560 if (!ISSET(lflag, ICANON)) { 561 ttwakeup(tp); 562 ttyecho(c, tp); 563 goto endcase; 564 } 565 if (TTBREAKC(c, lflag)) { 566 tp->t_rocount = 0; 567 catq(&tp->t_rawq, &tp->t_canq); 568 ttwakeup(tp); 569 } else if (tp->t_rocount++ == 0) 570 tp->t_rocol = tp->t_column; 571 if (ISSET(tp->t_state, TS_ERASE)) { 572 /* 573 * end of prterase \.../ 574 */ 575 CLR(tp->t_state, TS_ERASE); 576 (void)ttyoutput('/', tp); 577 } 578 i = tp->t_column; 579 ttyecho(c, tp); 580 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) { 581 /* 582 * Place the cursor over the '^' of the ^D. 583 */ 584 i = imin(2, tp->t_column - i); 585 while (i > 0) { 586 (void)ttyoutput('\b', tp); 587 i--; 588 } 589 } 590 } 591endcase: 592 /* 593 * IXANY means allow any character to restart output. 594 */ 595 if (ISSET(tp->t_state, TS_TTSTOP) && 596 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) 597 return (0); 598restartoutput: 599 CLR(tp->t_lflag, FLUSHO); 600 CLR(tp->t_state, TS_TTSTOP); 601startoutput: 602 return (ttstart(tp)); 603} 604 605/* 606 * Output a single character on a tty, doing output processing 607 * as needed (expanding tabs, newline processing, etc.). 608 * Returns < 0 if succeeds, otherwise returns char to resend. 609 * Must be recursive. 610 */ 611static int 612ttyoutput(c, tp) 613 register int c; 614 register struct tty *tp; 615{ 616 register tcflag_t oflag; 617 register int col, s; 618 619 oflag = tp->t_oflag; 620 if (!ISSET(oflag, OPOST)) { 621 if (ISSET(tp->t_lflag, FLUSHO)) 622 return (-1); 623 if (putc(c, &tp->t_outq)) 624 return (c); 625 tk_nout++; 626 tp->t_outcc++; 627 return (-1); 628 } 629 /* 630 * Do tab expansion if OXTABS is set. Special case if we external 631 * processing, we don't do the tab expansion because we'll probably 632 * get it wrong. If tab expansion needs to be done, let it happen 633 * externally. 634 */ 635 CLR(c, ~TTY_CHARMASK); 636 if (c == '\t' && 637 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) { 638 c = 8 - (tp->t_column & 7); 639 if (!ISSET(tp->t_lflag, FLUSHO)) { 640 s = spltty(); /* Don't interrupt tabs. */ 641 c -= b_to_q(" ", c, &tp->t_outq); 642 tk_nout += c; 643 tp->t_outcc += c; 644 splx(s); 645 } 646 tp->t_column += c; 647 return (c ? -1 : '\t'); 648 } 649 if (c == CEOT && ISSET(oflag, ONOEOT)) 650 return (-1); 651 652 /* 653 * Newline translation: if ONLCR is set, 654 * translate newline into "\r\n". 655 */ 656 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) { 657 tk_nout++; 658 tp->t_outcc++; 659 if (putc('\r', &tp->t_outq)) 660 return (c); 661 } 662 tk_nout++; 663 tp->t_outcc++; 664 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq)) 665 return (c); 666 667 col = tp->t_column; 668 switch (CCLASS(c)) { 669 case BACKSPACE: 670 if (col > 0) 671 --col; 672 break; 673 case CONTROL: 674 break; 675 case NEWLINE: 676 case RETURN: 677 col = 0; 678 break; 679 case ORDINARY: 680 ++col; 681 break; 682 case TAB: 683 col = (col + 8) & ~7; 684 break; 685 } 686 tp->t_column = col; 687 return (-1); 688} 689 690/* 691 * Ioctls for all tty devices. Called after line-discipline specific ioctl 692 * has been called to do discipline-specific functions and/or reject any 693 * of these ioctl commands. 694 */ 695/* ARGSUSED */ 696int 697ttioctl(tp, cmd, data, flag) 698 register struct tty *tp; 699 u_long cmd; 700 int flag; 701 void *data; 702{ 703 register struct proc *p; 704 int s, error; 705 706 p = curproc; /* XXX */ 707 708 /* If the ioctl involves modification, hang if in the background. */ 709 switch (cmd) { 710 case TIOCCBRK: 711 case TIOCCONS: 712 case TIOCDRAIN: 713 case TIOCEXCL: 714 case TIOCFLUSH: 715#ifdef TIOCHPCL 716 case TIOCHPCL: 717#endif 718 case TIOCNXCL: 719 case TIOCSBRK: 720 case TIOCSCTTY: 721 case TIOCSDRAINWAIT: 722 case TIOCSETA: 723 case TIOCSETAF: 724 case TIOCSETAW: 725 case TIOCSETD: 726 case TIOCSPGRP: 727 case TIOCSTART: 728 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) && 743 (p->p_flag & P_PPWAIT) == 0 && 744 (p->p_sigignore & sigmask(SIGTTOU)) == 0 && 745 (p->p_sigmask & sigmask(SIGTTOU)) == 0) { 746 if (p->p_pgrp->pg_jobc == 0) 747 return (EIO); 748 pgsignal(p->p_pgrp, SIGTTOU, 1); 749 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1", 750 0); 751 if (error) 752 return (error); 753 } 754 break; 755 } 756 757 switch (cmd) { /* Process the ioctl. */ 758 case FIOASYNC: /* set/clear async i/o */ 759 s = spltty(); 760 if (*(int *)data) 761 SET(tp->t_state, TS_ASYNC); 762 else 763 CLR(tp->t_state, TS_ASYNC); 764 splx(s); 765 break; 766 case FIONBIO: /* set/clear non-blocking i/o */ 767 break; /* XXX: delete. */ 768 case FIONREAD: /* get # bytes to read */ 769 s = spltty(); 770 *(int *)data = ttnread(tp); 771 splx(s); 772 break; 773 774 case FIOSETOWN: 775 /* 776 * Policy -- Don't allow FIOSETOWN on someone else's 777 * controlling tty 778 */ 779 if (tp->t_session != NULL && !isctty(p, tp)) 780 return (ENOTTY); 781 782 error = fsetown(*(int *)data, &tp->t_sigio); 783 if (error) 784 return (error); 785 break; 786 case FIOGETOWN: 787 if (tp->t_session != NULL && !isctty(p, tp)) 788 return (ENOTTY); 789 *(int *)data = fgetown(tp->t_sigio); 790 break; 791 792 case TIOCEXCL: /* set exclusive use of tty */ 793 s = spltty(); 794 SET(tp->t_state, TS_XCLUDE); 795 splx(s); 796 break; 797 case TIOCFLUSH: { /* flush buffers */ 798 register int flags = *(int *)data; 799 800 if (flags == 0) 801 flags = FREAD | FWRITE; 802 else 803 flags &= FREAD | FWRITE; 804 ttyflush(tp, flags); 805 break; 806 } 807 case TIOCCONS: /* become virtual console */ 808 if (*(int *)data) { 809 if (constty && constty != tp && 810 ISSET(constty->t_state, TS_CONNECTED)) 811 return (EBUSY); 812#ifndef UCONSOLE 813 if ((error = suser(p)) != 0) 814 return (error); 815#endif 816 constty = tp; 817 } else if (tp == constty) 818 constty = NULL; 819 break; 820 case TIOCDRAIN: /* wait till output drained */ 821 error = ttywait(tp); 822 if (error) 823 return (error); 824 break; 825 case TIOCGETA: { /* get termios struct */ 826 struct termios *t = (struct termios *)data; 827 828 bcopy(&tp->t_termios, t, sizeof(struct termios)); 829 break; 830 } 831 case TIOCGETD: /* get line discipline */ 832 *(int *)data = tp->t_line; 833 break; 834 case TIOCGWINSZ: /* get window size */ 835 *(struct winsize *)data = tp->t_winsize; 836 break; 837 case TIOCGPGRP: /* get pgrp of tty */ 838 if (!isctty(p, tp)) 839 return (ENOTTY); 840 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 841 break; 842#ifdef TIOCHPCL 843 case TIOCHPCL: /* hang up on last close */ 844 s = spltty(); 845 SET(tp->t_cflag, HUPCL); 846 splx(s); 847 break; 848#endif 849 case TIOCNXCL: /* reset exclusive use of tty */ 850 s = spltty(); 851 CLR(tp->t_state, TS_XCLUDE); 852 splx(s); 853 break; 854 case TIOCOUTQ: /* output queue size */ 855 *(int *)data = tp->t_outq.c_cc; 856 break; 857 case TIOCSETA: /* set termios struct */ 858 case TIOCSETAW: /* drain output, set */ 859 case TIOCSETAF: { /* drn out, fls in, set */ 860 register struct termios *t = (struct termios *)data; 861 862 if (t->c_ispeed == 0) 863 t->c_ispeed = t->c_ospeed; 864 if (t->c_ispeed == 0) 865 t->c_ispeed = tp->t_ospeed; 866 if (t->c_ispeed == 0) 867 return (EINVAL); 868 s = spltty(); 869 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 870 error = ttywait(tp); 871 if (error) { 872 splx(s); 873 return (error); 874 } 875 if (cmd == TIOCSETAF) 876 ttyflush(tp, FREAD); 877 } 878 if (!ISSET(t->c_cflag, CIGNORE)) { 879 /* 880 * Set device hardware. 881 */ 882 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 883 splx(s); 884 return (error); 885 } 886 if (ISSET(t->c_cflag, CLOCAL) && 887 !ISSET(tp->t_cflag, CLOCAL)) { 888 /* 889 * XXX disconnections would be too hard to 890 * get rid of without this kludge. The only 891 * way to get rid of controlling terminals 892 * is to exit from the session leader. 893 */ 894 CLR(tp->t_state, TS_ZOMBIE); 895 896 wakeup(TSA_CARR_ON(tp)); 897 ttwakeup(tp); 898 ttwwakeup(tp); 899 } 900 if ((ISSET(tp->t_state, TS_CARR_ON) || 901 ISSET(t->c_cflag, CLOCAL)) && 902 !ISSET(tp->t_state, TS_ZOMBIE)) 903 SET(tp->t_state, TS_CONNECTED); 904 else 905 CLR(tp->t_state, TS_CONNECTED); 906 tp->t_cflag = t->c_cflag; 907 tp->t_ispeed = t->c_ispeed; 908 if (t->c_ospeed != 0) 909 tp->t_ospeed = t->c_ospeed; 910 ttsetwater(tp); 911 } 912 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) && 913 cmd != TIOCSETAF) { 914 if (ISSET(t->c_lflag, ICANON)) 915 SET(tp->t_lflag, PENDIN); 916 else { 917 /* 918 * XXX we really shouldn't allow toggling 919 * ICANON while we're in a non-termios line 920 * discipline. Now we have to worry about 921 * panicing for a null queue. 922 */ 923 if (tp->t_canq.c_cbreserved > 0 && 924 tp->t_rawq.c_cbreserved > 0) { 925 catq(&tp->t_rawq, &tp->t_canq); 926 /* 927 * XXX the queue limits may be 928 * different, so the old queue 929 * swapping method no longer works. 930 */ 931 catq(&tp->t_canq, &tp->t_rawq); 932 } 933 CLR(tp->t_lflag, PENDIN); 934 } 935 ttwakeup(tp); 936 } 937 tp->t_iflag = t->c_iflag; 938 tp->t_oflag = t->c_oflag; 939 /* 940 * Make the EXTPROC bit read only. 941 */ 942 if (ISSET(tp->t_lflag, EXTPROC)) 943 SET(t->c_lflag, EXTPROC); 944 else 945 CLR(t->c_lflag, EXTPROC); 946 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 947 if (t->c_cc[VMIN] != tp->t_cc[VMIN] || 948 t->c_cc[VTIME] != tp->t_cc[VTIME]) 949 ttwakeup(tp); 950 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 951 splx(s); 952 break; 953 } 954 case TIOCSETD: { /* set line discipline */ 955 register int t = *(int *)data; 956 dev_t device = tp->t_dev; 957 958 if ((u_int)t >= nlinesw) 959 return (ENXIO); 960 if (t != tp->t_line) { 961 s = spltty(); 962 (*linesw[tp->t_line].l_close)(tp, flag); 963 error = (*linesw[t].l_open)(device, tp); 964 if (error) { 965 (void)(*linesw[tp->t_line].l_open)(device, tp); 966 splx(s); 967 return (error); 968 } 969 tp->t_line = t; 970 splx(s); 971 } 972 break; 973 } 974 case TIOCSTART: /* start output, like ^Q */ 975 s = spltty(); 976 if (ISSET(tp->t_state, TS_TTSTOP) || 977 ISSET(tp->t_lflag, FLUSHO)) { 978 CLR(tp->t_lflag, FLUSHO); 979 CLR(tp->t_state, TS_TTSTOP); 980 ttstart(tp); 981 } 982 splx(s); 983 break; 984 case TIOCSTI: /* simulate terminal input */ 985 if ((flag & FREAD) == 0 && suser(p)) 986 return (EPERM); 987 if (!isctty(p, tp) && suser(p)) 988 return (EACCES); 989 s = spltty(); 990 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 991 splx(s); 992 break; 993 case TIOCSTOP: /* stop output, like ^S */ 994 s = spltty(); 995 if (!ISSET(tp->t_state, TS_TTSTOP)) { 996 SET(tp->t_state, TS_TTSTOP); 997 (*tp->t_stop)(tp, 0); 998 } 999 splx(s); 1000 break; 1001 case TIOCSCTTY: /* become controlling tty */ 1002 /* Session ctty vnode pointer set in vnode layer. */ 1003 if (!SESS_LEADER(p) || 1004 ((p->p_session->s_ttyvp || tp->t_session) && 1005 (tp->t_session != p->p_session))) 1006 return (EPERM); 1007 tp->t_session = p->p_session; 1008 tp->t_pgrp = p->p_pgrp; 1009 p->p_session->s_ttyp = tp; 1010 p->p_flag |= P_CONTROLT; 1011 break; 1012 case TIOCSPGRP: { /* set pgrp of tty */ 1013 register struct pgrp *pgrp = pgfind(*(int *)data); 1014 1015 if (!isctty(p, tp)) 1016 return (ENOTTY); 1017 else if (pgrp == NULL || pgrp->pg_session != p->p_session) 1018 return (EPERM); 1019 tp->t_pgrp = pgrp; 1020 break; 1021 } 1022 case TIOCSTAT: /* simulate control-T */ 1023 s = spltty(); 1024 ttyinfo(tp); 1025 splx(s); 1026 break; 1027 case TIOCSWINSZ: /* set window size */ 1028 if (bcmp((caddr_t)&tp->t_winsize, data, 1029 sizeof (struct winsize))) { 1030 tp->t_winsize = *(struct winsize *)data; 1031 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1032 } 1033 break; 1034 case TIOCSDRAINWAIT: 1035 error = suser(p); 1036 if (error) 1037 return (error); 1038 tp->t_timeout = *(int *)data * hz; 1039 wakeup(TSA_OCOMPLETE(tp)); 1040 wakeup(TSA_OLOWAT(tp)); 1041 break; 1042 case TIOCGDRAINWAIT: 1043 *(int *)data = tp->t_timeout / hz; 1044 break; 1045 default: 1046#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1047 return (ttcompat(tp, cmd, data, flag)); 1048#else 1049 return (ENOIOCTL); 1050#endif 1051 } 1052 return (0); 1053} 1054 1055int 1056ttypoll(dev, events, p) 1057 dev_t dev; 1058 int events; 1059 struct proc *p; 1060{ 1061 int s; 1062 int revents = 0; 1063 struct tty *tp; 1064 1065 tp = dev->si_tty; 1066 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */ 1067 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) 1068 | POLLHUP); 1069 1070 s = spltty(); 1071 if (events & (POLLIN | POLLRDNORM)) { 1072 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) 1073 revents |= events & (POLLIN | POLLRDNORM); 1074 else 1075 selrecord(p, &tp->t_rsel); 1076 } 1077 if (events & (POLLOUT | POLLWRNORM)) { 1078 if ((tp->t_outq.c_cc <= tp->t_olowat && 1079 ISSET(tp->t_state, TS_CONNECTED)) 1080 || ISSET(tp->t_state, TS_ZOMBIE)) 1081 revents |= events & (POLLOUT | POLLWRNORM); 1082 else 1083 selrecord(p, &tp->t_wsel); 1084 } 1085 splx(s); 1086 return (revents); 1087} 1088 1089/* 1090 * Must be called at spltty(). 1091 */ 1092static int 1093ttnread(tp) 1094 struct tty *tp; 1095{ 1096 int nread; 1097 1098 if (ISSET(tp->t_lflag, PENDIN)) 1099 ttypend(tp); 1100 nread = tp->t_canq.c_cc; 1101 if (!ISSET(tp->t_lflag, ICANON)) { 1102 nread += tp->t_rawq.c_cc; 1103 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) 1104 nread = 0; 1105 } 1106 return (nread); 1107} 1108 1109/* 1110 * Wait for output to drain. 1111 */ 1112int 1113ttywait(tp) 1114 register struct tty *tp; 1115{ 1116 int error, s; 1117 1118 error = 0; 1119 s = spltty(); 1120 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1121 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) { 1122 (*tp->t_oproc)(tp); 1123 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1124 ISSET(tp->t_state, TS_CONNECTED)) { 1125 SET(tp->t_state, TS_SO_OCOMPLETE); 1126 error = ttysleep(tp, TSA_OCOMPLETE(tp), 1127 TTOPRI | PCATCH, "ttywai", 1128 tp->t_timeout); 1129 if (error) { 1130 if (error == EWOULDBLOCK) 1131 error = EIO; 1132 break; 1133 } 1134 } else 1135 break; 1136 } 1137 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY))) 1138 error = EIO; 1139 splx(s); 1140 return (error); 1141} 1142 1143/* 1144 * Flush if successfully wait. 1145 */ 1146static int 1147ttywflush(tp) 1148 struct tty *tp; 1149{ 1150 int error; 1151 1152 if ((error = ttywait(tp)) == 0) 1153 ttyflush(tp, FREAD); 1154 return (error); 1155} 1156 1157/* 1158 * Flush tty read and/or write queues, notifying anyone waiting. 1159 */ 1160void 1161ttyflush(tp, rw) 1162 register struct tty *tp; 1163 int rw; 1164{ 1165 register int s; 1166 1167 s = spltty(); 1168#if 0 1169again: 1170#endif 1171 if (rw & FWRITE) { 1172 FLUSHQ(&tp->t_outq); 1173 CLR(tp->t_state, TS_TTSTOP); 1174 } 1175 (*tp->t_stop)(tp, rw); 1176 if (rw & FREAD) { 1177 FLUSHQ(&tp->t_canq); 1178 FLUSHQ(&tp->t_rawq); 1179 CLR(tp->t_lflag, PENDIN); 1180 tp->t_rocount = 0; 1181 tp->t_rocol = 0; 1182 CLR(tp->t_state, TS_LOCAL); 1183 ttwakeup(tp); 1184 if (ISSET(tp->t_state, TS_TBLOCK)) { 1185 if (rw & FWRITE) 1186 FLUSHQ(&tp->t_outq); 1187 ttyunblock(tp); 1188 1189 /* 1190 * Don't let leave any state that might clobber the 1191 * next line discipline (although we should do more 1192 * to send the START char). Not clearing the state 1193 * may have caused the "putc to a clist with no 1194 * reserved cblocks" panic/printf. 1195 */ 1196 CLR(tp->t_state, TS_TBLOCK); 1197 1198#if 0 /* forget it, sleeping isn't always safe and we don't know when it is */ 1199 if (ISSET(tp->t_iflag, IXOFF)) { 1200 /* 1201 * XXX wait a bit in the hope that the stop 1202 * character (if any) will go out. Waiting 1203 * isn't good since it allows races. This 1204 * will be fixed when the stop character is 1205 * put in a special queue. Don't bother with 1206 * the checks in ttywait() since the timeout 1207 * will save us. 1208 */ 1209 SET(tp->t_state, TS_SO_OCOMPLETE); 1210 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI, 1211 "ttyfls", hz / 10); 1212 /* 1213 * Don't try sending the stop character again. 1214 */ 1215 CLR(tp->t_state, TS_TBLOCK); 1216 goto again; 1217 } 1218#endif 1219 } 1220 } 1221 if (rw & FWRITE) { 1222 FLUSHQ(&tp->t_outq); 1223 ttwwakeup(tp); 1224 } 1225 splx(s); 1226} 1227 1228/* 1229 * Copy in the default termios characters. 1230 */ 1231void 1232termioschars(t) 1233 struct termios *t; 1234{ 1235 1236 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc); 1237} 1238 1239/* 1240 * Old interface. 1241 */ 1242void 1243ttychars(tp) 1244 struct tty *tp; 1245{ 1246 1247 termioschars(&tp->t_termios); 1248} 1249 1250/* 1251 * Handle input high water. Send stop character for the IXOFF case. Turn 1252 * on our input flow control bit and propagate the changes to the driver. 1253 * XXX the stop character should be put in a special high priority queue. 1254 */ 1255void 1256ttyblock(tp) 1257 struct tty *tp; 1258{ 1259 1260 SET(tp->t_state, TS_TBLOCK); 1261 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1262 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0) 1263 CLR(tp->t_state, TS_TBLOCK); /* try again later */ 1264 ttstart(tp); 1265} 1266 1267/* 1268 * Handle input low water. Send start character for the IXOFF case. Turn 1269 * off our input flow control bit and propagate the changes to the driver. 1270 * XXX the start character should be put in a special high priority queue. 1271 */ 1272static void 1273ttyunblock(tp) 1274 struct tty *tp; 1275{ 1276 1277 CLR(tp->t_state, TS_TBLOCK); 1278 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE && 1279 putc(tp->t_cc[VSTART], &tp->t_outq) != 0) 1280 SET(tp->t_state, TS_TBLOCK); /* try again later */ 1281 ttstart(tp); 1282} 1283 1284#ifdef notyet 1285/* Not used by any current (i386) drivers. */ 1286/* 1287 * Restart after an inter-char delay. 1288 */ 1289void 1290ttrstrt(tp_arg) 1291 void *tp_arg; 1292{ 1293 struct tty *tp; 1294 int s; 1295 1296 KASSERT(tp_arg != NULL, ("ttrstrt")); 1297 1298 tp = tp_arg; 1299 s = spltty(); 1300 1301 CLR(tp->t_state, TS_TIMEOUT); 1302 ttstart(tp); 1303 1304 splx(s); 1305} 1306#endif 1307 1308int 1309ttstart(tp) 1310 struct tty *tp; 1311{ 1312 1313 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1314 (*tp->t_oproc)(tp); 1315 return (0); 1316} 1317 1318/* 1319 * "close" a line discipline 1320 */ 1321int 1322ttylclose(tp, flag) 1323 struct tty *tp; 1324 int flag; 1325{ 1326 1327 if (flag & FNONBLOCK || ttywflush(tp)) 1328 ttyflush(tp, FREAD | FWRITE); 1329 return (0); 1330} 1331 1332/* 1333 * Handle modem control transition on a tty. 1334 * Flag indicates new state of carrier. 1335 * Returns 0 if the line should be turned off, otherwise 1. 1336 */ 1337int 1338ttymodem(tp, flag) 1339 register struct tty *tp; 1340 int flag; 1341{ 1342 1343 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) { 1344 /* 1345 * MDMBUF: do flow control according to carrier flag 1346 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP 1347 * works if IXON and IXANY are clear. 1348 */ 1349 if (flag) { 1350 CLR(tp->t_state, TS_CAR_OFLOW); 1351 CLR(tp->t_state, TS_TTSTOP); 1352 ttstart(tp); 1353 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) { 1354 SET(tp->t_state, TS_CAR_OFLOW); 1355 SET(tp->t_state, TS_TTSTOP); 1356 (*tp->t_stop)(tp, 0); 1357 } 1358 } else if (flag == 0) { 1359 /* 1360 * Lost carrier. 1361 */ 1362 CLR(tp->t_state, TS_CARR_ON); 1363 if (ISSET(tp->t_state, TS_ISOPEN) && 1364 !ISSET(tp->t_cflag, CLOCAL)) { 1365 SET(tp->t_state, TS_ZOMBIE); 1366 CLR(tp->t_state, TS_CONNECTED); 1367 if (tp->t_session && tp->t_session->s_leader) 1368 psignal(tp->t_session->s_leader, SIGHUP); 1369 ttyflush(tp, FREAD | FWRITE); 1370 return (0); 1371 } 1372 } else { 1373 /* 1374 * Carrier now on. 1375 */ 1376 SET(tp->t_state, TS_CARR_ON); 1377 if (!ISSET(tp->t_state, TS_ZOMBIE)) 1378 SET(tp->t_state, TS_CONNECTED); 1379 wakeup(TSA_CARR_ON(tp)); 1380 ttwakeup(tp); 1381 ttwwakeup(tp); 1382 } 1383 return (1); 1384} 1385 1386/* 1387 * Reinput pending characters after state switch 1388 * call at spltty(). 1389 */ 1390static void 1391ttypend(tp) 1392 register struct tty *tp; 1393{ 1394 struct clist tq; 1395 register int c; 1396 1397 CLR(tp->t_lflag, PENDIN); 1398 SET(tp->t_state, TS_TYPEN); 1399 /* 1400 * XXX this assumes too much about clist internals. It may even 1401 * fail if the cblock slush pool is empty. We can't allocate more 1402 * cblocks here because we are called from an interrupt handler 1403 * and clist_alloc_cblocks() can wait. 1404 */ 1405 tq = tp->t_rawq; 1406 bzero(&tp->t_rawq, sizeof tp->t_rawq); 1407 tp->t_rawq.c_cbmax = tq.c_cbmax; 1408 tp->t_rawq.c_cbreserved = tq.c_cbreserved; 1409 while ((c = getc(&tq)) >= 0) 1410 ttyinput(c, tp); 1411 CLR(tp->t_state, TS_TYPEN); 1412} 1413 1414/* 1415 * Process a read call on a tty device. 1416 */ 1417int 1418ttread(tp, uio, flag) 1419 register struct tty *tp; 1420 struct uio *uio; 1421 int flag; 1422{ 1423 register struct clist *qp; 1424 register int c; 1425 register tcflag_t lflag; 1426 register cc_t *cc = tp->t_cc; 1427 register struct proc *p = curproc; 1428 int s, first, error = 0; 1429 int has_stime = 0, last_cc = 0; 1430 long slp = 0; /* XXX this should be renamed `timo'. */ 1431 struct timeval stime; 1432 1433loop: 1434 s = spltty(); 1435 lflag = tp->t_lflag; 1436 /* 1437 * take pending input first 1438 */ 1439 if (ISSET(lflag, PENDIN)) { 1440 ttypend(tp); 1441 splx(s); /* reduce latency */ 1442 s = spltty(); 1443 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */ 1444 } 1445 1446 /* 1447 * Hang process if it's in the background. 1448 */ 1449 if (isbackground(p, tp)) { 1450 splx(s); 1451 if ((p->p_sigignore & sigmask(SIGTTIN)) || 1452 (p->p_sigmask & sigmask(SIGTTIN)) || 1453 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0) 1454 return (EIO); 1455 pgsignal(p->p_pgrp, SIGTTIN, 1); 1456 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0); 1457 if (error) 1458 return (error); 1459 goto loop; 1460 } 1461 1462 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1463 splx(s); 1464 return (0); /* EOF */ 1465 } 1466 1467 /* 1468 * If canonical, use the canonical queue, 1469 * else use the raw queue. 1470 * 1471 * (should get rid of clists...) 1472 */ 1473 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 1474 1475 if (flag & IO_NDELAY) { 1476 if (qp->c_cc > 0) 1477 goto read; 1478 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) { 1479 splx(s); 1480 return (0); 1481 } 1482 splx(s); 1483 return (EWOULDBLOCK); 1484 } 1485 if (!ISSET(lflag, ICANON)) { 1486 int m = cc[VMIN]; 1487 long t = cc[VTIME]; 1488 struct timeval timecopy; 1489 1490 /* 1491 * Check each of the four combinations. 1492 * (m > 0 && t == 0) is the normal read case. 1493 * It should be fairly efficient, so we check that and its 1494 * companion case (m == 0 && t == 0) first. 1495 * For the other two cases, we compute the target sleep time 1496 * into slp. 1497 */ 1498 if (t == 0) { 1499 if (qp->c_cc < m) 1500 goto sleep; 1501 if (qp->c_cc > 0) 1502 goto read; 1503 1504 /* m, t and qp->c_cc are all 0. 0 is enough input. */ 1505 splx(s); 1506 return (0); 1507 } 1508 t *= 100000; /* time in us */ 1509#define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1510 ((t1).tv_usec - (t2).tv_usec)) 1511 if (m > 0) { 1512 if (qp->c_cc <= 0) 1513 goto sleep; 1514 if (qp->c_cc >= m) 1515 goto read; 1516 getmicrotime(&timecopy); 1517 if (!has_stime) { 1518 /* first character, start timer */ 1519 has_stime = 1; 1520 stime = timecopy; 1521 slp = t; 1522 } else if (qp->c_cc > last_cc) { 1523 /* got a character, restart timer */ 1524 stime = timecopy; 1525 slp = t; 1526 } else { 1527 /* nothing, check expiration */ 1528 slp = t - diff(timecopy, stime); 1529 if (slp <= 0) 1530 goto read; 1531 } 1532 last_cc = qp->c_cc; 1533 } else { /* m == 0 */ 1534 if (qp->c_cc > 0) 1535 goto read; 1536 getmicrotime(&timecopy); 1537 if (!has_stime) { 1538 has_stime = 1; 1539 stime = timecopy; 1540 slp = t; 1541 } else { 1542 slp = t - diff(timecopy, stime); 1543 if (slp <= 0) { 1544 /* Timed out, but 0 is enough input. */ 1545 splx(s); 1546 return (0); 1547 } 1548 } 1549 } 1550#undef diff 1551 /* 1552 * Rounding down may make us wake up just short 1553 * of the target, so we round up. 1554 * The formula is ceiling(slp * hz/1000000). 1555 * 32-bit arithmetic is enough for hz < 169. 1556 * XXX see tvtohz() for how to avoid overflow if hz 1557 * is large (divide by `tick' and/or arrange to 1558 * use tvtohz() if hz is large). 1559 */ 1560 slp = (long) (((u_long)slp * hz) + 999999) / 1000000; 1561 goto sleep; 1562 } 1563 if (qp->c_cc <= 0) { 1564sleep: 1565 /* 1566 * There is no input, or not enough input and we can block. 1567 */ 1568 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH, 1569 ISSET(tp->t_state, TS_CONNECTED) ? 1570 "ttyin" : "ttyhup", (int)slp); 1571 splx(s); 1572 if (error == EWOULDBLOCK) 1573 error = 0; 1574 else if (error) 1575 return (error); 1576 /* 1577 * XXX what happens if another process eats some input 1578 * while we are asleep (not just here)? It would be 1579 * safest to detect changes and reset our state variables 1580 * (has_stime and last_cc). 1581 */ 1582 slp = 0; 1583 goto loop; 1584 } 1585read: 1586 splx(s); 1587 /* 1588 * Input present, check for input mapping and processing. 1589 */ 1590 first = 1; 1591 if (ISSET(lflag, ICANON | ISIG)) 1592 goto slowcase; 1593 for (;;) { 1594 char ibuf[IBUFSIZ]; 1595 int icc; 1596 1597 icc = imin(uio->uio_resid, IBUFSIZ); 1598 icc = q_to_b(qp, ibuf, icc); 1599 if (icc <= 0) { 1600 if (first) 1601 goto loop; 1602 break; 1603 } 1604 error = uiomove(ibuf, icc, uio); 1605 /* 1606 * XXX if there was an error then we should ungetc() the 1607 * unmoved chars and reduce icc here. 1608 */ 1609#if NSNP > 0 1610 if (ISSET(tp->t_lflag, ECHO) && 1611 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1612 snpin((struct snoop *)tp->t_sc, ibuf, icc); 1613#endif 1614 if (error) 1615 break; 1616 if (uio->uio_resid == 0) 1617 break; 1618 first = 0; 1619 } 1620 goto out; 1621slowcase: 1622 for (;;) { 1623 c = getc(qp); 1624 if (c < 0) { 1625 if (first) 1626 goto loop; 1627 break; 1628 } 1629 /* 1630 * delayed suspend (^Y) 1631 */ 1632 if (CCEQ(cc[VDSUSP], c) && 1633 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) { 1634 pgsignal(tp->t_pgrp, SIGTSTP, 1); 1635 if (first) { 1636 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, 1637 "ttybg3", 0); 1638 if (error) 1639 break; 1640 goto loop; 1641 } 1642 break; 1643 } 1644 /* 1645 * Interpret EOF only in canonical mode. 1646 */ 1647 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 1648 break; 1649 /* 1650 * Give user character. 1651 */ 1652 error = ureadc(c, uio); 1653 if (error) 1654 /* XXX should ungetc(c, qp). */ 1655 break; 1656#if NSNP > 0 1657 /* 1658 * Only snoop directly on input in echo mode. Non-echoed 1659 * input will be snooped later iff the application echoes it. 1660 */ 1661 if (ISSET(tp->t_lflag, ECHO) && 1662 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1663 snpinc((struct snoop *)tp->t_sc, (char)c); 1664#endif 1665 if (uio->uio_resid == 0) 1666 break; 1667 /* 1668 * In canonical mode check for a "break character" 1669 * marking the end of a "line of input". 1670 */ 1671 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag)) 1672 break; 1673 first = 0; 1674 } 1675 1676out: 1677 /* 1678 * Look to unblock input now that (presumably) 1679 * the input queue has gone down. 1680 */ 1681 s = spltty(); 1682 if (ISSET(tp->t_state, TS_TBLOCK) && 1683 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat) 1684 ttyunblock(tp); 1685 splx(s); 1686 1687 return (error); 1688} 1689 1690/* 1691 * Check the output queue on tp for space for a kernel message (from uprintf 1692 * or tprintf). Allow some space over the normal hiwater mark so we don't 1693 * lose messages due to normal flow control, but don't let the tty run amok. 1694 * Sleeps here are not interruptible, but we return prematurely if new signals 1695 * arrive. 1696 */ 1697int 1698ttycheckoutq(tp, wait) 1699 register struct tty *tp; 1700 int wait; 1701{ 1702 int hiwat, s, oldsig; 1703 1704 hiwat = tp->t_ohiwat; 1705 s = spltty(); 1706 oldsig = wait ? curproc->p_siglist : 0; 1707 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100) 1708 while (tp->t_outq.c_cc > hiwat) { 1709 ttstart(tp); 1710 if (tp->t_outq.c_cc <= hiwat) 1711 break; 1712 if (wait == 0 || curproc->p_siglist != oldsig) { 1713 splx(s); 1714 return (0); 1715 } 1716 SET(tp->t_state, TS_SO_OLOWAT); 1717 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz); 1718 } 1719 splx(s); 1720 return (1); 1721} 1722 1723/* 1724 * Process a write call on a tty device. 1725 */ 1726int 1727ttwrite(tp, uio, flag) 1728 register struct tty *tp; 1729 register struct uio *uio; 1730 int flag; 1731{ 1732 register char *cp = NULL; 1733 register int cc, ce; 1734 register struct proc *p; 1735 int i, hiwat, cnt, error, s; 1736 char obuf[OBUFSIZ]; 1737 1738 hiwat = tp->t_ohiwat; 1739 cnt = uio->uio_resid; 1740 error = 0; 1741 cc = 0; 1742loop: 1743 s = spltty(); 1744 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1745 splx(s); 1746 if (uio->uio_resid == cnt) 1747 error = EIO; 1748 goto out; 1749 } 1750 if (!ISSET(tp->t_state, TS_CONNECTED)) { 1751 if (flag & IO_NDELAY) { 1752 splx(s); 1753 error = EWOULDBLOCK; 1754 goto out; 1755 } 1756 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1757 "ttydcd", 0); 1758 splx(s); 1759 if (error) 1760 goto out; 1761 goto loop; 1762 } 1763 splx(s); 1764 /* 1765 * Hang the process if it's in the background. 1766 */ 1767 p = curproc; 1768 if (isbackground(p, tp) && 1769 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 && 1770 (p->p_sigignore & sigmask(SIGTTOU)) == 0 && 1771 (p->p_sigmask & sigmask(SIGTTOU)) == 0) { 1772 if (p->p_pgrp->pg_jobc == 0) { 1773 error = EIO; 1774 goto out; 1775 } 1776 pgsignal(p->p_pgrp, SIGTTOU, 1); 1777 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0); 1778 if (error) 1779 goto out; 1780 goto loop; 1781 } 1782 /* 1783 * Process the user's data in at most OBUFSIZ chunks. Perform any 1784 * output translation. Keep track of high water mark, sleep on 1785 * overflow awaiting device aid in acquiring new space. 1786 */ 1787 while (uio->uio_resid > 0 || cc > 0) { 1788 if (ISSET(tp->t_lflag, FLUSHO)) { 1789 uio->uio_resid = 0; 1790 return (0); 1791 } 1792 if (tp->t_outq.c_cc > hiwat) 1793 goto ovhiwat; 1794 /* 1795 * Grab a hunk of data from the user, unless we have some 1796 * leftover from last time. 1797 */ 1798 if (cc == 0) { 1799 cc = imin(uio->uio_resid, OBUFSIZ); 1800 cp = obuf; 1801 error = uiomove(cp, cc, uio); 1802 if (error) { 1803 cc = 0; 1804 break; 1805 } 1806#if NSNP > 0 1807 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1808 snpin((struct snoop *)tp->t_sc, cp, cc); 1809#endif 1810 } 1811 /* 1812 * If nothing fancy need be done, grab those characters we 1813 * can handle without any of ttyoutput's processing and 1814 * just transfer them to the output q. For those chars 1815 * which require special processing (as indicated by the 1816 * bits in char_type), call ttyoutput. After processing 1817 * a hunk of data, look for FLUSHO so ^O's will take effect 1818 * immediately. 1819 */ 1820 while (cc > 0) { 1821 if (!ISSET(tp->t_oflag, OPOST)) 1822 ce = cc; 1823 else { 1824 ce = cc - scanc((u_int)cc, (u_char *)cp, 1825 char_type, CCLASSMASK); 1826 /* 1827 * If ce is zero, then we're processing 1828 * a special character through ttyoutput. 1829 */ 1830 if (ce == 0) { 1831 tp->t_rocount = 0; 1832 if (ttyoutput(*cp, tp) >= 0) { 1833 /* No Clists, wait a bit. */ 1834 ttstart(tp); 1835 if (flag & IO_NDELAY) { 1836 error = EWOULDBLOCK; 1837 goto out; 1838 } 1839 error = ttysleep(tp, &lbolt, 1840 TTOPRI|PCATCH, 1841 "ttybf1", 0); 1842 if (error) 1843 goto out; 1844 goto loop; 1845 } 1846 cp++; 1847 cc--; 1848 if (ISSET(tp->t_lflag, FLUSHO) || 1849 tp->t_outq.c_cc > hiwat) 1850 goto ovhiwat; 1851 continue; 1852 } 1853 } 1854 /* 1855 * A bunch of normal characters have been found. 1856 * Transfer them en masse to the output queue and 1857 * continue processing at the top of the loop. 1858 * If there are any further characters in this 1859 * <= OBUFSIZ chunk, the first should be a character 1860 * requiring special handling by ttyoutput. 1861 */ 1862 tp->t_rocount = 0; 1863 i = b_to_q(cp, ce, &tp->t_outq); 1864 ce -= i; 1865 tp->t_column += ce; 1866 cp += ce, cc -= ce, tk_nout += ce; 1867 tp->t_outcc += ce; 1868 if (i > 0) { 1869 /* No Clists, wait a bit. */ 1870 ttstart(tp); 1871 if (flag & IO_NDELAY) { 1872 error = EWOULDBLOCK; 1873 goto out; 1874 } 1875 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, 1876 "ttybf2", 0); 1877 if (error) 1878 goto out; 1879 goto loop; 1880 } 1881 if (ISSET(tp->t_lflag, FLUSHO) || 1882 tp->t_outq.c_cc > hiwat) 1883 break; 1884 } 1885 ttstart(tp); 1886 } 1887out: 1888 /* 1889 * If cc is nonzero, we leave the uio structure inconsistent, as the 1890 * offset and iov pointers have moved forward, but it doesn't matter 1891 * (the call will either return short or restart with a new uio). 1892 */ 1893 uio->uio_resid += cc; 1894 return (error); 1895 1896ovhiwat: 1897 ttstart(tp); 1898 s = spltty(); 1899 /* 1900 * This can only occur if FLUSHO is set in t_lflag, 1901 * or if ttstart/oproc is synchronous (or very fast). 1902 */ 1903 if (tp->t_outq.c_cc <= hiwat) { 1904 splx(s); 1905 goto loop; 1906 } 1907 if (flag & IO_NDELAY) { 1908 splx(s); 1909 uio->uio_resid += cc; 1910 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 1911 } 1912 SET(tp->t_state, TS_SO_OLOWAT); 1913 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri", 1914 tp->t_timeout); 1915 splx(s); 1916 if (error == EWOULDBLOCK) 1917 error = EIO; 1918 if (error) 1919 goto out; 1920 goto loop; 1921} 1922 1923/* 1924 * Rubout one character from the rawq of tp 1925 * as cleanly as possible. 1926 */ 1927static void 1928ttyrub(c, tp) 1929 register int c; 1930 register struct tty *tp; 1931{ 1932 register char *cp; 1933 register int savecol; 1934 int tabc, s; 1935 1936 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 1937 return; 1938 CLR(tp->t_lflag, FLUSHO); 1939 if (ISSET(tp->t_lflag, ECHOE)) { 1940 if (tp->t_rocount == 0) { 1941 /* 1942 * Screwed by ttwrite; retype 1943 */ 1944 ttyretype(tp); 1945 return; 1946 } 1947 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 1948 ttyrubo(tp, 2); 1949 else { 1950 CLR(c, ~TTY_CHARMASK); 1951 switch (CCLASS(c)) { 1952 case ORDINARY: 1953 ttyrubo(tp, 1); 1954 break; 1955 case BACKSPACE: 1956 case CONTROL: 1957 case NEWLINE: 1958 case RETURN: 1959 case VTAB: 1960 if (ISSET(tp->t_lflag, ECHOCTL)) 1961 ttyrubo(tp, 2); 1962 break; 1963 case TAB: 1964 if (tp->t_rocount < tp->t_rawq.c_cc) { 1965 ttyretype(tp); 1966 return; 1967 } 1968 s = spltty(); 1969 savecol = tp->t_column; 1970 SET(tp->t_state, TS_CNTTB); 1971 SET(tp->t_lflag, FLUSHO); 1972 tp->t_column = tp->t_rocol; 1973 cp = tp->t_rawq.c_cf; 1974 if (cp) 1975 tabc = *cp; /* XXX FIX NEXTC */ 1976 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 1977 ttyecho(tabc, tp); 1978 CLR(tp->t_lflag, FLUSHO); 1979 CLR(tp->t_state, TS_CNTTB); 1980 splx(s); 1981 1982 /* savecol will now be length of the tab. */ 1983 savecol -= tp->t_column; 1984 tp->t_column += savecol; 1985 if (savecol > 8) 1986 savecol = 8; /* overflow screw */ 1987 while (--savecol >= 0) 1988 (void)ttyoutput('\b', tp); 1989 break; 1990 default: /* XXX */ 1991#define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 1992 (void)printf(PANICSTR, c, CCLASS(c)); 1993#ifdef notdef 1994 panic(PANICSTR, c, CCLASS(c)); 1995#endif 1996 } 1997 } 1998 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 1999 if (!ISSET(tp->t_state, TS_ERASE)) { 2000 SET(tp->t_state, TS_ERASE); 2001 (void)ttyoutput('\\', tp); 2002 } 2003 ttyecho(c, tp); 2004 } else 2005 ttyecho(tp->t_cc[VERASE], tp); 2006 --tp->t_rocount; 2007} 2008 2009/* 2010 * Back over cnt characters, erasing them. 2011 */ 2012static void 2013ttyrubo(tp, cnt) 2014 register struct tty *tp; 2015 int cnt; 2016{ 2017 2018 while (cnt-- > 0) { 2019 (void)ttyoutput('\b', tp); 2020 (void)ttyoutput(' ', tp); 2021 (void)ttyoutput('\b', tp); 2022 } 2023} 2024 2025/* 2026 * ttyretype -- 2027 * Reprint the rawq line. Note, it is assumed that c_cc has already 2028 * been checked. 2029 */ 2030static void 2031ttyretype(tp) 2032 register struct tty *tp; 2033{ 2034 register char *cp; 2035 int s, c; 2036 2037 /* Echo the reprint character. */ 2038 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 2039 ttyecho(tp->t_cc[VREPRINT], tp); 2040 2041 (void)ttyoutput('\n', tp); 2042 2043 /* 2044 * XXX 2045 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 2046 * BIT OF FIRST CHAR. 2047 */ 2048 s = spltty(); 2049 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 2050 cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 2051 ttyecho(c, tp); 2052 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 2053 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 2054 ttyecho(c, tp); 2055 CLR(tp->t_state, TS_ERASE); 2056 splx(s); 2057 2058 tp->t_rocount = tp->t_rawq.c_cc; 2059 tp->t_rocol = 0; 2060} 2061 2062/* 2063 * Echo a typed character to the terminal. 2064 */ 2065static void 2066ttyecho(c, tp) 2067 register int c; 2068 register struct tty *tp; 2069{ 2070 2071 if (!ISSET(tp->t_state, TS_CNTTB)) 2072 CLR(tp->t_lflag, FLUSHO); 2073 if ((!ISSET(tp->t_lflag, ECHO) && 2074 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) || 2075 ISSET(tp->t_lflag, EXTPROC)) 2076 return; 2077 if (ISSET(tp->t_lflag, ECHOCTL) && 2078 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') || 2079 ISSET(c, TTY_CHARMASK) == 0177)) { 2080 (void)ttyoutput('^', tp); 2081 CLR(c, ~TTY_CHARMASK); 2082 if (c == 0177) 2083 c = '?'; 2084 else 2085 c += 'A' - 1; 2086 } 2087 (void)ttyoutput(c, tp); 2088} 2089 2090/* 2091 * Wake up any readers on a tty. 2092 */ 2093void 2094ttwakeup(tp) 2095 register struct tty *tp; 2096{ 2097 2098 if (tp->t_rsel.si_pid != 0) 2099 selwakeup(&tp->t_rsel); 2100 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2101 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2102 wakeup(TSA_HUP_OR_INPUT(tp)); 2103} 2104 2105/* 2106 * Wake up any writers on a tty. 2107 */ 2108void 2109ttwwakeup(tp) 2110 register struct tty *tp; 2111{ 2112 2113 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat) 2114 selwakeup(&tp->t_wsel); 2115 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == 2116 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { 2117 CLR(tp->t_state, TS_SO_OCOMPLETE); 2118 wakeup(TSA_OCOMPLETE(tp)); 2119 } 2120 if (ISSET(tp->t_state, TS_SO_OLOWAT) && 2121 tp->t_outq.c_cc <= tp->t_olowat) { 2122 CLR(tp->t_state, TS_SO_OLOWAT); 2123 wakeup(TSA_OLOWAT(tp)); 2124 } 2125} 2126 2127/* 2128 * Look up a code for a specified speed in a conversion table; 2129 * used by drivers to map software speed values to hardware parameters. 2130 */ 2131int 2132ttspeedtab(speed, table) 2133 int speed; 2134 register struct speedtab *table; 2135{ 2136 2137 for ( ; table->sp_speed != -1; table++) 2138 if (table->sp_speed == speed) 2139 return (table->sp_code); 2140 return (-1); 2141} 2142 2143/* 2144 * Set input and output watermarks and buffer sizes. For input, the 2145 * high watermark is about one second's worth of input above empty, the 2146 * low watermark is slightly below high water, and the buffer size is a 2147 * driver-dependent amount above high water. For output, the watermarks 2148 * are near the ends of the buffer, with about 1 second's worth of input 2149 * between them. All this only applies to the standard line discipline. 2150 */ 2151void 2152ttsetwater(tp) 2153 struct tty *tp; 2154{ 2155 register int cps, ttmaxhiwat, x; 2156 2157 /* Input. */ 2158 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512); 2159 switch (tp->t_ispeedwat) { 2160 case (speed_t)-1: 2161 cps = tp->t_ispeed / 10; 2162 break; 2163 case 0: 2164 /* 2165 * This case is for old drivers that don't know about 2166 * t_ispeedwat. Arrange for them to get the old buffer 2167 * sizes and watermarks. 2168 */ 2169 cps = TTYHOG - 2 * 256; 2170 tp->t_ififosize = 2 * 256; 2171 break; 2172 default: 2173 cps = tp->t_ispeedwat / 10; 2174 break; 2175 } 2176 tp->t_ihiwat = cps; 2177 tp->t_ilowat = 7 * cps / 8; 2178 x = cps + tp->t_ififosize; 2179 clist_alloc_cblocks(&tp->t_rawq, x, x); 2180 2181 /* Output. */ 2182 switch (tp->t_ospeedwat) { 2183 case (speed_t)-1: 2184 cps = tp->t_ospeed / 10; 2185 ttmaxhiwat = 2 * TTMAXHIWAT; 2186 break; 2187 case 0: 2188 cps = tp->t_ospeed / 10; 2189 ttmaxhiwat = TTMAXHIWAT; 2190 break; 2191 default: 2192 cps = tp->t_ospeedwat / 10; 2193 ttmaxhiwat = 8 * TTMAXHIWAT; 2194 break; 2195 } 2196#define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 2197 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 2198 x += cps; 2199 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */ 2200 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */ 2201 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */ 2202 x += OBUFSIZ + 100; 2203 clist_alloc_cblocks(&tp->t_outq, x, x); 2204#undef CLAMP 2205} 2206 2207/* 2208 * Report on state of foreground process group. 2209 */ 2210void 2211ttyinfo(tp) 2212 register struct tty *tp; 2213{ 2214 register struct proc *p, *pick; 2215 struct timeval utime, stime; 2216 int tmp; 2217 2218 if (ttycheckoutq(tp,0) == 0) 2219 return; 2220 2221 /* Print load average. */ 2222 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 2223 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 2224 2225 if (tp->t_session == NULL) 2226 ttyprintf(tp, "not a controlling terminal\n"); 2227 else if (tp->t_pgrp == NULL) 2228 ttyprintf(tp, "no foreground process group\n"); 2229 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0) 2230 ttyprintf(tp, "empty foreground process group\n"); 2231 else { 2232 /* Pick interesting process. */ 2233 for (pick = NULL; p != 0; p = p->p_pglist.le_next) 2234 if (proc_compare(pick, p)) 2235 pick = p; 2236 2237 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 2238 pick->p_stat == SRUN ? "running" : 2239 pick->p_wmesg ? pick->p_wmesg : "iowait"); 2240 2241 if (pick->p_flag & P_INMEM) { 2242 calcru(pick, &utime, &stime, NULL); 2243 2244 /* Print user time. */ 2245 ttyprintf(tp, "%ld.%02ldu ", 2246 utime.tv_sec, utime.tv_usec / 10000); 2247 2248 /* Print system time. */ 2249 ttyprintf(tp, "%ld.%02lds ", 2250 stime.tv_sec, stime.tv_usec / 10000); 2251 } else 2252 ttyprintf(tp, "?.??u ?.??s "); 2253 2254 /* Print percentage cpu, resident set size. */ 2255 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT; 2256 ttyprintf(tp, "%d%% %ldk\n", 2257 tmp / 100, 2258 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 : 2259 (long)pgtok(vmspace_resident_count(pick->p_vmspace))); 2260 } 2261 tp->t_rocount = 0; /* so pending input will be retyped if BS */ 2262} 2263 2264/* 2265 * Returns 1 if p2 is "better" than p1 2266 * 2267 * The algorithm for picking the "interesting" process is thus: 2268 * 2269 * 1) Only foreground processes are eligible - implied. 2270 * 2) Runnable processes are favored over anything else. The runner 2271 * with the highest cpu utilization is picked (p_estcpu). Ties are 2272 * broken by picking the highest pid. 2273 * 3) The sleeper with the shortest sleep time is next. With ties, 2274 * we pick out just "short-term" sleepers (P_SINTR == 0). 2275 * 4) Further ties are broken by picking the highest pid. 2276 */ 2277#define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 2278#define TESTAB(a, b) ((a)<<1 | (b)) 2279#define ONLYA 2 2280#define ONLYB 1 2281#define BOTH 3 2282 2283static int 2284proc_compare(p1, p2) 2285 register struct proc *p1, *p2; 2286{ 2287 2288 if (p1 == NULL) 2289 return (1); 2290 /* 2291 * see if at least one of them is runnable 2292 */ 2293 switch (TESTAB(ISRUN(p1), ISRUN(p2))) { 2294 case ONLYA: 2295 return (0); 2296 case ONLYB: 2297 return (1); 2298 case BOTH: 2299 /* 2300 * tie - favor one with highest recent cpu utilization 2301 */ 2302 if (p2->p_estcpu > p1->p_estcpu) 2303 return (1); 2304 if (p1->p_estcpu > p2->p_estcpu) 2305 return (0); 2306 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2307 } 2308 /* 2309 * weed out zombies 2310 */ 2311 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { 2312 case ONLYA: 2313 return (1); 2314 case ONLYB: 2315 return (0); 2316 case BOTH: 2317 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2318 } 2319 /* 2320 * pick the one with the smallest sleep time 2321 */ 2322 if (p2->p_slptime > p1->p_slptime) 2323 return (0); 2324 if (p1->p_slptime > p2->p_slptime) 2325 return (1); 2326 /* 2327 * favor one sleeping in a non-interruptible sleep 2328 */ 2329 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0) 2330 return (1); 2331 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0) 2332 return (0); 2333 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2334} 2335 2336/* 2337 * Output char to tty; console putchar style. 2338 */ 2339int 2340tputchar(c, tp) 2341 int c; 2342 struct tty *tp; 2343{ 2344 register int s; 2345 2346 s = spltty(); 2347 if (!ISSET(tp->t_state, TS_CONNECTED)) { 2348 splx(s); 2349 return (-1); 2350 } 2351 if (c == '\n') 2352 (void)ttyoutput('\r', tp); 2353 (void)ttyoutput(c, tp); 2354 ttstart(tp); 2355 splx(s); 2356 return (0); 2357} 2358 2359/* 2360 * Sleep on chan, returning ERESTART if tty changed while we napped and 2361 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If 2362 * the tty is revoked, restarting a pending call will redo validation done 2363 * at the start of the call. 2364 */ 2365int 2366ttysleep(tp, chan, pri, wmesg, timo) 2367 struct tty *tp; 2368 void *chan; 2369 int pri, timo; 2370 char *wmesg; 2371{ 2372 int error; 2373 int gen; 2374 2375 gen = tp->t_gen; 2376 error = tsleep(chan, pri, wmesg, timo); 2377 if (error) 2378 return (error); 2379 return (tp->t_gen == gen ? 0 : ERESTART); 2380} 2381 2382/* 2383 * Allocate a tty struct. Clists in the struct will be allocated by 2384 * ttyopen(). 2385 */ 2386struct tty * 2387ttymalloc(tp) 2388 struct tty *tp; 2389{ 2390 2391 if (tp) 2392 return(tp); 2393 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK); 2394 bzero(tp, sizeof *tp); 2395 ttyregister(tp); 2396 return (tp); 2397} 2398 2399#if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */ 2400/* 2401 * Free a tty struct. Clists in the struct should have been freed by 2402 * ttyclose(). 2403 */ 2404void 2405ttyfree(tp) 2406 struct tty *tp; 2407{ 2408 free(tp, M_TTYS); 2409} 2410#endif /* 0 */ 2411 2412void 2413ttyregister(tp) 2414 struct tty *tp; 2415{ 2416 SLIST_INSERT_HEAD(&tty_list, tp, t_list); 2417} 2418 2419static int 2420sysctl_kern_ttys SYSCTL_HANDLER_ARGS 2421{ 2422 int error; 2423 struct tty *tp, t; 2424 SLIST_FOREACH(tp, &tty_list, t_list) { 2425 t = *tp; 2426 if (t.t_dev) 2427 t.t_dev = (dev_t)dev2udev(t.t_dev); 2428 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t)); 2429 if (error) 2430 return (error); 2431 } 2432 return (0); 2433} 2434 2435SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD, 2436 0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys"); 2437 2438void 2439nottystop(tp, rw) 2440 struct tty *tp; 2441 int rw; 2442{ 2443 return; 2444} 2445