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