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