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