tty.c revision 20021
1/*- 2 * Copyright (c) 1982, 1986, 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)tty.c 8.8 (Berkeley) 1/21/94 39 * $Id: tty.c,v 1.85 1996/11/29 15:06:09 bde Exp $ 40 */ 41 42/*- 43 * TODO: 44 * o Fix races for sending the start char in ttyflush(). 45 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect(). 46 * With luck, there will be MIN chars before select() returns(). 47 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it. 48 * o Don't allow input in TS_ZOMBIE case. It would be visible through 49 * FIONREAD. 50 * o Do the new sio locking stuff here and use it to avoid special 51 * case for EXTPROC? 52 * o Lock PENDIN too? 53 * o Move EXTPROC and/or PENDIN to t_state? 54 * o Wrap most of ttioctl in spltty/splx. 55 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>. 56 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set. 57 * o Don't allow certain termios flags to affect disciplines other 58 * than TTYDISC. Cancel their effects before switch disciplines 59 * and ignore them if they are set while we are in another 60 * discipline. 61 * o Handle c_ispeed = 0 to c_ispeed = c_ospeed conversion here instead 62 * of in drivers and fix drivers that write to tp->t_termios. 63 * o Check for TS_CARR_ON being set while everything is closed and not 64 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open, 65 * so it would live until the next open even if carrier drops. 66 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared 67 * only when _all_ openers leave open(). 68 */ 69 70#include "snp.h" 71#include "opt_uconsole.h" 72 73#include <sys/param.h> 74#include <sys/systm.h> 75#include <sys/ioctl.h> 76#include <sys/proc.h> 77#define TTYDEFCHARS 78#include <sys/tty.h> 79#undef TTYDEFCHARS 80#include <sys/file.h> 81#include <sys/conf.h> 82#include <sys/dkstat.h> 83#include <sys/uio.h> 84#include <sys/kernel.h> 85#include <sys/vnode.h> 86#include <sys/syslog.h> 87#include <sys/signalvar.h> 88#include <sys/resourcevar.h> 89#include <sys/malloc.h> 90#if NSNP > 0 91#include <sys/snoop.h> 92#endif 93 94#include <vm/vm.h> 95#include <vm/vm_param.h> 96#include <vm/vm_prot.h> 97#include <vm/lock.h> 98#include <vm/pmap.h> 99#include <vm/vm_map.h> 100 101static int proc_compare __P((struct proc *p1, struct proc *p2)); 102static int ttnread __P((struct tty *tp)); 103static void ttyecho __P((int c, struct tty *tp)); 104static int ttyoutput __P((int c, register struct tty *tp)); 105static void ttypend __P((struct tty *tp)); 106static void ttyretype __P((struct tty *tp)); 107static void ttyrub __P((int c, struct tty *tp)); 108static void ttyrubo __P((struct tty *tp, int cnt)); 109static void ttyunblock __P((struct tty *tp)); 110static int ttywflush __P((struct tty *tp)); 111 112/* 113 * Table with character classes and parity. The 8th bit indicates parity, 114 * the 7th bit indicates the character is an alphameric or underscore (for 115 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 116 * are 0 then the character needs no special processing on output; classes 117 * other than 0 might be translated or (not currently) require delays. 118 */ 119#define E 0x00 /* Even parity. */ 120#define O 0x80 /* Odd parity. */ 121#define PARITY(c) (char_type[c] & O) 122 123#define ALPHA 0x40 /* Alpha or underscore. */ 124#define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 125 126#define CCLASSMASK 0x3f 127#define CCLASS(c) (char_type[c] & CCLASSMASK) 128 129#define BS BACKSPACE 130#define CC CONTROL 131#define CR RETURN 132#define NA ORDINARY | ALPHA 133#define NL NEWLINE 134#define NO ORDINARY 135#define TB TAB 136#define VT VTAB 137 138static u_char const char_type[] = { 139 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 140 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 141 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 142 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 143 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 144 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 145 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 146 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 147 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 148 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 149 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 150 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 151 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 152 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 153 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 154 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 155 /* 156 * Meta chars; should be settable per character set; 157 * for now, treat them all as normal characters. 158 */ 159 NA, NA, NA, NA, NA, NA, NA, NA, 160 NA, NA, NA, NA, NA, NA, NA, NA, 161 NA, NA, NA, NA, NA, NA, NA, NA, 162 NA, NA, NA, NA, NA, NA, NA, NA, 163 NA, NA, NA, NA, NA, NA, NA, NA, 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}; 176#undef BS 177#undef CC 178#undef CR 179#undef NA 180#undef NL 181#undef NO 182#undef TB 183#undef VT 184 185/* Macros to clear/set/test flags. */ 186#define SET(t, f) (t) |= (f) 187#define CLR(t, f) (t) &= ~(f) 188#define ISSET(t, f) ((t) & (f)) 189 190/* 191 * Input control starts when we would not be able to fit the maximum 192 * contents of the ping-pong buffers and finishes when we would be able 193 * to fit that much plus 1/8 more. 194 */ 195#define I_HIGH_WATER (TTYHOG - 2 * 256) /* XXX */ 196#define I_LOW_WATER ((TTYHOG - 2 * 256) * 7 / 8) /* XXX */ 197 198#undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */ 199#define MAX_INPUT TTYHOG 200 201/* 202 * Initial open of tty, or (re)entry to standard tty line discipline. 203 */ 204int 205ttyopen(device, tp) 206 dev_t device; 207 register struct tty *tp; 208{ 209 int s; 210 211 s = spltty(); 212 tp->t_dev = device; 213 if (!ISSET(tp->t_state, TS_ISOPEN)) { 214 SET(tp->t_state, TS_ISOPEN); 215 if (ISSET(tp->t_cflag, CLOCAL)) 216 SET(tp->t_state, TS_CONNECTED); 217 bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 218 } 219 220 /* 221 * Initialize or restore a cblock allocation policy suitable for 222 * the standard line discipline. 223 */ 224 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512); 225 clist_alloc_cblocks(&tp->t_outq, TTMAXHIWAT + OBUFSIZ + 100, 226 TTMAXHIWAT + OBUFSIZ + 100); 227 clist_alloc_cblocks(&tp->t_rawq, TTYHOG, TTYHOG); 228 229 splx(s); 230 return (0); 231} 232 233/* 234 * Handle close() on a tty line: flush and set to initial state, 235 * bumping generation number so that pending read/write calls 236 * can detect recycling of the tty. 237 * XXX our caller should have done `spltty(); l_close(); ttyclose();' 238 * and l_close() should have flushed, but we repeat the spltty() and 239 * the flush in case there are buggy callers. 240 */ 241int 242ttyclose(tp) 243 register struct tty *tp; 244{ 245 int s; 246 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 > I_HIGH_WATER - 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#ifdef sun4c /* XXX */ 432 (*tp->t_stop)(tp, 0); 433#else 434 (*cdevsw[major(tp->t_dev)]->d_stop)(tp, 435 0); 436#endif 437 return (0); 438 } 439 if (!CCEQ(cc[VSTART], c)) 440 return (0); 441 /* 442 * if VSTART == VSTOP then toggle 443 */ 444 goto endcase; 445 } 446 if (CCEQ(cc[VSTART], c)) 447 goto restartoutput; 448 } 449 /* 450 * IGNCR, ICRNL, & INLCR 451 */ 452 if (c == '\r') { 453 if (ISSET(iflag, IGNCR)) 454 return (0); 455 else if (ISSET(iflag, ICRNL)) 456 c = '\n'; 457 } else if (c == '\n' && ISSET(iflag, INLCR)) 458 c = '\r'; 459 } 460 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) { 461 /* 462 * From here on down canonical mode character 463 * processing takes place. 464 */ 465 /* 466 * erase (^H / ^?) 467 */ 468 if (CCEQ(cc[VERASE], c)) { 469 if (tp->t_rawq.c_cc) 470 ttyrub(unputc(&tp->t_rawq), tp); 471 goto endcase; 472 } 473 /* 474 * kill (^U) 475 */ 476 if (CCEQ(cc[VKILL], c)) { 477 if (ISSET(lflag, ECHOKE) && 478 tp->t_rawq.c_cc == tp->t_rocount && 479 !ISSET(lflag, ECHOPRT)) 480 while (tp->t_rawq.c_cc) 481 ttyrub(unputc(&tp->t_rawq), tp); 482 else { 483 ttyecho(c, tp); 484 if (ISSET(lflag, ECHOK) || 485 ISSET(lflag, ECHOKE)) 486 ttyecho('\n', tp); 487 FLUSHQ(&tp->t_rawq); 488 tp->t_rocount = 0; 489 } 490 CLR(tp->t_state, TS_LOCAL); 491 goto endcase; 492 } 493 /* 494 * word erase (^W) 495 */ 496 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) { 497 int ctype; 498 499 /* 500 * erase whitespace 501 */ 502 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 503 ttyrub(c, tp); 504 if (c == -1) 505 goto endcase; 506 /* 507 * erase last char of word and remember the 508 * next chars type (for ALTWERASE) 509 */ 510 ttyrub(c, tp); 511 c = unputc(&tp->t_rawq); 512 if (c == -1) 513 goto endcase; 514 if (c == ' ' || c == '\t') { 515 (void)putc(c, &tp->t_rawq); 516 goto endcase; 517 } 518 ctype = ISALPHA(c); 519 /* 520 * erase rest of word 521 */ 522 do { 523 ttyrub(c, tp); 524 c = unputc(&tp->t_rawq); 525 if (c == -1) 526 goto endcase; 527 } while (c != ' ' && c != '\t' && 528 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype)); 529 (void)putc(c, &tp->t_rawq); 530 goto endcase; 531 } 532 /* 533 * reprint line (^R) 534 */ 535 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) { 536 ttyretype(tp); 537 goto endcase; 538 } 539 /* 540 * ^T - kernel info and generate SIGINFO 541 */ 542 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) { 543 if (ISSET(lflag, ISIG)) 544 pgsignal(tp->t_pgrp, SIGINFO, 1); 545 if (!ISSET(lflag, NOKERNINFO)) 546 ttyinfo(tp); 547 goto endcase; 548 } 549 } 550 /* 551 * Check for input buffer overflow 552 */ 553 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) { 554input_overflow: 555 if (ISSET(iflag, IMAXBEL)) { 556 if (tp->t_outq.c_cc < tp->t_hiwat) 557 (void)ttyoutput(CTRL('g'), tp); 558 } 559 goto endcase; 560 } 561 562 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP) 563 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR)) 564 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 565 566 /* 567 * Put data char in q for user and 568 * wakeup on seeing a line delimiter. 569 */ 570 if (putc(c, &tp->t_rawq) >= 0) { 571 if (!ISSET(lflag, ICANON)) { 572 ttwakeup(tp); 573 ttyecho(c, tp); 574 goto endcase; 575 } 576 if (TTBREAKC(c, lflag)) { 577 tp->t_rocount = 0; 578 catq(&tp->t_rawq, &tp->t_canq); 579 ttwakeup(tp); 580 } else if (tp->t_rocount++ == 0) 581 tp->t_rocol = tp->t_column; 582 if (ISSET(tp->t_state, TS_ERASE)) { 583 /* 584 * end of prterase \.../ 585 */ 586 CLR(tp->t_state, TS_ERASE); 587 (void)ttyoutput('/', tp); 588 } 589 i = tp->t_column; 590 ttyecho(c, tp); 591 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) { 592 /* 593 * Place the cursor over the '^' of the ^D. 594 */ 595 i = imin(2, tp->t_column - i); 596 while (i > 0) { 597 (void)ttyoutput('\b', tp); 598 i--; 599 } 600 } 601 } 602endcase: 603 /* 604 * IXANY means allow any character to restart output. 605 */ 606 if (ISSET(tp->t_state, TS_TTSTOP) && 607 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) 608 return (0); 609restartoutput: 610 CLR(tp->t_lflag, FLUSHO); 611 CLR(tp->t_state, TS_TTSTOP); 612startoutput: 613 return (ttstart(tp)); 614} 615 616/* 617 * Output a single character on a tty, doing output processing 618 * as needed (expanding tabs, newline processing, etc.). 619 * Returns < 0 if succeeds, otherwise returns char to resend. 620 * Must be recursive. 621 */ 622static int 623ttyoutput(c, tp) 624 register int c; 625 register struct tty *tp; 626{ 627 register tcflag_t oflag; 628 register int col, s; 629 630 oflag = tp->t_oflag; 631 if (!ISSET(oflag, OPOST)) { 632 if (ISSET(tp->t_lflag, FLUSHO)) 633 return (-1); 634 if (putc(c, &tp->t_outq)) 635 return (c); 636 tk_nout++; 637 tp->t_outcc++; 638 return (-1); 639 } 640 /* 641 * Do tab expansion if OXTABS is set. Special case if we external 642 * processing, we don't do the tab expansion because we'll probably 643 * get it wrong. If tab expansion needs to be done, let it happen 644 * externally. 645 */ 646 CLR(c, ~TTY_CHARMASK); 647 if (c == '\t' && 648 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) { 649 c = 8 - (tp->t_column & 7); 650 if (!ISSET(tp->t_lflag, FLUSHO)) { 651 s = spltty(); /* Don't interrupt tabs. */ 652 c -= b_to_q(" ", c, &tp->t_outq); 653 tk_nout += c; 654 tp->t_outcc += c; 655 splx(s); 656 } 657 tp->t_column += c; 658 return (c ? -1 : '\t'); 659 } 660 if (c == CEOT && ISSET(oflag, ONOEOT)) 661 return (-1); 662 663 /* 664 * Newline translation: if ONLCR is set, 665 * translate newline into "\r\n". 666 */ 667 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) { 668 tk_nout++; 669 tp->t_outcc++; 670 if (putc('\r', &tp->t_outq)) 671 return (c); 672 } 673 tk_nout++; 674 tp->t_outcc++; 675 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq)) 676 return (c); 677 678 col = tp->t_column; 679 switch (CCLASS(c)) { 680 case BACKSPACE: 681 if (col > 0) 682 --col; 683 break; 684 case CONTROL: 685 break; 686 case NEWLINE: 687 case RETURN: 688 col = 0; 689 break; 690 case ORDINARY: 691 ++col; 692 break; 693 case TAB: 694 col = (col + 8) & ~7; 695 break; 696 } 697 tp->t_column = col; 698 return (-1); 699} 700 701/* 702 * Ioctls for all tty devices. Called after line-discipline specific ioctl 703 * has been called to do discipline-specific functions and/or reject any 704 * of these ioctl commands. 705 */ 706/* ARGSUSED */ 707int 708ttioctl(tp, cmd, data, flag) 709 register struct tty *tp; 710 int cmd, flag; 711 void *data; 712{ 713 register struct proc *p; 714 int s, error; 715 716 p = curproc; /* XXX */ 717 718 /* If the ioctl involves modification, hang if in the background. */ 719 switch (cmd) { 720 case TIOCFLUSH: 721 case TIOCSETA: 722 case TIOCSETD: 723 case TIOCSETAF: 724 case TIOCSETAW: 725#ifdef notdef 726 case TIOCSPGRP: 727#endif 728 case TIOCSTAT: 729 case TIOCSTI: 730 case TIOCSWINSZ: 731#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 732 case TIOCLBIC: 733 case TIOCLBIS: 734 case TIOCLSET: 735 case TIOCSETC: 736 case OTIOCSETD: 737 case TIOCSETN: 738 case TIOCSETP: 739 case TIOCSLTC: 740#endif 741 while (isbackground(curproc, tp) && 742 p->p_pgrp->pg_jobc && (p->p_flag & P_PPWAIT) == 0 && 743 (p->p_sigignore & sigmask(SIGTTOU)) == 0 && 744 (p->p_sigmask & sigmask(SIGTTOU)) == 0) { 745 pgsignal(p->p_pgrp, SIGTTOU, 1); 746 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1", 747 0); 748 if (error) 749 return (error); 750 } 751 break; 752 } 753 754 switch (cmd) { /* Process the ioctl. */ 755 case FIOASYNC: /* set/clear async i/o */ 756 s = spltty(); 757 if (*(int *)data) 758 SET(tp->t_state, TS_ASYNC); 759 else 760 CLR(tp->t_state, TS_ASYNC); 761 splx(s); 762 break; 763 case FIONBIO: /* set/clear non-blocking i/o */ 764 break; /* XXX: delete. */ 765 case FIONREAD: /* get # bytes to read */ 766 s = spltty(); 767 *(int *)data = ttnread(tp); 768 splx(s); 769 break; 770 case TIOCEXCL: /* set exclusive use of tty */ 771 s = spltty(); 772 SET(tp->t_state, TS_XCLUDE); 773 splx(s); 774 break; 775 case TIOCFLUSH: { /* flush buffers */ 776 register int flags = *(int *)data; 777 778 if (flags == 0) 779 flags = FREAD | FWRITE; 780 else 781 flags &= FREAD | FWRITE; 782 ttyflush(tp, flags); 783 break; 784 } 785 case TIOCCONS: /* become virtual console */ 786 if (*(int *)data) { 787 if (constty && constty != tp && 788 ISSET(constty->t_state, TS_CONNECTED)) 789 return (EBUSY); 790#ifndef UCONSOLE 791 if (error = suser(p->p_ucred, &p->p_acflag)) 792 return (error); 793#endif 794 constty = tp; 795 } else if (tp == constty) 796 constty = NULL; 797 break; 798 case TIOCDRAIN: /* wait till output drained */ 799 error = ttywait(tp); 800 if (error) 801 return (error); 802 break; 803 case TIOCGETA: { /* get termios struct */ 804 struct termios *t = (struct termios *)data; 805 806 bcopy(&tp->t_termios, t, sizeof(struct termios)); 807 break; 808 } 809 case TIOCGETD: /* get line discipline */ 810 *(int *)data = tp->t_line; 811 break; 812 case TIOCGWINSZ: /* get window size */ 813 *(struct winsize *)data = tp->t_winsize; 814 break; 815 case TIOCGPGRP: /* get pgrp of tty */ 816 if (!isctty(p, tp)) 817 return (ENOTTY); 818 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 819 break; 820#ifdef TIOCHPCL 821 case TIOCHPCL: /* hang up on last close */ 822 s = spltty(); 823 SET(tp->t_cflag, HUPCL); 824 splx(s); 825 break; 826#endif 827 case TIOCNXCL: /* reset exclusive use of tty */ 828 s = spltty(); 829 CLR(tp->t_state, TS_XCLUDE); 830 splx(s); 831 break; 832 case TIOCOUTQ: /* output queue size */ 833 *(int *)data = tp->t_outq.c_cc; 834 break; 835 case TIOCSETA: /* set termios struct */ 836 case TIOCSETAW: /* drain output, set */ 837 case TIOCSETAF: { /* drn out, fls in, set */ 838 register struct termios *t = (struct termios *)data; 839 840 if (t->c_ispeed < 0 || t->c_ospeed < 0) 841 return (EINVAL); 842 s = spltty(); 843 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 844 error = ttywait(tp); 845 if (error) { 846 splx(s); 847 return (error); 848 } 849 if (cmd == TIOCSETAF) 850 ttyflush(tp, FREAD); 851 } 852 if (!ISSET(t->c_cflag, CIGNORE)) { 853 /* 854 * Set device hardware. 855 */ 856 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 857 splx(s); 858 return (error); 859 } 860 if (ISSET(t->c_cflag, CLOCAL) && 861 !ISSET(tp->t_cflag, CLOCAL)) { 862 /* 863 * XXX disconnections would be too hard to 864 * get rid of without this kludge. The only 865 * way to get rid of controlling terminals 866 * is to exit from the session leader. 867 */ 868 CLR(tp->t_state, TS_ZOMBIE); 869 870 wakeup(TSA_CARR_ON(tp)); 871 ttwakeup(tp); 872 ttwwakeup(tp); 873 } 874 if ((ISSET(tp->t_state, TS_CARR_ON) || 875 ISSET(t->c_cflag, CLOCAL)) && 876 !ISSET(tp->t_state, TS_ZOMBIE)) 877 SET(tp->t_state, TS_CONNECTED); 878 else 879 CLR(tp->t_state, TS_CONNECTED); 880 tp->t_cflag = t->c_cflag; 881 tp->t_ispeed = t->c_ispeed; 882 tp->t_ospeed = t->c_ospeed; 883 ttsetwater(tp); 884 } 885 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) && 886 cmd != TIOCSETAF) { 887 if (ISSET(t->c_lflag, ICANON)) 888 SET(tp->t_lflag, PENDIN); 889 else { 890 /* 891 * XXX we really shouldn't allow toggling 892 * ICANON while we're in a non-termios line 893 * discipline. Now we have to worry about 894 * panicing for a null queue. 895 */ 896 if (tp->t_canq.c_cbreserved > 0 && 897 tp->t_rawq.c_cbreserved > 0) { 898 catq(&tp->t_rawq, &tp->t_canq); 899 /* 900 * XXX the queue limits may be 901 * different, so the old queue 902 * swapping method no longer works. 903 */ 904 catq(&tp->t_canq, &tp->t_rawq); 905 } 906 CLR(tp->t_lflag, PENDIN); 907 } 908 ttwakeup(tp); 909 } 910 tp->t_iflag = t->c_iflag; 911 tp->t_oflag = t->c_oflag; 912 /* 913 * Make the EXTPROC bit read only. 914 */ 915 if (ISSET(tp->t_lflag, EXTPROC)) 916 SET(t->c_lflag, EXTPROC); 917 else 918 CLR(t->c_lflag, EXTPROC); 919 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 920 if (t->c_cc[VMIN] != tp->t_cc[VMIN] || 921 t->c_cc[VTIME] != tp->t_cc[VTIME]) 922 ttwakeup(tp); 923 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 924 splx(s); 925 break; 926 } 927 case TIOCSETD: { /* set line discipline */ 928 register int t = *(int *)data; 929 dev_t device = tp->t_dev; 930 931 if ((u_int)t >= nlinesw) 932 return (ENXIO); 933 if (t != tp->t_line) { 934 s = spltty(); 935 (*linesw[tp->t_line].l_close)(tp, flag); 936 error = (*linesw[t].l_open)(device, tp); 937 if (error) { 938 (void)(*linesw[tp->t_line].l_open)(device, tp); 939 splx(s); 940 return (error); 941 } 942 tp->t_line = t; 943 splx(s); 944 } 945 break; 946 } 947 case TIOCSTART: /* start output, like ^Q */ 948 s = spltty(); 949 if (ISSET(tp->t_state, TS_TTSTOP) || 950 ISSET(tp->t_lflag, FLUSHO)) { 951 CLR(tp->t_lflag, FLUSHO); 952 CLR(tp->t_state, TS_TTSTOP); 953 ttstart(tp); 954 } 955 splx(s); 956 break; 957 case TIOCSTI: /* simulate terminal input */ 958 if (p->p_ucred->cr_uid && (flag & FREAD) == 0) 959 return (EPERM); 960 if (p->p_ucred->cr_uid && !isctty(p, tp)) 961 return (EACCES); 962 s = spltty(); 963 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 964 splx(s); 965 break; 966 case TIOCSTOP: /* stop output, like ^S */ 967 s = spltty(); 968 if (!ISSET(tp->t_state, TS_TTSTOP)) { 969 SET(tp->t_state, TS_TTSTOP); 970#ifdef sun4c /* XXX */ 971 (*tp->t_stop)(tp, 0); 972#else 973 (*cdevsw[major(tp->t_dev)]->d_stop)(tp, 0); 974#endif 975 } 976 splx(s); 977 break; 978 case TIOCSCTTY: /* become controlling tty */ 979 /* Session ctty vnode pointer set in vnode layer. */ 980 if (!SESS_LEADER(p) || 981 ((p->p_session->s_ttyvp || tp->t_session) && 982 (tp->t_session != p->p_session))) 983 return (EPERM); 984 tp->t_session = p->p_session; 985 tp->t_pgrp = p->p_pgrp; 986 p->p_session->s_ttyp = tp; 987 p->p_flag |= P_CONTROLT; 988 break; 989 case TIOCSPGRP: { /* set pgrp of tty */ 990 register struct pgrp *pgrp = pgfind(*(int *)data); 991 992 if (!isctty(p, tp)) 993 return (ENOTTY); 994 else if (pgrp == NULL || pgrp->pg_session != p->p_session) 995 return (EPERM); 996 tp->t_pgrp = pgrp; 997 break; 998 } 999 case TIOCSTAT: /* simulate control-T */ 1000 s = spltty(); 1001 ttyinfo(tp); 1002 splx(s); 1003 break; 1004 case TIOCSWINSZ: /* set window size */ 1005 if (bcmp((caddr_t)&tp->t_winsize, data, 1006 sizeof (struct winsize))) { 1007 tp->t_winsize = *(struct winsize *)data; 1008 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1009 } 1010 break; 1011 case TIOCSDRAINWAIT: 1012 error = suser(p->p_ucred, &p->p_acflag); 1013 if (error) 1014 return (error); 1015 tp->t_timeout = *(int *)data * hz; 1016 wakeup(TSA_OCOMPLETE(tp)); 1017 wakeup(TSA_OLOWAT(tp)); 1018 break; 1019 case TIOCGDRAINWAIT: 1020 *(int *)data = tp->t_timeout / hz; 1021 break; 1022 default: 1023#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1024 return (ttcompat(tp, cmd, data, flag)); 1025#else 1026 return (-1); 1027#endif 1028 } 1029 return (0); 1030} 1031 1032int 1033ttyselect(tp, rw, p) 1034 struct tty *tp; 1035 int rw; 1036 struct proc *p; 1037{ 1038 int s; 1039 1040 if (tp == NULL) 1041 return (ENXIO); 1042 1043 s = spltty(); 1044 switch (rw) { 1045 case FREAD: 1046 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) 1047 goto win; 1048 selrecord(p, &tp->t_rsel); 1049 break; 1050 case FWRITE: 1051 if ((tp->t_outq.c_cc <= tp->t_lowat && 1052 ISSET(tp->t_state, TS_CONNECTED)) 1053 || ISSET(tp->t_state, TS_ZOMBIE)) { 1054win: splx(s); 1055 return (1); 1056 } 1057 selrecord(p, &tp->t_wsel); 1058 break; 1059 } 1060 splx(s); 1061 return (0); 1062} 1063 1064/* 1065 * This is a wrapper for compatibility with the select vector used by 1066 * cdevsw. It relies on a proper xxxdevtotty routine. 1067 */ 1068int 1069ttselect(dev, rw, p) 1070 dev_t dev; 1071 int rw; 1072 struct proc *p; 1073{ 1074 return ttyselect((*cdevsw[major(dev)]->d_devtotty)(dev), rw, p); 1075} 1076 1077/* 1078 * Must be called at spltty(). 1079 */ 1080static int 1081ttnread(tp) 1082 struct tty *tp; 1083{ 1084 int nread; 1085 1086 if (ISSET(tp->t_lflag, PENDIN)) 1087 ttypend(tp); 1088 nread = tp->t_canq.c_cc; 1089 if (!ISSET(tp->t_lflag, ICANON)) { 1090 nread += tp->t_rawq.c_cc; 1091 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) 1092 nread = 0; 1093 } 1094 return (nread); 1095} 1096 1097/* 1098 * Wait for output to drain. 1099 */ 1100int 1101ttywait(tp) 1102 register struct tty *tp; 1103{ 1104 int error, s; 1105 1106 error = 0; 1107 s = spltty(); 1108 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1109 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) { 1110 (*tp->t_oproc)(tp); 1111 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1112 ISSET(tp->t_state, TS_CONNECTED)) { 1113 SET(tp->t_state, TS_SO_OCOMPLETE); 1114 error = ttysleep(tp, TSA_OCOMPLETE(tp), 1115 TTOPRI | PCATCH, "ttywai", 1116 tp->t_timeout); 1117 if (error) { 1118 if (error == EWOULDBLOCK) 1119 error = EIO; 1120 break; 1121 } 1122 } else 1123 break; 1124 } 1125 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY))) 1126 error = EIO; 1127 splx(s); 1128 return (error); 1129} 1130 1131/* 1132 * Flush if successfully wait. 1133 */ 1134static int 1135ttywflush(tp) 1136 struct tty *tp; 1137{ 1138 int error; 1139 1140 if ((error = ttywait(tp)) == 0) 1141 ttyflush(tp, FREAD); 1142 return (error); 1143} 1144 1145/* 1146 * Flush tty read and/or write queues, notifying anyone waiting. 1147 */ 1148void 1149ttyflush(tp, rw) 1150 register struct tty *tp; 1151 int rw; 1152{ 1153 register int s; 1154 1155 s = spltty(); 1156#if 0 1157again: 1158#endif 1159 if (rw & FWRITE) 1160 CLR(tp->t_state, TS_TTSTOP); 1161#ifdef sun4c /* XXX */ 1162 (*tp->t_stop)(tp, rw); 1163#else 1164 (*cdevsw[major(tp->t_dev)]->d_stop)(tp, rw); 1165#endif 1166 if (rw & FREAD) { 1167 FLUSHQ(&tp->t_canq); 1168 FLUSHQ(&tp->t_rawq); 1169 CLR(tp->t_lflag, PENDIN); 1170 tp->t_rocount = 0; 1171 tp->t_rocol = 0; 1172 CLR(tp->t_state, TS_LOCAL); 1173 ttwakeup(tp); 1174 if (ISSET(tp->t_state, TS_TBLOCK)) { 1175 if (rw & FWRITE) 1176 FLUSHQ(&tp->t_outq); 1177 ttyunblock(tp); 1178 1179 /* 1180 * Don't let leave any state that might clobber the 1181 * next line discipline (although we should do more 1182 * to send the START char). Not clearing the state 1183 * may have caused the "putc to a clist with no 1184 * reserved cblocks" panic/printf. 1185 */ 1186 CLR(tp->t_state, TS_TBLOCK); 1187 1188#if 0 /* forget it, sleeping isn't always safe and we don't know when it is */ 1189 if (ISSET(tp->t_iflag, IXOFF)) { 1190 /* 1191 * XXX wait a bit in the hope that the stop 1192 * character (if any) will go out. Waiting 1193 * isn't good since it allows races. This 1194 * will be fixed when the stop character is 1195 * put in a special queue. Don't bother with 1196 * the checks in ttywait() since the timeout 1197 * will save us. 1198 */ 1199 SET(tp->t_state, TS_SO_OCOMPLETE); 1200 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI, 1201 "ttyfls", hz / 10); 1202 /* 1203 * Don't try sending the stop character again. 1204 */ 1205 CLR(tp->t_state, TS_TBLOCK); 1206 goto again; 1207 } 1208#endif 1209 } 1210 } 1211 if (rw & FWRITE) { 1212 FLUSHQ(&tp->t_outq); 1213 ttwwakeup(tp); 1214 } 1215 splx(s); 1216} 1217 1218/* 1219 * Copy in the default termios characters. 1220 */ 1221void 1222termioschars(t) 1223 struct termios *t; 1224{ 1225 1226 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc); 1227} 1228 1229/* 1230 * Old interface. 1231 */ 1232void 1233ttychars(tp) 1234 struct tty *tp; 1235{ 1236 1237 termioschars(&tp->t_termios); 1238} 1239 1240/* 1241 * Handle input high water. Send stop character for the IXOFF case. Turn 1242 * on our input flow control bit and propagate the changes to the driver. 1243 * XXX the stop character should be put in a special high priority queue. 1244 */ 1245void 1246ttyblock(tp) 1247 struct tty *tp; 1248{ 1249 1250 SET(tp->t_state, TS_TBLOCK); 1251 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1252 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0) 1253 CLR(tp->t_state, TS_TBLOCK); /* try again later */ 1254 ttstart(tp); 1255} 1256 1257/* 1258 * Handle input low water. Send start character for the IXOFF case. Turn 1259 * off our input flow control bit and propagate the changes to the driver. 1260 * XXX the start character should be put in a special high priority queue. 1261 */ 1262static void 1263ttyunblock(tp) 1264 struct tty *tp; 1265{ 1266 1267 CLR(tp->t_state, TS_TBLOCK); 1268 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE && 1269 putc(tp->t_cc[VSTART], &tp->t_outq) != 0) 1270 SET(tp->t_state, TS_TBLOCK); /* try again later */ 1271 ttstart(tp); 1272} 1273 1274#ifdef notyet 1275/* Not used by any current (i386) drivers. */ 1276/* 1277 * Restart after an inter-char delay. 1278 */ 1279void 1280ttrstrt(tp_arg) 1281 void *tp_arg; 1282{ 1283 struct tty *tp; 1284 int s; 1285 1286#ifdef DIAGNOSTIC 1287 if (tp_arg == NULL) 1288 panic("ttrstrt"); 1289#endif 1290 tp = tp_arg; 1291 s = spltty(); 1292 1293 CLR(tp->t_state, TS_TIMEOUT); 1294 ttstart(tp); 1295 1296 splx(s); 1297} 1298#endif 1299 1300int 1301ttstart(tp) 1302 struct tty *tp; 1303{ 1304 1305 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1306 (*tp->t_oproc)(tp); 1307 return (0); 1308} 1309 1310/* 1311 * "close" a line discipline 1312 */ 1313int 1314ttylclose(tp, flag) 1315 struct tty *tp; 1316 int flag; 1317{ 1318 1319 if (flag & FNONBLOCK || ttywflush(tp)) 1320 ttyflush(tp, FREAD | FWRITE); 1321 return (0); 1322} 1323 1324/* 1325 * Handle modem control transition on a tty. 1326 * Flag indicates new state of carrier. 1327 * Returns 0 if the line should be turned off, otherwise 1. 1328 */ 1329int 1330ttymodem(tp, flag) 1331 register struct tty *tp; 1332 int flag; 1333{ 1334 1335 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) { 1336 /* 1337 * MDMBUF: do flow control according to carrier flag 1338 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP 1339 * works if IXON and IXANY are clear. 1340 */ 1341 if (flag) { 1342 CLR(tp->t_state, TS_CAR_OFLOW); 1343 CLR(tp->t_state, TS_TTSTOP); 1344 ttstart(tp); 1345 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) { 1346 SET(tp->t_state, TS_CAR_OFLOW); 1347 SET(tp->t_state, TS_TTSTOP); 1348#ifdef sun4c /* XXX */ 1349 (*tp->t_stop)(tp, 0); 1350#else 1351 (*cdevsw[major(tp->t_dev)]->d_stop)(tp, 0); 1352#endif 1353 } 1354 } else if (flag == 0) { 1355 /* 1356 * Lost carrier. 1357 */ 1358 CLR(tp->t_state, TS_CARR_ON); 1359 if (ISSET(tp->t_state, TS_ISOPEN) && 1360 !ISSET(tp->t_cflag, CLOCAL)) { 1361 SET(tp->t_state, TS_ZOMBIE); 1362 CLR(tp->t_state, TS_CONNECTED); 1363 if (tp->t_session && tp->t_session->s_leader) 1364 psignal(tp->t_session->s_leader, SIGHUP); 1365 ttyflush(tp, FREAD | FWRITE); 1366 return (0); 1367 } 1368 } else { 1369 /* 1370 * Carrier now on. 1371 */ 1372 SET(tp->t_state, TS_CARR_ON); 1373 if (!ISSET(tp->t_state, TS_ZOMBIE)) 1374 SET(tp->t_state, TS_CONNECTED); 1375 wakeup(TSA_CARR_ON(tp)); 1376 ttwakeup(tp); 1377 ttwwakeup(tp); 1378 } 1379 return (1); 1380} 1381 1382/* 1383 * Reinput pending characters after state switch 1384 * call at spltty(). 1385 */ 1386static void 1387ttypend(tp) 1388 register struct tty *tp; 1389{ 1390 struct clist tq; 1391 register int c; 1392 1393 CLR(tp->t_lflag, PENDIN); 1394 SET(tp->t_state, TS_TYPEN); 1395 /* 1396 * XXX this assumes too much about clist internals. It may even 1397 * fail if the cblock slush pool is empty. We can't allocate more 1398 * cblocks here because we are called from an interrupt handler 1399 * and clist_alloc_cblocks() can wait. 1400 */ 1401 tq = tp->t_rawq; 1402 bzero(&tp->t_rawq, sizeof tp->t_rawq); 1403 tp->t_rawq.c_cbmax = tq.c_cbmax; 1404 tp->t_rawq.c_cbreserved = tq.c_cbreserved; 1405 while ((c = getc(&tq)) >= 0) 1406 ttyinput(c, tp); 1407 CLR(tp->t_state, TS_TYPEN); 1408} 1409 1410/* 1411 * Process a read call on a tty device. 1412 */ 1413int 1414ttread(tp, uio, flag) 1415 register struct tty *tp; 1416 struct uio *uio; 1417 int flag; 1418{ 1419 register struct clist *qp; 1420 register int c; 1421 register tcflag_t lflag; 1422 register cc_t *cc = tp->t_cc; 1423 register struct proc *p = curproc; 1424 int s, first, error = 0; 1425 int has_stime = 0, last_cc = 0; 1426 long slp = 0; /* XXX this should be renamed `timo'. */ 1427 1428loop: 1429 s = spltty(); 1430 lflag = tp->t_lflag; 1431 /* 1432 * take pending input first 1433 */ 1434 if (ISSET(lflag, PENDIN)) { 1435 ttypend(tp); 1436 splx(s); /* reduce latency */ 1437 s = spltty(); 1438 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */ 1439 } 1440 1441 /* 1442 * Hang process if it's in the background. 1443 */ 1444 if (isbackground(p, tp)) { 1445 splx(s); 1446 if ((p->p_sigignore & sigmask(SIGTTIN)) || 1447 (p->p_sigmask & sigmask(SIGTTIN)) || 1448 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0) 1449 return (EIO); 1450 pgsignal(p->p_pgrp, SIGTTIN, 1); 1451 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0); 1452 if (error) 1453 return (error); 1454 goto loop; 1455 } 1456 1457 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1458 splx(s); 1459 return (0); /* EOF */ 1460 } 1461 1462 /* 1463 * If canonical, use the canonical queue, 1464 * else use the raw queue. 1465 * 1466 * (should get rid of clists...) 1467 */ 1468 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 1469 1470 if (flag & IO_NDELAY) { 1471 if (qp->c_cc > 0) 1472 goto read; 1473 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) { 1474 splx(s); 1475 return (0); 1476 } 1477 splx(s); 1478 return (EWOULDBLOCK); 1479 } 1480 if (!ISSET(lflag, ICANON)) { 1481 int m = cc[VMIN]; 1482 long t = cc[VTIME]; 1483 struct timeval stime, timecopy; 1484 int x; 1485 1486 /* 1487 * Check each of the four combinations. 1488 * (m > 0 && t == 0) is the normal read case. 1489 * It should be fairly efficient, so we check that and its 1490 * companion case (m == 0 && t == 0) first. 1491 * For the other two cases, we compute the target sleep time 1492 * into slp. 1493 */ 1494 if (t == 0) { 1495 if (qp->c_cc < m) 1496 goto sleep; 1497 if (qp->c_cc > 0) 1498 goto read; 1499 1500 /* m, t and qp->c_cc are all 0. 0 is enough input. */ 1501 splx(s); 1502 return (0); 1503 } 1504 t *= 100000; /* time in us */ 1505#define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1506 ((t1).tv_usec - (t2).tv_usec)) 1507 if (m > 0) { 1508 if (qp->c_cc <= 0) 1509 goto sleep; 1510 if (qp->c_cc >= m) 1511 goto read; 1512 x = splclock(); 1513 timecopy = time; 1514 splx(x); 1515 if (!has_stime) { 1516 /* first character, start timer */ 1517 has_stime = 1; 1518 stime = timecopy; 1519 slp = t; 1520 } else if (qp->c_cc > last_cc) { 1521 /* got a character, restart timer */ 1522 stime = timecopy; 1523 slp = t; 1524 } else { 1525 /* nothing, check expiration */ 1526 slp = t - diff(timecopy, stime); 1527 if (slp <= 0) 1528 goto read; 1529 } 1530 last_cc = qp->c_cc; 1531 } else { /* m == 0 */ 1532 if (qp->c_cc > 0) 1533 goto read; 1534 x = splclock(); 1535 timecopy = time; 1536 splx(x); 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 hzto() for how to avoid overflow if hz 1557 * is large (divide by `tick' and/or arrange to 1558 * use hzto() 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 <= I_LOW_WATER) 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_hiwat; 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_hiwat; 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 p->p_pgrp->pg_jobc) { 1773 pgsignal(p->p_pgrp, SIGTTOU, 1); 1774 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0); 1775 if (error) 1776 goto out; 1777 goto loop; 1778 } 1779 /* 1780 * Process the user's data in at most OBUFSIZ chunks. Perform any 1781 * output translation. Keep track of high water mark, sleep on 1782 * overflow awaiting device aid in acquiring new space. 1783 */ 1784 while (uio->uio_resid > 0 || cc > 0) { 1785 if (ISSET(tp->t_lflag, FLUSHO)) { 1786 uio->uio_resid = 0; 1787 return (0); 1788 } 1789 if (tp->t_outq.c_cc > hiwat) 1790 goto ovhiwat; 1791 /* 1792 * Grab a hunk of data from the user, unless we have some 1793 * leftover from last time. 1794 */ 1795 if (cc == 0) { 1796 cc = imin(uio->uio_resid, OBUFSIZ); 1797 cp = obuf; 1798 error = uiomove(cp, cc, uio); 1799 if (error) { 1800 cc = 0; 1801 break; 1802 } 1803#if NSNP > 0 1804 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1805 snpin((struct snoop *)tp->t_sc, cp, cc); 1806#endif 1807 } 1808 /* 1809 * If nothing fancy need be done, grab those characters we 1810 * can handle without any of ttyoutput's processing and 1811 * just transfer them to the output q. For those chars 1812 * which require special processing (as indicated by the 1813 * bits in char_type), call ttyoutput. After processing 1814 * a hunk of data, look for FLUSHO so ^O's will take effect 1815 * immediately. 1816 */ 1817 while (cc > 0) { 1818 if (!ISSET(tp->t_oflag, OPOST)) 1819 ce = cc; 1820 else { 1821 ce = cc - scanc((u_int)cc, (u_char *)cp, 1822 char_type, CCLASSMASK); 1823 /* 1824 * If ce is zero, then we're processing 1825 * a special character through ttyoutput. 1826 */ 1827 if (ce == 0) { 1828 tp->t_rocount = 0; 1829 if (ttyoutput(*cp, tp) >= 0) { 1830 /* No Clists, wait a bit. */ 1831 ttstart(tp); 1832 if (flag & IO_NDELAY) { 1833 error = EWOULDBLOCK; 1834 goto out; 1835 } 1836 error = ttysleep(tp, &lbolt, 1837 TTOPRI|PCATCH, 1838 "ttybf1", 0); 1839 if (error) 1840 goto out; 1841 goto loop; 1842 } 1843 cp++; 1844 cc--; 1845 if (ISSET(tp->t_lflag, FLUSHO) || 1846 tp->t_outq.c_cc > hiwat) 1847 goto ovhiwat; 1848 continue; 1849 } 1850 } 1851 /* 1852 * A bunch of normal characters have been found. 1853 * Transfer them en masse to the output queue and 1854 * continue processing at the top of the loop. 1855 * If there are any further characters in this 1856 * <= OBUFSIZ chunk, the first should be a character 1857 * requiring special handling by ttyoutput. 1858 */ 1859 tp->t_rocount = 0; 1860 i = b_to_q(cp, ce, &tp->t_outq); 1861 ce -= i; 1862 tp->t_column += ce; 1863 cp += ce, cc -= ce, tk_nout += ce; 1864 tp->t_outcc += ce; 1865 if (i > 0) { 1866 /* No Clists, wait a bit. */ 1867 ttstart(tp); 1868 if (flag & IO_NDELAY) { 1869 error = EWOULDBLOCK; 1870 goto out; 1871 } 1872 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, 1873 "ttybf2", 0); 1874 if (error) 1875 goto out; 1876 goto loop; 1877 } 1878 if (ISSET(tp->t_lflag, FLUSHO) || 1879 tp->t_outq.c_cc > hiwat) 1880 break; 1881 } 1882 ttstart(tp); 1883 } 1884out: 1885 /* 1886 * If cc is nonzero, we leave the uio structure inconsistent, as the 1887 * offset and iov pointers have moved forward, but it doesn't matter 1888 * (the call will either return short or restart with a new uio). 1889 */ 1890 uio->uio_resid += cc; 1891 return (error); 1892 1893ovhiwat: 1894 ttstart(tp); 1895 s = spltty(); 1896 /* 1897 * This can only occur if FLUSHO is set in t_lflag, 1898 * or if ttstart/oproc is synchronous (or very fast). 1899 */ 1900 if (tp->t_outq.c_cc <= hiwat) { 1901 splx(s); 1902 goto loop; 1903 } 1904 if (flag & IO_NDELAY) { 1905 splx(s); 1906 uio->uio_resid += cc; 1907 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 1908 } 1909 SET(tp->t_state, TS_SO_OLOWAT); 1910 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri", 1911 tp->t_timeout); 1912 splx(s); 1913 if (error == EWOULDBLOCK) 1914 error = EIO; 1915 if (error) 1916 goto out; 1917 goto loop; 1918} 1919 1920/* 1921 * Rubout one character from the rawq of tp 1922 * as cleanly as possible. 1923 */ 1924static void 1925ttyrub(c, tp) 1926 register int c; 1927 register struct tty *tp; 1928{ 1929 register char *cp; 1930 register int savecol; 1931 int tabc, s; 1932 1933 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 1934 return; 1935 CLR(tp->t_lflag, FLUSHO); 1936 if (ISSET(tp->t_lflag, ECHOE)) { 1937 if (tp->t_rocount == 0) { 1938 /* 1939 * Screwed by ttwrite; retype 1940 */ 1941 ttyretype(tp); 1942 return; 1943 } 1944 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 1945 ttyrubo(tp, 2); 1946 else { 1947 CLR(c, ~TTY_CHARMASK); 1948 switch (CCLASS(c)) { 1949 case ORDINARY: 1950 ttyrubo(tp, 1); 1951 break; 1952 case BACKSPACE: 1953 case CONTROL: 1954 case NEWLINE: 1955 case RETURN: 1956 case VTAB: 1957 if (ISSET(tp->t_lflag, ECHOCTL)) 1958 ttyrubo(tp, 2); 1959 break; 1960 case TAB: 1961 if (tp->t_rocount < tp->t_rawq.c_cc) { 1962 ttyretype(tp); 1963 return; 1964 } 1965 s = spltty(); 1966 savecol = tp->t_column; 1967 SET(tp->t_state, TS_CNTTB); 1968 SET(tp->t_lflag, FLUSHO); 1969 tp->t_column = tp->t_rocol; 1970 cp = tp->t_rawq.c_cf; 1971 if (cp) 1972 tabc = *cp; /* XXX FIX NEXTC */ 1973 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 1974 ttyecho(tabc, tp); 1975 CLR(tp->t_lflag, FLUSHO); 1976 CLR(tp->t_state, TS_CNTTB); 1977 splx(s); 1978 1979 /* savecol will now be length of the tab. */ 1980 savecol -= tp->t_column; 1981 tp->t_column += savecol; 1982 if (savecol > 8) 1983 savecol = 8; /* overflow screw */ 1984 while (--savecol >= 0) 1985 (void)ttyoutput('\b', tp); 1986 break; 1987 default: /* XXX */ 1988#define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 1989 (void)printf(PANICSTR, c, CCLASS(c)); 1990#ifdef notdef 1991 panic(PANICSTR, c, CCLASS(c)); 1992#endif 1993 } 1994 } 1995 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 1996 if (!ISSET(tp->t_state, TS_ERASE)) { 1997 SET(tp->t_state, TS_ERASE); 1998 (void)ttyoutput('\\', tp); 1999 } 2000 ttyecho(c, tp); 2001 } else 2002 ttyecho(tp->t_cc[VERASE], tp); 2003 --tp->t_rocount; 2004} 2005 2006/* 2007 * Back over cnt characters, erasing them. 2008 */ 2009static void 2010ttyrubo(tp, cnt) 2011 register struct tty *tp; 2012 int cnt; 2013{ 2014 2015 while (cnt-- > 0) { 2016 (void)ttyoutput('\b', tp); 2017 (void)ttyoutput(' ', tp); 2018 (void)ttyoutput('\b', tp); 2019 } 2020} 2021 2022/* 2023 * ttyretype -- 2024 * Reprint the rawq line. Note, it is assumed that c_cc has already 2025 * been checked. 2026 */ 2027static void 2028ttyretype(tp) 2029 register struct tty *tp; 2030{ 2031 register char *cp; 2032 int s, c; 2033 2034 /* Echo the reprint character. */ 2035 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 2036 ttyecho(tp->t_cc[VREPRINT], tp); 2037 2038 (void)ttyoutput('\n', tp); 2039 2040 /* 2041 * XXX 2042 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 2043 * BIT OF FIRST CHAR. 2044 */ 2045 s = spltty(); 2046 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 2047 cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 2048 ttyecho(c, tp); 2049 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 2050 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 2051 ttyecho(c, tp); 2052 CLR(tp->t_state, TS_ERASE); 2053 splx(s); 2054 2055 tp->t_rocount = tp->t_rawq.c_cc; 2056 tp->t_rocol = 0; 2057} 2058 2059/* 2060 * Echo a typed character to the terminal. 2061 */ 2062static void 2063ttyecho(c, tp) 2064 register int c; 2065 register struct tty *tp; 2066{ 2067 2068 if (!ISSET(tp->t_state, TS_CNTTB)) 2069 CLR(tp->t_lflag, FLUSHO); 2070 if ((!ISSET(tp->t_lflag, ECHO) && 2071 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) || 2072 ISSET(tp->t_lflag, EXTPROC)) 2073 return; 2074 if (ISSET(tp->t_lflag, ECHOCTL) && 2075 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') || 2076 ISSET(c, TTY_CHARMASK) == 0177)) { 2077 (void)ttyoutput('^', tp); 2078 CLR(c, ~TTY_CHARMASK); 2079 if (c == 0177) 2080 c = '?'; 2081 else 2082 c += 'A' - 1; 2083 } 2084 (void)ttyoutput(c, tp); 2085} 2086 2087/* 2088 * Wake up any readers on a tty. 2089 */ 2090void 2091ttwakeup(tp) 2092 register struct tty *tp; 2093{ 2094 2095 if (tp->t_rsel.si_pid != 0) 2096 selwakeup(&tp->t_rsel); 2097 if (ISSET(tp->t_state, TS_ASYNC)) 2098 pgsignal(tp->t_pgrp, SIGIO, 1); 2099 wakeup(TSA_HUP_OR_INPUT(tp)); 2100} 2101 2102/* 2103 * Wake up any writers on a tty. 2104 */ 2105void 2106ttwwakeup(tp) 2107 register struct tty *tp; 2108{ 2109 2110 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_lowat) 2111 selwakeup(&tp->t_wsel); 2112 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == 2113 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { 2114 CLR(tp->t_state, TS_SO_OCOMPLETE); 2115 wakeup(TSA_OCOMPLETE(tp)); 2116 } 2117 if (ISSET(tp->t_state, TS_SO_OLOWAT) && 2118 tp->t_outq.c_cc <= tp->t_lowat) { 2119 CLR(tp->t_state, TS_SO_OLOWAT); 2120 wakeup(TSA_OLOWAT(tp)); 2121 } 2122} 2123 2124/* 2125 * Look up a code for a specified speed in a conversion table; 2126 * used by drivers to map software speed values to hardware parameters. 2127 */ 2128int 2129ttspeedtab(speed, table) 2130 int speed; 2131 register struct speedtab *table; 2132{ 2133 2134 for ( ; table->sp_speed != -1; table++) 2135 if (table->sp_speed == speed) 2136 return (table->sp_code); 2137 return (-1); 2138} 2139 2140/* 2141 * Set tty hi and low water marks. 2142 * 2143 * Try to arrange the dynamics so there's about one second 2144 * from hi to low water. 2145 * 2146 */ 2147void 2148ttsetwater(tp) 2149 struct tty *tp; 2150{ 2151 register int cps, x; 2152 2153#define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 2154 2155 cps = tp->t_ospeed / 10; 2156 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 2157 x += cps; 2158 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT); 2159 tp->t_hiwat = roundup(x, CBSIZE); 2160#undef CLAMP 2161} 2162 2163/* 2164 * Report on state of foreground process group. 2165 */ 2166void 2167ttyinfo(tp) 2168 register struct tty *tp; 2169{ 2170 register struct proc *p, *pick; 2171 struct timeval utime, stime; 2172 int tmp; 2173 2174 if (ttycheckoutq(tp,0) == 0) 2175 return; 2176 2177 /* Print load average. */ 2178 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 2179 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 2180 2181 if (tp->t_session == NULL) 2182 ttyprintf(tp, "not a controlling terminal\n"); 2183 else if (tp->t_pgrp == NULL) 2184 ttyprintf(tp, "no foreground process group\n"); 2185 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0) 2186 ttyprintf(tp, "empty foreground process group\n"); 2187 else { 2188 /* Pick interesting process. */ 2189 for (pick = NULL; p != 0; p = p->p_pglist.le_next) 2190 if (proc_compare(pick, p)) 2191 pick = p; 2192 2193 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 2194 pick->p_stat == SRUN ? "running" : 2195 pick->p_wmesg ? pick->p_wmesg : "iowait"); 2196 2197 calcru(pick, &utime, &stime, NULL); 2198 2199 /* Print user time. */ 2200 ttyprintf(tp, "%d.%02du ", 2201 utime.tv_sec, utime.tv_usec / 10000); 2202 2203 /* Print system time. */ 2204 ttyprintf(tp, "%d.%02ds ", 2205 stime.tv_sec, stime.tv_usec / 10000); 2206 2207#define pgtok(a) (((a) * PAGE_SIZE) / 1024) 2208 /* Print percentage cpu, resident set size. */ 2209 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT; 2210 ttyprintf(tp, "%d%% %dk\n", 2211 tmp / 100, 2212 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 : 2213#ifdef pmap_resident_count 2214 pgtok(pmap_resident_count(&pick->p_vmspace->vm_pmap)) 2215#else 2216 pgtok(pick->p_vmspace->vm_rssize) 2217#endif 2218 ); 2219 } 2220 tp->t_rocount = 0; /* so pending input will be retyped if BS */ 2221} 2222 2223/* 2224 * Returns 1 if p2 is "better" than p1 2225 * 2226 * The algorithm for picking the "interesting" process is thus: 2227 * 2228 * 1) Only foreground processes are eligible - implied. 2229 * 2) Runnable processes are favored over anything else. The runner 2230 * with the highest cpu utilization is picked (p_estcpu). Ties are 2231 * broken by picking the highest pid. 2232 * 3) The sleeper with the shortest sleep time is next. With ties, 2233 * we pick out just "short-term" sleepers (P_SINTR == 0). 2234 * 4) Further ties are broken by picking the highest pid. 2235 */ 2236#define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 2237#define TESTAB(a, b) ((a)<<1 | (b)) 2238#define ONLYA 2 2239#define ONLYB 1 2240#define BOTH 3 2241 2242static int 2243proc_compare(p1, p2) 2244 register struct proc *p1, *p2; 2245{ 2246 2247 if (p1 == NULL) 2248 return (1); 2249 /* 2250 * see if at least one of them is runnable 2251 */ 2252 switch (TESTAB(ISRUN(p1), ISRUN(p2))) { 2253 case ONLYA: 2254 return (0); 2255 case ONLYB: 2256 return (1); 2257 case BOTH: 2258 /* 2259 * tie - favor one with highest recent cpu utilization 2260 */ 2261 if (p2->p_estcpu > p1->p_estcpu) 2262 return (1); 2263 if (p1->p_estcpu > p2->p_estcpu) 2264 return (0); 2265 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2266 } 2267 /* 2268 * weed out zombies 2269 */ 2270 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { 2271 case ONLYA: 2272 return (1); 2273 case ONLYB: 2274 return (0); 2275 case BOTH: 2276 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2277 } 2278 /* 2279 * pick the one with the smallest sleep time 2280 */ 2281 if (p2->p_slptime > p1->p_slptime) 2282 return (0); 2283 if (p1->p_slptime > p2->p_slptime) 2284 return (1); 2285 /* 2286 * favor one sleeping in a non-interruptible sleep 2287 */ 2288 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0) 2289 return (1); 2290 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0) 2291 return (0); 2292 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2293} 2294 2295/* 2296 * Output char to tty; console putchar style. 2297 */ 2298int 2299tputchar(c, tp) 2300 int c; 2301 struct tty *tp; 2302{ 2303 register int s; 2304 2305 s = spltty(); 2306 if (!ISSET(tp->t_state, TS_CONNECTED)) { 2307 splx(s); 2308 return (-1); 2309 } 2310 if (c == '\n') 2311 (void)ttyoutput('\r', tp); 2312 (void)ttyoutput(c, tp); 2313 ttstart(tp); 2314 splx(s); 2315 return (0); 2316} 2317 2318/* 2319 * Sleep on chan, returning ERESTART if tty changed while we napped and 2320 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If 2321 * the tty is revoked, restarting a pending call will redo validation done 2322 * at the start of the call. 2323 */ 2324int 2325ttysleep(tp, chan, pri, wmesg, timo) 2326 struct tty *tp; 2327 void *chan; 2328 int pri, timo; 2329 char *wmesg; 2330{ 2331 int error; 2332 int gen; 2333 2334 gen = tp->t_gen; 2335 error = tsleep(chan, pri, wmesg, timo); 2336 if (error) 2337 return (error); 2338 return (tp->t_gen == gen ? 0 : ERESTART); 2339} 2340 2341#ifdef notyet 2342/* 2343 * XXX this is usable not useful or used. Most tty drivers have 2344 * ifdefs for using ttymalloc() but assume a different interface. 2345 */ 2346/* 2347 * Allocate a tty struct. Clists in the struct will be allocated by 2348 * ttyopen(). 2349 */ 2350struct tty * 2351ttymalloc() 2352{ 2353 struct tty *tp; 2354 2355 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK); 2356 bzero(tp, sizeof *tp); 2357 return (tp); 2358} 2359#endif 2360 2361#if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */ 2362/* 2363 * Free a tty struct. Clists in the struct should have been freed by 2364 * ttyclose(). 2365 */ 2366void 2367ttyfree(tp) 2368 struct tty *tp; 2369{ 2370 free(tp, M_TTYS); 2371} 2372#endif /* 0 */ 2373