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