Home | History | Annotate | Line # | Download | only in kern
tty.c revision 1.225
      1  1.225        ad /*	$NetBSD: tty.c,v 1.225 2008/06/16 09:51:14 ad Exp $	*/
      2  1.215        ad 
      3  1.215        ad /*-
      4  1.215        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.215        ad  * All rights reserved.
      6  1.215        ad  *
      7  1.215        ad  * Redistribution and use in source and binary forms, with or without
      8  1.215        ad  * modification, are permitted provided that the following conditions
      9  1.215        ad  * are met:
     10  1.215        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.215        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.215        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.215        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.215        ad  *    documentation and/or other materials provided with the distribution.
     15  1.215        ad  *
     16  1.215        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.215        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.215        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.215        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.215        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.215        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.215        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.215        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.215        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.215        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.215        ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.215        ad  */
     28   1.49       cgd 
     29   1.49       cgd /*-
     30   1.49       cgd  * Copyright (c) 1982, 1986, 1990, 1991, 1993
     31   1.49       cgd  *	The Regents of the University of California.  All rights reserved.
     32   1.49       cgd  * (c) UNIX System Laboratories, Inc.
     33   1.49       cgd  * All or some portions of this file are derived from material licensed
     34   1.49       cgd  * to the University of California by American Telephone and Telegraph
     35   1.49       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     36   1.49       cgd  * the permission of UNIX System Laboratories, Inc.
     37   1.49       cgd  *
     38   1.49       cgd  * Redistribution and use in source and binary forms, with or without
     39   1.49       cgd  * modification, are permitted provided that the following conditions
     40   1.49       cgd  * are met:
     41   1.49       cgd  * 1. Redistributions of source code must retain the above copyright
     42   1.49       cgd  *    notice, this list of conditions and the following disclaimer.
     43   1.49       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     44   1.49       cgd  *    notice, this list of conditions and the following disclaimer in the
     45   1.49       cgd  *    documentation and/or other materials provided with the distribution.
     46  1.155       agc  * 3. Neither the name of the University nor the names of its contributors
     47   1.49       cgd  *    may be used to endorse or promote products derived from this software
     48   1.49       cgd  *    without specific prior written permission.
     49   1.49       cgd  *
     50   1.49       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51   1.49       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52   1.49       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53   1.49       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54   1.49       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55   1.49       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56   1.49       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57   1.49       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58   1.49       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59   1.49       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60   1.49       cgd  * SUCH DAMAGE.
     61   1.49       cgd  *
     62  1.105      fvdl  *	@(#)tty.c	8.13 (Berkeley) 1/9/95
     63   1.49       cgd  */
     64  1.129     lukem 
     65  1.129     lukem #include <sys/cdefs.h>
     66  1.225        ad __KERNEL_RCSID(0, "$NetBSD: tty.c,v 1.225 2008/06/16 09:51:14 ad Exp $");
     67   1.49       cgd 
     68   1.49       cgd #include <sys/param.h>
     69   1.49       cgd #include <sys/systm.h>
     70   1.49       cgd #include <sys/ioctl.h>
     71   1.49       cgd #include <sys/proc.h>
     72   1.49       cgd #define	TTYDEFCHARS
     73   1.49       cgd #include <sys/tty.h>
     74   1.49       cgd #undef	TTYDEFCHARS
     75   1.49       cgd #include <sys/file.h>
     76   1.49       cgd #include <sys/conf.h>
     77   1.49       cgd #include <sys/dkstat.h>
     78   1.49       cgd #include <sys/uio.h>
     79   1.49       cgd #include <sys/kernel.h>
     80   1.49       cgd #include <sys/vnode.h>
     81   1.49       cgd #include <sys/syslog.h>
     82   1.49       cgd #include <sys/malloc.h>
     83  1.216        ad #include <sys/kmem.h>
     84   1.65  christos #include <sys/signalvar.h>
     85   1.65  christos #include <sys/resourcevar.h>
     86   1.74   mycroft #include <sys/poll.h>
     87  1.149  christos #include <sys/kprintf.h>
     88  1.151  christos #include <sys/namei.h>
     89  1.159    atatat #include <sys/sysctl.h>
     90  1.182      elad #include <sys/kauth.h>
     91  1.201        ad #include <sys/intr.h>
     92  1.149  christos 
     93  1.149  christos #include <machine/stdarg.h>
     94   1.49       cgd 
     95  1.126     lukem static int	ttnread(struct tty *);
     96  1.126     lukem static void	ttyblock(struct tty *);
     97  1.126     lukem static void	ttyecho(int, struct tty *);
     98  1.126     lukem static void	ttyrubo(struct tty *, int);
     99  1.149  christos static void	ttyprintf_nolock(struct tty *, const char *fmt, ...)
    100  1.149  christos     __attribute__((__format__(__printf__,2,3)));
    101  1.126     lukem static int	proc_compare(struct proc *, struct proc *);
    102  1.201        ad static void	ttysigintr(void *);
    103   1.49       cgd 
    104   1.49       cgd /* Symbolic sleep message strings. */
    105   1.98   mycroft const char	ttclos[] = "ttycls";
    106   1.98   mycroft const char	ttopen[] = "ttyopn";
    107   1.98   mycroft const char	ttybg[] = "ttybg";
    108   1.98   mycroft const char	ttyin[] = "ttyin";
    109   1.98   mycroft const char	ttyout[] = "ttyout";
    110   1.49       cgd 
    111   1.49       cgd /*
    112   1.83   mycroft  * Used to determine whether we still have a connection.  This is true in
    113   1.83   mycroft  * one of 3 cases:
    114   1.83   mycroft  * 1) We have carrier.
    115   1.83   mycroft  * 2) It's a locally attached terminal, and we are therefore ignoring carrier.
    116   1.83   mycroft  * 3) We're using a flow control mechanism that overloads the carrier signal.
    117   1.83   mycroft  */
    118   1.83   mycroft #define	CONNECTED(tp)	(ISSET(tp->t_state, TS_CARR_ON) ||	\
    119   1.83   mycroft 			 ISSET(tp->t_cflag, CLOCAL | MDMBUF))
    120   1.83   mycroft 
    121   1.83   mycroft /*
    122   1.49       cgd  * Table with character classes and parity. The 8th bit indicates parity,
    123   1.49       cgd  * the 7th bit indicates the character is an alphameric or underscore (for
    124   1.49       cgd  * ALTWERASE), and the low 6 bits indicate delay type.  If the low 6 bits
    125   1.49       cgd  * are 0 then the character needs no special processing on output; classes
    126   1.49       cgd  * other than 0 might be translated or (not currently) require delays.
    127   1.49       cgd  */
    128   1.49       cgd #define	E	0x00	/* Even parity. */
    129   1.49       cgd #define	O	0x80	/* Odd parity. */
    130   1.49       cgd #define	PARITY(c)	(char_type[c] & O)
    131   1.49       cgd 
    132   1.49       cgd #define	ALPHA	0x40	/* Alpha or underscore. */
    133   1.49       cgd #define	ISALPHA(c)	(char_type[(c) & TTY_CHARMASK] & ALPHA)
    134   1.49       cgd 
    135   1.49       cgd #define	CCLASSMASK	0x3f
    136   1.49       cgd #define	CCLASS(c)	(char_type[c] & CCLASSMASK)
    137   1.49       cgd 
    138   1.49       cgd #define	BS	BACKSPACE
    139   1.49       cgd #define	CC	CONTROL
    140   1.49       cgd #define	CR	RETURN
    141   1.49       cgd #define	NA	ORDINARY | ALPHA
    142   1.49       cgd #define	NL	NEWLINE
    143   1.49       cgd #define	NO	ORDINARY
    144   1.49       cgd #define	TB	TAB
    145   1.49       cgd #define	VT	VTAB
    146   1.49       cgd 
    147  1.146    simonb unsigned char const char_type[] = {
    148   1.49       cgd 	E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC,	/* nul - bel */
    149  1.137    simonb 	O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC,	/* bs - si */
    150  1.138     enami 	O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC,	/* dle - etb */
    151  1.138     enami 	E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC,	/* can - us */
    152  1.138     enami 	O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO,	/* sp - ' */
    153  1.138     enami 	E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO,	/* ( - / */
    154  1.138     enami 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* 0 - 7 */
    155  1.138     enami 	O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO,	/* 8 - ? */
    156  1.138     enami 	O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA,	/* @ - G */
    157  1.138     enami 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* H - O */
    158  1.138     enami 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* P - W */
    159  1.138     enami 	O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA,	/* X - _ */
    160  1.138     enami 	E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA,	/* ` - g */
    161  1.138     enami 	O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA,	/* h - o */
    162  1.138     enami 	O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA,	/* p - w */
    163  1.138     enami 	E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC,	/* x - del */
    164   1.49       cgd 	/*
    165   1.49       cgd 	 * Meta chars; should be settable per character set;
    166   1.49       cgd 	 * for now, treat them all as normal characters.
    167   1.49       cgd 	 */
    168   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    169   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    170   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    171   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    172   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    173   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    174   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    175   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    176   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    177   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    178   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    179   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    180   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    181   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    182   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    183   1.49       cgd 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
    184   1.49       cgd };
    185   1.49       cgd #undef	BS
    186   1.49       cgd #undef	CC
    187   1.49       cgd #undef	CR
    188   1.49       cgd #undef	NA
    189   1.49       cgd #undef	NL
    190   1.49       cgd #undef	NO
    191   1.49       cgd #undef	TB
    192   1.49       cgd #undef	VT
    193   1.49       cgd 
    194  1.201        ad static struct ttylist_head tty_sigqueue = TAILQ_HEAD_INITIALIZER(tty_sigqueue);
    195  1.201        ad static void *tty_sigsih;
    196  1.201        ad 
    197  1.169   thorpej struct ttylist_head ttylist = TAILQ_HEAD_INITIALIZER(ttylist);
    198   1.69       mrg int tty_count;
    199  1.201        ad kmutex_t tty_lock;
    200   1.69       mrg 
    201  1.179     perry uint64_t tk_cancc;
    202  1.179     perry uint64_t tk_nin;
    203  1.179     perry uint64_t tk_nout;
    204  1.179     perry uint64_t tk_rawcc;
    205  1.159    atatat 
    206  1.159    atatat SYSCTL_SETUP(sysctl_kern_tkstat_setup, "sysctl kern.tkstat subtree setup")
    207  1.159    atatat {
    208  1.159    atatat 
    209  1.165    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    210  1.165    atatat 		       CTLFLAG_PERMANENT,
    211  1.159    atatat 		       CTLTYPE_NODE, "kern", NULL,
    212  1.159    atatat 		       NULL, 0, NULL, 0,
    213  1.159    atatat 		       CTL_KERN, CTL_EOL);
    214  1.165    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    215  1.165    atatat 		       CTLFLAG_PERMANENT,
    216  1.168    atatat 		       CTLTYPE_NODE, "tkstat",
    217  1.168    atatat 		       SYSCTL_DESCR("Number of characters sent and and "
    218  1.168    atatat 				    "received on ttys"),
    219  1.159    atatat 		       NULL, 0, NULL, 0,
    220  1.159    atatat 		       CTL_KERN, KERN_TKSTAT, CTL_EOL);
    221  1.159    atatat 
    222  1.165    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    223  1.165    atatat 		       CTLFLAG_PERMANENT,
    224  1.168    atatat 		       CTLTYPE_QUAD, "nin",
    225  1.168    atatat 		       SYSCTL_DESCR("Total number of tty input characters"),
    226  1.159    atatat 		       NULL, 0, &tk_nin, 0,
    227  1.159    atatat 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_NIN, CTL_EOL);
    228  1.165    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    229  1.165    atatat 		       CTLFLAG_PERMANENT,
    230  1.168    atatat 		       CTLTYPE_QUAD, "nout",
    231  1.168    atatat 		       SYSCTL_DESCR("Total number of tty output characters"),
    232  1.159    atatat 		       NULL, 0, &tk_nout, 0,
    233  1.159    atatat 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_NOUT, CTL_EOL);
    234  1.165    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    235  1.165    atatat 		       CTLFLAG_PERMANENT,
    236  1.168    atatat 		       CTLTYPE_QUAD, "cancc",
    237  1.168    atatat 		       SYSCTL_DESCR("Number of canonical tty input characters"),
    238  1.159    atatat 		       NULL, 0, &tk_cancc, 0,
    239  1.159    atatat 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_CANCC, CTL_EOL);
    240  1.165    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    241  1.165    atatat 		       CTLFLAG_PERMANENT,
    242  1.168    atatat 		       CTLTYPE_QUAD, "rawcc",
    243  1.168    atatat 		       SYSCTL_DESCR("Number of raw tty input characters"),
    244  1.159    atatat 		       NULL, 0, &tk_rawcc, 0,
    245  1.159    atatat 		       CTL_KERN, KERN_TKSTAT, KERN_TKSTAT_RAWCC, CTL_EOL);
    246  1.159    atatat }
    247  1.111   thorpej 
    248  1.107   mycroft int
    249  1.126     lukem ttyopen(struct tty *tp, int dialout, int nonblock)
    250  1.107   mycroft {
    251  1.201        ad 	int	error;
    252  1.107   mycroft 
    253  1.147        pk 	error = 0;
    254  1.147        pk 
    255  1.201        ad 	mutex_spin_enter(&tty_lock);
    256  1.107   mycroft 
    257  1.107   mycroft 	if (dialout) {
    258  1.107   mycroft 		/*
    259  1.107   mycroft 		 * If the device is already open for non-dialout, fail.
    260  1.107   mycroft 		 * Otherwise, set TS_DIALOUT to block any pending non-dialout
    261  1.107   mycroft 		 * opens.
    262  1.107   mycroft 		 */
    263  1.107   mycroft 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    264  1.107   mycroft 		    !ISSET(tp->t_state, TS_DIALOUT)) {
    265  1.147        pk 			error = EBUSY;
    266  1.147        pk 			goto out;
    267  1.107   mycroft 		}
    268  1.107   mycroft 		SET(tp->t_state, TS_DIALOUT);
    269  1.107   mycroft 	} else {
    270  1.107   mycroft 		if (!nonblock) {
    271  1.107   mycroft 			/*
    272  1.107   mycroft 			 * Wait for carrier.  Also wait for any dialout
    273  1.107   mycroft 			 * processes to close the tty first.
    274  1.107   mycroft 			 */
    275  1.107   mycroft 			while (ISSET(tp->t_state, TS_DIALOUT) ||
    276  1.147        pk 			       !CONNECTED(tp)) {
    277  1.107   mycroft 				tp->t_wopen++;
    278  1.224        ad 				error = ttysleep(tp, &tp->t_rawcv, true, 0);
    279  1.107   mycroft 				tp->t_wopen--;
    280  1.147        pk 				if (error)
    281  1.147        pk 					goto out;
    282  1.107   mycroft 			}
    283  1.107   mycroft 		} else {
    284  1.107   mycroft 			/*
    285  1.107   mycroft 			 * Don't allow a non-blocking non-dialout open if the
    286  1.107   mycroft 			 * device is already open for dialout.
    287  1.107   mycroft 			 */
    288  1.138     enami 			if (ISSET(tp->t_state, TS_DIALOUT)) {
    289  1.147        pk 				error = EBUSY;
    290  1.147        pk 				goto out;
    291  1.107   mycroft 			}
    292  1.107   mycroft 		}
    293  1.107   mycroft 	}
    294  1.107   mycroft 
    295  1.147        pk out:
    296  1.201        ad 	mutex_spin_exit(&tty_lock);
    297  1.147        pk 	return (error);
    298  1.107   mycroft }
    299  1.107   mycroft 
    300   1.49       cgd /*
    301   1.49       cgd  * Initial open of tty, or (re)entry to standard tty line discipline.
    302   1.49       cgd  */
    303   1.49       cgd int
    304  1.126     lukem ttylopen(dev_t device, struct tty *tp)
    305   1.49       cgd {
    306   1.49       cgd 
    307  1.201        ad 	mutex_spin_enter(&tty_lock);
    308   1.49       cgd 	tp->t_dev = device;
    309   1.49       cgd 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    310   1.49       cgd 		SET(tp->t_state, TS_ISOPEN);
    311  1.109     perry 		memset(&tp->t_winsize, 0, sizeof(tp->t_winsize));
    312   1.66  christos #ifdef COMPAT_OLDTTY
    313   1.50   mycroft 		tp->t_flags = 0;
    314   1.50   mycroft #endif
    315   1.49       cgd 	}
    316  1.201        ad 	mutex_spin_exit(&tty_lock);
    317   1.49       cgd 	return (0);
    318   1.49       cgd }
    319   1.49       cgd 
    320   1.49       cgd /*
    321   1.49       cgd  * Handle close() on a tty line: flush and set to initial state,
    322   1.49       cgd  * bumping generation number so that pending read/write calls
    323   1.49       cgd  * can detect recycling of the tty.
    324   1.49       cgd  */
    325   1.49       cgd int
    326  1.126     lukem ttyclose(struct tty *tp)
    327   1.49       cgd {
    328   1.49       cgd 	extern struct tty *constty;	/* Temporary virtual console. */
    329  1.201        ad 	struct session *sess;
    330  1.147        pk 
    331  1.201        ad 	mutex_spin_enter(&tty_lock);
    332   1.49       cgd 
    333   1.49       cgd 	if (constty == tp)
    334   1.49       cgd 		constty = NULL;
    335   1.49       cgd 
    336   1.49       cgd 	ttyflush(tp, FREAD | FWRITE);
    337   1.49       cgd 
    338   1.49       cgd 	tp->t_gen++;
    339   1.49       cgd 	tp->t_pgrp = NULL;
    340  1.190        ad 	tp->t_state = 0;
    341  1.209        ad 	sess = tp->t_session;
    342  1.209        ad 	tp->t_session = NULL;
    343  1.190        ad 
    344  1.201        ad 	mutex_spin_exit(&tty_lock);
    345  1.190        ad 
    346  1.220        ad 	mutex_enter(proc_lock);
    347  1.209        ad 	if (sess != NULL)
    348  1.209        ad 		SESSRELE(sess);
    349  1.220        ad 	mutex_exit(proc_lock);
    350  1.190        ad 
    351   1.49       cgd 	return (0);
    352   1.49       cgd }
    353   1.49       cgd 
    354   1.49       cgd #define	FLUSHQ(q) {							\
    355   1.49       cgd 	if ((q)->c_cc)							\
    356   1.49       cgd 		ndflush(q, (q)->c_cc);					\
    357   1.49       cgd }
    358   1.49       cgd 
    359   1.88    kleink /*
    360   1.88    kleink  * This macro is used in canonical mode input processing, where a read
    361   1.88    kleink  * request shall not return unless a 'line delimiter' ('\n') or 'break'
    362   1.88    kleink  * (EOF, EOL, EOL2) character (or a signal) has been received. As EOL2
    363   1.88    kleink  * is an extension to the POSIX.1 defined set of special characters,
    364   1.88    kleink  * recognize it only if IEXTEN is set in the set of local flags.
    365   1.88    kleink  */
    366   1.85    kleink #define	TTBREAKC(c, lflg)						\
    367   1.87       cgd 	((c) == '\n' || (((c) == cc[VEOF] || (c) == cc[VEOL] ||		\
    368   1.85    kleink 	((c) == cc[VEOL2] && ISSET(lflg, IEXTEN))) && (c) != _POSIX_VDISABLE))
    369   1.49       cgd 
    370   1.49       cgd 
    371  1.147        pk 
    372   1.49       cgd /*
    373  1.147        pk  * ttyinput() helper.
    374  1.201        ad  * Call with the tty lock held.
    375   1.49       cgd  */
    376  1.147        pk static int
    377  1.147        pk ttyinput_wlock(int c, struct tty *tp)
    378   1.49       cgd {
    379  1.126     lukem 	int	iflag, lflag, i, error;
    380  1.126     lukem 	u_char	*cc;
    381   1.78  christos 
    382  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
    383  1.201        ad 
    384   1.78  christos 	/*
    385   1.49       cgd 	 * If input is pending take it first.
    386   1.49       cgd 	 */
    387   1.49       cgd 	lflag = tp->t_lflag;
    388   1.49       cgd 	if (ISSET(lflag, PENDIN))
    389   1.49       cgd 		ttypend(tp);
    390   1.49       cgd 	/*
    391   1.49       cgd 	 * Gather stats.
    392   1.49       cgd 	 */
    393   1.49       cgd 	if (ISSET(lflag, ICANON)) {
    394   1.49       cgd 		++tk_cancc;
    395   1.49       cgd 		++tp->t_cancc;
    396   1.49       cgd 	} else {
    397   1.49       cgd 		++tk_rawcc;
    398   1.49       cgd 		++tp->t_rawcc;
    399   1.49       cgd 	}
    400   1.49       cgd 	++tk_nin;
    401   1.49       cgd 
    402   1.49       cgd 	cc = tp->t_cc;
    403   1.94    kleink 
    404   1.94    kleink 	/*
    405   1.94    kleink 	 * Handle exceptional conditions (break, parity, framing).
    406   1.94    kleink 	 */
    407   1.49       cgd 	iflag = tp->t_iflag;
    408   1.65  christos 	if ((error = (ISSET(c, TTY_ERRORMASK))) != 0) {
    409   1.49       cgd 		CLR(c, TTY_ERRORMASK);
    410   1.94    kleink 		if (ISSET(error, TTY_FE) && c == 0) {		/* Break. */
    411   1.49       cgd 			if (ISSET(iflag, IGNBRK))
    412   1.94    kleink 				return (0);
    413   1.94    kleink 			else if (ISSET(iflag, BRKINT)) {
    414   1.94    kleink 				ttyflush(tp, FREAD | FWRITE);
    415  1.201        ad 				ttysig(tp, TTYSIG_PG1, SIGINT);
    416   1.94    kleink 				return (0);
    417  1.138     enami 			} else if (ISSET(iflag, PARMRK))
    418   1.49       cgd 				goto parmrk;
    419  1.138     enami 		} else if ((ISSET(error, TTY_PE) && ISSET(iflag, INPCK)) ||
    420   1.94    kleink 		    ISSET(error, TTY_FE)) {
    421   1.49       cgd 			if (ISSET(iflag, IGNPAR))
    422   1.94    kleink 				return (0);
    423   1.49       cgd 			else if (ISSET(iflag, PARMRK)) {
    424  1.126     lukem  parmrk:			(void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
    425   1.94    kleink 				(void)putc(0    | TTY_QUOTE, &tp->t_rawq);
    426   1.94    kleink 				(void)putc(c    | TTY_QUOTE, &tp->t_rawq);
    427   1.94    kleink 				return (0);
    428  1.138     enami 			} else
    429   1.49       cgd 				c = 0;
    430   1.49       cgd 		}
    431  1.138     enami 	} else if (c == 0377 &&
    432   1.94    kleink 	    ISSET(iflag, ISTRIP|IGNPAR|INPCK|PARMRK) == (INPCK|PARMRK)) {
    433   1.94    kleink 		/* "Escape" a valid character of '\377'. */
    434   1.94    kleink 		(void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
    435   1.94    kleink 		(void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
    436   1.94    kleink 		goto endcase;
    437   1.94    kleink 	}
    438   1.94    kleink 
    439   1.49       cgd 	/*
    440   1.49       cgd 	 * In tandem mode, check high water mark.
    441   1.49       cgd 	 */
    442   1.49       cgd 	if (ISSET(iflag, IXOFF) || ISSET(tp->t_cflag, CHWFLOW))
    443   1.49       cgd 		ttyblock(tp);
    444   1.49       cgd 	if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
    445   1.49       cgd 		CLR(c, 0x80);
    446   1.49       cgd 	if (!ISSET(lflag, EXTPROC)) {
    447   1.49       cgd 		/*
    448   1.49       cgd 		 * Check for literal nexting very first
    449   1.49       cgd 		 */
    450   1.49       cgd 		if (ISSET(tp->t_state, TS_LNCH)) {
    451   1.49       cgd 			SET(c, TTY_QUOTE);
    452   1.49       cgd 			CLR(tp->t_state, TS_LNCH);
    453   1.49       cgd 		}
    454   1.49       cgd 		/*
    455   1.49       cgd 		 * Scan for special characters.  This code
    456   1.49       cgd 		 * is really just a big case statement with
    457   1.49       cgd 		 * non-constant cases.  The bottom of the
    458   1.49       cgd 		 * case statement is labeled ``endcase'', so goto
    459   1.49       cgd 		 * it after a case match, or similar.
    460   1.49       cgd 		 */
    461   1.49       cgd 
    462   1.49       cgd 		/*
    463   1.49       cgd 		 * Control chars which aren't controlled
    464   1.49       cgd 		 * by ICANON, ISIG, or IXON.
    465   1.49       cgd 		 */
    466   1.49       cgd 		if (ISSET(lflag, IEXTEN)) {
    467   1.49       cgd 			if (CCEQ(cc[VLNEXT], c)) {
    468   1.49       cgd 				if (ISSET(lflag, ECHO)) {
    469   1.49       cgd 					if (ISSET(lflag, ECHOE)) {
    470   1.49       cgd 						(void)ttyoutput('^', tp);
    471   1.49       cgd 						(void)ttyoutput('\b', tp);
    472   1.49       cgd 					} else
    473   1.49       cgd 						ttyecho(c, tp);
    474   1.49       cgd 				}
    475   1.49       cgd 				SET(tp->t_state, TS_LNCH);
    476   1.49       cgd 				goto endcase;
    477   1.49       cgd 			}
    478   1.49       cgd 			if (CCEQ(cc[VDISCARD], c)) {
    479   1.49       cgd 				if (ISSET(lflag, FLUSHO))
    480   1.49       cgd 					CLR(tp->t_lflag, FLUSHO);
    481   1.49       cgd 				else {
    482   1.49       cgd 					ttyflush(tp, FWRITE);
    483   1.49       cgd 					ttyecho(c, tp);
    484   1.49       cgd 					if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
    485   1.49       cgd 						ttyretype(tp);
    486   1.49       cgd 					SET(tp->t_lflag, FLUSHO);
    487   1.49       cgd 				}
    488   1.49       cgd 				goto startoutput;
    489   1.49       cgd 			}
    490   1.49       cgd 		}
    491   1.49       cgd 		/*
    492   1.49       cgd 		 * Signals.
    493   1.49       cgd 		 */
    494   1.49       cgd 		if (ISSET(lflag, ISIG)) {
    495   1.49       cgd 			if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
    496   1.49       cgd 				if (!ISSET(lflag, NOFLSH))
    497   1.49       cgd 					ttyflush(tp, FREAD | FWRITE);
    498   1.49       cgd 				ttyecho(c, tp);
    499  1.201        ad 				ttysig(tp, TTYSIG_PG1, CCEQ(cc[VINTR], c) ?
    500  1.201        ad 				    SIGINT : SIGQUIT);
    501   1.49       cgd 				goto endcase;
    502   1.49       cgd 			}
    503   1.49       cgd 			if (CCEQ(cc[VSUSP], c)) {
    504   1.49       cgd 				if (!ISSET(lflag, NOFLSH))
    505   1.49       cgd 					ttyflush(tp, FREAD);
    506   1.49       cgd 				ttyecho(c, tp);
    507  1.201        ad 				ttysig(tp, TTYSIG_PG1, SIGTSTP);
    508   1.49       cgd 				goto endcase;
    509   1.49       cgd 			}
    510   1.49       cgd 		}
    511   1.49       cgd 		/*
    512   1.49       cgd 		 * Handle start/stop characters.
    513   1.49       cgd 		 */
    514   1.49       cgd 		if (ISSET(iflag, IXON)) {
    515   1.49       cgd 			if (CCEQ(cc[VSTOP], c)) {
    516   1.49       cgd 				if (!ISSET(tp->t_state, TS_TTSTOP)) {
    517   1.49       cgd 					SET(tp->t_state, TS_TTSTOP);
    518  1.201        ad 					cdev_stop(tp, 0);
    519   1.49       cgd 					return (0);
    520   1.49       cgd 				}
    521   1.49       cgd 				if (!CCEQ(cc[VSTART], c))
    522   1.49       cgd 					return (0);
    523   1.49       cgd 				/*
    524   1.49       cgd 				 * if VSTART == VSTOP then toggle
    525   1.49       cgd 				 */
    526   1.49       cgd 				goto endcase;
    527   1.49       cgd 			}
    528   1.49       cgd 			if (CCEQ(cc[VSTART], c))
    529   1.49       cgd 				goto restartoutput;
    530   1.49       cgd 		}
    531   1.49       cgd 		/*
    532   1.49       cgd 		 * IGNCR, ICRNL, & INLCR
    533   1.49       cgd 		 */
    534   1.49       cgd 		if (c == '\r') {
    535   1.49       cgd 			if (ISSET(iflag, IGNCR))
    536   1.49       cgd 				goto endcase;
    537   1.49       cgd 			else if (ISSET(iflag, ICRNL))
    538   1.49       cgd 				c = '\n';
    539   1.49       cgd 		} else if (c == '\n' && ISSET(iflag, INLCR))
    540   1.49       cgd 			c = '\r';
    541   1.49       cgd 	}
    542  1.147        pk 	if (!ISSET(lflag, EXTPROC) && ISSET(lflag, ICANON)) {
    543   1.49       cgd 		/*
    544   1.49       cgd 		 * From here on down canonical mode character
    545   1.49       cgd 		 * processing takes place.
    546   1.49       cgd 		 */
    547   1.49       cgd 		/*
    548   1.49       cgd 		 * erase (^H / ^?)
    549   1.49       cgd 		 */
    550   1.49       cgd 		if (CCEQ(cc[VERASE], c)) {
    551   1.49       cgd 			if (tp->t_rawq.c_cc)
    552   1.49       cgd 				ttyrub(unputc(&tp->t_rawq), tp);
    553   1.49       cgd 			goto endcase;
    554   1.49       cgd 		}
    555   1.49       cgd 		/*
    556   1.49       cgd 		 * kill (^U)
    557   1.49       cgd 		 */
    558   1.49       cgd 		if (CCEQ(cc[VKILL], c)) {
    559   1.49       cgd 			if (ISSET(lflag, ECHOKE) &&
    560   1.49       cgd 			    tp->t_rawq.c_cc == tp->t_rocount &&
    561   1.49       cgd 			    !ISSET(lflag, ECHOPRT))
    562   1.49       cgd 				while (tp->t_rawq.c_cc)
    563   1.49       cgd 					ttyrub(unputc(&tp->t_rawq), tp);
    564   1.49       cgd 			else {
    565   1.49       cgd 				ttyecho(c, tp);
    566   1.49       cgd 				if (ISSET(lflag, ECHOK) ||
    567   1.49       cgd 				    ISSET(lflag, ECHOKE))
    568   1.49       cgd 					ttyecho('\n', tp);
    569   1.49       cgd 				FLUSHQ(&tp->t_rawq);
    570   1.49       cgd 				tp->t_rocount = 0;
    571   1.49       cgd 			}
    572   1.49       cgd 			CLR(tp->t_state, TS_LOCAL);
    573   1.49       cgd 			goto endcase;
    574   1.49       cgd 		}
    575   1.49       cgd 		/*
    576   1.81    kleink 		 * Extensions to the POSIX.1 GTI set of functions.
    577   1.49       cgd 		 */
    578   1.81    kleink 		if (ISSET(lflag, IEXTEN)) {
    579   1.49       cgd 			/*
    580   1.81    kleink 			 * word erase (^W)
    581   1.49       cgd 			 */
    582   1.81    kleink 			if (CCEQ(cc[VWERASE], c)) {
    583   1.81    kleink 				int alt = ISSET(lflag, ALTWERASE);
    584   1.81    kleink 				int ctype;
    585   1.81    kleink 
    586   1.81    kleink 				/*
    587   1.81    kleink 				 * erase whitespace
    588   1.81    kleink 				 */
    589   1.81    kleink 				while ((c = unputc(&tp->t_rawq)) == ' ' ||
    590  1.138     enami 				    c == '\t')
    591   1.81    kleink 					ttyrub(c, tp);
    592   1.81    kleink 				if (c == -1)
    593   1.81    kleink 					goto endcase;
    594   1.81    kleink 				/*
    595   1.81    kleink 				 * erase last char of word and remember the
    596   1.81    kleink 				 * next chars type (for ALTWERASE)
    597   1.81    kleink 				 */
    598   1.49       cgd 				ttyrub(c, tp);
    599   1.81    kleink 				c = unputc(&tp->t_rawq);
    600   1.81    kleink 				if (c == -1)
    601   1.81    kleink 					goto endcase;
    602   1.81    kleink 				if (c == ' ' || c == '\t') {
    603   1.81    kleink 					(void)putc(c, &tp->t_rawq);
    604   1.81    kleink 					goto endcase;
    605   1.81    kleink 				}
    606   1.81    kleink 				ctype = ISALPHA(c);
    607   1.81    kleink 				/*
    608   1.81    kleink 				 * erase rest of word
    609   1.81    kleink 				 */
    610   1.81    kleink 				do {
    611   1.81    kleink 					ttyrub(c, tp);
    612   1.81    kleink 					c = unputc(&tp->t_rawq);
    613   1.81    kleink 					if (c == -1)
    614   1.81    kleink 						goto endcase;
    615   1.81    kleink 				} while (c != ' ' && c != '\t' &&
    616  1.138     enami 				    (alt == 0 || ISALPHA(c) == ctype));
    617   1.81    kleink 				(void)putc(c, &tp->t_rawq);
    618   1.49       cgd 				goto endcase;
    619   1.81    kleink 			}
    620   1.49       cgd 			/*
    621   1.81    kleink 			 * reprint line (^R)
    622   1.49       cgd 			 */
    623   1.81    kleink 			if (CCEQ(cc[VREPRINT], c)) {
    624   1.81    kleink 				ttyretype(tp);
    625   1.49       cgd 				goto endcase;
    626   1.49       cgd 			}
    627   1.49       cgd 			/*
    628   1.81    kleink 			 * ^T - kernel info and generate SIGINFO
    629   1.49       cgd 			 */
    630   1.81    kleink 			if (CCEQ(cc[VSTATUS], c)) {
    631  1.215        ad 				ttysig(tp, TTYSIG_PG1, SIGINFO);
    632   1.81    kleink 				goto endcase;
    633   1.81    kleink 			}
    634   1.49       cgd 		}
    635   1.49       cgd 	}
    636   1.49       cgd 	/*
    637   1.49       cgd 	 * Check for input buffer overflow
    638   1.49       cgd 	 */
    639   1.49       cgd 	if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
    640   1.49       cgd 		if (ISSET(iflag, IMAXBEL)) {
    641   1.49       cgd 			if (tp->t_outq.c_cc < tp->t_hiwat)
    642   1.49       cgd 				(void)ttyoutput(CTRL('g'), tp);
    643   1.49       cgd 		} else
    644   1.49       cgd 			ttyflush(tp, FREAD | FWRITE);
    645   1.49       cgd 		goto endcase;
    646   1.49       cgd 	}
    647   1.49       cgd 	/*
    648   1.49       cgd 	 * Put data char in q for user and
    649   1.49       cgd 	 * wakeup on seeing a line delimiter.
    650   1.49       cgd 	 */
    651   1.49       cgd 	if (putc(c, &tp->t_rawq) >= 0) {
    652   1.49       cgd 		if (!ISSET(lflag, ICANON)) {
    653   1.49       cgd 			ttwakeup(tp);
    654   1.49       cgd 			ttyecho(c, tp);
    655   1.49       cgd 			goto endcase;
    656   1.49       cgd 		}
    657   1.85    kleink 		if (TTBREAKC(c, lflag)) {
    658   1.49       cgd 			tp->t_rocount = 0;
    659   1.49       cgd 			catq(&tp->t_rawq, &tp->t_canq);
    660   1.49       cgd 			ttwakeup(tp);
    661   1.49       cgd 		} else if (tp->t_rocount++ == 0)
    662   1.49       cgd 			tp->t_rocol = tp->t_column;
    663   1.49       cgd 		if (ISSET(tp->t_state, TS_ERASE)) {
    664   1.49       cgd 			/*
    665   1.49       cgd 			 * end of prterase \.../
    666   1.49       cgd 			 */
    667   1.49       cgd 			CLR(tp->t_state, TS_ERASE);
    668   1.49       cgd 			(void)ttyoutput('/', tp);
    669   1.49       cgd 		}
    670   1.49       cgd 		i = tp->t_column;
    671   1.49       cgd 		ttyecho(c, tp);
    672   1.49       cgd 		if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
    673   1.49       cgd 			/*
    674   1.49       cgd 			 * Place the cursor over the '^' of the ^D.
    675   1.49       cgd 			 */
    676   1.49       cgd 			i = min(2, tp->t_column - i);
    677   1.49       cgd 			while (i > 0) {
    678   1.49       cgd 				(void)ttyoutput('\b', tp);
    679   1.49       cgd 				i--;
    680   1.49       cgd 			}
    681   1.49       cgd 		}
    682   1.49       cgd 	}
    683  1.126     lukem  endcase:
    684   1.49       cgd 	/*
    685   1.49       cgd 	 * IXANY means allow any character to restart output.
    686   1.49       cgd 	 */
    687   1.49       cgd 	if (ISSET(tp->t_state, TS_TTSTOP) &&
    688  1.147        pk 	    !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) {
    689   1.49       cgd 		return (0);
    690  1.147        pk 	}
    691  1.126     lukem  restartoutput:
    692   1.49       cgd 	CLR(tp->t_lflag, FLUSHO);
    693   1.49       cgd 	CLR(tp->t_state, TS_TTSTOP);
    694  1.126     lukem  startoutput:
    695   1.49       cgd 	return (ttstart(tp));
    696   1.49       cgd }
    697   1.49       cgd 
    698   1.49       cgd /*
    699  1.147        pk  * Process input of a single character received on a tty.
    700  1.147        pk  *
    701  1.147        pk  * XXX - this is a hack, all drivers must changed to acquire the
    702  1.147        pk  *	 lock before calling linesw->l_rint()
    703  1.147        pk  */
    704  1.147        pk int
    705  1.147        pk ttyinput(int c, struct tty *tp)
    706  1.147        pk {
    707  1.147        pk 	int error;
    708  1.147        pk 
    709  1.147        pk 	/*
    710  1.147        pk 	 * Unless the receiver is enabled, drop incoming data.
    711  1.147        pk 	 */
    712  1.147        pk 	if (!ISSET(tp->t_cflag, CREAD))
    713  1.147        pk 		return (0);
    714  1.147        pk 
    715  1.201        ad 	mutex_spin_enter(&tty_lock);
    716  1.147        pk 	error = ttyinput_wlock(c, tp);
    717  1.201        ad 	mutex_spin_exit(&tty_lock);
    718  1.201        ad 
    719  1.147        pk 	return (error);
    720  1.147        pk }
    721  1.147        pk 
    722  1.147        pk /*
    723   1.49       cgd  * Output a single character on a tty, doing output processing
    724   1.49       cgd  * as needed (expanding tabs, newline processing, etc.).
    725   1.49       cgd  * Returns < 0 if succeeds, otherwise returns char to resend.
    726   1.49       cgd  * Must be recursive.
    727  1.201        ad  *
    728  1.201        ad  * Call with tty lock held.
    729   1.49       cgd  */
    730   1.49       cgd int
    731  1.126     lukem ttyoutput(int c, struct tty *tp)
    732   1.49       cgd {
    733  1.126     lukem 	long	oflag;
    734  1.201        ad 	int	col, notout;
    735  1.201        ad 
    736  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
    737   1.49       cgd 
    738   1.49       cgd 	oflag = tp->t_oflag;
    739   1.49       cgd 	if (!ISSET(oflag, OPOST)) {
    740   1.49       cgd 		tk_nout++;
    741   1.49       cgd 		tp->t_outcc++;
    742   1.99   mycroft 		if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
    743   1.99   mycroft 			return (c);
    744   1.49       cgd 		return (-1);
    745   1.49       cgd 	}
    746   1.49       cgd 	/*
    747  1.103    kleink 	 * Do tab expansion if OXTABS is set.  Special case if we do external
    748   1.49       cgd 	 * processing, we don't do the tab expansion because we'll probably
    749   1.49       cgd 	 * get it wrong.  If tab expansion needs to be done, let it happen
    750   1.49       cgd 	 * externally.
    751   1.49       cgd 	 */
    752   1.49       cgd 	CLR(c, ~TTY_CHARMASK);
    753   1.49       cgd 	if (c == '\t' &&
    754   1.49       cgd 	    ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
    755   1.49       cgd 		c = 8 - (tp->t_column & 7);
    756   1.49       cgd 		if (ISSET(tp->t_lflag, FLUSHO)) {
    757   1.49       cgd 			notout = 0;
    758   1.49       cgd 		} else {
    759   1.49       cgd 			notout = b_to_q("        ", c, &tp->t_outq);
    760   1.49       cgd 			c -= notout;
    761   1.49       cgd 			tk_nout += c;
    762   1.49       cgd 			tp->t_outcc += c;
    763   1.49       cgd 		}
    764   1.49       cgd 		tp->t_column += c;
    765   1.49       cgd 		return (notout ? '\t' : -1);
    766   1.49       cgd 	}
    767   1.49       cgd 	if (c == CEOT && ISSET(oflag, ONOEOT))
    768   1.49       cgd 		return (-1);
    769   1.49       cgd 
    770   1.49       cgd 	/*
    771   1.49       cgd 	 * Newline translation: if ONLCR is set,
    772   1.49       cgd 	 * translate newline into "\r\n".
    773   1.49       cgd 	 */
    774   1.49       cgd 	if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
    775   1.49       cgd 		tk_nout++;
    776   1.49       cgd 		tp->t_outcc++;
    777   1.99   mycroft 		if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
    778   1.49       cgd 			return (c);
    779   1.49       cgd 	}
    780  1.103    kleink 	/* If OCRNL is set, translate "\r" into "\n". */
    781   1.79    kleink 	else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
    782   1.79    kleink 		c = '\n';
    783  1.103    kleink 	/* If ONOCR is set, don't transmit CRs when on column 0. */
    784  1.103    kleink 	else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
    785  1.103    kleink 		return (-1);
    786   1.79    kleink 
    787   1.49       cgd 	tk_nout++;
    788   1.49       cgd 	tp->t_outcc++;
    789   1.49       cgd 	if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
    790   1.49       cgd 		return (c);
    791   1.49       cgd 
    792   1.49       cgd 	col = tp->t_column;
    793   1.49       cgd 	switch (CCLASS(c)) {
    794   1.49       cgd 	case BACKSPACE:
    795   1.49       cgd 		if (col > 0)
    796   1.49       cgd 			--col;
    797   1.49       cgd 		break;
    798   1.49       cgd 	case CONTROL:
    799   1.49       cgd 		break;
    800   1.49       cgd 	case NEWLINE:
    801  1.103    kleink 		if (ISSET(tp->t_oflag, ONLCR | ONLRET))
    802   1.79    kleink 			col = 0;
    803   1.79    kleink 		break;
    804   1.49       cgd 	case RETURN:
    805   1.49       cgd 		col = 0;
    806   1.49       cgd 		break;
    807   1.49       cgd 	case ORDINARY:
    808   1.49       cgd 		++col;
    809   1.49       cgd 		break;
    810   1.49       cgd 	case TAB:
    811   1.49       cgd 		col = (col + 8) & ~7;
    812   1.49       cgd 		break;
    813   1.49       cgd 	}
    814   1.49       cgd 	tp->t_column = col;
    815   1.49       cgd 	return (-1);
    816   1.49       cgd }
    817   1.49       cgd 
    818   1.49       cgd /*
    819   1.49       cgd  * Ioctls for all tty devices.  Called after line-discipline specific ioctl
    820   1.49       cgd  * has been called to do discipline-specific functions and/or reject any
    821   1.49       cgd  * of these ioctl commands.
    822   1.49       cgd  */
    823   1.49       cgd /* ARGSUSED */
    824   1.49       cgd int
    825  1.192  christos ttioctl(struct tty *tp, u_long cmd, void *data, int flag, struct lwp *l)
    826   1.49       cgd {
    827   1.49       cgd 	extern struct tty *constty;	/* Temporary virtual console. */
    828  1.188    martin 	struct proc *p = l ? l->l_proc : NULL;
    829  1.126     lukem 	struct linesw	*lp;
    830  1.126     lukem 	int		s, error;
    831  1.151  christos 	struct nameidata nd;
    832  1.215        ad 	char		infobuf[200];
    833   1.49       cgd 
    834   1.49       cgd 	/* If the ioctl involves modification, hang if in the background. */
    835   1.49       cgd 	switch (cmd) {
    836   1.49       cgd 	case  TIOCFLUSH:
    837   1.92    kleink 	case  TIOCDRAIN:
    838   1.95    kleink 	case  TIOCSBRK:
    839   1.95    kleink 	case  TIOCCBRK:
    840   1.95    kleink 	case  TIOCSTART:
    841   1.49       cgd 	case  TIOCSETA:
    842   1.49       cgd 	case  TIOCSETD:
    843  1.121       eeh 	case  TIOCSLINED:
    844   1.49       cgd 	case  TIOCSETAF:
    845   1.49       cgd 	case  TIOCSETAW:
    846   1.49       cgd #ifdef notdef
    847   1.49       cgd 	case  TIOCSPGRP:
    848  1.158  jdolecek 	case  FIOSETOWN:
    849   1.49       cgd #endif
    850   1.49       cgd 	case  TIOCSTAT:
    851   1.49       cgd 	case  TIOCSTI:
    852   1.49       cgd 	case  TIOCSWINSZ:
    853   1.66  christos #ifdef COMPAT_OLDTTY
    854   1.49       cgd 	case  TIOCLBIC:
    855   1.49       cgd 	case  TIOCLBIS:
    856   1.49       cgd 	case  TIOCLSET:
    857   1.49       cgd 	case  TIOCSETC:
    858   1.49       cgd 	case OTIOCSETD:
    859   1.49       cgd 	case  TIOCSETN:
    860   1.49       cgd 	case  TIOCSETP:
    861   1.49       cgd 	case  TIOCSLTC:
    862   1.49       cgd #endif
    863  1.209        ad 		mutex_spin_enter(&tty_lock);
    864   1.49       cgd 		while (isbackground(curproc, tp) &&
    865  1.225        ad 		    p->p_pgrp->pg_jobc && (p->p_lflag & PL_PPWAIT) == 0 &&
    866  1.190        ad 		    !sigismasked(l, SIGTTOU)) {
    867  1.210        ad 			mutex_spin_exit(&tty_lock);
    868  1.210        ad 
    869  1.220        ad 			mutex_enter(proc_lock);
    870  1.210        ad 			pgsignal(p->p_pgrp, SIGTTOU, 1);
    871  1.220        ad 			mutex_exit(proc_lock);
    872  1.210        ad 
    873  1.210        ad 			mutex_spin_enter(&tty_lock);
    874  1.201        ad 			error = ttysleep(tp, &lbolt, true, 0);
    875  1.147        pk 			if (error) {
    876  1.209        ad 				mutex_spin_exit(&tty_lock);
    877   1.49       cgd 				return (error);
    878  1.147        pk 			}
    879   1.49       cgd 		}
    880  1.209        ad 		mutex_spin_exit(&tty_lock);
    881   1.49       cgd 		break;
    882   1.49       cgd 	}
    883   1.49       cgd 
    884   1.49       cgd 	switch (cmd) {			/* Process the ioctl. */
    885   1.49       cgd 	case FIOASYNC:			/* set/clear async i/o */
    886  1.201        ad 		mutex_spin_enter(&tty_lock);
    887   1.49       cgd 		if (*(int *)data)
    888   1.49       cgd 			SET(tp->t_state, TS_ASYNC);
    889   1.49       cgd 		else
    890   1.49       cgd 			CLR(tp->t_state, TS_ASYNC);
    891  1.201        ad 		mutex_spin_exit(&tty_lock);
    892   1.49       cgd 		break;
    893   1.49       cgd 	case FIONBIO:			/* set/clear non-blocking i/o */
    894   1.49       cgd 		break;			/* XXX: delete. */
    895   1.49       cgd 	case FIONREAD:			/* get # bytes to read */
    896  1.201        ad 		mutex_spin_enter(&tty_lock);
    897   1.49       cgd 		*(int *)data = ttnread(tp);
    898  1.201        ad 		mutex_spin_exit(&tty_lock);
    899   1.49       cgd 		break;
    900  1.170  wrstuden 	case FIONWRITE:			/* get # bytes to written & unsent */
    901  1.201        ad 		mutex_spin_enter(&tty_lock);
    902  1.170  wrstuden 		*(int *)data = tp->t_outq.c_cc;
    903  1.201        ad 		mutex_spin_exit(&tty_lock);
    904  1.170  wrstuden 		break;
    905  1.170  wrstuden 	case FIONSPACE:			/* get # bytes to written & unsent */
    906  1.201        ad 		mutex_spin_enter(&tty_lock);
    907  1.170  wrstuden 		*(int *)data = tp->t_outq.c_cn - tp->t_outq.c_cc;
    908  1.201        ad 		mutex_spin_exit(&tty_lock);
    909  1.170  wrstuden 		break;
    910   1.49       cgd 	case TIOCEXCL:			/* set exclusive use of tty */
    911  1.201        ad 		mutex_spin_enter(&tty_lock);
    912   1.49       cgd 		SET(tp->t_state, TS_XCLUDE);
    913  1.201        ad 		mutex_spin_exit(&tty_lock);
    914   1.49       cgd 		break;
    915   1.49       cgd 	case TIOCFLUSH: {		/* flush buffers */
    916  1.118  augustss 		int flags = *(int *)data;
    917   1.49       cgd 
    918   1.49       cgd 		if (flags == 0)
    919   1.49       cgd 			flags = FREAD | FWRITE;
    920   1.49       cgd 		else
    921   1.49       cgd 			flags &= FREAD | FWRITE;
    922  1.201        ad 		mutex_spin_enter(&tty_lock);
    923   1.49       cgd 		ttyflush(tp, flags);
    924  1.201        ad 		mutex_spin_exit(&tty_lock);
    925   1.49       cgd 		break;
    926   1.49       cgd 	}
    927   1.49       cgd 	case TIOCCONS:			/* become virtual console */
    928   1.49       cgd 		if (*(int *)data) {
    929   1.49       cgd 			if (constty && constty != tp &&
    930   1.49       cgd 			    ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) ==
    931   1.49       cgd 			    (TS_CARR_ON | TS_ISOPEN))
    932  1.151  christos 				return EBUSY;
    933  1.151  christos 
    934  1.151  christos 			NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
    935  1.208     pooka 			    "/dev/console");
    936  1.151  christos 			if ((error = namei(&nd)) != 0)
    937  1.151  christos 				return error;
    938  1.206     pooka 			error = VOP_ACCESS(nd.ni_vp, VREAD, l->l_cred);
    939  1.151  christos 			vput(nd.ni_vp);
    940  1.151  christos 			if (error)
    941  1.151  christos 				return error;
    942  1.151  christos 
    943   1.49       cgd 			constty = tp;
    944   1.49       cgd 		} else if (tp == constty)
    945   1.49       cgd 			constty = NULL;
    946   1.49       cgd 		break;
    947   1.49       cgd 	case TIOCDRAIN:			/* wait till output drained */
    948   1.65  christos 		if ((error = ttywait(tp)) != 0)
    949   1.49       cgd 			return (error);
    950   1.49       cgd 		break;
    951   1.49       cgd 	case TIOCGETA: {		/* get termios struct */
    952   1.49       cgd 		struct termios *t = (struct termios *)data;
    953   1.49       cgd 
    954  1.109     perry 		memcpy(t, &tp->t_termios, sizeof(struct termios));
    955   1.49       cgd 		break;
    956   1.49       cgd 	}
    957  1.177   thorpej 	case TIOCGETD:			/* get line discipline (old) */
    958  1.127     enami 		*(int *)data = tp->t_linesw->l_no;
    959  1.121       eeh 		break;
    960  1.177   thorpej 	case TIOCGLINED:		/* get line discipline (new) */
    961  1.138     enami 		(void)strncpy((char *)data, tp->t_linesw->l_name,
    962  1.131  christos 		    TTLINEDNAMELEN - 1);
    963   1.49       cgd 		break;
    964   1.49       cgd 	case TIOCGWINSZ:		/* get window size */
    965   1.49       cgd 		*(struct winsize *)data = tp->t_winsize;
    966   1.49       cgd 		break;
    967  1.158  jdolecek 	case FIOGETOWN:
    968  1.220        ad 		mutex_enter(proc_lock);
    969  1.220        ad 		if (tp->t_session != NULL && !isctty(p, tp)) {
    970  1.220        ad 			mutex_exit(proc_lock);
    971  1.158  jdolecek 			return (ENOTTY);
    972  1.220        ad 		}
    973  1.158  jdolecek 		*(int *)data = tp->t_pgrp ? -tp->t_pgrp->pg_id : 0;
    974  1.220        ad 		mutex_exit(proc_lock);
    975  1.158  jdolecek 		break;
    976   1.49       cgd 	case TIOCGPGRP:			/* get pgrp of tty */
    977  1.220        ad 		mutex_enter(proc_lock);
    978  1.220        ad 		if (!isctty(p, tp)) {
    979  1.220        ad 			mutex_exit(proc_lock);
    980   1.49       cgd 			return (ENOTTY);
    981  1.220        ad 		}
    982  1.150       dsl 		*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    983  1.220        ad 		mutex_exit(proc_lock);
    984  1.104   thorpej 		break;
    985  1.104   thorpej 	case TIOCGSID:			/* get sid of tty */
    986  1.220        ad 		mutex_enter(proc_lock);
    987  1.220        ad 		if (!isctty(p, tp)) {
    988  1.220        ad 			mutex_exit(proc_lock);
    989  1.104   thorpej 			return (ENOTTY);
    990  1.220        ad 		}
    991  1.104   thorpej 		*(int *)data = tp->t_session->s_sid;
    992  1.220        ad 		mutex_exit(proc_lock);
    993   1.49       cgd 		break;
    994   1.49       cgd #ifdef TIOCHPCL
    995   1.49       cgd 	case TIOCHPCL:			/* hang up on last close */
    996  1.201        ad 		mutex_spin_enter(&tty_lock);
    997   1.49       cgd 		SET(tp->t_cflag, HUPCL);
    998  1.201        ad 		mutex_spin_exit(&tty_lock);
    999   1.49       cgd 		break;
   1000   1.49       cgd #endif
   1001   1.49       cgd 	case TIOCNXCL:			/* reset exclusive use of tty */
   1002  1.201        ad 		mutex_spin_enter(&tty_lock);
   1003   1.49       cgd 		CLR(tp->t_state, TS_XCLUDE);
   1004  1.201        ad 		mutex_spin_exit(&tty_lock);
   1005   1.49       cgd 		break;
   1006   1.49       cgd 	case TIOCOUTQ:			/* output queue size */
   1007   1.49       cgd 		*(int *)data = tp->t_outq.c_cc;
   1008   1.49       cgd 		break;
   1009   1.49       cgd 	case TIOCSETA:			/* set termios struct */
   1010   1.49       cgd 	case TIOCSETAW:			/* drain output, set */
   1011   1.49       cgd 	case TIOCSETAF: {		/* drn out, fls in, set */
   1012  1.118  augustss 		struct termios *t = (struct termios *)data;
   1013   1.49       cgd 
   1014   1.49       cgd 		if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
   1015  1.147        pk 			if ((error = ttywait(tp)) != 0)
   1016  1.147        pk 				return (error);
   1017  1.147        pk 
   1018  1.147        pk 			if (cmd == TIOCSETAF) {
   1019  1.201        ad 				mutex_spin_enter(&tty_lock);
   1020  1.147        pk 				ttyflush(tp, FREAD);
   1021  1.201        ad 				mutex_spin_exit(&tty_lock);
   1022   1.49       cgd 			}
   1023   1.49       cgd 		}
   1024  1.147        pk 
   1025  1.147        pk 		s = spltty();
   1026  1.148        pk 		/*
   1027  1.148        pk 		 * XXXSMP - some drivers call back on us from t_param(), so
   1028  1.148        pk 		 *	    don't take the tty spin lock here.
   1029  1.148        pk 		 *	    require t_param() to unlock upon callback?
   1030  1.148        pk 		 */
   1031  1.201        ad 		/* wanted here: mutex_spin_enter(&tty_lock); */
   1032   1.49       cgd 		if (!ISSET(t->c_cflag, CIGNORE)) {
   1033   1.49       cgd 			/*
   1034   1.49       cgd 			 * Set device hardware.
   1035   1.49       cgd 			 */
   1036   1.49       cgd 			if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
   1037  1.201        ad 				/* wanted here: mutex_spin_exit(&tty_lock); */
   1038   1.49       cgd 				splx(s);
   1039   1.49       cgd 				return (error);
   1040   1.49       cgd 			} else {
   1041   1.49       cgd 				tp->t_cflag = t->c_cflag;
   1042   1.49       cgd 				tp->t_ispeed = t->c_ispeed;
   1043   1.49       cgd 				tp->t_ospeed = t->c_ospeed;
   1044  1.201        ad 				if (t->c_ospeed == 0)
   1045  1.201        ad 					ttysig(tp, TTYSIG_LEADER, SIGHUP);
   1046   1.49       cgd 			}
   1047   1.49       cgd 			ttsetwater(tp);
   1048   1.49       cgd 		}
   1049  1.148        pk 
   1050  1.201        ad 		/* delayed lock acquiring */
   1051  1.201        ad 		mutex_spin_enter(&tty_lock);
   1052   1.49       cgd 		if (cmd != TIOCSETAF) {
   1053   1.49       cgd 			if (ISSET(t->c_lflag, ICANON) !=
   1054  1.110   thorpej 			    ISSET(tp->t_lflag, ICANON)) {
   1055   1.49       cgd 				if (ISSET(t->c_lflag, ICANON)) {
   1056   1.49       cgd 					SET(tp->t_lflag, PENDIN);
   1057   1.49       cgd 					ttwakeup(tp);
   1058   1.49       cgd 				} else {
   1059   1.49       cgd 					struct clist tq;
   1060   1.49       cgd 
   1061   1.49       cgd 					catq(&tp->t_rawq, &tp->t_canq);
   1062   1.49       cgd 					tq = tp->t_rawq;
   1063   1.49       cgd 					tp->t_rawq = tp->t_canq;
   1064   1.49       cgd 					tp->t_canq = tq;
   1065   1.49       cgd 					CLR(tp->t_lflag, PENDIN);
   1066   1.49       cgd 				}
   1067  1.110   thorpej 			}
   1068   1.49       cgd 		}
   1069   1.49       cgd 		tp->t_iflag = t->c_iflag;
   1070   1.49       cgd 		tp->t_oflag = t->c_oflag;
   1071   1.49       cgd 		/*
   1072   1.49       cgd 		 * Make the EXTPROC bit read only.
   1073   1.49       cgd 		 */
   1074   1.49       cgd 		if (ISSET(tp->t_lflag, EXTPROC))
   1075   1.49       cgd 			SET(t->c_lflag, EXTPROC);
   1076   1.49       cgd 		else
   1077   1.49       cgd 			CLR(t->c_lflag, EXTPROC);
   1078   1.49       cgd 		tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
   1079  1.109     perry 		memcpy(tp->t_cc, t->c_cc, sizeof(t->c_cc));
   1080  1.201        ad 		mutex_spin_exit(&tty_lock);
   1081   1.49       cgd 		splx(s);
   1082   1.49       cgd 		break;
   1083   1.49       cgd 	}
   1084  1.177   thorpej 	case TIOCSETD:			/* set line discipline (old) */
   1085  1.177   thorpej 		lp = ttyldisc_lookup_bynum(*(int *)data);
   1086  1.177   thorpej 		goto setldisc;
   1087   1.49       cgd 
   1088  1.177   thorpej 	case TIOCSLINED: {		/* set line discipline (new) */
   1089  1.121       eeh 		char *name = (char *)data;
   1090  1.124     enami 		dev_t device;
   1091  1.121       eeh 
   1092  1.121       eeh 		/* Null terminate to prevent buffer overflow */
   1093  1.138     enami 		name[TTLINEDNAMELEN - 1] = '\0';
   1094  1.121       eeh 		lp = ttyldisc_lookup(name);
   1095  1.126     lukem  setldisc:
   1096  1.124     enami 		if (lp == NULL)
   1097  1.121       eeh 			return (ENXIO);
   1098  1.121       eeh 
   1099  1.121       eeh 		if (lp != tp->t_linesw) {
   1100  1.124     enami 			device = tp->t_dev;
   1101   1.49       cgd 			s = spltty();
   1102  1.124     enami 			(*tp->t_linesw->l_close)(tp, flag);
   1103  1.121       eeh 			error = (*lp->l_open)(device, tp);
   1104   1.49       cgd 			if (error) {
   1105  1.121       eeh 				(void)(*tp->t_linesw->l_open)(device, tp);
   1106   1.49       cgd 				splx(s);
   1107  1.177   thorpej 				ttyldisc_release(lp);
   1108   1.49       cgd 				return (error);
   1109   1.49       cgd 			}
   1110  1.177   thorpej 			ttyldisc_release(tp->t_linesw);
   1111  1.121       eeh 			tp->t_linesw = lp;
   1112   1.49       cgd 			splx(s);
   1113  1.177   thorpej 		} else {
   1114  1.177   thorpej 			/* Drop extra reference. */
   1115  1.177   thorpej 			ttyldisc_release(lp);
   1116   1.49       cgd 		}
   1117   1.49       cgd 		break;
   1118   1.49       cgd 	}
   1119   1.49       cgd 	case TIOCSTART:			/* start output, like ^Q */
   1120  1.201        ad 		mutex_spin_enter(&tty_lock);
   1121   1.49       cgd 		if (ISSET(tp->t_state, TS_TTSTOP) ||
   1122   1.49       cgd 		    ISSET(tp->t_lflag, FLUSHO)) {
   1123   1.49       cgd 			CLR(tp->t_lflag, FLUSHO);
   1124   1.49       cgd 			CLR(tp->t_state, TS_TTSTOP);
   1125   1.49       cgd 			ttstart(tp);
   1126   1.49       cgd 		}
   1127  1.201        ad 		mutex_spin_exit(&tty_lock);
   1128   1.49       cgd 		break;
   1129   1.49       cgd 	case TIOCSTI:			/* simulate terminal input */
   1130  1.212      elad 		if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_STI,
   1131  1.212      elad 		    tp) != 0) {
   1132  1.189      elad 			if (!ISSET(flag, FREAD))
   1133  1.189      elad 				return (EPERM);
   1134  1.189      elad 			if (!isctty(p, tp))
   1135  1.189      elad 				return (EACCES);
   1136  1.189      elad 		}
   1137  1.121       eeh 		(*tp->t_linesw->l_rint)(*(u_char *)data, tp);
   1138   1.49       cgd 		break;
   1139   1.49       cgd 	case TIOCSTOP:			/* stop output, like ^S */
   1140  1.142   gehenna 	{
   1141  1.201        ad 		mutex_spin_enter(&tty_lock);
   1142   1.49       cgd 		if (!ISSET(tp->t_state, TS_TTSTOP)) {
   1143   1.49       cgd 			SET(tp->t_state, TS_TTSTOP);
   1144  1.201        ad 			cdev_stop(tp, 0);
   1145   1.49       cgd 		}
   1146  1.201        ad 		mutex_spin_exit(&tty_lock);
   1147   1.49       cgd 		break;
   1148  1.142   gehenna 	}
   1149   1.49       cgd 	case TIOCSCTTY:			/* become controlling tty */
   1150  1.220        ad 		mutex_enter(proc_lock);
   1151  1.209        ad 		mutex_spin_enter(&tty_lock);
   1152  1.190        ad 
   1153   1.49       cgd 		/* Session ctty vnode pointer set in vnode layer. */
   1154   1.49       cgd 		if (!SESS_LEADER(p) ||
   1155   1.65  christos 		    ((p->p_session->s_ttyvp || tp->t_session) &&
   1156  1.190        ad 		    (tp->t_session != p->p_session))) {
   1157  1.209        ad 			mutex_spin_exit(&tty_lock);
   1158  1.220        ad 			mutex_exit(proc_lock);
   1159   1.49       cgd 			return (EPERM);
   1160  1.190        ad 		}
   1161  1.139  jdolecek 
   1162  1.160        pk 		/*
   1163  1.160        pk 		 * `p_session' acquires a reference.
   1164  1.160        pk 		 * But note that if `t_session' is set at this point,
   1165  1.160        pk 		 * it must equal `p_session', in which case the session
   1166  1.160        pk 		 * already has the correct reference count.
   1167  1.160        pk 		 */
   1168  1.160        pk 		if (tp->t_session == NULL)
   1169  1.160        pk 			SESSHOLD(p->p_session);
   1170  1.139  jdolecek 
   1171   1.49       cgd 		tp->t_session = p->p_session;
   1172   1.49       cgd 		tp->t_pgrp = p->p_pgrp;
   1173   1.49       cgd 		p->p_session->s_ttyp = tp;
   1174  1.190        ad 		p->p_lflag |= PL_CONTROLT;
   1175  1.209        ad 		mutex_spin_exit(&tty_lock);
   1176  1.220        ad 		mutex_exit(proc_lock);
   1177   1.49       cgd 		break;
   1178  1.158  jdolecek 	case FIOSETOWN: {		/* set pgrp of tty */
   1179  1.158  jdolecek 		pid_t pgid = *(int *)data;
   1180  1.158  jdolecek 		struct pgrp *pgrp;
   1181  1.158  jdolecek 
   1182  1.220        ad 		mutex_enter(proc_lock);
   1183  1.209        ad 		if (tp->t_session != NULL && !isctty(p, tp)) {
   1184  1.220        ad 			mutex_exit(proc_lock);
   1185  1.158  jdolecek 			return (ENOTTY);
   1186  1.209        ad 		}
   1187  1.191       dsl 
   1188  1.191       dsl 		if (pgid < 0) {
   1189  1.191       dsl 			pgrp = pg_find(-pgid, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
   1190  1.191       dsl 			if (pgrp == NULL)
   1191  1.191       dsl 				return (EINVAL);
   1192  1.191       dsl 		} else {
   1193  1.191       dsl 			struct proc *p1;
   1194  1.191       dsl 			p1 = p_find(pgid, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
   1195  1.158  jdolecek 			if (!p1)
   1196  1.158  jdolecek 				return (ESRCH);
   1197  1.158  jdolecek 			pgrp = p1->p_pgrp;
   1198  1.158  jdolecek 		}
   1199  1.158  jdolecek 
   1200  1.191       dsl 		if (pgrp->pg_session != p->p_session) {
   1201  1.220        ad 			mutex_exit(proc_lock);
   1202  1.158  jdolecek 			return (EPERM);
   1203  1.191       dsl 		}
   1204  1.209        ad 		mutex_spin_enter(&tty_lock);
   1205  1.158  jdolecek 		tp->t_pgrp = pgrp;
   1206  1.209        ad 		mutex_spin_exit(&tty_lock);
   1207  1.220        ad 		mutex_exit(proc_lock);
   1208  1.158  jdolecek 		break;
   1209  1.158  jdolecek 	}
   1210   1.49       cgd 	case TIOCSPGRP: {		/* set pgrp of tty */
   1211  1.191       dsl 		struct pgrp *pgrp;
   1212   1.49       cgd 
   1213  1.220        ad 		mutex_enter(proc_lock);
   1214  1.209        ad 		if (!isctty(p, tp)) {
   1215  1.220        ad 			mutex_exit(proc_lock);
   1216   1.49       cgd 			return (ENOTTY);
   1217  1.209        ad 		}
   1218  1.191       dsl 		pgrp = pg_find(*(int *)data, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
   1219  1.191       dsl 		if (pgrp == NULL)
   1220   1.93    kleink 			return (EINVAL);
   1221  1.191       dsl 		if (pgrp->pg_session != p->p_session) {
   1222  1.220        ad 			mutex_exit(proc_lock);
   1223   1.49       cgd 			return (EPERM);
   1224  1.191       dsl 		}
   1225  1.209        ad 		mutex_spin_enter(&tty_lock);
   1226   1.49       cgd 		tp->t_pgrp = pgrp;
   1227  1.209        ad 		mutex_spin_exit(&tty_lock);
   1228  1.220        ad 		mutex_exit(proc_lock);
   1229   1.49       cgd 		break;
   1230   1.49       cgd 	}
   1231   1.49       cgd 	case TIOCSTAT:			/* get load avg stats */
   1232  1.220        ad 		mutex_enter(proc_lock);
   1233  1.215        ad 		ttygetinfo(tp, 0, infobuf, sizeof(infobuf));
   1234  1.220        ad 		mutex_exit(proc_lock);
   1235  1.215        ad 
   1236  1.201        ad 		mutex_spin_enter(&tty_lock);
   1237  1.215        ad 		ttyputinfo(tp, infobuf);
   1238  1.201        ad 		mutex_spin_exit(&tty_lock);
   1239   1.49       cgd 		break;
   1240   1.49       cgd 	case TIOCSWINSZ:		/* set window size */
   1241  1.201        ad 		mutex_spin_enter(&tty_lock);
   1242  1.192  christos 		if (memcmp((void *)&tp->t_winsize, data,
   1243  1.108     perry 		    sizeof(struct winsize))) {
   1244   1.49       cgd 			tp->t_winsize = *(struct winsize *)data;
   1245  1.201        ad 			ttysig(tp, TTYSIG_PG1, SIGWINCH);
   1246   1.49       cgd 		}
   1247  1.201        ad 		mutex_spin_exit(&tty_lock);
   1248   1.49       cgd 		break;
   1249   1.49       cgd 	default:
   1250   1.66  christos #ifdef COMPAT_OLDTTY
   1251  1.178  christos 		return (ttcompat(tp, cmd, data, flag, l));
   1252   1.49       cgd #else
   1253  1.134    atatat 		return (EPASSTHROUGH);
   1254   1.49       cgd #endif
   1255   1.49       cgd 	}
   1256   1.49       cgd 	return (0);
   1257   1.49       cgd }
   1258   1.49       cgd 
   1259   1.49       cgd int
   1260  1.178  christos ttpoll(struct tty *tp, int events, struct lwp *l)
   1261  1.126     lukem {
   1262  1.201        ad 	int	revents;
   1263   1.74   mycroft 
   1264  1.126     lukem 	revents = 0;
   1265  1.201        ad 	mutex_spin_enter(&tty_lock);
   1266   1.74   mycroft 	if (events & (POLLIN | POLLRDNORM))
   1267   1.74   mycroft 		if (ttnread(tp) > 0)
   1268   1.74   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
   1269   1.74   mycroft 
   1270   1.74   mycroft 	if (events & (POLLOUT | POLLWRNORM))
   1271   1.74   mycroft 		if (tp->t_outq.c_cc <= tp->t_lowat)
   1272   1.74   mycroft 			revents |= events & (POLLOUT | POLLWRNORM);
   1273   1.74   mycroft 
   1274   1.74   mycroft 	if (events & POLLHUP)
   1275   1.83   mycroft 		if (!CONNECTED(tp))
   1276   1.74   mycroft 			revents |= POLLHUP;
   1277   1.74   mycroft 
   1278   1.74   mycroft 	if (revents == 0) {
   1279   1.74   mycroft 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
   1280  1.178  christos 			selrecord(l, &tp->t_rsel);
   1281   1.49       cgd 
   1282   1.74   mycroft 		if (events & (POLLOUT | POLLWRNORM))
   1283  1.178  christos 			selrecord(l, &tp->t_wsel);
   1284   1.74   mycroft 	}
   1285   1.49       cgd 
   1286  1.201        ad 	mutex_spin_exit(&tty_lock);
   1287  1.201        ad 
   1288   1.74   mycroft 	return (revents);
   1289   1.49       cgd }
   1290   1.49       cgd 
   1291  1.143  jdolecek static void
   1292  1.143  jdolecek filt_ttyrdetach(struct knote *kn)
   1293  1.143  jdolecek {
   1294  1.143  jdolecek 	struct tty	*tp;
   1295  1.143  jdolecek 
   1296  1.143  jdolecek 	tp = kn->kn_hook;
   1297  1.201        ad 	mutex_spin_enter(&tty_lock);
   1298  1.144  christos 	SLIST_REMOVE(&tp->t_rsel.sel_klist, kn, knote, kn_selnext);
   1299  1.201        ad 	mutex_spin_exit(&tty_lock);
   1300  1.143  jdolecek }
   1301  1.143  jdolecek 
   1302  1.143  jdolecek static int
   1303  1.143  jdolecek filt_ttyread(struct knote *kn, long hint)
   1304  1.143  jdolecek {
   1305  1.143  jdolecek 	struct tty	*tp;
   1306  1.143  jdolecek 
   1307  1.143  jdolecek 	tp = kn->kn_hook;
   1308  1.162  jdolecek 	if ((hint & NOTE_SUBMIT) == 0)
   1309  1.201        ad 		mutex_spin_enter(&tty_lock);
   1310  1.143  jdolecek 	kn->kn_data = ttnread(tp);
   1311  1.162  jdolecek 	if ((hint & NOTE_SUBMIT) == 0)
   1312  1.201        ad 		mutex_spin_exit(&tty_lock);
   1313  1.143  jdolecek 	return (kn->kn_data > 0);
   1314  1.143  jdolecek }
   1315  1.143  jdolecek 
   1316  1.143  jdolecek static void
   1317  1.143  jdolecek filt_ttywdetach(struct knote *kn)
   1318  1.143  jdolecek {
   1319  1.143  jdolecek 	struct tty	*tp;
   1320  1.143  jdolecek 
   1321  1.143  jdolecek 	tp = kn->kn_hook;
   1322  1.201        ad 	mutex_spin_enter(&tty_lock);
   1323  1.144  christos 	SLIST_REMOVE(&tp->t_wsel.sel_klist, kn, knote, kn_selnext);
   1324  1.201        ad 	mutex_spin_exit(&tty_lock);
   1325  1.143  jdolecek }
   1326  1.143  jdolecek 
   1327  1.143  jdolecek static int
   1328  1.143  jdolecek filt_ttywrite(struct knote *kn, long hint)
   1329  1.143  jdolecek {
   1330  1.143  jdolecek 	struct tty	*tp;
   1331  1.201        ad 	int		canwrite;
   1332  1.143  jdolecek 
   1333  1.143  jdolecek 	tp = kn->kn_hook;
   1334  1.162  jdolecek 	if ((hint & NOTE_SUBMIT) == 0)
   1335  1.201        ad 		mutex_spin_enter(&tty_lock);
   1336  1.143  jdolecek 	kn->kn_data = tp->t_outq.c_cn - tp->t_outq.c_cc;
   1337  1.147        pk 	canwrite = (tp->t_outq.c_cc <= tp->t_lowat) && CONNECTED(tp);
   1338  1.162  jdolecek 	if ((hint & NOTE_SUBMIT) == 0)
   1339  1.201        ad 		mutex_spin_exit(&tty_lock);
   1340  1.147        pk 	return (canwrite);
   1341  1.143  jdolecek }
   1342  1.143  jdolecek 
   1343  1.143  jdolecek static const struct filterops ttyread_filtops =
   1344  1.143  jdolecek 	{ 1, NULL, filt_ttyrdetach, filt_ttyread };
   1345  1.143  jdolecek static const struct filterops ttywrite_filtops =
   1346  1.143  jdolecek 	{ 1, NULL, filt_ttywdetach, filt_ttywrite };
   1347  1.143  jdolecek 
   1348  1.143  jdolecek int
   1349  1.143  jdolecek ttykqfilter(dev_t dev, struct knote *kn)
   1350  1.143  jdolecek {
   1351  1.143  jdolecek 	struct tty	*tp;
   1352  1.143  jdolecek 	struct klist	*klist;
   1353  1.143  jdolecek 
   1354  1.201        ad 	if ((tp = cdev_tty(dev)) == NULL)
   1355  1.143  jdolecek 		return (ENXIO);
   1356  1.157      manu 
   1357  1.143  jdolecek 	switch (kn->kn_filter) {
   1358  1.143  jdolecek 	case EVFILT_READ:
   1359  1.144  christos 		klist = &tp->t_rsel.sel_klist;
   1360  1.143  jdolecek 		kn->kn_fop = &ttyread_filtops;
   1361  1.143  jdolecek 		break;
   1362  1.143  jdolecek 	case EVFILT_WRITE:
   1363  1.144  christos 		klist = &tp->t_wsel.sel_klist;
   1364  1.143  jdolecek 		kn->kn_fop = &ttywrite_filtops;
   1365  1.143  jdolecek 		break;
   1366  1.143  jdolecek 	default:
   1367  1.176  christos 		return EINVAL;
   1368  1.143  jdolecek 	}
   1369  1.143  jdolecek 
   1370  1.143  jdolecek 	kn->kn_hook = tp;
   1371  1.143  jdolecek 
   1372  1.201        ad 	mutex_spin_enter(&tty_lock);
   1373  1.143  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1374  1.201        ad 	mutex_spin_exit(&tty_lock);
   1375  1.143  jdolecek 
   1376  1.143  jdolecek 	return (0);
   1377  1.143  jdolecek }
   1378  1.143  jdolecek 
   1379  1.147        pk /*
   1380  1.147        pk  * Find the number of chars ready to be read from this tty.
   1381  1.201        ad  * Call with the tty lock held.
   1382  1.147        pk  */
   1383   1.49       cgd static int
   1384  1.126     lukem ttnread(struct tty *tp)
   1385   1.49       cgd {
   1386  1.126     lukem 	int	nread;
   1387   1.49       cgd 
   1388  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   1389  1.201        ad 
   1390   1.49       cgd 	if (ISSET(tp->t_lflag, PENDIN))
   1391   1.49       cgd 		ttypend(tp);
   1392   1.49       cgd 	nread = tp->t_canq.c_cc;
   1393   1.49       cgd 	if (!ISSET(tp->t_lflag, ICANON)) {
   1394   1.49       cgd 		nread += tp->t_rawq.c_cc;
   1395   1.49       cgd 		if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
   1396   1.49       cgd 			nread = 0;
   1397   1.49       cgd 	}
   1398   1.49       cgd 	return (nread);
   1399   1.49       cgd }
   1400   1.49       cgd 
   1401   1.49       cgd /*
   1402   1.49       cgd  * Wait for output to drain.
   1403   1.49       cgd  */
   1404   1.49       cgd int
   1405  1.138     enami ttywait(struct tty *tp)
   1406   1.49       cgd {
   1407  1.201        ad 	int	error;
   1408   1.49       cgd 
   1409   1.49       cgd 	error = 0;
   1410  1.201        ad 
   1411  1.201        ad 	mutex_spin_enter(&tty_lock);
   1412   1.49       cgd 	while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
   1413   1.83   mycroft 	    CONNECTED(tp) && tp->t_oproc) {
   1414   1.49       cgd 		(*tp->t_oproc)(tp);
   1415  1.224        ad 		error = ttysleep(tp, &tp->t_outcv, true, 0);
   1416   1.65  christos 		if (error)
   1417   1.49       cgd 			break;
   1418   1.49       cgd 	}
   1419  1.201        ad 	mutex_spin_exit(&tty_lock);
   1420  1.201        ad 
   1421   1.49       cgd 	return (error);
   1422   1.49       cgd }
   1423   1.49       cgd 
   1424   1.49       cgd /*
   1425   1.49       cgd  * Flush if successfully wait.
   1426   1.49       cgd  */
   1427   1.49       cgd int
   1428  1.126     lukem ttywflush(struct tty *tp)
   1429   1.49       cgd {
   1430  1.126     lukem 	int	error;
   1431   1.49       cgd 
   1432  1.147        pk 	if ((error = ttywait(tp)) == 0) {
   1433  1.201        ad 		mutex_spin_enter(&tty_lock);
   1434   1.49       cgd 		ttyflush(tp, FREAD);
   1435  1.201        ad 		mutex_spin_exit(&tty_lock);
   1436  1.147        pk 	}
   1437   1.49       cgd 	return (error);
   1438   1.49       cgd }
   1439   1.49       cgd 
   1440   1.49       cgd /*
   1441   1.49       cgd  * Flush tty read and/or write queues, notifying anyone waiting.
   1442  1.201        ad  * Call with the tty lock held.
   1443   1.49       cgd  */
   1444   1.49       cgd void
   1445  1.126     lukem ttyflush(struct tty *tp, int rw)
   1446   1.49       cgd {
   1447  1.201        ad 
   1448  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   1449   1.49       cgd 
   1450   1.49       cgd 	if (rw & FREAD) {
   1451   1.49       cgd 		FLUSHQ(&tp->t_canq);
   1452   1.49       cgd 		FLUSHQ(&tp->t_rawq);
   1453   1.49       cgd 		tp->t_rocount = 0;
   1454   1.49       cgd 		tp->t_rocol = 0;
   1455   1.49       cgd 		CLR(tp->t_state, TS_LOCAL);
   1456   1.49       cgd 		ttwakeup(tp);
   1457   1.49       cgd 	}
   1458   1.49       cgd 	if (rw & FWRITE) {
   1459   1.49       cgd 		CLR(tp->t_state, TS_TTSTOP);
   1460  1.201        ad 		cdev_stop(tp, rw);
   1461   1.49       cgd 		FLUSHQ(&tp->t_outq);
   1462  1.224        ad 		cv_broadcast(&tp->t_outcv);
   1463  1.213     rmind 		selnotify(&tp->t_wsel, 0, NOTE_SUBMIT);
   1464   1.49       cgd 	}
   1465   1.49       cgd }
   1466   1.49       cgd 
   1467   1.49       cgd /*
   1468   1.49       cgd  * Copy in the default termios characters.
   1469   1.49       cgd  */
   1470   1.49       cgd void
   1471  1.126     lukem ttychars(struct tty *tp)
   1472   1.49       cgd {
   1473   1.49       cgd 
   1474  1.109     perry 	memcpy(tp->t_cc, ttydefchars, sizeof(ttydefchars));
   1475   1.49       cgd }
   1476   1.49       cgd 
   1477   1.49       cgd /*
   1478   1.49       cgd  * Send stop character on input overflow.
   1479  1.203        ad  * Call with the tty lock held.
   1480   1.49       cgd  */
   1481   1.49       cgd static void
   1482  1.126     lukem ttyblock(struct tty *tp)
   1483   1.49       cgd {
   1484  1.126     lukem 	int	total;
   1485   1.49       cgd 
   1486  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   1487  1.201        ad 
   1488   1.49       cgd 	total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
   1489   1.49       cgd 	if (tp->t_rawq.c_cc > TTYHOG) {
   1490   1.49       cgd 		ttyflush(tp, FREAD | FWRITE);
   1491   1.49       cgd 		CLR(tp->t_state, TS_TBLOCK);
   1492   1.49       cgd 	}
   1493   1.49       cgd 	/*
   1494   1.49       cgd 	 * Block further input iff: current input > threshold
   1495   1.49       cgd 	 * AND input is available to user program.
   1496   1.49       cgd 	 */
   1497  1.101  drochner 	if (total >= TTYHOG / 2 &&
   1498  1.101  drochner 	    !ISSET(tp->t_state, TS_TBLOCK) &&
   1499  1.101  drochner 	    (!ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0)) {
   1500   1.60   mycroft 		if (ISSET(tp->t_iflag, IXOFF) &&
   1501   1.60   mycroft 		    tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
   1502   1.60   mycroft 		    putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
   1503   1.49       cgd 			SET(tp->t_state, TS_TBLOCK);
   1504   1.49       cgd 			ttstart(tp);
   1505   1.49       cgd 		}
   1506   1.59   mycroft 		/* Try to block remote output via hardware flow control. */
   1507   1.49       cgd 		if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
   1508   1.49       cgd 		    (*tp->t_hwiflow)(tp, 1) != 0)
   1509   1.49       cgd 			SET(tp->t_state, TS_TBLOCK);
   1510   1.49       cgd 	}
   1511   1.49       cgd }
   1512   1.49       cgd 
   1513  1.147        pk /*
   1514  1.147        pk  * Delayed line discipline output
   1515  1.147        pk  */
   1516   1.49       cgd void
   1517  1.126     lukem ttrstrt(void *tp_arg)
   1518   1.49       cgd {
   1519  1.126     lukem 	struct tty	*tp;
   1520   1.49       cgd 
   1521   1.49       cgd #ifdef DIAGNOSTIC
   1522   1.49       cgd 	if (tp_arg == NULL)
   1523   1.49       cgd 		panic("ttrstrt");
   1524   1.49       cgd #endif
   1525   1.49       cgd 	tp = tp_arg;
   1526  1.201        ad 	mutex_spin_enter(&tty_lock);
   1527   1.49       cgd 
   1528   1.49       cgd 	CLR(tp->t_state, TS_TIMEOUT);
   1529  1.147        pk 	ttstart(tp); /* XXX - Shouldn't this be tp->l_start(tp)? */
   1530   1.49       cgd 
   1531  1.201        ad 	mutex_spin_exit(&tty_lock);
   1532   1.49       cgd }
   1533   1.49       cgd 
   1534  1.147        pk /*
   1535  1.147        pk  * start a line discipline
   1536  1.201        ad  * Always call with tty lock held?
   1537  1.147        pk  */
   1538   1.49       cgd int
   1539  1.126     lukem ttstart(struct tty *tp)
   1540   1.49       cgd {
   1541   1.49       cgd 
   1542   1.49       cgd 	if (tp->t_oproc != NULL)	/* XXX: Kludge for pty. */
   1543   1.49       cgd 		(*tp->t_oproc)(tp);
   1544   1.49       cgd 	return (0);
   1545   1.49       cgd }
   1546   1.49       cgd 
   1547   1.49       cgd /*
   1548   1.49       cgd  * "close" a line discipline
   1549   1.49       cgd  */
   1550   1.49       cgd int
   1551  1.126     lukem ttylclose(struct tty *tp, int flag)
   1552   1.49       cgd {
   1553   1.49       cgd 
   1554  1.147        pk 	if (flag & FNONBLOCK) {
   1555  1.201        ad 		mutex_spin_enter(&tty_lock);
   1556   1.49       cgd 		ttyflush(tp, FREAD | FWRITE);
   1557  1.201        ad 		mutex_spin_exit(&tty_lock);
   1558  1.147        pk 	} else
   1559   1.49       cgd 		ttywflush(tp);
   1560   1.49       cgd 	return (0);
   1561   1.49       cgd }
   1562   1.49       cgd 
   1563   1.49       cgd /*
   1564   1.49       cgd  * Handle modem control transition on a tty.
   1565   1.49       cgd  * Flag indicates new state of carrier.
   1566   1.49       cgd  * Returns 0 if the line should be turned off, otherwise 1.
   1567   1.49       cgd  */
   1568   1.49       cgd int
   1569  1.126     lukem ttymodem(struct tty *tp, int flag)
   1570   1.49       cgd {
   1571   1.49       cgd 
   1572  1.201        ad 	mutex_spin_enter(&tty_lock);
   1573   1.83   mycroft 	if (flag == 0) {
   1574   1.96    kleink 		if (ISSET(tp->t_state, TS_CARR_ON)) {
   1575   1.96    kleink 			/*
   1576   1.96    kleink 			 * Lost carrier.
   1577   1.96    kleink 			 */
   1578   1.96    kleink 			CLR(tp->t_state, TS_CARR_ON);
   1579   1.96    kleink 			if (ISSET(tp->t_state, TS_ISOPEN) && !CONNECTED(tp)) {
   1580  1.201        ad 				ttysig(tp, TTYSIG_LEADER, SIGHUP);
   1581   1.96    kleink 				ttyflush(tp, FREAD | FWRITE);
   1582  1.201        ad 				mutex_spin_exit(&tty_lock);
   1583   1.96    kleink 				return (0);
   1584   1.96    kleink 			}
   1585   1.49       cgd 		}
   1586   1.49       cgd 	} else {
   1587   1.96    kleink 		if (!ISSET(tp->t_state, TS_CARR_ON)) {
   1588   1.96    kleink 			/*
   1589   1.96    kleink 			 * Carrier now on.
   1590   1.96    kleink 			 */
   1591   1.96    kleink 			SET(tp->t_state, TS_CARR_ON);
   1592   1.96    kleink 			ttwakeup(tp);
   1593   1.96    kleink 		}
   1594   1.49       cgd 	}
   1595  1.201        ad 	mutex_spin_exit(&tty_lock);
   1596  1.201        ad 
   1597   1.49       cgd 	return (1);
   1598   1.49       cgd }
   1599   1.49       cgd 
   1600   1.49       cgd /*
   1601   1.49       cgd  * Default modem control routine (for other line disciplines).
   1602   1.49       cgd  * Return argument flag, to turn off device on carrier drop.
   1603   1.49       cgd  */
   1604   1.49       cgd int
   1605  1.126     lukem nullmodem(struct tty *tp, int flag)
   1606   1.49       cgd {
   1607   1.49       cgd 
   1608  1.201        ad 	mutex_spin_enter(&tty_lock);
   1609   1.49       cgd 	if (flag)
   1610   1.49       cgd 		SET(tp->t_state, TS_CARR_ON);
   1611   1.49       cgd 	else {
   1612   1.49       cgd 		CLR(tp->t_state, TS_CARR_ON);
   1613   1.83   mycroft 		if (!CONNECTED(tp)) {
   1614  1.201        ad 			ttysig(tp, TTYSIG_LEADER, SIGHUP);
   1615  1.201        ad 			mutex_spin_exit(&tty_lock);
   1616   1.49       cgd 			return (0);
   1617   1.49       cgd 		}
   1618   1.49       cgd 	}
   1619  1.201        ad 	mutex_spin_exit(&tty_lock);
   1620  1.201        ad 
   1621   1.49       cgd 	return (1);
   1622   1.49       cgd }
   1623   1.49       cgd 
   1624   1.49       cgd /*
   1625  1.147        pk  * Reinput pending characters after state switch.
   1626   1.49       cgd  */
   1627   1.49       cgd void
   1628  1.126     lukem ttypend(struct tty *tp)
   1629   1.49       cgd {
   1630  1.126     lukem 	struct clist	tq;
   1631  1.126     lukem 	int		c;
   1632   1.49       cgd 
   1633  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   1634  1.201        ad 
   1635   1.49       cgd 	CLR(tp->t_lflag, PENDIN);
   1636   1.49       cgd 	SET(tp->t_state, TS_TYPEN);
   1637   1.49       cgd 	tq = tp->t_rawq;
   1638   1.49       cgd 	tp->t_rawq.c_cc = 0;
   1639   1.49       cgd 	tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
   1640   1.49       cgd 	while ((c = getc(&tq)) >= 0)
   1641  1.147        pk 		ttyinput_wlock(c, tp);
   1642   1.49       cgd 	CLR(tp->t_state, TS_TYPEN);
   1643   1.49       cgd }
   1644   1.49       cgd 
   1645   1.49       cgd /*
   1646   1.49       cgd  * Process a read call on a tty device.
   1647   1.49       cgd  */
   1648   1.49       cgd int
   1649  1.126     lukem ttread(struct tty *tp, struct uio *uio, int flag)
   1650   1.49       cgd {
   1651  1.126     lukem 	struct clist	*qp;
   1652  1.126     lukem 	u_char		*cc;
   1653  1.126     lukem 	struct proc	*p;
   1654  1.201        ad 	int		c, first, error, has_stime, last_cc;
   1655  1.126     lukem 	long		lflag, slp;
   1656  1.185    kardel 	struct timeval	now, stime;
   1657   1.49       cgd 
   1658  1.181       mrg 	stime.tv_usec = 0;	/* XXX gcc */
   1659  1.181       mrg 	stime.tv_sec = 0;	/* XXX gcc */
   1660  1.181       mrg 
   1661  1.126     lukem 	cc = tp->t_cc;
   1662  1.126     lukem 	p = curproc;
   1663  1.126     lukem 	error = 0;
   1664  1.126     lukem 	has_stime = 0;
   1665  1.126     lukem 	last_cc = 0;
   1666  1.126     lukem 	slp = 0;
   1667  1.126     lukem 
   1668  1.126     lukem  loop:
   1669  1.201        ad 	mutex_spin_enter(&tty_lock);
   1670  1.126     lukem 	lflag = tp->t_lflag;
   1671   1.49       cgd 	/*
   1672   1.49       cgd 	 * take pending input first
   1673   1.49       cgd 	 */
   1674   1.49       cgd 	if (ISSET(lflag, PENDIN))
   1675   1.49       cgd 		ttypend(tp);
   1676   1.49       cgd 
   1677   1.49       cgd 	/*
   1678  1.209        ad 	 * Hang process if it's in the background.
   1679   1.49       cgd 	 */
   1680   1.49       cgd 	if (isbackground(p, tp)) {
   1681  1.223      yamt 		if (sigismasked(curlwp, SIGTTIN) ||
   1682  1.225        ad 		    p->p_lflag & PL_PPWAIT || p->p_pgrp->pg_jobc == 0) {
   1683  1.201        ad 			mutex_spin_exit(&tty_lock);
   1684   1.49       cgd 			return (EIO);
   1685  1.147        pk 		}
   1686  1.210        ad 		mutex_spin_exit(&tty_lock);
   1687  1.210        ad 
   1688  1.220        ad 		mutex_enter(proc_lock);
   1689  1.210        ad 		pgsignal(p->p_pgrp, SIGTTIN, 1);
   1690  1.220        ad 		mutex_exit(proc_lock);
   1691  1.210        ad 
   1692  1.210        ad 		mutex_spin_enter(&tty_lock);
   1693  1.201        ad 		error = ttysleep(tp, &lbolt, true, 0);
   1694  1.201        ad 		mutex_spin_exit(&tty_lock);
   1695   1.65  christos 		if (error)
   1696   1.49       cgd 			return (error);
   1697   1.49       cgd 		goto loop;
   1698   1.49       cgd 	}
   1699   1.49       cgd 
   1700   1.49       cgd 	if (!ISSET(lflag, ICANON)) {
   1701   1.49       cgd 		int m = cc[VMIN];
   1702   1.49       cgd 		long t = cc[VTIME];
   1703   1.49       cgd 
   1704   1.49       cgd 		qp = &tp->t_rawq;
   1705   1.49       cgd 		/*
   1706   1.49       cgd 		 * Check each of the four combinations.
   1707   1.49       cgd 		 * (m > 0 && t == 0) is the normal read case.
   1708   1.49       cgd 		 * It should be fairly efficient, so we check that and its
   1709   1.49       cgd 		 * companion case (m == 0 && t == 0) first.
   1710   1.49       cgd 		 * For the other two cases, we compute the target sleep time
   1711   1.49       cgd 		 * into slp.
   1712   1.49       cgd 		 */
   1713   1.49       cgd 		if (t == 0) {
   1714   1.49       cgd 			if (qp->c_cc < m)
   1715   1.49       cgd 				goto sleep;
   1716   1.49       cgd 			goto read;
   1717   1.49       cgd 		}
   1718  1.156       dsl 		t *= hz;		/* time in deca-ticks */
   1719  1.156       dsl /*
   1720  1.156       dsl  * Time difference in deca-ticks, split division to avoid numeric overflow.
   1721  1.156       dsl  * Ok for hz < ~200kHz
   1722  1.156       dsl  */
   1723  1.156       dsl #define	diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 10 * hz + \
   1724  1.156       dsl 			 ((t1).tv_usec - (t2).tv_usec) / 100 * hz / 1000)
   1725   1.49       cgd 		if (m > 0) {
   1726   1.49       cgd 			if (qp->c_cc <= 0)
   1727   1.49       cgd 				goto sleep;
   1728   1.49       cgd 			if (qp->c_cc >= m)
   1729   1.49       cgd 				goto read;
   1730   1.49       cgd 			if (!has_stime) {
   1731   1.49       cgd 				/* first character, start timer */
   1732   1.49       cgd 				has_stime = 1;
   1733  1.185    kardel 				getmicrotime(&stime);
   1734   1.49       cgd 				slp = t;
   1735   1.49       cgd 			} else if (qp->c_cc > last_cc) {
   1736   1.49       cgd 				/* got a character, restart timer */
   1737  1.185    kardel 				getmicrotime(&stime);
   1738   1.49       cgd 				slp = t;
   1739   1.49       cgd 			} else {
   1740   1.49       cgd 				/* nothing, check expiration */
   1741  1.185    kardel 				getmicrotime(&now);
   1742  1.185    kardel 				slp = t - diff(now, stime);
   1743   1.49       cgd 			}
   1744   1.49       cgd 		} else {	/* m == 0 */
   1745   1.49       cgd 			if (qp->c_cc > 0)
   1746   1.49       cgd 				goto read;
   1747   1.49       cgd 			if (!has_stime) {
   1748   1.49       cgd 				has_stime = 1;
   1749  1.185    kardel 				getmicrotime(&stime);
   1750   1.49       cgd 				slp = t;
   1751  1.185    kardel 			} else {
   1752  1.185    kardel 				getmicrotime(&now);
   1753  1.185    kardel 				slp = t - diff(now, stime);
   1754  1.185    kardel 			}
   1755   1.49       cgd 		}
   1756   1.54   mycroft 		last_cc = qp->c_cc;
   1757   1.49       cgd #undef diff
   1758   1.49       cgd 		if (slp > 0) {
   1759   1.49       cgd 			/*
   1760  1.156       dsl 			 * Convert deca-ticks back to ticks.
   1761   1.49       cgd 			 * Rounding down may make us wake up just short
   1762   1.49       cgd 			 * of the target, so we round up.
   1763  1.156       dsl 			 * Maybe we should do 'slp/10 + 1' because the
   1764  1.156       dsl 			 * first tick maybe almost immediate.
   1765  1.156       dsl 			 * However it is more useful for a program that sets
   1766  1.156       dsl 			 * VTIME=10 to wakeup every second not every 1.01
   1767  1.156       dsl 			 * seconds (if hz=100).
   1768   1.49       cgd 			 */
   1769  1.156       dsl 			slp = (slp + 9)/ 10;
   1770   1.49       cgd 			goto sleep;
   1771   1.49       cgd 		}
   1772   1.49       cgd 	} else if ((qp = &tp->t_canq)->c_cc <= 0) {
   1773  1.126     lukem 		int	carrier;
   1774   1.49       cgd 
   1775  1.126     lukem  sleep:
   1776   1.49       cgd 		/*
   1777   1.49       cgd 		 * If there is no input, sleep on rawq
   1778   1.49       cgd 		 * awaiting hardware receipt and notification.
   1779   1.49       cgd 		 * If we have data, we don't need to check for carrier.
   1780   1.49       cgd 		 */
   1781   1.83   mycroft 		carrier = CONNECTED(tp);
   1782   1.49       cgd 		if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
   1783  1.201        ad 			mutex_spin_exit(&tty_lock);
   1784   1.49       cgd 			return (0);	/* EOF */
   1785   1.49       cgd 		}
   1786   1.49       cgd 		if (flag & IO_NDELAY) {
   1787  1.201        ad 			mutex_spin_exit(&tty_lock);
   1788   1.49       cgd 			return (EWOULDBLOCK);
   1789   1.49       cgd 		}
   1790  1.224        ad 		error = ttysleep(tp, &tp->t_rawcv, true, slp);
   1791  1.201        ad 		mutex_spin_exit(&tty_lock);
   1792   1.82    kleink 		/* VMIN == 0: any quantity read satisfies */
   1793   1.82    kleink 		if (cc[VMIN] == 0 && error == EWOULDBLOCK)
   1794   1.82    kleink 			return (0);
   1795   1.54   mycroft 		if (error && error != EWOULDBLOCK)
   1796   1.49       cgd 			return (error);
   1797   1.49       cgd 		goto loop;
   1798   1.49       cgd 	}
   1799  1.126     lukem  read:
   1800  1.201        ad 	mutex_spin_exit(&tty_lock);
   1801   1.49       cgd 
   1802   1.49       cgd 	/*
   1803   1.49       cgd 	 * Input present, check for input mapping and processing.
   1804   1.49       cgd 	 */
   1805   1.49       cgd 	first = 1;
   1806   1.49       cgd 	while ((c = getc(qp)) >= 0) {
   1807   1.49       cgd 		/*
   1808   1.49       cgd 		 * delayed suspend (^Y)
   1809   1.49       cgd 		 */
   1810   1.81    kleink 		if (CCEQ(cc[VDSUSP], c) &&
   1811   1.81    kleink 		    ISSET(lflag, IEXTEN|ISIG) == (IEXTEN|ISIG)) {
   1812  1.201        ad 			mutex_spin_enter(&tty_lock);
   1813  1.201        ad 			ttysig(tp, TTYSIG_PG1, SIGTSTP);
   1814   1.49       cgd 			if (first) {
   1815  1.201        ad 				error = ttysleep(tp, &lbolt, true, 0);
   1816  1.201        ad 				mutex_spin_exit(&tty_lock);
   1817   1.65  christos 				if (error)
   1818   1.49       cgd 					break;
   1819   1.49       cgd 				goto loop;
   1820  1.201        ad 			} else
   1821  1.201        ad 				mutex_spin_exit(&tty_lock);
   1822   1.49       cgd 			break;
   1823   1.49       cgd 		}
   1824   1.49       cgd 		/*
   1825   1.49       cgd 		 * Interpret EOF only in canonical mode.
   1826   1.49       cgd 		 */
   1827   1.49       cgd 		if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
   1828   1.49       cgd 			break;
   1829   1.49       cgd 		/*
   1830   1.49       cgd 		 * Give user character.
   1831   1.49       cgd 		 */
   1832   1.49       cgd  		error = ureadc(c, uio);
   1833   1.49       cgd 		if (error)
   1834   1.49       cgd 			break;
   1835   1.49       cgd  		if (uio->uio_resid == 0)
   1836   1.49       cgd 			break;
   1837   1.49       cgd 		/*
   1838   1.49       cgd 		 * In canonical mode check for a "break character"
   1839   1.49       cgd 		 * marking the end of a "line of input".
   1840   1.49       cgd 		 */
   1841   1.85    kleink 		if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
   1842   1.49       cgd 			break;
   1843   1.49       cgd 		first = 0;
   1844   1.49       cgd 	}
   1845   1.49       cgd 	/*
   1846   1.49       cgd 	 * Look to unblock output now that (presumably)
   1847   1.49       cgd 	 * the input queue has gone down.
   1848   1.49       cgd 	 */
   1849  1.201        ad 	mutex_spin_enter(&tty_lock);
   1850  1.138     enami 	if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG / 5) {
   1851   1.60   mycroft 		if (ISSET(tp->t_iflag, IXOFF) &&
   1852   1.60   mycroft 		    cc[VSTART] != _POSIX_VDISABLE &&
   1853   1.49       cgd 		    putc(cc[VSTART], &tp->t_outq) == 0) {
   1854   1.49       cgd 			CLR(tp->t_state, TS_TBLOCK);
   1855   1.49       cgd 			ttstart(tp);
   1856   1.49       cgd 		}
   1857   1.59   mycroft 		/* Try to unblock remote output via hardware flow control. */
   1858   1.59   mycroft 		if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
   1859   1.49       cgd 		    (*tp->t_hwiflow)(tp, 0) != 0)
   1860   1.59   mycroft 			CLR(tp->t_state, TS_TBLOCK);
   1861   1.49       cgd 	}
   1862  1.201        ad 	mutex_spin_exit(&tty_lock);
   1863  1.201        ad 
   1864   1.49       cgd 	return (error);
   1865   1.49       cgd }
   1866   1.49       cgd 
   1867   1.49       cgd /*
   1868   1.49       cgd  * Check the output queue on tp for space for a kernel message (from uprintf
   1869   1.49       cgd  * or tprintf).  Allow some space over the normal hiwater mark so we don't
   1870   1.49       cgd  * lose messages due to normal flow control, but don't let the tty run amok.
   1871   1.49       cgd  * Sleeps here are not interruptible, but we return prematurely if new signals
   1872   1.49       cgd  * arrive.
   1873  1.201        ad  * Call with tty lock held.
   1874   1.49       cgd  */
   1875  1.147        pk static int
   1876  1.147        pk ttycheckoutq_wlock(struct tty *tp, int wait)
   1877   1.49       cgd {
   1878  1.201        ad 	int	hiwat, error;
   1879  1.201        ad 
   1880  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   1881   1.49       cgd 
   1882   1.49       cgd 	hiwat = tp->t_hiwat;
   1883   1.49       cgd 	if (tp->t_outq.c_cc > hiwat + 200)
   1884   1.49       cgd 		while (tp->t_outq.c_cc > hiwat) {
   1885   1.49       cgd 			ttstart(tp);
   1886  1.201        ad 			if (wait == 0)
   1887   1.49       cgd 				return (0);
   1888  1.224        ad 			error = ttysleep(tp, &tp->t_outcv, true, hz);
   1889  1.112   mycroft 			if (error == EINTR)
   1890  1.112   mycroft 				wait = 0;
   1891   1.49       cgd 		}
   1892  1.147        pk 
   1893   1.49       cgd 	return (1);
   1894   1.49       cgd }
   1895   1.49       cgd 
   1896  1.147        pk int
   1897  1.147        pk ttycheckoutq(struct tty *tp, int wait)
   1898  1.147        pk {
   1899  1.201        ad 	int	r;
   1900  1.147        pk 
   1901  1.201        ad 	mutex_spin_enter(&tty_lock);
   1902  1.147        pk 	r = ttycheckoutq_wlock(tp, wait);
   1903  1.201        ad 	mutex_spin_exit(&tty_lock);
   1904  1.201        ad 
   1905  1.147        pk 	return (r);
   1906  1.147        pk }
   1907  1.147        pk 
   1908   1.49       cgd /*
   1909   1.49       cgd  * Process a write call on a tty device.
   1910   1.49       cgd  */
   1911   1.49       cgd int
   1912  1.126     lukem ttwrite(struct tty *tp, struct uio *uio, int flag)
   1913   1.49       cgd {
   1914  1.126     lukem 	u_char		*cp;
   1915  1.126     lukem 	struct proc	*p;
   1916  1.201        ad 	int		cc, ce, i, hiwat, error;
   1917  1.140   thorpej 	size_t		cnt;
   1918  1.126     lukem 	u_char		obuf[OBUFSIZ];
   1919   1.49       cgd 
   1920  1.126     lukem 	cp = NULL;
   1921   1.49       cgd 	hiwat = tp->t_hiwat;
   1922   1.49       cgd 	cnt = uio->uio_resid;
   1923   1.49       cgd 	error = 0;
   1924   1.49       cgd 	cc = 0;
   1925  1.126     lukem  loop:
   1926  1.201        ad 	mutex_spin_enter(&tty_lock);
   1927   1.83   mycroft 	if (!CONNECTED(tp)) {
   1928   1.49       cgd 		if (ISSET(tp->t_state, TS_ISOPEN)) {
   1929  1.201        ad 			mutex_spin_exit(&tty_lock);
   1930   1.49       cgd 			return (EIO);
   1931   1.49       cgd 		} else if (flag & IO_NDELAY) {
   1932  1.201        ad 			mutex_spin_exit(&tty_lock);
   1933   1.49       cgd 			error = EWOULDBLOCK;
   1934   1.49       cgd 			goto out;
   1935   1.49       cgd 		} else {
   1936   1.49       cgd 			/* Sleep awaiting carrier. */
   1937  1.224        ad 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
   1938  1.201        ad 			mutex_spin_exit(&tty_lock);
   1939   1.49       cgd 			if (error)
   1940   1.49       cgd 				goto out;
   1941   1.49       cgd 			goto loop;
   1942   1.49       cgd 		}
   1943   1.49       cgd 	}
   1944  1.211        ad 
   1945   1.49       cgd 	/*
   1946  1.209        ad 	 * Hang the process if it's in the background.
   1947   1.49       cgd 	 */
   1948   1.49       cgd 	p = curproc;
   1949   1.49       cgd 	if (isbackground(p, tp) &&
   1950  1.225        ad 	    ISSET(tp->t_lflag, TOSTOP) && (p->p_lflag & PL_PPWAIT) == 0 &&
   1951  1.223      yamt 	    !sigismasked(curlwp, SIGTTOU)) {
   1952   1.86    kleink 		if (p->p_pgrp->pg_jobc == 0) {
   1953   1.86    kleink 			error = EIO;
   1954  1.209        ad 			mutex_spin_exit(&tty_lock);
   1955   1.86    kleink 			goto out;
   1956   1.86    kleink 		}
   1957  1.210        ad 		mutex_spin_exit(&tty_lock);
   1958  1.210        ad 
   1959  1.220        ad 		mutex_enter(proc_lock);
   1960  1.210        ad 		pgsignal(p->p_pgrp, SIGTTOU, 1);
   1961  1.220        ad 		mutex_exit(proc_lock);
   1962  1.210        ad 
   1963  1.210        ad 		mutex_spin_enter(&tty_lock);
   1964  1.201        ad 		error = ttysleep(tp, &lbolt, true, 0);
   1965  1.201        ad 		mutex_spin_exit(&tty_lock);
   1966   1.65  christos 		if (error)
   1967   1.49       cgd 			goto out;
   1968   1.49       cgd 		goto loop;
   1969   1.49       cgd 	}
   1970  1.209        ad 	mutex_spin_exit(&tty_lock);
   1971  1.209        ad 
   1972   1.49       cgd 	/*
   1973   1.49       cgd 	 * Process the user's data in at most OBUFSIZ chunks.  Perform any
   1974   1.49       cgd 	 * output translation.  Keep track of high water mark, sleep on
   1975   1.49       cgd 	 * overflow awaiting device aid in acquiring new space.
   1976   1.49       cgd 	 */
   1977   1.49       cgd 	while (uio->uio_resid > 0 || cc > 0) {
   1978   1.49       cgd 		if (ISSET(tp->t_lflag, FLUSHO)) {
   1979   1.49       cgd 			uio->uio_resid = 0;
   1980   1.49       cgd 			return (0);
   1981   1.49       cgd 		}
   1982   1.49       cgd 		if (tp->t_outq.c_cc > hiwat)
   1983   1.49       cgd 			goto ovhiwat;
   1984   1.49       cgd 		/*
   1985   1.49       cgd 		 * Grab a hunk of data from the user, unless we have some
   1986   1.49       cgd 		 * leftover from last time.
   1987   1.49       cgd 		 */
   1988   1.49       cgd 		if (cc == 0) {
   1989   1.49       cgd 			cc = min(uio->uio_resid, OBUFSIZ);
   1990   1.49       cgd 			cp = obuf;
   1991   1.49       cgd 			error = uiomove(cp, cc, uio);
   1992   1.49       cgd 			if (error) {
   1993   1.49       cgd 				cc = 0;
   1994  1.147        pk 				goto out;
   1995   1.49       cgd 			}
   1996   1.49       cgd 		}
   1997   1.49       cgd 		/*
   1998   1.49       cgd 		 * If nothing fancy need be done, grab those characters we
   1999   1.49       cgd 		 * can handle without any of ttyoutput's processing and
   2000   1.49       cgd 		 * just transfer them to the output q.  For those chars
   2001   1.49       cgd 		 * which require special processing (as indicated by the
   2002   1.49       cgd 		 * bits in char_type), call ttyoutput.  After processing
   2003   1.49       cgd 		 * a hunk of data, look for FLUSHO so ^O's will take effect
   2004   1.49       cgd 		 * immediately.
   2005   1.49       cgd 		 */
   2006  1.201        ad 		mutex_spin_enter(&tty_lock);
   2007   1.49       cgd 		while (cc > 0) {
   2008   1.49       cgd 			if (!ISSET(tp->t_oflag, OPOST))
   2009   1.49       cgd 				ce = cc;
   2010   1.49       cgd 			else {
   2011   1.77       cgd 				ce = cc - scanc((u_int)cc, cp, char_type,
   2012   1.77       cgd 				    CCLASSMASK);
   2013   1.49       cgd 				/*
   2014   1.49       cgd 				 * If ce is zero, then we're processing
   2015   1.49       cgd 				 * a special character through ttyoutput.
   2016   1.49       cgd 				 */
   2017   1.49       cgd 				if (ce == 0) {
   2018   1.49       cgd 					tp->t_rocount = 0;
   2019   1.49       cgd 					if (ttyoutput(*cp, tp) >= 0) {
   2020   1.49       cgd 						/* out of space */
   2021  1.201        ad 						mutex_spin_exit(&tty_lock);
   2022   1.49       cgd 						goto overfull;
   2023   1.49       cgd 					}
   2024   1.49       cgd 					cp++;
   2025   1.49       cgd 					cc--;
   2026   1.49       cgd 					if (ISSET(tp->t_lflag, FLUSHO) ||
   2027  1.147        pk 					    tp->t_outq.c_cc > hiwat) {
   2028  1.201        ad 						mutex_spin_exit(&tty_lock);
   2029   1.49       cgd 						goto ovhiwat;
   2030  1.147        pk 					}
   2031   1.49       cgd 					continue;
   2032   1.49       cgd 				}
   2033   1.49       cgd 			}
   2034   1.49       cgd 			/*
   2035   1.49       cgd 			 * A bunch of normal characters have been found.
   2036   1.49       cgd 			 * Transfer them en masse to the output queue and
   2037   1.49       cgd 			 * continue processing at the top of the loop.
   2038   1.49       cgd 			 * If there are any further characters in this
   2039   1.49       cgd 			 * <= OBUFSIZ chunk, the first should be a character
   2040   1.49       cgd 			 * requiring special handling by ttyoutput.
   2041   1.49       cgd 			 */
   2042   1.49       cgd 			tp->t_rocount = 0;
   2043   1.49       cgd 			i = b_to_q(cp, ce, &tp->t_outq);
   2044   1.49       cgd 			ce -= i;
   2045   1.49       cgd 			tp->t_column += ce;
   2046   1.49       cgd 			cp += ce, cc -= ce, tk_nout += ce;
   2047   1.49       cgd 			tp->t_outcc += ce;
   2048   1.49       cgd 			if (i > 0) {
   2049   1.49       cgd 				/* out of space */
   2050  1.201        ad 				mutex_spin_exit(&tty_lock);
   2051   1.49       cgd 				goto overfull;
   2052   1.49       cgd 			}
   2053   1.49       cgd 			if (ISSET(tp->t_lflag, FLUSHO) ||
   2054   1.49       cgd 			    tp->t_outq.c_cc > hiwat)
   2055   1.49       cgd 				break;
   2056   1.49       cgd 		}
   2057  1.205        ad 		ttstart(tp);
   2058  1.201        ad 		mutex_spin_exit(&tty_lock);
   2059   1.49       cgd 	}
   2060  1.147        pk 
   2061  1.126     lukem  out:
   2062   1.49       cgd 	/*
   2063   1.49       cgd 	 * If cc is nonzero, we leave the uio structure inconsistent, as the
   2064   1.49       cgd 	 * offset and iov pointers have moved forward, but it doesn't matter
   2065   1.49       cgd 	 * (the call will either return short or restart with a new uio).
   2066   1.49       cgd 	 */
   2067   1.49       cgd 	uio->uio_resid += cc;
   2068   1.49       cgd 	return (error);
   2069   1.49       cgd 
   2070  1.126     lukem  overfull:
   2071   1.49       cgd 	/*
   2072   1.49       cgd 	 * Since we are using ring buffers, if we can't insert any more into
   2073   1.49       cgd 	 * the output queue, we can assume the ring is full and that someone
   2074   1.49       cgd 	 * forgot to set the high water mark correctly.  We set it and then
   2075   1.49       cgd 	 * proceed as normal.
   2076   1.49       cgd 	 */
   2077   1.49       cgd 	hiwat = tp->t_outq.c_cc - 1;
   2078   1.49       cgd 
   2079  1.126     lukem  ovhiwat:
   2080  1.205        ad 	mutex_spin_enter(&tty_lock);
   2081   1.49       cgd 	ttstart(tp);
   2082   1.49       cgd 	/*
   2083   1.49       cgd 	 * This can only occur if FLUSHO is set in t_lflag,
   2084   1.49       cgd 	 * or if ttstart/oproc is synchronous (or very fast).
   2085   1.49       cgd 	 */
   2086   1.49       cgd 	if (tp->t_outq.c_cc <= hiwat) {
   2087  1.201        ad 		mutex_spin_exit(&tty_lock);
   2088   1.49       cgd 		goto loop;
   2089   1.49       cgd 	}
   2090   1.49       cgd 	if (flag & IO_NDELAY) {
   2091  1.201        ad 		mutex_spin_exit(&tty_lock);
   2092  1.187  christos 		error = EWOULDBLOCK;
   2093  1.147        pk 		goto out;
   2094   1.49       cgd 	}
   2095  1.224        ad 	error = ttysleep(tp, &tp->t_outcv, true, 0);
   2096  1.201        ad 	mutex_spin_exit(&tty_lock);
   2097   1.49       cgd 	if (error)
   2098   1.49       cgd 		goto out;
   2099   1.49       cgd 	goto loop;
   2100   1.49       cgd }
   2101   1.49       cgd 
   2102   1.49       cgd /*
   2103  1.203        ad  * Try to pull more output from the producer.  Return non-zero if
   2104  1.203        ad  * there is output ready to be sent.
   2105  1.203        ad  */
   2106  1.203        ad bool
   2107  1.203        ad ttypull(struct tty *tp)
   2108  1.203        ad {
   2109  1.203        ad 
   2110  1.203        ad 	/* XXXSMP not yet KASSERT(mutex_owned(&tty_lock)); */
   2111  1.203        ad 
   2112  1.203        ad 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   2113  1.224        ad 		cv_broadcast(&tp->t_outcv);
   2114  1.213     rmind 		selnotify(&tp->t_wsel, 0, NOTE_SUBMIT);
   2115  1.203        ad 	}
   2116  1.203        ad 	return tp->t_outq.c_cc != 0;
   2117  1.203        ad }
   2118  1.203        ad 
   2119  1.203        ad /*
   2120   1.49       cgd  * Rubout one character from the rawq of tp
   2121   1.49       cgd  * as cleanly as possible.
   2122  1.201        ad  * Called with tty lock held.
   2123   1.49       cgd  */
   2124   1.49       cgd void
   2125  1.126     lukem ttyrub(int c, struct tty *tp)
   2126   1.49       cgd {
   2127  1.126     lukem 	u_char	*cp;
   2128  1.201        ad 	int	savecol, tabc;
   2129  1.201        ad 
   2130  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   2131   1.49       cgd 
   2132   1.49       cgd 	if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
   2133   1.49       cgd 		return;
   2134   1.49       cgd 	CLR(tp->t_lflag, FLUSHO);
   2135   1.49       cgd 	if (ISSET(tp->t_lflag, ECHOE)) {
   2136   1.49       cgd 		if (tp->t_rocount == 0) {
   2137   1.49       cgd 			/*
   2138   1.49       cgd 			 * Screwed by ttwrite; retype
   2139   1.49       cgd 			 */
   2140   1.49       cgd 			ttyretype(tp);
   2141   1.49       cgd 			return;
   2142   1.49       cgd 		}
   2143   1.49       cgd 		if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
   2144   1.49       cgd 			ttyrubo(tp, 2);
   2145   1.49       cgd 		else {
   2146   1.49       cgd 			CLR(c, ~TTY_CHARMASK);
   2147   1.49       cgd 			switch (CCLASS(c)) {
   2148   1.49       cgd 			case ORDINARY:
   2149   1.49       cgd 				ttyrubo(tp, 1);
   2150   1.49       cgd 				break;
   2151   1.49       cgd 			case BACKSPACE:
   2152   1.49       cgd 			case CONTROL:
   2153   1.49       cgd 			case NEWLINE:
   2154   1.49       cgd 			case RETURN:
   2155   1.49       cgd 			case VTAB:
   2156   1.49       cgd 				if (ISSET(tp->t_lflag, ECHOCTL))
   2157   1.49       cgd 					ttyrubo(tp, 2);
   2158   1.49       cgd 				break;
   2159   1.49       cgd 			case TAB:
   2160   1.49       cgd 				if (tp->t_rocount < tp->t_rawq.c_cc) {
   2161   1.49       cgd 					ttyretype(tp);
   2162   1.49       cgd 					return;
   2163   1.49       cgd 				}
   2164   1.49       cgd 				savecol = tp->t_column;
   2165   1.49       cgd 				SET(tp->t_state, TS_CNTTB);
   2166   1.49       cgd 				SET(tp->t_lflag, FLUSHO);
   2167   1.49       cgd 				tp->t_column = tp->t_rocol;
   2168   1.52   deraadt 				for (cp = firstc(&tp->t_rawq, &tabc); cp;
   2169   1.49       cgd 				    cp = nextc(&tp->t_rawq, cp, &tabc))
   2170   1.49       cgd 					ttyecho(tabc, tp);
   2171   1.49       cgd 				CLR(tp->t_lflag, FLUSHO);
   2172   1.49       cgd 				CLR(tp->t_state, TS_CNTTB);
   2173   1.49       cgd 
   2174   1.49       cgd 				/* savecol will now be length of the tab. */
   2175   1.49       cgd 				savecol -= tp->t_column;
   2176   1.49       cgd 				tp->t_column += savecol;
   2177   1.49       cgd 				if (savecol > 8)
   2178   1.49       cgd 					savecol = 8;	/* overflow screw */
   2179   1.49       cgd 				while (--savecol >= 0)
   2180   1.49       cgd 					(void)ttyoutput('\b', tp);
   2181   1.49       cgd 				break;
   2182   1.49       cgd 			default:			/* XXX */
   2183  1.172  christos 				(void)printf("ttyrub: would panic c = %d, "
   2184  1.172  christos 				    "val = %d\n", c, CCLASS(c));
   2185   1.49       cgd 			}
   2186   1.49       cgd 		}
   2187   1.49       cgd 	} else if (ISSET(tp->t_lflag, ECHOPRT)) {
   2188   1.49       cgd 		if (!ISSET(tp->t_state, TS_ERASE)) {
   2189   1.49       cgd 			SET(tp->t_state, TS_ERASE);
   2190   1.49       cgd 			(void)ttyoutput('\\', tp);
   2191   1.49       cgd 		}
   2192   1.49       cgd 		ttyecho(c, tp);
   2193   1.49       cgd 	} else
   2194   1.49       cgd 		ttyecho(tp->t_cc[VERASE], tp);
   2195   1.49       cgd 	--tp->t_rocount;
   2196   1.49       cgd }
   2197   1.49       cgd 
   2198   1.49       cgd /*
   2199   1.49       cgd  * Back over cnt characters, erasing them.
   2200  1.201        ad  * Called with tty lock held.
   2201   1.49       cgd  */
   2202   1.49       cgd static void
   2203  1.126     lukem ttyrubo(struct tty *tp, int cnt)
   2204   1.49       cgd {
   2205   1.49       cgd 
   2206  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   2207  1.201        ad 
   2208   1.49       cgd 	while (cnt-- > 0) {
   2209   1.49       cgd 		(void)ttyoutput('\b', tp);
   2210   1.49       cgd 		(void)ttyoutput(' ', tp);
   2211   1.49       cgd 		(void)ttyoutput('\b', tp);
   2212   1.49       cgd 	}
   2213   1.49       cgd }
   2214   1.49       cgd 
   2215   1.49       cgd /*
   2216   1.49       cgd  * ttyretype --
   2217   1.49       cgd  *	Reprint the rawq line.  Note, it is assumed that c_cc has already
   2218   1.49       cgd  *	been checked.
   2219  1.147        pk  *
   2220  1.201        ad  * Called with tty lock held.
   2221   1.49       cgd  */
   2222   1.49       cgd void
   2223  1.126     lukem ttyretype(struct tty *tp)
   2224   1.49       cgd {
   2225  1.126     lukem 	u_char	*cp;
   2226  1.201        ad 	int	c;
   2227  1.201        ad 
   2228  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   2229   1.49       cgd 
   2230   1.49       cgd 	/* Echo the reprint character. */
   2231   1.49       cgd 	if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
   2232   1.49       cgd 		ttyecho(tp->t_cc[VREPRINT], tp);
   2233   1.49       cgd 
   2234   1.49       cgd 	(void)ttyoutput('\n', tp);
   2235   1.49       cgd 
   2236   1.49       cgd 	for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
   2237   1.49       cgd 		ttyecho(c, tp);
   2238   1.49       cgd 	for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
   2239   1.49       cgd 		ttyecho(c, tp);
   2240   1.49       cgd 	CLR(tp->t_state, TS_ERASE);
   2241   1.49       cgd 
   2242   1.49       cgd 	tp->t_rocount = tp->t_rawq.c_cc;
   2243   1.49       cgd 	tp->t_rocol = 0;
   2244   1.49       cgd }
   2245   1.49       cgd 
   2246   1.49       cgd /*
   2247   1.49       cgd  * Echo a typed character to the terminal.
   2248  1.201        ad  * Called with tty lock held.
   2249   1.49       cgd  */
   2250   1.49       cgd static void
   2251  1.126     lukem ttyecho(int c, struct tty *tp)
   2252   1.49       cgd {
   2253   1.49       cgd 
   2254  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   2255  1.201        ad 
   2256   1.49       cgd 	if (!ISSET(tp->t_state, TS_CNTTB))
   2257   1.49       cgd 		CLR(tp->t_lflag, FLUSHO);
   2258   1.49       cgd 	if ((!ISSET(tp->t_lflag, ECHO) &&
   2259   1.64        pk 	    (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) ||
   2260   1.49       cgd 	    ISSET(tp->t_lflag, EXTPROC))
   2261   1.49       cgd 		return;
   2262   1.65  christos 	if (((ISSET(tp->t_lflag, ECHOCTL) &&
   2263  1.138     enami 	    (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) ||
   2264   1.49       cgd 	    ISSET(c, TTY_CHARMASK) == 0177)) {
   2265   1.49       cgd 		(void)ttyoutput('^', tp);
   2266   1.49       cgd 		CLR(c, ~TTY_CHARMASK);
   2267   1.49       cgd 		if (c == 0177)
   2268   1.49       cgd 			c = '?';
   2269   1.49       cgd 		else
   2270   1.49       cgd 			c += 'A' - 1;
   2271   1.49       cgd 	}
   2272   1.49       cgd 	(void)ttyoutput(c, tp);
   2273   1.49       cgd }
   2274   1.49       cgd 
   2275   1.49       cgd /*
   2276   1.49       cgd  * Wake up any readers on a tty.
   2277  1.201        ad  * Called with tty lock held.
   2278   1.49       cgd  */
   2279   1.49       cgd void
   2280  1.126     lukem ttwakeup(struct tty *tp)
   2281   1.49       cgd {
   2282   1.49       cgd 
   2283  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   2284  1.201        ad 
   2285  1.213     rmind 	selnotify(&tp->t_rsel, 0, NOTE_SUBMIT);
   2286  1.201        ad 	if (ISSET(tp->t_state, TS_ASYNC))
   2287  1.201        ad 		ttysig(tp, TTYSIG_PG2, SIGIO);
   2288  1.224        ad 	cv_broadcast(&tp->t_rawcv);
   2289   1.49       cgd }
   2290   1.49       cgd 
   2291   1.49       cgd /*
   2292   1.49       cgd  * Look up a code for a specified speed in a conversion table;
   2293   1.49       cgd  * used by drivers to map software speed values to hardware parameters.
   2294   1.49       cgd  */
   2295   1.49       cgd int
   2296  1.166      matt ttspeedtab(int speed, const struct speedtab *table)
   2297   1.49       cgd {
   2298   1.49       cgd 
   2299  1.138     enami 	for (; table->sp_speed != -1; table++)
   2300   1.49       cgd 		if (table->sp_speed == speed)
   2301   1.49       cgd 			return (table->sp_code);
   2302   1.49       cgd 	return (-1);
   2303   1.49       cgd }
   2304   1.49       cgd 
   2305   1.49       cgd /*
   2306   1.49       cgd  * Set tty hi and low water marks.
   2307   1.49       cgd  *
   2308   1.49       cgd  * Try to arrange the dynamics so there's about one second
   2309   1.49       cgd  * from hi to low water.
   2310   1.49       cgd  */
   2311   1.49       cgd void
   2312  1.126     lukem ttsetwater(struct tty *tp)
   2313   1.49       cgd {
   2314  1.126     lukem 	int	cps, x;
   2315   1.49       cgd 
   2316  1.201        ad 	/* XXX not yet KASSERT(mutex_owned(&tty_lock)); */
   2317  1.201        ad 
   2318  1.126     lukem #define	CLAMP(x, h, l)	((x) > h ? h : ((x) < l) ? l : (x))
   2319   1.49       cgd 
   2320   1.49       cgd 	cps = tp->t_ospeed / 10;
   2321   1.49       cgd 	tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
   2322   1.49       cgd 	x += cps;
   2323   1.49       cgd 	x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
   2324   1.49       cgd 	tp->t_hiwat = roundup(x, CBSIZE);
   2325   1.49       cgd #undef	CLAMP
   2326   1.49       cgd }
   2327   1.49       cgd 
   2328   1.49       cgd /*
   2329  1.215        ad  * Prepare report on state of foreground process group.
   2330  1.220        ad  * Call with proc_lock held.
   2331   1.49       cgd  */
   2332   1.49       cgd void
   2333  1.215        ad ttygetinfo(struct tty *tp, int fromsig, char *buf, size_t bufsz)
   2334   1.49       cgd {
   2335  1.145   thorpej 	struct lwp	*l;
   2336  1.184  christos 	struct proc	*p, *pick = NULL;
   2337  1.126     lukem 	struct timeval	utime, stime;
   2338  1.126     lukem 	int		tmp;
   2339  1.196      yamt 	fixpt_t		pctcpu = 0;
   2340  1.183  christos 	const char	*msg;
   2341  1.215        ad 	char		lmsg[100];
   2342  1.215        ad 	long		rss;
   2343   1.49       cgd 
   2344  1.220        ad 	KASSERT(mutex_owned(proc_lock));
   2345  1.215        ad 
   2346  1.215        ad 	*buf = '\0';
   2347   1.49       cgd 
   2348   1.49       cgd 	if (tp->t_session == NULL)
   2349  1.183  christos 		msg = "not a controlling terminal\n";
   2350   1.49       cgd 	else if (tp->t_pgrp == NULL)
   2351  1.183  christos 		msg = "no foreground process group\n";
   2352  1.183  christos 	else if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == NULL)
   2353  1.183  christos 		msg = "empty foreground process group\n";
   2354   1.49       cgd 	else {
   2355   1.49       cgd 		/* Pick interesting process. */
   2356  1.215        ad 		for (; p != NULL; p = LIST_NEXT(p, p_pglist)) {
   2357  1.217      yamt 			struct proc *oldpick;
   2358  1.217      yamt 
   2359  1.215        ad 			if (pick == NULL) {
   2360  1.215        ad 				pick = p;
   2361  1.215        ad 				continue;
   2362  1.215        ad 			}
   2363  1.221        ad 			if (pick->p_lock < p->p_lock) {
   2364  1.221        ad 				mutex_enter(pick->p_lock);
   2365  1.221        ad 				mutex_enter(p->p_lock);
   2366  1.221        ad 			} else if (pick->p_lock > p->p_lock) {
   2367  1.221        ad 				mutex_enter(p->p_lock);
   2368  1.221        ad 				mutex_enter(pick->p_lock);
   2369  1.221        ad 			} else
   2370  1.221        ad 				mutex_enter(p->p_lock);
   2371  1.217      yamt 			oldpick = pick;
   2372   1.49       cgd 			if (proc_compare(pick, p))
   2373   1.49       cgd 				pick = p;
   2374  1.221        ad 			mutex_exit(p->p_lock);
   2375  1.221        ad 			if (p->p_lock != oldpick->p_lock)
   2376  1.221        ad 				mutex_exit(oldpick->p_lock);
   2377  1.215        ad 		}
   2378  1.183  christos 		if (fromsig &&
   2379  1.183  christos 		    (SIGACTION_PS(pick->p_sigacts, SIGINFO).sa_flags &
   2380  1.183  christos 		    SA_NOKERNINFO))
   2381  1.183  christos 			return;
   2382  1.183  christos 		msg = NULL;
   2383  1.183  christos 	}
   2384  1.183  christos 
   2385  1.183  christos 	/* Print load average. */
   2386  1.183  christos 	tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
   2387  1.215        ad 	snprintf(lmsg, sizeof(lmsg), "load: %d.%02d ", tmp / 100, tmp % 100);
   2388  1.215        ad 	strlcat(buf, lmsg, bufsz);
   2389   1.49       cgd 
   2390  1.184  christos 	if (pick == NULL) {
   2391  1.215        ad 		strlcat(buf, msg, bufsz);
   2392  1.183  christos 		return;
   2393  1.183  christos 	}
   2394  1.183  christos 
   2395  1.215        ad 	snprintf(lmsg, sizeof(lmsg), " cmd: %s %d [", pick->p_comm,
   2396  1.215        ad 	    pick->p_pid);
   2397  1.215        ad 	strlcat(buf, lmsg, bufsz);
   2398  1.215        ad 
   2399  1.221        ad 	mutex_enter(pick->p_lock);
   2400  1.196      yamt 	LIST_FOREACH(l, &pick->p_lwps, l_sibling) {
   2401  1.214      yamt 		lwp_lock(l);
   2402  1.214      yamt 		snprintf(lmsg, sizeof(lmsg), "%s%s",
   2403  1.214      yamt 		    l->l_stat == LSONPROC ? "running" :
   2404  1.214      yamt 		    l->l_stat == LSRUN ? "runnable" :
   2405  1.214      yamt 		    l->l_wchan ? l->l_wmesg : "iowait",
   2406  1.214      yamt 		    (LIST_NEXT(l, l_sibling) != NULL) ? " " : "] ");
   2407  1.214      yamt 		lwp_unlock(l);
   2408  1.215        ad 		strlcat(buf, lmsg, bufsz);
   2409  1.214      yamt 		pctcpu += l->l_pctcpu;
   2410  1.196      yamt 	}
   2411  1.196      yamt 	pctcpu += pick->p_pctcpu;
   2412  1.190        ad 	calcru(pick, &utime, &stime, NULL, NULL);
   2413  1.221        ad 	mutex_exit(pick->p_lock);
   2414  1.183  christos 
   2415  1.215        ad 	/* Round up and print user+system time, %CPU and RSS. */
   2416  1.183  christos 	utime.tv_usec += 5000;
   2417  1.183  christos 	if (utime.tv_usec >= 1000000) {
   2418  1.183  christos 		utime.tv_sec += 1;
   2419  1.183  christos 		utime.tv_usec -= 1000000;
   2420  1.183  christos 	}
   2421  1.183  christos 	stime.tv_usec += 5000;
   2422  1.183  christos 	if (stime.tv_usec >= 1000000) {
   2423  1.183  christos 		stime.tv_sec += 1;
   2424  1.183  christos 		stime.tv_usec -= 1000000;
   2425  1.183  christos 	}
   2426  1.123   thorpej #define	pgtok(a)	(((u_long) ((a) * PAGE_SIZE) / 1024))
   2427  1.196      yamt 	tmp = (pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
   2428  1.215        ad 	if (pick->p_stat == SIDL || P_ZOMBIE(pick))
   2429  1.215        ad 		rss = 0;
   2430  1.215        ad 	else
   2431  1.215        ad 		rss = pgtok(vm_resident_count(pick->p_vmspace));
   2432  1.215        ad 
   2433  1.215        ad 	snprintf(lmsg, sizeof(lmsg), "%ld.%02ldu %ld.%02lds %d%% %ldk",
   2434  1.215        ad 	    (long)utime.tv_sec, (long)utime.tv_usec / 10000,
   2435  1.215        ad 	    (long)stime.tv_sec, (long)stime.tv_usec / 10000,
   2436  1.215        ad 	    tmp / 100, rss);
   2437  1.215        ad 	strlcat(buf, lmsg, bufsz);
   2438  1.215        ad }
   2439  1.183  christos 
   2440  1.215        ad /*
   2441  1.215        ad  * Print report on state of foreground process group.
   2442  1.215        ad  * Call with tty_lock held.
   2443  1.215        ad  */
   2444  1.215        ad void
   2445  1.215        ad ttyputinfo(struct tty *tp, char *buf)
   2446  1.215        ad {
   2447  1.215        ad 
   2448  1.215        ad 	KASSERT(mutex_owned(&tty_lock));
   2449  1.215        ad 
   2450  1.215        ad 	if (ttycheckoutq_wlock(tp, 0) == 0)
   2451  1.215        ad 		return;
   2452  1.215        ad 	ttyprintf_nolock(tp, "%s\n", buf);
   2453   1.49       cgd 	tp->t_rocount = 0;	/* so pending input will be retyped if BS */
   2454   1.49       cgd }
   2455   1.49       cgd 
   2456   1.49       cgd /*
   2457   1.49       cgd  * Returns 1 if p2 is "better" than p1
   2458   1.49       cgd  *
   2459   1.49       cgd  * The algorithm for picking the "interesting" process is thus:
   2460   1.49       cgd  *
   2461   1.49       cgd  *	1) Only foreground processes are eligible - implied.
   2462   1.49       cgd  *	2) Runnable processes are favored over anything else.  The runner
   2463  1.200        ad  *	   with the highest CPU utilization is picked (l_pctcpu).  Ties are
   2464   1.49       cgd  *	   broken by picking the highest pid.
   2465   1.49       cgd  *	3) The sleeper with the shortest sleep time is next.  With ties,
   2466   1.49       cgd  *	   we pick out just "short-term" sleepers (P_SINTR == 0).
   2467   1.49       cgd  *	4) Further ties are broken by picking the highest pid.
   2468   1.49       cgd  */
   2469  1.145   thorpej #define	ISRUN(p)	((p)->p_nrlwps > 0)
   2470  1.138     enami #define	TESTAB(a, b)	((a)<<1 | (b))
   2471  1.138     enami #define	ONLYA	2
   2472  1.138     enami #define	ONLYB	1
   2473  1.138     enami #define	BOTH	3
   2474   1.49       cgd 
   2475   1.49       cgd static int
   2476  1.126     lukem proc_compare(struct proc *p1, struct proc *p2)
   2477   1.49       cgd {
   2478  1.200        ad 	lwp_t *l1, *l2;
   2479   1.49       cgd 
   2480  1.221        ad 	KASSERT(mutex_owned(p1->p_lock));
   2481  1.221        ad 	KASSERT(mutex_owned(p2->p_lock));
   2482  1.215        ad 
   2483  1.215        ad 	if ((l1 = LIST_FIRST(&p1->p_lwps)) == NULL)
   2484   1.49       cgd 		return (1);
   2485  1.215        ad 	if ((l2 = LIST_FIRST(&p2->p_lwps)) == NULL)
   2486  1.215        ad 		return (0);
   2487   1.49       cgd 	/*
   2488   1.49       cgd 	 * see if at least one of them is runnable
   2489   1.49       cgd 	 */
   2490   1.49       cgd 	switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
   2491   1.49       cgd 	case ONLYA:
   2492   1.49       cgd 		return (0);
   2493   1.49       cgd 	case ONLYB:
   2494   1.49       cgd 		return (1);
   2495   1.49       cgd 	case BOTH:
   2496   1.49       cgd 		/*
   2497  1.161       wiz 		 * tie - favor one with highest recent CPU utilization
   2498   1.49       cgd 		 */
   2499  1.200        ad 		if (l2->l_pctcpu > l1->l_pctcpu)
   2500   1.49       cgd 			return (1);
   2501   1.49       cgd 		return (p2->p_pid > p1->p_pid);	/* tie - return highest pid */
   2502   1.49       cgd 	}
   2503   1.49       cgd 	/*
   2504   1.49       cgd  	 * weed out zombies
   2505   1.49       cgd 	 */
   2506  1.114   thorpej 	switch (TESTAB(P_ZOMBIE(p1), P_ZOMBIE(p2))) {
   2507   1.49       cgd 	case ONLYA:
   2508   1.49       cgd 		return (1);
   2509   1.49       cgd 	case ONLYB:
   2510   1.49       cgd 		return (0);
   2511   1.49       cgd 	case BOTH:
   2512  1.138     enami 		return (p2->p_pid > p1->p_pid);	/* tie - return highest pid */
   2513   1.49       cgd 	}
   2514   1.49       cgd 	/*
   2515   1.49       cgd 	 * pick the one with the smallest sleep time
   2516   1.49       cgd 	 */
   2517  1.215        ad 	if (l2->l_slptime > l2->l_slptime)
   2518   1.49       cgd 		return (0);
   2519  1.215        ad 	if (l2->l_slptime > l2->l_slptime)
   2520   1.49       cgd 		return (1);
   2521   1.49       cgd 	/*
   2522   1.49       cgd 	 * favor one sleeping in a non-interruptible sleep
   2523   1.49       cgd 	 */
   2524  1.215        ad 	if (l2->l_flag & LW_SINTR && (l2->l_flag & LW_SINTR) == 0)
   2525   1.49       cgd 		return (1);
   2526  1.215        ad 	if (l2->l_flag & LW_SINTR && (l2->l_flag & LW_SINTR) == 0)
   2527   1.49       cgd 		return (0);
   2528   1.49       cgd 	return (p2->p_pid > p1->p_pid);		/* tie - return highest pid */
   2529   1.49       cgd }
   2530   1.49       cgd 
   2531   1.49       cgd /*
   2532   1.49       cgd  * Output char to tty; console putchar style.
   2533  1.147        pk  * Can be called with tty lock held through kprintf() machinery..
   2534   1.49       cgd  */
   2535   1.49       cgd int
   2536  1.149  christos tputchar(int c, int flags, struct tty *tp)
   2537   1.49       cgd {
   2538  1.201        ad 	int r = 0;
   2539   1.49       cgd 
   2540  1.149  christos 	if ((flags & NOLOCK) == 0)
   2541  1.201        ad 		mutex_spin_enter(&tty_lock);
   2542  1.147        pk 	if (!CONNECTED(tp)) {
   2543  1.147        pk 		r = -1;
   2544  1.147        pk 		goto out;
   2545   1.49       cgd 	}
   2546   1.49       cgd 	if (c == '\n')
   2547   1.49       cgd 		(void)ttyoutput('\r', tp);
   2548   1.49       cgd 	(void)ttyoutput(c, tp);
   2549   1.49       cgd 	ttstart(tp);
   2550  1.147        pk out:
   2551  1.149  christos 	if ((flags & NOLOCK) == 0)
   2552  1.201        ad 		mutex_spin_exit(&tty_lock);
   2553  1.147        pk 	return (r);
   2554   1.49       cgd }
   2555   1.49       cgd 
   2556   1.49       cgd /*
   2557   1.49       cgd  * Sleep on chan, returning ERESTART if tty changed while we napped and
   2558   1.49       cgd  * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep.  If
   2559   1.49       cgd  * the tty is revoked, restarting a pending call will redo validation done
   2560   1.49       cgd  * at the start of the call.
   2561  1.147        pk  *
   2562  1.201        ad  * Must be called with the tty lock held.
   2563   1.49       cgd  */
   2564   1.49       cgd int
   2565  1.201        ad ttysleep(struct tty *tp, kcondvar_t *cv, bool catch, int timo)
   2566   1.49       cgd {
   2567  1.126     lukem 	int	error;
   2568  1.126     lukem 	short	gen;
   2569   1.49       cgd 
   2570  1.201        ad 	KASSERT(mutex_owned(&tty_lock));
   2571  1.201        ad 
   2572   1.49       cgd 	gen = tp->t_gen;
   2573  1.201        ad 	if (catch)
   2574  1.201        ad 		error = cv_timedwait_sig(cv, &tty_lock, timo);
   2575  1.201        ad 	else
   2576  1.201        ad 		error = cv_timedwait(cv, &tty_lock, timo);
   2577  1.201        ad 	if (error != 0)
   2578   1.49       cgd 		return (error);
   2579   1.49       cgd 	return (tp->t_gen == gen ? 0 : ERESTART);
   2580   1.49       cgd }
   2581   1.49       cgd 
   2582   1.49       cgd /*
   2583   1.69       mrg  * Attach a tty to the tty list.
   2584   1.72       mrg  *
   2585   1.72       mrg  * This should be called ONLY once per real tty (including pty's).
   2586   1.72       mrg  * eg, on the sparc, the keyboard and mouse have struct tty's that are
   2587   1.72       mrg  * distinctly NOT usable as tty's, and thus should not be attached to
   2588   1.72       mrg  * the ttylist.  This is why this call is not done from ttymalloc().
   2589   1.72       mrg  *
   2590   1.72       mrg  * Device drivers should attach tty's at a similar time that they are
   2591   1.72       mrg  * ttymalloc()'ed, or, for the case of statically allocated struct tty's
   2592   1.72       mrg  * either in the attach or (first) open routine.
   2593   1.69       mrg  */
   2594   1.69       mrg void
   2595  1.126     lukem tty_attach(struct tty *tp)
   2596   1.69       mrg {
   2597   1.71       cgd 
   2598  1.201        ad 	mutex_spin_enter(&tty_lock);
   2599   1.69       mrg 	TAILQ_INSERT_TAIL(&ttylist, tp, tty_link);
   2600   1.69       mrg 	++tty_count;
   2601  1.201        ad 	mutex_spin_exit(&tty_lock);
   2602   1.69       mrg }
   2603   1.69       mrg 
   2604   1.69       mrg /*
   2605   1.69       mrg  * Remove a tty from the tty list.
   2606   1.69       mrg  */
   2607   1.69       mrg void
   2608  1.126     lukem tty_detach(struct tty *tp)
   2609   1.69       mrg {
   2610   1.71       cgd 
   2611  1.201        ad 	mutex_spin_enter(&tty_lock);
   2612   1.69       mrg 	--tty_count;
   2613   1.69       mrg #ifdef DIAGNOSTIC
   2614   1.69       mrg 	if (tty_count < 0)
   2615   1.69       mrg 		panic("tty_detach: tty_count < 0");
   2616   1.69       mrg #endif
   2617   1.70       mrg 	TAILQ_REMOVE(&ttylist, tp, tty_link);
   2618  1.201        ad 	mutex_spin_exit(&tty_lock);
   2619   1.69       mrg }
   2620   1.69       mrg 
   2621   1.69       mrg /*
   2622   1.49       cgd  * Allocate a tty structure and its associated buffers.
   2623   1.49       cgd  */
   2624   1.49       cgd struct tty *
   2625  1.126     lukem ttymalloc(void)
   2626   1.49       cgd {
   2627  1.126     lukem 	struct tty	*tp;
   2628  1.202        ad 	int i;
   2629   1.49       cgd 
   2630  1.216        ad 	tp = kmem_zalloc(sizeof(*tp), KM_SLEEP);
   2631  1.197        ad 	callout_init(&tp->t_rstrt_ch, 0);
   2632  1.199     joerg 	callout_setfunc(&tp->t_rstrt_ch, ttrstrt, tp);
   2633   1.84   mycroft 	/* XXX: default to 1024 chars for now */
   2634   1.84   mycroft 	clalloc(&tp->t_rawq, 1024, 1);
   2635  1.224        ad 	cv_init(&tp->t_rawcv, "ttyraw");
   2636  1.224        ad 	cv_init(&tp->t_rawcvf, "ttyrawf");
   2637   1.84   mycroft 	clalloc(&tp->t_canq, 1024, 1);
   2638  1.224        ad 	cv_init(&tp->t_cancv, "ttycan");
   2639  1.224        ad 	cv_init(&tp->t_cancvf, "ttycanf");
   2640   1.49       cgd 	/* output queue doesn't need quoting */
   2641   1.84   mycroft 	clalloc(&tp->t_outq, 1024, 0);
   2642  1.224        ad 	cv_init(&tp->t_outcv, "ttycan");
   2643  1.224        ad 	cv_init(&tp->t_outcvf, "ttycanf");
   2644  1.121       eeh 	/* Set default line discipline. */
   2645  1.177   thorpej 	tp->t_linesw = ttyldisc_default();
   2646  1.198        ad 	selinit(&tp->t_rsel);
   2647  1.198        ad 	selinit(&tp->t_wsel);
   2648  1.202        ad 	for (i = 0; i < TTYSIG_COUNT; i++)
   2649  1.202        ad 		sigemptyset(&tp->t_sigs[i]);
   2650  1.138     enami 	return (tp);
   2651   1.49       cgd }
   2652   1.49       cgd 
   2653   1.49       cgd /*
   2654   1.49       cgd  * Free a tty structure and its buffers.
   2655   1.73       mrg  *
   2656   1.73       mrg  * Be sure to call tty_detach() for any tty that has been
   2657   1.73       mrg  * tty_attach()ed.
   2658   1.49       cgd  */
   2659   1.49       cgd void
   2660  1.126     lukem ttyfree(struct tty *tp)
   2661   1.49       cgd {
   2662  1.202        ad 	int i;
   2663   1.71       cgd 
   2664  1.220        ad 	mutex_enter(proc_lock);
   2665  1.201        ad 	mutex_enter(&tty_lock);
   2666  1.202        ad 	for (i = 0; i < TTYSIG_COUNT; i++)
   2667  1.202        ad 		sigemptyset(&tp->t_sigs[i]);
   2668  1.201        ad 	if (tp->t_sigcount != 0)
   2669  1.201        ad 		TAILQ_REMOVE(&tty_sigqueue, tp, t_sigqueue);
   2670  1.201        ad 	mutex_exit(&tty_lock);
   2671  1.220        ad 	mutex_exit(proc_lock);
   2672  1.201        ad 
   2673  1.219        ad 	callout_halt(&tp->t_rstrt_ch, NULL);
   2674  1.216        ad 	callout_destroy(&tp->t_rstrt_ch);
   2675  1.177   thorpej 	ttyldisc_release(tp->t_linesw);
   2676   1.49       cgd 	clfree(&tp->t_rawq);
   2677   1.49       cgd 	clfree(&tp->t_canq);
   2678   1.49       cgd 	clfree(&tp->t_outq);
   2679  1.224        ad 	cv_destroy(&tp->t_rawcv);
   2680  1.224        ad 	cv_destroy(&tp->t_rawcvf);
   2681  1.224        ad 	cv_destroy(&tp->t_cancv);
   2682  1.224        ad 	cv_destroy(&tp->t_cancvf);
   2683  1.224        ad 	cv_destroy(&tp->t_outcv);
   2684  1.224        ad 	cv_destroy(&tp->t_outcvf);
   2685  1.198        ad 	seldestroy(&tp->t_rsel);
   2686  1.198        ad 	seldestroy(&tp->t_wsel);
   2687  1.216        ad 	kmem_free(tp, sizeof(*tp));
   2688  1.149  christos }
   2689  1.149  christos 
   2690  1.149  christos /*
   2691  1.149  christos  * ttyprintf_nolock: send a message to a specific tty, without locking.
   2692  1.149  christos  *
   2693  1.149  christos  * => should be used only by tty driver or anything that knows the
   2694  1.149  christos  *    underlying tty will not be revoked(2)'d away.  [otherwise,
   2695  1.149  christos  *    use tprintf]
   2696  1.149  christos  */
   2697  1.149  christos static void
   2698  1.149  christos ttyprintf_nolock(struct tty *tp, const char *fmt, ...)
   2699  1.149  christos {
   2700  1.149  christos 	va_list ap;
   2701  1.149  christos 
   2702  1.149  christos 	/* No mutex needed; going to process TTY. */
   2703  1.149  christos 	va_start(ap, fmt);
   2704  1.149  christos 	kprintf(fmt, TOTTY|NOLOCK, tp, NULL, ap);
   2705  1.149  christos 	va_end(ap);
   2706   1.49       cgd }
   2707  1.201        ad 
   2708  1.201        ad /*
   2709  1.201        ad  * Initialize the tty subsystem.
   2710  1.201        ad  */
   2711  1.201        ad void
   2712  1.201        ad tty_init(void)
   2713  1.201        ad {
   2714  1.201        ad 
   2715  1.201        ad 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
   2716  1.201        ad 	tty_sigsih = softint_establish(SOFTINT_CLOCK, ttysigintr, NULL);
   2717  1.201        ad 	KASSERT(tty_sigsih != NULL);
   2718  1.201        ad }
   2719  1.201        ad 
   2720  1.201        ad /*
   2721  1.201        ad  * Send a signal from a tty to its process group or session leader.
   2722  1.201        ad  * Handoff to the target is deferred to a soft interrupt.
   2723  1.201        ad  */
   2724  1.201        ad void
   2725  1.201        ad ttysig(struct tty *tp, enum ttysigtype st, int sig)
   2726  1.201        ad {
   2727  1.202        ad 	sigset_t *sp;
   2728  1.201        ad 
   2729  1.201        ad 	/* XXXSMP not yet KASSERT(mutex_owned(&tty_lock)); */
   2730  1.201        ad 
   2731  1.202        ad 	sp = &tp->t_sigs[st];
   2732  1.202        ad 	if (sigismember(sp, sig))
   2733  1.202        ad 		return;
   2734  1.202        ad 	sigaddset(sp, sig);
   2735  1.202        ad 	if (tp->t_sigcount++ == 0)
   2736  1.202        ad 		TAILQ_INSERT_TAIL(&tty_sigqueue, tp, t_sigqueue);
   2737  1.201        ad 	softint_schedule(tty_sigsih);
   2738  1.201        ad }
   2739  1.201        ad 
   2740  1.201        ad /*
   2741  1.201        ad  * Deliver deferred signals from ttys.  Note that the process groups
   2742  1.201        ad  * and sessions associated with the ttys may have changed from when
   2743  1.201        ad  * the signal was originally sent, but in practice it should not matter.
   2744  1.201        ad  * For signals produced as a result of a syscall, the soft interrupt
   2745  1.201        ad  * will fire before the syscall returns to the user.
   2746  1.201        ad  */
   2747  1.201        ad static void
   2748  1.201        ad ttysigintr(void *cookie)
   2749  1.201        ad {
   2750  1.201        ad 	struct tty *tp;
   2751  1.201        ad 	enum ttysigtype st;
   2752  1.201        ad 	struct pgrp *pgrp;
   2753  1.201        ad 	struct session *sess;
   2754  1.215        ad 	int sig, lflag;
   2755  1.215        ad 	char infobuf[200];
   2756  1.201        ad 
   2757  1.220        ad 	mutex_enter(proc_lock);
   2758  1.215        ad 	mutex_spin_enter(&tty_lock);
   2759  1.215        ad 	while ((tp = TAILQ_FIRST(&tty_sigqueue)) != NULL) {
   2760  1.201        ad 		KASSERT(tp->t_sigcount > 0);
   2761  1.202        ad 		for (st = 0; st < TTYSIG_COUNT; st++) {
   2762  1.202        ad 			if ((sig = firstsig(&tp->t_sigs[st])) != 0)
   2763  1.202        ad 				break;
   2764  1.201        ad 		}
   2765  1.202        ad 		KASSERT(st < TTYSIG_COUNT);
   2766  1.202        ad 		sigdelset(&tp->t_sigs[st], sig);
   2767  1.201        ad 		if (--tp->t_sigcount == 0)
   2768  1.201        ad 			TAILQ_REMOVE(&tty_sigqueue, tp, t_sigqueue);
   2769  1.201        ad 		pgrp = tp->t_pgrp;
   2770  1.201        ad 		sess = tp->t_session;
   2771  1.215        ad 		lflag = tp->t_lflag;
   2772  1.215        ad 		if  (sig == SIGINFO) {
   2773  1.215        ad 			if (ISSET(tp->t_state, TS_SIGINFO)) {
   2774  1.215        ad 				/* Via ioctl: ignore tty option. */
   2775  1.215        ad 				tp->t_state &= ~TS_SIGINFO;
   2776  1.218        ad 				lflag |= ISIG;
   2777  1.215        ad 			}
   2778  1.215        ad 			if (!ISSET(lflag, NOKERNINFO)) {
   2779  1.215        ad 				mutex_spin_exit(&tty_lock);
   2780  1.215        ad 				ttygetinfo(tp, 1, infobuf, sizeof(infobuf));
   2781  1.215        ad 				mutex_spin_enter(&tty_lock);
   2782  1.215        ad 				ttyputinfo(tp, infobuf);
   2783  1.215        ad 			}
   2784  1.215        ad 			if (!ISSET(lflag, ISIG))
   2785  1.215        ad 				continue;
   2786  1.215        ad 		}
   2787  1.201        ad 		mutex_spin_exit(&tty_lock);
   2788  1.215        ad 		KASSERT(sig != 0);
   2789  1.201        ad 		switch (st) {
   2790  1.201        ad 		case TTYSIG_PG1:
   2791  1.201        ad 			if (pgrp != NULL)
   2792  1.201        ad 				pgsignal(pgrp, sig, 1);
   2793  1.201        ad 			break;
   2794  1.201        ad 		case TTYSIG_PG2:
   2795  1.203        ad 			if (pgrp != NULL)
   2796  1.203        ad 				pgsignal(pgrp, sig, sess != NULL);
   2797  1.201        ad 			break;
   2798  1.201        ad 		case TTYSIG_LEADER:
   2799  1.201        ad 			if (sess != NULL && sess->s_leader != NULL)
   2800  1.201        ad 				psignal(sess->s_leader, sig);
   2801  1.201        ad 			break;
   2802  1.202        ad 		default:
   2803  1.202        ad 			/* NOTREACHED */
   2804  1.202        ad 			break;
   2805  1.201        ad 		}
   2806  1.215        ad 		mutex_spin_enter(&tty_lock);
   2807  1.201        ad 	}
   2808  1.215        ad 	mutex_spin_exit(&tty_lock);
   2809  1.220        ad 	mutex_exit(proc_lock);
   2810  1.201        ad }
   2811