tty.c revision 59786
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 3. All advertising materials mentioning features or use of this software 191541Srgrimes * must display the following acknowledgement: 201541Srgrimes * This product includes software developed by the University of 211541Srgrimes * California, Berkeley and its contributors. 221541Srgrimes * 4. Neither the name of the University nor the names of its contributors 231541Srgrimes * may be used to endorse or promote products derived from this software 241541Srgrimes * without specific prior written permission. 251541Srgrimes * 261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3314501Shsu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3450477Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361541Srgrimes * SUCH DAMAGE. 371541Srgrimes * 381541Srgrimes * @(#)tty.c 8.8 (Berkeley) 1/21/94 391541Srgrimes * $FreeBSD: head/sys/kern/tty.c 59786 2000-04-30 16:00:53Z ache $ 4034924Sbde */ 4165495Struckman 4276166Smarkm/*- 4376166Smarkm * TODO: 4476166Smarkm * o Fix races for sending the start char in ttyflush(). 4576166Smarkm * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect(). 4634924Sbde * With luck, there will be MIN chars before select() returns(). 471541Srgrimes * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it. 481541Srgrimes * o Don't allow input in TS_ZOMBIE case. It would be visible through 491541Srgrimes * FIONREAD. 501541Srgrimes * o Do the new sio locking stuff here and use it to avoid special 511541Srgrimes * case for EXTPROC? 521541Srgrimes * o Lock PENDIN too? 531541Srgrimes * o Move EXTPROC and/or PENDIN to t_state? 541541Srgrimes * o Wrap most of ttioctl in spltty/splx. 551541Srgrimes * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>. 561541Srgrimes * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set. 571541Srgrimes * o Don't allow certain termios flags to affect disciplines other 581541Srgrimes * than TTYDISC. Cancel their effects before switch disciplines 591541Srgrimes * and ignore them if they are set while we are in another 601541Srgrimes * discipline. 611541Srgrimes * o Now that historical speed conversions are handled here, don't 621541Srgrimes * do them in drivers. 631541Srgrimes * o Check for TS_CARR_ON being set while everything is closed and not 641541Srgrimes * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open, 651541Srgrimes * so it would live until the next open even if carrier drops. 6681493Sjhb * o Restore TS_WOPEN since it is useful in pstat. It must be cleared 671541Srgrimes * only when _all_ openers leave open(). 681541Srgrimes */ 691541Srgrimes 701541Srgrimes#include "snp.h" 711541Srgrimes#include "opt_compat.h" 721541Srgrimes#include "opt_uconsole.h" 731541Srgrimes 741541Srgrimes#include <sys/param.h> 751541Srgrimes#include <sys/systm.h> 761541Srgrimes#include <sys/filio.h> 771541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 781541Srgrimes#include <sys/ioctl_compat.h> 791541Srgrimes#endif 801541Srgrimes#include <sys/proc.h> 811541Srgrimes#define TTYDEFCHARS 821541Srgrimes#include <sys/tty.h> 831541Srgrimes#undef TTYDEFCHARS 841541Srgrimes#include <sys/fcntl.h> 8536441Sphk#include <sys/conf.h> 861541Srgrimes#include <sys/dkstat.h> 871541Srgrimes#include <sys/poll.h> 8869403Salfred#include <sys/kernel.h> 8969403Salfred#include <sys/vnode.h> 9065495Struckman#include <sys/signalvar.h> 9165495Struckman#include <sys/resourcevar.h> 9265495Struckman#include <sys/malloc.h> 9365495Struckman#include <sys/filedesc.h> 9465495Struckman#if NSNP > 0 9565495Struckman#include <sys/snoop.h> 9665495Struckman#endif 9765495Struckman#include <sys/sysctl.h> 9865495Struckman 9989594Salfred#include <vm/vm.h> 10065495Struckman#include <sys/lock.h> 10165495Struckman#include <vm/pmap.h> 10291342Salfred#include <vm/vm_map.h> 10391342Salfred 10489594SalfredMALLOC_DEFINE(M_TTYS, "ttys", "tty data structures"); 10583366Sjulian 10683366Sjulianstatic int proc_compare __P((struct proc *p1, struct proc *p2)); 10734924Sbdestatic int ttnread __P((struct tty *tp)); 10834924Sbdestatic void ttyecho __P((int c, struct tty *tp)); 10992719Salfredstatic int ttyoutput __P((int c, register struct tty *tp)); 11092719Salfredstatic void ttypend __P((struct tty *tp)); 11192719Salfredstatic void ttyretype __P((struct tty *tp)); 11292719Salfredstatic void ttyrub __P((int c, struct tty *tp)); 11392719Salfredstatic void ttyrubo __P((struct tty *tp, int cnt)); 114100591Sjdpstatic void ttyunblock __P((struct tty *tp)); 11592719Salfredstatic int ttywflush __P((struct tty *tp)); 11692719Salfred 1171541Srgrimes/* 11892719Salfred * Table with character classes and parity. The 8th bit indicates parity, 11992719Salfred * the 7th bit indicates the character is an alphameric or underscore (for 12092719Salfred * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 12165495Struckman * are 0 then the character needs no special processing on output; classes 12292719Salfred * other than 0 might be translated or (not currently) require delays. 12392719Salfred */ 12492719Salfred#define E 0x00 /* Even parity. */ 12592719Salfred#define O 0x80 /* Odd parity. */ 1261541Srgrimes#define PARITY(c) (char_type[c] & O) 1274470Sbde 1281541Srgrimes#define ALPHA 0x40 /* Alpha or underscore. */ 129#define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 130 131#define CCLASSMASK 0x3f 132#define CCLASS(c) (char_type[c] & CCLASSMASK) 133 134#define BS BACKSPACE 135#define CC CONTROL 136#define CR RETURN 137#define NA ORDINARY | ALPHA 138#define NL NEWLINE 139#define NO ORDINARY 140#define TB TAB 141#define VT VTAB 142 143static u_char const char_type[] = { 144 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 145 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 146 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 147 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 148 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 149 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 150 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 151 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 152 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 153 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 154 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 155 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 156 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 157 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 158 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 159 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 160 /* 161 * Meta chars; should be settable per character set; 162 * for now, treat them all as normal characters. 163 */ 164 NA, NA, NA, NA, NA, NA, NA, NA, 165 NA, NA, NA, NA, NA, NA, NA, NA, 166 NA, NA, NA, NA, NA, NA, NA, NA, 167 NA, NA, NA, NA, NA, NA, NA, NA, 168 NA, NA, NA, NA, NA, NA, NA, NA, 169 NA, NA, NA, NA, NA, NA, NA, NA, 170 NA, NA, NA, NA, NA, NA, NA, NA, 171 NA, NA, NA, NA, NA, NA, NA, NA, 172 NA, NA, NA, NA, NA, NA, NA, NA, 173 NA, NA, NA, NA, NA, NA, NA, NA, 174 NA, NA, NA, NA, NA, NA, NA, NA, 175 NA, NA, NA, NA, NA, NA, NA, NA, 176 NA, NA, NA, NA, NA, NA, NA, NA, 177 NA, NA, NA, NA, NA, NA, NA, NA, 178 NA, NA, NA, NA, NA, NA, NA, NA, 179 NA, NA, NA, NA, NA, NA, NA, NA, 180}; 181#undef BS 182#undef CC 183#undef CR 184#undef NA 185#undef NL 186#undef NO 187#undef TB 188#undef VT 189 190/* Macros to clear/set/test flags. */ 191#define SET(t, f) (t) |= (f) 192#define CLR(t, f) (t) &= ~(f) 193#define ISSET(t, f) ((t) & (f)) 194 195#undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */ 196#define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */ 197 198/* 199 * list of struct tty where pstat(8) can pick it up with sysctl 200 */ 201static SLIST_HEAD(, tty) tty_list; 202 203static int drainwait = 5*60; 204SYSCTL_INT(_kern, OID_AUTO, drainwait, CTLFLAG_RW, &drainwait, 205 0, "Output drain timeout in seconds"); 206 207/* 208 * Initial open of tty, or (re)entry to standard tty line discipline. 209 */ 210int 211ttyopen(device, tp) 212 dev_t device; 213 register struct tty *tp; 214{ 215 int s; 216 217 s = spltty(); 218 tp->t_dev = device; 219 if (!ISSET(tp->t_state, TS_ISOPEN)) { 220 SET(tp->t_state, TS_ISOPEN); 221 if (ISSET(tp->t_cflag, CLOCAL)) 222 SET(tp->t_state, TS_CONNECTED); 223 bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 224 } 225 /* XXX don't hang forever on output */ 226 tp->t_timeout = drainwait*hz; 227 ttsetwater(tp); 228 splx(s); 229 return (0); 230} 231 232/* 233 * Handle close() on a tty line: flush and set to initial state, 234 * bumping generation number so that pending read/write calls 235 * can detect recycling of the tty. 236 * XXX our caller should have done `spltty(); l_close(); ttyclose();' 237 * and l_close() should have flushed, but we repeat the spltty() and 238 * the flush in case there are buggy callers. 239 */ 240int 241ttyclose(tp) 242 register struct tty *tp; 243{ 244 int s; 245 246 funsetown(tp->t_sigio); 247 s = spltty(); 248 if (constty == tp) 249 constty = NULL; 250 251 ttyflush(tp, FREAD | FWRITE); 252 clist_free_cblocks(&tp->t_canq); 253 clist_free_cblocks(&tp->t_outq); 254 clist_free_cblocks(&tp->t_rawq); 255 256#if NSNP > 0 257 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 258 snpdown((struct snoop *)tp->t_sc); 259#endif 260 261 tp->t_gen++; 262 tp->t_line = TTYDISC; 263 tp->t_pgrp = NULL; 264 tp->t_session = NULL; 265 tp->t_state = 0; 266 splx(s); 267 return (0); 268} 269 270#define FLUSHQ(q) { \ 271 if ((q)->c_cc) \ 272 ndflush(q, (q)->c_cc); \ 273} 274 275/* Is 'c' a line delimiter ("break" character)? */ 276#define TTBREAKC(c, lflag) \ 277 ((c) == '\n' || (((c) == cc[VEOF] || \ 278 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \ 279 (c) != _POSIX_VDISABLE)) 280 281/* 282 * Process input of a single character received on a tty. 283 */ 284int 285ttyinput(c, tp) 286 register int c; 287 register struct tty *tp; 288{ 289 register tcflag_t iflag, lflag; 290 register cc_t *cc; 291 int i, err; 292 293 /* 294 * If input is pending take it first. 295 */ 296 lflag = tp->t_lflag; 297 if (ISSET(lflag, PENDIN)) 298 ttypend(tp); 299 /* 300 * Gather stats. 301 */ 302 if (ISSET(lflag, ICANON)) { 303 ++tk_cancc; 304 ++tp->t_cancc; 305 } else { 306 ++tk_rawcc; 307 ++tp->t_rawcc; 308 } 309 ++tk_nin; 310 311 /* 312 * Block further input iff: 313 * current input > threshold AND input is available to user program 314 * AND input flow control is enabled and not yet invoked. 315 * The 3 is slop for PARMRK. 316 */ 317 iflag = tp->t_iflag; 318 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 && 319 (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) && 320 (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) && 321 !ISSET(tp->t_state, TS_TBLOCK)) 322 ttyblock(tp); 323 324 /* Handle exceptional conditions (break, parity, framing). */ 325 cc = tp->t_cc; 326 err = (ISSET(c, TTY_ERRORMASK)); 327 if (err) { 328 CLR(c, TTY_ERRORMASK); 329 if (ISSET(err, TTY_BI)) { 330 if (ISSET(iflag, IGNBRK)) 331 return (0); 332 if (ISSET(iflag, BRKINT)) { 333 ttyflush(tp, FREAD | FWRITE); 334 pgsignal(tp->t_pgrp, SIGINT, 1); 335 goto endcase; 336 } 337 if (ISSET(iflag, PARMRK)) 338 goto parmrk; 339 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK)) 340 || ISSET(err, TTY_FE)) { 341 if (ISSET(iflag, IGNPAR)) 342 return (0); 343 else if (ISSET(iflag, PARMRK)) { 344parmrk: 345 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > 346 MAX_INPUT - 3) 347 goto input_overflow; 348 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 349 (void)putc(0 | TTY_QUOTE, &tp->t_rawq); 350 (void)putc(c | TTY_QUOTE, &tp->t_rawq); 351 goto endcase; 352 } else 353 c = 0; 354 } 355 } 356 357 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP)) 358 CLR(c, 0x80); 359 if (!ISSET(lflag, EXTPROC)) { 360 /* 361 * Check for literal nexting very first 362 */ 363 if (ISSET(tp->t_state, TS_LNCH)) { 364 SET(c, TTY_QUOTE); 365 CLR(tp->t_state, TS_LNCH); 366 } 367 /* 368 * Scan for special characters. This code 369 * is really just a big case statement with 370 * non-constant cases. The bottom of the 371 * case statement is labeled ``endcase'', so goto 372 * it after a case match, or similar. 373 */ 374 375 /* 376 * Control chars which aren't controlled 377 * by ICANON, ISIG, or IXON. 378 */ 379 if (ISSET(lflag, IEXTEN)) { 380 if (CCEQ(cc[VLNEXT], c)) { 381 if (ISSET(lflag, ECHO)) { 382 if (ISSET(lflag, ECHOE)) { 383 (void)ttyoutput('^', tp); 384 (void)ttyoutput('\b', tp); 385 } else 386 ttyecho(c, tp); 387 } 388 SET(tp->t_state, TS_LNCH); 389 goto endcase; 390 } 391 if (CCEQ(cc[VDISCARD], c)) { 392 if (ISSET(lflag, FLUSHO)) 393 CLR(tp->t_lflag, FLUSHO); 394 else { 395 ttyflush(tp, FWRITE); 396 ttyecho(c, tp); 397 if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 398 ttyretype(tp); 399 SET(tp->t_lflag, FLUSHO); 400 } 401 goto startoutput; 402 } 403 } 404 /* 405 * Signals. 406 */ 407 if (ISSET(lflag, ISIG)) { 408 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 409 if (!ISSET(lflag, NOFLSH)) 410 ttyflush(tp, FREAD | FWRITE); 411 ttyecho(c, tp); 412 pgsignal(tp->t_pgrp, 413 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 414 goto endcase; 415 } 416 if (CCEQ(cc[VSUSP], c)) { 417 if (!ISSET(lflag, NOFLSH)) 418 ttyflush(tp, FREAD); 419 ttyecho(c, tp); 420 pgsignal(tp->t_pgrp, SIGTSTP, 1); 421 goto endcase; 422 } 423 } 424 /* 425 * Handle start/stop characters. 426 */ 427 if (ISSET(iflag, IXON)) { 428 if (CCEQ(cc[VSTOP], c)) { 429 if (!ISSET(tp->t_state, TS_TTSTOP)) { 430 SET(tp->t_state, TS_TTSTOP); 431 (*tp->t_stop)(tp, 0); 432 return (0); 433 } 434 if (!CCEQ(cc[VSTART], c)) 435 return (0); 436 /* 437 * if VSTART == VSTOP then toggle 438 */ 439 goto endcase; 440 } 441 if (CCEQ(cc[VSTART], c)) 442 goto restartoutput; 443 } 444 /* 445 * IGNCR, ICRNL, & INLCR 446 */ 447 if (c == '\r') { 448 if (ISSET(iflag, IGNCR)) 449 return (0); 450 else if (ISSET(iflag, ICRNL)) 451 c = '\n'; 452 } else if (c == '\n' && ISSET(iflag, INLCR)) 453 c = '\r'; 454 } 455 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) { 456 /* 457 * From here on down canonical mode character 458 * processing takes place. 459 */ 460 /* 461 * erase (^H / ^?) 462 */ 463 if (CCEQ(cc[VERASE], c)) { 464 if (tp->t_rawq.c_cc) 465 ttyrub(unputc(&tp->t_rawq), tp); 466 goto endcase; 467 } 468 /* 469 * kill (^U) 470 */ 471 if (CCEQ(cc[VKILL], c)) { 472 if (ISSET(lflag, ECHOKE) && 473 tp->t_rawq.c_cc == tp->t_rocount && 474 !ISSET(lflag, ECHOPRT)) 475 while (tp->t_rawq.c_cc) 476 ttyrub(unputc(&tp->t_rawq), tp); 477 else { 478 ttyecho(c, tp); 479 if (ISSET(lflag, ECHOK) || 480 ISSET(lflag, ECHOKE)) 481 ttyecho('\n', tp); 482 FLUSHQ(&tp->t_rawq); 483 tp->t_rocount = 0; 484 } 485 CLR(tp->t_state, TS_LOCAL); 486 goto endcase; 487 } 488 /* 489 * word erase (^W) 490 */ 491 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) { 492 int ctype; 493 494 /* 495 * erase whitespace 496 */ 497 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 498 ttyrub(c, tp); 499 if (c == -1) 500 goto endcase; 501 /* 502 * erase last char of word and remember the 503 * next chars type (for ALTWERASE) 504 */ 505 ttyrub(c, tp); 506 c = unputc(&tp->t_rawq); 507 if (c == -1) 508 goto endcase; 509 if (c == ' ' || c == '\t') { 510 (void)putc(c, &tp->t_rawq); 511 goto endcase; 512 } 513 ctype = ISALPHA(c); 514 /* 515 * erase rest of word 516 */ 517 do { 518 ttyrub(c, tp); 519 c = unputc(&tp->t_rawq); 520 if (c == -1) 521 goto endcase; 522 } while (c != ' ' && c != '\t' && 523 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype)); 524 (void)putc(c, &tp->t_rawq); 525 goto endcase; 526 } 527 /* 528 * reprint line (^R) 529 */ 530 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) { 531 ttyretype(tp); 532 goto endcase; 533 } 534 /* 535 * ^T - kernel info and generate SIGINFO 536 */ 537 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) { 538 if (ISSET(lflag, ISIG)) 539 pgsignal(tp->t_pgrp, SIGINFO, 1); 540 if (!ISSET(lflag, NOKERNINFO)) 541 ttyinfo(tp); 542 goto endcase; 543 } 544 } 545 /* 546 * Check for input buffer overflow 547 */ 548 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) { 549input_overflow: 550 if (ISSET(iflag, IMAXBEL)) { 551 if (tp->t_outq.c_cc < tp->t_ohiwat) 552 (void)ttyoutput(CTRL('g'), tp); 553 } 554 goto endcase; 555 } 556 557 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP) 558 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR)) 559 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 560 561 /* 562 * Put data char in q for user and 563 * wakeup on seeing a line delimiter. 564 */ 565 if (putc(c, &tp->t_rawq) >= 0) { 566 if (!ISSET(lflag, ICANON)) { 567 ttwakeup(tp); 568 ttyecho(c, tp); 569 goto endcase; 570 } 571 if (TTBREAKC(c, lflag)) { 572 tp->t_rocount = 0; 573 catq(&tp->t_rawq, &tp->t_canq); 574 ttwakeup(tp); 575 } else if (tp->t_rocount++ == 0) 576 tp->t_rocol = tp->t_column; 577 if (ISSET(tp->t_state, TS_ERASE)) { 578 /* 579 * end of prterase \.../ 580 */ 581 CLR(tp->t_state, TS_ERASE); 582 (void)ttyoutput('/', tp); 583 } 584 i = tp->t_column; 585 ttyecho(c, tp); 586 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) { 587 /* 588 * Place the cursor over the '^' of the ^D. 589 */ 590 i = imin(2, tp->t_column - i); 591 while (i > 0) { 592 (void)ttyoutput('\b', tp); 593 i--; 594 } 595 } 596 } 597endcase: 598 /* 599 * IXANY means allow any character to restart output. 600 */ 601 if (ISSET(tp->t_state, TS_TTSTOP) && 602 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) 603 return (0); 604restartoutput: 605 CLR(tp->t_lflag, FLUSHO); 606 CLR(tp->t_state, TS_TTSTOP); 607startoutput: 608 return (ttstart(tp)); 609} 610 611/* 612 * Output a single character on a tty, doing output processing 613 * as needed (expanding tabs, newline processing, etc.). 614 * Returns < 0 if succeeds, otherwise returns char to resend. 615 * Must be recursive. 616 */ 617static int 618ttyoutput(c, tp) 619 register int c; 620 register struct tty *tp; 621{ 622 register tcflag_t oflag; 623 register int col, s; 624 625 oflag = tp->t_oflag; 626 if (!ISSET(oflag, OPOST)) { 627 if (ISSET(tp->t_lflag, FLUSHO)) 628 return (-1); 629 if (putc(c, &tp->t_outq)) 630 return (c); 631 tk_nout++; 632 tp->t_outcc++; 633 return (-1); 634 } 635 /* 636 * Do tab expansion if OXTABS is set. Special case if we external 637 * processing, we don't do the tab expansion because we'll probably 638 * get it wrong. If tab expansion needs to be done, let it happen 639 * externally. 640 */ 641 CLR(c, ~TTY_CHARMASK); 642 if (c == '\t' && 643 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) { 644 c = 8 - (tp->t_column & 7); 645 if (!ISSET(tp->t_lflag, FLUSHO)) { 646 s = spltty(); /* Don't interrupt tabs. */ 647 c -= b_to_q(" ", c, &tp->t_outq); 648 tk_nout += c; 649 tp->t_outcc += c; 650 splx(s); 651 } 652 tp->t_column += c; 653 return (c ? -1 : '\t'); 654 } 655 if (c == CEOT && ISSET(oflag, ONOEOT)) 656 return (-1); 657 658 /* 659 * Newline translation: if ONLCR is set, 660 * translate newline into "\r\n". 661 */ 662 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) { 663 tk_nout++; 664 tp->t_outcc++; 665 if (putc('\r', &tp->t_outq)) 666 return (c); 667 } 668 tk_nout++; 669 tp->t_outcc++; 670 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq)) 671 return (c); 672 673 col = tp->t_column; 674 switch (CCLASS(c)) { 675 case BACKSPACE: 676 if (col > 0) 677 --col; 678 break; 679 case CONTROL: 680 break; 681 case NEWLINE: 682 case RETURN: 683 col = 0; 684 break; 685 case ORDINARY: 686 ++col; 687 break; 688 case TAB: 689 col = (col + 8) & ~7; 690 break; 691 } 692 tp->t_column = col; 693 return (-1); 694} 695 696/* 697 * Ioctls for all tty devices. Called after line-discipline specific ioctl 698 * has been called to do discipline-specific functions and/or reject any 699 * of these ioctl commands. 700 */ 701/* ARGSUSED */ 702int 703ttioctl(tp, cmd, data, flag) 704 register struct tty *tp; 705 u_long cmd; 706 int flag; 707 void *data; 708{ 709 register struct proc *p; 710 int s, error; 711 712 p = curproc; /* XXX */ 713 714 /* If the ioctl involves modification, hang if in the background. */ 715 switch (cmd) { 716 case TIOCCBRK: 717 case TIOCCONS: 718 case TIOCDRAIN: 719 case TIOCEXCL: 720 case TIOCFLUSH: 721#ifdef TIOCHPCL 722 case TIOCHPCL: 723#endif 724 case TIOCNXCL: 725 case TIOCSBRK: 726 case TIOCSCTTY: 727 case TIOCSDRAINWAIT: 728 case TIOCSETA: 729 case TIOCSETAF: 730 case TIOCSETAW: 731 case TIOCSETD: 732 case TIOCSPGRP: 733 case TIOCSTART: 734 case TIOCSTAT: 735 case TIOCSTI: 736 case TIOCSTOP: 737 case TIOCSWINSZ: 738#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 739 case TIOCLBIC: 740 case TIOCLBIS: 741 case TIOCLSET: 742 case TIOCSETC: 743 case OTIOCSETD: 744 case TIOCSETN: 745 case TIOCSETP: 746 case TIOCSLTC: 747#endif 748 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) && 749 !SIGISMEMBER(p->p_sigignore, SIGTTOU) && 750 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) { 751 if (p->p_pgrp->pg_jobc == 0) 752 return (EIO); 753 pgsignal(p->p_pgrp, SIGTTOU, 1); 754 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1", 755 0); 756 if (error) 757 return (error); 758 } 759 break; 760 } 761 762 switch (cmd) { /* Process the ioctl. */ 763 case FIOASYNC: /* set/clear async i/o */ 764 s = spltty(); 765 if (*(int *)data) 766 SET(tp->t_state, TS_ASYNC); 767 else 768 CLR(tp->t_state, TS_ASYNC); 769 splx(s); 770 break; 771 case FIONBIO: /* set/clear non-blocking i/o */ 772 break; /* XXX: delete. */ 773 case FIONREAD: /* get # bytes to read */ 774 s = spltty(); 775 *(int *)data = ttnread(tp); 776 splx(s); 777 break; 778 779 case FIOSETOWN: 780 /* 781 * Policy -- Don't allow FIOSETOWN on someone else's 782 * controlling tty 783 */ 784 if (tp->t_session != NULL && !isctty(p, tp)) 785 return (ENOTTY); 786 787 error = fsetown(*(int *)data, &tp->t_sigio); 788 if (error) 789 return (error); 790 break; 791 case FIOGETOWN: 792 if (tp->t_session != NULL && !isctty(p, tp)) 793 return (ENOTTY); 794 *(int *)data = fgetown(tp->t_sigio); 795 break; 796 797 case TIOCEXCL: /* set exclusive use of tty */ 798 s = spltty(); 799 SET(tp->t_state, TS_XCLUDE); 800 splx(s); 801 break; 802 case TIOCFLUSH: { /* flush buffers */ 803 register int flags = *(int *)data; 804 805 if (flags == 0) 806 flags = FREAD | FWRITE; 807 else 808 flags &= FREAD | FWRITE; 809 ttyflush(tp, flags); 810 break; 811 } 812 case TIOCCONS: /* become virtual console */ 813 if (*(int *)data) { 814 if (constty && constty != tp && 815 ISSET(constty->t_state, TS_CONNECTED)) 816 return (EBUSY); 817#ifndef UCONSOLE 818 if ((error = suser(p)) != 0) 819 return (error); 820#endif 821 constty = tp; 822 } else if (tp == constty) 823 constty = NULL; 824 break; 825 case TIOCDRAIN: /* wait till output drained */ 826 error = ttywait(tp); 827 if (error) 828 return (error); 829 break; 830 case TIOCGETA: { /* get termios struct */ 831 struct termios *t = (struct termios *)data; 832 833 bcopy(&tp->t_termios, t, sizeof(struct termios)); 834 break; 835 } 836 case TIOCGETD: /* get line discipline */ 837 *(int *)data = tp->t_line; 838 break; 839 case TIOCGWINSZ: /* get window size */ 840 *(struct winsize *)data = tp->t_winsize; 841 break; 842 case TIOCGPGRP: /* get pgrp of tty */ 843 if (!isctty(p, tp)) 844 return (ENOTTY); 845 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 846 break; 847#ifdef TIOCHPCL 848 case TIOCHPCL: /* hang up on last close */ 849 s = spltty(); 850 SET(tp->t_cflag, HUPCL); 851 splx(s); 852 break; 853#endif 854 case TIOCNXCL: /* reset exclusive use of tty */ 855 s = spltty(); 856 CLR(tp->t_state, TS_XCLUDE); 857 splx(s); 858 break; 859 case TIOCOUTQ: /* output queue size */ 860 *(int *)data = tp->t_outq.c_cc; 861 break; 862 case TIOCSETA: /* set termios struct */ 863 case TIOCSETAW: /* drain output, set */ 864 case TIOCSETAF: { /* drn out, fls in, set */ 865 register struct termios *t = (struct termios *)data; 866 867 if (t->c_ispeed == 0) 868 t->c_ispeed = t->c_ospeed; 869 if (t->c_ispeed == 0) 870 t->c_ispeed = tp->t_ospeed; 871 if (t->c_ispeed == 0) 872 return (EINVAL); 873 s = spltty(); 874 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 875 error = ttywait(tp); 876 if (error) { 877 splx(s); 878 return (error); 879 } 880 if (cmd == TIOCSETAF) 881 ttyflush(tp, FREAD); 882 } 883 if (!ISSET(t->c_cflag, CIGNORE)) { 884 /* 885 * Set device hardware. 886 */ 887 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 888 splx(s); 889 return (error); 890 } 891 if (ISSET(t->c_cflag, CLOCAL) && 892 !ISSET(tp->t_cflag, CLOCAL)) { 893 /* 894 * XXX disconnections would be too hard to 895 * get rid of without this kludge. The only 896 * way to get rid of controlling terminals 897 * is to exit from the session leader. 898 */ 899 CLR(tp->t_state, TS_ZOMBIE); 900 901 wakeup(TSA_CARR_ON(tp)); 902 ttwakeup(tp); 903 ttwwakeup(tp); 904 } 905 if ((ISSET(tp->t_state, TS_CARR_ON) || 906 ISSET(t->c_cflag, CLOCAL)) && 907 !ISSET(tp->t_state, TS_ZOMBIE)) 908 SET(tp->t_state, TS_CONNECTED); 909 else 910 CLR(tp->t_state, TS_CONNECTED); 911 tp->t_cflag = t->c_cflag; 912 tp->t_ispeed = t->c_ispeed; 913 if (t->c_ospeed != 0) 914 tp->t_ospeed = t->c_ospeed; 915 ttsetwater(tp); 916 } 917 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) && 918 cmd != TIOCSETAF) { 919 if (ISSET(t->c_lflag, ICANON)) 920 SET(tp->t_lflag, PENDIN); 921 else { 922 /* 923 * XXX we really shouldn't allow toggling 924 * ICANON while we're in a non-termios line 925 * discipline. Now we have to worry about 926 * panicing for a null queue. 927 */ 928 if (tp->t_canq.c_cbreserved > 0 && 929 tp->t_rawq.c_cbreserved > 0) { 930 catq(&tp->t_rawq, &tp->t_canq); 931 /* 932 * XXX the queue limits may be 933 * different, so the old queue 934 * swapping method no longer works. 935 */ 936 catq(&tp->t_canq, &tp->t_rawq); 937 } 938 CLR(tp->t_lflag, PENDIN); 939 } 940 ttwakeup(tp); 941 } 942 tp->t_iflag = t->c_iflag; 943 tp->t_oflag = t->c_oflag; 944 /* 945 * Make the EXTPROC bit read only. 946 */ 947 if (ISSET(tp->t_lflag, EXTPROC)) 948 SET(t->c_lflag, EXTPROC); 949 else 950 CLR(t->c_lflag, EXTPROC); 951 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 952 if (t->c_cc[VMIN] != tp->t_cc[VMIN] || 953 t->c_cc[VTIME] != tp->t_cc[VTIME]) 954 ttwakeup(tp); 955 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 956 splx(s); 957 break; 958 } 959 case TIOCSETD: { /* set line discipline */ 960 register int t = *(int *)data; 961 dev_t device = tp->t_dev; 962 963 if ((u_int)t >= nlinesw) 964 return (ENXIO); 965 if (t != tp->t_line) { 966 s = spltty(); 967 (*linesw[tp->t_line].l_close)(tp, flag); 968 error = (*linesw[t].l_open)(device, tp); 969 if (error) { 970 (void)(*linesw[tp->t_line].l_open)(device, tp); 971 splx(s); 972 return (error); 973 } 974 tp->t_line = t; 975 splx(s); 976 } 977 break; 978 } 979 case TIOCSTART: /* start output, like ^Q */ 980 s = spltty(); 981 if (ISSET(tp->t_state, TS_TTSTOP) || 982 ISSET(tp->t_lflag, FLUSHO)) { 983 CLR(tp->t_lflag, FLUSHO); 984 CLR(tp->t_state, TS_TTSTOP); 985 ttstart(tp); 986 } 987 splx(s); 988 break; 989 case TIOCSTI: /* simulate terminal input */ 990 if ((flag & FREAD) == 0 && suser(p)) 991 return (EPERM); 992 if (!isctty(p, tp) && suser(p)) 993 return (EACCES); 994 s = spltty(); 995 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 996 splx(s); 997 break; 998 case TIOCSTOP: /* stop output, like ^S */ 999 s = spltty(); 1000 if (!ISSET(tp->t_state, TS_TTSTOP)) { 1001 SET(tp->t_state, TS_TTSTOP); 1002 (*tp->t_stop)(tp, 0); 1003 } 1004 splx(s); 1005 break; 1006 case TIOCSCTTY: /* become controlling tty */ 1007 /* Session ctty vnode pointer set in vnode layer. */ 1008 if (!SESS_LEADER(p) || 1009 ((p->p_session->s_ttyvp || tp->t_session) && 1010 (tp->t_session != p->p_session))) 1011 return (EPERM); 1012 tp->t_session = p->p_session; 1013 tp->t_pgrp = p->p_pgrp; 1014 p->p_session->s_ttyp = tp; 1015 p->p_flag |= P_CONTROLT; 1016 break; 1017 case TIOCSPGRP: { /* set pgrp of tty */ 1018 register struct pgrp *pgrp = pgfind(*(int *)data); 1019 1020 if (!isctty(p, tp)) 1021 return (ENOTTY); 1022 else if (pgrp == NULL || pgrp->pg_session != p->p_session) 1023 return (EPERM); 1024 tp->t_pgrp = pgrp; 1025 break; 1026 } 1027 case TIOCSTAT: /* simulate control-T */ 1028 s = spltty(); 1029 ttyinfo(tp); 1030 splx(s); 1031 break; 1032 case TIOCSWINSZ: /* set window size */ 1033 if (bcmp((caddr_t)&tp->t_winsize, data, 1034 sizeof (struct winsize))) { 1035 tp->t_winsize = *(struct winsize *)data; 1036 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1037 } 1038 break; 1039 case TIOCSDRAINWAIT: 1040 error = suser(p); 1041 if (error) 1042 return (error); 1043 tp->t_timeout = *(int *)data * hz; 1044 wakeup(TSA_OCOMPLETE(tp)); 1045 wakeup(TSA_OLOWAT(tp)); 1046 break; 1047 case TIOCGDRAINWAIT: 1048 *(int *)data = tp->t_timeout / hz; 1049 break; 1050 default: 1051#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1052 return (ttcompat(tp, cmd, data, flag)); 1053#else 1054 return (ENOIOCTL); 1055#endif 1056 } 1057 return (0); 1058} 1059 1060int 1061ttypoll(dev, events, p) 1062 dev_t dev; 1063 int events; 1064 struct proc *p; 1065{ 1066 int s; 1067 int revents = 0; 1068 struct tty *tp; 1069 1070 tp = dev->si_tty; 1071 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */ 1072 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) 1073 | POLLHUP); 1074 1075 s = spltty(); 1076 if (events & (POLLIN | POLLRDNORM)) { 1077 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) 1078 revents |= events & (POLLIN | POLLRDNORM); 1079 else 1080 selrecord(p, &tp->t_rsel); 1081 } 1082 if (events & (POLLOUT | POLLWRNORM)) { 1083 if ((tp->t_outq.c_cc <= tp->t_olowat && 1084 ISSET(tp->t_state, TS_CONNECTED)) 1085 || ISSET(tp->t_state, TS_ZOMBIE)) 1086 revents |= events & (POLLOUT | POLLWRNORM); 1087 else 1088 selrecord(p, &tp->t_wsel); 1089 } 1090 splx(s); 1091 return (revents); 1092} 1093 1094/* 1095 * Must be called at spltty(). 1096 */ 1097static int 1098ttnread(tp) 1099 struct tty *tp; 1100{ 1101 int nread; 1102 1103 if (ISSET(tp->t_lflag, PENDIN)) 1104 ttypend(tp); 1105 nread = tp->t_canq.c_cc; 1106 if (!ISSET(tp->t_lflag, ICANON)) { 1107 nread += tp->t_rawq.c_cc; 1108 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) 1109 nread = 0; 1110 } 1111 return (nread); 1112} 1113 1114/* 1115 * Wait for output to drain. 1116 */ 1117int 1118ttywait(tp) 1119 register struct tty *tp; 1120{ 1121 int error, s; 1122 1123 error = 0; 1124 s = spltty(); 1125 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1126 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) { 1127 (*tp->t_oproc)(tp); 1128 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1129 ISSET(tp->t_state, TS_CONNECTED)) { 1130 SET(tp->t_state, TS_SO_OCOMPLETE); 1131 error = ttysleep(tp, TSA_OCOMPLETE(tp), 1132 TTOPRI | PCATCH, "ttywai", 1133 tp->t_timeout); 1134 if (error) { 1135 if (error == EWOULDBLOCK) 1136 error = EIO; 1137 break; 1138 } 1139 } else 1140 break; 1141 } 1142 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY))) 1143 error = EIO; 1144 splx(s); 1145 return (error); 1146} 1147 1148/* 1149 * Flush if successfully wait. 1150 */ 1151static int 1152ttywflush(tp) 1153 struct tty *tp; 1154{ 1155 int error; 1156 1157 if ((error = ttywait(tp)) == 0) 1158 ttyflush(tp, FREAD); 1159 return (error); 1160} 1161 1162/* 1163 * Flush tty read and/or write queues, notifying anyone waiting. 1164 */ 1165void 1166ttyflush(tp, rw) 1167 register struct tty *tp; 1168 int rw; 1169{ 1170 register int s; 1171 1172 s = spltty(); 1173#if 0 1174again: 1175#endif 1176 if (rw & FWRITE) { 1177 FLUSHQ(&tp->t_outq); 1178 CLR(tp->t_state, TS_TTSTOP); 1179 } 1180 (*tp->t_stop)(tp, rw); 1181 if (rw & FREAD) { 1182 FLUSHQ(&tp->t_canq); 1183 FLUSHQ(&tp->t_rawq); 1184 CLR(tp->t_lflag, PENDIN); 1185 tp->t_rocount = 0; 1186 tp->t_rocol = 0; 1187 CLR(tp->t_state, TS_LOCAL); 1188 ttwakeup(tp); 1189 if (ISSET(tp->t_state, TS_TBLOCK)) { 1190 if (rw & FWRITE) 1191 FLUSHQ(&tp->t_outq); 1192 ttyunblock(tp); 1193 1194 /* 1195 * Don't let leave any state that might clobber the 1196 * next line discipline (although we should do more 1197 * to send the START char). Not clearing the state 1198 * may have caused the "putc to a clist with no 1199 * reserved cblocks" panic/printf. 1200 */ 1201 CLR(tp->t_state, TS_TBLOCK); 1202 1203#if 0 /* forget it, sleeping isn't always safe and we don't know when it is */ 1204 if (ISSET(tp->t_iflag, IXOFF)) { 1205 /* 1206 * XXX wait a bit in the hope that the stop 1207 * character (if any) will go out. Waiting 1208 * isn't good since it allows races. This 1209 * will be fixed when the stop character is 1210 * put in a special queue. Don't bother with 1211 * the checks in ttywait() since the timeout 1212 * will save us. 1213 */ 1214 SET(tp->t_state, TS_SO_OCOMPLETE); 1215 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI, 1216 "ttyfls", hz / 10); 1217 /* 1218 * Don't try sending the stop character again. 1219 */ 1220 CLR(tp->t_state, TS_TBLOCK); 1221 goto again; 1222 } 1223#endif 1224 } 1225 } 1226 if (rw & FWRITE) { 1227 FLUSHQ(&tp->t_outq); 1228 ttwwakeup(tp); 1229 } 1230 splx(s); 1231} 1232 1233/* 1234 * Copy in the default termios characters. 1235 */ 1236void 1237termioschars(t) 1238 struct termios *t; 1239{ 1240 1241 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc); 1242} 1243 1244/* 1245 * Old interface. 1246 */ 1247void 1248ttychars(tp) 1249 struct tty *tp; 1250{ 1251 1252 termioschars(&tp->t_termios); 1253} 1254 1255/* 1256 * Handle input high water. Send stop character for the IXOFF case. Turn 1257 * on our input flow control bit and propagate the changes to the driver. 1258 * XXX the stop character should be put in a special high priority queue. 1259 */ 1260void 1261ttyblock(tp) 1262 struct tty *tp; 1263{ 1264 1265 SET(tp->t_state, TS_TBLOCK); 1266 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1267 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0) 1268 CLR(tp->t_state, TS_TBLOCK); /* try again later */ 1269 ttstart(tp); 1270} 1271 1272/* 1273 * Handle input low water. Send start character for the IXOFF case. Turn 1274 * off our input flow control bit and propagate the changes to the driver. 1275 * XXX the start character should be put in a special high priority queue. 1276 */ 1277static void 1278ttyunblock(tp) 1279 struct tty *tp; 1280{ 1281 1282 CLR(tp->t_state, TS_TBLOCK); 1283 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE && 1284 putc(tp->t_cc[VSTART], &tp->t_outq) != 0) 1285 SET(tp->t_state, TS_TBLOCK); /* try again later */ 1286 ttstart(tp); 1287} 1288 1289#ifdef notyet 1290/* Not used by any current (i386) drivers. */ 1291/* 1292 * Restart after an inter-char delay. 1293 */ 1294void 1295ttrstrt(tp_arg) 1296 void *tp_arg; 1297{ 1298 struct tty *tp; 1299 int s; 1300 1301 KASSERT(tp_arg != NULL, ("ttrstrt")); 1302 1303 tp = tp_arg; 1304 s = spltty(); 1305 1306 CLR(tp->t_state, TS_TIMEOUT); 1307 ttstart(tp); 1308 1309 splx(s); 1310} 1311#endif 1312 1313int 1314ttstart(tp) 1315 struct tty *tp; 1316{ 1317 1318 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1319 (*tp->t_oproc)(tp); 1320 return (0); 1321} 1322 1323/* 1324 * "close" a line discipline 1325 */ 1326int 1327ttylclose(tp, flag) 1328 struct tty *tp; 1329 int flag; 1330{ 1331 1332 if (flag & FNONBLOCK || ttywflush(tp)) 1333 ttyflush(tp, FREAD | FWRITE); 1334 return (0); 1335} 1336 1337/* 1338 * Handle modem control transition on a tty. 1339 * Flag indicates new state of carrier. 1340 * Returns 0 if the line should be turned off, otherwise 1. 1341 */ 1342int 1343ttymodem(tp, flag) 1344 register struct tty *tp; 1345 int flag; 1346{ 1347 1348 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) { 1349 /* 1350 * MDMBUF: do flow control according to carrier flag 1351 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP 1352 * works if IXON and IXANY are clear. 1353 */ 1354 if (flag) { 1355 CLR(tp->t_state, TS_CAR_OFLOW); 1356 CLR(tp->t_state, TS_TTSTOP); 1357 ttstart(tp); 1358 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) { 1359 SET(tp->t_state, TS_CAR_OFLOW); 1360 SET(tp->t_state, TS_TTSTOP); 1361 (*tp->t_stop)(tp, 0); 1362 } 1363 } else if (flag == 0) { 1364 /* 1365 * Lost carrier. 1366 */ 1367 CLR(tp->t_state, TS_CARR_ON); 1368 if (ISSET(tp->t_state, TS_ISOPEN) && 1369 !ISSET(tp->t_cflag, CLOCAL)) { 1370 SET(tp->t_state, TS_ZOMBIE); 1371 CLR(tp->t_state, TS_CONNECTED); 1372 if (tp->t_session && tp->t_session->s_leader) 1373 psignal(tp->t_session->s_leader, SIGHUP); 1374 ttyflush(tp, FREAD | FWRITE); 1375 return (0); 1376 } 1377 } else { 1378 /* 1379 * Carrier now on. 1380 */ 1381 SET(tp->t_state, TS_CARR_ON); 1382 if (!ISSET(tp->t_state, TS_ZOMBIE)) 1383 SET(tp->t_state, TS_CONNECTED); 1384 wakeup(TSA_CARR_ON(tp)); 1385 ttwakeup(tp); 1386 ttwwakeup(tp); 1387 } 1388 return (1); 1389} 1390 1391/* 1392 * Reinput pending characters after state switch 1393 * call at spltty(). 1394 */ 1395static void 1396ttypend(tp) 1397 register struct tty *tp; 1398{ 1399 struct clist tq; 1400 register int c; 1401 1402 CLR(tp->t_lflag, PENDIN); 1403 SET(tp->t_state, TS_TYPEN); 1404 /* 1405 * XXX this assumes too much about clist internals. It may even 1406 * fail if the cblock slush pool is empty. We can't allocate more 1407 * cblocks here because we are called from an interrupt handler 1408 * and clist_alloc_cblocks() can wait. 1409 */ 1410 tq = tp->t_rawq; 1411 bzero(&tp->t_rawq, sizeof tp->t_rawq); 1412 tp->t_rawq.c_cbmax = tq.c_cbmax; 1413 tp->t_rawq.c_cbreserved = tq.c_cbreserved; 1414 while ((c = getc(&tq)) >= 0) 1415 ttyinput(c, tp); 1416 CLR(tp->t_state, TS_TYPEN); 1417} 1418 1419/* 1420 * Process a read call on a tty device. 1421 */ 1422int 1423ttread(tp, uio, flag) 1424 register struct tty *tp; 1425 struct uio *uio; 1426 int flag; 1427{ 1428 register struct clist *qp; 1429 register int c; 1430 register tcflag_t lflag; 1431 register cc_t *cc = tp->t_cc; 1432 register struct proc *p = curproc; 1433 int s, first, error = 0; 1434 int has_stime = 0, last_cc = 0; 1435 long slp = 0; /* XXX this should be renamed `timo'. */ 1436 struct timeval stime; 1437 1438loop: 1439 s = spltty(); 1440 lflag = tp->t_lflag; 1441 /* 1442 * take pending input first 1443 */ 1444 if (ISSET(lflag, PENDIN)) { 1445 ttypend(tp); 1446 splx(s); /* reduce latency */ 1447 s = spltty(); 1448 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */ 1449 } 1450 1451 /* 1452 * Hang process if it's in the background. 1453 */ 1454 if (isbackground(p, tp)) { 1455 splx(s); 1456 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 1457 SIGISMEMBER(p->p_sigmask, SIGTTIN) || 1458 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0) 1459 return (EIO); 1460 pgsignal(p->p_pgrp, SIGTTIN, 1); 1461 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0); 1462 if (error) 1463 return (error); 1464 goto loop; 1465 } 1466 1467 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1468 splx(s); 1469 return (0); /* EOF */ 1470 } 1471 1472 /* 1473 * If canonical, use the canonical queue, 1474 * else use the raw queue. 1475 * 1476 * (should get rid of clists...) 1477 */ 1478 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 1479 1480 if (flag & IO_NDELAY) { 1481 if (qp->c_cc > 0) 1482 goto read; 1483 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) { 1484 splx(s); 1485 return (0); 1486 } 1487 splx(s); 1488 return (EWOULDBLOCK); 1489 } 1490 if (!ISSET(lflag, ICANON)) { 1491 int m = cc[VMIN]; 1492 long t = cc[VTIME]; 1493 struct timeval timecopy; 1494 1495 /* 1496 * Check each of the four combinations. 1497 * (m > 0 && t == 0) is the normal read case. 1498 * It should be fairly efficient, so we check that and its 1499 * companion case (m == 0 && t == 0) first. 1500 * For the other two cases, we compute the target sleep time 1501 * into slp. 1502 */ 1503 if (t == 0) { 1504 if (qp->c_cc < m) 1505 goto sleep; 1506 if (qp->c_cc > 0) 1507 goto read; 1508 1509 /* m, t and qp->c_cc are all 0. 0 is enough input. */ 1510 splx(s); 1511 return (0); 1512 } 1513 t *= 100000; /* time in us */ 1514#define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1515 ((t1).tv_usec - (t2).tv_usec)) 1516 if (m > 0) { 1517 if (qp->c_cc <= 0) 1518 goto sleep; 1519 if (qp->c_cc >= m) 1520 goto read; 1521 getmicrotime(&timecopy); 1522 if (!has_stime) { 1523 /* first character, start timer */ 1524 has_stime = 1; 1525 stime = timecopy; 1526 slp = t; 1527 } else if (qp->c_cc > last_cc) { 1528 /* got a character, restart timer */ 1529 stime = timecopy; 1530 slp = t; 1531 } else { 1532 /* nothing, check expiration */ 1533 slp = t - diff(timecopy, stime); 1534 if (slp <= 0) 1535 goto read; 1536 } 1537 last_cc = qp->c_cc; 1538 } else { /* m == 0 */ 1539 if (qp->c_cc > 0) 1540 goto read; 1541 getmicrotime(&timecopy); 1542 if (!has_stime) { 1543 has_stime = 1; 1544 stime = timecopy; 1545 slp = t; 1546 } else { 1547 slp = t - diff(timecopy, stime); 1548 if (slp <= 0) { 1549 /* Timed out, but 0 is enough input. */ 1550 splx(s); 1551 return (0); 1552 } 1553 } 1554 } 1555#undef diff 1556 /* 1557 * Rounding down may make us wake up just short 1558 * of the target, so we round up. 1559 * The formula is ceiling(slp * hz/1000000). 1560 * 32-bit arithmetic is enough for hz < 169. 1561 * XXX see tvtohz() for how to avoid overflow if hz 1562 * is large (divide by `tick' and/or arrange to 1563 * use tvtohz() if hz is large). 1564 */ 1565 slp = (long) (((u_long)slp * hz) + 999999) / 1000000; 1566 goto sleep; 1567 } 1568 if (qp->c_cc <= 0) { 1569sleep: 1570 /* 1571 * There is no input, or not enough input and we can block. 1572 */ 1573 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH, 1574 ISSET(tp->t_state, TS_CONNECTED) ? 1575 "ttyin" : "ttyhup", (int)slp); 1576 splx(s); 1577 if (error == EWOULDBLOCK) 1578 error = 0; 1579 else if (error) 1580 return (error); 1581 /* 1582 * XXX what happens if another process eats some input 1583 * while we are asleep (not just here)? It would be 1584 * safest to detect changes and reset our state variables 1585 * (has_stime and last_cc). 1586 */ 1587 slp = 0; 1588 goto loop; 1589 } 1590read: 1591 splx(s); 1592 /* 1593 * Input present, check for input mapping and processing. 1594 */ 1595 first = 1; 1596 if (ISSET(lflag, ICANON | ISIG)) 1597 goto slowcase; 1598 for (;;) { 1599 char ibuf[IBUFSIZ]; 1600 int icc; 1601 1602 icc = imin(uio->uio_resid, IBUFSIZ); 1603 icc = q_to_b(qp, ibuf, icc); 1604 if (icc <= 0) { 1605 if (first) 1606 goto loop; 1607 break; 1608 } 1609 error = uiomove(ibuf, icc, uio); 1610 /* 1611 * XXX if there was an error then we should ungetc() the 1612 * unmoved chars and reduce icc here. 1613 */ 1614#if NSNP > 0 1615 if (ISSET(tp->t_lflag, ECHO) && 1616 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1617 snpin((struct snoop *)tp->t_sc, ibuf, icc); 1618#endif 1619 if (error) 1620 break; 1621 if (uio->uio_resid == 0) 1622 break; 1623 first = 0; 1624 } 1625 goto out; 1626slowcase: 1627 for (;;) { 1628 c = getc(qp); 1629 if (c < 0) { 1630 if (first) 1631 goto loop; 1632 break; 1633 } 1634 /* 1635 * delayed suspend (^Y) 1636 */ 1637 if (CCEQ(cc[VDSUSP], c) && 1638 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) { 1639 pgsignal(tp->t_pgrp, SIGTSTP, 1); 1640 if (first) { 1641 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, 1642 "ttybg3", 0); 1643 if (error) 1644 break; 1645 goto loop; 1646 } 1647 break; 1648 } 1649 /* 1650 * Interpret EOF only in canonical mode. 1651 */ 1652 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 1653 break; 1654 /* 1655 * Give user character. 1656 */ 1657 error = ureadc(c, uio); 1658 if (error) 1659 /* XXX should ungetc(c, qp). */ 1660 break; 1661#if NSNP > 0 1662 /* 1663 * Only snoop directly on input in echo mode. Non-echoed 1664 * input will be snooped later iff the application echoes it. 1665 */ 1666 if (ISSET(tp->t_lflag, ECHO) && 1667 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1668 snpinc((struct snoop *)tp->t_sc, (char)c); 1669#endif 1670 if (uio->uio_resid == 0) 1671 break; 1672 /* 1673 * In canonical mode check for a "break character" 1674 * marking the end of a "line of input". 1675 */ 1676 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag)) 1677 break; 1678 first = 0; 1679 } 1680 1681out: 1682 /* 1683 * Look to unblock input now that (presumably) 1684 * the input queue has gone down. 1685 */ 1686 s = spltty(); 1687 if (ISSET(tp->t_state, TS_TBLOCK) && 1688 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat) 1689 ttyunblock(tp); 1690 splx(s); 1691 1692 return (error); 1693} 1694 1695/* 1696 * Check the output queue on tp for space for a kernel message (from uprintf 1697 * or tprintf). Allow some space over the normal hiwater mark so we don't 1698 * lose messages due to normal flow control, but don't let the tty run amok. 1699 * Sleeps here are not interruptible, but we return prematurely if new signals 1700 * arrive. 1701 */ 1702int 1703ttycheckoutq(tp, wait) 1704 register struct tty *tp; 1705 int wait; 1706{ 1707 int hiwat, s; 1708 sigset_t oldmask; 1709 1710 hiwat = tp->t_ohiwat; 1711 SIGEMPTYSET(oldmask); 1712 s = spltty(); 1713 if (wait) 1714 oldmask = curproc->p_siglist; 1715 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100) 1716 while (tp->t_outq.c_cc > hiwat) { 1717 ttstart(tp); 1718 if (tp->t_outq.c_cc <= hiwat) 1719 break; 1720 if (!(wait && SIGSETEQ(curproc->p_siglist, oldmask))) { 1721 splx(s); 1722 return (0); 1723 } 1724 SET(tp->t_state, TS_SO_OLOWAT); 1725 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz); 1726 } 1727 splx(s); 1728 return (1); 1729} 1730 1731/* 1732 * Process a write call on a tty device. 1733 */ 1734int 1735ttwrite(tp, uio, flag) 1736 register struct tty *tp; 1737 register struct uio *uio; 1738 int flag; 1739{ 1740 register char *cp = NULL; 1741 register int cc, ce; 1742 register struct proc *p; 1743 int i, hiwat, cnt, error, s; 1744 char obuf[OBUFSIZ]; 1745 1746 hiwat = tp->t_ohiwat; 1747 cnt = uio->uio_resid; 1748 error = 0; 1749 cc = 0; 1750loop: 1751 s = spltty(); 1752 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1753 splx(s); 1754 if (uio->uio_resid == cnt) 1755 error = EIO; 1756 goto out; 1757 } 1758 if (!ISSET(tp->t_state, TS_CONNECTED)) { 1759 if (flag & IO_NDELAY) { 1760 splx(s); 1761 error = EWOULDBLOCK; 1762 goto out; 1763 } 1764 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1765 "ttydcd", 0); 1766 splx(s); 1767 if (error) 1768 goto out; 1769 goto loop; 1770 } 1771 splx(s); 1772 /* 1773 * Hang the process if it's in the background. 1774 */ 1775 p = curproc; 1776 if (isbackground(p, tp) && 1777 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) && 1778 !SIGISMEMBER(p->p_sigignore, SIGTTOU) && 1779 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) { 1780 if (p->p_pgrp->pg_jobc == 0) { 1781 error = EIO; 1782 goto out; 1783 } 1784 pgsignal(p->p_pgrp, SIGTTOU, 1); 1785 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0); 1786 if (error) 1787 goto out; 1788 goto loop; 1789 } 1790 /* 1791 * Process the user's data in at most OBUFSIZ chunks. Perform any 1792 * output translation. Keep track of high water mark, sleep on 1793 * overflow awaiting device aid in acquiring new space. 1794 */ 1795 while (uio->uio_resid > 0 || cc > 0) { 1796 if (ISSET(tp->t_lflag, FLUSHO)) { 1797 uio->uio_resid = 0; 1798 return (0); 1799 } 1800 if (tp->t_outq.c_cc > hiwat) 1801 goto ovhiwat; 1802 /* 1803 * Grab a hunk of data from the user, unless we have some 1804 * leftover from last time. 1805 */ 1806 if (cc == 0) { 1807 cc = imin(uio->uio_resid, OBUFSIZ); 1808 cp = obuf; 1809 error = uiomove(cp, cc, uio); 1810 if (error) { 1811 cc = 0; 1812 break; 1813 } 1814#if NSNP > 0 1815 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1816 snpin((struct snoop *)tp->t_sc, cp, cc); 1817#endif 1818 } 1819 /* 1820 * If nothing fancy need be done, grab those characters we 1821 * can handle without any of ttyoutput's processing and 1822 * just transfer them to the output q. For those chars 1823 * which require special processing (as indicated by the 1824 * bits in char_type), call ttyoutput. After processing 1825 * a hunk of data, look for FLUSHO so ^O's will take effect 1826 * immediately. 1827 */ 1828 while (cc > 0) { 1829 if (!ISSET(tp->t_oflag, OPOST)) 1830 ce = cc; 1831 else { 1832 ce = cc - scanc((u_int)cc, (u_char *)cp, 1833 char_type, CCLASSMASK); 1834 /* 1835 * If ce is zero, then we're processing 1836 * a special character through ttyoutput. 1837 */ 1838 if (ce == 0) { 1839 tp->t_rocount = 0; 1840 if (ttyoutput(*cp, tp) >= 0) { 1841 /* No Clists, wait a bit. */ 1842 ttstart(tp); 1843 if (flag & IO_NDELAY) { 1844 error = EWOULDBLOCK; 1845 goto out; 1846 } 1847 error = ttysleep(tp, &lbolt, 1848 TTOPRI|PCATCH, 1849 "ttybf1", 0); 1850 if (error) 1851 goto out; 1852 goto loop; 1853 } 1854 cp++; 1855 cc--; 1856 if (ISSET(tp->t_lflag, FLUSHO) || 1857 tp->t_outq.c_cc > hiwat) 1858 goto ovhiwat; 1859 continue; 1860 } 1861 } 1862 /* 1863 * A bunch of normal characters have been found. 1864 * Transfer them en masse to the output queue and 1865 * continue processing at the top of the loop. 1866 * If there are any further characters in this 1867 * <= OBUFSIZ chunk, the first should be a character 1868 * requiring special handling by ttyoutput. 1869 */ 1870 tp->t_rocount = 0; 1871 i = b_to_q(cp, ce, &tp->t_outq); 1872 ce -= i; 1873 tp->t_column += ce; 1874 cp += ce, cc -= ce, tk_nout += ce; 1875 tp->t_outcc += ce; 1876 if (i > 0) { 1877 /* No Clists, wait a bit. */ 1878 ttstart(tp); 1879 if (flag & IO_NDELAY) { 1880 error = EWOULDBLOCK; 1881 goto out; 1882 } 1883 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, 1884 "ttybf2", 0); 1885 if (error) 1886 goto out; 1887 goto loop; 1888 } 1889 if (ISSET(tp->t_lflag, FLUSHO) || 1890 tp->t_outq.c_cc > hiwat) 1891 break; 1892 } 1893 ttstart(tp); 1894 } 1895out: 1896 /* 1897 * If cc is nonzero, we leave the uio structure inconsistent, as the 1898 * offset and iov pointers have moved forward, but it doesn't matter 1899 * (the call will either return short or restart with a new uio). 1900 */ 1901 uio->uio_resid += cc; 1902 return (error); 1903 1904ovhiwat: 1905 ttstart(tp); 1906 s = spltty(); 1907 /* 1908 * This can only occur if FLUSHO is set in t_lflag, 1909 * or if ttstart/oproc is synchronous (or very fast). 1910 */ 1911 if (tp->t_outq.c_cc <= hiwat) { 1912 splx(s); 1913 goto loop; 1914 } 1915 if (flag & IO_NDELAY) { 1916 splx(s); 1917 uio->uio_resid += cc; 1918 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 1919 } 1920 SET(tp->t_state, TS_SO_OLOWAT); 1921 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri", 1922 tp->t_timeout); 1923 splx(s); 1924 if (error == EWOULDBLOCK) 1925 error = EIO; 1926 if (error) 1927 goto out; 1928 goto loop; 1929} 1930 1931/* 1932 * Rubout one character from the rawq of tp 1933 * as cleanly as possible. 1934 */ 1935static void 1936ttyrub(c, tp) 1937 register int c; 1938 register struct tty *tp; 1939{ 1940 register char *cp; 1941 register int savecol; 1942 int tabc, s; 1943 1944 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 1945 return; 1946 CLR(tp->t_lflag, FLUSHO); 1947 if (ISSET(tp->t_lflag, ECHOE)) { 1948 if (tp->t_rocount == 0) { 1949 /* 1950 * Screwed by ttwrite; retype 1951 */ 1952 ttyretype(tp); 1953 return; 1954 } 1955 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 1956 ttyrubo(tp, 2); 1957 else { 1958 CLR(c, ~TTY_CHARMASK); 1959 switch (CCLASS(c)) { 1960 case ORDINARY: 1961 ttyrubo(tp, 1); 1962 break; 1963 case BACKSPACE: 1964 case CONTROL: 1965 case NEWLINE: 1966 case RETURN: 1967 case VTAB: 1968 if (ISSET(tp->t_lflag, ECHOCTL)) 1969 ttyrubo(tp, 2); 1970 break; 1971 case TAB: 1972 if (tp->t_rocount < tp->t_rawq.c_cc) { 1973 ttyretype(tp); 1974 return; 1975 } 1976 s = spltty(); 1977 savecol = tp->t_column; 1978 SET(tp->t_state, TS_CNTTB); 1979 SET(tp->t_lflag, FLUSHO); 1980 tp->t_column = tp->t_rocol; 1981 cp = tp->t_rawq.c_cf; 1982 if (cp) 1983 tabc = *cp; /* XXX FIX NEXTC */ 1984 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 1985 ttyecho(tabc, tp); 1986 CLR(tp->t_lflag, FLUSHO); 1987 CLR(tp->t_state, TS_CNTTB); 1988 splx(s); 1989 1990 /* savecol will now be length of the tab. */ 1991 savecol -= tp->t_column; 1992 tp->t_column += savecol; 1993 if (savecol > 8) 1994 savecol = 8; /* overflow screw */ 1995 while (--savecol >= 0) 1996 (void)ttyoutput('\b', tp); 1997 break; 1998 default: /* XXX */ 1999#define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 2000 (void)printf(PANICSTR, c, CCLASS(c)); 2001#ifdef notdef 2002 panic(PANICSTR, c, CCLASS(c)); 2003#endif 2004 } 2005 } 2006 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 2007 if (!ISSET(tp->t_state, TS_ERASE)) { 2008 SET(tp->t_state, TS_ERASE); 2009 (void)ttyoutput('\\', tp); 2010 } 2011 ttyecho(c, tp); 2012 } else 2013 ttyecho(tp->t_cc[VERASE], tp); 2014 --tp->t_rocount; 2015} 2016 2017/* 2018 * Back over cnt characters, erasing them. 2019 */ 2020static void 2021ttyrubo(tp, cnt) 2022 register struct tty *tp; 2023 int cnt; 2024{ 2025 2026 while (cnt-- > 0) { 2027 (void)ttyoutput('\b', tp); 2028 (void)ttyoutput(' ', tp); 2029 (void)ttyoutput('\b', tp); 2030 } 2031} 2032 2033/* 2034 * ttyretype -- 2035 * Reprint the rawq line. Note, it is assumed that c_cc has already 2036 * been checked. 2037 */ 2038static void 2039ttyretype(tp) 2040 register struct tty *tp; 2041{ 2042 register char *cp; 2043 int s, c; 2044 2045 /* Echo the reprint character. */ 2046 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 2047 ttyecho(tp->t_cc[VREPRINT], tp); 2048 2049 (void)ttyoutput('\n', tp); 2050 2051 /* 2052 * XXX 2053 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 2054 * BIT OF FIRST CHAR. 2055 */ 2056 s = spltty(); 2057 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 2058 cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 2059 ttyecho(c, tp); 2060 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 2061 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 2062 ttyecho(c, tp); 2063 CLR(tp->t_state, TS_ERASE); 2064 splx(s); 2065 2066 tp->t_rocount = tp->t_rawq.c_cc; 2067 tp->t_rocol = 0; 2068} 2069 2070/* 2071 * Echo a typed character to the terminal. 2072 */ 2073static void 2074ttyecho(c, tp) 2075 register int c; 2076 register struct tty *tp; 2077{ 2078 2079 if (!ISSET(tp->t_state, TS_CNTTB)) 2080 CLR(tp->t_lflag, FLUSHO); 2081 if ((!ISSET(tp->t_lflag, ECHO) && 2082 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) || 2083 ISSET(tp->t_lflag, EXTPROC)) 2084 return; 2085 if (ISSET(tp->t_lflag, ECHOCTL) && 2086 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') || 2087 ISSET(c, TTY_CHARMASK) == 0177)) { 2088 (void)ttyoutput('^', tp); 2089 CLR(c, ~TTY_CHARMASK); 2090 if (c == 0177) 2091 c = '?'; 2092 else 2093 c += 'A' - 1; 2094 } 2095 (void)ttyoutput(c, tp); 2096} 2097 2098/* 2099 * Wake up any readers on a tty. 2100 */ 2101void 2102ttwakeup(tp) 2103 register struct tty *tp; 2104{ 2105 2106 if (tp->t_rsel.si_pid != 0) 2107 selwakeup(&tp->t_rsel); 2108 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2109 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2110 wakeup(TSA_HUP_OR_INPUT(tp)); 2111} 2112 2113/* 2114 * Wake up any writers on a tty. 2115 */ 2116void 2117ttwwakeup(tp) 2118 register struct tty *tp; 2119{ 2120 2121 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat) 2122 selwakeup(&tp->t_wsel); 2123 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2124 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2125 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == 2126 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { 2127 CLR(tp->t_state, TS_SO_OCOMPLETE); 2128 wakeup(TSA_OCOMPLETE(tp)); 2129 } 2130 if (ISSET(tp->t_state, TS_SO_OLOWAT) && 2131 tp->t_outq.c_cc <= tp->t_olowat) { 2132 CLR(tp->t_state, TS_SO_OLOWAT); 2133 wakeup(TSA_OLOWAT(tp)); 2134 } 2135} 2136 2137/* 2138 * Look up a code for a specified speed in a conversion table; 2139 * used by drivers to map software speed values to hardware parameters. 2140 */ 2141int 2142ttspeedtab(speed, table) 2143 int speed; 2144 register struct speedtab *table; 2145{ 2146 2147 for ( ; table->sp_speed != -1; table++) 2148 if (table->sp_speed == speed) 2149 return (table->sp_code); 2150 return (-1); 2151} 2152 2153/* 2154 * Set input and output watermarks and buffer sizes. For input, the 2155 * high watermark is about one second's worth of input above empty, the 2156 * low watermark is slightly below high water, and the buffer size is a 2157 * driver-dependent amount above high water. For output, the watermarks 2158 * are near the ends of the buffer, with about 1 second's worth of input 2159 * between them. All this only applies to the standard line discipline. 2160 */ 2161void 2162ttsetwater(tp) 2163 struct tty *tp; 2164{ 2165 register int cps, ttmaxhiwat, x; 2166 2167 /* Input. */ 2168 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512); 2169 switch (tp->t_ispeedwat) { 2170 case (speed_t)-1: 2171 cps = tp->t_ispeed / 10; 2172 break; 2173 case 0: 2174 /* 2175 * This case is for old drivers that don't know about 2176 * t_ispeedwat. Arrange for them to get the old buffer 2177 * sizes and watermarks. 2178 */ 2179 cps = TTYHOG - 2 * 256; 2180 tp->t_ififosize = 2 * 256; 2181 break; 2182 default: 2183 cps = tp->t_ispeedwat / 10; 2184 break; 2185 } 2186 tp->t_ihiwat = cps; 2187 tp->t_ilowat = 7 * cps / 8; 2188 x = cps + tp->t_ififosize; 2189 clist_alloc_cblocks(&tp->t_rawq, x, x); 2190 2191 /* Output. */ 2192 switch (tp->t_ospeedwat) { 2193 case (speed_t)-1: 2194 cps = tp->t_ospeed / 10; 2195 ttmaxhiwat = 2 * TTMAXHIWAT; 2196 break; 2197 case 0: 2198 cps = tp->t_ospeed / 10; 2199 ttmaxhiwat = TTMAXHIWAT; 2200 break; 2201 default: 2202 cps = tp->t_ospeedwat / 10; 2203 ttmaxhiwat = 8 * TTMAXHIWAT; 2204 break; 2205 } 2206#define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 2207 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 2208 x += cps; 2209 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */ 2210 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */ 2211 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */ 2212 x += OBUFSIZ + 100; 2213 clist_alloc_cblocks(&tp->t_outq, x, x); 2214#undef CLAMP 2215} 2216 2217/* 2218 * Report on state of foreground process group. 2219 */ 2220void 2221ttyinfo(tp) 2222 register struct tty *tp; 2223{ 2224 register struct proc *p, *pick; 2225 struct timeval utime, stime; 2226 int tmp; 2227 2228 if (ttycheckoutq(tp,0) == 0) 2229 return; 2230 2231 /* Print load average. */ 2232 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 2233 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 2234 2235 if (tp->t_session == NULL) 2236 ttyprintf(tp, "not a controlling terminal\n"); 2237 else if (tp->t_pgrp == NULL) 2238 ttyprintf(tp, "no foreground process group\n"); 2239 else if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0) 2240 ttyprintf(tp, "empty foreground process group\n"); 2241 else { 2242 /* Pick interesting process. */ 2243 for (pick = NULL; p != 0; p = LIST_NEXT(p, p_pglist)) 2244 if (proc_compare(pick, p)) 2245 pick = p; 2246 2247 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 2248 pick->p_stat == SRUN ? "running" : 2249 pick->p_wmesg ? pick->p_wmesg : "iowait"); 2250 2251 if (pick->p_flag & P_INMEM) { 2252 calcru(pick, &utime, &stime, NULL); 2253 2254 /* Print user time. */ 2255 ttyprintf(tp, "%ld.%02ldu ", 2256 utime.tv_sec, utime.tv_usec / 10000); 2257 2258 /* Print system time. */ 2259 ttyprintf(tp, "%ld.%02lds ", 2260 stime.tv_sec, stime.tv_usec / 10000); 2261 } else 2262 ttyprintf(tp, "?.??u ?.??s "); 2263 2264 /* Print percentage cpu, resident set size. */ 2265 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT; 2266 ttyprintf(tp, "%d%% %ldk\n", 2267 tmp / 100, 2268 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 : 2269 (long)pgtok(vmspace_resident_count(pick->p_vmspace))); 2270 } 2271 tp->t_rocount = 0; /* so pending input will be retyped if BS */ 2272} 2273 2274/* 2275 * Returns 1 if p2 is "better" than p1 2276 * 2277 * The algorithm for picking the "interesting" process is thus: 2278 * 2279 * 1) Only foreground processes are eligible - implied. 2280 * 2) Runnable processes are favored over anything else. The runner 2281 * with the highest cpu utilization is picked (p_estcpu). Ties are 2282 * broken by picking the highest pid. 2283 * 3) The sleeper with the shortest sleep time is next. With ties, 2284 * we pick out just "short-term" sleepers (P_SINTR == 0). 2285 * 4) Further ties are broken by picking the highest pid. 2286 */ 2287#define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 2288#define TESTAB(a, b) ((a)<<1 | (b)) 2289#define ONLYA 2 2290#define ONLYB 1 2291#define BOTH 3 2292 2293static int 2294proc_compare(p1, p2) 2295 register struct proc *p1, *p2; 2296{ 2297 2298 if (p1 == NULL) 2299 return (1); 2300 /* 2301 * see if at least one of them is runnable 2302 */ 2303 switch (TESTAB(ISRUN(p1), ISRUN(p2))) { 2304 case ONLYA: 2305 return (0); 2306 case ONLYB: 2307 return (1); 2308 case BOTH: 2309 /* 2310 * tie - favor one with highest recent cpu utilization 2311 */ 2312 if (p2->p_estcpu > p1->p_estcpu) 2313 return (1); 2314 if (p1->p_estcpu > p2->p_estcpu) 2315 return (0); 2316 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2317 } 2318 /* 2319 * weed out zombies 2320 */ 2321 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { 2322 case ONLYA: 2323 return (1); 2324 case ONLYB: 2325 return (0); 2326 case BOTH: 2327 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2328 } 2329 /* 2330 * pick the one with the smallest sleep time 2331 */ 2332 if (p2->p_slptime > p1->p_slptime) 2333 return (0); 2334 if (p1->p_slptime > p2->p_slptime) 2335 return (1); 2336 /* 2337 * favor one sleeping in a non-interruptible sleep 2338 */ 2339 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0) 2340 return (1); 2341 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0) 2342 return (0); 2343 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2344} 2345 2346/* 2347 * Output char to tty; console putchar style. 2348 */ 2349int 2350tputchar(c, tp) 2351 int c; 2352 struct tty *tp; 2353{ 2354 register int s; 2355 2356 s = spltty(); 2357 if (!ISSET(tp->t_state, TS_CONNECTED)) { 2358 splx(s); 2359 return (-1); 2360 } 2361 if (c == '\n') 2362 (void)ttyoutput('\r', tp); 2363 (void)ttyoutput(c, tp); 2364 ttstart(tp); 2365 splx(s); 2366 return (0); 2367} 2368 2369/* 2370 * Sleep on chan, returning ERESTART if tty changed while we napped and 2371 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If 2372 * the tty is revoked, restarting a pending call will redo validation done 2373 * at the start of the call. 2374 */ 2375int 2376ttysleep(tp, chan, pri, wmesg, timo) 2377 struct tty *tp; 2378 void *chan; 2379 int pri, timo; 2380 char *wmesg; 2381{ 2382 int error; 2383 int gen; 2384 2385 gen = tp->t_gen; 2386 error = tsleep(chan, pri, wmesg, timo); 2387 if (error) 2388 return (error); 2389 return (tp->t_gen == gen ? 0 : ERESTART); 2390} 2391 2392/* 2393 * Allocate a tty struct. Clists in the struct will be allocated by 2394 * ttyopen(). 2395 */ 2396struct tty * 2397ttymalloc(tp) 2398 struct tty *tp; 2399{ 2400 2401 if (tp) 2402 return(tp); 2403 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK); 2404 bzero(tp, sizeof *tp); 2405 ttyregister(tp); 2406 return (tp); 2407} 2408 2409#if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */ 2410/* 2411 * Free a tty struct. Clists in the struct should have been freed by 2412 * ttyclose(). 2413 */ 2414void 2415ttyfree(tp) 2416 struct tty *tp; 2417{ 2418 free(tp, M_TTYS); 2419} 2420#endif /* 0 */ 2421 2422void 2423ttyregister(tp) 2424 struct tty *tp; 2425{ 2426 SLIST_INSERT_HEAD(&tty_list, tp, t_list); 2427} 2428 2429static int 2430sysctl_kern_ttys SYSCTL_HANDLER_ARGS 2431{ 2432 int error; 2433 struct tty *tp, t; 2434 SLIST_FOREACH(tp, &tty_list, t_list) { 2435 t = *tp; 2436 if (t.t_dev) 2437 t.t_dev = (dev_t)dev2udev(t.t_dev); 2438 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t)); 2439 if (error) 2440 return (error); 2441 } 2442 return (0); 2443} 2444 2445SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD, 2446 0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys"); 2447 2448void 2449nottystop(tp, rw) 2450 struct tty *tp; 2451 int rw; 2452{ 2453 2454 return; 2455} 2456 2457int 2458ttyread(dev, uio, flag) 2459 dev_t dev; 2460 struct uio *uio; 2461 int flag; 2462{ 2463 struct tty *tp; 2464 2465 tp = dev->si_tty; 2466 if (tp == NULL) 2467 return (ENODEV); 2468 return ((*linesw[tp->t_line].l_read)(tp, uio, flag)); 2469} 2470 2471int 2472ttywrite(dev, uio, flag) 2473 dev_t dev; 2474 struct uio *uio; 2475 int flag; 2476{ 2477 struct tty *tp; 2478 2479 tp = dev->si_tty; 2480 if (tp == NULL) 2481 return (ENODEV); 2482 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2483} 2484