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