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