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 * $FreeBSD$ 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 Now that historical speed conversions are handled here, don't 62 * do them in drivers. 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 "opt_compat.h" 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/filio.h> 75 #include <sys/lock.h> 76 #include <sys/mutex.h> 77 #include <sys/namei.h> 78 #include <sys/sx.h> 79 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 80 #include <sys/ioctl_compat.h> 81 #endif 82 #include <sys/proc.h> 83 #define TTYDEFCHARS 84 #include <sys/tty.h> 85 #undef TTYDEFCHARS 86 #include <sys/fcntl.h> 87 #include <sys/conf.h> 88 #include <sys/dkstat.h> 89 #include <sys/poll.h> 90 #include <sys/kernel.h> 91 #include <sys/vnode.h> 92 #include <sys/signalvar.h> 93 #include <sys/resourcevar.h> 94 #include <sys/malloc.h> 95 #include <sys/filedesc.h> 96 #include <sys/sysctl.h> 97 98 #include <vm/vm.h> 99 #include <vm/pmap.h> 100 #include <vm/vm_map.h> 101 102 MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures"); 103 104 static int proc_compare(struct proc *p1, struct proc *p2); 105 static int ttnread(struct tty *tp); 106 static void ttyecho(int c, struct tty *tp); 107 static int ttyoutput(int c, register struct tty *tp); 108 static void ttypend(struct tty *tp); 109 static void ttyretype(struct tty *tp); 110 static void ttyrub(int c, struct tty *tp); 111 static void ttyrubo(struct tty *tp, int cnt); 112 static void ttyunblock(struct tty *tp); 113 static int ttywflush(struct tty *tp); 114 static int filt_ttyread(struct knote *kn, long hint); 115 static void filt_ttyrdetach(struct knote *kn); 116 static int filt_ttywrite(struct knote *kn, long hint); 117 static void filt_ttywdetach(struct knote *kn); 118 119 /* 120 * Table with character classes and parity. The 8th bit indicates parity, 121 * the 7th bit indicates the character is an alphameric or underscore (for 122 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 123 * are 0 then the character needs no special processing on output; classes 124 * other than 0 might be translated or (not currently) require delays. 125 */ 126 #define E 0x00 /* Even parity. */ 127 #define O 0x80 /* Odd parity. */ 128 #define PARITY(c) (char_type[c] & O) 129 130 #define ALPHA 0x40 /* Alpha or underscore. */ 131 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 132 133 #define CCLASSMASK 0x3f 134 #define CCLASS(c) (char_type[c] & CCLASSMASK) 135 136 #define BS BACKSPACE 137 #define CC CONTROL 138 #define CR RETURN 139 #define NA ORDINARY | ALPHA 140 #define NL NEWLINE 141 #define NO ORDINARY 142 #define TB TAB 143 #define VT VTAB 144 145 static u_char const char_type[] = { 146 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 147 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 148 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 149 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 150 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 151 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 152 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 153 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 154 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 155 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 156 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 157 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 158 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 159 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 160 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 161 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 162 /* 163 * Meta chars; should be settable per character set; 164 * for now, treat them all as normal characters. 165 */ 166 NA, NA, NA, NA, NA, NA, NA, NA, 167 NA, NA, NA, NA, NA, NA, NA, NA, 168 NA, NA, NA, NA, NA, NA, NA, NA, 169 NA, NA, NA, NA, NA, NA, NA, NA, 170 NA, NA, NA, NA, NA, NA, NA, NA, 171 NA, NA, NA, NA, NA, NA, NA, NA, 172 NA, NA, NA, NA, NA, NA, NA, NA, 173 NA, NA, NA, NA, NA, NA, NA, NA, 174 NA, NA, NA, NA, NA, NA, NA, NA, 175 NA, NA, NA, NA, NA, NA, NA, NA, 176 NA, NA, NA, NA, NA, NA, NA, NA, 177 NA, NA, NA, NA, NA, NA, NA, NA, 178 NA, NA, NA, NA, NA, NA, NA, NA, 179 NA, NA, NA, NA, NA, NA, NA, NA, 180 NA, NA, NA, NA, NA, NA, NA, NA, 181 NA, NA, NA, NA, NA, NA, NA, NA, 182 }; 183 #undef BS 184 #undef CC 185 #undef CR 186 #undef NA 187 #undef NL 188 #undef NO 189 #undef TB 190 #undef VT 191 192 /* Macros to clear/set/test flags. */ 193 #define SET(t, f) (t) |= (f) 194 #define CLR(t, f) (t) &= ~(f) 195 #define ISSET(t, f) ((t) & (f)) 196 197 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */ 198 #define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */ 199 200 /* 201 * list of struct tty where pstat(8) can pick it up with sysctl 202 */ 203 static SLIST_HEAD(, tty) tty_list; 204 205 static int drainwait = 5*60; 206 SYSCTL_INT(_kern, OID_AUTO, drainwait, CTLFLAG_RW, &drainwait, 207 0, "Output drain timeout in seconds"); 208 209 /* 210 * Initial open of tty, or (re)entry to standard tty line discipline. 211 */ 212 int 213 ttyopen(device, tp) 214 dev_t device; 215 register struct tty *tp; 216 { 217 int s; 218 219 s = spltty(); 220 tp->t_dev = device; 221 if (!ISSET(tp->t_state, TS_ISOPEN)) { 222 SET(tp->t_state, TS_ISOPEN); 223 if (ISSET(tp->t_cflag, CLOCAL)) 224 SET(tp->t_state, TS_CONNECTED); 225 bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 226 } 227 /* XXX don't hang forever on output */ 228 if (tp->t_timeout < 0) 229 tp->t_timeout = drainwait*hz; 230 ttsetwater(tp); 231 splx(s); 232 return (0); 233 } 234 235 /* 236 * Handle close() on a tty line: flush and set to initial state, 237 * bumping generation number so that pending read/write calls 238 * can detect recycling of the tty. 239 * XXX our caller should have done `spltty(); l_close(); ttyclose();' 240 * and l_close() should have flushed, but we repeat the spltty() and 241 * the flush in case there are buggy callers. 242 */ 243 int 244 ttyclose(tp) 245 register struct tty *tp; 246 { 247 int s; 248 249 funsetown(&tp->t_sigio); 250 s = spltty(); 251 if (constty == tp) 252 constty = NULL; 253 254 ttyflush(tp, FREAD | FWRITE); 255 clist_free_cblocks(&tp->t_canq); 256 clist_free_cblocks(&tp->t_outq); 257 clist_free_cblocks(&tp->t_rawq); 258 259 tp->t_gen++; 260 tp->t_line = TTYDISC; 261 tp->t_pgrp = NULL; 262 tp->t_session = NULL; 263 tp->t_state = 0; 264 splx(s); 265 return (0); 266 } 267 268 #define FLUSHQ(q) { \ 269 if ((q)->c_cc) \ 270 ndflush(q, (q)->c_cc); \ 271 } 272 273 /* Is 'c' a line delimiter ("break" character)? */ 274 #define TTBREAKC(c, lflag) \ 275 ((c) == '\n' || (((c) == cc[VEOF] || \ 276 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \ 277 (c) != _POSIX_VDISABLE)) 278 279 /* 280 * Process input of a single character received on a tty. 281 */ 282 int 283 ttyinput(c, tp) 284 register int c; 285 register struct tty *tp; 286 { 287 register tcflag_t iflag, lflag; 288 register cc_t *cc; 289 int i, err; 290 291 /* 292 * If input is pending take it first. 293 */ 294 lflag = tp->t_lflag; 295 if (ISSET(lflag, PENDIN)) 296 ttypend(tp); 297 /* 298 * Gather stats. 299 */ 300 if (ISSET(lflag, ICANON)) { 301 ++tk_cancc; 302 ++tp->t_cancc; 303 } else { 304 ++tk_rawcc; 305 ++tp->t_rawcc; 306 } 307 ++tk_nin; 308 309 /* 310 * Block further input iff: 311 * current input > threshold AND input is available to user program 312 * AND input flow control is enabled and not yet invoked. 313 * The 3 is slop for PARMRK. 314 */ 315 iflag = tp->t_iflag; 316 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 && 317 (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) && 318 (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) && 319 !ISSET(tp->t_state, TS_TBLOCK)) 320 ttyblock(tp); 321 322 /* Handle exceptional conditions (break, parity, framing). */ 323 cc = tp->t_cc; 324 err = (ISSET(c, TTY_ERRORMASK)); 325 if (err) { 326 CLR(c, TTY_ERRORMASK); 327 if (ISSET(err, TTY_BI)) { 328 if (ISSET(iflag, IGNBRK)) 329 return (0); 330 if (ISSET(iflag, BRKINT)) { 331 ttyflush(tp, FREAD | FWRITE); 332 if (tp->t_pgrp != NULL) { 333 PGRP_LOCK(tp->t_pgrp); 334 pgsignal(tp->t_pgrp, SIGINT, 1); 335 PGRP_UNLOCK(tp->t_pgrp); 336 } 337 goto endcase; 338 } 339 if (ISSET(iflag, PARMRK)) 340 goto parmrk; 341 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK)) 342 || ISSET(err, TTY_FE)) { 343 if (ISSET(iflag, IGNPAR)) 344 return (0); 345 else if (ISSET(iflag, PARMRK)) { 346 parmrk: 347 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > 348 MAX_INPUT - 3) 349 goto input_overflow; 350 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 351 (void)putc(0 | TTY_QUOTE, &tp->t_rawq); 352 (void)putc(c | TTY_QUOTE, &tp->t_rawq); 353 goto endcase; 354 } else 355 c = 0; 356 } 357 } 358 359 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP)) 360 CLR(c, 0x80); 361 if (!ISSET(lflag, EXTPROC)) { 362 /* 363 * Check for literal nexting very first 364 */ 365 if (ISSET(tp->t_state, TS_LNCH)) { 366 SET(c, TTY_QUOTE); 367 CLR(tp->t_state, TS_LNCH); 368 } 369 /* 370 * Scan for special characters. This code 371 * is really just a big case statement with 372 * non-constant cases. The bottom of the 373 * case statement is labeled ``endcase'', so goto 374 * it after a case match, or similar. 375 */ 376 377 /* 378 * Control chars which aren't controlled 379 * by ICANON, ISIG, or IXON. 380 */ 381 if (ISSET(lflag, IEXTEN)) { 382 if (CCEQ(cc[VLNEXT], c)) { 383 if (ISSET(lflag, ECHO)) { 384 if (ISSET(lflag, ECHOE)) { 385 (void)ttyoutput('^', tp); 386 (void)ttyoutput('\b', tp); 387 } else 388 ttyecho(c, tp); 389 } 390 SET(tp->t_state, TS_LNCH); 391 goto endcase; 392 } 393 if (CCEQ(cc[VDISCARD], c)) { 394 if (ISSET(lflag, FLUSHO)) 395 CLR(tp->t_lflag, FLUSHO); 396 else { 397 ttyflush(tp, FWRITE); 398 ttyecho(c, tp); 399 if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 400 ttyretype(tp); 401 SET(tp->t_lflag, FLUSHO); 402 } 403 goto startoutput; 404 } 405 } 406 /* 407 * Signals. 408 */ 409 if (ISSET(lflag, ISIG)) { 410 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 411 if (!ISSET(lflag, NOFLSH)) 412 ttyflush(tp, FREAD | FWRITE); 413 ttyecho(c, tp); 414 if (tp->t_pgrp != NULL) { 415 PGRP_LOCK(tp->t_pgrp); 416 pgsignal(tp->t_pgrp, 417 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 418 PGRP_UNLOCK(tp->t_pgrp); 419 } 420 goto endcase; 421 } 422 if (CCEQ(cc[VSUSP], c)) { 423 if (!ISSET(lflag, NOFLSH)) 424 ttyflush(tp, FREAD); 425 ttyecho(c, tp); 426 if (tp->t_pgrp != NULL) { 427 PGRP_LOCK(tp->t_pgrp); 428 pgsignal(tp->t_pgrp, SIGTSTP, 1); 429 PGRP_UNLOCK(tp->t_pgrp); 430 } 431 goto endcase; 432 } 433 } 434 /* 435 * Handle start/stop characters. 436 */ 437 if (ISSET(iflag, IXON)) { 438 if (CCEQ(cc[VSTOP], c)) { 439 if (!ISSET(tp->t_state, TS_TTSTOP)) { 440 SET(tp->t_state, TS_TTSTOP); 441 (*tp->t_stop)(tp, 0); 442 return (0); 443 } 444 if (!CCEQ(cc[VSTART], c)) 445 return (0); 446 /* 447 * if VSTART == VSTOP then toggle 448 */ 449 goto endcase; 450 } 451 if (CCEQ(cc[VSTART], c)) 452 goto restartoutput; 453 } 454 /* 455 * IGNCR, ICRNL, & INLCR 456 */ 457 if (c == '\r') { 458 if (ISSET(iflag, IGNCR)) 459 return (0); 460 else if (ISSET(iflag, ICRNL)) 461 c = '\n'; 462 } else if (c == '\n' && ISSET(iflag, INLCR)) 463 c = '\r'; 464 } 465 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) { 466 /* 467 * From here on down canonical mode character 468 * processing takes place. 469 */ 470 /* 471 * erase or erase2 (^H / ^?) 472 */ 473 if (CCEQ(cc[VERASE], c) || CCEQ(cc[VERASE2], c) ) { 474 if (tp->t_rawq.c_cc) 475 ttyrub(unputc(&tp->t_rawq), tp); 476 goto endcase; 477 } 478 /* 479 * kill (^U) 480 */ 481 if (CCEQ(cc[VKILL], c)) { 482 if (ISSET(lflag, ECHOKE) && 483 tp->t_rawq.c_cc == tp->t_rocount && 484 !ISSET(lflag, ECHOPRT)) 485 while (tp->t_rawq.c_cc) 486 ttyrub(unputc(&tp->t_rawq), tp); 487 else { 488 ttyecho(c, tp); 489 if (ISSET(lflag, ECHOK) || 490 ISSET(lflag, ECHOKE)) 491 ttyecho('\n', tp); 492 FLUSHQ(&tp->t_rawq); 493 tp->t_rocount = 0; 494 } 495 CLR(tp->t_state, TS_LOCAL); 496 goto endcase; 497 } 498 /* 499 * word erase (^W) 500 */ 501 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) { 502 int ctype; 503 504 /* 505 * erase whitespace 506 */ 507 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 508 ttyrub(c, tp); 509 if (c == -1) 510 goto endcase; 511 /* 512 * erase last char of word and remember the 513 * next chars type (for ALTWERASE) 514 */ 515 ttyrub(c, tp); 516 c = unputc(&tp->t_rawq); 517 if (c == -1) 518 goto endcase; 519 if (c == ' ' || c == '\t') { 520 (void)putc(c, &tp->t_rawq); 521 goto endcase; 522 } 523 ctype = ISALPHA(c); 524 /* 525 * erase rest of word 526 */ 527 do { 528 ttyrub(c, tp); 529 c = unputc(&tp->t_rawq); 530 if (c == -1) 531 goto endcase; 532 } while (c != ' ' && c != '\t' && 533 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype)); 534 (void)putc(c, &tp->t_rawq); 535 goto endcase; 536 } 537 /* 538 * reprint line (^R) 539 */ 540 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) { 541 ttyretype(tp); 542 goto endcase; 543 } 544 /* 545 * ^T - kernel info and generate SIGINFO 546 */ 547 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) { 548 if (ISSET(lflag, ISIG) && tp->t_pgrp != NULL) { 549 PGRP_LOCK(tp->t_pgrp); 550 pgsignal(tp->t_pgrp, SIGINFO, 1); 551 PGRP_UNLOCK(tp->t_pgrp); 552 } 553 if (!ISSET(lflag, NOKERNINFO)) 554 ttyinfo(tp); 555 goto endcase; 556 } 557 } 558 /* 559 * Check for input buffer overflow 560 */ 561 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) { 562 input_overflow: 563 if (ISSET(iflag, IMAXBEL)) { 564 if (tp->t_outq.c_cc < tp->t_ohiwat) 565 (void)ttyoutput(CTRL('g'), tp); 566 } 567 goto endcase; 568 } 569 570 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP) 571 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR)) 572 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq); 573 574 /* 575 * Put data char in q for user and 576 * wakeup on seeing a line delimiter. 577 */ 578 if (putc(c, &tp->t_rawq) >= 0) { 579 if (!ISSET(lflag, ICANON)) { 580 ttwakeup(tp); 581 ttyecho(c, tp); 582 goto endcase; 583 } 584 if (TTBREAKC(c, lflag)) { 585 tp->t_rocount = 0; 586 catq(&tp->t_rawq, &tp->t_canq); 587 ttwakeup(tp); 588 } else if (tp->t_rocount++ == 0) 589 tp->t_rocol = tp->t_column; 590 if (ISSET(tp->t_state, TS_ERASE)) { 591 /* 592 * end of prterase \.../ 593 */ 594 CLR(tp->t_state, TS_ERASE); 595 (void)ttyoutput('/', tp); 596 } 597 i = tp->t_column; 598 ttyecho(c, tp); 599 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) { 600 /* 601 * Place the cursor over the '^' of the ^D. 602 */ 603 i = imin(2, tp->t_column - i); 604 while (i > 0) { 605 (void)ttyoutput('\b', tp); 606 i--; 607 } 608 } 609 } 610 endcase: 611 /* 612 * IXANY means allow any character to restart output. 613 */ 614 if (ISSET(tp->t_state, TS_TTSTOP) && 615 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) 616 return (0); 617 restartoutput: 618 CLR(tp->t_lflag, FLUSHO); 619 CLR(tp->t_state, TS_TTSTOP); 620 startoutput: 621 return (ttstart(tp)); 622 } 623 624 /* 625 * Output a single character on a tty, doing output processing 626 * as needed (expanding tabs, newline processing, etc.). 627 * Returns < 0 if succeeds, otherwise returns char to resend. 628 * Must be recursive. 629 */ 630 static int 631 ttyoutput(c, tp) 632 register int c; 633 register struct tty *tp; 634 { 635 register tcflag_t oflag; 636 register int col, s; 637 638 oflag = tp->t_oflag; 639 if (!ISSET(oflag, OPOST)) { 640 if (ISSET(tp->t_lflag, FLUSHO)) 641 return (-1); 642 if (putc(c, &tp->t_outq)) 643 return (c); 644 tk_nout++; 645 tp->t_outcc++; 646 return (-1); 647 } 648 /* 649 * Do tab expansion if OXTABS is set. Special case if we external 650 * processing, we don't do the tab expansion because we'll probably 651 * get it wrong. If tab expansion needs to be done, let it happen 652 * externally. 653 */ 654 CLR(c, ~TTY_CHARMASK); 655 if (c == '\t' && 656 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) { 657 c = 8 - (tp->t_column & 7); 658 if (!ISSET(tp->t_lflag, FLUSHO)) { 659 s = spltty(); /* Don't interrupt tabs. */ 660 c -= b_to_q(" ", c, &tp->t_outq); 661 tk_nout += c; 662 tp->t_outcc += c; 663 splx(s); 664 } 665 tp->t_column += c; 666 return (c ? -1 : '\t'); 667 } 668 if (c == CEOT && ISSET(oflag, ONOEOT)) 669 return (-1); 670 671 /* 672 * Newline translation: if ONLCR is set, 673 * translate newline into "\r\n". 674 */ 675 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) { 676 tk_nout++; 677 tp->t_outcc++; 678 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq)) 679 return (c); 680 } 681 /* If OCRNL is set, translate "\r" into "\n". */ 682 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL)) 683 c = '\n'; 684 /* If ONOCR is set, don't transmit CRs when on column 0. */ 685 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0) 686 return (-1); 687 688 tk_nout++; 689 tp->t_outcc++; 690 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq)) 691 return (c); 692 693 col = tp->t_column; 694 switch (CCLASS(c)) { 695 case BACKSPACE: 696 if (col > 0) 697 --col; 698 break; 699 case CONTROL: 700 break; 701 case NEWLINE: 702 if (ISSET(tp->t_oflag, ONLCR | ONLRET)) 703 col = 0; 704 break; 705 case RETURN: 706 col = 0; 707 break; 708 case ORDINARY: 709 ++col; 710 break; 711 case TAB: 712 col = (col + 8) & ~7; 713 break; 714 } 715 tp->t_column = col; 716 return (-1); 717 } 718 719 /* 720 * Ioctls for all tty devices. Called after line-discipline specific ioctl 721 * has been called to do discipline-specific functions and/or reject any 722 * of these ioctl commands. 723 */ 724 /* ARGSUSED */ 725 int 726 ttioctl(tp, cmd, data, flag) 727 register struct tty *tp; 728 u_long cmd; 729 int flag; 730 void *data; 731 { 732 register struct proc *p; 733 struct thread *td; 734 struct pgrp *pgrp; 735 int s, error; 736 737 td = curthread; /* XXX */ 738 p = td->td_proc; 739 740 /* If the ioctl involves modification, hang if in the background. */ 741 switch (cmd) { 742 case TIOCCBRK: 743 case TIOCCONS: 744 case TIOCDRAIN: 745 case TIOCEXCL: 746 case TIOCFLUSH: 747 #ifdef TIOCHPCL 748 case TIOCHPCL: 749 #endif 750 case TIOCNXCL: 751 case TIOCSBRK: 752 case TIOCSCTTY: 753 case TIOCSDRAINWAIT: 754 case TIOCSETA: 755 case TIOCSETAF: 756 case TIOCSETAW: 757 case TIOCSETD: 758 case TIOCSPGRP: 759 case TIOCSTART: 760 case TIOCSTAT: 761 case TIOCSTI: 762 case TIOCSTOP: 763 case TIOCSWINSZ: 764 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 765 case TIOCLBIC: 766 case TIOCLBIS: 767 case TIOCLSET: 768 case TIOCSETC: 769 case OTIOCSETD: 770 case TIOCSETN: 771 case TIOCSETP: 772 case TIOCSLTC: 773 #endif 774 sx_slock(&proctree_lock); 775 PROC_LOCK(p); 776 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) && 777 !SIGISMEMBER(p->p_sigignore, SIGTTOU) && 778 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) { 779 pgrp = p->p_pgrp; 780 PROC_UNLOCK(p); 781 if (pgrp->pg_jobc == 0) { 782 sx_sunlock(&proctree_lock); 783 return (EIO); 784 } 785 PGRP_LOCK(pgrp); 786 sx_sunlock(&proctree_lock); 787 pgsignal(pgrp, SIGTTOU, 1); 788 PGRP_UNLOCK(pgrp); 789 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1", 790 0); 791 if (error) 792 return (error); 793 sx_slock(&proctree_lock); 794 PROC_LOCK(p); 795 } 796 PROC_UNLOCK(p); 797 sx_sunlock(&proctree_lock); 798 break; 799 } 800 801 switch (cmd) { /* Process the ioctl. */ 802 case FIOASYNC: /* set/clear async i/o */ 803 s = spltty(); 804 if (*(int *)data) 805 SET(tp->t_state, TS_ASYNC); 806 else 807 CLR(tp->t_state, TS_ASYNC); 808 splx(s); 809 break; 810 case FIONBIO: /* set/clear non-blocking i/o */ 811 break; /* XXX: delete. */ 812 case FIONREAD: /* get # bytes to read */ 813 s = spltty(); 814 *(int *)data = ttnread(tp); 815 splx(s); 816 break; 817 818 case FIOSETOWN: 819 /* 820 * Policy -- Don't allow FIOSETOWN on someone else's 821 * controlling tty 822 */ 823 if (tp->t_session != NULL && !isctty(p, tp)) 824 return (ENOTTY); 825 826 error = fsetown(*(int *)data, &tp->t_sigio); 827 if (error) 828 return (error); 829 break; 830 case FIOGETOWN: 831 if (tp->t_session != NULL && !isctty(p, tp)) 832 return (ENOTTY); 833 *(int *)data = fgetown(tp->t_sigio); 834 break; 835 836 case TIOCEXCL: /* set exclusive use of tty */ 837 s = spltty(); 838 SET(tp->t_state, TS_XCLUDE); 839 splx(s); 840 break; 841 case TIOCFLUSH: { /* flush buffers */ 842 register int flags = *(int *)data; 843 844 if (flags == 0) 845 flags = FREAD | FWRITE; 846 else 847 flags &= FREAD | FWRITE; 848 ttyflush(tp, flags); 849 break; 850 } 851 case TIOCCONS: /* become virtual console */ 852 if (*(int *)data) { 853 struct nameidata nid; 854 855 if (constty && constty != tp && 856 ISSET(constty->t_state, TS_CONNECTED)) 857 return (EBUSY); 858 859 /* Ensure user can open the real console. */ 860 NDINIT(&nid, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, 861 "/dev/console", td); 862 if ((error = namei(&nid)) != 0) 863 return (error); 864 NDFREE(&nid, NDF_ONLY_PNBUF); 865 error = VOP_ACCESS(nid.ni_vp, VREAD, td->td_ucred, td); 866 vput(nid.ni_vp); 867 if (error) 868 return (error); 869 870 constty = tp; 871 } else if (tp == constty) 872 constty = NULL; 873 break; 874 case TIOCDRAIN: /* wait till output drained */ 875 error = ttywait(tp); 876 if (error) 877 return (error); 878 break; 879 case TIOCGETA: { /* get termios struct */ 880 struct termios *t = (struct termios *)data; 881 882 bcopy(&tp->t_termios, t, sizeof(struct termios)); 883 break; 884 } 885 case TIOCGETD: /* get line discipline */ 886 *(int *)data = tp->t_line; 887 break; 888 case TIOCGWINSZ: /* get window size */ 889 *(struct winsize *)data = tp->t_winsize; 890 break; 891 case TIOCGPGRP: /* get pgrp of tty */ 892 if (!isctty(p, tp)) 893 return (ENOTTY); 894 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 895 break; 896 #ifdef TIOCHPCL 897 case TIOCHPCL: /* hang up on last close */ 898 s = spltty(); 899 SET(tp->t_cflag, HUPCL); 900 splx(s); 901 break; 902 #endif 903 case TIOCNXCL: /* reset exclusive use of tty */ 904 s = spltty(); 905 CLR(tp->t_state, TS_XCLUDE); 906 splx(s); 907 break; 908 case TIOCOUTQ: /* output queue size */ 909 *(int *)data = tp->t_outq.c_cc; 910 break; 911 case TIOCSETA: /* set termios struct */ 912 case TIOCSETAW: /* drain output, set */ 913 case TIOCSETAF: { /* drn out, fls in, set */ 914 register struct termios *t = (struct termios *)data; 915 916 if (t->c_ispeed == 0) 917 t->c_ispeed = t->c_ospeed; 918 if (t->c_ispeed == 0) 919 t->c_ispeed = tp->t_ospeed; 920 if (t->c_ispeed == 0) 921 return (EINVAL); 922 s = spltty(); 923 if (cmd == TIOCSETAW || cmd == TIOCSETAF) { 924 error = ttywait(tp); 925 if (error) { 926 splx(s); 927 return (error); 928 } 929 if (cmd == TIOCSETAF) 930 ttyflush(tp, FREAD); 931 } 932 if (!ISSET(t->c_cflag, CIGNORE)) { 933 /* 934 * Set device hardware. 935 */ 936 if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 937 splx(s); 938 return (error); 939 } 940 if (ISSET(t->c_cflag, CLOCAL) && 941 !ISSET(tp->t_cflag, CLOCAL)) { 942 /* 943 * XXX disconnections would be too hard to 944 * get rid of without this kludge. The only 945 * way to get rid of controlling terminals 946 * is to exit from the session leader. 947 */ 948 CLR(tp->t_state, TS_ZOMBIE); 949 950 wakeup(TSA_CARR_ON(tp)); 951 ttwakeup(tp); 952 ttwwakeup(tp); 953 } 954 if ((ISSET(tp->t_state, TS_CARR_ON) || 955 ISSET(t->c_cflag, CLOCAL)) && 956 !ISSET(tp->t_state, TS_ZOMBIE)) 957 SET(tp->t_state, TS_CONNECTED); 958 else 959 CLR(tp->t_state, TS_CONNECTED); 960 tp->t_cflag = t->c_cflag; 961 tp->t_ispeed = t->c_ispeed; 962 if (t->c_ospeed != 0) 963 tp->t_ospeed = t->c_ospeed; 964 ttsetwater(tp); 965 } 966 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) && 967 cmd != TIOCSETAF) { 968 if (ISSET(t->c_lflag, ICANON)) 969 SET(tp->t_lflag, PENDIN); 970 else { 971 /* 972 * XXX we really shouldn't allow toggling 973 * ICANON while we're in a non-termios line 974 * discipline. Now we have to worry about 975 * panicing for a null queue. 976 */ 977 if (tp->t_canq.c_cbreserved > 0 && 978 tp->t_rawq.c_cbreserved > 0) { 979 catq(&tp->t_rawq, &tp->t_canq); 980 /* 981 * XXX the queue limits may be 982 * different, so the old queue 983 * swapping method no longer works. 984 */ 985 catq(&tp->t_canq, &tp->t_rawq); 986 } 987 CLR(tp->t_lflag, PENDIN); 988 } 989 ttwakeup(tp); 990 } 991 tp->t_iflag = t->c_iflag; 992 tp->t_oflag = t->c_oflag; 993 /* 994 * Make the EXTPROC bit read only. 995 */ 996 if (ISSET(tp->t_lflag, EXTPROC)) 997 SET(t->c_lflag, EXTPROC); 998 else 999 CLR(t->c_lflag, EXTPROC); 1000 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN); 1001 if (t->c_cc[VMIN] != tp->t_cc[VMIN] || 1002 t->c_cc[VTIME] != tp->t_cc[VTIME]) 1003 ttwakeup(tp); 1004 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 1005 splx(s); 1006 break; 1007 } 1008 case TIOCSETD: { /* set line discipline */ 1009 register int t = *(int *)data; 1010 dev_t device = tp->t_dev; 1011 1012 if ((u_int)t >= nlinesw) 1013 return (ENXIO); 1014 if (t != tp->t_line) { 1015 s = spltty(); 1016 (*linesw[tp->t_line].l_close)(tp, flag); 1017 error = (*linesw[t].l_open)(device, tp); 1018 if (error) { 1019 (void)(*linesw[tp->t_line].l_open)(device, tp); 1020 splx(s); 1021 return (error); 1022 } 1023 tp->t_line = t; 1024 splx(s); 1025 } 1026 break; 1027 } 1028 case TIOCSTART: /* start output, like ^Q */ 1029 s = spltty(); 1030 if (ISSET(tp->t_state, TS_TTSTOP) || 1031 ISSET(tp->t_lflag, FLUSHO)) { 1032 CLR(tp->t_lflag, FLUSHO); 1033 CLR(tp->t_state, TS_TTSTOP); 1034 ttstart(tp); 1035 } 1036 splx(s); 1037 break; 1038 case TIOCSTI: /* simulate terminal input */ 1039 if ((flag & FREAD) == 0 && suser(td)) 1040 return (EPERM); 1041 if (!isctty(p, tp) && suser(td)) 1042 return (EACCES); 1043 s = spltty(); 1044 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 1045 splx(s); 1046 break; 1047 case TIOCSTOP: /* stop output, like ^S */ 1048 s = spltty(); 1049 if (!ISSET(tp->t_state, TS_TTSTOP)) { 1050 SET(tp->t_state, TS_TTSTOP); 1051 (*tp->t_stop)(tp, 0); 1052 } 1053 splx(s); 1054 break; 1055 case TIOCSCTTY: /* become controlling tty */ 1056 /* Session ctty vnode pointer set in vnode layer. */ 1057 sx_slock(&proctree_lock); 1058 if (!SESS_LEADER(p) || 1059 ((p->p_session->s_ttyvp || tp->t_session) && 1060 (tp->t_session != p->p_session))) { 1061 sx_sunlock(&proctree_lock); 1062 return (EPERM); 1063 } 1064 tp->t_session = p->p_session; 1065 tp->t_pgrp = p->p_pgrp; 1066 SESS_LOCK(p->p_session); 1067 p->p_session->s_ttyp = tp; 1068 SESS_UNLOCK(p->p_session); 1069 PROC_LOCK(p); 1070 p->p_flag |= P_CONTROLT; 1071 PROC_UNLOCK(p); 1072 sx_sunlock(&proctree_lock); 1073 break; 1074 case TIOCSPGRP: { /* set pgrp of tty */ 1075 sx_slock(&proctree_lock); 1076 pgrp = pgfind(*(int *)data); 1077 if (!isctty(p, tp)) { 1078 if (pgrp != NULL) 1079 PGRP_UNLOCK(pgrp); 1080 sx_sunlock(&proctree_lock); 1081 return (ENOTTY); 1082 } 1083 if (pgrp == NULL) { 1084 sx_sunlock(&proctree_lock); 1085 return (EPERM); 1086 } 1087 PGRP_UNLOCK(pgrp); 1088 if (pgrp->pg_session != p->p_session) { 1089 sx_sunlock(&proctree_lock); 1090 return (EPERM); 1091 } 1092 sx_sunlock(&proctree_lock); 1093 tp->t_pgrp = pgrp; 1094 break; 1095 } 1096 case TIOCSTAT: /* simulate control-T */ 1097 s = spltty(); 1098 ttyinfo(tp); 1099 splx(s); 1100 break; 1101 case TIOCSWINSZ: /* set window size */ 1102 if (bcmp((caddr_t)&tp->t_winsize, data, 1103 sizeof (struct winsize))) { 1104 tp->t_winsize = *(struct winsize *)data; 1105 if (tp->t_pgrp != NULL) { 1106 PGRP_LOCK(tp->t_pgrp); 1107 pgsignal(tp->t_pgrp, SIGWINCH, 1); 1108 PGRP_UNLOCK(tp->t_pgrp); 1109 } 1110 } 1111 break; 1112 case TIOCSDRAINWAIT: 1113 error = suser(td); 1114 if (error) 1115 return (error); 1116 tp->t_timeout = *(int *)data * hz; 1117 wakeup(TSA_OCOMPLETE(tp)); 1118 wakeup(TSA_OLOWAT(tp)); 1119 break; 1120 case TIOCGDRAINWAIT: 1121 *(int *)data = tp->t_timeout / hz; 1122 break; 1123 default: 1124 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1125 return (ttcompat(tp, cmd, data, flag)); 1126 #else 1127 return (ENOIOCTL); 1128 #endif 1129 } 1130 return (0); 1131 } 1132 1133 int 1134 ttypoll(dev, events, td) 1135 dev_t dev; 1136 int events; 1137 struct thread *td; 1138 { 1139 int s; 1140 int revents = 0; 1141 struct tty *tp; 1142 1143 tp = dev->si_tty; 1144 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */ 1145 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) 1146 | POLLHUP); 1147 1148 s = spltty(); 1149 if (events & (POLLIN | POLLRDNORM)) { 1150 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) 1151 revents |= events & (POLLIN | POLLRDNORM); 1152 else 1153 selrecord(td, &tp->t_rsel); 1154 } 1155 if (events & (POLLOUT | POLLWRNORM)) { 1156 if ((tp->t_outq.c_cc <= tp->t_olowat && 1157 ISSET(tp->t_state, TS_CONNECTED)) 1158 || ISSET(tp->t_state, TS_ZOMBIE)) 1159 revents |= events & (POLLOUT | POLLWRNORM); 1160 else 1161 selrecord(td, &tp->t_wsel); 1162 } 1163 splx(s); 1164 return (revents); 1165 } 1166 1167 static struct filterops ttyread_filtops = 1168 { 1, NULL, filt_ttyrdetach, filt_ttyread }; 1169 static struct filterops ttywrite_filtops = 1170 { 1, NULL, filt_ttywdetach, filt_ttywrite }; 1171 1172 int 1173 ttykqfilter(dev, kn) 1174 dev_t dev; 1175 struct knote *kn; 1176 { 1177 struct tty *tp = dev->si_tty; 1178 struct klist *klist; 1179 int s; 1180 1181 switch (kn->kn_filter) { 1182 case EVFILT_READ: 1183 klist = &tp->t_rsel.si_note; 1184 kn->kn_fop = &ttyread_filtops; 1185 break; 1186 case EVFILT_WRITE: 1187 klist = &tp->t_wsel.si_note; 1188 kn->kn_fop = &ttywrite_filtops; 1189 break; 1190 default: 1191 return (1); 1192 } 1193 1194 kn->kn_hook = (caddr_t)dev; 1195 1196 s = spltty(); 1197 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1198 splx(s); 1199 1200 return (0); 1201 } 1202 1203 static void 1204 filt_ttyrdetach(struct knote *kn) 1205 { 1206 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty; 1207 int s = spltty(); 1208 1209 SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext); 1210 splx(s); 1211 } 1212 1213 static int 1214 filt_ttyread(struct knote *kn, long hint) 1215 { 1216 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty; 1217 1218 kn->kn_data = ttnread(tp); 1219 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1220 kn->kn_flags |= EV_EOF; 1221 return (1); 1222 } 1223 return (kn->kn_data > 0); 1224 } 1225 1226 static void 1227 filt_ttywdetach(struct knote *kn) 1228 { 1229 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty; 1230 int s = spltty(); 1231 1232 SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext); 1233 splx(s); 1234 } 1235 1236 static int 1237 filt_ttywrite(kn, hint) 1238 struct knote *kn; 1239 long hint; 1240 { 1241 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty; 1242 1243 kn->kn_data = tp->t_outq.c_cc; 1244 if (ISSET(tp->t_state, TS_ZOMBIE)) 1245 return (1); 1246 return (kn->kn_data <= tp->t_olowat && 1247 ISSET(tp->t_state, TS_CONNECTED)); 1248 } 1249 1250 /* 1251 * Must be called at spltty(). 1252 */ 1253 static int 1254 ttnread(tp) 1255 struct tty *tp; 1256 { 1257 int nread; 1258 1259 if (ISSET(tp->t_lflag, PENDIN)) 1260 ttypend(tp); 1261 nread = tp->t_canq.c_cc; 1262 if (!ISSET(tp->t_lflag, ICANON)) { 1263 nread += tp->t_rawq.c_cc; 1264 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) 1265 nread = 0; 1266 } 1267 return (nread); 1268 } 1269 1270 /* 1271 * Wait for output to drain. 1272 */ 1273 int 1274 ttywait(tp) 1275 register struct tty *tp; 1276 { 1277 int error, s; 1278 1279 error = 0; 1280 s = spltty(); 1281 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1282 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) { 1283 (*tp->t_oproc)(tp); 1284 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 1285 ISSET(tp->t_state, TS_CONNECTED)) { 1286 SET(tp->t_state, TS_SO_OCOMPLETE); 1287 error = ttysleep(tp, TSA_OCOMPLETE(tp), 1288 TTOPRI | PCATCH, "ttywai", 1289 tp->t_timeout); 1290 if (error) { 1291 if (error == EWOULDBLOCK) 1292 error = EIO; 1293 break; 1294 } 1295 } else 1296 break; 1297 } 1298 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY))) 1299 error = EIO; 1300 splx(s); 1301 return (error); 1302 } 1303 1304 /* 1305 * Flush if successfully wait. 1306 */ 1307 static int 1308 ttywflush(tp) 1309 struct tty *tp; 1310 { 1311 int error; 1312 1313 if ((error = ttywait(tp)) == 0) 1314 ttyflush(tp, FREAD); 1315 return (error); 1316 } 1317 1318 /* 1319 * Flush tty read and/or write queues, notifying anyone waiting. 1320 */ 1321 void 1322 ttyflush(tp, rw) 1323 register struct tty *tp; 1324 int rw; 1325 { 1326 register int s; 1327 1328 s = spltty(); 1329 #if 0 1330 again: 1331 #endif 1332 if (rw & FWRITE) { 1333 FLUSHQ(&tp->t_outq); 1334 CLR(tp->t_state, TS_TTSTOP); 1335 } 1336 (*tp->t_stop)(tp, rw); 1337 if (rw & FREAD) { 1338 FLUSHQ(&tp->t_canq); 1339 FLUSHQ(&tp->t_rawq); 1340 CLR(tp->t_lflag, PENDIN); 1341 tp->t_rocount = 0; 1342 tp->t_rocol = 0; 1343 CLR(tp->t_state, TS_LOCAL); 1344 ttwakeup(tp); 1345 if (ISSET(tp->t_state, TS_TBLOCK)) { 1346 if (rw & FWRITE) 1347 FLUSHQ(&tp->t_outq); 1348 ttyunblock(tp); 1349 1350 /* 1351 * Don't let leave any state that might clobber the 1352 * next line discipline (although we should do more 1353 * to send the START char). Not clearing the state 1354 * may have caused the "putc to a clist with no 1355 * reserved cblocks" panic/printf. 1356 */ 1357 CLR(tp->t_state, TS_TBLOCK); 1358 1359 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */ 1360 if (ISSET(tp->t_iflag, IXOFF)) { 1361 /* 1362 * XXX wait a bit in the hope that the stop 1363 * character (if any) will go out. Waiting 1364 * isn't good since it allows races. This 1365 * will be fixed when the stop character is 1366 * put in a special queue. Don't bother with 1367 * the checks in ttywait() since the timeout 1368 * will save us. 1369 */ 1370 SET(tp->t_state, TS_SO_OCOMPLETE); 1371 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI, 1372 "ttyfls", hz / 10); 1373 /* 1374 * Don't try sending the stop character again. 1375 */ 1376 CLR(tp->t_state, TS_TBLOCK); 1377 goto again; 1378 } 1379 #endif 1380 } 1381 } 1382 if (rw & FWRITE) { 1383 FLUSHQ(&tp->t_outq); 1384 ttwwakeup(tp); 1385 } 1386 splx(s); 1387 } 1388 1389 /* 1390 * Copy in the default termios characters. 1391 */ 1392 void 1393 termioschars(t) 1394 struct termios *t; 1395 { 1396 1397 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc); 1398 } 1399 1400 /* 1401 * Old interface. 1402 */ 1403 void 1404 ttychars(tp) 1405 struct tty *tp; 1406 { 1407 1408 termioschars(&tp->t_termios); 1409 } 1410 1411 /* 1412 * Handle input high water. Send stop character for the IXOFF case. Turn 1413 * on our input flow control bit and propagate the changes to the driver. 1414 * XXX the stop character should be put in a special high priority queue. 1415 */ 1416 void 1417 ttyblock(tp) 1418 struct tty *tp; 1419 { 1420 1421 SET(tp->t_state, TS_TBLOCK); 1422 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE && 1423 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0) 1424 CLR(tp->t_state, TS_TBLOCK); /* try again later */ 1425 ttstart(tp); 1426 } 1427 1428 /* 1429 * Handle input low water. Send start character for the IXOFF case. Turn 1430 * off our input flow control bit and propagate the changes to the driver. 1431 * XXX the start character should be put in a special high priority queue. 1432 */ 1433 static void 1434 ttyunblock(tp) 1435 struct tty *tp; 1436 { 1437 1438 CLR(tp->t_state, TS_TBLOCK); 1439 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE && 1440 putc(tp->t_cc[VSTART], &tp->t_outq) != 0) 1441 SET(tp->t_state, TS_TBLOCK); /* try again later */ 1442 ttstart(tp); 1443 } 1444 1445 #ifdef notyet 1446 /* Not used by any current (i386) drivers. */ 1447 /* 1448 * Restart after an inter-char delay. 1449 */ 1450 void 1451 ttrstrt(tp_arg) 1452 void *tp_arg; 1453 { 1454 struct tty *tp; 1455 int s; 1456 1457 KASSERT(tp_arg != NULL, ("ttrstrt")); 1458 1459 tp = tp_arg; 1460 s = spltty(); 1461 1462 CLR(tp->t_state, TS_TIMEOUT); 1463 ttstart(tp); 1464 1465 splx(s); 1466 } 1467 #endif 1468 1469 int 1470 ttstart(tp) 1471 struct tty *tp; 1472 { 1473 1474 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 1475 (*tp->t_oproc)(tp); 1476 return (0); 1477 } 1478 1479 /* 1480 * "close" a line discipline 1481 */ 1482 int 1483 ttylclose(tp, flag) 1484 struct tty *tp; 1485 int flag; 1486 { 1487 1488 if (flag & FNONBLOCK || ttywflush(tp)) 1489 ttyflush(tp, FREAD | FWRITE); 1490 return (0); 1491 } 1492 1493 /* 1494 * Handle modem control transition on a tty. 1495 * Flag indicates new state of carrier. 1496 * Returns 0 if the line should be turned off, otherwise 1. 1497 */ 1498 int 1499 ttymodem(tp, flag) 1500 register struct tty *tp; 1501 int flag; 1502 { 1503 1504 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) { 1505 /* 1506 * MDMBUF: do flow control according to carrier flag 1507 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP 1508 * works if IXON and IXANY are clear. 1509 */ 1510 if (flag) { 1511 CLR(tp->t_state, TS_CAR_OFLOW); 1512 CLR(tp->t_state, TS_TTSTOP); 1513 ttstart(tp); 1514 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) { 1515 SET(tp->t_state, TS_CAR_OFLOW); 1516 SET(tp->t_state, TS_TTSTOP); 1517 (*tp->t_stop)(tp, 0); 1518 } 1519 } else if (flag == 0) { 1520 /* 1521 * Lost carrier. 1522 */ 1523 CLR(tp->t_state, TS_CARR_ON); 1524 if (ISSET(tp->t_state, TS_ISOPEN) && 1525 !ISSET(tp->t_cflag, CLOCAL)) { 1526 SET(tp->t_state, TS_ZOMBIE); 1527 CLR(tp->t_state, TS_CONNECTED); 1528 if (tp->t_session) { 1529 sx_slock(&proctree_lock); 1530 if (tp->t_session->s_leader) { 1531 struct proc *p; 1532 1533 p = tp->t_session->s_leader; 1534 PROC_LOCK(p); 1535 psignal(p, SIGHUP); 1536 PROC_UNLOCK(p); 1537 } 1538 sx_sunlock(&proctree_lock); 1539 } 1540 ttyflush(tp, FREAD | FWRITE); 1541 return (0); 1542 } 1543 } else { 1544 /* 1545 * Carrier now on. 1546 */ 1547 SET(tp->t_state, TS_CARR_ON); 1548 if (!ISSET(tp->t_state, TS_ZOMBIE)) 1549 SET(tp->t_state, TS_CONNECTED); 1550 wakeup(TSA_CARR_ON(tp)); 1551 ttwakeup(tp); 1552 ttwwakeup(tp); 1553 } 1554 return (1); 1555 } 1556 1557 /* 1558 * Reinput pending characters after state switch 1559 * call at spltty(). 1560 */ 1561 static void 1562 ttypend(tp) 1563 register struct tty *tp; 1564 { 1565 struct clist tq; 1566 register int c; 1567 1568 CLR(tp->t_lflag, PENDIN); 1569 SET(tp->t_state, TS_TYPEN); 1570 /* 1571 * XXX this assumes too much about clist internals. It may even 1572 * fail if the cblock slush pool is empty. We can't allocate more 1573 * cblocks here because we are called from an interrupt handler 1574 * and clist_alloc_cblocks() can wait. 1575 */ 1576 tq = tp->t_rawq; 1577 bzero(&tp->t_rawq, sizeof tp->t_rawq); 1578 tp->t_rawq.c_cbmax = tq.c_cbmax; 1579 tp->t_rawq.c_cbreserved = tq.c_cbreserved; 1580 while ((c = getc(&tq)) >= 0) 1581 ttyinput(c, tp); 1582 CLR(tp->t_state, TS_TYPEN); 1583 } 1584 1585 /* 1586 * Process a read call on a tty device. 1587 */ 1588 int 1589 ttread(tp, uio, flag) 1590 register struct tty *tp; 1591 struct uio *uio; 1592 int flag; 1593 { 1594 register struct clist *qp; 1595 register int c; 1596 register tcflag_t lflag; 1597 register cc_t *cc = tp->t_cc; 1598 register struct proc *p = curproc; 1599 int s, first, error = 0; 1600 int has_stime = 0, last_cc = 0; 1601 long slp = 0; /* XXX this should be renamed `timo'. */ 1602 struct timeval stime; 1603 struct pgrp *pg; 1604 1605 loop: 1606 s = spltty(); 1607 lflag = tp->t_lflag; 1608 /* 1609 * take pending input first 1610 */ 1611 if (ISSET(lflag, PENDIN)) { 1612 ttypend(tp); 1613 splx(s); /* reduce latency */ 1614 s = spltty(); 1615 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */ 1616 } 1617 1618 /* 1619 * Hang process if it's in the background. 1620 */ 1621 if (isbackground(p, tp)) { 1622 splx(s); 1623 sx_slock(&proctree_lock); 1624 PROC_LOCK(p); 1625 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 1626 SIGISMEMBER(p->p_sigmask, SIGTTIN) || 1627 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0) { 1628 PROC_UNLOCK(p); 1629 sx_sunlock(&proctree_lock); 1630 return (EIO); 1631 } 1632 pg = p->p_pgrp; 1633 PROC_UNLOCK(p); 1634 PGRP_LOCK(pg); 1635 sx_sunlock(&proctree_lock); 1636 pgsignal(pg, SIGTTIN, 1); 1637 PGRP_UNLOCK(pg); 1638 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0); 1639 if (error) 1640 return (error); 1641 goto loop; 1642 } 1643 1644 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1645 splx(s); 1646 return (0); /* EOF */ 1647 } 1648 1649 /* 1650 * If canonical, use the canonical queue, 1651 * else use the raw queue. 1652 * 1653 * (should get rid of clists...) 1654 */ 1655 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 1656 1657 if (flag & IO_NDELAY) { 1658 if (qp->c_cc > 0) 1659 goto read; 1660 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) { 1661 splx(s); 1662 return (0); 1663 } 1664 splx(s); 1665 return (EWOULDBLOCK); 1666 } 1667 if (!ISSET(lflag, ICANON)) { 1668 int m = cc[VMIN]; 1669 long t = cc[VTIME]; 1670 struct timeval timecopy; 1671 1672 /* 1673 * Check each of the four combinations. 1674 * (m > 0 && t == 0) is the normal read case. 1675 * It should be fairly efficient, so we check that and its 1676 * companion case (m == 0 && t == 0) first. 1677 * For the other two cases, we compute the target sleep time 1678 * into slp. 1679 */ 1680 if (t == 0) { 1681 if (qp->c_cc < m) 1682 goto sleep; 1683 if (qp->c_cc > 0) 1684 goto read; 1685 1686 /* m, t and qp->c_cc are all 0. 0 is enough input. */ 1687 splx(s); 1688 return (0); 1689 } 1690 t *= 100000; /* time in us */ 1691 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \ 1692 ((t1).tv_usec - (t2).tv_usec)) 1693 if (m > 0) { 1694 if (qp->c_cc <= 0) 1695 goto sleep; 1696 if (qp->c_cc >= m) 1697 goto read; 1698 getmicrotime(&timecopy); 1699 if (!has_stime) { 1700 /* first character, start timer */ 1701 has_stime = 1; 1702 stime = timecopy; 1703 slp = t; 1704 } else if (qp->c_cc > last_cc) { 1705 /* got a character, restart timer */ 1706 stime = timecopy; 1707 slp = t; 1708 } else { 1709 /* nothing, check expiration */ 1710 slp = t - diff(timecopy, stime); 1711 if (slp <= 0) 1712 goto read; 1713 } 1714 last_cc = qp->c_cc; 1715 } else { /* m == 0 */ 1716 if (qp->c_cc > 0) 1717 goto read; 1718 getmicrotime(&timecopy); 1719 if (!has_stime) { 1720 has_stime = 1; 1721 stime = timecopy; 1722 slp = t; 1723 } else { 1724 slp = t - diff(timecopy, stime); 1725 if (slp <= 0) { 1726 /* Timed out, but 0 is enough input. */ 1727 splx(s); 1728 return (0); 1729 } 1730 } 1731 } 1732 #undef diff 1733 /* 1734 * Rounding down may make us wake up just short 1735 * of the target, so we round up. 1736 * The formula is ceiling(slp * hz/1000000). 1737 * 32-bit arithmetic is enough for hz < 169. 1738 * XXX see tvtohz() for how to avoid overflow if hz 1739 * is large (divide by `tick' and/or arrange to 1740 * use tvtohz() if hz is large). 1741 */ 1742 slp = (long) (((u_long)slp * hz) + 999999) / 1000000; 1743 goto sleep; 1744 } 1745 if (qp->c_cc <= 0) { 1746 sleep: 1747 /* 1748 * There is no input, or not enough input and we can block. 1749 */ 1750 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH, 1751 ISSET(tp->t_state, TS_CONNECTED) ? 1752 "ttyin" : "ttyhup", (int)slp); 1753 splx(s); 1754 if (error == EWOULDBLOCK) 1755 error = 0; 1756 else if (error) 1757 return (error); 1758 /* 1759 * XXX what happens if another process eats some input 1760 * while we are asleep (not just here)? It would be 1761 * safest to detect changes and reset our state variables 1762 * (has_stime and last_cc). 1763 */ 1764 slp = 0; 1765 goto loop; 1766 } 1767 read: 1768 splx(s); 1769 /* 1770 * Input present, check for input mapping and processing. 1771 */ 1772 first = 1; 1773 if (ISSET(lflag, ICANON | ISIG)) 1774 goto slowcase; 1775 for (;;) { 1776 char ibuf[IBUFSIZ]; 1777 int icc; 1778 1779 icc = imin(uio->uio_resid, IBUFSIZ); 1780 icc = q_to_b(qp, ibuf, icc); 1781 if (icc <= 0) { 1782 if (first) 1783 goto loop; 1784 break; 1785 } 1786 error = uiomove(ibuf, icc, uio); 1787 /* 1788 * XXX if there was an error then we should ungetc() the 1789 * unmoved chars and reduce icc here. 1790 */ 1791 if (error) 1792 break; 1793 if (uio->uio_resid == 0) 1794 break; 1795 first = 0; 1796 } 1797 goto out; 1798 slowcase: 1799 for (;;) { 1800 c = getc(qp); 1801 if (c < 0) { 1802 if (first) 1803 goto loop; 1804 break; 1805 } 1806 /* 1807 * delayed suspend (^Y) 1808 */ 1809 if (CCEQ(cc[VDSUSP], c) && 1810 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) { 1811 if (tp->t_pgrp != NULL) { 1812 PGRP_LOCK(tp->t_pgrp); 1813 pgsignal(tp->t_pgrp, SIGTSTP, 1); 1814 PGRP_UNLOCK(tp->t_pgrp); 1815 } 1816 if (first) { 1817 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, 1818 "ttybg3", 0); 1819 if (error) 1820 break; 1821 goto loop; 1822 } 1823 break; 1824 } 1825 /* 1826 * Interpret EOF only in canonical mode. 1827 */ 1828 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 1829 break; 1830 /* 1831 * Give user character. 1832 */ 1833 error = ureadc(c, uio); 1834 if (error) 1835 /* XXX should ungetc(c, qp). */ 1836 break; 1837 if (uio->uio_resid == 0) 1838 break; 1839 /* 1840 * In canonical mode check for a "break character" 1841 * marking the end of a "line of input". 1842 */ 1843 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag)) 1844 break; 1845 first = 0; 1846 } 1847 1848 out: 1849 /* 1850 * Look to unblock input now that (presumably) 1851 * the input queue has gone down. 1852 */ 1853 s = spltty(); 1854 if (ISSET(tp->t_state, TS_TBLOCK) && 1855 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat) 1856 ttyunblock(tp); 1857 splx(s); 1858 1859 return (error); 1860 } 1861 1862 /* 1863 * Check the output queue on tp for space for a kernel message (from uprintf 1864 * or tprintf). Allow some space over the normal hiwater mark so we don't 1865 * lose messages due to normal flow control, but don't let the tty run amok. 1866 * Sleeps here are not interruptible, but we return prematurely if new signals 1867 * arrive. 1868 */ 1869 int 1870 ttycheckoutq(tp, wait) 1871 register struct tty *tp; 1872 int wait; 1873 { 1874 int hiwat, s; 1875 sigset_t oldmask; 1876 1877 hiwat = tp->t_ohiwat; 1878 SIGEMPTYSET(oldmask); 1879 s = spltty(); 1880 if (wait) 1881 oldmask = curproc->p_siglist; 1882 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100) 1883 while (tp->t_outq.c_cc > hiwat) { 1884 ttstart(tp); 1885 if (tp->t_outq.c_cc <= hiwat) 1886 break; 1887 if (!(wait && SIGSETEQ(curproc->p_siglist, oldmask))) { 1888 splx(s); 1889 return (0); 1890 } 1891 SET(tp->t_state, TS_SO_OLOWAT); 1892 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz); 1893 } 1894 splx(s); 1895 return (1); 1896 } 1897 1898 /* 1899 * Process a write call on a tty device. 1900 */ 1901 int 1902 ttwrite(tp, uio, flag) 1903 register struct tty *tp; 1904 register struct uio *uio; 1905 int flag; 1906 { 1907 register char *cp = NULL; 1908 register int cc, ce; 1909 register struct proc *p; 1910 int i, hiwat, cnt, error, s; 1911 char obuf[OBUFSIZ]; 1912 1913 hiwat = tp->t_ohiwat; 1914 cnt = uio->uio_resid; 1915 error = 0; 1916 cc = 0; 1917 loop: 1918 s = spltty(); 1919 if (ISSET(tp->t_state, TS_ZOMBIE)) { 1920 splx(s); 1921 if (uio->uio_resid == cnt) 1922 error = EIO; 1923 goto out; 1924 } 1925 if (!ISSET(tp->t_state, TS_CONNECTED)) { 1926 if (flag & IO_NDELAY) { 1927 splx(s); 1928 error = EWOULDBLOCK; 1929 goto out; 1930 } 1931 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1932 "ttydcd", 0); 1933 splx(s); 1934 if (error) 1935 goto out; 1936 goto loop; 1937 } 1938 splx(s); 1939 /* 1940 * Hang the process if it's in the background. 1941 */ 1942 p = curproc; 1943 sx_slock(&proctree_lock); 1944 PROC_LOCK(p); 1945 if (isbackground(p, tp) && 1946 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) && 1947 !SIGISMEMBER(p->p_sigignore, SIGTTOU) && 1948 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) { 1949 if (p->p_pgrp->pg_jobc == 0) { 1950 PROC_UNLOCK(p); 1951 sx_sunlock(&proctree_lock); 1952 error = EIO; 1953 goto out; 1954 } 1955 PROC_UNLOCK(p); 1956 PGRP_LOCK(p->p_pgrp); 1957 sx_sunlock(&proctree_lock); 1958 pgsignal(p->p_pgrp, SIGTTOU, 1); 1959 PGRP_UNLOCK(p->p_pgrp); 1960 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0); 1961 if (error) 1962 goto out; 1963 goto loop; 1964 } else { 1965 PROC_UNLOCK(p); 1966 sx_sunlock(&proctree_lock); 1967 } 1968 /* 1969 * Process the user's data in at most OBUFSIZ chunks. Perform any 1970 * output translation. Keep track of high water mark, sleep on 1971 * overflow awaiting device aid in acquiring new space. 1972 */ 1973 while (uio->uio_resid > 0 || cc > 0) { 1974 if (ISSET(tp->t_lflag, FLUSHO)) { 1975 uio->uio_resid = 0; 1976 return (0); 1977 } 1978 if (tp->t_outq.c_cc > hiwat) 1979 goto ovhiwat; 1980 /* 1981 * Grab a hunk of data from the user, unless we have some 1982 * leftover from last time. 1983 */ 1984 if (cc == 0) { 1985 cc = imin(uio->uio_resid, OBUFSIZ); 1986 cp = obuf; 1987 error = uiomove(cp, cc, uio); 1988 if (error) { 1989 cc = 0; 1990 break; 1991 } 1992 } 1993 /* 1994 * If nothing fancy need be done, grab those characters we 1995 * can handle without any of ttyoutput's processing and 1996 * just transfer them to the output q. For those chars 1997 * which require special processing (as indicated by the 1998 * bits in char_type), call ttyoutput. After processing 1999 * a hunk of data, look for FLUSHO so ^O's will take effect 2000 * immediately. 2001 */ 2002 while (cc > 0) { 2003 if (!ISSET(tp->t_oflag, OPOST)) 2004 ce = cc; 2005 else { 2006 ce = cc - scanc((u_int)cc, (u_char *)cp, 2007 char_type, CCLASSMASK); 2008 /* 2009 * If ce is zero, then we're processing 2010 * a special character through ttyoutput. 2011 */ 2012 if (ce == 0) { 2013 tp->t_rocount = 0; 2014 if (ttyoutput(*cp, tp) >= 0) { 2015 /* No Clists, wait a bit. */ 2016 ttstart(tp); 2017 if (flag & IO_NDELAY) { 2018 error = EWOULDBLOCK; 2019 goto out; 2020 } 2021 error = ttysleep(tp, &lbolt, 2022 TTOPRI|PCATCH, 2023 "ttybf1", 0); 2024 if (error) 2025 goto out; 2026 goto loop; 2027 } 2028 cp++; 2029 cc--; 2030 if (ISSET(tp->t_lflag, FLUSHO) || 2031 tp->t_outq.c_cc > hiwat) 2032 goto ovhiwat; 2033 continue; 2034 } 2035 } 2036 /* 2037 * A bunch of normal characters have been found. 2038 * Transfer them en masse to the output queue and 2039 * continue processing at the top of the loop. 2040 * If there are any further characters in this 2041 * <= OBUFSIZ chunk, the first should be a character 2042 * requiring special handling by ttyoutput. 2043 */ 2044 tp->t_rocount = 0; 2045 i = b_to_q(cp, ce, &tp->t_outq); 2046 ce -= i; 2047 tp->t_column += ce; 2048 cp += ce, cc -= ce, tk_nout += ce; 2049 tp->t_outcc += ce; 2050 if (i > 0) { 2051 /* No Clists, wait a bit. */ 2052 ttstart(tp); 2053 if (flag & IO_NDELAY) { 2054 error = EWOULDBLOCK; 2055 goto out; 2056 } 2057 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, 2058 "ttybf2", 0); 2059 if (error) 2060 goto out; 2061 goto loop; 2062 } 2063 if (ISSET(tp->t_lflag, FLUSHO) || 2064 tp->t_outq.c_cc > hiwat) 2065 break; 2066 } 2067 ttstart(tp); 2068 } 2069 out: 2070 /* 2071 * If cc is nonzero, we leave the uio structure inconsistent, as the 2072 * offset and iov pointers have moved forward, but it doesn't matter 2073 * (the call will either return short or restart with a new uio). 2074 */ 2075 uio->uio_resid += cc; 2076 return (error); 2077 2078 ovhiwat: 2079 ttstart(tp); 2080 s = spltty(); 2081 /* 2082 * This can only occur if FLUSHO is set in t_lflag, 2083 * or if ttstart/oproc is synchronous (or very fast). 2084 */ 2085 if (tp->t_outq.c_cc <= hiwat) { 2086 splx(s); 2087 goto loop; 2088 } 2089 if (flag & IO_NDELAY) { 2090 splx(s); 2091 uio->uio_resid += cc; 2092 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 2093 } 2094 SET(tp->t_state, TS_SO_OLOWAT); 2095 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri", 2096 tp->t_timeout); 2097 splx(s); 2098 if (error == EWOULDBLOCK) 2099 error = EIO; 2100 if (error) 2101 goto out; 2102 goto loop; 2103 } 2104 2105 /* 2106 * Rubout one character from the rawq of tp 2107 * as cleanly as possible. 2108 */ 2109 static void 2110 ttyrub(c, tp) 2111 register int c; 2112 register struct tty *tp; 2113 { 2114 register char *cp; 2115 register int savecol; 2116 int tabc, s; 2117 2118 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 2119 return; 2120 CLR(tp->t_lflag, FLUSHO); 2121 if (ISSET(tp->t_lflag, ECHOE)) { 2122 if (tp->t_rocount == 0) { 2123 /* 2124 * Screwed by ttwrite; retype 2125 */ 2126 ttyretype(tp); 2127 return; 2128 } 2129 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 2130 ttyrubo(tp, 2); 2131 else { 2132 CLR(c, ~TTY_CHARMASK); 2133 switch (CCLASS(c)) { 2134 case ORDINARY: 2135 ttyrubo(tp, 1); 2136 break; 2137 case BACKSPACE: 2138 case CONTROL: 2139 case NEWLINE: 2140 case RETURN: 2141 case VTAB: 2142 if (ISSET(tp->t_lflag, ECHOCTL)) 2143 ttyrubo(tp, 2); 2144 break; 2145 case TAB: 2146 if (tp->t_rocount < tp->t_rawq.c_cc) { 2147 ttyretype(tp); 2148 return; 2149 } 2150 s = spltty(); 2151 savecol = tp->t_column; 2152 SET(tp->t_state, TS_CNTTB); 2153 SET(tp->t_lflag, FLUSHO); 2154 tp->t_column = tp->t_rocol; 2155 cp = tp->t_rawq.c_cf; 2156 if (cp) 2157 tabc = *cp; /* XXX FIX NEXTC */ 2158 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 2159 ttyecho(tabc, tp); 2160 CLR(tp->t_lflag, FLUSHO); 2161 CLR(tp->t_state, TS_CNTTB); 2162 splx(s); 2163 2164 /* savecol will now be length of the tab. */ 2165 savecol -= tp->t_column; 2166 tp->t_column += savecol; 2167 if (savecol > 8) 2168 savecol = 8; /* overflow screw */ 2169 while (--savecol >= 0) 2170 (void)ttyoutput('\b', tp); 2171 break; 2172 default: /* XXX */ 2173 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 2174 (void)printf(PANICSTR, c, CCLASS(c)); 2175 #ifdef notdef 2176 panic(PANICSTR, c, CCLASS(c)); 2177 #endif 2178 } 2179 } 2180 } else if (ISSET(tp->t_lflag, ECHOPRT)) { 2181 if (!ISSET(tp->t_state, TS_ERASE)) { 2182 SET(tp->t_state, TS_ERASE); 2183 (void)ttyoutput('\\', tp); 2184 } 2185 ttyecho(c, tp); 2186 } else { 2187 ttyecho(tp->t_cc[VERASE], tp); 2188 /* 2189 * This code may be executed not only when an ERASE key 2190 * is pressed, but also when ^U (KILL) or ^W (WERASE) are. 2191 * So, I didn't think it was worthwhile to pass the extra 2192 * information (which would need an extra parameter, 2193 * changing every call) needed to distinguish the ERASE2 2194 * case from the ERASE. 2195 */ 2196 } 2197 --tp->t_rocount; 2198 } 2199 2200 /* 2201 * Back over cnt characters, erasing them. 2202 */ 2203 static void 2204 ttyrubo(tp, cnt) 2205 register struct tty *tp; 2206 int cnt; 2207 { 2208 2209 while (cnt-- > 0) { 2210 (void)ttyoutput('\b', tp); 2211 (void)ttyoutput(' ', tp); 2212 (void)ttyoutput('\b', tp); 2213 } 2214 } 2215 2216 /* 2217 * ttyretype -- 2218 * Reprint the rawq line. Note, it is assumed that c_cc has already 2219 * been checked. 2220 */ 2221 static void 2222 ttyretype(tp) 2223 register struct tty *tp; 2224 { 2225 register char *cp; 2226 int s, c; 2227 2228 /* Echo the reprint character. */ 2229 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 2230 ttyecho(tp->t_cc[VREPRINT], tp); 2231 2232 (void)ttyoutput('\n', tp); 2233 2234 /* 2235 * XXX 2236 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 2237 * BIT OF FIRST CHAR. 2238 */ 2239 s = spltty(); 2240 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 2241 cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 2242 ttyecho(c, tp); 2243 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 2244 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 2245 ttyecho(c, tp); 2246 CLR(tp->t_state, TS_ERASE); 2247 splx(s); 2248 2249 tp->t_rocount = tp->t_rawq.c_cc; 2250 tp->t_rocol = 0; 2251 } 2252 2253 /* 2254 * Echo a typed character to the terminal. 2255 */ 2256 static void 2257 ttyecho(c, tp) 2258 register int c; 2259 register struct tty *tp; 2260 { 2261 2262 if (!ISSET(tp->t_state, TS_CNTTB)) 2263 CLR(tp->t_lflag, FLUSHO); 2264 if ((!ISSET(tp->t_lflag, ECHO) && 2265 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) || 2266 ISSET(tp->t_lflag, EXTPROC)) 2267 return; 2268 if (ISSET(tp->t_lflag, ECHOCTL) && 2269 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') || 2270 ISSET(c, TTY_CHARMASK) == 0177)) { 2271 (void)ttyoutput('^', tp); 2272 CLR(c, ~TTY_CHARMASK); 2273 if (c == 0177) 2274 c = '?'; 2275 else 2276 c += 'A' - 1; 2277 } 2278 (void)ttyoutput(c, tp); 2279 } 2280 2281 /* 2282 * Wake up any readers on a tty. 2283 */ 2284 void 2285 ttwakeup(tp) 2286 register struct tty *tp; 2287 { 2288 2289 if (SEL_WAITING(&tp->t_rsel)) 2290 selwakeup(&tp->t_rsel); 2291 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2292 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2293 wakeup(TSA_HUP_OR_INPUT(tp)); 2294 KNOTE(&tp->t_rsel.si_note, 0); 2295 } 2296 2297 /* 2298 * Wake up any writers on a tty. 2299 */ 2300 void 2301 ttwwakeup(tp) 2302 register struct tty *tp; 2303 { 2304 2305 if (SEL_WAITING(&tp->t_wsel) && tp->t_outq.c_cc <= tp->t_olowat) 2306 selwakeup(&tp->t_wsel); 2307 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL) 2308 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL)); 2309 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == 2310 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { 2311 CLR(tp->t_state, TS_SO_OCOMPLETE); 2312 wakeup(TSA_OCOMPLETE(tp)); 2313 } 2314 if (ISSET(tp->t_state, TS_SO_OLOWAT) && 2315 tp->t_outq.c_cc <= tp->t_olowat) { 2316 CLR(tp->t_state, TS_SO_OLOWAT); 2317 wakeup(TSA_OLOWAT(tp)); 2318 } 2319 KNOTE(&tp->t_wsel.si_note, 0); 2320 } 2321 2322 /* 2323 * Look up a code for a specified speed in a conversion table; 2324 * used by drivers to map software speed values to hardware parameters. 2325 */ 2326 int 2327 ttspeedtab(speed, table) 2328 int speed; 2329 register struct speedtab *table; 2330 { 2331 2332 for ( ; table->sp_speed != -1; table++) 2333 if (table->sp_speed == speed) 2334 return (table->sp_code); 2335 return (-1); 2336 } 2337 2338 /* 2339 * Set input and output watermarks and buffer sizes. For input, the 2340 * high watermark is about one second's worth of input above empty, the 2341 * low watermark is slightly below high water, and the buffer size is a 2342 * driver-dependent amount above high water. For output, the watermarks 2343 * are near the ends of the buffer, with about 1 second's worth of input 2344 * between them. All this only applies to the standard line discipline. 2345 */ 2346 void 2347 ttsetwater(tp) 2348 struct tty *tp; 2349 { 2350 register int cps, ttmaxhiwat, x; 2351 2352 /* Input. */ 2353 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512); 2354 switch (tp->t_ispeedwat) { 2355 case (speed_t)-1: 2356 cps = tp->t_ispeed / 10; 2357 break; 2358 case 0: 2359 /* 2360 * This case is for old drivers that don't know about 2361 * t_ispeedwat. Arrange for them to get the old buffer 2362 * sizes and watermarks. 2363 */ 2364 cps = TTYHOG - 2 * 256; 2365 tp->t_ififosize = 2 * 256; 2366 break; 2367 default: 2368 cps = tp->t_ispeedwat / 10; 2369 break; 2370 } 2371 tp->t_ihiwat = cps; 2372 tp->t_ilowat = 7 * cps / 8; 2373 x = cps + tp->t_ififosize; 2374 clist_alloc_cblocks(&tp->t_rawq, x, x); 2375 2376 /* Output. */ 2377 switch (tp->t_ospeedwat) { 2378 case (speed_t)-1: 2379 cps = tp->t_ospeed / 10; 2380 ttmaxhiwat = 2 * TTMAXHIWAT; 2381 break; 2382 case 0: 2383 cps = tp->t_ospeed / 10; 2384 ttmaxhiwat = TTMAXHIWAT; 2385 break; 2386 default: 2387 cps = tp->t_ospeedwat / 10; 2388 ttmaxhiwat = 8 * TTMAXHIWAT; 2389 break; 2390 } 2391 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 2392 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 2393 x += cps; 2394 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */ 2395 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */ 2396 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */ 2397 x += OBUFSIZ + 100; 2398 clist_alloc_cblocks(&tp->t_outq, x, x); 2399 #undef CLAMP 2400 } 2401 2402 /* 2403 * Report on state of foreground process group. 2404 */ 2405 void 2406 ttyinfo(tp) 2407 register struct tty *tp; 2408 { 2409 register struct proc *p, *pick; 2410 struct timeval utime, stime; 2411 const char *stmp; 2412 long ltmp; 2413 int tmp; 2414 struct thread *td; 2415 2416 if (ttycheckoutq(tp,0) == 0) 2417 return; 2418 2419 /* Print load average. */ 2420 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 2421 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 2422 2423 if (tp->t_session == NULL) 2424 ttyprintf(tp, "not a controlling terminal\n"); 2425 else if (tp->t_pgrp == NULL) 2426 ttyprintf(tp, "no foreground process group\n"); 2427 else { 2428 PGRP_LOCK(tp->t_pgrp); 2429 if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0) { 2430 PGRP_UNLOCK(tp->t_pgrp); 2431 ttyprintf(tp, "empty foreground process group\n"); 2432 } else { 2433 mtx_lock_spin(&sched_lock); 2434 2435 /* Pick interesting process. */ 2436 for (pick = NULL; p != 0; p = LIST_NEXT(p, p_pglist)) 2437 if (proc_compare(pick, p)) 2438 pick = p; 2439 PGRP_UNLOCK(tp->t_pgrp); 2440 2441 td = FIRST_THREAD_IN_PROC(pick); 2442 stmp = pick->p_stat == SRUN ? "running" : /* XXXKSE */ 2443 td->td_wmesg ? td->td_wmesg : "iowait"; 2444 calcru(pick, &utime, &stime, NULL); 2445 ltmp = pick->p_stat == SIDL || pick->p_stat == SWAIT || 2446 pick->p_stat == SZOMB ? 0 : 2447 pgtok(vmspace_resident_count(pick->p_vmspace)); 2448 mtx_unlock_spin(&sched_lock); 2449 2450 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 2451 stmp); 2452 2453 /* Print user time. */ 2454 ttyprintf(tp, "%ld.%02ldu ", 2455 utime.tv_sec, utime.tv_usec / 10000); 2456 2457 /* Print system time. */ 2458 ttyprintf(tp, "%ld.%02lds ", 2459 (long)stime.tv_sec, stime.tv_usec / 10000); 2460 2461 /* Print percentage cpu, resident set size. */ 2462 ttyprintf(tp, "%d%% %ldk\n", tmp / 100, ltmp); 2463 2464 } 2465 } 2466 tp->t_rocount = 0; /* so pending input will be retyped if BS */ 2467 } 2468 2469 /* 2470 * Returns 1 if p2 is "better" than p1 2471 * 2472 * The algorithm for picking the "interesting" process is thus: 2473 * 2474 * 1) Only foreground processes are eligible - implied. 2475 * 2) Runnable processes are favored over anything else. The runner 2476 * with the highest cpu utilization is picked (p_estcpu). Ties are 2477 * broken by picking the highest pid. 2478 * 3) The sleeper with the shortest sleep time is next. With ties, 2479 * we pick out just "short-term" sleepers (P_SINTR == 0). 2480 * 4) Further ties are broken by picking the highest pid. 2481 */ 2482 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 2483 #define TESTAB(a, b) ((a)<<1 | (b)) 2484 #define ONLYA 2 2485 #define ONLYB 1 2486 #define BOTH 3 2487 2488 static int 2489 proc_compare(p1, p2) 2490 register struct proc *p1, *p2; 2491 { 2492 2493 int esta, estb; 2494 struct ksegrp *kg; 2495 mtx_assert(&sched_lock, MA_OWNED); 2496 if (p1 == NULL) 2497 return (1); 2498 2499 /* 2500 * see if at least one of them is runnable 2501 */ 2502 switch (TESTAB(ISRUN(p1), ISRUN(p2))) { 2503 case ONLYA: 2504 return (0); 2505 case ONLYB: 2506 return (1); 2507 case BOTH: 2508 /* 2509 * tie - favor one with highest recent cpu utilization 2510 */ 2511 esta = estb = 0; 2512 FOREACH_KSEGRP_IN_PROC(p1,kg) { 2513 esta += kg->kg_estcpu; 2514 } 2515 FOREACH_KSEGRP_IN_PROC(p2,kg) { 2516 estb += kg->kg_estcpu; 2517 } 2518 if (estb > esta) 2519 return (1); 2520 if (esta > estb) 2521 return (0); 2522 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2523 } 2524 /* 2525 * weed out zombies 2526 */ 2527 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { 2528 case ONLYA: 2529 return (1); 2530 case ONLYB: 2531 return (0); 2532 case BOTH: 2533 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2534 } 2535 2536 #if 0 /* XXXKSE */ 2537 /* 2538 * pick the one with the smallest sleep time 2539 */ 2540 if (p2->p_slptime > p1->p_slptime) 2541 return (0); 2542 if (p1->p_slptime > p2->p_slptime) 2543 return (1); 2544 /* 2545 * favor one sleeping in a non-interruptible sleep 2546 */ 2547 if (p1->p_sflag & PS_SINTR && (p2->p_sflag & PS_SINTR) == 0) 2548 return (1); 2549 if (p2->p_sflag & PS_SINTR && (p1->p_sflag & PS_SINTR) == 0) 2550 return (0); 2551 #endif 2552 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 2553 } 2554 2555 /* 2556 * Output char to tty; console putchar style. 2557 */ 2558 int 2559 tputchar(c, tp) 2560 int c; 2561 struct tty *tp; 2562 { 2563 register int s; 2564 2565 s = spltty(); 2566 if (!ISSET(tp->t_state, TS_CONNECTED)) { 2567 splx(s); 2568 return (-1); 2569 } 2570 if (c == '\n') 2571 (void)ttyoutput('\r', tp); 2572 (void)ttyoutput(c, tp); 2573 ttstart(tp); 2574 splx(s); 2575 return (0); 2576 } 2577 2578 /* 2579 * Sleep on chan, returning ERESTART if tty changed while we napped and 2580 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If 2581 * the tty is revoked, restarting a pending call will redo validation done 2582 * at the start of the call. 2583 */ 2584 int 2585 ttysleep(tp, chan, pri, wmesg, timo) 2586 struct tty *tp; 2587 void *chan; 2588 int pri, timo; 2589 char *wmesg; 2590 { 2591 int error; 2592 int gen; 2593 2594 gen = tp->t_gen; 2595 error = tsleep(chan, pri, wmesg, timo); 2596 if (error) 2597 return (error); 2598 return (tp->t_gen == gen ? 0 : ERESTART); 2599 } 2600 2601 /* 2602 * Allocate a tty struct. Clists in the struct will be allocated by 2603 * ttyopen(). 2604 */ 2605 struct tty * 2606 ttymalloc(tp) 2607 struct tty *tp; 2608 { 2609 2610 if (tp) 2611 return(tp); 2612 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK | M_ZERO); 2613 ttyregister(tp); 2614 return (tp); 2615 } 2616 2617 #if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */ 2618 /* 2619 * Free a tty struct. Clists in the struct should have been freed by 2620 * ttyclose(). 2621 */ 2622 void 2623 ttyfree(tp) 2624 struct tty *tp; 2625 { 2626 free(tp, M_TTYS); 2627 } 2628 #endif /* 0 */ 2629 2630 void 2631 ttyregister(tp) 2632 struct tty *tp; 2633 { 2634 tp->t_timeout = -1; 2635 SLIST_INSERT_HEAD(&tty_list, tp, t_list); 2636 } 2637 2638 static int 2639 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS) 2640 { 2641 int error; 2642 struct tty *tp, t; 2643 SLIST_FOREACH(tp, &tty_list, t_list) { 2644 t = *tp; 2645 if (t.t_dev) 2646 t.ttyu.t_udev = dev2udev(t.t_dev); 2647 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t)); 2648 if (error) 2649 return (error); 2650 } 2651 return (0); 2652 } 2653 2654 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD, 2655 0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys"); 2656 SYSCTL_LONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD, 2657 &tk_nin, 0, "Total TTY in characters"); 2658 SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD, 2659 &tk_nout, 0, "Total TTY out characters"); 2660 2661 void 2662 nottystop(tp, rw) 2663 struct tty *tp; 2664 int rw; 2665 { 2666 2667 return; 2668 } 2669 2670 int 2671 ttyread(dev, uio, flag) 2672 dev_t dev; 2673 struct uio *uio; 2674 int flag; 2675 { 2676 struct tty *tp; 2677 2678 tp = dev->si_tty; 2679 if (tp == NULL) 2680 return (ENODEV); 2681 return ((*linesw[tp->t_line].l_read)(tp, uio, flag)); 2682 } 2683 2684 int 2685 ttywrite(dev, uio, flag) 2686 dev_t dev; 2687 struct uio *uio; 2688 int flag; 2689 { 2690 struct tty *tp; 2691 2692 tp = dev->si_tty; 2693 if (tp == NULL) 2694 return (ENODEV); 2695 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2696 } 2697