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