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