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