Deleted Added
full compact
tty.c (90375) tty.c (91140)
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)tty.c 8.8 (Berkeley) 1/21/94
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)tty.c 8.8 (Berkeley) 1/21/94
39 * $FreeBSD: head/sys/kern/tty.c 90375 2002-02-07 23:06:26Z peter $
39 * $FreeBSD: head/sys/kern/tty.c 91140 2002-02-23 11:12:57Z tanimura $
40 */
41
42/*-
43 * TODO:
44 * o Fix races for sending the start char in ttyflush().
45 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
46 * With luck, there will be MIN chars before select() returns().
47 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
48 * o Don't allow input in TS_ZOMBIE case. It would be visible through
49 * FIONREAD.
50 * o Do the new sio locking stuff here and use it to avoid special
51 * case for EXTPROC?
52 * o Lock PENDIN too?
53 * o Move EXTPROC and/or PENDIN to t_state?
54 * o Wrap most of ttioctl in spltty/splx.
55 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
56 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
57 * o Don't allow certain termios flags to affect disciplines other
58 * than TTYDISC. Cancel their effects before switch disciplines
59 * and ignore them if they are set while we are in another
60 * discipline.
61 * o Now that historical speed conversions are handled here, don't
62 * do them in drivers.
63 * o Check for TS_CARR_ON being set while everything is closed and not
64 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
65 * so it would live until the next open even if carrier drops.
66 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
67 * only when _all_ openers leave open().
68 */
69
70#include "opt_compat.h"
71#include "opt_uconsole.h"
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/filio.h>
76#include <sys/lock.h>
77#include <sys/mutex.h>
40 */
41
42/*-
43 * TODO:
44 * o Fix races for sending the start char in ttyflush().
45 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
46 * With luck, there will be MIN chars before select() returns().
47 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
48 * o Don't allow input in TS_ZOMBIE case. It would be visible through
49 * FIONREAD.
50 * o Do the new sio locking stuff here and use it to avoid special
51 * case for EXTPROC?
52 * o Lock PENDIN too?
53 * o Move EXTPROC and/or PENDIN to t_state?
54 * o Wrap most of ttioctl in spltty/splx.
55 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
56 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
57 * o Don't allow certain termios flags to affect disciplines other
58 * than TTYDISC. Cancel their effects before switch disciplines
59 * and ignore them if they are set while we are in another
60 * discipline.
61 * o Now that historical speed conversions are handled here, don't
62 * do them in drivers.
63 * o Check for TS_CARR_ON being set while everything is closed and not
64 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
65 * so it would live until the next open even if carrier drops.
66 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
67 * only when _all_ openers leave open().
68 */
69
70#include "opt_compat.h"
71#include "opt_uconsole.h"
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/filio.h>
76#include <sys/lock.h>
77#include <sys/mutex.h>
78#include <sys/sx.h>
78#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
79#include <sys/ioctl_compat.h>
80#endif
81#include <sys/proc.h>
82#define TTYDEFCHARS
83#include <sys/tty.h>
84#undef TTYDEFCHARS
85#include <sys/fcntl.h>
86#include <sys/conf.h>
87#include <sys/dkstat.h>
88#include <sys/poll.h>
89#include <sys/kernel.h>
90#include <sys/vnode.h>
91#include <sys/signalvar.h>
92#include <sys/resourcevar.h>
93#include <sys/malloc.h>
94#include <sys/filedesc.h>
95#include <sys/sysctl.h>
96
97#include <vm/vm.h>
98#include <vm/pmap.h>
99#include <vm/vm_map.h>
100
101MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures");
102
103static int proc_compare __P((struct proc *p1, struct proc *p2));
104static int ttnread __P((struct tty *tp));
105static void ttyecho __P((int c, struct tty *tp));
106static int ttyoutput __P((int c, register struct tty *tp));
107static void ttypend __P((struct tty *tp));
108static void ttyretype __P((struct tty *tp));
109static void ttyrub __P((int c, struct tty *tp));
110static void ttyrubo __P((struct tty *tp, int cnt));
111static void ttyunblock __P((struct tty *tp));
112static int ttywflush __P((struct tty *tp));
113static int filt_ttyread __P((struct knote *kn, long hint));
114static void filt_ttyrdetach __P((struct knote *kn));
115static int filt_ttywrite __P((struct knote *kn, long hint));
116static void filt_ttywdetach __P((struct knote *kn));
117
118/*
119 * Table with character classes and parity. The 8th bit indicates parity,
120 * the 7th bit indicates the character is an alphameric or underscore (for
121 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
122 * are 0 then the character needs no special processing on output; classes
123 * other than 0 might be translated or (not currently) require delays.
124 */
125#define E 0x00 /* Even parity. */
126#define O 0x80 /* Odd parity. */
127#define PARITY(c) (char_type[c] & O)
128
129#define ALPHA 0x40 /* Alpha or underscore. */
130#define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
131
132#define CCLASSMASK 0x3f
133#define CCLASS(c) (char_type[c] & CCLASSMASK)
134
135#define BS BACKSPACE
136#define CC CONTROL
137#define CR RETURN
138#define NA ORDINARY | ALPHA
139#define NL NEWLINE
140#define NO ORDINARY
141#define TB TAB
142#define VT VTAB
143
144static u_char const char_type[] = {
145 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
146 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
147 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
148 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
149 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
150 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
151 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
152 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
153 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
154 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
155 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
156 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
157 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
158 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
159 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
160 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
161 /*
162 * Meta chars; should be settable per character set;
163 * for now, treat them all as normal characters.
164 */
165 NA, NA, NA, NA, NA, NA, NA, NA,
166 NA, NA, NA, NA, NA, NA, NA, NA,
167 NA, NA, NA, NA, NA, NA, NA, NA,
168 NA, NA, NA, NA, NA, NA, NA, NA,
169 NA, NA, NA, NA, NA, NA, NA, NA,
170 NA, NA, NA, NA, NA, NA, NA, NA,
171 NA, NA, NA, NA, NA, NA, NA, NA,
172 NA, NA, NA, NA, NA, NA, NA, NA,
173 NA, NA, NA, NA, NA, NA, NA, NA,
174 NA, NA, NA, NA, NA, NA, NA, NA,
175 NA, NA, NA, NA, NA, NA, NA, NA,
176 NA, NA, NA, NA, NA, NA, NA, NA,
177 NA, NA, NA, NA, NA, NA, NA, NA,
178 NA, NA, NA, NA, NA, NA, NA, NA,
179 NA, NA, NA, NA, NA, NA, NA, NA,
180 NA, NA, NA, NA, NA, NA, NA, NA,
181};
182#undef BS
183#undef CC
184#undef CR
185#undef NA
186#undef NL
187#undef NO
188#undef TB
189#undef VT
190
191/* Macros to clear/set/test flags. */
192#define SET(t, f) (t) |= (f)
193#define CLR(t, f) (t) &= ~(f)
194#define ISSET(t, f) ((t) & (f))
195
196#undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
197#define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */
198
199/*
200 * list of struct tty where pstat(8) can pick it up with sysctl
201 */
202static SLIST_HEAD(, tty) tty_list;
203
204static int drainwait = 5*60;
205SYSCTL_INT(_kern, OID_AUTO, drainwait, CTLFLAG_RW, &drainwait,
206 0, "Output drain timeout in seconds");
207
208/*
209 * Initial open of tty, or (re)entry to standard tty line discipline.
210 */
211int
212ttyopen(device, tp)
213 dev_t device;
214 register struct tty *tp;
215{
216 int s;
217
218 s = spltty();
219 tp->t_dev = device;
220 if (!ISSET(tp->t_state, TS_ISOPEN)) {
221 SET(tp->t_state, TS_ISOPEN);
222 if (ISSET(tp->t_cflag, CLOCAL))
223 SET(tp->t_state, TS_CONNECTED);
224 bzero(&tp->t_winsize, sizeof(tp->t_winsize));
225 }
226 /* XXX don't hang forever on output */
227 if (tp->t_timeout < 0)
228 tp->t_timeout = drainwait*hz;
229 ttsetwater(tp);
230 splx(s);
231 return (0);
232}
233
234/*
235 * Handle close() on a tty line: flush and set to initial state,
236 * bumping generation number so that pending read/write calls
237 * can detect recycling of the tty.
238 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
239 * and l_close() should have flushed, but we repeat the spltty() and
240 * the flush in case there are buggy callers.
241 */
242int
243ttyclose(tp)
244 register struct tty *tp;
245{
246 int s;
247
248 funsetown(tp->t_sigio);
249 s = spltty();
250 if (constty == tp)
251 constty = NULL;
252
253 ttyflush(tp, FREAD | FWRITE);
254 clist_free_cblocks(&tp->t_canq);
255 clist_free_cblocks(&tp->t_outq);
256 clist_free_cblocks(&tp->t_rawq);
257
258 tp->t_gen++;
259 tp->t_line = TTYDISC;
260 tp->t_pgrp = NULL;
261 tp->t_session = NULL;
262 tp->t_state = 0;
263 splx(s);
264 return (0);
265}
266
267#define FLUSHQ(q) { \
268 if ((q)->c_cc) \
269 ndflush(q, (q)->c_cc); \
270}
271
272/* Is 'c' a line delimiter ("break" character)? */
273#define TTBREAKC(c, lflag) \
274 ((c) == '\n' || (((c) == cc[VEOF] || \
275 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
276 (c) != _POSIX_VDISABLE))
277
278/*
279 * Process input of a single character received on a tty.
280 */
281int
282ttyinput(c, tp)
283 register int c;
284 register struct tty *tp;
285{
286 register tcflag_t iflag, lflag;
287 register cc_t *cc;
288 int i, err;
289
290 /*
291 * If input is pending take it first.
292 */
293 lflag = tp->t_lflag;
294 if (ISSET(lflag, PENDIN))
295 ttypend(tp);
296 /*
297 * Gather stats.
298 */
299 if (ISSET(lflag, ICANON)) {
300 ++tk_cancc;
301 ++tp->t_cancc;
302 } else {
303 ++tk_rawcc;
304 ++tp->t_rawcc;
305 }
306 ++tk_nin;
307
308 /*
309 * Block further input iff:
310 * current input > threshold AND input is available to user program
311 * AND input flow control is enabled and not yet invoked.
312 * The 3 is slop for PARMRK.
313 */
314 iflag = tp->t_iflag;
315 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 &&
316 (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) &&
317 (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) &&
318 !ISSET(tp->t_state, TS_TBLOCK))
319 ttyblock(tp);
320
321 /* Handle exceptional conditions (break, parity, framing). */
322 cc = tp->t_cc;
323 err = (ISSET(c, TTY_ERRORMASK));
324 if (err) {
325 CLR(c, TTY_ERRORMASK);
326 if (ISSET(err, TTY_BI)) {
327 if (ISSET(iflag, IGNBRK))
328 return (0);
329 if (ISSET(iflag, BRKINT)) {
330 ttyflush(tp, FREAD | FWRITE);
79#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
80#include <sys/ioctl_compat.h>
81#endif
82#include <sys/proc.h>
83#define TTYDEFCHARS
84#include <sys/tty.h>
85#undef TTYDEFCHARS
86#include <sys/fcntl.h>
87#include <sys/conf.h>
88#include <sys/dkstat.h>
89#include <sys/poll.h>
90#include <sys/kernel.h>
91#include <sys/vnode.h>
92#include <sys/signalvar.h>
93#include <sys/resourcevar.h>
94#include <sys/malloc.h>
95#include <sys/filedesc.h>
96#include <sys/sysctl.h>
97
98#include <vm/vm.h>
99#include <vm/pmap.h>
100#include <vm/vm_map.h>
101
102MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures");
103
104static int proc_compare __P((struct proc *p1, struct proc *p2));
105static int ttnread __P((struct tty *tp));
106static void ttyecho __P((int c, struct tty *tp));
107static int ttyoutput __P((int c, register struct tty *tp));
108static void ttypend __P((struct tty *tp));
109static void ttyretype __P((struct tty *tp));
110static void ttyrub __P((int c, struct tty *tp));
111static void ttyrubo __P((struct tty *tp, int cnt));
112static void ttyunblock __P((struct tty *tp));
113static int ttywflush __P((struct tty *tp));
114static int filt_ttyread __P((struct knote *kn, long hint));
115static void filt_ttyrdetach __P((struct knote *kn));
116static int filt_ttywrite __P((struct knote *kn, long hint));
117static void filt_ttywdetach __P((struct knote *kn));
118
119/*
120 * Table with character classes and parity. The 8th bit indicates parity,
121 * the 7th bit indicates the character is an alphameric or underscore (for
122 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
123 * are 0 then the character needs no special processing on output; classes
124 * other than 0 might be translated or (not currently) require delays.
125 */
126#define E 0x00 /* Even parity. */
127#define O 0x80 /* Odd parity. */
128#define PARITY(c) (char_type[c] & O)
129
130#define ALPHA 0x40 /* Alpha or underscore. */
131#define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
132
133#define CCLASSMASK 0x3f
134#define CCLASS(c) (char_type[c] & CCLASSMASK)
135
136#define BS BACKSPACE
137#define CC CONTROL
138#define CR RETURN
139#define NA ORDINARY | ALPHA
140#define NL NEWLINE
141#define NO ORDINARY
142#define TB TAB
143#define VT VTAB
144
145static u_char const char_type[] = {
146 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
147 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
148 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
149 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
150 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
151 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
152 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
153 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
154 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
155 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
156 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
157 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
158 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
159 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
160 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
161 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
162 /*
163 * Meta chars; should be settable per character set;
164 * for now, treat them all as normal characters.
165 */
166 NA, NA, NA, NA, NA, NA, NA, NA,
167 NA, NA, NA, NA, NA, NA, NA, NA,
168 NA, NA, NA, NA, NA, NA, NA, NA,
169 NA, NA, NA, NA, NA, NA, NA, NA,
170 NA, NA, NA, NA, NA, NA, NA, NA,
171 NA, NA, NA, NA, NA, NA, NA, NA,
172 NA, NA, NA, NA, NA, NA, NA, NA,
173 NA, NA, NA, NA, NA, NA, NA, NA,
174 NA, NA, NA, NA, NA, NA, NA, NA,
175 NA, NA, NA, NA, NA, NA, NA, NA,
176 NA, NA, NA, NA, NA, NA, NA, NA,
177 NA, NA, NA, NA, NA, NA, NA, NA,
178 NA, NA, NA, NA, NA, NA, NA, NA,
179 NA, NA, NA, NA, NA, NA, NA, NA,
180 NA, NA, NA, NA, NA, NA, NA, NA,
181 NA, NA, NA, NA, NA, NA, NA, NA,
182};
183#undef BS
184#undef CC
185#undef CR
186#undef NA
187#undef NL
188#undef NO
189#undef TB
190#undef VT
191
192/* Macros to clear/set/test flags. */
193#define SET(t, f) (t) |= (f)
194#define CLR(t, f) (t) &= ~(f)
195#define ISSET(t, f) ((t) & (f))
196
197#undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
198#define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */
199
200/*
201 * list of struct tty where pstat(8) can pick it up with sysctl
202 */
203static SLIST_HEAD(, tty) tty_list;
204
205static int drainwait = 5*60;
206SYSCTL_INT(_kern, OID_AUTO, drainwait, CTLFLAG_RW, &drainwait,
207 0, "Output drain timeout in seconds");
208
209/*
210 * Initial open of tty, or (re)entry to standard tty line discipline.
211 */
212int
213ttyopen(device, tp)
214 dev_t device;
215 register struct tty *tp;
216{
217 int s;
218
219 s = spltty();
220 tp->t_dev = device;
221 if (!ISSET(tp->t_state, TS_ISOPEN)) {
222 SET(tp->t_state, TS_ISOPEN);
223 if (ISSET(tp->t_cflag, CLOCAL))
224 SET(tp->t_state, TS_CONNECTED);
225 bzero(&tp->t_winsize, sizeof(tp->t_winsize));
226 }
227 /* XXX don't hang forever on output */
228 if (tp->t_timeout < 0)
229 tp->t_timeout = drainwait*hz;
230 ttsetwater(tp);
231 splx(s);
232 return (0);
233}
234
235/*
236 * Handle close() on a tty line: flush and set to initial state,
237 * bumping generation number so that pending read/write calls
238 * can detect recycling of the tty.
239 * XXX our caller should have done `spltty(); l_close(); ttyclose();'
240 * and l_close() should have flushed, but we repeat the spltty() and
241 * the flush in case there are buggy callers.
242 */
243int
244ttyclose(tp)
245 register struct tty *tp;
246{
247 int s;
248
249 funsetown(tp->t_sigio);
250 s = spltty();
251 if (constty == tp)
252 constty = NULL;
253
254 ttyflush(tp, FREAD | FWRITE);
255 clist_free_cblocks(&tp->t_canq);
256 clist_free_cblocks(&tp->t_outq);
257 clist_free_cblocks(&tp->t_rawq);
258
259 tp->t_gen++;
260 tp->t_line = TTYDISC;
261 tp->t_pgrp = NULL;
262 tp->t_session = NULL;
263 tp->t_state = 0;
264 splx(s);
265 return (0);
266}
267
268#define FLUSHQ(q) { \
269 if ((q)->c_cc) \
270 ndflush(q, (q)->c_cc); \
271}
272
273/* Is 'c' a line delimiter ("break" character)? */
274#define TTBREAKC(c, lflag) \
275 ((c) == '\n' || (((c) == cc[VEOF] || \
276 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
277 (c) != _POSIX_VDISABLE))
278
279/*
280 * Process input of a single character received on a tty.
281 */
282int
283ttyinput(c, tp)
284 register int c;
285 register struct tty *tp;
286{
287 register tcflag_t iflag, lflag;
288 register cc_t *cc;
289 int i, err;
290
291 /*
292 * If input is pending take it first.
293 */
294 lflag = tp->t_lflag;
295 if (ISSET(lflag, PENDIN))
296 ttypend(tp);
297 /*
298 * Gather stats.
299 */
300 if (ISSET(lflag, ICANON)) {
301 ++tk_cancc;
302 ++tp->t_cancc;
303 } else {
304 ++tk_rawcc;
305 ++tp->t_rawcc;
306 }
307 ++tk_nin;
308
309 /*
310 * Block further input iff:
311 * current input > threshold AND input is available to user program
312 * AND input flow control is enabled and not yet invoked.
313 * The 3 is slop for PARMRK.
314 */
315 iflag = tp->t_iflag;
316 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 &&
317 (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) &&
318 (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) &&
319 !ISSET(tp->t_state, TS_TBLOCK))
320 ttyblock(tp);
321
322 /* Handle exceptional conditions (break, parity, framing). */
323 cc = tp->t_cc;
324 err = (ISSET(c, TTY_ERRORMASK));
325 if (err) {
326 CLR(c, TTY_ERRORMASK);
327 if (ISSET(err, TTY_BI)) {
328 if (ISSET(iflag, IGNBRK))
329 return (0);
330 if (ISSET(iflag, BRKINT)) {
331 ttyflush(tp, FREAD | FWRITE);
331 pgsignal(tp->t_pgrp, SIGINT, 1);
332 if (tp->t_pgrp != NULL) {
333 PGRP_LOCK(tp->t_pgrp);
334 pgsignal(tp->t_pgrp, SIGINT, 1);
335 PGRP_UNLOCK(tp->t_pgrp);
336 }
332 goto endcase;
333 }
334 if (ISSET(iflag, PARMRK))
335 goto parmrk;
336 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK))
337 || ISSET(err, TTY_FE)) {
338 if (ISSET(iflag, IGNPAR))
339 return (0);
340 else if (ISSET(iflag, PARMRK)) {
341parmrk:
342 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >
343 MAX_INPUT - 3)
344 goto input_overflow;
345 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
346 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
347 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
348 goto endcase;
349 } else
350 c = 0;
351 }
352 }
353
354 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
355 CLR(c, 0x80);
356 if (!ISSET(lflag, EXTPROC)) {
357 /*
358 * Check for literal nexting very first
359 */
360 if (ISSET(tp->t_state, TS_LNCH)) {
361 SET(c, TTY_QUOTE);
362 CLR(tp->t_state, TS_LNCH);
363 }
364 /*
365 * Scan for special characters. This code
366 * is really just a big case statement with
367 * non-constant cases. The bottom of the
368 * case statement is labeled ``endcase'', so goto
369 * it after a case match, or similar.
370 */
371
372 /*
373 * Control chars which aren't controlled
374 * by ICANON, ISIG, or IXON.
375 */
376 if (ISSET(lflag, IEXTEN)) {
377 if (CCEQ(cc[VLNEXT], c)) {
378 if (ISSET(lflag, ECHO)) {
379 if (ISSET(lflag, ECHOE)) {
380 (void)ttyoutput('^', tp);
381 (void)ttyoutput('\b', tp);
382 } else
383 ttyecho(c, tp);
384 }
385 SET(tp->t_state, TS_LNCH);
386 goto endcase;
387 }
388 if (CCEQ(cc[VDISCARD], c)) {
389 if (ISSET(lflag, FLUSHO))
390 CLR(tp->t_lflag, FLUSHO);
391 else {
392 ttyflush(tp, FWRITE);
393 ttyecho(c, tp);
394 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
395 ttyretype(tp);
396 SET(tp->t_lflag, FLUSHO);
397 }
398 goto startoutput;
399 }
400 }
401 /*
402 * Signals.
403 */
404 if (ISSET(lflag, ISIG)) {
405 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
406 if (!ISSET(lflag, NOFLSH))
407 ttyflush(tp, FREAD | FWRITE);
408 ttyecho(c, tp);
337 goto endcase;
338 }
339 if (ISSET(iflag, PARMRK))
340 goto parmrk;
341 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK))
342 || ISSET(err, TTY_FE)) {
343 if (ISSET(iflag, IGNPAR))
344 return (0);
345 else if (ISSET(iflag, PARMRK)) {
346parmrk:
347 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >
348 MAX_INPUT - 3)
349 goto input_overflow;
350 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
351 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
352 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
353 goto endcase;
354 } else
355 c = 0;
356 }
357 }
358
359 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
360 CLR(c, 0x80);
361 if (!ISSET(lflag, EXTPROC)) {
362 /*
363 * Check for literal nexting very first
364 */
365 if (ISSET(tp->t_state, TS_LNCH)) {
366 SET(c, TTY_QUOTE);
367 CLR(tp->t_state, TS_LNCH);
368 }
369 /*
370 * Scan for special characters. This code
371 * is really just a big case statement with
372 * non-constant cases. The bottom of the
373 * case statement is labeled ``endcase'', so goto
374 * it after a case match, or similar.
375 */
376
377 /*
378 * Control chars which aren't controlled
379 * by ICANON, ISIG, or IXON.
380 */
381 if (ISSET(lflag, IEXTEN)) {
382 if (CCEQ(cc[VLNEXT], c)) {
383 if (ISSET(lflag, ECHO)) {
384 if (ISSET(lflag, ECHOE)) {
385 (void)ttyoutput('^', tp);
386 (void)ttyoutput('\b', tp);
387 } else
388 ttyecho(c, tp);
389 }
390 SET(tp->t_state, TS_LNCH);
391 goto endcase;
392 }
393 if (CCEQ(cc[VDISCARD], c)) {
394 if (ISSET(lflag, FLUSHO))
395 CLR(tp->t_lflag, FLUSHO);
396 else {
397 ttyflush(tp, FWRITE);
398 ttyecho(c, tp);
399 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
400 ttyretype(tp);
401 SET(tp->t_lflag, FLUSHO);
402 }
403 goto startoutput;
404 }
405 }
406 /*
407 * Signals.
408 */
409 if (ISSET(lflag, ISIG)) {
410 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
411 if (!ISSET(lflag, NOFLSH))
412 ttyflush(tp, FREAD | FWRITE);
413 ttyecho(c, tp);
409 pgsignal(tp->t_pgrp,
410 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
414 if (tp->t_pgrp != NULL) {
415 PGRP_LOCK(tp->t_pgrp);
416 pgsignal(tp->t_pgrp,
417 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
418 PGRP_UNLOCK(tp->t_pgrp);
419 }
411 goto endcase;
412 }
413 if (CCEQ(cc[VSUSP], c)) {
414 if (!ISSET(lflag, NOFLSH))
415 ttyflush(tp, FREAD);
416 ttyecho(c, tp);
420 goto endcase;
421 }
422 if (CCEQ(cc[VSUSP], c)) {
423 if (!ISSET(lflag, NOFLSH))
424 ttyflush(tp, FREAD);
425 ttyecho(c, tp);
417 pgsignal(tp->t_pgrp, SIGTSTP, 1);
426 if (tp->t_pgrp != NULL) {
427 PGRP_LOCK(tp->t_pgrp);
428 pgsignal(tp->t_pgrp, SIGTSTP, 1);
429 PGRP_UNLOCK(tp->t_pgrp);
430 }
418 goto endcase;
419 }
420 }
421 /*
422 * Handle start/stop characters.
423 */
424 if (ISSET(iflag, IXON)) {
425 if (CCEQ(cc[VSTOP], c)) {
426 if (!ISSET(tp->t_state, TS_TTSTOP)) {
427 SET(tp->t_state, TS_TTSTOP);
428 (*tp->t_stop)(tp, 0);
429 return (0);
430 }
431 if (!CCEQ(cc[VSTART], c))
432 return (0);
433 /*
434 * if VSTART == VSTOP then toggle
435 */
436 goto endcase;
437 }
438 if (CCEQ(cc[VSTART], c))
439 goto restartoutput;
440 }
441 /*
442 * IGNCR, ICRNL, & INLCR
443 */
444 if (c == '\r') {
445 if (ISSET(iflag, IGNCR))
446 return (0);
447 else if (ISSET(iflag, ICRNL))
448 c = '\n';
449 } else if (c == '\n' && ISSET(iflag, INLCR))
450 c = '\r';
451 }
452 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
453 /*
454 * From here on down canonical mode character
455 * processing takes place.
456 */
457 /*
458 * erase or erase2 (^H / ^?)
459 */
460 if (CCEQ(cc[VERASE], c) || CCEQ(cc[VERASE2], c) ) {
461 if (tp->t_rawq.c_cc)
462 ttyrub(unputc(&tp->t_rawq), tp);
463 goto endcase;
464 }
465 /*
466 * kill (^U)
467 */
468 if (CCEQ(cc[VKILL], c)) {
469 if (ISSET(lflag, ECHOKE) &&
470 tp->t_rawq.c_cc == tp->t_rocount &&
471 !ISSET(lflag, ECHOPRT))
472 while (tp->t_rawq.c_cc)
473 ttyrub(unputc(&tp->t_rawq), tp);
474 else {
475 ttyecho(c, tp);
476 if (ISSET(lflag, ECHOK) ||
477 ISSET(lflag, ECHOKE))
478 ttyecho('\n', tp);
479 FLUSHQ(&tp->t_rawq);
480 tp->t_rocount = 0;
481 }
482 CLR(tp->t_state, TS_LOCAL);
483 goto endcase;
484 }
485 /*
486 * word erase (^W)
487 */
488 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) {
489 int ctype;
490
491 /*
492 * erase whitespace
493 */
494 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
495 ttyrub(c, tp);
496 if (c == -1)
497 goto endcase;
498 /*
499 * erase last char of word and remember the
500 * next chars type (for ALTWERASE)
501 */
502 ttyrub(c, tp);
503 c = unputc(&tp->t_rawq);
504 if (c == -1)
505 goto endcase;
506 if (c == ' ' || c == '\t') {
507 (void)putc(c, &tp->t_rawq);
508 goto endcase;
509 }
510 ctype = ISALPHA(c);
511 /*
512 * erase rest of word
513 */
514 do {
515 ttyrub(c, tp);
516 c = unputc(&tp->t_rawq);
517 if (c == -1)
518 goto endcase;
519 } while (c != ' ' && c != '\t' &&
520 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype));
521 (void)putc(c, &tp->t_rawq);
522 goto endcase;
523 }
524 /*
525 * reprint line (^R)
526 */
527 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) {
528 ttyretype(tp);
529 goto endcase;
530 }
531 /*
532 * ^T - kernel info and generate SIGINFO
533 */
534 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) {
431 goto endcase;
432 }
433 }
434 /*
435 * Handle start/stop characters.
436 */
437 if (ISSET(iflag, IXON)) {
438 if (CCEQ(cc[VSTOP], c)) {
439 if (!ISSET(tp->t_state, TS_TTSTOP)) {
440 SET(tp->t_state, TS_TTSTOP);
441 (*tp->t_stop)(tp, 0);
442 return (0);
443 }
444 if (!CCEQ(cc[VSTART], c))
445 return (0);
446 /*
447 * if VSTART == VSTOP then toggle
448 */
449 goto endcase;
450 }
451 if (CCEQ(cc[VSTART], c))
452 goto restartoutput;
453 }
454 /*
455 * IGNCR, ICRNL, & INLCR
456 */
457 if (c == '\r') {
458 if (ISSET(iflag, IGNCR))
459 return (0);
460 else if (ISSET(iflag, ICRNL))
461 c = '\n';
462 } else if (c == '\n' && ISSET(iflag, INLCR))
463 c = '\r';
464 }
465 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
466 /*
467 * From here on down canonical mode character
468 * processing takes place.
469 */
470 /*
471 * erase or erase2 (^H / ^?)
472 */
473 if (CCEQ(cc[VERASE], c) || CCEQ(cc[VERASE2], c) ) {
474 if (tp->t_rawq.c_cc)
475 ttyrub(unputc(&tp->t_rawq), tp);
476 goto endcase;
477 }
478 /*
479 * kill (^U)
480 */
481 if (CCEQ(cc[VKILL], c)) {
482 if (ISSET(lflag, ECHOKE) &&
483 tp->t_rawq.c_cc == tp->t_rocount &&
484 !ISSET(lflag, ECHOPRT))
485 while (tp->t_rawq.c_cc)
486 ttyrub(unputc(&tp->t_rawq), tp);
487 else {
488 ttyecho(c, tp);
489 if (ISSET(lflag, ECHOK) ||
490 ISSET(lflag, ECHOKE))
491 ttyecho('\n', tp);
492 FLUSHQ(&tp->t_rawq);
493 tp->t_rocount = 0;
494 }
495 CLR(tp->t_state, TS_LOCAL);
496 goto endcase;
497 }
498 /*
499 * word erase (^W)
500 */
501 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) {
502 int ctype;
503
504 /*
505 * erase whitespace
506 */
507 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
508 ttyrub(c, tp);
509 if (c == -1)
510 goto endcase;
511 /*
512 * erase last char of word and remember the
513 * next chars type (for ALTWERASE)
514 */
515 ttyrub(c, tp);
516 c = unputc(&tp->t_rawq);
517 if (c == -1)
518 goto endcase;
519 if (c == ' ' || c == '\t') {
520 (void)putc(c, &tp->t_rawq);
521 goto endcase;
522 }
523 ctype = ISALPHA(c);
524 /*
525 * erase rest of word
526 */
527 do {
528 ttyrub(c, tp);
529 c = unputc(&tp->t_rawq);
530 if (c == -1)
531 goto endcase;
532 } while (c != ' ' && c != '\t' &&
533 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype));
534 (void)putc(c, &tp->t_rawq);
535 goto endcase;
536 }
537 /*
538 * reprint line (^R)
539 */
540 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) {
541 ttyretype(tp);
542 goto endcase;
543 }
544 /*
545 * ^T - kernel info and generate SIGINFO
546 */
547 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) {
535 if (ISSET(lflag, ISIG))
548 if (ISSET(lflag, ISIG) && tp->t_pgrp != NULL) {
549 PGRP_LOCK(tp->t_pgrp);
536 pgsignal(tp->t_pgrp, SIGINFO, 1);
550 pgsignal(tp->t_pgrp, SIGINFO, 1);
551 PGRP_UNLOCK(tp->t_pgrp);
552 }
537 if (!ISSET(lflag, NOKERNINFO))
538 ttyinfo(tp);
539 goto endcase;
540 }
541 }
542 /*
543 * Check for input buffer overflow
544 */
545 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) {
546input_overflow:
547 if (ISSET(iflag, IMAXBEL)) {
548 if (tp->t_outq.c_cc < tp->t_ohiwat)
549 (void)ttyoutput(CTRL('g'), tp);
550 }
551 goto endcase;
552 }
553
554 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP)
555 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR))
556 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
557
558 /*
559 * Put data char in q for user and
560 * wakeup on seeing a line delimiter.
561 */
562 if (putc(c, &tp->t_rawq) >= 0) {
563 if (!ISSET(lflag, ICANON)) {
564 ttwakeup(tp);
565 ttyecho(c, tp);
566 goto endcase;
567 }
568 if (TTBREAKC(c, lflag)) {
569 tp->t_rocount = 0;
570 catq(&tp->t_rawq, &tp->t_canq);
571 ttwakeup(tp);
572 } else if (tp->t_rocount++ == 0)
573 tp->t_rocol = tp->t_column;
574 if (ISSET(tp->t_state, TS_ERASE)) {
575 /*
576 * end of prterase \.../
577 */
578 CLR(tp->t_state, TS_ERASE);
579 (void)ttyoutput('/', tp);
580 }
581 i = tp->t_column;
582 ttyecho(c, tp);
583 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
584 /*
585 * Place the cursor over the '^' of the ^D.
586 */
587 i = imin(2, tp->t_column - i);
588 while (i > 0) {
589 (void)ttyoutput('\b', tp);
590 i--;
591 }
592 }
593 }
594endcase:
595 /*
596 * IXANY means allow any character to restart output.
597 */
598 if (ISSET(tp->t_state, TS_TTSTOP) &&
599 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
600 return (0);
601restartoutput:
602 CLR(tp->t_lflag, FLUSHO);
603 CLR(tp->t_state, TS_TTSTOP);
604startoutput:
605 return (ttstart(tp));
606}
607
608/*
609 * Output a single character on a tty, doing output processing
610 * as needed (expanding tabs, newline processing, etc.).
611 * Returns < 0 if succeeds, otherwise returns char to resend.
612 * Must be recursive.
613 */
614static int
615ttyoutput(c, tp)
616 register int c;
617 register struct tty *tp;
618{
619 register tcflag_t oflag;
620 register int col, s;
621
622 oflag = tp->t_oflag;
623 if (!ISSET(oflag, OPOST)) {
624 if (ISSET(tp->t_lflag, FLUSHO))
625 return (-1);
626 if (putc(c, &tp->t_outq))
627 return (c);
628 tk_nout++;
629 tp->t_outcc++;
630 return (-1);
631 }
632 /*
633 * Do tab expansion if OXTABS is set. Special case if we external
634 * processing, we don't do the tab expansion because we'll probably
635 * get it wrong. If tab expansion needs to be done, let it happen
636 * externally.
637 */
638 CLR(c, ~TTY_CHARMASK);
639 if (c == '\t' &&
640 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
641 c = 8 - (tp->t_column & 7);
642 if (!ISSET(tp->t_lflag, FLUSHO)) {
643 s = spltty(); /* Don't interrupt tabs. */
644 c -= b_to_q(" ", c, &tp->t_outq);
645 tk_nout += c;
646 tp->t_outcc += c;
647 splx(s);
648 }
649 tp->t_column += c;
650 return (c ? -1 : '\t');
651 }
652 if (c == CEOT && ISSET(oflag, ONOEOT))
653 return (-1);
654
655 /*
656 * Newline translation: if ONLCR is set,
657 * translate newline into "\r\n".
658 */
659 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
660 tk_nout++;
661 tp->t_outcc++;
662 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
663 return (c);
664 }
665 /* If OCRNL is set, translate "\r" into "\n". */
666 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
667 c = '\n';
668 /* If ONOCR is set, don't transmit CRs when on column 0. */
669 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
670 return (-1);
671
672 tk_nout++;
673 tp->t_outcc++;
674 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
675 return (c);
676
677 col = tp->t_column;
678 switch (CCLASS(c)) {
679 case BACKSPACE:
680 if (col > 0)
681 --col;
682 break;
683 case CONTROL:
684 break;
685 case NEWLINE:
686 if (ISSET(tp->t_oflag, ONLCR | ONLRET))
687 col = 0;
688 break;
689 case RETURN:
690 col = 0;
691 break;
692 case ORDINARY:
693 ++col;
694 break;
695 case TAB:
696 col = (col + 8) & ~7;
697 break;
698 }
699 tp->t_column = col;
700 return (-1);
701}
702
703/*
704 * Ioctls for all tty devices. Called after line-discipline specific ioctl
705 * has been called to do discipline-specific functions and/or reject any
706 * of these ioctl commands.
707 */
708/* ARGSUSED */
709int
710ttioctl(tp, cmd, data, flag)
711 register struct tty *tp;
712 u_long cmd;
713 int flag;
714 void *data;
715{
716 register struct proc *p;
717 int s, error;
718
719 p = curproc; /* XXX */
720
721 /* If the ioctl involves modification, hang if in the background. */
722 switch (cmd) {
723 case TIOCCBRK:
724 case TIOCCONS:
725 case TIOCDRAIN:
726 case TIOCEXCL:
727 case TIOCFLUSH:
728#ifdef TIOCHPCL
729 case TIOCHPCL:
730#endif
731 case TIOCNXCL:
732 case TIOCSBRK:
733 case TIOCSCTTY:
734 case TIOCSDRAINWAIT:
735 case TIOCSETA:
736 case TIOCSETAF:
737 case TIOCSETAW:
738 case TIOCSETD:
739 case TIOCSPGRP:
740 case TIOCSTART:
741 case TIOCSTAT:
742 case TIOCSTI:
743 case TIOCSTOP:
744 case TIOCSWINSZ:
745#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
746 case TIOCLBIC:
747 case TIOCLBIS:
748 case TIOCLSET:
749 case TIOCSETC:
750 case OTIOCSETD:
751 case TIOCSETN:
752 case TIOCSETP:
753 case TIOCSLTC:
754#endif
553 if (!ISSET(lflag, NOKERNINFO))
554 ttyinfo(tp);
555 goto endcase;
556 }
557 }
558 /*
559 * Check for input buffer overflow
560 */
561 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) {
562input_overflow:
563 if (ISSET(iflag, IMAXBEL)) {
564 if (tp->t_outq.c_cc < tp->t_ohiwat)
565 (void)ttyoutput(CTRL('g'), tp);
566 }
567 goto endcase;
568 }
569
570 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP)
571 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR))
572 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
573
574 /*
575 * Put data char in q for user and
576 * wakeup on seeing a line delimiter.
577 */
578 if (putc(c, &tp->t_rawq) >= 0) {
579 if (!ISSET(lflag, ICANON)) {
580 ttwakeup(tp);
581 ttyecho(c, tp);
582 goto endcase;
583 }
584 if (TTBREAKC(c, lflag)) {
585 tp->t_rocount = 0;
586 catq(&tp->t_rawq, &tp->t_canq);
587 ttwakeup(tp);
588 } else if (tp->t_rocount++ == 0)
589 tp->t_rocol = tp->t_column;
590 if (ISSET(tp->t_state, TS_ERASE)) {
591 /*
592 * end of prterase \.../
593 */
594 CLR(tp->t_state, TS_ERASE);
595 (void)ttyoutput('/', tp);
596 }
597 i = tp->t_column;
598 ttyecho(c, tp);
599 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
600 /*
601 * Place the cursor over the '^' of the ^D.
602 */
603 i = imin(2, tp->t_column - i);
604 while (i > 0) {
605 (void)ttyoutput('\b', tp);
606 i--;
607 }
608 }
609 }
610endcase:
611 /*
612 * IXANY means allow any character to restart output.
613 */
614 if (ISSET(tp->t_state, TS_TTSTOP) &&
615 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
616 return (0);
617restartoutput:
618 CLR(tp->t_lflag, FLUSHO);
619 CLR(tp->t_state, TS_TTSTOP);
620startoutput:
621 return (ttstart(tp));
622}
623
624/*
625 * Output a single character on a tty, doing output processing
626 * as needed (expanding tabs, newline processing, etc.).
627 * Returns < 0 if succeeds, otherwise returns char to resend.
628 * Must be recursive.
629 */
630static int
631ttyoutput(c, tp)
632 register int c;
633 register struct tty *tp;
634{
635 register tcflag_t oflag;
636 register int col, s;
637
638 oflag = tp->t_oflag;
639 if (!ISSET(oflag, OPOST)) {
640 if (ISSET(tp->t_lflag, FLUSHO))
641 return (-1);
642 if (putc(c, &tp->t_outq))
643 return (c);
644 tk_nout++;
645 tp->t_outcc++;
646 return (-1);
647 }
648 /*
649 * Do tab expansion if OXTABS is set. Special case if we external
650 * processing, we don't do the tab expansion because we'll probably
651 * get it wrong. If tab expansion needs to be done, let it happen
652 * externally.
653 */
654 CLR(c, ~TTY_CHARMASK);
655 if (c == '\t' &&
656 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
657 c = 8 - (tp->t_column & 7);
658 if (!ISSET(tp->t_lflag, FLUSHO)) {
659 s = spltty(); /* Don't interrupt tabs. */
660 c -= b_to_q(" ", c, &tp->t_outq);
661 tk_nout += c;
662 tp->t_outcc += c;
663 splx(s);
664 }
665 tp->t_column += c;
666 return (c ? -1 : '\t');
667 }
668 if (c == CEOT && ISSET(oflag, ONOEOT))
669 return (-1);
670
671 /*
672 * Newline translation: if ONLCR is set,
673 * translate newline into "\r\n".
674 */
675 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
676 tk_nout++;
677 tp->t_outcc++;
678 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
679 return (c);
680 }
681 /* If OCRNL is set, translate "\r" into "\n". */
682 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
683 c = '\n';
684 /* If ONOCR is set, don't transmit CRs when on column 0. */
685 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
686 return (-1);
687
688 tk_nout++;
689 tp->t_outcc++;
690 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
691 return (c);
692
693 col = tp->t_column;
694 switch (CCLASS(c)) {
695 case BACKSPACE:
696 if (col > 0)
697 --col;
698 break;
699 case CONTROL:
700 break;
701 case NEWLINE:
702 if (ISSET(tp->t_oflag, ONLCR | ONLRET))
703 col = 0;
704 break;
705 case RETURN:
706 col = 0;
707 break;
708 case ORDINARY:
709 ++col;
710 break;
711 case TAB:
712 col = (col + 8) & ~7;
713 break;
714 }
715 tp->t_column = col;
716 return (-1);
717}
718
719/*
720 * Ioctls for all tty devices. Called after line-discipline specific ioctl
721 * has been called to do discipline-specific functions and/or reject any
722 * of these ioctl commands.
723 */
724/* ARGSUSED */
725int
726ttioctl(tp, cmd, data, flag)
727 register struct tty *tp;
728 u_long cmd;
729 int flag;
730 void *data;
731{
732 register struct proc *p;
733 int s, error;
734
735 p = curproc; /* XXX */
736
737 /* If the ioctl involves modification, hang if in the background. */
738 switch (cmd) {
739 case TIOCCBRK:
740 case TIOCCONS:
741 case TIOCDRAIN:
742 case TIOCEXCL:
743 case TIOCFLUSH:
744#ifdef TIOCHPCL
745 case TIOCHPCL:
746#endif
747 case TIOCNXCL:
748 case TIOCSBRK:
749 case TIOCSCTTY:
750 case TIOCSDRAINWAIT:
751 case TIOCSETA:
752 case TIOCSETAF:
753 case TIOCSETAW:
754 case TIOCSETD:
755 case TIOCSPGRP:
756 case TIOCSTART:
757 case TIOCSTAT:
758 case TIOCSTI:
759 case TIOCSTOP:
760 case TIOCSWINSZ:
761#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
762 case TIOCLBIC:
763 case TIOCLBIS:
764 case TIOCLSET:
765 case TIOCSETC:
766 case OTIOCSETD:
767 case TIOCSETN:
768 case TIOCSETP:
769 case TIOCSLTC:
770#endif
771 PGRPSESS_SLOCK();
772 PROC_LOCK(p);
755 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) &&
756 !SIGISMEMBER(p->p_sigignore, SIGTTOU) &&
757 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) {
773 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) &&
774 !SIGISMEMBER(p->p_sigignore, SIGTTOU) &&
775 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) {
758 if (p->p_pgrp->pg_jobc == 0)
776 if (p->p_pgrp->pg_jobc == 0) {
777 PROC_UNLOCK(p);
778 PGRPSESS_SUNLOCK();
759 return (EIO);
779 return (EIO);
780 }
781 PROC_UNLOCK(p);
782 PGRP_LOCK(p->p_pgrp);
760 pgsignal(p->p_pgrp, SIGTTOU, 1);
783 pgsignal(p->p_pgrp, SIGTTOU, 1);
784 PGRP_UNLOCK(p->p_pgrp);
761 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1",
762 0);
785 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1",
786 0);
763 if (error)
787 if (error) {
788 PGRPSESS_SUNLOCK();
764 return (error);
789 return (error);
790 }
791 PROC_LOCK(p);
765 }
792 }
793 PROC_UNLOCK(p);
794 PGRPSESS_SUNLOCK();
766 break;
767 }
768
769 switch (cmd) { /* Process the ioctl. */
770 case FIOASYNC: /* set/clear async i/o */
771 s = spltty();
772 if (*(int *)data)
773 SET(tp->t_state, TS_ASYNC);
774 else
775 CLR(tp->t_state, TS_ASYNC);
776 splx(s);
777 break;
778 case FIONBIO: /* set/clear non-blocking i/o */
779 break; /* XXX: delete. */
780 case FIONREAD: /* get # bytes to read */
781 s = spltty();
782 *(int *)data = ttnread(tp);
783 splx(s);
784 break;
785
786 case FIOSETOWN:
787 /*
788 * Policy -- Don't allow FIOSETOWN on someone else's
789 * controlling tty
790 */
791 if (tp->t_session != NULL && !isctty(p, tp))
792 return (ENOTTY);
793
794 error = fsetown(*(int *)data, &tp->t_sigio);
795 if (error)
796 return (error);
797 break;
798 case FIOGETOWN:
799 if (tp->t_session != NULL && !isctty(p, tp))
800 return (ENOTTY);
801 *(int *)data = fgetown(tp->t_sigio);
802 break;
803
804 case TIOCEXCL: /* set exclusive use of tty */
805 s = spltty();
806 SET(tp->t_state, TS_XCLUDE);
807 splx(s);
808 break;
809 case TIOCFLUSH: { /* flush buffers */
810 register int flags = *(int *)data;
811
812 if (flags == 0)
813 flags = FREAD | FWRITE;
814 else
815 flags &= FREAD | FWRITE;
816 ttyflush(tp, flags);
817 break;
818 }
819 case TIOCCONS: /* become virtual console */
820 if (*(int *)data) {
821 if (constty && constty != tp &&
822 ISSET(constty->t_state, TS_CONNECTED))
823 return (EBUSY);
824#ifndef UCONSOLE
825 if ((error = suser_xxx(p->p_ucred, NULL, 0)) != 0)
826 return (error);
827#endif
828 constty = tp;
829 } else if (tp == constty)
830 constty = NULL;
831 break;
832 case TIOCDRAIN: /* wait till output drained */
833 error = ttywait(tp);
834 if (error)
835 return (error);
836 break;
837 case TIOCGETA: { /* get termios struct */
838 struct termios *t = (struct termios *)data;
839
840 bcopy(&tp->t_termios, t, sizeof(struct termios));
841 break;
842 }
843 case TIOCGETD: /* get line discipline */
844 *(int *)data = tp->t_line;
845 break;
846 case TIOCGWINSZ: /* get window size */
847 *(struct winsize *)data = tp->t_winsize;
848 break;
849 case TIOCGPGRP: /* get pgrp of tty */
850 if (!isctty(p, tp))
851 return (ENOTTY);
852 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
853 break;
854#ifdef TIOCHPCL
855 case TIOCHPCL: /* hang up on last close */
856 s = spltty();
857 SET(tp->t_cflag, HUPCL);
858 splx(s);
859 break;
860#endif
861 case TIOCNXCL: /* reset exclusive use of tty */
862 s = spltty();
863 CLR(tp->t_state, TS_XCLUDE);
864 splx(s);
865 break;
866 case TIOCOUTQ: /* output queue size */
867 *(int *)data = tp->t_outq.c_cc;
868 break;
869 case TIOCSETA: /* set termios struct */
870 case TIOCSETAW: /* drain output, set */
871 case TIOCSETAF: { /* drn out, fls in, set */
872 register struct termios *t = (struct termios *)data;
873
874 if (t->c_ispeed == 0)
875 t->c_ispeed = t->c_ospeed;
876 if (t->c_ispeed == 0)
877 t->c_ispeed = tp->t_ospeed;
878 if (t->c_ispeed == 0)
879 return (EINVAL);
880 s = spltty();
881 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
882 error = ttywait(tp);
883 if (error) {
884 splx(s);
885 return (error);
886 }
887 if (cmd == TIOCSETAF)
888 ttyflush(tp, FREAD);
889 }
890 if (!ISSET(t->c_cflag, CIGNORE)) {
891 /*
892 * Set device hardware.
893 */
894 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
895 splx(s);
896 return (error);
897 }
898 if (ISSET(t->c_cflag, CLOCAL) &&
899 !ISSET(tp->t_cflag, CLOCAL)) {
900 /*
901 * XXX disconnections would be too hard to
902 * get rid of without this kludge. The only
903 * way to get rid of controlling terminals
904 * is to exit from the session leader.
905 */
906 CLR(tp->t_state, TS_ZOMBIE);
907
908 wakeup(TSA_CARR_ON(tp));
909 ttwakeup(tp);
910 ttwwakeup(tp);
911 }
912 if ((ISSET(tp->t_state, TS_CARR_ON) ||
913 ISSET(t->c_cflag, CLOCAL)) &&
914 !ISSET(tp->t_state, TS_ZOMBIE))
915 SET(tp->t_state, TS_CONNECTED);
916 else
917 CLR(tp->t_state, TS_CONNECTED);
918 tp->t_cflag = t->c_cflag;
919 tp->t_ispeed = t->c_ispeed;
920 if (t->c_ospeed != 0)
921 tp->t_ospeed = t->c_ospeed;
922 ttsetwater(tp);
923 }
924 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) &&
925 cmd != TIOCSETAF) {
926 if (ISSET(t->c_lflag, ICANON))
927 SET(tp->t_lflag, PENDIN);
928 else {
929 /*
930 * XXX we really shouldn't allow toggling
931 * ICANON while we're in a non-termios line
932 * discipline. Now we have to worry about
933 * panicing for a null queue.
934 */
935 if (tp->t_canq.c_cbreserved > 0 &&
936 tp->t_rawq.c_cbreserved > 0) {
937 catq(&tp->t_rawq, &tp->t_canq);
938 /*
939 * XXX the queue limits may be
940 * different, so the old queue
941 * swapping method no longer works.
942 */
943 catq(&tp->t_canq, &tp->t_rawq);
944 }
945 CLR(tp->t_lflag, PENDIN);
946 }
947 ttwakeup(tp);
948 }
949 tp->t_iflag = t->c_iflag;
950 tp->t_oflag = t->c_oflag;
951 /*
952 * Make the EXTPROC bit read only.
953 */
954 if (ISSET(tp->t_lflag, EXTPROC))
955 SET(t->c_lflag, EXTPROC);
956 else
957 CLR(t->c_lflag, EXTPROC);
958 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
959 if (t->c_cc[VMIN] != tp->t_cc[VMIN] ||
960 t->c_cc[VTIME] != tp->t_cc[VTIME])
961 ttwakeup(tp);
962 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
963 splx(s);
964 break;
965 }
966 case TIOCSETD: { /* set line discipline */
967 register int t = *(int *)data;
968 dev_t device = tp->t_dev;
969
970 if ((u_int)t >= nlinesw)
971 return (ENXIO);
972 if (t != tp->t_line) {
973 s = spltty();
974 (*linesw[tp->t_line].l_close)(tp, flag);
975 error = (*linesw[t].l_open)(device, tp);
976 if (error) {
977 (void)(*linesw[tp->t_line].l_open)(device, tp);
978 splx(s);
979 return (error);
980 }
981 tp->t_line = t;
982 splx(s);
983 }
984 break;
985 }
986 case TIOCSTART: /* start output, like ^Q */
987 s = spltty();
988 if (ISSET(tp->t_state, TS_TTSTOP) ||
989 ISSET(tp->t_lflag, FLUSHO)) {
990 CLR(tp->t_lflag, FLUSHO);
991 CLR(tp->t_state, TS_TTSTOP);
992 ttstart(tp);
993 }
994 splx(s);
995 break;
996 case TIOCSTI: /* simulate terminal input */
997 if ((flag & FREAD) == 0 && suser_xxx(p->p_ucred, NULL, 0))
998 return (EPERM);
999 if (!isctty(p, tp) && suser_xxx(p->p_ucred, NULL, 0))
1000 return (EACCES);
1001 s = spltty();
1002 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
1003 splx(s);
1004 break;
1005 case TIOCSTOP: /* stop output, like ^S */
1006 s = spltty();
1007 if (!ISSET(tp->t_state, TS_TTSTOP)) {
1008 SET(tp->t_state, TS_TTSTOP);
1009 (*tp->t_stop)(tp, 0);
1010 }
1011 splx(s);
1012 break;
1013 case TIOCSCTTY: /* become controlling tty */
1014 /* Session ctty vnode pointer set in vnode layer. */
795 break;
796 }
797
798 switch (cmd) { /* Process the ioctl. */
799 case FIOASYNC: /* set/clear async i/o */
800 s = spltty();
801 if (*(int *)data)
802 SET(tp->t_state, TS_ASYNC);
803 else
804 CLR(tp->t_state, TS_ASYNC);
805 splx(s);
806 break;
807 case FIONBIO: /* set/clear non-blocking i/o */
808 break; /* XXX: delete. */
809 case FIONREAD: /* get # bytes to read */
810 s = spltty();
811 *(int *)data = ttnread(tp);
812 splx(s);
813 break;
814
815 case FIOSETOWN:
816 /*
817 * Policy -- Don't allow FIOSETOWN on someone else's
818 * controlling tty
819 */
820 if (tp->t_session != NULL && !isctty(p, tp))
821 return (ENOTTY);
822
823 error = fsetown(*(int *)data, &tp->t_sigio);
824 if (error)
825 return (error);
826 break;
827 case FIOGETOWN:
828 if (tp->t_session != NULL && !isctty(p, tp))
829 return (ENOTTY);
830 *(int *)data = fgetown(tp->t_sigio);
831 break;
832
833 case TIOCEXCL: /* set exclusive use of tty */
834 s = spltty();
835 SET(tp->t_state, TS_XCLUDE);
836 splx(s);
837 break;
838 case TIOCFLUSH: { /* flush buffers */
839 register int flags = *(int *)data;
840
841 if (flags == 0)
842 flags = FREAD | FWRITE;
843 else
844 flags &= FREAD | FWRITE;
845 ttyflush(tp, flags);
846 break;
847 }
848 case TIOCCONS: /* become virtual console */
849 if (*(int *)data) {
850 if (constty && constty != tp &&
851 ISSET(constty->t_state, TS_CONNECTED))
852 return (EBUSY);
853#ifndef UCONSOLE
854 if ((error = suser_xxx(p->p_ucred, NULL, 0)) != 0)
855 return (error);
856#endif
857 constty = tp;
858 } else if (tp == constty)
859 constty = NULL;
860 break;
861 case TIOCDRAIN: /* wait till output drained */
862 error = ttywait(tp);
863 if (error)
864 return (error);
865 break;
866 case TIOCGETA: { /* get termios struct */
867 struct termios *t = (struct termios *)data;
868
869 bcopy(&tp->t_termios, t, sizeof(struct termios));
870 break;
871 }
872 case TIOCGETD: /* get line discipline */
873 *(int *)data = tp->t_line;
874 break;
875 case TIOCGWINSZ: /* get window size */
876 *(struct winsize *)data = tp->t_winsize;
877 break;
878 case TIOCGPGRP: /* get pgrp of tty */
879 if (!isctty(p, tp))
880 return (ENOTTY);
881 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
882 break;
883#ifdef TIOCHPCL
884 case TIOCHPCL: /* hang up on last close */
885 s = spltty();
886 SET(tp->t_cflag, HUPCL);
887 splx(s);
888 break;
889#endif
890 case TIOCNXCL: /* reset exclusive use of tty */
891 s = spltty();
892 CLR(tp->t_state, TS_XCLUDE);
893 splx(s);
894 break;
895 case TIOCOUTQ: /* output queue size */
896 *(int *)data = tp->t_outq.c_cc;
897 break;
898 case TIOCSETA: /* set termios struct */
899 case TIOCSETAW: /* drain output, set */
900 case TIOCSETAF: { /* drn out, fls in, set */
901 register struct termios *t = (struct termios *)data;
902
903 if (t->c_ispeed == 0)
904 t->c_ispeed = t->c_ospeed;
905 if (t->c_ispeed == 0)
906 t->c_ispeed = tp->t_ospeed;
907 if (t->c_ispeed == 0)
908 return (EINVAL);
909 s = spltty();
910 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
911 error = ttywait(tp);
912 if (error) {
913 splx(s);
914 return (error);
915 }
916 if (cmd == TIOCSETAF)
917 ttyflush(tp, FREAD);
918 }
919 if (!ISSET(t->c_cflag, CIGNORE)) {
920 /*
921 * Set device hardware.
922 */
923 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
924 splx(s);
925 return (error);
926 }
927 if (ISSET(t->c_cflag, CLOCAL) &&
928 !ISSET(tp->t_cflag, CLOCAL)) {
929 /*
930 * XXX disconnections would be too hard to
931 * get rid of without this kludge. The only
932 * way to get rid of controlling terminals
933 * is to exit from the session leader.
934 */
935 CLR(tp->t_state, TS_ZOMBIE);
936
937 wakeup(TSA_CARR_ON(tp));
938 ttwakeup(tp);
939 ttwwakeup(tp);
940 }
941 if ((ISSET(tp->t_state, TS_CARR_ON) ||
942 ISSET(t->c_cflag, CLOCAL)) &&
943 !ISSET(tp->t_state, TS_ZOMBIE))
944 SET(tp->t_state, TS_CONNECTED);
945 else
946 CLR(tp->t_state, TS_CONNECTED);
947 tp->t_cflag = t->c_cflag;
948 tp->t_ispeed = t->c_ispeed;
949 if (t->c_ospeed != 0)
950 tp->t_ospeed = t->c_ospeed;
951 ttsetwater(tp);
952 }
953 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) &&
954 cmd != TIOCSETAF) {
955 if (ISSET(t->c_lflag, ICANON))
956 SET(tp->t_lflag, PENDIN);
957 else {
958 /*
959 * XXX we really shouldn't allow toggling
960 * ICANON while we're in a non-termios line
961 * discipline. Now we have to worry about
962 * panicing for a null queue.
963 */
964 if (tp->t_canq.c_cbreserved > 0 &&
965 tp->t_rawq.c_cbreserved > 0) {
966 catq(&tp->t_rawq, &tp->t_canq);
967 /*
968 * XXX the queue limits may be
969 * different, so the old queue
970 * swapping method no longer works.
971 */
972 catq(&tp->t_canq, &tp->t_rawq);
973 }
974 CLR(tp->t_lflag, PENDIN);
975 }
976 ttwakeup(tp);
977 }
978 tp->t_iflag = t->c_iflag;
979 tp->t_oflag = t->c_oflag;
980 /*
981 * Make the EXTPROC bit read only.
982 */
983 if (ISSET(tp->t_lflag, EXTPROC))
984 SET(t->c_lflag, EXTPROC);
985 else
986 CLR(t->c_lflag, EXTPROC);
987 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
988 if (t->c_cc[VMIN] != tp->t_cc[VMIN] ||
989 t->c_cc[VTIME] != tp->t_cc[VTIME])
990 ttwakeup(tp);
991 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
992 splx(s);
993 break;
994 }
995 case TIOCSETD: { /* set line discipline */
996 register int t = *(int *)data;
997 dev_t device = tp->t_dev;
998
999 if ((u_int)t >= nlinesw)
1000 return (ENXIO);
1001 if (t != tp->t_line) {
1002 s = spltty();
1003 (*linesw[tp->t_line].l_close)(tp, flag);
1004 error = (*linesw[t].l_open)(device, tp);
1005 if (error) {
1006 (void)(*linesw[tp->t_line].l_open)(device, tp);
1007 splx(s);
1008 return (error);
1009 }
1010 tp->t_line = t;
1011 splx(s);
1012 }
1013 break;
1014 }
1015 case TIOCSTART: /* start output, like ^Q */
1016 s = spltty();
1017 if (ISSET(tp->t_state, TS_TTSTOP) ||
1018 ISSET(tp->t_lflag, FLUSHO)) {
1019 CLR(tp->t_lflag, FLUSHO);
1020 CLR(tp->t_state, TS_TTSTOP);
1021 ttstart(tp);
1022 }
1023 splx(s);
1024 break;
1025 case TIOCSTI: /* simulate terminal input */
1026 if ((flag & FREAD) == 0 && suser_xxx(p->p_ucred, NULL, 0))
1027 return (EPERM);
1028 if (!isctty(p, tp) && suser_xxx(p->p_ucred, NULL, 0))
1029 return (EACCES);
1030 s = spltty();
1031 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
1032 splx(s);
1033 break;
1034 case TIOCSTOP: /* stop output, like ^S */
1035 s = spltty();
1036 if (!ISSET(tp->t_state, TS_TTSTOP)) {
1037 SET(tp->t_state, TS_TTSTOP);
1038 (*tp->t_stop)(tp, 0);
1039 }
1040 splx(s);
1041 break;
1042 case TIOCSCTTY: /* become controlling tty */
1043 /* Session ctty vnode pointer set in vnode layer. */
1044 PGRPSESS_XLOCK();
1015 if (!SESS_LEADER(p) ||
1016 ((p->p_session->s_ttyvp || tp->t_session) &&
1045 if (!SESS_LEADER(p) ||
1046 ((p->p_session->s_ttyvp || tp->t_session) &&
1017 (tp->t_session != p->p_session)))
1047 (tp->t_session != p->p_session))) {
1048 PGRPSESS_XUNLOCK();
1018 return (EPERM);
1049 return (EPERM);
1050 }
1019 tp->t_session = p->p_session;
1020 tp->t_pgrp = p->p_pgrp;
1051 tp->t_session = p->p_session;
1052 tp->t_pgrp = p->p_pgrp;
1053 SESS_LOCK(p->p_session);
1021 p->p_session->s_ttyp = tp;
1054 p->p_session->s_ttyp = tp;
1055 SESS_UNLOCK(p->p_session);
1056 PROC_LOCK(p);
1022 p->p_flag |= P_CONTROLT;
1057 p->p_flag |= P_CONTROLT;
1058 PROC_UNLOCK(p);
1059 PGRPSESS_XUNLOCK();
1023 break;
1024 case TIOCSPGRP: { /* set pgrp of tty */
1060 break;
1061 case TIOCSPGRP: { /* set pgrp of tty */
1025 register struct pgrp *pgrp = pgfind(*(int *)data);
1062 register struct pgrp *pgrp;
1026
1063
1027 if (!isctty(p, tp))
1064 PGRPSESS_SLOCK();
1065 pgrp = pgfind(*(int *)data);
1066 if (!isctty(p, tp)) {
1067 if (pgrp != NULL)
1068 PGRP_UNLOCK(pgrp);
1069 PGRPSESS_SUNLOCK();
1028 return (ENOTTY);
1070 return (ENOTTY);
1029 else if (pgrp == NULL || pgrp->pg_session != p->p_session)
1071 }
1072 if (pgrp == NULL) {
1073 PGRPSESS_SUNLOCK();
1030 return (EPERM);
1074 return (EPERM);
1075 }
1076 PGRP_UNLOCK(pgrp);
1077 if (pgrp->pg_session != p->p_session) {
1078 PGRPSESS_SUNLOCK();
1079 return (EPERM);
1080 }
1081 PGRPSESS_SUNLOCK();
1031 tp->t_pgrp = pgrp;
1032 break;
1033 }
1034 case TIOCSTAT: /* simulate control-T */
1035 s = spltty();
1036 ttyinfo(tp);
1037 splx(s);
1038 break;
1039 case TIOCSWINSZ: /* set window size */
1040 if (bcmp((caddr_t)&tp->t_winsize, data,
1041 sizeof (struct winsize))) {
1042 tp->t_winsize = *(struct winsize *)data;
1082 tp->t_pgrp = pgrp;
1083 break;
1084 }
1085 case TIOCSTAT: /* simulate control-T */
1086 s = spltty();
1087 ttyinfo(tp);
1088 splx(s);
1089 break;
1090 case TIOCSWINSZ: /* set window size */
1091 if (bcmp((caddr_t)&tp->t_winsize, data,
1092 sizeof (struct winsize))) {
1093 tp->t_winsize = *(struct winsize *)data;
1043 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1094 if (tp->t_pgrp != NULL) {
1095 PGRP_LOCK(tp->t_pgrp);
1096 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1097 PGRP_UNLOCK(tp->t_pgrp);
1098 }
1044 }
1045 break;
1046 case TIOCSDRAINWAIT:
1047 error = suser_xxx(p->p_ucred, NULL, 0);
1048 if (error)
1049 return (error);
1050 tp->t_timeout = *(int *)data * hz;
1051 wakeup(TSA_OCOMPLETE(tp));
1052 wakeup(TSA_OLOWAT(tp));
1053 break;
1054 case TIOCGDRAINWAIT:
1055 *(int *)data = tp->t_timeout / hz;
1056 break;
1057 default:
1058#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1059 return (ttcompat(tp, cmd, data, flag));
1060#else
1061 return (ENOIOCTL);
1062#endif
1063 }
1064 return (0);
1065}
1066
1067int
1068ttypoll(dev, events, td)
1069 dev_t dev;
1070 int events;
1071 struct thread *td;
1072{
1073 int s;
1074 int revents = 0;
1075 struct tty *tp;
1076
1077 tp = dev->si_tty;
1078 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */
1079 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM))
1080 | POLLHUP);
1081
1082 s = spltty();
1083 if (events & (POLLIN | POLLRDNORM)) {
1084 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE))
1085 revents |= events & (POLLIN | POLLRDNORM);
1086 else
1087 selrecord(td, &tp->t_rsel);
1088 }
1089 if (events & (POLLOUT | POLLWRNORM)) {
1090 if ((tp->t_outq.c_cc <= tp->t_olowat &&
1091 ISSET(tp->t_state, TS_CONNECTED))
1092 || ISSET(tp->t_state, TS_ZOMBIE))
1093 revents |= events & (POLLOUT | POLLWRNORM);
1094 else
1095 selrecord(td, &tp->t_wsel);
1096 }
1097 splx(s);
1098 return (revents);
1099}
1100
1101static struct filterops ttyread_filtops =
1102 { 1, NULL, filt_ttyrdetach, filt_ttyread };
1103static struct filterops ttywrite_filtops =
1104 { 1, NULL, filt_ttywdetach, filt_ttywrite };
1105
1106int
1107ttykqfilter(dev, kn)
1108 dev_t dev;
1109 struct knote *kn;
1110{
1111 struct tty *tp = dev->si_tty;
1112 struct klist *klist;
1113 int s;
1114
1115 switch (kn->kn_filter) {
1116 case EVFILT_READ:
1117 klist = &tp->t_rsel.si_note;
1118 kn->kn_fop = &ttyread_filtops;
1119 break;
1120 case EVFILT_WRITE:
1121 klist = &tp->t_wsel.si_note;
1122 kn->kn_fop = &ttywrite_filtops;
1123 break;
1124 default:
1125 return (1);
1126 }
1127
1128 kn->kn_hook = (caddr_t)dev;
1129
1130 s = spltty();
1131 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1132 splx(s);
1133
1134 return (0);
1135}
1136
1137static void
1138filt_ttyrdetach(struct knote *kn)
1139{
1140 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1141 int s = spltty();
1142
1143 SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext);
1144 splx(s);
1145}
1146
1147static int
1148filt_ttyread(struct knote *kn, long hint)
1149{
1150 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1151
1152 kn->kn_data = ttnread(tp);
1153 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1154 kn->kn_flags |= EV_EOF;
1155 return (1);
1156 }
1157 return (kn->kn_data > 0);
1158}
1159
1160static void
1161filt_ttywdetach(struct knote *kn)
1162{
1163 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1164 int s = spltty();
1165
1166 SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext);
1167 splx(s);
1168}
1169
1170static int
1171filt_ttywrite(kn, hint)
1172 struct knote *kn;
1173 long hint;
1174{
1175 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1176
1177 kn->kn_data = tp->t_outq.c_cc;
1178 if (ISSET(tp->t_state, TS_ZOMBIE))
1179 return (1);
1180 return (kn->kn_data <= tp->t_olowat &&
1181 ISSET(tp->t_state, TS_CONNECTED));
1182}
1183
1184/*
1185 * Must be called at spltty().
1186 */
1187static int
1188ttnread(tp)
1189 struct tty *tp;
1190{
1191 int nread;
1192
1193 if (ISSET(tp->t_lflag, PENDIN))
1194 ttypend(tp);
1195 nread = tp->t_canq.c_cc;
1196 if (!ISSET(tp->t_lflag, ICANON)) {
1197 nread += tp->t_rawq.c_cc;
1198 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0)
1199 nread = 0;
1200 }
1201 return (nread);
1202}
1203
1204/*
1205 * Wait for output to drain.
1206 */
1207int
1208ttywait(tp)
1209 register struct tty *tp;
1210{
1211 int error, s;
1212
1213 error = 0;
1214 s = spltty();
1215 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1216 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) {
1217 (*tp->t_oproc)(tp);
1218 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1219 ISSET(tp->t_state, TS_CONNECTED)) {
1220 SET(tp->t_state, TS_SO_OCOMPLETE);
1221 error = ttysleep(tp, TSA_OCOMPLETE(tp),
1222 TTOPRI | PCATCH, "ttywai",
1223 tp->t_timeout);
1224 if (error) {
1225 if (error == EWOULDBLOCK)
1226 error = EIO;
1227 break;
1228 }
1229 } else
1230 break;
1231 }
1232 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)))
1233 error = EIO;
1234 splx(s);
1235 return (error);
1236}
1237
1238/*
1239 * Flush if successfully wait.
1240 */
1241static int
1242ttywflush(tp)
1243 struct tty *tp;
1244{
1245 int error;
1246
1247 if ((error = ttywait(tp)) == 0)
1248 ttyflush(tp, FREAD);
1249 return (error);
1250}
1251
1252/*
1253 * Flush tty read and/or write queues, notifying anyone waiting.
1254 */
1255void
1256ttyflush(tp, rw)
1257 register struct tty *tp;
1258 int rw;
1259{
1260 register int s;
1261
1262 s = spltty();
1263#if 0
1264again:
1265#endif
1266 if (rw & FWRITE) {
1267 FLUSHQ(&tp->t_outq);
1268 CLR(tp->t_state, TS_TTSTOP);
1269 }
1270 (*tp->t_stop)(tp, rw);
1271 if (rw & FREAD) {
1272 FLUSHQ(&tp->t_canq);
1273 FLUSHQ(&tp->t_rawq);
1274 CLR(tp->t_lflag, PENDIN);
1275 tp->t_rocount = 0;
1276 tp->t_rocol = 0;
1277 CLR(tp->t_state, TS_LOCAL);
1278 ttwakeup(tp);
1279 if (ISSET(tp->t_state, TS_TBLOCK)) {
1280 if (rw & FWRITE)
1281 FLUSHQ(&tp->t_outq);
1282 ttyunblock(tp);
1283
1284 /*
1285 * Don't let leave any state that might clobber the
1286 * next line discipline (although we should do more
1287 * to send the START char). Not clearing the state
1288 * may have caused the "putc to a clist with no
1289 * reserved cblocks" panic/printf.
1290 */
1291 CLR(tp->t_state, TS_TBLOCK);
1292
1293#if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1294 if (ISSET(tp->t_iflag, IXOFF)) {
1295 /*
1296 * XXX wait a bit in the hope that the stop
1297 * character (if any) will go out. Waiting
1298 * isn't good since it allows races. This
1299 * will be fixed when the stop character is
1300 * put in a special queue. Don't bother with
1301 * the checks in ttywait() since the timeout
1302 * will save us.
1303 */
1304 SET(tp->t_state, TS_SO_OCOMPLETE);
1305 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI,
1306 "ttyfls", hz / 10);
1307 /*
1308 * Don't try sending the stop character again.
1309 */
1310 CLR(tp->t_state, TS_TBLOCK);
1311 goto again;
1312 }
1313#endif
1314 }
1315 }
1316 if (rw & FWRITE) {
1317 FLUSHQ(&tp->t_outq);
1318 ttwwakeup(tp);
1319 }
1320 splx(s);
1321}
1322
1323/*
1324 * Copy in the default termios characters.
1325 */
1326void
1327termioschars(t)
1328 struct termios *t;
1329{
1330
1331 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc);
1332}
1333
1334/*
1335 * Old interface.
1336 */
1337void
1338ttychars(tp)
1339 struct tty *tp;
1340{
1341
1342 termioschars(&tp->t_termios);
1343}
1344
1345/*
1346 * Handle input high water. Send stop character for the IXOFF case. Turn
1347 * on our input flow control bit and propagate the changes to the driver.
1348 * XXX the stop character should be put in a special high priority queue.
1349 */
1350void
1351ttyblock(tp)
1352 struct tty *tp;
1353{
1354
1355 SET(tp->t_state, TS_TBLOCK);
1356 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1357 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0)
1358 CLR(tp->t_state, TS_TBLOCK); /* try again later */
1359 ttstart(tp);
1360}
1361
1362/*
1363 * Handle input low water. Send start character for the IXOFF case. Turn
1364 * off our input flow control bit and propagate the changes to the driver.
1365 * XXX the start character should be put in a special high priority queue.
1366 */
1367static void
1368ttyunblock(tp)
1369 struct tty *tp;
1370{
1371
1372 CLR(tp->t_state, TS_TBLOCK);
1373 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE &&
1374 putc(tp->t_cc[VSTART], &tp->t_outq) != 0)
1375 SET(tp->t_state, TS_TBLOCK); /* try again later */
1376 ttstart(tp);
1377}
1378
1379#ifdef notyet
1380/* Not used by any current (i386) drivers. */
1381/*
1382 * Restart after an inter-char delay.
1383 */
1384void
1385ttrstrt(tp_arg)
1386 void *tp_arg;
1387{
1388 struct tty *tp;
1389 int s;
1390
1391 KASSERT(tp_arg != NULL, ("ttrstrt"));
1392
1393 tp = tp_arg;
1394 s = spltty();
1395
1396 CLR(tp->t_state, TS_TIMEOUT);
1397 ttstart(tp);
1398
1399 splx(s);
1400}
1401#endif
1402
1403int
1404ttstart(tp)
1405 struct tty *tp;
1406{
1407
1408 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1409 (*tp->t_oproc)(tp);
1410 return (0);
1411}
1412
1413/*
1414 * "close" a line discipline
1415 */
1416int
1417ttylclose(tp, flag)
1418 struct tty *tp;
1419 int flag;
1420{
1421
1422 if (flag & FNONBLOCK || ttywflush(tp))
1423 ttyflush(tp, FREAD | FWRITE);
1424 return (0);
1425}
1426
1427/*
1428 * Handle modem control transition on a tty.
1429 * Flag indicates new state of carrier.
1430 * Returns 0 if the line should be turned off, otherwise 1.
1431 */
1432int
1433ttymodem(tp, flag)
1434 register struct tty *tp;
1435 int flag;
1436{
1437
1438 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) {
1439 /*
1440 * MDMBUF: do flow control according to carrier flag
1441 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1442 * works if IXON and IXANY are clear.
1443 */
1444 if (flag) {
1445 CLR(tp->t_state, TS_CAR_OFLOW);
1446 CLR(tp->t_state, TS_TTSTOP);
1447 ttstart(tp);
1448 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) {
1449 SET(tp->t_state, TS_CAR_OFLOW);
1450 SET(tp->t_state, TS_TTSTOP);
1451 (*tp->t_stop)(tp, 0);
1452 }
1453 } else if (flag == 0) {
1454 /*
1455 * Lost carrier.
1456 */
1457 CLR(tp->t_state, TS_CARR_ON);
1458 if (ISSET(tp->t_state, TS_ISOPEN) &&
1459 !ISSET(tp->t_cflag, CLOCAL)) {
1460 SET(tp->t_state, TS_ZOMBIE);
1461 CLR(tp->t_state, TS_CONNECTED);
1099 }
1100 break;
1101 case TIOCSDRAINWAIT:
1102 error = suser_xxx(p->p_ucred, NULL, 0);
1103 if (error)
1104 return (error);
1105 tp->t_timeout = *(int *)data * hz;
1106 wakeup(TSA_OCOMPLETE(tp));
1107 wakeup(TSA_OLOWAT(tp));
1108 break;
1109 case TIOCGDRAINWAIT:
1110 *(int *)data = tp->t_timeout / hz;
1111 break;
1112 default:
1113#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1114 return (ttcompat(tp, cmd, data, flag));
1115#else
1116 return (ENOIOCTL);
1117#endif
1118 }
1119 return (0);
1120}
1121
1122int
1123ttypoll(dev, events, td)
1124 dev_t dev;
1125 int events;
1126 struct thread *td;
1127{
1128 int s;
1129 int revents = 0;
1130 struct tty *tp;
1131
1132 tp = dev->si_tty;
1133 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */
1134 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM))
1135 | POLLHUP);
1136
1137 s = spltty();
1138 if (events & (POLLIN | POLLRDNORM)) {
1139 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE))
1140 revents |= events & (POLLIN | POLLRDNORM);
1141 else
1142 selrecord(td, &tp->t_rsel);
1143 }
1144 if (events & (POLLOUT | POLLWRNORM)) {
1145 if ((tp->t_outq.c_cc <= tp->t_olowat &&
1146 ISSET(tp->t_state, TS_CONNECTED))
1147 || ISSET(tp->t_state, TS_ZOMBIE))
1148 revents |= events & (POLLOUT | POLLWRNORM);
1149 else
1150 selrecord(td, &tp->t_wsel);
1151 }
1152 splx(s);
1153 return (revents);
1154}
1155
1156static struct filterops ttyread_filtops =
1157 { 1, NULL, filt_ttyrdetach, filt_ttyread };
1158static struct filterops ttywrite_filtops =
1159 { 1, NULL, filt_ttywdetach, filt_ttywrite };
1160
1161int
1162ttykqfilter(dev, kn)
1163 dev_t dev;
1164 struct knote *kn;
1165{
1166 struct tty *tp = dev->si_tty;
1167 struct klist *klist;
1168 int s;
1169
1170 switch (kn->kn_filter) {
1171 case EVFILT_READ:
1172 klist = &tp->t_rsel.si_note;
1173 kn->kn_fop = &ttyread_filtops;
1174 break;
1175 case EVFILT_WRITE:
1176 klist = &tp->t_wsel.si_note;
1177 kn->kn_fop = &ttywrite_filtops;
1178 break;
1179 default:
1180 return (1);
1181 }
1182
1183 kn->kn_hook = (caddr_t)dev;
1184
1185 s = spltty();
1186 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1187 splx(s);
1188
1189 return (0);
1190}
1191
1192static void
1193filt_ttyrdetach(struct knote *kn)
1194{
1195 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1196 int s = spltty();
1197
1198 SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext);
1199 splx(s);
1200}
1201
1202static int
1203filt_ttyread(struct knote *kn, long hint)
1204{
1205 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1206
1207 kn->kn_data = ttnread(tp);
1208 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1209 kn->kn_flags |= EV_EOF;
1210 return (1);
1211 }
1212 return (kn->kn_data > 0);
1213}
1214
1215static void
1216filt_ttywdetach(struct knote *kn)
1217{
1218 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1219 int s = spltty();
1220
1221 SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext);
1222 splx(s);
1223}
1224
1225static int
1226filt_ttywrite(kn, hint)
1227 struct knote *kn;
1228 long hint;
1229{
1230 struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1231
1232 kn->kn_data = tp->t_outq.c_cc;
1233 if (ISSET(tp->t_state, TS_ZOMBIE))
1234 return (1);
1235 return (kn->kn_data <= tp->t_olowat &&
1236 ISSET(tp->t_state, TS_CONNECTED));
1237}
1238
1239/*
1240 * Must be called at spltty().
1241 */
1242static int
1243ttnread(tp)
1244 struct tty *tp;
1245{
1246 int nread;
1247
1248 if (ISSET(tp->t_lflag, PENDIN))
1249 ttypend(tp);
1250 nread = tp->t_canq.c_cc;
1251 if (!ISSET(tp->t_lflag, ICANON)) {
1252 nread += tp->t_rawq.c_cc;
1253 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0)
1254 nread = 0;
1255 }
1256 return (nread);
1257}
1258
1259/*
1260 * Wait for output to drain.
1261 */
1262int
1263ttywait(tp)
1264 register struct tty *tp;
1265{
1266 int error, s;
1267
1268 error = 0;
1269 s = spltty();
1270 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1271 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) {
1272 (*tp->t_oproc)(tp);
1273 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1274 ISSET(tp->t_state, TS_CONNECTED)) {
1275 SET(tp->t_state, TS_SO_OCOMPLETE);
1276 error = ttysleep(tp, TSA_OCOMPLETE(tp),
1277 TTOPRI | PCATCH, "ttywai",
1278 tp->t_timeout);
1279 if (error) {
1280 if (error == EWOULDBLOCK)
1281 error = EIO;
1282 break;
1283 }
1284 } else
1285 break;
1286 }
1287 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)))
1288 error = EIO;
1289 splx(s);
1290 return (error);
1291}
1292
1293/*
1294 * Flush if successfully wait.
1295 */
1296static int
1297ttywflush(tp)
1298 struct tty *tp;
1299{
1300 int error;
1301
1302 if ((error = ttywait(tp)) == 0)
1303 ttyflush(tp, FREAD);
1304 return (error);
1305}
1306
1307/*
1308 * Flush tty read and/or write queues, notifying anyone waiting.
1309 */
1310void
1311ttyflush(tp, rw)
1312 register struct tty *tp;
1313 int rw;
1314{
1315 register int s;
1316
1317 s = spltty();
1318#if 0
1319again:
1320#endif
1321 if (rw & FWRITE) {
1322 FLUSHQ(&tp->t_outq);
1323 CLR(tp->t_state, TS_TTSTOP);
1324 }
1325 (*tp->t_stop)(tp, rw);
1326 if (rw & FREAD) {
1327 FLUSHQ(&tp->t_canq);
1328 FLUSHQ(&tp->t_rawq);
1329 CLR(tp->t_lflag, PENDIN);
1330 tp->t_rocount = 0;
1331 tp->t_rocol = 0;
1332 CLR(tp->t_state, TS_LOCAL);
1333 ttwakeup(tp);
1334 if (ISSET(tp->t_state, TS_TBLOCK)) {
1335 if (rw & FWRITE)
1336 FLUSHQ(&tp->t_outq);
1337 ttyunblock(tp);
1338
1339 /*
1340 * Don't let leave any state that might clobber the
1341 * next line discipline (although we should do more
1342 * to send the START char). Not clearing the state
1343 * may have caused the "putc to a clist with no
1344 * reserved cblocks" panic/printf.
1345 */
1346 CLR(tp->t_state, TS_TBLOCK);
1347
1348#if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1349 if (ISSET(tp->t_iflag, IXOFF)) {
1350 /*
1351 * XXX wait a bit in the hope that the stop
1352 * character (if any) will go out. Waiting
1353 * isn't good since it allows races. This
1354 * will be fixed when the stop character is
1355 * put in a special queue. Don't bother with
1356 * the checks in ttywait() since the timeout
1357 * will save us.
1358 */
1359 SET(tp->t_state, TS_SO_OCOMPLETE);
1360 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI,
1361 "ttyfls", hz / 10);
1362 /*
1363 * Don't try sending the stop character again.
1364 */
1365 CLR(tp->t_state, TS_TBLOCK);
1366 goto again;
1367 }
1368#endif
1369 }
1370 }
1371 if (rw & FWRITE) {
1372 FLUSHQ(&tp->t_outq);
1373 ttwwakeup(tp);
1374 }
1375 splx(s);
1376}
1377
1378/*
1379 * Copy in the default termios characters.
1380 */
1381void
1382termioschars(t)
1383 struct termios *t;
1384{
1385
1386 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc);
1387}
1388
1389/*
1390 * Old interface.
1391 */
1392void
1393ttychars(tp)
1394 struct tty *tp;
1395{
1396
1397 termioschars(&tp->t_termios);
1398}
1399
1400/*
1401 * Handle input high water. Send stop character for the IXOFF case. Turn
1402 * on our input flow control bit and propagate the changes to the driver.
1403 * XXX the stop character should be put in a special high priority queue.
1404 */
1405void
1406ttyblock(tp)
1407 struct tty *tp;
1408{
1409
1410 SET(tp->t_state, TS_TBLOCK);
1411 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1412 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0)
1413 CLR(tp->t_state, TS_TBLOCK); /* try again later */
1414 ttstart(tp);
1415}
1416
1417/*
1418 * Handle input low water. Send start character for the IXOFF case. Turn
1419 * off our input flow control bit and propagate the changes to the driver.
1420 * XXX the start character should be put in a special high priority queue.
1421 */
1422static void
1423ttyunblock(tp)
1424 struct tty *tp;
1425{
1426
1427 CLR(tp->t_state, TS_TBLOCK);
1428 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE &&
1429 putc(tp->t_cc[VSTART], &tp->t_outq) != 0)
1430 SET(tp->t_state, TS_TBLOCK); /* try again later */
1431 ttstart(tp);
1432}
1433
1434#ifdef notyet
1435/* Not used by any current (i386) drivers. */
1436/*
1437 * Restart after an inter-char delay.
1438 */
1439void
1440ttrstrt(tp_arg)
1441 void *tp_arg;
1442{
1443 struct tty *tp;
1444 int s;
1445
1446 KASSERT(tp_arg != NULL, ("ttrstrt"));
1447
1448 tp = tp_arg;
1449 s = spltty();
1450
1451 CLR(tp->t_state, TS_TIMEOUT);
1452 ttstart(tp);
1453
1454 splx(s);
1455}
1456#endif
1457
1458int
1459ttstart(tp)
1460 struct tty *tp;
1461{
1462
1463 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1464 (*tp->t_oproc)(tp);
1465 return (0);
1466}
1467
1468/*
1469 * "close" a line discipline
1470 */
1471int
1472ttylclose(tp, flag)
1473 struct tty *tp;
1474 int flag;
1475{
1476
1477 if (flag & FNONBLOCK || ttywflush(tp))
1478 ttyflush(tp, FREAD | FWRITE);
1479 return (0);
1480}
1481
1482/*
1483 * Handle modem control transition on a tty.
1484 * Flag indicates new state of carrier.
1485 * Returns 0 if the line should be turned off, otherwise 1.
1486 */
1487int
1488ttymodem(tp, flag)
1489 register struct tty *tp;
1490 int flag;
1491{
1492
1493 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) {
1494 /*
1495 * MDMBUF: do flow control according to carrier flag
1496 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1497 * works if IXON and IXANY are clear.
1498 */
1499 if (flag) {
1500 CLR(tp->t_state, TS_CAR_OFLOW);
1501 CLR(tp->t_state, TS_TTSTOP);
1502 ttstart(tp);
1503 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) {
1504 SET(tp->t_state, TS_CAR_OFLOW);
1505 SET(tp->t_state, TS_TTSTOP);
1506 (*tp->t_stop)(tp, 0);
1507 }
1508 } else if (flag == 0) {
1509 /*
1510 * Lost carrier.
1511 */
1512 CLR(tp->t_state, TS_CARR_ON);
1513 if (ISSET(tp->t_state, TS_ISOPEN) &&
1514 !ISSET(tp->t_cflag, CLOCAL)) {
1515 SET(tp->t_state, TS_ZOMBIE);
1516 CLR(tp->t_state, TS_CONNECTED);
1462 if (tp->t_session && tp->t_session->s_leader) {
1463 struct proc *p;
1517 if (tp->t_session) {
1518 PGRPSESS_SLOCK();
1519 if (tp->t_session->s_leader) {
1520 struct proc *p;
1464
1521
1465 p = tp->t_session->s_leader;
1466 PROC_LOCK(p);
1467 psignal(p, SIGHUP);
1468 PROC_UNLOCK(p);
1522 p = tp->t_session->s_leader;
1523 PROC_LOCK(p);
1524 psignal(p, SIGHUP);
1525 PROC_UNLOCK(p);
1526 }
1527 PGRPSESS_SUNLOCK();
1469 }
1470 ttyflush(tp, FREAD | FWRITE);
1471 return (0);
1472 }
1473 } else {
1474 /*
1475 * Carrier now on.
1476 */
1477 SET(tp->t_state, TS_CARR_ON);
1478 if (!ISSET(tp->t_state, TS_ZOMBIE))
1479 SET(tp->t_state, TS_CONNECTED);
1480 wakeup(TSA_CARR_ON(tp));
1481 ttwakeup(tp);
1482 ttwwakeup(tp);
1483 }
1484 return (1);
1485}
1486
1487/*
1488 * Reinput pending characters after state switch
1489 * call at spltty().
1490 */
1491static void
1492ttypend(tp)
1493 register struct tty *tp;
1494{
1495 struct clist tq;
1496 register int c;
1497
1498 CLR(tp->t_lflag, PENDIN);
1499 SET(tp->t_state, TS_TYPEN);
1500 /*
1501 * XXX this assumes too much about clist internals. It may even
1502 * fail if the cblock slush pool is empty. We can't allocate more
1503 * cblocks here because we are called from an interrupt handler
1504 * and clist_alloc_cblocks() can wait.
1505 */
1506 tq = tp->t_rawq;
1507 bzero(&tp->t_rawq, sizeof tp->t_rawq);
1508 tp->t_rawq.c_cbmax = tq.c_cbmax;
1509 tp->t_rawq.c_cbreserved = tq.c_cbreserved;
1510 while ((c = getc(&tq)) >= 0)
1511 ttyinput(c, tp);
1512 CLR(tp->t_state, TS_TYPEN);
1513}
1514
1515/*
1516 * Process a read call on a tty device.
1517 */
1518int
1519ttread(tp, uio, flag)
1520 register struct tty *tp;
1521 struct uio *uio;
1522 int flag;
1523{
1524 register struct clist *qp;
1525 register int c;
1526 register tcflag_t lflag;
1527 register cc_t *cc = tp->t_cc;
1528 register struct proc *p = curproc;
1529 int s, first, error = 0;
1530 int has_stime = 0, last_cc = 0;
1531 long slp = 0; /* XXX this should be renamed `timo'. */
1532 struct timeval stime;
1533
1534loop:
1535 s = spltty();
1536 lflag = tp->t_lflag;
1537 /*
1538 * take pending input first
1539 */
1540 if (ISSET(lflag, PENDIN)) {
1541 ttypend(tp);
1542 splx(s); /* reduce latency */
1543 s = spltty();
1544 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */
1545 }
1546
1547 /*
1548 * Hang process if it's in the background.
1549 */
1550 if (isbackground(p, tp)) {
1551 splx(s);
1528 }
1529 ttyflush(tp, FREAD | FWRITE);
1530 return (0);
1531 }
1532 } else {
1533 /*
1534 * Carrier now on.
1535 */
1536 SET(tp->t_state, TS_CARR_ON);
1537 if (!ISSET(tp->t_state, TS_ZOMBIE))
1538 SET(tp->t_state, TS_CONNECTED);
1539 wakeup(TSA_CARR_ON(tp));
1540 ttwakeup(tp);
1541 ttwwakeup(tp);
1542 }
1543 return (1);
1544}
1545
1546/*
1547 * Reinput pending characters after state switch
1548 * call at spltty().
1549 */
1550static void
1551ttypend(tp)
1552 register struct tty *tp;
1553{
1554 struct clist tq;
1555 register int c;
1556
1557 CLR(tp->t_lflag, PENDIN);
1558 SET(tp->t_state, TS_TYPEN);
1559 /*
1560 * XXX this assumes too much about clist internals. It may even
1561 * fail if the cblock slush pool is empty. We can't allocate more
1562 * cblocks here because we are called from an interrupt handler
1563 * and clist_alloc_cblocks() can wait.
1564 */
1565 tq = tp->t_rawq;
1566 bzero(&tp->t_rawq, sizeof tp->t_rawq);
1567 tp->t_rawq.c_cbmax = tq.c_cbmax;
1568 tp->t_rawq.c_cbreserved = tq.c_cbreserved;
1569 while ((c = getc(&tq)) >= 0)
1570 ttyinput(c, tp);
1571 CLR(tp->t_state, TS_TYPEN);
1572}
1573
1574/*
1575 * Process a read call on a tty device.
1576 */
1577int
1578ttread(tp, uio, flag)
1579 register struct tty *tp;
1580 struct uio *uio;
1581 int flag;
1582{
1583 register struct clist *qp;
1584 register int c;
1585 register tcflag_t lflag;
1586 register cc_t *cc = tp->t_cc;
1587 register struct proc *p = curproc;
1588 int s, first, error = 0;
1589 int has_stime = 0, last_cc = 0;
1590 long slp = 0; /* XXX this should be renamed `timo'. */
1591 struct timeval stime;
1592
1593loop:
1594 s = spltty();
1595 lflag = tp->t_lflag;
1596 /*
1597 * take pending input first
1598 */
1599 if (ISSET(lflag, PENDIN)) {
1600 ttypend(tp);
1601 splx(s); /* reduce latency */
1602 s = spltty();
1603 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */
1604 }
1605
1606 /*
1607 * Hang process if it's in the background.
1608 */
1609 if (isbackground(p, tp)) {
1610 splx(s);
1611 PGRPSESS_SLOCK();
1612 PROC_LOCK(p);
1552 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
1553 SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
1613 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
1614 SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
1554 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0)
1615 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0) {
1616 PROC_UNLOCK(p);
1617 PGRPSESS_SUNLOCK();
1555 return (EIO);
1618 return (EIO);
1619 }
1620 PROC_UNLOCK(p);
1621 PGRP_LOCK(p->p_pgrp);
1622 PGRPSESS_SUNLOCK();
1556 pgsignal(p->p_pgrp, SIGTTIN, 1);
1623 pgsignal(p->p_pgrp, SIGTTIN, 1);
1624 PGRP_UNLOCK(p->p_pgrp);
1557 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0);
1558 if (error)
1559 return (error);
1560 goto loop;
1561 }
1562
1563 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1564 splx(s);
1565 return (0); /* EOF */
1566 }
1567
1568 /*
1569 * If canonical, use the canonical queue,
1570 * else use the raw queue.
1571 *
1572 * (should get rid of clists...)
1573 */
1574 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq;
1575
1576 if (flag & IO_NDELAY) {
1577 if (qp->c_cc > 0)
1578 goto read;
1579 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) {
1580 splx(s);
1581 return (0);
1582 }
1583 splx(s);
1584 return (EWOULDBLOCK);
1585 }
1586 if (!ISSET(lflag, ICANON)) {
1587 int m = cc[VMIN];
1588 long t = cc[VTIME];
1589 struct timeval timecopy;
1590
1591 /*
1592 * Check each of the four combinations.
1593 * (m > 0 && t == 0) is the normal read case.
1594 * It should be fairly efficient, so we check that and its
1595 * companion case (m == 0 && t == 0) first.
1596 * For the other two cases, we compute the target sleep time
1597 * into slp.
1598 */
1599 if (t == 0) {
1600 if (qp->c_cc < m)
1601 goto sleep;
1602 if (qp->c_cc > 0)
1603 goto read;
1604
1605 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1606 splx(s);
1607 return (0);
1608 }
1609 t *= 100000; /* time in us */
1610#define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1611 ((t1).tv_usec - (t2).tv_usec))
1612 if (m > 0) {
1613 if (qp->c_cc <= 0)
1614 goto sleep;
1615 if (qp->c_cc >= m)
1616 goto read;
1617 getmicrotime(&timecopy);
1618 if (!has_stime) {
1619 /* first character, start timer */
1620 has_stime = 1;
1621 stime = timecopy;
1622 slp = t;
1623 } else if (qp->c_cc > last_cc) {
1624 /* got a character, restart timer */
1625 stime = timecopy;
1626 slp = t;
1627 } else {
1628 /* nothing, check expiration */
1629 slp = t - diff(timecopy, stime);
1630 if (slp <= 0)
1631 goto read;
1632 }
1633 last_cc = qp->c_cc;
1634 } else { /* m == 0 */
1635 if (qp->c_cc > 0)
1636 goto read;
1637 getmicrotime(&timecopy);
1638 if (!has_stime) {
1639 has_stime = 1;
1640 stime = timecopy;
1641 slp = t;
1642 } else {
1643 slp = t - diff(timecopy, stime);
1644 if (slp <= 0) {
1645 /* Timed out, but 0 is enough input. */
1646 splx(s);
1647 return (0);
1648 }
1649 }
1650 }
1651#undef diff
1652 /*
1653 * Rounding down may make us wake up just short
1654 * of the target, so we round up.
1655 * The formula is ceiling(slp * hz/1000000).
1656 * 32-bit arithmetic is enough for hz < 169.
1657 * XXX see tvtohz() for how to avoid overflow if hz
1658 * is large (divide by `tick' and/or arrange to
1659 * use tvtohz() if hz is large).
1660 */
1661 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1662 goto sleep;
1663 }
1664 if (qp->c_cc <= 0) {
1665sleep:
1666 /*
1667 * There is no input, or not enough input and we can block.
1668 */
1669 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH,
1670 ISSET(tp->t_state, TS_CONNECTED) ?
1671 "ttyin" : "ttyhup", (int)slp);
1672 splx(s);
1673 if (error == EWOULDBLOCK)
1674 error = 0;
1675 else if (error)
1676 return (error);
1677 /*
1678 * XXX what happens if another process eats some input
1679 * while we are asleep (not just here)? It would be
1680 * safest to detect changes and reset our state variables
1681 * (has_stime and last_cc).
1682 */
1683 slp = 0;
1684 goto loop;
1685 }
1686read:
1687 splx(s);
1688 /*
1689 * Input present, check for input mapping and processing.
1690 */
1691 first = 1;
1692 if (ISSET(lflag, ICANON | ISIG))
1693 goto slowcase;
1694 for (;;) {
1695 char ibuf[IBUFSIZ];
1696 int icc;
1697
1698 icc = imin(uio->uio_resid, IBUFSIZ);
1699 icc = q_to_b(qp, ibuf, icc);
1700 if (icc <= 0) {
1701 if (first)
1702 goto loop;
1703 break;
1704 }
1705 error = uiomove(ibuf, icc, uio);
1706 /*
1707 * XXX if there was an error then we should ungetc() the
1708 * unmoved chars and reduce icc here.
1709 */
1710 if (error)
1711 break;
1712 if (uio->uio_resid == 0)
1713 break;
1714 first = 0;
1715 }
1716 goto out;
1717slowcase:
1718 for (;;) {
1719 c = getc(qp);
1720 if (c < 0) {
1721 if (first)
1722 goto loop;
1723 break;
1724 }
1725 /*
1726 * delayed suspend (^Y)
1727 */
1728 if (CCEQ(cc[VDSUSP], c) &&
1729 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) {
1625 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0);
1626 if (error)
1627 return (error);
1628 goto loop;
1629 }
1630
1631 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1632 splx(s);
1633 return (0); /* EOF */
1634 }
1635
1636 /*
1637 * If canonical, use the canonical queue,
1638 * else use the raw queue.
1639 *
1640 * (should get rid of clists...)
1641 */
1642 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq;
1643
1644 if (flag & IO_NDELAY) {
1645 if (qp->c_cc > 0)
1646 goto read;
1647 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) {
1648 splx(s);
1649 return (0);
1650 }
1651 splx(s);
1652 return (EWOULDBLOCK);
1653 }
1654 if (!ISSET(lflag, ICANON)) {
1655 int m = cc[VMIN];
1656 long t = cc[VTIME];
1657 struct timeval timecopy;
1658
1659 /*
1660 * Check each of the four combinations.
1661 * (m > 0 && t == 0) is the normal read case.
1662 * It should be fairly efficient, so we check that and its
1663 * companion case (m == 0 && t == 0) first.
1664 * For the other two cases, we compute the target sleep time
1665 * into slp.
1666 */
1667 if (t == 0) {
1668 if (qp->c_cc < m)
1669 goto sleep;
1670 if (qp->c_cc > 0)
1671 goto read;
1672
1673 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1674 splx(s);
1675 return (0);
1676 }
1677 t *= 100000; /* time in us */
1678#define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1679 ((t1).tv_usec - (t2).tv_usec))
1680 if (m > 0) {
1681 if (qp->c_cc <= 0)
1682 goto sleep;
1683 if (qp->c_cc >= m)
1684 goto read;
1685 getmicrotime(&timecopy);
1686 if (!has_stime) {
1687 /* first character, start timer */
1688 has_stime = 1;
1689 stime = timecopy;
1690 slp = t;
1691 } else if (qp->c_cc > last_cc) {
1692 /* got a character, restart timer */
1693 stime = timecopy;
1694 slp = t;
1695 } else {
1696 /* nothing, check expiration */
1697 slp = t - diff(timecopy, stime);
1698 if (slp <= 0)
1699 goto read;
1700 }
1701 last_cc = qp->c_cc;
1702 } else { /* m == 0 */
1703 if (qp->c_cc > 0)
1704 goto read;
1705 getmicrotime(&timecopy);
1706 if (!has_stime) {
1707 has_stime = 1;
1708 stime = timecopy;
1709 slp = t;
1710 } else {
1711 slp = t - diff(timecopy, stime);
1712 if (slp <= 0) {
1713 /* Timed out, but 0 is enough input. */
1714 splx(s);
1715 return (0);
1716 }
1717 }
1718 }
1719#undef diff
1720 /*
1721 * Rounding down may make us wake up just short
1722 * of the target, so we round up.
1723 * The formula is ceiling(slp * hz/1000000).
1724 * 32-bit arithmetic is enough for hz < 169.
1725 * XXX see tvtohz() for how to avoid overflow if hz
1726 * is large (divide by `tick' and/or arrange to
1727 * use tvtohz() if hz is large).
1728 */
1729 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1730 goto sleep;
1731 }
1732 if (qp->c_cc <= 0) {
1733sleep:
1734 /*
1735 * There is no input, or not enough input and we can block.
1736 */
1737 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH,
1738 ISSET(tp->t_state, TS_CONNECTED) ?
1739 "ttyin" : "ttyhup", (int)slp);
1740 splx(s);
1741 if (error == EWOULDBLOCK)
1742 error = 0;
1743 else if (error)
1744 return (error);
1745 /*
1746 * XXX what happens if another process eats some input
1747 * while we are asleep (not just here)? It would be
1748 * safest to detect changes and reset our state variables
1749 * (has_stime and last_cc).
1750 */
1751 slp = 0;
1752 goto loop;
1753 }
1754read:
1755 splx(s);
1756 /*
1757 * Input present, check for input mapping and processing.
1758 */
1759 first = 1;
1760 if (ISSET(lflag, ICANON | ISIG))
1761 goto slowcase;
1762 for (;;) {
1763 char ibuf[IBUFSIZ];
1764 int icc;
1765
1766 icc = imin(uio->uio_resid, IBUFSIZ);
1767 icc = q_to_b(qp, ibuf, icc);
1768 if (icc <= 0) {
1769 if (first)
1770 goto loop;
1771 break;
1772 }
1773 error = uiomove(ibuf, icc, uio);
1774 /*
1775 * XXX if there was an error then we should ungetc() the
1776 * unmoved chars and reduce icc here.
1777 */
1778 if (error)
1779 break;
1780 if (uio->uio_resid == 0)
1781 break;
1782 first = 0;
1783 }
1784 goto out;
1785slowcase:
1786 for (;;) {
1787 c = getc(qp);
1788 if (c < 0) {
1789 if (first)
1790 goto loop;
1791 break;
1792 }
1793 /*
1794 * delayed suspend (^Y)
1795 */
1796 if (CCEQ(cc[VDSUSP], c) &&
1797 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) {
1730 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1798 if (tp->t_pgrp != NULL) {
1799 PGRP_LOCK(tp->t_pgrp);
1800 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1801 PGRP_UNLOCK(tp->t_pgrp);
1802 }
1731 if (first) {
1732 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH,
1733 "ttybg3", 0);
1734 if (error)
1735 break;
1736 goto loop;
1737 }
1738 break;
1739 }
1740 /*
1741 * Interpret EOF only in canonical mode.
1742 */
1743 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1744 break;
1745 /*
1746 * Give user character.
1747 */
1748 error = ureadc(c, uio);
1749 if (error)
1750 /* XXX should ungetc(c, qp). */
1751 break;
1752 if (uio->uio_resid == 0)
1753 break;
1754 /*
1755 * In canonical mode check for a "break character"
1756 * marking the end of a "line of input".
1757 */
1758 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1759 break;
1760 first = 0;
1761 }
1762
1763out:
1764 /*
1765 * Look to unblock input now that (presumably)
1766 * the input queue has gone down.
1767 */
1768 s = spltty();
1769 if (ISSET(tp->t_state, TS_TBLOCK) &&
1770 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat)
1771 ttyunblock(tp);
1772 splx(s);
1773
1774 return (error);
1775}
1776
1777/*
1778 * Check the output queue on tp for space for a kernel message (from uprintf
1779 * or tprintf). Allow some space over the normal hiwater mark so we don't
1780 * lose messages due to normal flow control, but don't let the tty run amok.
1781 * Sleeps here are not interruptible, but we return prematurely if new signals
1782 * arrive.
1783 */
1784int
1785ttycheckoutq(tp, wait)
1786 register struct tty *tp;
1787 int wait;
1788{
1789 int hiwat, s;
1790 sigset_t oldmask;
1791
1792 hiwat = tp->t_ohiwat;
1793 SIGEMPTYSET(oldmask);
1794 s = spltty();
1795 if (wait)
1796 oldmask = curproc->p_siglist;
1797 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100)
1798 while (tp->t_outq.c_cc > hiwat) {
1799 ttstart(tp);
1800 if (tp->t_outq.c_cc <= hiwat)
1801 break;
1802 if (!(wait && SIGSETEQ(curproc->p_siglist, oldmask))) {
1803 splx(s);
1804 return (0);
1805 }
1806 SET(tp->t_state, TS_SO_OLOWAT);
1807 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz);
1808 }
1809 splx(s);
1810 return (1);
1811}
1812
1813/*
1814 * Process a write call on a tty device.
1815 */
1816int
1817ttwrite(tp, uio, flag)
1818 register struct tty *tp;
1819 register struct uio *uio;
1820 int flag;
1821{
1822 register char *cp = NULL;
1823 register int cc, ce;
1824 register struct proc *p;
1825 int i, hiwat, cnt, error, s;
1826 char obuf[OBUFSIZ];
1827
1828 hiwat = tp->t_ohiwat;
1829 cnt = uio->uio_resid;
1830 error = 0;
1831 cc = 0;
1832loop:
1833 s = spltty();
1834 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1835 splx(s);
1836 if (uio->uio_resid == cnt)
1837 error = EIO;
1838 goto out;
1839 }
1840 if (!ISSET(tp->t_state, TS_CONNECTED)) {
1841 if (flag & IO_NDELAY) {
1842 splx(s);
1843 error = EWOULDBLOCK;
1844 goto out;
1845 }
1846 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1847 "ttydcd", 0);
1848 splx(s);
1849 if (error)
1850 goto out;
1851 goto loop;
1852 }
1853 splx(s);
1854 /*
1855 * Hang the process if it's in the background.
1856 */
1857 p = curproc;
1803 if (first) {
1804 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH,
1805 "ttybg3", 0);
1806 if (error)
1807 break;
1808 goto loop;
1809 }
1810 break;
1811 }
1812 /*
1813 * Interpret EOF only in canonical mode.
1814 */
1815 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1816 break;
1817 /*
1818 * Give user character.
1819 */
1820 error = ureadc(c, uio);
1821 if (error)
1822 /* XXX should ungetc(c, qp). */
1823 break;
1824 if (uio->uio_resid == 0)
1825 break;
1826 /*
1827 * In canonical mode check for a "break character"
1828 * marking the end of a "line of input".
1829 */
1830 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1831 break;
1832 first = 0;
1833 }
1834
1835out:
1836 /*
1837 * Look to unblock input now that (presumably)
1838 * the input queue has gone down.
1839 */
1840 s = spltty();
1841 if (ISSET(tp->t_state, TS_TBLOCK) &&
1842 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat)
1843 ttyunblock(tp);
1844 splx(s);
1845
1846 return (error);
1847}
1848
1849/*
1850 * Check the output queue on tp for space for a kernel message (from uprintf
1851 * or tprintf). Allow some space over the normal hiwater mark so we don't
1852 * lose messages due to normal flow control, but don't let the tty run amok.
1853 * Sleeps here are not interruptible, but we return prematurely if new signals
1854 * arrive.
1855 */
1856int
1857ttycheckoutq(tp, wait)
1858 register struct tty *tp;
1859 int wait;
1860{
1861 int hiwat, s;
1862 sigset_t oldmask;
1863
1864 hiwat = tp->t_ohiwat;
1865 SIGEMPTYSET(oldmask);
1866 s = spltty();
1867 if (wait)
1868 oldmask = curproc->p_siglist;
1869 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100)
1870 while (tp->t_outq.c_cc > hiwat) {
1871 ttstart(tp);
1872 if (tp->t_outq.c_cc <= hiwat)
1873 break;
1874 if (!(wait && SIGSETEQ(curproc->p_siglist, oldmask))) {
1875 splx(s);
1876 return (0);
1877 }
1878 SET(tp->t_state, TS_SO_OLOWAT);
1879 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz);
1880 }
1881 splx(s);
1882 return (1);
1883}
1884
1885/*
1886 * Process a write call on a tty device.
1887 */
1888int
1889ttwrite(tp, uio, flag)
1890 register struct tty *tp;
1891 register struct uio *uio;
1892 int flag;
1893{
1894 register char *cp = NULL;
1895 register int cc, ce;
1896 register struct proc *p;
1897 int i, hiwat, cnt, error, s;
1898 char obuf[OBUFSIZ];
1899
1900 hiwat = tp->t_ohiwat;
1901 cnt = uio->uio_resid;
1902 error = 0;
1903 cc = 0;
1904loop:
1905 s = spltty();
1906 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1907 splx(s);
1908 if (uio->uio_resid == cnt)
1909 error = EIO;
1910 goto out;
1911 }
1912 if (!ISSET(tp->t_state, TS_CONNECTED)) {
1913 if (flag & IO_NDELAY) {
1914 splx(s);
1915 error = EWOULDBLOCK;
1916 goto out;
1917 }
1918 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1919 "ttydcd", 0);
1920 splx(s);
1921 if (error)
1922 goto out;
1923 goto loop;
1924 }
1925 splx(s);
1926 /*
1927 * Hang the process if it's in the background.
1928 */
1929 p = curproc;
1930 PGRPSESS_SLOCK();
1931 PROC_LOCK(p);
1858 if (isbackground(p, tp) &&
1859 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) &&
1860 !SIGISMEMBER(p->p_sigignore, SIGTTOU) &&
1861 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) {
1862 if (p->p_pgrp->pg_jobc == 0) {
1932 if (isbackground(p, tp) &&
1933 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) &&
1934 !SIGISMEMBER(p->p_sigignore, SIGTTOU) &&
1935 !SIGISMEMBER(p->p_sigmask, SIGTTOU)) {
1936 if (p->p_pgrp->pg_jobc == 0) {
1937 PROC_UNLOCK(p);
1938 PGRPSESS_SUNLOCK();
1863 error = EIO;
1864 goto out;
1865 }
1939 error = EIO;
1940 goto out;
1941 }
1942 PROC_UNLOCK(p);
1943 PGRP_LOCK(p->p_pgrp);
1944 PGRPSESS_SUNLOCK();
1866 pgsignal(p->p_pgrp, SIGTTOU, 1);
1945 pgsignal(p->p_pgrp, SIGTTOU, 1);
1946 PGRP_UNLOCK(p->p_pgrp);
1867 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0);
1868 if (error)
1869 goto out;
1870 goto loop;
1947 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0);
1948 if (error)
1949 goto out;
1950 goto loop;
1951 } else {
1952 PROC_UNLOCK(p);
1953 PGRPSESS_SUNLOCK();
1871 }
1872 /*
1873 * Process the user's data in at most OBUFSIZ chunks. Perform any
1874 * output translation. Keep track of high water mark, sleep on
1875 * overflow awaiting device aid in acquiring new space.
1876 */
1877 while (uio->uio_resid > 0 || cc > 0) {
1878 if (ISSET(tp->t_lflag, FLUSHO)) {
1879 uio->uio_resid = 0;
1880 return (0);
1881 }
1882 if (tp->t_outq.c_cc > hiwat)
1883 goto ovhiwat;
1884 /*
1885 * Grab a hunk of data from the user, unless we have some
1886 * leftover from last time.
1887 */
1888 if (cc == 0) {
1889 cc = imin(uio->uio_resid, OBUFSIZ);
1890 cp = obuf;
1891 error = uiomove(cp, cc, uio);
1892 if (error) {
1893 cc = 0;
1894 break;
1895 }
1896 }
1897 /*
1898 * If nothing fancy need be done, grab those characters we
1899 * can handle without any of ttyoutput's processing and
1900 * just transfer them to the output q. For those chars
1901 * which require special processing (as indicated by the
1902 * bits in char_type), call ttyoutput. After processing
1903 * a hunk of data, look for FLUSHO so ^O's will take effect
1904 * immediately.
1905 */
1906 while (cc > 0) {
1907 if (!ISSET(tp->t_oflag, OPOST))
1908 ce = cc;
1909 else {
1910 ce = cc - scanc((u_int)cc, (u_char *)cp,
1911 char_type, CCLASSMASK);
1912 /*
1913 * If ce is zero, then we're processing
1914 * a special character through ttyoutput.
1915 */
1916 if (ce == 0) {
1917 tp->t_rocount = 0;
1918 if (ttyoutput(*cp, tp) >= 0) {
1919 /* No Clists, wait a bit. */
1920 ttstart(tp);
1921 if (flag & IO_NDELAY) {
1922 error = EWOULDBLOCK;
1923 goto out;
1924 }
1925 error = ttysleep(tp, &lbolt,
1926 TTOPRI|PCATCH,
1927 "ttybf1", 0);
1928 if (error)
1929 goto out;
1930 goto loop;
1931 }
1932 cp++;
1933 cc--;
1934 if (ISSET(tp->t_lflag, FLUSHO) ||
1935 tp->t_outq.c_cc > hiwat)
1936 goto ovhiwat;
1937 continue;
1938 }
1939 }
1940 /*
1941 * A bunch of normal characters have been found.
1942 * Transfer them en masse to the output queue and
1943 * continue processing at the top of the loop.
1944 * If there are any further characters in this
1945 * <= OBUFSIZ chunk, the first should be a character
1946 * requiring special handling by ttyoutput.
1947 */
1948 tp->t_rocount = 0;
1949 i = b_to_q(cp, ce, &tp->t_outq);
1950 ce -= i;
1951 tp->t_column += ce;
1952 cp += ce, cc -= ce, tk_nout += ce;
1953 tp->t_outcc += ce;
1954 if (i > 0) {
1955 /* No Clists, wait a bit. */
1956 ttstart(tp);
1957 if (flag & IO_NDELAY) {
1958 error = EWOULDBLOCK;
1959 goto out;
1960 }
1961 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH,
1962 "ttybf2", 0);
1963 if (error)
1964 goto out;
1965 goto loop;
1966 }
1967 if (ISSET(tp->t_lflag, FLUSHO) ||
1968 tp->t_outq.c_cc > hiwat)
1969 break;
1970 }
1971 ttstart(tp);
1972 }
1973out:
1974 /*
1975 * If cc is nonzero, we leave the uio structure inconsistent, as the
1976 * offset and iov pointers have moved forward, but it doesn't matter
1977 * (the call will either return short or restart with a new uio).
1978 */
1979 uio->uio_resid += cc;
1980 return (error);
1981
1982ovhiwat:
1983 ttstart(tp);
1984 s = spltty();
1985 /*
1986 * This can only occur if FLUSHO is set in t_lflag,
1987 * or if ttstart/oproc is synchronous (or very fast).
1988 */
1989 if (tp->t_outq.c_cc <= hiwat) {
1990 splx(s);
1991 goto loop;
1992 }
1993 if (flag & IO_NDELAY) {
1994 splx(s);
1995 uio->uio_resid += cc;
1996 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1997 }
1998 SET(tp->t_state, TS_SO_OLOWAT);
1999 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri",
2000 tp->t_timeout);
2001 splx(s);
2002 if (error == EWOULDBLOCK)
2003 error = EIO;
2004 if (error)
2005 goto out;
2006 goto loop;
2007}
2008
2009/*
2010 * Rubout one character from the rawq of tp
2011 * as cleanly as possible.
2012 */
2013static void
2014ttyrub(c, tp)
2015 register int c;
2016 register struct tty *tp;
2017{
2018 register char *cp;
2019 register int savecol;
2020 int tabc, s;
2021
2022 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
2023 return;
2024 CLR(tp->t_lflag, FLUSHO);
2025 if (ISSET(tp->t_lflag, ECHOE)) {
2026 if (tp->t_rocount == 0) {
2027 /*
2028 * Screwed by ttwrite; retype
2029 */
2030 ttyretype(tp);
2031 return;
2032 }
2033 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
2034 ttyrubo(tp, 2);
2035 else {
2036 CLR(c, ~TTY_CHARMASK);
2037 switch (CCLASS(c)) {
2038 case ORDINARY:
2039 ttyrubo(tp, 1);
2040 break;
2041 case BACKSPACE:
2042 case CONTROL:
2043 case NEWLINE:
2044 case RETURN:
2045 case VTAB:
2046 if (ISSET(tp->t_lflag, ECHOCTL))
2047 ttyrubo(tp, 2);
2048 break;
2049 case TAB:
2050 if (tp->t_rocount < tp->t_rawq.c_cc) {
2051 ttyretype(tp);
2052 return;
2053 }
2054 s = spltty();
2055 savecol = tp->t_column;
2056 SET(tp->t_state, TS_CNTTB);
2057 SET(tp->t_lflag, FLUSHO);
2058 tp->t_column = tp->t_rocol;
2059 cp = tp->t_rawq.c_cf;
2060 if (cp)
2061 tabc = *cp; /* XXX FIX NEXTC */
2062 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc))
2063 ttyecho(tabc, tp);
2064 CLR(tp->t_lflag, FLUSHO);
2065 CLR(tp->t_state, TS_CNTTB);
2066 splx(s);
2067
2068 /* savecol will now be length of the tab. */
2069 savecol -= tp->t_column;
2070 tp->t_column += savecol;
2071 if (savecol > 8)
2072 savecol = 8; /* overflow screw */
2073 while (--savecol >= 0)
2074 (void)ttyoutput('\b', tp);
2075 break;
2076 default: /* XXX */
2077#define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2078 (void)printf(PANICSTR, c, CCLASS(c));
2079#ifdef notdef
2080 panic(PANICSTR, c, CCLASS(c));
2081#endif
2082 }
2083 }
2084 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
2085 if (!ISSET(tp->t_state, TS_ERASE)) {
2086 SET(tp->t_state, TS_ERASE);
2087 (void)ttyoutput('\\', tp);
2088 }
2089 ttyecho(c, tp);
2090 } else {
2091 ttyecho(tp->t_cc[VERASE], tp);
2092 /*
2093 * This code may be executed not only when an ERASE key
2094 * is pressed, but also when ^U (KILL) or ^W (WERASE) are.
2095 * So, I didn't think it was worthwhile to pass the extra
2096 * information (which would need an extra parameter,
2097 * changing every call) needed to distinguish the ERASE2
2098 * case from the ERASE.
2099 */
2100 }
2101 --tp->t_rocount;
2102}
2103
2104/*
2105 * Back over cnt characters, erasing them.
2106 */
2107static void
2108ttyrubo(tp, cnt)
2109 register struct tty *tp;
2110 int cnt;
2111{
2112
2113 while (cnt-- > 0) {
2114 (void)ttyoutput('\b', tp);
2115 (void)ttyoutput(' ', tp);
2116 (void)ttyoutput('\b', tp);
2117 }
2118}
2119
2120/*
2121 * ttyretype --
2122 * Reprint the rawq line. Note, it is assumed that c_cc has already
2123 * been checked.
2124 */
2125static void
2126ttyretype(tp)
2127 register struct tty *tp;
2128{
2129 register char *cp;
2130 int s, c;
2131
2132 /* Echo the reprint character. */
2133 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
2134 ttyecho(tp->t_cc[VREPRINT], tp);
2135
2136 (void)ttyoutput('\n', tp);
2137
2138 /*
2139 * XXX
2140 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2141 * BIT OF FIRST CHAR.
2142 */
2143 s = spltty();
2144 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0);
2145 cp != NULL; cp = nextc(&tp->t_canq, cp, &c))
2146 ttyecho(c, tp);
2147 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0);
2148 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c))
2149 ttyecho(c, tp);
2150 CLR(tp->t_state, TS_ERASE);
2151 splx(s);
2152
2153 tp->t_rocount = tp->t_rawq.c_cc;
2154 tp->t_rocol = 0;
2155}
2156
2157/*
2158 * Echo a typed character to the terminal.
2159 */
2160static void
2161ttyecho(c, tp)
2162 register int c;
2163 register struct tty *tp;
2164{
2165
2166 if (!ISSET(tp->t_state, TS_CNTTB))
2167 CLR(tp->t_lflag, FLUSHO);
2168 if ((!ISSET(tp->t_lflag, ECHO) &&
2169 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) ||
2170 ISSET(tp->t_lflag, EXTPROC))
2171 return;
2172 if (ISSET(tp->t_lflag, ECHOCTL) &&
2173 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') ||
2174 ISSET(c, TTY_CHARMASK) == 0177)) {
2175 (void)ttyoutput('^', tp);
2176 CLR(c, ~TTY_CHARMASK);
2177 if (c == 0177)
2178 c = '?';
2179 else
2180 c += 'A' - 1;
2181 }
2182 (void)ttyoutput(c, tp);
2183}
2184
2185/*
2186 * Wake up any readers on a tty.
2187 */
2188void
2189ttwakeup(tp)
2190 register struct tty *tp;
2191{
2192
2193 if (tp->t_rsel.si_pid != 0)
2194 selwakeup(&tp->t_rsel);
2195 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2196 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL));
2197 wakeup(TSA_HUP_OR_INPUT(tp));
2198 KNOTE(&tp->t_rsel.si_note, 0);
2199}
2200
2201/*
2202 * Wake up any writers on a tty.
2203 */
2204void
2205ttwwakeup(tp)
2206 register struct tty *tp;
2207{
2208
2209 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat)
2210 selwakeup(&tp->t_wsel);
2211 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2212 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL));
2213 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
2214 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
2215 CLR(tp->t_state, TS_SO_OCOMPLETE);
2216 wakeup(TSA_OCOMPLETE(tp));
2217 }
2218 if (ISSET(tp->t_state, TS_SO_OLOWAT) &&
2219 tp->t_outq.c_cc <= tp->t_olowat) {
2220 CLR(tp->t_state, TS_SO_OLOWAT);
2221 wakeup(TSA_OLOWAT(tp));
2222 }
2223 KNOTE(&tp->t_wsel.si_note, 0);
2224}
2225
2226/*
2227 * Look up a code for a specified speed in a conversion table;
2228 * used by drivers to map software speed values to hardware parameters.
2229 */
2230int
2231ttspeedtab(speed, table)
2232 int speed;
2233 register struct speedtab *table;
2234{
2235
2236 for ( ; table->sp_speed != -1; table++)
2237 if (table->sp_speed == speed)
2238 return (table->sp_code);
2239 return (-1);
2240}
2241
2242/*
2243 * Set input and output watermarks and buffer sizes. For input, the
2244 * high watermark is about one second's worth of input above empty, the
2245 * low watermark is slightly below high water, and the buffer size is a
2246 * driver-dependent amount above high water. For output, the watermarks
2247 * are near the ends of the buffer, with about 1 second's worth of input
2248 * between them. All this only applies to the standard line discipline.
2249 */
2250void
2251ttsetwater(tp)
2252 struct tty *tp;
2253{
2254 register int cps, ttmaxhiwat, x;
2255
2256 /* Input. */
2257 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512);
2258 switch (tp->t_ispeedwat) {
2259 case (speed_t)-1:
2260 cps = tp->t_ispeed / 10;
2261 break;
2262 case 0:
2263 /*
2264 * This case is for old drivers that don't know about
2265 * t_ispeedwat. Arrange for them to get the old buffer
2266 * sizes and watermarks.
2267 */
2268 cps = TTYHOG - 2 * 256;
2269 tp->t_ififosize = 2 * 256;
2270 break;
2271 default:
2272 cps = tp->t_ispeedwat / 10;
2273 break;
2274 }
2275 tp->t_ihiwat = cps;
2276 tp->t_ilowat = 7 * cps / 8;
2277 x = cps + tp->t_ififosize;
2278 clist_alloc_cblocks(&tp->t_rawq, x, x);
2279
2280 /* Output. */
2281 switch (tp->t_ospeedwat) {
2282 case (speed_t)-1:
2283 cps = tp->t_ospeed / 10;
2284 ttmaxhiwat = 2 * TTMAXHIWAT;
2285 break;
2286 case 0:
2287 cps = tp->t_ospeed / 10;
2288 ttmaxhiwat = TTMAXHIWAT;
2289 break;
2290 default:
2291 cps = tp->t_ospeedwat / 10;
2292 ttmaxhiwat = 8 * TTMAXHIWAT;
2293 break;
2294 }
2295#define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2296 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
2297 x += cps;
2298 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */
2299 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */
2300 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */
2301 x += OBUFSIZ + 100;
2302 clist_alloc_cblocks(&tp->t_outq, x, x);
2303#undef CLAMP
2304}
2305
2306/*
2307 * Report on state of foreground process group.
2308 */
2309void
2310ttyinfo(tp)
2311 register struct tty *tp;
2312{
2313 register struct proc *p, *pick;
2314 struct timeval utime, stime;
2315 const char *stmp;
2316 long ltmp;
2317 int tmp;
2318 struct thread *td;
2319
2320 if (ttycheckoutq(tp,0) == 0)
2321 return;
2322
2323 /* Print load average. */
2324 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
2325 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
2326
2327 if (tp->t_session == NULL)
2328 ttyprintf(tp, "not a controlling terminal\n");
2329 else if (tp->t_pgrp == NULL)
2330 ttyprintf(tp, "no foreground process group\n");
1954 }
1955 /*
1956 * Process the user's data in at most OBUFSIZ chunks. Perform any
1957 * output translation. Keep track of high water mark, sleep on
1958 * overflow awaiting device aid in acquiring new space.
1959 */
1960 while (uio->uio_resid > 0 || cc > 0) {
1961 if (ISSET(tp->t_lflag, FLUSHO)) {
1962 uio->uio_resid = 0;
1963 return (0);
1964 }
1965 if (tp->t_outq.c_cc > hiwat)
1966 goto ovhiwat;
1967 /*
1968 * Grab a hunk of data from the user, unless we have some
1969 * leftover from last time.
1970 */
1971 if (cc == 0) {
1972 cc = imin(uio->uio_resid, OBUFSIZ);
1973 cp = obuf;
1974 error = uiomove(cp, cc, uio);
1975 if (error) {
1976 cc = 0;
1977 break;
1978 }
1979 }
1980 /*
1981 * If nothing fancy need be done, grab those characters we
1982 * can handle without any of ttyoutput's processing and
1983 * just transfer them to the output q. For those chars
1984 * which require special processing (as indicated by the
1985 * bits in char_type), call ttyoutput. After processing
1986 * a hunk of data, look for FLUSHO so ^O's will take effect
1987 * immediately.
1988 */
1989 while (cc > 0) {
1990 if (!ISSET(tp->t_oflag, OPOST))
1991 ce = cc;
1992 else {
1993 ce = cc - scanc((u_int)cc, (u_char *)cp,
1994 char_type, CCLASSMASK);
1995 /*
1996 * If ce is zero, then we're processing
1997 * a special character through ttyoutput.
1998 */
1999 if (ce == 0) {
2000 tp->t_rocount = 0;
2001 if (ttyoutput(*cp, tp) >= 0) {
2002 /* No Clists, wait a bit. */
2003 ttstart(tp);
2004 if (flag & IO_NDELAY) {
2005 error = EWOULDBLOCK;
2006 goto out;
2007 }
2008 error = ttysleep(tp, &lbolt,
2009 TTOPRI|PCATCH,
2010 "ttybf1", 0);
2011 if (error)
2012 goto out;
2013 goto loop;
2014 }
2015 cp++;
2016 cc--;
2017 if (ISSET(tp->t_lflag, FLUSHO) ||
2018 tp->t_outq.c_cc > hiwat)
2019 goto ovhiwat;
2020 continue;
2021 }
2022 }
2023 /*
2024 * A bunch of normal characters have been found.
2025 * Transfer them en masse to the output queue and
2026 * continue processing at the top of the loop.
2027 * If there are any further characters in this
2028 * <= OBUFSIZ chunk, the first should be a character
2029 * requiring special handling by ttyoutput.
2030 */
2031 tp->t_rocount = 0;
2032 i = b_to_q(cp, ce, &tp->t_outq);
2033 ce -= i;
2034 tp->t_column += ce;
2035 cp += ce, cc -= ce, tk_nout += ce;
2036 tp->t_outcc += ce;
2037 if (i > 0) {
2038 /* No Clists, wait a bit. */
2039 ttstart(tp);
2040 if (flag & IO_NDELAY) {
2041 error = EWOULDBLOCK;
2042 goto out;
2043 }
2044 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH,
2045 "ttybf2", 0);
2046 if (error)
2047 goto out;
2048 goto loop;
2049 }
2050 if (ISSET(tp->t_lflag, FLUSHO) ||
2051 tp->t_outq.c_cc > hiwat)
2052 break;
2053 }
2054 ttstart(tp);
2055 }
2056out:
2057 /*
2058 * If cc is nonzero, we leave the uio structure inconsistent, as the
2059 * offset and iov pointers have moved forward, but it doesn't matter
2060 * (the call will either return short or restart with a new uio).
2061 */
2062 uio->uio_resid += cc;
2063 return (error);
2064
2065ovhiwat:
2066 ttstart(tp);
2067 s = spltty();
2068 /*
2069 * This can only occur if FLUSHO is set in t_lflag,
2070 * or if ttstart/oproc is synchronous (or very fast).
2071 */
2072 if (tp->t_outq.c_cc <= hiwat) {
2073 splx(s);
2074 goto loop;
2075 }
2076 if (flag & IO_NDELAY) {
2077 splx(s);
2078 uio->uio_resid += cc;
2079 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
2080 }
2081 SET(tp->t_state, TS_SO_OLOWAT);
2082 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri",
2083 tp->t_timeout);
2084 splx(s);
2085 if (error == EWOULDBLOCK)
2086 error = EIO;
2087 if (error)
2088 goto out;
2089 goto loop;
2090}
2091
2092/*
2093 * Rubout one character from the rawq of tp
2094 * as cleanly as possible.
2095 */
2096static void
2097ttyrub(c, tp)
2098 register int c;
2099 register struct tty *tp;
2100{
2101 register char *cp;
2102 register int savecol;
2103 int tabc, s;
2104
2105 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
2106 return;
2107 CLR(tp->t_lflag, FLUSHO);
2108 if (ISSET(tp->t_lflag, ECHOE)) {
2109 if (tp->t_rocount == 0) {
2110 /*
2111 * Screwed by ttwrite; retype
2112 */
2113 ttyretype(tp);
2114 return;
2115 }
2116 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
2117 ttyrubo(tp, 2);
2118 else {
2119 CLR(c, ~TTY_CHARMASK);
2120 switch (CCLASS(c)) {
2121 case ORDINARY:
2122 ttyrubo(tp, 1);
2123 break;
2124 case BACKSPACE:
2125 case CONTROL:
2126 case NEWLINE:
2127 case RETURN:
2128 case VTAB:
2129 if (ISSET(tp->t_lflag, ECHOCTL))
2130 ttyrubo(tp, 2);
2131 break;
2132 case TAB:
2133 if (tp->t_rocount < tp->t_rawq.c_cc) {
2134 ttyretype(tp);
2135 return;
2136 }
2137 s = spltty();
2138 savecol = tp->t_column;
2139 SET(tp->t_state, TS_CNTTB);
2140 SET(tp->t_lflag, FLUSHO);
2141 tp->t_column = tp->t_rocol;
2142 cp = tp->t_rawq.c_cf;
2143 if (cp)
2144 tabc = *cp; /* XXX FIX NEXTC */
2145 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc))
2146 ttyecho(tabc, tp);
2147 CLR(tp->t_lflag, FLUSHO);
2148 CLR(tp->t_state, TS_CNTTB);
2149 splx(s);
2150
2151 /* savecol will now be length of the tab. */
2152 savecol -= tp->t_column;
2153 tp->t_column += savecol;
2154 if (savecol > 8)
2155 savecol = 8; /* overflow screw */
2156 while (--savecol >= 0)
2157 (void)ttyoutput('\b', tp);
2158 break;
2159 default: /* XXX */
2160#define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2161 (void)printf(PANICSTR, c, CCLASS(c));
2162#ifdef notdef
2163 panic(PANICSTR, c, CCLASS(c));
2164#endif
2165 }
2166 }
2167 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
2168 if (!ISSET(tp->t_state, TS_ERASE)) {
2169 SET(tp->t_state, TS_ERASE);
2170 (void)ttyoutput('\\', tp);
2171 }
2172 ttyecho(c, tp);
2173 } else {
2174 ttyecho(tp->t_cc[VERASE], tp);
2175 /*
2176 * This code may be executed not only when an ERASE key
2177 * is pressed, but also when ^U (KILL) or ^W (WERASE) are.
2178 * So, I didn't think it was worthwhile to pass the extra
2179 * information (which would need an extra parameter,
2180 * changing every call) needed to distinguish the ERASE2
2181 * case from the ERASE.
2182 */
2183 }
2184 --tp->t_rocount;
2185}
2186
2187/*
2188 * Back over cnt characters, erasing them.
2189 */
2190static void
2191ttyrubo(tp, cnt)
2192 register struct tty *tp;
2193 int cnt;
2194{
2195
2196 while (cnt-- > 0) {
2197 (void)ttyoutput('\b', tp);
2198 (void)ttyoutput(' ', tp);
2199 (void)ttyoutput('\b', tp);
2200 }
2201}
2202
2203/*
2204 * ttyretype --
2205 * Reprint the rawq line. Note, it is assumed that c_cc has already
2206 * been checked.
2207 */
2208static void
2209ttyretype(tp)
2210 register struct tty *tp;
2211{
2212 register char *cp;
2213 int s, c;
2214
2215 /* Echo the reprint character. */
2216 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
2217 ttyecho(tp->t_cc[VREPRINT], tp);
2218
2219 (void)ttyoutput('\n', tp);
2220
2221 /*
2222 * XXX
2223 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2224 * BIT OF FIRST CHAR.
2225 */
2226 s = spltty();
2227 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0);
2228 cp != NULL; cp = nextc(&tp->t_canq, cp, &c))
2229 ttyecho(c, tp);
2230 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0);
2231 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c))
2232 ttyecho(c, tp);
2233 CLR(tp->t_state, TS_ERASE);
2234 splx(s);
2235
2236 tp->t_rocount = tp->t_rawq.c_cc;
2237 tp->t_rocol = 0;
2238}
2239
2240/*
2241 * Echo a typed character to the terminal.
2242 */
2243static void
2244ttyecho(c, tp)
2245 register int c;
2246 register struct tty *tp;
2247{
2248
2249 if (!ISSET(tp->t_state, TS_CNTTB))
2250 CLR(tp->t_lflag, FLUSHO);
2251 if ((!ISSET(tp->t_lflag, ECHO) &&
2252 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) ||
2253 ISSET(tp->t_lflag, EXTPROC))
2254 return;
2255 if (ISSET(tp->t_lflag, ECHOCTL) &&
2256 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') ||
2257 ISSET(c, TTY_CHARMASK) == 0177)) {
2258 (void)ttyoutput('^', tp);
2259 CLR(c, ~TTY_CHARMASK);
2260 if (c == 0177)
2261 c = '?';
2262 else
2263 c += 'A' - 1;
2264 }
2265 (void)ttyoutput(c, tp);
2266}
2267
2268/*
2269 * Wake up any readers on a tty.
2270 */
2271void
2272ttwakeup(tp)
2273 register struct tty *tp;
2274{
2275
2276 if (tp->t_rsel.si_pid != 0)
2277 selwakeup(&tp->t_rsel);
2278 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2279 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL));
2280 wakeup(TSA_HUP_OR_INPUT(tp));
2281 KNOTE(&tp->t_rsel.si_note, 0);
2282}
2283
2284/*
2285 * Wake up any writers on a tty.
2286 */
2287void
2288ttwwakeup(tp)
2289 register struct tty *tp;
2290{
2291
2292 if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat)
2293 selwakeup(&tp->t_wsel);
2294 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2295 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL));
2296 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
2297 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
2298 CLR(tp->t_state, TS_SO_OCOMPLETE);
2299 wakeup(TSA_OCOMPLETE(tp));
2300 }
2301 if (ISSET(tp->t_state, TS_SO_OLOWAT) &&
2302 tp->t_outq.c_cc <= tp->t_olowat) {
2303 CLR(tp->t_state, TS_SO_OLOWAT);
2304 wakeup(TSA_OLOWAT(tp));
2305 }
2306 KNOTE(&tp->t_wsel.si_note, 0);
2307}
2308
2309/*
2310 * Look up a code for a specified speed in a conversion table;
2311 * used by drivers to map software speed values to hardware parameters.
2312 */
2313int
2314ttspeedtab(speed, table)
2315 int speed;
2316 register struct speedtab *table;
2317{
2318
2319 for ( ; table->sp_speed != -1; table++)
2320 if (table->sp_speed == speed)
2321 return (table->sp_code);
2322 return (-1);
2323}
2324
2325/*
2326 * Set input and output watermarks and buffer sizes. For input, the
2327 * high watermark is about one second's worth of input above empty, the
2328 * low watermark is slightly below high water, and the buffer size is a
2329 * driver-dependent amount above high water. For output, the watermarks
2330 * are near the ends of the buffer, with about 1 second's worth of input
2331 * between them. All this only applies to the standard line discipline.
2332 */
2333void
2334ttsetwater(tp)
2335 struct tty *tp;
2336{
2337 register int cps, ttmaxhiwat, x;
2338
2339 /* Input. */
2340 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512);
2341 switch (tp->t_ispeedwat) {
2342 case (speed_t)-1:
2343 cps = tp->t_ispeed / 10;
2344 break;
2345 case 0:
2346 /*
2347 * This case is for old drivers that don't know about
2348 * t_ispeedwat. Arrange for them to get the old buffer
2349 * sizes and watermarks.
2350 */
2351 cps = TTYHOG - 2 * 256;
2352 tp->t_ififosize = 2 * 256;
2353 break;
2354 default:
2355 cps = tp->t_ispeedwat / 10;
2356 break;
2357 }
2358 tp->t_ihiwat = cps;
2359 tp->t_ilowat = 7 * cps / 8;
2360 x = cps + tp->t_ififosize;
2361 clist_alloc_cblocks(&tp->t_rawq, x, x);
2362
2363 /* Output. */
2364 switch (tp->t_ospeedwat) {
2365 case (speed_t)-1:
2366 cps = tp->t_ospeed / 10;
2367 ttmaxhiwat = 2 * TTMAXHIWAT;
2368 break;
2369 case 0:
2370 cps = tp->t_ospeed / 10;
2371 ttmaxhiwat = TTMAXHIWAT;
2372 break;
2373 default:
2374 cps = tp->t_ospeedwat / 10;
2375 ttmaxhiwat = 8 * TTMAXHIWAT;
2376 break;
2377 }
2378#define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2379 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
2380 x += cps;
2381 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */
2382 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */
2383 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */
2384 x += OBUFSIZ + 100;
2385 clist_alloc_cblocks(&tp->t_outq, x, x);
2386#undef CLAMP
2387}
2388
2389/*
2390 * Report on state of foreground process group.
2391 */
2392void
2393ttyinfo(tp)
2394 register struct tty *tp;
2395{
2396 register struct proc *p, *pick;
2397 struct timeval utime, stime;
2398 const char *stmp;
2399 long ltmp;
2400 int tmp;
2401 struct thread *td;
2402
2403 if (ttycheckoutq(tp,0) == 0)
2404 return;
2405
2406 /* Print load average. */
2407 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
2408 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
2409
2410 if (tp->t_session == NULL)
2411 ttyprintf(tp, "not a controlling terminal\n");
2412 else if (tp->t_pgrp == NULL)
2413 ttyprintf(tp, "no foreground process group\n");
2331 else if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0)
2332 ttyprintf(tp, "empty foreground process group\n");
2333 else {
2414 else {
2334 mtx_lock_spin(&sched_lock);
2415 PGRP_LOCK(tp->t_pgrp);
2416 if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0) {
2417 PGRP_UNLOCK(tp->t_pgrp);
2418 ttyprintf(tp, "empty foreground process group\n");
2419 } else {
2420 PGRP_UNLOCK(tp->t_pgrp);
2421 mtx_lock_spin(&sched_lock);
2335
2422
2336 /* Pick interesting process. */
2337 for (pick = NULL; p != 0; p = LIST_NEXT(p, p_pglist))
2338 if (proc_compare(pick, p))
2339 pick = p;
2423 /* Pick interesting process. */
2424 for (pick = NULL; p != 0; p = LIST_NEXT(p, p_pglist))
2425 if (proc_compare(pick, p))
2426 pick = p;
2340
2427
2341 td = FIRST_THREAD_IN_PROC(pick);
2342 stmp = pick->p_stat == SRUN ? "running" : /* XXXKSE */
2343 td->td_wmesg ? td->td_wmesg : "iowait";
2344 calcru(pick, &utime, &stime, NULL);
2345 ltmp = pick->p_stat == SIDL || pick->p_stat == SWAIT ||
2346 pick->p_stat == SZOMB ? 0 :
2347 pgtok(vmspace_resident_count(pick->p_vmspace));
2348 mtx_unlock_spin(&sched_lock);
2428 td = FIRST_THREAD_IN_PROC(pick);
2429 stmp = pick->p_stat == SRUN ? "running" : /* XXXKSE */
2430 td->td_wmesg ? td->td_wmesg : "iowait";
2431 calcru(pick, &utime, &stime, NULL);
2432 ltmp = pick->p_stat == SIDL || pick->p_stat == SWAIT ||
2433 pick->p_stat == SZOMB ? 0 :
2434 pgtok(vmspace_resident_count(pick->p_vmspace));
2435 mtx_unlock_spin(&sched_lock);
2349
2436
2350 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
2351 stmp);
2437 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
2438 stmp);
2352
2439
2353 /* Print user time. */
2354 ttyprintf(tp, "%ld.%02ldu ",
2355 (long)utime.tv_sec, utime.tv_usec / 10000);
2440 /* Print user time. */
2441 ttyprintf(tp, "%lld.%02ldu ",
2442 utime.tv_sec, utime.tv_usec / 10000);
2356
2443
2357 /* Print system time. */
2358 ttyprintf(tp, "%ld.%02lds ",
2359 (long)stime.tv_sec, stime.tv_usec / 10000);
2444 /* Print system time. */
2445 ttyprintf(tp, "%ld.%02lds ",
2446 (long)stime.tv_sec, stime.tv_usec / 10000);
2360
2447
2361 /* Print percentage cpu, resident set size. */
2362 ttyprintf(tp, "%d%% %ldk\n", tmp / 100, ltmp);
2448 /* Print percentage cpu, resident set size. */
2449 ttyprintf(tp, "%d%% %ldk\n", tmp / 100, ltmp);
2450
2451 }
2363 }
2364 tp->t_rocount = 0; /* so pending input will be retyped if BS */
2365}
2366
2367/*
2368 * Returns 1 if p2 is "better" than p1
2369 *
2370 * The algorithm for picking the "interesting" process is thus:
2371 *
2372 * 1) Only foreground processes are eligible - implied.
2373 * 2) Runnable processes are favored over anything else. The runner
2374 * with the highest cpu utilization is picked (p_estcpu). Ties are
2375 * broken by picking the highest pid.
2376 * 3) The sleeper with the shortest sleep time is next. With ties,
2377 * we pick out just "short-term" sleepers (P_SINTR == 0).
2378 * 4) Further ties are broken by picking the highest pid.
2379 */
2380#define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2381#define TESTAB(a, b) ((a)<<1 | (b))
2382#define ONLYA 2
2383#define ONLYB 1
2384#define BOTH 3
2385
2386static int
2387proc_compare(p1, p2)
2388 register struct proc *p1, *p2;
2389{
2390
2391 int esta, estb;
2392 struct ksegrp *kg;
2393 mtx_assert(&sched_lock, MA_OWNED);
2394 if (p1 == NULL)
2395 return (1);
2396
2397 /*
2398 * see if at least one of them is runnable
2399 */
2400 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2401 case ONLYA:
2402 return (0);
2403 case ONLYB:
2404 return (1);
2405 case BOTH:
2406 /*
2407 * tie - favor one with highest recent cpu utilization
2408 */
2409 esta = estb = 0;
2410 FOREACH_KSEGRP_IN_PROC(p1,kg) {
2411 esta += kg->kg_estcpu;
2412 }
2413 FOREACH_KSEGRP_IN_PROC(p2,kg) {
2414 estb += kg->kg_estcpu;
2415 }
2416 if (estb > esta)
2417 return (1);
2418 if (esta > estb)
2419 return (0);
2420 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2421 }
2422 /*
2423 * weed out zombies
2424 */
2425 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
2426 case ONLYA:
2427 return (1);
2428 case ONLYB:
2429 return (0);
2430 case BOTH:
2431 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2432 }
2433
2434#if 0 /* XXXKSE */
2435 /*
2436 * pick the one with the smallest sleep time
2437 */
2438 if (p2->p_slptime > p1->p_slptime)
2439 return (0);
2440 if (p1->p_slptime > p2->p_slptime)
2441 return (1);
2442 /*
2443 * favor one sleeping in a non-interruptible sleep
2444 */
2445 if (p1->p_sflag & PS_SINTR && (p2->p_sflag & PS_SINTR) == 0)
2446 return (1);
2447 if (p2->p_sflag & PS_SINTR && (p1->p_sflag & PS_SINTR) == 0)
2448 return (0);
2449#endif
2450 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2451}
2452
2453/*
2454 * Output char to tty; console putchar style.
2455 */
2456int
2457tputchar(c, tp)
2458 int c;
2459 struct tty *tp;
2460{
2461 register int s;
2462
2463 s = spltty();
2464 if (!ISSET(tp->t_state, TS_CONNECTED)) {
2465 splx(s);
2466 return (-1);
2467 }
2468 if (c == '\n')
2469 (void)ttyoutput('\r', tp);
2470 (void)ttyoutput(c, tp);
2471 ttstart(tp);
2472 splx(s);
2473 return (0);
2474}
2475
2476/*
2477 * Sleep on chan, returning ERESTART if tty changed while we napped and
2478 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2479 * the tty is revoked, restarting a pending call will redo validation done
2480 * at the start of the call.
2481 */
2482int
2483ttysleep(tp, chan, pri, wmesg, timo)
2484 struct tty *tp;
2485 void *chan;
2486 int pri, timo;
2487 char *wmesg;
2488{
2489 int error;
2490 int gen;
2491
2492 gen = tp->t_gen;
2493 error = tsleep(chan, pri, wmesg, timo);
2494 if (error)
2495 return (error);
2496 return (tp->t_gen == gen ? 0 : ERESTART);
2497}
2498
2499/*
2500 * Allocate a tty struct. Clists in the struct will be allocated by
2501 * ttyopen().
2502 */
2503struct tty *
2504ttymalloc(tp)
2505 struct tty *tp;
2506{
2507
2508 if (tp)
2509 return(tp);
2510 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK | M_ZERO);
2511 ttyregister(tp);
2512 return (tp);
2513}
2514
2515#if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */
2516/*
2517 * Free a tty struct. Clists in the struct should have been freed by
2518 * ttyclose().
2519 */
2520void
2521ttyfree(tp)
2522 struct tty *tp;
2523{
2524 free(tp, M_TTYS);
2525}
2526#endif /* 0 */
2527
2528void
2529ttyregister(tp)
2530 struct tty *tp;
2531{
2532 tp->t_timeout = -1;
2533 SLIST_INSERT_HEAD(&tty_list, tp, t_list);
2534}
2535
2536static int
2537sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
2538{
2539 int error;
2540 struct tty *tp, t;
2541 SLIST_FOREACH(tp, &tty_list, t_list) {
2542 t = *tp;
2543 if (t.t_dev)
2544 t.ttyu.t_udev = dev2udev(t.t_dev);
2545 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t));
2546 if (error)
2547 return (error);
2548 }
2549 return (0);
2550}
2551
2552SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD,
2553 0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys");
2554SYSCTL_LONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
2555 &tk_nin, 0, "Total TTY in characters");
2556SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
2557 &tk_nout, 0, "Total TTY out characters");
2558
2559void
2560nottystop(tp, rw)
2561 struct tty *tp;
2562 int rw;
2563{
2564
2565 return;
2566}
2567
2568int
2569ttyread(dev, uio, flag)
2570 dev_t dev;
2571 struct uio *uio;
2572 int flag;
2573{
2574 struct tty *tp;
2575
2576 tp = dev->si_tty;
2577 if (tp == NULL)
2578 return (ENODEV);
2579 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
2580}
2581
2582int
2583ttywrite(dev, uio, flag)
2584 dev_t dev;
2585 struct uio *uio;
2586 int flag;
2587{
2588 struct tty *tp;
2589
2590 tp = dev->si_tty;
2591 if (tp == NULL)
2592 return (ENODEV);
2593 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2594}
2452 }
2453 tp->t_rocount = 0; /* so pending input will be retyped if BS */
2454}
2455
2456/*
2457 * Returns 1 if p2 is "better" than p1
2458 *
2459 * The algorithm for picking the "interesting" process is thus:
2460 *
2461 * 1) Only foreground processes are eligible - implied.
2462 * 2) Runnable processes are favored over anything else. The runner
2463 * with the highest cpu utilization is picked (p_estcpu). Ties are
2464 * broken by picking the highest pid.
2465 * 3) The sleeper with the shortest sleep time is next. With ties,
2466 * we pick out just "short-term" sleepers (P_SINTR == 0).
2467 * 4) Further ties are broken by picking the highest pid.
2468 */
2469#define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2470#define TESTAB(a, b) ((a)<<1 | (b))
2471#define ONLYA 2
2472#define ONLYB 1
2473#define BOTH 3
2474
2475static int
2476proc_compare(p1, p2)
2477 register struct proc *p1, *p2;
2478{
2479
2480 int esta, estb;
2481 struct ksegrp *kg;
2482 mtx_assert(&sched_lock, MA_OWNED);
2483 if (p1 == NULL)
2484 return (1);
2485
2486 /*
2487 * see if at least one of them is runnable
2488 */
2489 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2490 case ONLYA:
2491 return (0);
2492 case ONLYB:
2493 return (1);
2494 case BOTH:
2495 /*
2496 * tie - favor one with highest recent cpu utilization
2497 */
2498 esta = estb = 0;
2499 FOREACH_KSEGRP_IN_PROC(p1,kg) {
2500 esta += kg->kg_estcpu;
2501 }
2502 FOREACH_KSEGRP_IN_PROC(p2,kg) {
2503 estb += kg->kg_estcpu;
2504 }
2505 if (estb > esta)
2506 return (1);
2507 if (esta > estb)
2508 return (0);
2509 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2510 }
2511 /*
2512 * weed out zombies
2513 */
2514 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
2515 case ONLYA:
2516 return (1);
2517 case ONLYB:
2518 return (0);
2519 case BOTH:
2520 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2521 }
2522
2523#if 0 /* XXXKSE */
2524 /*
2525 * pick the one with the smallest sleep time
2526 */
2527 if (p2->p_slptime > p1->p_slptime)
2528 return (0);
2529 if (p1->p_slptime > p2->p_slptime)
2530 return (1);
2531 /*
2532 * favor one sleeping in a non-interruptible sleep
2533 */
2534 if (p1->p_sflag & PS_SINTR && (p2->p_sflag & PS_SINTR) == 0)
2535 return (1);
2536 if (p2->p_sflag & PS_SINTR && (p1->p_sflag & PS_SINTR) == 0)
2537 return (0);
2538#endif
2539 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2540}
2541
2542/*
2543 * Output char to tty; console putchar style.
2544 */
2545int
2546tputchar(c, tp)
2547 int c;
2548 struct tty *tp;
2549{
2550 register int s;
2551
2552 s = spltty();
2553 if (!ISSET(tp->t_state, TS_CONNECTED)) {
2554 splx(s);
2555 return (-1);
2556 }
2557 if (c == '\n')
2558 (void)ttyoutput('\r', tp);
2559 (void)ttyoutput(c, tp);
2560 ttstart(tp);
2561 splx(s);
2562 return (0);
2563}
2564
2565/*
2566 * Sleep on chan, returning ERESTART if tty changed while we napped and
2567 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2568 * the tty is revoked, restarting a pending call will redo validation done
2569 * at the start of the call.
2570 */
2571int
2572ttysleep(tp, chan, pri, wmesg, timo)
2573 struct tty *tp;
2574 void *chan;
2575 int pri, timo;
2576 char *wmesg;
2577{
2578 int error;
2579 int gen;
2580
2581 gen = tp->t_gen;
2582 error = tsleep(chan, pri, wmesg, timo);
2583 if (error)
2584 return (error);
2585 return (tp->t_gen == gen ? 0 : ERESTART);
2586}
2587
2588/*
2589 * Allocate a tty struct. Clists in the struct will be allocated by
2590 * ttyopen().
2591 */
2592struct tty *
2593ttymalloc(tp)
2594 struct tty *tp;
2595{
2596
2597 if (tp)
2598 return(tp);
2599 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK | M_ZERO);
2600 ttyregister(tp);
2601 return (tp);
2602}
2603
2604#if 0 /* XXX not yet usable: session leader holds a ref (see kern_exit.c). */
2605/*
2606 * Free a tty struct. Clists in the struct should have been freed by
2607 * ttyclose().
2608 */
2609void
2610ttyfree(tp)
2611 struct tty *tp;
2612{
2613 free(tp, M_TTYS);
2614}
2615#endif /* 0 */
2616
2617void
2618ttyregister(tp)
2619 struct tty *tp;
2620{
2621 tp->t_timeout = -1;
2622 SLIST_INSERT_HEAD(&tty_list, tp, t_list);
2623}
2624
2625static int
2626sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
2627{
2628 int error;
2629 struct tty *tp, t;
2630 SLIST_FOREACH(tp, &tty_list, t_list) {
2631 t = *tp;
2632 if (t.t_dev)
2633 t.ttyu.t_udev = dev2udev(t.t_dev);
2634 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t));
2635 if (error)
2636 return (error);
2637 }
2638 return (0);
2639}
2640
2641SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD,
2642 0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys");
2643SYSCTL_LONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
2644 &tk_nin, 0, "Total TTY in characters");
2645SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
2646 &tk_nout, 0, "Total TTY out characters");
2647
2648void
2649nottystop(tp, rw)
2650 struct tty *tp;
2651 int rw;
2652{
2653
2654 return;
2655}
2656
2657int
2658ttyread(dev, uio, flag)
2659 dev_t dev;
2660 struct uio *uio;
2661 int flag;
2662{
2663 struct tty *tp;
2664
2665 tp = dev->si_tty;
2666 if (tp == NULL)
2667 return (ENODEV);
2668 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
2669}
2670
2671int
2672ttywrite(dev, uio, flag)
2673 dev_t dev;
2674 struct uio *uio;
2675 int flag;
2676{
2677 struct tty *tp;
2678
2679 tp = dev->si_tty;
2680 if (tp == NULL)
2681 return (ENODEV);
2682 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2683}