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