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