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