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