tty.c revision 1.61 1 /* $NetBSD: tty.c,v 1.61 1995/07/02 18:13:02 mycroft 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) || defined(COMPAT_SVR4)
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 u_long cmd;
624 caddr_t data;
625 int flag;
626 struct proc *p;
627 {
628 extern struct tty *constty; /* Temporary virtual console. */
629 extern int nlinesw;
630 int s, error;
631
632 /* If the ioctl involves modification, hang if in the background. */
633 switch (cmd) {
634 case TIOCFLUSH:
635 case TIOCSETA:
636 case TIOCSETD:
637 case TIOCSETAF:
638 case TIOCSETAW:
639 #ifdef notdef
640 case TIOCSPGRP:
641 #endif
642 case TIOCSTAT:
643 case TIOCSTI:
644 case TIOCSWINSZ:
645 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_SVR4)
646 case TIOCLBIC:
647 case TIOCLBIS:
648 case TIOCLSET:
649 case TIOCSETC:
650 case OTIOCSETD:
651 case TIOCSETN:
652 case TIOCSETP:
653 case TIOCSLTC:
654 #endif
655 while (isbackground(curproc, tp) &&
656 p->p_pgrp->pg_jobc && (p->p_flag & P_PPWAIT) == 0 &&
657 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
658 (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
659 pgsignal(p->p_pgrp, SIGTTOU, 1);
660 if (error = ttysleep(tp,
661 &lbolt, TTOPRI | PCATCH, ttybg, 0))
662 return (error);
663 }
664 break;
665 }
666
667 switch (cmd) { /* Process the ioctl. */
668 case FIOASYNC: /* set/clear async i/o */
669 s = spltty();
670 if (*(int *)data)
671 SET(tp->t_state, TS_ASYNC);
672 else
673 CLR(tp->t_state, TS_ASYNC);
674 splx(s);
675 break;
676 case FIONBIO: /* set/clear non-blocking i/o */
677 break; /* XXX: delete. */
678 case FIONREAD: /* get # bytes to read */
679 *(int *)data = ttnread(tp);
680 break;
681 case TIOCEXCL: /* set exclusive use of tty */
682 s = spltty();
683 SET(tp->t_state, TS_XCLUDE);
684 splx(s);
685 break;
686 case TIOCFLUSH: { /* flush buffers */
687 register int flags = *(int *)data;
688
689 if (flags == 0)
690 flags = FREAD | FWRITE;
691 else
692 flags &= FREAD | FWRITE;
693 ttyflush(tp, flags);
694 break;
695 }
696 case TIOCCONS: /* become virtual console */
697 if (*(int *)data) {
698 if (constty && constty != tp &&
699 ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) ==
700 (TS_CARR_ON | TS_ISOPEN))
701 return (EBUSY);
702 #ifndef UCONSOLE
703 if (error = suser(p->p_ucred, &p->p_acflag))
704 return (error);
705 #endif
706 constty = tp;
707 } else if (tp == constty)
708 constty = NULL;
709 break;
710 case TIOCDRAIN: /* wait till output drained */
711 if (error = ttywait(tp))
712 return (error);
713 break;
714 case TIOCGETA: { /* get termios struct */
715 struct termios *t = (struct termios *)data;
716
717 bcopy(&tp->t_termios, t, sizeof(struct termios));
718 break;
719 }
720 case TIOCGETD: /* get line discipline */
721 *(int *)data = tp->t_line;
722 break;
723 case TIOCGWINSZ: /* get window size */
724 *(struct winsize *)data = tp->t_winsize;
725 break;
726 case TIOCGPGRP: /* get pgrp of tty */
727 if (!isctty(p, tp))
728 return (ENOTTY);
729 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
730 break;
731 #ifdef TIOCHPCL
732 case TIOCHPCL: /* hang up on last close */
733 s = spltty();
734 SET(tp->t_cflag, HUPCL);
735 splx(s);
736 break;
737 #endif
738 case TIOCNXCL: /* reset exclusive use of tty */
739 s = spltty();
740 CLR(tp->t_state, TS_XCLUDE);
741 splx(s);
742 break;
743 case TIOCOUTQ: /* output queue size */
744 *(int *)data = tp->t_outq.c_cc;
745 break;
746 case TIOCSETA: /* set termios struct */
747 case TIOCSETAW: /* drain output, set */
748 case TIOCSETAF: { /* drn out, fls in, set */
749 register struct termios *t = (struct termios *)data;
750
751 s = spltty();
752 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
753 if (error = ttywait(tp)) {
754 splx(s);
755 return (error);
756 }
757 if (cmd == TIOCSETAF)
758 ttyflush(tp, FREAD);
759 }
760 if (!ISSET(t->c_cflag, CIGNORE)) {
761 /*
762 * Set device hardware.
763 */
764 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
765 splx(s);
766 return (error);
767 } else {
768 if (!ISSET(tp->t_state, TS_CARR_ON) &&
769 ISSET(tp->t_cflag, CLOCAL) &&
770 !ISSET(t->c_cflag, CLOCAL)) {
771 CLR(tp->t_state, TS_ISOPEN);
772 SET(tp->t_state, TS_WOPEN);
773 ttwakeup(tp);
774 }
775 tp->t_cflag = t->c_cflag;
776 tp->t_ispeed = t->c_ispeed;
777 tp->t_ospeed = t->c_ospeed;
778 if (t->c_ospeed == 0 && tp->t_session &&
779 tp->t_session->s_leader)
780 psignal(tp->t_session->s_leader,
781 SIGHUP);
782 }
783 ttsetwater(tp);
784 }
785 if (cmd != TIOCSETAF) {
786 if (ISSET(t->c_lflag, ICANON) !=
787 ISSET(tp->t_lflag, ICANON))
788 if (ISSET(t->c_lflag, ICANON)) {
789 SET(tp->t_lflag, PENDIN);
790 ttwakeup(tp);
791 } else {
792 struct clist tq;
793
794 catq(&tp->t_rawq, &tp->t_canq);
795 tq = tp->t_rawq;
796 tp->t_rawq = tp->t_canq;
797 tp->t_canq = tq;
798 CLR(tp->t_lflag, PENDIN);
799 }
800 }
801 tp->t_iflag = t->c_iflag;
802 tp->t_oflag = t->c_oflag;
803 /*
804 * Make the EXTPROC bit read only.
805 */
806 if (ISSET(tp->t_lflag, EXTPROC))
807 SET(t->c_lflag, EXTPROC);
808 else
809 CLR(t->c_lflag, EXTPROC);
810 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
811 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
812 splx(s);
813 break;
814 }
815 case TIOCSETD: { /* set line discipline */
816 register int t = *(int *)data;
817 dev_t device = tp->t_dev;
818
819 if ((u_int)t >= nlinesw)
820 return (ENXIO);
821 if (t != tp->t_line) {
822 s = spltty();
823 (*linesw[tp->t_line].l_close)(tp, flag);
824 error = (*linesw[t].l_open)(device, tp);
825 if (error) {
826 (void)(*linesw[tp->t_line].l_open)(device, tp);
827 splx(s);
828 return (error);
829 }
830 tp->t_line = t;
831 splx(s);
832 }
833 break;
834 }
835 case TIOCSTART: /* start output, like ^Q */
836 s = spltty();
837 if (ISSET(tp->t_state, TS_TTSTOP) ||
838 ISSET(tp->t_lflag, FLUSHO)) {
839 CLR(tp->t_lflag, FLUSHO);
840 CLR(tp->t_state, TS_TTSTOP);
841 ttstart(tp);
842 }
843 splx(s);
844 break;
845 case TIOCSTI: /* simulate terminal input */
846 if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
847 return (EPERM);
848 if (p->p_ucred->cr_uid && !isctty(p, tp))
849 return (EACCES);
850 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
851 break;
852 case TIOCSTOP: /* stop output, like ^S */
853 s = spltty();
854 if (!ISSET(tp->t_state, TS_TTSTOP)) {
855 SET(tp->t_state, TS_TTSTOP);
856 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
857 }
858 splx(s);
859 break;
860 case TIOCSCTTY: /* become controlling tty */
861 /* Session ctty vnode pointer set in vnode layer. */
862 if (!SESS_LEADER(p) ||
863 (p->p_session->s_ttyvp || tp->t_session) &&
864 (tp->t_session != p->p_session))
865 return (EPERM);
866 tp->t_session = p->p_session;
867 tp->t_pgrp = p->p_pgrp;
868 p->p_session->s_ttyp = tp;
869 p->p_flag |= P_CONTROLT;
870 break;
871 case TIOCSPGRP: { /* set pgrp of tty */
872 register struct pgrp *pgrp = pgfind(*(int *)data);
873
874 if (!isctty(p, tp))
875 return (ENOTTY);
876 else if (pgrp == NULL || pgrp->pg_session != p->p_session)
877 return (EPERM);
878 tp->t_pgrp = pgrp;
879 break;
880 }
881 case TIOCSTAT: /* get load avg stats */
882 ttyinfo(tp);
883 break;
884 case TIOCSWINSZ: /* set window size */
885 if (bcmp((caddr_t)&tp->t_winsize, data,
886 sizeof (struct winsize))) {
887 tp->t_winsize = *(struct winsize *)data;
888 pgsignal(tp->t_pgrp, SIGWINCH, 1);
889 }
890 break;
891 default:
892 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_SVR4)
893 return (ttcompat(tp, cmd, data, flag, p));
894 #else
895 return (-1);
896 #endif
897 }
898 return (0);
899 }
900
901 int
902 ttselect(device, rw, p)
903 dev_t device;
904 int rw;
905 struct proc *p;
906 {
907 register struct tty *tp;
908 int nread, s;
909
910 tp = (*cdevsw[major(device)].d_tty)(device);
911
912 s = spltty();
913 switch (rw) {
914 case FREAD:
915 nread = ttnread(tp);
916 if (nread > 0 || !ISSET(tp->t_cflag, CLOCAL) &&
917 !ISSET(tp->t_state, TS_CARR_ON))
918 goto win;
919 selrecord(p, &tp->t_rsel);
920 break;
921 case FWRITE:
922 if (tp->t_outq.c_cc <= tp->t_lowat) {
923 win: splx(s);
924 return (1);
925 }
926 selrecord(p, &tp->t_wsel);
927 break;
928 }
929 splx(s);
930 return (0);
931 }
932
933 static int
934 ttnread(tp)
935 struct tty *tp;
936 {
937 int nread;
938
939 if (ISSET(tp->t_lflag, PENDIN))
940 ttypend(tp);
941 nread = tp->t_canq.c_cc;
942 if (!ISSET(tp->t_lflag, ICANON)) {
943 nread += tp->t_rawq.c_cc;
944 if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
945 nread = 0;
946 }
947 return (nread);
948 }
949
950 /*
951 * Wait for output to drain.
952 */
953 int
954 ttywait(tp)
955 register struct tty *tp;
956 {
957 int error, s;
958
959 error = 0;
960 s = spltty();
961 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
962 (ISSET(tp->t_state, TS_CARR_ON) || ISSET(tp->t_cflag, CLOCAL))
963 && tp->t_oproc) {
964 (*tp->t_oproc)(tp);
965 SET(tp->t_state, TS_ASLEEP);
966 if (error = ttysleep(tp,
967 &tp->t_outq, TTOPRI | PCATCH, ttyout, 0))
968 break;
969 }
970 splx(s);
971 return (error);
972 }
973
974 /*
975 * Flush if successfully wait.
976 */
977 int
978 ttywflush(tp)
979 struct tty *tp;
980 {
981 int error;
982
983 if ((error = ttywait(tp)) == 0)
984 ttyflush(tp, FREAD);
985 return (error);
986 }
987
988 /*
989 * Flush tty read and/or write queues, notifying anyone waiting.
990 */
991 void
992 ttyflush(tp, rw)
993 register struct tty *tp;
994 int rw;
995 {
996 register int s;
997
998 s = spltty();
999 if (rw & FREAD) {
1000 FLUSHQ(&tp->t_canq);
1001 FLUSHQ(&tp->t_rawq);
1002 tp->t_rocount = 0;
1003 tp->t_rocol = 0;
1004 CLR(tp->t_state, TS_LOCAL);
1005 ttwakeup(tp);
1006 }
1007 if (rw & FWRITE) {
1008 CLR(tp->t_state, TS_TTSTOP);
1009 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
1010 FLUSHQ(&tp->t_outq);
1011 wakeup((caddr_t)&tp->t_outq);
1012 selwakeup(&tp->t_wsel);
1013 }
1014 splx(s);
1015 }
1016
1017 /*
1018 * Copy in the default termios characters.
1019 */
1020 void
1021 ttychars(tp)
1022 struct tty *tp;
1023 {
1024
1025 bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars));
1026 }
1027
1028 /*
1029 * Send stop character on input overflow.
1030 */
1031 static void
1032 ttyblock(tp)
1033 register struct tty *tp;
1034 {
1035 register int total;
1036
1037 total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
1038 if (tp->t_rawq.c_cc > TTYHOG) {
1039 ttyflush(tp, FREAD | FWRITE);
1040 CLR(tp->t_state, TS_TBLOCK);
1041 }
1042 /*
1043 * Block further input iff: current input > threshold
1044 * AND input is available to user program.
1045 */
1046 if (total >= TTYHOG / 2 &&
1047 !ISSET(tp->t_state, TS_TBLOCK) &&
1048 !ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0) {
1049 if (ISSET(tp->t_iflag, IXOFF) &&
1050 tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1051 putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
1052 SET(tp->t_state, TS_TBLOCK);
1053 ttstart(tp);
1054 }
1055 /* Try to block remote output via hardware flow control. */
1056 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1057 (*tp->t_hwiflow)(tp, 1) != 0)
1058 SET(tp->t_state, TS_TBLOCK);
1059 }
1060 }
1061
1062 void
1063 ttrstrt(tp_arg)
1064 void *tp_arg;
1065 {
1066 struct tty *tp;
1067 int s;
1068
1069 #ifdef DIAGNOSTIC
1070 if (tp_arg == NULL)
1071 panic("ttrstrt");
1072 #endif
1073 tp = tp_arg;
1074 s = spltty();
1075
1076 CLR(tp->t_state, TS_TIMEOUT);
1077 ttstart(tp);
1078
1079 splx(s);
1080 }
1081
1082 int
1083 ttstart(tp)
1084 struct tty *tp;
1085 {
1086
1087 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1088 (*tp->t_oproc)(tp);
1089 return (0);
1090 }
1091
1092 /*
1093 * "close" a line discipline
1094 */
1095 int
1096 ttylclose(tp, flag)
1097 struct tty *tp;
1098 int flag;
1099 {
1100
1101 if (flag & FNONBLOCK)
1102 ttyflush(tp, FREAD | FWRITE);
1103 else
1104 ttywflush(tp);
1105 return (0);
1106 }
1107
1108 /*
1109 * Handle modem control transition on a tty.
1110 * Flag indicates new state of carrier.
1111 * Returns 0 if the line should be turned off, otherwise 1.
1112 */
1113 int
1114 ttymodem(tp, flag)
1115 register struct tty *tp;
1116 int flag;
1117 {
1118
1119 if (!ISSET(tp->t_state, TS_WOPEN) && ISSET(tp->t_cflag, MDMBUF)) {
1120 /*
1121 * MDMBUF: do flow control according to carrier flag
1122 */
1123 if (flag) {
1124 CLR(tp->t_state, TS_TTSTOP);
1125 ttstart(tp);
1126 } else if (!ISSET(tp->t_state, TS_TTSTOP)) {
1127 SET(tp->t_state, TS_TTSTOP);
1128 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
1129 }
1130 } else if (flag == 0) {
1131 /*
1132 * Lost carrier.
1133 */
1134 CLR(tp->t_state, TS_CARR_ON);
1135 if (ISSET(tp->t_state, TS_ISOPEN) &&
1136 !ISSET(tp->t_cflag, CLOCAL)) {
1137 if (tp->t_session && tp->t_session->s_leader)
1138 psignal(tp->t_session->s_leader, SIGHUP);
1139 ttyflush(tp, FREAD | FWRITE);
1140 return (0);
1141 }
1142 } else {
1143 /*
1144 * Carrier now on.
1145 */
1146 SET(tp->t_state, TS_CARR_ON);
1147 ttwakeup(tp);
1148 }
1149 return (1);
1150 }
1151
1152 /*
1153 * Default modem control routine (for other line disciplines).
1154 * Return argument flag, to turn off device on carrier drop.
1155 */
1156 int
1157 nullmodem(tp, flag)
1158 register struct tty *tp;
1159 int flag;
1160 {
1161
1162 if (flag)
1163 SET(tp->t_state, TS_CARR_ON);
1164 else {
1165 CLR(tp->t_state, TS_CARR_ON);
1166 if (!ISSET(tp->t_cflag, CLOCAL)) {
1167 if (tp->t_session && tp->t_session->s_leader)
1168 psignal(tp->t_session->s_leader, SIGHUP);
1169 return (0);
1170 }
1171 }
1172 return (1);
1173 }
1174
1175 /*
1176 * Reinput pending characters after state switch
1177 * call at spltty().
1178 */
1179 void
1180 ttypend(tp)
1181 register struct tty *tp;
1182 {
1183 struct clist tq;
1184 register c;
1185
1186 CLR(tp->t_lflag, PENDIN);
1187 SET(tp->t_state, TS_TYPEN);
1188 tq = tp->t_rawq;
1189 tp->t_rawq.c_cc = 0;
1190 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
1191 while ((c = getc(&tq)) >= 0)
1192 ttyinput(c, tp);
1193 CLR(tp->t_state, TS_TYPEN);
1194 }
1195
1196 /*
1197 * Process a read call on a tty device.
1198 */
1199 int
1200 ttread(tp, uio, flag)
1201 register struct tty *tp;
1202 struct uio *uio;
1203 int flag;
1204 {
1205 register struct clist *qp;
1206 register int c;
1207 register long lflag;
1208 register u_char *cc = tp->t_cc;
1209 register struct proc *p = curproc;
1210 int s, first, error = 0;
1211 struct timeval stime;
1212 int has_stime = 0, last_cc;
1213 long slp = 0;
1214
1215 loop: lflag = tp->t_lflag;
1216 s = spltty();
1217 /*
1218 * take pending input first
1219 */
1220 if (ISSET(lflag, PENDIN))
1221 ttypend(tp);
1222 splx(s);
1223
1224 /*
1225 * Hang process if it's in the background.
1226 */
1227 if (isbackground(p, tp)) {
1228 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
1229 (p->p_sigmask & sigmask(SIGTTIN)) ||
1230 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0)
1231 return (EIO);
1232 pgsignal(p->p_pgrp, SIGTTIN, 1);
1233 if (error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0))
1234 return (error);
1235 goto loop;
1236 }
1237
1238 s = spltty();
1239 if (!ISSET(lflag, ICANON)) {
1240 int m = cc[VMIN];
1241 long t = cc[VTIME];
1242
1243 qp = &tp->t_rawq;
1244 /*
1245 * Check each of the four combinations.
1246 * (m > 0 && t == 0) is the normal read case.
1247 * It should be fairly efficient, so we check that and its
1248 * companion case (m == 0 && t == 0) first.
1249 * For the other two cases, we compute the target sleep time
1250 * into slp.
1251 */
1252 if (t == 0) {
1253 if (qp->c_cc < m)
1254 goto sleep;
1255 goto read;
1256 }
1257 t *= 100000; /* time in us */
1258 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1259 ((t1).tv_usec - (t2).tv_usec))
1260 if (m > 0) {
1261 if (qp->c_cc <= 0)
1262 goto sleep;
1263 if (qp->c_cc >= m)
1264 goto read;
1265 if (!has_stime) {
1266 /* first character, start timer */
1267 has_stime = 1;
1268 stime = time;
1269 slp = t;
1270 } else if (qp->c_cc > last_cc) {
1271 /* got a character, restart timer */
1272 stime = time;
1273 slp = t;
1274 } else {
1275 /* nothing, check expiration */
1276 slp = t - diff(time, stime);
1277 }
1278 } else { /* m == 0 */
1279 if (qp->c_cc > 0)
1280 goto read;
1281 if (!has_stime) {
1282 has_stime = 1;
1283 stime = time;
1284 slp = t;
1285 } else
1286 slp = t - diff(time, stime);
1287 }
1288 last_cc = qp->c_cc;
1289 #undef diff
1290 if (slp > 0) {
1291 /*
1292 * Rounding down may make us wake up just short
1293 * of the target, so we round up.
1294 * The formula is ceiling(slp * hz/1000000).
1295 * 32-bit arithmetic is enough for hz < 169.
1296 *
1297 * Also, use plain wakeup() not ttwakeup().
1298 */
1299 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1300 goto sleep;
1301 }
1302 } else if ((qp = &tp->t_canq)->c_cc <= 0) {
1303 int carrier;
1304
1305 sleep:
1306 /*
1307 * If there is no input, sleep on rawq
1308 * awaiting hardware receipt and notification.
1309 * If we have data, we don't need to check for carrier.
1310 */
1311 carrier = ISSET(tp->t_state, TS_CARR_ON) ||
1312 ISSET(tp->t_cflag, CLOCAL);
1313 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1314 splx(s);
1315 return (0); /* EOF */
1316 }
1317 if (flag & IO_NDELAY) {
1318 splx(s);
1319 return (EWOULDBLOCK);
1320 }
1321 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
1322 carrier ? ttyin : ttopen, slp);
1323 splx(s);
1324 if (error && error != EWOULDBLOCK)
1325 return (error);
1326 goto loop;
1327 }
1328 read:
1329 splx(s);
1330
1331 /*
1332 * Input present, check for input mapping and processing.
1333 */
1334 first = 1;
1335 while ((c = getc(qp)) >= 0) {
1336 /*
1337 * delayed suspend (^Y)
1338 */
1339 if (CCEQ(cc[VDSUSP], c) && ISSET(lflag, ISIG)) {
1340 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1341 if (first) {
1342 if (error = ttysleep(tp,
1343 &lbolt, TTIPRI | PCATCH, ttybg, 0))
1344 break;
1345 goto loop;
1346 }
1347 break;
1348 }
1349 /*
1350 * Interpret EOF only in canonical mode.
1351 */
1352 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1353 break;
1354 /*
1355 * Give user character.
1356 */
1357 error = ureadc(c, uio);
1358 if (error)
1359 break;
1360 if (uio->uio_resid == 0)
1361 break;
1362 /*
1363 * In canonical mode check for a "break character"
1364 * marking the end of a "line of input".
1365 */
1366 if (ISSET(lflag, ICANON) && TTBREAKC(c))
1367 break;
1368 first = 0;
1369 }
1370 /*
1371 * Look to unblock output now that (presumably)
1372 * the input queue has gone down.
1373 */
1374 s = spltty();
1375 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) {
1376 if (ISSET(tp->t_iflag, IXOFF) &&
1377 cc[VSTART] != _POSIX_VDISABLE &&
1378 putc(cc[VSTART], &tp->t_outq) == 0) {
1379 CLR(tp->t_state, TS_TBLOCK);
1380 ttstart(tp);
1381 }
1382 /* Try to unblock remote output via hardware flow control. */
1383 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1384 (*tp->t_hwiflow)(tp, 0) != 0)
1385 CLR(tp->t_state, TS_TBLOCK);
1386 }
1387 splx(s);
1388 return (error);
1389 }
1390
1391 /*
1392 * Check the output queue on tp for space for a kernel message (from uprintf
1393 * or tprintf). Allow some space over the normal hiwater mark so we don't
1394 * lose messages due to normal flow control, but don't let the tty run amok.
1395 * Sleeps here are not interruptible, but we return prematurely if new signals
1396 * arrive.
1397 */
1398 int
1399 ttycheckoutq(tp, wait)
1400 register struct tty *tp;
1401 int wait;
1402 {
1403 int hiwat, s, oldsig;
1404
1405 hiwat = tp->t_hiwat;
1406 s = spltty();
1407 oldsig = wait ? curproc->p_siglist : 0;
1408 if (tp->t_outq.c_cc > hiwat + 200)
1409 while (tp->t_outq.c_cc > hiwat) {
1410 ttstart(tp);
1411 if (wait == 0 || curproc->p_siglist != oldsig) {
1412 splx(s);
1413 return (0);
1414 }
1415 timeout((void (*)__P((void *)))wakeup,
1416 (void *)&tp->t_outq, hz);
1417 SET(tp->t_state, TS_ASLEEP);
1418 tsleep(&tp->t_outq, PZERO - 1, "ttckoutq", 0);
1419 }
1420 splx(s);
1421 return (1);
1422 }
1423
1424 /*
1425 * Process a write call on a tty device.
1426 */
1427 int
1428 ttwrite(tp, uio, flag)
1429 register struct tty *tp;
1430 register struct uio *uio;
1431 int flag;
1432 {
1433 register u_char *cp;
1434 register int cc, ce;
1435 register struct proc *p;
1436 int i, hiwat, cnt, error, s;
1437 u_char obuf[OBUFSIZ];
1438
1439 hiwat = tp->t_hiwat;
1440 cnt = uio->uio_resid;
1441 error = 0;
1442 cc = 0;
1443 loop:
1444 s = spltty();
1445 if (!ISSET(tp->t_state, TS_CARR_ON) &&
1446 !ISSET(tp->t_cflag, CLOCAL)) {
1447 if (ISSET(tp->t_state, TS_ISOPEN)) {
1448 splx(s);
1449 return (EIO);
1450 } else if (flag & IO_NDELAY) {
1451 splx(s);
1452 error = EWOULDBLOCK;
1453 goto out;
1454 } else {
1455 /* Sleep awaiting carrier. */
1456 error = ttysleep(tp,
1457 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
1458 splx(s);
1459 if (error)
1460 goto out;
1461 goto loop;
1462 }
1463 }
1464 splx(s);
1465 /*
1466 * Hang the process if it's in the background.
1467 */
1468 p = curproc;
1469 if (isbackground(p, tp) &&
1470 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1471 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
1472 (p->p_sigmask & sigmask(SIGTTOU)) == 0 &&
1473 p->p_pgrp->pg_jobc) {
1474 pgsignal(p->p_pgrp, SIGTTOU, 1);
1475 if (error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0))
1476 goto out;
1477 goto loop;
1478 }
1479 /*
1480 * Process the user's data in at most OBUFSIZ chunks. Perform any
1481 * output translation. Keep track of high water mark, sleep on
1482 * overflow awaiting device aid in acquiring new space.
1483 */
1484 while (uio->uio_resid > 0 || cc > 0) {
1485 if (ISSET(tp->t_lflag, FLUSHO)) {
1486 uio->uio_resid = 0;
1487 return (0);
1488 }
1489 if (tp->t_outq.c_cc > hiwat)
1490 goto ovhiwat;
1491 /*
1492 * Grab a hunk of data from the user, unless we have some
1493 * leftover from last time.
1494 */
1495 if (cc == 0) {
1496 cc = min(uio->uio_resid, OBUFSIZ);
1497 cp = obuf;
1498 error = uiomove(cp, cc, uio);
1499 if (error) {
1500 cc = 0;
1501 break;
1502 }
1503 }
1504 /*
1505 * If nothing fancy need be done, grab those characters we
1506 * can handle without any of ttyoutput's processing and
1507 * just transfer them to the output q. For those chars
1508 * which require special processing (as indicated by the
1509 * bits in char_type), call ttyoutput. After processing
1510 * a hunk of data, look for FLUSHO so ^O's will take effect
1511 * immediately.
1512 */
1513 while (cc > 0) {
1514 if (!ISSET(tp->t_oflag, OPOST))
1515 ce = cc;
1516 else {
1517 ce = cc - scanc((u_int)cc, cp,
1518 (u_char *)char_type, CCLASSMASK);
1519 /*
1520 * If ce is zero, then we're processing
1521 * a special character through ttyoutput.
1522 */
1523 if (ce == 0) {
1524 tp->t_rocount = 0;
1525 if (ttyoutput(*cp, tp) >= 0) {
1526 #ifdef REAL_CLISTS
1527 /* No Clists, wait a bit. */
1528 ttstart(tp);
1529 if (error = ttysleep(tp, &lbolt,
1530 TTOPRI | PCATCH, ttybuf, 0))
1531 break;
1532 goto loop;
1533 #else
1534 /* out of space */
1535 goto overfull;
1536 #endif
1537 }
1538 cp++;
1539 cc--;
1540 if (ISSET(tp->t_lflag, FLUSHO) ||
1541 tp->t_outq.c_cc > hiwat)
1542 goto ovhiwat;
1543 continue;
1544 }
1545 }
1546 /*
1547 * A bunch of normal characters have been found.
1548 * Transfer them en masse to the output queue and
1549 * continue processing at the top of the loop.
1550 * If there are any further characters in this
1551 * <= OBUFSIZ chunk, the first should be a character
1552 * requiring special handling by ttyoutput.
1553 */
1554 tp->t_rocount = 0;
1555 i = b_to_q(cp, ce, &tp->t_outq);
1556 ce -= i;
1557 tp->t_column += ce;
1558 cp += ce, cc -= ce, tk_nout += ce;
1559 tp->t_outcc += ce;
1560 if (i > 0) {
1561 #ifdef REAL_CLISTS
1562 /* No Clists, wait a bit. */
1563 ttstart(tp);
1564 if (error = ttysleep(tp,
1565 &lbolt, TTOPRI | PCATCH, ttybuf, 0))
1566 break;
1567 goto loop;
1568 #else
1569 /* out of space */
1570 goto overfull;
1571 #endif
1572 }
1573 if (ISSET(tp->t_lflag, FLUSHO) ||
1574 tp->t_outq.c_cc > hiwat)
1575 break;
1576 }
1577 ttstart(tp);
1578 }
1579 out:
1580 /*
1581 * If cc is nonzero, we leave the uio structure inconsistent, as the
1582 * offset and iov pointers have moved forward, but it doesn't matter
1583 * (the call will either return short or restart with a new uio).
1584 */
1585 uio->uio_resid += cc;
1586 return (error);
1587
1588 #ifndef REAL_CLISTS
1589 overfull:
1590 /*
1591 * Since we are using ring buffers, if we can't insert any more into
1592 * the output queue, we can assume the ring is full and that someone
1593 * forgot to set the high water mark correctly. We set it and then
1594 * proceed as normal.
1595 */
1596 hiwat = tp->t_outq.c_cc - 1;
1597 #endif
1598
1599 ovhiwat:
1600 ttstart(tp);
1601 s = spltty();
1602 /*
1603 * This can only occur if FLUSHO is set in t_lflag,
1604 * or if ttstart/oproc is synchronous (or very fast).
1605 */
1606 if (tp->t_outq.c_cc <= hiwat) {
1607 splx(s);
1608 goto loop;
1609 }
1610 if (flag & IO_NDELAY) {
1611 splx(s);
1612 uio->uio_resid += cc;
1613 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1614 }
1615 SET(tp->t_state, TS_ASLEEP);
1616 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1617 splx(s);
1618 if (error)
1619 goto out;
1620 goto loop;
1621 }
1622
1623 /*
1624 * Rubout one character from the rawq of tp
1625 * as cleanly as possible.
1626 */
1627 void
1628 ttyrub(c, tp)
1629 int c;
1630 register struct tty *tp;
1631 {
1632 register u_char *cp;
1633 register int savecol;
1634 int tabc, s;
1635
1636 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1637 return;
1638 CLR(tp->t_lflag, FLUSHO);
1639 if (ISSET(tp->t_lflag, ECHOE)) {
1640 if (tp->t_rocount == 0) {
1641 /*
1642 * Screwed by ttwrite; retype
1643 */
1644 ttyretype(tp);
1645 return;
1646 }
1647 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1648 ttyrubo(tp, 2);
1649 else {
1650 CLR(c, ~TTY_CHARMASK);
1651 switch (CCLASS(c)) {
1652 case ORDINARY:
1653 ttyrubo(tp, 1);
1654 break;
1655 case BACKSPACE:
1656 case CONTROL:
1657 case NEWLINE:
1658 case RETURN:
1659 case VTAB:
1660 if (ISSET(tp->t_lflag, ECHOCTL))
1661 ttyrubo(tp, 2);
1662 break;
1663 case TAB:
1664 if (tp->t_rocount < tp->t_rawq.c_cc) {
1665 ttyretype(tp);
1666 return;
1667 }
1668 s = spltty();
1669 savecol = tp->t_column;
1670 SET(tp->t_state, TS_CNTTB);
1671 SET(tp->t_lflag, FLUSHO);
1672 tp->t_column = tp->t_rocol;
1673 for (cp = firstc(&tp->t_rawq, &tabc); cp;
1674 cp = nextc(&tp->t_rawq, cp, &tabc))
1675 ttyecho(tabc, tp);
1676 CLR(tp->t_lflag, FLUSHO);
1677 CLR(tp->t_state, TS_CNTTB);
1678 splx(s);
1679
1680 /* savecol will now be length of the tab. */
1681 savecol -= tp->t_column;
1682 tp->t_column += savecol;
1683 if (savecol > 8)
1684 savecol = 8; /* overflow screw */
1685 while (--savecol >= 0)
1686 (void)ttyoutput('\b', tp);
1687 break;
1688 default: /* XXX */
1689 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
1690 (void)printf(PANICSTR, c, CCLASS(c));
1691 #ifdef notdef
1692 panic(PANICSTR, c, CCLASS(c));
1693 #endif
1694 }
1695 }
1696 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
1697 if (!ISSET(tp->t_state, TS_ERASE)) {
1698 SET(tp->t_state, TS_ERASE);
1699 (void)ttyoutput('\\', tp);
1700 }
1701 ttyecho(c, tp);
1702 } else
1703 ttyecho(tp->t_cc[VERASE], tp);
1704 --tp->t_rocount;
1705 }
1706
1707 /*
1708 * Back over cnt characters, erasing them.
1709 */
1710 static void
1711 ttyrubo(tp, cnt)
1712 register struct tty *tp;
1713 int cnt;
1714 {
1715
1716 while (cnt-- > 0) {
1717 (void)ttyoutput('\b', tp);
1718 (void)ttyoutput(' ', tp);
1719 (void)ttyoutput('\b', tp);
1720 }
1721 }
1722
1723 /*
1724 * ttyretype --
1725 * Reprint the rawq line. Note, it is assumed that c_cc has already
1726 * been checked.
1727 */
1728 void
1729 ttyretype(tp)
1730 register struct tty *tp;
1731 {
1732 register u_char *cp;
1733 int s, c;
1734
1735 /* Echo the reprint character. */
1736 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1737 ttyecho(tp->t_cc[VREPRINT], tp);
1738
1739 (void)ttyoutput('\n', tp);
1740
1741 s = spltty();
1742 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
1743 ttyecho(c, tp);
1744 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
1745 ttyecho(c, tp);
1746 CLR(tp->t_state, TS_ERASE);
1747 splx(s);
1748
1749 tp->t_rocount = tp->t_rawq.c_cc;
1750 tp->t_rocol = 0;
1751 }
1752
1753 /*
1754 * Echo a typed character to the terminal.
1755 */
1756 static void
1757 ttyecho(c, tp)
1758 register int c;
1759 register struct tty *tp;
1760 {
1761
1762 if (!ISSET(tp->t_state, TS_CNTTB))
1763 CLR(tp->t_lflag, FLUSHO);
1764 if ((!ISSET(tp->t_lflag, ECHO) &&
1765 (!ISSET(tp->t_lflag, ECHONL) || c == '\n')) ||
1766 ISSET(tp->t_lflag, EXTPROC))
1767 return;
1768 if (ISSET(tp->t_lflag, ECHOCTL) &&
1769 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' ||
1770 ISSET(c, TTY_CHARMASK) == 0177)) {
1771 (void)ttyoutput('^', tp);
1772 CLR(c, ~TTY_CHARMASK);
1773 if (c == 0177)
1774 c = '?';
1775 else
1776 c += 'A' - 1;
1777 }
1778 (void)ttyoutput(c, tp);
1779 }
1780
1781 /*
1782 * Wake up any readers on a tty.
1783 */
1784 void
1785 ttwakeup(tp)
1786 register struct tty *tp;
1787 {
1788
1789 selwakeup(&tp->t_rsel);
1790 if (ISSET(tp->t_state, TS_ASYNC))
1791 pgsignal(tp->t_pgrp, SIGIO, 1);
1792 wakeup((caddr_t)&tp->t_rawq);
1793 }
1794
1795 /*
1796 * Look up a code for a specified speed in a conversion table;
1797 * used by drivers to map software speed values to hardware parameters.
1798 */
1799 int
1800 ttspeedtab(speed, table)
1801 int speed;
1802 register struct speedtab *table;
1803 {
1804
1805 for ( ; table->sp_speed != -1; table++)
1806 if (table->sp_speed == speed)
1807 return (table->sp_code);
1808 return (-1);
1809 }
1810
1811 /*
1812 * Set tty hi and low water marks.
1813 *
1814 * Try to arrange the dynamics so there's about one second
1815 * from hi to low water.
1816 */
1817 void
1818 ttsetwater(tp)
1819 struct tty *tp;
1820 {
1821 register int cps, x;
1822
1823 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
1824
1825 cps = tp->t_ospeed / 10;
1826 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
1827 x += cps;
1828 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
1829 tp->t_hiwat = roundup(x, CBSIZE);
1830 #undef CLAMP
1831 }
1832
1833 /*
1834 * Report on state of foreground process group.
1835 */
1836 void
1837 ttyinfo(tp)
1838 register struct tty *tp;
1839 {
1840 register struct proc *p, *pick;
1841 struct timeval utime, stime;
1842 int tmp;
1843
1844 if (ttycheckoutq(tp,0) == 0)
1845 return;
1846
1847 /* Print load average. */
1848 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
1849 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1850
1851 if (tp->t_session == NULL)
1852 ttyprintf(tp, "not a controlling terminal\n");
1853 else if (tp->t_pgrp == NULL)
1854 ttyprintf(tp, "no foreground process group\n");
1855 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
1856 ttyprintf(tp, "empty foreground process group\n");
1857 else {
1858 /* Pick interesting process. */
1859 for (pick = NULL; p != 0; p = p->p_pglist.le_next)
1860 if (proc_compare(pick, p))
1861 pick = p;
1862
1863 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
1864 pick->p_stat == SRUN ? "running" :
1865 pick->p_wmesg ? pick->p_wmesg : "iowait");
1866
1867 calcru(pick, &utime, &stime, NULL);
1868
1869 /* Print user time. */
1870 ttyprintf(tp, "%d.%02du ",
1871 utime.tv_sec, (utime.tv_usec + 5000) / 10000);
1872
1873 /* Print system time. */
1874 ttyprintf(tp, "%d.%02ds ",
1875 stime.tv_sec, (stime.tv_usec + 5000) / 10000);
1876
1877 #define pgtok(a) (((a) * NBPG) / 1024)
1878 /* Print percentage cpu, resident set size. */
1879 tmp = pick->p_pctcpu * 10000 + FSCALE / 2 >> FSHIFT;
1880 ttyprintf(tp, "%d%% %dk\n",
1881 tmp / 100,
1882 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 :
1883 #ifdef pmap_resident_count
1884 pgtok(pmap_resident_count(&pick->p_vmspace->vm_pmap))
1885 #else
1886 pgtok(pick->p_vmspace->vm_rssize)
1887 #endif
1888 );
1889 }
1890 tp->t_rocount = 0; /* so pending input will be retyped if BS */
1891 }
1892
1893 /*
1894 * Returns 1 if p2 is "better" than p1
1895 *
1896 * The algorithm for picking the "interesting" process is thus:
1897 *
1898 * 1) Only foreground processes are eligible - implied.
1899 * 2) Runnable processes are favored over anything else. The runner
1900 * with the highest cpu utilization is picked (p_estcpu). Ties are
1901 * broken by picking the highest pid.
1902 * 3) The sleeper with the shortest sleep time is next. With ties,
1903 * we pick out just "short-term" sleepers (P_SINTR == 0).
1904 * 4) Further ties are broken by picking the highest pid.
1905 */
1906 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
1907 #define TESTAB(a, b) ((a)<<1 | (b))
1908 #define ONLYA 2
1909 #define ONLYB 1
1910 #define BOTH 3
1911
1912 static int
1913 proc_compare(p1, p2)
1914 register struct proc *p1, *p2;
1915 {
1916
1917 if (p1 == NULL)
1918 return (1);
1919 /*
1920 * see if at least one of them is runnable
1921 */
1922 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
1923 case ONLYA:
1924 return (0);
1925 case ONLYB:
1926 return (1);
1927 case BOTH:
1928 /*
1929 * tie - favor one with highest recent cpu utilization
1930 */
1931 if (p2->p_estcpu > p1->p_estcpu)
1932 return (1);
1933 if (p1->p_estcpu > p2->p_estcpu)
1934 return (0);
1935 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1936 }
1937 /*
1938 * weed out zombies
1939 */
1940 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
1941 case ONLYA:
1942 return (1);
1943 case ONLYB:
1944 return (0);
1945 case BOTH:
1946 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1947 }
1948 /*
1949 * pick the one with the smallest sleep time
1950 */
1951 if (p2->p_slptime > p1->p_slptime)
1952 return (0);
1953 if (p1->p_slptime > p2->p_slptime)
1954 return (1);
1955 /*
1956 * favor one sleeping in a non-interruptible sleep
1957 */
1958 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
1959 return (1);
1960 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
1961 return (0);
1962 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1963 }
1964
1965 /*
1966 * Output char to tty; console putchar style.
1967 */
1968 int
1969 tputchar(c, tp)
1970 int c;
1971 struct tty *tp;
1972 {
1973 register int s;
1974
1975 s = spltty();
1976 if (ISSET(tp->t_state,
1977 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) {
1978 splx(s);
1979 return (-1);
1980 }
1981 if (c == '\n')
1982 (void)ttyoutput('\r', tp);
1983 (void)ttyoutput(c, tp);
1984 ttstart(tp);
1985 splx(s);
1986 return (0);
1987 }
1988
1989 /*
1990 * Sleep on chan, returning ERESTART if tty changed while we napped and
1991 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
1992 * the tty is revoked, restarting a pending call will redo validation done
1993 * at the start of the call.
1994 */
1995 int
1996 ttysleep(tp, chan, pri, wmesg, timo)
1997 struct tty *tp;
1998 void *chan;
1999 int pri, timo;
2000 char *wmesg;
2001 {
2002 int error;
2003 short gen;
2004
2005 gen = tp->t_gen;
2006 if (error = tsleep(chan, pri, wmesg, timo))
2007 return (error);
2008 return (tp->t_gen == gen ? 0 : ERESTART);
2009 }
2010
2011 /*
2012 * Allocate a tty structure and its associated buffers.
2013 */
2014 struct tty *
2015 ttymalloc()
2016 {
2017 struct tty *tp;
2018
2019 MALLOC(tp, struct tty *, sizeof(struct tty), M_TTYS, M_WAITOK);
2020 bzero(tp, sizeof *tp);
2021 /* XXX: default to 1024 chars for now */
2022 clalloc(&tp->t_rawq, 1024, 1);
2023 clalloc(&tp->t_canq, 1024, 1);
2024 /* output queue doesn't need quoting */
2025 clalloc(&tp->t_outq, 1024, 0);
2026 return(tp);
2027 }
2028
2029 /*
2030 * Free a tty structure and its buffers.
2031 */
2032 void
2033 ttyfree(tp)
2034 struct tty *tp;
2035 {
2036 clfree(&tp->t_rawq);
2037 clfree(&tp->t_canq);
2038 clfree(&tp->t_outq);
2039 FREE(tp, M_TTYS);
2040 }
2041