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