1 /* $NetBSD: tty.c,v 1.120 2000/06/27 17:41:41 mrg 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 int s, error; 742 743 /* If the ioctl involves modification, hang if in the background. */ 744 switch (cmd) { 745 case TIOCFLUSH: 746 case TIOCDRAIN: 747 case TIOCSBRK: 748 case TIOCCBRK: 749 case TIOCSTART: 750 case TIOCSETA: 751 case TIOCSETD: 752 case TIOCSETAF: 753 case TIOCSETAW: 754 #ifdef notdef 755 case TIOCSPGRP: 756 #endif 757 case TIOCSTAT: 758 case TIOCSTI: 759 case TIOCSWINSZ: 760 #ifdef COMPAT_OLDTTY 761 case TIOCLBIC: 762 case TIOCLBIS: 763 case TIOCLSET: 764 case TIOCSETC: 765 case OTIOCSETD: 766 case TIOCSETN: 767 case TIOCSETP: 768 case TIOCSLTC: 769 #endif 770 while (isbackground(curproc, tp) && 771 p->p_pgrp->pg_jobc && (p->p_flag & P_PPWAIT) == 0 && 772 !sigismember(&p->p_sigignore, SIGTTOU) && 773 !sigismember(&p->p_sigmask, SIGTTOU)) { 774 pgsignal(p->p_pgrp, SIGTTOU, 1); 775 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, ttybg, 0); 776 if (error) 777 return (error); 778 } 779 break; 780 } 781 782 switch (cmd) { /* Process the ioctl. */ 783 case FIOASYNC: /* set/clear async i/o */ 784 s = spltty(); 785 if (*(int *)data) 786 SET(tp->t_state, TS_ASYNC); 787 else 788 CLR(tp->t_state, TS_ASYNC); 789 splx(s); 790 break; 791 case FIONBIO: /* set/clear non-blocking i/o */ 792 break; /* XXX: delete. */ 793 case FIONREAD: /* get # bytes to read */ 794 *(int *)data = ttnread(tp); 795 break; 796 case TIOCEXCL: /* set exclusive use of tty */ 797 s = spltty(); 798 SET(tp->t_state, TS_XCLUDE); 799 splx(s); 800 break; 801 case TIOCFLUSH: { /* flush buffers */ 802 int flags = *(int *)data; 803 804 if (flags == 0) 805 flags = FREAD | FWRITE; 806 else 807 flags &= FREAD | FWRITE; 808 ttyflush(tp, flags); 809 break; 810 } 811 case TIOCCONS: /* become virtual console */ 812 if (*(int *)data) { 813 if (constty && constty != tp && 814 ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) == 815 (TS_CARR_ON | TS_ISOPEN)) 816 return (EBUSY); 817 #ifndef UCONSOLE 818 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 819 return (error); 820 #endif 821 constty = tp; 822 } else if (tp == constty) 823 constty = NULL; 824 break; 825 case TIOCDRAIN: /* wait till output drained */ 826 if ((error = ttywait(tp)) != 0) 827 return (error); 828 break; 829 case TIOCGETA: { /* get termios struct */ 830 struct termios *t = (struct termios *)data; 831 832 memcpy(t, &tp->t_termios, sizeof(struct termios)); 833 break; 834 } 835 case TIOCGETD: /* get line discipline */ 836 *(int *)data = tp->t_line; 837 break; 838 case TIOCGWINSZ: /* get window size */ 839 *(struct winsize *)data = tp->t_winsize; 840 break; 841 case TIOCGPGRP: /* get pgrp of tty */ 842 if (!isctty(p, tp)) 843 return (ENOTTY); 844 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 845 break; 846 case TIOCGSID: /* get sid of tty */ 847 if (!isctty(p, tp)) 848 return (ENOTTY); 849 *(int *)data = tp->t_session->s_sid; 850 break; 851 #ifdef TIOCHPCL 852 case TIOCHPCL: /* hang up on last close */ 853 s = spltty(); 854 SET(tp->t_cflag, HUPCL); 855 splx(s); 856 break; 857 #endif 858 case TIOCNXCL: /* reset exclusive use of tty */ 859 s = spltty(); 860 CLR(tp->t_state, TS_XCLUDE); 861 splx(s); 862 break; 863 case TIOCOUTQ: /* output queue size */ 864 *(int *)data = tp->t_outq.c_cc; 865 break; 866 case TIOCSETA: /* set termios struct */ 867 case TIOCSETAW: /* drain output, set */ 868 case TIOCSETAF: { /* drn out, fls in, set */ 869 struct termios *t = (struct termios *)data; 870 871 s = spltty(); 872 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 873 if ((error = ttywait(tp)) != 0) { 874 splx(s); 875 return (error); 876 } 877 if (cmd == TIOCSETAF) 878 ttyflush(tp, FREAD); 879 } 880 if (!ISSET(t->c_cflag, CIGNORE)) { 881 /* 882 * Set device hardware. 883 */ 884 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 885 splx(s); 886 return (error); 887 } else { 888 tp->t_cflag = t->c_cflag; 889 tp->t_ispeed = t->c_ispeed; 890 tp->t_ospeed = t->c_ospeed; 891 if (t->c_ospeed == 0 && tp->t_session && 892 tp->t_session->s_leader) 893 psignal(tp->t_session->s_leader, 894 SIGHUP); 895 } 896 ttsetwater(tp); 897 } 898 if (cmd != TIOCSETAF) { 899 if (ISSET(t->c_lflag, ICANON) != 900 ISSET(tp->t_lflag, ICANON)) { 901 if (ISSET(t->c_lflag, ICANON)) { 902 SET(tp->t_lflag, PENDIN); 903 ttwakeup(tp); 904 } else { 905 struct clist tq; 906 907 catq(&tp->t_rawq, &tp->t_canq); 908 tq = tp->t_rawq; 909 tp->t_rawq = tp->t_canq; 910 tp->t_canq = tq; 911 CLR(tp->t_lflag, PENDIN); 912 } 913 } 914 } 915 tp->t_iflag = t->c_iflag; 916 tp->t_oflag = t->c_oflag; 917 /* 918 * Make the EXTPROC bit read only. 919 */ 920 if (ISSET(tp->t_lflag, EXTPROC)) 921 SET(t->c_lflag, EXTPROC); 922 else 923 CLR(t->c_lflag, EXTPROC); 924 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 925 memcpy(tp->t_cc, t->c_cc, sizeof(t->c_cc)); 926 splx(s); 927 break; 928 } 929 case TIOCSETD: { /* set line discipline */ 930 int t = *(int *)data; 931 dev_t device = tp->t_dev; 932 933 if ((u_int)t >= nlinesw) 934 return (ENXIO); 935 if (t != tp->t_line) { 936 s = spltty(); 937 (*linesw[tp->t_line].l_close)(tp, flag); 938 error = (*linesw[t].l_open)(device, tp); 939 if (error) { 940 (void)(*linesw[tp->t_line].l_open)(device, tp); 941 splx(s); 942 return (error); 943 } 944 tp->t_line = t; 945 splx(s); 946 } 947 break; 948 } 949 case TIOCSTART: /* start output, like ^Q */ 950 s = spltty(); 951 if (ISSET(tp->t_state, TS_TTSTOP) || 952 ISSET(tp->t_lflag, FLUSHO)) { 953 CLR(tp->t_lflag, FLUSHO); 954 CLR(tp->t_state, TS_TTSTOP); 955 ttstart(tp); 956 } 957 splx(s); 958 break; 959 case TIOCSTI: /* simulate terminal input */ 960 if (p->p_ucred->cr_uid && (flag & FREAD) == 0) 961 return (EPERM); 962 if (p->p_ucred->cr_uid && !isctty(p, tp)) 963 return (EACCES); 964 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 965 break; 966 case TIOCSTOP: /* stop output, like ^S */ 967 s = spltty(); 968 if (!ISSET(tp->t_state, TS_TTSTOP)) { 969 SET(tp->t_state, TS_TTSTOP); 970 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 971 } 972 splx(s); 973 break; 974 case TIOCSCTTY: /* become controlling tty */ 975 /* Session ctty vnode pointer set in vnode layer. */ 976 if (!SESS_LEADER(p) || 977 ((p->p_session->s_ttyvp || tp->t_session) && 978 (tp->t_session != p->p_session))) 979 return (EPERM); 980 tp->t_session = p->p_session; 981 tp->t_pgrp = p->p_pgrp; 982 p->p_session->s_ttyp = tp; 983 p->p_flag |= P_CONTROLT; 984 break; 985 case TIOCSPGRP: { /* set pgrp of tty */ 986 struct pgrp *pgrp = pgfind(*(int *)data); 987 988 if (!isctty(p, tp)) 989 return (ENOTTY); 990 else if (pgrp == NULL) 991 return (EINVAL); 992 else if (pgrp->pg_session != p->p_session) 993 return (EPERM); 994 tp->t_pgrp = pgrp; 995 break; 996 } 997 case TIOCSTAT: /* get load avg stats */ 998 ttyinfo(tp); 999 break; 1000 case TIOCSWINSZ: /* set window size */ 1001 if (memcmp((caddr_t)&tp->t_winsize, data, 1002 sizeof(struct winsize))) { 1003 tp->t_winsize = *(struct winsize *)data; 1004 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1005 } 1006 break; 1007 default: 1008 #ifdef COMPAT_OLDTTY 1009 return (ttcompat(tp, cmd, data, flag, p)); 1010 #else 1011 return (-1); 1012 #endif 1013 } 1014 return (0); 1015 } 1016 1017 int 1018 ttpoll(dev, events, p) 1019 dev_t dev; 1020 int events; 1021 struct proc *p; 1022 { 1023 struct tty *tp = (*cdevsw[major(dev)].d_tty)(dev); 1024 int revents = 0; 1025 int s = spltty(); 1026 1027 if (events & (POLLIN | POLLRDNORM)) 1028 if (ttnread(tp) > 0) 1029 revents |= events & (POLLIN | POLLRDNORM); 1030 1031 if (events & (POLLOUT | POLLWRNORM)) 1032 if (tp->t_outq.c_cc <= tp->t_lowat) 1033 revents |= events & (POLLOUT | POLLWRNORM); 1034 1035 if (events & POLLHUP) 1036 if (!CONNECTED(tp)) 1037 revents |= POLLHUP; 1038 1039 if (revents == 0) { 1040 if (events & (POLLIN | POLLHUP | POLLRDNORM)) 1041 selrecord(p, &tp->t_rsel); 1042 1043 if (events & (POLLOUT | POLLWRNORM)) 1044 selrecord(p, &tp->t_wsel); 1045 } 1046 1047 splx(s); 1048 return (revents); 1049 } 1050 1051 static int 1052 ttnread(tp) 1053 struct tty *tp; 1054 { 1055 int nread; 1056 1057 if (ISSET(tp->t_lflag, PENDIN)) 1058 ttypend(tp); 1059 nread = tp->t_canq.c_cc; 1060 if (!ISSET(tp->t_lflag, ICANON)) { 1061 nread += tp->t_rawq.c_cc; 1062 if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME]) 1063 nread = 0; 1064 } 1065 return (nread); 1066 } 1067 1068 /* 1069 * Wait for output to drain. 1070 */ 1071 int 1072 ttywait(tp) 1073 struct tty *tp; 1074 { 1075 int error, s; 1076 1077 error = 0; 1078 s = spltty(); 1079 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1080 CONNECTED(tp) && tp->t_oproc) { 1081 (*tp->t_oproc)(tp); 1082 SET(tp->t_state, TS_ASLEEP); 1083 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0); 1084 if (error) 1085 break; 1086 } 1087 splx(s); 1088 return (error); 1089 } 1090 1091 /* 1092 * Flush if successfully wait. 1093 */ 1094 int 1095 ttywflush(tp) 1096 struct tty *tp; 1097 { 1098 int error; 1099 1100 if ((error = ttywait(tp)) == 0) 1101 ttyflush(tp, FREAD); 1102 return (error); 1103 } 1104 1105 /* 1106 * Flush tty read and/or write queues, notifying anyone waiting. 1107 */ 1108 void 1109 ttyflush(tp, rw) 1110 struct tty *tp; 1111 int rw; 1112 { 1113 int s; 1114 1115 s = spltty(); 1116 if (rw & FREAD) { 1117 FLUSHQ(&tp->t_canq); 1118 FLUSHQ(&tp->t_rawq); 1119 tp->t_rocount = 0; 1120 tp->t_rocol = 0; 1121 CLR(tp->t_state, TS_LOCAL); 1122 ttwakeup(tp); 1123 } 1124 if (rw & FWRITE) { 1125 CLR(tp->t_state, TS_TTSTOP); 1126 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw); 1127 FLUSHQ(&tp->t_outq); 1128 wakeup((caddr_t)&tp->t_outq); 1129 selwakeup(&tp->t_wsel); 1130 } 1131 splx(s); 1132 } 1133 1134 /* 1135 * Copy in the default termios characters. 1136 */ 1137 void 1138 ttychars(tp) 1139 struct tty *tp; 1140 { 1141 1142 memcpy(tp->t_cc, ttydefchars, sizeof(ttydefchars)); 1143 } 1144 1145 /* 1146 * Send stop character on input overflow. 1147 */ 1148 static void 1149 ttyblock(tp) 1150 struct tty *tp; 1151 { 1152 int total; 1153 1154 total = tp->t_rawq.c_cc + tp->t_canq.c_cc; 1155 if (tp->t_rawq.c_cc > TTYHOG) { 1156 ttyflush(tp, FREAD | FWRITE); 1157 CLR(tp->t_state, TS_TBLOCK); 1158 } 1159 /* 1160 * Block further input iff: current input > threshold 1161 * AND input is available to user program. 1162 */ 1163 if (total >= TTYHOG / 2 && 1164 !ISSET(tp->t_state, TS_TBLOCK) && 1165 (!ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0)) { 1166 if (ISSET(tp->t_iflag, IXOFF) && 1167 tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1168 putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) { 1169 SET(tp->t_state, TS_TBLOCK); 1170 ttstart(tp); 1171 } 1172 /* Try to block remote output via hardware flow control. */ 1173 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow && 1174 (*tp->t_hwiflow)(tp, 1) != 0) 1175 SET(tp->t_state, TS_TBLOCK); 1176 } 1177 } 1178 1179 void 1180 ttrstrt(tp_arg) 1181 void *tp_arg; 1182 { 1183 struct tty *tp; 1184 int s; 1185 1186 #ifdef DIAGNOSTIC 1187 if (tp_arg == NULL) 1188 panic("ttrstrt"); 1189 #endif 1190 tp = tp_arg; 1191 s = spltty(); 1192 1193 CLR(tp->t_state, TS_TIMEOUT); 1194 ttstart(tp); 1195 1196 splx(s); 1197 } 1198 1199 int 1200 ttstart(tp) 1201 struct tty *tp; 1202 { 1203 1204 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1205 (*tp->t_oproc)(tp); 1206 return (0); 1207 } 1208 1209 /* 1210 * "close" a line discipline 1211 */ 1212 int 1213 ttylclose(tp, flag) 1214 struct tty *tp; 1215 int flag; 1216 { 1217 1218 if (flag & FNONBLOCK) 1219 ttyflush(tp, FREAD | FWRITE); 1220 else 1221 ttywflush(tp); 1222 return (0); 1223 } 1224 1225 /* 1226 * Handle modem control transition on a tty. 1227 * Flag indicates new state of carrier. 1228 * Returns 0 if the line should be turned off, otherwise 1. 1229 */ 1230 int 1231 ttymodem(tp, flag) 1232 struct tty *tp; 1233 int flag; 1234 { 1235 1236 if (flag == 0) { 1237 if (ISSET(tp->t_state, TS_CARR_ON)) { 1238 /* 1239 * Lost carrier. 1240 */ 1241 CLR(tp->t_state, TS_CARR_ON); 1242 if (ISSET(tp->t_state, TS_ISOPEN) && !CONNECTED(tp)) { 1243 if (tp->t_session && tp->t_session->s_leader) 1244 psignal(tp->t_session->s_leader, SIGHUP); 1245 ttyflush(tp, FREAD | FWRITE); 1246 return (0); 1247 } 1248 } 1249 } else { 1250 if (!ISSET(tp->t_state, TS_CARR_ON)) { 1251 /* 1252 * Carrier now on. 1253 */ 1254 SET(tp->t_state, TS_CARR_ON); 1255 ttwakeup(tp); 1256 } 1257 } 1258 return (1); 1259 } 1260 1261 /* 1262 * Default modem control routine (for other line disciplines). 1263 * Return argument flag, to turn off device on carrier drop. 1264 */ 1265 int 1266 nullmodem(tp, flag) 1267 struct tty *tp; 1268 int flag; 1269 { 1270 1271 if (flag) 1272 SET(tp->t_state, TS_CARR_ON); 1273 else { 1274 CLR(tp->t_state, TS_CARR_ON); 1275 if (!CONNECTED(tp)) { 1276 if (tp->t_session && tp->t_session->s_leader) 1277 psignal(tp->t_session->s_leader, SIGHUP); 1278 return (0); 1279 } 1280 } 1281 return (1); 1282 } 1283 1284 /* 1285 * Reinput pending characters after state switch 1286 * call at spltty(). 1287 */ 1288 void 1289 ttypend(tp) 1290 struct tty *tp; 1291 { 1292 struct clist tq; 1293 int c; 1294 1295 CLR(tp->t_lflag, PENDIN); 1296 SET(tp->t_state, TS_TYPEN); 1297 tq = tp->t_rawq; 1298 tp->t_rawq.c_cc = 0; 1299 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0; 1300 while ((c = getc(&tq)) >= 0) 1301 ttyinput(c, tp); 1302 CLR(tp->t_state, TS_TYPEN); 1303 } 1304 1305 /* 1306 * Process a read call on a tty device. 1307 */ 1308 int 1309 ttread(tp, uio, flag) 1310 struct tty *tp; 1311 struct uio *uio; 1312 int flag; 1313 { 1314 struct clist *qp; 1315 int c; 1316 long lflag; 1317 u_char *cc = tp->t_cc; 1318 struct proc *p = curproc; 1319 int s, first, error = 0; 1320 struct timeval stime; 1321 int has_stime = 0, last_cc = 0; 1322 long slp = 0; 1323 1324 loop: lflag = tp->t_lflag; 1325 s = spltty(); 1326 /* 1327 * take pending input first 1328 */ 1329 if (ISSET(lflag, PENDIN)) 1330 ttypend(tp); 1331 splx(s); 1332 1333 /* 1334 * Hang process if it's in the background. 1335 */ 1336 if (isbackground(p, tp)) { 1337 if (sigismember(&p->p_sigignore, SIGTTIN) || 1338 sigismember(&p->p_sigmask, SIGTTIN) || 1339 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0) 1340 return (EIO); 1341 pgsignal(p->p_pgrp, SIGTTIN, 1); 1342 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0); 1343 if (error) 1344 return (error); 1345 goto loop; 1346 } 1347 1348 s = spltty(); 1349 if (!ISSET(lflag, ICANON)) { 1350 int m = cc[VMIN]; 1351 long t = cc[VTIME]; 1352 1353 qp = &tp->t_rawq; 1354 /* 1355 * Check each of the four combinations. 1356 * (m > 0 && t == 0) is the normal read case. 1357 * It should be fairly efficient, so we check that and its 1358 * companion case (m == 0 && t == 0) first. 1359 * For the other two cases, we compute the target sleep time 1360 * into slp. 1361 */ 1362 if (t == 0) { 1363 if (qp->c_cc < m) 1364 goto sleep; 1365 goto read; 1366 } 1367 t *= 100000; /* time in us */ 1368 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1369 ((t1).tv_usec - (t2).tv_usec)) 1370 if (m > 0) { 1371 if (qp->c_cc <= 0) 1372 goto sleep; 1373 if (qp->c_cc >= m) 1374 goto read; 1375 if (!has_stime) { 1376 /* first character, start timer */ 1377 has_stime = 1; 1378 stime = time; 1379 slp = t; 1380 } else if (qp->c_cc > last_cc) { 1381 /* got a character, restart timer */ 1382 stime = time; 1383 slp = t; 1384 } else { 1385 /* nothing, check expiration */ 1386 slp = t - diff(time, stime); 1387 } 1388 } else { /* m == 0 */ 1389 if (qp->c_cc > 0) 1390 goto read; 1391 if (!has_stime) { 1392 has_stime = 1; 1393 stime = time; 1394 slp = t; 1395 } else 1396 slp = t - diff(time, stime); 1397 } 1398 last_cc = qp->c_cc; 1399 #undef diff 1400 if (slp > 0) { 1401 /* 1402 * Rounding down may make us wake up just short 1403 * of the target, so we round up. 1404 * The formula is ceiling(slp * hz/1000000). 1405 * 32-bit arithmetic is enough for hz < 169. 1406 * 1407 * Also, use plain wakeup() not ttwakeup(). 1408 */ 1409 slp = (long) (((u_long)slp * hz) + 999999) / 1000000; 1410 goto sleep; 1411 } 1412 } else if ((qp = &tp->t_canq)->c_cc <= 0) { 1413 int carrier; 1414 1415 sleep: 1416 /* 1417 * If there is no input, sleep on rawq 1418 * awaiting hardware receipt and notification. 1419 * If we have data, we don't need to check for carrier. 1420 */ 1421 carrier = CONNECTED(tp); 1422 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) { 1423 splx(s); 1424 return (0); /* EOF */ 1425 } 1426 if (flag & IO_NDELAY) { 1427 splx(s); 1428 return (EWOULDBLOCK); 1429 } 1430 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, 1431 carrier ? ttyin : ttopen, slp); 1432 splx(s); 1433 /* VMIN == 0: any quantity read satisfies */ 1434 if (cc[VMIN] == 0 && error == EWOULDBLOCK) 1435 return (0); 1436 if (error && error != EWOULDBLOCK) 1437 return (error); 1438 goto loop; 1439 } 1440 read: 1441 splx(s); 1442 1443 /* 1444 * Input present, check for input mapping and processing. 1445 */ 1446 first = 1; 1447 while ((c = getc(qp)) >= 0) { 1448 /* 1449 * delayed suspend (^Y) 1450 */ 1451 if (CCEQ(cc[VDSUSP], c) && 1452 ISSET(lflag, IEXTEN|ISIG) == (IEXTEN|ISIG)) { 1453 pgsignal(tp->t_pgrp, SIGTSTP, 1); 1454 if (first) { 1455 error = ttysleep(tp, &lbolt, 1456 TTIPRI | PCATCH, ttybg, 0); 1457 if (error) 1458 break; 1459 goto loop; 1460 } 1461 break; 1462 } 1463 /* 1464 * Interpret EOF only in canonical mode. 1465 */ 1466 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 1467 break; 1468 /* 1469 * Give user character. 1470 */ 1471 error = ureadc(c, uio); 1472 if (error) 1473 break; 1474 if (uio->uio_resid == 0) 1475 break; 1476 /* 1477 * In canonical mode check for a "break character" 1478 * marking the end of a "line of input". 1479 */ 1480 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag)) 1481 break; 1482 first = 0; 1483 } 1484 /* 1485 * Look to unblock output now that (presumably) 1486 * the input queue has gone down. 1487 */ 1488 s = spltty(); 1489 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) { 1490 if (ISSET(tp->t_iflag, IXOFF) && 1491 cc[VSTART] != _POSIX_VDISABLE && 1492 putc(cc[VSTART], &tp->t_outq) == 0) { 1493 CLR(tp->t_state, TS_TBLOCK); 1494 ttstart(tp); 1495 } 1496 /* Try to unblock remote output via hardware flow control. */ 1497 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow && 1498 (*tp->t_hwiflow)(tp, 0) != 0) 1499 CLR(tp->t_state, TS_TBLOCK); 1500 } 1501 splx(s); 1502 return (error); 1503 } 1504 1505 /* 1506 * Check the output queue on tp for space for a kernel message (from uprintf 1507 * or tprintf). Allow some space over the normal hiwater mark so we don't 1508 * lose messages due to normal flow control, but don't let the tty run amok. 1509 * Sleeps here are not interruptible, but we return prematurely if new signals 1510 * arrive. 1511 */ 1512 int 1513 ttycheckoutq(tp, wait) 1514 struct tty *tp; 1515 int wait; 1516 { 1517 int hiwat, s; 1518 int error; 1519 1520 hiwat = tp->t_hiwat; 1521 s = spltty(); 1522 if (tp->t_outq.c_cc > hiwat + 200) 1523 while (tp->t_outq.c_cc > hiwat) { 1524 ttstart(tp); 1525 if (wait == 0) { 1526 splx(s); 1527 return (0); 1528 } 1529 callout_reset(&tp->t_outq_ch, hz, 1530 (void (*)__P((void *)))wakeup, &tp->t_outq); 1531 SET(tp->t_state, TS_ASLEEP); 1532 error = tsleep(&tp->t_outq, (PZERO - 1) | PCATCH, 1533 "ttckoutq", 0); 1534 if (error == EINTR) 1535 wait = 0; 1536 } 1537 splx(s); 1538 return (1); 1539 } 1540 1541 /* 1542 * Process a write call on a tty device. 1543 */ 1544 int 1545 ttwrite(tp, uio, flag) 1546 struct tty *tp; 1547 struct uio *uio; 1548 int flag; 1549 { 1550 u_char *cp = NULL; 1551 int cc, ce; 1552 struct proc *p; 1553 int i, hiwat, cnt, error, s; 1554 u_char obuf[OBUFSIZ]; 1555 1556 hiwat = tp->t_hiwat; 1557 cnt = uio->uio_resid; 1558 error = 0; 1559 cc = 0; 1560 loop: 1561 s = spltty(); 1562 if (!CONNECTED(tp)) { 1563 if (ISSET(tp->t_state, TS_ISOPEN)) { 1564 splx(s); 1565 return (EIO); 1566 } else if (flag & IO_NDELAY) { 1567 splx(s); 1568 error = EWOULDBLOCK; 1569 goto out; 1570 } else { 1571 /* Sleep awaiting carrier. */ 1572 error = ttysleep(tp, 1573 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0); 1574 splx(s); 1575 if (error) 1576 goto out; 1577 goto loop; 1578 } 1579 } 1580 splx(s); 1581 /* 1582 * Hang the process if it's in the background. 1583 */ 1584 p = curproc; 1585 if (isbackground(p, tp) && 1586 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 && 1587 !sigismember(&p->p_sigignore, SIGTTOU) && 1588 !sigismember(&p->p_sigmask, SIGTTOU)) { 1589 if (p->p_pgrp->pg_jobc == 0) { 1590 error = EIO; 1591 goto out; 1592 } 1593 pgsignal(p->p_pgrp, SIGTTOU, 1); 1594 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0); 1595 if (error) 1596 goto out; 1597 goto loop; 1598 } 1599 /* 1600 * Process the user's data in at most OBUFSIZ chunks. Perform any 1601 * output translation. Keep track of high water mark, sleep on 1602 * overflow awaiting device aid in acquiring new space. 1603 */ 1604 while (uio->uio_resid > 0 || cc > 0) { 1605 if (ISSET(tp->t_lflag, FLUSHO)) { 1606 uio->uio_resid = 0; 1607 return (0); 1608 } 1609 if (tp->t_outq.c_cc > hiwat) 1610 goto ovhiwat; 1611 /* 1612 * Grab a hunk of data from the user, unless we have some 1613 * leftover from last time. 1614 */ 1615 if (cc == 0) { 1616 cc = min(uio->uio_resid, OBUFSIZ); 1617 cp = obuf; 1618 error = uiomove(cp, cc, uio); 1619 if (error) { 1620 cc = 0; 1621 break; 1622 } 1623 } 1624 /* 1625 * If nothing fancy need be done, grab those characters we 1626 * can handle without any of ttyoutput's processing and 1627 * just transfer them to the output q. For those chars 1628 * which require special processing (as indicated by the 1629 * bits in char_type), call ttyoutput. After processing 1630 * a hunk of data, look for FLUSHO so ^O's will take effect 1631 * immediately. 1632 */ 1633 while (cc > 0) { 1634 if (!ISSET(tp->t_oflag, OPOST)) 1635 ce = cc; 1636 else { 1637 ce = cc - scanc((u_int)cc, cp, char_type, 1638 CCLASSMASK); 1639 /* 1640 * If ce is zero, then we're processing 1641 * a special character through ttyoutput. 1642 */ 1643 if (ce == 0) { 1644 tp->t_rocount = 0; 1645 if (ttyoutput(*cp, tp) >= 0) { 1646 /* out of space */ 1647 goto overfull; 1648 } 1649 cp++; 1650 cc--; 1651 if (ISSET(tp->t_lflag, FLUSHO) || 1652 tp->t_outq.c_cc > hiwat) 1653 goto ovhiwat; 1654 continue; 1655 } 1656 } 1657 /* 1658 * A bunch of normal characters have been found. 1659 * Transfer them en masse to the output queue and 1660 * continue processing at the top of the loop. 1661 * If there are any further characters in this 1662 * <= OBUFSIZ chunk, the first should be a character 1663 * requiring special handling by ttyoutput. 1664 */ 1665 tp->t_rocount = 0; 1666 i = b_to_q(cp, ce, &tp->t_outq); 1667 ce -= i; 1668 tp->t_column += ce; 1669 cp += ce, cc -= ce, tk_nout += ce; 1670 tp->t_outcc += ce; 1671 if (i > 0) { 1672 /* out of space */ 1673 goto overfull; 1674 } 1675 if (ISSET(tp->t_lflag, FLUSHO) || 1676 tp->t_outq.c_cc > hiwat) 1677 break; 1678 } 1679 ttstart(tp); 1680 } 1681 out: 1682 /* 1683 * If cc is nonzero, we leave the uio structure inconsistent, as the 1684 * offset and iov pointers have moved forward, but it doesn't matter 1685 * (the call will either return short or restart with a new uio). 1686 */ 1687 uio->uio_resid += cc; 1688 return (error); 1689 1690 overfull: 1691 /* 1692 * Since we are using ring buffers, if we can't insert any more into 1693 * the output queue, we can assume the ring is full and that someone 1694 * forgot to set the high water mark correctly. We set it and then 1695 * proceed as normal. 1696 */ 1697 hiwat = tp->t_outq.c_cc - 1; 1698 1699 ovhiwat: 1700 ttstart(tp); 1701 s = spltty(); 1702 /* 1703 * This can only occur if FLUSHO is set in t_lflag, 1704 * or if ttstart/oproc is synchronous (or very fast). 1705 */ 1706 if (tp->t_outq.c_cc <= hiwat) { 1707 splx(s); 1708 goto loop; 1709 } 1710 if (flag & IO_NDELAY) { 1711 splx(s); 1712 uio->uio_resid += cc; 1713 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 1714 } 1715 SET(tp->t_state, TS_ASLEEP); 1716 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0); 1717 splx(s); 1718 if (error) 1719 goto out; 1720 goto loop; 1721 } 1722 1723 /* 1724 * Rubout one character from the rawq of tp 1725 * as cleanly as possible. 1726 */ 1727 void 1728 ttyrub(c, tp) 1729 int c; 1730 struct tty *tp; 1731 { 1732 u_char *cp; 1733 int savecol; 1734 int tabc, s; 1735 1736 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 1737 return; 1738 CLR(tp->t_lflag, FLUSHO); 1739 if (ISSET(tp->t_lflag, ECHOE)) { 1740 if (tp->t_rocount == 0) { 1741 /* 1742 * Screwed by ttwrite; retype 1743 */ 1744 ttyretype(tp); 1745 return; 1746 } 1747 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 1748 ttyrubo(tp, 2); 1749 else { 1750 CLR(c, ~TTY_CHARMASK); 1751 switch (CCLASS(c)) { 1752 case ORDINARY: 1753 ttyrubo(tp, 1); 1754 break; 1755 case BACKSPACE: 1756 case CONTROL: 1757 case NEWLINE: 1758 case RETURN: 1759 case VTAB: 1760 if (ISSET(tp->t_lflag, ECHOCTL)) 1761 ttyrubo(tp, 2); 1762 break; 1763 case TAB: 1764 if (tp->t_rocount < tp->t_rawq.c_cc) { 1765 ttyretype(tp); 1766 return; 1767 } 1768 s = spltty(); 1769 savecol = tp->t_column; 1770 SET(tp->t_state, TS_CNTTB); 1771 SET(tp->t_lflag, FLUSHO); 1772 tp->t_column = tp->t_rocol; 1773 for (cp = firstc(&tp->t_rawq, &tabc); cp; 1774 cp = nextc(&tp->t_rawq, cp, &tabc)) 1775 ttyecho(tabc, tp); 1776 CLR(tp->t_lflag, FLUSHO); 1777 CLR(tp->t_state, TS_CNTTB); 1778 splx(s); 1779 1780 /* savecol will now be length of the tab. */ 1781 savecol -= tp->t_column; 1782 tp->t_column += savecol; 1783 if (savecol > 8) 1784 savecol = 8; /* overflow screw */ 1785 while (--savecol >= 0) 1786 (void)ttyoutput('\b', tp); 1787 break; 1788 default: /* XXX */ 1789 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 1790 (void)printf(PANICSTR, c, CCLASS(c)); 1791 #ifdef notdef 1792 panic(PANICSTR, c, CCLASS(c)); 1793 #endif 1794 } 1795 } 1796 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 1797 if (!ISSET(tp->t_state, TS_ERASE)) { 1798 SET(tp->t_state, TS_ERASE); 1799 (void)ttyoutput('\\', tp); 1800 } 1801 ttyecho(c, tp); 1802 } else 1803 ttyecho(tp->t_cc[VERASE], tp); 1804 --tp->t_rocount; 1805 } 1806 1807 /* 1808 * Back over cnt characters, erasing them. 1809 */ 1810 static void 1811 ttyrubo(tp, cnt) 1812 struct tty *tp; 1813 int cnt; 1814 { 1815 1816 while (cnt-- > 0) { 1817 (void)ttyoutput('\b', tp); 1818 (void)ttyoutput(' ', tp); 1819 (void)ttyoutput('\b', tp); 1820 } 1821 } 1822 1823 /* 1824 * ttyretype -- 1825 * Reprint the rawq line. Note, it is assumed that c_cc has already 1826 * been checked. 1827 */ 1828 void 1829 ttyretype(tp) 1830 struct tty *tp; 1831 { 1832 u_char *cp; 1833 int s, c; 1834 1835 /* Echo the reprint character. */ 1836 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 1837 ttyecho(tp->t_cc[VREPRINT], tp); 1838 1839 (void)ttyoutput('\n', tp); 1840 1841 s = spltty(); 1842 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c)) 1843 ttyecho(c, tp); 1844 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c)) 1845 ttyecho(c, tp); 1846 CLR(tp->t_state, TS_ERASE); 1847 splx(s); 1848 1849 tp->t_rocount = tp->t_rawq.c_cc; 1850 tp->t_rocol = 0; 1851 } 1852 1853 /* 1854 * Echo a typed character to the terminal. 1855 */ 1856 static void 1857 ttyecho(c, tp) 1858 int c; 1859 struct tty *tp; 1860 { 1861 1862 if (!ISSET(tp->t_state, TS_CNTTB)) 1863 CLR(tp->t_lflag, FLUSHO); 1864 if ((!ISSET(tp->t_lflag, ECHO) && 1865 (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) || 1866 ISSET(tp->t_lflag, EXTPROC)) 1867 return; 1868 if (((ISSET(tp->t_lflag, ECHOCTL) && 1869 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) || 1870 ISSET(c, TTY_CHARMASK) == 0177)) { 1871 (void)ttyoutput('^', tp); 1872 CLR(c, ~TTY_CHARMASK); 1873 if (c == 0177) 1874 c = '?'; 1875 else 1876 c += 'A' - 1; 1877 } 1878 (void)ttyoutput(c, tp); 1879 } 1880 1881 /* 1882 * Wake up any readers on a tty. 1883 */ 1884 void 1885 ttwakeup(tp) 1886 struct tty *tp; 1887 { 1888 1889 selwakeup(&tp->t_rsel); 1890 if (ISSET(tp->t_state, TS_ASYNC)) 1891 pgsignal(tp->t_pgrp, SIGIO, 1); 1892 wakeup((caddr_t)&tp->t_rawq); 1893 } 1894 1895 /* 1896 * Look up a code for a specified speed in a conversion table; 1897 * used by drivers to map software speed values to hardware parameters. 1898 */ 1899 int 1900 ttspeedtab(speed, table) 1901 int speed; 1902 struct speedtab *table; 1903 { 1904 1905 for ( ; table->sp_speed != -1; table++) 1906 if (table->sp_speed == speed) 1907 return (table->sp_code); 1908 return (-1); 1909 } 1910 1911 /* 1912 * Set tty hi and low water marks. 1913 * 1914 * Try to arrange the dynamics so there's about one second 1915 * from hi to low water. 1916 */ 1917 void 1918 ttsetwater(tp) 1919 struct tty *tp; 1920 { 1921 int cps, x; 1922 1923 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 1924 1925 cps = tp->t_ospeed / 10; 1926 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 1927 x += cps; 1928 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT); 1929 tp->t_hiwat = roundup(x, CBSIZE); 1930 #undef CLAMP 1931 } 1932 1933 /* 1934 * Report on state of foreground process group. 1935 */ 1936 void 1937 ttyinfo(tp) 1938 struct tty *tp; 1939 { 1940 struct proc *p, *pick; 1941 struct timeval utime, stime; 1942 int tmp; 1943 1944 if (ttycheckoutq(tp,0) == 0) 1945 return; 1946 1947 /* Print load average. */ 1948 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 1949 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 1950 1951 if (tp->t_session == NULL) 1952 ttyprintf(tp, "not a controlling terminal\n"); 1953 else if (tp->t_pgrp == NULL) 1954 ttyprintf(tp, "no foreground process group\n"); 1955 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0) 1956 ttyprintf(tp, "empty foreground process group\n"); 1957 else { 1958 /* Pick interesting process. */ 1959 for (pick = NULL; p != NULL; p = p->p_pglist.le_next) 1960 if (proc_compare(pick, p)) 1961 pick = p; 1962 1963 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 1964 pick->p_stat == SONPROC ? "running" : 1965 pick->p_stat == SRUN ? "runnable" : 1966 pick->p_wmesg ? pick->p_wmesg : "iowait"); 1967 1968 calcru(pick, &utime, &stime, NULL); 1969 1970 /* Round up and print user time. */ 1971 utime.tv_usec += 5000; 1972 if (utime.tv_usec >= 1000000) { 1973 utime.tv_sec += 1; 1974 utime.tv_usec -= 1000000; 1975 } 1976 ttyprintf(tp, "%ld.%02ldu ", (long int)utime.tv_sec, 1977 (long int)utime.tv_usec / 10000); 1978 1979 /* Round up and print system time. */ 1980 stime.tv_usec += 5000; 1981 if (stime.tv_usec >= 1000000) { 1982 stime.tv_sec += 1; 1983 stime.tv_usec -= 1000000; 1984 } 1985 ttyprintf(tp, "%ld.%02lds ", (long int)stime.tv_sec, 1986 (long int)stime.tv_usec / 10000); 1987 1988 #define pgtok(a) (((u_long) ((a) * NBPG) / 1024)) 1989 /* Print percentage cpu. */ 1990 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT; 1991 ttyprintf(tp, "%d%% ", tmp / 100); 1992 1993 /* Print resident set size. */ 1994 if (pick->p_stat == SIDL || P_ZOMBIE(pick)) 1995 tmp = 0; 1996 else { 1997 struct vmspace *vm = pick->p_vmspace; 1998 tmp = pgtok(vm_resident_count(vm)); 1999 } 2000 ttyprintf(tp, "%dk\n", tmp); 2001 } 2002 tp->t_rocount = 0; /* so pending input will be retyped if BS */ 2003 } 2004 2005 /* 2006 * Returns 1 if p2 is "better" than p1 2007 * 2008 * The algorithm for picking the "interesting" process is thus: 2009 * 2010 * 1) Only foreground processes are eligible - implied. 2011 * 2) Runnable processes are favored over anything else. The runner 2012 * with the highest cpu utilization is picked (p_estcpu). Ties are 2013 * broken by picking the highest pid. 2014 * 3) The sleeper with the shortest sleep time is next. With ties, 2015 * we pick out just "short-term" sleepers (P_SINTR == 0). 2016 * 4) Further ties are broken by picking the highest pid. 2017 */ 2018 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL) || \ 2019 ((p)->p_stat == SONPROC)) 2020 #define TESTAB(a, b) ((a)<<1 | (b)) 2021 #define ONLYA 2 2022 #define ONLYB 1 2023 #define BOTH 3 2024 2025 static int 2026 proc_compare(p1, p2) 2027 struct proc *p1, *p2; 2028 { 2029 2030 if (p1 == NULL) 2031 return (1); 2032 /* 2033 * see if at least one of them is runnable 2034 */ 2035 switch (TESTAB(ISRUN(p1), ISRUN(p2))) { 2036 case ONLYA: 2037 return (0); 2038 case ONLYB: 2039 return (1); 2040 case BOTH: 2041 /* 2042 * tie - favor one with highest recent cpu utilization 2043 */ 2044 if (p2->p_estcpu > p1->p_estcpu) 2045 return (1); 2046 if (p1->p_estcpu > p2->p_estcpu) 2047 return (0); 2048 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2049 } 2050 /* 2051 * weed out zombies 2052 */ 2053 switch (TESTAB(P_ZOMBIE(p1), P_ZOMBIE(p2))) { 2054 case ONLYA: 2055 return (1); 2056 case ONLYB: 2057 return (0); 2058 case BOTH: 2059 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2060 } 2061 /* 2062 * pick the one with the smallest sleep time 2063 */ 2064 if (p2->p_slptime > p1->p_slptime) 2065 return (0); 2066 if (p1->p_slptime > p2->p_slptime) 2067 return (1); 2068 /* 2069 * favor one sleeping in a non-interruptible sleep 2070 */ 2071 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0) 2072 return (1); 2073 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0) 2074 return (0); 2075 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2076 } 2077 2078 /* 2079 * Output char to tty; console putchar style. 2080 */ 2081 int 2082 tputchar(c, tp) 2083 int c; 2084 struct tty *tp; 2085 { 2086 int s; 2087 2088 s = spltty(); 2089 if (ISSET(tp->t_state, 2090 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) { 2091 splx(s); 2092 return (-1); 2093 } 2094 if (c == '\n') 2095 (void)ttyoutput('\r', tp); 2096 (void)ttyoutput(c, tp); 2097 ttstart(tp); 2098 splx(s); 2099 return (0); 2100 } 2101 2102 /* 2103 * Sleep on chan, returning ERESTART if tty changed while we napped and 2104 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If 2105 * the tty is revoked, restarting a pending call will redo validation done 2106 * at the start of the call. 2107 */ 2108 int 2109 ttysleep(tp, chan, pri, wmesg, timo) 2110 struct tty *tp; 2111 void *chan; 2112 int pri, timo; 2113 const char *wmesg; 2114 { 2115 int error; 2116 short gen; 2117 2118 gen = tp->t_gen; 2119 if ((error = tsleep(chan, pri, wmesg, timo)) != 0) 2120 return (error); 2121 return (tp->t_gen == gen ? 0 : ERESTART); 2122 } 2123 2124 /* 2125 * Initialise the global tty list. 2126 */ 2127 void 2128 tty_init() 2129 { 2130 2131 TAILQ_INIT(&ttylist); 2132 tty_count = 0; 2133 2134 pool_init(&tty_pool, sizeof(struct tty), 0, 0, 0, "ttypl", 2135 0, pool_page_alloc_nointr, pool_page_free_nointr, M_TTYS); 2136 } 2137 2138 /* 2139 * Attach a tty to the tty list. 2140 * 2141 * This should be called ONLY once per real tty (including pty's). 2142 * eg, on the sparc, the keyboard and mouse have struct tty's that are 2143 * distinctly NOT usable as tty's, and thus should not be attached to 2144 * the ttylist. This is why this call is not done from ttymalloc(). 2145 * 2146 * Device drivers should attach tty's at a similar time that they are 2147 * ttymalloc()'ed, or, for the case of statically allocated struct tty's 2148 * either in the attach or (first) open routine. 2149 */ 2150 void 2151 tty_attach(tp) 2152 struct tty *tp; 2153 { 2154 2155 TAILQ_INSERT_TAIL(&ttylist, tp, tty_link); 2156 ++tty_count; 2157 } 2158 2159 /* 2160 * Remove a tty from the tty list. 2161 */ 2162 void 2163 tty_detach(tp) 2164 struct tty *tp; 2165 { 2166 2167 --tty_count; 2168 #ifdef DIAGNOSTIC 2169 if (tty_count < 0) 2170 panic("tty_detach: tty_count < 0"); 2171 #endif 2172 TAILQ_REMOVE(&ttylist, tp, tty_link); 2173 } 2174 2175 /* 2176 * Allocate a tty structure and its associated buffers. 2177 */ 2178 struct tty * 2179 ttymalloc() 2180 { 2181 struct tty *tp; 2182 2183 tp = pool_get(&tty_pool, PR_WAITOK); 2184 memset(tp, 0, sizeof(*tp)); 2185 callout_init(&tp->t_outq_ch); 2186 callout_init(&tp->t_rstrt_ch); 2187 /* XXX: default to 1024 chars for now */ 2188 clalloc(&tp->t_rawq, 1024, 1); 2189 clalloc(&tp->t_canq, 1024, 1); 2190 /* output queue doesn't need quoting */ 2191 clalloc(&tp->t_outq, 1024, 0); 2192 return(tp); 2193 } 2194 2195 /* 2196 * Free a tty structure and its buffers. 2197 * 2198 * Be sure to call tty_detach() for any tty that has been 2199 * tty_attach()ed. 2200 */ 2201 void 2202 ttyfree(tp) 2203 struct tty *tp; 2204 { 2205 2206 callout_stop(&tp->t_outq_ch); 2207 callout_stop(&tp->t_rstrt_ch); 2208 clfree(&tp->t_rawq); 2209 clfree(&tp->t_canq); 2210 clfree(&tp->t_outq); 2211 pool_put(&tty_pool, tp); 2212 } 2213