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