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