1 static volatile int ttyverbose = 0; 2 3 /*- 4 * Copyright (c) 1982, 1986, 1990, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)tty.c 8.8 (Berkeley) 1/21/94 41 * $Id: tty.c,v 1.104 1998/06/07 17:11:41 dfr Exp $ 42 */ 43 44 /*- 45 * TODO: 46 * o Fix races for sending the start char in ttyflush(). 47 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect(). 48 * With luck, there will be MIN chars before select() returns(). 49 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it. 50 * o Don't allow input in TS_ZOMBIE case. It would be visible through 51 * FIONREAD. 52 * o Do the new sio locking stuff here and use it to avoid special 53 * case for EXTPROC? 54 * o Lock PENDIN too? 55 * o Move EXTPROC and/or PENDIN to t_state? 56 * o Wrap most of ttioctl in spltty/splx. 57 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>. 58 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set. 59 * o Don't allow certain termios flags to affect disciplines other 60 * than TTYDISC. Cancel their effects before switch disciplines 61 * and ignore them if they are set while we are in another 62 * discipline. 63 * o Handle c_ispeed = 0 to c_ispeed = c_ospeed conversion here instead 64 * of in drivers and fix drivers that write to tp->t_termios. 65 * o Check for TS_CARR_ON being set while everything is closed and not 66 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open, 67 * so it would live until the next open even if carrier drops. 68 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared 69 * only when _all_ openers leave open(). 70 */ 71 72 #include "snp.h" 73 #include "opt_compat.h" 74 #include "opt_uconsole.h" 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/filio.h> 79 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 80 #include <sys/ioctl_compat.h> 81 #endif 82 #include <sys/proc.h> 83 #define TTYDEFCHARS 84 #include <sys/tty.h> 85 #undef TTYDEFCHARS 86 #include <sys/fcntl.h> 87 #include <sys/conf.h> 88 #include <sys/dkstat.h> 89 #include <sys/poll.h> 90 #include <sys/kernel.h> 91 #include <sys/vnode.h> 92 #include <sys/signalvar.h> 93 #include <sys/resourcevar.h> 94 #include <sys/malloc.h> 95 #if NSNP > 0 96 #include <sys/snoop.h> 97 #endif 98 99 #include <vm/vm.h> 100 #include <sys/lock.h> 101 #include <vm/pmap.h> 102 #include <vm/vm_map.h> 103 104 MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures"); 105 106 static int proc_compare __P((struct proc *p1, struct proc *p2)); 107 static int ttnread __P((struct tty *tp)); 108 static void ttyecho __P((int c, struct tty *tp)); 109 static int ttyoutput __P((int c, register struct tty *tp)); 110 static void ttypend __P((struct tty *tp)); 111 static void ttyretype __P((struct tty *tp)); 112 static void ttyrub __P((int c, struct tty *tp)); 113 static void ttyrubo __P((struct tty *tp, int cnt)); 114 static void ttyunblock __P((struct tty *tp)); 115 static int ttywflush __P((struct tty *tp)); 116 117 /* 118 * Table with character classes and parity. The 8th bit indicates parity, 119 * the 7th bit indicates the character is an alphameric or underscore (for 120 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 121 * are 0 then the character needs no special processing on output; classes 122 * other than 0 might be translated or (not currently) require delays. 123 */ 124 #define E 0x00 /* Even parity. */ 125 #define O 0x80 /* Odd parity. */ 126 #define PARITY(c) (char_type[c] & O) 127 128 #define ALPHA 0x40 /* Alpha or underscore. */ 129 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 130 131 #define CCLASSMASK 0x3f 132 #define CCLASS(c) (char_type[c] & CCLASSMASK) 133 134 #define BS BACKSPACE 135 #define CC CONTROL 136 #define CR RETURN 137 #define NA ORDINARY | ALPHA 138 #define NL NEWLINE 139 #define NO ORDINARY 140 #define TB TAB 141 #define VT VTAB 142 143 static u_char const char_type[] = { 144 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 145 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 146 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 147 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 148 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 149 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 150 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 151 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 152 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 153 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 154 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 155 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 156 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 157 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 158 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 159 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 160 /* 161 * Meta chars; should be settable per character set; 162 * for now, treat them all as normal characters. 163 */ 164 NA, NA, NA, NA, NA, NA, NA, NA, 165 NA, NA, NA, NA, NA, NA, NA, NA, 166 NA, NA, NA, NA, NA, NA, NA, NA, 167 NA, NA, NA, NA, NA, NA, NA, NA, 168 NA, NA, NA, NA, NA, NA, NA, NA, 169 NA, NA, NA, NA, NA, NA, NA, NA, 170 NA, NA, NA, NA, NA, NA, NA, NA, 171 NA, NA, NA, NA, NA, NA, NA, NA, 172 NA, NA, NA, NA, NA, NA, NA, NA, 173 NA, NA, NA, NA, NA, NA, NA, NA, 174 NA, NA, NA, NA, NA, NA, NA, NA, 175 NA, NA, NA, NA, NA, NA, NA, NA, 176 NA, NA, NA, NA, NA, NA, NA, NA, 177 NA, NA, NA, NA, NA, NA, NA, NA, 178 NA, NA, NA, NA, NA, NA, NA, NA, 179 NA, NA, NA, NA, NA, NA, NA, NA, 180 }; 181 #undef BS 182 #undef CC 183 #undef CR 184 #undef NA 185 #undef NL 186 #undef NO 187 #undef TB 188 #undef VT 189 190 /* Macros to clear/set/test flags. */ 191 #define SET(t, f) (t) |= (f) 192 #define CLR(t, f) (t) &= ~(f) 193 #define ISSET(t, f) ((t) & (f)) 194 195 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */ 196 #define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */ 197 198 /* 199 * Initial open of tty, or (re)entry to standard tty line discipline. 200 */ 201 int 202 ttyopen(device, tp) 203 dev_t device; 204 register struct tty *tp; 205 { 206 int s; 207 208 s = spltty(); 209 tp->t_dev = device; 210 if (!ISSET(tp->t_state, TS_ISOPEN)) { 211 SET(tp->t_state, TS_ISOPEN); 212 if (ISSET(tp->t_cflag, CLOCAL)) 213 SET(tp->t_state, TS_CONNECTED); 214 bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 215 } 216 ttsetwater(tp); 217 splx(s); 218 return (0); 219 } 220 221 /* 222 * Handle close() on a tty line: flush and set to initial state, 223 * bumping generation number so that pending read/write calls 224 * can detect recycling of the tty. 225 * XXX our caller should have done `spltty(); l_close(); ttyclose();' 226 * and l_close() should have flushed, but we repeat the spltty() and 227 * the flush in case there are buggy callers. 228 */ 229 int 230 ttyclose(tp) 231 register struct tty *tp; 232 { 233 int s; 234 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 */ 272 int 273 ttyinput(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)) { 332 parmrk: 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) { 542 input_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 } 590 endcase: 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); 597 restartoutput: 598 CLR(tp->t_lflag, FLUSHO); 599 CLR(tp->t_state, TS_TTSTOP); 600 startoutput: 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 */ 610 static int 611 ttyoutput(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 */ 695 int 696 ttioctl(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 TIOCFLUSH: 710 case TIOCSETA: 711 case TIOCSETD: 712 case TIOCSETAF: 713 case TIOCSETAW: 714 #ifdef notdef 715 case TIOCSPGRP: 716 #endif 717 case TIOCSTAT: 718 case TIOCSTI: 719 case TIOCSWINSZ: 720 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 721 case TIOCLBIC: 722 case TIOCLBIS: 723 case TIOCLSET: 724 case TIOCSETC: 725 case OTIOCSETD: 726 case TIOCSETN: 727 case TIOCSETP: 728 case TIOCSLTC: 729 #endif 730 while (isbackground(p, tp) && 731 (p->p_flag & P_PPWAIT) == 0 && 732 (p->p_sigignore & sigmask(SIGTTOU)) == 0 && 733 (p->p_sigmask & sigmask(SIGTTOU)) == 0) { 734 if (p->p_pgrp->pg_jobc == 0) 735 return (EIO); 736 pgsignal(p->p_pgrp, SIGTTOU, 1); 737 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1", 738 0); 739 if (error) 740 return (error); 741 } 742 break; 743 } 744 745 switch (cmd) { /* Process the ioctl. */ 746 case FIOASYNC: /* set/clear async i/o */ 747 s = spltty(); 748 if (*(int *)data) 749 SET(tp->t_state, TS_ASYNC); 750 else 751 CLR(tp->t_state, TS_ASYNC); 752 splx(s); 753 break; 754 case FIONBIO: /* set/clear non-blocking i/o */ 755 break; /* XXX: delete. */ 756 case FIONREAD: /* get # bytes to read */ 757 s = spltty(); 758 *(int *)data = ttnread(tp); 759 splx(s); 760 break; 761 case TIOCEXCL: /* set exclusive use of tty */ 762 s = spltty(); 763 SET(tp->t_state, TS_XCLUDE); 764 splx(s); 765 break; 766 case TIOCFLUSH: { /* flush buffers */ 767 register int flags = *(int *)data; 768 769 if (flags == 0) 770 flags = FREAD | FWRITE; 771 else 772 flags &= FREAD | FWRITE; 773 ttyflush(tp, flags); 774 break; 775 } 776 case TIOCCONS: /* become virtual console */ 777 if (*(int *)data) { 778 if (constty && constty != tp && 779 ISSET(constty->t_state, TS_CONNECTED)) 780 return (EBUSY); 781 #ifndef UCONSOLE 782 if (error = suser(p->p_ucred, &p->p_acflag)) 783 return (error); 784 #endif 785 constty = tp; 786 } else if (tp == constty) 787 constty = NULL; 788 break; 789 case TIOCDRAIN: /* wait till output drained */ 790 error = ttywait(tp); 791 if (error) 792 return (error); 793 break; 794 case TIOCGETA: { /* get termios struct */ 795 struct termios *t = (struct termios *)data; 796 797 bcopy(&tp->t_termios, t, sizeof(struct termios)); 798 break; 799 } 800 case TIOCGETD: /* get line discipline */ 801 *(int *)data = tp->t_line; 802 break; 803 case TIOCGWINSZ: /* get window size */ 804 *(struct winsize *)data = tp->t_winsize; 805 break; 806 case TIOCGPGRP: /* get pgrp of tty */ 807 if (!isctty(p, tp)) 808 return (ENOTTY); 809 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 810 break; 811 #ifdef TIOCHPCL 812 case TIOCHPCL: /* hang up on last close */ 813 s = spltty(); 814 SET(tp->t_cflag, HUPCL); 815 splx(s); 816 break; 817 #endif 818 case TIOCNXCL: /* reset exclusive use of tty */ 819 s = spltty(); 820 CLR(tp->t_state, TS_XCLUDE); 821 splx(s); 822 break; 823 case TIOCOUTQ: /* output queue size */ 824 *(int *)data = tp->t_outq.c_cc; 825 break; 826 case TIOCSETA: /* set termios struct */ 827 case TIOCSETAW: /* drain output, set */ 828 case TIOCSETAF: { /* drn out, fls in, set */ 829 register struct termios *t = (struct termios *)data; 830 831 if (t->c_ispeed < 0 || t->c_ospeed < 0) 832 return (EINVAL); 833 s = spltty(); 834 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 835 error = ttywait(tp); 836 if (error) { 837 splx(s); 838 return (error); 839 } 840 if (cmd == TIOCSETAF) 841 ttyflush(tp, FREAD); 842 } 843 if (!ISSET(t->c_cflag, CIGNORE)) { 844 /* 845 * Set device hardware. 846 */ 847 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 848 splx(s); 849 return (error); 850 } 851 if (ISSET(t->c_cflag, CLOCAL) && 852 !ISSET(tp->t_cflag, CLOCAL)) { 853 /* 854 * XXX disconnections would be too hard to 855 * get rid of without this kludge. The only 856 * way to get rid of controlling terminals 857 * is to exit from the session leader. 858 */ 859 CLR(tp->t_state, TS_ZOMBIE); 860 861 wakeup(TSA_CARR_ON(tp)); 862 ttwakeup(tp); 863 ttwwakeup(tp); 864 } 865 if ((ISSET(tp->t_state, TS_CARR_ON) || 866 ISSET(t->c_cflag, CLOCAL)) && 867 !ISSET(tp->t_state, TS_ZOMBIE)) 868 SET(tp->t_state, TS_CONNECTED); 869 else 870 CLR(tp->t_state, TS_CONNECTED); 871 tp->t_cflag = t->c_cflag; 872 tp->t_ispeed = t->c_ispeed; 873 tp->t_ospeed = t->c_ospeed; 874 ttsetwater(tp); 875 } 876 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) && 877 cmd != TIOCSETAF) { 878 if (ISSET(t->c_lflag, ICANON)) 879 SET(tp->t_lflag, PENDIN); 880 else { 881 /* 882 * XXX we really shouldn't allow toggling 883 * ICANON while we're in a non-termios line 884 * discipline. Now we have to worry about 885 * panicing for a null queue. 886 */ 887 if (tp->t_canq.c_cbreserved > 0 && 888 tp->t_rawq.c_cbreserved > 0) { 889 catq(&tp->t_rawq, &tp->t_canq); 890 /* 891 * XXX the queue limits may be 892 * different, so the old queue 893 * swapping method no longer works. 894 */ 895 catq(&tp->t_canq, &tp->t_rawq); 896 } 897 CLR(tp->t_lflag, PENDIN); 898 } 899 ttwakeup(tp); 900 } 901 tp->t_iflag = t->c_iflag; 902 tp->t_oflag = t->c_oflag; 903 /* 904 * Make the EXTPROC bit read only. 905 */ 906 if (ISSET(tp->t_lflag, EXTPROC)) 907 SET(t->c_lflag, EXTPROC); 908 else 909 CLR(t->c_lflag, EXTPROC); 910 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 911 if (t->c_cc[VMIN] != tp->t_cc[VMIN] || 912 t->c_cc[VTIME] != tp->t_cc[VTIME]) 913 ttwakeup(tp); 914 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 915 splx(s); 916 break; 917 } 918 case TIOCSETD: { /* set line discipline */ 919 register int t = *(int *)data; 920 dev_t device = tp->t_dev; 921 922 if ((u_int)t >= nlinesw) 923 return (ENXIO); 924 if (t != tp->t_line) { 925 s = spltty(); 926 (*linesw[tp->t_line].l_close)(tp, flag); 927 error = (*linesw[t].l_open)(device, tp); 928 if (error) { 929 (void)(*linesw[tp->t_line].l_open)(device, tp); 930 splx(s); 931 return (error); 932 } 933 tp->t_line = t; 934 splx(s); 935 } 936 break; 937 } 938 case TIOCSTART: /* start output, like ^Q */ 939 s = spltty(); 940 if (ISSET(tp->t_state, TS_TTSTOP) || 941 ISSET(tp->t_lflag, FLUSHO)) { 942 CLR(tp->t_lflag, FLUSHO); 943 CLR(tp->t_state, TS_TTSTOP); 944 ttstart(tp); 945 } 946 splx(s); 947 break; 948 case TIOCSTI: /* simulate terminal input */ 949 if (p->p_ucred->cr_uid && (flag & FREAD) == 0) 950 return (EPERM); 951 if (p->p_ucred->cr_uid && !isctty(p, tp)) 952 return (EACCES); 953 s = spltty(); 954 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 955 splx(s); 956 break; 957 case TIOCSTOP: /* stop output, like ^S */ 958 s = spltty(); 959 if (!ISSET(tp->t_state, TS_TTSTOP)) { 960 SET(tp->t_state, TS_TTSTOP); 961 #ifdef sun4c /* XXX */ 962 (*tp->t_stop)(tp, 0); 963 #else 964 (*cdevsw[major(tp->t_dev)]->d_stop)(tp, 0); 965 #endif 966 } 967 splx(s); 968 break; 969 case TIOCSCTTY: /* become controlling tty */ 970 /* Session ctty vnode pointer set in vnode layer. */ 971 if (!SESS_LEADER(p) || 972 ((p->p_session->s_ttyvp || tp->t_session) && 973 (tp->t_session != p->p_session))) 974 return (EPERM); 975 tp->t_session = p->p_session; 976 tp->t_pgrp = p->p_pgrp; 977 p->p_session->s_ttyp = tp; 978 p->p_flag |= P_CONTROLT; 979 break; 980 case TIOCSPGRP: { /* set pgrp of tty */ 981 register struct pgrp *pgrp = pgfind(*(int *)data); 982 983 if (!isctty(p, tp)) 984 return (ENOTTY); 985 else if (pgrp == NULL || pgrp->pg_session != p->p_session) 986 return (EPERM); 987 tp->t_pgrp = pgrp; 988 break; 989 } 990 case TIOCSTAT: /* simulate control-T */ 991 s = spltty(); 992 ttyinfo(tp); 993 splx(s); 994 break; 995 case TIOCSWINSZ: /* set window size */ 996 if (bcmp((caddr_t)&tp->t_winsize, data, 997 sizeof (struct winsize))) { 998 tp->t_winsize = *(struct winsize *)data; 999 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1000 } 1001 break; 1002 case TIOCSDRAINWAIT: 1003 error = suser(p->p_ucred, &p->p_acflag); 1004 if (error) 1005 return (error); 1006 tp->t_timeout = *(int *)data * hz; 1007 wakeup(TSA_OCOMPLETE(tp)); 1008 wakeup(TSA_OLOWAT(tp)); 1009 break; 1010 case TIOCGDRAINWAIT: 1011 *(int *)data = tp->t_timeout / hz; 1012 break; 1013 default: 1014 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1015 return (ttcompat(tp, cmd, data, flag)); 1016 #else 1017 return (ENOIOCTL); 1018 #endif 1019 } 1020 return (0); 1021 } 1022 1023 int 1024 ttypoll(tp, events, p) 1025 struct tty *tp; 1026 int events; 1027 struct proc *p; 1028 { 1029 int s; 1030 int revents = 0; 1031 1032 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */ 1033 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) 1034 | POLLHUP); 1035 1036 s = spltty(); 1037 if (events & (POLLIN | POLLRDNORM)) 1038 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) 1039 revents |= events & (POLLIN | POLLRDNORM); 1040 else 1041 selrecord(p, &tp->t_rsel); 1042 1043 if (events & (POLLOUT | POLLWRNORM)) 1044 if ((tp->t_outq.c_cc <= tp->t_olowat && 1045 ISSET(tp->t_state, TS_CONNECTED)) 1046 || ISSET(tp->t_state, TS_ZOMBIE)) 1047 revents |= events & (POLLOUT | POLLWRNORM); 1048 else 1049 selrecord(p, &tp->t_wsel); 1050 splx(s); 1051 return (revents); 1052 } 1053 1054 /* 1055 * This is a wrapper for compatibility with the select vector used by 1056 * cdevsw. It relies on a proper xxxdevtotty routine. 1057 */ 1058 int 1059 ttpoll(dev, events, p) 1060 dev_t dev; 1061 int events; 1062 struct proc *p; 1063 { 1064 return ttypoll((*cdevsw[major(dev)]->d_devtotty)(dev), events, p); 1065 } 1066 1067 /* 1068 * Must be called at spltty(). 1069 */ 1070 static int 1071 ttnread(tp) 1072 struct tty *tp; 1073 { 1074 int nread; 1075 1076 if (ISSET(tp->t_lflag, PENDIN)) 1077 ttypend(tp); 1078 nread = tp->t_canq.c_cc; 1079 if (!ISSET(tp->t_lflag, ICANON)) { 1080 nread += tp->t_rawq.c_cc; 1081 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) 1082 nread = 0; 1083 } 1084 return (nread); 1085 } 1086 1087 /* 1088 * Wait for output to drain. 1089 */ 1090 int 1091 ttywait(tp) 1092 register struct tty *tp; 1093 { 1094 int error, s; 1095 1096 error = 0; 1097 s = spltty(); 1098 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1099 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) { 1100 (*tp->t_oproc)(tp); 1101 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1102 ISSET(tp->t_state, TS_CONNECTED)) { 1103 SET(tp->t_state, TS_SO_OCOMPLETE); 1104 error = ttysleep(tp, TSA_OCOMPLETE(tp), 1105 TTOPRI | PCATCH, "ttywai", 1106 tp->t_timeout); 1107 if (error) { 1108 if (error == EWOULDBLOCK) 1109 error = EIO; 1110 break; 1111 } 1112 } else 1113 break; 1114 } 1115 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY))) 1116 error = EIO; 1117 splx(s); 1118 return (error); 1119 } 1120 1121 /* 1122 * Flush if successfully wait. 1123 */ 1124 static int 1125 ttywflush(tp) 1126 struct tty *tp; 1127 { 1128 int error; 1129 1130 if ((error = ttywait(tp)) == 0) 1131 ttyflush(tp, FREAD); 1132 return (error); 1133 } 1134 1135 /* 1136 * Flush tty read and/or write queues, notifying anyone waiting. 1137 */ 1138 void 1139 ttyflush(tp, rw) 1140 register struct tty *tp; 1141 int rw; 1142 { 1143 register int s; 1144 1145 s = spltty(); 1146 #if 0 1147 again: 1148 #endif 1149 if (rw & FWRITE) { 1150 FLUSHQ(&tp->t_outq); 1151 CLR(tp->t_state, TS_TTSTOP); 1152 } 1153 #ifdef sun4c /* XXX */ 1154 (*tp->t_stop)(tp, rw); 1155 #else 1156 (*cdevsw[major(tp->t_dev)]->d_stop)(tp, rw); 1157 #endif 1158 if (rw & FREAD) { 1159 FLUSHQ(&tp->t_canq); 1160 FLUSHQ(&tp->t_rawq); 1161 CLR(tp->t_lflag, PENDIN); 1162 tp->t_rocount = 0; 1163 tp->t_rocol = 0; 1164 CLR(tp->t_state, TS_LOCAL); 1165 ttwakeup(tp); 1166 if (ISSET(tp->t_state, TS_TBLOCK)) { 1167 if (rw & FWRITE) 1168 FLUSHQ(&tp->t_outq); 1169 ttyunblock(tp); 1170 1171 /* 1172 * Don't let leave any state that might clobber the 1173 * next line discipline (although we should do more 1174 * to send the START char). Not clearing the state 1175 * may have caused the "putc to a clist with no 1176 * reserved cblocks" panic/printf. 1177 */ 1178 CLR(tp->t_state, TS_TBLOCK); 1179 1180 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */ 1181 if (ISSET(tp->t_iflag, IXOFF)) { 1182 /* 1183 * XXX wait a bit in the hope that the stop 1184 * character (if any) will go out. Waiting 1185 * isn't good since it allows races. This 1186 * will be fixed when the stop character is 1187 * put in a special queue. Don't bother with 1188 * the checks in ttywait() since the timeout 1189 * will save us. 1190 */ 1191 SET(tp->t_state, TS_SO_OCOMPLETE); 1192 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI, 1193 "ttyfls", hz / 10); 1194 /* 1195 * Don't try sending the stop character again. 1196 */ 1197 CLR(tp->t_state, TS_TBLOCK); 1198 goto again; 1199 } 1200 #endif 1201 } 1202 } 1203 if (rw & FWRITE) { 1204 FLUSHQ(&tp->t_outq); 1205 ttwwakeup(tp); 1206 } 1207 splx(s); 1208 } 1209 1210 /* 1211 * Copy in the default termios characters. 1212 */ 1213 void 1214 termioschars(t) 1215 struct termios *t; 1216 { 1217 1218 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc); 1219 } 1220 1221 /* 1222 * Old interface. 1223 */ 1224 void 1225 ttychars(tp) 1226 struct tty *tp; 1227 { 1228 1229 termioschars(&tp->t_termios); 1230 } 1231 1232 /* 1233 * Handle input high water. Send stop character for the IXOFF case. Turn 1234 * on our input flow control bit and propagate the changes to the driver. 1235 * XXX the stop character should be put in a special high priority queue. 1236 */ 1237 void 1238 ttyblock(tp) 1239 struct tty *tp; 1240 { 1241 1242 SET(tp->t_state, TS_TBLOCK); 1243 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1244 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0) 1245 CLR(tp->t_state, TS_TBLOCK); /* try again later */ 1246 ttstart(tp); 1247 } 1248 1249 /* 1250 * Handle input low water. Send start character for the IXOFF case. Turn 1251 * off our input flow control bit and propagate the changes to the driver. 1252 * XXX the start character should be put in a special high priority queue. 1253 */ 1254 static void 1255 ttyunblock(tp) 1256 struct tty *tp; 1257 { 1258 1259 CLR(tp->t_state, TS_TBLOCK); 1260 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE && 1261 putc(tp->t_cc[VSTART], &tp->t_outq) != 0) 1262 SET(tp->t_state, TS_TBLOCK); /* try again later */ 1263 ttstart(tp); 1264 } 1265 1266 #ifdef notyet 1267 /* Not used by any current (i386) drivers. */ 1268 /* 1269 * Restart after an inter-char delay. 1270 */ 1271 void 1272 ttrstrt(tp_arg) 1273 void *tp_arg; 1274 { 1275 struct tty *tp; 1276 int s; 1277 1278 #ifdef DIAGNOSTIC 1279 if (tp_arg == NULL) 1280 panic("ttrstrt"); 1281 #endif 1282 tp = tp_arg; 1283 s = spltty(); 1284 1285 CLR(tp->t_state, TS_TIMEOUT); 1286 ttstart(tp); 1287 1288 splx(s); 1289 } 1290 #endif 1291 1292 int 1293 ttstart(tp) 1294 struct tty *tp; 1295 { 1296 1297 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1298 (*tp->t_oproc)(tp); 1299 return (0); 1300 } 1301 1302 /* 1303 * "close" a line discipline 1304 */ 1305 int 1306 ttylclose(tp, flag) 1307 struct tty *tp; 1308 int flag; 1309 { 1310 1311 if (flag & FNONBLOCK || ttywflush(tp)) 1312 ttyflush(tp, FREAD | FWRITE); 1313 return (0); 1314 } 1315 1316 /* 1317 * Handle modem control transition on a tty. 1318 * Flag indicates new state of carrier. 1319 * Returns 0 if the line should be turned off, otherwise 1. 1320 */ 1321 int 1322 ttymodem(tp, flag) 1323 register struct tty *tp; 1324 int flag; 1325 { 1326 1327 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) { 1328 /* 1329 * MDMBUF: do flow control according to carrier flag 1330 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP 1331 * works if IXON and IXANY are clear. 1332 */ 1333 if (flag) { 1334 CLR(tp->t_state, TS_CAR_OFLOW); 1335 CLR(tp->t_state, TS_TTSTOP); 1336 ttstart(tp); 1337 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) { 1338 SET(tp->t_state, TS_CAR_OFLOW); 1339 SET(tp->t_state, TS_TTSTOP); 1340 #ifdef sun4c /* XXX */ 1341 (*tp->t_stop)(tp, 0); 1342 #else 1343 (*cdevsw[major(tp->t_dev)]->d_stop)(tp, 0); 1344 #endif 1345 } 1346 } else if (flag == 0) { 1347 /* 1348 * Lost carrier. 1349 */ 1350 CLR(tp->t_state, TS_CARR_ON); 1351 if (ISSET(tp->t_state, TS_ISOPEN) && 1352 !ISSET(tp->t_cflag, CLOCAL)) { 1353 SET(tp->t_state, TS_ZOMBIE); 1354 CLR(tp->t_state, TS_CONNECTED); 1355 if (tp->t_session && tp->t_session->s_leader) 1356 psignal(tp->t_session->s_leader, SIGHUP); 1357 ttyflush(tp, FREAD | FWRITE); 1358 return (0); 1359 } 1360 } else { 1361 /* 1362 * Carrier now on. 1363 */ 1364 SET(tp->t_state, TS_CARR_ON); 1365 if (!ISSET(tp->t_state, TS_ZOMBIE)) 1366 SET(tp->t_state, TS_CONNECTED); 1367 wakeup(TSA_CARR_ON(tp)); 1368 ttwakeup(tp); 1369 ttwwakeup(tp); 1370 } 1371 return (1); 1372 } 1373 1374 /* 1375 * Reinput pending characters after state switch 1376 * call at spltty(). 1377 */ 1378 static void 1379 ttypend(tp) 1380 register struct tty *tp; 1381 { 1382 struct clist tq; 1383 register int c; 1384 1385 CLR(tp->t_lflag, PENDIN); 1386 SET(tp->t_state, TS_TYPEN); 1387 /* 1388 * XXX this assumes too much about clist internals. It may even 1389 * fail if the cblock slush pool is empty. We can't allocate more 1390 * cblocks here because we are called from an interrupt handler 1391 * and clist_alloc_cblocks() can wait. 1392 */ 1393 tq = tp->t_rawq; 1394 bzero(&tp->t_rawq, sizeof tp->t_rawq); 1395 tp->t_rawq.c_cbmax = tq.c_cbmax; 1396 tp->t_rawq.c_cbreserved = tq.c_cbreserved; 1397 while ((c = getc(&tq)) >= 0) 1398 ttyinput(c, tp); 1399 CLR(tp->t_state, TS_TYPEN); 1400 } 1401 1402 /* 1403 * Process a read call on a tty device. 1404 */ 1405 int 1406 ttread(tp, uio, flag) 1407 register struct tty *tp; 1408 struct uio *uio; 1409 int flag; 1410 { 1411 register struct clist *qp; 1412 register int c; 1413 register tcflag_t lflag; 1414 register cc_t *cc = tp->t_cc; 1415 register struct proc *p = curproc; 1416 int s, first, error = 0; 1417 int has_stime = 0, last_cc = 0; 1418 long slp = 0; /* XXX this should be renamed `timo'. */ 1419 1420 loop: 1421 s = spltty(); 1422 lflag = tp->t_lflag; 1423 /* 1424 * take pending input first 1425 */ 1426 if (ISSET(lflag, PENDIN)) { 1427 ttypend(tp); 1428 splx(s); /* reduce latency */ 1429 s = spltty(); 1430 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */ 1431 } 1432 1433 /* 1434 * Hang process if it's in the background. 1435 */ 1436 if (isbackground(p, tp)) { 1437 splx(s); 1438 if ((p->p_sigignore & sigmask(SIGTTIN)) || 1439 (p->p_sigmask & sigmask(SIGTTIN)) || 1440 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0) 1441 return (EIO); 1442 pgsignal(p->p_pgrp, SIGTTIN, 1); 1443 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0); 1444 if (error) 1445 return (error); 1446 goto loop; 1447 } 1448 1449 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1450 splx(s); 1451 return (0); /* EOF */ 1452 } 1453 1454 /* 1455 * If canonical, use the canonical queue, 1456 * else use the raw queue. 1457 * 1458 * (should get rid of clists...) 1459 */ 1460 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 1461 1462 if (flag & IO_NDELAY) { 1463 if (qp->c_cc > 0) 1464 goto read; 1465 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) { 1466 splx(s); 1467 return (0); 1468 } 1469 splx(s); 1470 return (EWOULDBLOCK); 1471 } 1472 if (!ISSET(lflag, ICANON)) { 1473 int m = cc[VMIN]; 1474 long t = cc[VTIME]; 1475 struct timeval stime, timecopy; 1476 1477 /* 1478 * Check each of the four combinations. 1479 * (m > 0 && t == 0) is the normal read case. 1480 * It should be fairly efficient, so we check that and its 1481 * companion case (m == 0 && t == 0) first. 1482 * For the other two cases, we compute the target sleep time 1483 * into slp. 1484 */ 1485 if (t == 0) { 1486 if (qp->c_cc < m) 1487 goto sleep; 1488 if (qp->c_cc > 0) 1489 goto read; 1490 1491 /* m, t and qp->c_cc are all 0. 0 is enough input. */ 1492 splx(s); 1493 return (0); 1494 } 1495 t *= 100000; /* time in us */ 1496 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1497 ((t1).tv_usec - (t2).tv_usec)) 1498 if (m > 0) { 1499 if (qp->c_cc <= 0) 1500 goto sleep; 1501 if (qp->c_cc >= m) 1502 goto read; 1503 getmicrotime(&timecopy); 1504 if (!has_stime) { 1505 /* first character, start timer */ 1506 has_stime = 1; 1507 stime = timecopy; 1508 slp = t; 1509 } else if (qp->c_cc > last_cc) { 1510 /* got a character, restart timer */ 1511 stime = timecopy; 1512 slp = t; 1513 } else { 1514 /* nothing, check expiration */ 1515 slp = t - diff(timecopy, stime); 1516 if (slp <= 0) 1517 goto read; 1518 } 1519 last_cc = qp->c_cc; 1520 } else { /* m == 0 */ 1521 if (qp->c_cc > 0) 1522 goto read; 1523 getmicrotime(&timecopy); 1524 if (!has_stime) { 1525 has_stime = 1; 1526 stime = timecopy; 1527 slp = t; 1528 } else { 1529 slp = t - diff(timecopy, stime); 1530 if (slp <= 0) { 1531 /* Timed out, but 0 is enough input. */ 1532 splx(s); 1533 return (0); 1534 } 1535 } 1536 } 1537 #undef diff 1538 /* 1539 * Rounding down may make us wake up just short 1540 * of the target, so we round up. 1541 * The formula is ceiling(slp * hz/1000000). 1542 * 32-bit arithmetic is enough for hz < 169. 1543 * XXX see tvtohz() for how to avoid overflow if hz 1544 * is large (divide by `tick' and/or arrange to 1545 * use tvtohz() if hz is large). 1546 */ 1547 slp = (long) (((u_long)slp * hz) + 999999) / 1000000; 1548 goto sleep; 1549 } 1550 if (qp->c_cc <= 0) { 1551 sleep: 1552 /* 1553 * There is no input, or not enough input and we can block. 1554 */ 1555 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH, 1556 ISSET(tp->t_state, TS_CONNECTED) ? 1557 "ttyin" : "ttyhup", (int)slp); 1558 splx(s); 1559 if (error == EWOULDBLOCK) 1560 error = 0; 1561 else if (error) 1562 return (error); 1563 /* 1564 * XXX what happens if another process eats some input 1565 * while we are asleep (not just here)? It would be 1566 * safest to detect changes and reset our state variables 1567 * (has_stime and last_cc). 1568 */ 1569 slp = 0; 1570 goto loop; 1571 } 1572 read: 1573 splx(s); 1574 /* 1575 * Input present, check for input mapping and processing. 1576 */ 1577 first = 1; 1578 if (ISSET(lflag, ICANON | ISIG)) 1579 goto slowcase; 1580 for (;;) { 1581 char ibuf[IBUFSIZ]; 1582 int icc; 1583 1584 icc = imin(uio->uio_resid, IBUFSIZ); 1585 icc = q_to_b(qp, ibuf, icc); 1586 if (icc <= 0) { 1587 if (first) 1588 goto loop; 1589 break; 1590 } 1591 error = uiomove(ibuf, icc, uio); 1592 /* 1593 * XXX if there was an error then we should ungetc() the 1594 * unmoved chars and reduce icc here. 1595 */ 1596 #if NSNP > 0 1597 if (ISSET(tp->t_lflag, ECHO) && 1598 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1599 snpin((struct snoop *)tp->t_sc, ibuf, icc); 1600 #endif 1601 if (error) 1602 break; 1603 if (uio->uio_resid == 0) 1604 break; 1605 first = 0; 1606 } 1607 goto out; 1608 slowcase: 1609 for (;;) { 1610 c = getc(qp); 1611 if (c < 0) { 1612 if (first) 1613 goto loop; 1614 break; 1615 } 1616 /* 1617 * delayed suspend (^Y) 1618 */ 1619 if (CCEQ(cc[VDSUSP], c) && 1620 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) { 1621 pgsignal(tp->t_pgrp, SIGTSTP, 1); 1622 if (first) { 1623 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, 1624 "ttybg3", 0); 1625 if (error) 1626 break; 1627 goto loop; 1628 } 1629 break; 1630 } 1631 /* 1632 * Interpret EOF only in canonical mode. 1633 */ 1634 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 1635 break; 1636 /* 1637 * Give user character. 1638 */ 1639 error = ureadc(c, uio); 1640 if (error) 1641 /* XXX should ungetc(c, qp). */ 1642 break; 1643 #if NSNP > 0 1644 /* 1645 * Only snoop directly on input in echo mode. Non-echoed 1646 * input will be snooped later iff the application echoes it. 1647 */ 1648 if (ISSET(tp->t_lflag, ECHO) && 1649 ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1650 snpinc((struct snoop *)tp->t_sc, (char)c); 1651 #endif 1652 if (uio->uio_resid == 0) 1653 break; 1654 /* 1655 * In canonical mode check for a "break character" 1656 * marking the end of a "line of input". 1657 */ 1658 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag)) 1659 break; 1660 first = 0; 1661 } 1662 1663 out: 1664 /* 1665 * Look to unblock input now that (presumably) 1666 * the input queue has gone down. 1667 */ 1668 s = spltty(); 1669 if (ISSET(tp->t_state, TS_TBLOCK) && 1670 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat) 1671 ttyunblock(tp); 1672 splx(s); 1673 1674 return (error); 1675 } 1676 1677 /* 1678 * Check the output queue on tp for space for a kernel message (from uprintf 1679 * or tprintf). Allow some space over the normal hiwater mark so we don't 1680 * lose messages due to normal flow control, but don't let the tty run amok. 1681 * Sleeps here are not interruptible, but we return prematurely if new signals 1682 * arrive. 1683 */ 1684 int 1685 ttycheckoutq(tp, wait) 1686 register struct tty *tp; 1687 int wait; 1688 { 1689 int hiwat, s, oldsig; 1690 1691 hiwat = tp->t_ohiwat; 1692 s = spltty(); 1693 oldsig = wait ? curproc->p_siglist : 0; 1694 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100) 1695 while (tp->t_outq.c_cc > hiwat) { 1696 ttstart(tp); 1697 if (tp->t_outq.c_cc <= hiwat) 1698 break; 1699 if (wait == 0 || curproc->p_siglist != oldsig) { 1700 splx(s); 1701 return (0); 1702 } 1703 SET(tp->t_state, TS_SO_OLOWAT); 1704 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz); 1705 } 1706 splx(s); 1707 return (1); 1708 } 1709 1710 /* 1711 * Process a write call on a tty device. 1712 */ 1713 int 1714 ttwrite(tp, uio, flag) 1715 register struct tty *tp; 1716 register struct uio *uio; 1717 int flag; 1718 { 1719 register char *cp = NULL; 1720 register int cc, ce; 1721 register struct proc *p; 1722 int i, hiwat, cnt, error, s; 1723 char obuf[OBUFSIZ]; 1724 1725 hiwat = tp->t_ohiwat; 1726 cnt = uio->uio_resid; 1727 error = 0; 1728 cc = 0; 1729 loop: 1730 s = spltty(); 1731 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1732 splx(s); 1733 if (uio->uio_resid == cnt) 1734 error = EIO; 1735 goto out; 1736 } 1737 if (!ISSET(tp->t_state, TS_CONNECTED)) { 1738 if (flag & IO_NDELAY) { 1739 splx(s); 1740 error = EWOULDBLOCK; 1741 goto out; 1742 } 1743 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1744 "ttydcd", 0); 1745 splx(s); 1746 if (error) 1747 goto out; 1748 goto loop; 1749 } 1750 splx(s); 1751 /* 1752 * Hang the process if it's in the background. 1753 */ 1754 p = curproc; 1755 if (isbackground(p, tp) && 1756 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 && 1757 (p->p_sigignore & sigmask(SIGTTOU)) == 0 && 1758 (p->p_sigmask & sigmask(SIGTTOU)) == 0) { 1759 if (p->p_pgrp->pg_jobc == 0) { 1760 error = EIO; 1761 goto out; 1762 } 1763 pgsignal(p->p_pgrp, SIGTTOU, 1); 1764 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0); 1765 if (error) 1766 goto out; 1767 goto loop; 1768 } 1769 /* 1770 * Process the user's data in at most OBUFSIZ chunks. Perform any 1771 * output translation. Keep track of high water mark, sleep on 1772 * overflow awaiting device aid in acquiring new space. 1773 */ 1774 while (uio->uio_resid > 0 || cc > 0) { 1775 if (ISSET(tp->t_lflag, FLUSHO)) { 1776 uio->uio_resid = 0; 1777 return (0); 1778 } 1779 if (tp->t_outq.c_cc > hiwat) 1780 goto ovhiwat; 1781 /* 1782 * Grab a hunk of data from the user, unless we have some 1783 * leftover from last time. 1784 */ 1785 if (cc == 0) { 1786 cc = imin(uio->uio_resid, OBUFSIZ); 1787 cp = obuf; 1788 error = uiomove(cp, cc, uio); 1789 if (error) { 1790 cc = 0; 1791 break; 1792 } 1793 #if NSNP > 0 1794 if (ISSET(tp->t_state, TS_SNOOP) && tp->t_sc != NULL) 1795 snpin((struct snoop *)tp->t_sc, cp, cc); 1796 #endif 1797 } 1798 /* 1799 * If nothing fancy need be done, grab those characters we 1800 * can handle without any of ttyoutput's processing and 1801 * just transfer them to the output q. For those chars 1802 * which require special processing (as indicated by the 1803 * bits in char_type), call ttyoutput. After processing 1804 * a hunk of data, look for FLUSHO so ^O's will take effect 1805 * immediately. 1806 */ 1807 while (cc > 0) { 1808 if (!ISSET(tp->t_oflag, OPOST)) 1809 ce = cc; 1810 else { 1811 ce = cc - scanc((u_int)cc, (u_char *)cp, 1812 char_type, CCLASSMASK); 1813 /* 1814 * If ce is zero, then we're processing 1815 * a special character through ttyoutput. 1816 */ 1817 if (ce == 0) { 1818 tp->t_rocount = 0; 1819 if (ttyoutput(*cp, tp) >= 0) { 1820 /* No Clists, wait a bit. */ 1821 ttstart(tp); 1822 if (flag & IO_NDELAY) { 1823 error = EWOULDBLOCK; 1824 goto out; 1825 } 1826 error = ttysleep(tp, &lbolt, 1827 TTOPRI|PCATCH, 1828 "ttybf1", 0); 1829 if (error) 1830 goto out; 1831 goto loop; 1832 } 1833 cp++; 1834 cc--; 1835 if (ISSET(tp->t_lflag, FLUSHO) || 1836 tp->t_outq.c_cc > hiwat) 1837 goto ovhiwat; 1838 continue; 1839 } 1840 } 1841 /* 1842 * A bunch of normal characters have been found. 1843 * Transfer them en masse to the output queue and 1844 * continue processing at the top of the loop. 1845 * If there are any further characters in this 1846 * <= OBUFSIZ chunk, the first should be a character 1847 * requiring special handling by ttyoutput. 1848 */ 1849 tp->t_rocount = 0; 1850 i = b_to_q(cp, ce, &tp->t_outq); 1851 ce -= i; 1852 tp->t_column += ce; 1853 cp += ce, cc -= ce, tk_nout += ce; 1854 tp->t_outcc += ce; 1855 if (i > 0) { 1856 /* No Clists, wait a bit. */ 1857 ttstart(tp); 1858 if (flag & IO_NDELAY) { 1859 error = EWOULDBLOCK; 1860 goto out; 1861 } 1862 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, 1863 "ttybf2", 0); 1864 if (error) 1865 goto out; 1866 goto loop; 1867 } 1868 if (ISSET(tp->t_lflag, FLUSHO) || 1869 tp->t_outq.c_cc > hiwat) 1870 break; 1871 } 1872 ttstart(tp); 1873 } 1874 out: 1875 /* 1876 * If cc is nonzero, we leave the uio structure inconsistent, as the 1877 * offset and iov pointers have moved forward, but it doesn't matter 1878 * (the call will either return short or restart with a new uio). 1879 */ 1880 uio->uio_resid += cc; 1881 return (error); 1882 1883 ovhiwat: 1884 ttstart(tp); 1885 s = spltty(); 1886 /* 1887 * This can only occur if FLUSHO is set in t_lflag, 1888 * or if ttstart/oproc is synchronous (or very fast). 1889 */ 1890 if (tp->t_outq.c_cc <= hiwat) { 1891 splx(s); 1892 goto loop; 1893 } 1894 if (flag & IO_NDELAY) { 1895 splx(s); 1896 uio->uio_resid += cc; 1897 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 1898 } 1899 SET(tp->t_state, TS_SO_OLOWAT); 1900 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri", 1901 tp->t_timeout); 1902 splx(s); 1903 if (error == EWOULDBLOCK) 1904 error = EIO; 1905 if (error) 1906 goto out; 1907 goto loop; 1908 } 1909 1910 /* 1911 * Rubout one character from the rawq of tp 1912 * as cleanly as possible. 1913 */ 1914 static void 1915 ttyrub(c, tp) 1916 register int c; 1917 register struct tty *tp; 1918 { 1919 register char *cp; 1920 register int savecol; 1921 int tabc, s; 1922 1923 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 1924 return; 1925 CLR(tp->t_lflag, FLUSHO); 1926 if (ISSET(tp->t_lflag, ECHOE)) { 1927 if (tp->t_rocount == 0) { 1928 /* 1929 * Screwed by ttwrite; retype 1930 */ 1931 ttyretype(tp); 1932 return; 1933 } 1934 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 1935 ttyrubo(tp, 2); 1936 else { 1937 CLR(c, ~TTY_CHARMASK); 1938 switch (CCLASS(c)) { 1939 case ORDINARY: 1940 ttyrubo(tp, 1); 1941 break; 1942 case BACKSPACE: 1943 case CONTROL: 1944 case NEWLINE: 1945 case RETURN: 1946 case VTAB: 1947 if (ISSET(tp->t_lflag, ECHOCTL)) 1948 ttyrubo(tp, 2); 1949 break; 1950 case TAB: 1951 if (tp->t_rocount < tp->t_rawq.c_cc) { 1952 ttyretype(tp); 1953 return; 1954 } 1955 s = spltty(); 1956 savecol = tp->t_column; 1957 SET(tp->t_state, TS_CNTTB); 1958 SET(tp->t_lflag, FLUSHO); 1959 tp->t_column = tp->t_rocol; 1960 cp = tp->t_rawq.c_cf; 1961 if (cp) 1962 tabc = *cp; /* XXX FIX NEXTC */ 1963 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 1964 ttyecho(tabc, tp); 1965 CLR(tp->t_lflag, FLUSHO); 1966 CLR(tp->t_state, TS_CNTTB); 1967 splx(s); 1968 1969 /* savecol will now be length of the tab. */ 1970 savecol -= tp->t_column; 1971 tp->t_column += savecol; 1972 if (savecol > 8) 1973 savecol = 8; /* overflow screw */ 1974 while (--savecol >= 0) 1975 (void)ttyoutput('\b', tp); 1976 break; 1977 default: /* XXX */ 1978 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 1979 (void)printf(PANICSTR, c, CCLASS(c)); 1980 #ifdef notdef 1981 panic(PANICSTR, c, CCLASS(c)); 1982 #endif 1983 } 1984 } 1985 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 1986 if (!ISSET(tp->t_state, TS_ERASE)) { 1987 SET(tp->t_state, TS_ERASE); 1988 (void)ttyoutput('\\', tp); 1989 } 1990 ttyecho(c, tp); 1991 } else 1992 ttyecho(tp->t_cc[VERASE], tp); 1993 --tp->t_rocount; 1994 } 1995 1996 /* 1997 * Back over cnt characters, erasing them. 1998 */ 1999 static void 2000 ttyrubo(tp, cnt) 2001 register struct tty *tp; 2002 int cnt; 2003 { 2004 2005 while (cnt-- > 0) { 2006 (void)ttyoutput('\b', tp); 2007 (void)ttyoutput(' ', tp); 2008 (void)ttyoutput('\b', tp); 2009 } 2010 } 2011 2012 /* 2013 * ttyretype -- 2014 * Reprint the rawq line. Note, it is assumed that c_cc has already 2015 * been checked. 2016 */ 2017 static void 2018 ttyretype(tp) 2019 register struct tty *tp; 2020 { 2021 register char *cp; 2022 int s, c; 2023 2024 /* Echo the reprint character. */ 2025 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 2026 ttyecho(tp->t_cc[VREPRINT], tp); 2027 2028 (void)ttyoutput('\n', tp); 2029 2030 /* 2031 * XXX 2032 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 2033 * BIT OF FIRST CHAR. 2034 */ 2035 s = spltty(); 2036 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 2037 cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 2038 ttyecho(c, tp); 2039 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 2040 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 2041 ttyecho(c, tp); 2042 CLR(tp->t_state, TS_ERASE); 2043 splx(s); 2044 2045 tp->t_rocount = tp->t_rawq.c_cc; 2046 tp->t_rocol = 0; 2047 } 2048 2049 /* 2050 * Echo a typed character to the terminal. 2051 */ 2052 static void 2053 ttyecho(c, tp) 2054 register int c; 2055 register struct tty *tp; 2056 { 2057 2058 if (!ISSET(tp->t_state, TS_CNTTB)) 2059 CLR(tp->t_lflag, FLUSHO); 2060 if ((!ISSET(tp->t_lflag, ECHO) && 2061 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) || 2062 ISSET(tp->t_lflag, EXTPROC)) 2063 return; 2064 if (ISSET(tp->t_lflag, ECHOCTL) && 2065 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') || 2066 ISSET(c, TTY_CHARMASK) == 0177)) { 2067 (void)ttyoutput('^', tp); 2068 CLR(c, ~TTY_CHARMASK); 2069 if (c == 0177) 2070 c = '?'; 2071 else 2072 c += 'A' - 1; 2073 } 2074 (void)ttyoutput(c, tp); 2075 } 2076 2077 /* 2078 * Wake up any readers on a tty. 2079 */ 2080 void 2081 ttwakeup(tp) 2082 register struct tty *tp; 2083 { 2084 2085 if (tp->t_rsel.si_pid != 0) 2086 selwakeup(&tp->t_rsel); 2087 if (ISSET(tp->t_state, TS_ASYNC)) 2088 pgsignal(tp->t_pgrp, SIGIO, 1); 2089 wakeup(TSA_HUP_OR_INPUT(tp)); 2090 } 2091 2092 /* 2093 * Wake up any writers on a tty. 2094 */ 2095 void 2096 ttwwakeup(tp) 2097 register struct tty *tp; 2098 { 2099 2100 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat) 2101 selwakeup(&tp->t_wsel); 2102 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == 2103 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { 2104 CLR(tp->t_state, TS_SO_OCOMPLETE); 2105 wakeup(TSA_OCOMPLETE(tp)); 2106 } 2107 if (ISSET(tp->t_state, TS_SO_OLOWAT) && 2108 tp->t_outq.c_cc <= tp->t_olowat) { 2109 CLR(tp->t_state, TS_SO_OLOWAT); 2110 wakeup(TSA_OLOWAT(tp)); 2111 } 2112 } 2113 2114 /* 2115 * Look up a code for a specified speed in a conversion table; 2116 * used by drivers to map software speed values to hardware parameters. 2117 */ 2118 int 2119 ttspeedtab(speed, table) 2120 int speed; 2121 register struct speedtab *table; 2122 { 2123 2124 for ( ; table->sp_speed != -1; table++) 2125 if (table->sp_speed == speed) 2126 return (table->sp_code); 2127 return (-1); 2128 } 2129 2130 /* 2131 * Set input and output watermarks and buffer sizes. For input, the 2132 * high watermark is about one second's worth of input above empty, the 2133 * low watermark is slightly below high water, and the buffer size is a 2134 * driver-dependent amount above high water. For output, the watermarks 2135 * are near the ends of the buffer, with about 1 second's worth of input 2136 * between them. All this only applies to the standard line discipline. 2137 */ 2138 void 2139 ttsetwater(tp) 2140 struct tty *tp; 2141 { 2142 register int cps, ttmaxhiwat, x; 2143 2144 /* Input. */ 2145 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512); 2146 if (ttyverbose) 2147 printf("ttsetwater: can: %d, ", TTYHOG); 2148 switch (tp->t_ispeedwat) { 2149 case (speed_t)-1: 2150 cps = tp->t_ispeed / 10; 2151 break; 2152 case 0: 2153 /* 2154 * This case is for old drivers that don't know about 2155 * t_ispeedwat. Arrange for them to get the old buffer 2156 * sizes and watermarks. 2157 */ 2158 cps = TTYHOG - 2 * 256; 2159 tp->t_ififosize = 2 * 256; 2160 break; 2161 default: 2162 cps = tp->t_ispeedwat / 10; 2163 break; 2164 } 2165 tp->t_ihiwat = cps; 2166 tp->t_ilowat = 7 * cps / 8; 2167 x = cps + tp->t_ififosize; 2168 clist_alloc_cblocks(&tp->t_rawq, x, x); 2169 if (ttyverbose) 2170 printf("raw: %d, ", x); 2171 2172 /* Output. */ 2173 switch (tp->t_ospeedwat) { 2174 case (speed_t)-1: 2175 cps = tp->t_ospeed / 10; 2176 ttmaxhiwat = 200000; 2177 break; 2178 case 0: 2179 cps = tp->t_ospeed / 10; 2180 ttmaxhiwat = TTMAXHIWAT; 2181 break; 2182 default: 2183 cps = tp->t_ospeedwat / 10; 2184 ttmaxhiwat = 200000; 2185 break; 2186 } 2187 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 2188 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 2189 x += cps; 2190 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */ 2191 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */ 2192 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */ 2193 x += OBUFSIZ + 100; 2194 clist_alloc_cblocks(&tp->t_outq, x, x); 2195 if (ttyverbose) 2196 printf("out: %d\n", x); 2197 #undef CLAMP 2198 } 2199 2200 /* 2201 * Report on state of foreground process group. 2202 */ 2203 void 2204 ttyinfo(tp) 2205 register struct tty *tp; 2206 { 2207 register struct proc *p, *pick; 2208 struct timeval utime, stime; 2209 int tmp; 2210 2211 if (ttycheckoutq(tp,0) == 0) 2212 return; 2213 2214 /* Print load average. */ 2215 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 2216 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 2217 2218 if (tp->t_session == NULL) 2219 ttyprintf(tp, "not a controlling terminal\n"); 2220 else if (tp->t_pgrp == NULL) 2221 ttyprintf(tp, "no foreground process group\n"); 2222 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0) 2223 ttyprintf(tp, "empty foreground process group\n"); 2224 else { 2225 /* Pick interesting process. */ 2226 for (pick = NULL; p != 0; p = p->p_pglist.le_next) 2227 if (proc_compare(pick, p)) 2228 pick = p; 2229 2230 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 2231 pick->p_stat == SRUN ? "running" : 2232 pick->p_wmesg ? pick->p_wmesg : "iowait"); 2233 2234 calcru(pick, &utime, &stime, NULL); 2235 2236 /* Print user time. */ 2237 ttyprintf(tp, "%ld.%02ldu ", 2238 utime.tv_sec, utime.tv_usec / 10000); 2239 2240 /* Print system time. */ 2241 ttyprintf(tp, "%ld.%02lds ", 2242 stime.tv_sec, stime.tv_usec / 10000); 2243 2244 #define pgtok(a) (((a) * PAGE_SIZE) / 1024) 2245 /* Print percentage cpu, resident set size. */ 2246 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT; 2247 ttyprintf(tp, "%d%% %ldk\n", 2248 tmp / 100, 2249 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 : 2250 #ifdef pmap_resident_count 2251 (long)pgtok(pmap_resident_count(&pick->p_vmspace->vm_pmap)) 2252 #else 2253 (long)pgtok(pick->p_vmspace->vm_rssize) 2254 #endif 2255 ); 2256 } 2257 tp->t_rocount = 0; /* so pending input will be retyped if BS */ 2258 } 2259 2260 /* 2261 * Returns 1 if p2 is "better" than p1 2262 * 2263 * The algorithm for picking the "interesting" process is thus: 2264 * 2265 * 1) Only foreground processes are eligible - implied. 2266 * 2) Runnable processes are favored over anything else. The runner 2267 * with the highest cpu utilization is picked (p_estcpu). Ties are 2268 * broken by picking the highest pid. 2269 * 3) The sleeper with the shortest sleep time is next. With ties, 2270 * we pick out just "short-term" sleepers (P_SINTR == 0). 2271 * 4) Further ties are broken by picking the highest pid. 2272 */ 2273 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 2274 #define TESTAB(a, b) ((a)<<1 | (b)) 2275 #define ONLYA 2 2276 #define ONLYB 1 2277 #define BOTH 3 2278 2279 static int 2280 proc_compare(p1, p2) 2281 register struct proc *p1, *p2; 2282 { 2283 2284 if (p1 == NULL) 2285 return (1); 2286 /* 2287 * see if at least one of them is runnable 2288 */ 2289 switch (TESTAB(ISRUN(p1), ISRUN(p2))) { 2290 case ONLYA: 2291 return (0); 2292 case ONLYB: 2293 return (1); 2294 case BOTH: 2295 /* 2296 * tie - favor one with highest recent cpu utilization 2297 */ 2298 if (p2->p_estcpu > p1->p_estcpu) 2299 return (1); 2300 if (p1->p_estcpu > p2->p_estcpu) 2301 return (0); 2302 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2303 } 2304 /* 2305 * weed out zombies 2306 */ 2307 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { 2308 case ONLYA: 2309 return (1); 2310 case ONLYB: 2311 return (0); 2312 case BOTH: 2313 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2314 } 2315 /* 2316 * pick the one with the smallest sleep time 2317 */ 2318 if (p2->p_slptime > p1->p_slptime) 2319 return (0); 2320 if (p1->p_slptime > p2->p_slptime) 2321 return (1); 2322 /* 2323 * favor one sleeping in a non-interruptible sleep 2324 */ 2325 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0) 2326 return (1); 2327 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0) 2328 return (0); 2329 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2330 } 2331 2332 /* 2333 * Output char to tty; console putchar style. 2334 */ 2335 int 2336 tputchar(c, tp) 2337 int c; 2338 struct tty *tp; 2339 { 2340 register int s; 2341 2342 s = spltty(); 2343 if (!ISSET(tp->t_state, TS_CONNECTED)) { 2344 splx(s); 2345 return (-1); 2346 } 2347 if (c == '\n') 2348 (void)ttyoutput('\r', tp); 2349 (void)ttyoutput(c, tp); 2350 ttstart(tp); 2351 splx(s); 2352 return (0); 2353 } 2354 2355 /* 2356 * Sleep on chan, returning ERESTART if tty changed while we napped and 2357 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If 2358 * the tty is revoked, restarting a pending call will redo validation done 2359 * at the start of the call. 2360 */ 2361 int 2362 ttysleep(tp, chan, pri, wmesg, timo) 2363 struct tty *tp; 2364 void *chan; 2365 int pri, timo; 2366 char *wmesg; 2367 { 2368 int error; 2369 int gen; 2370 2371 gen = tp->t_gen; 2372 error = tsleep(chan, pri, wmesg, timo); 2373 if (error) 2374 return (error); 2375 return (tp->t_gen == gen ? 0 : ERESTART); 2376 } 2377 2378 #ifdef notyet 2379 /* 2380 * XXX this is usable not useful or used. Most tty drivers have 2381 * ifdefs for using ttymalloc() but assume a different interface. 2382 */ 2383 /* 2384 * Allocate a tty struct. Clists in the struct will be allocated by 2385 * ttyopen(). 2386 */ 2387 struct tty * 2388 ttymalloc() 2389 { 2390 struct tty *tp; 2391 2392 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK); 2393 bzero(tp, sizeof *tp); 2394 return (tp); 2395 } 2396 #endif 2397 2398 #if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */ 2399 /* 2400 * Free a tty struct. Clists in the struct should have been freed by 2401 * ttyclose(). 2402 */ 2403 void 2404 ttyfree(tp) 2405 struct tty *tp; 2406 { 2407 free(tp, M_TTYS); 2408 } 2409 #endif /* 0 */ 2410