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