tty.c revision 1.54 1 /* $NetBSD: tty.c,v 1.54 1994/10/24 09:09:06 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)
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 } else { /* m == 0 */
1277 if (qp->c_cc > 0)
1278 goto read;
1279 if (!has_stime) {
1280 has_stime = 1;
1281 stime = time;
1282 slp = t;
1283 } else
1284 slp = t - diff(time, stime);
1285 }
1286 last_cc = qp->c_cc;
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 goto sleep;
1299 }
1300 } else if ((qp = &tp->t_canq)->c_cc <= 0) {
1301 int carrier;
1302
1303 sleep:
1304 /*
1305 * If there is no input, sleep on rawq
1306 * awaiting hardware receipt and notification.
1307 * If we have data, we don't need to check for carrier.
1308 */
1309 carrier = ISSET(tp->t_state, TS_CARR_ON) ||
1310 ISSET(tp->t_cflag, CLOCAL);
1311 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1312 splx(s);
1313 return (0); /* EOF */
1314 }
1315 if (flag & IO_NDELAY) {
1316 splx(s);
1317 return (EWOULDBLOCK);
1318 }
1319 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
1320 carrier ? ttyin : ttopen, slp);
1321 splx(s);
1322 if (error && error != EWOULDBLOCK)
1323 return (error);
1324 goto loop;
1325 }
1326 read:
1327 splx(s);
1328
1329 /*
1330 * Input present, check for input mapping and processing.
1331 */
1332 first = 1;
1333 while ((c = getc(qp)) >= 0) {
1334 /*
1335 * delayed suspend (^Y)
1336 */
1337 if (CCEQ(cc[VDSUSP], c) && ISSET(lflag, ISIG)) {
1338 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1339 if (first) {
1340 if (error = ttysleep(tp,
1341 &lbolt, TTIPRI | PCATCH, ttybg, 0))
1342 break;
1343 goto loop;
1344 }
1345 break;
1346 }
1347 /*
1348 * Interpret EOF only in canonical mode.
1349 */
1350 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1351 break;
1352 /*
1353 * Give user character.
1354 */
1355 error = ureadc(c, uio);
1356 if (error)
1357 break;
1358 if (uio->uio_resid == 0)
1359 break;
1360 /*
1361 * In canonical mode check for a "break character"
1362 * marking the end of a "line of input".
1363 */
1364 if (ISSET(lflag, ICANON) && TTBREAKC(c))
1365 break;
1366 first = 0;
1367 }
1368 /*
1369 * Look to unblock output now that (presumably)
1370 * the input queue has gone down.
1371 */
1372 s = spltty();
1373 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) {
1374 if (cc[VSTART] != _POSIX_VDISABLE &&
1375 putc(cc[VSTART], &tp->t_outq) == 0) {
1376 CLR(tp->t_state, TS_TBLOCK);
1377 ttstart(tp);
1378 }
1379 /* try to unblock remote output via hardware flow control */
1380 if (tp->t_cflag&CHWFLOW && tp->t_hwiflow &&
1381 (*tp->t_hwiflow)(tp, 0) != 0)
1382 tp->t_state &= ~TS_TBLOCK;
1383 }
1384 splx(s);
1385 return (error);
1386 }
1387
1388 /*
1389 * Check the output queue on tp for space for a kernel message (from uprintf
1390 * or tprintf). Allow some space over the normal hiwater mark so we don't
1391 * lose messages due to normal flow control, but don't let the tty run amok.
1392 * Sleeps here are not interruptible, but we return prematurely if new signals
1393 * arrive.
1394 */
1395 int
1396 ttycheckoutq(tp, wait)
1397 register struct tty *tp;
1398 int wait;
1399 {
1400 int hiwat, s, oldsig;
1401
1402 hiwat = tp->t_hiwat;
1403 s = spltty();
1404 oldsig = wait ? curproc->p_siglist : 0;
1405 if (tp->t_outq.c_cc > hiwat + 200)
1406 while (tp->t_outq.c_cc > hiwat) {
1407 ttstart(tp);
1408 if (wait == 0 || curproc->p_siglist != oldsig) {
1409 splx(s);
1410 return (0);
1411 }
1412 timeout((void (*)__P((void *)))wakeup,
1413 (void *)&tp->t_outq, hz);
1414 SET(tp->t_state, TS_ASLEEP);
1415 tsleep(&tp->t_outq, PZERO - 1, "ttckoutq", 0);
1416 }
1417 splx(s);
1418 return (1);
1419 }
1420
1421 /*
1422 * Process a write call on a tty device.
1423 */
1424 int
1425 ttwrite(tp, uio, flag)
1426 register struct tty *tp;
1427 register struct uio *uio;
1428 int flag;
1429 {
1430 register u_char *cp;
1431 register int cc, ce;
1432 register struct proc *p;
1433 int i, hiwat, cnt, error, s;
1434 u_char obuf[OBUFSIZ];
1435
1436 hiwat = tp->t_hiwat;
1437 cnt = uio->uio_resid;
1438 error = 0;
1439 cc = 0;
1440 loop:
1441 s = spltty();
1442 if (!ISSET(tp->t_state, TS_CARR_ON) &&
1443 !ISSET(tp->t_cflag, CLOCAL)) {
1444 if (ISSET(tp->t_state, TS_ISOPEN)) {
1445 splx(s);
1446 return (EIO);
1447 } else if (flag & IO_NDELAY) {
1448 splx(s);
1449 error = EWOULDBLOCK;
1450 goto out;
1451 } else {
1452 /* Sleep awaiting carrier. */
1453 error = ttysleep(tp,
1454 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
1455 splx(s);
1456 if (error)
1457 goto out;
1458 goto loop;
1459 }
1460 }
1461 splx(s);
1462 /*
1463 * Hang the process if it's in the background.
1464 */
1465 p = curproc;
1466 if (isbackground(p, tp) &&
1467 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1468 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
1469 (p->p_sigmask & sigmask(SIGTTOU)) == 0 &&
1470 p->p_pgrp->pg_jobc) {
1471 pgsignal(p->p_pgrp, SIGTTOU, 1);
1472 if (error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0))
1473 goto out;
1474 goto loop;
1475 }
1476 /*
1477 * Process the user's data in at most OBUFSIZ chunks. Perform any
1478 * output translation. Keep track of high water mark, sleep on
1479 * overflow awaiting device aid in acquiring new space.
1480 */
1481 while (uio->uio_resid > 0 || cc > 0) {
1482 if (ISSET(tp->t_lflag, FLUSHO)) {
1483 uio->uio_resid = 0;
1484 return (0);
1485 }
1486 if (tp->t_outq.c_cc > hiwat)
1487 goto ovhiwat;
1488 /*
1489 * Grab a hunk of data from the user, unless we have some
1490 * leftover from last time.
1491 */
1492 if (cc == 0) {
1493 cc = min(uio->uio_resid, OBUFSIZ);
1494 cp = obuf;
1495 error = uiomove(cp, cc, uio);
1496 if (error) {
1497 cc = 0;
1498 break;
1499 }
1500 }
1501 /*
1502 * If nothing fancy need be done, grab those characters we
1503 * can handle without any of ttyoutput's processing and
1504 * just transfer them to the output q. For those chars
1505 * which require special processing (as indicated by the
1506 * bits in char_type), call ttyoutput. After processing
1507 * a hunk of data, look for FLUSHO so ^O's will take effect
1508 * immediately.
1509 */
1510 while (cc > 0) {
1511 if (!ISSET(tp->t_oflag, OPOST))
1512 ce = cc;
1513 else {
1514 ce = cc - scanc((u_int)cc, cp,
1515 (u_char *)char_type, CCLASSMASK);
1516 /*
1517 * If ce is zero, then we're processing
1518 * a special character through ttyoutput.
1519 */
1520 if (ce == 0) {
1521 tp->t_rocount = 0;
1522 if (ttyoutput(*cp, tp) >= 0) {
1523 #ifdef REAL_CLISTS
1524 /* No Clists, wait a bit. */
1525 ttstart(tp);
1526 if (error = ttysleep(tp, &lbolt,
1527 TTOPRI | PCATCH, ttybuf, 0))
1528 break;
1529 goto loop;
1530 #else
1531 /* out of space */
1532 goto overfull;
1533 #endif
1534 }
1535 cp++;
1536 cc--;
1537 if (ISSET(tp->t_lflag, FLUSHO) ||
1538 tp->t_outq.c_cc > hiwat)
1539 goto ovhiwat;
1540 continue;
1541 }
1542 }
1543 /*
1544 * A bunch of normal characters have been found.
1545 * Transfer them en masse to the output queue and
1546 * continue processing at the top of the loop.
1547 * If there are any further characters in this
1548 * <= OBUFSIZ chunk, the first should be a character
1549 * requiring special handling by ttyoutput.
1550 */
1551 tp->t_rocount = 0;
1552 i = b_to_q(cp, ce, &tp->t_outq);
1553 ce -= i;
1554 tp->t_column += ce;
1555 cp += ce, cc -= ce, tk_nout += ce;
1556 tp->t_outcc += ce;
1557 if (i > 0) {
1558 #ifdef REAL_CLISTS
1559 /* No Clists, wait a bit. */
1560 ttstart(tp);
1561 if (error = ttysleep(tp,
1562 &lbolt, TTOPRI | PCATCH, ttybuf, 0))
1563 break;
1564 goto loop;
1565 #else
1566 /* out of space */
1567 goto overfull;
1568 #endif
1569 }
1570 if (ISSET(tp->t_lflag, FLUSHO) ||
1571 tp->t_outq.c_cc > hiwat)
1572 break;
1573 }
1574 ttstart(tp);
1575 }
1576 out:
1577 /*
1578 * If cc is nonzero, we leave the uio structure inconsistent, as the
1579 * offset and iov pointers have moved forward, but it doesn't matter
1580 * (the call will either return short or restart with a new uio).
1581 */
1582 uio->uio_resid += cc;
1583 return (error);
1584
1585 #ifndef REAL_CLISTS
1586 overfull:
1587 /*
1588 * Since we are using ring buffers, if we can't insert any more into
1589 * the output queue, we can assume the ring is full and that someone
1590 * forgot to set the high water mark correctly. We set it and then
1591 * proceed as normal.
1592 */
1593 hiwat = tp->t_outq.c_cc - 1;
1594 #endif
1595
1596 ovhiwat:
1597 ttstart(tp);
1598 s = spltty();
1599 /*
1600 * This can only occur if FLUSHO is set in t_lflag,
1601 * or if ttstart/oproc is synchronous (or very fast).
1602 */
1603 if (tp->t_outq.c_cc <= hiwat) {
1604 splx(s);
1605 goto loop;
1606 }
1607 if (flag & IO_NDELAY) {
1608 splx(s);
1609 uio->uio_resid += cc;
1610 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1611 }
1612 SET(tp->t_state, TS_ASLEEP);
1613 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1614 splx(s);
1615 if (error)
1616 goto out;
1617 goto loop;
1618 }
1619
1620 /*
1621 * Rubout one character from the rawq of tp
1622 * as cleanly as possible.
1623 */
1624 void
1625 ttyrub(c, tp)
1626 int c;
1627 register struct tty *tp;
1628 {
1629 register u_char *cp;
1630 register int savecol;
1631 int tabc, s;
1632
1633 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1634 return;
1635 CLR(tp->t_lflag, FLUSHO);
1636 if (ISSET(tp->t_lflag, ECHOE)) {
1637 if (tp->t_rocount == 0) {
1638 /*
1639 * Screwed by ttwrite; retype
1640 */
1641 ttyretype(tp);
1642 return;
1643 }
1644 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1645 ttyrubo(tp, 2);
1646 else {
1647 CLR(c, ~TTY_CHARMASK);
1648 switch (CCLASS(c)) {
1649 case ORDINARY:
1650 ttyrubo(tp, 1);
1651 break;
1652 case BACKSPACE:
1653 case CONTROL:
1654 case NEWLINE:
1655 case RETURN:
1656 case VTAB:
1657 if (ISSET(tp->t_lflag, ECHOCTL))
1658 ttyrubo(tp, 2);
1659 break;
1660 case TAB:
1661 if (tp->t_rocount < tp->t_rawq.c_cc) {
1662 ttyretype(tp);
1663 return;
1664 }
1665 s = spltty();
1666 savecol = tp->t_column;
1667 SET(tp->t_state, TS_CNTTB);
1668 SET(tp->t_lflag, FLUSHO);
1669 tp->t_column = tp->t_rocol;
1670 for (cp = firstc(&tp->t_rawq, &tabc); cp;
1671 cp = nextc(&tp->t_rawq, cp, &tabc))
1672 ttyecho(tabc, tp);
1673 CLR(tp->t_lflag, FLUSHO);
1674 CLR(tp->t_state, TS_CNTTB);
1675 splx(s);
1676
1677 /* savecol will now be length of the tab. */
1678 savecol -= tp->t_column;
1679 tp->t_column += savecol;
1680 if (savecol > 8)
1681 savecol = 8; /* overflow screw */
1682 while (--savecol >= 0)
1683 (void)ttyoutput('\b', tp);
1684 break;
1685 default: /* XXX */
1686 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
1687 (void)printf(PANICSTR, c, CCLASS(c));
1688 #ifdef notdef
1689 panic(PANICSTR, c, CCLASS(c));
1690 #endif
1691 }
1692 }
1693 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
1694 if (!ISSET(tp->t_state, TS_ERASE)) {
1695 SET(tp->t_state, TS_ERASE);
1696 (void)ttyoutput('\\', tp);
1697 }
1698 ttyecho(c, tp);
1699 } else
1700 ttyecho(tp->t_cc[VERASE], tp);
1701 --tp->t_rocount;
1702 }
1703
1704 /*
1705 * Back over cnt characters, erasing them.
1706 */
1707 static void
1708 ttyrubo(tp, cnt)
1709 register struct tty *tp;
1710 int cnt;
1711 {
1712
1713 while (cnt-- > 0) {
1714 (void)ttyoutput('\b', tp);
1715 (void)ttyoutput(' ', tp);
1716 (void)ttyoutput('\b', tp);
1717 }
1718 }
1719
1720 /*
1721 * ttyretype --
1722 * Reprint the rawq line. Note, it is assumed that c_cc has already
1723 * been checked.
1724 */
1725 void
1726 ttyretype(tp)
1727 register struct tty *tp;
1728 {
1729 register u_char *cp;
1730 int s, c;
1731
1732 /* Echo the reprint character. */
1733 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1734 ttyecho(tp->t_cc[VREPRINT], tp);
1735
1736 (void)ttyoutput('\n', tp);
1737
1738 s = spltty();
1739 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
1740 ttyecho(c, tp);
1741 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
1742 ttyecho(c, tp);
1743 CLR(tp->t_state, TS_ERASE);
1744 splx(s);
1745
1746 tp->t_rocount = tp->t_rawq.c_cc;
1747 tp->t_rocol = 0;
1748 }
1749
1750 /*
1751 * Echo a typed character to the terminal.
1752 */
1753 static void
1754 ttyecho(c, tp)
1755 register int c;
1756 register struct tty *tp;
1757 {
1758
1759 if (!ISSET(tp->t_state, TS_CNTTB))
1760 CLR(tp->t_lflag, FLUSHO);
1761 if ((!ISSET(tp->t_lflag, ECHO) &&
1762 (!ISSET(tp->t_lflag, ECHONL) || c == '\n')) ||
1763 ISSET(tp->t_lflag, EXTPROC))
1764 return;
1765 if (ISSET(tp->t_lflag, ECHOCTL) &&
1766 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' ||
1767 ISSET(c, TTY_CHARMASK) == 0177)) {
1768 (void)ttyoutput('^', tp);
1769 CLR(c, ~TTY_CHARMASK);
1770 if (c == 0177)
1771 c = '?';
1772 else
1773 c += 'A' - 1;
1774 }
1775 (void)ttyoutput(c, tp);
1776 }
1777
1778 /*
1779 * Wake up any readers on a tty.
1780 */
1781 void
1782 ttwakeup(tp)
1783 register struct tty *tp;
1784 {
1785
1786 selwakeup(&tp->t_rsel);
1787 if (ISSET(tp->t_state, TS_ASYNC))
1788 pgsignal(tp->t_pgrp, SIGIO, 1);
1789 wakeup((caddr_t)&tp->t_rawq);
1790 }
1791
1792 /*
1793 * Look up a code for a specified speed in a conversion table;
1794 * used by drivers to map software speed values to hardware parameters.
1795 */
1796 int
1797 ttspeedtab(speed, table)
1798 int speed;
1799 register struct speedtab *table;
1800 {
1801
1802 for ( ; table->sp_speed != -1; table++)
1803 if (table->sp_speed == speed)
1804 return (table->sp_code);
1805 return (-1);
1806 }
1807
1808 /*
1809 * Set tty hi and low water marks.
1810 *
1811 * Try to arrange the dynamics so there's about one second
1812 * from hi to low water.
1813 */
1814 void
1815 ttsetwater(tp)
1816 struct tty *tp;
1817 {
1818 register int cps, x;
1819
1820 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
1821
1822 cps = tp->t_ospeed / 10;
1823 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
1824 x += cps;
1825 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
1826 tp->t_hiwat = roundup(x, CBSIZE);
1827 #undef CLAMP
1828 }
1829
1830 /*
1831 * Report on state of foreground process group.
1832 */
1833 void
1834 ttyinfo(tp)
1835 register struct tty *tp;
1836 {
1837 register struct proc *p, *pick;
1838 struct timeval utime, stime;
1839 int tmp;
1840
1841 if (ttycheckoutq(tp,0) == 0)
1842 return;
1843
1844 /* Print load average. */
1845 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
1846 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1847
1848 if (tp->t_session == NULL)
1849 ttyprintf(tp, "not a controlling terminal\n");
1850 else if (tp->t_pgrp == NULL)
1851 ttyprintf(tp, "no foreground process group\n");
1852 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
1853 ttyprintf(tp, "empty foreground process group\n");
1854 else {
1855 /* Pick interesting process. */
1856 for (pick = NULL; p != 0; p = p->p_pglist.le_next)
1857 if (proc_compare(pick, p))
1858 pick = p;
1859
1860 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
1861 pick->p_stat == SRUN ? "running" :
1862 pick->p_wmesg ? pick->p_wmesg : "iowait");
1863
1864 calcru(pick, &utime, &stime, NULL);
1865
1866 /* Print user time. */
1867 ttyprintf(tp, "%d.%02du ",
1868 utime.tv_sec, (utime.tv_usec + 5000) / 10000);
1869
1870 /* Print system time. */
1871 ttyprintf(tp, "%d.%02ds ",
1872 stime.tv_sec, (stime.tv_usec + 5000) / 10000);
1873
1874 #define pgtok(a) (((a) * NBPG) / 1024)
1875 /* Print percentage cpu, resident set size. */
1876 tmp = pick->p_pctcpu * 10000 + FSCALE / 2 >> FSHIFT;
1877 ttyprintf(tp, "%d%% %dk\n",
1878 tmp / 100,
1879 pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 :
1880 #ifdef pmap_resident_count
1881 pgtok(pmap_resident_count(&pick->p_vmspace->vm_pmap))
1882 #else
1883 pgtok(pick->p_vmspace->vm_rssize)
1884 #endif
1885 );
1886 }
1887 tp->t_rocount = 0; /* so pending input will be retyped if BS */
1888 }
1889
1890 /*
1891 * Returns 1 if p2 is "better" than p1
1892 *
1893 * The algorithm for picking the "interesting" process is thus:
1894 *
1895 * 1) Only foreground processes are eligible - implied.
1896 * 2) Runnable processes are favored over anything else. The runner
1897 * with the highest cpu utilization is picked (p_estcpu). Ties are
1898 * broken by picking the highest pid.
1899 * 3) The sleeper with the shortest sleep time is next. With ties,
1900 * we pick out just "short-term" sleepers (P_SINTR == 0).
1901 * 4) Further ties are broken by picking the highest pid.
1902 */
1903 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
1904 #define TESTAB(a, b) ((a)<<1 | (b))
1905 #define ONLYA 2
1906 #define ONLYB 1
1907 #define BOTH 3
1908
1909 static int
1910 proc_compare(p1, p2)
1911 register struct proc *p1, *p2;
1912 {
1913
1914 if (p1 == NULL)
1915 return (1);
1916 /*
1917 * see if at least one of them is runnable
1918 */
1919 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
1920 case ONLYA:
1921 return (0);
1922 case ONLYB:
1923 return (1);
1924 case BOTH:
1925 /*
1926 * tie - favor one with highest recent cpu utilization
1927 */
1928 if (p2->p_estcpu > p1->p_estcpu)
1929 return (1);
1930 if (p1->p_estcpu > p2->p_estcpu)
1931 return (0);
1932 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1933 }
1934 /*
1935 * weed out zombies
1936 */
1937 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
1938 case ONLYA:
1939 return (1);
1940 case ONLYB:
1941 return (0);
1942 case BOTH:
1943 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1944 }
1945 /*
1946 * pick the one with the smallest sleep time
1947 */
1948 if (p2->p_slptime > p1->p_slptime)
1949 return (0);
1950 if (p1->p_slptime > p2->p_slptime)
1951 return (1);
1952 /*
1953 * favor one sleeping in a non-interruptible sleep
1954 */
1955 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
1956 return (1);
1957 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
1958 return (0);
1959 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
1960 }
1961
1962 /*
1963 * Output char to tty; console putchar style.
1964 */
1965 int
1966 tputchar(c, tp)
1967 int c;
1968 struct tty *tp;
1969 {
1970 register int s;
1971
1972 s = spltty();
1973 if (ISSET(tp->t_state,
1974 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) {
1975 splx(s);
1976 return (-1);
1977 }
1978 if (c == '\n')
1979 (void)ttyoutput('\r', tp);
1980 (void)ttyoutput(c, tp);
1981 ttstart(tp);
1982 splx(s);
1983 return (0);
1984 }
1985
1986 /*
1987 * Sleep on chan, returning ERESTART if tty changed while we napped and
1988 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
1989 * the tty is revoked, restarting a pending call will redo validation done
1990 * at the start of the call.
1991 */
1992 int
1993 ttysleep(tp, chan, pri, wmesg, timo)
1994 struct tty *tp;
1995 void *chan;
1996 int pri, timo;
1997 char *wmesg;
1998 {
1999 int error;
2000 short gen;
2001
2002 gen = tp->t_gen;
2003 if (error = tsleep(chan, pri, wmesg, timo))
2004 return (error);
2005 return (tp->t_gen == gen ? 0 : ERESTART);
2006 }
2007
2008 /*
2009 * Allocate a tty structure and its associated buffers.
2010 */
2011 struct tty *
2012 ttymalloc()
2013 {
2014 struct tty *tp;
2015
2016 MALLOC(tp, struct tty *, sizeof(struct tty), M_TTYS, M_WAITOK);
2017 bzero(tp, sizeof *tp);
2018 /* XXX: default to 1024 chars for now */
2019 clalloc(&tp->t_rawq, 1024, 1);
2020 clalloc(&tp->t_canq, 1024, 1);
2021 /* output queue doesn't need quoting */
2022 clalloc(&tp->t_outq, 1024, 0);
2023 return(tp);
2024 }
2025
2026 /*
2027 * Free a tty structure and its buffers.
2028 */
2029 void
2030 ttyfree(tp)
2031 struct tty *tp;
2032 {
2033 clfree(&tp->t_rawq);
2034 clfree(&tp->t_canq);
2035 clfree(&tp->t_outq);
2036 FREE(tp, M_TTYS);
2037 }
2038