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