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