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