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